aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore63
-rw-r--r--.travis.yml4
-rw-r--r--Android.mk486
-rw-r--r--CHANGES.txt122
-rw-r--r--CONTRIBUTORS.txt9
-rw-r--r--LICENSE15
-rw-r--r--Makefile.am116
-rw-r--r--README.md10
-rwxr-xr-xautogen.sh8
-rw-r--r--configure.ac21
-rw-r--r--editors/proto.vim2
-rw-r--r--java/pom.xml35
-rw-r--r--java/src/main/java/com/google/protobuf/AbstractMessage.java60
-rw-r--r--java/src/main/java/com/google/protobuf/BoundedByteString.java17
-rw-r--r--java/src/main/java/com/google/protobuf/ByteString.java9
-rw-r--r--java/src/main/java/com/google/protobuf/CodedInputStream.java30
-rw-r--r--java/src/main/java/com/google/protobuf/Descriptors.java115
-rw-r--r--java/src/main/java/com/google/protobuf/DynamicMessage.java29
-rw-r--r--java/src/main/java/com/google/protobuf/Extension.java23
-rw-r--r--java/src/main/java/com/google/protobuf/ExtensionLite.java63
-rw-r--r--java/src/main/java/com/google/protobuf/FieldSet.java4
-rw-r--r--java/src/main/java/com/google/protobuf/GeneratedMessage.java364
-rw-r--r--java/src/main/java/com/google/protobuf/GeneratedMessageLite.java378
-rw-r--r--java/src/main/java/com/google/protobuf/Internal.java192
-rw-r--r--java/src/main/java/com/google/protobuf/LazyStringArrayList.java4
-rw-r--r--java/src/main/java/com/google/protobuf/LiteralByteString.java11
-rw-r--r--java/src/main/java/com/google/protobuf/MapEntry.java433
-rw-r--r--java/src/main/java/com/google/protobuf/MapEntryLite.java331
-rw-r--r--java/src/main/java/com/google/protobuf/MapField.java259
-rw-r--r--java/src/main/java/com/google/protobuf/MapFieldLite.java182
-rw-r--r--java/src/main/java/com/google/protobuf/Message.java19
-rw-r--r--java/src/main/java/com/google/protobuf/MessageReflection.java33
-rw-r--r--java/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java26
-rw-r--r--java/src/main/java/com/google/protobuf/RopeByteString.java16
-rw-r--r--java/src/main/java/com/google/protobuf/SingleFieldBuilder.java2
-rw-r--r--java/src/main/java/com/google/protobuf/TextFormat.java25
-rw-r--r--java/src/main/java/com/google/protobuf/UnknownFieldSet.java15
-rw-r--r--java/src/main/java/com/google/protobuf/UnknownFieldSetLite.java297
-rw-r--r--java/src/test/java/com/google/protobuf/BoundedByteStringTest.java19
-rw-r--r--java/src/test/java/com/google/protobuf/CodedInputStreamTest.java10
-rw-r--r--java/src/test/java/com/google/protobuf/DescriptorsTest.java6
-rw-r--r--java/src/test/java/com/google/protobuf/FieldPresenceTest.java363
-rw-r--r--java/src/test/java/com/google/protobuf/GeneratedMessageTest.java141
-rw-r--r--java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java31
-rw-r--r--java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java23
-rw-r--r--java/src/test/java/com/google/protobuf/LiteTest.java13
-rw-r--r--java/src/test/java/com/google/protobuf/LiteralByteStringTest.java16
-rw-r--r--java/src/test/java/com/google/protobuf/MapForProto2LiteTest.java277
-rw-r--r--java/src/test/java/com/google/protobuf/MapForProto2Test.java502
-rw-r--r--java/src/test/java/com/google/protobuf/MapTest.java576
-rw-r--r--java/src/test/java/com/google/protobuf/RopeByteStringTest.java18
-rw-r--r--java/src/test/java/com/google/protobuf/TestBadIdentifiers.java2
-rw-r--r--java/src/test/java/com/google/protobuf/TextFormatTest.java33
-rw-r--r--java/src/test/java/com/google/protobuf/UnknownEnumValueTest.java255
-rw-r--r--java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java316
-rw-r--r--java/src/test/java/com/google/protobuf/field_presence_test.proto93
-rw-r--r--java/src/test/java/com/google/protobuf/lazy_fields_lite.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto17
-rw-r--r--java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto68
-rw-r--r--java/src/test/java/com/google/protobuf/map_for_proto2_test.proto67
-rw-r--r--java/src/test/java/com/google/protobuf/map_test.proto63
-rw-r--r--java/src/test/java/com/google/protobuf/multiple_files_test.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/nested_builders_test.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/nested_extension.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/nested_extension_lite.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/non_nested_extension.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/outer_class_name_test.proto2
-rw-r--r--java/src/test/java/com/google/protobuf/outer_class_name_test2.proto2
-rw-r--r--java/src/test/java/com/google/protobuf/outer_class_name_test3.proto2
-rw-r--r--java/src/test/java/com/google/protobuf/test_bad_identifiers.proto24
-rw-r--r--java/src/test/java/com/google/protobuf/test_check_utf8.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/test_check_utf8_size.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/test_custom_options.proto1
-rw-r--r--java/src/test/java/com/google/protobuf/test_extra_interfaces.proto1
-rw-r--r--javanano/README.txt354
-rw-r--r--javanano/pom.xml164
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java641
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java879
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java187
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/Extension.java722
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/FieldArray.java273
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/FieldData.java190
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/InternalNano.java333
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java93
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/MessageNano.java190
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java257
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/UnknownFieldData.java84
-rw-r--r--javanano/src/main/java/com/google/protobuf/nano/WireFormatNano.java124
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/NanoTest.java3797
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto118
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.proto48
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto48
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto28
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto33
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto29
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto34
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto34
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_has_nano.proto82
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_import_nano.proto48
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto41
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto63
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_nano.proto186
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto49
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto116
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto47
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto95
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto54
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_single_nano.proto38
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto43
-rwxr-xr-xpost_process_dist.sh24
-rwxr-xr-xpython/google/protobuf/descriptor.py5
-rw-r--r--python/google/protobuf/descriptor_database.py4
-rw-r--r--python/google/protobuf/descriptor_pool.py2
-rw-r--r--python/google/protobuf/internal/descriptor_database_test.py2
-rw-r--r--python/google/protobuf/internal/descriptor_pool_test.py29
-rw-r--r--python/google/protobuf/internal/descriptor_pool_test1.proto2
-rw-r--r--python/google/protobuf/internal/descriptor_pool_test2.proto2
-rwxr-xr-xpython/google/protobuf/internal/descriptor_test.py10
-rw-r--r--python/google/protobuf/internal/factory_test1.proto1
-rw-r--r--python/google/protobuf/internal/factory_test2.proto7
-rwxr-xr-xpython/google/protobuf/internal/generator_test.py4
-rw-r--r--python/google/protobuf/internal/import_test_package/BUILD27
-rw-r--r--python/google/protobuf/internal/import_test_package/__init__.py33
-rw-r--r--python/google/protobuf/internal/import_test_package/inner.proto37
-rw-r--r--python/google/protobuf/internal/import_test_package/outer.proto39
-rwxr-xr-xpython/google/protobuf/internal/message_test.py40
-rw-r--r--python/google/protobuf/internal/missing_enum_values.proto2
-rw-r--r--python/google/protobuf/internal/more_extensions.proto1
-rw-r--r--python/google/protobuf/internal/more_extensions_dynamic.proto1
-rw-r--r--python/google/protobuf/internal/more_messages.proto1
-rw-r--r--python/google/protobuf/internal/proto_builder_test.py77
-rwxr-xr-xpython/google/protobuf/internal/python_message.py24
-rwxr-xr-xpython/google/protobuf/internal/reflection_test.py59
-rwxr-xr-xpython/google/protobuf/internal/service_reflection_test.py2
-rw-r--r--python/google/protobuf/internal/test_bad_identifiers.proto1
-rwxr-xr-xpython/google/protobuf/internal/text_format_test.py21
-rwxr-xr-xpython/google/protobuf/internal/type_checkers.py9
-rwxr-xr-xpython/google/protobuf/internal/unknown_fields_test.py9
-rwxr-xr-xpython/google/protobuf/message.py15
-rw-r--r--python/google/protobuf/proto_builder.py98
-rw-r--r--python/google/protobuf/pyext/cpp_message.py6
-rw-r--r--python/google/protobuf/pyext/descriptor.cc226
-rw-r--r--python/google/protobuf/pyext/descriptor.h70
-rw-r--r--python/google/protobuf/pyext/extension_dict.cc110
-rw-r--r--python/google/protobuf/pyext/extension_dict.h22
-rw-r--r--python/google/protobuf/pyext/message.cc495
-rw-r--r--python/google/protobuf/pyext/message.h47
-rw-r--r--python/google/protobuf/pyext/proto2_api_test.proto2
-rw-r--r--python/google/protobuf/pyext/python.proto2
-rw-r--r--python/google/protobuf/pyext/repeated_composite_container.cc135
-rw-r--r--python/google/protobuf/pyext/repeated_composite_container.h15
-rw-r--r--python/google/protobuf/pyext/repeated_scalar_container.cc102
-rw-r--r--python/google/protobuf/pyext/repeated_scalar_container.h12
-rw-r--r--python/google/protobuf/pyext/scoped_pyobject_ptr.h4
-rwxr-xr-xpython/google/protobuf/text_format.py46
-rwxr-xr-xpython/mox.py2
-rwxr-xr-xpython/setup.py20
-rw-r--r--ruby/README.md34
-rw-r--r--ruby/Rakefile21
-rw-r--r--ruby/ext/google/protobuf_c/defs.c1286
-rw-r--r--ruby/ext/google/protobuf_c/encode_decode.c755
-rw-r--r--ruby/ext/google/protobuf_c/extconf.rb10
-rw-r--r--ruby/ext/google/protobuf_c/message.c463
-rw-r--r--ruby/ext/google/protobuf_c/protobuf.c102
-rw-r--r--ruby/ext/google/protobuf_c/protobuf.h396
-rw-r--r--ruby/ext/google/protobuf_c/repeated_field.c597
-rw-r--r--ruby/ext/google/protobuf_c/storage.c577
-rw-r--r--ruby/ext/google/protobuf_c/upb.c10078
-rw-r--r--ruby/ext/google/protobuf_c/upb.h7439
-rw-r--r--ruby/google-protobuf.gemspec22
-rw-r--r--ruby/lib/google/protobuf.rb31
-rw-r--r--ruby/tests/basic.rb633
-rw-r--r--ruby/tests/stress.rb38
-rw-r--r--src/Makefile.am144
-rw-r--r--src/google/protobuf/arena.cc258
-rw-r--r--src/google/protobuf/arena.h484
-rw-r--r--src/google/protobuf/arena_nc.cc45
-rw-r--r--src/google/protobuf/arena_nc_test.py59
-rw-r--r--src/google/protobuf/arena_unittest.cc1009
-rw-r--r--src/google/protobuf/arenastring.cc53
-rwxr-xr-xsrc/google/protobuf/arenastring.h315
-rw-r--r--src/google/protobuf/arenastring_unittest.cc112
-rw-r--r--src/google/protobuf/compiler/command_line_interface.cc9
-rw-r--r--src/google/protobuf/compiler/command_line_interface.h2
-rw-r--r--src/google/protobuf/compiler/command_line_interface_unittest.cc23
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_enum.cc9
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_enum_field.cc118
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_extension.h2
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_field.cc37
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_field.h26
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_file.cc42
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_file.h11
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_generator.cc7
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_helpers.cc40
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_helpers.h41
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_map_field.cc255
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_map_field.h75
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_message.cc1204
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_message.h15
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_message_field.cc383
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc130
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_primitive_field.cc8
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_string_field.cc546
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_string_field.h1
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto20
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_unittest.cc23
-rw-r--r--src/google/protobuf/compiler/importer.cc9
-rw-r--r--src/google/protobuf/compiler/importer_unittest.cc132
-rw-r--r--src/google/protobuf/compiler/java/java_context.h5
-rw-r--r--src/google/protobuf/compiler/java/java_enum.cc32
-rw-r--r--src/google/protobuf/compiler/java/java_enum_field.cc354
-rw-r--r--src/google/protobuf/compiler/java/java_enum_field.h2
-rw-r--r--src/google/protobuf/compiler/java/java_field.cc20
-rw-r--r--src/google/protobuf/compiler/java/java_field.h5
-rw-r--r--src/google/protobuf/compiler/java/java_file.cc41
-rw-r--r--src/google/protobuf/compiler/java/java_file.h11
-rw-r--r--src/google/protobuf/compiler/java/java_generator.cc11
-rw-r--r--src/google/protobuf/compiler/java/java_helpers.cc20
-rw-r--r--src/google/protobuf/compiler/java/java_helpers.h28
-rw-r--r--src/google/protobuf/compiler/java/java_map_field.cc455
-rw-r--r--src/google/protobuf/compiler/java/java_map_field.h82
-rw-r--r--src/google/protobuf/compiler/java/java_message.cc661
-rw-r--r--src/google/protobuf/compiler/java/java_message.h1
-rw-r--r--src/google/protobuf/compiler/java/java_message_field.cc54
-rw-r--r--src/google/protobuf/compiler/java/java_plugin_unittest.cc5
-rw-r--r--src/google/protobuf/compiler/java/java_primitive_field.cc20
-rw-r--r--src/google/protobuf/compiler/java/java_shared_code_generator.cc10
-rw-r--r--src/google/protobuf/compiler/java/java_shared_code_generator.h6
-rw-r--r--src/google/protobuf/compiler/java/java_string_field.cc27
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum.cc111
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum.h87
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum_field.cc520
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_enum_field.h125
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_extension.cc150
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_extension.h74
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_field.cc143
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_field.h119
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_file.cc263
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_file.h94
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_generator.cc219
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_generator.h72
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_helpers.cc566
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_helpers.h189
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message.cc555
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message.h95
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message_field.cc259
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_message_field.h96
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_params.h240
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_primitive_field.cc910
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_primitive_field.h126
-rw-r--r--src/google/protobuf/compiler/main.cc13
-rw-r--r--src/google/protobuf/compiler/mock_code_generator.cc9
-rw-r--r--src/google/protobuf/compiler/parser.cc159
-rw-r--r--src/google/protobuf/compiler/parser.h24
-rw-r--r--src/google/protobuf/compiler/parser_unittest.cc324
-rw-r--r--src/google/protobuf/compiler/plugin.pb.cc330
-rw-r--r--src/google/protobuf/compiler/plugin.pb.h294
-rw-r--r--src/google/protobuf/compiler/plugin.proto1
-rw-r--r--src/google/protobuf/compiler/python/python_generator.cc98
-rw-r--r--src/google/protobuf/compiler/python/python_generator.h4
-rw-r--r--src/google/protobuf/compiler/python/python_plugin_unittest.cc5
-rw-r--r--src/google/protobuf/compiler/ruby/ruby_generator.cc321
-rw-r--r--src/google/protobuf/compiler/ruby/ruby_generator.h57
-rw-r--r--src/google/protobuf/descriptor.cc745
-rw-r--r--src/google/protobuf/descriptor.h145
-rw-r--r--src/google/protobuf/descriptor.pb.cc2568
-rw-r--r--src/google/protobuf/descriptor.pb.h1798
-rw-r--r--src/google/protobuf/descriptor.proto61
-rw-r--r--src/google/protobuf/descriptor_pb2_test.py6
-rw-r--r--src/google/protobuf/descriptor_unittest.cc627
-rw-r--r--src/google/protobuf/drop_unknown_fields_test.cc88
-rw-r--r--src/google/protobuf/dynamic_message.cc147
-rw-r--r--src/google/protobuf/dynamic_message.h5
-rw-r--r--src/google/protobuf/dynamic_message_unittest.cc55
-rw-r--r--src/google/protobuf/extension_set.cc404
-rw-r--r--src/google/protobuf/extension_set.h18
-rw-r--r--src/google/protobuf/extension_set_heavy.cc17
-rw-r--r--src/google/protobuf/extension_set_unittest.cc112
-rw-r--r--src/google/protobuf/generated_message_reflection.cc649
-rw-r--r--src/google/protobuf/generated_message_reflection.h104
-rw-r--r--src/google/protobuf/generated_message_util.cc1
-rw-r--r--src/google/protobuf/io/coded_stream.cc15
-rw-r--r--src/google/protobuf/io/coded_stream.h53
-rw-r--r--src/google/protobuf/io/coded_stream_inl.h11
-rw-r--r--src/google/protobuf/io/coded_stream_unittest.cc1
-rw-r--r--src/google/protobuf/io/gzip_stream.cc11
-rw-r--r--src/google/protobuf/io/gzip_stream.h1
-rw-r--r--src/google/protobuf/io/printer.cc13
-rw-r--r--src/google/protobuf/io/printer.h5
-rw-r--r--src/google/protobuf/io/tokenizer.h2
-rw-r--r--src/google/protobuf/io/zero_copy_stream_impl_lite.cc1
-rw-r--r--src/google/protobuf/io/zero_copy_stream_impl_lite.h31
-rw-r--r--src/google/protobuf/io/zero_copy_stream_unittest.cc37
-rw-r--r--src/google/protobuf/map.h311
-rw-r--r--src/google/protobuf/map_entry.h454
-rw-r--r--src/google/protobuf/map_field.cc140
-rw-r--r--src/google/protobuf/map_field.h227
-rw-r--r--src/google/protobuf/map_field_inl.h278
-rw-r--r--src/google/protobuf/map_field_test.cc435
-rw-r--r--src/google/protobuf/map_lite_unittest.proto39
-rw-r--r--src/google/protobuf/map_proto2_unittest.proto68
-rw-r--r--src/google/protobuf/map_test.cc2252
-rw-r--r--src/google/protobuf/map_test_util.cc1479
-rw-r--r--src/google/protobuf/map_test_util.h149
-rw-r--r--src/google/protobuf/map_type_handler.h486
-rw-r--r--src/google/protobuf/map_unittest.proto82
-rw-r--r--src/google/protobuf/message.cc93
-rw-r--r--src/google/protobuf/message.h200
-rw-r--r--src/google/protobuf/message_lite.cc22
-rw-r--r--src/google/protobuf/message_lite.h23
-rw-r--r--src/google/protobuf/message_unittest.cc19
-rw-r--r--src/google/protobuf/metadata.h164
-rw-r--r--src/google/protobuf/new_delete_capture.cc121
-rw-r--r--src/google/protobuf/new_delete_capture.h175
-rw-r--r--src/google/protobuf/no_field_presence_test.cc537
-rw-r--r--src/google/protobuf/preserve_unknown_enum_test.cc232
-rw-r--r--src/google/protobuf/proto3_arena_unittest.cc185
-rw-r--r--src/google/protobuf/proto_cast.h58
-rw-r--r--src/google/protobuf/proto_cast_test.cc60
-rw-r--r--src/google/protobuf/reflection.h306
-rw-r--r--src/google/protobuf/reflection_internal.h378
-rw-r--r--src/google/protobuf/repeated_field.cc72
-rw-r--r--src/google/protobuf/repeated_field.h1120
-rw-r--r--src/google/protobuf/repeated_field_reflection.h337
-rw-r--r--src/google/protobuf/repeated_field_reflection_unittest.cc519
-rw-r--r--src/google/protobuf/repeated_field_unittest.cc69
-rw-r--r--src/google/protobuf/stubs/atomic_sequence_num.h54
-rw-r--r--src/google/protobuf/stubs/atomicops.h14
-rw-r--r--src/google/protobuf/stubs/atomicops_internals_aix.h440
-rw-r--r--src/google/protobuf/stubs/casts.h123
-rw-r--r--src/google/protobuf/stubs/common.cc1
-rw-r--r--src/google/protobuf/stubs/common.h162
-rw-r--r--src/google/protobuf/stubs/common_unittest.cc15
-rw-r--r--src/google/protobuf/stubs/fastmem.h152
-rw-r--r--src/google/protobuf/stubs/map_util.h2
-rw-r--r--src/google/protobuf/stubs/platform_macros.h5
-rw-r--r--src/google/protobuf/stubs/singleton.h64
-rw-r--r--src/google/protobuf/stubs/strutil.cc56
-rw-r--r--src/google/protobuf/stubs/strutil.h32
-rw-r--r--src/google/protobuf/stubs/template_util.h2
-rw-r--r--src/google/protobuf/stubs/type_traits.h46
-rw-r--r--src/google/protobuf/stubs/type_traits_unittest.cc5
-rw-r--r--src/google/protobuf/testing/file.cc2
-rw-r--r--src/google/protobuf/text_format.cc60
-rw-r--r--src/google/protobuf/text_format.h5
-rw-r--r--src/google/protobuf/text_format_unittest.cc12
-rw-r--r--src/google/protobuf/unittest.proto13
-rw-r--r--src/google/protobuf/unittest_arena.proto46
-rw-r--r--src/google/protobuf/unittest_custom_options.proto3
-rw-r--r--src/google/protobuf/unittest_drop_unknown_fields.proto55
-rw-r--r--src/google/protobuf/unittest_embed_optimize_for.proto1
-rw-r--r--src/google/protobuf/unittest_empty.proto1
-rw-r--r--src/google/protobuf/unittest_enormous_descriptor.proto1
-rw-r--r--src/google/protobuf/unittest_import.proto4
-rw-r--r--src/google/protobuf/unittest_import_lite.proto1
-rw-r--r--src/google/protobuf/unittest_import_public.proto1
-rw-r--r--src/google/protobuf/unittest_import_public_lite.proto1
-rw-r--r--src/google/protobuf/unittest_lite.proto1
-rw-r--r--src/google/protobuf/unittest_lite_imports_nonlite.proto1
-rw-r--r--src/google/protobuf/unittest_mset.proto2
-rw-r--r--src/google/protobuf/unittest_no_arena.proto200
-rw-r--r--src/google/protobuf/unittest_no_arena_import.proto37
-rw-r--r--src/google/protobuf/unittest_no_field_presence.proto138
-rw-r--r--src/google/protobuf/unittest_no_generic_services.proto1
-rw-r--r--src/google/protobuf/unittest_optimize_for.proto1
-rw-r--r--src/google/protobuf/unittest_preserve_unknown_enum.proto66
-rw-r--r--src/google/protobuf/unittest_proto3_arena.proto144
-rw-r--r--src/google/protobuf/unknown_enum_impl.h132
-rw-r--r--src/google/protobuf/unknown_enum_test.proto61
-rw-r--r--src/google/protobuf/unknown_field_set.cc112
-rw-r--r--src/google/protobuf/unknown_field_set.h40
-rw-r--r--src/google/protobuf/unknown_field_set_unittest.cc7
-rw-r--r--src/google/protobuf/wire_format.cc45
-rw-r--r--src/google/protobuf/wire_format_lite.cc42
-rw-r--r--src/google/protobuf/wire_format_lite.h28
-rw-r--r--src/google/protobuf/wire_format_lite_inl.h26
-rw-r--r--src/google/protobuf/wire_format_unittest.cc6
-rwxr-xr-xvsprojects/extract_includes.bat78
-rw-r--r--vsprojects/libprotobuf-lite.vcproj34
-rw-r--r--vsprojects/libprotobuf.vcproj90
-rw-r--r--vsprojects/libprotoc.vcproj88
-rw-r--r--vsprojects/lite-test.vcproj4
-rw-r--r--vsprojects/protoc.vcproj4
-rw-r--r--vsprojects/readme.txt7
-rwxr-xr-xvsprojects/test_plugin.vcproj4
-rw-r--r--vsprojects/tests.vcproj540
387 files changed, 71166 insertions, 5808 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..3938196c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,63 @@
+# autogen.sh-generated files
+Makefile.in
+src/Makefile.in
+config.guess
+config.h.in
+config.sub
+configure
+depcomp
+install-sh
+ltmain.sh
+missing
+
+aclocal.m4
+m4/libtool.m4
+m4/ltoptions.m4
+m4/ltsugar.m4
+m4/ltversion.m4
+m4/lt~obsolete.m4
+
+# downloaded files
+gtest
+
+# in-tree configure-generated files
+Makefile
+src/Makefile
+config.h
+config.log
+config.status
+
+libtool
+protobuf-lite.pc
+protobuf.pc
+.deps
+stamp-h1
+
+# in-tree build products
+*.o
+*.lo
+*.la
+src/.libs
+
+.dirstamp
+
+map*unittest.pb.*
+unittest*.pb.*
+cpp_test*.pb.*
+
+*.pyc
+*.egg-info
+*_pb2.py
+python/.eggs/
+python/build/
+python/google/protobuf/compiler/
+
+src/protoc
+src/unittest_proto_middleman
+
+# Generated test scaffolding
+src/protobuf*-test
+src/test_plugin
+src/testzip.*
+src/zcg*zip
+ar-lib
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 00000000..93013b8b
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: cpp
+script: ./autogen.sh && ./configure && make distcheck
+notifications:
+ email: false
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 00000000..18bdd091
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,486 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+LOCAL_PATH := $(call my-dir)
+
+IGNORED_WARNINGS := -Wno-sign-compare -Wno-unused-parameter -Wno-sign-promo
+
+CC_LITE_SRC_FILES := \
+ src/google/protobuf/stubs/common.cc \
+ src/google/protobuf/stubs/once.cc \
+ src/google/protobuf/stubs/hash.cc \
+ src/google/protobuf/stubs/hash.h \
+ src/google/protobuf/stubs/map-util.h \
+ src/google/protobuf/stubs/stl_util-inl.h \
+ src/google/protobuf/extension_set.cc \
+ src/google/protobuf/generated_message_util.cc \
+ src/google/protobuf/message_lite.cc \
+ src/google/protobuf/repeated_field.cc \
+ src/google/protobuf/wire_format_lite.cc \
+ src/google/protobuf/io/coded_stream.cc \
+ src/google/protobuf/io/coded_stream_inl.h \
+ src/google/protobuf/io/zero_copy_stream.cc \
+ src/google/protobuf/io/zero_copy_stream_impl_lite.cc
+
+JAVA_LITE_SRC_FILES := \
+ java/src/main/java/com/google/protobuf/UninitializedMessageException.java \
+ java/src/main/java/com/google/protobuf/MessageLite.java \
+ java/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java \
+ java/src/main/java/com/google/protobuf/CodedOutputStream.java \
+ java/src/main/java/com/google/protobuf/ByteString.java \
+ java/src/main/java/com/google/protobuf/CodedInputStream.java \
+ java/src/main/java/com/google/protobuf/ExtensionRegistryLite.java \
+ java/src/main/java/com/google/protobuf/AbstractMessageLite.java \
+ java/src/main/java/com/google/protobuf/FieldSet.java \
+ java/src/main/java/com/google/protobuf/Internal.java \
+ java/src/main/java/com/google/protobuf/WireFormat.java \
+ java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
+
+COMPILER_SRC_FILES := \
+ src/google/protobuf/descriptor.cc \
+ src/google/protobuf/descriptor.pb.cc \
+ src/google/protobuf/descriptor_database.cc \
+ src/google/protobuf/dynamic_message.cc \
+ src/google/protobuf/extension_set.cc \
+ src/google/protobuf/extension_set_heavy.cc \
+ src/google/protobuf/generated_message_reflection.cc \
+ src/google/protobuf/generated_message_util.cc \
+ src/google/protobuf/message.cc \
+ src/google/protobuf/message_lite.cc \
+ src/google/protobuf/reflection_ops.cc \
+ src/google/protobuf/repeated_field.cc \
+ src/google/protobuf/service.cc \
+ src/google/protobuf/text_format.cc \
+ src/google/protobuf/unknown_field_set.cc \
+ src/google/protobuf/wire_format.cc \
+ src/google/protobuf/wire_format_lite.cc \
+ src/google/protobuf/compiler/code_generator.cc \
+ src/google/protobuf/compiler/command_line_interface.cc \
+ src/google/protobuf/compiler/importer.cc \
+ src/google/protobuf/compiler/main.cc \
+ src/google/protobuf/compiler/parser.cc \
+ src/google/protobuf/compiler/plugin.cc \
+ src/google/protobuf/compiler/plugin.pb.cc \
+ src/google/protobuf/compiler/subprocess.cc \
+ src/google/protobuf/compiler/zip_writer.cc \
+ src/google/protobuf/compiler/cpp/cpp_enum.cc \
+ src/google/protobuf/compiler/cpp/cpp_enum_field.cc \
+ src/google/protobuf/compiler/cpp/cpp_extension.cc \
+ src/google/protobuf/compiler/cpp/cpp_field.cc \
+ src/google/protobuf/compiler/cpp/cpp_file.cc \
+ src/google/protobuf/compiler/cpp/cpp_generator.cc \
+ src/google/protobuf/compiler/cpp/cpp_helpers.cc \
+ src/google/protobuf/compiler/cpp/cpp_message.cc \
+ src/google/protobuf/compiler/cpp/cpp_message_field.cc \
+ src/google/protobuf/compiler/cpp/cpp_primitive_field.cc \
+ src/google/protobuf/compiler/cpp/cpp_service.cc \
+ src/google/protobuf/compiler/cpp/cpp_string_field.cc \
+ src/google/protobuf/compiler/java/java_enum.cc \
+ src/google/protobuf/compiler/java/java_enum_field.cc \
+ src/google/protobuf/compiler/java/java_extension.cc \
+ src/google/protobuf/compiler/java/java_field.cc \
+ src/google/protobuf/compiler/java/java_file.cc \
+ src/google/protobuf/compiler/java/java_generator.cc \
+ src/google/protobuf/compiler/java/java_helpers.cc \
+ src/google/protobuf/compiler/java/java_message.cc \
+ src/google/protobuf/compiler/java/java_message_field.cc \
+ src/google/protobuf/compiler/java/java_primitive_field.cc \
+ src/google/protobuf/compiler/java/java_service.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_enum.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_field.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_file.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_generator.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_helpers.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_message.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_message_field.cc \
+ src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc \
+ src/google/protobuf/compiler/javanano/javanano_enum.cc \
+ src/google/protobuf/compiler/javanano/javanano_enum_field.cc \
+ src/google/protobuf/compiler/javanano/javanano_extension.cc \
+ src/google/protobuf/compiler/javanano/javanano_field.cc \
+ src/google/protobuf/compiler/javanano/javanano_file.cc \
+ src/google/protobuf/compiler/javanano/javanano_generator.cc \
+ src/google/protobuf/compiler/javanano/javanano_helpers.cc \
+ src/google/protobuf/compiler/javanano/javanano_message.cc \
+ src/google/protobuf/compiler/javanano/javanano_message_field.cc \
+ src/google/protobuf/compiler/javanano/javanano_primitive_field.cc \
+ src/google/protobuf/compiler/python/python_generator.cc \
+ src/google/protobuf/io/coded_stream.cc \
+ src/google/protobuf/io/gzip_stream.cc \
+ src/google/protobuf/io/printer.cc \
+ src/google/protobuf/io/tokenizer.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/stubs/common.cc \
+ src/google/protobuf/stubs/hash.cc \
+ src/google/protobuf/stubs/once.cc \
+ src/google/protobuf/stubs/structurally_valid.cc \
+ src/google/protobuf/stubs/strutil.cc \
+ src/google/protobuf/stubs/substitute.cc
+
+# Java nano library (for device-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-java-nano
+LOCAL_MODULE_TAGS := optional
+LOCAL_SDK_VERSION := 8
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/nano)
+LOCAL_SRC_FILES += $(call all-java-files-under, java/src/device/main/java/com/google/protobuf/nano)
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# Java nano library (for host-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := host-libprotobuf-java-nano
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/nano)
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# Java micro library (for device-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-java-micro
+LOCAL_MODULE_TAGS := optional
+LOCAL_SDK_VERSION := 8
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/micro)
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# Java micro library (for host-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := host-libprotobuf-java-micro
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/main/java/com/google/protobuf/micro)
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# Java lite library (for device-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-java-lite
+LOCAL_MODULE_TAGS := optional
+LOCAL_SDK_VERSION := 8
+
+LOCAL_SRC_FILES := $(JAVA_LITE_SRC_FILES)
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# Java lite library (for host-side users)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := host-libprotobuf-java-lite
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(JAVA_LITE_SRC_FILES)
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# C++ lite library
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-cpp-lite
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CPP_EXTENSION := .cc
+
+LOCAL_SRC_FILES := $(CC_LITE_SRC_FILES)
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ $(LOCAL_PATH)/src
+
+# Define the header files to be copied
+#LOCAL_COPY_HEADERS := \
+# src/google/protobuf/stubs/once.h \
+# src/google/protobuf/stubs/common.h \
+# src/google/protobuf/io/coded_stream.h \
+# src/google/protobuf/generated_message_util.h \
+# src/google/protobuf/repeated_field.h \
+# src/google/protobuf/extension_set.h \
+# src/google/protobuf/wire_format_lite_inl.h
+#
+#LOCAL_COPY_HEADERS_TO := $(LOCAL_MODULE)
+
+LOCAL_CFLAGS := -DGOOGLE_PROTOBUF_NO_RTTI $(IGNORED_WARNINGS)
+
+# These are the minimum versions and don't need to be update.
+ifeq ($(TARGET_ARCH),arm)
+LOCAL_SDK_VERSION := 8
+else
+# x86/mips support only available from API 9.
+LOCAL_SDK_VERSION := 9
+endif
+LOCAL_NDK_STL_VARIANT := stlport_static
+
+include $(BUILD_STATIC_LIBRARY)
+
+# C++ lite library (libc++ flavored for the platform)
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-cpp-lite
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CPP_EXTENSION := .cc
+
+LOCAL_SRC_FILES := $(CC_LITE_SRC_FILES)
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ $(LOCAL_PATH)/src
+
+LOCAL_CFLAGS := -DGOOGLE_PROTOBUF_NO_RTTI $(IGNORED_WARNINGS)
+
+include $(BUILD_SHARED_LIBRARY)
+
+# C++ full library
+# =======================================================
+protobuf_cc_full_src_files := \
+ $(CC_LITE_SRC_FILES) \
+ src/google/protobuf/stubs/strutil.cc \
+ src/google/protobuf/stubs/strutil.h \
+ src/google/protobuf/stubs/substitute.cc \
+ src/google/protobuf/stubs/substitute.h \
+ src/google/protobuf/stubs/structurally_valid.cc \
+ src/google/protobuf/descriptor.cc \
+ src/google/protobuf/descriptor.pb.cc \
+ src/google/protobuf/descriptor_database.cc \
+ src/google/protobuf/dynamic_message.cc \
+ src/google/protobuf/extension_set_heavy.cc \
+ src/google/protobuf/generated_message_reflection.cc \
+ src/google/protobuf/message.cc \
+ src/google/protobuf/reflection_ops.cc \
+ src/google/protobuf/service.cc \
+ src/google/protobuf/text_format.cc \
+ src/google/protobuf/unknown_field_set.cc \
+ src/google/protobuf/wire_format.cc \
+ src/google/protobuf/io/gzip_stream.cc \
+ src/google/protobuf/io/printer.cc \
+ src/google/protobuf/io/tokenizer.cc \
+ src/google/protobuf/io/zero_copy_stream_impl.cc \
+ src/google/protobuf/compiler/importer.cc \
+ src/google/protobuf/compiler/parser.cc
+
+# C++ full library - stlport version
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-cpp-full
+LOCAL_MODULE_TAGS := optional
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := $(protobuf_cc_full_src_files)
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ external/zlib \
+ $(LOCAL_PATH)/src
+
+# Define the header files to be copied
+#LOCAL_COPY_HEADERS := \
+# src/google/protobuf/stubs/once.h \
+# src/google/protobuf/stubs/common.h \
+# src/google/protobuf/io/coded_stream.h \
+# src/google/protobuf/generated_message_util.h \
+# src/google/protobuf/repeated_field.h \
+# src/google/protobuf/extension_set.h \
+# src/google/protobuf/wire_format_lite_inl.h
+#
+#LOCAL_COPY_HEADERS_TO := $(LOCAL_MODULE)
+
+LOCAL_CFLAGS := -DGOOGLE_PROTOBUF_NO_RTTI $(IGNORED_WARNINGS)
+
+# These are the minimum versions and don't need to be update.
+ifeq ($(TARGET_ARCH),arm)
+LOCAL_SDK_VERSION := 8
+else
+# x86/mips support only available from API 9.
+LOCAL_SDK_VERSION := 9
+endif
+LOCAL_NDK_STL_VARIANT := stlport_static
+
+include $(BUILD_STATIC_LIBRARY)
+
+# C++ full library - Gnustl+rtti version
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-cpp-full-gnustl-rtti
+LOCAL_MODULE_TAGS := optional
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := $(protobuf_cc_full_src_files)
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ external/zlib \
+ $(LOCAL_PATH)/src
+
+LOCAL_CFLAGS := -frtti $(IGNORED_WARNINGS)
+LOCAL_SDK_VERSION := 14
+LOCAL_NDK_STL_VARIANT := gnustl_static
+
+include $(BUILD_STATIC_LIBRARY)
+
+# C++ full library - libc++ version for the platform
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libprotobuf-cpp-full
+LOCAL_MODULE_TAGS := optional
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := $(protobuf_cc_full_src_files)
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ external/zlib \
+ $(LOCAL_PATH)/src
+
+LOCAL_CFLAGS := -DGOOGLE_PROTOBUF_NO_RTTI $(IGNORED_WARNINGS)
+LOCAL_SHARED_LIBRARIES := libz
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Clean temp vars
+protobuf_cc_full_src_files :=
+
+
+# Android Protocol buffer compiler, aprotoc (host executable)
+# used by the build systems as $(PROTOC) defined in
+# build/core/config.mk
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := aprotoc
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := $(COMPILER_SRC_FILES)
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/android \
+ external/zlib \
+ $(LOCAL_PATH)/src
+
+LOCAL_STATIC_LIBRARIES += libz
+
+ifneq ($(HOST_OS),windows)
+LOCAL_LDLIBS := -lpthread
+endif
+
+LOCAL_CFLAGS := $(IGNORED_WARNINGS)
+
+include $(BUILD_HOST_EXECUTABLE)
+
+# To test java proto params build rules.
+# =======================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := aprotoc-test-nano-params
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := current
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+
+LOCAL_SRC_FILES := \
+ src/google/protobuf/unittest_import_nano.proto \
+ src/google/protobuf/unittest_simple_nano.proto \
+ src/google/protobuf/unittest_stringutf8_nano.proto \
+ src/google/protobuf/unittest_recursive_nano.proto
+
+
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/src
+
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
+ java_package = $(LOCAL_PATH)/src/google/protobuf/unittest_import_nano.proto|com.google.protobuf.nano, \
+ java_outer_classname = $(LOCAL_PATH)/src/google/protobuf/unittest_import_nano.proto|UnittestImportNano
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# To test Android-specific nanoproto features.
+# =======================================================
+include $(CLEAR_VARS)
+
+# Parcelable messages
+LOCAL_MODULE := android-nano-test-parcelable
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := current
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+
+LOCAL_SRC_FILES := src/google/protobuf/unittest_simple_nano.proto
+
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/src
+
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
+ parcelable_messages = true
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+include $(CLEAR_VARS)
+
+# Parcelable and extendable messages
+LOCAL_MODULE := android-nano-test-parcelable-extendable
+LOCAL_MODULE_TAGS := tests
+LOCAL_SDK_VERSION := current
+
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
+
+LOCAL_SRC_FILES := src/google/protobuf/unittest_extension_nano.proto
+
+LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/src
+
+LOCAL_PROTO_JAVA_OUTPUT_PARAMS := \
+ parcelable_messages = true, \
+ store_unknown_fields = true
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+include $(CLEAR_VARS)
+
+# Test APK
+LOCAL_PACKAGE_NAME := NanoAndroidTest
+
+LOCAL_SDK_VERSION := 8
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-java-files-under, java/src/device/test/java/com/google/protobuf/nano)
+
+LOCAL_MANIFEST_FILE := java/src/device/test/AndroidManifest.xml
+
+LOCAL_STATIC_JAVA_LIBRARIES := libprotobuf-java-nano \
+ android-nano-test-parcelable \
+ android-nano-test-parcelable-extendable
+
+LOCAL_DEX_PREOPT := false
+
+include $(BUILD_PACKAGE)
+
+# 2.3.0 prebuilts for backwards compatibility.
+include $(LOCAL_PATH)/prebuilts/Android.mk
diff --git a/CHANGES.txt b/CHANGES.txt
index 214d8d9f..0d4ce0ec 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,125 @@
+2014-12-01 version 3.0.0-alpha-1 (C++/Java):
+
+ General
+ * Introduced Protocol Buffers language version 3 (aka proto3).
+
+ When protobuf was initially opensourced it implemented Protocol Buffers
+ language version 2 (aka proto2), which is why the version number
+ started from v2.0.0. From v3.0.0, a new language version (proto3) is
+ introduced while the old version (proto2) will continue to be supported.
+
+ The main intent of introducing proto3 is to clean up protobuf before
+ pushing the language as the foundation of Google's new API platform.
+ In proto3, the language is simplified, both for ease of use and to
+ make it available in a wider range of programming languages. At the
+ same time a few features are added to better support common idioms
+ found in APIs.
+
+ The following are the main new features in language version 3:
+
+ 1. Removal of field presence logic for primitive value fields, removal
+ of required fields, and removal of default values. This makes proto3
+ significantly easier to implement with open struct representations,
+ as in languages like Android Java, Objective C, or Go.
+ 2. Removal of unknown fields.
+ 3. Removal of extensions, which are instead replaced by a new standard
+ type called Any.
+ 4. Fix semantics for unknown enum values.
+ 5. Addition of maps.
+ 6. Addition of a small set of standard types for representation of time,
+ dynamic data, etc.
+ 7. A well-defined encoding in JSON as an alternative to binary proto
+ encoding.
+
+ This release (v3.0.0-alpha-1) includes partial proto3 support for C++ and
+ Java. Items 6 (well-known types) and 7 (JSON format) in the above feature
+ list are not impelmented.
+
+ A new notion "syntax" is introduced to specify whether a .proto file
+ uses proto2 or proto3:
+
+ // foo.proto
+ syntax = "proto3";
+ message Bar {...}
+
+ If omitted, the protocol compiler will generate a warning and "proto2" will
+ be used as the default. This warning will be turned into an error in a
+ future release.
+
+ We recommend that new Protocol Buffers users use proto3. However, we do not
+ generally recommend that existing users migrate from proto2 from proto3 due
+ to API incompatibility, and we will continue to support proto2 for a long
+ time.
+
+ * Added support for map fields (implemented in C++/Java for both proto2 and
+ proto3).
+
+ Map fields can be declared using the following syntax:
+
+ message Foo {
+ map<string, string> values = 1;
+ }
+
+ Data of a map field will be stored in memory as an unordered map and it
+ can be accessed through generated accessors.
+
+ C++
+ * Added arena allocation support (for both proto2 and proto3).
+
+ Profiling shows memory allocation and deallocation constitutes a significant
+ fraction of CPU-time spent in protobuf code and arena allocation is a
+ technique introduced to reduce this cost. With arena allocation, new
+ objects will be allocated from a large piece of preallocated memory and
+ deallocation of these objects is almost free. Early adoption shows 20% to
+ 50% improvement in some Google binaries.
+
+ To enable arena support, add the following option to your .proto file:
+
+ option cc_enable_arenas = true;
+
+ Protocol compiler will generate additional code to make the generated
+ message classes work with arenas. This does not change the existing API
+ of protobuf messages and does not affect wire format. Your existing code
+ should continue to work after adding this option. In the future we will
+ make this option enabled by default.
+
+ To actually take advantage of arena allocation, you need to use the arena
+ APIs when creating messages. A quick example of using the arena API:
+
+ {
+ google::protobuf::Arena arena;
+ // Allocate a protobuf message in the arena.
+ MyMessage* message = Arena::CreateMessage<MyMessage>(&arena);
+ // All submessages will be allocated in the same arena.
+ if (!message->ParseFromString(data)) {
+ // Deal with malformed input data.
+ }
+ // Must not delete the message here. It will be deleted automatically
+ // when the arena is destroyed.
+ }
+
+ Currently arena does not work with map fields. Enabling arena in a .proto
+ file containing map fields will result in compile errors in the generated
+ code. This will be addressed in a future release.
+
+2014-10-20 version 2.6.1:
+
+ C++
+ * Added atomicops support for Solaris.
+ * Released memory allocated by InitializeDefaultRepeatedFields() and
+ GetEmptyString(). Some memory sanitizers reported them as memory leaks.
+
+ Java
+ * Updated DynamicMessage.setField() to handle repeated enum values
+ correctly.
+ * Fixed a bug that caused NullPointerException to be thrown when
+ converting manually constructed FileDescriptorProto to
+ FileDescriptor.
+
+ Python
+ * Fixed WhichOneof() to work with de-serialized protobuf messages.
+ * Fixed a missing file problem of Python C++ implementation.
+
2014-08-15 version 2.6.0:
General
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 717ffc14..b8d97fc2 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -17,6 +17,12 @@ Proto2 Python primary authors:
Will Robinson <robinson@google.com>
Petar Petrov <petar@google.com>
+Java Nano primary authors:
+ Brian Duff <bduff@google.com>
+ Tom Chao <chaot@google.com>
+ Max Cai <maxtroy@google.com>
+ Ulas Kirazci <ulas@google.com>
+
Large code contributions:
Jason Hsueh <jasonh@google.com>
Joseph Schorr <jschorr@google.com>
@@ -91,3 +97,6 @@ Patch contributors:
William Orr <will@worrbase.com>
* Fixed detection of sched_yield on Solaris.
* Added atomicops for Solaris
+ Andrew Paprocki <andrew@ishiboo.com>
+ * Fixed minor IBM xlC compiler build issues
+ * Added atomicops for AIX (POWER)
diff --git a/LICENSE b/LICENSE
index 705db579..f086efdb 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,5 +1,16 @@
-Copyright 2008, Google Inc.
-All rights reserved.
+This license applies to all parts of Protocol Buffers except the following:
+
+ - Atomicops support for generic gcc, located in
+ src/google/protobuf/stubs/atomicops_internals_generic_gcc.h.
+ This file is copyrighted by Red Hat Inc.
+
+ - Atomicops support for AIX/POWER, located in
+ src/google/protobuf/stubs/atomicops_internals_aix.h.
+ This file is copyrighted by Bloomberg Finance LP.
+
+ - Andorid.mk, which is copyrighted by The Android Open Source Project.
+
+Copyright 2014, Google Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
diff --git a/Makefile.am b/Makefile.am
index 24e5e55c..566b2850 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -35,38 +35,7 @@ clean-local:
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = protobuf.pc protobuf-lite.pc
-EXTRA_DIST = \
- autogen.sh \
- generate_descriptor_proto.sh \
- README.md \
- INSTALL.txt \
- LICENSE \
- CONTRIBUTORS.txt \
- CHANGES.txt \
- editors/README.txt \
- editors/proto.vim \
- editors/protobuf-mode.el \
- vsprojects/config.h \
- vsprojects/extract_includes.bat \
- vsprojects/libprotobuf.vcproj \
- vsprojects/libprotobuf-lite.vcproj \
- vsprojects/libprotoc.vcproj \
- vsprojects/protobuf.sln \
- vsprojects/protoc.vcproj \
- vsprojects/readme.txt \
- vsprojects/test_plugin.vcproj \
- vsprojects/tests.vcproj \
- vsprojects/lite-test.vcproj \
- vsprojects/convert2008to2005.sh \
- examples/README.txt \
- examples/Makefile \
- examples/addressbook.proto \
- examples/add_person.cc \
- examples/list_people.cc \
- examples/AddPerson.java \
- examples/ListPeople.java \
- examples/add_person.py \
- examples/list_people.py \
+java_EXTRA_DIST= \
java/src/main/java/com/google/protobuf/AbstractMessage.java \
java/src/main/java/com/google/protobuf/AbstractMessageLite.java \
java/src/main/java/com/google/protobuf/AbstractParser.java \
@@ -79,6 +48,7 @@ EXTRA_DIST = \
java/src/main/java/com/google/protobuf/Descriptors.java \
java/src/main/java/com/google/protobuf/DynamicMessage.java \
java/src/main/java/com/google/protobuf/Extension.java \
+ java/src/main/java/com/google/protobuf/ExtensionLite.java \
java/src/main/java/com/google/protobuf/ExtensionRegistry.java \
java/src/main/java/com/google/protobuf/ExtensionRegistryLite.java \
java/src/main/java/com/google/protobuf/FieldSet.java \
@@ -91,6 +61,10 @@ EXTRA_DIST = \
java/src/main/java/com/google/protobuf/LazyStringArrayList.java \
java/src/main/java/com/google/protobuf/LazyStringList.java \
java/src/main/java/com/google/protobuf/LiteralByteString.java \
+ java/src/main/java/com/google/protobuf/MapEntry.java \
+ java/src/main/java/com/google/protobuf/MapEntryLite.java \
+ java/src/main/java/com/google/protobuf/MapField.java \
+ java/src/main/java/com/google/protobuf/MapFieldLite.java \
java/src/main/java/com/google/protobuf/Message.java \
java/src/main/java/com/google/protobuf/MessageLite.java \
java/src/main/java/com/google/protobuf/MessageLiteOrBuilder.java \
@@ -112,6 +86,7 @@ EXTRA_DIST = \
java/src/main/java/com/google/protobuf/TextFormat.java \
java/src/main/java/com/google/protobuf/UninitializedMessageException.java \
java/src/main/java/com/google/protobuf/UnknownFieldSet.java \
+ java/src/main/java/com/google/protobuf/UnknownFieldSetLite.java \
java/src/main/java/com/google/protobuf/UnmodifiableLazyStringList.java \
java/src/main/java/com/google/protobuf/Utf8.java \
java/src/main/java/com/google/protobuf/WireFormat.java \
@@ -124,18 +99,22 @@ EXTRA_DIST = \
java/src/test/java/com/google/protobuf/DeprecatedFieldTest.java \
java/src/test/java/com/google/protobuf/DescriptorsTest.java \
java/src/test/java/com/google/protobuf/DynamicMessageTest.java \
+ java/src/test/java/com/google/protobuf/FieldPresenceTest.java \
java/src/test/java/com/google/protobuf/ForceFieldBuildersPreRun.java \
java/src/test/java/com/google/protobuf/GeneratedMessageTest.java \
java/src/test/java/com/google/protobuf/IsValidUtf8Test.java \
java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java \
- java/src/test/java/com/google/protobuf/LazyFieldTest.java \
java/src/test/java/com/google/protobuf/LazyFieldLiteTest.java \
+ java/src/test/java/com/google/protobuf/LazyFieldTest.java \
java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java \
java/src/test/java/com/google/protobuf/LazyStringArrayListTest.java \
java/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java \
java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java \
java/src/test/java/com/google/protobuf/LiteralByteStringTest.java \
java/src/test/java/com/google/protobuf/LiteTest.java \
+ java/src/test/java/com/google/protobuf/MapForProto2LiteTest.java \
+ java/src/test/java/com/google/protobuf/MapForProto2Test.java \
+ java/src/test/java/com/google/protobuf/MapTest.java \
java/src/test/java/com/google/protobuf/MessageTest.java \
java/src/test/java/com/google/protobuf/NestedBuildersTest.java \
java/src/test/java/com/google/protobuf/ParserTest.java \
@@ -148,26 +127,34 @@ EXTRA_DIST = \
java/src/test/java/com/google/protobuf/TestBadIdentifiers.java \
java/src/test/java/com/google/protobuf/TestUtil.java \
java/src/test/java/com/google/protobuf/TextFormatTest.java \
+ java/src/test/java/com/google/protobuf/UnknownEnumValueTest.java \
+ java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java \
java/src/test/java/com/google/protobuf/UnknownFieldSetTest.java \
java/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java \
java/src/test/java/com/google/protobuf/WireFormatTest.java \
+ java/src/test/java/com/google/protobuf/field_presence_test.proto \
java/src/test/java/com/google/protobuf/lazy_fields_lite.proto \
java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto \
+ java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto \
+ java/src/test/java/com/google/protobuf/map_for_proto2_test.proto \
+ java/src/test/java/com/google/protobuf/map_test.proto \
java/src/test/java/com/google/protobuf/multiple_files_test.proto \
java/src/test/java/com/google/protobuf/nested_builders_test.proto \
java/src/test/java/com/google/protobuf/nested_extension_lite.proto \
java/src/test/java/com/google/protobuf/nested_extension.proto \
java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto \
java/src/test/java/com/google/protobuf/non_nested_extension.proto \
- java/src/test/java/com/google/protobuf/outer_class_name_test.proto \
java/src/test/java/com/google/protobuf/outer_class_name_test2.proto \
java/src/test/java/com/google/protobuf/outer_class_name_test3.proto \
+ java/src/test/java/com/google/protobuf/outer_class_name_test.proto \
java/src/test/java/com/google/protobuf/test_bad_identifiers.proto \
java/src/test/java/com/google/protobuf/test_check_utf8.proto \
java/src/test/java/com/google/protobuf/test_check_utf8_size.proto \
java/src/test/java/com/google/protobuf/test_custom_options.proto \
java/pom.xml \
- java/README.txt \
+ java/README.txt
+
+python_EXTRA_DIST= \
python/google/protobuf/internal/api_implementation.cc \
python/google/protobuf/internal/api_implementation.py \
python/google/protobuf/internal/api_implementation_default_test.py \
@@ -194,6 +181,7 @@ EXTRA_DIST = \
python/google/protobuf/internal/more_extensions.proto \
python/google/protobuf/internal/more_extensions_dynamic.proto \
python/google/protobuf/internal/more_messages.proto \
+ python/google/protobuf/internal/proto_builder_test.py \
python/google/protobuf/internal/python_message.py \
python/google/protobuf/internal/reflection_test.py \
python/google/protobuf/internal/service_reflection_test.py \
@@ -207,6 +195,10 @@ EXTRA_DIST = \
python/google/protobuf/internal/wire_format.py \
python/google/protobuf/internal/wire_format_test.py \
python/google/protobuf/internal/__init__.py \
+ python/google/protobuf/internal/import_test_package/BUILD \
+ python/google/protobuf/internal/import_test_package/__init__.py \
+ python/google/protobuf/internal/import_test_package/inner.proto \
+ python/google/protobuf/internal/import_test_package/outer.proto \
python/google/protobuf/pyext/README \
python/google/protobuf/pyext/cpp_message.py \
python/google/protobuf/pyext/descriptor.h \
@@ -232,6 +224,7 @@ EXTRA_DIST = \
python/google/protobuf/descriptor_pool.py \
python/google/protobuf/message.py \
python/google/protobuf/message_factory.py \
+ python/google/protobuf/proto_builder.py \
python/google/protobuf/reflection.py \
python/google/protobuf/service.py \
python/google/protobuf/service_reflection.py \
@@ -246,6 +239,59 @@ EXTRA_DIST = \
python/stubout.py \
python/README.txt
+ruby_EXTRA_DIST= \
+ ruby/README.md \
+ ruby/Rakefile \
+ ruby/ext/google/protobuf_c/defs.c \
+ ruby/ext/google/protobuf_c/encode_decode.c \
+ ruby/ext/google/protobuf_c/extconf.rb \
+ ruby/ext/google/protobuf_c/message.c \
+ ruby/ext/google/protobuf_c/protobuf.c \
+ ruby/ext/google/protobuf_c/protobuf.h \
+ ruby/ext/google/protobuf_c/repeated_field.c \
+ ruby/ext/google/protobuf_c/storage.c \
+ ruby/ext/google/protobuf_c/upb.c \
+ ruby/ext/google/protobuf_c/upb.h \
+ ruby/google-protobuf.gemspec \
+ ruby/lib/google/protobuf.rb \
+ ruby/tests/basic.rb \
+ ruby/tests/stress.rb
+
+all_EXTRA_DIST=$(java_EXTRA_DIST) $(python_EXTRA_DIST) $(ruby_EXTRA_DIST)
+
+EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \
+ autogen.sh \
+ generate_descriptor_proto.sh \
+ README.md \
+ INSTALL.txt \
+ LICENSE \
+ CONTRIBUTORS.txt \
+ CHANGES.txt \
+ editors/README.txt \
+ editors/proto.vim \
+ editors/protobuf-mode.el \
+ vsprojects/config.h \
+ vsprojects/extract_includes.bat \
+ vsprojects/libprotobuf.vcproj \
+ vsprojects/libprotobuf-lite.vcproj \
+ vsprojects/libprotoc.vcproj \
+ vsprojects/protobuf.sln \
+ vsprojects/protoc.vcproj \
+ vsprojects/readme.txt \
+ vsprojects/test_plugin.vcproj \
+ vsprojects/tests.vcproj \
+ vsprojects/lite-test.vcproj \
+ vsprojects/convert2008to2005.sh \
+ examples/README.txt \
+ examples/Makefile \
+ examples/addressbook.proto \
+ examples/add_person.cc \
+ examples/list_people.cc \
+ examples/AddPerson.java \
+ examples/ListPeople.java \
+ examples/add_person.py \
+ examples/list_people.py
+
# Deletes all the files generated by autogen.sh.
MAINTAINERCLEANFILES = \
aclocal.m4 \
diff --git a/README.md b/README.md
index 5fbb3443..ae406792 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
Protocol Buffers - Google's data interchange format
===================================================
+[![Build Status](https://travis-ci.org/google/protobuf.svg?branch=master)](https://travis-ci.org/google/protobuf)
+
Copyright 2008 Google Inc.
https://developers.google.com/protocol-buffers/
@@ -127,6 +129,14 @@ For advanced usage information on configure and make, see INSTALL.txt.
Also, you will need to use gmake instead of make.
+**Note for AIX users**
+
+ Compile using the IBM xlC C++ compiler as follows:
+
+ ./configure CXX=xlC
+
+ Also, you will need to use GNU `make` (`gmake`) instead of AIX `make`.
+
C++ Installation - Windows
--------------------------
diff --git a/autogen.sh b/autogen.sh
index c3e026d2..08966c63 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -18,9 +18,11 @@ fi
# Check that gtest is present. Usually it is already there since the
# directory is set up as an SVN external.
if test ! -e gtest; then
- echo "Google Test not present. Fetching gtest-1.5.0 from the web..."
- curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx
- mv gtest-1.5.0 gtest
+ echo "Google Test not present. Fetching gtest-1.7.0 from the web..."
+ curl -O https://googletest.googlecode.com/files/gtest-1.7.0.zip
+ unzip -q gtest-1.7.0.zip
+ rm gtest-1.7.0.zip
+ mv gtest-1.7.0 gtest
fi
set -ex
diff --git a/configure.ac b/configure.ac
index d8a8ee7a..d1fde9d5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -12,7 +12,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],[2.6.0],[protobuf@googlegroups.com],[protobuf])
+AC_INIT([Protocol Buffers],[3.0.0-pre],[protobuf@googlegroups.com],[protobuf])
AM_MAINTAINER_MODE([enable])
@@ -20,6 +20,14 @@ AC_CONFIG_SRCDIR(src/google/protobuf/message.cc)
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
+AC_ARG_VAR(DIST_LANG, [language to include in the distribution package (i.e., make dist)])
+case "$DIST_LANG" in
+ "") DIST_LANG=all ;;
+ all | cpp | java | python | javanano | ruby) ;;
+ *) AC_MSG_FAILURE([unknown language: $DIST_LANG]) ;;
+esac
+AC_SUBST(DIST_LANG)
+
# autoconf's default CXXFLAGS are usually "-g -O2". These aren't necessarily
# the best choice for libprotobuf.
AS_IF([test "x${ac_cv_env_CFLAGS_set}" = "x"],
@@ -46,6 +54,7 @@ AC_PROG_CC
AC_PROG_CXX
AC_LANG([C++])
ACX_USE_SYSTEM_EXTENSIONS
+AM_PROG_AR
AM_CONDITIONAL(GCC, test "$GCC" = yes) # let the Makefile know if we're gcc
# test_util.cc takes forever to compile with GCC and optimization turned on.
@@ -139,8 +148,14 @@ AM_CONDITIONAL([USE_EXTERNAL_PROTOC], [test "$with_protoc" != "no"])
ACX_PTHREAD
AC_CXX_STL_HASH
-# Need to link against rt on Solaris
-AC_SEARCH_LIBS([sched_yield], [rt], [], [AC_MSG_FAILURE([sched_yield was not found on your system])])
+case "$target_os" in
+ mingw* | cygwin* | win*)
+ ;;
+ *)
+ # 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
# HACK: Make gtest's configure script pick up our copy of CFLAGS and CXXFLAGS,
# since the flags added by ACX_CHECK_SUNCC must be used when compiling gtest
diff --git a/editors/proto.vim b/editors/proto.vim
index 7cd1dbf2..23085a28 100644
--- a/editors/proto.vim
+++ b/editors/proto.vim
@@ -54,7 +54,7 @@ syn keyword pbTodo contained TODO FIXME XXX
syn cluster pbCommentGrp contains=pbTodo
syn keyword pbSyntax syntax import option
-syn keyword pbStructure package message group
+syn keyword pbStructure package message group oneof
syn keyword pbRepeat optional required repeated
syn keyword pbDefault default
syn keyword pbExtend extend extensions to max
diff --git a/java/pom.xml b/java/pom.xml
index 8bf242dd..bbadc656 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -10,7 +10,7 @@
</parent>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
- <version>2.6.0</version>
+ <version>3.0.0-pre</version>
<packaging>bundle</packaging>
<name>Protocol Buffer Java API</name>
<description>
@@ -130,6 +130,10 @@
<arg value="../src/google/protobuf/unittest_lite_imports_nonlite.proto" />
<arg value="../src/google/protobuf/unittest_enormous_descriptor.proto" />
<arg value="../src/google/protobuf/unittest_no_generic_services.proto" />
+ <arg value="src/test/java/com/google/protobuf/field_presence_test.proto" />
+ <arg value="src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto" />
+ <arg value="src/test/java/com/google/protobuf/map_for_proto2_test.proto" />
+ <arg value="src/test/java/com/google/protobuf/map_test.proto" />
</exec>
</tasks>
<testSourceRoot>target/generated-test-sources</testSourceRoot>
@@ -148,7 +152,7 @@
<instructions>
<Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
<Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
- <Export-Package>com.google.protobuf;version=2.6.0</Export-Package>
+ <Export-Package>com.google.protobuf;version=3.0.0-pre</Export-Package>
</instructions>
</configuration>
</plugin>
@@ -164,35 +168,40 @@
<configuration>
<includes>
<include>**/AbstractMessageLite.java</include>
+ <include>**/AbstractParser.java</include>
+ <include>**/BoundedByteString.java</include>
<include>**/ByteString.java</include>
<include>**/CodedInputStream.java</include>
<include>**/CodedOutputStream.java</include>
+ <include>**/ExtensionLite.java</include>
<include>**/ExtensionRegistryLite.java</include>
<include>**/FieldSet.java</include>
<include>**/GeneratedMessageLite.java</include>
<include>**/Internal.java</include>
<include>**/InvalidProtocolBufferException.java</include>
+ <include>**/LazyFieldLite.java</include>
<include>**/LazyStringArrayList.java</include>
<include>**/LazyStringList.java</include>
+ <include>**/LiteralByteString.java</include>
+ <include>**/MapEntryLite.java</include>
+ <include>**/MapFieldLite.java</include>
<include>**/MessageLite.java</include>
<include>**/MessageLiteOrBuilder.java</include>
+ <include>**/Parser.java</include>
+ <include>**/ProtocolStringList.java</include>
+ <include>**/RopeByteString.java</include>
<include>**/SmallSortedMap.java</include>
<include>**/UninitializedMessageException.java</include>
+ <include>**/UnknownFieldSetLite.java</include>
<include>**/UnmodifiableLazyStringList.java</include>
- <include>**/WireFormat.java</include>
- <include>**/Parser.java</include>
- <include>**/AbstractParser.java</include>
- <include>**/BoundedByteString.java</include>
- <include>**/LiteralByteString.java</include>
- <include>**/RopeByteString.java</include>
<include>**/Utf8.java</include>
- <include>**/LazyField.java</include>
- <include>**/LazyFieldLite.java</include>
- <include>**/ProtocolStringList.java</include>
+ <include>**/WireFormat.java</include>
</includes>
<testIncludes>
- <testInclude>**/LiteTest.java</testInclude>
<testInclude>**/*Lite.java</testInclude>
+ <testInclude>**/LazyMessageLiteTest.java</testInclude>
+ <testInclude>**/LiteTest.java</testInclude>
+ <testInclude>**/UnknownFieldSetLiteTest.java</testInclude>
</testIncludes>
</configuration>
</plugin>
@@ -200,7 +209,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
- <include>**/LiteTest.java</include>
+ <include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
diff --git a/java/src/main/java/com/google/protobuf/AbstractMessage.java b/java/src/main/java/com/google/protobuf/AbstractMessage.java
index ae9d5e39..6de4cae3 100644
--- a/java/src/main/java/com/google/protobuf/AbstractMessage.java
+++ b/java/src/main/java/com/google/protobuf/AbstractMessage.java
@@ -37,6 +37,9 @@ import com.google.protobuf.Internal.EnumLite;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -144,6 +147,40 @@ public abstract class AbstractMessage extends AbstractMessageLite
}
/**
+ * 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()) {
+ return Collections.emptyMap();
+ }
+ Map result = new HashMap();
+ Iterator iterator = list.iterator();
+ Message entry = (Message) iterator.next();
+ Descriptors.Descriptor descriptor = entry.getDescriptorForType();
+ Descriptors.FieldDescriptor key = descriptor.findFieldByName("key");
+ Descriptors.FieldDescriptor value = descriptor.findFieldByName("value");
+ result.put(entry.getField(key), entry.getField(value));
+ while (iterator.hasNext()) {
+ entry = (Message) iterator.next();
+ result.put(entry.getField(key), entry.getField(value));
+ }
+ return result;
+ }
+
+ /**
+ * 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);
+ Map mb = convertMapEntryListToMap((List) b);
+ return MapFieldLite.equals(ma, mb);
+ }
+
+ /**
* Compares two set of fields.
* This method is used to implement {@link AbstractMessage#equals(Object)}
* and {@link AbstractMutableMessage#equals(Object)}. It takes special care
@@ -181,6 +218,10 @@ public abstract class AbstractMessage extends AbstractMessageLite
return false;
}
}
+ } else if (descriptor.isMapField()) {
+ if (!compareMapField(value1, value2)) {
+ return false;
+ }
} else {
// Compare non-bytes fields.
if (!value1.equals(value2)) {
@@ -190,6 +231,15 @@ public abstract class AbstractMessage extends AbstractMessageLite
}
return true;
}
+
+ /**
+ * 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));
+ }
/** Get a hash code for given fields and values, using the given seed. */
@SuppressWarnings("unchecked")
@@ -198,7 +248,9 @@ public abstract class AbstractMessage extends AbstractMessageLite
FieldDescriptor field = entry.getKey();
Object value = entry.getValue();
hash = (37 * hash) + field.getNumber();
- if (field.getType() != FieldDescriptor.Type.ENUM){
+ if (field.isMapField()) {
+ hash = (53 * hash) + hashMapField(value);
+ } else if (field.getType() != FieldDescriptor.Type.ENUM){
hash = (53 * hash) + value.hashCode();
} else if (field.isRepeated()) {
List<? extends EnumLite> list = (List<? extends EnumLite>) value;
@@ -359,6 +411,12 @@ public abstract class AbstractMessage extends AbstractMessageLite
"getFieldBuilder() called on an unsupported message type.");
}
+ public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field,
+ int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldBuilder() called on an unsupported message type.");
+ }
+
public String toString() {
return TextFormat.printToString(this);
}
diff --git a/java/src/main/java/com/google/protobuf/BoundedByteString.java b/java/src/main/java/com/google/protobuf/BoundedByteString.java
index 2828e9c7..8cb6f463 100644
--- a/java/src/main/java/com/google/protobuf/BoundedByteString.java
+++ b/java/src/main/java/com/google/protobuf/BoundedByteString.java
@@ -30,6 +30,9 @@
package com.google.protobuf;
+import java.io.InvalidObjectException;
+import java.io.IOException;
+import java.io.ObjectInputStream;
import java.util.NoSuchElementException;
/**
@@ -123,6 +126,20 @@ class BoundedByteString extends LiteralByteString {
}
// =================================================================
+ // Serializable
+
+ private static final long serialVersionUID = 1L;
+
+ Object writeReplace() {
+ return new LiteralByteString(toByteArray());
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException {
+ throw new InvalidObjectException(
+ "BoundedByteStream instances are not to be serialized directly");
+ }
+
+ // =================================================================
// ByteIterator
@Override
diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java
index 7da56127..cff1ee51 100644
--- a/java/src/main/java/com/google/protobuf/ByteString.java
+++ b/java/src/main/java/com/google/protobuf/ByteString.java
@@ -34,6 +34,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@@ -57,7 +58,7 @@ import java.util.NoSuchElementException;
* @author carlanton@google.com Carl Haverl
* @author martinrb@google.com Martin Buchholz
*/
-public abstract class ByteString implements Iterable<Byte> {
+public abstract class ByteString implements Iterable<Byte>, Serializable {
/**
* When two strings to be concatenated have a combined length shorter than
@@ -502,9 +503,9 @@ public abstract class ByteString implements Iterable<Byte> {
/**
* Internal (package private) implementation of
- * @link{#copyTo(byte[],int,int,int}.
+ * {@link #copyTo(byte[],int,int,int)}.
* It assumes that all error checking has already been performed and that
- * @code{numberToCopy > 0}.
+ * {@code numberToCopy > 0}.
*/
protected abstract void copyToInternal(byte[] target, int sourceOffset,
int targetOffset, int numberToCopy);
@@ -699,7 +700,7 @@ public abstract class ByteString implements Iterable<Byte> {
* 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)}
+ * {@link InputStream#read(byte[])}, {@link InputStream#read(byte[],int,int)}
* and {@link InputStream#skip(long)} will read/skip as many bytes as are
* available.
* <p>
diff --git a/java/src/main/java/com/google/protobuf/CodedInputStream.java b/java/src/main/java/com/google/protobuf/CodedInputStream.java
index a00ae86f..0ca00bab 100644
--- a/java/src/main/java/com/google/protobuf/CodedInputStream.java
+++ b/java/src/main/java/com/google/protobuf/CodedInputStream.java
@@ -612,16 +612,16 @@ public final class CodedInputStream {
return x;
} else if (bufferSize - pos < 9) {
break fastpath;
- } else if ((x ^= (buffer[pos++] << 7)) < 0L) {
- x ^= (~0L << 7);
- } else if ((x ^= (buffer[pos++] << 14)) >= 0L) {
- x ^= (~0L << 7) ^ (~0L << 14);
- } else if ((x ^= (buffer[pos++] << 21)) < 0L) {
- x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21);
+ } else if ((x ^= (buffer[pos++] << 7)) < 0) {
+ x ^= (~0 << 7);
+ } else if ((x ^= (buffer[pos++] << 14)) >= 0) {
+ x ^= (~0 << 7) ^ (~0 << 14);
+ } else if ((x ^= (buffer[pos++] << 21)) < 0) {
+ x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21);
} else {
int y = buffer[pos++];
x ^= y << 28;
- x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28);
+ x ^= (~0 << 7) ^ (~0 << 14) ^ (~0 << 21) ^ (~0 << 28);
if (y < 0 &&
buffer[pos++] < 0 &&
buffer[pos++] < 0 &&
@@ -739,13 +739,13 @@ public final class CodedInputStream {
return y;
} else if (bufferSize - pos < 9) {
break fastpath;
- } else if ((x = y ^ (buffer[pos++] << 7)) < 0L) {
- x ^= (~0L << 7);
- } else if ((x ^= (buffer[pos++] << 14)) >= 0L) {
- x ^= (~0L << 7) ^ (~0L << 14);
- } else if ((x ^= (buffer[pos++] << 21)) < 0L) {
- x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21);
- } else if ((x ^= ((long) buffer[pos++] << 28)) >= 0L) {
+ } else if ((y ^= (buffer[pos++] << 7)) < 0) {
+ x = y ^ (~0 << 7);
+ } else if ((y ^= (buffer[pos++] << 14)) >= 0) {
+ x = y ^ ((~0 << 7) ^ (~0 << 14));
+ } else if ((y ^= (buffer[pos++] << 21)) < 0) {
+ x = y ^ ((~0 << 7) ^ (~0 << 14) ^ (~0 << 21));
+ } else if ((x = ((long) y) ^ ((long) buffer[pos++] << 28)) >= 0L) {
x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28);
} else if ((x ^= ((long) buffer[pos++] << 35)) < 0L) {
x ^= (~0L << 7) ^ (~0L << 14) ^ (~0L << 21) ^ (~0L << 28) ^ (~0L << 35);
@@ -882,7 +882,7 @@ public final class CodedInputStream {
/** See setSizeLimit() */
private int sizeLimit = DEFAULT_SIZE_LIMIT;
- private static final int DEFAULT_RECURSION_LIMIT = 64;
+ private static final int DEFAULT_RECURSION_LIMIT = 100;
private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB
private static final int BUFFER_SIZE = 4096;
diff --git a/java/src/main/java/com/google/protobuf/Descriptors.java b/java/src/main/java/com/google/protobuf/Descriptors.java
index caae0f77..d65e8b49 100644
--- a/java/src/main/java/com/google/protobuf/Descriptors.java
+++ b/java/src/main/java/com/google/protobuf/Descriptors.java
@@ -32,6 +32,7 @@ package com.google.protobuf;
import com.google.protobuf.DescriptorProtos.*;
+import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -40,6 +41,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.WeakHashMap;
import java.util.logging.Logger;
import java.io.UnsupportedEncodingException;
@@ -123,6 +125,26 @@ public final class Descriptors {
return Collections.unmodifiableList(Arrays.asList(publicDependencies));
}
+ /** The syntax of the .proto file. */
+ public enum Syntax {
+ UNKNOWN("unknown"),
+ PROTO2("proto2"),
+ PROTO3("proto3");
+
+ Syntax(String name) {
+ this.name = name;
+ }
+ private final String name;
+ }
+
+ /** Get the syntax of the .proto file. */
+ public Syntax getSyntax() {
+ if (Syntax.PROTO3.name.equals(proto.getSyntax())) {
+ return Syntax.PROTO3;
+ }
+ return Syntax.PROTO2;
+ }
+
/**
* Find a message type in the file by name. Does not find nested types.
*
@@ -539,6 +561,10 @@ public final class Descriptors {
extensions[i].setProto(proto.getExtension(i));
}
}
+
+ boolean supportsUnknownEnumValue() {
+ return getSyntax() == Syntax.PROTO3;
+ }
}
// =================================================================
@@ -871,6 +897,11 @@ public final class Descriptors {
return (type == Type.STRING) && (getFile().getOptions().getJavaStringCheckUtf8());
}
+ boolean isMapField() {
+ 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.
@@ -1001,6 +1032,11 @@ public final class Descriptors {
return getNumber() - other.getNumber();
}
+ @Override
+ public String toString() {
+ return getFullName();
+ }
+
private final int index;
private FieldDescriptorProto proto;
@@ -1420,6 +1456,64 @@ public final class Descriptors {
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.
+ */
+ public EnumValueDescriptor findValueByNumberCreatingIfUnknown(final int number) {
+ EnumValueDescriptor result = findValueByNumber(number);
+ if (result != null) {
+ return result;
+ }
+ // The number represents an unknown enum value.
+ synchronized (this) {
+ // Descriptors are compared by object identity so for the same number
+ // we need to return the same EnumValueDescriptor object. This means
+ // we have to store created EnumValueDescriptors. However, as there
+ // are potentially 2G unknown enum values, storing all of these
+ // objects persistently will consume lots of memory for long-running
+ // services and it's also unnecessary as not many EnumValueDescriptors
+ // will be used at the same time.
+ //
+ // To solve the problem we take advantage of Java's weak references and
+ // rely on gc to release unused descriptors.
+ //
+ // Here is how it works:
+ // * We store unknown EnumValueDescriptors in a WeakHashMap with the
+ // value being a weak reference to the descriptor.
+ // * The descriptor holds a strong reference to the key so as long
+ // as the EnumValueDescriptor is in use, the key will be there
+ // and the corresponding map entry will be there. Following-up
+ // queries with the same number will return the same descriptor.
+ // * If the user no longer uses an unknown EnumValueDescriptor,
+ // it will be gc-ed since we only hold a weak reference to it in
+ // the map. The key in the corresponding map entry will also be
+ // gc-ed as the only strong reference to it is in the descriptor
+ // which is just gc-ed. With the key being gone WeakHashMap will
+ // then remove the whole entry. This way unknown descriptors will
+ // be freed automatically and we don't need to do anything to
+ // clean-up unused map entries.
+
+ // Note: We must use "new Integer(number)" here because we don't want
+ // these Integer objects to be cached.
+ Integer key = new Integer(number);
+ WeakReference<EnumValueDescriptor> reference = unknownValues.get(key);
+ if (reference != null) {
+ result = reference.get();
+ }
+ if (result == null) {
+ result = new EnumValueDescriptor(file, this, key);
+ unknownValues.put(key, new WeakReference<EnumValueDescriptor>(result));
+ }
+ }
+ return result;
+ }
+
+ // Used in tests only.
+ int getUnknownEnumValueDescriptorCount() {
+ return unknownValues.size();
+ }
private final int index;
private EnumDescriptorProto proto;
@@ -1427,6 +1521,8 @@ public final class Descriptors {
private final FileDescriptor file;
private final Descriptor containingType;
private EnumValueDescriptor[] values;
+ private final WeakHashMap<Integer, WeakReference<EnumValueDescriptor>> unknownValues =
+ new WeakHashMap<Integer, WeakReference<EnumValueDescriptor>>();
private EnumDescriptor(final EnumDescriptorProto proto,
final FileDescriptor file,
@@ -1531,6 +1627,25 @@ public final class Descriptors {
file.pool.addSymbol(this);
file.pool.addEnumValueByNumber(this);
}
+
+ private Integer number;
+ // Create an unknown enum value.
+ private EnumValueDescriptor(
+ 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();
+ 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.
+ }
/** See {@link FileDescriptor#setProto}. */
private void setProto(final EnumValueDescriptorProto proto) {
diff --git a/java/src/main/java/com/google/protobuf/DynamicMessage.java b/java/src/main/java/com/google/protobuf/DynamicMessage.java
index c9ce667b..9c5e6c61 100644
--- a/java/src/main/java/com/google/protobuf/DynamicMessage.java
+++ b/java/src/main/java/com/google/protobuf/DynamicMessage.java
@@ -59,7 +59,7 @@ public final class DynamicMessage extends AbstractMessage {
* oneofCases stores the FieldDescriptor for each oneof to indicate
* which field is set. Caller should make sure the array is immutable.
*
- * This contructor is package private and will be used in
+ * This constructor is package private and will be used in
* {@code DynamicMutableMessage} to convert a mutable message to an immutable
* message.
*/
@@ -549,12 +549,22 @@ public final class DynamicMessage extends AbstractMessage {
}
public Builder setUnknownFields(UnknownFieldSet unknownFields) {
+ if (getDescriptorForType().getFile().getSyntax()
+ == Descriptors.FileDescriptor.Syntax.PROTO3) {
+ // Proto3 discards unknown fields.
+ return this;
+ }
this.unknownFields = unknownFields;
return this;
}
@Override
public Builder mergeUnknownFields(UnknownFieldSet unknownFields) {
+ if (getDescriptorForType().getFile().getSyntax()
+ == Descriptors.FileDescriptor.Syntax.PROTO3) {
+ // Proto3 discards unknown fields.
+ return this;
+ }
this.unknownFields =
UnknownFieldSet.newBuilder(this.unknownFields)
.mergeFrom(unknownFields)
@@ -588,10 +598,12 @@ public final class DynamicMessage extends AbstractMessage {
throw new IllegalArgumentException(
"DynamicMessage should use EnumValueDescriptor to set Enum Value.");
}
- if (field.getEnumType() != ((EnumValueDescriptor) value).getType()) {
- throw new IllegalArgumentException(
- "EnumValueDescriptor doesn't much Enum Field.");
- }
+ // TODO(xiaofeng): Re-enable this check after Orgstore is fixed to not
+ // set incorrect EnumValueDescriptors.
+ // if (field.getEnumType() != ((EnumValueDescriptor) value).getType()) {
+ // throw new IllegalArgumentException(
+ // "EnumValueDescriptor doesn't match Enum Field.");
+ // }
}
/** Verifies the value for an enum field. */
@@ -618,5 +630,12 @@ public final class DynamicMessage extends AbstractMessage {
throw new UnsupportedOperationException(
"getFieldBuilder() called on a dynamic message type.");
}
+
+ @Override
+ public com.google.protobuf.Message.Builder getRepeatedFieldBuilder(FieldDescriptor field,
+ int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldBuilder() called on a dynamic message type.");
+ }
}
}
diff --git a/java/src/main/java/com/google/protobuf/Extension.java b/java/src/main/java/com/google/protobuf/Extension.java
index 0baa22b3..68d29f33 100644
--- a/java/src/main/java/com/google/protobuf/Extension.java
+++ b/java/src/main/java/com/google/protobuf/Extension.java
@@ -35,27 +35,16 @@ package com.google.protobuf;
*
* @author liujisi@google.com (Jisi Liu)
*/
-public abstract class Extension<ContainingType extends MessageLite, Type> {
- /** Returns the field number of the extension. */
- public abstract int getNumber();
-
- /** Returns the type of the field. */
- public abstract WireFormat.FieldType getLiteType();
-
- /** Returns whether it is a repeated field. */
- public abstract boolean isRepeated();
+public abstract class Extension<ContainingType extends MessageLite, Type>
+ extends ExtensionLite<ContainingType, Type> {
/** Returns the descriptor of the extension. */
public abstract Descriptors.FieldDescriptor getDescriptor();
- /** 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.
- */
- public abstract MessageLite getMessageDefaultInstance();
+ /** Returns whether or not this extension is a Lite Extension. */
+ final boolean isLite() {
+ return false;
+ }
// All the methods below are extension implementation details.
diff --git a/java/src/main/java/com/google/protobuf/ExtensionLite.java b/java/src/main/java/com/google/protobuf/ExtensionLite.java
new file mode 100644
index 00000000..f8f5bd2c
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/ExtensionLite.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;
+
+/**
+ * Lite interface that generated extensions implement.
+ * <p>
+ * Methods are for use by generated code only. You can hold a reference to
+ * extensions using this type name.
+ */
+public abstract class ExtensionLite<ContainingType extends MessageLite, Type> {
+
+ /** Returns the field number of the extension. */
+ public abstract int getNumber();
+
+ /** Returns the type of the field. */
+ public abstract WireFormat.FieldType getLiteType();
+
+ /** Returns whether it is a repeated field. */
+ public abstract boolean isRepeated();
+
+ /** 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.
+ */
+ public abstract MessageLite getMessageDefaultInstance();
+
+ /** Returns whether or not this extension is a Lite Extension. */
+ boolean isLite() {
+ return true;
+ }
+}
diff --git a/java/src/main/java/com/google/protobuf/FieldSet.java b/java/src/main/java/com/google/protobuf/FieldSet.java
index 392f4efc..ff9b5bc0 100644
--- a/java/src/main/java/com/google/protobuf/FieldSet.java
+++ b/java/src/main/java/com/google/protobuf/FieldSet.java
@@ -672,7 +672,7 @@ final class FieldSet<FieldDescriptorType extends
* {@link Message#getField(Descriptors.FieldDescriptor)} for
* this field.
*/
- private static void writeElementNoTag(
+ static void writeElementNoTag(
final CodedOutputStream output,
final WireFormat.FieldType type,
final Object value) throws IOException {
@@ -830,7 +830,7 @@ final class FieldSet<FieldDescriptorType extends
* {@link Message#getField(Descriptors.FieldDescriptor)} for
* this field.
*/
- private static int computeElementSizeNoTag(
+ static int computeElementSizeNoTag(
final WireFormat.FieldType type, final Object value) {
switch (type) {
// Note: Minor violation of 80-char limit rule here because this would
diff --git a/java/src/main/java/com/google/protobuf/GeneratedMessage.java b/java/src/main/java/com/google/protobuf/GeneratedMessage.java
index a6101cb0..156d1633 100644
--- a/java/src/main/java/com/google/protobuf/GeneratedMessage.java
+++ b/java/src/main/java/com/google/protobuf/GeneratedMessage.java
@@ -31,16 +31,20 @@
package com.google.protobuf;
import com.google.protobuf.Descriptors.Descriptor;
+import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor;
+import com.google.protobuf.GeneratedMessageLite.ExtendableMessage;
+import com.google.protobuf.GeneratedMessageLite.GeneratedExtension;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@@ -67,10 +71,15 @@ public abstract class GeneratedMessage extends AbstractMessage
*/
protected static boolean alwaysUseFieldBuilders = false;
+ /** For use by generated code only. */
+ protected UnknownFieldSet unknownFields;
+
protected GeneratedMessage() {
+ unknownFields = UnknownFieldSet.getDefaultInstance();
}
protected GeneratedMessage(Builder<?> builder) {
+ unknownFields = builder.getUnknownFields();
}
public Parser<? extends GeneratedMessage> getParserForType() {
@@ -291,13 +300,12 @@ public abstract class GeneratedMessage extends AbstractMessage
return isClean;
}
- // This is implemented here only to work around an apparent bug in the
- // Java compiler and/or build system. See bug #1898463. The mere presence
- // of this dummy clone() implementation makes it go away.
@Override
public BuilderType clone() {
- throw new UnsupportedOperationException(
- "This is supposed to be overridden by subclasses.");
+ BuilderType builder =
+ (BuilderType) getDefaultInstanceForType().newBuilderForType();
+ builder.mergeFrom(buildPartial());
+ return builder;
}
/**
@@ -358,6 +366,13 @@ public abstract class GeneratedMessage extends AbstractMessage
}
//@Override (Java 1.6 override semantics, but we must support 1.5)
+ public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field,
+ int index) {
+ return internalGetFieldAccessorTable().getField(field).getRepeatedBuilder(
+ this, index);
+ }
+
+ //@Override (Java 1.6 override semantics, but we must support 1.5)
public boolean hasOneof(final OneofDescriptor oneof) {
return internalGetFieldAccessorTable().getOneof(oneof).has(this);
}
@@ -428,7 +443,7 @@ public abstract class GeneratedMessage extends AbstractMessage
return (BuilderType) this;
}
- public final BuilderType setUnknownFields(
+ public BuilderType setUnknownFields(
final UnknownFieldSet unknownFields) {
this.unknownFields = unknownFields;
onChanged();
@@ -436,7 +451,7 @@ public abstract class GeneratedMessage extends AbstractMessage
}
@Override
- public final BuilderType mergeUnknownFields(
+ public BuilderType mergeUnknownFields(
final UnknownFieldSet unknownFields) {
this.unknownFields =
UnknownFieldSet.newBuilder(this.unknownFields)
@@ -529,6 +544,25 @@ public abstract class GeneratedMessage extends AbstractMessage
isClean = false;
}
}
+
+ /**
+ * Gets the map field with the given field number. This method should be
+ * overridden in the generated message class if the message contains map
+ * fields.
+ *
+ * Unlike other field types, reflection support for map fields can't be
+ * implemented based on generated public API because we need to access a
+ * map field as a list in reflection API but the generated API only allows
+ * us to access it as a map. This method returns the underlying map field
+ * directly and thus enables us to access the map field as a list.
+ */
+ @SuppressWarnings({"unused", "rawtypes"})
+ protected MapField internalGetMapField(int fieldNumber) {
+ // Note that we can't use descriptor names here because this method will
+ // be called when descriptor is being initialized.
+ throw new RuntimeException(
+ "No map fields found in " + getClass().getName());
+ }
}
// =================================================================
@@ -541,19 +575,19 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Check if a singular extension is present. */
<Type> boolean hasExtension(
- Extension<MessageType, Type> extension);
+ ExtensionLite<MessageType, Type> extension);
/** Get the number of elements in a repeated extension. */
<Type> int getExtensionCount(
- Extension<MessageType, List<Type>> extension);
+ ExtensionLite<MessageType, List<Type>> extension);
/** Get the value of an extension. */
<Type> Type getExtension(
- Extension<MessageType, Type> extension);
+ ExtensionLite<MessageType, Type> extension);
/** Get one element of a repeated extension. */
<Type> Type getExtension(
- Extension<MessageType, List<Type>> extension,
+ ExtensionLite<MessageType, List<Type>> extension,
int index);
}
@@ -625,7 +659,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Check if a singular extension is present. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> boolean hasExtension(
- final Extension<MessageType, Type> extension) {
+ final ExtensionLite<MessageType, Type> extensionLite) {
+ Extension<MessageType, Type> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
return extensions.hasField(extension.getDescriptor());
}
@@ -633,7 +669,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Get the number of elements in a repeated extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> int getExtensionCount(
- final Extension<MessageType, List<Type>> extension) {
+ final ExtensionLite<MessageType, List<Type>> extensionLite) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
final FieldDescriptor descriptor = extension.getDescriptor();
return extensions.getRepeatedFieldCount(descriptor);
@@ -643,7 +681,9 @@ public abstract class GeneratedMessage extends AbstractMessage
//@Override (Java 1.6 override semantics, but we must support 1.5)
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
- final Extension<MessageType, Type> extension) {
+ final ExtensionLite<MessageType, Type> extensionLite) {
+ Extension<MessageType, Type> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
final Object value = extensions.getField(descriptor);
@@ -666,8 +706,10 @@ public abstract class GeneratedMessage extends AbstractMessage
//@Override (Java 1.6 override semantics, but we must support 1.5)
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
- final Extension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extensionLite,
final int index) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
return (Type) extension.singularFromReflectionType(
@@ -914,11 +956,10 @@ public abstract class GeneratedMessage extends AbstractMessage
// This is implemented here only to work around an apparent bug in the
// Java compiler and/or build system. See bug #1898463. The mere presence
- // of this dummy clone() implementation makes it go away.
+ // of this clone() implementation makes it go away.
@Override
public BuilderType clone() {
- throw new UnsupportedOperationException(
- "This is supposed to be overridden by subclasses.");
+ return super.clone();
}
private void ensureExtensionsIsMutable() {
@@ -943,7 +984,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Check if a singular extension is present. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> boolean hasExtension(
- final Extension<MessageType, Type> extension) {
+ final ExtensionLite<MessageType, Type> extensionLite) {
+ Extension<MessageType, Type> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
return extensions.hasField(extension.getDescriptor());
}
@@ -951,7 +994,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Get the number of elements in a repeated extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> int getExtensionCount(
- final Extension<MessageType, List<Type>> extension) {
+ final ExtensionLite<MessageType, List<Type>> extensionLite) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
final FieldDescriptor descriptor = extension.getDescriptor();
return extensions.getRepeatedFieldCount(descriptor);
@@ -960,7 +1005,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Get the value of an extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> Type getExtension(
- final Extension<MessageType, Type> extension) {
+ final ExtensionLite<MessageType, Type> extensionLite) {
+ Extension<MessageType, Type> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
final Object value = extensions.getField(descriptor);
@@ -982,8 +1029,10 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Get one element of a repeated extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> Type getExtension(
- final Extension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extensionLite,
final int index) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
FieldDescriptor descriptor = extension.getDescriptor();
return (Type) extension.singularFromReflectionType(
@@ -992,8 +1041,10 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Set the value of an extension. */
public final <Type> BuilderType setExtension(
- final Extension<MessageType, Type> extension,
+ final ExtensionLite<MessageType, Type> extensionLite,
final Type value) {
+ Extension<MessageType, Type> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
ensureExtensionsIsMutable();
final FieldDescriptor descriptor = extension.getDescriptor();
@@ -1004,8 +1055,10 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Set the value of one element of a repeated extension. */
public final <Type> BuilderType setExtension(
- final Extension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extensionLite,
final int index, final Type value) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
ensureExtensionsIsMutable();
final FieldDescriptor descriptor = extension.getDescriptor();
@@ -1018,8 +1071,10 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Append a value to a repeated extension. */
public final <Type> BuilderType addExtension(
- final Extension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extensionLite,
final Type value) {
+ Extension<MessageType, List<Type>> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
ensureExtensionsIsMutable();
final FieldDescriptor descriptor = extension.getDescriptor();
@@ -1031,7 +1086,9 @@ public abstract class GeneratedMessage extends AbstractMessage
/** Clear an extension. */
public final <Type> BuilderType clearExtension(
- final Extension<MessageType, ?> extension) {
+ final ExtensionLite<MessageType, ?> extensionLite) {
+ Extension<MessageType, ?> extension = checkNotLite(extensionLite);
+
verifyExtensionContainingType(extension);
ensureExtensionsIsMutable();
extensions.clearField(extension.getDescriptor());
@@ -1594,6 +1651,25 @@ public abstract class GeneratedMessage extends AbstractMessage
}
}
}
+
+ /**
+ * Gets the map field with the given field number. This method should be
+ * overridden in the generated message class if the message contains map
+ * fields.
+ *
+ * Unlike other field types, reflection support for map fields can't be
+ * implemented based on generated public API because we need to access a
+ * map field as a list in reflection API but the generated API only allows
+ * us to access it as a map. This method returns the underlying map field
+ * directly and thus enables us to access the map field as a list.
+ */
+ @SuppressWarnings({"rawtypes", "unused"})
+ protected MapField internalGetMapField(int fieldNumber) {
+ // Note that we can't use descriptor names here because this method will
+ // be called when descriptor is being initialized.
+ throw new RuntimeException(
+ "No map fields found in " + getClass().getName());
+ }
/**
* Users should ignore this class. This class provides the implementation
@@ -1633,6 +1709,11 @@ public abstract class GeneratedMessage extends AbstractMessage
oneofs = new OneofAccessor[descriptor.getOneofs().size()];
initialized = false;
}
+
+ private boolean isMapFieldEnabled(FieldDescriptor field) {
+ boolean result = true;
+ return result;
+ }
/**
* Ensures the field accessors are initialized. This method is thread-safe.
@@ -1657,8 +1738,13 @@ public abstract class GeneratedMessage extends AbstractMessage
}
if (field.isRepeated()) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
- fields[i] = new RepeatedMessageFieldAccessor(
- field, camelCaseNames[i], messageClass, builderClass);
+ if (field.isMapField() && isMapFieldEnabled(field)) {
+ fields[i] = new MapFieldAccessor(
+ field, camelCaseNames[i], messageClass, builderClass);
+ } else {
+ fields[i] = new RepeatedMessageFieldAccessor(
+ field, camelCaseNames[i], messageClass, builderClass);
+ }
} else if (field.getJavaType() == FieldDescriptor.JavaType.ENUM) {
fields[i] = new RepeatedEnumFieldAccessor(
field, camelCaseNames[i], messageClass, builderClass);
@@ -1744,6 +1830,8 @@ public abstract class GeneratedMessage extends AbstractMessage
void clear(Builder builder);
Message.Builder newBuilder();
Message.Builder getBuilder(GeneratedMessage.Builder builder);
+ Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder,
+ int index);
}
/** OneofAccessor provides access to a single oneof. */
@@ -1799,9 +1887,9 @@ public abstract class GeneratedMessage extends AbstractMessage
invokeOrDie(clearMethod, builder);
}
}
-
+
private static boolean supportFieldPresence(FileDescriptor file) {
- return true;
+ return file.getSyntax() == FileDescriptor.Syntax.PROTO2;
}
// ---------------------------------------------------------------
@@ -1919,6 +2007,11 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new UnsupportedOperationException(
"getFieldBuilder() called on a non-Message type.");
}
+ public Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder,
+ int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldBuilder() called on a non-Message type.");
+ }
}
private static class RepeatedFieldAccessor implements FieldAccessor {
@@ -2014,6 +2107,113 @@ public abstract class GeneratedMessage extends AbstractMessage
throw new UnsupportedOperationException(
"getFieldBuilder() called on a non-Message type.");
}
+ public Message.Builder getRepeatedBuilder(GeneratedMessage.Builder builder,
+ int index) {
+ throw new UnsupportedOperationException(
+ "getRepeatedFieldBuilder() called on a non-Message type.");
+ }
+ }
+
+ private static class MapFieldAccessor implements FieldAccessor {
+ MapFieldAccessor(
+ final FieldDescriptor descriptor, final String camelCaseName,
+ final Class<? extends GeneratedMessage> messageClass,
+ final Class<? extends Builder> builderClass) {
+ field = descriptor;
+ Method getDefaultInstanceMethod =
+ getMethodOrDie(messageClass, "getDefaultInstance");
+ MapField defaultMapField = getMapField(
+ (GeneratedMessage) invokeOrDie(getDefaultInstanceMethod, null));
+ mapEntryMessageDefaultInstance =
+ defaultMapField.getMapEntryMessageDefaultInstance();
+ }
+
+ private final FieldDescriptor field;
+ private final Message mapEntryMessageDefaultInstance;
+
+ private MapField<?, ?> getMapField(GeneratedMessage message) {
+ return (MapField<?, ?>) message.internalGetMapField(field.getNumber());
+ }
+
+ private MapField<?, ?> getMapField(GeneratedMessage.Builder builder) {
+ return (MapField<?, ?>) builder.internalGetMapField(field.getNumber());
+ }
+
+ public Object get(GeneratedMessage message) {
+ List result = new ArrayList();
+ for (int i = 0; i < getRepeatedCount(message); i++) {
+ result.add(getRepeated(message, i));
+ }
+ return Collections.unmodifiableList(result);
+ }
+
+ public Object get(Builder builder) {
+ List result = new ArrayList();
+ for (int i = 0; i < getRepeatedCount(builder); i++) {
+ result.add(getRepeated(builder, i));
+ }
+ return Collections.unmodifiableList(result);
+ }
+
+ public void set(Builder builder, Object value) {
+ clear(builder);
+ for (Object entry : (List) value) {
+ addRepeated(builder, entry);
+ }
+ }
+
+ public Object getRepeated(GeneratedMessage message, int index) {
+ return getMapField(message).getList().get(index);
+ }
+
+ public Object getRepeated(Builder builder, int index) {
+ return getMapField(builder).getList().get(index);
+ }
+
+ public void setRepeated(Builder builder, int index, Object value) {
+ getMapField(builder).getMutableList().set(index, (Message) value);
+ }
+
+ public void addRepeated(Builder builder, Object value) {
+ getMapField(builder).getMutableList().add((Message) value);
+ }
+
+ public boolean has(GeneratedMessage message) {
+ throw new UnsupportedOperationException(
+ "hasField() is not supported for repeated fields.");
+ }
+
+ public boolean has(Builder builder) {
+ throw new UnsupportedOperationException(
+ "hasField() is not supported for repeated fields.");
+ }
+
+ public int getRepeatedCount(GeneratedMessage message) {
+ return getMapField(message).getList().size();
+ }
+
+ public int getRepeatedCount(Builder builder) {
+ return getMapField(builder).getList().size();
+ }
+
+ public void clear(Builder builder) {
+ getMapField(builder).getMutableList().clear();
+ }
+
+ public com.google.protobuf.Message.Builder newBuilder() {
+ return mapEntryMessageDefaultInstance.newBuilderForType();
+ }
+
+ public com.google.protobuf.Message.Builder getBuilder(Builder builder) {
+ throw new UnsupportedOperationException(
+ "Nested builder not supported for map fields.");
+ }
+
+ public com.google.protobuf.Message.Builder getRepeatedBuilder(
+ Builder builder, int index) {
+ throw new UnsupportedOperationException(
+ "Nested builder not supported for map fields.");
+ }
}
// ---------------------------------------------------------------
@@ -2026,28 +2226,60 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends Builder> builderClass,
final String containingOneofCamelCaseName) {
super(descriptor, camelCaseName, messageClass, builderClass, containingOneofCamelCaseName);
+
+ enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf",
EnumValueDescriptor.class);
getValueDescriptorMethod =
getMethodOrDie(type, "getValueDescriptor");
+
+ supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue();
+ if (supportUnknownEnumValue) {
+ getValueMethod =
+ getMethodOrDie(messageClass, "get" + camelCaseName + "Value");
+ getValueMethodBuilder =
+ getMethodOrDie(builderClass, "get" + camelCaseName + "Value");
+ setValueMethod =
+ getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class);
+ }
}
+
+ private EnumDescriptor enumDescriptor;
private Method valueOfMethod;
private Method getValueDescriptorMethod;
+
+ private boolean supportUnknownEnumValue;
+ private Method getValueMethod;
+ private Method getValueMethodBuilder;
+ private Method setValueMethod;
@Override
public Object get(final GeneratedMessage message) {
+ if (supportUnknownEnumValue) {
+ int value = (Integer) invokeOrDie(getValueMethod, message);
+ return enumDescriptor.findValueByNumberCreatingIfUnknown(value);
+ }
return invokeOrDie(getValueDescriptorMethod, super.get(message));
}
@Override
public Object get(final GeneratedMessage.Builder builder) {
+ if (supportUnknownEnumValue) {
+ int value = (Integer) invokeOrDie(getValueMethodBuilder, builder);
+ return enumDescriptor.findValueByNumberCreatingIfUnknown(value);
+ }
return invokeOrDie(getValueDescriptorMethod, super.get(builder));
}
@Override
public void set(final Builder builder, final Object value) {
+ if (supportUnknownEnumValue) {
+ invokeOrDie(setValueMethod, builder,
+ ((EnumValueDescriptor) value).getNumber());
+ return;
+ }
super.set(builder, invokeOrDie(valueOfMethod, null, value));
}
}
@@ -2059,22 +2291,44 @@ public abstract class GeneratedMessage extends AbstractMessage
final Class<? extends GeneratedMessage> messageClass,
final Class<? extends Builder> builderClass) {
super(descriptor, camelCaseName, messageClass, builderClass);
+
+ enumDescriptor = descriptor.getEnumType();
valueOfMethod = getMethodOrDie(type, "valueOf",
EnumValueDescriptor.class);
getValueDescriptorMethod =
getMethodOrDie(type, "getValueDescriptor");
+
+ supportUnknownEnumValue = descriptor.getFile().supportsUnknownEnumValue();
+ if (supportUnknownEnumValue) {
+ getRepeatedValueMethod =
+ getMethodOrDie(messageClass, "get" + camelCaseName + "Value", int.class);
+ getRepeatedValueMethodBuilder =
+ getMethodOrDie(builderClass, "get" + camelCaseName + "Value", int.class);
+ setRepeatedValueMethod =
+ getMethodOrDie(builderClass, "set" + camelCaseName + "Value", int.class, int.class);
+ addRepeatedValueMethod =
+ getMethodOrDie(builderClass, "add" + camelCaseName + "Value", int.class);
+ }
}
+ private EnumDescriptor enumDescriptor;
private final Method valueOfMethod;
private final Method getValueDescriptorMethod;
+
+ private boolean supportUnknownEnumValue;
+ private Method getRepeatedValueMethod;
+ private Method getRepeatedValueMethodBuilder;
+ private Method setRepeatedValueMethod;
+ private Method addRepeatedValueMethod;
@Override
@SuppressWarnings("unchecked")
public Object get(final GeneratedMessage message) {
final List newList = new ArrayList();
- for (final Object element : (List) super.get(message)) {
- newList.add(invokeOrDie(getValueDescriptorMethod, element));
+ final int size = getRepeatedCount(message);
+ for (int i = 0; i < size; i++) {
+ newList.add(getRepeated(message, i));
}
return Collections.unmodifiableList(newList);
}
@@ -2083,8 +2337,9 @@ public abstract class GeneratedMessage extends AbstractMessage
@SuppressWarnings("unchecked")
public Object get(final GeneratedMessage.Builder builder) {
final List newList = new ArrayList();
- for (final Object element : (List) super.get(builder)) {
- newList.add(invokeOrDie(getValueDescriptorMethod, element));
+ final int size = getRepeatedCount(builder);
+ for (int i = 0; i < size; i++) {
+ newList.add(getRepeated(builder, i));
}
return Collections.unmodifiableList(newList);
}
@@ -2092,23 +2347,41 @@ public abstract class GeneratedMessage extends AbstractMessage
@Override
public Object getRepeated(final GeneratedMessage message,
final int index) {
+ if (supportUnknownEnumValue) {
+ int value = (Integer) invokeOrDie(getRepeatedValueMethod, message, index);
+ return enumDescriptor.findValueByNumberCreatingIfUnknown(value);
+ }
return invokeOrDie(getValueDescriptorMethod,
super.getRepeated(message, index));
}
@Override
public Object getRepeated(final GeneratedMessage.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));
}
@Override
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));
}
@Override
public void addRepeated(final Builder builder, final Object value) {
+ if (supportUnknownEnumValue) {
+ invokeOrDie(addRepeatedValueMethod, builder,
+ ((EnumValueDescriptor) value).getNumber());
+ return;
+ }
super.addRepeated(builder, invokeOrDie(valueOfMethod, null, value));
}
}
@@ -2168,9 +2441,12 @@ public abstract class GeneratedMessage extends AbstractMessage
super(descriptor, camelCaseName, messageClass, builderClass);
newBuilderMethod = getMethodOrDie(type, "newBuilder");
+ getBuilderMethodBuilder = getMethodOrDie(builderClass,
+ "get" + camelCaseName + "Builder", Integer.TYPE);
}
private final Method newBuilderMethod;
+ private final Method getBuilderMethodBuilder;
private Object coerceType(final Object value) {
if (type.isInstance(value)) {
@@ -2198,6 +2474,12 @@ public abstract class GeneratedMessage extends AbstractMessage
public Message.Builder newBuilder() {
return (Message.Builder) invokeOrDie(newBuilderMethod, null);
}
+ @Override
+ public Message.Builder getRepeatedBuilder(
+ final GeneratedMessage.Builder builder, final int index) {
+ return (Message.Builder) invokeOrDie(
+ getBuilderMethodBuilder, builder, index);
+ }
}
}
@@ -2210,4 +2492,18 @@ public abstract class GeneratedMessage extends AbstractMessage
protected Object writeReplace() throws ObjectStreamException {
return new GeneratedMessageLite.SerializedForm(this);
}
+
+ /**
+ * Checks that the {@link Extension} is non-Lite and returns it as a
+ * {@link GeneratedExtension}.
+ */
+ private static <MessageType extends ExtendableMessage<MessageType>, T>
+ Extension<MessageType, T> checkNotLite(
+ ExtensionLite<MessageType, T> extension) {
+ if (extension.isLite()) {
+ throw new IllegalArgumentException("Expected non-lite extension.");
+ }
+
+ return (Extension<MessageType, T>) extension;
+ }
}
diff --git a/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java b/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
index 6c5136fd..4d25c077 100644
--- a/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
+++ b/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java
@@ -30,6 +30,8 @@
package com.google.protobuf;
+import com.google.protobuf.WireFormat.FieldType;
+
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.Serializable;
@@ -50,62 +52,74 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
implements Serializable {
private static final long serialVersionUID = 1L;
- protected GeneratedMessageLite() {
- }
-
- protected GeneratedMessageLite(Builder builder) {
- }
-
+ /** For use by generated code only. */
+ protected UnknownFieldSetLite unknownFields;
+
public Parser<? extends MessageLite> getParserForType() {
throw new UnsupportedOperationException(
"This is supposed to be overridden by subclasses.");
}
/**
- * Called by subclasses to parse an unknown field.
+ * Called by subclasses to parse an unknown field. For use by generated code
+ * only.
* @return {@code true} unless the tag is an end-group tag.
*/
- protected boolean parseUnknownField(
+ protected static boolean parseUnknownField(
CodedInputStream input,
- CodedOutputStream unknownFieldsCodedOutput,
+ UnknownFieldSetLite.Builder unknownFields,
ExtensionRegistryLite extensionRegistry,
int tag) throws IOException {
- return input.skipField(tag, unknownFieldsCodedOutput);
- }
-
- /**
- * Used by parsing constructors in generated classes.
- */
- protected void makeExtensionsImmutable() {
- // Noop for messages without extensions.
+ return unknownFields.mergeFieldFrom(tag, input);
}
@SuppressWarnings("unchecked")
public abstract static class Builder<MessageType extends GeneratedMessageLite,
BuilderType extends Builder>
extends AbstractMessageLite.Builder<BuilderType> {
- protected Builder() {}
+
+ private final MessageType defaultInstance;
+
+ /** For use by generated code only. */
+ protected UnknownFieldSetLite unknownFields =
+ UnknownFieldSetLite.getDefaultInstance();
+
+ protected Builder(MessageType defaultInstance) {
+ this.defaultInstance = defaultInstance;
+ }
//@Override (Java 1.6 override semantics, but we must support 1.5)
public BuilderType clear() {
- unknownFields = ByteString.EMPTY;
+ unknownFields = UnknownFieldSetLite.getDefaultInstance();
return (BuilderType) this;
}
- // This is implemented here only to work around an apparent bug in the
- // Java compiler and/or build system. See bug #1898463. The mere presence
- // of this dummy clone() implementation makes it go away.
- @Override
+ //@Override (Java 1.6 override semantics, but we must support 1.5)
public BuilderType clone() {
- throw new UnsupportedOperationException(
- "This is supposed to be overridden by subclasses.");
+ BuilderType builder =
+ (BuilderType) getDefaultInstanceForType().newBuilderForType();
+ builder.mergeFrom(buildPartial());
+ return builder;
}
/** All subclasses implement this. */
- public abstract BuilderType mergeFrom(MessageType message);
+ public abstract MessageType buildPartial();
+
+ //@Override (Java 1.6 override semantics, but we must support 1.5)
+ public final MessageType build() {
+ MessageType result = buildPartial();
+ if (!result.isInitialized()) {
+ throw newUninitializedMessageException(result);
+ }
+ return result;
+ }
- // Defined here for return type covariance.
- public abstract MessageType getDefaultInstanceForType();
+ /** All subclasses implement this. */
+ public abstract BuilderType mergeFrom(MessageType message);
+
+ public MessageType getDefaultInstanceForType() {
+ return defaultInstance;
+ }
/**
* Called by subclasses to parse an unknown field.
@@ -113,22 +127,43 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
*/
protected boolean parseUnknownField(
CodedInputStream input,
- CodedOutputStream unknownFieldsCodedOutput,
+ UnknownFieldSetLite.Builder unknownFields,
ExtensionRegistryLite extensionRegistry,
int tag) throws IOException {
- return input.skipField(tag, unknownFieldsCodedOutput);
+ return unknownFields.mergeFieldFrom(tag, input);
}
- public final ByteString getUnknownFields() {
- return unknownFields;
+ /**
+ * Merge some unknown fields into the {@link UnknownFieldSetLite} for this
+ * message.
+ *
+ * <p>For use by generated code only.
+ */
+ protected final BuilderType mergeUnknownFields(
+ final UnknownFieldSetLite unknownFields) {
+ this.unknownFields = UnknownFieldSetLite.concat(this.unknownFields, unknownFields);
+ return (BuilderType) this;
}
-
- public final BuilderType setUnknownFields(final ByteString unknownFields) {
- this.unknownFields = unknownFields;
+
+ public BuilderType mergeFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ MessageType parsedMessage = null;
+ try {
+ parsedMessage =
+ (MessageType) getDefaultInstanceForType().getParserForType().parsePartialFrom(
+ input, extensionRegistry);
+ } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+ parsedMessage = (MessageType) e.getUnfinishedMessage();
+ throw e;
+ } finally {
+ if (parsedMessage != null) {
+ mergeFrom(parsedMessage);
+ }
+ }
return (BuilderType) this;
}
-
- private ByteString unknownFields = ByteString.EMPTY;
}
@@ -143,18 +178,18 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
/** Check if a singular extension is present. */
<Type> boolean hasExtension(
- GeneratedExtension<MessageType, Type> extension);
+ ExtensionLite<MessageType, Type> extension);
/** Get the number of elements in a repeated extension. */
<Type> int getExtensionCount(
- GeneratedExtension<MessageType, List<Type>> extension);
+ ExtensionLite<MessageType, List<Type>> extension);
/** Get the value of an extension. */
- <Type> Type getExtension(GeneratedExtension<MessageType, Type> extension);
+ <Type> Type getExtension(ExtensionLite<MessageType, Type> extension);
/** Get one element of a repeated extension. */
<Type> Type getExtension(
- GeneratedExtension<MessageType, List<Type>> extension,
+ ExtensionLite<MessageType, List<Type>> extension,
int index);
}
@@ -166,16 +201,12 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
extends GeneratedMessageLite
implements ExtendableMessageOrBuilder<MessageType> {
- private final FieldSet<ExtensionDescriptor> extensions;
-
- protected ExtendableMessage() {
- this.extensions = FieldSet.newFieldSet();
- }
-
- protected ExtendableMessage(ExtendableBuilder<MessageType, ?> builder) {
- this.extensions = builder.buildExtensions();
- }
-
+ /**
+ * Represents the set of extensions on this message. For use by generated
+ * code only.
+ */
+ protected FieldSet<ExtensionDescriptor> extensions = FieldSet.newFieldSet();
+
private void verifyExtensionContainingType(
final GeneratedExtension<MessageType, ?> extension) {
if (extension.getContainingTypeDefaultInstance() !=
@@ -190,30 +221,39 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
/** Check if a singular extension is present. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> boolean hasExtension(
- final GeneratedExtension<MessageType, Type> extension) {
- verifyExtensionContainingType(extension);
- return extensions.hasField(extension.descriptor);
+ final ExtensionLite<MessageType, Type> extension) {
+ GeneratedExtension<MessageType, Type> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return extensions.hasField(extensionLite.descriptor);
}
/** Get the number of elements in a repeated extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> int getExtensionCount(
- final GeneratedExtension<MessageType, List<Type>> extension) {
- verifyExtensionContainingType(extension);
- return extensions.getRepeatedFieldCount(extension.descriptor);
+ final ExtensionLite<MessageType, List<Type>> extension) {
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return extensions.getRepeatedFieldCount(extensionLite.descriptor);
}
/** Get the value of an extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
- final GeneratedExtension<MessageType, Type> extension) {
- verifyExtensionContainingType(extension);
- final Object value = extensions.getField(extension.descriptor);
+ final ExtensionLite<MessageType, Type> extension) {
+ GeneratedExtension<MessageType, Type> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ final Object value = extensions.getField(extensionLite.descriptor);
if (value == null) {
- return extension.defaultValue;
+ return extensionLite.defaultValue;
} else {
- return (Type) extension.fromFieldSetType(value);
+ return (Type) extensionLite.fromFieldSetType(value);
}
}
@@ -221,11 +261,14 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
//@Override (Java 1.6 override semantics, but we must support 1.5)
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
- final GeneratedExtension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extension,
final int index) {
- verifyExtensionContainingType(extension);
- return (Type) extension.singularFromFieldSetType(
- extensions.getRepeatedField(extension.descriptor, index));
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return (Type) extensionLite.singularFromFieldSetType(
+ extensions.getRepeatedField(extensionLite.descriptor, index));
}
/** Called by subclasses to check if all extensions are initialized. */
@@ -233,31 +276,12 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
return extensions.isInitialized();
}
- /**
- * Called by subclasses to parse an unknown field or an extension.
- * @return {@code true} unless the tag is an end-group tag.
- */
- @Override
- protected boolean parseUnknownField(
- CodedInputStream input,
- CodedOutputStream unknownFieldsCodedOutput,
- ExtensionRegistryLite extensionRegistry,
- int tag) throws IOException {
- return GeneratedMessageLite.parseUnknownField(
- extensions,
- getDefaultInstanceForType(),
- input,
- unknownFieldsCodedOutput,
- extensionRegistry,
- tag);
- }
-
/**
* Used by parsing constructors in generated classes.
*/
- @Override
- protected void makeExtensionsImmutable() {
+ protected static void makeExtensionsImmutable(
+ FieldSet<ExtensionDescriptor> extensions) {
extensions.makeImmutable();
}
@@ -329,7 +353,9 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
BuilderType extends ExtendableBuilder<MessageType, BuilderType>>
extends Builder<MessageType, BuilderType>
implements ExtendableMessageOrBuilder<MessageType> {
- protected ExtendableBuilder() {}
+ protected ExtendableBuilder(MessageType defaultInstance) {
+ super(defaultInstance);
+ }
private FieldSet<ExtensionDescriptor> extensions = FieldSet.emptySet();
private boolean extensionsIsMutable;
@@ -356,8 +382,10 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
/**
* Called by the build code path to create a copy of the extensions for
* building the message.
+ * <p>
+ * For use by generated code only.
*/
- private FieldSet<ExtensionDescriptor> buildExtensions() {
+ protected final FieldSet<ExtensionDescriptor> buildExtensions() {
extensions.makeImmutable();
extensionsIsMutable = false;
return extensions;
@@ -377,30 +405,39 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
/** Check if a singular extension is present. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> boolean hasExtension(
- final GeneratedExtension<MessageType, Type> extension) {
- verifyExtensionContainingType(extension);
- return extensions.hasField(extension.descriptor);
+ final ExtensionLite<MessageType, Type> extension) {
+ GeneratedExtension<MessageType, Type> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return extensions.hasField(extensionLite.descriptor);
}
/** Get the number of elements in a repeated extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> int getExtensionCount(
- final GeneratedExtension<MessageType, List<Type>> extension) {
- verifyExtensionContainingType(extension);
- return extensions.getRepeatedFieldCount(extension.descriptor);
+ final ExtensionLite<MessageType, List<Type>> extension) {
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return extensions.getRepeatedFieldCount(extensionLite.descriptor);
}
/** Get the value of an extension. */
//@Override (Java 1.6 override semantics, but we must support 1.5)
@SuppressWarnings("unchecked")
public final <Type> Type getExtension(
- final GeneratedExtension<MessageType, Type> extension) {
- verifyExtensionContainingType(extension);
- final Object value = extensions.getField(extension.descriptor);
+ final ExtensionLite<MessageType, Type> extension) {
+ GeneratedExtension<MessageType, Type> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ final Object value = extensions.getField(extensionLite.descriptor);
if (value == null) {
- return extension.defaultValue;
+ return extensionLite.defaultValue;
} else {
- return (Type) extension.fromFieldSetType(value);
+ return (Type) extensionLite.fromFieldSetType(value);
}
}
@@ -408,11 +445,14 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
@SuppressWarnings("unchecked")
//@Override (Java 1.6 override semantics, but we must support 1.5)
public final <Type> Type getExtension(
- final GeneratedExtension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extension,
final int index) {
- verifyExtensionContainingType(extension);
- return (Type) extension.singularFromFieldSetType(
- extensions.getRepeatedField(extension.descriptor, index));
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
+ return (Type) extensionLite.singularFromFieldSetType(
+ extensions.getRepeatedField(extensionLite.descriptor, index));
}
// This is implemented here only to work around an apparent bug in the
@@ -420,49 +460,59 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
// of this dummy clone() implementation makes it go away.
@Override
public BuilderType clone() {
- throw new UnsupportedOperationException(
- "This is supposed to be overridden by subclasses.");
+ return super.clone();
}
-
+
/** Set the value of an extension. */
public final <Type> BuilderType setExtension(
- final GeneratedExtension<MessageType, Type> extension,
+ final ExtensionLite<MessageType, Type> extension,
final Type value) {
- verifyExtensionContainingType(extension);
+ GeneratedExtension<MessageType, Type> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
ensureExtensionsIsMutable();
- extensions.setField(extension.descriptor,
- extension.toFieldSetType(value));
+ extensions.setField(extensionLite.descriptor,
+ extensionLite.toFieldSetType(value));
return (BuilderType) this;
}
/** Set the value of one element of a repeated extension. */
public final <Type> BuilderType setExtension(
- final GeneratedExtension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extension,
final int index, final Type value) {
- verifyExtensionContainingType(extension);
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
ensureExtensionsIsMutable();
- extensions.setRepeatedField(extension.descriptor, index,
- extension.singularToFieldSetType(value));
+ extensions.setRepeatedField(extensionLite.descriptor, index,
+ extensionLite.singularToFieldSetType(value));
return (BuilderType) this;
}
/** Append a value to a repeated extension. */
public final <Type> BuilderType addExtension(
- final GeneratedExtension<MessageType, List<Type>> extension,
+ final ExtensionLite<MessageType, List<Type>> extension,
final Type value) {
- verifyExtensionContainingType(extension);
+ GeneratedExtension<MessageType, List<Type>> extensionLite =
+ checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
ensureExtensionsIsMutable();
- extensions.addRepeatedField(extension.descriptor,
- extension.singularToFieldSetType(value));
+ extensions.addRepeatedField(extensionLite.descriptor,
+ extensionLite.singularToFieldSetType(value));
return (BuilderType) this;
}
/** Clear an extension. */
public final <Type> BuilderType clearExtension(
- final GeneratedExtension<MessageType, ?> extension) {
- verifyExtensionContainingType(extension);
+ final ExtensionLite<MessageType, ?> extension) {
+ GeneratedExtension<MessageType, ?> extensionLite = checkIsLite(extension);
+
+ verifyExtensionContainingType(extensionLite);
ensureExtensionsIsMutable();
- extensions.clearField(extension.descriptor);
+ extensions.clearField(extensionLite.descriptor);
return (BuilderType) this;
}
@@ -471,44 +521,24 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
return extensions.isInitialized();
}
- /**
- * Called by subclasses to parse an unknown field or an extension.
- * @return {@code true} unless the tag is an end-group tag.
- */
- @Override
- protected boolean parseUnknownField(
- CodedInputStream input,
- CodedOutputStream unknownFieldsCodedOutput,
- ExtensionRegistryLite extensionRegistry,
- int tag) throws IOException {
- ensureExtensionsIsMutable();
- return GeneratedMessageLite.parseUnknownField(
- extensions,
- getDefaultInstanceForType(),
- input,
- unknownFieldsCodedOutput,
- extensionRegistry,
- tag);
- }
-
protected final void mergeExtensionFields(final MessageType other) {
ensureExtensionsIsMutable();
extensions.mergeFrom(((ExtendableMessage) other).extensions);
}
}
- // -----------------------------------------------------------------
+ //-----------------------------------------------------------------
/**
- * Parse an unknown field or an extension.
+ * Parse an unknown field or an extension. For use by generated code only.
* @return {@code true} unless the tag is an end-group tag.
*/
- private static <MessageType extends MessageLite>
+ protected static <MessageType extends MessageLite>
boolean parseUnknownField(
FieldSet<ExtensionDescriptor> extensions,
MessageType defaultInstance,
CodedInputStream input,
- CodedOutputStream unknownFieldsCodedOutput,
+ UnknownFieldSetLite.Builder unknownFields,
ExtensionRegistryLite extensionRegistry,
int tag) throws IOException {
int wireType = WireFormat.getTagWireType(tag);
@@ -537,7 +567,7 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
}
if (unknown) { // Unknown field or wrong wire type. Skip.
- return input.skipField(tag, unknownFieldsCodedOutput);
+ return unknownFields.mergeFieldFrom(tag, input);
}
if (packed) {
@@ -599,8 +629,7 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
// If the number isn't recognized as a valid value for this enum,
// write it to unknown fields object.
if (value == null) {
- unknownFieldsCodedOutput.writeRawVarint32(tag);
- unknownFieldsCodedOutput.writeUInt32NoTag(rawValue);
+ unknownFields.mergeVarintField(fieldNumber, rawValue);
return true;
}
break;
@@ -768,7 +797,8 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
* this type as parameters to extension accessors and ExtensionRegistry.add().
*/
public static class GeneratedExtension<
- ContainingType extends MessageLite, Type> {
+ ContainingType extends MessageLite, Type>
+ extends ExtensionLite<ContainingType, Type> {
/**
* Create a new isntance with the given parameters.
@@ -888,6 +918,18 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
return value;
}
}
+
+ public FieldType getLiteType() {
+ return descriptor.getLiteType();
+ }
+
+ public boolean isRepeated() {
+ return descriptor.isRepeated;
+ }
+
+ public Type getDefaultValue() {
+ return defaultValue;
+ }
}
/**
@@ -897,8 +939,8 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
static final class SerializedForm implements Serializable {
private static final long serialVersionUID = 0L;
- private String messageClassName;
- private byte[] asBytes;
+ private final String messageClassName;
+ private final byte[] asBytes;
/**
* Creates the serialized form by calling {@link com.google.protobuf.MessageLite#toByteArray}.
@@ -918,19 +960,17 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
protected Object readResolve() throws ObjectStreamException {
try {
Class messageClass = Class.forName(messageClassName);
- Method newBuilder = messageClass.getMethod("newBuilder");
- MessageLite.Builder builder =
- (MessageLite.Builder) newBuilder.invoke(null);
- builder.mergeFrom(asBytes);
- return builder.buildPartial();
+ Parser<?> parser =
+ (Parser<?>) messageClass.getField("PARSER").get(null);
+ return parser.parsePartialFrom(asBytes);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Unable to find proto buffer class", e);
- } catch (NoSuchMethodException e) {
- throw new RuntimeException("Unable to find newBuilder method", e);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException("Unable to find PARSER", e);
+ } catch (SecurityException e) {
+ throw new RuntimeException("Unable to call PARSER", e);
} catch (IllegalAccessException e) {
- throw new RuntimeException("Unable to call newBuilder method", e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException("Error calling newBuilder", e.getCause());
+ throw new RuntimeException("Unable to call parseFrom method", e);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Unable to understand proto buffer", e);
}
@@ -946,4 +986,18 @@ public abstract class GeneratedMessageLite extends AbstractMessageLite
protected Object writeReplace() throws ObjectStreamException {
return new SerializedForm(this);
}
+
+ /**
+ * Checks that the {@link Extension} is Lite and returns it as a
+ * {@link GeneratedExtension}.
+ */
+ private static <MessageType extends ExtendableMessage<MessageType>, T>
+ GeneratedExtension<MessageType, T> checkIsLite(
+ ExtensionLite<MessageType, T> extension) {
+ if (!extension.isLite()) {
+ throw new IllegalArgumentException("Expected a lite extension.");
+ }
+
+ return (GeneratedExtension<MessageType, T>) extension;
+ }
}
diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java
index 48d29e69..5a0de6d1 100644
--- a/java/src/main/java/com/google/protobuf/Internal.java
+++ b/java/src/main/java/com/google/protobuf/Internal.java
@@ -33,8 +33,14 @@ package com.google.protobuf;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.util.AbstractList;
+import java.util.AbstractMap;
+import java.util.AbstractSet;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* The classes contained within are used internally by the Protocol Buffer
@@ -230,7 +236,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for longs.
+ * Helper method for implementing {@link Message#hashCode()} for longs.
* @see Long#hashCode()
*/
public static int hashLong(long n) {
@@ -238,7 +244,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for
+ * Helper method for implementing {@link Message#hashCode()} for
* booleans.
* @see Boolean#hashCode()
*/
@@ -247,7 +253,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for enums.
+ * Helper method for implementing {@link Message#hashCode()} for enums.
* <p>
* 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
@@ -258,7 +264,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for
+ * Helper method for implementing {@link Message#hashCode()} for
* enum lists.
*/
public static int hashEnumList(List<? extends EnumLite> list) {
@@ -270,7 +276,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#equals()} for bytes field.
+ * Helper method for implementing {@link Message#equals(Object)} for bytes field.
*/
public static boolean equals(List<byte[]> a, List<byte[]> b) {
if (a.size() != b.size()) return false;
@@ -283,7 +289,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for bytes field.
+ * Helper method for implementing {@link Message#hashCode()} for bytes field.
*/
public static int hashCode(List<byte[]> list) {
int hash = 1;
@@ -294,7 +300,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#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
@@ -305,7 +311,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#equals()} for bytes
+ * Helper method for implementing {@link Message#equals(Object)} for bytes
* field.
*/
public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) {
@@ -318,7 +324,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#equals()} for bytes
+ * Helper method for implementing {@link Message#equals(Object)} for bytes
* field.
*/
public static boolean equalsByteBuffer(
@@ -335,7 +341,7 @@ public class Internal {
}
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for bytes
+ * Helper method for implementing {@link Message#hashCode()} for bytes
* field.
*/
public static int hashCodeByteBuffer(List<ByteBuffer> list) {
@@ -349,7 +355,7 @@ public class Internal {
private static final int DEFAULT_BUFFER_SIZE = 4096;
/**
- * Helper method for implementing {@link MessageLite#hashCode()} for bytes
+ * Helper method for implementing {@link Message#hashCode()} for bytes
* field.
*/
public static int hashCodeByteBuffer(ByteBuffer bytes) {
@@ -387,5 +393,169 @@ public class Internal {
*/
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 =
+ CodedInputStream.newInstance(EMPTY_BYTE_ARRAY);
+
+
+ /**
+ * Provides an immutable view of List<T> around a List<F>.
+ *
+ * Protobuf internal. Used in protobuf generated code only.
+ */
+ public static class ListAdapter<F, T> extends AbstractList<T> {
+ /**
+ * Convert individual elements of the List from F to T.
+ */
+ public interface Converter<F, T> {
+ T convert(F from);
+ }
+
+ private final List<F> fromList;
+ private final Converter<F, T> converter;
+
+ public ListAdapter(List<F> fromList, Converter<F, T> converter) {
+ this.fromList = fromList;
+ this.converter = converter;
+ }
+
+ @Override
+ public T get(int index) {
+ return converter.convert(fromList.get(index));
+ }
+ @Override
+ public int size() {
+ return fromList.size();
+ }
+ }
+
+ /**
+ * Wrap around a Map<K, RealValue> and provide a Map<K, V> interface.
+ */
+ public static class MapAdapter<K, V, RealValue> extends AbstractMap<K, V> {
+ /**
+ * An interface used to convert between two types.
+ */
+ public interface Converter<A, B> {
+ B doForward(A object);
+ A doBackward(B object);
+ }
+
+ public static <T extends EnumLite> Converter<Integer, T> newEnumConverter(
+ final EnumLiteMap<T> enumMap, final T unrecognizedValue) {
+ return new Converter<Integer, T>() {
+ public T doForward(Integer value) {
+ T result = enumMap.findValueByNumber(value);
+ return result == null ? unrecognizedValue : result;
+ }
+ public Integer doBackward(T value) {
+ return value.getNumber();
+ }
+ };
+ }
+
+ private final Map<K, RealValue> realMap;
+ private final Converter<RealValue, V> valueConverter;
+
+ public MapAdapter(Map<K, RealValue> realMap,
+ Converter<RealValue, V> valueConverter) {
+ this.realMap = realMap;
+ this.valueConverter = valueConverter;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V get(Object key) {
+ RealValue result = realMap.get(key);
+ if (result == null) {
+ return null;
+ }
+ return valueConverter.doForward(result);
+ }
+
+ @Override
+ public V put(K key, V value) {
+ RealValue oldValue = realMap.put(key, valueConverter.doBackward(value));
+ if (oldValue == null) {
+ return null;
+ }
+ return valueConverter.doForward(oldValue);
+ }
+
+ @Override
+ public Set<java.util.Map.Entry<K, V>> entrySet() {
+ return new SetAdapter(realMap.entrySet());
+ }
+
+ private class SetAdapter extends AbstractSet<Map.Entry<K, V>> {
+ private final Set<Map.Entry<K, RealValue>> realSet;
+ public SetAdapter(Set<Map.Entry<K, RealValue>> realSet) {
+ this.realSet = realSet;
+ }
+
+ @Override
+ public Iterator<java.util.Map.Entry<K, V>> iterator() {
+ return new IteratorAdapter(realSet.iterator());
+ }
+
+ @Override
+ public int size() {
+ return realSet.size();
+ }
+ }
+
+ private class IteratorAdapter implements Iterator<Map.Entry<K, V>> {
+ private final Iterator<Map.Entry<K, RealValue>> realIterator;
+
+ public IteratorAdapter(
+ Iterator<Map.Entry<K, RealValue>> realIterator) {
+ this.realIterator = realIterator;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return realIterator.hasNext();
+ }
+
+ @Override
+ public java.util.Map.Entry<K, V> next() {
+ return new EntryAdapter(realIterator.next());
+ }
+
+ @Override
+ public void remove() {
+ realIterator.remove();
+ }
+ }
+
+ private class EntryAdapter implements Map.Entry<K, V> {
+ private final Map.Entry<K, RealValue> realEntry;
+
+ public EntryAdapter(Map.Entry<K, RealValue> realEntry) {
+ this.realEntry = realEntry;
+ }
+
+ @Override
+ public K getKey() {
+ return realEntry.getKey();
+ }
+
+ @Override
+ public V getValue() {
+ return valueConverter.doForward(realEntry.getValue());
+ }
+
+ @Override
+ public V setValue(V value) {
+ RealValue oldValue = realEntry.setValue(
+ valueConverter.doBackward(value));
+ if (oldValue == null) {
+ return null;
+ }
+ return valueConverter.doForward(oldValue);
+ }
+ }
+ }
}
diff --git a/java/src/main/java/com/google/protobuf/LazyStringArrayList.java b/java/src/main/java/com/google/protobuf/LazyStringArrayList.java
index 61c7e1ea..2d40a51f 100644
--- a/java/src/main/java/com/google/protobuf/LazyStringArrayList.java
+++ b/java/src/main/java/com/google/protobuf/LazyStringArrayList.java
@@ -74,6 +74,10 @@ public class LazyStringArrayList extends AbstractList<String>
list = new ArrayList<Object>();
}
+ public LazyStringArrayList(int intialCapacity) {
+ list = new ArrayList<Object>(intialCapacity);
+ }
+
public LazyStringArrayList(LazyStringList from) {
list = new ArrayList<Object>(from.size());
addAll(from);
diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java
index 83e71e93..767b9f35 100644
--- a/java/src/main/java/com/google/protobuf/LiteralByteString.java
+++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java
@@ -51,6 +51,8 @@ import java.util.NoSuchElementException;
*/
class LiteralByteString extends ByteString {
+ private static final long serialVersionUID = 1L;
+
protected final byte[] bytes;
/**
@@ -190,6 +192,15 @@ class LiteralByteString extends ByteString {
}
if (other instanceof LiteralByteString) {
+ LiteralByteString otherAsLiteral = (LiteralByteString) other;
+ // If we know the hash codes and they are not equal, we know the byte
+ // strings are not equal.
+ if (hash != 0
+ && otherAsLiteral.hash != 0
+ && hash != otherAsLiteral.hash) {
+ return false;
+ }
+
return equalsRange((LiteralByteString) other, 0, size());
} else if (other instanceof RopeByteString) {
return other.equals(this);
diff --git a/java/src/main/java/com/google/protobuf/MapEntry.java b/java/src/main/java/com/google/protobuf/MapEntry.java
new file mode 100644
index 00000000..31414bb4
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/MapEntry.java
@@ -0,0 +1,433 @@
+// 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.Descriptor;
+import com.google.protobuf.Descriptors.EnumValueDescriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+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.
+ *
+ * Protobuf internal. Users shouldn't use this class.
+ */
+public final class MapEntry<K, V> extends AbstractMessage {
+ private static class Metadata<K, V> {
+ public final Descriptor descriptor;
+ public final MapEntry<K, V> defaultInstance;
+ public final AbstractParser<MapEntry<K, V>> parser;
+
+ public Metadata(
+ final Descriptor descriptor, final MapEntry<K, V> defaultInstance) {
+ this.descriptor = descriptor;
+ this.defaultInstance = defaultInstance;
+ final Metadata<K, V> thisMetadata = this;
+ this.parser = new AbstractParser<MapEntry<K, V>>() {
+ private final Parser<MapEntryLite<K, V>> dataParser =
+ defaultInstance.data.getParserForType();
+ @Override
+ public MapEntry<K, V> parsePartialFrom(
+ CodedInputStream input, ExtensionRegistryLite extensionRegistry)
+ throws InvalidProtocolBufferException {
+ MapEntryLite<K, V> data =
+ dataParser.parsePartialFrom(input, extensionRegistry);
+ return new MapEntry<K, V>(thisMetadata, data);
+ }
+
+ };
+ }
+ }
+
+ private final Metadata<K, V> metadata;
+ private final MapEntryLite<K, V> data;
+
+ /** Create a default MapEntry instance. */
+ private MapEntry(Descriptor descriptor,
+ WireFormat.FieldType keyType, K defaultKey,
+ WireFormat.FieldType valueType, V defaultValue) {
+ this.data = MapEntryLite.newDefaultInstance(
+ keyType, defaultKey, valueType, defaultValue);
+ this.metadata = new Metadata<K, V>(descriptor, this);
+ }
+
+ /** Create a new MapEntry message. */
+ private MapEntry(Metadata<K, V> metadata, MapEntryLite<K, V> data) {
+ this.metadata = metadata;
+ this.data = data;
+ }
+
+ /**
+ * 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 <K, V> MapEntry<K, V> newDefaultInstance(
+ Descriptor descriptor,
+ WireFormat.FieldType keyType, K defaultKey,
+ WireFormat.FieldType valueType, V defaultValue) {
+ return new MapEntry<K, V>(
+ descriptor, keyType, defaultKey, valueType, defaultValue);
+ }
+
+ public K getKey() {
+ return data.getKey();
+ }
+
+ public V getValue() {
+ return data.getValue();
+ }
+
+ @Override
+ public int getSerializedSize() {
+ return data.getSerializedSize();
+ }
+
+ @Override
+ public void writeTo(CodedOutputStream output) throws IOException {
+ data.writeTo(output);
+ }
+
+ @Override
+ public boolean isInitialized() {
+ return data.isInitialized();
+ }
+
+ @Override
+ public Parser<MapEntry<K, V>> getParserForType() {
+ return metadata.parser;
+ }
+
+ @Override
+ public Builder<K, V> newBuilderForType() {
+ return new Builder<K, V>(metadata);
+ }
+
+ @Override
+ public Builder<K, V> toBuilder() {
+ return new Builder<K, V>(metadata, data);
+ }
+
+ @Override
+ public MapEntry<K, V> getDefaultInstanceForType() {
+ return metadata.defaultInstance;
+ }
+
+ @Override
+ public Descriptor getDescriptorForType() {
+ return metadata.descriptor;
+ }
+
+ @Override
+ public Map<FieldDescriptor, Object> getAllFields() {
+ final TreeMap<FieldDescriptor, Object> result =
+ new TreeMap<FieldDescriptor, Object>();
+ for (final FieldDescriptor field : metadata.descriptor.getFields()) {
+ if (hasField(field)) {
+ result.put(field, getField(field));
+ }
+ }
+ return Collections.unmodifiableMap(result);
+ }
+
+ private void checkFieldDescriptor(FieldDescriptor field) {
+ if (field.getContainingType() != metadata.descriptor) {
+ throw new RuntimeException(
+ "Wrong FieldDescriptor \"" + field.getFullName()
+ + "\" used in message \"" + metadata.descriptor.getFullName());
+ }
+ }
+
+ @Override
+ public boolean hasField(FieldDescriptor field) {
+ checkFieldDescriptor(field);;
+ // A MapEntry always contains two fields.
+ return true;
+ }
+
+ @Override
+ public Object getField(FieldDescriptor field) {
+ checkFieldDescriptor(field);
+ 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);
+ }
+ return result;
+ }
+
+ @Override
+ public int getRepeatedFieldCount(FieldDescriptor field) {
+ 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.");
+ }
+
+ @Override
+ public UnknownFieldSet getUnknownFields() {
+ return UnknownFieldSet.getDefaultInstance();
+ }
+
+ /**
+ * Builder to create {@link MapEntry} messages.
+ */
+ public static class Builder<K, V>
+ extends AbstractMessage.Builder<Builder<K, V>> {
+ private final Metadata<K, V> metadata;
+ private MapEntryLite<K, V> data;
+ private MapEntryLite.Builder<K, V> dataBuilder;
+
+ private Builder(Metadata<K, V> metadata) {
+ this.metadata = metadata;
+ this.data = metadata.defaultInstance.data;
+ this.dataBuilder = null;
+ }
+
+ private Builder(Metadata<K, V> metadata, MapEntryLite<K, V> data) {
+ this.metadata = metadata;
+ this.data = data;
+ this.dataBuilder = null;
+ }
+
+ public K getKey() {
+ return dataBuilder == null ? data.getKey() : dataBuilder.getKey();
+ }
+
+ public V getValue() {
+ return dataBuilder == null ? data.getValue() : dataBuilder.getValue();
+ }
+
+ private void ensureMutable() {
+ if (dataBuilder == null) {
+ dataBuilder = data.toBuilder();
+ }
+ }
+
+ public Builder<K, V> setKey(K key) {
+ ensureMutable();
+ dataBuilder.setKey(key);
+ return this;
+ }
+
+ public Builder<K, V> clearKey() {
+ ensureMutable();
+ dataBuilder.clearKey();
+ return this;
+ }
+
+ public Builder<K, V> setValue(V value) {
+ ensureMutable();
+ dataBuilder.setValue(value);
+ return this;
+ }
+
+ public Builder<K, V> clearValue() {
+ ensureMutable();
+ dataBuilder.clearValue();
+ return this;
+ }
+
+ @Override
+ public MapEntry<K, V> build() {
+ MapEntry<K, V> result = buildPartial();
+ if (!result.isInitialized()) {
+ throw newUninitializedMessageException(result);
+ }
+ return result;
+ }
+
+ @Override
+ public MapEntry<K, V> buildPartial() {
+ if (dataBuilder != null) {
+ data = dataBuilder.buildPartial();
+ dataBuilder = null;
+ }
+ return new MapEntry<K, V>(metadata, data);
+ }
+
+ @Override
+ public Descriptor getDescriptorForType() {
+ return metadata.descriptor;
+ }
+
+ private void checkFieldDescriptor(FieldDescriptor field) {
+ if (field.getContainingType() != metadata.descriptor) {
+ throw new RuntimeException(
+ "Wrong FieldDescriptor \"" + field.getFullName()
+ + "\" used in message \"" + metadata.descriptor.getFullName());
+ }
+ }
+
+ @Override
+ public com.google.protobuf.Message.Builder newBuilderForField(
+ FieldDescriptor 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.");
+ }
+ return ((Message) data.getValue()).newBuilderForType();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Builder<K, V> setField(FieldDescriptor field, Object value) {
+ checkFieldDescriptor(field);
+ if (field.getNumber() == 1) {
+ setKey((K) value);
+ } else {
+ if (field.getType() == FieldDescriptor.Type.ENUM) {
+ value = ((EnumValueDescriptor) value).getNumber();
+ }
+ setValue((V) value);
+ }
+ return this;
+ }
+
+ @Override
+ public Builder<K, V> clearField(FieldDescriptor field) {
+ checkFieldDescriptor(field);
+ if (field.getNumber() == 1) {
+ clearKey();
+ } else {
+ clearValue();
+ }
+ return this;
+ }
+
+ @Override
+ public Builder<K, V> setRepeatedField(FieldDescriptor field, int index,
+ Object value) {
+ throw new RuntimeException(
+ "There is no repeated field in a map entry message.");
+ }
+
+ @Override
+ public Builder<K, V> addRepeatedField(FieldDescriptor field, Object value) {
+ throw new RuntimeException(
+ "There is no repeated field in a map entry message.");
+ }
+
+ @Override
+ public Builder<K, V> setUnknownFields(UnknownFieldSet unknownFields) {
+ // Unknown fields are discarded for MapEntry message.
+ return this;
+ }
+
+ @Override
+ public MapEntry<K, V> getDefaultInstanceForType() {
+ return metadata.defaultInstance;
+ }
+
+ @Override
+ public boolean isInitialized() {
+ if (dataBuilder != null) {
+ return dataBuilder.isInitialized();
+ } else {
+ return data.isInitialized();
+ }
+ }
+
+ @Override
+ public Map<FieldDescriptor, Object> getAllFields() {
+ final TreeMap<FieldDescriptor, Object> result =
+ new TreeMap<FieldDescriptor, Object>();
+ for (final FieldDescriptor field : metadata.descriptor.getFields()) {
+ if (hasField(field)) {
+ result.put(field, getField(field));
+ }
+ }
+ return Collections.unmodifiableMap(result);
+ }
+
+ @Override
+ public boolean hasField(FieldDescriptor field) {
+ checkFieldDescriptor(field);
+ return true;
+ }
+
+ @Override
+ public Object getField(FieldDescriptor field) {
+ checkFieldDescriptor(field);
+ 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);
+ }
+ return result;
+ }
+
+ @Override
+ public int getRepeatedFieldCount(FieldDescriptor field) {
+ 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.");
+ }
+
+ @Override
+ public UnknownFieldSet getUnknownFields() {
+ return UnknownFieldSet.getDefaultInstance();
+ }
+
+ @Override
+ public Builder<K, V> clone() {
+ if (dataBuilder == null) {
+ return new Builder<K, V>(metadata, data);
+ } else {
+ return new Builder<K, V>(metadata, dataBuilder.build());
+ }
+ }
+ }
+}
diff --git a/java/src/main/java/com/google/protobuf/MapEntryLite.java b/java/src/main/java/com/google/protobuf/MapEntryLite.java
new file mode 100644
index 00000000..bcffa946
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/MapEntryLite.java
@@ -0,0 +1,331 @@
+// 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;
+
+/**
+ * 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.
+ *
+ * Protobuf internal. Users shouldn't use.
+ */
+public class MapEntryLite<K, V> extends AbstractMessageLite {
+ private static class Metadata<K, V> {
+ public final MapEntryLite<K, V> defaultInstance;
+ public final WireFormat.FieldType keyType;
+ public final WireFormat.FieldType valueType;
+ public final Parser<MapEntryLite<K, V>> parser;
+ public Metadata(
+ MapEntryLite<K, V> defaultInstance,
+ WireFormat.FieldType keyType,
+ WireFormat.FieldType valueType) {
+ this.defaultInstance = defaultInstance;
+ this.keyType = keyType;
+ this.valueType = valueType;
+ final Metadata<K, V> finalThis = this;
+ this.parser = new AbstractParser<MapEntryLite<K, V>>() {
+ @Override
+ public MapEntryLite<K, V> parsePartialFrom(
+ CodedInputStream input, ExtensionRegistryLite extensionRegistry)
+ throws InvalidProtocolBufferException {
+ return new MapEntryLite<K, V>(finalThis, input, extensionRegistry);
+ }
+ };
+ }
+ }
+
+ private static final int KEY_FIELD_NUMBER = 1;
+ private static final int VALUE_FIELD_NUMBER = 2;
+
+ private final Metadata<K, V> metadata;
+ private final K key;
+ private final V value;
+
+ /** Creates a default MapEntryLite message instance. */
+ private MapEntryLite(
+ WireFormat.FieldType keyType, K defaultKey,
+ WireFormat.FieldType valueType, V defaultValue) {
+ this.metadata = new Metadata<K, V>(this, keyType, valueType);
+ this.key = defaultKey;
+ this.value = defaultValue;
+ }
+
+ /** Creates a new MapEntryLite message. */
+ private MapEntryLite(Metadata<K, V> metadata, K key, V value) {
+ this.metadata = metadata;
+ this.key = key;
+ this.value = value;
+ }
+
+ public K getKey() {
+ return key;
+ }
+
+ public V getValue() {
+ return value;
+ }
+
+ /**
+ * 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.
+ */
+ public static <K, V> MapEntryLite<K, V> newDefaultInstance(
+ WireFormat.FieldType keyType, K defaultKey,
+ WireFormat.FieldType valueType, V defaultValue) {
+ return new MapEntryLite<K, V>(
+ keyType, defaultKey, valueType, defaultValue);
+ }
+
+ @Override
+ public void writeTo(CodedOutputStream output) throws IOException {
+ writeField(KEY_FIELD_NUMBER, metadata.keyType, key, output);
+ writeField(VALUE_FIELD_NUMBER, metadata.valueType, value, output);
+ }
+
+ private void writeField(
+ int number, WireFormat.FieldType type, Object value,
+ CodedOutputStream output) throws IOException {
+ output.writeTag(number, type.getWireType());
+ FieldSet.writeElementNoTag(output, type, value);
+ }
+
+ private volatile int cachedSerializedSize = -1;
+ @Override
+ public int getSerializedSize() {
+ if (cachedSerializedSize != -1) {
+ return cachedSerializedSize;
+ }
+ int size = 0;
+ size += getFieldSize(KEY_FIELD_NUMBER, metadata.keyType, key);
+ size += getFieldSize(VALUE_FIELD_NUMBER, metadata.valueType, value);
+ cachedSerializedSize = size;
+ return size;
+ }
+
+ private int getFieldSize(
+ int number, WireFormat.FieldType type, Object value) {
+ return CodedOutputStream.computeTagSize(number)
+ + FieldSet.computeElementSizeNoTag(type, value);
+ }
+
+ /** Parsing constructor. */
+ private MapEntryLite(
+ Metadata<K, V> metadata,
+ CodedInputStream input,
+ ExtensionRegistryLite extensionRegistry)
+ throws InvalidProtocolBufferException {
+ try {
+ K key = metadata.defaultInstance.key;
+ V value = metadata.defaultInstance.value;
+ while (true) {
+ int tag = input.readTag();
+ if (tag == 0) {
+ break;
+ }
+ if (tag == WireFormat.makeTag(
+ KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
+ key = mergeField(
+ input, extensionRegistry, metadata.keyType, key);
+ } else if (tag == WireFormat.makeTag(
+ VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
+ value = mergeField(
+ input, extensionRegistry, metadata.valueType, value);
+ } else {
+ if (!input.skipField(tag)) {
+ break;
+ }
+ }
+ }
+ this.metadata = metadata;
+ this.key = key;
+ this.value = value;
+ } catch (InvalidProtocolBufferException e) {
+ throw e.setUnfinishedMessage(this);
+ } catch (IOException e) {
+ throw new InvalidProtocolBufferException(e.getMessage())
+ .setUnfinishedMessage(this);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T mergeField(
+ CodedInputStream input, ExtensionRegistryLite extensionRegistry,
+ WireFormat.FieldType type, T value) throws IOException {
+ switch (type) {
+ case MESSAGE:
+ MessageLite.Builder subBuilder = ((MessageLite) value).toBuilder();
+ input.readMessage(subBuilder, extensionRegistry);
+ return (T) subBuilder.buildPartial();
+ case ENUM:
+ return (T) (java.lang.Integer) input.readEnum();
+ case GROUP:
+ throw new RuntimeException("Groups are not allowed in maps.");
+ default:
+ return (T) FieldSet.readPrimitiveField(input, type, true);
+ }
+ }
+
+ @Override
+ public Parser<MapEntryLite<K, V>> getParserForType() {
+ return metadata.parser;
+ }
+
+ @Override
+ public Builder<K, V> newBuilderForType() {
+ return new Builder<K, V>(metadata);
+ }
+
+ @Override
+ public Builder<K, V> toBuilder() {
+ return new Builder<K, V>(metadata, key, value);
+ }
+
+ @Override
+ public MapEntryLite<K, V> getDefaultInstanceForType() {
+ return metadata.defaultInstance;
+ }
+
+ @Override
+ public boolean isInitialized() {
+ if (metadata.valueType.getJavaType() == WireFormat.JavaType.MESSAGE) {
+ return ((MessageLite) value).isInitialized();
+ }
+ return true;
+ }
+
+ /**
+ * Builder used to create {@link MapEntryLite} messages.
+ */
+ public static class Builder<K, V>
+ extends AbstractMessageLite.Builder<Builder<K, V>> {
+ private final Metadata<K, V> metadata;
+ private K key;
+ private V value;
+
+ private Builder(Metadata<K, V> metadata) {
+ this.metadata = metadata;
+ this.key = metadata.defaultInstance.key;
+ this.value = metadata.defaultInstance.value;
+ }
+
+ public K getKey() {
+ return key;
+ }
+
+ public V getValue() {
+ return value;
+ }
+
+ public Builder<K, V> setKey(K key) {
+ this.key = key;
+ return this;
+ }
+
+ public Builder<K, V> setValue(V value) {
+ this.value = value;
+ return this;
+ }
+
+ public Builder<K, V> clearKey() {
+ this.key = metadata.defaultInstance.key;
+ return this;
+ }
+
+ public Builder<K, V> clearValue() {
+ this.value = metadata.defaultInstance.value;
+ return this;
+ }
+
+ @Override
+ public Builder<K, V> clear() {
+ this.key = metadata.defaultInstance.key;
+ this.value = metadata.defaultInstance.value;
+ return this;
+ }
+
+ @Override
+ public MapEntryLite<K, V> build() {
+ MapEntryLite<K, V> result = buildPartial();
+ if (!result.isInitialized()) {
+ throw newUninitializedMessageException(result);
+ }
+ return result;
+ }
+
+ @Override
+ public MapEntryLite<K, V> buildPartial() {
+ return new MapEntryLite<K, V>(metadata, key, value);
+ }
+
+ @Override
+ public MessageLite getDefaultInstanceForType() {
+ return metadata.defaultInstance;
+ }
+
+ @Override
+ public boolean isInitialized() {
+ if (metadata.valueType.getJavaType() == WireFormat.JavaType.MESSAGE) {
+ return ((MessageLite) value).isInitialized();
+ }
+ return true;
+ }
+
+ private Builder(Metadata<K, V> metadata, K key, V value) {
+ this.metadata = metadata;
+ this.key = key;
+ this.value = value;
+ }
+
+ @Override
+ public Builder<K, V> clone() {
+ return new Builder<K, V>(metadata, key, value);
+ }
+
+ @Override
+ public Builder<K, V> mergeFrom(
+ CodedInputStream input, ExtensionRegistryLite extensionRegistry)
+ throws IOException {
+ MapEntryLite<K, V> entry =
+ new MapEntryLite<K, V>(metadata, input, extensionRegistry);
+ this.key = entry.key;
+ this.value = entry.value;
+ return this;
+ }
+ }
+}
diff --git a/java/src/main/java/com/google/protobuf/MapField.java b/java/src/main/java/com/google/protobuf/MapField.java
new file mode 100644
index 00000000..82906d37
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/MapField.java
@@ -0,0 +1,259 @@
+// 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.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 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 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.
+ */
+public class MapField<K, V> {
+ /**
+ * 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.
+ *
+ * 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
+ *
+ * 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 volatile StorageMode mode;
+ private Map<K, V> mapData;
+ private List<Message> listData;
+
+ // Convert between a map entry Message and a key-value pair.
+ private static interface Converter<K, V> {
+ Message convertKeyAndValueToMessage(K key, V value);
+ void convertMessageToKeyAndValue(Message message, Map<K, V> map);
+
+ Message getMessageDefaultInstance();
+ }
+
+ private static class ImmutableMessageConverter<K, V> implements Converter<K, V> {
+ private final MapEntry<K, V> defaultEntry;
+ public ImmutableMessageConverter(MapEntry<K, V> defaultEntry) {
+ this.defaultEntry = defaultEntry;
+ }
+
+ public Message convertKeyAndValueToMessage(K key, V value) {
+ return defaultEntry.newBuilderForType().setKey(key).setValue(value).buildPartial();
+ }
+
+ public void convertMessageToKeyAndValue(Message message, Map<K, V> map) {
+ MapEntry<K, V> entry = (MapEntry<K, V>) message;
+ map.put(entry.getKey(), entry.getValue());
+ }
+
+ public Message getMessageDefaultInstance() {
+ return defaultEntry;
+ }
+ }
+
+
+ private final Converter<K, V> converter;
+
+ private MapField(
+ Converter<K, V> converter,
+ StorageMode mode,
+ Map<K, V> mapData,
+ List<Message> listData) {
+ this.converter = converter;
+ this.mode = mode;
+ this.mapData = mapData;
+ this.listData = listData;
+ }
+
+ private MapField(
+ MapEntry<K, V> defaultEntry,
+ StorageMode mode,
+ Map<K, V> mapData,
+ List<Message> listData) {
+ this(new ImmutableMessageConverter<K, V>(defaultEntry), mode, mapData, listData);
+ }
+
+
+ /** Returns an immutable empty MapField. */
+ public static <K, V> MapField<K, V> emptyMapField(
+ MapEntry<K, V> defaultEntry) {
+ return new MapField<K, V>(
+ defaultEntry, StorageMode.MAP, Collections.<K, V>emptyMap(), null);
+ }
+
+
+ /** Creates a new mutable empty MapField. */
+ public static <K, V> MapField<K, V> newMapField(MapEntry<K, V> defaultEntry) {
+ return new MapField<K, V>(
+ defaultEntry, StorageMode.MAP, new HashMap<K, V>(), null);
+ }
+
+
+ private Message convertKeyAndValueToMessage(K key, V value) {
+ return converter.convertKeyAndValueToMessage(key, value);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void convertMessageToKeyAndValue(Message message, Map<K, V> map) {
+ converter.convertMessageToKeyAndValue(message, map);
+ }
+
+ private List<Message> convertMapToList(Map<K, V> mapData) {
+ List<Message> listData = new ArrayList<Message>();
+ for (Map.Entry<K, V> entry : mapData.entrySet()) {
+ listData.add(
+ convertKeyAndValueToMessage(
+ entry.getKey(), entry.getValue()));
+ }
+ return listData;
+ }
+
+ private Map<K, V> convertListToMap(List<Message> listData) {
+ Map<K, V> mapData = new HashMap<K, V>();
+ for (Message item : listData) {
+ convertMessageToKeyAndValue(item, mapData);
+ }
+ return mapData;
+ }
+
+ /** Returns the content of this MapField as a read-only Map. */
+ public Map<K, V> getMap() {
+ if (mode == StorageMode.LIST) {
+ synchronized (this) {
+ if (mode == StorageMode.LIST) {
+ mapData = convertListToMap(listData);
+ mode = StorageMode.BOTH;
+ }
+ }
+ }
+ return Collections.unmodifiableMap(mapData);
+ }
+
+ /** Gets a mutable Map view of this MapField. */
+ public Map<K, V> getMutableMap() {
+ if (mode != StorageMode.MAP) {
+ if (mode == StorageMode.LIST) {
+ mapData = convertListToMap(listData);
+ }
+ listData = null;
+ mode = StorageMode.MAP;
+ }
+ return mapData;
+ }
+
+ public void mergeFrom(MapField<K, V> other) {
+ getMutableMap().putAll(MapFieldLite.copy(other.getMap()));
+ }
+
+ public void clear() {
+ mapData = new HashMap<K, V>();
+ mode = StorageMode.MAP;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(Object object) {
+ if (!(object instanceof MapField)) {
+ return false;
+ }
+ MapField<K, V> other = (MapField<K, V>) object;
+ return MapFieldLite.<K, V>equals(getMap(), other.getMap());
+ }
+
+ @Override
+ public int hashCode() {
+ return MapFieldLite.<K, V>calculateHashCodeForMap(getMap());
+ }
+
+ /** Returns a deep copy of this MapField. */
+ public MapField<K, V> copy() {
+ return new MapField<K, V>(
+ converter, StorageMode.MAP, MapFieldLite.copy(getMap()), null);
+ }
+
+ /** Gets the content of this MapField as a read-only List. */
+ List<Message> getList() {
+ if (mode == StorageMode.MAP) {
+ synchronized (this) {
+ if (mode == StorageMode.MAP) {
+ listData = convertMapToList(mapData);
+ mode = StorageMode.BOTH;
+ }
+ }
+ }
+ return Collections.unmodifiableList(listData);
+ }
+
+ /** Gets a mutable List view of this MapField. */
+ List<Message> getMutableList() {
+ if (mode != StorageMode.LIST) {
+ if (mode == StorageMode.MAP) {
+ listData = convertMapToList(mapData);
+ }
+ mapData = null;
+ mode = StorageMode.LIST;
+ }
+ return listData;
+ }
+
+ /**
+ * Gets the default instance of the message stored in the list view of this
+ * map field.
+ */
+ Message getMapEntryMessageDefaultInstance() {
+ return converter.getMessageDefaultInstance();
+ }
+}
diff --git a/java/src/main/java/com/google/protobuf/MapFieldLite.java b/java/src/main/java/com/google/protobuf/MapFieldLite.java
new file mode 100644
index 00000000..7f94c690
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/MapFieldLite.java
@@ -0,0 +1,182 @@
+// 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.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Internal representation of map fields in generated lite-runtime messages.
+ *
+ * This class is a protobuf implementation detail. Users shouldn't use this
+ * class directly.
+ */
+public class MapFieldLite<K, V> {
+ private Map<K, V> mapData;
+
+ private MapFieldLite(Map<K, V> mapData) {
+ this.mapData = mapData;
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private static final MapFieldLite EMPTY_MAP_FIELD =
+ new MapFieldLite(Collections.emptyMap());
+
+ /** Returns an singleton immutable empty MapFieldLite instance. */
+ @SuppressWarnings({"unchecked", "cast"})
+ public static <K, V> MapFieldLite<K, V> emptyMapField() {
+ return (MapFieldLite<K, V>) EMPTY_MAP_FIELD;
+ }
+
+ /** Creates a new MapFieldLite instance. */
+ public static <K, V> MapFieldLite<K, V> newMapField() {
+ return new MapFieldLite<K, V>(new HashMap<K, V>());
+ }
+
+ /** Gets the content of this MapField as a read-only Map. */
+ public Map<K, V> getMap() {
+ return Collections.unmodifiableMap(mapData);
+ }
+
+ /** Gets a mutable Map view of this MapField. */
+ public Map<K, V> getMutableMap() {
+ return mapData;
+ }
+
+ public void mergeFrom(MapFieldLite<K, V> other) {
+ mapData.putAll(copy(other.mapData));
+ }
+
+ public void clear() {
+ mapData.clear();
+ }
+
+ private static boolean equals(Object a, Object b) {
+ if (a instanceof byte[] && b instanceof byte[]) {
+ return Arrays.equals((byte[]) a, (byte[]) b);
+ }
+ return a.equals(b);
+ }
+
+ /**
+ * 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 <K, V> boolean equals(Map<K, V> a, Map<K, V> b) {
+ if (a == b) {
+ return true;
+ }
+ if (a.size() != b.size()) {
+ return false;
+ }
+ for (Map.Entry<K, V> entry : a.entrySet()) {
+ if (!b.containsKey(entry.getKey())) {
+ return false;
+ }
+ if (!equals(entry.getValue(), b.get(entry.getKey()))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Checks whether two map fields are equal.
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(Object object) {
+ if (!(object instanceof MapFieldLite)) {
+ return false;
+ }
+ MapFieldLite<K, V> other = (MapFieldLite<K, V>) object;
+ return equals(mapData, other.mapData);
+ }
+
+ private static int calculateHashCodeForObject(Object a) {
+ if (a instanceof byte[]) {
+ return LiteralByteString.hashCode((byte[]) a);
+ }
+ if (a instanceof Internal.EnumLite) {
+ return Internal.hashEnum((Internal.EnumLite) a);
+ }
+ return a.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 <K, V> int calculateHashCodeForMap(Map<K, V> a) {
+ int result = 0;
+ for (Map.Entry<K, V> entry : a.entrySet()) {
+ result += calculateHashCodeForObject(entry.getKey())
+ ^ calculateHashCodeForObject(entry.getValue());
+ }
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ return calculateHashCodeForMap(mapData);
+ }
+
+ private static Object copy(Object object) {
+ if (object instanceof byte[]) {
+ byte[] data = (byte[]) object;
+ return Arrays.copyOf(data, data.length);
+ }
+ return object;
+ }
+
+ /**
+ * 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 <K, V> Map<K, V> copy(Map<K, V> map) {
+ Map<K, V> result = new HashMap<K, V>();
+ for (Map.Entry<K, V> entry : map.entrySet()) {
+ result.put(entry.getKey(), (V) copy(entry.getValue()));
+ }
+ return result;
+ }
+
+ /** Returns a deep copy of this map field. */
+ public MapFieldLite<K, V> copy() {
+ return new MapFieldLite<K, V>(copy(mapData));
+ }
+}
diff --git a/java/src/main/java/com/google/protobuf/Message.java b/java/src/main/java/com/google/protobuf/Message.java
index 5673d3bb..5c75b58b 100644
--- a/java/src/main/java/com/google/protobuf/Message.java
+++ b/java/src/main/java/com/google/protobuf/Message.java
@@ -168,6 +168,25 @@ public interface Message extends MessageLite, MessageOrBuilder {
Builder getFieldBuilder(Descriptors.FieldDescriptor field);
/**
+ * Get a nested builder instance for the given repeated field instance.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * NOTE: implementations that do not support nested builders will throw
+ * <code>UnsupportedException</code>.
+ */
+ 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.
diff --git a/java/src/main/java/com/google/protobuf/MessageReflection.java b/java/src/main/java/com/google/protobuf/MessageReflection.java
index edb5b5c2..06e3c99b 100644
--- a/java/src/main/java/com/google/protobuf/MessageReflection.java
+++ b/java/src/main/java/com/google/protobuf/MessageReflection.java
@@ -752,13 +752,18 @@ class MessageReflection {
if (field.getLiteType() == WireFormat.FieldType.ENUM) {
while (input.getBytesUntilLimit() > 0) {
final int rawValue = input.readEnum();
- final Object value = field.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;
+ if (field.getFile().supportsUnknownEnumValue()) {
+ target.addRepeatedField(field,
+ field.getEnumType().findValueByNumberCreatingIfUnknown(rawValue));
+ } else {
+ final Object value = field.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;
+ }
+ target.addRepeatedField(field, value);
}
- target.addRepeatedField(field, value);
}
} else {
while (input.getBytesUntilLimit() > 0) {
@@ -783,12 +788,16 @@ class MessageReflection {
}
case ENUM:
final int rawValue = input.readEnum();
- value = field.getEnumType().findValueByNumber(rawValue);
- // If the number isn't recognized as a valid value for this enum,
- // drop it.
- if (value == null) {
- unknownFields.mergeVarintField(fieldNumber, rawValue);
- return true;
+ if (field.getFile().supportsUnknownEnumValue()) {
+ value = field.getEnumType().findValueByNumberCreatingIfUnknown(rawValue);
+ } else {
+ value = field.getEnumType().findValueByNumber(rawValue);
+ // If the number isn't recognized as a valid value for this enum,
+ // drop it.
+ if (value == null) {
+ unknownFields.mergeVarintField(fieldNumber, rawValue);
+ return true;
+ }
}
break;
default:
diff --git a/java/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java b/java/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java
index 22760d3a..be737b1a 100644
--- a/java/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java
+++ b/java/src/main/java/com/google/protobuf/RepeatedFieldBuilder.java
@@ -54,7 +54,7 @@ import java.util.List;
* 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 occured in its builder or a builder for one of its
+ * created when some change occurred in its builder or a builder for one of its
* descendants.
*
* @param <MType> the type of message for the field
@@ -367,22 +367,28 @@ public class RepeatedFieldBuilder
throw new NullPointerException();
}
}
+
+ // If we can inspect the size, we can more efficiently add messages.
+ int size = -1;
if (values instanceof Collection) {
@SuppressWarnings("unchecked") final
Collection<MType> collection = (Collection<MType>) values;
if (collection.size() == 0) {
return this;
}
- ensureMutableMessageList();
- for (MType value : values) {
- addMessage(value);
- }
- } else {
- ensureMutableMessageList();
- for (MType value : values) {
- addMessage(value);
- }
+ size = collection.size();
+ }
+ ensureMutableMessageList();
+
+ if (size >= 0 && messages instanceof ArrayList) {
+ ((ArrayList<MType>) messages)
+ .ensureCapacity(messages.size() + size);
}
+
+ for (MType value : values) {
+ addMessage(value);
+ }
+
onChanged();
incrementModCounts();
return this;
diff --git a/java/src/main/java/com/google/protobuf/RopeByteString.java b/java/src/main/java/com/google/protobuf/RopeByteString.java
index d1655b80..fa23c9dd 100644
--- a/java/src/main/java/com/google/protobuf/RopeByteString.java
+++ b/java/src/main/java/com/google/protobuf/RopeByteString.java
@@ -30,8 +30,10 @@
package com.google.protobuf;
+import java.io.InvalidObjectException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.ByteArrayInputStream;
@@ -772,6 +774,20 @@ class RopeByteString extends ByteString {
}
// =================================================================
+ // Serializable
+
+ private static final long serialVersionUID = 1L;
+
+ Object writeReplace() {
+ return new LiteralByteString(toByteArray());
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException {
+ throw new InvalidObjectException(
+ "RopeByteStream instances are not to be serialized directly");
+ }
+
+ // =================================================================
// ByteIterator
@Override
diff --git a/java/src/main/java/com/google/protobuf/SingleFieldBuilder.java b/java/src/main/java/com/google/protobuf/SingleFieldBuilder.java
index 13a36d47..aba65e32 100644
--- a/java/src/main/java/com/google/protobuf/SingleFieldBuilder.java
+++ b/java/src/main/java/com/google/protobuf/SingleFieldBuilder.java
@@ -47,7 +47,7 @@ package com.google.protobuf;
* 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 occured in its builder or a builder for one of its
+ * created when some change occurred in its builder or a builder for one of its
* descendants.
*
* @param <MType> the type of message for the field
diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java
index 57d0ca68..4f6756ed 100644
--- a/java/src/main/java/com/google/protobuf/TextFormat.java
+++ b/java/src/main/java/com/google/protobuf/TextFormat.java
@@ -411,7 +411,8 @@ public final class TextFormat {
generator.print("\"");
generator.print(escapeNonAscii ?
escapeText((String) value) :
- escapeDoubleQuotesAndBackslashes((String) value));
+ escapeDoubleQuotesAndBackslashes((String) value)
+ .replace("\n", "\\n"));
generator.print("\"");
break;
@@ -1211,6 +1212,7 @@ public final class TextFormat {
private SingularOverwritePolicy singularOverwritePolicy =
SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES;
+
/**
* Sets parser behavior when a non-repeated field appears more than once.
*/
@@ -1386,7 +1388,7 @@ public final class TextFormat {
// Try to guess the type of this field.
// If this field is not a message, there should be a ":" between the
// field name and the field value and also the field value should not
- // start with "{" or "<" which indicates the begining of a message body.
+ // 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("{") &&
@@ -1418,6 +1420,12 @@ public final class TextFormat {
} else {
consumeFieldValue(tokenizer, extensionRegistry, target, field, extension);
}
+
+ // For historical reasons, fields may optionally be separated by commas or
+ // semicolons.
+ if (!tokenizer.tryConsume(";")) {
+ tokenizer.tryConsume(",");
+ }
}
/**
@@ -1567,7 +1575,7 @@ public final class TextFormat {
// Try to guess the type of this field.
// If this field is not a message, there should be a ":" between the
// field name and the field value and also the field value should not
- // start with "{" or "<" which indicates the begining of a message body.
+ // 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("<") &&
@@ -1584,8 +1592,8 @@ public final class TextFormat {
}
/**
- * Skips the whole body of a message including the beginning delimeter and
- * the ending delimeter.
+ * 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;
@@ -1656,10 +1664,9 @@ public final class TextFormat {
case '\'': builder.append("\\\'"); break;
case '"' : builder.append("\\\""); break;
default:
- // Note: Bytes with the high-order bit set should be escaped. Since
- // bytes are signed, such bytes will compare less than 0x20, hence
- // the following line is correct.
- if (b >= 0x20) {
+ // Only ASCII characters between 0x20 (space) and 0x7e (tilde) are
+ // printable. Other byte values must be escaped.
+ if (b >= 0x20 && b <= 0x7e) {
builder.append((char) b);
} else {
builder.append('\\');
diff --git a/java/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/src/main/java/com/google/protobuf/UnknownFieldSet.java
index 99de3732..7cd2250e 100644
--- a/java/src/main/java/com/google/protobuf/UnknownFieldSet.java
+++ b/java/src/main/java/com/google/protobuf/UnknownFieldSet.java
@@ -431,6 +431,21 @@ public final class UnknownFieldSet implements MessageLite {
return this;
}
+
+ /**
+ * Convenience method for merging a length-delimited field.
+ *
+ * <p>For use by generated code only.
+ */
+ public Builder mergeLengthDelimitedField(
+ final int number, final ByteString value) {
+ if (number == 0) {
+ throw new IllegalArgumentException("Zero is not a valid field number.");
+ }
+ getFieldBuilder(number).addLengthDelimited(value);
+ return this;
+ }
+
/** Check if the given field number is present in the set. */
public boolean hasField(final int number) {
if (number == 0) {
diff --git a/java/src/main/java/com/google/protobuf/UnknownFieldSetLite.java b/java/src/main/java/com/google/protobuf/UnknownFieldSetLite.java
new file mode 100644
index 00000000..7ea84022
--- /dev/null
+++ b/java/src/main/java/com/google/protobuf/UnknownFieldSetLite.java
@@ -0,0 +1,297 @@
+// 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;
+
+/**
+ * {@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.
+ *
+ * <p>For use by generated code only.
+ *
+ * @author dweis@google.com (Daniel Weis)
+ */
+public final class UnknownFieldSetLite {
+
+ private static final UnknownFieldSetLite DEFAULT_INSTANCE =
+ new UnknownFieldSetLite(ByteString.EMPTY);
+
+ /**
+ * Get an empty {@code UnknownFieldSetLite}.
+ *
+ * <p>For use by generated code only.
+ */
+ public static UnknownFieldSetLite getDefaultInstance() {
+ return DEFAULT_INSTANCE;
+ }
+
+ /**
+ * Create a new {@link Builder}.
+ *
+ * <p>For use by generated code only.
+ */
+ public static Builder newBuilder() {
+ return new Builder();
+ }
+
+ /**
+ * Returns an {@code UnknownFieldSetLite} that is the composite of {@code first} and
+ * {@code second}.
+ */
+ static UnknownFieldSetLite concat(UnknownFieldSetLite first, UnknownFieldSetLite second) {
+ return new UnknownFieldSetLite(first.byteString.concat(second.byteString));
+ }
+
+ /**
+ * The internal representation of the unknown fields.
+ */
+ private final ByteString byteString;
+
+ /**
+ * Constructs the {@code UnknownFieldSetLite} as a thin wrapper around {@link ByteString}.
+ */
+ private UnknownFieldSetLite(ByteString byteString) {
+ this.byteString = byteString;
+ }
+
+ /**
+ * Serializes the set and writes it to {@code output}.
+ *
+ * <p>For use by generated code only.
+ */
+ public void writeTo(CodedOutputStream output) throws IOException {
+ output.writeRawBytes(byteString);
+ }
+
+
+ /**
+ * Get the number of bytes required to encode this set.
+ *
+ * <p>For use by generated code only.
+ */
+ public int getSerializedSize() {
+ return byteString.size();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (obj instanceof UnknownFieldSetLite) {
+ return byteString.equals(((UnknownFieldSetLite) obj).byteString);
+ }
+
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return byteString.hashCode();
+ }
+
+ /**
+ * Builder for {@link UnknownFieldSetLite}s.
+ *
+ * <p>Use {@link UnknownFieldSet#newBuilder()} to construct a {@code Builder}.
+ *
+ * <p>For use by generated code only.
+ */
+ public static final class Builder {
+
+ private ByteString.Output byteStringOutput;
+ private CodedOutputStream output;
+ private boolean built;
+
+ /**
+ * Constructs a {@code Builder}. Lazily initialized by
+ * {@link #ensureInitializedButNotBuilt()}.
+ */
+ private Builder() {}
+
+ /**
+ * Ensures internal state is initialized for use.
+ */
+ private void ensureInitializedButNotBuilt() {
+ if (built) {
+ throw new IllegalStateException("Do not reuse UnknownFieldSetLite Builders.");
+ }
+
+ if (output == null && byteStringOutput == null) {
+ byteStringOutput = ByteString.newOutput(100 /* initialCapacity */);
+ output = CodedOutputStream.newInstance(byteStringOutput);
+ }
+ }
+
+ /**
+ * Parse a single field from {@code input} and merge it into this set.
+ *
+ * <p>For use by generated code only.
+ *
+ * @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 {
+ ensureInitializedButNotBuilt();
+
+ final int fieldNumber = WireFormat.getTagFieldNumber(tag);
+ switch (WireFormat.getTagWireType(tag)) {
+ case WireFormat.WIRETYPE_VARINT:
+ output.writeUInt64(fieldNumber, input.readInt64());
+ return true;
+ case WireFormat.WIRETYPE_FIXED32:
+ output.writeFixed32(fieldNumber, input.readFixed32());
+ return true;
+ case WireFormat.WIRETYPE_FIXED64:
+ output.writeFixed64(fieldNumber, input.readFixed64());
+ return true;
+ case WireFormat.WIRETYPE_LENGTH_DELIMITED:
+ output.writeBytes(fieldNumber, input.readBytes());
+ return true;
+ case WireFormat.WIRETYPE_START_GROUP:
+ final Builder subBuilder = newBuilder();
+ subBuilder.mergeFrom(input);
+ input.checkLastTagWas(
+ WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP));
+
+ output.writeTag(fieldNumber, WireFormat.WIRETYPE_START_GROUP);
+ subBuilder.build().writeTo(output);
+ output.writeTag(fieldNumber, WireFormat.WIRETYPE_END_GROUP);
+ return true;
+ case WireFormat.WIRETYPE_END_GROUP:
+ return false;
+ default:
+ throw InvalidProtocolBufferException.invalidWireType();
+ }
+ }
+
+ /**
+ * Convenience method for merging a new field containing a single varint
+ * value. This is used in particular when an unknown enum value is
+ * encountered.
+ *
+ * <p>For use by generated code only.
+ */
+ public Builder mergeVarintField(int fieldNumber, int value) {
+ if (fieldNumber == 0) {
+ throw new IllegalArgumentException("Zero is not a valid field number.");
+ }
+ ensureInitializedButNotBuilt();
+ try {
+ output.writeUInt64(fieldNumber, value);
+ } catch (IOException e) {
+ // Should never happen.
+ }
+ return this;
+ }
+
+ /**
+ * Convenience method for merging a length-delimited field.
+ *
+ * <p>For use by generated code only.
+ */
+ public Builder mergeLengthDelimitedField(
+ final int fieldNumber, final ByteString value) {
+ if (fieldNumber == 0) {
+ throw new IllegalArgumentException("Zero is not a valid field number.");
+ }
+ ensureInitializedButNotBuilt();
+ try {
+ output.writeBytes(fieldNumber, value);
+ } catch (IOException e) {
+ // Should never happen.
+ }
+ return this;
+ }
+
+ /**
+ * Build the {@link UnknownFieldSetLite} and return it.
+ *
+ * <p>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 IllegalStateException} to be
+ * thrown.
+ *
+ * <p>For use by generated code only.
+ */
+ public UnknownFieldSetLite build() {
+ if (built) {
+ throw new IllegalStateException("Do not reuse UnknownFieldSetLite Builders.");
+ }
+
+ built = true;
+
+ final UnknownFieldSetLite result;
+ // If we were never initialized, no data was written.
+ if (output == null) {
+ result = getDefaultInstance();
+ } else {
+ try {
+ output.flush();
+ } catch (IOException e) {
+ // Should never happen.
+ }
+ ByteString byteString = byteStringOutput.toByteString();
+ if (byteString.isEmpty()) {
+ result = getDefaultInstance();
+ } else {
+ result = new UnknownFieldSetLite(byteString);
+ }
+ }
+
+ // Allow for garbage collection.
+ output = null;
+ byteStringOutput = null;
+ return result;
+ }
+
+ /**
+ * Parse an entire message from {@code input} and merge its fields into
+ * this set.
+ */
+ private Builder mergeFrom(final CodedInputStream input) throws IOException {
+ // Ensures initialization in mergeFieldFrom.
+ while (true) {
+ final int tag = input.readTag();
+ if (tag == 0 || !mergeFieldFrom(tag, input)) {
+ break;
+ }
+ }
+ return this;
+ }
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java
index 2b71cfbc..6c9596ca 100644
--- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java
+++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java
@@ -30,8 +30,14 @@
package com.google.protobuf;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+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
@@ -65,4 +71,17 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
assertEquals(classUnderTest + " unicode bytes must match",
testString.substring(2, testString.length() - 6), roundTripString);
}
+
+ public void testJavaSerialization() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(out);
+ oos.writeObject(stringUnderTest);
+ oos.close();
+ byte[] pickled = out.toByteArray();
+ InputStream in = new ByteArrayInputStream(pickled);
+ ObjectInputStream ois = new ObjectInputStream(in);
+ Object o = ois.readObject();
+ assertTrue("Didn't get a ByteString back", o instanceof ByteString);
+ assertEquals("Should get an equal ByteString back", stringUnderTest, o);
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java
index 3f656e25..18d8142c 100644
--- a/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java
+++ b/java/src/test/java/com/google/protobuf/CodedInputStreamTest.java
@@ -455,19 +455,19 @@ public class CodedInputStreamTest extends TestCase {
}
public void testMaliciousRecursion() throws Exception {
- ByteString data64 = makeRecursiveMessage(64).toByteString();
- ByteString data65 = makeRecursiveMessage(65).toByteString();
+ ByteString data100 = makeRecursiveMessage(100).toByteString();
+ ByteString data101 = makeRecursiveMessage(101).toByteString();
- assertMessageDepth(TestRecursiveMessage.parseFrom(data64), 64);
+ assertMessageDepth(TestRecursiveMessage.parseFrom(data100), 100);
try {
- TestRecursiveMessage.parseFrom(data65);
+ TestRecursiveMessage.parseFrom(data101);
fail("Should have thrown an exception!");
} catch (InvalidProtocolBufferException e) {
// success.
}
- CodedInputStream input = data64.newCodedInput();
+ CodedInputStream input = data100.newCodedInput();
input.setRecursionLimit(8);
try {
TestRecursiveMessage.parseFrom(input);
diff --git a/java/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/src/test/java/com/google/protobuf/DescriptorsTest.java
index 0092771b..05832a12 100644
--- a/java/src/test/java/com/google/protobuf/DescriptorsTest.java
+++ b/java/src/test/java/com/google/protobuf/DescriptorsTest.java
@@ -706,6 +706,12 @@ public class DescriptorsTest extends TestCase {
assertTrue(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(4143));
}
+ public void testToString() {
+ 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")
diff --git a/java/src/test/java/com/google/protobuf/FieldPresenceTest.java b/java/src/test/java/com/google/protobuf/FieldPresenceTest.java
new file mode 100644
index 00000000..acf2b023
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/FieldPresenceTest.java
@@ -0,0 +1,363 @@
+// 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.Descriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor;
+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.
+ */
+public class FieldPresenceTest extends TestCase {
+ private static boolean hasMethod(Class clazz, String name) {
+ try {
+ if (clazz.getMethod(name, new Class[]{}) != null) {
+ return true;
+ } else {
+ return false;
+ }
+ } catch (NoSuchMethodException e) {
+ return false;
+ }
+ }
+
+ private static boolean isHasMethodRemoved(
+ Class classWithFieldPresence,
+ Class classWithoutFieldPresence,
+ String camelName) {
+ return hasMethod(classWithFieldPresence, "get" + camelName)
+ && hasMethod(classWithFieldPresence, "has" + camelName)
+ && hasMethod(classWithoutFieldPresence, "get" + camelName)
+ && !hasMethod(classWithoutFieldPresence, "has" + camelName);
+ }
+
+ public void testHasMethod() {
+ // Optional non-message fields don't have a hasFoo() method generated.
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OptionalInt32"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OptionalString"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OptionalBytes"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OptionalNestedEnum"));
+
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OptionalInt32"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OptionalString"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OptionalBytes"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OptionalNestedEnum"));
+
+ // message fields still have the hasFoo() method generated.
+ assertFalse(TestAllTypes.newBuilder().build().hasOptionalNestedMessage());
+ assertFalse(TestAllTypes.newBuilder().hasOptionalNestedMessage());
+
+ // oneof fields don't have hasFoo() methods (even for message types).
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OneofUint32"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OneofString"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OneofBytes"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.class,
+ TestAllTypes.class,
+ "OneofNestedMessage"));
+
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OneofUint32"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OneofString"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OneofBytes"));
+ assertTrue(isHasMethodRemoved(
+ UnittestProto.TestAllTypes.Builder.class,
+ TestAllTypes.Builder.class,
+ "OneofNestedMessage"));
+ }
+
+ public void testFieldPresence() {
+ // Optional non-message fields set to their default value are treated the
+ // same way as not set.
+
+ // Serialization will ignore such fields.
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ builder.setOptionalInt32(0);
+ builder.setOptionalString("");
+ builder.setOptionalBytes(ByteString.EMPTY);
+ builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO);
+ TestAllTypes message = builder.build();
+ assertEquals(0, message.getSerializedSize());
+
+ // mergeFrom() will ignore such fields.
+ TestAllTypes.Builder a = TestAllTypes.newBuilder();
+ a.setOptionalInt32(1);
+ a.setOptionalString("x");
+ a.setOptionalBytes(ByteString.copyFromUtf8("y"));
+ a.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR);
+ TestAllTypes.Builder b = TestAllTypes.newBuilder();
+ b.setOptionalInt32(0);
+ b.setOptionalString("");
+ b.setOptionalBytes(ByteString.EMPTY);
+ b.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO);
+ a.mergeFrom(b.build());
+ message = a.build();
+ assertEquals(1, message.getOptionalInt32());
+ assertEquals("x", message.getOptionalString());
+ assertEquals(ByteString.copyFromUtf8("y"), message.getOptionalBytes());
+ assertEquals(TestAllTypes.NestedEnum.BAR, message.getOptionalNestedEnum());
+
+ // equals()/hashCode() should produce the same results.
+ TestAllTypes empty = TestAllTypes.newBuilder().build();
+ message = builder.build();
+ assertTrue(empty.equals(message));
+ assertTrue(message.equals(empty));
+ assertEquals(empty.hashCode(), message.hashCode());
+ }
+
+ public void testFieldPresenceByReflection() {
+ Descriptor descriptor = TestAllTypes.getDescriptor();
+ FieldDescriptor optionalInt32Field = descriptor.findFieldByName("optional_int32");
+ FieldDescriptor optionalStringField = descriptor.findFieldByName("optional_string");
+ FieldDescriptor optionalBytesField = descriptor.findFieldByName("optional_bytes");
+ FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("optional_nested_enum");
+
+ // Field not present.
+ TestAllTypes message = TestAllTypes.newBuilder().build();
+ assertFalse(message.hasField(optionalInt32Field));
+ assertFalse(message.hasField(optionalStringField));
+ assertFalse(message.hasField(optionalBytesField));
+ assertFalse(message.hasField(optionalNestedEnumField));
+ 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();
+ assertFalse(message.hasField(optionalInt32Field));
+ assertFalse(message.hasField(optionalStringField));
+ assertFalse(message.hasField(optionalBytesField));
+ assertFalse(message.hasField(optionalNestedEnumField));
+ 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();
+ assertTrue(message.hasField(optionalInt32Field));
+ assertTrue(message.hasField(optionalStringField));
+ assertTrue(message.hasField(optionalBytesField));
+ assertTrue(message.hasField(optionalNestedEnumField));
+ assertEquals(4, message.getAllFields().size());
+ }
+
+ public void testMessageField() {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ assertFalse(builder.hasOptionalNestedMessage());
+ assertFalse(builder.build().hasOptionalNestedMessage());
+
+ TestAllTypes.NestedMessage.Builder nestedBuilder =
+ builder.getOptionalNestedMessageBuilder();
+ assertTrue(builder.hasOptionalNestedMessage());
+ assertTrue(builder.build().hasOptionalNestedMessage());
+
+ nestedBuilder.setValue(1);
+ assertEquals(1, builder.build().getOptionalNestedMessage().getValue());
+
+ builder.clearOptionalNestedMessage();
+ assertFalse(builder.hasOptionalNestedMessage());
+ assertFalse(builder.build().hasOptionalNestedMessage());
+
+ // Unlike non-message fields, if we set a message field to its default value (i.e.,
+ // default instance), the field should be seen as present.
+ builder.setOptionalNestedMessage(TestAllTypes.NestedMessage.getDefaultInstance());
+ assertTrue(builder.hasOptionalNestedMessage());
+ assertTrue(builder.build().hasOptionalNestedMessage());
+ }
+
+ public void testSerializeAndParse() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ builder.setOptionalInt32(1234);
+ builder.setOptionalString("hello");
+ builder.setOptionalNestedMessage(TestAllTypes.NestedMessage.getDefaultInstance());
+ // Set an oneof field to its default value and expect it to be serialized (i.e.,
+ // an oneof field set to the default value should be treated as present).
+ builder.setOneofInt32(0);
+ ByteString data = builder.build().toByteString();
+
+ TestAllTypes message = TestAllTypes.parseFrom(data);
+ assertEquals(1234, message.getOptionalInt32());
+ assertEquals("hello", message.getOptionalString());
+ // Fields not set will have the default value.
+ assertEquals(ByteString.EMPTY, message.getOptionalBytes());
+ assertEquals(TestAllTypes.NestedEnum.FOO, message.getOptionalNestedEnum());
+ // The message field is set despite that it's set with a default instance.
+ 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());
+ }
+
+ // Regression test for b/16173397
+ // Make sure we haven't screwed up the code generation for repeated fields.
+ public void testRepeatedFields() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ builder.setOptionalInt32(1234);
+ builder.setOptionalString("hello");
+ builder.setOptionalNestedMessage(TestAllTypes.NestedMessage.getDefaultInstance());
+ builder.addRepeatedInt32(4321);
+ builder.addRepeatedString("world");
+ builder.addRepeatedNestedMessage(TestAllTypes.NestedMessage.getDefaultInstance());
+ ByteString data = builder.build().toByteString();
+
+ TestOptionalFieldsOnly optionalOnlyMessage = TestOptionalFieldsOnly.parseFrom(data);
+ assertEquals(1234, optionalOnlyMessage.getOptionalInt32());
+ assertEquals("hello", optionalOnlyMessage.getOptionalString());
+ assertTrue(optionalOnlyMessage.hasOptionalNestedMessage());
+ assertEquals(0, optionalOnlyMessage.getOptionalNestedMessage().getValue());
+
+ TestRepeatedFieldsOnly repeatedOnlyMessage = TestRepeatedFieldsOnly.parseFrom(data);
+ assertEquals(1, repeatedOnlyMessage.getRepeatedInt32Count());
+ assertEquals(4321, repeatedOnlyMessage.getRepeatedInt32(0));
+ assertEquals(1, repeatedOnlyMessage.getRepeatedStringCount());
+ assertEquals("world", repeatedOnlyMessage.getRepeatedString(0));
+ assertEquals(1, repeatedOnlyMessage.getRepeatedNestedMessageCount());
+ assertEquals(0, repeatedOnlyMessage.getRepeatedNestedMessage(0).getValue());
+ }
+
+ public void testIsInitialized() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+
+ // Test optional proto2 message fields.
+ UnittestProto.TestRequired.Builder proto2Builder =
+ builder.getOptionalProto2MessageBuilder();
+ assertFalse(builder.isInitialized());
+ assertFalse(builder.buildPartial().isInitialized());
+
+ proto2Builder.setA(1).setB(2).setC(3);
+ assertTrue(builder.isInitialized());
+ assertTrue(builder.buildPartial().isInitialized());
+
+ // Test oneof proto2 message fields.
+ proto2Builder = builder.getOneofProto2MessageBuilder();
+ assertFalse(builder.isInitialized());
+ assertFalse(builder.buildPartial().isInitialized());
+
+ proto2Builder.setA(1).setB(2).setC(3);
+ assertTrue(builder.isInitialized());
+ assertTrue(builder.buildPartial().isInitialized());
+
+ // Test repeated proto2 message fields.
+ proto2Builder = builder.addRepeatedProto2MessageBuilder();
+ assertFalse(builder.isInitialized());
+ assertFalse(builder.buildPartial().isInitialized());
+
+ proto2Builder.setA(1).setB(2).setC(3);
+ assertTrue(builder.isInitialized());
+ assertTrue(builder.buildPartial().isInitialized());
+ }
+
+
+ // Test that unknown fields are dropped.
+ public void testUnknownFields() throws Exception {
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ builder.setOptionalInt32(1234);
+ builder.addRepeatedInt32(5678);
+ TestAllTypes message = builder.build();
+ ByteString data = message.toByteString();
+
+ TestOptionalFieldsOnly optionalOnlyMessage =
+ TestOptionalFieldsOnly.parseFrom(data);
+ // UnknownFieldSet should be empty.
+ assertEquals(
+ 0, optionalOnlyMessage.getUnknownFields().toByteString().size());
+ assertEquals(1234, optionalOnlyMessage.getOptionalInt32());
+ message = TestAllTypes.parseFrom(optionalOnlyMessage.toByteString());
+ assertEquals(1234, message.getOptionalInt32());
+ // The repeated field is discarded because it's unknown to the optional-only
+ // message.
+ assertEquals(0, message.getRepeatedInt32Count());
+
+ DynamicMessage dynamicOptionalOnlyMessage =
+ DynamicMessage.getDefaultInstance(
+ TestOptionalFieldsOnly.getDescriptor())
+ .getParserForType().parseFrom(data);
+ assertEquals(
+ 0, dynamicOptionalOnlyMessage.getUnknownFields().toByteString().size());
+ assertEquals(optionalOnlyMessage.toByteString(),
+ dynamicOptionalOnlyMessage.toByteString());
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
index 0b3482c3..41ed7bd0 100644
--- a/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
+++ b/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
@@ -157,15 +157,12 @@ public class GeneratedMessageTest extends TestCase {
public void testProtosShareRepeatedArraysIfDidntChange() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
builder.addRepeatedInt32(100);
- builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR);
builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance());
TestAllTypes value1 = builder.build();
TestAllTypes value2 = value1.toBuilder().build();
assertSame(value1.getRepeatedInt32List(), value2.getRepeatedInt32List());
- assertSame(value1.getRepeatedImportEnumList(),
- value2.getRepeatedImportEnumList());
assertSame(value1.getRepeatedForeignMessageList(),
value2.getRepeatedForeignMessageList());
}
@@ -1512,4 +1509,142 @@ public class GeneratedMessageTest extends TestCase {
assertEquals(message2.getFooMessage().getQuxInt(), 234);
}
}
+
+ 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");
+
+ // Mutate the message with new field builder
+ // Mutate nested message
+ TestAllTypes.Builder builder1 = TestAllTypes.newBuilder();
+ 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);
+ FieldDescriptor subForeignFieldDescriptor1 =
+ foreignFieldBuilder1.getDescriptorForType().findFieldByName("c");
+ foreignFieldBuilder1.setField(subForeignFieldDescriptor1, 2);
+ builder1.addRepeatedField(foreignFieldDescriptor,
+ foreignFieldBuilder1.build());
+
+ // Mutate import message
+ Message.Builder importFieldBuilder1 = builder1.newBuilderForField(
+ importFieldDescriptor);
+ FieldDescriptor subImportFieldDescriptor1 =
+ importFieldBuilder1.getDescriptorForType().findFieldByName("d");
+ importFieldBuilder1.setField(subImportFieldDescriptor1, 3);
+ builder1.addRepeatedField(importFieldDescriptor,
+ importFieldBuilder1.build());
+
+ Message newMessage1 = builder1.build();
+
+ // Mutate the message with existing field builder
+ // Mutate nested message
+ TestAllTypes.Builder builder2 = TestAllTypes.newBuilder();
+ builder2.addRepeatedNestedMessageBuilder();
+ 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);
+ FieldDescriptor subForeignFieldDescriptor2 =
+ foreignFieldBuilder2.getDescriptorForType().findFieldByName("c");
+ foreignFieldBuilder2.setField(subForeignFieldDescriptor2, 2);
+ builder2.addRepeatedField(foreignFieldDescriptor,
+ foreignFieldBuilder2.build());
+
+ // Mutate import message
+ Message.Builder importFieldBuilder2 = builder2.newBuilderForField(
+ importFieldDescriptor);
+ FieldDescriptor subImportFieldDescriptor2 =
+ importFieldBuilder2.getDescriptorForType().findFieldByName("d");
+ importFieldBuilder2.setField(subImportFieldDescriptor2, 3);
+ builder2.addRepeatedField(importFieldDescriptor,
+ importFieldBuilder2.build());
+
+ Message newMessage2 = builder2.build();
+
+ // These two messages should be equal.
+ assertEquals(newMessage1, newMessage2);
+ }
+
+ public void testGetRepeatedFieldBuilderWithInitializedValue() {
+ Descriptor descriptor = TestAllTypes.getDescriptor();
+ FieldDescriptor fieldDescriptor =
+ descriptor.findFieldByName("repeated_nested_message");
+
+ // Before setting field, builder is initialized by default value.
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ builder.addRepeatedNestedMessageBuilder();
+ NestedMessage.Builder fieldBuilder =
+ (NestedMessage.Builder) builder.getRepeatedFieldBuilder(fieldDescriptor, 0);
+ assertEquals(0, fieldBuilder.getBb());
+
+ // Setting field value with new field builder instance.
+ builder = TestAllTypes.newBuilder();
+ NestedMessage.Builder newFieldBuilder =
+ builder.addRepeatedNestedMessageBuilder();
+ newFieldBuilder.setBb(2);
+ // Then get the field builder instance by getRepeatedFieldBuilder().
+ fieldBuilder =
+ (NestedMessage.Builder) builder.getRepeatedFieldBuilder(fieldDescriptor, 0);
+ // It should contain new value.
+ assertEquals(2, fieldBuilder.getBb());
+ // These two builder should be equal.
+ assertSame(fieldBuilder, newFieldBuilder);
+ }
+
+ public void testGetRepeatedFieldBuilderNotSupportedException() {
+ Descriptor descriptor = TestAllTypes.getDescriptor();
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ try {
+ builder.getRepeatedFieldBuilder(descriptor.findFieldByName("repeated_int32"), 0);
+ fail("Exception was not thrown");
+ } catch (UnsupportedOperationException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.getRepeatedFieldBuilder(
+ descriptor.findFieldByName("repeated_nested_enum"), 0);
+ fail("Exception was not thrown");
+ } catch (UnsupportedOperationException e) {
+ // We expect this exception.
+ }
+ try {
+ builder.getRepeatedFieldBuilder(descriptor.findFieldByName("optional_int32"), 0);
+ fail("Exception was not thrown");
+ } catch (UnsupportedOperationException e) {
+ // We expect this exception.
+ }
+ try {
+ 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);
+ fail("Exception was not thrown");
+ } catch (UnsupportedOperationException e) {
+ // We expect this exception.
+ }
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java b/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
index 00e3a843..9de794fe 100644
--- a/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
+++ b/java/src/test/java/com/google/protobuf/LazyMessageLiteTest.java
@@ -36,8 +36,6 @@ import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite;
import junit.framework.TestCase;
-import org.easymock.classextension.EasyMock;
-
import java.util.ArrayList;
/**
@@ -52,14 +50,10 @@ public class LazyMessageLiteTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
-
- originalLazyInnerMessageLiteParser = LazyInnerMessageLite.PARSER;
}
@Override
protected void tearDown() throws Exception {
- LazyInnerMessageLite.PARSER = originalLazyInnerMessageLiteParser;
-
super.tearDown();
}
@@ -291,29 +285,4 @@ public class LazyMessageLiteTest extends TestCase {
assertEquals(bytes, deserialized.toByteString());
}
-
- public void testLaziness() throws InvalidProtocolBufferException {
- LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder()
- .setNum(2)
- .build();
- LazyMessageLite outer = LazyMessageLite.newBuilder()
- .setNum(1)
- .setInner(inner)
- .setOneofInner(inner)
- .build();
- ByteString bytes = outer.toByteString();
-
-
- // The parser for inner / oneofInner message shouldn't be used if
- // getInner / getOneofInner is not called.
- LazyInnerMessageLite.PARSER = EasyMock.createStrictMock(Parser.class);
-
- EasyMock.replay(LazyInnerMessageLite.PARSER);
-
- LazyMessageLite deserialized = LazyMessageLite.parseFrom(bytes);
- assertEquals(1, deserialized.getNum());
- assertEquals(421, deserialized.getNumWithDefault());
-
- EasyMock.verify(LazyInnerMessageLite.PARSER);
- }
}
diff --git a/java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java b/java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java
index 94f4fcf5..035917c8 100644
--- a/java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java
+++ b/java/src/test/java/com/google/protobuf/LiteEqualsAndHashTest.java
@@ -82,4 +82,27 @@ public class LiteEqualsAndHashTest extends TestCase {
BarPrime barPrime = BarPrime.newBuilder().setName("bar").build();
assertFalse(bar.equals(barPrime));
}
+
+ public void testEqualsAndHashCodeWithUnknownFields() throws InvalidProtocolBufferException {
+ Foo fooWithOnlyValue = Foo.newBuilder()
+ .setValue(1)
+ .build();
+
+ Foo fooWithValueAndExtension = fooWithOnlyValue.toBuilder()
+ .setValue(1)
+ .setExtension(Bar.fooExt, Bar.newBuilder()
+ .setName("name")
+ .build())
+ .build();
+
+ Foo fooWithValueAndUnknownFields = Foo.parseFrom(fooWithValueAndExtension.toByteArray());
+
+ assertEqualsAndHashCodeAreFalse(fooWithOnlyValue, fooWithValueAndUnknownFields);
+ assertEqualsAndHashCodeAreFalse(fooWithValueAndExtension, fooWithValueAndUnknownFields);
+ }
+
+ private void assertEqualsAndHashCodeAreFalse(Object o1, Object o2) {
+ assertFalse(o1.equals(o2));
+ assertFalse(o1.hashCode() == o2.hashCode());
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/LiteTest.java b/java/src/test/java/com/google/protobuf/LiteTest.java
index 8bb30ad9..4d8037fc 100644
--- a/java/src/test/java/com/google/protobuf/LiteTest.java
+++ b/java/src/test/java/com/google/protobuf/LiteTest.java
@@ -145,4 +145,17 @@ public class LiteTest extends TestCase {
assertEquals(expected.getOptionalNestedMessage().getBb(),
actual.getOptionalNestedMessage().getBb());
}
+
+ public void testClone() {
+ TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder()
+ .setOptionalInt32(123);
+ assertEquals(
+ expected.getOptionalInt32(), expected.clone().getOptionalInt32());
+
+ TestAllExtensionsLite.Builder expected2 = TestAllExtensionsLite.newBuilder()
+ .setExtension(UnittestLite.optionalInt32ExtensionLite, 123);
+ assertEquals(
+ expected2.getExtension(UnittestLite.optionalInt32ExtensionLite),
+ expected2.clone().getExtension(UnittestLite.optionalInt32ExtensionLite));
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
index 475f7ffb..ff39ca3f 100644
--- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
+++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
@@ -32,9 +32,12 @@ package com.google.protobuf;
import junit.framework.TestCase;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
@@ -393,4 +396,17 @@ public class LiteralByteStringTest extends TestCase {
assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest,
ByteString.EMPTY.concat(stringUnderTest), stringUnderTest);
}
+
+ public void testJavaSerialization() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(out);
+ oos.writeObject(stringUnderTest);
+ oos.close();
+ byte[] pickled = out.toByteArray();
+ InputStream in = new ByteArrayInputStream(pickled);
+ ObjectInputStream ois = new ObjectInputStream(in);
+ Object o = ois.readObject();
+ assertTrue("Didn't get a ByteString back", o instanceof ByteString);
+ assertEquals("Should get an equal ByteString back", stringUnderTest, o);
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/MapForProto2LiteTest.java b/java/src/test/java/com/google/protobuf/MapForProto2LiteTest.java
new file mode 100644
index 00000000..22dbeb76
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/MapForProto2LiteTest.java
@@ -0,0 +1,277 @@
+// 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 map_lite_test.MapForProto2TestProto.TestMap;
+import map_lite_test.MapForProto2TestProto.TestMap.MessageValue;
+import map_lite_test.MapForProto2TestProto.TestUnknownEnumValue;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for map fields.
+ */
+public class MapForProto2LiteTest extends TestCase {
+ private void setMapValues(TestMap.Builder builder) {
+ builder.getMutableInt32ToInt32Field().put(1, 11);
+ builder.getMutableInt32ToInt32Field().put(2, 22);
+ builder.getMutableInt32ToInt32Field().put(3, 33);
+
+ builder.getMutableInt32ToStringField().put(1, "11");
+ builder.getMutableInt32ToStringField().put(2, "22");
+ builder.getMutableInt32ToStringField().put(3, "33");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11"));
+ builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22"));
+ builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO);
+ builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(11).build());
+ builder.getMutableInt32ToMessageField().put(
+ 2, MessageValue.newBuilder().setValue(22).build());
+ builder.getMutableInt32ToMessageField().put(
+ 3, MessageValue.newBuilder().setValue(33).build());
+
+ builder.getMutableStringToInt32Field().put("1", 11);
+ builder.getMutableStringToInt32Field().put("2", 22);
+ builder.getMutableStringToInt32Field().put("3", 33);
+ }
+
+ 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.getMutableInt32ToInt32Field().put(1, 111);
+ builder.getMutableInt32ToInt32Field().remove(2);
+ builder.getMutableInt32ToInt32Field().put(4, 44);
+
+ builder.getMutableInt32ToStringField().put(1, "111");
+ builder.getMutableInt32ToStringField().remove(2);
+ builder.getMutableInt32ToStringField().put(4, "44");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111"));
+ builder.getMutableInt32ToBytesField().remove(2);
+ builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().remove(2);
+ builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(111).build());
+ builder.getMutableInt32ToMessageField().remove(2);
+ builder.getMutableInt32ToMessageField().put(
+ 4, MessageValue.newBuilder().setValue(44).build());
+
+ builder.getMutableStringToInt32Field().put("1", 111);
+ builder.getMutableStringToInt32Field().remove("2");
+ builder.getMutableStringToInt32Field().put("4", 44);
+ }
+
+ 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(TestMap message) {
+ assertEquals(0, message.getInt32ToInt32Field().size());
+ assertEquals(0, message.getInt32ToStringField().size());
+ assertEquals(0, message.getInt32ToBytesField().size());
+ assertEquals(0, message.getInt32ToEnumField().size());
+ assertEquals(0, message.getInt32ToMessageField().size());
+ assertEquals(0, message.getStringToInt32Field().size());
+ }
+
+ 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();
+ message = builder.build();
+ assertMapValuesCleared(message);
+ }
+
+ 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);
+ }
+
+ 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();
+ b1.getMutableInt32ToInt32Field().put(1, 2);
+ b1.getMutableInt32ToInt32Field().put(3, 4);
+ b1.getMutableInt32ToInt32Field().put(5, 6);
+ TestMap m1 = b1.build();
+
+ TestMap.Builder b2 = TestMap.newBuilder();
+ b2.getMutableInt32ToInt32Field().put(5, 6);
+ b2.getMutableInt32ToInt32Field().put(1, 2);
+ b2.getMutableInt32ToInt32Field().put(3, 4);
+ TestMap m2 = b2.build();
+
+ assertEquals(m1, m2);
+ assertEquals(m1.hashCode(), m2.hashCode());
+
+ // Make sure we did compare map fields.
+ b2.getMutableInt32ToInt32Field().put(1, 0);
+ m2 = b2.build();
+ assertFalse(m1.equals(m2));
+ // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
+ // to be different.
+ }
+
+ public void testUnknownEnumValues() throws Exception {
+ TestUnknownEnumValue.Builder builder =
+ TestUnknownEnumValue.newBuilder();
+ builder.getMutableInt32ToInt32Field().put(1, 1);
+ builder.getMutableInt32ToInt32Field().put(2, 54321);
+ ByteString data = builder.build().toByteString();
+
+ TestMap message = TestMap.parseFrom(data);
+ // Entries with unknown enum values will be stored into UnknownFieldSet so
+ // there is only one entry in the map.
+ assertEquals(1, message.getInt32ToEnumField().size());
+ assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
+ // Serializing and parsing should preserve the unknown entry.
+ data = message.toByteString();
+ TestUnknownEnumValue messageWithUnknownEnums =
+ TestUnknownEnumValue.parseFrom(data);
+ assertEquals(2, messageWithUnknownEnums.getInt32ToInt32Field().size());
+ assertEquals(1, messageWithUnknownEnums.getInt32ToInt32Field().get(1).intValue());
+ assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue());
+ }
+
+}
diff --git a/java/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/src/test/java/com/google/protobuf/MapForProto2Test.java
new file mode 100644
index 00000000..33ba7150
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/MapForProto2Test.java
@@ -0,0 +1,502 @@
+// 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 map_test.MapForProto2TestProto.TestMap;
+import map_test.MapForProto2TestProto.TestMap.MessageValue;
+import map_test.MapForProto2TestProto.TestMap.MessageWithRequiredFields;
+import map_test.MapForProto2TestProto.TestUnknownEnumValue;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Unit tests for map fields in proto2 protos.
+ */
+public class MapForProto2Test extends TestCase {
+ private void setMapValues(TestMap.Builder builder) {
+ builder.getMutableInt32ToInt32Field().put(1, 11);
+ builder.getMutableInt32ToInt32Field().put(2, 22);
+ builder.getMutableInt32ToInt32Field().put(3, 33);
+
+ builder.getMutableInt32ToStringField().put(1, "11");
+ builder.getMutableInt32ToStringField().put(2, "22");
+ builder.getMutableInt32ToStringField().put(3, "33");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11"));
+ builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22"));
+ builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO);
+ builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(11).build());
+ builder.getMutableInt32ToMessageField().put(
+ 2, MessageValue.newBuilder().setValue(22).build());
+ builder.getMutableInt32ToMessageField().put(
+ 3, MessageValue.newBuilder().setValue(33).build());
+
+ builder.getMutableStringToInt32Field().put("1", 11);
+ builder.getMutableStringToInt32Field().put("2", 22);
+ builder.getMutableStringToInt32Field().put("3", 33);
+ }
+
+ 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.getMutableInt32ToInt32Field().put(1, 111);
+ builder.getMutableInt32ToInt32Field().remove(2);
+ builder.getMutableInt32ToInt32Field().put(4, 44);
+
+ builder.getMutableInt32ToStringField().put(1, "111");
+ builder.getMutableInt32ToStringField().remove(2);
+ builder.getMutableInt32ToStringField().put(4, "44");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111"));
+ builder.getMutableInt32ToBytesField().remove(2);
+ builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().remove(2);
+ builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(111).build());
+ builder.getMutableInt32ToMessageField().remove(2);
+ builder.getMutableInt32ToMessageField().put(
+ 4, MessageValue.newBuilder().setValue(44).build());
+
+ builder.getMutableStringToInt32Field().put("1", 111);
+ builder.getMutableStringToInt32Field().remove("2");
+ builder.getMutableStringToInt32Field().put("4", 44);
+ }
+
+ 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(TestMap message) {
+ assertEquals(0, message.getInt32ToInt32Field().size());
+ assertEquals(0, message.getInt32ToStringField().size());
+ assertEquals(0, message.getInt32ToBytesField().size());
+ assertEquals(0, message.getInt32ToEnumField().size());
+ assertEquals(0, message.getInt32ToMessageField().size());
+ assertEquals(0, message.getStringToInt32Field().size());
+ }
+
+ 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();
+ message = builder.build();
+ assertMapValuesCleared(message);
+ }
+
+ 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);
+ }
+
+ 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();
+ b1.getMutableInt32ToInt32Field().put(1, 2);
+ b1.getMutableInt32ToInt32Field().put(3, 4);
+ b1.getMutableInt32ToInt32Field().put(5, 6);
+ TestMap m1 = b1.build();
+
+ TestMap.Builder b2 = TestMap.newBuilder();
+ b2.getMutableInt32ToInt32Field().put(5, 6);
+ b2.getMutableInt32ToInt32Field().put(1, 2);
+ b2.getMutableInt32ToInt32Field().put(3, 4);
+ TestMap m2 = b2.build();
+
+ assertEquals(m1, m2);
+ assertEquals(m1.hashCode(), m2.hashCode());
+
+ // Make sure we did compare map fields.
+ b2.getMutableInt32ToInt32Field().put(1, 0);
+ m2 = b2.build();
+ assertFalse(m1.equals(m2));
+ // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
+ // to be different.
+ }
+
+
+ // The following methods are used to test reflection API.
+
+ private static FieldDescriptor f(String name) {
+ return TestMap.getDescriptor().findFieldByName(name);
+ }
+
+ private static Object getFieldValue(Message mapEntry, String name) {
+ FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
+ return mapEntry.getField(field);
+ }
+
+ private static Message.Builder setFieldValue(
+ Message.Builder mapEntry, String name, Object value) {
+ FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
+ mapEntry.setField(field, value);
+ return mapEntry;
+ }
+
+ private static void assertHasMapValues(Message message, String name, Map<?, ?> values) {
+ FieldDescriptor field = f(name);
+ for (Object entry : (List<?>) message.getField(field)) {
+ Message mapEntry = (Message) entry;
+ Object key = getFieldValue(mapEntry, "key");
+ Object value = getFieldValue(mapEntry, "value");
+ assertTrue(values.containsKey(key));
+ assertEquals(value, values.get(key));
+ }
+ assertEquals(values.size(), message.getRepeatedFieldCount(field));
+ for (int i = 0; i < message.getRepeatedFieldCount(field); i++) {
+ Message mapEntry = (Message) message.getRepeatedField(field, i);
+ Object key = getFieldValue(mapEntry, "key");
+ Object value = getFieldValue(mapEntry, "value");
+ assertTrue(values.containsKey(key));
+ assertEquals(value, values.get(key));
+ }
+ }
+
+ private static <KeyType, ValueType>
+ Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) {
+ FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
+ Message.Builder entryBuilder = builder.newBuilderForField(field);
+ FieldDescriptor keyField = entryBuilder.getDescriptorForType().findFieldByName("key");
+ FieldDescriptor valueField = entryBuilder.getDescriptorForType().findFieldByName("value");
+ entryBuilder.setField(keyField, key);
+ entryBuilder.setField(valueField, value);
+ return entryBuilder.build();
+ }
+
+ private static void setMapValues(Message.Builder builder, String name, Map<?, ?> values) {
+ List<Message> entryList = new ArrayList<Message>();
+ for (Map.Entry<?, ?> entry : values.entrySet()) {
+ entryList.add(newMapEntry(builder, name, entry.getKey(), entry.getValue()));
+ }
+ FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
+ builder.setField(field, entryList);
+ }
+
+ private static <KeyType, ValueType>
+ Map<KeyType, ValueType> mapForValues(
+ KeyType key1, ValueType value1, KeyType key2, ValueType value2) {
+ Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>();
+ map.put(key1, value1);
+ map.put(key2, value2);
+ return map;
+ }
+
+ public void testReflectionApi() throws Exception {
+ // In reflection API, map fields are just repeated message fields.
+ TestMap.Builder builder = TestMap.newBuilder();
+ builder.getMutableInt32ToInt32Field().put(1, 2);
+ builder.getMutableInt32ToInt32Field().put(3, 4);
+ builder.getMutableInt32ToMessageField().put(
+ 11, MessageValue.newBuilder().setValue(22).build());
+ builder.getMutableInt32ToMessageField().put(
+ 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",
+ mapForValues(
+ 11, MessageValue.newBuilder().setValue(22).build(),
+ 33, MessageValue.newBuilder().setValue(44).build()));
+
+ // Test clearField()
+ builder.clearField(f("int32_to_int32_field"));
+ builder.clearField(f("int32_to_message_field"));
+ message = builder.build();
+ assertEquals(0, message.getInt32ToInt32Field().size());
+ assertEquals(0, message.getInt32ToMessageField().size());
+
+ // Test setField()
+ 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()));
+ message = builder.build();
+ assertEquals(22, message.getInt32ToInt32Field().get(11).intValue());
+ assertEquals(44, message.getInt32ToInt32Field().get(33).intValue());
+ assertEquals(222, message.getInt32ToMessageField().get(111).getValue());
+ 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,
+ 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,
+ MessageValue.newBuilder().setValue(555).build()));
+ message = builder.build();
+ assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
+ assertEquals(555, message.getInt32ToMessageField().get(555).getValue());
+
+ // Test setRepeatedField
+ for (int i = 0; i < builder.getRepeatedFieldCount(f("int32_to_int32_field")); i++) {
+ Message mapEntry = (Message) builder.getRepeatedField(f("int32_to_int32_field"), i);
+ int oldKey = ((Integer) getFieldValue(mapEntry, "key")).intValue();
+ int oldValue = ((Integer) getFieldValue(mapEntry, "value")).intValue();
+ // Swap key with value for each entry.
+ Message.Builder mapEntryBuilder = mapEntry.toBuilder();
+ setFieldValue(mapEntryBuilder, "key", oldValue);
+ setFieldValue(mapEntryBuilder, "value", oldKey);
+ builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.build());
+ }
+ message = builder.build();
+ assertEquals(11, message.getInt32ToInt32Field().get(22).intValue());
+ assertEquals(33, message.getInt32ToInt32Field().get(44).intValue());
+ assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
+ }
+
+ public void testTextFormat() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ setMapValues(builder);
+ TestMap message = builder.build();
+
+ String textData = TextFormat.printToString(message);
+
+ builder = TestMap.newBuilder();
+ TextFormat.merge(textData, builder);
+ message = builder.build();
+
+ assertMapValuesSet(message);
+ }
+
+ public void testDynamicMessage() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ setMapValues(builder);
+ TestMap message = builder.build();
+
+ Message dynamicDefaultInstance =
+ DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
+ Message dynamicMessage = dynamicDefaultInstance
+ .newBuilderForType().mergeFrom(message.toByteString()).build();
+
+ assertEquals(message, dynamicMessage);
+ assertEquals(message.hashCode(), dynamicMessage.hashCode());
+ }
+
+ public void testReflectionEqualsAndHashCode() throws Exception {
+ // Test that generated equals() and hashCode() will disregard the order
+ // of map entries when comparing/hashing map fields.
+
+ // We use DynamicMessage to test reflection based equals()/hashCode().
+ Message dynamicDefaultInstance =
+ DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
+ FieldDescriptor field = f("int32_to_int32_field");
+
+ Message.Builder b1 = dynamicDefaultInstance.newBuilderForType();
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 1, 2));
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 3, 4));
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 5, 6));
+ Message m1 = b1.build();
+
+ Message.Builder b2 = dynamicDefaultInstance.newBuilderForType();
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 5, 6));
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 1, 2));
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 3, 4));
+ Message m2 = b2.build();
+
+ assertEquals(m1, m2);
+ assertEquals(m1.hashCode(), m2.hashCode());
+
+ // Make sure we did compare map fields.
+ b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0));
+ m2 = b2.build();
+ assertFalse(m1.equals(m2));
+ // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
+ // to be different.
+ }
+
+ public void testUnknownEnumValues() throws Exception {
+ TestUnknownEnumValue.Builder builder =
+ TestUnknownEnumValue.newBuilder();
+ builder.getMutableInt32ToInt32Field().put(1, 1);
+ builder.getMutableInt32ToInt32Field().put(2, 54321);
+ ByteString data = builder.build().toByteString();
+
+ TestMap message = TestMap.parseFrom(data);
+ // Entries with unknown enum values will be stored into UnknownFieldSet so
+ // there is only one entry in the map.
+ assertEquals(1, message.getInt32ToEnumField().size());
+ assertEquals(TestMap.EnumValue.BAR, message.getInt32ToEnumField().get(1));
+ // UnknownFieldSet should not be empty.
+ assertFalse(message.getUnknownFields().asMap().isEmpty());
+ // Serializing and parsing should preserve the unknown entry.
+ data = message.toByteString();
+ TestUnknownEnumValue messageWithUnknownEnums =
+ TestUnknownEnumValue.parseFrom(data);
+ assertEquals(2, messageWithUnknownEnums.getInt32ToInt32Field().size());
+ assertEquals(1, messageWithUnknownEnums.getInt32ToInt32Field().get(1).intValue());
+ assertEquals(54321, messageWithUnknownEnums.getInt32ToInt32Field().get(2).intValue());
+ }
+
+
+ public void testRequiredMessage() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ builder.getMutableRequiredMessageMap().put(0,
+ MessageWithRequiredFields.newBuilder().buildPartial());
+ TestMap message = builder.buildPartial();
+ assertFalse(message.isInitialized());
+
+ builder.getMutableRequiredMessageMap().put(0,
+ MessageWithRequiredFields.newBuilder().setValue(1).build());
+ message = builder.build();
+ assertTrue(message.isInitialized());
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/MapTest.java b/java/src/test/java/com/google/protobuf/MapTest.java
new file mode 100644
index 00000000..9a25e302
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/MapTest.java
@@ -0,0 +1,576 @@
+// 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.Descriptor;
+import com.google.protobuf.Descriptors.EnumDescriptor;
+import com.google.protobuf.Descriptors.EnumValueDescriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor;
+import map_test.MapTestProto.TestMap;
+import map_test.MapTestProto.TestMap.MessageValue;
+import map_test.MapTestProto.TestOnChangeEventPropagation;
+
+import junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Unit tests for map fields.
+ */
+public class MapTest extends TestCase {
+ private void setMapValues(TestMap.Builder builder) {
+ builder.getMutableInt32ToInt32Field().put(1, 11);
+ builder.getMutableInt32ToInt32Field().put(2, 22);
+ builder.getMutableInt32ToInt32Field().put(3, 33);
+
+ builder.getMutableInt32ToStringField().put(1, "11");
+ builder.getMutableInt32ToStringField().put(2, "22");
+ builder.getMutableInt32ToStringField().put(3, "33");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("11"));
+ builder.getMutableInt32ToBytesField().put(2, TestUtil.toBytes("22"));
+ builder.getMutableInt32ToBytesField().put(3, TestUtil.toBytes("33"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.FOO);
+ builder.getMutableInt32ToEnumField().put(2, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().put(3, TestMap.EnumValue.BAZ);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(11).build());
+ builder.getMutableInt32ToMessageField().put(
+ 2, MessageValue.newBuilder().setValue(22).build());
+ builder.getMutableInt32ToMessageField().put(
+ 3, MessageValue.newBuilder().setValue(33).build());
+
+ builder.getMutableStringToInt32Field().put("1", 11);
+ builder.getMutableStringToInt32Field().put("2", 22);
+ builder.getMutableStringToInt32Field().put("3", 33);
+ }
+
+ 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.getMutableInt32ToInt32Field().put(1, 111);
+ builder.getMutableInt32ToInt32Field().remove(2);
+ builder.getMutableInt32ToInt32Field().put(4, 44);
+
+ builder.getMutableInt32ToStringField().put(1, "111");
+ builder.getMutableInt32ToStringField().remove(2);
+ builder.getMutableInt32ToStringField().put(4, "44");
+
+ builder.getMutableInt32ToBytesField().put(1, TestUtil.toBytes("111"));
+ builder.getMutableInt32ToBytesField().remove(2);
+ builder.getMutableInt32ToBytesField().put(4, TestUtil.toBytes("44"));
+
+ builder.getMutableInt32ToEnumField().put(1, TestMap.EnumValue.BAR);
+ builder.getMutableInt32ToEnumField().remove(2);
+ builder.getMutableInt32ToEnumField().put(4, TestMap.EnumValue.QUX);
+
+ builder.getMutableInt32ToMessageField().put(
+ 1, MessageValue.newBuilder().setValue(111).build());
+ builder.getMutableInt32ToMessageField().remove(2);
+ builder.getMutableInt32ToMessageField().put(
+ 4, MessageValue.newBuilder().setValue(44).build());
+
+ builder.getMutableStringToInt32Field().put("1", 111);
+ builder.getMutableStringToInt32Field().remove("2");
+ builder.getMutableStringToInt32Field().put("4", 44);
+ }
+
+ 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(TestMap message) {
+ assertEquals(0, message.getInt32ToInt32Field().size());
+ assertEquals(0, message.getInt32ToStringField().size());
+ assertEquals(0, message.getInt32ToBytesField().size());
+ assertEquals(0, message.getInt32ToEnumField().size());
+ assertEquals(0, message.getInt32ToMessageField().size());
+ assertEquals(0, message.getStringToInt32Field().size());
+ }
+
+ 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();
+ message = builder.build();
+ assertMapValuesCleared(message);
+ }
+
+ 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);
+ }
+
+ 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();
+ b1.getMutableInt32ToInt32Field().put(1, 2);
+ b1.getMutableInt32ToInt32Field().put(3, 4);
+ b1.getMutableInt32ToInt32Field().put(5, 6);
+ TestMap m1 = b1.build();
+
+ TestMap.Builder b2 = TestMap.newBuilder();
+ b2.getMutableInt32ToInt32Field().put(5, 6);
+ b2.getMutableInt32ToInt32Field().put(1, 2);
+ b2.getMutableInt32ToInt32Field().put(3, 4);
+ TestMap m2 = b2.build();
+
+ assertEquals(m1, m2);
+ assertEquals(m1.hashCode(), m2.hashCode());
+
+ // Make sure we did compare map fields.
+ b2.getMutableInt32ToInt32Field().put(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.getMutableInt32ToInt32Field().remove(1);
+ m2 = b2.build();
+ assertFalse(m1.equals(m2));
+ assertFalse(m2.equals(m1));
+ }
+
+
+ public void testNestedBuilderOnChangeEventPropagation() {
+ TestOnChangeEventPropagation.Builder parent =
+ TestOnChangeEventPropagation.newBuilder();
+ parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 2);
+ TestOnChangeEventPropagation message = parent.build();
+ assertEquals(2, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
+
+ // Make a change using nested builder.
+ parent.getOptionalMessageBuilder().getMutableInt32ToInt32Field().put(1, 3);
+
+ // Should be able to observe the change.
+ message = parent.build();
+ assertEquals(3, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
+
+ // Make another change using mergeFrom()
+ TestMap.Builder other = TestMap.newBuilder();
+ other.getMutableInt32ToInt32Field().put(1, 4);
+ parent.getOptionalMessageBuilder().mergeFrom(other.build());
+
+ // Should be able to observe the change.
+ message = parent.build();
+ assertEquals(4, message.getOptionalMessage().getInt32ToInt32Field().get(1).intValue());
+
+ // Make yet another change by clearing the nested builder.
+ parent.getOptionalMessageBuilder().clear();
+
+ // Should be able to observe the change.
+ message = parent.build();
+ assertEquals(0, message.getOptionalMessage().getInt32ToInt32Field().size());
+ }
+
+ // The following methods are used to test reflection API.
+
+ private static FieldDescriptor f(String name) {
+ return TestMap.getDescriptor().findFieldByName(name);
+ }
+
+ private static Object getFieldValue(Message mapEntry, String name) {
+ FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
+ return mapEntry.getField(field);
+ }
+
+ private static Message.Builder setFieldValue(
+ Message.Builder mapEntry, String name, Object value) {
+ FieldDescriptor field = mapEntry.getDescriptorForType().findFieldByName(name);
+ mapEntry.setField(field, value);
+ return mapEntry;
+ }
+
+ private static void assertHasMapValues(Message message, String name, Map<?, ?> values) {
+ FieldDescriptor field = f(name);
+ for (Object entry : (List<?>) message.getField(field)) {
+ Message mapEntry = (Message) entry;
+ Object key = getFieldValue(mapEntry, "key");
+ Object value = getFieldValue(mapEntry, "value");
+ assertTrue(values.containsKey(key));
+ assertEquals(value, values.get(key));
+ }
+ assertEquals(values.size(), message.getRepeatedFieldCount(field));
+ for (int i = 0; i < message.getRepeatedFieldCount(field); i++) {
+ Message mapEntry = (Message) message.getRepeatedField(field, i);
+ Object key = getFieldValue(mapEntry, "key");
+ Object value = getFieldValue(mapEntry, "value");
+ assertTrue(values.containsKey(key));
+ assertEquals(value, values.get(key));
+ }
+ }
+
+ private static <KeyType, ValueType>
+ Message newMapEntry(Message.Builder builder, String name, KeyType key, ValueType value) {
+ FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
+ Message.Builder entryBuilder = builder.newBuilderForField(field);
+ FieldDescriptor keyField = entryBuilder.getDescriptorForType().findFieldByName("key");
+ FieldDescriptor valueField = entryBuilder.getDescriptorForType().findFieldByName("value");
+ entryBuilder.setField(keyField, key);
+ entryBuilder.setField(valueField, value);
+ return entryBuilder.build();
+ }
+
+ private static void setMapValues(Message.Builder builder, String name, Map<?, ?> values) {
+ List<Message> entryList = new ArrayList<Message>();
+ for (Map.Entry<?, ?> entry : values.entrySet()) {
+ entryList.add(newMapEntry(builder, name, entry.getKey(), entry.getValue()));
+ }
+ FieldDescriptor field = builder.getDescriptorForType().findFieldByName(name);
+ builder.setField(field, entryList);
+ }
+
+ private static <KeyType, ValueType>
+ Map<KeyType, ValueType> mapForValues(
+ KeyType key1, ValueType value1, KeyType key2, ValueType value2) {
+ Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>();
+ map.put(key1, value1);
+ map.put(key2, value2);
+ return map;
+ }
+
+ public void testReflectionApi() throws Exception {
+ // In reflection API, map fields are just repeated message fields.
+ TestMap.Builder builder = TestMap.newBuilder();
+ builder.getMutableInt32ToInt32Field().put(1, 2);
+ builder.getMutableInt32ToInt32Field().put(3, 4);
+ builder.getMutableInt32ToMessageField().put(
+ 11, MessageValue.newBuilder().setValue(22).build());
+ builder.getMutableInt32ToMessageField().put(
+ 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",
+ mapForValues(
+ 11, MessageValue.newBuilder().setValue(22).build(),
+ 33, MessageValue.newBuilder().setValue(44).build()));
+
+ // Test clearField()
+ builder.clearField(f("int32_to_int32_field"));
+ builder.clearField(f("int32_to_message_field"));
+ message = builder.build();
+ assertEquals(0, message.getInt32ToInt32Field().size());
+ assertEquals(0, message.getInt32ToMessageField().size());
+
+ // Test setField()
+ 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()));
+ message = builder.build();
+ assertEquals(22, message.getInt32ToInt32Field().get(11).intValue());
+ assertEquals(44, message.getInt32ToInt32Field().get(33).intValue());
+ assertEquals(222, message.getInt32ToMessageField().get(111).getValue());
+ 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,
+ 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,
+ MessageValue.newBuilder().setValue(555).build()));
+ message = builder.build();
+ assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
+ assertEquals(555, message.getInt32ToMessageField().get(555).getValue());
+
+ // Test setRepeatedField
+ for (int i = 0; i < builder.getRepeatedFieldCount(f("int32_to_int32_field")); i++) {
+ Message mapEntry = (Message) builder.getRepeatedField(f("int32_to_int32_field"), i);
+ int oldKey = ((Integer) getFieldValue(mapEntry, "key")).intValue();
+ int oldValue = ((Integer) getFieldValue(mapEntry, "value")).intValue();
+ // Swap key with value for each entry.
+ Message.Builder mapEntryBuilder = mapEntry.toBuilder();
+ setFieldValue(mapEntryBuilder, "key", oldValue);
+ setFieldValue(mapEntryBuilder, "value", oldKey);
+ builder.setRepeatedField(f("int32_to_int32_field"), i, mapEntryBuilder.build());
+ }
+ message = builder.build();
+ assertEquals(11, message.getInt32ToInt32Field().get(22).intValue());
+ assertEquals(33, message.getInt32ToInt32Field().get(44).intValue());
+ assertEquals(55, message.getInt32ToInt32Field().get(55).intValue());
+ }
+
+ public void testTextFormat() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ setMapValues(builder);
+ TestMap message = builder.build();
+
+ String textData = TextFormat.printToString(message);
+
+ builder = TestMap.newBuilder();
+ TextFormat.merge(textData, builder);
+ message = builder.build();
+
+ assertMapValuesSet(message);
+ }
+
+ public void testDynamicMessage() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ setMapValues(builder);
+ TestMap message = builder.build();
+
+ Message dynamicDefaultInstance =
+ DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
+ Message dynamicMessage = dynamicDefaultInstance
+ .newBuilderForType().mergeFrom(message.toByteString()).build();
+
+ assertEquals(message, dynamicMessage);
+ assertEquals(message.hashCode(), dynamicMessage.hashCode());
+ }
+
+ public void testReflectionEqualsAndHashCode() throws Exception {
+ // Test that generated equals() and hashCode() will disregard the order
+ // of map entries when comparing/hashing map fields.
+
+ // We use DynamicMessage to test reflection based equals()/hashCode().
+ Message dynamicDefaultInstance =
+ DynamicMessage.getDefaultInstance(TestMap.getDescriptor());
+ FieldDescriptor field = f("int32_to_int32_field");
+
+ Message.Builder b1 = dynamicDefaultInstance.newBuilderForType();
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 1, 2));
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 3, 4));
+ b1.addRepeatedField(field, newMapEntry(b1, "int32_to_int32_field", 5, 6));
+ Message m1 = b1.build();
+
+ Message.Builder b2 = dynamicDefaultInstance.newBuilderForType();
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 5, 6));
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 1, 2));
+ b2.addRepeatedField(field, newMapEntry(b2, "int32_to_int32_field", 3, 4));
+ Message m2 = b2.build();
+
+ assertEquals(m1, m2);
+ assertEquals(m1.hashCode(), m2.hashCode());
+
+ // Make sure we did compare map fields.
+ b2.setRepeatedField(field, 0, newMapEntry(b1, "int32_to_int32_field", 0, 0));
+ m2 = b2.build();
+ assertFalse(m1.equals(m2));
+ // Don't check m1.hashCode() != m2.hashCode() because it's not guaranteed
+ // to be different.
+ }
+
+ public void testUnknownEnumValues() throws Exception {
+ TestMap.Builder builder = TestMap.newBuilder();
+ builder.getMutableInt32ToEnumFieldValue().put(0, 0);
+ builder.getMutableInt32ToEnumFieldValue().put(1, 1);
+ builder.getMutableInt32ToEnumFieldValue().put(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(1000, message.getInt32ToEnumFieldValue().get(2).intValue());
+
+ // 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.getMutableInt32ToEnumFieldValue().put(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.
+ assertTrue(message.getInt32ToEnumField().equals(message2.getInt32ToEnumField()));
+ }
+
+ public void testUnknownEnumValuesInReflectionApi() throws Exception {
+ Descriptor descriptor = TestMap.getDescriptor();
+ EnumDescriptor enumDescriptor = TestMap.EnumValue.getDescriptor();
+ FieldDescriptor field = descriptor.findFieldByName("int32_to_enum_field");
+
+ Map<Integer, Integer> data = new HashMap<Integer, Integer>();
+ data.put(0, 0);
+ data.put(1, 1);
+ data.put(2, 1000); // unknown value.
+
+ TestMap.Builder builder = TestMap.newBuilder();
+ for (Map.Entry<Integer, Integer> entry : data.entrySet()) {
+ builder.getMutableInt32ToEnumFieldValue().put(entry.getKey(), entry.getValue());
+ }
+
+ // Try to read unknown enum values using reflection API.
+ for (int i = 0; i < builder.getRepeatedFieldCount(field); i++) {
+ Message mapEntry = (Message) builder.getRepeatedField(field, i);
+ int key = ((Integer) getFieldValue(mapEntry, "key")).intValue();
+ int value = ((EnumValueDescriptor) getFieldValue(mapEntry, "value")).getNumber();
+ assertEquals(data.get(key).intValue(), value);
+ Message.Builder mapEntryBuilder = mapEntry.toBuilder();
+ // Increase the value by 1.
+ setFieldValue(mapEntryBuilder, "value",
+ enumDescriptor.findValueByNumberCreatingIfUnknown(value + 1));
+ builder.setRepeatedField(field, i, mapEntryBuilder.build());
+ }
+
+ // Verify that enum values have been successfully updated.
+ TestMap message = builder.build();
+ for (Map.Entry<Integer, Integer> entry : message.getInt32ToEnumFieldValue().entrySet()) {
+ assertEquals(data.get(entry.getKey()) + 1, entry.getValue().intValue());
+ }
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
index 9676f527..b3970196 100644
--- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
+++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java
@@ -30,6 +30,11 @@
package com.google.protobuf;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Iterator;
@@ -112,4 +117,17 @@ public class RopeByteStringTest extends LiteralByteStringTest {
assertEquals(classUnderTest + " string must must have same hashCode as the flat string",
flatString.hashCode(), unicode.hashCode());
}
+
+ public void testJavaSerialization() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(out);
+ oos.writeObject(stringUnderTest);
+ oos.close();
+ byte[] pickled = out.toByteArray();
+ InputStream in = new ByteArrayInputStream(pickled);
+ ObjectInputStream ois = new ObjectInputStream(in);
+ Object o = ois.readObject();
+ assertTrue("Didn't get a ByteString back", o instanceof ByteString);
+ assertEquals("Should get an equal ByteString back", stringUnderTest, o);
+ }
}
diff --git a/java/src/test/java/com/google/protobuf/TestBadIdentifiers.java b/java/src/test/java/com/google/protobuf/TestBadIdentifiers.java
index 614ac7fe..2c60fe0e 100644
--- a/java/src/test/java/com/google/protobuf/TestBadIdentifiers.java
+++ b/java/src/test/java/com/google/protobuf/TestBadIdentifiers.java
@@ -72,7 +72,7 @@ public class TestBadIdentifiers extends TestCase {
assertEquals(0, message.getMessageField5Count());
assertEquals(0, message.getInt32FieldCount11());
- assertEquals(1, message.getEnumFieldCount12().getNumber());
+ assertEquals(0, message.getEnumFieldCount12().getNumber());
assertEquals("", message.getStringFieldCount13());
assertEquals(ByteString.EMPTY, message.getBytesFieldCount14());
assertEquals(0, message.getMessageFieldCount15().getSerializedSize());
diff --git a/java/src/test/java/com/google/protobuf/TextFormatTest.java b/java/src/test/java/com/google/protobuf/TextFormatTest.java
index 152bdadc..eba06ca0 100644
--- a/java/src/test/java/com/google/protobuf/TextFormatTest.java
+++ b/java/src/test/java/com/google/protobuf/TextFormatTest.java
@@ -73,7 +73,7 @@ public class TextFormatTest extends TestCase {
private static String exoticText =
"repeated_int32: -1\n" +
"repeated_int32: -2147483648\n" +
- "repeated_int64: -1\n" +
+ "repeated_int64: -1,\n" +
"repeated_int64: -9223372036854775808\n" +
"repeated_uint32: 4294967295\n" +
"repeated_uint32: 2147483648\n" +
@@ -101,7 +101,7 @@ public class TextFormatTest extends TestCase {
private static String canonicalExoticText =
exoticText.replace(": .", ": 0.").replace(": -.", ": -0.") // short-form double
- .replace("23e", "23E").replace("E+", "E").replace("0.23E17", "2.3E16");
+ .replace("23e", "23E").replace("E+", "E").replace("0.23E17", "2.3E16").replace(",", "");
private String messageSetText =
"[protobuf_unittest.TestMessageSetExtension1] {\n" +
@@ -119,6 +119,7 @@ public class TextFormatTest extends TestCase {
" i: 456\n" +
"}\n";
+
private final TextFormat.Parser parserWithOverwriteForbidden =
TextFormat.Parser.newBuilder()
.setSingularOverwritePolicy(
@@ -460,6 +461,7 @@ public class TextFormatTest extends TestCase {
}
}
+
private void assertParseErrorWithOverwriteForbidden(String error,
String text) {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
@@ -553,10 +555,10 @@ public class TextFormatTest extends TestCase {
public void testEscape() throws Exception {
// Escape sequences.
- assertEquals("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
- TextFormat.escapeBytes(bytes("\0\001\007\b\f\n\r\t\013\\\'\"")));
- assertEquals("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
- TextFormat.escapeText("\0\001\007\b\f\n\r\t\013\\\'\""));
+ 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\\\'\"",
@@ -862,15 +864,15 @@ public class TextFormatTest extends TestCase {
assertEquals(message.getOptionalString(), builder.getOptionalString());
}
- public void testPrintToUnicodeStringWithNewlines() {
+ public void testPrintToUnicodeStringWithNewlines() throws Exception {
// No newlines at start and end
- assertEquals("optional_string: \"test newlines\n\nin\nstring\"\n",
+ assertEquals("optional_string: \"test newlines\\n\\nin\\nstring\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("test newlines\n\nin\nstring")
.build()));
// Newlines at start and end
- assertEquals("optional_string: \"\ntest\nnewlines\n\nin\nstring\n\"\n",
+ assertEquals("optional_string: \"\\ntest\\nnewlines\\n\\nin\\nstring\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\ntest\nnewlines\n\nin\nstring\n")
.build()));
@@ -880,14 +882,22 @@ public class TextFormatTest extends TestCase {
TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("")
.build()));
- assertEquals("optional_string: \"\n\"\n",
+ assertEquals("optional_string: \"\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\n")
.build()));
- assertEquals("optional_string: \"\n\n\"\n",
+ assertEquals("optional_string: \"\\n\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\n\n")
.build()));
+
+ // Test escaping roundtrip
+ TestAllTypes message = TestAllTypes.newBuilder()
+ .setOptionalString("\ntest\nnewlines\n\nin\nstring\n")
+ .build();
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ TextFormat.merge(TextFormat.printToUnicodeString(message), builder);
+ assertEquals(message.getOptionalString(), builder.getOptionalString());
}
public void testPrintToUnicodeString_unknown() {
@@ -900,6 +910,7 @@ public class TextFormatTest extends TestCase {
.build()));
}
+
public void testParseNonRepeatedFields() throws Exception {
assertParseSuccessWithOverwriteForbidden(
"repeated_int32: 1\n" +
diff --git a/java/src/test/java/com/google/protobuf/UnknownEnumValueTest.java b/java/src/test/java/com/google/protobuf/UnknownEnumValueTest.java
new file mode 100644
index 00000000..8f45976f
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/UnknownEnumValueTest.java
@@ -0,0 +1,255 @@
+// 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.Descriptor;
+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.TextFormat.ParseException;
+
+import junit.framework.TestCase;
+
+/**
+ * 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);
+ builder.addRepeatedNestedEnumValue(5432);
+ builder.addPackedNestedEnumValue(6543);
+ TestAllTypes message = builder.build();
+ assertEquals(4321, message.getOptionalNestedEnumValue());
+ assertEquals(5432, message.getRepeatedNestedEnumValue(0));
+ assertEquals(5432, message.getRepeatedNestedEnumValueList().get(0).intValue());
+ assertEquals(6543, message.getPackedNestedEnumValue(0));
+ // Returns UNRECOGNIZED if an enum type is requested.
+ assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getOptionalNestedEnum());
+ 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);
+ assertEquals(4321, message.getOptionalNestedEnumValue());
+ assertEquals(5432, message.getRepeatedNestedEnumValue(0));
+ assertEquals(5432, message.getRepeatedNestedEnumValueList().get(0).intValue());
+ assertEquals(6543, message.getPackedNestedEnumValue(0));
+ // Returns UNRECOGNIZED if an enum type is requested.
+ assertEquals(TestAllTypes.NestedEnum.UNRECOGNIZED, message.getOptionalNestedEnum());
+ 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));
+ // 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());
+ assertEquals(5432, builder.getRepeatedNestedEnumValue(0));
+ assertEquals(5432, builder.getRepeatedNestedEnumValueList().get(0).intValue());
+ 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 equals() and hashCode()
+ TestAllTypes sameMessage = builder.build();
+ assertEquals(message, sameMessage);
+ assertEquals(message.hashCode(), sameMessage.hashCode());
+
+ // Getting the numeric value of UNRECOGNIZED will throw an exception.
+ try {
+ TestAllTypes.NestedEnum.UNRECOGNIZED.getNumber();
+ fail("Exception is expected.");
+ } catch (IllegalArgumentException e) {
+ // Expected.
+ }
+
+ // Setting an enum field to an UNRECOGNIZED value will throw an exception.
+ try {
+ builder.setOptionalNestedEnum(builder.getOptionalNestedEnum());
+ fail("Exception is expected.");
+ } catch (IllegalArgumentException e) {
+ // Expected.
+ }
+ try {
+ builder.addRepeatedNestedEnum(builder.getOptionalNestedEnum());
+ fail("Exception is expected.");
+ } catch (IllegalArgumentException e) {
+ // Expected.
+ }
+ }
+
+ public void testUnknownEnumValueInReflectionApi() 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();
+
+ TestAllTypes.Builder builder = TestAllTypes.newBuilder();
+ 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);
+ EnumValueDescriptor unknown5432 =
+ (EnumValueDescriptor) message.getRepeatedField(repeatedNestedEnumField, 0);
+ EnumValueDescriptor unknown6543 =
+ (EnumValueDescriptor) message.getRepeatedField(packedNestedEnumField, 0);
+ assertEquals(4321, unknown4321.getNumber());
+ assertEquals(5432, unknown5432.getNumber());
+ assertEquals(6543, unknown6543.getNumber());
+
+ // Unknown EnumValueDescriptor will map to UNRECOGNIZED.
+ assertEquals(
+ TestAllTypes.NestedEnum.valueOf(unknown4321),
+ TestAllTypes.NestedEnum.UNRECOGNIZED);
+ assertEquals(
+ TestAllTypes.NestedEnum.valueOf(unknown5432),
+ TestAllTypes.NestedEnum.UNRECOGNIZED);
+ assertEquals(
+ TestAllTypes.NestedEnum.valueOf(unknown6543),
+ TestAllTypes.NestedEnum.UNRECOGNIZED);
+
+ // Setters also accept unknown EnumValueDescriptor.
+ builder.setField(optionalNestedEnumField, unknown6543);
+ builder.setRepeatedField(repeatedNestedEnumField, 0, unknown4321);
+ builder.setRepeatedField(packedNestedEnumField, 0, unknown5432);
+ 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));
+ }
+
+ 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));
+ Message message = builder.build();
+ assertEquals(4321,
+ ((EnumValueDescriptor) message.getField(optionalNestedEnumField)).getNumber());
+ assertEquals(5432,
+ ((EnumValueDescriptor) message.getRepeatedField(repeatedNestedEnumField, 0)).getNumber());
+ 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,
+ ((EnumValueDescriptor) message.getRepeatedField(repeatedNestedEnumField, 0)).getNumber());
+ 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));
+ Message sameMessage = builder.build();
+ assertEquals(message, sameMessage);
+ assertEquals(message.hashCode(), sameMessage.hashCode());
+ 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);
+ 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);
+
+ // Parsing unknown enum values will fail just like parsing other kinds of
+ // unknown fields.
+ try {
+ TextFormat.merge(textData, builder);
+ fail();
+ } catch (ParseException e) {
+ // expected.
+ }
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java
new file mode 100644
index 00000000..cec3da1e
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java
@@ -0,0 +1,316 @@
+// 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 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 junit.framework.TestCase;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * Tests for {@link UnknownFieldSetLite}.
+ *
+ * @author dweis@google.com (Daniel Weis)
+ */
+public class UnknownFieldSetLiteTest extends TestCase {
+
+ public void testNoDataIsDefaultInstance() {
+ assertSame(
+ UnknownFieldSetLite.getDefaultInstance(),
+ UnknownFieldSetLite.newBuilder()
+ .build());
+ }
+
+ public void testDefaultInstance() {
+ UnknownFieldSetLite unknownFields = UnknownFieldSetLite.getDefaultInstance();
+
+ assertEquals(0, unknownFields.getSerializedSize());
+ assertEquals(ByteString.EMPTY, toByteString(unknownFields));
+ }
+
+ public void testMergeFieldFrom() throws IOException {
+ Foo foo = Foo.newBuilder()
+ .setValue(2)
+ .build();
+
+ CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray());
+
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeFieldFrom(input.readTag(), input);
+
+ assertEquals(foo.toByteString(), toByteString(builder.build()));
+ }
+
+ public void testSerializedSize() throws IOException {
+ Foo foo = Foo.newBuilder()
+ .setValue(2)
+ .build();
+
+ CodedInputStream input = CodedInputStream.newInstance(foo.toByteArray());
+
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeFieldFrom(input.readTag(), input);
+
+ assertEquals(foo.toByteString().size(), builder.build().getSerializedSize());
+ }
+
+ public void testMergeVarintField() throws IOException {
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeVarintField(10, 2);
+
+ CodedInputStream input =
+ CodedInputStream.newInstance(toByteString(builder.build()).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 builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeVarintField(10, -6);
+
+ CodedInputStream input =
+ CodedInputStream.newInstance(toByteString(builder.build()).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.Builder builder1 = UnknownFieldSetLite.newBuilder();
+ builder1.mergeVarintField(10, 2);
+ UnknownFieldSetLite unknownFields1 = builder1.build();
+
+ UnknownFieldSetLite.Builder builder2 = UnknownFieldSetLite.newBuilder();
+ builder2.mergeVarintField(10, 2);
+ UnknownFieldSetLite unknownFields2 = builder2.build();
+
+ assertEquals(unknownFields1, unknownFields2);
+ assertEquals(unknownFields1.hashCode(), unknownFields2.hashCode());
+ assertFalse(unknownFields1.equals(UnknownFieldSetLite.getDefaultInstance()));
+ assertFalse(unknownFields1.hashCode() == UnknownFieldSetLite.getDefaultInstance().hashCode());
+ }
+
+ public void testConcat() throws IOException {
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeVarintField(10, 2);
+ UnknownFieldSetLite unknownFields = builder.build();
+
+ unknownFields = UnknownFieldSetLite.concat(unknownFields, unknownFields);
+
+ 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 testConcat_empty() {
+ UnknownFieldSetLite unknownFields = UnknownFieldSetLite.concat(
+ UnknownFieldSetLite.getDefaultInstance(), UnknownFieldSetLite.getDefaultInstance());
+
+ assertEquals(0, unknownFields.getSerializedSize());
+ assertEquals(ByteString.EMPTY, toByteString(unknownFields));
+ }
+
+ public void testBuilderReuse() throws IOException {
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.mergeVarintField(10, 2);
+ builder.build();
+
+ try {
+ builder.build();
+ fail();
+ } catch (IllegalStateException e) {
+ // Expected.
+ }
+
+ try {
+ builder.mergeFieldFrom(0, CodedInputStream.newInstance(new byte[0]));
+ fail();
+ } catch (IllegalStateException e) {
+ // Expected.
+ }
+
+ try {
+ builder.mergeVarintField(5, 1);
+ fail();
+ } catch (IllegalStateException e) {
+ // Expected.
+ }
+ }
+
+ public void testBuilderReuse_empty() {
+ UnknownFieldSetLite.Builder builder = UnknownFieldSetLite.newBuilder();
+ builder.build();
+
+ try {
+ builder.build();
+ fail();
+ } catch (IllegalStateException e) {
+ // Expected.
+ }
+ }
+
+ 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("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.
+ }
+ }
+
+ 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());
+ }
+}
diff --git a/java/src/test/java/com/google/protobuf/field_presence_test.proto b/java/src/test/java/com/google/protobuf/field_presence_test.proto
new file mode 100644
index 00000000..8d8ea8ef
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/field_presence_test.proto
@@ -0,0 +1,93 @@
+// 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 field_presence_test;
+
+import "google/protobuf/unittest.proto";
+
+option java_package = "com.google.protobuf";
+option java_outer_classname = "FieldPresenceTestProto";
+option java_generate_equals_and_hash = true;
+
+message TestAllTypes {
+ enum NestedEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ }
+ message NestedMessage {
+ optional int32 value = 1;
+ }
+
+ optional int32 optional_int32 = 1;
+ optional string optional_string = 2;
+ optional bytes optional_bytes = 3;
+ optional NestedEnum optional_nested_enum = 4;
+ optional NestedMessage optional_nested_message = 5;
+ optional protobuf_unittest.TestRequired optional_proto2_message = 6;
+
+ oneof oneof_field {
+ int32 oneof_int32 = 11;
+ uint32 oneof_uint32 = 12;
+ string oneof_string = 13;
+ bytes oneof_bytes = 14;
+ NestedEnum oneof_nested_enum = 15;
+ NestedMessage oneof_nested_message = 16;
+ protobuf_unittest.TestRequired oneof_proto2_message = 17;
+ }
+
+ repeated int32 repeated_int32 = 21;
+ repeated string repeated_string = 22;
+ repeated bytes repeated_bytes = 23;
+ repeated NestedEnum repeated_nested_enum = 24;
+ repeated NestedMessage repeated_nested_message = 25;
+ repeated protobuf_unittest.TestRequired repeated_proto2_message = 26;
+ repeated NestedEnum packed_nested_enum = 27 [packed = true];
+}
+
+message TestOptionalFieldsOnly {
+ optional int32 optional_int32 = 1;
+ optional string optional_string = 2;
+ optional bytes optional_bytes = 3;
+ optional TestAllTypes.NestedEnum optional_nested_enum = 4;
+ optional TestAllTypes.NestedMessage optional_nested_message = 5;
+ optional protobuf_unittest.TestRequired optional_proto2_message = 6;
+}
+
+message TestRepeatedFieldsOnly {
+ repeated int32 repeated_int32 = 21;
+ repeated string repeated_string = 22;
+ repeated bytes repeated_bytes = 23;
+ repeated TestAllTypes.NestedEnum repeated_nested_enum = 24;
+ repeated TestAllTypes.NestedMessage repeated_nested_message = 25;
+ repeated protobuf_unittest.TestRequired repeated_proto2_message = 26;
+}
diff --git a/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto b/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
index baed4e10..015dc267 100644
--- a/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
+++ b/java/src/test/java/com/google/protobuf/lazy_fields_lite.proto
@@ -32,6 +32,7 @@
//
// A proto file with lazy fields
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto b/java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto
index 68615672..86837250 100644
--- a/java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto
+++ b/java/src/test/java/com/google/protobuf/lite_equals_and_hash.proto
@@ -30,6 +30,7 @@
// Author: pbogle@google.com (Phil Bogle)
+syntax = "proto2";
package protobuf_unittest.lite_equals_and_hash;
@@ -41,9 +42,15 @@ option optimize_for = LITE_RUNTIME;
message Foo {
optional int32 value = 1;
repeated Bar bar = 2;
+
+ extensions 100 to max;
}
message Bar {
+ extend Foo {
+ optional Bar foo_ext = 100;
+ }
+
optional string name = 1;
}
@@ -53,3 +60,13 @@ message BarPrime {
message Empty {
}
+
+extend Foo {
+ optional int32 varint = 101;
+ optional fixed32 fixed32 = 102;
+ optional fixed64 fixed64 = 103;
+ optional group MyGroup = 104 {
+ optional string group_value = 1;
+ }
+}
+
diff --git a/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto b/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto
new file mode 100644
index 00000000..a1fe856c
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/map_for_proto2_lite_test.proto
@@ -0,0 +1,68 @@
+// 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";
+
+
+option java_outer_classname = "MapForProto2TestProto";
+option java_generate_equals_and_hash = true;
+
+message TestMap {
+ message MessageValue {
+ optional int32 value = 1;
+ }
+ enum EnumValue {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ QUX = 3;
+ }
+
+ map<int32, int32> int32_to_int32_field = 1;
+ map<int32, string> int32_to_string_field = 2;
+ map<int32, bytes> int32_to_bytes_field = 3;
+ map<int32, EnumValue> int32_to_enum_field = 4;
+ map<int32, MessageValue> int32_to_message_field = 5;
+ map<string, int32> string_to_int32_field = 6;
+
+ message MessageWithRequiredFields {
+ required int32 value = 1;
+ }
+ map<int32, MessageWithRequiredFields> required_message_map = 11;
+}
+
+message TestUnknownEnumValue {
+ // Wire-compatible with TestMap.int32_to_enum_field so we can test the
+ // parsing behavior of TestMap regarding unknown enum values.
+ map<int32, int32> int32_to_int32_field = 4;
+}
+package map_for_proto2_lite_test;
+option java_package = "map_lite_test";
+option optimize_for = LITE_RUNTIME;
diff --git a/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto b/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto
new file mode 100644
index 00000000..a0ec7ac5
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/map_for_proto2_test.proto
@@ -0,0 +1,67 @@
+// 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 map_for_proto2_test;
+
+option java_package = "map_test";
+option java_outer_classname = "MapForProto2TestProto";
+option java_generate_equals_and_hash = true;
+
+message TestMap {
+ message MessageValue {
+ optional int32 value = 1;
+ }
+ enum EnumValue {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ QUX = 3;
+ }
+
+ map<int32, int32> int32_to_int32_field = 1;
+ map<int32, string> int32_to_string_field = 2;
+ map<int32, bytes> int32_to_bytes_field = 3;
+ map<int32, EnumValue> int32_to_enum_field = 4;
+ map<int32, MessageValue> int32_to_message_field = 5;
+ map<string, int32> string_to_int32_field = 6;
+
+ message MessageWithRequiredFields {
+ required int32 value = 1;
+ }
+ map<int32, MessageWithRequiredFields> required_message_map = 11;
+}
+
+message TestUnknownEnumValue {
+ // Wire-compatible with TestMap.int32_to_enum_field so we can test the
+ // parsing behavior of TestMap regarding unknown enum values.
+ map<int32, int32> int32_to_int32_field = 4;
+}
diff --git a/java/src/test/java/com/google/protobuf/map_test.proto b/java/src/test/java/com/google/protobuf/map_test.proto
new file mode 100644
index 00000000..105ee3f8
--- /dev/null
+++ b/java/src/test/java/com/google/protobuf/map_test.proto
@@ -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.
+
+syntax = "proto3";
+
+package map_test;
+
+option java_package = "map_test";
+option java_outer_classname = "MapTestProto";
+option java_generate_equals_and_hash = true;
+
+message TestMap {
+ message MessageValue {
+ optional int32 value = 1;
+ }
+ enum EnumValue {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ QUX = 3;
+ }
+
+ map<int32, int32> int32_to_int32_field = 1;
+ map<int32, string> int32_to_string_field = 2;
+ map<int32, bytes> int32_to_bytes_field = 3;
+ map<int32, EnumValue> int32_to_enum_field = 4;
+ map<int32, MessageValue> int32_to_message_field = 5;
+ map<string, int32> string_to_int32_field = 6;
+}
+
+// Used to test that a nested bulider containing map fields will properly
+// propagate the onChange event and mark its parent dirty when a change
+// is made to a map field.
+message TestOnChangeEventPropagation {
+ optional TestMap optional_message = 1;
+}
diff --git a/java/src/test/java/com/google/protobuf/multiple_files_test.proto b/java/src/test/java/com/google/protobuf/multiple_files_test.proto
index 7ec89a05..92790506 100644
--- a/java/src/test/java/com/google/protobuf/multiple_files_test.proto
+++ b/java/src/test/java/com/google/protobuf/multiple_files_test.proto
@@ -32,6 +32,7 @@
//
// A proto file which tests the java_multiple_files option.
+syntax = "proto2";
// Some generic_services option(s) added automatically.
// See: http://go/proto2-generic-services-default
diff --git a/java/src/test/java/com/google/protobuf/nested_builders_test.proto b/java/src/test/java/com/google/protobuf/nested_builders_test.proto
index 8ab4a433..a5dd66d8 100644
--- a/java/src/test/java/com/google/protobuf/nested_builders_test.proto
+++ b/java/src/test/java/com/google/protobuf/nested_builders_test.proto
@@ -30,6 +30,7 @@
// Author: jonp@google.com (Jon Perlow)
//
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/nested_extension.proto b/java/src/test/java/com/google/protobuf/nested_extension.proto
index 86492867..704e649e 100644
--- a/java/src/test/java/com/google/protobuf/nested_extension.proto
+++ b/java/src/test/java/com/google/protobuf/nested_extension.proto
@@ -33,6 +33,7 @@
// A proto file with nested extensions. Note that this must be defined in
// a separate file to properly test the initialization of the outer class.
+syntax = "proto2";
import "com/google/protobuf/non_nested_extension.proto";
diff --git a/java/src/test/java/com/google/protobuf/nested_extension_lite.proto b/java/src/test/java/com/google/protobuf/nested_extension_lite.proto
index 9c686829..a95c38b2 100644
--- a/java/src/test/java/com/google/protobuf/nested_extension_lite.proto
+++ b/java/src/test/java/com/google/protobuf/nested_extension_lite.proto
@@ -34,6 +34,7 @@
// this must be defined in a separate file to properly test the initialization
// of the outer class.
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/non_nested_extension.proto b/java/src/test/java/com/google/protobuf/non_nested_extension.proto
index cb2f8b00..31fac552 100644
--- a/java/src/test/java/com/google/protobuf/non_nested_extension.proto
+++ b/java/src/test/java/com/google/protobuf/non_nested_extension.proto
@@ -32,6 +32,7 @@
//
// A proto file with extensions.
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto b/java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto
index c1f744b8..37c369ed 100644
--- a/java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto
+++ b/java/src/test/java/com/google/protobuf/non_nested_extension_lite.proto
@@ -32,6 +32,7 @@
//
// A proto file with extensions for a MessageLite messages.
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/outer_class_name_test.proto b/java/src/test/java/com/google/protobuf/outer_class_name_test.proto
index c5114f89..42083681 100644
--- a/java/src/test/java/com/google/protobuf/outer_class_name_test.proto
+++ b/java/src/test/java/com/google/protobuf/outer_class_name_test.proto
@@ -28,6 +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.
+syntax = "proto2";
+
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/outer_class_name_test2.proto b/java/src/test/java/com/google/protobuf/outer_class_name_test2.proto
index 741cd281..3e5956b0 100644
--- a/java/src/test/java/com/google/protobuf/outer_class_name_test2.proto
+++ b/java/src/test/java/com/google/protobuf/outer_class_name_test2.proto
@@ -28,6 +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.
+syntax = "proto2";
+
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/outer_class_name_test3.proto b/java/src/test/java/com/google/protobuf/outer_class_name_test3.proto
index 5d5d4ac9..74a8ba3c 100644
--- a/java/src/test/java/com/google/protobuf/outer_class_name_test3.proto
+++ b/java/src/test/java/com/google/protobuf/outer_class_name_test3.proto
@@ -28,6 +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.
+syntax = "proto2";
+
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto b/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
index 202e8c91..67035fd5 100644
--- a/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
+++ b/java/src/test/java/com/google/protobuf/test_bad_identifiers.proto
@@ -33,6 +33,7 @@
// This file tests that various identifiers work as field and type names even
// though the same identifiers are used internally by the java code generator.
+syntax = "proto2";
// Some generic_services option(s) added automatically.
// See: http://go/proto2-generic-services-default
@@ -59,12 +60,14 @@ message Descriptor {
}
optional NestedDescriptor nested_descriptor = 2;
enum NestedEnum {
+ UNKNOWN = 0;
FOO = 1;
}
}
message Parser {
enum ParserEnum {
+ UNKNOWN = 0;
PARSER = 1;
}
optional ParserEnum parser = 1;
@@ -72,6 +75,7 @@ message Parser {
message Deprecated {
enum TestEnum {
+ UNKNOWN = 0;
FOO = 1;
// Test if @Deprecated annotation conflicts with Deprecated message name.
@@ -118,6 +122,7 @@ service TestConflictingMethodNames {
message TestConflictingFieldNames {
enum TestEnum {
+ UNKNOWN = 0;
FOO = 1;
}
message TestMessage {
@@ -142,16 +147,23 @@ message TestConflictingFieldNames {
// This field conflicts with "int32_field" as they both generate
// the method getInt32FieldList().
- required int32 int32_field_list = 31;
+ required int32 int32_field_list = 31; // NO_PROTO3
- extensions 1000 to max;
+ extensions 1000 to max; // NO_PROTO3
repeated int64 int64_field = 41;
- extend TestConflictingFieldNames {
+ extend TestConflictingFieldNames { // NO_PROTO3
// 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;
- optional int64 int64_field_list = 1002;
- }
+ optional int64 int64_field_count = 1001; // NO_PROTO3
+ optional int64 int64_field_list = 1002; // NO_PROTO3
+ } // NO_PROTO3
}
+message TestMapField {
+ message MapField {}
+ message Pair {}
+ message Message {}
+
+ map<int32, int32> map_field = 1;
+}
diff --git a/java/src/test/java/com/google/protobuf/test_check_utf8.proto b/java/src/test/java/com/google/protobuf/test_check_utf8.proto
index 206946d8..119c1dcb 100644
--- a/java/src/test/java/com/google/protobuf/test_check_utf8.proto
+++ b/java/src/test/java/com/google/protobuf/test_check_utf8.proto
@@ -31,6 +31,7 @@
// Author: Jacob Butcher (jbaum@google.com)
//
// Test file option java_string_check_utf8.
+syntax = "proto2";
package proto2_test_check_utf8;
diff --git a/java/src/test/java/com/google/protobuf/test_check_utf8_size.proto b/java/src/test/java/com/google/protobuf/test_check_utf8_size.proto
index fa057a88..f06d76d6 100644
--- a/java/src/test/java/com/google/protobuf/test_check_utf8_size.proto
+++ b/java/src/test/java/com/google/protobuf/test_check_utf8_size.proto
@@ -31,6 +31,7 @@
// Author: Jacob Butcher (jbaum@google.com)
//
// Test file option java_string_check_utf8.
+syntax = "proto2";
package proto2_test_check_utf8_size;
diff --git a/java/src/test/java/com/google/protobuf/test_custom_options.proto b/java/src/test/java/com/google/protobuf/test_custom_options.proto
index f6a5ecd4..f8efd455 100644
--- a/java/src/test/java/com/google/protobuf/test_custom_options.proto
+++ b/java/src/test/java/com/google/protobuf/test_custom_options.proto
@@ -32,6 +32,7 @@
//
// Test that custom options defined in a proto file's dependencies are properly
// initialized.
+syntax = "proto2";
package protobuf_unittest;
diff --git a/java/src/test/java/com/google/protobuf/test_extra_interfaces.proto b/java/src/test/java/com/google/protobuf/test_extra_interfaces.proto
index 72e05a36..645f57b4 100644
--- a/java/src/test/java/com/google/protobuf/test_extra_interfaces.proto
+++ b/java/src/test/java/com/google/protobuf/test_extra_interfaces.proto
@@ -29,6 +29,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Darick Tong (darick@google.com)
+syntax = "proto2";
package protobuf_unittest;
diff --git a/javanano/README.txt b/javanano/README.txt
new file mode 100644
index 00000000..5a05b865
--- /dev/null
+++ b/javanano/README.txt
@@ -0,0 +1,354 @@
+Protocol Buffers - Google's data interchange format
+Copyright 2008 Google Inc.
+
+This directory contains the Java Protocol Buffers Nano runtime library.
+
+Installation - With Maven
+=========================
+
+The Protocol Buffers build is managed using Maven. If you would
+rather build without Maven, see below.
+
+1) Install Apache Maven if you don't have it:
+
+ http://maven.apache.org/
+
+2) Build the C++ code, or obtain a binary distribution of protoc. If
+ you install a binary distribution, make sure that it is the same
+ version as this package. If in doubt, run:
+
+ $ protoc --version
+
+ You will need to place the protoc executable in ../src. (If you
+ built it yourself, it should already be there.)
+
+3) Run the tests:
+
+ $ mvn test
+
+ If some tests fail, this library may not work correctly on your
+ system. Continue at your own risk.
+
+4) Install the library into your Maven repository:
+
+ $ mvn install
+
+5) If you do not use Maven to manage your own build, you can build a
+ .jar file to use:
+
+ $ mvn package
+
+ The .jar will be placed in the "target" directory.
+
+Installation - Without Maven
+============================
+
+If you would rather not install Maven to build the library, you may
+follow these instructions instead. Note that these instructions skip
+running unit tests.
+
+1) Build the C++ code, or obtain a binary distribution of protoc. If
+ you install a binary distribution, make sure that it is the same
+ version as this package. If in doubt, run:
+
+ $ protoc --version
+
+ If you built the C++ code without installing, the compiler binary
+ should be located in ../src.
+
+2) Invoke protoc to build DescriptorProtos.java:
+
+ $ protoc --java_out=src/main/java -I../src \
+ ../src/google/protobuf/descriptor.proto
+
+3) Compile the code in src/main/java using whatever means you prefer.
+
+4) Install the classes wherever you prefer.
+
+Nano version
+============================
+
+Nano is a special code generator and runtime library designed specially
+for Android, and is very resource-friendly in both the amount of code
+and the runtime overhead. An overview of Nano features:
+
+- No descriptors or message builders.
+- All messages are mutable; fields are public Java fields.
+- For optional fields only, encapsulation behind setter/getter/hazzer/
+ clearer functions is opt-in, which provide proper 'has' state support.
+- If not opted in, has state is not available. Serialization outputs
+ all fields not equal to their defaults (see important implications
+ below).
+- Required fields are always serialized.
+- Enum constants are integers; protection against invalid values only
+ when parsing from the wire.
+- Enum constants can be generated into container interfaces bearing
+ the enum's name (so the referencing code is in Java style).
+- CodedInputByteBufferNano can only take byte[] (not InputStream).
+- Similarly CodedOutputByteBufferNano can only write to byte[].
+- Repeated fields are in arrays, not ArrayList or Vector. Null array
+ elements are allowed and silently ignored.
+- Full support of serializing/deserializing repeated packed fields.
+- Support of extensions.
+- Unset messages/groups are null, not an immutable empty default
+ instance.
+- toByteArray(...) and mergeFrom(...) are now static functions of
+ MessageNano.
+- The 'bytes' type translates to the Java type byte[].
+
+The generated messages are not thread-safe for writes, but may be
+used simultaneously from multiple threads in a read-only manner.
+In other words, an appropriate synchronization mechanism (such as
+a ReadWriteLock) must be used to ensure that a message, its
+ancestors, and descendants are not accessed by any other threads
+while the message is being modified. Field reads, getter methods
+(but not getExtension(...)), toByteArray(...), writeTo(...),
+getCachedSize(), and getSerializedSize() are all considered read-only
+operations.
+
+IMPORTANT: If you have fields with defaults and opt out of accessors
+
+How fields with defaults are serialized has changed. Because we don't
+keep "has" state, any field equal to its default is assumed to be not
+set and therefore is not serialized. Consider the situation where we
+change the default value of a field. Senders compiled against an older
+version of the proto continue to match against the old default, and
+don't send values to the receiver even though the receiver assumes the
+new default value. Therefore, think carefully about the implications
+of changing the default value. Alternatively, turn on accessors and
+enjoy the benefit of the explicit has() checks.
+
+IMPORTANT: If you have "bytes" fields with non-empty defaults
+
+Because the byte buffer is now of mutable type byte[], the default
+static final cannot be exposed through a public field. Each time a
+message's constructor or clear() function is called, the default value
+(kept in a private byte[]) is cloned. This causes a small memory
+penalty. This is not a problem if the field has no default or is an
+empty default.
+
+Nano Generator options
+
+java_package -> <file-name>|<package-name>
+java_outer_classname -> <file-name>|<package-name>
+java_multiple_files -> true or false
+java_nano_generate_has -> true or false [DEPRECATED]
+optional_field_style -> default or accessors
+enum_style -> c or java
+ignore_services -> true or false
+parcelable_messages -> true or false
+
+java_package=<file-name>|<package-name> (no default)
+ This allows overriding the 'java_package' option value
+ for the given file from the command line. Use multiple
+ java_package options to override the option for multiple
+ files. The final Java package for each file is the value
+ of this command line option if present, or the value of
+ the same option defined in the file if present, or the
+ proto package if present, or the default Java package.
+
+java_outer_classname=<file-name>|<outer-classname> (no default)
+ This allows overriding the 'java_outer_classname' option
+ for the given file from the command line. Use multiple
+ java_outer_classname options to override the option for
+ multiple files. The final Java outer class name for each
+ file is the value of this command line option if present,
+ or the value of the same option defined in the file if
+ present, or the file name converted to CamelCase. This
+ outer class will nest all classes and integer constants
+ generated from file-scope messages and enums.
+
+java_multiple_files={true,false} (no default)
+ This allows overriding the 'java_multiple_files' option
+ in all source files and their imported files from the
+ command line. The final value of this option for each
+ file is the value defined in this command line option, or
+ the value of the same option defined in the file if
+ present, or false. This specifies whether to generate
+ package-level classes for the file-scope messages in the
+ same Java package as the outer class (instead of nested
+ classes in the outer class). File-scope enum constants
+ are still generated as integer constants in the outer
+ class. This affects the fully qualified references in the
+ Java code. NOTE: because the command line option
+ overrides the value for all files and their imported
+ files, using this option inconsistently may result in
+ incorrect references to the imported messages and enum
+ constants.
+
+java_nano_generate_has={true,false} (default: false)
+ DEPRECATED. Use optional_field_style=accessors.
+
+ If true, generates a public boolean variable has<fieldname>
+ accompanying each optional or required field (not present for
+ repeated fields, groups or messages). It is set to false initially
+ and upon clear(). If parseFrom(...) reads the field from the wire,
+ it is set to true. This is a way for clients to inspect the "has"
+ value upon parse. If it is set to true, writeTo(...) will ALWAYS
+ output that field (even if field value is equal to its
+ default).
+
+ IMPORTANT: This option costs an extra 4 bytes per primitive field in
+ the message. Think carefully about whether you really need this. In
+ many cases reading the default works and determining whether the
+ field was received over the wire is irrelevant.
+
+optional_field_style={default,accessors,reftypes} (default: default)
+ Defines the style of the generated code for fields.
+
+ * default *
+
+ In the default style, optional fields translate into public mutable
+ Java fields, and the serialization process is as discussed in the
+ "IMPORTANT" section above.
+
+ * accessors *
+
+ When set to 'accessors', each optional field is encapsulated behind
+ 4 accessors, namely get<fieldname>(), set<fieldname>(), has<fieldname>()
+ and clear<fieldname>() methods, with the standard semantics. The hazzer's
+ return value determines whether a field is serialized, so this style is
+ useful when you need to serialize a field with the default value, or check
+ if a field has been explicitly set to its default value from the wire.
+
+ In the 'accessors' style, required and nested message fields are still
+ translated to one public mutable Java field each, repeated fields are still
+ translated to arrays. No accessors are generated for them.
+
+ IMPORTANT: When using the 'accessors' style, ProGuard should always
+ be enabled with optimization (don't use -dontoptimize) and allowing
+ access modification (use -allowaccessmodification). This removes the
+ unused accessors and maybe inline the rest at the call sites,
+ reducing the final code size.
+ TODO(maxtroy): find ProGuard config that would work the best.
+
+ * reftypes *
+
+ When set to 'reftypes', each proto field is generated as a public Java
+ field. For primitive types, these fields use the Java reference types
+ such as java.lang.Integer instead of primitive types such as int.
+
+ In the 'reftypes' style, fields are initialized to null (or empty
+ arrays for repeated fields), and their default values are not available.
+ They are serialized over the wire based on equality to null.
+
+ The 'reftypes' mode has some additional cost due to autoboxing and usage
+ of reference types. In practice, many boxed types are cached, and so don't
+ result in object creation. However, references do take slightly more memory
+ than primitives.
+
+ The 'reftypes' mode is useful when you want to be able to serialize fields
+ with default values, or check if a field has been explicitly set to the
+ default over the wire without paying the extra method cost of the
+ 'accessors' mode.
+
+ Note that if you attempt to write null to a required field in the reftypes
+ mode, serialization of the proto will cause a NullPointerException. This is
+ an intentional indicator that you must set required fields.
+
+ NOTE
+ optional_field_style=accessors or reftypes cannot be used together with
+ java_nano_generate_has=true. If you need the 'has' flag for any
+ required field (you have no reason to), you can only use
+ java_nano_generate_has=true.
+
+enum_style={c,java} (default: c)
+ Defines where to put the int constants generated from enum members.
+
+ * c *
+
+ Use C-style, so the enum constants are available at the scope where
+ the enum is defined. A file-scope enum's members are referenced like
+ 'FileOuterClass.ENUM_VALUE'; a message-scope enum's members are
+ referenced as 'Message.ENUM_VALUE'. The enum name is unavailable.
+ This complies with the Micro code generator's behavior.
+
+ * java *
+
+ Use Java-style, so the enum constants are available under the enum
+ name and referenced like 'EnumName.ENUM_VALUE' (they are still int
+ constants). The enum name becomes the name of a public interface, at
+ the scope where the enum is defined. If the enum is file-scope and
+ the java_multiple_files option is on, the interface will be defined
+ in its own file. To reduce code size, this interface should not be
+ implemented and ProGuard shrinking should be used, so after the Java
+ compiler inlines all referenced enum constants into the call sites,
+ the interface remains unused and can be removed by ProGuard.
+
+ignore_services={true,false} (default: false)
+ Skips services definitions.
+
+ Nano doesn't support services. By default, if a service is defined
+ it will generate a compilation error. If this flag is set to true,
+ services will be silently ignored, instead.
+
+parcelable_messages={true,false} (default: false)
+ Android-specific option to generate Parcelable messages.
+
+
+To use nano protobufs within the Android repo:
+
+- Set 'LOCAL_PROTOC_OPTIMIZE_TYPE := nano' in your local .mk file.
+ When building a Java library or an app (package) target, the build
+ system will add the Java nano runtime library to the
+ LOCAL_STATIC_JAVA_LIBRARIES variable, so you don't need to.
+- Set 'LOCAL_PROTO_JAVA_OUTPUT_PARAMS := ...' in your local .mk file
+ for any command-line options you need. Use commas to join multiple
+ options. In the nano flavor only, whitespace surrounding the option
+ names and values are ignored, so you can use backslash-newline or
+ '+=' to structure your make files nicely.
+- The options will be applied to *all* proto files in LOCAL_SRC_FILES
+ when you build a Java library or package. In case different options
+ are needed for different proto files, build separate Java libraries
+ and reference them in your main target. Note: you should make sure
+ that, for each separate target, all proto files imported from any
+ proto file in LOCAL_SRC_FILES are included in LOCAL_SRC_FILES. This
+ is because the generator has to assume that the imported files are
+ built using the same options, and will generate code that reference
+ the fields and enums from the imported files using the same code
+ style.
+- Hint: 'include $(CLEAR_VARS)' resets all LOCAL_ variables, including
+ the two above.
+
+To use nano protobufs outside of Android repo:
+
+- Link with the generated jar file
+ <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
+- Invoke with --javanano_out, e.g.:
+
+./protoc '--javanano_out=\
+ java_package=src/proto/simple-data.proto|my_package,\
+ java_outer_classname=src/proto/simple-data.proto|OuterName\
+ :.' src/proto/simple-data.proto
+
+Contributing to nano:
+
+Please add/edit tests in NanoTest.java.
+
+Please run the following steps to test:
+
+- cd external/protobuf
+- ./configure
+- Run "make -j12 check" and verify all tests pass.
+- cd java
+- Run "mvn test" and verify all tests pass.
+- cd ../../..
+- . build/envsetup.sh
+- lunch 1
+- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params NanoAndroidTest" and
+ check for build errors.
+- Plug in an Android device or start an emulator.
+- adb install -r out/target/product/generic/data/app/NanoAndroidTest.apk
+- Run:
+ "adb shell am instrument -w com.google.protobuf.nano.test/android.test.InstrumentationTestRunner"
+ and verify all tests pass.
+- repo sync -c -j256
+- "make -j12" and check for build errors
+
+Usage
+=====
+
+The complete documentation for Protocol Buffers is available via the
+web at:
+
+ https://developers.google.com/protocol-buffers/
diff --git a/javanano/pom.xml b/javanano/pom.xml
new file mode 100644
index 00000000..3d98a5e0
--- /dev/null
+++ b/javanano/pom.xml
@@ -0,0 +1,164 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>com.google</groupId>
+ <artifactId>google</artifactId>
+ <version>1</version>
+ </parent>
+ <groupId>com.google.protobuf.nano</groupId>
+ <artifactId>protobuf-javanano</artifactId>
+ <version>2.6.2-pre</version>
+ <packaging>bundle</packaging>
+ <name>Protocol Buffer JavaNano API</name>
+ <description>
+ Protocol Buffers are a way of encoding structured data in an efficient yet
+ extensible format.
+ </description>
+ <inceptionYear>2008</inceptionYear>
+ <url>https://developers.google.com/protocol-buffers/</url>
+ <licenses>
+ <license>
+ <name>New BSD license</name>
+ <url>http://www.opensource.org/licenses/bsd-license.php</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+ <scm>
+ <url>https://github.com/google/protobuf</url>
+ <connection>
+ scm:git:https://github.com/google/protobuf.git
+ </connection>
+ </scm>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>2.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymockclassextension</artifactId>
+ <version>2.2.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*Test.java</include>
+ </includes>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-antrun-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>generate-test-sources</id>
+ <phase>generate-test-sources</phase>
+ <configuration>
+ <tasks>
+ <mkdir dir="target/generated-test-sources" />
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=generate_equals=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_import_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_single_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=store_unknown_fields=true,generate_equals=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=store_unknown_fields=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=java_nano_generate_has=true,generate_equals=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_has_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=optional_field_style=accessors,generate_equals=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=enum_style=java:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=
+ optional_field_style=accessors,
+ java_outer_classname=google/protobuf/nano/unittest_enum_validity_nano.proto|EnumValidityAccessors
+ :target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto" />
+ </exec>
+ <exec executable="../src/protoc">
+ <arg value="--javanano_out=optional_field_style=reftypes,generate_equals=true:target/generated-test-sources" />
+ <arg value="--proto_path=src/test/java/com" />
+ <arg value="src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto" />
+ </exec>
+ </tasks>
+ <testSourceRoot>target/generated-test-sources</testSourceRoot>
+ </configuration>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Bundle-DocURL>https://developers.google.com/protocol-buffers/</Bundle-DocURL>
+ <Bundle-SymbolicName>com.google.protobuf</Bundle-SymbolicName>
+ <Export-Package>com.google.protobuf;version=2.6.2-pre</Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
new file mode 100644
index 00000000..b147f69d
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java
@@ -0,0 +1,641 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+
+/**
+ * Reads and decodes protocol message fields.
+ *
+ * This class contains two kinds of methods: methods that read specific
+ * protocol message constructs and field types (e.g. {@link #readTag()} and
+ * {@link #readInt32()}) and methods that read low-level values (e.g.
+ * {@link #readRawVarint32()} and {@link #readRawBytes}). If you are reading
+ * encoded protocol messages, you should use the former methods, but if you are
+ * reading some other format of your own design, use the latter.
+ *
+ * @author kenton@google.com Kenton Varda
+ */
+public final class CodedInputByteBufferNano {
+ /**
+ * Create a new CodedInputStream wrapping the given byte array.
+ */
+ public static CodedInputByteBufferNano newInstance(final byte[] buf) {
+ return newInstance(buf, 0, buf.length);
+ }
+
+ /**
+ * Create a new CodedInputStream wrapping the given byte array slice.
+ */
+ public static CodedInputByteBufferNano newInstance(final byte[] buf, final int off,
+ final int len) {
+ return new CodedInputByteBufferNano(buf, off, len);
+ }
+
+ // -----------------------------------------------------------------
+
+ /**
+ * Attempt to read a field tag, returning zero if we have reached EOF.
+ * Protocol message parsers use this to read tags, since a protocol message
+ * may legally end wherever a tag occurs, and zero is not a valid tag number.
+ */
+ public int readTag() throws IOException {
+ if (isAtEnd()) {
+ lastTag = 0;
+ return 0;
+ }
+
+ lastTag = readRawVarint32();
+ if (lastTag == 0) {
+ // If we actually read zero, that's not a valid tag.
+ throw InvalidProtocolBufferNanoException.invalidTag();
+ }
+ return lastTag;
+ }
+
+ /**
+ * Verifies that the last call to readTag() returned the given tag value.
+ * This is used to verify that a nested group ended with the correct
+ * end tag.
+ *
+ * @throws InvalidProtocolBufferNanoException {@code value} does not match the
+ * last tag.
+ */
+ public void checkLastTagWas(final int value)
+ throws InvalidProtocolBufferNanoException {
+ if (lastTag != value) {
+ throw InvalidProtocolBufferNanoException.invalidEndTag();
+ }
+ }
+
+ /**
+ * Reads and discards a single field, given its tag value.
+ *
+ * @return {@code false} if the tag is an endgroup tag, in which case
+ * nothing is skipped. Otherwise, returns {@code true}.
+ */
+ public boolean skipField(final int tag) throws IOException {
+ switch (WireFormatNano.getTagWireType(tag)) {
+ case WireFormatNano.WIRETYPE_VARINT:
+ readInt32();
+ return true;
+ case WireFormatNano.WIRETYPE_FIXED64:
+ readRawLittleEndian64();
+ return true;
+ case WireFormatNano.WIRETYPE_LENGTH_DELIMITED:
+ skipRawBytes(readRawVarint32());
+ return true;
+ case WireFormatNano.WIRETYPE_START_GROUP:
+ skipMessage();
+ checkLastTagWas(
+ WireFormatNano.makeTag(WireFormatNano.getTagFieldNumber(tag),
+ WireFormatNano.WIRETYPE_END_GROUP));
+ return true;
+ case WireFormatNano.WIRETYPE_END_GROUP:
+ return false;
+ case WireFormatNano.WIRETYPE_FIXED32:
+ readRawLittleEndian32();
+ return true;
+ default:
+ throw InvalidProtocolBufferNanoException.invalidWireType();
+ }
+ }
+
+ /**
+ * Reads and discards an entire message. This will read either until EOF
+ * or until an endgroup tag, whichever comes first.
+ */
+ public void skipMessage() throws IOException {
+ while (true) {
+ final int tag = readTag();
+ if (tag == 0 || !skipField(tag)) {
+ return;
+ }
+ }
+ }
+
+ // -----------------------------------------------------------------
+
+ /** Read a {@code double} field value from the stream. */
+ public double readDouble() throws IOException {
+ return Double.longBitsToDouble(readRawLittleEndian64());
+ }
+
+ /** Read a {@code float} field value from the stream. */
+ public float readFloat() throws IOException {
+ return Float.intBitsToFloat(readRawLittleEndian32());
+ }
+
+ /** Read a {@code uint64} field value from the stream. */
+ public long readUInt64() throws IOException {
+ return readRawVarint64();
+ }
+
+ /** Read an {@code int64} field value from the stream. */
+ public long readInt64() throws IOException {
+ return readRawVarint64();
+ }
+
+ /** Read an {@code int32} field value from the stream. */
+ public int readInt32() throws IOException {
+ return readRawVarint32();
+ }
+
+ /** Read a {@code fixed64} field value from the stream. */
+ public long readFixed64() throws IOException {
+ return readRawLittleEndian64();
+ }
+
+ /** Read a {@code fixed32} field value from the stream. */
+ public int readFixed32() throws IOException {
+ return readRawLittleEndian32();
+ }
+
+ /** Read a {@code bool} field value from the stream. */
+ public boolean readBool() throws IOException {
+ return readRawVarint32() != 0;
+ }
+
+ /** Read a {@code string} field value from the stream. */
+ public String readString() throws IOException {
+ final int size = readRawVarint32();
+ if (size <= (bufferSize - bufferPos) && size > 0) {
+ // Fast path: We already have the bytes in a contiguous buffer, so
+ // just copy directly from it.
+ final String result = new String(buffer, bufferPos, size, "UTF-8");
+ bufferPos += size;
+ return result;
+ } else {
+ // Slow path: Build a byte array first then copy it.
+ return new String(readRawBytes(size), "UTF-8");
+ }
+ }
+
+ /** Read a {@code group} field value from the stream. */
+ public void readGroup(final MessageNano msg, final int fieldNumber)
+ throws IOException {
+ if (recursionDepth >= recursionLimit) {
+ throw InvalidProtocolBufferNanoException.recursionLimitExceeded();
+ }
+ ++recursionDepth;
+ msg.mergeFrom(this);
+ checkLastTagWas(
+ WireFormatNano.makeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP));
+ --recursionDepth;
+ }
+
+ public void readMessage(final MessageNano msg)
+ throws IOException {
+ final int length = readRawVarint32();
+ if (recursionDepth >= recursionLimit) {
+ throw InvalidProtocolBufferNanoException.recursionLimitExceeded();
+ }
+ final int oldLimit = pushLimit(length);
+ ++recursionDepth;
+ msg.mergeFrom(this);
+ checkLastTagWas(0);
+ --recursionDepth;
+ popLimit(oldLimit);
+ }
+
+ /** Read a {@code bytes} field value from the stream. */
+ public byte[] readBytes() throws IOException {
+ final int size = readRawVarint32();
+ if (size <= (bufferSize - bufferPos) && size > 0) {
+ // Fast path: We already have the bytes in a contiguous buffer, so
+ // just copy directly from it.
+ final byte[] result = new byte[size];
+ System.arraycopy(buffer, bufferPos, result, 0, size);
+ bufferPos += size;
+ return result;
+ } else {
+ // Slow path: Build a byte array first then copy it.
+ return readRawBytes(size);
+ }
+ }
+
+ /** Read a {@code uint32} field value from the stream. */
+ public int readUInt32() throws IOException {
+ return readRawVarint32();
+ }
+
+ /**
+ * Read an enum field value from the stream. Caller is responsible
+ * for converting the numeric value to an actual enum.
+ */
+ public int readEnum() throws IOException {
+ return readRawVarint32();
+ }
+
+ /** Read an {@code sfixed32} field value from the stream. */
+ public int readSFixed32() throws IOException {
+ return readRawLittleEndian32();
+ }
+
+ /** Read an {@code sfixed64} field value from the stream. */
+ public long readSFixed64() throws IOException {
+ return readRawLittleEndian64();
+ }
+
+ /** Read an {@code sint32} field value from the stream. */
+ public int readSInt32() throws IOException {
+ return decodeZigZag32(readRawVarint32());
+ }
+
+ /** Read an {@code sint64} field value from the stream. */
+ public long readSInt64() throws IOException {
+ return decodeZigZag64(readRawVarint64());
+ }
+
+ // =================================================================
+
+ /**
+ * Read a raw Varint from the stream. If larger than 32 bits, discard the
+ * upper bits.
+ */
+ public int readRawVarint32() throws IOException {
+ byte tmp = readRawByte();
+ if (tmp >= 0) {
+ return tmp;
+ }
+ int result = tmp & 0x7f;
+ if ((tmp = readRawByte()) >= 0) {
+ result |= tmp << 7;
+ } else {
+ result |= (tmp & 0x7f) << 7;
+ if ((tmp = readRawByte()) >= 0) {
+ result |= tmp << 14;
+ } else {
+ result |= (tmp & 0x7f) << 14;
+ if ((tmp = readRawByte()) >= 0) {
+ result |= tmp << 21;
+ } else {
+ result |= (tmp & 0x7f) << 21;
+ result |= (tmp = readRawByte()) << 28;
+ if (tmp < 0) {
+ // Discard upper 32 bits.
+ for (int i = 0; i < 5; i++) {
+ if (readRawByte() >= 0) {
+ return result;
+ }
+ }
+ throw InvalidProtocolBufferNanoException.malformedVarint();
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ /** Read a raw Varint from the stream. */
+ public long readRawVarint64() throws IOException {
+ int shift = 0;
+ long result = 0;
+ while (shift < 64) {
+ final byte b = readRawByte();
+ result |= (long)(b & 0x7F) << shift;
+ if ((b & 0x80) == 0) {
+ return result;
+ }
+ shift += 7;
+ }
+ throw InvalidProtocolBufferNanoException.malformedVarint();
+ }
+
+ /** Read a 32-bit little-endian integer from the stream. */
+ public int readRawLittleEndian32() throws IOException {
+ final byte b1 = readRawByte();
+ final byte b2 = readRawByte();
+ final byte b3 = readRawByte();
+ final byte b4 = readRawByte();
+ return ((b1 & 0xff) ) |
+ ((b2 & 0xff) << 8) |
+ ((b3 & 0xff) << 16) |
+ ((b4 & 0xff) << 24);
+ }
+
+ /** Read a 64-bit little-endian integer from the stream. */
+ public long readRawLittleEndian64() throws IOException {
+ final byte b1 = readRawByte();
+ final byte b2 = readRawByte();
+ final byte b3 = readRawByte();
+ final byte b4 = readRawByte();
+ final byte b5 = readRawByte();
+ final byte b6 = readRawByte();
+ final byte b7 = readRawByte();
+ final byte b8 = readRawByte();
+ return (((long)b1 & 0xff) ) |
+ (((long)b2 & 0xff) << 8) |
+ (((long)b3 & 0xff) << 16) |
+ (((long)b4 & 0xff) << 24) |
+ (((long)b5 & 0xff) << 32) |
+ (((long)b6 & 0xff) << 40) |
+ (((long)b7 & 0xff) << 48) |
+ (((long)b8 & 0xff) << 56);
+ }
+
+ /**
+ * Decode 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 An unsigned 32-bit integer, stored in a signed int because
+ * Java has no explicit unsigned support.
+ * @return A signed 32-bit integer.
+ */
+ public static int decodeZigZag32(final int n) {
+ return (n >>> 1) ^ -(n & 1);
+ }
+
+ /**
+ * Decode 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 An unsigned 64-bit integer, stored in a signed int because
+ * Java has no explicit unsigned support.
+ * @return A signed 64-bit integer.
+ */
+ public static long decodeZigZag64(final long n) {
+ return (n >>> 1) ^ -(n & 1);
+ }
+
+ // -----------------------------------------------------------------
+
+ private final byte[] buffer;
+ private int bufferStart;
+ private int bufferSize;
+ private int bufferSizeAfterLimit;
+ private int bufferPos;
+ private int lastTag;
+
+ /** The absolute position of the end of the current message. */
+ private int currentLimit = Integer.MAX_VALUE;
+
+ /** See setRecursionLimit() */
+ private int recursionDepth;
+ private int recursionLimit = DEFAULT_RECURSION_LIMIT;
+
+ /** See setSizeLimit() */
+ private int sizeLimit = DEFAULT_SIZE_LIMIT;
+
+ private static final int DEFAULT_RECURSION_LIMIT = 64;
+ private static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB
+
+ private CodedInputByteBufferNano(final byte[] buffer, final int off, final int len) {
+ this.buffer = buffer;
+ bufferStart = off;
+ bufferSize = off + len;
+ bufferPos = off;
+ }
+
+ /**
+ * 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.
+ *
+ * @return the old limit.
+ */
+ public int setRecursionLimit(final int limit) {
+ if (limit < 0) {
+ throw new IllegalArgumentException(
+ "Recursion limit cannot be negative: " + limit);
+ }
+ final int oldLimit = recursionLimit;
+ recursionLimit = limit;
+ return oldLimit;
+ }
+
+ /**
+ * Set the maximum message size. In order to prevent malicious
+ * messages from exhausting memory or causing integer overflows,
+ * {@code CodedInputStream} limits how large a message may be.
+ * The default limit is 64MB. You should set this limit as small
+ * as you can without harming your app's functionality. Note that
+ * size limits only apply when reading from an {@code InputStream}, not
+ * when constructed around a raw byte array.
+ * <p>
+ * If you want to read several messages from a single CodedInputStream, you
+ * could call {@link #resetSizeCounter()} after each one to avoid hitting the
+ * size limit.
+ *
+ * @return the old limit.
+ */
+ public int setSizeLimit(final int limit) {
+ if (limit < 0) {
+ throw new IllegalArgumentException(
+ "Size limit cannot be negative: " + limit);
+ }
+ final int oldLimit = sizeLimit;
+ sizeLimit = limit;
+ return oldLimit;
+ }
+
+ /**
+ * Resets the current size counter to zero (see {@link #setSizeLimit(int)}).
+ */
+ public void resetSizeCounter() {
+ }
+
+ /**
+ * Sets {@code currentLimit} to (current position) + {@code byteLimit}. This
+ * is called when descending into a length-delimited embedded message.
+ *
+ * @return the old limit.
+ */
+ public int pushLimit(int byteLimit) throws InvalidProtocolBufferNanoException {
+ if (byteLimit < 0) {
+ throw InvalidProtocolBufferNanoException.negativeSize();
+ }
+ byteLimit += bufferPos;
+ final int oldLimit = currentLimit;
+ if (byteLimit > oldLimit) {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+ currentLimit = byteLimit;
+
+ recomputeBufferSizeAfterLimit();
+
+ return oldLimit;
+ }
+
+ private void recomputeBufferSizeAfterLimit() {
+ bufferSize += bufferSizeAfterLimit;
+ final int bufferEnd = bufferSize;
+ if (bufferEnd > currentLimit) {
+ // Limit is in current buffer.
+ bufferSizeAfterLimit = bufferEnd - currentLimit;
+ bufferSize -= bufferSizeAfterLimit;
+ } else {
+ bufferSizeAfterLimit = 0;
+ }
+ }
+
+ /**
+ * Discards the current limit, returning to the previous limit.
+ *
+ * @param oldLimit The old limit, as returned by {@code pushLimit}.
+ */
+ public void popLimit(final int oldLimit) {
+ currentLimit = oldLimit;
+ recomputeBufferSizeAfterLimit();
+ }
+
+ /**
+ * Returns the number of bytes to be read before the current limit.
+ * If no limit is set, returns -1.
+ */
+ public int getBytesUntilLimit() {
+ if (currentLimit == Integer.MAX_VALUE) {
+ return -1;
+ }
+
+ final int currentAbsolutePosition = bufferPos;
+ return currentLimit - currentAbsolutePosition;
+ }
+
+ /**
+ * 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)}.
+ */
+ public boolean isAtEnd() {
+ return bufferPos == bufferSize;
+ }
+
+ /**
+ * Get current position in buffer relative to beginning offset.
+ */
+ public int getPosition() {
+ return bufferPos - bufferStart;
+ }
+
+ /**
+ * Retrieves a subset of data in the buffer. The returned array is not backed by the original
+ * buffer array.
+ *
+ * @param offset the position (relative to the buffer start position) to start at.
+ * @param length the number of bytes to retrieve.
+ */
+ public byte[] getData(int offset, int length) {
+ if (length == 0) {
+ return WireFormatNano.EMPTY_BYTES;
+ }
+ byte[] copy = new byte[length];
+ int start = bufferStart + offset;
+ System.arraycopy(buffer, start, copy, 0, length);
+ return copy;
+ }
+
+ /**
+ * Rewind to previous position. Cannot go forward.
+ */
+ public void rewindToPosition(int position) {
+ if (position > bufferPos - bufferStart) {
+ throw new IllegalArgumentException(
+ "Position " + position + " is beyond current " + (bufferPos - bufferStart));
+ }
+ if (position < 0) {
+ throw new IllegalArgumentException("Bad position " + position);
+ }
+ bufferPos = bufferStart + position;
+ }
+
+ /**
+ * Read one byte from the input.
+ *
+ * @throws InvalidProtocolBufferNanoException The end of the stream or the current
+ * limit was reached.
+ */
+ public byte readRawByte() throws IOException {
+ if (bufferPos == bufferSize) {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+ return buffer[bufferPos++];
+ }
+
+ /**
+ * Read a fixed size of bytes from the input.
+ *
+ * @throws InvalidProtocolBufferNanoException The end of the stream or the current
+ * limit was reached.
+ */
+ public byte[] readRawBytes(final int size) throws IOException {
+ if (size < 0) {
+ throw InvalidProtocolBufferNanoException.negativeSize();
+ }
+
+ if (bufferPos + size > currentLimit) {
+ // Read to the end of the stream anyway.
+ skipRawBytes(currentLimit - bufferPos);
+ // Then fail.
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+
+ if (size <= bufferSize - bufferPos) {
+ // We have all the bytes we need already.
+ final byte[] bytes = new byte[size];
+ System.arraycopy(buffer, bufferPos, bytes, 0, size);
+ bufferPos += size;
+ return bytes;
+ } else {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+ }
+
+ /**
+ * Reads and discards {@code size} bytes.
+ *
+ * @throws InvalidProtocolBufferNanoException The end of the stream or the current
+ * limit was reached.
+ */
+ public void skipRawBytes(final int size) throws IOException {
+ if (size < 0) {
+ throw InvalidProtocolBufferNanoException.negativeSize();
+ }
+
+ if (bufferPos + size > currentLimit) {
+ // Read to the end of the stream anyway.
+ skipRawBytes(currentLimit - bufferPos);
+ // Then fail.
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+
+ if (size <= bufferSize - bufferPos) {
+ // We have all the bytes we need already.
+ bufferPos += size;
+ } else {
+ throw InvalidProtocolBufferNanoException.truncatedMessage();
+ }
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
new file mode 100644
index 00000000..37982b57
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -0,0 +1,879 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Encodes and writes protocol message fields.
+ *
+ * <p>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.
+ *
+ * <p>This class is totally unsynchronized.
+ *
+ * @author kneton@google.com Kenton Varda
+ */
+public final class CodedOutputByteBufferNano {
+ private final byte[] buffer;
+ private final int limit;
+ private int position;
+
+ private CodedOutputByteBufferNano(final byte[] buffer, final int offset,
+ final int length) {
+ this.buffer = buffer;
+ position = offset;
+ limit = offset + length;
+ }
+
+ /**
+ * 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}.
+ */
+ public static CodedOutputByteBufferNano 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}.
+ */
+ public static CodedOutputByteBufferNano newInstance(final byte[] flatArray,
+ final int offset,
+ final int length) {
+ return new CodedOutputByteBufferNano(flatArray, offset, length);
+ }
+
+ // -----------------------------------------------------------------
+
+ /** Write a {@code double} field, including tag, to the stream. */
+ public void writeDouble(final int fieldNumber, final double value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64);
+ writeDoubleNoTag(value);
+ }
+
+ /** Write a {@code float} field, including tag, to the stream. */
+ public void writeFloat(final int fieldNumber, final float value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32);
+ writeFloatNoTag(value);
+ }
+
+ /** Write a {@code uint64} field, including tag, to the stream. */
+ public void writeUInt64(final int fieldNumber, final long value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeUInt64NoTag(value);
+ }
+
+ /** Write an {@code int64} field, including tag, to the stream. */
+ public void writeInt64(final int fieldNumber, final long value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeInt64NoTag(value);
+ }
+
+ /** Write an {@code int32} field, including tag, to the stream. */
+ public void writeInt32(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeInt32NoTag(value);
+ }
+
+ /** Write a {@code fixed64} field, including tag, to the stream. */
+ public void writeFixed64(final int fieldNumber, final long value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64);
+ writeFixed64NoTag(value);
+ }
+
+ /** Write a {@code fixed32} field, including tag, to the stream. */
+ public void writeFixed32(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32);
+ writeFixed32NoTag(value);
+ }
+
+ /** Write a {@code bool} field, including tag, to the stream. */
+ public void writeBool(final int fieldNumber, final boolean value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeBoolNoTag(value);
+ }
+
+ /** Write a {@code string} field, including tag, to the stream. */
+ public void writeString(final int fieldNumber, final String value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED);
+ writeStringNoTag(value);
+ }
+
+ /** Write a {@code group} field, including tag, to the stream. */
+ public void writeGroup(final int fieldNumber, final MessageNano value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_START_GROUP);
+ writeGroupNoTag(value);
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP);
+ }
+
+ /** Write an embedded message field, including tag, to the stream. */
+ public void writeMessage(final int fieldNumber, final MessageNano value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED);
+ writeMessageNoTag(value);
+ }
+
+ /** Write a {@code bytes} field, including tag, to the stream. */
+ public void writeBytes(final int fieldNumber, final byte[] value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_LENGTH_DELIMITED);
+ writeBytesNoTag(value);
+ }
+
+ /** Write a {@code uint32} field, including tag, to the stream. */
+ public void writeUInt32(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeUInt32NoTag(value);
+ }
+
+ /**
+ * Write an enum field, including tag, to the stream. Caller is responsible
+ * for converting the enum value to its numeric value.
+ */
+ public void writeEnum(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeEnumNoTag(value);
+ }
+
+ /** Write an {@code sfixed32} field, including tag, to the stream. */
+ public void writeSFixed32(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED32);
+ writeSFixed32NoTag(value);
+ }
+
+ /** Write an {@code sfixed64} field, including tag, to the stream. */
+ public void writeSFixed64(final int fieldNumber, final long value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_FIXED64);
+ writeSFixed64NoTag(value);
+ }
+
+ /** Write an {@code sint32} field, including tag, to the stream. */
+ public void writeSInt32(final int fieldNumber, final int value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeSInt32NoTag(value);
+ }
+
+ /** Write an {@code sint64} field, including tag, to the stream. */
+ public void writeSInt64(final int fieldNumber, final long value)
+ throws IOException {
+ writeTag(fieldNumber, WireFormatNano.WIRETYPE_VARINT);
+ writeSInt64NoTag(value);
+ }
+
+ /**
+ * Write a MessageSet extension field to the stream. For historical reasons,
+ * the wire format differs from normal fields.
+ */
+// public void writeMessageSetExtension(final int fieldNumber,
+// final MessageMicro value)
+// throws IOException {
+// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_START_GROUP);
+// writeUInt32(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber);
+// writeMessage(WireFormatMicro.MESSAGE_SET_MESSAGE, value);
+// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_END_GROUP);
+// }
+
+ /**
+ * Write an unparsed MessageSet extension field to the stream. For
+ * historical reasons, the wire format differs from normal fields.
+ */
+// public void writeRawMessageSetExtension(final int fieldNumber,
+// final ByteStringMicro value)
+// throws IOException {
+// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_START_GROUP);
+// writeUInt32(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber);
+// writeBytes(WireFormatMicro.MESSAGE_SET_MESSAGE, value);
+// writeTag(WireFormatMicro.MESSAGE_SET_ITEM, WireFormatMicro.WIRETYPE_END_GROUP);
+// }
+
+ // -----------------------------------------------------------------
+
+ /** Write a {@code double} field to the stream. */
+ public void writeDoubleNoTag(final double value) throws IOException {
+ writeRawLittleEndian64(Double.doubleToLongBits(value));
+ }
+
+ /** Write a {@code float} field to the stream. */
+ public void writeFloatNoTag(final float value) throws IOException {
+ writeRawLittleEndian32(Float.floatToIntBits(value));
+ }
+
+ /** Write a {@code uint64} field to the stream. */
+ public void writeUInt64NoTag(final long value) throws IOException {
+ writeRawVarint64(value);
+ }
+
+ /** Write an {@code int64} field to the stream. */
+ public void writeInt64NoTag(final long value) throws IOException {
+ writeRawVarint64(value);
+ }
+
+ /** Write an {@code int32} field to the stream. */
+ public void writeInt32NoTag(final int value) throws IOException {
+ if (value >= 0) {
+ writeRawVarint32(value);
+ } else {
+ // Must sign-extend.
+ writeRawVarint64(value);
+ }
+ }
+
+ /** Write a {@code fixed64} field to the stream. */
+ public void writeFixed64NoTag(final long value) throws IOException {
+ writeRawLittleEndian64(value);
+ }
+
+ /** Write a {@code fixed32} field to the stream. */
+ public void writeFixed32NoTag(final int value) throws IOException {
+ writeRawLittleEndian32(value);
+ }
+
+ /** Write a {@code bool} field to the stream. */
+ public void writeBoolNoTag(final boolean value) throws IOException {
+ writeRawByte(value ? 1 : 0);
+ }
+
+ /** Write a {@code string} field to the stream. */
+ public void writeStringNoTag(final String value) throws IOException {
+ // 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
+ // array and then copy.
+ final byte[] bytes = value.getBytes("UTF-8");
+ writeRawVarint32(bytes.length);
+ writeRawBytes(bytes);
+ }
+
+ /** Write a {@code group} field to the stream. */
+ public void writeGroupNoTag(final MessageNano value) throws IOException {
+ value.writeTo(this);
+ }
+
+ /** Write an embedded message field to the stream. */
+ public void writeMessageNoTag(final MessageNano value) throws IOException {
+ writeRawVarint32(value.getCachedSize());
+ value.writeTo(this);
+ }
+
+ /** Write a {@code bytes} field to the stream. */
+ public void writeBytesNoTag(final byte[] value) throws IOException {
+ writeRawVarint32(value.length);
+ writeRawBytes(value);
+ }
+
+ /** Write a {@code uint32} field to the stream. */
+ public void writeUInt32NoTag(final int value) throws IOException {
+ writeRawVarint32(value);
+ }
+
+ /**
+ * Write an enum field to the stream. Caller is responsible
+ * for converting the enum value to its numeric value.
+ */
+ public void writeEnumNoTag(final int value) throws IOException {
+ writeRawVarint32(value);
+ }
+
+ /** Write an {@code sfixed32} field to the stream. */
+ public void writeSFixed32NoTag(final int value) throws IOException {
+ writeRawLittleEndian32(value);
+ }
+
+ /** Write an {@code sfixed64} field to the stream. */
+ public void writeSFixed64NoTag(final long value) throws IOException {
+ writeRawLittleEndian64(value);
+ }
+
+ /** Write an {@code sint32} field to the stream. */
+ public void writeSInt32NoTag(final int value) throws IOException {
+ writeRawVarint32(encodeZigZag32(value));
+ }
+
+ /** Write an {@code sint64} field to the stream. */
+ public void writeSInt64NoTag(final long value) throws IOException {
+ writeRawVarint64(encodeZigZag64(value));
+ }
+
+ // =================================================================
+
+ /**
+ * 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 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 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 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 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 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 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 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 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 group} field, including tag.
+ */
+ public static int computeGroupSize(final int fieldNumber,
+ final MessageNano value) {
+ return computeTagSize(fieldNumber) * 2 + computeGroupSizeNoTag(value);
+ }
+
+ /**
+ * 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 MessageNano value) {
+ return computeTagSize(fieldNumber) + computeMessageSizeNoTag(value);
+ }
+
+ /**
+ * 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 byte[] value) {
+ return computeTagSize(fieldNumber) + computeBytesSizeNoTag(value);
+ }
+
+ /**
+ * 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
+ * enum field, including tag. Caller is responsible for converting the
+ * enum value to its numeric 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 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 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 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 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
+ * MessageSet extension to the stream. For historical reasons,
+ * the wire format differs from normal fields.
+ */
+// public static int computeMessageSetExtensionSize(
+// final int fieldNumber, final MessageMicro value) {
+// return computeTagSize(WireFormatMicro.MESSAGE_SET_ITEM) * 2 +
+// computeUInt32Size(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber) +
+// computeMessageSize(WireFormatMicro.MESSAGE_SET_MESSAGE, value);
+// }
+
+ /**
+ * 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 ByteStringMicro value) {
+// return computeTagSize(WireFormatMicro.MESSAGE_SET_ITEM) * 2 +
+// computeUInt32Size(WireFormatMicro.MESSAGE_SET_TYPE_ID, fieldNumber) +
+// computeBytesSize(WireFormatMicro.MESSAGE_SET_MESSAGE, value);
+// }
+
+ // -----------------------------------------------------------------
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code double} field, including tag.
+ */
+ public static int computeDoubleSizeNoTag(final double value) {
+ return LITTLE_ENDIAN_64_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code float} field, including tag.
+ */
+ public static int computeFloatSizeNoTag(final float value) {
+ return LITTLE_ENDIAN_32_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code uint64} field, including tag.
+ */
+ public static int computeUInt64SizeNoTag(final long value) {
+ return computeRawVarint64Size(value);
+ }
+
+ /**
+ * 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 computeRawVarint64Size(value);
+ }
+
+ /**
+ * 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) {
+ return computeRawVarint32Size(value);
+ } else {
+ // Must sign-extend.
+ return 10;
+ }
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code fixed64} field.
+ */
+ public static int computeFixed64SizeNoTag(final long value) {
+ return LITTLE_ENDIAN_64_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code fixed32} field.
+ */
+ public static int computeFixed32SizeNoTag(final int value) {
+ return LITTLE_ENDIAN_32_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code bool} field.
+ */
+ public static int computeBoolSizeNoTag(final boolean value) {
+ return 1;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code string} field.
+ */
+ public static int computeStringSizeNoTag(final String value) {
+ try {
+ final byte[] bytes = value.getBytes("UTF-8");
+ return computeRawVarint32Size(bytes.length) +
+ bytes.length;
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("UTF-8 not supported.");
+ }
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code group} field.
+ */
+ public static int computeGroupSizeNoTag(final MessageNano value) {
+ return value.getSerializedSize();
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an embedded
+ * message field.
+ */
+ public static int computeMessageSizeNoTag(final MessageNano value) {
+ final int size = value.getSerializedSize();
+ return computeRawVarint32Size(size) + size;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code bytes} field.
+ */
+ public static int computeBytesSizeNoTag(final byte[] value) {
+ return computeRawVarint32Size(value.length) + value.length;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode a
+ * {@code uint32} field.
+ */
+ public static int computeUInt32SizeNoTag(final int value) {
+ return computeRawVarint32Size(value);
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an enum field.
+ * Caller is responsible for converting the enum value to its numeric value.
+ */
+ public static int computeEnumSizeNoTag(final int value) {
+ return computeRawVarint32Size(value);
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an
+ * {@code sfixed32} field.
+ */
+ public static int computeSFixed32SizeNoTag(final int value) {
+ return LITTLE_ENDIAN_32_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an
+ * {@code sfixed64} field.
+ */
+ public static int computeSFixed64SizeNoTag(final long value) {
+ return LITTLE_ENDIAN_64_SIZE;
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an
+ * {@code sint32} field.
+ */
+ public static int computeSInt32SizeNoTag(final int value) {
+ return computeRawVarint32Size(encodeZigZag32(value));
+ }
+
+ /**
+ * Compute the number of bytes that would be needed to encode an
+ * {@code sint64} field.
+ */
+ public static int computeSInt64SizeNoTag(final long value) {
+ return computeRawVarint64Size(encodeZigZag64(value));
+ }
+
+ // =================================================================
+
+ /**
+ * If writing to a flat array, return the space left in the array.
+ * Otherwise, throws {@code UnsupportedOperationException}.
+ */
+ public int spaceLeft() {
+ return limit - position;
+ }
+
+ /**
+ * 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 void checkNoSpaceLeft() {
+ if (spaceLeft() != 0) {
+ throw new IllegalStateException(
+ "Did not write as much data as expected.");
+ }
+ }
+
+ /**
+ * 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;
+
+ OutOfSpaceException(int position, int limit) {
+ super("CodedOutputStream was writing to a flat byte array and ran " +
+ "out of space (pos " + position + " limit " + limit + ").");
+ }
+ }
+
+ /** Write a single byte. */
+ public void writeRawByte(final byte value) throws IOException {
+ if (position == limit) {
+ // We're writing to a single buffer.
+ throw new OutOfSpaceException(position, limit);
+ }
+
+ buffer[position++] = value;
+ }
+
+ /** Write a single byte, represented by an integer value. */
+ public void writeRawByte(final int value) throws IOException {
+ writeRawByte((byte) value);
+ }
+
+ /** Write an array of bytes. */
+ public void writeRawBytes(final byte[] value) throws IOException {
+ writeRawBytes(value, 0, value.length);
+ }
+
+ /** Write part of an array of bytes. */
+ public void writeRawBytes(final 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);
+ position += length;
+ } else {
+ // We're writing to a single buffer.
+ throw new OutOfSpaceException(position, limit);
+ }
+ }
+
+ /** Encode and write a tag. */
+ public void writeTag(final int fieldNumber, final int wireType)
+ throws IOException {
+ writeRawVarint32(WireFormatNano.makeTag(fieldNumber, wireType));
+ }
+
+ /** Compute the number of bytes that would be needed to encode a tag. */
+ public static int computeTagSize(final int fieldNumber) {
+ return computeRawVarint32Size(WireFormatNano.makeTag(fieldNumber, 0));
+ }
+
+ /**
+ * Encode and write a varint. {@code value} is treated as
+ * unsigned, so it won't be sign-extended if negative.
+ */
+ public void writeRawVarint32(int value) throws IOException {
+ while (true) {
+ if ((value & ~0x7F) == 0) {
+ writeRawByte(value);
+ return;
+ } else {
+ writeRawByte((value & 0x7F) | 0x80);
+ value >>>= 7;
+ }
+ }
+ }
+
+ /**
+ * 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.
+ */
+ public static int computeRawVarint32Size(final int value) {
+ if ((value & (0xffffffff << 7)) == 0) return 1;
+ if ((value & (0xffffffff << 14)) == 0) return 2;
+ if ((value & (0xffffffff << 21)) == 0) return 3;
+ if ((value & (0xffffffff << 28)) == 0) return 4;
+ return 5;
+ }
+
+ /** Encode and write a varint. */
+ public void writeRawVarint64(long value) throws IOException {
+ while (true) {
+ if ((value & ~0x7FL) == 0) {
+ writeRawByte((int)value);
+ return;
+ } else {
+ writeRawByte(((int)value & 0x7F) | 0x80);
+ value >>>= 7;
+ }
+ }
+ }
+
+ /** Compute the number of bytes that would be needed to encode a varint. */
+ public static int computeRawVarint64Size(final long value) {
+ if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
+ if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
+ if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
+ if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
+ if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
+ if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
+ if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
+ if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
+ if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
+ return 10;
+ }
+
+ /** Write a little-endian 32-bit integer. */
+ public void writeRawLittleEndian32(final int value) throws IOException {
+ writeRawByte((value ) & 0xFF);
+ writeRawByte((value >> 8) & 0xFF);
+ writeRawByte((value >> 16) & 0xFF);
+ writeRawByte((value >> 24) & 0xFF);
+ }
+
+ public static final int LITTLE_ENDIAN_32_SIZE = 4;
+
+ /** Write a little-endian 64-bit integer. */
+ public void writeRawLittleEndian64(final long value) throws IOException {
+ writeRawByte((int)(value ) & 0xFF);
+ writeRawByte((int)(value >> 8) & 0xFF);
+ writeRawByte((int)(value >> 16) & 0xFF);
+ writeRawByte((int)(value >> 24) & 0xFF);
+ writeRawByte((int)(value >> 32) & 0xFF);
+ writeRawByte((int)(value >> 40) & 0xFF);
+ writeRawByte((int)(value >> 48) & 0xFF);
+ writeRawByte((int)(value >> 56) & 0xFF);
+ }
+
+ public static final int LITTLE_ENDIAN_64_SIZE = 8;
+
+ /**
+ * 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.
+ */
+ public static int encodeZigZag32(final int n) {
+ // Note: the right-shift must be arithmetic
+ return (n << 1) ^ (n >> 31);
+ }
+
+ /**
+ * 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.
+ */
+ public static long encodeZigZag64(final long n) {
+ // Note: the right-shift must be arithmetic
+ return (n << 1) ^ (n >> 63);
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java b/javanano/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
new file mode 100644
index 00000000..b979390d
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
@@ -0,0 +1,187 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+
+/**
+ * Base class of those Protocol Buffer messages that need to store unknown fields,
+ * such as extensions.
+ */
+public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>>
+ extends MessageNano {
+ /**
+ * A container for fields unknown to the message, including extensions. Extension fields can
+ * can be accessed through the {@link #getExtension} and {@link #setExtension} methods.
+ */
+ protected FieldArray unknownFieldData;
+
+ @Override
+ protected int computeSerializedSize() {
+ int size = 0;
+ if (unknownFieldData != null) {
+ for (int i = 0; i < unknownFieldData.size(); i++) {
+ FieldData field = unknownFieldData.dataAt(i);
+ size += field.computeSerializedSize();
+ }
+ }
+ return size;
+ }
+
+ @Override
+ public void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ if (unknownFieldData == null) {
+ return;
+ }
+ for (int i = 0; i < unknownFieldData.size(); i++) {
+ FieldData field = unknownFieldData.dataAt(i);
+ field.writeTo(output);
+ }
+ }
+
+ /**
+ * Checks if there is a value stored for the specified extension in this
+ * message.
+ */
+ public final boolean hasExtension(Extension<M, ?> extension) {
+ if (unknownFieldData == null) {
+ return false;
+ }
+ FieldData field = unknownFieldData.get(WireFormatNano.getTagFieldNumber(extension.tag));
+ return field != null;
+ }
+
+ /**
+ * Gets the value stored in the specified extension of this message.
+ */
+ public final <T> T getExtension(Extension<M, T> extension) {
+ if (unknownFieldData == null) {
+ return null;
+ }
+ FieldData field = unknownFieldData.get(WireFormatNano.getTagFieldNumber(extension.tag));
+ return field == null ? null : field.getValue(extension);
+ }
+
+ /**
+ * Sets the value of the specified extension of this message.
+ */
+ public final <T> M setExtension(Extension<M, T> extension, T value) {
+ int fieldNumber = WireFormatNano.getTagFieldNumber(extension.tag);
+ if (value == null) {
+ if (unknownFieldData != null) {
+ unknownFieldData.remove(fieldNumber);
+ if (unknownFieldData.isEmpty()) {
+ unknownFieldData = null;
+ }
+ }
+ } else {
+ FieldData field = null;
+ if (unknownFieldData == null) {
+ unknownFieldData = new FieldArray();
+ } else {
+ field = unknownFieldData.get(fieldNumber);
+ }
+ if (field == null) {
+ unknownFieldData.put(fieldNumber, new FieldData(extension, value));
+ } else {
+ field.setValue(extension, value);
+ }
+ }
+
+ @SuppressWarnings("unchecked") // Generated code should guarantee type safety
+ M typedThis = (M) this;
+ return typedThis;
+ }
+
+ /**
+ * Stores the binary data of an unknown field.
+ *
+ * <p>Generated messages will call this for unknown fields if the store_unknown_fields
+ * option is on.
+ *
+ * <p>Note that the tag might be a end-group tag (rather than the start of an unknown field) in
+ * which case we do not want to add an unknown field entry.
+ *
+ * @param input the input buffer.
+ * @param tag the tag of the field.
+
+ * @return {@literal true} unless the tag is an end-group tag.
+ */
+ protected final boolean storeUnknownField(CodedInputByteBufferNano input, int tag)
+ throws IOException {
+ int startPos = input.getPosition();
+ if (!input.skipField(tag)) {
+ return false; // This wasn't an unknown field, it's an end-group tag.
+ }
+ int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
+ int endPos = input.getPosition();
+ byte[] bytes = input.getData(startPos, endPos - startPos);
+ UnknownFieldData unknownField = new UnknownFieldData(tag, bytes);
+
+ FieldData field = null;
+ if (unknownFieldData == null) {
+ unknownFieldData = new FieldArray();
+ } else {
+ field = unknownFieldData.get(fieldNumber);
+ }
+ if (field == null) {
+ field = new FieldData();
+ unknownFieldData.put(fieldNumber, field);
+ }
+ field.addUnknownField(unknownField);
+ return true;
+ }
+
+ /**
+ * Returns whether the stored unknown field data in this message is equivalent to that in the
+ * other message.
+ *
+ * @param other the other message.
+ * @return whether the two sets of unknown field data are equal.
+ */
+ protected final boolean unknownFieldDataEquals(M other) {
+ if (unknownFieldData == null || unknownFieldData.isEmpty()) {
+ return other.unknownFieldData == null || other.unknownFieldData.isEmpty();
+ } else {
+ return unknownFieldData.equals(other.unknownFieldData);
+ }
+ }
+
+ /**
+ * Computes the hashcode representing the unknown field data stored in this message.
+ *
+ * @return the hashcode for the unknown field data.
+ */
+ protected final int unknownFieldDataHashCode() {
+ return (unknownFieldData == null || unknownFieldData.isEmpty()
+ ? 0 : unknownFieldData.hashCode());
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/Extension.java b/javanano/src/main/java/com/google/protobuf/nano/Extension.java
new file mode 100644
index 00000000..5d18ae6e
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/Extension.java
@@ -0,0 +1,722 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents an extension.
+ *
+ * @author bduff@google.com (Brian Duff)
+ * @author maxtroy@google.com (Max Cai)
+ * @param <M> the type of the extendable message this extension is for.
+ * @param <T> the Java type of the extension; see {@link #clazz}.
+ */
+public class Extension<M extends ExtendableMessageNano<M>, T> {
+
+ /*
+ * Because we typically only define message-typed extensions, the Extension class hierarchy is
+ * designed as follows, to allow a big amount of code in this file to be removed by ProGuard:
+ *
+ * Extension // ready to use for message/group typed extensions
+ * Δ
+ * |
+ * PrimitiveExtension // for primitive/enum typed extensions
+ */
+
+ public static final int TYPE_DOUBLE = 1;
+ public static final int TYPE_FLOAT = 2;
+ public static final int TYPE_INT64 = 3;
+ public static final int TYPE_UINT64 = 4;
+ public static final int TYPE_INT32 = 5;
+ public static final int TYPE_FIXED64 = 6;
+ public static final int TYPE_FIXED32 = 7;
+ public static final int TYPE_BOOL = 8;
+ public static final int TYPE_STRING = 9;
+ public static final int TYPE_GROUP = 10;
+ public static final int TYPE_MESSAGE = 11;
+ public static final int TYPE_BYTES = 12;
+ public static final int TYPE_UINT32 = 13;
+ public static final int TYPE_ENUM = 14;
+ public static final int TYPE_SFIXED32 = 15;
+ public static final int TYPE_SFIXED64 = 16;
+ public static final int TYPE_SINT32 = 17;
+ public static final int TYPE_SINT64 = 18;
+
+ /**
+ * Creates an {@code Extension} of the given message type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
+ */
+ public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
+ Extension<M, T> createMessageTyped(int type, Class<T> clazz, int tag) {
+ return new Extension<M, T>(type, clazz, tag, false);
+ }
+
+ /**
+ * Creates a repeated {@code Extension} of the given message type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type {@link #TYPE_MESSAGE} or {@link #TYPE_GROUP}
+ */
+ public static <M extends ExtendableMessageNano<M>, T extends MessageNano>
+ Extension<M, T[]> createRepeatedMessageTyped(int type, Class<T[]> clazz, int tag) {
+ return new Extension<M, T[]>(type, clazz, tag, true);
+ }
+
+ /**
+ * Creates an {@code Extension} of the given primitive type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
+ * @param clazz the boxed Java type of this extension
+ */
+ public static <M extends ExtendableMessageNano<M>, T>
+ Extension<M, T> createPrimitiveTyped(int type, Class<T> clazz, int tag) {
+ return new PrimitiveExtension<M, T>(type, clazz, tag, false, 0, 0);
+ }
+
+ /**
+ * Creates a repeated {@code Extension} of the given primitive type and tag number.
+ * Should be used by the generated code only.
+ *
+ * @param type one of {@code TYPE_*}, except {@link #TYPE_MESSAGE} and {@link #TYPE_GROUP}
+ * @param clazz the Java array type of this extension, with an unboxed component type
+ */
+ public static <M extends ExtendableMessageNano<M>, T>
+ Extension<M, T> createRepeatedPrimitiveTyped(
+ int type, Class<T> clazz, int tag, int nonPackedTag, int packedTag) {
+ return new PrimitiveExtension<M, T>(type, clazz, tag, true, nonPackedTag, packedTag);
+ }
+
+ /**
+ * Protocol Buffer type of this extension; one of the {@code TYPE_} constants.
+ */
+ protected final int type;
+
+ /**
+ * Java type of this extension. For a singular extension, this is the boxed Java type for the
+ * Protocol Buffer {@link #type}; for a repeated extension, this is an array type whose
+ * component type is the unboxed Java type for {@link #type}. For example, for a singular
+ * {@code int32}/{@link #TYPE_INT32} extension, this equals {@code Integer.class}; for a
+ * repeated {@code int32} extension, this equals {@code int[].class}.
+ */
+ protected final Class<T> clazz;
+
+ /**
+ * Tag number of this extension.
+ */
+ public final int tag;
+
+ /**
+ * Whether this extension is repeated.
+ */
+ protected final boolean repeated;
+
+ private Extension(int type, Class<T> clazz, int tag, boolean repeated) {
+ this.type = type;
+ this.clazz = clazz;
+ this.tag = tag;
+ this.repeated = repeated;
+ }
+
+ /**
+ * Returns the value of this extension stored in the given list of unknown fields, or
+ * {@code null} if no unknown fields matches this extension.
+ *
+ * @param unknownFields a list of {@link UnknownFieldData}. All of the elements must have a tag
+ * that matches this Extension's tag.
+ *
+ */
+ final T getValueFrom(List<UnknownFieldData> unknownFields) {
+ if (unknownFields == null) {
+ return null;
+ }
+ return repeated ? getRepeatedValueFrom(unknownFields) : getSingularValueFrom(unknownFields);
+ }
+
+ private T getRepeatedValueFrom(List<UnknownFieldData> unknownFields) {
+ // For repeated extensions, read all matching unknown fields in their original order.
+ List<Object> resultList = new ArrayList<Object>();
+ for (int i = 0; i < unknownFields.size(); i++) {
+ UnknownFieldData data = unknownFields.get(i);
+ if (data.bytes.length != 0) {
+ readDataInto(data, resultList);
+ }
+ }
+
+ int resultSize = resultList.size();
+ if (resultSize == 0) {
+ return null;
+ } else {
+ T result = clazz.cast(Array.newInstance(clazz.getComponentType(), resultSize));
+ for (int i = 0; i < resultSize; i++) {
+ Array.set(result, i, resultList.get(i));
+ }
+ return result;
+ }
+ }
+
+ private T getSingularValueFrom(List<UnknownFieldData> unknownFields) {
+ // For singular extensions, get the last piece of data stored under this extension.
+ if (unknownFields.isEmpty()) {
+ return null;
+ }
+ UnknownFieldData lastData = unknownFields.get(unknownFields.size() - 1);
+ return clazz.cast(readData(CodedInputByteBufferNano.newInstance(lastData.bytes)));
+ }
+
+ protected Object readData(CodedInputByteBufferNano input) {
+ // This implementation is for message/group extensions.
+ Class<?> messageType = repeated ? clazz.getComponentType() : clazz;
+ try {
+ switch (type) {
+ case TYPE_GROUP:
+ MessageNano group = (MessageNano) messageType.newInstance();
+ input.readGroup(group, WireFormatNano.getTagFieldNumber(tag));
+ return group;
+ case TYPE_MESSAGE:
+ MessageNano message = (MessageNano) messageType.newInstance();
+ input.readMessage(message);
+ return message;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(
+ "Error creating instance of class " + messageType, e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(
+ "Error creating instance of class " + messageType, e);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ }
+
+ protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
+ // This implementation is for message/group extensions.
+ resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
+ }
+
+ void writeTo(Object value, CodedOutputByteBufferNano output) throws IOException {
+ if (repeated) {
+ writeRepeatedData(value, output);
+ } else {
+ writeSingularData(value, output);
+ }
+ }
+
+ protected void writeSingularData(Object value, CodedOutputByteBufferNano out) {
+ // This implementation is for message/group extensions.
+ try {
+ out.writeRawVarint32(tag);
+ switch (type) {
+ case TYPE_GROUP:
+ MessageNano groupValue = (MessageNano) value;
+ int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
+ out.writeGroupNoTag(groupValue);
+ // The endgroup tag must be included in the data payload.
+ out.writeTag(fieldNumber, WireFormatNano.WIRETYPE_END_GROUP);
+ break;
+ case TYPE_MESSAGE:
+ MessageNano messageValue = (MessageNano) value;
+ out.writeMessageNoTag(messageValue);
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen
+ throw new IllegalStateException(e);
+ }
+ }
+
+ protected void writeRepeatedData(Object array, CodedOutputByteBufferNano output) {
+ // This implementation is for non-packed extensions.
+ int arrayLength = Array.getLength(array);
+ for (int i = 0; i < arrayLength; i++) {
+ Object element = Array.get(array, i);
+ if (element != null) {
+ writeSingularData(element, output);
+ }
+ }
+ }
+
+ int computeSerializedSize(Object value) {
+ if (repeated) {
+ return computeRepeatedSerializedSize(value);
+ } else {
+ return computeSingularSerializedSize(value);
+ }
+ }
+
+ protected int computeRepeatedSerializedSize(Object array) {
+ // This implementation is for non-packed extensions.
+ int size = 0;
+ int arrayLength = Array.getLength(array);
+ for (int i = 0; i < arrayLength; i++) {
+ Object element = Array.get(array, i);
+ if (element != null) {
+ size += computeSingularSerializedSize(Array.get(array, i));
+ }
+ }
+ return size;
+ }
+
+ protected int computeSingularSerializedSize(Object value) {
+ // This implementation is for message/group extensions.
+ int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
+ switch (type) {
+ case TYPE_GROUP:
+ MessageNano groupValue = (MessageNano) value;
+ return CodedOutputByteBufferNano.computeGroupSize(fieldNumber, groupValue);
+ case TYPE_MESSAGE:
+ MessageNano messageValue = (MessageNano) value;
+ return CodedOutputByteBufferNano.computeMessageSize(fieldNumber, messageValue);
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ }
+
+ /**
+ * Represents an extension of a primitive (including enum) type. If there is no primitive
+ * extensions, this subclass will be removable by ProGuard.
+ */
+ private static class PrimitiveExtension<M extends ExtendableMessageNano<M>, T>
+ extends Extension<M, T> {
+
+ /**
+ * Tag of a piece of non-packed data from the wire compatible with this extension.
+ */
+ private final int nonPackedTag;
+
+ /**
+ * Tag of a piece of packed data from the wire compatible with this extension.
+ * 0 if the type of this extension is not packable.
+ */
+ private final int packedTag;
+
+ public PrimitiveExtension(int type, Class<T> clazz, int tag, boolean repeated,
+ int nonPackedTag, int packedTag) {
+ super(type, clazz, tag, repeated);
+ this.nonPackedTag = nonPackedTag;
+ this.packedTag = packedTag;
+ }
+
+ @Override
+ protected Object readData(CodedInputByteBufferNano input) {
+ try {
+ switch (type) {
+ case TYPE_DOUBLE:
+ return input.readDouble();
+ case TYPE_FLOAT:
+ return input.readFloat();
+ case TYPE_INT64:
+ return input.readInt64();
+ case TYPE_UINT64:
+ return input.readUInt64();
+ case TYPE_INT32:
+ return input.readInt32();
+ case TYPE_FIXED64:
+ return input.readFixed64();
+ case TYPE_FIXED32:
+ return input.readFixed32();
+ case TYPE_BOOL:
+ return input.readBool();
+ case TYPE_STRING:
+ return input.readString();
+ case TYPE_BYTES:
+ return input.readBytes();
+ case TYPE_UINT32:
+ return input.readUInt32();
+ case TYPE_ENUM:
+ return input.readEnum();
+ case TYPE_SFIXED32:
+ return input.readSFixed32();
+ case TYPE_SFIXED64:
+ return input.readSFixed64();
+ case TYPE_SINT32:
+ return input.readSInt32();
+ case TYPE_SINT64:
+ return input.readSInt64();
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ }
+
+ @Override
+ protected void readDataInto(UnknownFieldData data, List<Object> resultList) {
+ // This implementation is for primitive typed extensions,
+ // which can read both packed and non-packed data.
+ if (data.tag == nonPackedTag) {
+ resultList.add(readData(CodedInputByteBufferNano.newInstance(data.bytes)));
+ } else {
+ CodedInputByteBufferNano buffer =
+ CodedInputByteBufferNano.newInstance(data.bytes);
+ try {
+ buffer.pushLimit(buffer.readRawVarint32()); // length limit
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Error reading extension field", e);
+ }
+ while (!buffer.isAtEnd()) {
+ resultList.add(readData(buffer));
+ }
+ }
+ }
+
+ @Override
+ protected final void writeSingularData(Object value, CodedOutputByteBufferNano output) {
+ try {
+ output.writeRawVarint32(tag);
+ switch (type) {
+ case TYPE_DOUBLE:
+ Double doubleValue = (Double) value;
+ output.writeDoubleNoTag(doubleValue);
+ break;
+ case TYPE_FLOAT:
+ Float floatValue = (Float) value;
+ output.writeFloatNoTag(floatValue);
+ break;
+ case TYPE_INT64:
+ Long int64Value = (Long) value;
+ output.writeInt64NoTag(int64Value);
+ break;
+ case TYPE_UINT64:
+ Long uint64Value = (Long) value;
+ output.writeUInt64NoTag(uint64Value);
+ break;
+ case TYPE_INT32:
+ Integer int32Value = (Integer) value;
+ output.writeInt32NoTag(int32Value);
+ break;
+ case TYPE_FIXED64:
+ Long fixed64Value = (Long) value;
+ output.writeFixed64NoTag(fixed64Value);
+ break;
+ case TYPE_FIXED32:
+ Integer fixed32Value = (Integer) value;
+ output.writeFixed32NoTag(fixed32Value);
+ break;
+ case TYPE_BOOL:
+ Boolean boolValue = (Boolean) value;
+ output.writeBoolNoTag(boolValue);
+ break;
+ case TYPE_STRING:
+ String stringValue = (String) value;
+ output.writeStringNoTag(stringValue);
+ break;
+ case TYPE_BYTES:
+ byte[] bytesValue = (byte[]) value;
+ output.writeBytesNoTag(bytesValue);
+ break;
+ case TYPE_UINT32:
+ Integer uint32Value = (Integer) value;
+ output.writeUInt32NoTag(uint32Value);
+ break;
+ case TYPE_ENUM:
+ Integer enumValue = (Integer) value;
+ output.writeEnumNoTag(enumValue);
+ break;
+ case TYPE_SFIXED32:
+ Integer sfixed32Value = (Integer) value;
+ output.writeSFixed32NoTag(sfixed32Value);
+ break;
+ case TYPE_SFIXED64:
+ Long sfixed64Value = (Long) value;
+ output.writeSFixed64NoTag(sfixed64Value);
+ break;
+ case TYPE_SINT32:
+ Integer sint32Value = (Integer) value;
+ output.writeSInt32NoTag(sint32Value);
+ break;
+ case TYPE_SINT64:
+ Long sint64Value = (Long) value;
+ output.writeSInt64NoTag(sint64Value);
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ protected void writeRepeatedData(Object array, CodedOutputByteBufferNano output) {
+ if (tag == nonPackedTag) {
+ // Use base implementation for non-packed data
+ super.writeRepeatedData(array, output);
+ } else if (tag == packedTag) {
+ // Packed. Note that the array element type is guaranteed to be primitive, so there
+ // won't be any null elements, so no null check in this block.
+ int arrayLength = Array.getLength(array);
+ int dataSize = computePackedDataSize(array);
+
+ try {
+ output.writeRawVarint32(tag);
+ output.writeRawVarint32(dataSize);
+ switch (type) {
+ case TYPE_BOOL:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeBoolNoTag(Array.getBoolean(array, i));
+ }
+ break;
+ case TYPE_FIXED32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFixed32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SFIXED32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSFixed32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_FLOAT:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFloatNoTag(Array.getFloat(array, i));
+ }
+ break;
+ case TYPE_FIXED64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeFixed64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SFIXED64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSFixed64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_DOUBLE:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeDoubleNoTag(Array.getDouble(array, i));
+ }
+ break;
+ case TYPE_INT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_UINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeUInt32NoTag(Array.getInt(array, i));
+ }
+ break;
+ case TYPE_INT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeSInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_UINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeUInt64NoTag(Array.getLong(array, i));
+ }
+ break;
+ case TYPE_ENUM:
+ for (int i = 0; i < arrayLength; i++) {
+ output.writeEnumNoTag(Array.getInt(array, i));
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Unpackable type " + type);
+ }
+ } catch (IOException e) {
+ // Should not happen.
+ throw new IllegalStateException(e);
+ }
+ } else {
+ throw new IllegalArgumentException("Unexpected repeated extension tag " + tag
+ + ", unequal to both non-packed variant " + nonPackedTag
+ + " and packed variant " + packedTag);
+ }
+ }
+
+ private int computePackedDataSize(Object array) {
+ int dataSize = 0;
+ int arrayLength = Array.getLength(array);
+ switch (type) {
+ case TYPE_BOOL:
+ // Bools are stored as int32 but just as 0 or 1, so 1 byte each.
+ dataSize = arrayLength;
+ break;
+ case TYPE_FIXED32:
+ case TYPE_SFIXED32:
+ case TYPE_FLOAT:
+ dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_32_SIZE;
+ break;
+ case TYPE_FIXED64:
+ case TYPE_SFIXED64:
+ case TYPE_DOUBLE:
+ dataSize = arrayLength * CodedOutputByteBufferNano.LITTLE_ENDIAN_64_SIZE;
+ break;
+ case TYPE_INT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_SINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeSInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_UINT32:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeUInt32SizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ case TYPE_INT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_SINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeSInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_UINT64:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeUInt64SizeNoTag(
+ Array.getLong(array, i));
+ }
+ break;
+ case TYPE_ENUM:
+ for (int i = 0; i < arrayLength; i++) {
+ dataSize += CodedOutputByteBufferNano.computeEnumSizeNoTag(
+ Array.getInt(array, i));
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Unexpected non-packable type " + type);
+ }
+ return dataSize;
+ }
+
+ @Override
+ protected int computeRepeatedSerializedSize(Object array) {
+ if (tag == nonPackedTag) {
+ // Use base implementation for non-packed data
+ return super.computeRepeatedSerializedSize(array);
+ } else if (tag == packedTag) {
+ // Packed.
+ int dataSize = computePackedDataSize(array);
+ int payloadSize =
+ dataSize + CodedOutputByteBufferNano.computeRawVarint32Size(dataSize);
+ return payloadSize + CodedOutputByteBufferNano.computeRawVarint32Size(tag);
+ } else {
+ throw new IllegalArgumentException("Unexpected repeated extension tag " + tag
+ + ", unequal to both non-packed variant " + nonPackedTag
+ + " and packed variant " + packedTag);
+ }
+ }
+
+ @Override
+ protected final int computeSingularSerializedSize(Object value) {
+ int fieldNumber = WireFormatNano.getTagFieldNumber(tag);
+ switch (type) {
+ case TYPE_DOUBLE:
+ Double doubleValue = (Double) value;
+ return CodedOutputByteBufferNano.computeDoubleSize(fieldNumber, doubleValue);
+ case TYPE_FLOAT:
+ Float floatValue = (Float) value;
+ return CodedOutputByteBufferNano.computeFloatSize(fieldNumber, floatValue);
+ case TYPE_INT64:
+ Long int64Value = (Long) value;
+ return CodedOutputByteBufferNano.computeInt64Size(fieldNumber, int64Value);
+ case TYPE_UINT64:
+ Long uint64Value = (Long) value;
+ return CodedOutputByteBufferNano.computeUInt64Size(fieldNumber, uint64Value);
+ case TYPE_INT32:
+ Integer int32Value = (Integer) value;
+ return CodedOutputByteBufferNano.computeInt32Size(fieldNumber, int32Value);
+ case TYPE_FIXED64:
+ Long fixed64Value = (Long) value;
+ return CodedOutputByteBufferNano.computeFixed64Size(fieldNumber, fixed64Value);
+ case TYPE_FIXED32:
+ Integer fixed32Value = (Integer) value;
+ return CodedOutputByteBufferNano.computeFixed32Size(fieldNumber, fixed32Value);
+ case TYPE_BOOL:
+ Boolean boolValue = (Boolean) value;
+ return CodedOutputByteBufferNano.computeBoolSize(fieldNumber, boolValue);
+ case TYPE_STRING:
+ String stringValue = (String) value;
+ return CodedOutputByteBufferNano.computeStringSize(fieldNumber, stringValue);
+ case TYPE_BYTES:
+ byte[] bytesValue = (byte[]) value;
+ return CodedOutputByteBufferNano.computeBytesSize(fieldNumber, bytesValue);
+ case TYPE_UINT32:
+ Integer uint32Value = (Integer) value;
+ return CodedOutputByteBufferNano.computeUInt32Size(fieldNumber, uint32Value);
+ case TYPE_ENUM:
+ Integer enumValue = (Integer) value;
+ return CodedOutputByteBufferNano.computeEnumSize(fieldNumber, enumValue);
+ case TYPE_SFIXED32:
+ Integer sfixed32Value = (Integer) value;
+ return CodedOutputByteBufferNano.computeSFixed32Size(fieldNumber,
+ sfixed32Value);
+ case TYPE_SFIXED64:
+ Long sfixed64Value = (Long) value;
+ return CodedOutputByteBufferNano.computeSFixed64Size(fieldNumber,
+ sfixed64Value);
+ case TYPE_SINT32:
+ Integer sint32Value = (Integer) value;
+ return CodedOutputByteBufferNano.computeSInt32Size(fieldNumber, sint32Value);
+ case TYPE_SINT64:
+ Long sint64Value = (Long) value;
+ return CodedOutputByteBufferNano.computeSInt64Size(fieldNumber, sint64Value);
+ default:
+ throw new IllegalArgumentException("Unknown type " + type);
+ }
+ }
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/FieldArray.java b/javanano/src/main/java/com/google/protobuf/nano/FieldArray.java
new file mode 100644
index 00000000..cdb66da2
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/FieldArray.java
@@ -0,0 +1,273 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+package com.google.protobuf.nano;
+
+
+/**
+ * A custom version of {@link android.util.SparseArray} with the minimal API
+ * for storing {@link FieldData} objects.
+ *
+ * Based on {@link android.support.v4.util.SpareArrayCompat}.
+ */
+class FieldArray {
+ private static final FieldData DELETED = new FieldData();
+ private boolean mGarbage = false;
+
+ private int[] mFieldNumbers;
+ private FieldData[] mData;
+ private int mSize;
+
+ /**
+ * Creates a new FieldArray containing no fields.
+ */
+ public FieldArray() {
+ this(10);
+ }
+
+ /**
+ * Creates a new FieldArray containing no mappings that will not
+ * require any additional memory allocation to store the specified
+ * number of mappings.
+ */
+ public FieldArray(int initialCapacity) {
+ initialCapacity = idealIntArraySize(initialCapacity);
+ mFieldNumbers = new int[initialCapacity];
+ mData = new FieldData[initialCapacity];
+ mSize = 0;
+ }
+
+ /**
+ * Gets the FieldData mapped from the specified fieldNumber, or <code>null</code>
+ * if no such mapping has been made.
+ */
+ public FieldData get(int fieldNumber) {
+ int i = binarySearch(fieldNumber);
+
+ if (i < 0 || mData[i] == DELETED) {
+ return null;
+ } else {
+ return mData[i];
+ }
+ }
+
+ /**
+ * Removes the data from the specified fieldNumber, if there was any.
+ */
+ public void remove(int fieldNumber) {
+ int i = binarySearch(fieldNumber);
+
+ if (i >= 0 && mData[i] != DELETED) {
+ mData[i] = DELETED;
+ mGarbage = true;
+ }
+ }
+
+ private void gc() {
+ int n = mSize;
+ int o = 0;
+ int[] keys = mFieldNumbers;
+ FieldData[] values = mData;
+
+ for (int i = 0; i < n; i++) {
+ FieldData val = values[i];
+
+ if (val != DELETED) {
+ if (i != o) {
+ keys[o] = keys[i];
+ values[o] = val;
+ values[i] = null;
+ }
+
+ o++;
+ }
+ }
+
+ mGarbage = false;
+ mSize = o;
+ }
+
+ /**
+ * Adds a mapping from the specified fieldNumber to the specified data,
+ * replacing the previous mapping if there was one.
+ */
+ public void put(int fieldNumber, FieldData data) {
+ int i = binarySearch(fieldNumber);
+
+ if (i >= 0) {
+ mData[i] = data;
+ } else {
+ i = ~i;
+
+ if (i < mSize && mData[i] == DELETED) {
+ mFieldNumbers[i] = fieldNumber;
+ mData[i] = data;
+ return;
+ }
+
+ if (mGarbage && mSize >= mFieldNumbers.length) {
+ gc();
+
+ // Search again because indices may have changed.
+ i = ~ binarySearch(fieldNumber);
+ }
+
+ if (mSize >= mFieldNumbers.length) {
+ int n = idealIntArraySize(mSize + 1);
+
+ int[] nkeys = new int[n];
+ FieldData[] nvalues = new FieldData[n];
+
+ System.arraycopy(mFieldNumbers, 0, nkeys, 0, mFieldNumbers.length);
+ System.arraycopy(mData, 0, nvalues, 0, mData.length);
+
+ mFieldNumbers = nkeys;
+ mData = nvalues;
+ }
+
+ if (mSize - i != 0) {
+ System.arraycopy(mFieldNumbers, i, mFieldNumbers, i + 1, mSize - i);
+ System.arraycopy(mData, i, mData, i + 1, mSize - i);
+ }
+
+ mFieldNumbers[i] = fieldNumber;
+ mData[i] = data;
+ mSize++;
+ }
+ }
+
+ /**
+ * Returns the number of key-value mappings that this FieldArray
+ * currently stores.
+ */
+ public int size() {
+ if (mGarbage) {
+ gc();
+ }
+
+ return mSize;
+ }
+
+ public boolean isEmpty() {
+ return size() == 0;
+ }
+
+ /**
+ * Given an index in the range <code>0...size()-1</code>, returns
+ * the value from the <code>index</code>th key-value mapping that this
+ * FieldArray stores.
+ */
+ public FieldData dataAt(int index) {
+ if (mGarbage) {
+ gc();
+ }
+
+ return mData[index];
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof FieldArray)) {
+ return false;
+ }
+
+ FieldArray other = (FieldArray) o;
+ if (size() != other.size()) { // size() will call gc() if necessary.
+ return false;
+ }
+ return arrayEquals(mFieldNumbers, other.mFieldNumbers, mSize) &&
+ arrayEquals(mData, other.mData, mSize);
+ }
+
+ @Override
+ public int hashCode() {
+ if (mGarbage) {
+ gc();
+ }
+ int result = 17;
+ for (int i = 0; i < mSize; i++) {
+ result = 31 * result + mFieldNumbers[i];
+ result = 31 * result + mData[i].hashCode();
+ }
+ return result;
+ }
+
+ private int idealIntArraySize(int need) {
+ return idealByteArraySize(need * 4) / 4;
+ }
+
+ private int idealByteArraySize(int need) {
+ for (int i = 4; i < 32; i++)
+ if (need <= (1 << i) - 12)
+ return (1 << i) - 12;
+
+ return need;
+ }
+
+ private int binarySearch(int value) {
+ int lo = 0;
+ int hi = mSize - 1;
+
+ while (lo <= hi) {
+ int mid = (lo + hi) >>> 1;
+ int midVal = mFieldNumbers[mid];
+
+ if (midVal < value) {
+ lo = mid + 1;
+ } else if (midVal > value) {
+ hi = mid - 1;
+ } else {
+ return mid; // value found
+ }
+ }
+ return ~lo; // value not present
+ }
+
+ private boolean arrayEquals(int[] a, int[] b, int size) {
+ for (int i = 0; i < size; i++) {
+ if (a[i] != b[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean arrayEquals(FieldData[] a, FieldData[] b, int size) {
+ for (int i = 0; i < size; i++) {
+ if (!a[i].equals(b[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/FieldData.java b/javanano/src/main/java/com/google/protobuf/nano/FieldData.java
new file mode 100644
index 00000000..21ead88b
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/FieldData.java
@@ -0,0 +1,190 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+package com.google.protobuf.nano;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Stores unknown fields. These might be extensions or fields that the generated API doesn't
+ * know about yet.
+ */
+class FieldData {
+ private Extension<?, ?> cachedExtension;
+ private Object value;
+ /** The serialised values for this object. Will be cleared if getValue is called */
+ private List<UnknownFieldData> unknownFieldData;
+
+ <T> FieldData(Extension<?, T> extension, T newValue) {
+ cachedExtension = extension;
+ value = newValue;
+ }
+
+ FieldData() {
+ unknownFieldData = new ArrayList<UnknownFieldData>();
+ }
+
+ void addUnknownField(UnknownFieldData unknownField) {
+ unknownFieldData.add(unknownField);
+ }
+
+ UnknownFieldData getUnknownField(int index) {
+ if (unknownFieldData == null) {
+ return null;
+ }
+ if (index < unknownFieldData.size()) {
+ return unknownFieldData.get(index);
+ }
+ return null;
+ }
+
+ int getUnknownFieldSize() {
+ if (unknownFieldData == null) {
+ return 0;
+ }
+ return unknownFieldData.size();
+ }
+
+ <T> T getValue(Extension<?, T> extension) {
+ if (value != null){
+ if (cachedExtension != extension) { // Extension objects are singletons.
+ throw new IllegalStateException(
+ "Tried to getExtension with a differernt Extension.");
+ }
+ } else {
+ cachedExtension = extension;
+ value = extension.getValueFrom(unknownFieldData);
+ unknownFieldData = null;
+ }
+ return (T) value;
+ }
+
+ <T> void setValue(Extension<?, T> extension, T newValue) {
+ cachedExtension = extension;
+ value = newValue;
+ unknownFieldData = null;
+ }
+
+ int computeSerializedSize() {
+ int size = 0;
+ if (value != null) {
+ size = cachedExtension.computeSerializedSize(value);
+ } else {
+ for (UnknownFieldData unknownField : unknownFieldData) {
+ size += unknownField.computeSerializedSize();
+ }
+ }
+ return size;
+ }
+
+ void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ if (value != null) {
+ cachedExtension.writeTo(value, output);
+ } else {
+ for (UnknownFieldData unknownField : unknownFieldData) {
+ unknownField.writeTo(output);
+ }
+ }
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof FieldData)) {
+ return false;
+ }
+
+ FieldData other = (FieldData) o;
+ if (value != null && other.value != null) {
+ // If both objects have deserialized values, compare those.
+ // Since unknown fields are only compared if messages have generated equals methods
+ // we know this will be a meaningful comparison (not identity) for all values.
+ if (cachedExtension != other.cachedExtension) { // Extension objects are singletons.
+ return false;
+ }
+ if (!cachedExtension.clazz.isArray()) {
+ // Can't test (!cachedExtension.repeated) due to 'bytes' -> 'byte[]'
+ return value.equals(other.value);
+ }
+ if (value instanceof byte[]) {
+ return Arrays.equals((byte[]) value, (byte[]) other.value);
+ } else if (value instanceof int[]) {
+ return Arrays.equals((int[]) value, (int[]) other.value);
+ } else if (value instanceof long[]) {
+ return Arrays.equals((long[]) value, (long[]) other.value);
+ } else if (value instanceof float[]) {
+ return Arrays.equals((float[]) value, (float[]) other.value);
+ } else if (value instanceof double[]) {
+ return Arrays.equals((double[]) value, (double[]) other.value);
+ } else if (value instanceof boolean[]) {
+ return Arrays.equals((boolean[]) value, (boolean[]) other.value);
+ } else {
+ return Arrays.deepEquals((Object[]) value, (Object[]) other.value);
+ }
+ }
+ if (unknownFieldData != null && other.unknownFieldData != null) {
+ // If both objects have byte arrays compare those directly.
+ return unknownFieldData.equals(other.unknownFieldData);
+ }
+ try {
+ // As a last resort, serialize and compare the resulting byte arrays.
+ return Arrays.equals(toByteArray(), other.toByteArray());
+ } catch (IOException e) {
+ // Should not happen.
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ int result = 17;
+ try {
+ // The only way to generate a consistent hash is to use the serialized form.
+ result = 31 * result + Arrays.hashCode(toByteArray());
+ } catch (IOException e) {
+ // Should not happen.
+ throw new IllegalStateException(e);
+ }
+ return result;
+ }
+
+ private byte[] toByteArray() throws IOException {
+ byte[] result = new byte[computeSerializedSize()];
+ CodedOutputByteBufferNano output = CodedOutputByteBufferNano.newInstance(result);
+ writeTo(output);
+ return result;
+ }
+
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
new file mode 100644
index 00000000..e08bb4b7
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java
@@ -0,0 +1,333 @@
+// 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.nano;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+
+/**
+ * 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)
+ */
+public final class InternalNano {
+
+ private InternalNano() {}
+
+ /**
+ * An object to provide synchronization when lazily initializing static fields
+ * of {@link MessageNano} subclasses.
+ * <p>
+ * To enable earlier versions of ProGuard to inline short methods from a
+ * generated MessageNano subclass to the call sites, that class must not have
+ * a class initializer, which will be created if there is any static variable
+ * initializers. To lazily initialize the static variables in a thread-safe
+ * manner, the initialization code will synchronize on this object.
+ */
+ public static final Object LAZY_INIT_LOCK = new Object();
+
+ /**
+ * Helper called by generated code to construct default values for string
+ * fields.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * 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) {
+ try {
+ return new String(bytes.getBytes("ISO-8859-1"), "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ // This should never happen since all JVMs are required to implement
+ // both of the above character sets.
+ throw new IllegalStateException(
+ "Java VM does not support a standard character set.", e);
+ }
+ }
+
+ /**
+ * Helper called by generated code to construct default values for bytes
+ * fields.
+ * <p>
+ * 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 byte[] bytesDefaultValue(String bytes) {
+ try {
+ return bytes.getBytes("ISO-8859-1");
+ } catch (UnsupportedEncodingException e) {
+ // This should never happen since all JVMs are required to implement
+ // ISO-8859-1.
+ throw new IllegalStateException(
+ "Java VM does not support a standard character set.", e);
+ }
+ }
+
+ /**
+ * Helper function to convert a string into UTF-8 while turning the
+ * UnsupportedEncodingException to a RuntimeException.
+ */
+ public static byte[] copyFromUtf8(final String text) {
+ try {
+ return text.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("UTF-8 not supported?");
+ }
+ }
+
+ /**
+ * Checks repeated int field equality; null-value and 0-length fields are
+ * considered equal.
+ */
+ public static boolean equals(int[] field1, int[] field2) {
+ if (field1 == null || field1.length == 0) {
+ return field2 == null || field2.length == 0;
+ } else {
+ return Arrays.equals(field1, field2);
+ }
+ }
+
+ /**
+ * Checks repeated long field equality; null-value and 0-length fields are
+ * considered equal.
+ */
+ public static boolean equals(long[] field1, long[] field2) {
+ if (field1 == null || field1.length == 0) {
+ return field2 == null || field2.length == 0;
+ } else {
+ return Arrays.equals(field1, field2);
+ }
+ }
+
+ /**
+ * Checks repeated float field equality; null-value and 0-length fields are
+ * considered equal.
+ */
+ public static boolean equals(float[] field1, float[] field2) {
+ if (field1 == null || field1.length == 0) {
+ return field2 == null || field2.length == 0;
+ } else {
+ return Arrays.equals(field1, field2);
+ }
+ }
+
+ /**
+ * Checks repeated double field equality; null-value and 0-length fields are
+ * considered equal.
+ */
+ public static boolean equals(double[] field1, double[] field2) {
+ if (field1 == null || field1.length == 0) {
+ return field2 == null || field2.length == 0;
+ } else {
+ return Arrays.equals(field1, field2);
+ }
+ }
+
+ /**
+ * Checks repeated boolean field equality; null-value and 0-length fields are
+ * considered equal.
+ */
+ public static boolean equals(boolean[] field1, boolean[] field2) {
+ if (field1 == null || field1.length == 0) {
+ return field2 == null || field2.length == 0;
+ } else {
+ return Arrays.equals(field1, field2);
+ }
+ }
+
+ /**
+ * Checks repeated bytes field equality. Only non-null elements are tested.
+ * Returns true if the two fields have the same sequence of non-null
+ * elements. Null-value fields and fields of any length with only null
+ * elements are considered equal.
+ */
+ public static boolean equals(byte[][] field1, byte[][] field2) {
+ int index1 = 0;
+ int length1 = field1 == null ? 0 : field1.length;
+ int index2 = 0;
+ int length2 = field2 == null ? 0 : field2.length;
+ while (true) {
+ while (index1 < length1 && field1[index1] == null) {
+ index1++;
+ }
+ while (index2 < length2 && field2[index2] == null) {
+ index2++;
+ }
+ boolean atEndOf1 = index1 >= length1;
+ boolean atEndOf2 = index2 >= length2;
+ if (atEndOf1 && atEndOf2) {
+ // no more non-null elements to test in both arrays
+ return true;
+ } else if (atEndOf1 != atEndOf2) {
+ // one of the arrays have extra non-null elements
+ return false;
+ } else if (!Arrays.equals(field1[index1], field2[index2])) {
+ // element mismatch
+ return false;
+ }
+ index1++;
+ index2++;
+ }
+ }
+
+ /**
+ * Checks repeated string/message field equality. Only non-null elements are
+ * tested. Returns true if the two fields have the same sequence of non-null
+ * elements. Null-value fields and fields of any length with only null
+ * elements are considered equal.
+ */
+ public static boolean equals(Object[] field1, Object[] field2) {
+ int index1 = 0;
+ int length1 = field1 == null ? 0 : field1.length;
+ int index2 = 0;
+ int length2 = field2 == null ? 0 : field2.length;
+ while (true) {
+ while (index1 < length1 && field1[index1] == null) {
+ index1++;
+ }
+ while (index2 < length2 && field2[index2] == null) {
+ index2++;
+ }
+ boolean atEndOf1 = index1 >= length1;
+ boolean atEndOf2 = index2 >= length2;
+ if (atEndOf1 && atEndOf2) {
+ // no more non-null elements to test in both arrays
+ return true;
+ } else if (atEndOf1 != atEndOf2) {
+ // one of the arrays have extra non-null elements
+ return false;
+ } else if (!field1[index1].equals(field2[index2])) {
+ // element mismatch
+ return false;
+ }
+ index1++;
+ index2++;
+ }
+ }
+
+ /**
+ * Computes the hash code of a repeated int field. Null-value and 0-length
+ * fields have the same hash code.
+ */
+ public static int hashCode(int[] field) {
+ return field == null || field.length == 0 ? 0 : Arrays.hashCode(field);
+ }
+
+ /**
+ * Computes the hash code of a repeated long field. Null-value and 0-length
+ * fields have the same hash code.
+ */
+ public static int hashCode(long[] field) {
+ return field == null || field.length == 0 ? 0 : Arrays.hashCode(field);
+ }
+
+ /**
+ * Computes the hash code of a repeated float field. Null-value and 0-length
+ * fields have the same hash code.
+ */
+ public static int hashCode(float[] field) {
+ return field == null || field.length == 0 ? 0 : Arrays.hashCode(field);
+ }
+
+ /**
+ * Computes the hash code of a repeated double field. Null-value and 0-length
+ * fields have the same hash code.
+ */
+ public static int hashCode(double[] field) {
+ return field == null || field.length == 0 ? 0 : Arrays.hashCode(field);
+ }
+
+ /**
+ * Computes the hash code of a repeated boolean field. Null-value and 0-length
+ * fields have the same hash code.
+ */
+ public static int hashCode(boolean[] field) {
+ return field == null || field.length == 0 ? 0 : Arrays.hashCode(field);
+ }
+
+ /**
+ * Computes the hash code of a repeated bytes field. Only the sequence of all
+ * non-null elements are used in the computation. Null-value fields and fields
+ * of any length with only null elements have the same hash code.
+ */
+ public static int hashCode(byte[][] field) {
+ int result = 0;
+ for (int i = 0, size = field == null ? 0 : field.length; i < size; i++) {
+ byte[] element = field[i];
+ if (element != null) {
+ result = 31 * result + Arrays.hashCode(element);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Computes the hash code of a repeated string/message field. Only the
+ * sequence of all non-null elements are used in the computation. Null-value
+ * fields and fields of any length with only null elements have the same hash
+ * code.
+ */
+ public static int hashCode(Object[] field) {
+ int result = 0;
+ for (int i = 0, size = field == null ? 0 : field.length; i < size; i++) {
+ Object element = field[i];
+ if (element != null) {
+ result = 31 * result + element.hashCode();
+ }
+ }
+ return result;
+ }
+
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java b/javanano/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java
new file mode 100644
index 00000000..3864d38a
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/InvalidProtocolBufferNanoException.java
@@ -0,0 +1,93 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+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.
+ *
+ * @author kenton@google.com Kenton Varda
+ */
+public class InvalidProtocolBufferNanoException extends IOException {
+ private static final long serialVersionUID = -1616151763072450476L;
+
+ public InvalidProtocolBufferNanoException(final String description) {
+ super(description);
+ }
+
+ static InvalidProtocolBufferNanoException truncatedMessage() {
+ return new InvalidProtocolBufferNanoException(
+ "While parsing a protocol message, the input ended unexpectedly " +
+ "in the middle of a field. This could mean either than the " +
+ "input has been truncated or that an embedded message " +
+ "misreported its own length.");
+ }
+
+ static InvalidProtocolBufferNanoException negativeSize() {
+ return new InvalidProtocolBufferNanoException(
+ "CodedInputStream encountered an embedded string or message " +
+ "which claimed to have negative size.");
+ }
+
+ static InvalidProtocolBufferNanoException malformedVarint() {
+ return new InvalidProtocolBufferNanoException(
+ "CodedInputStream encountered a malformed varint.");
+ }
+
+ static InvalidProtocolBufferNanoException invalidTag() {
+ return new InvalidProtocolBufferNanoException(
+ "Protocol message contained an invalid tag (zero).");
+ }
+
+ static InvalidProtocolBufferNanoException invalidEndTag() {
+ return new InvalidProtocolBufferNanoException(
+ "Protocol message end-group tag did not match expected tag.");
+ }
+
+ static InvalidProtocolBufferNanoException invalidWireType() {
+ return new InvalidProtocolBufferNanoException(
+ "Protocol message tag had invalid wire type.");
+ }
+
+ static InvalidProtocolBufferNanoException recursionLimitExceeded() {
+ return new InvalidProtocolBufferNanoException(
+ "Protocol message had too many levels of nesting. May be malicious. " +
+ "Use CodedInputStream.setRecursionLimit() to increase the depth limit.");
+ }
+
+ static InvalidProtocolBufferNanoException sizeLimitExceeded() {
+ return new InvalidProtocolBufferNanoException(
+ "Protocol message was too large. May be malicious. " +
+ "Use CodedInputStream.setSizeLimit() to increase the size limit.");
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/MessageNano.java b/javanano/src/main/java/com/google/protobuf/nano/MessageNano.java
new file mode 100644
index 00000000..81e58571
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/MessageNano.java
@@ -0,0 +1,190 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * Abstract interface implemented by Protocol Message objects.
+ *
+ * @author wink@google.com Wink Saville
+ */
+public abstract class MessageNano {
+ protected volatile int cachedSize = -1;
+
+ /**
+ * Get the number of bytes required to encode this message.
+ * Returns the cached size or calls getSerializedSize which
+ * sets the cached size. This is used internally when serializing
+ * so the size is only computed once. If a member is modified
+ * then this could be stale call getSerializedSize if in doubt.
+ */
+ public int getCachedSize() {
+ if (cachedSize < 0) {
+ // getSerializedSize sets cachedSize
+ getSerializedSize();
+ }
+ return cachedSize;
+ }
+
+ /**
+ * Computes the number of bytes required to encode this message.
+ * The size is cached and the cached result can be retrieved
+ * using getCachedSize().
+ */
+ public int getSerializedSize() {
+ int size = computeSerializedSize();
+ cachedSize = size;
+ return size;
+ }
+
+ /**
+ * Computes the number of bytes required to encode this message. This does not update the
+ * cached size.
+ */
+ protected int computeSerializedSize() {
+ // This is overridden if the generated message has serialized fields.
+ return 0;
+ }
+
+ /**
+ * Serializes the message and writes it to {@code output}.
+ *
+ * @param output the output to receive the serialized form.
+ * @throws IOException if an error occurred writing to {@code output}.
+ */
+ public void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ // Does nothing by default. Overridden by subclasses which have data to write.
+ }
+
+ /**
+ * Parse {@code input} as a message of this type and merge it with the
+ * message being built.
+ */
+ public abstract MessageNano mergeFrom(CodedInputByteBufferNano input) throws IOException;
+
+ /**
+ * Serialize to a byte array.
+ * @return byte array with the serialized data.
+ */
+ public static final byte[] toByteArray(MessageNano msg) {
+ final byte[] result = new byte[msg.getSerializedSize()];
+ toByteArray(msg, result, 0, result.length);
+ return result;
+ }
+
+ /**
+ * Serialize to a byte array starting at offset through length. The
+ * method getSerializedSize must have been called prior to calling
+ * this method so the proper length is know. If an attempt to
+ * write more than length bytes OutOfSpaceException will be thrown
+ * and if length bytes are not written then IllegalStateException
+ * is thrown.
+ */
+ public static final void toByteArray(MessageNano msg, byte[] data, int offset, int length) {
+ try {
+ final CodedOutputByteBufferNano output =
+ CodedOutputByteBufferNano.newInstance(data, offset, length);
+ msg.writeTo(output);
+ output.checkNoSpaceLeft();
+ } catch (IOException e) {
+ throw new RuntimeException("Serializing to a byte array threw an IOException "
+ + "(should never happen).", e);
+ }
+ }
+
+ /**
+ * Parse {@code data} as a message of this type and merge it with the
+ * message being built.
+ */
+ public static final <T extends MessageNano> T mergeFrom(T msg, final byte[] data)
+ throws InvalidProtocolBufferNanoException {
+ return mergeFrom(msg, data, 0, data.length);
+ }
+
+ /**
+ * Parse {@code data} as a message of this type and merge it with the
+ * message being built.
+ */
+ public static final <T extends MessageNano> T mergeFrom(T msg, final byte[] data,
+ final int off, final int len) throws InvalidProtocolBufferNanoException {
+ try {
+ final CodedInputByteBufferNano input =
+ CodedInputByteBufferNano.newInstance(data, off, len);
+ msg.mergeFrom(input);
+ input.checkLastTagWas(0);
+ return msg;
+ } catch (InvalidProtocolBufferNanoException e) {
+ throw e;
+ } catch (IOException e) {
+ throw new RuntimeException("Reading from a byte array threw an IOException (should "
+ + "never happen).");
+ }
+ }
+
+ /**
+ * Compares two {@code MessageNano}s and returns true if the message's are the same class and
+ * have serialized form equality (i.e. all of the field values are the same).
+ */
+ public static final boolean messageNanoEquals(MessageNano a, MessageNano b) {
+ if (a == b) {
+ return true;
+ }
+ if (a == null || b == null) {
+ return false;
+ }
+ if (a.getClass() != b.getClass()) {
+ return false;
+ }
+ final int serializedSize = a.getSerializedSize();
+ if (b.getSerializedSize() != serializedSize) {
+ return false;
+ }
+ final byte[] aByteArray = new byte[serializedSize];
+ final byte[] bByteArray = new byte[serializedSize];
+ toByteArray(a, aByteArray, 0, serializedSize);
+ toByteArray(b, bByteArray, 0, serializedSize);
+ return Arrays.equals(aByteArray, bByteArray);
+ }
+
+ /**
+ * Returns a string that is (mostly) compatible with ProtoBuffer's TextFormat. Note that groups
+ * (which are deprecated) are not serialized with the correct field name.
+ *
+ * <p>This is implemented using reflection, so it is not especially fast nor is it guaranteed
+ * to find all fields if you have method removal turned on for proguard.
+ */
+ @Override
+ public String toString() {
+ return MessageNanoPrinter.print(this);
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java b/javanano/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java
new file mode 100644
index 00000000..dd43cdbb
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/MessageNanoPrinter.java
@@ -0,0 +1,257 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * Static helper methods for printing nano protos.
+ *
+ * @author flynn@google.com Andrew Flynn
+ */
+public final class MessageNanoPrinter {
+ // Do not allow instantiation
+ private MessageNanoPrinter() {}
+
+ private static final String INDENT = " ";
+ private static final int MAX_STRING_LEN = 200;
+
+ /**
+ * Returns an text representation of a MessageNano suitable for debugging. The returned string
+ * is mostly compatible with Protocol Buffer's TextFormat (as provided by non-nano protocol
+ * buffers) -- groups (which are deprecated) are output with an underscore name (e.g. foo_bar
+ * instead of FooBar) and will thus not parse.
+ *
+ * <p>Employs Java reflection on the given object and recursively prints primitive fields,
+ * groups, and messages.</p>
+ */
+ public static <T extends MessageNano> String print(T message) {
+ if (message == null) {
+ return "";
+ }
+
+ StringBuffer buf = new StringBuffer();
+ try {
+ print(null, message, new StringBuffer(), buf);
+ } catch (IllegalAccessException e) {
+ return "Error printing proto: " + e.getMessage();
+ } catch (InvocationTargetException e) {
+ return "Error printing proto: " + e.getMessage();
+ }
+ return buf.toString();
+ }
+
+ /**
+ * Function that will print the given message/field into the StringBuffer.
+ * Meant to be called recursively.
+ *
+ * @param identifier the identifier to use, or {@code null} if this is the root message to
+ * print.
+ * @param object the value to print. May in fact be a primitive value or byte array and not a
+ * message.
+ * @param indentBuf the indentation each line should begin with.
+ * @param buf the output buffer.
+ */
+ private static void print(String identifier, Object object,
+ StringBuffer indentBuf, StringBuffer buf) throws IllegalAccessException,
+ InvocationTargetException {
+ if (object == null) {
+ // This can happen if...
+ // - we're about to print a message, String, or byte[], but it not present;
+ // - we're about to print a primitive, but "reftype" optional style is enabled, and
+ // the field is unset.
+ // In both cases the appropriate behavior is to output nothing.
+ } else if (object instanceof MessageNano) { // Nano proto message
+ int origIndentBufLength = indentBuf.length();
+ if (identifier != null) {
+ buf.append(indentBuf).append(deCamelCaseify(identifier)).append(" <\n");
+ indentBuf.append(INDENT);
+ }
+ Class<?> clazz = object.getClass();
+
+ // Proto fields follow one of two formats:
+ //
+ // 1) Public, non-static variables that do not begin or end with '_'
+ // Find and print these using declared public fields
+ for (Field field : clazz.getFields()) {
+ int modifiers = field.getModifiers();
+ String fieldName = field.getName();
+
+ if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC
+ && (modifiers & Modifier.STATIC) != Modifier.STATIC
+ && !fieldName.startsWith("_")
+ && !fieldName.endsWith("_")) {
+ Class<?> fieldType = field.getType();
+ Object value = field.get(object);
+
+ if (fieldType.isArray()) {
+ Class<?> arrayType = fieldType.getComponentType();
+
+ // bytes is special since it's not repeated, but is represented by an array
+ if (arrayType == byte.class) {
+ print(fieldName, value, indentBuf, buf);
+ } else {
+ int len = value == null ? 0 : Array.getLength(value);
+ for (int i = 0; i < len; i++) {
+ Object elem = Array.get(value, i);
+ print(fieldName, elem, indentBuf, buf);
+ }
+ }
+ } else {
+ print(fieldName, value, indentBuf, buf);
+ }
+ }
+ }
+
+ // 2) Fields that are accessed via getter methods (when accessors
+ // mode is turned on)
+ // Find and print these using getter methods.
+ for (Method method : clazz.getMethods()) {
+ String name = method.getName();
+ // Check for the setter accessor method since getters and hazzers both have
+ // non-proto-field name collisions (hashCode() and getSerializedSize())
+ if (name.startsWith("set")) {
+ String subfieldName = name.substring(3);
+
+ Method hazzer = null;
+ try {
+ hazzer = clazz.getMethod("has" + subfieldName);
+ } catch (NoSuchMethodException e) {
+ continue;
+ }
+ // If hazzer does't exist or returns false, no need to continue
+ if (!(Boolean) hazzer.invoke(object)) {
+ continue;
+ }
+
+ Method getter = null;
+ try {
+ getter = clazz.getMethod("get" + subfieldName);
+ } catch (NoSuchMethodException e) {
+ continue;
+ }
+
+ print(subfieldName, getter.invoke(object), indentBuf, buf);
+ }
+ }
+ if (identifier != null) {
+ indentBuf.setLength(origIndentBufLength);
+ buf.append(indentBuf).append(">\n");
+ }
+ } else {
+ // Non-null primitive value
+ identifier = deCamelCaseify(identifier);
+ buf.append(indentBuf).append(identifier).append(": ");
+ if (object instanceof String) {
+ String stringMessage = sanitizeString((String) object);
+ buf.append("\"").append(stringMessage).append("\"");
+ } else if (object instanceof byte[]) {
+ appendQuotedBytes((byte[]) object, buf);
+ } else {
+ buf.append(object);
+ }
+ buf.append("\n");
+ }
+ }
+
+ /**
+ * Converts an identifier of the format "FieldName" into "field_name".
+ */
+ private static String deCamelCaseify(String identifier) {
+ StringBuffer out = new StringBuffer();
+ for (int i = 0; i < identifier.length(); i++) {
+ char currentChar = identifier.charAt(i);
+ if (i == 0) {
+ out.append(Character.toLowerCase(currentChar));
+ } else if (Character.isUpperCase(currentChar)) {
+ out.append('_').append(Character.toLowerCase(currentChar));
+ } else {
+ out.append(currentChar);
+ }
+ }
+ return out.toString();
+ }
+
+ /**
+ * Shortens and escapes the given string.
+ */
+ private static String sanitizeString(String str) {
+ if (!str.startsWith("http") && str.length() > MAX_STRING_LEN) {
+ // Trim non-URL strings.
+ str = str.substring(0, MAX_STRING_LEN) + "[...]";
+ }
+ return escapeString(str);
+ }
+
+ /**
+ * Escape everything except for low ASCII code points.
+ */
+ private static String escapeString(String str) {
+ int strLen = str.length();
+ StringBuilder b = new StringBuilder(strLen);
+ for (int i = 0; i < strLen; i++) {
+ char original = str.charAt(i);
+ if (original >= ' ' && original <= '~' && original != '"' && original != '\'') {
+ b.append(original);
+ } else {
+ b.append(String.format("\\u%04x", (int) original));
+ }
+ }
+ return b.toString();
+ }
+
+ /**
+ * Appends a quoted byte array to the provided {@code StringBuffer}.
+ */
+ private static void appendQuotedBytes(byte[] bytes, StringBuffer builder) {
+ if (bytes == null) {
+ builder.append("\"\"");
+ return;
+ }
+
+ builder.append('"');
+ for (int i = 0; i < bytes.length; ++i) {
+ int ch = bytes[i] & 0xff;
+ if (ch == '\\' || ch == '"') {
+ builder.append('\\').append((char) ch);
+ } else if (ch >= 32 && ch < 127) {
+ builder.append((char) ch);
+ } else {
+ builder.append(String.format("\\%03o", ch));
+ }
+ }
+ builder.append('"');
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/UnknownFieldData.java b/javanano/src/main/java/com/google/protobuf/nano/UnknownFieldData.java
new file mode 100644
index 00000000..a17fccf3
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/UnknownFieldData.java
@@ -0,0 +1,84 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * Stores unknown fields. These might be extensions or fields that the generated
+ * API doesn't know about yet.
+ *
+ * @author bduff@google.com (Brian Duff)
+ */
+final class UnknownFieldData {
+
+ final int tag;
+ final byte[] bytes;
+
+ UnknownFieldData(int tag, byte[] bytes) {
+ this.tag = tag;
+ this.bytes = bytes;
+ }
+
+ int computeSerializedSize() {
+ int size = 0;
+ size += CodedOutputByteBufferNano.computeRawVarint32Size(tag);
+ size += bytes.length;
+ return size;
+ }
+
+ void writeTo(CodedOutputByteBufferNano output) throws IOException {
+ output.writeRawVarint32(tag);
+ output.writeRawBytes(bytes);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof UnknownFieldData)) {
+ return false;
+ }
+
+ UnknownFieldData other = (UnknownFieldData) o;
+ return tag == other.tag && Arrays.equals(bytes, other.bytes);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = 17;
+ result = 31 * result + tag;
+ result = 31 * result + Arrays.hashCode(bytes);
+ return result;
+ }
+}
diff --git a/javanano/src/main/java/com/google/protobuf/nano/WireFormatNano.java b/javanano/src/main/java/com/google/protobuf/nano/WireFormatNano.java
new file mode 100644
index 00000000..bbb6370a
--- /dev/null
+++ b/javanano/src/main/java/com/google/protobuf/nano/WireFormatNano.java
@@ -0,0 +1,124 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+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 contains constants and helper functions useful for dealing with
+ * the Protocol Buffer wire format.
+ *
+ * @author kenton@google.com Kenton Varda
+ */
+public final class WireFormatNano {
+ // Do not allow instantiation.
+ private WireFormatNano() {}
+
+ static final int WIRETYPE_VARINT = 0;
+ static final int WIRETYPE_FIXED64 = 1;
+ static final int WIRETYPE_LENGTH_DELIMITED = 2;
+ static final int WIRETYPE_START_GROUP = 3;
+ static final int WIRETYPE_END_GROUP = 4;
+ static final int WIRETYPE_FIXED32 = 5;
+
+ static final int TAG_TYPE_BITS = 3;
+ static final int TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1;
+
+ /** Given a tag value, determines the wire type (the lower 3 bits). */
+ static int getTagWireType(final int tag) {
+ return tag & TAG_TYPE_MASK;
+ }
+
+ /** Given a tag value, determines the field number (the upper 29 bits). */
+ public static int getTagFieldNumber(final int tag) {
+ return tag >>> TAG_TYPE_BITS;
+ }
+
+ /** Makes a tag value given a field number and wire type. */
+ static int makeTag(final int fieldNumber, final int wireType) {
+ return (fieldNumber << TAG_TYPE_BITS) | wireType;
+ }
+
+ public static final int EMPTY_INT_ARRAY[] = {};
+ public static final long EMPTY_LONG_ARRAY[] = {};
+ public static final float EMPTY_FLOAT_ARRAY[] = {};
+ public static final double EMPTY_DOUBLE_ARRAY[] = {};
+ public static final boolean EMPTY_BOOLEAN_ARRAY[] = {};
+ public static final String EMPTY_STRING_ARRAY[] = {};
+ public static final byte[] EMPTY_BYTES_ARRAY[] = {};
+ public static final byte[] EMPTY_BYTES = {};
+
+ /**
+ * Parses an unknown field. This implementation skips the field.
+ *
+ * <p>Generated messages will call this for unknown fields if the store_unknown_fields
+ * option is off.
+ *
+ * @return {@literal true} unless the tag is an end-group tag.
+ */
+ public static boolean parseUnknownField(
+ final CodedInputByteBufferNano input,
+ final int tag) throws IOException {
+ return input.skipField(tag);
+ }
+
+ /**
+ * Computes the array length of a repeated field. We assume that in the common case repeated
+ * fields are contiguously serialized but we still correctly handle interspersed values of a
+ * repeated field (but with extra allocations).
+ *
+ * Rewinds to current input position before returning.
+ *
+ * @param input stream input, pointing to the byte after the first tag
+ * @param tag repeated field tag just read
+ * @return length of array
+ * @throws IOException
+ */
+ public static final int getRepeatedFieldArrayLength(
+ final CodedInputByteBufferNano input,
+ final int tag) throws IOException {
+ int arrayLength = 1;
+ int startPos = input.getPosition();
+ input.skipField(tag);
+ while (input.readTag() == tag) {
+ input.skipField(tag);
+ arrayLength++;
+ }
+ input.rewindToPosition(startPos);
+ return arrayLength;
+ }
+
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
new file mode 100644
index 00000000..442f0b74
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
@@ -0,0 +1,3797 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2013 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.nano;
+
+import com.google.protobuf.nano.CodedInputByteBufferNano;
+import com.google.protobuf.nano.EnumClassNanoMultiple;
+import com.google.protobuf.nano.EnumClassNanos;
+import com.google.protobuf.nano.EnumValidity;
+import com.google.protobuf.nano.EnumValidityAccessors;
+import com.google.protobuf.nano.FileScopeEnumMultiple;
+import com.google.protobuf.nano.FileScopeEnumRefNano;
+import com.google.protobuf.nano.InternalNano;
+import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
+import com.google.protobuf.nano.MessageNano;
+import com.google.protobuf.nano.MessageScopeEnumRefNano;
+import com.google.protobuf.nano.MultipleImportingNonMultipleNano1;
+import com.google.protobuf.nano.MultipleImportingNonMultipleNano2;
+import com.google.protobuf.nano.MultipleNameClashNano;
+import com.google.protobuf.nano.NanoAccessorsOuterClass.TestNanoAccessors;
+import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas;
+import com.google.protobuf.nano.NanoOuterClass;
+import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
+import com.google.protobuf.nano.NanoReferenceTypes;
+import com.google.protobuf.nano.NanoRepeatedPackables;
+import com.google.protobuf.nano.PackedExtensions;
+import com.google.protobuf.nano.RepeatedExtensions;
+import com.google.protobuf.nano.SingularExtensions;
+import com.google.protobuf.nano.TestRepeatedMergeNano;
+import com.google.protobuf.nano.UnittestMultipleNano;
+import com.google.protobuf.nano.UnittestRecursiveNano.RecursiveMessageNano;
+import com.google.protobuf.nano.UnittestSimpleNano.SimpleMessageNano;
+import com.google.protobuf.nano.UnittestSingleNano.SingleMessageNano;
+import com.google.protobuf.nano.testext.Extensions;
+import com.google.protobuf.nano.testext.Extensions.AnotherMessage;
+import com.google.protobuf.nano.testext.Extensions.MessageWithGroup;
+import com.google.protobuf.nano.testimport.UnittestImportNano;
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+import java.util.HashMap;
+
+/**
+ * Test nano runtime.
+ *
+ * @author ulas@google.com Ulas Kirazci
+ */
+public class NanoTest extends TestCase {
+ @Override
+ public void setUp() throws Exception {
+ }
+
+ public void testSimpleMessageNano() throws Exception {
+ SimpleMessageNano msg = new SimpleMessageNano();
+ assertEquals(123, msg.d);
+ assertEquals(null, msg.nestedMsg);
+ assertEquals(SimpleMessageNano.BAZ, msg.defaultNestedEnum);
+
+ msg.d = 456;
+ assertEquals(456, msg.d);
+
+ SimpleMessageNano.NestedMessage nestedMsg = new SimpleMessageNano.NestedMessage();
+ nestedMsg.bb = 2;
+ assertEquals(2, nestedMsg.bb);
+ msg.nestedMsg = nestedMsg;
+ assertEquals(2, msg.nestedMsg.bb);
+
+ msg.defaultNestedEnum = SimpleMessageNano.BAR;
+ assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ SimpleMessageNano newMsg = SimpleMessageNano.parseFrom(result);
+ assertEquals(456, newMsg.d);
+ assertEquals(2, msg.nestedMsg.bb);
+ assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum);
+
+ msg.nestedMsg = null;
+ assertTrue(msgSerializedSize != msg.getSerializedSize());
+
+ msg.clear();
+ assertEquals(0, msg.getSerializedSize());
+ }
+
+ public void testRecursiveMessageNano() throws Exception {
+ RecursiveMessageNano msg = new RecursiveMessageNano();
+ assertTrue(msg.repeatedRecursiveMessageNano.length == 0);
+
+ RecursiveMessageNano msg1 = new RecursiveMessageNano();
+ msg1.id = 1;
+ assertEquals(1, msg1.id);
+ RecursiveMessageNano msg2 = new RecursiveMessageNano();
+ msg2.id = 2;
+ RecursiveMessageNano msg3 = new RecursiveMessageNano();
+ msg3.id = 3;
+
+ RecursiveMessageNano.NestedMessage nestedMsg = new RecursiveMessageNano.NestedMessage();
+ nestedMsg.a = msg1;
+ assertEquals(1, nestedMsg.a.id);
+
+ msg.id = 0;
+ msg.nestedMessage = nestedMsg;
+ msg.optionalRecursiveMessageNano = msg2;
+ msg.repeatedRecursiveMessageNano = new RecursiveMessageNano[] { msg3 };
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 16);
+ assertEquals(result.length, msgSerializedSize);
+
+ RecursiveMessageNano newMsg = RecursiveMessageNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedRecursiveMessageNano.length);
+
+ assertEquals(0, newMsg.id);
+ assertEquals(1, newMsg.nestedMessage.a.id);
+ assertEquals(2, newMsg.optionalRecursiveMessageNano.id);
+ assertEquals(3, newMsg.repeatedRecursiveMessageNano[0].id);
+ }
+
+ public void testMessageNoFields() {
+ SingleMessageNano msg = new SingleMessageNano();
+ assertEquals(0, msg.getSerializedSize());
+ assertEquals(0, MessageNano.toByteArray(msg).length);
+ }
+
+ public void testNanoRequiredInt32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.id = 123;
+ assertEquals(123, msg.id);
+ msg.clear().id = 456;
+ assertEquals(456, msg.id);
+ msg.clear();
+
+ msg.id = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 3);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.id);
+ }
+
+ public void testNanoOptionalInt32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalInt32 = 123;
+ assertEquals(123, msg.optionalInt32);
+ msg.clear()
+ .optionalInt32 = 456;
+ assertEquals(456, msg.optionalInt32);
+ msg.clear();
+
+ msg.optionalInt32 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 5);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalInt32);
+ }
+
+ public void testNanoOptionalInt64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalInt64 = 123;
+ assertEquals(123, msg.optionalInt64);
+ msg.clear()
+ .optionalInt64 = 456;
+ assertEquals(456, msg.optionalInt64);
+ msg.clear();
+ assertEquals(0, msg.optionalInt64);
+
+ msg.optionalInt64 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 5);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalInt64);
+ }
+
+ public void testNanoOptionalUint32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalUint32 = 123;
+ assertEquals(123, msg.optionalUint32);
+ msg.clear()
+ .optionalUint32 = 456;
+ assertEquals(456, msg.optionalUint32);
+ msg.clear();
+ assertEquals(0, msg.optionalUint32);
+
+ msg.optionalUint32 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 5);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalUint32);
+ }
+
+ public void testNanoOptionalUint64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalUint64 = 123;
+ assertEquals(123, msg.optionalUint64);
+ msg.clear()
+ .optionalUint64 = 456;
+ assertEquals(456, msg.optionalUint64);
+ msg.clear();
+ assertEquals(0, msg.optionalUint64);
+
+ msg.optionalUint64 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 5);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalUint64);
+ }
+
+ public void testNanoOptionalSint32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalSint32 = 123;
+ assertEquals(123, msg.optionalSint32);
+ msg.clear()
+ .optionalSint32 = 456;
+ assertEquals(456, msg.optionalSint32);
+ msg.clear();
+ assertEquals(0, msg.optionalSint32);
+
+ msg.optionalSint32 = -123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(-123, newMsg.optionalSint32);
+ }
+
+ public void testNanoOptionalSint64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalSint64 = 123;
+ assertEquals(123, msg.optionalSint64);
+ msg.clear()
+ .optionalSint64 = 456;
+ assertEquals(456, msg.optionalSint64);
+ msg.clear();
+ assertEquals(0, msg.optionalSint64);
+
+ msg.optionalSint64 = -123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(-123, newMsg.optionalSint64);
+ }
+
+ public void testNanoOptionalFixed32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalFixed32 = 123;
+ assertEquals(123, msg.optionalFixed32);
+ msg.clear()
+ .optionalFixed32 = 456;
+ assertEquals(456, msg.optionalFixed32);
+ msg.clear();
+ assertEquals(0, msg.optionalFixed32);
+
+ msg.optionalFixed32 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalFixed32);
+ }
+
+ public void testNanoOptionalFixed64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalFixed64 = 123;
+ assertEquals(123, msg.optionalFixed64);
+ msg.clear()
+ .optionalFixed64 = 456;
+ assertEquals(456, msg.optionalFixed64);
+ msg.clear();
+ assertEquals(0, msg.optionalFixed64);
+
+ msg.optionalFixed64 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 12);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalFixed64);
+ }
+
+ public void testNanoOptionalSfixed32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalSfixed32 = 123;
+ assertEquals(123, msg.optionalSfixed32);
+ msg.clear()
+ .optionalSfixed32 = 456;
+ assertEquals(456, msg.optionalSfixed32);
+ msg.clear();
+ assertEquals(0, msg.optionalSfixed32);
+
+ msg.optionalSfixed32 = 123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(123, newMsg.optionalSfixed32);
+ }
+
+ public void testNanoOptionalSfixed64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalSfixed64 = 123;
+ assertEquals(123, msg.optionalSfixed64);
+ msg.clear()
+ .optionalSfixed64 = 456;
+ assertEquals(456, msg.optionalSfixed64);
+ msg.clear();
+ assertEquals(0, msg.optionalSfixed64);
+
+ msg.optionalSfixed64 = -123;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 12);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(-123, newMsg.optionalSfixed64);
+ }
+
+ public void testNanoOptionalFloat() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalFloat = 123f;
+ assertTrue(123.0f == msg.optionalFloat);
+ msg.clear()
+ .optionalFloat = 456.0f;
+ assertTrue(456.0f == msg.optionalFloat);
+ msg.clear();
+ assertTrue(0.0f == msg.optionalFloat);
+
+ msg.optionalFloat = -123.456f;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(-123.456f == newMsg.optionalFloat);
+ }
+
+ public void testNanoOptionalDouble() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalDouble = 123;
+ assertTrue(123.0 == msg.optionalDouble);
+ msg.clear()
+ .optionalDouble = 456.0;
+ assertTrue(456.0 == msg.optionalDouble);
+ msg.clear();
+ assertTrue(0.0 == msg.optionalDouble);
+
+ msg.optionalDouble = -123.456;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 12);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(-123.456 == newMsg.optionalDouble);
+ }
+
+ public void testNanoOptionalBool() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalBool = true;
+ assertTrue(msg.optionalBool);
+ msg.clear()
+ .optionalBool = true;
+ assertTrue(msg.optionalBool);
+ msg.clear();
+ assertFalse(msg.optionalBool);
+
+ msg.optionalBool = true;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 5);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalBool);
+ }
+
+ public void testNanoOptionalString() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalString = "hello";
+ assertEquals("hello", msg.optionalString);
+ msg.clear();
+ assertTrue(msg.optionalString.isEmpty());
+ msg.clear()
+ .optionalString = "hello2";
+ assertEquals("hello2", msg.optionalString);
+ msg.clear();
+ assertTrue(msg.optionalString.isEmpty());
+
+ msg.optionalString = "bye";
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalString != null);
+ assertEquals("bye", newMsg.optionalString);
+ }
+
+ public void testNanoOptionalBytes() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertFalse(msg.optionalBytes.length > 0);
+ msg.optionalBytes = InternalNano.copyFromUtf8("hello");
+ assertTrue(msg.optionalBytes.length > 0);
+ assertEquals("hello", new String(msg.optionalBytes, "UTF-8"));
+ msg.clear();
+ assertFalse(msg.optionalBytes.length > 0);
+ msg.clear()
+ .optionalBytes = InternalNano.copyFromUtf8("hello");
+ assertTrue(msg.optionalBytes.length > 0);
+ msg.clear();
+ assertFalse(msg.optionalBytes.length > 0);
+
+ msg.optionalBytes = InternalNano.copyFromUtf8("bye");
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalBytes.length > 0);
+ assertEquals("bye", new String(newMsg.optionalBytes, "UTF-8"));
+ }
+
+ public void testNanoOptionalGroup() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ TestAllTypesNano.OptionalGroup grp = new TestAllTypesNano.OptionalGroup();
+ grp.a = 1;
+ assertFalse(msg.optionalGroup != null);
+ msg.optionalGroup = grp;
+ assertTrue(msg.optionalGroup != null);
+ assertEquals(1, msg.optionalGroup.a);
+ msg.clear();
+ assertFalse(msg.optionalGroup != null);
+ msg.clear()
+ .optionalGroup = new TestAllTypesNano.OptionalGroup();
+ msg.optionalGroup.a = 2;
+ assertTrue(msg.optionalGroup != null);
+ msg.clear();
+ assertFalse(msg.optionalGroup != null);
+
+ msg.optionalGroup = grp;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalGroup != null);
+ assertEquals(1, newMsg.optionalGroup.a);
+ }
+
+ public void testNanoOptionalGroupWithUnknownFieldsEnabled() throws Exception {
+ MessageWithGroup msg = new MessageWithGroup();
+ MessageWithGroup.Group grp = new MessageWithGroup.Group();
+ grp.a = 1;
+ msg.group = grp;
+ byte [] serialized = MessageNano.toByteArray(msg);
+
+ MessageWithGroup parsed = MessageWithGroup.parseFrom(serialized);
+ assertEquals(1, parsed.group.a);
+
+ byte [] serialized2 = MessageNano.toByteArray(parsed);
+ assertEquals(serialized.length, serialized2.length);
+ MessageWithGroup parsed2 = MessageWithGroup.parseFrom(serialized2);
+ assertEquals(1, parsed2.group.a);
+ }
+
+ public void testNanoOptionalNestedMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ TestAllTypesNano.NestedMessage nestedMsg = new TestAllTypesNano.NestedMessage();
+ nestedMsg.bb = 1;
+ assertFalse(msg.optionalNestedMessage != null);
+ msg.optionalNestedMessage = nestedMsg;
+ assertTrue(msg.optionalNestedMessage != null);
+ assertEquals(1, msg.optionalNestedMessage.bb);
+ msg.clear();
+ assertFalse(msg.optionalNestedMessage != null);
+ msg.clear()
+ .optionalNestedMessage = new TestAllTypesNano.NestedMessage();
+ msg.optionalNestedMessage.bb = 2;
+ assertTrue(msg.optionalNestedMessage != null);
+ msg.clear();
+ assertFalse(msg.optionalNestedMessage != null);
+
+ msg.optionalNestedMessage = nestedMsg;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalNestedMessage != null);
+ assertEquals(1, newMsg.optionalNestedMessage.bb);
+ }
+
+ public void testNanoOptionalForeignMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ NanoOuterClass.ForeignMessageNano nestedMsg = new NanoOuterClass.ForeignMessageNano();
+ nestedMsg.c = 1;
+ assertFalse(msg.optionalForeignMessage != null);
+ msg.optionalForeignMessage = nestedMsg;
+ assertTrue(msg.optionalForeignMessage != null);
+ assertEquals(1, msg.optionalForeignMessage.c);
+ msg.clear();
+ assertFalse(msg.optionalForeignMessage != null);
+ msg.clear()
+ .optionalForeignMessage = new NanoOuterClass.ForeignMessageNano();
+ msg.optionalForeignMessage.c = 2;
+ assertTrue(msg.optionalForeignMessage != null);
+ msg.clear();
+ assertFalse(msg.optionalForeignMessage != null);
+
+ msg.optionalForeignMessage = nestedMsg;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalForeignMessage != null);
+ assertEquals(1, newMsg.optionalForeignMessage.c);
+ }
+
+ public void testNanoOptionalImportMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ UnittestImportNano.ImportMessageNano nestedMsg = new UnittestImportNano.ImportMessageNano();
+ nestedMsg.d = 1;
+ assertFalse(msg.optionalImportMessage != null);
+ msg.optionalImportMessage = nestedMsg;
+ assertTrue(msg.optionalImportMessage != null);
+ assertEquals(1, msg.optionalImportMessage.d);
+ msg.clear();
+ assertFalse(msg.optionalImportMessage != null);
+ msg.clear()
+ .optionalImportMessage = new UnittestImportNano.ImportMessageNano();
+ msg.optionalImportMessage.d = 2;
+ assertTrue(msg.optionalImportMessage != null);
+ msg.clear();
+ assertFalse(msg.optionalImportMessage != null);
+
+ msg.optionalImportMessage = nestedMsg;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalImportMessage != null);
+ assertEquals(1, newMsg.optionalImportMessage.d);
+ }
+
+ public void testNanoOptionalNestedEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalNestedEnum = TestAllTypesNano.BAR;
+ assertEquals(TestAllTypesNano.BAR, msg.optionalNestedEnum);
+ msg.clear()
+ .optionalNestedEnum = TestAllTypesNano.BAZ;
+ assertEquals(TestAllTypesNano.BAZ, msg.optionalNestedEnum);
+ msg.clear();
+ assertEquals(TestAllTypesNano.FOO, msg.optionalNestedEnum);
+
+ msg.optionalNestedEnum = TestAllTypesNano.BAR;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(TestAllTypesNano.BAR, newMsg.optionalNestedEnum);
+ }
+
+ public void testNanoOptionalForeignEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR;
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.optionalForeignEnum);
+ msg.clear()
+ .optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAZ;
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.optionalForeignEnum);
+ msg.clear();
+ assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.optionalForeignEnum);
+
+ msg.optionalForeignEnum = NanoOuterClass.FOREIGN_NANO_BAR;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, newMsg.optionalForeignEnum);
+ }
+
+ public void testNanoOptionalImportEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR;
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.optionalImportEnum);
+ msg.clear()
+ .optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAZ;
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.optionalImportEnum);
+ msg.clear();
+ assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.optionalImportEnum);
+
+ msg.optionalImportEnum = UnittestImportNano.IMPORT_NANO_BAR;
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, newMsg.optionalImportEnum);
+ }
+
+ public void testNanoOptionalStringPiece() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalStringPiece = "hello";
+ assertEquals("hello", msg.optionalStringPiece);
+ msg.clear();
+ assertTrue(msg.optionalStringPiece.isEmpty());
+ msg.clear()
+ .optionalStringPiece = "hello2";
+ assertEquals("hello2", msg.optionalStringPiece);
+ msg.clear();
+ assertTrue(msg.optionalStringPiece.isEmpty());
+
+ msg.optionalStringPiece = "bye";
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalStringPiece != null);
+ assertEquals("bye", newMsg.optionalStringPiece);
+ }
+
+ public void testNanoOptionalCord() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalCord = "hello";
+ assertEquals("hello", msg.optionalCord);
+ msg.clear();
+ assertTrue(msg.optionalCord.isEmpty());
+ msg.clear()
+ .optionalCord = "hello2";
+ assertEquals("hello2", msg.optionalCord);
+ msg.clear();
+ assertTrue(msg.optionalCord.isEmpty());
+
+ msg.optionalCord = "bye";
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertTrue(newMsg.optionalCord != null);
+ assertEquals("bye", newMsg.optionalCord);
+ }
+
+ public void testNanoRepeatedInt32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedInt32.length);
+ msg.repeatedInt32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedInt32[1]);
+ assertEquals(456, msg.repeatedInt32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedInt32.length);
+ msg.clear()
+ .repeatedInt32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedInt32.length);
+ assertEquals(456, msg.repeatedInt32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedInt32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedInt32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedInt32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedInt32.length);
+ assertEquals(123, newMsg.repeatedInt32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedInt32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedInt32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedInt32.length);
+ assertEquals(123, newMsg.repeatedInt32[0]);
+ assertEquals(456, newMsg.repeatedInt32[1]);
+ }
+
+ public void testNanoRepeatedInt64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedInt64.length);
+ msg.repeatedInt64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedInt64[1]);
+ assertEquals(456, msg.repeatedInt64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedInt64.length);
+ msg.clear()
+ .repeatedInt64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedInt64.length);
+ assertEquals(456, msg.repeatedInt64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedInt64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedInt64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedInt64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedInt64.length);
+ assertEquals(123, newMsg.repeatedInt64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedInt64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedInt64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedInt64.length);
+ assertEquals(123, newMsg.repeatedInt64[0]);
+ assertEquals(456, newMsg.repeatedInt64[1]);
+ }
+
+ public void testNanoRepeatedUint32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedUint32.length);
+ msg.repeatedUint32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedUint32[1]);
+ assertEquals(456, msg.repeatedUint32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedUint32.length);
+ msg.clear()
+ .repeatedUint32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedUint32.length);
+ assertEquals(456, msg.repeatedUint32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedUint32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedUint32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedUint32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedUint32.length);
+ assertEquals(123, newMsg.repeatedUint32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedUint32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedUint32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedUint32.length);
+ assertEquals(123, newMsg.repeatedUint32[0]);
+ assertEquals(456, newMsg.repeatedUint32[1]);
+ }
+
+ public void testNanoRepeatedUint64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedUint64.length);
+ msg.repeatedUint64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedUint64[1]);
+ assertEquals(456, msg.repeatedUint64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedUint64.length);
+ msg.clear()
+ .repeatedUint64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedUint64.length);
+ assertEquals(456, msg.repeatedUint64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedUint64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedUint64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedUint64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedUint64.length);
+ assertEquals(123, newMsg.repeatedUint64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedUint64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedUint64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedUint64.length);
+ assertEquals(123, newMsg.repeatedUint64[0]);
+ assertEquals(456, newMsg.repeatedUint64[1]);
+ }
+
+ public void testNanoRepeatedSint32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedSint32.length);
+ msg.repeatedSint32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedSint32[1]);
+ assertEquals(456, msg.repeatedSint32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSint32.length);
+ msg.clear()
+ .repeatedSint32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedSint32.length);
+ assertEquals(456, msg.repeatedSint32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSint32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedSint32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedSint32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 7);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedSint32.length);
+ assertEquals(123, newMsg.repeatedSint32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedSint32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedSint32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedSint32.length);
+ assertEquals(123, newMsg.repeatedSint32[0]);
+ assertEquals(456, newMsg.repeatedSint32[1]);
+ }
+
+ public void testNanoRepeatedSint64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedSint64.length);
+ msg.repeatedSint64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedSint64[1]);
+ assertEquals(456, msg.repeatedSint64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSint64.length);
+ msg.clear()
+ .repeatedSint64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedSint64.length);
+ assertEquals(456, msg.repeatedSint64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSint64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedSint64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedSint64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 7);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedSint64.length);
+ assertEquals(123, newMsg.repeatedSint64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedSint64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedSint64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedSint64.length);
+ assertEquals(123, newMsg.repeatedSint64[0]);
+ assertEquals(456, newMsg.repeatedSint64[1]);
+ }
+
+ public void testNanoRepeatedFixed32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedFixed32.length);
+ msg.repeatedFixed32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedFixed32[1]);
+ assertEquals(456, msg.repeatedFixed32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFixed32.length);
+ msg.clear()
+ .repeatedFixed32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedFixed32.length);
+ assertEquals(456, msg.repeatedFixed32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFixed32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedFixed32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedFixed32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedFixed32.length);
+ assertEquals(123, newMsg.repeatedFixed32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedFixed32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedFixed32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 15);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedFixed32.length);
+ assertEquals(123, newMsg.repeatedFixed32[0]);
+ assertEquals(456, newMsg.repeatedFixed32[1]);
+ }
+
+ public void testNanoRepeatedFixed64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedFixed64.length);
+ msg.repeatedFixed64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedFixed64[1]);
+ assertEquals(456, msg.repeatedFixed64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFixed64.length);
+ msg.clear()
+ .repeatedFixed64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedFixed64.length);
+ assertEquals(456, msg.repeatedFixed64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFixed64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedFixed64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedFixed64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 13);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedFixed64.length);
+ assertEquals(123, newMsg.repeatedFixed64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedFixed64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedFixed64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 23);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedFixed64.length);
+ assertEquals(123, newMsg.repeatedFixed64[0]);
+ assertEquals(456, newMsg.repeatedFixed64[1]);
+ }
+
+ public void testNanoRepeatedSfixed32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedSfixed32.length);
+ msg.repeatedSfixed32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedSfixed32[1]);
+ assertEquals(456, msg.repeatedSfixed32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSfixed32.length);
+ msg.clear()
+ .repeatedSfixed32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedSfixed32.length);
+ assertEquals(456, msg.repeatedSfixed32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSfixed32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedSfixed32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedSfixed32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedSfixed32.length);
+ assertEquals(123, newMsg.repeatedSfixed32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedSfixed32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedSfixed32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 15);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedSfixed32.length);
+ assertEquals(123, newMsg.repeatedSfixed32[0]);
+ assertEquals(456, newMsg.repeatedSfixed32[1]);
+ }
+
+ public void testNanoRepeatedSfixed64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedSfixed64.length);
+ msg.repeatedSfixed64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedSfixed64[1]);
+ assertEquals(456, msg.repeatedSfixed64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSfixed64.length);
+ msg.clear()
+ .repeatedSfixed64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedSfixed64.length);
+ assertEquals(456, msg.repeatedSfixed64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedSfixed64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedSfixed64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedSfixed64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 13);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedSfixed64.length);
+ assertEquals(123, newMsg.repeatedSfixed64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedSfixed64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedSfixed64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 23);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedSfixed64.length);
+ assertEquals(123, newMsg.repeatedSfixed64[0]);
+ assertEquals(456, newMsg.repeatedSfixed64[1]);
+ }
+
+ public void testNanoRepeatedFloat() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedFloat.length);
+ msg.repeatedFloat = new float[] { 123f, 789f, 456f };
+ assertEquals(789f, msg.repeatedFloat[1]);
+ assertEquals(456f, msg.repeatedFloat[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFloat.length);
+ msg.clear()
+ .repeatedFloat = new float[] { 456f };
+ assertEquals(1, msg.repeatedFloat.length);
+ assertEquals(456f, msg.repeatedFloat[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedFloat.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedFloat = new float[] { 123f };
+ assertEquals(1, msg.repeatedFloat.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedFloat.length);
+ assertEquals(123f, newMsg.repeatedFloat[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedFloat = new float[] { 123f, 456f };
+ assertEquals(2, msg.repeatedFloat.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 15);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedFloat.length);
+ assertEquals(123f, newMsg.repeatedFloat[0]);
+ assertEquals(456f, newMsg.repeatedFloat[1]);
+ }
+
+ public void testNanoRepeatedDouble() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedDouble.length);
+ msg.repeatedDouble = new double[] { 123.0, 789.0, 456.0 };
+ assertEquals(789.0, msg.repeatedDouble[1]);
+ assertEquals(456.0, msg.repeatedDouble[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedDouble.length);
+ msg.clear()
+ .repeatedDouble = new double[] { 456.0 };
+ assertEquals(1, msg.repeatedDouble.length);
+ assertEquals(456.0, msg.repeatedDouble[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedDouble.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedDouble = new double[] { 123.0 };
+ assertEquals(1, msg.repeatedDouble.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 13);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedDouble.length);
+ assertEquals(123.0, newMsg.repeatedDouble[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedDouble = new double[] { 123.0, 456.0 };
+ assertEquals(2, msg.repeatedDouble.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 23);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedDouble.length);
+ assertEquals(123.0, newMsg.repeatedDouble[0]);
+ assertEquals(456.0, newMsg.repeatedDouble[1]);
+ }
+
+ public void testNanoRepeatedBool() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedBool.length);
+ msg.repeatedBool = new boolean[] { false, true, false };
+ assertTrue(msg.repeatedBool[1]);
+ assertFalse(msg.repeatedBool[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedBool.length);
+ msg.clear()
+ .repeatedBool = new boolean[] { true };
+ assertEquals(1, msg.repeatedBool.length);
+ assertTrue(msg.repeatedBool[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedBool.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedBool = new boolean[] { false };
+ assertEquals(1, msg.repeatedBool.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedBool.length);
+ assertFalse(newMsg.repeatedBool[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedBool = new boolean[] { true, false };
+ assertEquals(2, msg.repeatedBool.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedBool.length);
+ assertTrue(newMsg.repeatedBool[0]);
+ assertFalse(newMsg.repeatedBool[1]);
+ }
+
+ public void testNanoRepeatedString() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedString.length);
+ msg.repeatedString = new String[] { "hello", "bye", "boo" };
+ assertEquals("bye", msg.repeatedString[1]);
+ assertEquals("boo", msg.repeatedString[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedString.length);
+ msg.clear()
+ .repeatedString = new String[] { "boo" };
+ assertEquals(1, msg.repeatedString.length);
+ assertEquals("boo", msg.repeatedString[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedString.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedString = new String[] { "" };
+ assertEquals(1, msg.repeatedString.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedString.length);
+ assertTrue(newMsg.repeatedString[0].isEmpty());
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedString = new String[] { "hello", "world" };
+ assertEquals(2, msg.repeatedString.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 19);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedString.length);
+ assertEquals("hello", newMsg.repeatedString[0]);
+ assertEquals("world", newMsg.repeatedString[1]);
+ }
+
+ public void testNanoRepeatedBytes() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedBytes.length);
+ msg.repeatedBytes = new byte[][] {
+ InternalNano.copyFromUtf8("hello"),
+ InternalNano.copyFromUtf8("bye"),
+ InternalNano.copyFromUtf8("boo")
+ };
+ assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8"));
+ assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8"));
+ msg.clear();
+ assertEquals(0, msg.repeatedBytes.length);
+ msg.clear()
+ .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") };
+ assertEquals(1, msg.repeatedBytes.length);
+ assertEquals("boo", new String(msg.repeatedBytes[0], "UTF-8"));
+ msg.clear();
+ assertEquals(0, msg.repeatedBytes.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("") };
+ assertEquals(1, msg.repeatedBytes.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedBytes.length);
+ assertTrue(newMsg.repeatedBytes[0].length == 0);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedBytes = new byte[][] {
+ InternalNano.copyFromUtf8("hello"),
+ InternalNano.copyFromUtf8("world")
+ };
+ assertEquals(2, msg.repeatedBytes.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 19);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedBytes.length);
+ assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8"));
+ assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8"));
+ }
+
+ public void testNanoRepeatedGroup() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ TestAllTypesNano.RepeatedGroup group0 =
+ new TestAllTypesNano.RepeatedGroup();
+ group0.a = 0;
+ TestAllTypesNano.RepeatedGroup group1 =
+ new TestAllTypesNano.RepeatedGroup();
+ group1.a = 1;
+ TestAllTypesNano.RepeatedGroup group2 =
+ new TestAllTypesNano.RepeatedGroup();
+ group2.a = 2;
+
+ msg.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1, group2 };
+ assertEquals(3, msg.repeatedGroup.length);
+ assertEquals(0, msg.repeatedGroup[0].a);
+ assertEquals(1, msg.repeatedGroup[1].a);
+ assertEquals(2, msg.repeatedGroup[2].a);
+ msg.clear();
+ assertEquals(0, msg.repeatedGroup.length);
+ msg.clear()
+ .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group1 };
+ assertEquals(1, msg.repeatedGroup.length);
+ assertEquals(1, msg.repeatedGroup[0].a);
+ msg.clear();
+ assertEquals(0, msg.repeatedGroup.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0 };
+ assertEquals(1, msg.repeatedGroup.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 7);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedGroup.length);
+ assertEquals(0, newMsg.repeatedGroup[0].a);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedGroup = new TestAllTypesNano.RepeatedGroup[] { group0, group1 };
+ assertEquals(2, msg.repeatedGroup.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 14);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedGroup.length);
+ assertEquals(0, newMsg.repeatedGroup[0].a);
+ assertEquals(1, newMsg.repeatedGroup[1].a);
+ }
+
+ public void testNanoRepeatedNestedMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ TestAllTypesNano.NestedMessage nestedMsg0 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg0.bb = 0;
+ TestAllTypesNano.NestedMessage nestedMsg1 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg1.bb = 1;
+ TestAllTypesNano.NestedMessage nestedMsg2 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg2.bb = 2;
+
+ msg.repeatedNestedMessage =
+ new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1, nestedMsg2 };
+ assertEquals(3, msg.repeatedNestedMessage.length);
+ assertEquals(0, msg.repeatedNestedMessage[0].bb);
+ assertEquals(1, msg.repeatedNestedMessage[1].bb);
+ assertEquals(2, msg.repeatedNestedMessage[2].bb);
+ msg.clear();
+ assertEquals(0, msg.repeatedNestedMessage.length);
+ msg.clear()
+ .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg1 };
+ assertEquals(1, msg.repeatedNestedMessage.length);
+ assertEquals(1, msg.repeatedNestedMessage[0].bb);
+ msg.clear();
+ assertEquals(0, msg.repeatedNestedMessage.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 };
+ assertEquals(1, msg.repeatedNestedMessage.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedNestedMessage.length);
+ assertEquals(0, newMsg.repeatedNestedMessage[0].bb);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0, nestedMsg1 };
+ assertEquals(2, msg.repeatedNestedMessage.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedNestedMessage.length);
+ assertEquals(0, newMsg.repeatedNestedMessage[0].bb);
+ assertEquals(1, newMsg.repeatedNestedMessage[1].bb);
+ }
+
+ public void testNanoRepeatedForeignMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ NanoOuterClass.ForeignMessageNano foreignMsg0 =
+ new NanoOuterClass.ForeignMessageNano();
+ foreignMsg0.c = 0;
+ NanoOuterClass.ForeignMessageNano foreignMsg1 =
+ new NanoOuterClass.ForeignMessageNano();
+ foreignMsg1.c = 1;
+ NanoOuterClass.ForeignMessageNano foreignMsg2 =
+ new NanoOuterClass.ForeignMessageNano();
+ foreignMsg2.c = 2;
+
+ msg.repeatedForeignMessage =
+ new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 };
+ assertEquals(3, msg.repeatedForeignMessage.length);
+ assertEquals(0, msg.repeatedForeignMessage[0].c);
+ assertEquals(1, msg.repeatedForeignMessage[1].c);
+ assertEquals(2, msg.repeatedForeignMessage[2].c);
+ msg.clear();
+ assertEquals(0, msg.repeatedForeignMessage.length);
+ msg.clear()
+ .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg1 };
+ assertEquals(1, msg.repeatedForeignMessage.length);
+ assertEquals(1, msg.repeatedForeignMessage[0].c);
+ msg.clear();
+ assertEquals(0, msg.repeatedForeignMessage.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0 };
+ assertEquals(1, msg.repeatedForeignMessage.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedForeignMessage.length);
+ assertEquals(0, newMsg.repeatedForeignMessage[0].c);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedForeignMessage = new NanoOuterClass.ForeignMessageNano[] { foreignMsg0, foreignMsg1 };
+ assertEquals(2, msg.repeatedForeignMessage.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedForeignMessage.length);
+ assertEquals(0, newMsg.repeatedForeignMessage[0].c);
+ assertEquals(1, newMsg.repeatedForeignMessage[1].c);
+ }
+
+ public void testNanoRepeatedImportMessage() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ UnittestImportNano.ImportMessageNano foreignMsg0 =
+ new UnittestImportNano.ImportMessageNano();
+ foreignMsg0.d = 0;
+ UnittestImportNano.ImportMessageNano foreignMsg1 =
+ new UnittestImportNano.ImportMessageNano();
+ foreignMsg1.d = 1;
+ UnittestImportNano.ImportMessageNano foreignMsg2 =
+ new UnittestImportNano.ImportMessageNano();
+ foreignMsg2.d = 2;
+
+ msg.repeatedImportMessage =
+ new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1, foreignMsg2 };
+ assertEquals(3, msg.repeatedImportMessage.length);
+ assertEquals(0, msg.repeatedImportMessage[0].d);
+ assertEquals(1, msg.repeatedImportMessage[1].d);
+ assertEquals(2, msg.repeatedImportMessage[2].d);
+ msg.clear();
+ assertEquals(0, msg.repeatedImportMessage.length);
+ msg.clear()
+ .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg1 };
+ assertEquals(1, msg.repeatedImportMessage.length);
+ assertEquals(1, msg.repeatedImportMessage[0].d);
+ msg.clear();
+ assertEquals(0, msg.repeatedImportMessage.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0 };
+ assertEquals(1, msg.repeatedImportMessage.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedImportMessage.length);
+ assertEquals(0, newMsg.repeatedImportMessage[0].d);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedImportMessage = new UnittestImportNano.ImportMessageNano[] { foreignMsg0, foreignMsg1 };
+ assertEquals(2, msg.repeatedImportMessage.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedImportMessage.length);
+ assertEquals(0, newMsg.repeatedImportMessage[0].d);
+ assertEquals(1, newMsg.repeatedImportMessage[1].d);
+ }
+
+ public void testNanoRepeatedNestedEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedNestedEnum = new int[] {
+ TestAllTypesNano.FOO,
+ TestAllTypesNano.BAR,
+ TestAllTypesNano.BAZ
+ };
+ assertEquals(3, msg.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]);
+ assertEquals(TestAllTypesNano.BAZ, msg.repeatedNestedEnum[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedNestedEnum.length);
+ msg.clear()
+ .repeatedNestedEnum = new int[] { TestAllTypesNano.BAR };
+ assertEquals(1, msg.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedNestedEnum.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR };
+ assertEquals(2, msg.repeatedNestedEnum.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedNestedEnum[1]);
+ }
+
+ public void testNanoRepeatedForeignEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedForeignEnum = new int[] {
+ NanoOuterClass.FOREIGN_NANO_FOO,
+ NanoOuterClass.FOREIGN_NANO_BAR,
+ NanoOuterClass.FOREIGN_NANO_BAZ
+ };
+ assertEquals(3, msg.repeatedForeignEnum.length);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAZ, msg.repeatedForeignEnum[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedForeignEnum.length);
+ msg.clear()
+ .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_BAR };
+ assertEquals(1, msg.repeatedForeignEnum.length);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedForeignEnum.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedForeignEnum = new int[] { NanoOuterClass.FOREIGN_NANO_FOO };
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedForeignEnum.length);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedForeignEnum = new int[] {
+ NanoOuterClass.FOREIGN_NANO_FOO,
+ NanoOuterClass.FOREIGN_NANO_BAR
+ };
+ assertEquals(2, msg.repeatedForeignEnum.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedForeignEnum.length);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_FOO, msg.repeatedForeignEnum[0]);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.repeatedForeignEnum[1]);
+ }
+
+ public void testNanoRepeatedImportEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedImportEnum = new int[] {
+ UnittestImportNano.IMPORT_NANO_FOO,
+ UnittestImportNano.IMPORT_NANO_BAR,
+ UnittestImportNano.IMPORT_NANO_BAZ
+ };
+ assertEquals(3, msg.repeatedImportEnum.length);
+ assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAZ, msg.repeatedImportEnum[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedImportEnum.length);
+ msg.clear()
+ .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_BAR };
+ assertEquals(1, msg.repeatedImportEnum.length);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedImportEnum.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedImportEnum = new int[] { UnittestImportNano.IMPORT_NANO_FOO };
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedImportEnum.length);
+ assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedImportEnum = new int[] {
+ UnittestImportNano.IMPORT_NANO_FOO,
+ UnittestImportNano.IMPORT_NANO_BAR
+ };
+ assertEquals(2, msg.repeatedImportEnum.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedImportEnum.length);
+ assertEquals(UnittestImportNano.IMPORT_NANO_FOO, msg.repeatedImportEnum[0]);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.repeatedImportEnum[1]);
+ }
+
+ public void testNanoRepeatedStringPiece() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedStringPiece.length);
+ msg.repeatedStringPiece = new String[] { "hello", "bye", "boo" };
+ assertEquals("bye", msg.repeatedStringPiece[1]);
+ assertEquals("boo", msg.repeatedStringPiece[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedStringPiece.length);
+ msg.clear()
+ .repeatedStringPiece = new String[] { "boo" };
+ assertEquals(1, msg.repeatedStringPiece.length);
+ assertEquals("boo", msg.repeatedStringPiece[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedStringPiece.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedStringPiece = new String[] { "" };
+ assertEquals(1, msg.repeatedStringPiece.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedStringPiece.length);
+ assertTrue(newMsg.repeatedStringPiece[0].isEmpty());
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedStringPiece = new String[] { "hello", "world" };
+ assertEquals(2, msg.repeatedStringPiece.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 19);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedStringPiece.length);
+ assertEquals("hello", newMsg.repeatedStringPiece[0]);
+ assertEquals("world", newMsg.repeatedStringPiece[1]);
+ }
+
+ public void testNanoRepeatedCord() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedCord.length);
+ msg.repeatedCord = new String[] { "hello", "bye", "boo" };
+ assertEquals("bye", msg.repeatedCord[1]);
+ assertEquals("boo", msg.repeatedCord[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedCord.length);
+ msg.clear()
+ .repeatedCord = new String[] { "boo" };
+ assertEquals(1, msg.repeatedCord.length);
+ assertEquals("boo", msg.repeatedCord[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedCord.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedCord = new String[] { "" };
+ assertEquals(1, msg.repeatedCord.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 6);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedCord.length);
+ assertTrue(newMsg.repeatedCord[0].isEmpty());
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedCord = new String[] { "hello", "world" };
+ assertEquals(2, msg.repeatedCord.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 19);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedCord.length);
+ assertEquals("hello", newMsg.repeatedCord[0]);
+ assertEquals("world", newMsg.repeatedCord[1]);
+ }
+
+ public void testNanoRepeatedPackedInt32() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedPackedInt32.length);
+ msg.repeatedPackedInt32 = new int[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedPackedInt32[1]);
+ assertEquals(456, msg.repeatedPackedInt32[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedInt32.length);
+ msg.clear()
+ .repeatedPackedInt32 = new int[] { 456 };
+ assertEquals(1, msg.repeatedPackedInt32.length);
+ assertEquals(456, msg.repeatedPackedInt32[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedInt32.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedPackedInt32 = new int[] { 123 };
+ assertEquals(1, msg.repeatedPackedInt32.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 7);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedPackedInt32.length);
+ assertEquals(123, newMsg.repeatedPackedInt32[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedPackedInt32 = new int[] { 123, 456 };
+ assertEquals(2, msg.repeatedPackedInt32.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 9);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedPackedInt32.length);
+ assertEquals(123, newMsg.repeatedPackedInt32[0]);
+ assertEquals(456, newMsg.repeatedPackedInt32[1]);
+ }
+
+ public void testNanoRepeatedPackedSfixed64() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ assertEquals(0, msg.repeatedPackedSfixed64.length);
+ msg.repeatedPackedSfixed64 = new long[] { 123, 789, 456 };
+ assertEquals(789, msg.repeatedPackedSfixed64[1]);
+ assertEquals(456, msg.repeatedPackedSfixed64[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedSfixed64.length);
+ msg.clear()
+ .repeatedPackedSfixed64 = new long[] { 456 };
+ assertEquals(1, msg.repeatedPackedSfixed64.length);
+ assertEquals(456, msg.repeatedPackedSfixed64[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedSfixed64.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedPackedSfixed64 = new long[] { 123 };
+ assertEquals(1, msg.repeatedPackedSfixed64.length);
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 14);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedPackedSfixed64.length);
+ assertEquals(123, newMsg.repeatedPackedSfixed64[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedPackedSfixed64 = new long[] { 123, 456 };
+ assertEquals(2, msg.repeatedPackedSfixed64.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 22);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedPackedSfixed64.length);
+ assertEquals(123, newMsg.repeatedPackedSfixed64[0]);
+ assertEquals(456, newMsg.repeatedPackedSfixed64[1]);
+ }
+
+ public void testNanoRepeatedPackedNestedEnum() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedPackedNestedEnum = new int[] {
+ TestAllTypesNano.FOO,
+ TestAllTypesNano.BAR,
+ TestAllTypesNano.BAZ
+ };
+ assertEquals(3, msg.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]);
+ assertEquals(TestAllTypesNano.BAZ, msg.repeatedPackedNestedEnum[2]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedNestedEnum.length);
+ msg.clear()
+ .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.BAR };
+ assertEquals(1, msg.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[0]);
+ msg.clear();
+ assertEquals(0, msg.repeatedPackedNestedEnum.length);
+
+ // Test 1 entry
+ msg.clear()
+ .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO };
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 7);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(1, newMsg.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
+
+ // Test 2 entries
+ msg.clear()
+ .repeatedPackedNestedEnum = new int[] { TestAllTypesNano.FOO, TestAllTypesNano.BAR };
+ assertEquals(2, msg.repeatedPackedNestedEnum.length);
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 8);
+ assertEquals(result.length, msgSerializedSize);
+
+ newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(2, newMsg.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, msg.repeatedPackedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.BAR, msg.repeatedPackedNestedEnum[1]);
+ }
+
+ public void testNanoRepeatedPackedSerializedSize() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedPackedInt32 = new int[] { 123, 789, 456 };
+ int msgSerializedSize = msg.getSerializedSize();
+ byte [] result = MessageNano.toByteArray(msg);
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 11);
+ assertEquals(result.length, msgSerializedSize);
+ TestAllTypesNano msg2 = new TestAllTypesNano();
+ msg2.repeatedPackedInt32 = new int[] { 123, 789, 456 };
+ byte [] result2 = new byte[msgSerializedSize];
+ MessageNano.toByteArray(msg2, result2, 0, msgSerializedSize);
+
+ // Check equal size and content.
+ assertEquals(msgSerializedSize, msg2.getSerializedSize());
+ assertTrue(Arrays.equals(result, result2));
+ }
+
+ public void testNanoRepeatedInt32ReMerge() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedInt32 = new int[] { 234 };
+ byte [] result1 = MessageNano.toByteArray(msg);
+
+ msg.clear().optionalInt32 = 789;
+ byte [] result2 = MessageNano.toByteArray(msg);
+
+ msg.clear().repeatedInt32 = new int[] { 123, 456 };
+ byte [] result3 = MessageNano.toByteArray(msg);
+
+ // Concatenate the three serializations and read as one message.
+ byte [] result = new byte[result1.length + result2.length + result3.length];
+ System.arraycopy(result1, 0, result, 0, result1.length);
+ System.arraycopy(result2, 0, result, result1.length, result2.length);
+ System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(789, newMsg.optionalInt32);
+ assertEquals(3, newMsg.repeatedInt32.length);
+ assertEquals(234, newMsg.repeatedInt32[0]);
+ assertEquals(123, newMsg.repeatedInt32[1]);
+ assertEquals(456, newMsg.repeatedInt32[2]);
+ }
+
+ public void testNanoRepeatedNestedEnumReMerge() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
+ byte [] result1 = MessageNano.toByteArray(msg);
+
+ msg.clear().optionalInt32 = 789;
+ byte [] result2 = MessageNano.toByteArray(msg);
+
+ msg.clear().repeatedNestedEnum = new int[] { TestAllTypesNano.BAR, TestAllTypesNano.FOO };
+ byte [] result3 = MessageNano.toByteArray(msg);
+
+ // Concatenate the three serializations and read as one message.
+ byte [] result = new byte[result1.length + result2.length + result3.length];
+ System.arraycopy(result1, 0, result, 0, result1.length);
+ System.arraycopy(result2, 0, result, result1.length, result2.length);
+ System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(789, newMsg.optionalInt32);
+ assertEquals(3, newMsg.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.BAR, newMsg.repeatedNestedEnum[1]);
+ assertEquals(TestAllTypesNano.FOO, newMsg.repeatedNestedEnum[2]);
+ }
+
+ public void testNanoRepeatedNestedMessageReMerge() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ TestAllTypesNano.NestedMessage nestedMsg0 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg0.bb = 0;
+ TestAllTypesNano.NestedMessage nestedMsg1 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg1.bb = 1;
+ TestAllTypesNano.NestedMessage nestedMsg2 =
+ new TestAllTypesNano.NestedMessage();
+ nestedMsg2.bb = 2;
+
+ msg.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] { nestedMsg0 };
+ byte [] result1 = MessageNano.toByteArray(msg);
+
+ msg.clear().optionalInt32 = 789;
+ byte [] result2 = MessageNano.toByteArray(msg);
+
+ msg.clear().repeatedNestedMessage =
+ new TestAllTypesNano.NestedMessage[] { nestedMsg1, nestedMsg2 };
+ byte [] result3 = MessageNano.toByteArray(msg);
+
+ // Concatenate the three serializations and read as one message.
+ byte [] result = new byte[result1.length + result2.length + result3.length];
+ System.arraycopy(result1, 0, result, 0, result1.length);
+ System.arraycopy(result2, 0, result, result1.length, result2.length);
+ System.arraycopy(result3, 0, result, result1.length + result2.length, result3.length);
+
+ TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result);
+ assertEquals(789, newMsg.optionalInt32);
+ assertEquals(3, newMsg.repeatedNestedMessage.length);
+ assertEquals(nestedMsg0.bb, newMsg.repeatedNestedMessage[0].bb);
+ assertEquals(nestedMsg1.bb, newMsg.repeatedNestedMessage[1].bb);
+ assertEquals(nestedMsg2.bb, newMsg.repeatedNestedMessage[2].bb);
+ }
+
+ /**
+ * Tests that invalid enum values from the wire are not accepted.
+ */
+ public void testNanoEnumValidity() throws Exception {
+ final int invalid = 120;
+ final int alsoInvalid = 121;
+
+ EnumValidity.M m = new EnumValidity.M();
+ // Sanity check & baseline of the assertions for the first case below.
+ assertEquals(EnumValidity.E.default_, m.optionalE);
+ assertEquals(EnumValidity.E.BAZ, m.defaultE);
+
+ m.optionalE = invalid;
+ m.defaultE = invalid;
+ // E contains all valid values
+ m.repeatedE = new int[] {EnumValidity.E.FOO, EnumValidity.E.BAR};
+ m.packedE = new int[] {EnumValidity.E.FOO, EnumValidity.E.BAZ};
+ // E2 contains some invalid values
+ m.repeatedE2 = new int[] {invalid, EnumValidity.E.BAR, alsoInvalid};
+ m.packedE2 = new int[] {EnumValidity.E.FOO, invalid, alsoInvalid};
+ // E3 contains all invalid values
+ m.repeatedE3 = new int[] {invalid, invalid};
+ m.packedE3 = new int[] {alsoInvalid, alsoInvalid};
+ byte[] serialized = MessageNano.toByteArray(m);
+ // Sanity check that we do have all data in the byte array.
+ assertEquals(31, serialized.length);
+
+ // Test 1: tests that invalid values aren't included in the deserialized message.
+ EnumValidity.M deserialized = MessageNano.mergeFrom(new EnumValidity.M(), serialized);
+ assertEquals(EnumValidity.E.default_, deserialized.optionalE);
+ assertEquals(EnumValidity.E.BAZ, deserialized.defaultE);
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.FOO, EnumValidity.E.BAR}, deserialized.repeatedE));
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.FOO, EnumValidity.E.BAZ}, deserialized.packedE));
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.BAR}, deserialized.repeatedE2));
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.FOO}, deserialized.packedE2));
+ assertEquals(0, deserialized.repeatedE3.length);
+ assertEquals(0, deserialized.packedE3.length);
+
+ // Test 2: tests that invalid values do not override previous values in the field, including
+ // arrays, including pre-existing invalid values.
+ deserialized.optionalE = EnumValidity.E.BAR;
+ deserialized.defaultE = alsoInvalid;
+ deserialized.repeatedE = new int[] {EnumValidity.E.BAZ};
+ deserialized.packedE = new int[] {EnumValidity.E.BAZ, alsoInvalid};
+ deserialized.repeatedE2 = new int[] {invalid, alsoInvalid};
+ deserialized.packedE2 = null;
+ deserialized.repeatedE3 = null;
+ deserialized.packedE3 = new int[0];
+ MessageNano.mergeFrom(deserialized, serialized);
+ assertEquals(EnumValidity.E.BAR, deserialized.optionalE);
+ assertEquals(alsoInvalid, deserialized.defaultE);
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.BAZ, /* + */ EnumValidity.E.FOO, EnumValidity.E.BAR},
+ deserialized.repeatedE));
+ assertTrue(Arrays.equals(
+ new int[] {EnumValidity.E.BAZ, alsoInvalid, /* + */ EnumValidity.E.FOO, EnumValidity.E.BAZ},
+ deserialized.packedE));
+ assertTrue(Arrays.equals(
+ new int[] {invalid, alsoInvalid, /* + */ EnumValidity.E.BAR},
+ deserialized.repeatedE2));
+ assertTrue(Arrays.equals(
+ new int[] {/* <null> + */ EnumValidity.E.FOO},
+ deserialized.packedE2));
+ assertNull(deserialized.repeatedE3); // null + all invalid == null
+ assertEquals(0, deserialized.packedE3.length); // empty + all invalid == empty
+
+ // Test 3: reading by alternative forms
+ EnumValidity.Alt alt = MessageNano.mergeFrom(new EnumValidity.Alt(), serialized);
+ assertEquals(EnumValidity.E.BAR, // last valid value in m.repeatedE2
+ alt.repeatedE2AsOptional);
+ assertTrue(Arrays.equals(new int[] {EnumValidity.E.FOO}, alt.packedE2AsNonPacked));
+ assertEquals(0, alt.nonPackedE3AsPacked.length);
+ }
+
+ /**
+ * Tests the same as {@link #testNanoEnumValidity()} with accessor style. Repeated fields are
+ * not re-tested here because they are not affected by the accessor style.
+ */
+ public void testNanoEnumValidityAccessors() throws Exception {
+ final int invalid = 120;
+ final int alsoInvalid = 121;
+
+ EnumValidityAccessors.M m = new EnumValidityAccessors.M();
+ // Sanity check & baseline of the assertions for the first case below.
+ assertEquals(EnumValidityAccessors.default_, m.getOptionalE());
+ assertEquals(EnumValidityAccessors.BAZ, m.getDefaultE());
+
+ m.setOptionalE(invalid);
+ m.setDefaultE(invalid);
+ // Set repeatedE2 for Alt.repeatedE2AsOptional
+ m.repeatedE2 = new int[] {invalid, EnumValidityAccessors.BAR, alsoInvalid};
+ byte[] serialized = MessageNano.toByteArray(m);
+ // Sanity check that we do have all data in the byte array.
+ assertEquals(10, serialized.length);
+
+ // Test 1: tests that invalid values aren't included in the deserialized message.
+ EnumValidityAccessors.M deserialized =
+ MessageNano.mergeFrom(new EnumValidityAccessors.M(), serialized);
+ assertEquals(EnumValidityAccessors.default_, deserialized.getOptionalE());
+ assertEquals(EnumValidityAccessors.BAZ, deserialized.getDefaultE());
+
+ // Test 2: tests that invalid values do not override previous values in the field, including
+ // pre-existing invalid values.
+ deserialized.setOptionalE(EnumValidityAccessors.BAR);
+ deserialized.setDefaultE(alsoInvalid);
+ MessageNano.mergeFrom(deserialized, serialized);
+ assertEquals(EnumValidityAccessors.BAR, deserialized.getOptionalE());
+ assertEquals(alsoInvalid, deserialized.getDefaultE());
+
+ // Test 3: reading by alternative forms
+ EnumValidityAccessors.Alt alt =
+ MessageNano.mergeFrom(new EnumValidityAccessors.Alt(), serialized);
+ assertEquals(EnumValidityAccessors.BAR, // last valid value in m.repeatedE2
+ alt.getRepeatedE2AsOptional());
+ }
+
+ /**
+ * Tests that code generation correctly wraps a single message into its outer
+ * class. The class {@code SingleMessageNano} is imported from the outer
+ * class {@code UnittestSingleNano}, whose name is implicit. Any error would
+ * cause this method to fail compilation.
+ */
+ public void testNanoSingle() throws Exception {
+ SingleMessageNano msg = new SingleMessageNano();
+ assertNotNull(msg);
+ }
+
+ /**
+ * Tests that code generation correctly skips generating the outer class if
+ * unnecessary, letting a file-scope entity have the same name. The class
+ * {@code MultipleNameClashNano} shares the same name with the file's outer
+ * class defined explicitly, but the file contains no other entities and has
+ * java_multiple_files set. Any error would cause this method to fail
+ * compilation.
+ */
+ public void testNanoMultipleNameClash() throws Exception {
+ MultipleNameClashNano msg = new MultipleNameClashNano();
+ msg.field = 0;
+ }
+
+ /**
+ * Tests that code generation correctly handles enums in different scopes in
+ * a source file with the option java_multiple_files set to true. Any error
+ * would cause this method to fail compilation.
+ */
+ public void testNanoMultipleEnumScoping() throws Exception {
+ FileScopeEnumRefNano msg1 = new FileScopeEnumRefNano();
+ msg1.enumField = UnittestMultipleNano.ONE;
+ MessageScopeEnumRefNano msg2 = new MessageScopeEnumRefNano();
+ msg2.enumField = MessageScopeEnumRefNano.TWO;
+ }
+
+ /**
+ * Tests that code generation with mixed values of the java_multiple_files
+ * options between the main source file and the imported source files would
+ * generate correct references. Any error would cause this method to fail
+ * compilation.
+ */
+ public void testNanoMultipleImportingNonMultiple() throws Exception {
+ UnittestImportNano.ImportMessageNano importMsg = new UnittestImportNano.ImportMessageNano();
+ MultipleImportingNonMultipleNano1 nano1 = new MultipleImportingNonMultipleNano1();
+ nano1.field = importMsg;
+ MultipleImportingNonMultipleNano2 nano2 = new MultipleImportingNonMultipleNano2();
+ nano2.nano1 = nano1;
+ }
+
+ public void testNanoDefaults() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ for (int i = 0; i < 2; i++) {
+ assertEquals(41, msg.defaultInt32);
+ assertEquals(42, msg.defaultInt64);
+ assertEquals(43, msg.defaultUint32);
+ assertEquals(44, msg.defaultUint64);
+ assertEquals(-45, msg.defaultSint32);
+ assertEquals(46, msg.defaultSint64);
+ assertEquals(47, msg.defaultFixed32);
+ assertEquals(48, msg.defaultFixed64);
+ assertEquals(49, msg.defaultSfixed32);
+ assertEquals(-50, msg.defaultSfixed64);
+ assertTrue(51.5f == msg.defaultFloat);
+ assertTrue(52.0e3 == msg.defaultDouble);
+ assertEquals(true, msg.defaultBool);
+ assertEquals("hello", msg.defaultString);
+ assertEquals("world", new String(msg.defaultBytes, "UTF-8"));
+ assertEquals("dünya", msg.defaultStringNonascii);
+ assertEquals("dünyab", new String(msg.defaultBytesNonascii, "UTF-8"));
+ assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum);
+ assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum);
+ assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum);
+ assertEquals(Float.POSITIVE_INFINITY, msg.defaultFloatInf);
+ assertEquals(Float.NEGATIVE_INFINITY, msg.defaultFloatNegInf);
+ assertEquals(Float.NaN, msg.defaultFloatNan);
+ assertEquals(Double.POSITIVE_INFINITY, msg.defaultDoubleInf);
+ assertEquals(Double.NEGATIVE_INFINITY, msg.defaultDoubleNegInf);
+ assertEquals(Double.NaN, msg.defaultDoubleNan);
+
+ // Default values are not output, except for required fields.
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 3);
+ assertEquals(result.length, msgSerializedSize);
+ msg.clear();
+ }
+ }
+
+ public void testNanoWithHasParseFrom() throws Exception {
+ TestAllTypesNanoHas msg = null;
+ // Test false on creation, after clear and upon empty parse.
+ for (int i = 0; i < 3; i++) {
+ if (i == 0) {
+ msg = new TestAllTypesNanoHas();
+ } else if (i == 1) {
+ msg.clear();
+ } else if (i == 2) {
+ msg = TestAllTypesNanoHas.parseFrom(new byte[0]);
+ }
+ assertFalse(msg.hasOptionalInt32);
+ assertFalse(msg.hasOptionalString);
+ assertFalse(msg.hasOptionalBytes);
+ assertFalse(msg.hasOptionalNestedEnum);
+ assertFalse(msg.hasDefaultInt32);
+ assertFalse(msg.hasDefaultString);
+ assertFalse(msg.hasDefaultBytes);
+ assertFalse(msg.hasDefaultFloatNan);
+ assertFalse(msg.hasDefaultNestedEnum);
+ assertFalse(msg.hasId);
+ assertFalse(msg.hasRequiredEnum);
+ msg.optionalInt32 = 123;
+ msg.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
+ msg.optionalNestedMessage.bb = 2;
+ msg.optionalNestedEnum = TestAllTypesNano.BAZ;
+ }
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 10);
+ assertEquals(result.length, msgSerializedSize);
+
+ // Has fields true upon parse.
+ TestAllTypesNanoHas newMsg = TestAllTypesNanoHas.parseFrom(result);
+ assertEquals(123, newMsg.optionalInt32);
+ assertTrue(newMsg.hasOptionalInt32);
+ assertEquals(2, newMsg.optionalNestedMessage.bb);
+ assertTrue(newMsg.optionalNestedMessage.hasBb);
+ assertEquals(TestAllTypesNanoHas.BAZ, newMsg.optionalNestedEnum);
+ assertTrue(newMsg.hasOptionalNestedEnum);
+ }
+
+ public void testNanoWithHasSerialize() throws Exception {
+ TestAllTypesNanoHas msg = new TestAllTypesNanoHas();
+ msg.hasOptionalInt32 = true;
+ msg.hasOptionalString = true;
+ msg.hasOptionalBytes = true;
+ msg.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
+ msg.optionalNestedMessage.hasBb = true;
+ msg.hasOptionalNestedEnum = true;
+ msg.hasDefaultInt32 = true;
+ msg.hasDefaultString = true;
+ msg.hasDefaultBytes = true;
+ msg.hasDefaultFloatNan = true;
+ msg.hasDefaultNestedEnum = true;
+ msg.hasId = true;
+ msg.hasRequiredEnum = true;
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ assertEquals(result.length, msgSerializedSize);
+
+ // Now deserialize and find that all fields are set and equal to their defaults.
+ TestAllTypesNanoHas newMsg = TestAllTypesNanoHas.parseFrom(result);
+ assertTrue(newMsg.hasOptionalInt32);
+ assertTrue(newMsg.hasOptionalString);
+ assertTrue(newMsg.hasOptionalBytes);
+ assertTrue(newMsg.optionalNestedMessage.hasBb);
+ assertTrue(newMsg.hasOptionalNestedEnum);
+ assertTrue(newMsg.hasDefaultInt32);
+ assertTrue(newMsg.hasDefaultString);
+ assertTrue(newMsg.hasDefaultBytes);
+ assertTrue(newMsg.hasDefaultFloatNan);
+ assertTrue(newMsg.hasDefaultNestedEnum);
+ assertTrue(newMsg.hasId);
+ assertTrue(newMsg.hasRequiredEnum);
+ assertEquals(0, newMsg.optionalInt32);
+ assertEquals(0, newMsg.optionalString.length());
+ assertEquals(0, newMsg.optionalBytes.length);
+ assertEquals(0, newMsg.optionalNestedMessage.bb);
+ assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum);
+ assertEquals(41, newMsg.defaultInt32);
+ assertEquals("hello", newMsg.defaultString);
+ assertEquals("world", new String(newMsg.defaultBytes, "UTF-8"));
+ assertEquals(TestAllTypesNanoHas.BAR, newMsg.defaultNestedEnum);
+ assertEquals(Float.NaN, newMsg.defaultFloatNan);
+ assertEquals(0, newMsg.id);
+ assertEquals(TestAllTypesNanoHas.FOO, newMsg.requiredEnum);
+ }
+
+ public void testNanoWithAccessorsBasic() throws Exception {
+ TestNanoAccessors msg = new TestNanoAccessors();
+
+ // Makes sure required, repeated, and message fields are still public
+ msg.id = 3;
+ msg.repeatedBytes = new byte[2][3];
+ msg.optionalNestedMessage = null;
+
+ // Test accessors
+ assertEquals(0, msg.getOptionalInt32());
+ assertFalse(msg.hasOptionalInt32());
+ msg.setOptionalInt32(135);
+ assertEquals(135, msg.getOptionalInt32());
+ assertTrue(msg.hasOptionalInt32());
+ msg.clearOptionalInt32();
+ assertFalse(msg.hasOptionalInt32());
+ msg.setOptionalInt32(0); // default value
+ assertTrue(msg.hasOptionalInt32());
+
+ // Test NPE
+ try {
+ msg.setOptionalBytes(null);
+ fail();
+ } catch (NullPointerException expected) {}
+ try {
+ msg.setOptionalString(null);
+ fail();
+ } catch (NullPointerException expected) {}
+
+ // Test has bit on bytes field with defaults and clear() re-clones the default array
+ assertFalse(msg.hasDefaultBytes());
+ byte[] defaultBytes = msg.getDefaultBytes();
+ msg.setDefaultBytes(defaultBytes);
+ assertTrue(msg.hasDefaultBytes());
+ msg.clearDefaultBytes();
+ assertFalse(msg.hasDefaultBytes());
+ defaultBytes[0]++; // modify original array
+ assertFalse(Arrays.equals(defaultBytes, msg.getDefaultBytes()));
+
+ // Test has bits that require additional bit fields
+ assertFalse(msg.hasBitFieldCheck());
+ msg.setBitFieldCheck(0);
+ assertTrue(msg.hasBitFieldCheck());
+ assertFalse(msg.hasBeforeBitFieldCheck()); // checks bit field does not leak
+ assertFalse(msg.hasAfterBitFieldCheck());
+
+ // Test clear() clears has bits
+ msg.setOptionalString("hi");
+ msg.setDefaultString("there");
+ msg.clear();
+ assertFalse(msg.hasOptionalString());
+ assertFalse(msg.hasDefaultString());
+ assertFalse(msg.hasBitFieldCheck());
+
+ // Test set() and clear() returns itself (compiles = success)
+ msg.clear()
+ .setOptionalInt32(3)
+ .clearDefaultBytes()
+ .setOptionalString("4");
+ }
+
+ public void testNanoWithAccessorsParseFrom() throws Exception {
+ TestNanoAccessors msg = null;
+ // Test false on creation, after clear and upon empty parse.
+ for (int i = 0; i < 3; i++) {
+ if (i == 0) {
+ msg = new TestNanoAccessors();
+ } else if (i == 1) {
+ msg.clear();
+ } else if (i == 2) {
+ msg = TestNanoAccessors.parseFrom(new byte[0]);
+ }
+ assertFalse(msg.hasOptionalInt32());
+ assertFalse(msg.hasOptionalString());
+ assertFalse(msg.hasOptionalBytes());
+ assertFalse(msg.hasOptionalNestedEnum());
+ assertFalse(msg.hasDefaultInt32());
+ assertFalse(msg.hasDefaultString());
+ assertFalse(msg.hasDefaultBytes());
+ assertFalse(msg.hasDefaultFloatNan());
+ assertFalse(msg.hasDefaultNestedEnum());
+ msg.optionalNestedMessage = new TestNanoAccessors.NestedMessage();
+ msg.optionalNestedMessage.setBb(2);
+ msg.setOptionalNestedEnum(TestNanoAccessors.BAZ);
+ msg.setDefaultInt32(msg.getDefaultInt32());
+ }
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ //System.out.printf("mss=%d result.length=%d\n", msgSerializedSize, result.length);
+ assertTrue(msgSerializedSize == 14);
+ assertEquals(result.length, msgSerializedSize);
+
+ // Has fields true upon parse.
+ TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(result);
+ assertEquals(2, newMsg.optionalNestedMessage.getBb());
+ assertTrue(newMsg.optionalNestedMessage.hasBb());
+ assertEquals(TestNanoAccessors.BAZ, newMsg.getOptionalNestedEnum());
+ assertTrue(newMsg.hasOptionalNestedEnum());
+
+ // Has field true on fields with explicit default values from wire.
+ assertTrue(newMsg.hasDefaultInt32());
+ assertEquals(41, newMsg.getDefaultInt32());
+ }
+
+ public void testNanoWithAccessorsPublicFieldTypes() throws Exception {
+ TestNanoAccessors msg = new TestNanoAccessors();
+ assertNull(msg.optionalNestedMessage);
+ assertEquals(0, msg.id);
+ assertEquals(0, msg.repeatedNestedEnum.length);
+
+ TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(MessageNano.toByteArray(msg));
+ assertNull(newMsg.optionalNestedMessage);
+ assertEquals(0, newMsg.id);
+ assertEquals(0, newMsg.repeatedNestedEnum.length);
+
+ TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
+ nestedMessage.setBb(5);
+ newMsg.optionalNestedMessage = nestedMessage;
+ newMsg.id = -1;
+ newMsg.repeatedNestedEnum = new int[] { TestAllTypesNano.FOO };
+
+ TestNanoAccessors newMsg2 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg));
+ assertEquals(nestedMessage.getBb(), newMsg2.optionalNestedMessage.getBb());
+ assertEquals(-1, newMsg2.id);
+ assertEquals(TestAllTypesNano.FOO, newMsg2.repeatedNestedEnum[0]);
+
+ newMsg2.optionalNestedMessage = null;
+ newMsg2.id = 0;
+ newMsg2.repeatedNestedEnum = null;
+
+ TestNanoAccessors newMsg3 = TestNanoAccessors.parseFrom(MessageNano.toByteArray(newMsg2));
+ assertNull(newMsg3.optionalNestedMessage);
+ assertEquals(0, newMsg3.id);
+ assertEquals(0, newMsg3.repeatedNestedEnum.length);
+ }
+
+ public void testNanoWithAccessorsSerialize() throws Exception {
+ TestNanoAccessors msg = new TestNanoAccessors();
+ msg.setOptionalInt32(msg.getOptionalInt32());
+ msg.setOptionalString(msg.getOptionalString());
+ msg.setOptionalBytes(msg.getOptionalBytes());
+ TestNanoAccessors.NestedMessage nestedMessage = new TestNanoAccessors.NestedMessage();
+ nestedMessage.setBb(nestedMessage.getBb());
+ msg.optionalNestedMessage = nestedMessage;
+ msg.setOptionalNestedEnum(msg.getOptionalNestedEnum());
+ msg.setDefaultInt32(msg.getDefaultInt32());
+ msg.setDefaultString(msg.getDefaultString());
+ msg.setDefaultBytes(msg.getDefaultBytes());
+ msg.setDefaultFloatNan(msg.getDefaultFloatNan());
+ msg.setDefaultNestedEnum(msg.getDefaultNestedEnum());
+
+ byte [] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ assertEquals(result.length, msgSerializedSize);
+
+ // Now deserialize and find that all fields are set and equal to their defaults.
+ TestNanoAccessors newMsg = TestNanoAccessors.parseFrom(result);
+ assertTrue(newMsg.hasOptionalInt32());
+ assertTrue(newMsg.hasOptionalString());
+ assertTrue(newMsg.hasOptionalBytes());
+ assertTrue(newMsg.optionalNestedMessage.hasBb());
+ assertTrue(newMsg.hasOptionalNestedEnum());
+ assertTrue(newMsg.hasDefaultInt32());
+ assertTrue(newMsg.hasDefaultString());
+ assertTrue(newMsg.hasDefaultBytes());
+ assertTrue(newMsg.hasDefaultFloatNan());
+ assertTrue(newMsg.hasDefaultNestedEnum());
+ assertEquals(0, newMsg.getOptionalInt32());
+ assertEquals(0, newMsg.getOptionalString().length());
+ assertEquals(0, newMsg.getOptionalBytes().length);
+ assertEquals(0, newMsg.optionalNestedMessage.getBb());
+ assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum());
+ assertEquals(41, newMsg.getDefaultInt32());
+ assertEquals("hello", newMsg.getDefaultString());
+ assertEquals("world", new String(newMsg.getDefaultBytes(), "UTF-8"));
+ assertEquals(TestNanoAccessors.BAR, newMsg.getDefaultNestedEnum());
+ assertEquals(Float.NaN, newMsg.getDefaultFloatNan());
+ assertEquals(0, newMsg.id);
+ }
+
+ public void testNanoJavaEnumStyle() throws Exception {
+ EnumClassNanos.EnumClassNano msg = new EnumClassNanos.EnumClassNano();
+ assertEquals(EnumClassNanos.FileScopeEnum.ONE, msg.one);
+ assertEquals(EnumClassNanos.EnumClassNano.MessageScopeEnum.TWO, msg.two);
+
+ EnumClassNanoMultiple msg2 = new EnumClassNanoMultiple();
+ assertEquals(FileScopeEnumMultiple.THREE, msg2.three);
+ assertEquals(EnumClassNanoMultiple.MessageScopeEnumMultiple.FOUR, msg2.four);
+ }
+
+ /**
+ * Tests that fields with a default value of NaN are not serialized when
+ * set to NaN. This is a special case as NaN != NaN, so normal equality
+ * checks don't work.
+ */
+ public void testNanoNotANumberDefaults() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.defaultDoubleNan = 0;
+ msg.defaultFloatNan = 0;
+ byte[] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ assertTrue(result.length == msgSerializedSize);
+ assertTrue(msgSerializedSize > 3);
+
+ msg.defaultDoubleNan = Double.NaN;
+ msg.defaultFloatNan = Float.NaN;
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ assertEquals(3, result.length);
+ assertEquals(3, msgSerializedSize);
+ }
+
+ /**
+ * Test that a bug in skipRawBytes() has been fixed: if the skip skips
+ * exactly up to a limit, this should not break things.
+ */
+ public void testSkipRawBytesBug() throws Exception {
+ byte[] rawBytes = new byte[] { 1, 2 };
+ CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes);
+
+ int limit = input.pushLimit(1);
+ input.skipRawBytes(1);
+ input.popLimit(limit);
+ assertEquals(2, input.readRawByte());
+ }
+
+ /**
+ * Test that a bug in skipRawBytes() has been fixed: if the skip skips
+ * past the end of a buffer with a limit that has been set past the end of
+ * that buffer, this should not break things.
+ */
+ public void testSkipRawBytesPastEndOfBufferWithLimit() throws Exception {
+ byte[] rawBytes = new byte[] { 1, 2, 3, 4, 5 };
+ CodedInputByteBufferNano input = CodedInputByteBufferNano.newInstance(rawBytes);
+
+ int limit = input.pushLimit(4);
+ // In order to expose the bug we need to read at least one byte to prime the
+ // buffer inside the CodedInputStream.
+ assertEquals(1, input.readRawByte());
+ // Skip to the end of the limit.
+ input.skipRawBytes(3);
+ assertTrue(input.isAtEnd());
+ input.popLimit(limit);
+ assertEquals(5, input.readRawByte());
+ }
+
+ // Test a smattering of various proto types for printing
+ public void testMessageNanoPrinter() {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.optionalInt32 = 14;
+ msg.optionalFloat = 42.3f;
+ msg.optionalString = "String \"with' both quotes";
+ msg.optionalBytes = new byte[] {'"', '\0', 1, 8};
+ msg.optionalGroup = new TestAllTypesNano.OptionalGroup();
+ msg.optionalGroup.a = 15;
+ msg.repeatedInt64 = new long[2];
+ msg.repeatedInt64[0] = 1L;
+ msg.repeatedInt64[1] = -1L;
+ msg.repeatedBytes = new byte[2][];
+ msg.repeatedBytes[1] = new byte[] {'h', 'e', 'l', 'l', 'o'};
+ msg.repeatedGroup = new TestAllTypesNano.RepeatedGroup[2];
+ msg.repeatedGroup[0] = new TestAllTypesNano.RepeatedGroup();
+ msg.repeatedGroup[0].a = -27;
+ msg.repeatedGroup[1] = new TestAllTypesNano.RepeatedGroup();
+ msg.repeatedGroup[1].a = -72;
+ msg.optionalNestedMessage = new TestAllTypesNano.NestedMessage();
+ msg.optionalNestedMessage.bb = 7;
+ msg.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[2];
+ msg.repeatedNestedMessage[0] = new TestAllTypesNano.NestedMessage();
+ msg.repeatedNestedMessage[0].bb = 77;
+ msg.repeatedNestedMessage[1] = new TestAllTypesNano.NestedMessage();
+ msg.repeatedNestedMessage[1].bb = 88;
+ msg.optionalNestedEnum = TestAllTypesNano.BAZ;
+ msg.repeatedNestedEnum = new int[2];
+ msg.repeatedNestedEnum[0] = TestAllTypesNano.BAR;
+ msg.repeatedNestedEnum[1] = TestAllTypesNano.FOO;
+ msg.repeatedStringPiece = new String[] {null, "world"};
+
+ String protoPrint = msg.toString();
+ assertTrue(protoPrint.contains("optional_int32: 14"));
+ assertTrue(protoPrint.contains("optional_float: 42.3"));
+ assertTrue(protoPrint.contains("optional_double: 0.0"));
+ assertTrue(protoPrint.contains("optional_string: \"String \\u0022with\\u0027 both quotes\""));
+ assertTrue(protoPrint.contains("optional_bytes: \"\\\"\\000\\001\\010\""));
+ assertTrue(protoPrint.contains("optional_group <\n a: 15\n>"));
+
+ assertTrue(protoPrint.contains("repeated_int64: 1\nrepeated_int64: -1"));
+ assertFalse(protoPrint.contains("repeated_bytes: \"\"")); // null should be dropped
+ assertTrue(protoPrint.contains("repeated_bytes: \"hello\""));
+ assertTrue(protoPrint.contains("repeated_group <\n a: -27\n>\n"
+ + "repeated_group <\n a: -72\n>"));
+ assertTrue(protoPrint.contains("optional_nested_message <\n bb: 7\n>"));
+ assertTrue(protoPrint.contains("repeated_nested_message <\n bb: 77\n>\n"
+ + "repeated_nested_message <\n bb: 88\n>"));
+ assertTrue(protoPrint.contains("optional_nested_enum: 3"));
+ assertTrue(protoPrint.contains("repeated_nested_enum: 2\nrepeated_nested_enum: 1"));
+ assertTrue(protoPrint.contains("default_int32: 41"));
+ assertTrue(protoPrint.contains("default_string: \"hello\""));
+ assertFalse(protoPrint.contains("repeated_string_piece: \"\"")); // null should be dropped
+ assertTrue(protoPrint.contains("repeated_string_piece: \"world\""));
+ }
+
+ public void testMessageNanoPrinterAccessors() throws Exception {
+ TestNanoAccessors msg = new TestNanoAccessors();
+ msg.setOptionalInt32(13);
+ msg.setOptionalString("foo");
+ msg.setOptionalBytes(new byte[] {'"', '\0', 1, 8});
+ msg.optionalNestedMessage = new TestNanoAccessors.NestedMessage();
+ msg.optionalNestedMessage.setBb(7);
+ msg.setOptionalNestedEnum(TestNanoAccessors.BAZ);
+ msg.repeatedInt32 = new int[] { 1, -1 };
+ msg.repeatedString = new String[] { "Hello", "world" };
+ msg.repeatedBytes = new byte[2][];
+ msg.repeatedBytes[1] = new byte[] {'h', 'e', 'l', 'l', 'o'};
+ msg.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[2];
+ msg.repeatedNestedMessage[0] = new TestNanoAccessors.NestedMessage();
+ msg.repeatedNestedMessage[0].setBb(5);
+ msg.repeatedNestedMessage[1] = new TestNanoAccessors.NestedMessage();
+ msg.repeatedNestedMessage[1].setBb(6);
+ msg.repeatedNestedEnum = new int[] { TestNanoAccessors.FOO, TestNanoAccessors.BAR };
+ msg.id = 33;
+
+ String protoPrint = msg.toString();
+ assertTrue(protoPrint.contains("optional_int32: 13"));
+ assertTrue(protoPrint.contains("optional_string: \"foo\""));
+ assertTrue(protoPrint.contains("optional_bytes: \"\\\"\\000\\001\\010\""));
+ assertTrue(protoPrint.contains("optional_nested_message <\n bb: 7\n>"));
+ assertTrue(protoPrint.contains("optional_nested_enum: 3"));
+ assertTrue(protoPrint.contains("repeated_int32: 1\nrepeated_int32: -1"));
+ assertTrue(protoPrint.contains("repeated_string: \"Hello\"\nrepeated_string: \"world\""));
+ assertFalse(protoPrint.contains("repeated_bytes: \"\"")); // null should be dropped
+ assertTrue(protoPrint.contains("repeated_bytes: \"hello\""));
+ assertTrue(protoPrint.contains("repeated_nested_message <\n bb: 5\n>\n"
+ + "repeated_nested_message <\n bb: 6\n>"));
+ assertTrue(protoPrint.contains("repeated_nested_enum: 1\nrepeated_nested_enum: 2"));
+ assertTrue(protoPrint.contains("id: 33"));
+ }
+
+ public void testExtensions() throws Exception {
+ Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
+ message.field = 5;
+ int[] int32s = {1, 2};
+ int[] uint32s = {3, 4};
+ int[] sint32s = {-5, -6};
+ long[] int64s = {7, 8};
+ long[] uint64s = {9, 10};
+ long[] sint64s = {-11, -12};
+ int[] fixed32s = {13, 14};
+ int[] sfixed32s = {-15, -16};
+ long[] fixed64s = {17, 18};
+ long[] sfixed64s = {-19, -20};
+ boolean[] bools = {true, false};
+ float[] floats = {2.1f, 2.2f};
+ double[] doubles = {2.3, 2.4};
+ int[] enums = {Extensions.SECOND_VALUE, Extensions.FIRST_VALUE};
+ String[] strings = {"vijfentwintig", "twenty-six"};
+ byte[][] bytess = {{2, 7}, {2, 8}};
+ AnotherMessage another1 = new AnotherMessage();
+ another1.string = "er shi jiu";
+ another1.value = false;
+ AnotherMessage another2 = new AnotherMessage();
+ another2.string = "trente";
+ another2.value = true;
+ AnotherMessage[] messages = {another1, another2};
+ RepeatedExtensions.RepeatedGroup group1 = new RepeatedExtensions.RepeatedGroup();
+ group1.a = 31;
+ RepeatedExtensions.RepeatedGroup group2 = new RepeatedExtensions.RepeatedGroup();
+ group2.a = 32;
+ RepeatedExtensions.RepeatedGroup[] groups = {group1, group2};
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedInt32));
+ message.setExtension(RepeatedExtensions.repeatedInt32, int32s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedInt32));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedUint32));
+ message.setExtension(RepeatedExtensions.repeatedUint32, uint32s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedUint32));
+ message.setExtension(RepeatedExtensions.repeatedSint32, sint32s);
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedInt64));
+ message.setExtension(RepeatedExtensions.repeatedInt64, int64s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedInt64));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedUint64));
+ message.setExtension(RepeatedExtensions.repeatedUint64, uint64s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedUint64));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedSint64));
+ message.setExtension(RepeatedExtensions.repeatedSint64, sint64s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedSint64));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedFixed32));
+ message.setExtension(RepeatedExtensions.repeatedFixed32, fixed32s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedFixed32));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedSfixed32));
+ message.setExtension(RepeatedExtensions.repeatedSfixed32, sfixed32s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedSfixed32));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedFixed64));
+ message.setExtension(RepeatedExtensions.repeatedFixed64, fixed64s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedFixed64));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedSfixed64));
+ message.setExtension(RepeatedExtensions.repeatedSfixed64, sfixed64s);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedSfixed64));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedBool));
+ message.setExtension(RepeatedExtensions.repeatedBool, bools);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedBool));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedFloat));
+ message.setExtension(RepeatedExtensions.repeatedFloat, floats);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedFloat));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedDouble));
+ message.setExtension(RepeatedExtensions.repeatedDouble, doubles);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedDouble));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedEnum));
+ message.setExtension(RepeatedExtensions.repeatedEnum, enums);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedEnum));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedString));
+ message.setExtension(RepeatedExtensions.repeatedString, strings);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedString));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedBytes));
+ message.setExtension(RepeatedExtensions.repeatedBytes, bytess);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedBytes));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedMessage));
+ message.setExtension(RepeatedExtensions.repeatedMessage, messages);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedMessage));
+ assertFalse(message.hasExtension(RepeatedExtensions.repeatedGroup));
+ message.setExtension(RepeatedExtensions.repeatedGroup, groups);
+ assertTrue(message.hasExtension(RepeatedExtensions.repeatedGroup));
+
+ byte[] data = MessageNano.toByteArray(message);
+ message = Extensions.ExtendableMessage.parseFrom(data);
+ assertEquals(5, message.field);
+
+ // Test reading back using SingularExtensions: the retrieved value should equal the last
+ // in each array.
+ assertEquals(int32s[1], (int) message.getExtension(SingularExtensions.someInt32));
+ assertEquals(uint32s[1], (int) message.getExtension(SingularExtensions.someUint32));
+ assertEquals(sint32s[1], (int) message.getExtension(SingularExtensions.someSint32));
+ assertEquals(int64s[1], (long) message.getExtension(SingularExtensions.someInt64));
+ assertEquals(uint64s[1], (long) message.getExtension(SingularExtensions.someUint64));
+ assertEquals(sint64s[1], (long) message.getExtension(SingularExtensions.someSint64));
+ assertEquals(fixed32s[1], (int) message.getExtension(SingularExtensions.someFixed32));
+ assertEquals(sfixed32s[1], (int) message.getExtension(SingularExtensions.someSfixed32));
+ assertEquals(fixed64s[1], (long) message.getExtension(SingularExtensions.someFixed64));
+ assertEquals(sfixed64s[1], (long) message.getExtension(SingularExtensions.someSfixed64));
+ assertEquals(bools[1], (boolean) message.getExtension(SingularExtensions.someBool));
+ assertEquals(floats[1], (float) message.getExtension(SingularExtensions.someFloat));
+ assertEquals(doubles[1], (double) message.getExtension(SingularExtensions.someDouble));
+ assertEquals(enums[1], (int) message.getExtension(SingularExtensions.someEnum));
+ assertEquals(strings[1], message.getExtension(SingularExtensions.someString));
+ assertTrue(Arrays.equals(bytess[1], message.getExtension(SingularExtensions.someBytes)));
+ AnotherMessage deserializedMessage = message.getExtension(SingularExtensions.someMessage);
+ assertEquals(another2.string, deserializedMessage.string);
+ assertEquals(another2.value, deserializedMessage.value);
+ assertEquals(group2.a, message.getExtension(SingularExtensions.someGroup).a);
+
+ // Test reading back using RepeatedExtensions: the arrays should be equal.
+ message = Extensions.ExtendableMessage.parseFrom(data);
+ assertEquals(5, message.field);
+ assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
+ assertTrue(Arrays.equals(strings, message.getExtension(RepeatedExtensions.repeatedString)));
+ byte[][] deserializedRepeatedBytes = message.getExtension(RepeatedExtensions.repeatedBytes);
+ assertEquals(2, deserializedRepeatedBytes.length);
+ assertTrue(Arrays.equals(bytess[0], deserializedRepeatedBytes[0]));
+ assertTrue(Arrays.equals(bytess[1], deserializedRepeatedBytes[1]));
+ AnotherMessage[] deserializedRepeatedMessage =
+ message.getExtension(RepeatedExtensions.repeatedMessage);
+ assertEquals(2, deserializedRepeatedMessage.length);
+ assertEquals(another1.string, deserializedRepeatedMessage[0].string);
+ assertEquals(another1.value, deserializedRepeatedMessage[0].value);
+ assertEquals(another2.string, deserializedRepeatedMessage[1].string);
+ assertEquals(another2.value, deserializedRepeatedMessage[1].value);
+ RepeatedExtensions.RepeatedGroup[] deserializedRepeatedGroup =
+ message.getExtension(RepeatedExtensions.repeatedGroup);
+ assertEquals(2, deserializedRepeatedGroup.length);
+ assertEquals(group1.a, deserializedRepeatedGroup[0].a);
+ assertEquals(group2.a, deserializedRepeatedGroup[1].a);
+
+ message = Extensions.ExtendableMessage.parseFrom(data);
+ assertEquals(5, message.field);
+ // Test hasExtension using PackedExtensions.
+ assertTrue(message.hasExtension(PackedExtensions.packedInt32));
+ assertTrue(message.hasExtension(PackedExtensions.packedUint32));
+ assertTrue(message.hasExtension(PackedExtensions.packedSint32));
+ assertTrue(message.hasExtension(PackedExtensions.packedInt64));
+ assertTrue(message.hasExtension(PackedExtensions.packedUint64));
+ assertTrue(message.hasExtension(PackedExtensions.packedSint64));
+ assertTrue(message.hasExtension(PackedExtensions.packedFixed32));
+ assertTrue(message.hasExtension(PackedExtensions.packedSfixed32));
+ assertTrue(message.hasExtension(PackedExtensions.packedFixed64));
+ assertTrue(message.hasExtension(PackedExtensions.packedSfixed64));
+ assertTrue(message.hasExtension(PackedExtensions.packedBool));
+ assertTrue(message.hasExtension(PackedExtensions.packedFloat));
+ assertTrue(message.hasExtension(PackedExtensions.packedDouble));
+ assertTrue(message.hasExtension(PackedExtensions.packedEnum));
+
+ // Test reading back using PackedExtensions: the arrays should be equal, even the fields
+ // are non-packed.
+ assertTrue(Arrays.equals(int32s, message.getExtension(PackedExtensions.packedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(PackedExtensions.packedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(PackedExtensions.packedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(PackedExtensions.packedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(PackedExtensions.packedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(PackedExtensions.packedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(PackedExtensions.packedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(PackedExtensions.packedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(PackedExtensions.packedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(PackedExtensions.packedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(PackedExtensions.packedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(PackedExtensions.packedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(PackedExtensions.packedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(PackedExtensions.packedEnum)));
+
+ // Now set the packable extension values using PackedExtensions so they're serialized packed.
+ message.setExtension(PackedExtensions.packedInt32, int32s);
+ message.setExtension(PackedExtensions.packedUint32, uint32s);
+ message.setExtension(PackedExtensions.packedSint32, sint32s);
+ message.setExtension(PackedExtensions.packedInt64, int64s);
+ message.setExtension(PackedExtensions.packedUint64, uint64s);
+ message.setExtension(PackedExtensions.packedSint64, sint64s);
+ message.setExtension(PackedExtensions.packedFixed32, fixed32s);
+ message.setExtension(PackedExtensions.packedSfixed32, sfixed32s);
+ message.setExtension(PackedExtensions.packedFixed64, fixed64s);
+ message.setExtension(PackedExtensions.packedSfixed64, sfixed64s);
+ message.setExtension(PackedExtensions.packedBool, bools);
+ message.setExtension(PackedExtensions.packedFloat, floats);
+ message.setExtension(PackedExtensions.packedDouble, doubles);
+ message.setExtension(PackedExtensions.packedEnum, enums);
+
+ // And read back using non-packed RepeatedExtensions.
+ byte[] data2 = MessageNano.toByteArray(message);
+ message = MessageNano.mergeFrom(new Extensions.ExtendableMessage(), data2);
+ assertTrue(Arrays.equals(int32s, message.getExtension(RepeatedExtensions.repeatedInt32)));
+ assertTrue(Arrays.equals(uint32s, message.getExtension(RepeatedExtensions.repeatedUint32)));
+ assertTrue(Arrays.equals(sint32s, message.getExtension(RepeatedExtensions.repeatedSint32)));
+ assertTrue(Arrays.equals(int64s, message.getExtension(RepeatedExtensions.repeatedInt64)));
+ assertTrue(Arrays.equals(uint64s, message.getExtension(RepeatedExtensions.repeatedUint64)));
+ assertTrue(Arrays.equals(sint64s, message.getExtension(RepeatedExtensions.repeatedSint64)));
+ assertTrue(Arrays.equals(fixed32s, message.getExtension(RepeatedExtensions.repeatedFixed32)));
+ assertTrue(Arrays.equals(sfixed32s, message.getExtension(RepeatedExtensions.repeatedSfixed32)));
+ assertTrue(Arrays.equals(fixed64s, message.getExtension(RepeatedExtensions.repeatedFixed64)));
+ assertTrue(Arrays.equals(sfixed64s, message.getExtension(RepeatedExtensions.repeatedSfixed64)));
+ assertTrue(Arrays.equals(bools, message.getExtension(RepeatedExtensions.repeatedBool)));
+ assertTrue(Arrays.equals(floats, message.getExtension(RepeatedExtensions.repeatedFloat)));
+ assertTrue(Arrays.equals(doubles, message.getExtension(RepeatedExtensions.repeatedDouble)));
+ assertTrue(Arrays.equals(enums, message.getExtension(RepeatedExtensions.repeatedEnum)));
+ }
+
+ public void testNullExtensions() throws Exception {
+ // Check that clearing the extension on an empty message is a no-op.
+ Extensions.ExtendableMessage message = new Extensions.ExtendableMessage();
+ assertFalse(message.hasExtension(SingularExtensions.someMessage));
+ message.setExtension(SingularExtensions.someMessage, null);
+ assertFalse(message.hasExtension(SingularExtensions.someMessage));
+ assertEquals(0, MessageNano.toByteArray(message).length);
+
+ // Check that the message is empty after setting and clearing an extension.
+ AnotherMessage another = new AnotherMessage();
+ assertFalse(message.hasExtension(SingularExtensions.someMessage));
+ message.setExtension(SingularExtensions.someMessage, another);
+ assertTrue(message.hasExtension(SingularExtensions.someMessage));
+ assertTrue(MessageNano.toByteArray(message).length > 0);
+ message.setExtension(SingularExtensions.someMessage, null);
+ assertFalse(message.hasExtension(SingularExtensions.someMessage));
+ assertEquals(0, MessageNano.toByteArray(message).length);
+ }
+
+ public void testExtensionsMutation() {
+ Extensions.ExtendableMessage extendableMessage = new Extensions.ExtendableMessage();
+ extendableMessage.setExtension(SingularExtensions.someMessage,
+ new Extensions.AnotherMessage());
+
+ extendableMessage.getExtension(SingularExtensions.someMessage).string = "not empty";
+
+ assertEquals("not empty",
+ extendableMessage.getExtension(SingularExtensions.someMessage).string);
+ }
+
+ public void testExtensionsMutation_Equals() throws InvalidProtocolBufferNanoException {
+ Extensions.ExtendableMessage extendableMessage = new Extensions.ExtendableMessage();
+ extendableMessage.field = 5;
+ int int32 = 42;
+ int[] uint32s = {3, 4};
+ int[] sint32s = {-5, -6};
+ long[] int64s = {7, 8};
+ long[] uint64s = {9, 10};
+ long[] sint64s = {-11, -12};
+ int[] fixed32s = {13, 14};
+ int[] sfixed32s = {-15, -16};
+ long[] fixed64s = {17, 18};
+ long[] sfixed64s = {-19, -20};
+ boolean[] bools = {true, false};
+ float[] floats = {2.1f, 2.2f};
+ double[] doubles = {2.3, 2.4};
+ int[] enums = {Extensions.SECOND_VALUE, Extensions.FIRST_VALUE};
+ String[] strings = {"vijfentwintig", "twenty-six"};
+ byte[][] bytess = {{2, 7}, {2, 8}};
+ AnotherMessage another1 = new AnotherMessage();
+ another1.string = "er shi jiu";
+ another1.value = false;
+ AnotherMessage another2 = new AnotherMessage();
+ another2.string = "trente";
+ another2.value = true;
+ AnotherMessage[] messages = {another1, another2};
+ RepeatedExtensions.RepeatedGroup group1 = new RepeatedExtensions.RepeatedGroup();
+ group1.a = 31;
+ RepeatedExtensions.RepeatedGroup group2 = new RepeatedExtensions.RepeatedGroup();
+ group2.a = 32;
+ RepeatedExtensions.RepeatedGroup[] groups = {group1, group2};
+ extendableMessage.setExtension(SingularExtensions.someInt32, int32);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedUint32, uint32s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedSint32, sint32s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedInt64, int64s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedUint64, uint64s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedSint64, sint64s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedFixed32, fixed32s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedSfixed32, sfixed32s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedFixed64, fixed64s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedSfixed64, sfixed64s);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedBool, bools);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedFloat, floats);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedDouble, doubles);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedEnum, enums);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedString, strings);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedBytes, bytess);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedMessage, messages);
+ extendableMessage.setExtension(RepeatedExtensions.repeatedGroup, groups);
+
+ byte[] data = MessageNano.toByteArray(extendableMessage);
+
+ extendableMessage = Extensions.ExtendableMessage.parseFrom(data);
+ Extensions.ExtendableMessage messageCopy = Extensions.ExtendableMessage.parseFrom(data);
+
+ // Without deserialising.
+ assertEquals(extendableMessage, messageCopy);
+ assertEquals(extendableMessage.hashCode(), messageCopy.hashCode());
+
+ // Only one deserialized.
+ extendableMessage.getExtension(SingularExtensions.someInt32);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedUint32);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedSint32);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedInt64);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedUint64);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedSint64);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedFixed32);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedSfixed32);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedFixed64);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedSfixed64);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedBool);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedFloat);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedDouble);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedEnum);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedString);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedBytes);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedMessage);
+ extendableMessage.getExtension(RepeatedExtensions.repeatedGroup);
+ assertEquals(extendableMessage, messageCopy);
+ assertEquals(extendableMessage.hashCode(), messageCopy.hashCode());
+
+ // Both deserialized.
+ messageCopy.getExtension(SingularExtensions.someInt32);
+ messageCopy.getExtension(RepeatedExtensions.repeatedUint32);
+ messageCopy.getExtension(RepeatedExtensions.repeatedSint32);
+ messageCopy.getExtension(RepeatedExtensions.repeatedInt64);
+ messageCopy.getExtension(RepeatedExtensions.repeatedUint64);
+ messageCopy.getExtension(RepeatedExtensions.repeatedSint64);
+ messageCopy.getExtension(RepeatedExtensions.repeatedFixed32);
+ messageCopy.getExtension(RepeatedExtensions.repeatedSfixed32);
+ messageCopy.getExtension(RepeatedExtensions.repeatedFixed64);
+ messageCopy.getExtension(RepeatedExtensions.repeatedSfixed64);
+ messageCopy.getExtension(RepeatedExtensions.repeatedBool);
+ messageCopy.getExtension(RepeatedExtensions.repeatedFloat);
+ messageCopy.getExtension(RepeatedExtensions.repeatedDouble);
+ messageCopy.getExtension(RepeatedExtensions.repeatedEnum);
+ messageCopy.getExtension(RepeatedExtensions.repeatedString);
+ messageCopy.getExtension(RepeatedExtensions.repeatedBytes);
+ messageCopy.getExtension(RepeatedExtensions.repeatedMessage);
+ messageCopy.getExtension(RepeatedExtensions.repeatedGroup);
+ assertEquals(extendableMessage, messageCopy);
+ assertEquals(extendableMessage.hashCode(), messageCopy.hashCode());
+
+ // Change one, make sure they are still different.
+ messageCopy.getExtension(RepeatedExtensions.repeatedMessage)[0].string = "not empty";
+ assertFalse(extendableMessage.equals(messageCopy));
+
+ // Even if the extension hasn't been deserialized.
+ extendableMessage = Extensions.ExtendableMessage.parseFrom(data);
+ assertFalse(extendableMessage.equals(messageCopy));
+ }
+
+ public void testExtensionsCaching() {
+ Extensions.ExtendableMessage extendableMessage = new Extensions.ExtendableMessage();
+ extendableMessage.setExtension(SingularExtensions.someMessage,
+ new Extensions.AnotherMessage());
+ assertSame("Consecutive calls to getExtensions should return the same object",
+ extendableMessage.getExtension(SingularExtensions.someMessage),
+ extendableMessage.getExtension(SingularExtensions.someMessage));
+ }
+
+ public void testUnknownFields() throws Exception {
+ // Check that we roundtrip (serialize and deserialize) unrecognized fields.
+ AnotherMessage message = new AnotherMessage();
+ message.string = "Hello World";
+ message.value = false;
+
+ byte[] bytes = MessageNano.toByteArray(message);
+ int extraFieldSize = CodedOutputByteBufferNano.computeStringSize(
+ 1001, "This is an unknown field");
+ byte[] newBytes = new byte[bytes.length + extraFieldSize];
+ System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
+ CodedOutputByteBufferNano.newInstance(newBytes, bytes.length, extraFieldSize)
+ .writeString(1001, "This is an unknown field");
+
+ // Deserialize with an unknown field.
+ AnotherMessage deserialized = AnotherMessage.parseFrom(newBytes);
+ byte[] serialized = MessageNano.toByteArray(deserialized);
+
+ assertEquals(newBytes.length, serialized.length);
+
+ // Clear, and make sure it clears everything.
+ deserialized.clear();
+ assertEquals(0, MessageNano.toByteArray(deserialized).length);
+ }
+
+ public void testMergeFrom() throws Exception {
+ SimpleMessageNano message = new SimpleMessageNano();
+ message.d = 123;
+ byte[] bytes = MessageNano.toByteArray(message);
+
+ SimpleMessageNano newMessage = MessageNano.mergeFrom(new SimpleMessageNano(), bytes);
+ assertEquals(message.d, newMessage.d);
+ }
+
+ public void testJavaKeyword() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.synchronized_ = 123;
+ assertEquals(123, msg.synchronized_);
+ }
+
+ public void testReferenceTypesForPrimitives() throws Exception {
+ NanoReferenceTypes.TestAllTypesNano message = new NanoReferenceTypes.TestAllTypesNano();
+
+ // Base check - when nothing is set, we serialize nothing.
+ assertHasWireData(message, false);
+
+ message.defaultBool = true;
+ assertHasWireData(message, true);
+
+ message.defaultBool = false;
+ assertHasWireData(message, true);
+
+ message.defaultBool = null;
+ assertHasWireData(message, false);
+
+ message.defaultInt32 = 5;
+ assertHasWireData(message, true);
+
+ message.defaultInt32 = null;
+ assertHasWireData(message, false);
+
+ message.defaultInt64 = 123456L;
+ assertHasWireData(message, true);
+
+ message.defaultInt64 = null;
+ assertHasWireData(message, false);
+
+ message.defaultFloat = 1f;
+ assertHasWireData(message, true);
+
+ message.defaultFloat = null;
+ assertHasWireData(message, false);
+
+ message.defaultDouble = 2.1;
+ assertHasWireData(message, true);
+
+ message.defaultDouble = null;
+ assertHasWireData(message, false);
+
+ message.defaultString = "hello";
+ assertHasWireData(message, true);
+
+ message.defaultString = null;
+ assertHasWireData(message, false);
+
+ message.defaultBytes = new byte[] { 1, 2, 3 };
+ assertHasWireData(message, true);
+
+ message.defaultBytes = null;
+ assertHasWireData(message, false);
+ }
+
+ public void testHashCodeEquals() throws Exception {
+ // Complete equality:
+ TestAllTypesNano a = createMessageForHashCodeEqualsTest();
+ TestAllTypesNano aEquivalent = createMessageForHashCodeEqualsTest();
+
+ assertTrue(MessageNano.messageNanoEquals(a, aEquivalent));
+ assertFalse(MessageNano.messageNanoEquals(a, new TestAllTypesNano()));
+
+ // Null and empty array for repeated fields equality:
+ TestAllTypesNano b = createMessageForHashCodeEqualsTest();
+ b.repeatedBool = null;
+ b.repeatedFloat = new float[0];
+ TestAllTypesNano bEquivalent = createMessageForHashCodeEqualsTest();
+ bEquivalent.repeatedBool = new boolean[0];
+ bEquivalent.repeatedFloat = null;
+
+ // Ref-element-type repeated fields use non-null subsequence equality:
+ TestAllTypesNano c = createMessageForHashCodeEqualsTest();
+ c.repeatedString = null;
+ c.repeatedStringPiece = new String[] {null, "one", null, "two"};
+ c.repeatedBytes = new byte[][] {{3, 4}, null};
+ TestAllTypesNano cEquivalent = createMessageForHashCodeEqualsTest();
+ cEquivalent.repeatedString = new String[3];
+ cEquivalent.repeatedStringPiece = new String[] {"one", "two", null};
+ cEquivalent.repeatedBytes = new byte[][] {{3, 4}};
+
+ // Complete equality for messages with has fields:
+ TestAllTypesNanoHas d = createMessageWithHasForHashCodeEqualsTest();
+ TestAllTypesNanoHas dEquivalent = createMessageWithHasForHashCodeEqualsTest();
+
+ // If has-fields exist, fields with the same default values but
+ // different has-field values are different.
+ TestAllTypesNanoHas e = createMessageWithHasForHashCodeEqualsTest();
+ e.optionalInt32++; // make different from d
+ e.hasDefaultString = false;
+ TestAllTypesNanoHas eDifferent = createMessageWithHasForHashCodeEqualsTest();
+ eDifferent.optionalInt32 = e.optionalInt32;
+ eDifferent.hasDefaultString = true;
+
+ // Complete equality for messages with accessors:
+ TestNanoAccessors f = createMessageWithAccessorsForHashCodeEqualsTest();
+ TestNanoAccessors fEquivalent = createMessageWithAccessorsForHashCodeEqualsTest();
+
+ // If using accessors, explicitly setting a field to its default value
+ // should make the message different.
+ TestNanoAccessors g = createMessageWithAccessorsForHashCodeEqualsTest();
+ g.setOptionalInt32(g.getOptionalInt32() + 1); // make different from f
+ g.clearDefaultString();
+ TestNanoAccessors gDifferent = createMessageWithAccessorsForHashCodeEqualsTest();
+ gDifferent.setOptionalInt32(g.getOptionalInt32());
+ gDifferent.setDefaultString(g.getDefaultString());
+
+ // Complete equality for reference typed messages:
+ NanoReferenceTypes.TestAllTypesNano h = createRefTypedMessageForHashCodeEqualsTest();
+ NanoReferenceTypes.TestAllTypesNano hEquivalent = createRefTypedMessageForHashCodeEqualsTest();
+
+ // Inequality of null and default value for reference typed messages:
+ NanoReferenceTypes.TestAllTypesNano i = createRefTypedMessageForHashCodeEqualsTest();
+ i.optionalInt32 = 1; // make different from h
+ i.optionalFloat = null;
+ NanoReferenceTypes.TestAllTypesNano iDifferent = createRefTypedMessageForHashCodeEqualsTest();
+ iDifferent.optionalInt32 = i.optionalInt32;
+ iDifferent.optionalFloat = 0.0f;
+
+ HashMap<MessageNano, String> hashMap = new HashMap<MessageNano, String>();
+ hashMap.put(a, "a");
+ hashMap.put(b, "b");
+ hashMap.put(c, "c");
+ hashMap.put(d, "d");
+ hashMap.put(e, "e");
+ hashMap.put(f, "f");
+ hashMap.put(g, "g");
+ hashMap.put(h, "h");
+ hashMap.put(i, "i");
+
+ assertEquals(9, hashMap.size()); // a-i should be different from each other.
+
+ assertEquals("a", hashMap.get(a));
+ assertEquals("a", hashMap.get(aEquivalent));
+
+ assertEquals("b", hashMap.get(b));
+ assertEquals("b", hashMap.get(bEquivalent));
+
+ assertEquals("c", hashMap.get(c));
+ assertEquals("c", hashMap.get(cEquivalent));
+
+ assertEquals("d", hashMap.get(d));
+ assertEquals("d", hashMap.get(dEquivalent));
+
+ assertEquals("e", hashMap.get(e));
+ assertNull(hashMap.get(eDifferent));
+
+ assertEquals("f", hashMap.get(f));
+ assertEquals("f", hashMap.get(fEquivalent));
+
+ assertEquals("g", hashMap.get(g));
+ assertNull(hashMap.get(gDifferent));
+
+ assertEquals("h", hashMap.get(h));
+ assertEquals("h", hashMap.get(hEquivalent));
+
+ assertEquals("i", hashMap.get(i));
+ assertNull(hashMap.get(iDifferent));
+ }
+
+ private TestAllTypesNano createMessageForHashCodeEqualsTest() {
+ TestAllTypesNano message = new TestAllTypesNano();
+ message.optionalInt32 = 5;
+ message.optionalInt64 = 777;
+ message.optionalFloat = 1.0f;
+ message.optionalDouble = 2.0;
+ message.optionalBool = true;
+ message.optionalString = "Hello";
+ message.optionalBytes = new byte[] { 1, 2, 3 };
+ message.optionalNestedMessage = new TestAllTypesNano.NestedMessage();
+ message.optionalNestedMessage.bb = 27;
+ message.optionalNestedEnum = TestAllTypesNano.BAR;
+ message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
+ message.repeatedInt64 = new long[] { 27L, 28L, 29L };
+ message.repeatedFloat = new float[] { 5.0f, 6.0f };
+ message.repeatedDouble = new double[] { 99.1, 22.5 };
+ message.repeatedBool = new boolean[] { true, false, true };
+ message.repeatedString = new String[] { "One", "Two" };
+ message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
+ message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
+ message.optionalNestedMessage,
+ message.optionalNestedMessage
+ };
+ message.repeatedNestedEnum = new int[] {
+ TestAllTypesNano.BAR,
+ TestAllTypesNano.BAZ
+ };
+ return message;
+ }
+
+ private TestAllTypesNanoHas createMessageWithHasForHashCodeEqualsTest() {
+ TestAllTypesNanoHas message = new TestAllTypesNanoHas();
+ message.optionalInt32 = 5;
+ message.optionalString = "Hello";
+ message.optionalBytes = new byte[] { 1, 2, 3 };
+ message.optionalNestedMessage = new TestAllTypesNanoHas.NestedMessage();
+ message.optionalNestedMessage.bb = 27;
+ message.optionalNestedEnum = TestAllTypesNano.BAR;
+ message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
+ message.repeatedString = new String[] { "One", "Two" };
+ message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
+ message.repeatedNestedMessage = new TestAllTypesNanoHas.NestedMessage[] {
+ message.optionalNestedMessage,
+ message.optionalNestedMessage
+ };
+ message.repeatedNestedEnum = new int[] {
+ TestAllTypesNano.BAR,
+ TestAllTypesNano.BAZ
+ };
+ return message;
+ }
+
+ private TestNanoAccessors createMessageWithAccessorsForHashCodeEqualsTest() {
+ TestNanoAccessors message = new TestNanoAccessors()
+ .setOptionalInt32(5)
+ .setOptionalString("Hello")
+ .setOptionalBytes(new byte[] {1, 2, 3})
+ .setOptionalNestedEnum(TestNanoAccessors.BAR);
+ message.optionalNestedMessage = new TestNanoAccessors.NestedMessage().setBb(27);
+ message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
+ message.repeatedString = new String[] { "One", "Two" };
+ message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
+ message.repeatedNestedMessage = new TestNanoAccessors.NestedMessage[] {
+ message.optionalNestedMessage,
+ message.optionalNestedMessage
+ };
+ message.repeatedNestedEnum = new int[] {
+ TestAllTypesNano.BAR,
+ TestAllTypesNano.BAZ
+ };
+ return message;
+ }
+
+ private NanoReferenceTypes.TestAllTypesNano createRefTypedMessageForHashCodeEqualsTest() {
+ NanoReferenceTypes.TestAllTypesNano message = new NanoReferenceTypes.TestAllTypesNano();
+ message.optionalInt32 = 5;
+ message.optionalInt64 = 777L;
+ message.optionalFloat = 1.0f;
+ message.optionalDouble = 2.0;
+ message.optionalBool = true;
+ message.optionalString = "Hello";
+ message.optionalBytes = new byte[] { 1, 2, 3 };
+ message.optionalNestedMessage =
+ new NanoReferenceTypes.TestAllTypesNano.NestedMessage();
+ message.optionalNestedMessage.foo = 27;
+ message.optionalNestedEnum = NanoReferenceTypes.TestAllTypesNano.BAR;
+ message.repeatedInt32 = new int[] { 5, 6, 7, 8 };
+ message.repeatedInt64 = new long[] { 27L, 28L, 29L };
+ message.repeatedFloat = new float[] { 5.0f, 6.0f };
+ message.repeatedDouble = new double[] { 99.1, 22.5 };
+ message.repeatedBool = new boolean[] { true, false, true };
+ message.repeatedString = new String[] { "One", "Two" };
+ message.repeatedBytes = new byte[][] { { 2, 7 }, { 2, 7 } };
+ message.repeatedNestedMessage =
+ new NanoReferenceTypes.TestAllTypesNano.NestedMessage[] {
+ message.optionalNestedMessage,
+ message.optionalNestedMessage
+ };
+ message.repeatedNestedEnum = new int[] {
+ NanoReferenceTypes.TestAllTypesNano.BAR,
+ NanoReferenceTypes.TestAllTypesNano.BAZ
+ };
+ return message;
+ }
+
+ public void testEqualsWithSpecialFloatingPointValues() throws Exception {
+ // Checks that the nano implementation complies with Object.equals() when treating
+ // floating point numbers, i.e. NaN == NaN and +0.0 != -0.0.
+ // This test assumes that the generated equals() implementations are symmetric, so
+ // there will only be one direction for each equality check.
+
+ TestAllTypesNano m1 = new TestAllTypesNano();
+ m1.optionalFloat = Float.NaN;
+ m1.optionalDouble = Double.NaN;
+ TestAllTypesNano m2 = new TestAllTypesNano();
+ m2.optionalFloat = Float.NaN;
+ m2.optionalDouble = Double.NaN;
+ assertTrue(m1.equals(m2));
+ assertTrue(m1.equals(
+ MessageNano.mergeFrom(new TestAllTypesNano(), MessageNano.toByteArray(m1))));
+
+ m1.optionalFloat = +0f;
+ m2.optionalFloat = -0f;
+ assertFalse(m1.equals(m2));
+
+ m1.optionalFloat = -0f;
+ m1.optionalDouble = +0d;
+ m2.optionalDouble = -0d;
+ assertFalse(m1.equals(m2));
+
+ m1.optionalDouble = -0d;
+ assertTrue(m1.equals(m2));
+ assertFalse(m1.equals(new TestAllTypesNano())); // -0 does not equals() the default +0
+ assertTrue(m1.equals(
+ MessageNano.mergeFrom(new TestAllTypesNano(), MessageNano.toByteArray(m1))));
+
+ // -------
+
+ TestAllTypesNanoHas m3 = new TestAllTypesNanoHas();
+ m3.optionalFloat = Float.NaN;
+ m3.hasOptionalFloat = true;
+ m3.optionalDouble = Double.NaN;
+ m3.hasOptionalDouble = true;
+ TestAllTypesNanoHas m4 = new TestAllTypesNanoHas();
+ m4.optionalFloat = Float.NaN;
+ m4.hasOptionalFloat = true;
+ m4.optionalDouble = Double.NaN;
+ m4.hasOptionalDouble = true;
+ assertTrue(m3.equals(m4));
+ assertTrue(m3.equals(
+ MessageNano.mergeFrom(new TestAllTypesNanoHas(), MessageNano.toByteArray(m3))));
+
+ m3.optionalFloat = +0f;
+ m4.optionalFloat = -0f;
+ assertFalse(m3.equals(m4));
+
+ m3.optionalFloat = -0f;
+ m3.optionalDouble = +0d;
+ m4.optionalDouble = -0d;
+ assertFalse(m3.equals(m4));
+
+ m3.optionalDouble = -0d;
+ m3.hasOptionalFloat = false; // -0 does not equals() the default +0,
+ m3.hasOptionalDouble = false; // so these incorrect 'has' flags should be disregarded.
+ assertTrue(m3.equals(m4)); // note: m4 has the 'has' flags set.
+ assertFalse(m3.equals(new TestAllTypesNanoHas())); // note: the new message has +0 defaults
+ assertTrue(m3.equals(
+ MessageNano.mergeFrom(new TestAllTypesNanoHas(), MessageNano.toByteArray(m3))));
+ // note: the deserialized message has the 'has' flags set.
+
+ // -------
+
+ TestNanoAccessors m5 = new TestNanoAccessors();
+ m5.setOptionalFloat(Float.NaN);
+ m5.setOptionalDouble(Double.NaN);
+ TestNanoAccessors m6 = new TestNanoAccessors();
+ m6.setOptionalFloat(Float.NaN);
+ m6.setOptionalDouble(Double.NaN);
+ assertTrue(m5.equals(m6));
+ assertTrue(m5.equals(
+ MessageNano.mergeFrom(new TestNanoAccessors(), MessageNano.toByteArray(m6))));
+
+ m5.setOptionalFloat(+0f);
+ m6.setOptionalFloat(-0f);
+ assertFalse(m5.equals(m6));
+
+ m5.setOptionalFloat(-0f);
+ m5.setOptionalDouble(+0d);
+ m6.setOptionalDouble(-0d);
+ assertFalse(m5.equals(m6));
+
+ m5.setOptionalDouble(-0d);
+ assertTrue(m5.equals(m6));
+ assertFalse(m5.equals(new TestNanoAccessors()));
+ assertTrue(m5.equals(
+ MessageNano.mergeFrom(new TestNanoAccessors(), MessageNano.toByteArray(m6))));
+
+ // -------
+
+ NanoReferenceTypes.TestAllTypesNano m7 = new NanoReferenceTypes.TestAllTypesNano();
+ m7.optionalFloat = Float.NaN;
+ m7.optionalDouble = Double.NaN;
+ NanoReferenceTypes.TestAllTypesNano m8 = new NanoReferenceTypes.TestAllTypesNano();
+ m8.optionalFloat = Float.NaN;
+ m8.optionalDouble = Double.NaN;
+ assertTrue(m7.equals(m8));
+ assertTrue(m7.equals(MessageNano.mergeFrom(
+ new NanoReferenceTypes.TestAllTypesNano(), MessageNano.toByteArray(m7))));
+
+ m7.optionalFloat = +0f;
+ m8.optionalFloat = -0f;
+ assertFalse(m7.equals(m8));
+
+ m7.optionalFloat = -0f;
+ m7.optionalDouble = +0d;
+ m8.optionalDouble = -0d;
+ assertFalse(m7.equals(m8));
+
+ m7.optionalDouble = -0d;
+ assertTrue(m7.equals(m8));
+ assertFalse(m7.equals(new NanoReferenceTypes.TestAllTypesNano()));
+ assertTrue(m7.equals(MessageNano.mergeFrom(
+ new NanoReferenceTypes.TestAllTypesNano(), MessageNano.toByteArray(m7))));
+ }
+
+ public void testNullRepeatedFields() throws Exception {
+ // Check that serialization after explicitly setting a repeated field
+ // to null doesn't NPE.
+ TestAllTypesNano message = new TestAllTypesNano();
+ message.repeatedInt32 = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message.repeatedNestedEnum = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message.repeatedBytes = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message.repeatedNestedMessage = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message.repeatedPackedInt32 = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ message.repeatedPackedNestedEnum = null;
+ MessageNano.toByteArray(message); // should not NPE
+ message.toString(); // should not NPE
+
+ // Create a second message to merge into message.
+ TestAllTypesNano secondMessage = new TestAllTypesNano();
+ secondMessage.repeatedInt32 = new int[] {1, 2, 3};
+ secondMessage.repeatedNestedEnum = new int[] {
+ TestAllTypesNano.FOO, TestAllTypesNano.BAR
+ };
+ secondMessage.repeatedBytes = new byte[][] {{1, 2}, {3, 4}};
+ TestAllTypesNano.NestedMessage nested =
+ new TestAllTypesNano.NestedMessage();
+ nested.bb = 55;
+ secondMessage.repeatedNestedMessage =
+ new TestAllTypesNano.NestedMessage[] {nested};
+ secondMessage.repeatedPackedInt32 = new int[] {1, 2, 3};
+ secondMessage.repeatedPackedNestedEnum = new int[] {
+ TestAllTypesNano.FOO, TestAllTypesNano.BAR
+ };
+
+ // Should not NPE
+ message.mergeFrom(CodedInputByteBufferNano.newInstance(
+ MessageNano.toByteArray(secondMessage)));
+ assertEquals(3, message.repeatedInt32.length);
+ assertEquals(3, message.repeatedInt32[2]);
+ assertEquals(2, message.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.FOO, message.repeatedNestedEnum[0]);
+ assertEquals(2, message.repeatedBytes.length);
+ assertEquals(4, message.repeatedBytes[1][1]);
+ assertEquals(1, message.repeatedNestedMessage.length);
+ assertEquals(55, message.repeatedNestedMessage[0].bb);
+ assertEquals(3, message.repeatedPackedInt32.length);
+ assertEquals(2, message.repeatedPackedInt32[1]);
+ assertEquals(2, message.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.BAR, message.repeatedPackedNestedEnum[1]);
+ }
+
+ public void testNullRepeatedFieldElements() throws Exception {
+ // Check that serialization with null array elements doesn't NPE.
+ String string1 = "1";
+ String string2 = "2";
+ byte[] bytes1 = {3, 4};
+ byte[] bytes2 = {5, 6};
+ TestAllTypesNano.NestedMessage msg1 = new TestAllTypesNano.NestedMessage();
+ msg1.bb = 7;
+ TestAllTypesNano.NestedMessage msg2 = new TestAllTypesNano.NestedMessage();
+ msg2.bb = 8;
+
+ TestAllTypesNano message = new TestAllTypesNano();
+ message.repeatedString = new String[] {null, string1, string2};
+ message.repeatedBytes = new byte[][] {bytes1, null, bytes2};
+ message.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {msg1, msg2, null};
+ message.repeatedGroup = new TestAllTypesNano.RepeatedGroup[] {null, null, null};
+
+ byte[] serialized = MessageNano.toByteArray(message); // should not NPE
+ TestAllTypesNano deserialized = MessageNano.mergeFrom(new TestAllTypesNano(), serialized);
+ assertEquals(2, deserialized.repeatedString.length);
+ assertEquals(string1, deserialized.repeatedString[0]);
+ assertEquals(string2, deserialized.repeatedString[1]);
+ assertEquals(2, deserialized.repeatedBytes.length);
+ assertTrue(Arrays.equals(bytes1, deserialized.repeatedBytes[0]));
+ assertTrue(Arrays.equals(bytes2, deserialized.repeatedBytes[1]));
+ assertEquals(2, deserialized.repeatedNestedMessage.length);
+ assertEquals(msg1.bb, deserialized.repeatedNestedMessage[0].bb);
+ assertEquals(msg2.bb, deserialized.repeatedNestedMessage[1].bb);
+ assertEquals(0, deserialized.repeatedGroup.length);
+ }
+
+ public void testRepeatedMerge() throws Exception {
+ // Check that merging repeated fields cause the arrays to expand with
+ // new data.
+ TestAllTypesNano first = new TestAllTypesNano();
+ first.repeatedInt32 = new int[] {1, 2, 3};
+ TestAllTypesNano second = new TestAllTypesNano();
+ second.repeatedInt32 = new int[] {4, 5};
+ MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
+ assertEquals(5, first.repeatedInt32.length);
+ assertEquals(1, first.repeatedInt32[0]);
+ assertEquals(4, first.repeatedInt32[3]);
+
+ first = new TestAllTypesNano();
+ first.repeatedNestedEnum = new int[] {TestAllTypesNano.BAR};
+ second = new TestAllTypesNano();
+ second.repeatedNestedEnum = new int[] {TestAllTypesNano.FOO};
+ MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
+ assertEquals(2, first.repeatedNestedEnum.length);
+ assertEquals(TestAllTypesNano.BAR, first.repeatedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.FOO, first.repeatedNestedEnum[1]);
+
+ first = new TestAllTypesNano();
+ first.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
+ new TestAllTypesNano.NestedMessage()
+ };
+ first.repeatedNestedMessage[0].bb = 3;
+ second = new TestAllTypesNano();
+ second.repeatedNestedMessage = new TestAllTypesNano.NestedMessage[] {
+ new TestAllTypesNano.NestedMessage()
+ };
+ second.repeatedNestedMessage[0].bb = 5;
+ MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
+ assertEquals(2, first.repeatedNestedMessage.length);
+ assertEquals(3, first.repeatedNestedMessage[0].bb);
+ assertEquals(5, first.repeatedNestedMessage[1].bb);
+
+ first = new TestAllTypesNano();
+ first.repeatedPackedSfixed64 = new long[] {-1, -2, -3};
+ second = new TestAllTypesNano();
+ second.repeatedPackedSfixed64 = new long[] {-4, -5};
+ MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
+ assertEquals(5, first.repeatedPackedSfixed64.length);
+ assertEquals(-1, first.repeatedPackedSfixed64[0]);
+ assertEquals(-4, first.repeatedPackedSfixed64[3]);
+
+ first = new TestAllTypesNano();
+ first.repeatedPackedNestedEnum = new int[] {TestAllTypesNano.BAR};
+ second = new TestAllTypesNano();
+ second.repeatedPackedNestedEnum = new int[] {TestAllTypesNano.FOO};
+ MessageNano.mergeFrom(first, MessageNano.toByteArray(second));
+ assertEquals(2, first.repeatedPackedNestedEnum.length);
+ assertEquals(TestAllTypesNano.BAR, first.repeatedPackedNestedEnum[0]);
+ assertEquals(TestAllTypesNano.FOO, first.repeatedPackedNestedEnum[1]);
+
+ // Now test repeated merging in a nested scope
+ TestRepeatedMergeNano firstContainer = new TestRepeatedMergeNano();
+ firstContainer.contained = new TestAllTypesNano();
+ firstContainer.contained.repeatedInt32 = new int[] {10, 20};
+ TestRepeatedMergeNano secondContainer = new TestRepeatedMergeNano();
+ secondContainer.contained = new TestAllTypesNano();
+ secondContainer.contained.repeatedInt32 = new int[] {30};
+ MessageNano.mergeFrom(firstContainer, MessageNano.toByteArray(secondContainer));
+ assertEquals(3, firstContainer.contained.repeatedInt32.length);
+ assertEquals(20, firstContainer.contained.repeatedInt32[1]);
+ assertEquals(30, firstContainer.contained.repeatedInt32[2]);
+ }
+
+ public void testRepeatedPackables() throws Exception {
+ // Check that repeated fields with packable types can accept both packed and unpacked
+ // serialized forms.
+ NanoRepeatedPackables.NonPacked nonPacked = new NanoRepeatedPackables.NonPacked();
+ // Exaggerates the first values of varint-typed arrays. This is to test that the parsing code
+ // of packed fields handles non-packed data correctly. If the code incorrectly thinks it is
+ // reading from a packed tag, it will read the first value as the byte length of the field,
+ // and the large number will cause the input to go out of bounds, thus capturing the error.
+ nonPacked.int32S = new int[] {1000, 2, 3};
+ nonPacked.int64S = new long[] {4000, 5, 6};
+ nonPacked.uint32S = new int[] {7000, 8, 9};
+ nonPacked.uint64S = new long[] {10000, 11, 12};
+ nonPacked.sint32S = new int[] {13000, 14, 15};
+ nonPacked.sint64S = new long[] {16000, 17, 18};
+ nonPacked.fixed32S = new int[] {19, 20, 21};
+ nonPacked.fixed64S = new long[] {22, 23, 24};
+ nonPacked.sfixed32S = new int[] {25, 26, 27};
+ nonPacked.sfixed64S = new long[] {28, 29, 30};
+ nonPacked.floats = new float[] {31, 32, 33};
+ nonPacked.doubles = new double[] {34, 35, 36};
+ nonPacked.bools = new boolean[] {false, true};
+ nonPacked.enums = new int[] {
+ NanoRepeatedPackables.Enum.OPTION_ONE,
+ NanoRepeatedPackables.Enum.OPTION_TWO,
+ };
+ nonPacked.noise = 13579;
+
+ byte[] nonPackedSerialized = MessageNano.toByteArray(nonPacked);
+
+ NanoRepeatedPackables.Packed packed =
+ MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), nonPackedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+
+ byte[] packedSerialized = MessageNano.toByteArray(packed);
+ // Just a cautious check that the two serialized forms are different,
+ // to make sure the remaining of this test is useful:
+ assertFalse(Arrays.equals(nonPackedSerialized, packedSerialized));
+
+ nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), packedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+
+ // Test mixed serialized form.
+ byte[] mixedSerialized = new byte[nonPackedSerialized.length + packedSerialized.length];
+ System.arraycopy(nonPackedSerialized, 0, mixedSerialized, 0, nonPackedSerialized.length);
+ System.arraycopy(packedSerialized, 0,
+ mixedSerialized, nonPackedSerialized.length, packedSerialized.length);
+
+ nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), mixedSerialized);
+ packed = MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), mixedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+ assertTrue(Arrays.equals(new int[] {1000, 2, 3, 1000, 2, 3}, nonPacked.int32S));
+ assertTrue(Arrays.equals(new int[] {13000, 14, 15, 13000, 14, 15}, nonPacked.sint32S));
+ assertTrue(Arrays.equals(new int[] {25, 26, 27, 25, 26, 27}, nonPacked.sfixed32S));
+ assertTrue(Arrays.equals(new boolean[] {false, true, false, true}, nonPacked.bools));
+ }
+
+ private void assertRepeatedPackablesEqual(
+ NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) {
+ // Not using MessageNano.equals() -- that belongs to a separate test.
+ assertTrue(Arrays.equals(nonPacked.int32S, packed.int32S));
+ assertTrue(Arrays.equals(nonPacked.int64S, packed.int64S));
+ assertTrue(Arrays.equals(nonPacked.uint32S, packed.uint32S));
+ assertTrue(Arrays.equals(nonPacked.uint64S, packed.uint64S));
+ assertTrue(Arrays.equals(nonPacked.sint32S, packed.sint32S));
+ assertTrue(Arrays.equals(nonPacked.sint64S, packed.sint64S));
+ assertTrue(Arrays.equals(nonPacked.fixed32S, packed.fixed32S));
+ assertTrue(Arrays.equals(nonPacked.fixed64S, packed.fixed64S));
+ assertTrue(Arrays.equals(nonPacked.sfixed32S, packed.sfixed32S));
+ assertTrue(Arrays.equals(nonPacked.sfixed64S, packed.sfixed64S));
+ assertTrue(Arrays.equals(nonPacked.floats, packed.floats));
+ assertTrue(Arrays.equals(nonPacked.doubles, packed.doubles));
+ assertTrue(Arrays.equals(nonPacked.bools, packed.bools));
+ assertTrue(Arrays.equals(nonPacked.enums, packed.enums));
+ }
+
+ private void assertHasWireData(MessageNano message, boolean expected) {
+ byte[] bytes = MessageNano.toByteArray(message);
+ int wireLength = bytes.length;
+ if (expected) {
+ assertFalse(wireLength == 0);
+ } else {
+ if (wireLength != 0) {
+ fail("Expected no wire data for message \n" + message
+ + "\nBut got:\n"
+ + hexDump(bytes));
+ }
+ }
+ }
+
+ private static String hexDump(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+ for (byte b : bytes) {
+ sb.append(String.format("%02x ", b));
+ }
+ return sb.toString();
+ }
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto
new file mode 100644
index 00000000..6e5a40f6
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_accessors_nano.proto
@@ -0,0 +1,118 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "NanoAccessorsOuterClass";
+
+message TestNanoAccessors {
+
+ message NestedMessage {
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ }
+
+ // Singular
+ optional int32 optional_int32 = 1;
+ optional float optional_float = 11;
+ optional double optional_double = 12;
+ optional string optional_string = 14;
+ optional bytes optional_bytes = 15;
+
+ optional NestedMessage optional_nested_message = 18;
+
+ optional NestedEnum optional_nested_enum = 21;
+
+ // Repeated
+ repeated int32 repeated_int32 = 31;
+ repeated string repeated_string = 44;
+ repeated bytes repeated_bytes = 45;
+
+ repeated NestedMessage repeated_nested_message = 48;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+
+ // Singular with defaults
+ optional int32 default_int32 = 61 [default = 41 ];
+ optional string default_string = 74 [default = "hello"];
+ optional bytes default_bytes = 75 [default = "world"];
+
+ optional float default_float_nan = 99 [default = nan];
+
+ optional NestedEnum default_nested_enum = 81 [default = BAR];
+
+ // Required
+ required int32 id = 86;
+
+ // Add enough optional fields to make 2 bit fields in total
+ optional int32 filler100 = 100;
+ optional int32 filler101 = 101;
+ optional int32 filler102 = 102;
+ optional int32 filler103 = 103;
+ optional int32 filler104 = 104;
+ optional int32 filler105 = 105;
+ optional int32 filler106 = 106;
+ optional int32 filler107 = 107;
+ optional int32 filler108 = 108;
+ optional int32 filler109 = 109;
+ optional int32 filler110 = 110;
+ optional int32 filler111 = 111;
+ optional int32 filler112 = 112;
+ optional int32 filler113 = 113;
+ optional int32 filler114 = 114;
+ optional int32 filler115 = 115;
+ optional int32 filler116 = 116;
+ optional int32 filler117 = 117;
+ optional int32 filler118 = 118;
+ optional int32 filler119 = 119;
+ optional int32 filler120 = 120;
+ optional int32 filler121 = 121;
+ optional int32 filler122 = 122;
+ optional int32 filler123 = 123;
+ optional int32 filler124 = 124;
+ optional int32 filler125 = 125;
+ optional int32 filler126 = 126;
+ optional int32 filler127 = 127;
+ optional int32 filler128 = 128;
+ optional int32 filler129 = 129;
+ optional int32 filler130 = 130;
+
+ optional int32 before_bit_field_check = 139;
+ optional int32 bit_field_check = 140;
+ optional int32 after_bit_field_check = 141;
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.proto
new file mode 100644
index 00000000..e6246bb1
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_multiple_nano.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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_multiple_files = true;
+
+enum FileScopeEnumMultiple {
+ THREE = 3;
+}
+
+message EnumClassNanoMultiple {
+ enum MessageScopeEnumMultiple {
+ FOUR = 4;
+ }
+ optional FileScopeEnumMultiple three = 3 [ default = THREE ];
+ optional MessageScopeEnumMultiple four = 4 [ default = FOUR ];
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.proto
new file mode 100644
index 00000000..33a86225
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_class_nano.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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "EnumClassNanos";
+
+enum FileScopeEnum {
+ ONE = 1;
+}
+
+message EnumClassNano {
+ enum MessageScopeEnum {
+ TWO = 2;
+ }
+ optional FileScopeEnum one = 1 [ default = ONE ];
+ optional MessageScopeEnum two = 2 [ default = TWO ];
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto
new file mode 100644
index 00000000..f7f57427
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_enum_validity_nano.proto
@@ -0,0 +1,28 @@
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "EnumValidity";
+
+enum E {
+ default = 1; // test java keyword renaming
+ FOO = 2;
+ BAR = 3;
+ BAZ = 4;
+}
+
+message M {
+ optional E optional_e = 1;
+ optional E default_e = 2 [ default = BAZ ];
+ repeated E repeated_e = 3;
+ repeated E packed_e = 4 [ packed = true ];
+ repeated E repeated_e2 = 5;
+ repeated E packed_e2 = 6 [ packed = true ];
+ repeated E repeated_e3 = 7;
+ repeated E packed_e3 = 8 [ packed = true ];
+}
+
+message Alt {
+ optional E repeated_e2_as_optional = 5;
+ repeated E packed_e2_as_non_packed = 6;
+ repeated E non_packed_e3_as_packed = 7 [ packed = true ];
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto
new file mode 100644
index 00000000..2a678a80
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_nano.proto
@@ -0,0 +1,33 @@
+syntax = "proto2";
+
+option java_outer_classname = "Extensions";
+option java_package = "com.google.protobuf.nano.testext";
+
+message ExtendableMessage {
+ optional int32 field = 1;
+ extensions 10 to max;
+}
+
+enum AnEnum {
+ FIRST_VALUE = 1;
+ SECOND_VALUE = 2;
+}
+
+message AnotherMessage {
+ optional string string = 1;
+ optional bool value = 2;
+}
+
+message ContainerMessage {
+ extend ExtendableMessage {
+ optional bool another_thing = 100;
+ }
+}
+
+// For testNanoOptionalGroupWithUnknownFieldsEnabled;
+// not part of the extensions tests.
+message MessageWithGroup {
+ optional group Group = 1 {
+ optional int32 a = 2;
+ }
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto
new file mode 100644
index 00000000..7d47682d
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_packed_nano.proto
@@ -0,0 +1,29 @@
+syntax = "proto2";
+
+option java_multiple_files = true;
+option java_package = "com.google.protobuf.nano";
+
+import "google/protobuf/nano/unittest_extension_nano.proto";
+
+// Must be compiled separately due to extension number reuse.
+// The reuse is deliberate, for testing wire compatibility.
+
+message PackedExtensions {
+ extend ExtendableMessage {
+ repeated int32 packed_int32 = 10 [ packed = true ];
+ repeated uint32 packed_uint32 = 11 [ packed = true ];
+ repeated sint32 packed_sint32 = 12 [ packed = true ];
+ repeated int64 packed_int64 = 13 [ packed = true ];
+ repeated uint64 packed_uint64 = 14 [ packed = true ];
+ repeated sint64 packed_sint64 = 15 [ packed = true ];
+ repeated fixed32 packed_fixed32 = 16 [ packed = true ];
+ repeated sfixed32 packed_sfixed32 = 17 [ packed = true ];
+ repeated fixed64 packed_fixed64 = 18 [ packed = true ];
+ repeated sfixed64 packed_sfixed64 = 19 [ packed = true ];
+ repeated bool packed_bool = 20 [ packed = true ];
+ repeated float packed_float = 21 [ packed = true ];
+ repeated double packed_double = 22 [ packed = true ];
+ repeated AnEnum packed_enum = 23 [ packed = true ];
+ // Non-packable types omitted.
+ }
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto
new file mode 100644
index 00000000..6d4b5dfb
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_repeated_nano.proto
@@ -0,0 +1,34 @@
+syntax = "proto2";
+
+option java_multiple_files = true;
+option java_package = "com.google.protobuf.nano";
+
+import "google/protobuf/nano/unittest_extension_nano.proto";
+
+// Must be compiled separately due to extension number reuse.
+// The reuse is deliberate, for testing wire compatibility.
+
+message RepeatedExtensions {
+ extend ExtendableMessage {
+ repeated int32 repeated_int32 = 10;
+ repeated uint32 repeated_uint32 = 11;
+ repeated sint32 repeated_sint32 = 12;
+ repeated int64 repeated_int64 = 13;
+ repeated uint64 repeated_uint64 = 14;
+ repeated sint64 repeated_sint64 = 15;
+ repeated fixed32 repeated_fixed32 = 16;
+ repeated sfixed32 repeated_sfixed32 = 17;
+ repeated fixed64 repeated_fixed64 = 18;
+ repeated sfixed64 repeated_sfixed64 = 19;
+ repeated bool repeated_bool = 20;
+ repeated float repeated_float = 21;
+ repeated double repeated_double = 22;
+ repeated AnEnum repeated_enum = 23;
+ repeated string repeated_string = 24;
+ repeated bytes repeated_bytes = 25;
+ repeated AnotherMessage repeated_message = 26;
+ repeated group RepeatedGroup = 27 {
+ optional int32 a = 1;
+ }
+ }
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto
new file mode 100644
index 00000000..589754e7
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_extension_singular_nano.proto
@@ -0,0 +1,34 @@
+syntax = "proto2";
+
+option java_multiple_files = true;
+option java_package = "com.google.protobuf.nano";
+
+import "google/protobuf/nano/unittest_extension_nano.proto";
+
+// Must be compiled separately due to extension number reuse.
+// The reuse is deliberate, for testing wire compatibility.
+
+message SingularExtensions {
+ extend ExtendableMessage {
+ optional int32 some_int32 = 10;
+ optional uint32 some_uint32 = 11;
+ optional sint32 some_sint32 = 12;
+ optional int64 some_int64 = 13;
+ optional uint64 some_uint64 = 14;
+ optional sint64 some_sint64 = 15;
+ optional fixed32 some_fixed32 = 16;
+ optional sfixed32 some_sfixed32 = 17;
+ optional fixed64 some_fixed64 = 18;
+ optional sfixed64 some_sfixed64 = 19;
+ optional bool some_bool = 20;
+ optional float some_float = 21;
+ optional double some_double = 22;
+ optional AnEnum some_enum = 23;
+ optional string some_string = 24;
+ optional bytes some_bytes = 25;
+ optional AnotherMessage some_message = 26;
+ optional group SomeGroup = 27 {
+ optional int32 a = 1;
+ }
+ }
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_has_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_has_nano.proto
new file mode 100644
index 00000000..bda1a6ef
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_has_nano.proto
@@ -0,0 +1,82 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: ulas@google.com (Ulas Kirazci)
+
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "NanoHasOuterClass";
+
+message TestAllTypesNanoHas {
+
+ message NestedMessage {
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ }
+
+ // Singular
+ optional int32 optional_int32 = 1;
+ optional float optional_float = 11;
+ optional double optional_double = 12;
+ optional string optional_string = 14;
+ optional bytes optional_bytes = 15;
+
+ optional NestedMessage optional_nested_message = 18;
+
+ optional NestedEnum optional_nested_enum = 21;
+
+ // Repeated
+ repeated int32 repeated_int32 = 31;
+ repeated string repeated_string = 44;
+ repeated bytes repeated_bytes = 45;
+
+ repeated NestedMessage repeated_nested_message = 48;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+
+ // Singular with defaults
+ optional int32 default_int32 = 61 [default = 41 ];
+ optional string default_string = 74 [default = "hello"];
+ optional bytes default_bytes = 75 [default = "world"];
+
+ optional float default_float_nan = 99 [default = nan];
+
+ optional NestedEnum default_nested_enum = 81 [default = BAR];
+
+ required int32 id = 86;
+ required NestedEnum required_enum = 87;
+
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_import_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_import_nano.proto
new file mode 100644
index 00000000..1a3ddc57
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_import_nano.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.
+
+// Author: kenton@google.com (Kenton Varda)
+//
+// This is like unittest_import.proto but with optimize_for = NANO_RUNTIME.
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano.testimport";
+option java_outer_classname = "UnittestImportNano";
+
+message ImportMessageNano {
+ optional int32 d = 1;
+}
+
+enum ImportEnumNano {
+ IMPORT_NANO_FOO = 7;
+ IMPORT_NANO_BAR = 8;
+ IMPORT_NANO_BAZ = 9;
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto
new file mode 100644
index 00000000..9db2d3e3
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nameclash_nano.proto
@@ -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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "MultipleNameClashNano";
+option java_multiple_files = true;
+
+message MultipleNameClashNano {
+ optional int32 field = 1;
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto
new file mode 100644
index 00000000..9dbf0de4
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_multiple_nano.proto
@@ -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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest_import;
+
+import "google/protobuf/nano/unittest_import_nano.proto";
+
+option java_package = "com.google.protobuf.nano";
+option java_multiple_files = true;
+
+enum FileScopeEnum {
+ ONE = 1;
+ TWO = 2;
+}
+
+message FileScopeEnumRefNano {
+ optional FileScopeEnum enum_field = 1;
+}
+
+message MessageScopeEnumRefNano {
+ enum MessageScopeEnum {
+ ONE = 1;
+ TWO = 2;
+ }
+ optional MessageScopeEnum enum_field = 1;
+}
+
+message MultipleImportingNonMultipleNano1 {
+ optional ImportMessageNano field = 1;
+}
+
+message MultipleImportingNonMultipleNano2 {
+ optional MultipleImportingNonMultipleNano1 nano1 = 1;
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_nano.proto
new file mode 100644
index 00000000..4519533c
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_nano.proto
@@ -0,0 +1,186 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: wink@google.com (Wink Saville)
+
+package protobuf_unittest;
+
+import "google/protobuf/nano/unittest_import_nano.proto";
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "NanoOuterClass";
+
+// Same as TestAllTypes but with the nano runtime.
+message TestAllTypesNano {
+
+ message NestedMessage {
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ }
+
+ // Singular
+ 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 group OptionalGroup = 16 {
+ optional int32 a = 17;
+ }
+
+ optional NestedMessage optional_nested_message = 18;
+ optional ForeignMessageNano optional_foreign_message = 19;
+ optional protobuf_unittest_import.ImportMessageNano
+ optional_import_message = 20;
+
+ optional NestedEnum optional_nested_enum = 21;
+ optional ForeignEnumNano optional_foreign_enum = 22;
+ optional protobuf_unittest_import.ImportEnumNano optional_import_enum = 23;
+
+ optional string optional_string_piece = 24 [ctype=STRING_PIECE];
+ optional string optional_cord = 25 [ctype=CORD];
+
+ // 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 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 group RepeatedGroup = 46 {
+ optional int32 a = 47;
+ }
+
+ repeated NestedMessage repeated_nested_message = 48;
+ repeated ForeignMessageNano repeated_foreign_message = 49;
+ repeated protobuf_unittest_import.ImportMessageNano
+ repeated_import_message = 50;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+ repeated ForeignEnumNano repeated_foreign_enum = 52;
+ repeated protobuf_unittest_import.ImportEnumNano repeated_import_enum = 53;
+
+ repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
+ repeated string repeated_cord = 55 [ctype=CORD];
+
+ // Repeated packed
+ repeated int32 repeated_packed_int32 = 87 [packed=true];
+ repeated sfixed64 repeated_packed_sfixed64 = 88 [packed=true];
+
+ repeated NestedEnum repeated_packed_nested_enum = 89 [packed=true];
+
+ // Singular with defaults
+ optional int32 default_int32 = 61 [default = 41 ];
+ optional int64 default_int64 = 62 [default = 42 ];
+ optional uint32 default_uint32 = 63 [default = 43 ];
+ optional uint64 default_uint64 = 64 [default = 44 ];
+ optional sint32 default_sint32 = 65 [default = -45 ];
+ optional sint64 default_sint64 = 66 [default = 46 ];
+ optional fixed32 default_fixed32 = 67 [default = 47 ];
+ optional fixed64 default_fixed64 = 68 [default = 48 ];
+ optional sfixed32 default_sfixed32 = 69 [default = 49 ];
+ optional sfixed64 default_sfixed64 = 70 [default = -50 ];
+ optional float default_float = 71 [default = 51.5 ];
+ optional double default_double = 72 [default = 52e3 ];
+ optional bool default_bool = 73 [default = true ];
+ optional string default_string = 74 [default = "hello"];
+ optional bytes default_bytes = 75 [default = "world"];
+
+ optional string default_string_nonascii = 76 [default = "dünya"];
+ optional bytes default_bytes_nonascii = 77 [default = "dünyab"];
+
+ optional float default_float_inf = 97 [default = inf];
+ optional float default_float_neg_inf = 98 [default = -inf];
+ optional float default_float_nan = 99 [default = nan];
+ optional double default_double_inf = 100 [default = inf];
+ optional double default_double_neg_inf = 101 [default = -inf];
+ optional double default_double_nan = 102 [default = nan];
+
+ optional NestedEnum default_nested_enum = 81 [default = BAR];
+ optional ForeignEnumNano default_foreign_enum = 82
+ [default = FOREIGN_NANO_BAR];
+ optional protobuf_unittest_import.ImportEnumNano
+ default_import_enum = 83 [default = IMPORT_NANO_BAR];
+
+ optional string default_string_piece = 84 [ctype=STRING_PIECE,default="abc"];
+ optional string default_cord = 85 [ctype=CORD,default="123"];
+
+ required int32 id = 86;
+
+ // Try to cause conflicts.
+ optional int32 tag = 93;
+ optional int32 get_serialized_size = 94;
+ optional int32 write_to = 95;
+
+ // Try to fail with java reserved keywords
+ optional int32 synchronized = 96;
+}
+
+message ForeignMessageNano {
+ optional int32 c = 1;
+}
+
+enum ForeignEnumNano {
+ FOREIGN_NANO_FOO = 4;
+ FOREIGN_NANO_BAR = 5;
+ FOREIGN_NANO_BAZ = 6;
+}
+
+// Test that deprecated fields work. We only verify that they compile (at one
+// point this failed).
+message TestDeprecatedNano {
+ optional int32 deprecated_field = 1 [deprecated = true];
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto
new file mode 100644
index 00000000..69d05834
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_recursive_nano.proto
@@ -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.
+
+// Author: wink@google.com (Wink Saville)
+//
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano";
+// Explicit outer classname to suppress legacy info.
+option java_outer_classname = "UnittestRecursiveNano";
+
+message RecursiveMessageNano {
+ message NestedMessage {
+ optional RecursiveMessageNano a = 1;
+ }
+
+ required int32 id = 1;
+ optional NestedMessage nested_message = 2;
+ optional RecursiveMessageNano optional_recursive_message_nano = 3;
+ repeated RecursiveMessageNano repeated_recursive_message_nano = 4;
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto
new file mode 100644
index 00000000..2b246150
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_reference_types_nano.proto
@@ -0,0 +1,116 @@
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "NanoReferenceTypes";
+
+message TestAllTypesNano {
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ }
+
+ message NestedMessage {
+ optional int32 foo = 1;
+ }
+
+ // Singular
+ 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 group OptionalGroup = 16 {
+ optional int32 a = 17;
+ }
+
+ optional NestedMessage optional_nested_message = 18;
+
+ optional NestedEnum optional_nested_enum = 21;
+
+ optional string optional_string_piece = 24 [ctype=STRING_PIECE];
+ optional string optional_cord = 25 [ctype=CORD];
+
+ // 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 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 group RepeatedGroup = 46 {
+ optional int32 a = 47;
+ }
+
+ repeated NestedMessage repeated_nested_message = 48;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+
+ repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
+ repeated string repeated_cord = 55 [ctype=CORD];
+
+ // Repeated packed
+ repeated int32 repeated_packed_int32 = 87 [packed=true];
+ repeated sfixed64 repeated_packed_sfixed64 = 88 [packed=true];
+
+ repeated NestedEnum repeated_packed_nested_enum = 89 [packed=true];
+
+ // Singular with defaults
+ optional int32 default_int32 = 61 [default = 41 ];
+ optional int64 default_int64 = 62 [default = 42 ];
+ optional uint32 default_uint32 = 63 [default = 43 ];
+ optional uint64 default_uint64 = 64 [default = 44 ];
+ optional sint32 default_sint32 = 65 [default = -45 ];
+ optional sint64 default_sint64 = 66 [default = 46 ];
+ optional fixed32 default_fixed32 = 67 [default = 47 ];
+ optional fixed64 default_fixed64 = 68 [default = 48 ];
+ optional sfixed32 default_sfixed32 = 69 [default = 49 ];
+ optional sfixed64 default_sfixed64 = 70 [default = -50 ];
+ optional float default_float = 71 [default = 51.5 ];
+ optional double default_double = 72 [default = 52e3 ];
+ optional bool default_bool = 73 [default = true ];
+ optional string default_string = 74 [default = "hello"];
+ optional bytes default_bytes = 75 [default = "world"];
+
+
+ optional float default_float_inf = 97 [default = inf];
+ optional float default_float_neg_inf = 98 [default = -inf];
+ optional float default_float_nan = 99 [default = nan];
+ optional double default_double_inf = 100 [default = inf];
+ optional double default_double_neg_inf = 101 [default = -inf];
+ optional double default_double_nan = 102 [default = nan];
+
+}
+
+message ForeignMessageNano {
+ optional int32 c = 1;
+}
+
+enum ForeignEnumNano {
+ FOREIGN_NANO_FOO = 4;
+ FOREIGN_NANO_BAR = 5;
+ FOREIGN_NANO_BAZ = 6;
+}
+
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto
new file mode 100644
index 00000000..9bfc3ad1
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_merge_nano.proto
@@ -0,0 +1,47 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest;
+
+import "google/protobuf/nano/unittest_nano.proto";
+
+option java_package = "com.google.protobuf.nano";
+option java_multiple_files = true;
+
+// A container message for testing the merging of repeated fields at a
+// nested level. Other tests will be done using the repeated fields in
+// TestAllTypesNano.
+message TestRepeatedMergeNano {
+
+ optional TestAllTypesNano contained = 1;
+
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto
new file mode 100644
index 00000000..a7ca7527
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_repeated_packables_nano.proto
@@ -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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest;
+
+option java_package = "com.google.protobuf.nano";
+option java_outer_classname = "NanoRepeatedPackables";
+
+enum Enum {
+ OPTION_ONE = 1;
+ OPTION_TWO = 2;
+}
+
+// Two almost identical messages with all packable repeated field types.
+// One with none marked as packed and the other all packed. For
+// compatibility, they should be able to parse each other's serialized
+// forms.
+
+message NonPacked {
+
+ // All packable types, none marked as packed.
+
+ repeated int32 int32s = 1;
+ repeated int64 int64s = 2;
+ repeated uint32 uint32s = 3;
+ repeated uint64 uint64s = 4;
+ repeated sint32 sint32s = 5;
+ repeated sint64 sint64s = 6;
+ repeated fixed32 fixed32s = 7;
+ repeated fixed64 fixed64s = 8;
+ repeated sfixed32 sfixed32s = 9;
+ repeated sfixed64 sfixed64s = 10;
+ repeated float floats = 11;
+ repeated double doubles = 12;
+ repeated bool bools = 13;
+ repeated Enum enums = 14;
+
+ // Noise for testing merged deserialization.
+ optional int32 noise = 15;
+
+}
+
+message Packed {
+
+ // All packable types, all matching the field numbers in NonPacked,
+ // all marked as packed.
+
+ repeated int32 int32s = 1 [ packed = true ];
+ repeated int64 int64s = 2 [ packed = true ];
+ repeated uint32 uint32s = 3 [ packed = true ];
+ repeated uint64 uint64s = 4 [ packed = true ];
+ repeated sint32 sint32s = 5 [ packed = true ];
+ repeated sint64 sint64s = 6 [ packed = true ];
+ repeated fixed32 fixed32s = 7 [ packed = true ];
+ repeated fixed64 fixed64s = 8 [ packed = true ];
+ repeated sfixed32 sfixed32s = 9 [ packed = true ];
+ repeated sfixed64 sfixed64s = 10 [ packed = true ];
+ repeated float floats = 11 [ packed = true ];
+ repeated double doubles = 12 [ packed = true ];
+ repeated bool bools = 13 [ packed = true ];
+ repeated Enum enums = 14 [ packed = true ];
+
+ // Noise for testing merged deserialization.
+ optional int32 noise = 15;
+
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto
new file mode 100644
index 00000000..48333aba
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_simple_nano.proto
@@ -0,0 +1,54 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: wink@google.com (Wink Saville)
+//
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano";
+// Explicit outer classname to suppress legacy info.
+option java_outer_classname = "UnittestSimpleNano";
+
+message SimpleMessageNano {
+ message NestedMessage {
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ }
+
+ optional int32 d = 1 [default = 123];
+ optional NestedMessage nested_msg = 2;
+ optional NestedEnum default_nested_enum = 3 [default = BAZ];
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_single_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_single_nano.proto
new file mode 100644
index 00000000..025428d8
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_single_nano.proto
@@ -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.
+
+// Author: maxtroy@google.com (Max Cai)
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano";
+
+message SingleMessageNano {
+}
diff --git a/javanano/src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto b/javanano/src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto
new file mode 100644
index 00000000..21bd8c0c
--- /dev/null
+++ b/javanano/src/test/java/com/google/protobuf/nano/unittest_stringutf8_nano.proto
@@ -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.
+
+// Author: wink@google.com (Wink Saville)
+//
+
+package protobuf_unittest_import;
+
+option java_package = "com.google.protobuf.nano";
+// Explicit outer classname to suppress legacy info.
+option java_outer_classname = "UnittestStringutf8Nano";
+
+message StringUtf8 {
+ optional string id = 1;
+ repeated string rs = 2;
+}
diff --git a/post_process_dist.sh b/post_process_dist.sh
index 7b2e599d..733fa088 100755
--- a/post_process_dist.sh
+++ b/post_process_dist.sh
@@ -16,7 +16,7 @@
# 5) Cleans up after itself.
if [ "$1" == "" ]; then
- echo "USAGE: $1 DISTFILE" >&2
+ echo "USAGE: $0 DISTFILE" >&2
exit 1
fi
@@ -27,7 +27,9 @@ fi
set -ex
+LANGUAGES="cpp java python"
BASENAME=`basename $1 .tar.gz`
+VERSION=${BASENAME:9}
# Create a directory called "dist", copy the tarball there and unpack it.
mkdir dist
@@ -44,17 +46,23 @@ cd $BASENAME/vsprojects
./convert2008to2005.sh
cd ..
-# Build the dist again in .tar.gz and .tar.bz2 formats.
-./configure
-make dist-gzip
-make dist-bzip2
+for LANG in $LANGUAGES; do
+ # Build the dist again in .tar.gz
+ ./configure DIST_LANG=$LANG
+ make dist-gzip
+ mv $BASENAME.tar.gz ../protobuf-$LANG-$VERSION.tar.gz
+done
# Convert all text files to use DOS-style line endings, then build a .zip
# distribution.
todos *.txt */*.txt
-make dist-zip
-# Clean up.
-mv $BASENAME.tar.gz $BASENAME.tar.bz2 $BASENAME.zip ..
+for LANG in $LANGUAGES; do
+ # Build the dist again in .zip
+ ./configure DIST_LANG=$LANG
+ make dist-zip
+ mv $BASENAME.zip ../protobuf-$LANG-$VERSION.zip
+done
+
cd ..
rm -rf $BASENAME
diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py
index 6da8bb0b..af571b7c 100755
--- a/python/google/protobuf/descriptor.py
+++ b/python/google/protobuf/descriptor.py
@@ -841,9 +841,10 @@ def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True):
field_proto.number, field_proto.type,
FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type),
field_proto.label, None, nested_desc, enum_desc, None, False, None,
- has_default_value=False)
+ options=field_proto.options, has_default_value=False)
fields.append(field)
desc_name = '.'.join(full_message_name)
return Descriptor(desc_proto.name, desc_name, None, None, fields,
- nested_types.values(), enum_types.values(), [])
+ nested_types.values(), enum_types.values(), [],
+ options=desc_proto.options)
diff --git a/python/google/protobuf/descriptor_database.py b/python/google/protobuf/descriptor_database.py
index 55fb8c70..b10021e9 100644
--- a/python/google/protobuf/descriptor_database.py
+++ b/python/google/protobuf/descriptor_database.py
@@ -133,5 +133,5 @@ def _ExtractSymbols(desc_proto, package):
for nested_type in desc_proto.nested_type:
for symbol in _ExtractSymbols(nested_type, message_name):
yield symbol
- for enum_type in desc_proto.enum_type:
- yield '.'.join((message_name, enum_type.name))
+ for enum_type in desc_proto.enum_type:
+ yield '.'.join((message_name, enum_type.name))
diff --git a/python/google/protobuf/descriptor_pool.py b/python/google/protobuf/descriptor_pool.py
index cf234cfa..bcac513a 100644
--- a/python/google/protobuf/descriptor_pool.py
+++ b/python/google/protobuf/descriptor_pool.py
@@ -554,7 +554,7 @@ class DescriptorPool(object):
field_desc.default_value = field_proto.default_value.lower() == 'true'
elif field_proto.type == descriptor.FieldDescriptor.TYPE_ENUM:
field_desc.default_value = field_desc.enum_type.values_by_name[
- field_proto.default_value].index
+ field_proto.default_value].number
elif field_proto.type == descriptor.FieldDescriptor.TYPE_BYTES:
field_desc.default_value = text_encoding.CUnescape(
field_proto.default_value)
diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py
index fc65b69a..8970f5c2 100644
--- a/python/google/protobuf/internal/descriptor_database_test.py
+++ b/python/google/protobuf/internal/descriptor_database_test.py
@@ -58,6 +58,8 @@ class DescriptorDatabaseTest(basetest.TestCase):
'google.protobuf.python.internal.Factory2Enum'))
self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum'))
+ self.assertEquals(file_desc_proto, db.FindFileContainingSymbol(
+ 'google.protobuf.python.internal.MessageWithNestedEnumOnly.NestedEnum'))
if __name__ == '__main__':
basetest.main()
diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py
index d2f85579..11ef61c5 100644
--- a/python/google/protobuf/internal/descriptor_pool_test.py
+++ b/python/google/protobuf/internal/descriptor_pool_test.py
@@ -48,6 +48,7 @@ from google.protobuf.internal import factory_test2_pb2
from google.protobuf import descriptor
from google.protobuf import descriptor_database
from google.protobuf import descriptor_pool
+from google.protobuf import symbol_database
class DescriptorPoolTest(basetest.TestCase):
@@ -237,6 +238,32 @@ class DescriptorPoolTest(basetest.TestCase):
TEST2_FILE.CheckFile(self, self.pool)
+ def testEnumDefaultValue(self):
+ """Test the default value of enums which don't start at zero."""
+ def _CheckDefaultValue(file_descriptor):
+ default_value = (file_descriptor
+ .message_types_by_name['DescriptorPoolTest1']
+ .fields_by_name['nested_enum']
+ .default_value)
+ self.assertEqual(default_value,
+ descriptor_pool_test1_pb2.DescriptorPoolTest1.BETA)
+ # First check what the generated descriptor contains.
+ _CheckDefaultValue(descriptor_pool_test1_pb2.DESCRIPTOR)
+ # Then check the generated pool. Normally this is the same descriptor.
+ file_descriptor = symbol_database.Default().pool.FindFileByName(
+ 'google/protobuf/internal/descriptor_pool_test1.proto')
+ self.assertIs(file_descriptor, descriptor_pool_test1_pb2.DESCRIPTOR)
+ _CheckDefaultValue(file_descriptor)
+
+ # Then check the dynamic pool and its internal DescriptorDatabase.
+ descriptor_proto = descriptor_pb2.FileDescriptorProto.FromString(
+ descriptor_pool_test1_pb2.DESCRIPTOR.serialized_pb)
+ self.pool.Add(descriptor_proto)
+ # And do the same check as above
+ file_descriptor = self.pool.FindFileByName(
+ 'google/protobuf/internal/descriptor_pool_test1.proto')
+ _CheckDefaultValue(file_descriptor)
+
class ProtoFile(object):
@@ -328,7 +355,7 @@ class EnumField(object):
test.assertEqual(descriptor.FieldDescriptor.CPPTYPE_ENUM,
field_desc.cpp_type)
test.assertTrue(field_desc.has_default_value)
- test.assertEqual(enum_desc.values_by_name[self.default_value].index,
+ test.assertEqual(enum_desc.values_by_name[self.default_value].number,
field_desc.default_value)
test.assertEqual(msg_desc, field_desc.containing_type)
test.assertEqual(enum_desc, field_desc.enum_type)
diff --git a/python/google/protobuf/internal/descriptor_pool_test1.proto b/python/google/protobuf/internal/descriptor_pool_test1.proto
index 6dfe4ef3..00816b78 100644
--- a/python/google/protobuf/internal/descriptor_pool_test1.proto
+++ b/python/google/protobuf/internal/descriptor_pool_test1.proto
@@ -28,6 +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.
+syntax = "proto2";
+
package google.protobuf.python.internal;
diff --git a/python/google/protobuf/internal/descriptor_pool_test2.proto b/python/google/protobuf/internal/descriptor_pool_test2.proto
index fbc84382..e3fa660c 100644
--- a/python/google/protobuf/internal/descriptor_pool_test2.proto
+++ b/python/google/protobuf/internal/descriptor_pool_test2.proto
@@ -28,6 +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.
+syntax = "proto2";
+
package google.protobuf.python.internal;
import "google/protobuf/internal/descriptor_pool_test1.proto";
diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py
index b3777e39..3924f21a 100755
--- a/python/google/protobuf/internal/descriptor_test.py
+++ b/python/google/protobuf/internal/descriptor_test.py
@@ -665,5 +665,15 @@ class MakeDescriptorTest(basetest.TestCase):
descriptor.FieldDescriptor.CPPTYPE_UINT64)
+ def testMakeDescriptorWithOptions(self):
+ descriptor_proto = descriptor_pb2.DescriptorProto()
+ aggregate_message = unittest_custom_options_pb2.AggregateMessage
+ aggregate_message.DESCRIPTOR.CopyToProto(descriptor_proto)
+ reformed_descriptor = descriptor.MakeDescriptor(descriptor_proto)
+
+ options = reformed_descriptor.GetOptions()
+ self.assertEquals(101,
+ options.Extensions[unittest_custom_options_pb2.msgopt].i)
+
if __name__ == '__main__':
basetest.main()
diff --git a/python/google/protobuf/internal/factory_test1.proto b/python/google/protobuf/internal/factory_test1.proto
index 9f5a3919..d2fbbeec 100644
--- a/python/google/protobuf/internal/factory_test1.proto
+++ b/python/google/protobuf/internal/factory_test1.proto
@@ -30,6 +30,7 @@
// Author: matthewtoia@google.com (Matt Toia)
+syntax = "proto2";
package google.protobuf.python.internal;
diff --git a/python/google/protobuf/internal/factory_test2.proto b/python/google/protobuf/internal/factory_test2.proto
index 27feb6ce..bb1b54ad 100644
--- a/python/google/protobuf/internal/factory_test2.proto
+++ b/python/google/protobuf/internal/factory_test2.proto
@@ -30,6 +30,7 @@
// Author: matthewtoia@google.com (Matt Toia)
+syntax = "proto2";
package google.protobuf.python.internal;
@@ -87,6 +88,12 @@ message LoopMessage {
optional Factory2Message loop = 1;
}
+message MessageWithNestedEnumOnly {
+ enum NestedEnum {
+ NESTED_MESSAGE_ENUM_0 = 0;
+ }
+}
+
extend Factory1Message {
optional string another_field = 1002;
}
diff --git a/python/google/protobuf/internal/generator_test.py b/python/google/protobuf/internal/generator_test.py
index 422fa9a6..03361e66 100755
--- a/python/google/protobuf/internal/generator_test.py
+++ b/python/google/protobuf/internal/generator_test.py
@@ -35,7 +35,7 @@
# indirect testing of the protocol compiler output.
"""Unittest that directly tests the output of the pure-Python protocol
-compiler. See //google/protobuf/reflection_test.py for a test which
+compiler. See //google/protobuf/internal/reflection_test.py for a test which
further ensures that we can use Python protocol message objects as we expect.
"""
@@ -149,7 +149,7 @@ class GeneratorTest(basetest.TestCase):
proto = unittest_custom_options_pb2.TestMessageWithCustomOptions()
enum_options = proto.DESCRIPTOR.enum_types_by_name['AnEnum'].GetOptions()
self.assertTrue(enum_options is not None)
- # TODO(gps): We really should test for the presense of the enum_opt1
+ # TODO(gps): We really should test for the presence of the enum_opt1
# extension and for its value to be set to -789.
def testNestedTypes(self):
diff --git a/python/google/protobuf/internal/import_test_package/BUILD b/python/google/protobuf/internal/import_test_package/BUILD
new file mode 100644
index 00000000..90e59505
--- /dev/null
+++ b/python/google/protobuf/internal/import_test_package/BUILD
@@ -0,0 +1,27 @@
+# Description:
+# An example package that contains nested protos that are imported from
+# __init__.py. See testPackageInitializationImport in reflection_test.py for
+# details.
+
+package(
+ default_visibility = ["//net/proto2/python/internal:__pkg__"],
+)
+
+proto_library(
+ name = "inner_proto",
+ srcs = ["inner.proto"],
+ py_api_version = 2,
+)
+
+proto_library(
+ name = "outer_proto",
+ srcs = ["outer.proto"],
+ py_api_version = 2,
+ deps = [":inner_proto"],
+)
+
+py_library(
+ name = "import_test_package",
+ srcs = ["__init__.py"],
+ deps = [":outer_proto"],
+)
diff --git a/python/google/protobuf/internal/import_test_package/__init__.py b/python/google/protobuf/internal/import_test_package/__init__.py
new file mode 100644
index 00000000..5121dd0e
--- /dev/null
+++ b/python/google/protobuf/internal/import_test_package/__init__.py
@@ -0,0 +1,33 @@
+# 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.
+
+"""Sample module importing a nested proto from itself."""
+
+from google.protobuf.internal.import_test_package import outer_pb2 as myproto
diff --git a/python/google/protobuf/internal/import_test_package/inner.proto b/python/google/protobuf/internal/import_test_package/inner.proto
new file mode 100644
index 00000000..2887c123
--- /dev/null
+++ b/python/google/protobuf/internal/import_test_package/inner.proto
@@ -0,0 +1,37 @@
+// 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 google.protobuf.python.internal.import_test_package;
+
+message Inner {
+ optional int32 value = 1 [default = 57];
+}
diff --git a/python/google/protobuf/internal/import_test_package/outer.proto b/python/google/protobuf/internal/import_test_package/outer.proto
new file mode 100644
index 00000000..a27fb5c8
--- /dev/null
+++ b/python/google/protobuf/internal/import_test_package/outer.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 google.protobuf.python.internal.import_test_package;
+
+import "google/protobuf/internal/import_test_package/inner.proto";
+
+message Outer {
+ optional Inner inner = 1;
+}
diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py
index 48b7ffd4..42e2ad7e 100755
--- a/python/google/protobuf/internal/message_test.py
+++ b/python/google/protobuf/internal/message_test.py
@@ -337,6 +337,20 @@ class MessageTest(basetest.TestCase):
empty.ParseFromString(populated.SerializeToString())
self.assertEqual(str(empty), '')
+ def testRepeatedNestedFieldIteration(self):
+ msg = unittest_pb2.TestAllTypes()
+ msg.repeated_nested_message.add(bb=1)
+ msg.repeated_nested_message.add(bb=2)
+ msg.repeated_nested_message.add(bb=3)
+ msg.repeated_nested_message.add(bb=4)
+
+ self.assertEquals([1, 2, 3, 4],
+ [m.bb for m in msg.repeated_nested_message])
+ self.assertEquals([4, 3, 2, 1],
+ [m.bb for m in reversed(msg.repeated_nested_message)])
+ self.assertEquals([4, 3, 2, 1],
+ [m.bb for m in msg.repeated_nested_message[::-1]])
+
def testSortingRepeatedScalarFieldsDefaultComparator(self):
"""Check some different types with the default comparator."""
message = unittest_pb2.TestAllTypes()
@@ -641,6 +655,32 @@ class MessageTest(basetest.TestCase):
m2.ParseFromString(m.SerializeToString())
self.assertEqual('oneof_uint32', m2.WhichOneof('oneof_field'))
+ def testOneofCopyFrom(self):
+ m = unittest_pb2.TestAllTypes()
+ m.oneof_uint32 = 11
+ m2 = unittest_pb2.TestAllTypes()
+ m2.CopyFrom(m)
+ self.assertEqual('oneof_uint32', m2.WhichOneof('oneof_field'))
+
+ def testOneofNestedMergeFrom(self):
+ m = unittest_pb2.NestedTestAllTypes()
+ m.payload.oneof_uint32 = 11
+ m2 = unittest_pb2.NestedTestAllTypes()
+ m2.payload.oneof_bytes = b'bb'
+ m2.child.payload.oneof_bytes = b'bb'
+ m2.MergeFrom(m)
+ self.assertEqual('oneof_uint32', m2.payload.WhichOneof('oneof_field'))
+ self.assertEqual('oneof_bytes', m2.child.payload.WhichOneof('oneof_field'))
+
+ def testOneofClear(self):
+ m = unittest_pb2.TestAllTypes()
+ m.oneof_uint32 = 11
+ m.Clear()
+ self.assertIsNone(m.WhichOneof('oneof_field'))
+ m.oneof_bytes = b'bb'
+ self.assertTrue(m.HasField('oneof_field'))
+
+
def testSortEmptyRepeatedCompositeContainer(self):
"""Exercise a scenario that has led to segfaults in the past.
"""
diff --git a/python/google/protobuf/internal/missing_enum_values.proto b/python/google/protobuf/internal/missing_enum_values.proto
index e90f0cd3..161fc5e1 100644
--- a/python/google/protobuf/internal/missing_enum_values.proto
+++ b/python/google/protobuf/internal/missing_enum_values.proto
@@ -28,6 +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.
+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 c04e597f..78f14673 100644
--- a/python/google/protobuf/internal/more_extensions.proto
+++ b/python/google/protobuf/internal/more_extensions.proto
@@ -30,6 +30,7 @@
// Author: robinson@google.com (Will Robinson)
+syntax = "proto2";
package google.protobuf.internal;
diff --git a/python/google/protobuf/internal/more_extensions_dynamic.proto b/python/google/protobuf/internal/more_extensions_dynamic.proto
index 88bd9c1b..11f85ef6 100644
--- a/python/google/protobuf/internal/more_extensions_dynamic.proto
+++ b/python/google/protobuf/internal/more_extensions_dynamic.proto
@@ -34,6 +34,7 @@
// generated C++ type is available for the extendee, but the extension is
// defined in a file whose C++ type is not in the binary.
+syntax = "proto2";
import "google/protobuf/internal/more_extensions.proto";
diff --git a/python/google/protobuf/internal/more_messages.proto b/python/google/protobuf/internal/more_messages.proto
index 61db66c5..2c6ab9ef 100644
--- a/python/google/protobuf/internal/more_messages.proto
+++ b/python/google/protobuf/internal/more_messages.proto
@@ -30,6 +30,7 @@
// Author: robinson@google.com (Will Robinson)
+syntax = "proto2";
package google.protobuf.internal;
diff --git a/python/google/protobuf/internal/proto_builder_test.py b/python/google/protobuf/internal/proto_builder_test.py
new file mode 100644
index 00000000..c74db7e7
--- /dev/null
+++ b/python/google/protobuf/internal/proto_builder_test.py
@@ -0,0 +1,77 @@
+#! /usr/bin/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.proto_builder."""
+
+from google.apputils import basetest
+
+from google.protobuf import descriptor_pb2
+from google.protobuf import descriptor_pool
+from google.protobuf import proto_builder
+from google.protobuf import text_format
+
+
+class ProtoBuilderTest(basetest.TestCase):
+
+ def setUp(self):
+ self._fields = {
+ 'foo': descriptor_pb2.FieldDescriptorProto.TYPE_INT64,
+ 'bar': descriptor_pb2.FieldDescriptorProto.TYPE_STRING,
+ }
+
+ def testMakeSimpleProtoClass(self):
+ """Test that we can create a proto class."""
+ proto_cls = proto_builder.MakeSimpleProtoClass(
+ self._fields,
+ full_name='net.proto2.python.public.proto_builder_test.Test')
+ proto = proto_cls()
+ proto.foo = 12345
+ proto.bar = 'asdf'
+ self.assertMultiLineEqual(
+ 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
+
+ def testMakeSameProtoClassTwice(self):
+ """Test that the DescriptorPool is used."""
+ pool = descriptor_pool.DescriptorPool()
+ proto_cls1 = proto_builder.MakeSimpleProtoClass(
+ self._fields,
+ full_name='net.proto2.python.public.proto_builder_test.Test',
+ pool=pool)
+ proto_cls2 = proto_builder.MakeSimpleProtoClass(
+ self._fields,
+ full_name='net.proto2.python.public.proto_builder_test.Test',
+ pool=pool)
+ self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
+
+
+if __name__ == '__main__':
+ basetest.main()
diff --git a/python/google/protobuf/internal/python_message.py b/python/google/protobuf/internal/python_message.py
index a5c26f45..6fda6ae0 100755
--- a/python/google/protobuf/internal/python_message.py
+++ b/python/google/protobuf/internal/python_message.py
@@ -306,6 +306,17 @@ def _DefaultValueConstructorForField(field):
return MakeScalarDefault
+def _ReraiseTypeErrorWithFieldName(message_name, field_name):
+ """Re-raise the currently-handled TypeError with the field name added."""
+ exc = sys.exc_info()[1]
+ if len(exc.args) == 1 and type(exc) is TypeError:
+ # simple TypeError; add field name to exception message
+ exc = TypeError('%s for field %s.%s' % (str(exc), message_name, field_name))
+
+ # re-raise possibly-amended exception with original traceback:
+ raise type(exc), exc, sys.exc_info()[2]
+
+
def _AddInitMethod(message_descriptor, cls):
"""Adds an __init__ method to cls."""
fields = message_descriptor.fields
@@ -338,10 +349,16 @@ def _AddInitMethod(message_descriptor, cls):
self._fields[field] = copy
elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
copy = field._default_constructor(self)
- copy.MergeFrom(field_value)
+ try:
+ copy.MergeFrom(field_value)
+ except TypeError:
+ _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
self._fields[field] = copy
else:
- setattr(self, field_name, field_value)
+ try:
+ setattr(self, field_name, field_value)
+ except TypeError:
+ _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
init.__module__ = None
init.__doc__ = None
@@ -691,6 +708,7 @@ def _AddClearMethod(message_descriptor, cls):
# Clear fields.
self._fields = {}
self._unknown_fields = ()
+ self._oneofs = {}
self._Modified()
cls.Clear = Clear
@@ -993,6 +1011,8 @@ def _AddMergeFromMethod(cls):
field_value.MergeFrom(value)
else:
self._fields[field] = value
+ if field.containing_oneof:
+ self._UpdateOneofState(field)
if msg._unknown_fields:
if not self._unknown_fields:
diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py
index d59815d0..6b24b092 100755
--- a/python/google/protobuf/internal/reflection_test.py
+++ b/python/google/protobuf/internal/reflection_test.py
@@ -35,8 +35,6 @@
pure-Python protocol compiler.
"""
-__author__ = 'robinson@google.com (Will Robinson)'
-
import copy
import gc
import operator
@@ -1252,15 +1250,18 @@ class ReflectionTest(basetest.TestCase):
# Try something that *is* an extension handle, just not for
# this message...
- unknown_handle = more_extensions_pb2.optional_int_extension
- self.assertRaises(KeyError, extendee_proto.HasExtension,
- unknown_handle)
- self.assertRaises(KeyError, extendee_proto.ClearExtension,
- unknown_handle)
- self.assertRaises(KeyError, extendee_proto.Extensions.__getitem__,
- unknown_handle)
- self.assertRaises(KeyError, extendee_proto.Extensions.__setitem__,
- unknown_handle, 5)
+ for unknown_handle in (more_extensions_pb2.optional_int_extension,
+ more_extensions_pb2.optional_message_extension,
+ more_extensions_pb2.repeated_int_extension,
+ more_extensions_pb2.repeated_message_extension):
+ self.assertRaises(KeyError, extendee_proto.HasExtension,
+ unknown_handle)
+ self.assertRaises(KeyError, extendee_proto.ClearExtension,
+ unknown_handle)
+ self.assertRaises(KeyError, extendee_proto.Extensions.__getitem__,
+ unknown_handle)
+ self.assertRaises(KeyError, extendee_proto.Extensions.__setitem__,
+ unknown_handle, 5)
# Try call HasExtension() with a valid handle, but for a
# *repeated* field. (Just as with non-extension repeated
@@ -1669,16 +1670,15 @@ class ReflectionTest(basetest.TestCase):
proto.optional_string = str('Testing')
self.assertEqual(proto.optional_string, unicode('Testing'))
- # Try to assign a 'str' value which contains bytes that aren't 7-bit ASCII.
+ # Try to assign a 'bytes' object which contains non-UTF-8.
self.assertRaises(ValueError,
setattr, proto, 'optional_string', b'a\x80a')
- if str is bytes: # PY2
- # Assign a 'str' object which contains a UTF-8 encoded string.
- self.assertRaises(ValueError,
- setattr, proto, 'optional_string', 'Тест')
- else:
- proto.optional_string = 'Тест'
- # No exception thrown.
+ # No exception: Assign already encoded UTF-8 bytes to a string field.
+ utf8_bytes = u'Тест'.encode('utf-8')
+ proto.optional_string = utf8_bytes
+ # No exception: Assign the a non-ascii unicode object.
+ proto.optional_string = u'Тест'
+ # No exception thrown (normal str assignment containing ASCII).
proto.optional_string = 'abc'
def testStringUTF8Serialization(self):
@@ -1774,6 +1774,24 @@ class ReflectionTest(basetest.TestCase):
proto.optionalgroup.SetInParent()
self.assertTrue(proto.HasField('optionalgroup'))
+ def testPackageInitializationImport(self):
+ """Test that we can import nested messages from their __init__.py.
+
+ Such setup is not trivial since at the time of processing of __init__.py one
+ can't refer to its submodules by name in code, so expressions like
+ google.protobuf.internal.import_test_package.inner_pb2
+ don't work. They do work in imports, so we have assign an alias at import
+ and then use that alias in generated code.
+ """
+ # We import here since it's the import that used to fail, and we want
+ # the failure to have the right context.
+ # pylint: disable=g-import-not-at-top
+ from google.protobuf.internal import import_test_package
+ # pylint: enable=g-import-not-at-top
+ msg = import_test_package.myproto.Outer()
+ # Just check the default value.
+ self.assertEqual(57, msg.inner.value)
+
# Since we had so many tests for protocol buffer equality, we broke these out
# into separate TestCase classes.
@@ -2802,6 +2820,9 @@ class OptionsTest(basetest.TestCase):
class ClassAPITest(basetest.TestCase):
+ @basetest.unittest.skipIf(
+ api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
+ 'C++ implementation requires a call to MakeDescriptor()')
def testMakeClassWithNestedDescriptor(self):
leaf_desc = descriptor.Descriptor('leaf', 'package.parent.child.leaf', '',
containing_type=None, fields=[],
diff --git a/python/google/protobuf/internal/service_reflection_test.py b/python/google/protobuf/internal/service_reflection_test.py
index 07dcf445..d066ae70 100755
--- a/python/google/protobuf/internal/service_reflection_test.py
+++ b/python/google/protobuf/internal/service_reflection_test.py
@@ -118,7 +118,7 @@ class FooUnitTest(basetest.TestCase):
rpc_controller = 'controller'
request = 'request'
- # GetDescriptor now static, still works as instance method for compatability
+ # GetDescriptor now static, still works as instance method for compatibility
self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(),
stub.GetDescriptor())
diff --git a/python/google/protobuf/internal/test_bad_identifiers.proto b/python/google/protobuf/internal/test_bad_identifiers.proto
index 9eb18cb0..c4860ea8 100644
--- a/python/google/protobuf/internal/test_bad_identifiers.proto
+++ b/python/google/protobuf/internal/test_bad_identifiers.proto
@@ -30,6 +30,7 @@
// Author: kenton@google.com (Kenton Varda)
+syntax = "proto2";
package protobuf_unittest;
diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py
index b0a3a5f7..55e3c2c8 100755
--- a/python/google/protobuf/internal/text_format_test.py
+++ b/python/google/protobuf/internal/text_format_test.py
@@ -69,13 +69,18 @@ class TextFormatTest(basetest.TestCase):
message.my_string = '115'
message.my_int = 101
message.my_float = 111
+ message.optional_nested_message.oo = 0
+ message.optional_nested_message.bb = 1
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(
message, use_index_order=True)),
- 'my_string: \"115\"\nmy_int: 101\nmy_float: 111\n')
+ 'my_string: \"115\"\nmy_int: 101\nmy_float: 111\n'
+ 'optional_nested_message {\n oo: 0\n bb: 1\n}\n')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(
- message)), 'my_int: 101\nmy_string: \"115\"\nmy_float: 111\n')
+ message)),
+ 'my_int: 101\nmy_string: \"115\"\nmy_float: 111\n'
+ 'optional_nested_message {\n bb: 1\n oo: 0\n}\n')
def testPrintAllExtensions(self):
message = unittest_pb2.TestAllExtensions()
@@ -511,7 +516,7 @@ class TextFormatTest(basetest.TestCase):
message.repeated_string[4])
self.assertEqual(SLASH + 'x20', message.repeated_string[5])
- def testMergeRepeatedScalars(self):
+ def testMergeDuplicateScalars(self):
message = unittest_pb2.TestAllTypes()
text = ('optional_int32: 42 '
'optional_int32: 67')
@@ -519,7 +524,7 @@ class TextFormatTest(basetest.TestCase):
self.assertIs(r, message)
self.assertEqual(67, message.optional_int32)
- def testParseRepeatedScalars(self):
+ def testParseDuplicateScalars(self):
message = unittest_pb2.TestAllTypes()
text = ('optional_int32: 42 '
'optional_int32: 67')
@@ -529,7 +534,7 @@ class TextFormatTest(basetest.TestCase):
'have multiple "optional_int32" fields.'),
text_format.Parse, text, message)
- def testMergeRepeatedNestedMessageScalars(self):
+ def testMergeDuplicateNestedMessageScalars(self):
message = unittest_pb2.TestAllTypes()
text = ('optional_nested_message { bb: 1 } '
'optional_nested_message { bb: 2 }')
@@ -537,7 +542,7 @@ class TextFormatTest(basetest.TestCase):
self.assertTrue(r is message)
self.assertEqual(2, message.optional_nested_message.bb)
- def testParseRepeatedNestedMessageScalars(self):
+ def testParseDuplicateNestedMessageScalars(self):
message = unittest_pb2.TestAllTypes()
text = ('optional_nested_message { bb: 1 } '
'optional_nested_message { bb: 2 }')
@@ -547,7 +552,7 @@ class TextFormatTest(basetest.TestCase):
'should not have multiple "bb" fields.'),
text_format.Parse, text, message)
- def testMergeRepeatedExtensionScalars(self):
+ def testMergeDuplicateExtensionScalars(self):
message = unittest_pb2.TestAllExtensions()
text = ('[protobuf_unittest.optional_int32_extension]: 42 '
'[protobuf_unittest.optional_int32_extension]: 67')
@@ -556,7 +561,7 @@ class TextFormatTest(basetest.TestCase):
67,
message.Extensions[unittest_pb2.optional_int32_extension])
- def testParseRepeatedExtensionScalars(self):
+ def testParseDuplicateExtensionScalars(self):
message = unittest_pb2.TestAllExtensions()
text = ('[protobuf_unittest.optional_int32_extension]: 42 '
'[protobuf_unittest.optional_int32_extension]: 67')
diff --git a/python/google/protobuf/internal/type_checkers.py b/python/google/protobuf/internal/type_checkers.py
index 56d26460..118725da 100755
--- a/python/google/protobuf/internal/type_checkers.py
+++ b/python/google/protobuf/internal/type_checkers.py
@@ -154,14 +154,13 @@ class UnicodeValueChecker(object):
(proposed_value, type(proposed_value), (bytes, unicode)))
raise TypeError(message)
- # If the value is of type 'bytes' make sure that it is in 7-bit ASCII
- # encoding.
+ # If the value is of type 'bytes' make sure that it is valid UTF-8 data.
if isinstance(proposed_value, bytes):
try:
- proposed_value = proposed_value.decode('ascii')
+ proposed_value = proposed_value.decode('utf-8')
except UnicodeDecodeError:
- raise ValueError('%.1024r has type bytes, but isn\'t in 7-bit ASCII '
- 'encoding. Non-ASCII strings must be converted to '
+ raise ValueError('%.1024r has type bytes, but isn\'t valid UTF-8 '
+ 'encoding. Non-UTF-8 strings must be converted to '
'unicode objects before being added.' %
(proposed_value))
return proposed_value
diff --git a/python/google/protobuf/internal/unknown_fields_test.py b/python/google/protobuf/internal/unknown_fields_test.py
index 71775609..a4dc1f7c 100755
--- a/python/google/protobuf/internal/unknown_fields_test.py
+++ b/python/google/protobuf/internal/unknown_fields_test.py
@@ -38,12 +38,16 @@ __author__ = 'bohdank@google.com (Bohdan Koval)'
from google.apputils import basetest
from google.protobuf import unittest_mset_pb2
from google.protobuf import unittest_pb2
+from google.protobuf.internal import api_implementation
from google.protobuf.internal import encoder
from google.protobuf.internal import missing_enum_values_pb2
from google.protobuf.internal import test_util
from google.protobuf.internal import type_checkers
+@basetest.unittest.skipIf(
+ api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
+ 'C++ implementation does not expose unknown fields to Python')
class UnknownFieldsTest(basetest.TestCase):
def setUp(self):
@@ -175,7 +179,10 @@ class UnknownFieldsTest(basetest.TestCase):
self.assertNotEqual(self.empty_message, message)
-class UnknownFieldsTest(basetest.TestCase):
+@basetest.unittest.skipIf(
+ api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
+ 'C++ implementation does not expose unknown fields to Python')
+class UnknownEnumValuesTest(basetest.TestCase):
def setUp(self):
self.descriptor = missing_enum_values_pb2.TestEnumValues.DESCRIPTOR
diff --git a/python/google/protobuf/message.py b/python/google/protobuf/message.py
index c186452a..88ed9f4c 100755
--- a/python/google/protobuf/message.py
+++ b/python/google/protobuf/message.py
@@ -233,12 +233,21 @@ class Message(object):
raise NotImplementedError
def HasField(self, field_name):
- """Checks if a certain field is set for the message. Note if the
- field_name is not defined in the message descriptor, ValueError will be
- raised."""
+ """Checks if a certain field is set for the message, or if any field inside
+ a oneof group is set. Note that if the field_name is not defined in the
+ message descriptor, ValueError will be raised."""
raise NotImplementedError
def ClearField(self, field_name):
+ """Clears the contents of a given field, or the field set inside a oneof
+ group. If the name neither refers to a defined field or oneof group,
+ ValueError is raised."""
+ raise NotImplementedError
+
+ def WhichOneof(self, oneof_group):
+ """Returns the name of the field that is set inside a oneof group, or
+ None if no field is set. If no group with the given name exists, ValueError
+ will be raised."""
raise NotImplementedError
def HasExtension(self, extension_handle):
diff --git a/python/google/protobuf/proto_builder.py b/python/google/protobuf/proto_builder.py
new file mode 100644
index 00000000..1fa28f1a
--- /dev/null
+++ b/python/google/protobuf/proto_builder.py
@@ -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.
+
+"""Dynamic Protobuf class creator."""
+
+import hashlib
+import os
+
+from google.protobuf import descriptor_pb2
+from google.protobuf import message_factory
+
+
+def _GetMessageFromFactory(factory, full_name):
+ """Get a proto class from the MessageFactory by name.
+
+ Args:
+ factory: a MessageFactory instance.
+ full_name: str, the fully qualified name of the proto type.
+ Returns:
+ a class, for the type identified by full_name.
+ Raises:
+ KeyError, if the proto is not found in the factory's descriptor pool.
+ """
+ proto_descriptor = factory.pool.FindMessageTypeByName(full_name)
+ proto_cls = factory.GetPrototype(proto_descriptor)
+ return proto_cls
+
+
+def MakeSimpleProtoClass(fields, full_name, pool=None):
+ """Create a Protobuf class whose fields are basic types.
+
+ Note: this doesn't validate field names!
+
+ Args:
+ fields: dict of {name: field_type} mappings for each field in the proto.
+ full_name: str, the fully-qualified name of the proto type.
+ pool: optional DescriptorPool instance.
+ Returns:
+ a class, the new protobuf class with a FileDescriptor.
+ """
+ factory = message_factory.MessageFactory(pool=pool)
+ try:
+ proto_cls = _GetMessageFromFactory(factory, full_name)
+ return proto_cls
+ except KeyError:
+ # The factory's DescriptorPool doesn't know about this class yet.
+ pass
+
+ # Use a consistent file name that is unlikely to conflict with any imported
+ # proto files.
+ fields_hash = hashlib.sha1()
+ for f_name, f_type in sorted(fields.items()):
+ fields_hash.update(f_name.encode('utf8'))
+ fields_hash.update(str(f_type).encode('utf8'))
+ proto_file_name = fields_hash.hexdigest() + '.proto'
+
+ package, name = full_name.rsplit('.', 1)
+ file_proto = descriptor_pb2.FileDescriptorProto()
+ file_proto.name = os.path.join(package.replace('.', '/'), proto_file_name)
+ file_proto.package = package
+ desc_proto = file_proto.message_type.add()
+ desc_proto.name = name
+ for f_number, (f_name, f_type) in enumerate(sorted(fields.items()), 1):
+ field_proto = desc_proto.field.add()
+ field_proto.name = f_name
+ field_proto.number = f_number
+ field_proto.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
+ field_proto.type = f_type
+
+ factory.pool.Add(file_proto)
+ return _GetMessageFromFactory(factory, full_name)
diff --git a/python/google/protobuf/pyext/cpp_message.py b/python/google/protobuf/pyext/cpp_message.py
index dcf34a02..037bb72c 100644
--- a/python/google/protobuf/pyext/cpp_message.py
+++ b/python/google/protobuf/pyext/cpp_message.py
@@ -53,9 +53,5 @@ def NewMessage(bases, message_descriptor, dictionary):
def InitMessage(message_descriptor, cls):
- """Constructs a new message instance (called before instance's __init__)."""
-
- def SubInit(self, **kwargs):
- super(cls, self).__init__(message_descriptor, **kwargs)
- cls.__init__ = SubInit
+ """Finalizes the creation of a message class."""
cls.AddDescriptors(message_descriptor)
diff --git a/python/google/protobuf/pyext/descriptor.cc b/python/google/protobuf/pyext/descriptor.cc
index 3f7be73c..7343c0b7 100644
--- a/python/google/protobuf/pyext/descriptor.cc
+++ b/python/google/protobuf/pyext/descriptor.cc
@@ -35,6 +35,7 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/pyext/descriptor.h>
+#include <google/protobuf/pyext/message.h>
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
#define C(str) const_cast<char*>(str)
@@ -46,7 +47,7 @@
#error "Python 3.0 - 3.2 are not supported."
#else
#define PyString_AsString(ob) \
- (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AS_STRING(ob))
+ (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob))
#endif
#endif
@@ -65,10 +66,80 @@ namespace python {
static google::protobuf::DescriptorPool* g_descriptor_pool = NULL;
+namespace cmessage_descriptor {
+
+static void Dealloc(CMessageDescriptor* self) {
+ Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
+}
+
+static PyObject* GetFullName(CMessageDescriptor* self, void *closure) {
+ return PyString_FromStringAndSize(
+ self->descriptor->full_name().c_str(),
+ self->descriptor->full_name().size());
+}
+
+static PyObject* GetName(CMessageDescriptor *self, void *closure) {
+ return PyString_FromStringAndSize(
+ self->descriptor->name().c_str(),
+ self->descriptor->name().size());
+}
+
+static PyGetSetDef Getters[] = {
+ { C("full_name"), (getter)GetFullName, NULL, "Full name", NULL},
+ { C("name"), (getter)GetName, NULL, "Unqualified name", NULL},
+ {NULL}
+};
+
+} // namespace cmessage_descriptor
+
+PyTypeObject CMessageDescriptor_Type = {
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)
+ C("google.protobuf.internal."
+ "_net_proto2___python."
+ "CMessageDescriptor"), // tp_name
+ sizeof(CMessageDescriptor), // tp_basicsize
+ 0, // tp_itemsize
+ (destructor)cmessage_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
+ C("A Message Descriptor"), // 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
+ cmessage_descriptor::Getters, // tp_getset
+ 0, // tp_base
+ 0, // tp_dict
+ 0, // tp_descr_get
+ 0, // tp_descr_set
+ 0, // tp_dictoffset
+ 0, // tp_init
+ PyType_GenericAlloc, // tp_alloc
+ PyType_GenericNew, // tp_new
+ PyObject_Del, // tp_free
+};
+
+
namespace cfield_descriptor {
static void Dealloc(CFieldDescriptor* self) {
- Py_CLEAR(self->descriptor_field);
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}
@@ -98,7 +169,7 @@ static PyObject* GetID(CFieldDescriptor *self, void *closure) {
static PyGetSetDef Getters[] = {
{ C("full_name"), (getter)GetFullName, NULL, "Full name", NULL},
- { C("name"), (getter)GetName, NULL, "last name", NULL},
+ { C("name"), (getter)GetName, NULL, "Unqualified name", NULL},
{ C("cpp_type"), (getter)GetCppType, NULL, "C++ Type", NULL},
{ C("label"), (getter)GetLabel, NULL, "Label", NULL},
{ C("id"), (getter)GetID, NULL, "ID", NULL},
@@ -151,13 +222,58 @@ PyTypeObject CFieldDescriptor_Type = {
PyObject_Del, // tp_free
};
+
namespace cdescriptor_pool {
-static void Dealloc(CDescriptorPool* self) {
+PyDescriptorPool* NewDescriptorPool() {
+ PyDescriptorPool* cdescriptor_pool = PyObject_New(
+ PyDescriptorPool, &PyDescriptorPool_Type);
+ if (cdescriptor_pool == NULL) {
+ return NULL;
+ }
+
+ // Build a DescriptorPool for messages only declared in Python libraries.
+ // generated_pool() contains all messages linked in C++ libraries, and is used
+ // as underlay.
+ cdescriptor_pool->pool = new google::protobuf::DescriptorPool(
+ google::protobuf::DescriptorPool::generated_pool());
+
+ // TODO(amauryfa): Rewrite the SymbolDatabase in C so that it uses the same
+ // storage.
+ cdescriptor_pool->classes_by_descriptor =
+ new PyDescriptorPool::ClassesByMessageMap();
+
+ return cdescriptor_pool;
+}
+
+static void Dealloc(PyDescriptorPool* self) {
+ typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
+ for (iterator it = self->classes_by_descriptor->begin();
+ it != self->classes_by_descriptor->end(); ++it) {
+ Py_DECREF(it->second);
+ }
+ delete self->classes_by_descriptor;
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
}
-static PyObject* NewCDescriptor(
+const google::protobuf::Descriptor* FindMessageTypeByName(PyDescriptorPool* self,
+ const string& name) {
+ return self->pool->FindMessageTypeByName(name);
+}
+
+static PyObject* NewCMessageDescriptor(
+ const google::protobuf::Descriptor* message_descriptor) {
+ CMessageDescriptor* cmessage_descriptor = PyObject_New(
+ CMessageDescriptor, &CMessageDescriptor_Type);
+ if (cmessage_descriptor == NULL) {
+ return NULL;
+ }
+ cmessage_descriptor->descriptor = message_descriptor;
+
+ return reinterpret_cast<PyObject*>(cmessage_descriptor);
+}
+
+static PyObject* NewCFieldDescriptor(
const google::protobuf::FieldDescriptor* field_descriptor) {
CFieldDescriptor* cfield_descriptor = PyObject_New(
CFieldDescriptor, &CFieldDescriptor_Type);
@@ -165,12 +281,63 @@ static PyObject* NewCDescriptor(
return NULL;
}
cfield_descriptor->descriptor = field_descriptor;
- cfield_descriptor->descriptor_field = NULL;
return reinterpret_cast<PyObject*>(cfield_descriptor);
}
-PyObject* FindFieldByName(CDescriptorPool* self, PyObject* name) {
+// Add a message class to our database.
+const google::protobuf::Descriptor* RegisterMessageClass(
+ PyDescriptorPool* self, PyObject *message_class, PyObject* descriptor) {
+ ScopedPyObjectPtr full_message_name(
+ PyObject_GetAttrString(descriptor, "full_name"));
+ const char* full_name = PyString_AsString(full_message_name);
+ if (full_name == NULL) {
+ return NULL;
+ }
+ const Descriptor *message_descriptor =
+ self->pool->FindMessageTypeByName(full_name);
+ if (!message_descriptor) {
+ PyErr_Format(PyExc_TypeError, "Could not find C++ descriptor for '%s'",
+ full_name);
+ return NULL;
+ }
+ Py_INCREF(message_class);
+ typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
+ std::pair<iterator, bool> ret = self->classes_by_descriptor->insert(
+ make_pair(message_descriptor, message_class));
+ if (!ret.second) {
+ // Update case: DECREF the previous value.
+ Py_DECREF(ret.first->second);
+ ret.first->second = message_class;
+ }
+
+ // Also add the C++ descriptor to the Python descriptor class.
+ ScopedPyObjectPtr cdescriptor(NewCMessageDescriptor(message_descriptor));
+ if (cdescriptor == NULL) {
+ return NULL;
+ }
+ if (PyObject_SetAttrString(
+ descriptor, "_cdescriptor", cdescriptor) < 0) {
+ return NULL;
+ }
+ return message_descriptor;
+}
+
+// Retrieve the message class added to our database.
+PyObject *GetMessageClass(PyDescriptorPool* self,
+ const Descriptor *message_descriptor) {
+ typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
+ iterator ret = self->classes_by_descriptor->find(message_descriptor);
+ if (ret == self->classes_by_descriptor->end()) {
+ PyErr_Format(PyExc_TypeError, "No message class registered for '%s'",
+ message_descriptor->full_name().c_str());
+ return NULL;
+ } else {
+ return ret->second;
+ }
+}
+
+PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* name) {
const char* full_field_name = PyString_AsString(name);
if (full_field_name == NULL) {
return NULL;
@@ -186,10 +353,10 @@ PyObject* FindFieldByName(CDescriptorPool* self, PyObject* name) {
return NULL;
}
- return NewCDescriptor(field_descriptor);
+ return NewCFieldDescriptor(field_descriptor);
}
-PyObject* FindExtensionByName(CDescriptorPool* self, PyObject* arg) {
+PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg) {
const char* full_field_name = PyString_AsString(arg);
if (full_field_name == NULL) {
return NULL;
@@ -203,7 +370,7 @@ PyObject* FindExtensionByName(CDescriptorPool* self, PyObject* arg) {
return NULL;
}
- return NewCDescriptor(field_descriptor);
+ return NewCFieldDescriptor(field_descriptor);
}
static PyMethodDef Methods[] = {
@@ -220,12 +387,12 @@ static PyMethodDef Methods[] = {
} // namespace cdescriptor_pool
-PyTypeObject CDescriptorPool_Type = {
+PyTypeObject PyDescriptorPool_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
C("google.protobuf.internal."
"_net_proto2___python."
"CFieldDescriptor"), // tp_name
- sizeof(CDescriptorPool), // tp_basicsize
+ sizeof(PyDescriptorPool), // tp_basicsize
0, // tp_itemsize
(destructor)cdescriptor_pool::Dealloc, // tp_dealloc
0, // tp_print
@@ -259,29 +426,11 @@ PyTypeObject CDescriptorPool_Type = {
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
- PyType_GenericAlloc, // tp_alloc
- PyType_GenericNew, // tp_new
+ 0, // tp_alloc
+ 0, // tp_new
PyObject_Del, // tp_free
};
-google::protobuf::DescriptorPool* GetDescriptorPool() {
- if (g_descriptor_pool == NULL) {
- g_descriptor_pool = new google::protobuf::DescriptorPool(
- google::protobuf::DescriptorPool::generated_pool());
- }
- return g_descriptor_pool;
-}
-
-PyObject* Python_NewCDescriptorPool(PyObject* ignored, PyObject* args) {
- CDescriptorPool* cdescriptor_pool = PyObject_New(
- CDescriptorPool, &CDescriptorPool_Type);
- if (cdescriptor_pool == NULL) {
- return NULL;
- }
- cdescriptor_pool->pool = GetDescriptorPool();
- return reinterpret_cast<PyObject*>(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.
@@ -321,6 +470,8 @@ PyObject* Python_BuildFile(PyObject* ignored, PyObject* arg) {
return NULL;
}
+ // If the file was already part of a C++ library, all its descriptors are in
+ // the underlying pool. No need to do anything else.
if (google::protobuf::DescriptorPool::generated_pool()->FindFileByName(
file_proto.name()) != NULL) {
Py_RETURN_NONE;
@@ -328,8 +479,8 @@ PyObject* Python_BuildFile(PyObject* ignored, PyObject* arg) {
BuildFileErrorCollector error_collector;
const google::protobuf::FileDescriptor* descriptor =
- GetDescriptorPool()->BuildFileCollectingErrors(file_proto,
- &error_collector);
+ GetDescriptorPool()->pool->BuildFileCollectingErrors(file_proto,
+ &error_collector);
if (descriptor == NULL) {
PyErr_Format(PyExc_TypeError,
"Couldn't build proto file into descriptor pool!\n%s",
@@ -341,12 +492,13 @@ PyObject* Python_BuildFile(PyObject* ignored, PyObject* arg) {
}
bool InitDescriptor() {
- CFieldDescriptor_Type.tp_new = PyType_GenericNew;
+ if (PyType_Ready(&CMessageDescriptor_Type) < 0)
+ return false;
if (PyType_Ready(&CFieldDescriptor_Type) < 0)
return false;
- CDescriptorPool_Type.tp_new = PyType_GenericNew;
- if (PyType_Ready(&CDescriptorPool_Type) < 0)
+ PyDescriptorPool_Type.tp_new = PyType_GenericNew;
+ if (PyType_Ready(&PyDescriptorPool_Type) < 0)
return false;
return true;
diff --git a/python/google/protobuf/pyext/descriptor.h b/python/google/protobuf/pyext/descriptor.h
index ae7a1b9c..9e5957b5 100644
--- a/python/google/protobuf/pyext/descriptor.h
+++ b/python/google/protobuf/pyext/descriptor.h
@@ -36,6 +36,8 @@
#include <Python.h>
#include <structmember.h>
+#include <google/protobuf/stubs/hash.h>
+
#include <google/protobuf/descriptor.h>
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
@@ -48,46 +50,90 @@ namespace google {
namespace protobuf {
namespace python {
+typedef struct CMessageDescriptor {
+ PyObject_HEAD
+
+ // The proto2 descriptor that this object represents.
+ const google::protobuf::Descriptor* descriptor;
+} CMessageDescriptor;
+
+
typedef struct CFieldDescriptor {
PyObject_HEAD
// The proto2 descriptor that this object represents.
const google::protobuf::FieldDescriptor* descriptor;
-
- // Reference to the original field object in the Python DESCRIPTOR.
- PyObject* descriptor_field;
} CFieldDescriptor;
-typedef struct {
+
+// Wraps operations to the global DescriptorPool which contains information
+// about all messages and fields.
+//
+// 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.
+typedef struct PyDescriptorPool {
PyObject_HEAD
- const google::protobuf::DescriptorPool* pool;
-} CDescriptorPool;
+ google::protobuf::DescriptorPool* pool;
+ // Make our own mapping to retrieve Python classes from C++ descriptors.
+ //
+ // Descriptor pointers stored here are owned by the DescriptorPool above.
+ // Python references to classes are owned by this PyDescriptorPool.
+ typedef hash_map<const Descriptor *, PyObject *> ClassesByMessageMap;
+ ClassesByMessageMap *classes_by_descriptor;
+} PyDescriptorPool;
+
+
+extern PyTypeObject CMessageDescriptor_Type;
extern PyTypeObject CFieldDescriptor_Type;
-extern PyTypeObject CDescriptorPool_Type;
+extern PyTypeObject PyDescriptorPool_Type;
+
namespace cdescriptor_pool {
+// Builds a new DescriptorPool. Normally called only once per process.
+PyDescriptorPool* NewDescriptorPool();
+
+// Looks up a message by name.
+// Returns a message Descriptor, or NULL if not found.
+const google::protobuf::Descriptor* FindMessageTypeByName(PyDescriptorPool* self,
+ const string& name);
+
+// Registers a new Python class for the given message descriptor.
+// Returns the message Descriptor.
+// On error, returns NULL with a Python exception set.
+const google::protobuf::Descriptor* RegisterMessageClass(
+ PyDescriptorPool* self, PyObject *message_class, PyObject *descriptor);
+
+// Retrieves the Python class registered with the given message descriptor.
+//
+// Returns a *borrowed* reference if found, otherwise returns NULL with an
+// exception set.
+PyObject *GetMessageClass(PyDescriptorPool* self,
+ const Descriptor *message_descriptor);
+
// Looks up a field by name. Returns a CDescriptor corresponding to
// the field on success, or NULL on failure.
//
// Returns a new reference.
-PyObject* FindFieldByName(CDescriptorPool* self, PyObject* name);
+PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* name);
// Looks up an extension by name. Returns a CDescriptor corresponding
// to the field on success, or NULL on failure.
//
// Returns a new reference.
-PyObject* FindExtensionByName(CDescriptorPool* self, PyObject* arg);
-
+PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg);
} // namespace cdescriptor_pool
-PyObject* Python_NewCDescriptorPool(PyObject* ignored, PyObject* args);
PyObject* Python_BuildFile(PyObject* ignored, PyObject* args);
bool InitDescriptor();
-google::protobuf::DescriptorPool* GetDescriptorPool();
} // namespace python
} // namespace protobuf
diff --git a/python/google/protobuf/pyext/extension_dict.cc b/python/google/protobuf/pyext/extension_dict.cc
index 3861c794..d83b57d5 100644
--- a/python/google/protobuf/pyext/extension_dict.cc
+++ b/python/google/protobuf/pyext/extension_dict.cc
@@ -62,22 +62,6 @@ static google::protobuf::Message* GetMessage(ExtensionDict* self) {
}
}
-CFieldDescriptor* InternalGetCDescriptorFromExtension(PyObject* extension) {
- PyObject* cdescriptor = PyObject_GetAttrString(extension, "_cdescriptor");
- if (cdescriptor == NULL) {
- PyErr_SetString(PyExc_KeyError, "Unregistered extension.");
- return NULL;
- }
- if (!PyObject_TypeCheck(cdescriptor, &CFieldDescriptor_Type)) {
- PyErr_SetString(PyExc_TypeError, "Not a CFieldDescriptor");
- Py_DECREF(cdescriptor);
- return NULL;
- }
- CFieldDescriptor* descriptor =
- reinterpret_cast<CFieldDescriptor*>(cdescriptor);
- return descriptor;
-}
-
PyObject* len(ExtensionDict* self) {
#if PY_MAJOR_VERSION >= 3
return PyLong_FromLong(PyDict_Size(self->values));
@@ -118,16 +102,15 @@ int ReleaseExtension(ExtensionDict* self,
}
PyObject* subscript(ExtensionDict* self, PyObject* key) {
- CFieldDescriptor* cdescriptor = InternalGetCDescriptorFromExtension(
- key);
- if (cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor =
+ cmessage::GetExtensionDescriptor(key);
+ if (descriptor == NULL) {
return NULL;
}
- ScopedPyObjectPtr py_cdescriptor(reinterpret_cast<PyObject*>(cdescriptor));
- const google::protobuf::FieldDescriptor* descriptor = cdescriptor->descriptor;
- if (descriptor == NULL) {
+ if (!CheckFieldBelongsToMessage(descriptor, self->parent->message)) {
return NULL;
}
+
if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
return cmessage::InternalGetScalar(self->parent, descriptor);
@@ -142,7 +125,7 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) {
if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
PyObject* sub_message = cmessage::InternalGetSubMessage(
- self->parent, cdescriptor);
+ self->parent, descriptor);
if (sub_message == NULL) {
return NULL;
}
@@ -152,33 +135,21 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) {
if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
- // COPIED
- PyObject* py_container = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&RepeatedCompositeContainer_Type),
- NULL);
+ PyObject *message_class = cdescriptor_pool::GetMessageClass(
+ GetDescriptorPool(), descriptor->message_type());
+ if (message_class == NULL) {
+ return NULL;
+ }
+ PyObject* py_container = repeated_composite_container::NewContainer(
+ self->parent, descriptor, message_class);
if (py_container == NULL) {
return NULL;
}
- RepeatedCompositeContainer* container =
- reinterpret_cast<RepeatedCompositeContainer*>(py_container);
- PyObject* field = cdescriptor->descriptor_field;
- PyObject* message_type = PyObject_GetAttrString(field, "message_type");
- PyObject* concrete_class = PyObject_GetAttrString(message_type,
- "_concrete_class");
- container->owner = self->owner;
- container->parent = self->parent;
- container->message = self->parent->message;
- container->parent_field = cdescriptor;
- container->subclass_init = concrete_class;
- Py_DECREF(message_type);
PyDict_SetItem(self->values, key, py_container);
return py_container;
} else {
- // COPIED
- ScopedPyObjectPtr init_args(PyTuple_Pack(2, self->parent, cdescriptor));
- PyObject* py_container = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&RepeatedScalarContainer_Type),
- init_args);
+ PyObject* py_container = repeated_scalar_container::NewContainer(
+ self->parent, descriptor);
if (py_container == NULL) {
return NULL;
}
@@ -191,13 +162,15 @@ PyObject* subscript(ExtensionDict* self, PyObject* key) {
}
int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value) {
- CFieldDescriptor* cdescriptor = InternalGetCDescriptorFromExtension(
- key);
- if (cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor =
+ cmessage::GetExtensionDescriptor(key);
+ if (descriptor == NULL) {
+ return -1;
+ }
+ if (!CheckFieldBelongsToMessage(descriptor, self->parent->message)) {
return -1;
}
- ScopedPyObjectPtr py_cdescriptor(reinterpret_cast<PyObject*>(cdescriptor));
- const google::protobuf::FieldDescriptor* descriptor = cdescriptor->descriptor;
+
if (descriptor->label() != FieldDescriptor::LABEL_OPTIONAL ||
descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
PyErr_SetString(PyExc_TypeError, "Extension is repeated and/or composite "
@@ -214,20 +187,18 @@ int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value) {
}
PyObject* ClearExtension(ExtensionDict* self, PyObject* extension) {
- CFieldDescriptor* cdescriptor = InternalGetCDescriptorFromExtension(
- extension);
- if (cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor =
+ cmessage::GetExtensionDescriptor(extension);
+ if (descriptor == NULL) {
return NULL;
}
- ScopedPyObjectPtr py_cdescriptor(reinterpret_cast<PyObject*>(cdescriptor));
PyObject* value = PyDict_GetItem(self->values, extension);
if (value != NULL) {
- if (ReleaseExtension(self, value, cdescriptor->descriptor) < 0) {
+ if (ReleaseExtension(self, value, descriptor) < 0) {
return NULL;
}
}
- if (cmessage::ClearFieldByDescriptor(self->parent,
- cdescriptor->descriptor) == NULL) {
+ if (cmessage::ClearFieldByDescriptor(self->parent, descriptor) == NULL) {
return NULL;
}
if (PyDict_DelItem(self->values, extension) < 0) {
@@ -237,14 +208,12 @@ PyObject* ClearExtension(ExtensionDict* self, PyObject* extension) {
}
PyObject* HasExtension(ExtensionDict* self, PyObject* extension) {
- CFieldDescriptor* cdescriptor = InternalGetCDescriptorFromExtension(
- extension);
- if (cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor =
+ cmessage::GetExtensionDescriptor(extension);
+ if (descriptor == NULL) {
return NULL;
}
- ScopedPyObjectPtr py_cdescriptor(reinterpret_cast<PyObject*>(cdescriptor));
- PyObject* result = cmessage::HasFieldByDescriptor(
- self->parent, cdescriptor->descriptor);
+ PyObject* result = cmessage::HasFieldByDescriptor(self->parent, descriptor);
return result;
}
@@ -263,11 +232,18 @@ PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* name) {
}
}
-int init(ExtensionDict* self, PyObject* args, PyObject* kwargs) {
- self->parent = NULL;
- self->message = NULL;
+ExtensionDict* NewExtensionDict(CMessage *parent) {
+ ExtensionDict* self = reinterpret_cast<ExtensionDict*>(
+ PyType_GenericAlloc(&ExtensionDict_Type, 0));
+ if (self == NULL) {
+ return NULL;
+ }
+
+ self->parent = parent; // Store a borrowed reference.
+ self->message = parent->message;
+ self->owner = parent->owner;
self->values = PyDict_New();
- return 0;
+ return self;
}
void dealloc(ExtensionDict* self) {
@@ -330,7 +306,7 @@ PyTypeObject ExtensionDict_Type = {
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
- (initproc)extension_dict::init, // tp_init
+ 0, // tp_init
};
} // namespace python
diff --git a/python/google/protobuf/pyext/extension_dict.h b/python/google/protobuf/pyext/extension_dict.h
index 13c874a4..47625e23 100644
--- a/python/google/protobuf/pyext/extension_dict.h
+++ b/python/google/protobuf/pyext/extension_dict.h
@@ -53,13 +53,26 @@ using internal::shared_ptr;
namespace python {
struct CMessage;
-struct CFieldDescriptor;
typedef struct ExtensionDict {
PyObject_HEAD;
+
+ // This is the top-level C++ Message object that owns the whole
+ // proto tree. Every Python container class 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.
shared_ptr<Message> owner;
+
+ // Weak reference to parent message. Used to make sure
+ // the parent is writable when an extension field is modified.
CMessage* parent;
+
+ // Pointer to the C++ Message that this ExtensionDict extends.
+ // Not owned by us.
Message* message;
+
+ // A dict of child messages, indexed by Extension descriptors.
+ // Similar to CMessage::composite_fields.
PyObject* values;
} ExtensionDict;
@@ -67,11 +80,8 @@ extern PyTypeObject ExtensionDict_Type;
namespace extension_dict {
-// Gets the _cdescriptor reference to a CFieldDescriptor object given a
-// python descriptor object.
-//
-// Returns a new reference.
-CFieldDescriptor* InternalGetCDescriptorFromExtension(PyObject* extension);
+// Builds an Extensions dict for a specific message.
+ExtensionDict* NewExtensionDict(CMessage *parent);
// Gets the number of extension values in this ExtensionDict as a python object.
//
diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc
index 9fb7083f..cd956e0e 100644
--- a/python/google/protobuf/pyext/message.cc
+++ b/python/google/protobuf/pyext/message.cc
@@ -71,7 +71,7 @@
#error "Python 3.0 - 3.2 are not supported."
#else
#define PyString_AsString(ob) \
- (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AS_STRING(ob))
+ (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob))
#endif
#endif
@@ -81,7 +81,9 @@ namespace python {
// Forward declarations
namespace cmessage {
-static PyObject* GetDescriptor(CMessage* self, PyObject* name);
+static const google::protobuf::FieldDescriptor* GetFieldDescriptor(
+ CMessage* self, PyObject* name);
+static const google::protobuf::Descriptor* GetMessageDescriptor(PyTypeObject* cls);
static string GetMessageName(CMessage* self);
int InternalReleaseFieldByDescriptor(
const google::protobuf::FieldDescriptor* field_descriptor,
@@ -147,12 +149,15 @@ int ForEachCompositeField(CMessage* self, Visitor visitor) {
PyObject* key;
PyObject* field;
+ // Never use self->message in this function, it may be already freed.
+ const google::protobuf::Descriptor* message_descriptor =
+ cmessage::GetMessageDescriptor(Py_TYPE(self));
+
// Visit normal fields.
while (PyDict_Next(self->composite_fields, &pos, &key, &field)) {
- PyObject* cdescriptor = cmessage::GetDescriptor(self, key);
- if (cdescriptor != NULL) {
- const google::protobuf::FieldDescriptor* descriptor =
- reinterpret_cast<CFieldDescriptor*>(cdescriptor)->descriptor;
+ const google::protobuf::FieldDescriptor* descriptor =
+ message_descriptor->FindFieldByName(PyString_AsString(key));
+ if (descriptor != NULL) {
if (VisitCompositeField(descriptor, field, visitor) == -1)
return -1;
}
@@ -161,11 +166,11 @@ int ForEachCompositeField(CMessage* self, Visitor visitor) {
// Visit extension fields.
if (self->extensions != NULL) {
while (PyDict_Next(self->extensions->values, &pos, &key, &field)) {
- CFieldDescriptor* cdescriptor =
- extension_dict::InternalGetCDescriptorFromExtension(key);
- if (cdescriptor == NULL)
+ const google::protobuf::FieldDescriptor* descriptor =
+ cmessage::GetExtensionDescriptor(key);
+ if (descriptor == NULL)
return -1;
- if (VisitCompositeField(cdescriptor->descriptor, field, visitor) == -1)
+ if (VisitCompositeField(descriptor, field, visitor) == -1)
return -1;
}
}
@@ -191,18 +196,19 @@ PyObject* PickleError_class;
// Constant PyString values used for GetAttr/GetItem.
static PyObject* kDESCRIPTOR;
-static PyObject* k__descriptors;
+static PyObject* k_cdescriptor;
static PyObject* kfull_name;
static PyObject* kname;
-static PyObject* kmessage_type;
-static PyObject* kis_extendable;
static PyObject* kextensions_by_name;
static PyObject* k_extensions_by_name;
static PyObject* k_extensions_by_number;
-static PyObject* k_concrete_class;
static PyObject* kfields_by_name;
-static CDescriptorPool* descriptor_pool;
+static PyDescriptorPool* descriptor_pool;
+
+PyDescriptorPool* GetDescriptorPool() {
+ return descriptor_pool;
+}
/* Is 64bit */
void FormatTypeError(PyObject* arg, char* expected_types) {
@@ -313,12 +319,12 @@ bool CheckAndSetString(
}
if (PyBytes_Check(arg)) {
- PyObject* unicode = PyUnicode_FromEncodedObject(arg, "ascii", NULL);
+ PyObject* unicode = PyUnicode_FromEncodedObject(arg, "utf-8", NULL);
if (unicode == NULL) {
PyObject* repr = PyObject_Repr(arg);
PyErr_Format(PyExc_ValueError,
- "%s has type str, but isn't in 7-bit ASCII "
- "encoding. Non-ASCII strings must be converted to "
+ "%s has type str, but isn't valid UTF-8 "
+ "encoding. Non-UTF-8 strings must be converted to "
"unicode objects before being added.",
PyString_AsString(repr));
Py_DECREF(repr);
@@ -335,12 +341,9 @@ bool CheckAndSetString(
PyObject* encoded_string = NULL;
if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
if (PyBytes_Check(arg)) {
-#if PY_MAJOR_VERSION < 3
- encoded_string = PyString_AsEncodedObject(arg, "utf-8", NULL);
-#else
+ // The bytes were already validated as correctly encoded UTF-8 above.
encoded_string = arg; // Already encoded.
Py_INCREF(encoded_string);
-#endif
} else {
encoded_string = PyUnicode_AsEncodedObject(arg, "utf-8", NULL);
}
@@ -391,6 +394,17 @@ PyObject* ToStringObject(
return result;
}
+bool CheckFieldBelongsToMessage(const google::protobuf::FieldDescriptor* field_descriptor,
+ const google::protobuf::Message* message) {
+ if (message->GetDescriptor() == field_descriptor->containing_type()) {
+ return true;
+ }
+ PyErr_Format(PyExc_KeyError, "Field '%s' does not belong to message '%s'",
+ field_descriptor->full_name().c_str(),
+ message->GetDescriptor()->full_name().c_str());
+ return false;
+}
+
google::protobuf::DynamicMessageFactory* global_message_factory;
namespace cmessage {
@@ -489,7 +503,7 @@ int AssureWritable(CMessage* self) {
google::protobuf::Message* parent_message = self->parent->message;
google::protobuf::Message* mutable_message = GetMutableMessage(
self->parent,
- self->parent_field->descriptor);
+ self->parent_field_descriptor);
if (mutable_message == NULL) {
return -1;
}
@@ -512,26 +526,61 @@ int AssureWritable(CMessage* self) {
// --- Globals:
-static PyObject* GetDescriptor(CMessage* self, PyObject* name) {
- PyObject* descriptors =
- PyDict_GetItem(Py_TYPE(self)->tp_dict, k__descriptors);
- if (descriptors == NULL) {
- PyErr_SetString(PyExc_TypeError, "No __descriptors");
+// Retrieve the C++ Descriptor of a message class.
+// On error, returns NULL with an exception set.
+static const google::protobuf::Descriptor* GetMessageDescriptor(PyTypeObject* cls) {
+ ScopedPyObjectPtr descriptor(PyObject_GetAttr(
+ reinterpret_cast<PyObject*>(cls), kDESCRIPTOR));
+ if (descriptor == NULL) {
+ PyErr_SetString(PyExc_TypeError, "Message class has no DESCRIPTOR");
+ return NULL;
+ }
+ ScopedPyObjectPtr cdescriptor(PyObject_GetAttr(descriptor, k_cdescriptor));
+ if (cdescriptor == NULL) {
+ PyErr_SetString(PyExc_TypeError, "Unregistered message.");
+ return NULL;
+ }
+ if (!PyObject_TypeCheck(cdescriptor, &CMessageDescriptor_Type)) {
+ PyErr_SetString(PyExc_TypeError, "Not a CMessageDescriptor");
return NULL;
}
+ return reinterpret_cast<CMessageDescriptor*>(cdescriptor.get())->descriptor;
+}
- return PyDict_GetItem(descriptors, name);
+// Retrieve a C++ FieldDescriptor for a message attribute.
+// The C++ message must be valid.
+// TODO(amauryfa): This function should stay internal, because exception
+// handling is not consistent.
+static const google::protobuf::FieldDescriptor* GetFieldDescriptor(
+ CMessage* self, PyObject* name) {
+ const google::protobuf::Descriptor *message_descriptor = self->message->GetDescriptor();
+ const char* field_name = PyString_AsString(name);
+ if (field_name == NULL) {
+ return NULL;
+ }
+ const google::protobuf::FieldDescriptor *field_descriptor =
+ message_descriptor->FindFieldByName(field_name);
+ if (field_descriptor == NULL) {
+ // Note: No exception is set!
+ return NULL;
+ }
+ return field_descriptor;
}
-static const google::protobuf::Message* CreateMessage(const char* message_type) {
- string message_name(message_type);
- const google::protobuf::Descriptor* descriptor =
- GetDescriptorPool()->FindMessageTypeByName(message_name);
- if (descriptor == NULL) {
- PyErr_SetString(PyExc_TypeError, message_type);
+// Retrieve a C++ FieldDescriptor for an extension handle.
+const google::protobuf::FieldDescriptor* GetExtensionDescriptor(PyObject* extension) {
+ ScopedPyObjectPtr cdescriptor(
+ PyObject_GetAttrString(extension, "_cdescriptor"));
+ if (cdescriptor == NULL) {
+ PyErr_SetString(PyExc_KeyError, "Unregistered extension.");
+ return NULL;
+ }
+ if (!PyObject_TypeCheck(cdescriptor, &CFieldDescriptor_Type)) {
+ PyErr_SetString(PyExc_TypeError, "Not a CFieldDescriptor");
+ Py_DECREF(cdescriptor);
return NULL;
}
- return global_message_factory->GetPrototype(descriptor);
+ return reinterpret_cast<CFieldDescriptor*>(cdescriptor.get())->descriptor;
}
// If cmessage_list is not NULL, this function releases values into the
@@ -627,39 +676,8 @@ int InternalDeleteRepeatedField(
return 0;
}
-int InitAttributes(CMessage* self, PyObject* arg, PyObject* kwargs) {
- ScopedPyObjectPtr descriptor;
- if (arg == NULL) {
- descriptor.reset(
- PyObject_GetAttr(reinterpret_cast<PyObject*>(self), kDESCRIPTOR));
- if (descriptor == NULL) {
- return NULL;
- }
- } else {
- descriptor.reset(arg);
- descriptor.inc();
- }
- ScopedPyObjectPtr is_extendable(PyObject_GetAttr(descriptor, kis_extendable));
- if (is_extendable == NULL) {
- return NULL;
- }
- int retcode = PyObject_IsTrue(is_extendable);
- if (retcode == -1) {
- return NULL;
- }
- if (retcode) {
- PyObject* py_extension_dict = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&ExtensionDict_Type), NULL);
- if (py_extension_dict == NULL) {
- return NULL;
- }
- ExtensionDict* extension_dict = reinterpret_cast<ExtensionDict*>(
- py_extension_dict);
- extension_dict->parent = self;
- extension_dict->message = self->message;
- self->extensions = extension_dict;
- }
-
+// Initializes fields of a message. Used in constructors.
+int InitAttributes(CMessage* self, PyObject* kwargs) {
if (kwargs == NULL) {
return 0;
}
@@ -672,14 +690,12 @@ int InitAttributes(CMessage* self, PyObject* arg, PyObject* kwargs) {
PyErr_SetString(PyExc_ValueError, "Field name must be a string");
return -1;
}
- PyObject* py_cdescriptor = GetDescriptor(self, name);
- if (py_cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor = GetFieldDescriptor(self, name);
+ if (descriptor == NULL) {
PyErr_Format(PyExc_ValueError, "Protocol message has no \"%s\" field.",
PyString_AsString(name));
return -1;
}
- const google::protobuf::FieldDescriptor* descriptor =
- reinterpret_cast<CFieldDescriptor*>(py_cdescriptor)->descriptor;
if (descriptor->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED) {
ScopedPyObjectPtr container(GetAttr(self, name));
if (container == NULL) {
@@ -719,15 +735,19 @@ int InitAttributes(CMessage* self, PyObject* arg, PyObject* kwargs) {
return 0;
}
-static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
- CMessage* self = reinterpret_cast<CMessage*>(type->tp_alloc(type, 0));
+// Allocates an incomplete Python Message: the caller must fill self->message,
+// self->owner and eventually self->parent.
+CMessage* NewEmptyMessage(PyObject* type,
+ const google::protobuf::Descriptor *descriptor) {
+ CMessage* self = reinterpret_cast<CMessage*>(
+ PyType_GenericAlloc(reinterpret_cast<PyTypeObject*>(type), 0));
if (self == NULL) {
return NULL;
}
self->message = NULL;
self->parent = NULL;
- self->parent_field = NULL;
+ self->parent_field_descriptor = NULL;
self->read_only = false;
self->extensions = NULL;
@@ -735,43 +755,58 @@ static PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
if (self->composite_fields == NULL) {
return NULL;
}
- return reinterpret_cast<PyObject*>(self);
-}
-
-PyObject* NewEmpty(PyObject* type) {
- return New(reinterpret_cast<PyTypeObject*>(type), NULL, NULL);
-}
-static int Init(CMessage* self, PyObject* args, PyObject* kwargs) {
- if (kwargs == NULL) {
- // TODO(anuraag): Set error
- return -1;
+ // If there are extension_ranges, the message is "extendable". Allocate a
+ // dictionary to store the extension fields.
+ if (descriptor->extension_range_count() > 0) {
+ // TODO(amauryfa): Delay the construction of this dict until extensions are
+ // really used on the object.
+ ExtensionDict* extension_dict = extension_dict::NewExtensionDict(self);
+ if (extension_dict == NULL) {
+ return NULL;
+ }
+ self->extensions = extension_dict;
}
- PyObject* descriptor = PyTuple_GetItem(args, 0);
- if (descriptor == NULL || PyTuple_Size(args) != 1) {
- PyErr_SetString(PyExc_ValueError, "args must contain one arg: descriptor");
- return -1;
- }
+ return self;
+}
- ScopedPyObjectPtr py_message_type(PyObject_GetAttr(descriptor, kfull_name));
- if (py_message_type == NULL) {
- return -1;
+// The __new__ method of Message classes.
+// Creates a new C++ message and takes ownership.
+static PyObject* New(PyTypeObject* type,
+ PyObject* unused_args, PyObject* unused_kwargs) {
+ // Retrieve the message descriptor and the default instance (=prototype).
+ const google::protobuf::Descriptor* message_descriptor = GetMessageDescriptor(type);
+ if (message_descriptor == NULL) {
+ return NULL;
}
-
- const char* message_type = PyString_AsString(py_message_type.get());
- const google::protobuf::Message* message = CreateMessage(message_type);
- if (message == NULL) {
- return -1;
+ const google::protobuf::Message* default_message =
+ global_message_factory->GetPrototype(message_descriptor);
+ if (default_message == NULL) {
+ PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str());
+ return NULL;
}
- self->message = message->New();
+ CMessage* self = NewEmptyMessage(reinterpret_cast<PyObject*>(type),
+ message_descriptor);
+ if (self == NULL) {
+ return NULL;
+ }
+ self->message = default_message->New();
self->owner.reset(self->message);
- if (InitAttributes(self, descriptor, kwargs) < 0) {
+ return reinterpret_cast<PyObject*>(self);
+}
+
+// The __init__ method of Message classes.
+// It initializes fields from keywords passed to the constructor.
+static int Init(CMessage* self, PyObject* args, PyObject* kwargs) {
+ if (PyTuple_Size(args) != 0) {
+ PyErr_SetString(PyExc_TypeError, "No positional arguments allowed");
return -1;
}
- return 0;
+
+ return InitAttributes(self, kwargs);
}
// ---------------------------------------------------------------------
@@ -853,9 +888,7 @@ PyObject* IsInitialized(CMessage* self, PyObject* args) {
PyObject* HasFieldByDescriptor(
CMessage* self, const google::protobuf::FieldDescriptor* field_descriptor) {
google::protobuf::Message* message = self->message;
- if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
- PyErr_SetString(PyExc_KeyError,
- "Field does not belong to message!");
+ if (!CheckFieldBelongsToMessage(field_descriptor, message)) {
return NULL;
}
if (field_descriptor->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED) {
@@ -1048,7 +1081,7 @@ int ReleaseSubMessage(google::protobuf::Message* message,
child_cmessage->message = released_message.get();
child_cmessage->owner.swap(released_message);
child_cmessage->parent = NULL;
- child_cmessage->parent_field = NULL;
+ child_cmessage->parent_field_descriptor = NULL;
child_cmessage->read_only = false;
return ForEachCompositeField(child_cmessage,
SetOwnerVisitor(child_cmessage->owner));
@@ -1090,10 +1123,8 @@ int InternalReleaseFieldByDescriptor(
int InternalReleaseField(CMessage* self, PyObject* composite_field,
PyObject* name) {
- PyObject* cdescriptor = GetDescriptor(self, name);
- if (cdescriptor != NULL) {
- const google::protobuf::FieldDescriptor* descriptor =
- reinterpret_cast<CFieldDescriptor*>(cdescriptor)->descriptor;
+ const google::protobuf::FieldDescriptor* descriptor = GetFieldDescriptor(self, name);
+ if (descriptor != NULL) {
return InternalReleaseFieldByDescriptor(
descriptor, composite_field, self->message);
}
@@ -1104,9 +1135,7 @@ int InternalReleaseField(CMessage* self, PyObject* composite_field,
PyObject* ClearFieldByDescriptor(
CMessage* self,
const google::protobuf::FieldDescriptor* descriptor) {
- if (!FIELD_BELONGS_TO_MESSAGE(descriptor, self->message)) {
- PyErr_SetString(PyExc_KeyError,
- "Field does not belong to message!");
+ if (!CheckFieldBelongsToMessage(descriptor, self->message)) {
return NULL;
}
AssureWritable(self);
@@ -1177,15 +1206,10 @@ PyObject* Clear(CMessage* self) {
// fields have been released.
if (self->extensions != NULL) {
Py_CLEAR(self->extensions);
- PyObject* py_extension_dict = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&ExtensionDict_Type), NULL);
- if (py_extension_dict == NULL) {
+ ExtensionDict* extension_dict = extension_dict::NewExtensionDict(self);
+ if (extension_dict == NULL) {
return NULL;
}
- ExtensionDict* extension_dict = reinterpret_cast<ExtensionDict*>(
- py_extension_dict);
- extension_dict->parent = self;
- extension_dict->message = self->message;
self->extensions = extension_dict;
}
PyDict_Clear(self->composite_fields);
@@ -1196,8 +1220,8 @@ PyObject* Clear(CMessage* self) {
// ---------------------------------------------------------------------
static string GetMessageName(CMessage* self) {
- if (self->parent_field != NULL) {
- return self->parent_field->descriptor->full_name();
+ if (self->parent_field_descriptor != NULL) {
+ return self->parent_field_descriptor->full_name();
} else {
return self->message->GetDescriptor()->full_name();
}
@@ -1219,7 +1243,7 @@ static PyObject* SerializeToString(CMessage* self, PyObject* args) {
return NULL;
}
PyErr_Format(EncodeError_class, "Message %s is missing required fields: %s",
- GetMessageName(self).c_str(), PyString_AsString(joined.get()));
+ GetMessageName(self).c_str(), PyString_AsString(joined));
return NULL;
}
int size = self->message->ByteSize();
@@ -1361,7 +1385,7 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) {
AssureWritable(self);
google::protobuf::io::CodedInputStream input(
reinterpret_cast<const uint8*>(data), data_length);
- input.SetExtensionRegistry(GetDescriptorPool(), global_message_factory);
+ input.SetExtensionRegistry(descriptor_pool->pool, global_message_factory);
bool success = self->message->MergePartialFromCodedStream(&input);
if (success) {
return PyInt_FromLong(input.CurrentPosition());
@@ -1421,15 +1445,11 @@ static PyObject* RegisterExtension(PyObject* cls,
return NULL;
}
- CFieldDescriptor* cdescriptor =
- extension_dict::InternalGetCDescriptorFromExtension(extension_handle);
- ScopedPyObjectPtr py_cdescriptor(reinterpret_cast<PyObject*>(cdescriptor));
- if (cdescriptor == NULL) {
+ const google::protobuf::FieldDescriptor* descriptor =
+ GetExtensionDescriptor(extension_handle);
+ if (descriptor == NULL) {
return NULL;
}
- Py_INCREF(extension_handle);
- cdescriptor->descriptor_field = extension_handle;
- const google::protobuf::FieldDescriptor* descriptor = cdescriptor->descriptor;
// Check if it's a message set
if (descriptor->is_extension() &&
descriptor->containing_type()->options().message_set_wire_format() &&
@@ -1608,8 +1628,14 @@ static PyObject* RichCompare(CMessage* self, PyObject* other, int opid) {
}
if (opid == Py_EQ || opid == Py_NE) {
ScopedPyObjectPtr self_fields(ListFields(self));
+ if (!self_fields) {
+ return NULL;
+ }
ScopedPyObjectPtr other_fields(ListFields(
reinterpret_cast<CMessage*>(other)));
+ if (!other_fields) {
+ return NULL;
+ }
return PyObject_RichCompare(self_fields, other_fields, opid);
} else {
Py_INCREF(Py_NotImplemented);
@@ -1623,9 +1649,7 @@ PyObject* InternalGetScalar(
google::protobuf::Message* message = self->message;
const google::protobuf::Reflection* reflection = message->GetReflection();
- if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
- PyErr_SetString(
- PyExc_KeyError, "Field does not belong to message!");
+ if (!CheckFieldBelongsToMessage(field_descriptor, message)) {
return NULL;
}
@@ -1701,43 +1725,31 @@ PyObject* InternalGetScalar(
return result;
}
-PyObject* InternalGetSubMessage(CMessage* self,
- CFieldDescriptor* cfield_descriptor) {
- PyObject* field = cfield_descriptor->descriptor_field;
- ScopedPyObjectPtr message_type(PyObject_GetAttr(field, kmessage_type));
- if (message_type == NULL) {
- return NULL;
- }
- ScopedPyObjectPtr concrete_class(
- PyObject_GetAttr(message_type, k_concrete_class));
- if (concrete_class == NULL) {
+PyObject* InternalGetSubMessage(
+ CMessage* self, const google::protobuf::FieldDescriptor* field_descriptor) {
+ const google::protobuf::Reflection* reflection = self->message->GetReflection();
+ const google::protobuf::Message& sub_message = reflection->GetMessage(
+ *self->message, field_descriptor, global_message_factory);
+
+ PyObject *message_class = cdescriptor_pool::GetMessageClass(
+ descriptor_pool, field_descriptor->message_type());
+ if (message_class == NULL) {
return NULL;
}
- PyObject* py_cmsg = cmessage::NewEmpty(concrete_class);
- if (py_cmsg == NULL) {
+
+ CMessage* cmsg = cmessage::NewEmptyMessage(message_class,
+ sub_message.GetDescriptor());
+ if (cmsg == NULL) {
return NULL;
}
- if (!PyObject_TypeCheck(py_cmsg, &CMessage_Type)) {
- PyErr_SetString(PyExc_TypeError, "Not a CMessage!");
- }
- CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg);
- const google::protobuf::FieldDescriptor* field_descriptor =
- cfield_descriptor->descriptor;
- const google::protobuf::Reflection* reflection = self->message->GetReflection();
- const google::protobuf::Message& sub_message = reflection->GetMessage(
- *self->message, field_descriptor, global_message_factory);
cmsg->owner = self->owner;
cmsg->parent = self;
- cmsg->parent_field = cfield_descriptor;
+ cmsg->parent_field_descriptor = field_descriptor;
cmsg->read_only = !reflection->HasField(*self->message, field_descriptor);
cmsg->message = const_cast<google::protobuf::Message*>(&sub_message);
- if (InitAttributes(cmsg, NULL, NULL) < 0) {
- Py_DECREF(py_cmsg);
- return NULL;
- }
- return py_cmsg;
+ return reinterpret_cast<PyObject*>(cmsg);
}
int InternalSetScalar(
@@ -1747,9 +1759,7 @@ int InternalSetScalar(
google::protobuf::Message* message = self->message;
const google::protobuf::Reflection* reflection = message->GetReflection();
- if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
- PyErr_SetString(
- PyExc_KeyError, "Field does not belong to message!");
+ if (!CheckFieldBelongsToMessage(field_descriptor, message)) {
return -1;
}
@@ -1838,25 +1848,35 @@ PyObject* FromString(PyTypeObject* cls, PyObject* serialized) {
return NULL;
}
- if (InitAttributes(cmsg, NULL, NULL) < 0) {
- Py_DECREF(py_cmsg);
- return NULL;
- }
return py_cmsg;
}
+
+// Finalize the creation of the Message class.
+// Called from its metaclass: GeneratedProtocolMessageType.__init__().
static PyObject* AddDescriptors(PyTypeObject* cls,
PyObject* descriptor) {
- if (PyObject_SetAttr(reinterpret_cast<PyObject*>(cls),
- k_extensions_by_name, PyDict_New()) < 0) {
- return NULL;
- }
- if (PyObject_SetAttr(reinterpret_cast<PyObject*>(cls),
- k_extensions_by_number, PyDict_New()) < 0) {
+ const google::protobuf::Descriptor* message_descriptor =
+ cdescriptor_pool::RegisterMessageClass(
+ descriptor_pool, reinterpret_cast<PyObject*>(cls), descriptor);
+ if (message_descriptor == NULL) {
return NULL;
}
- ScopedPyObjectPtr field_descriptors(PyDict_New());
+ // If there are extension_ranges, the message is "extendable", and extension
+ // classes will register themselves in this class.
+ if (message_descriptor->extension_range_count() > 0) {
+ ScopedPyObjectPtr by_name(PyDict_New());
+ if (PyObject_SetAttr(reinterpret_cast<PyObject*>(cls),
+ k_extensions_by_name, by_name) < 0) {
+ return NULL;
+ }
+ ScopedPyObjectPtr by_number(PyDict_New());
+ if (PyObject_SetAttr(reinterpret_cast<PyObject*>(cls),
+ k_extensions_by_number, by_number) < 0) {
+ return NULL;
+ }
+ }
ScopedPyObjectPtr fields(PyObject_GetAttrString(descriptor, "fields"));
if (fields == NULL) {
@@ -1878,19 +1898,14 @@ static PyObject* AddDescriptors(PyTypeObject* cls,
return NULL;
}
- PyObject* field_descriptor =
- cdescriptor_pool::FindFieldByName(descriptor_pool, full_field_name);
+ ScopedPyObjectPtr field_descriptor(
+ cdescriptor_pool::FindFieldByName(descriptor_pool, full_field_name));
if (field_descriptor == NULL) {
PyErr_SetString(PyExc_TypeError, "Couldn't find field");
return NULL;
}
- Py_INCREF(field);
CFieldDescriptor* cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(
- field_descriptor);
- cfield_descriptor->descriptor_field = field;
- if (PyDict_SetItem(field_descriptors, field_name, field_descriptor) < 0) {
- return NULL;
- }
+ field_descriptor.get());
// The FieldDescriptor's name field might either be of type bytes or
// of type unicode, depending on whether the FieldDescriptor was
@@ -1919,8 +1934,6 @@ static PyObject* AddDescriptors(PyTypeObject* cls,
}
}
- PyDict_SetItem(cls->tp_dict, k__descriptors, field_descriptors);
-
// Enum Values
ScopedPyObjectPtr enum_types(PyObject_GetAttrString(descriptor,
"enum_types"));
@@ -1994,15 +2007,11 @@ static PyObject* AddDescriptors(PyTypeObject* cls,
extension_name, extension_field) == -1) {
return NULL;
}
- ScopedPyObjectPtr py_cfield_descriptor(
- PyObject_GetAttrString(extension_field, "_cdescriptor"));
- if (py_cfield_descriptor == NULL) {
+ const google::protobuf::FieldDescriptor* field_descriptor =
+ GetExtensionDescriptor(extension_field);
+ if (field_descriptor == NULL) {
return NULL;
}
- CFieldDescriptor* cfield_descriptor =
- reinterpret_cast<CFieldDescriptor*>(py_cfield_descriptor.get());
- Py_INCREF(extension_field);
- cfield_descriptor->descriptor_field = extension_field;
ScopedPyObjectPtr field_name_upcased(
PyObject_CallMethod(extension_name, "upper", NULL));
@@ -2015,13 +2024,12 @@ static PyObject* AddDescriptors(PyTypeObject* cls,
return NULL;
}
ScopedPyObjectPtr number(PyInt_FromLong(
- cfield_descriptor->descriptor->number()));
+ field_descriptor->number()));
if (number == NULL) {
return NULL;
}
if (PyObject_SetAttr(reinterpret_cast<PyObject*>(cls),
- field_number_name, PyInt_FromLong(
- cfield_descriptor->descriptor->number())) == -1) {
+ field_number_name, number) == -1) {
return NULL;
}
}
@@ -2039,10 +2047,6 @@ PyObject* DeepCopy(CMessage* self, PyObject* arg) {
Py_DECREF(clone);
return NULL;
}
- if (InitAttributes(reinterpret_cast<CMessage*>(clone), NULL, NULL) < 0) {
- Py_DECREF(clone);
- return NULL;
- }
if (MergeFrom(reinterpret_cast<CMessage*>(clone),
reinterpret_cast<PyObject*>(self)) == NULL) {
Py_DECREF(clone);
@@ -2202,48 +2206,30 @@ PyObject* GetAttr(CMessage* self, PyObject* name) {
return value;
}
- PyObject* descriptor = GetDescriptor(self, name);
- if (descriptor != NULL) {
- CFieldDescriptor* cdescriptor =
- reinterpret_cast<CFieldDescriptor*>(descriptor);
- const google::protobuf::FieldDescriptor* field_descriptor = cdescriptor->descriptor;
+ const google::protobuf::FieldDescriptor* field_descriptor = GetFieldDescriptor(
+ self, name);
+ if (field_descriptor != NULL) {
if (field_descriptor->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED) {
if (field_descriptor->cpp_type() ==
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
- PyObject* py_container = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&RepeatedCompositeContainer_Type),
- NULL);
- if (py_container == NULL) {
+ PyObject *message_class = cdescriptor_pool::GetMessageClass(
+ descriptor_pool, field_descriptor->message_type());
+ if (message_class == NULL) {
return NULL;
}
- RepeatedCompositeContainer* container =
- reinterpret_cast<RepeatedCompositeContainer*>(py_container);
- PyObject* field = cdescriptor->descriptor_field;
- PyObject* message_type = PyObject_GetAttr(field, kmessage_type);
- if (message_type == NULL) {
- return NULL;
- }
- PyObject* concrete_class =
- PyObject_GetAttr(message_type, k_concrete_class);
- if (concrete_class == NULL) {
+ PyObject* py_container = repeated_composite_container::NewContainer(
+ self, field_descriptor, message_class);
+ if (py_container == NULL) {
return NULL;
}
- container->parent = self;
- container->parent_field = cdescriptor;
- container->message = self->message;
- container->owner = self->owner;
- container->subclass_init = concrete_class;
- Py_DECREF(message_type);
if (PyDict_SetItem(self->composite_fields, name, py_container) < 0) {
Py_DECREF(py_container);
return NULL;
}
return py_container;
} else {
- ScopedPyObjectPtr init_args(PyTuple_Pack(2, self, cdescriptor));
- PyObject* py_container = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&RepeatedScalarContainer_Type),
- init_args);
+ PyObject* py_container = repeated_scalar_container::NewContainer(
+ self, field_descriptor);
if (py_container == NULL) {
return NULL;
}
@@ -2256,7 +2242,7 @@ PyObject* GetAttr(CMessage* self, PyObject* name) {
} else {
if (field_descriptor->cpp_type() ==
google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
- PyObject* sub_message = InternalGetSubMessage(self, cdescriptor);
+ PyObject* sub_message = InternalGetSubMessage(self, field_descriptor);
if (PyDict_SetItem(self->composite_fields, name, sub_message) < 0) {
Py_DECREF(sub_message);
return NULL;
@@ -2278,12 +2264,10 @@ int SetAttr(CMessage* self, PyObject* name, PyObject* value) {
return -1;
}
- PyObject* descriptor = GetDescriptor(self, name);
- if (descriptor != NULL) {
+ const google::protobuf::FieldDescriptor* field_descriptor =
+ GetFieldDescriptor(self, name);
+ if (field_descriptor != NULL) {
AssureWritable(self);
- CFieldDescriptor* cdescriptor =
- reinterpret_cast<CFieldDescriptor*>(descriptor);
- const google::protobuf::FieldDescriptor* field_descriptor = cdescriptor->descriptor;
if (field_descriptor->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED) {
PyErr_Format(PyExc_AttributeError, "Assignment not allowed to repeated "
"field \"%s\" in protocol message object.",
@@ -2401,22 +2385,18 @@ void InitGlobals() {
kuint64max_py = PyLong_FromUnsignedLongLong(kuint64max);
kDESCRIPTOR = PyString_FromString("DESCRIPTOR");
- k__descriptors = PyString_FromString("__descriptors");
+ k_cdescriptor = PyString_FromString("_cdescriptor");
kfull_name = PyString_FromString("full_name");
- kis_extendable = PyString_FromString("is_extendable");
kextensions_by_name = PyString_FromString("extensions_by_name");
k_extensions_by_name = PyString_FromString("_extensions_by_name");
k_extensions_by_number = PyString_FromString("_extensions_by_number");
- k_concrete_class = PyString_FromString("_concrete_class");
- kmessage_type = PyString_FromString("message_type");
kname = PyString_FromString("name");
kfields_by_name = PyString_FromString("fields_by_name");
- global_message_factory = new DynamicMessageFactory(GetDescriptorPool());
- global_message_factory->SetDelegateToGeneratedFactory(true);
+ descriptor_pool = cdescriptor_pool::NewDescriptorPool();
- descriptor_pool = reinterpret_cast<google::protobuf::python::CDescriptorPool*>(
- Python_NewCDescriptorPool(NULL, NULL));
+ global_message_factory = new DynamicMessageFactory(descriptor_pool->pool);
+ global_message_factory->SetDelegateToGeneratedFactory(true);
}
bool InitProto2MessageModule(PyObject *m) {
@@ -2427,19 +2407,32 @@ bool InitProto2MessageModule(PyObject *m) {
return false;
}
- // All three of these are actually set elsewhere, directly onto the child
- // protocol buffer message class, but set them here as well to document that
- // subclasses need to set these.
+ // DESCRIPTOR is set on each protocol buffer message class elsewhere, but set
+ // it here as well to document that subclasses need to set it.
PyDict_SetItem(google::protobuf::python::CMessage_Type.tp_dict, kDESCRIPTOR, Py_None);
- PyDict_SetItem(google::protobuf::python::CMessage_Type.tp_dict,
- k_extensions_by_name, Py_None);
- PyDict_SetItem(google::protobuf::python::CMessage_Type.tp_dict,
- k_extensions_by_number, Py_None);
+ // Subclasses with message extensions will override _extensions_by_name and
+ // _extensions_by_number with fresh mutable dictionaries in AddDescriptors.
+ // All other classes can share this same immutable mapping.
+ ScopedPyObjectPtr empty_dict(PyDict_New());
+ if (empty_dict == NULL) {
+ return false;
+ }
+ ScopedPyObjectPtr immutable_dict(PyDictProxy_New(empty_dict));
+ if (immutable_dict == NULL) {
+ return false;
+ }
+ if (PyDict_SetItem(google::protobuf::python::CMessage_Type.tp_dict,
+ k_extensions_by_name, immutable_dict) < 0) {
+ return false;
+ }
+ if (PyDict_SetItem(google::protobuf::python::CMessage_Type.tp_dict,
+ k_extensions_by_number, immutable_dict) < 0) {
+ return false;
+ }
PyModule_AddObject(m, "Message", reinterpret_cast<PyObject*>(
&google::protobuf::python::CMessage_Type));
- google::protobuf::python::RepeatedScalarContainer_Type.tp_new = PyType_GenericNew;
google::protobuf::python::RepeatedScalarContainer_Type.tp_hash =
PyObject_HashNotImplemented;
if (PyType_Ready(&google::protobuf::python::RepeatedScalarContainer_Type) < 0) {
@@ -2450,7 +2443,6 @@ bool InitProto2MessageModule(PyObject *m) {
reinterpret_cast<PyObject*>(
&google::protobuf::python::RepeatedScalarContainer_Type));
- google::protobuf::python::RepeatedCompositeContainer_Type.tp_new = PyType_GenericNew;
google::protobuf::python::RepeatedCompositeContainer_Type.tp_hash =
PyObject_HashNotImplemented;
if (PyType_Ready(&google::protobuf::python::RepeatedCompositeContainer_Type) < 0) {
@@ -2462,7 +2454,6 @@ bool InitProto2MessageModule(PyObject *m) {
reinterpret_cast<PyObject*>(
&google::protobuf::python::RepeatedCompositeContainer_Type));
- google::protobuf::python::ExtensionDict_Type.tp_new = PyType_GenericNew;
google::protobuf::python::ExtensionDict_Type.tp_hash = PyObject_HashNotImplemented;
if (PyType_Ready(&google::protobuf::python::ExtensionDict_Type) < 0) {
return false;
diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h
index 9f4978f4..0fef92a0 100644
--- a/python/google/protobuf/pyext/message.h
+++ b/python/google/protobuf/pyext/message.h
@@ -49,12 +49,13 @@ namespace protobuf {
class Message;
class Reflection;
class FieldDescriptor;
+class Descriptor;
using internal::shared_ptr;
namespace python {
-struct CFieldDescriptor;
+struct PyDescriptorPool;
struct ExtensionDict;
typedef struct CMessage {
@@ -79,13 +80,11 @@ typedef struct CMessage {
// to use this pointer will result in a crash.
struct CMessage* parent;
- // Weak reference to the parent's descriptor that describes this submessage.
+ // Pointer to the parent's descriptor that describes this submessage.
// Used together with the parent's message when making a default message
// instance mutable.
- // TODO(anuraag): With a bit of work on the Python/C++ layer, it should be
- // possible to make this a direct pointer to a C++ FieldDescriptor, this would
- // be easier if this implementation replaces upstream.
- CFieldDescriptor* parent_field;
+ // The pointer is owned by the global DescriptorPool.
+ const google::protobuf::FieldDescriptor* parent_field_descriptor;
// Pointer to the C++ Message object for this CMessage. The
// CMessage does not own this pointer.
@@ -113,8 +112,11 @@ extern PyTypeObject CMessage_Type;
namespace cmessage {
-// Create a new empty message that can be populated by the parent.
-PyObject* NewEmpty(PyObject* type);
+// Internal function to create a new empty Message Python object, but with empty
+// pointers to the C++ objects.
+// The caller must fill self->message, self->owner and eventually self->parent.
+CMessage* NewEmptyMessage(PyObject* type,
+ const google::protobuf::Descriptor* descriptor);
// Release a submessage from its proto tree, making it a new top-level messgae.
// A new message will be created if this is a read-only default instance.
@@ -124,19 +126,23 @@ int ReleaseSubMessage(google::protobuf::Message* message,
const google::protobuf::FieldDescriptor* field_descriptor,
CMessage* child_cmessage);
+// Retrieves the C++ descriptor of a Python Extension descriptor.
+// On error, return NULL with an exception set.
+const google::protobuf::FieldDescriptor* GetExtensionDescriptor(PyObject* extension);
+
// Initializes a new CMessage instance for a submessage. Only called once per
// submessage as the result is cached in composite_fields.
//
// Corresponds to reflection api method GetMessage.
-PyObject* InternalGetSubMessage(CMessage* self,
- CFieldDescriptor* cfield_descriptor);
+PyObject* InternalGetSubMessage(
+ CMessage* self, const google::protobuf::FieldDescriptor* field_descriptor);
// Deletes a range of C++ submessages 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 refered to
+// 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.
@@ -190,10 +196,8 @@ PyObject* HasFieldByDescriptor(
// Corresponds to reflection api method HasField.
PyObject* HasField(CMessage* self, PyObject* arg);
-// Initializes constants/enum values on a message. This is called by
-// RepeatedCompositeContainer and ExtensionDict after calling the constructor.
-// TODO(anuraag): Make it always called from within the constructor since it can
-int InitAttributes(CMessage* self, PyObject* descriptor, PyObject* kwargs);
+// Initializes values of fields on a newly constructed message.
+int InitAttributes(CMessage* self, PyObject* kwargs);
PyObject* MergeFrom(CMessage* self, PyObject* arg);
@@ -218,12 +222,14 @@ int AssureWritable(CMessage* self);
} // namespace cmessage
+
+// Retrieve the global descriptor pool owned by the _message module.
+PyDescriptorPool* GetDescriptorPool();
+
+
/* Is 64bit */
#define IS_64BIT (SIZEOF_LONG == 8)
-#define FIELD_BELONGS_TO_MESSAGE(field_descriptor, message) \
- ((message)->GetDescriptor() == (field_descriptor)->containing_type())
-
#define FIELD_IS_REPEATED(field_descriptor) \
((field_descriptor)->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
@@ -296,6 +302,11 @@ bool CheckAndSetString(
PyObject* ToStringObject(
const google::protobuf::FieldDescriptor* descriptor, string value);
+// Check if the passed field descriptor belongs to the given message.
+// If not, return false and set a Python exception (a KeyError)
+bool CheckFieldBelongsToMessage(const google::protobuf::FieldDescriptor* field_descriptor,
+ const google::protobuf::Message* message);
+
extern PyObject* PickleError_class;
} // namespace python
diff --git a/python/google/protobuf/pyext/proto2_api_test.proto b/python/google/protobuf/pyext/proto2_api_test.proto
index 72c31b1c..18aecfb7 100644
--- a/python/google/protobuf/pyext/proto2_api_test.proto
+++ b/python/google/protobuf/pyext/proto2_api_test.proto
@@ -28,6 +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.
+syntax = "proto2";
+
import "google/protobuf/internal/cpp/proto1_api_test.proto";
package google.protobuf.python.internal;
diff --git a/python/google/protobuf/pyext/python.proto b/python/google/protobuf/pyext/python.proto
index d47d402c..cce645d7 100644
--- a/python/google/protobuf/pyext/python.proto
+++ b/python/google/protobuf/pyext/python.proto
@@ -33,6 +33,7 @@
// These message definitions are used to exercises known corner cases
// in the C++ implementation of the Python API.
+syntax = "proto2";
package google.protobuf.python.internal;
@@ -63,4 +64,5 @@ message TestAllExtensions {
extend TestAllExtensions {
optional TestAllTypes.NestedMessage optional_nested_message_extension = 1;
+ repeated TestAllTypes.NestedMessage repeated_nested_message_extension = 2;
}
diff --git a/python/google/protobuf/pyext/repeated_composite_container.cc b/python/google/protobuf/pyext/repeated_composite_container.cc
index 5c05b3d8..36fe86ae 100644
--- a/python/google/protobuf/pyext/repeated_composite_container.cc
+++ b/python/google/protobuf/pyext/repeated_composite_container.cc
@@ -65,14 +65,14 @@ namespace repeated_composite_container {
#define GOOGLE_CHECK_ATTACHED(self) \
do { \
GOOGLE_CHECK_NOTNULL((self)->message); \
- GOOGLE_CHECK_NOTNULL((self)->parent_field); \
+ 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 == NULL); \
+ GOOGLE_CHECK((self)->parent_field_descriptor == NULL); \
GOOGLE_CHECK((self)->parent == NULL); \
} while (0);
@@ -122,7 +122,7 @@ static int InternalQuickSort(RepeatedCompositeContainer* self,
google::protobuf::Message* message = self->message;
const google::protobuf::Reflection* reflection = message->GetReflection();
- const google::protobuf::FieldDescriptor* descriptor = self->parent_field->descriptor;
+ const google::protobuf::FieldDescriptor* descriptor = self->parent_field_descriptor;
Py_ssize_t left;
Py_ssize_t right;
@@ -202,7 +202,7 @@ static Py_ssize_t Length(RepeatedCompositeContainer* self) {
google::protobuf::Message* message = self->message;
if (message != NULL) {
return message->GetReflection()->FieldSize(*message,
- self->parent_field->descriptor);
+ 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.
@@ -225,19 +225,19 @@ static int UpdateChildMessages(RepeatedCompositeContainer* self) {
const google::protobuf::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);
- ScopedPyObjectPtr py_cmsg(cmessage::NewEmpty(self->subclass_init));
- if (py_cmsg == NULL) {
+ *(self->message), self->parent_field_descriptor, i);
+ CMessage* cmsg = cmessage::NewEmptyMessage(self->subclass_init,
+ sub_message.GetDescriptor());
+ ScopedPyObjectPtr py_cmsg(reinterpret_cast<PyObject*>(cmsg));
+ if (cmsg == NULL) {
return -1;
}
- CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg.get());
cmsg->owner = self->owner;
cmsg->message = const_cast<google::protobuf::Message*>(&sub_message);
cmsg->parent = self->parent;
- if (cmessage::InitAttributes(cmsg, NULL, NULL) < 0) {
+ if (PyList_Append(self->child_messages, py_cmsg) < 0) {
return -1;
}
- PyList_Append(self->child_messages, py_cmsg);
}
return 0;
}
@@ -258,23 +258,25 @@ static PyObject* AddToAttached(RepeatedCompositeContainer* self,
google::protobuf::Message* message = self->message;
google::protobuf::Message* sub_message =
message->GetReflection()->AddMessage(message,
- self->parent_field->descriptor);
- PyObject* py_cmsg = cmessage::NewEmpty(self->subclass_init);
- if (py_cmsg == NULL) {
+ self->parent_field_descriptor);
+ CMessage* cmsg = cmessage::NewEmptyMessage(self->subclass_init,
+ sub_message->GetDescriptor());
+ if (cmsg == NULL)
return NULL;
- }
- CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg);
cmsg->owner = self->owner;
cmsg->message = sub_message;
cmsg->parent = self->parent;
- // cmessage::InitAttributes must be called after cmsg->message has
- // been set.
- if (cmessage::InitAttributes(cmsg, NULL, kwargs) < 0) {
+ if (cmessage::InitAttributes(cmsg, kwargs) < 0) {
+ Py_DECREF(cmsg);
+ return NULL;
+ }
+
+ PyObject* py_cmsg = reinterpret_cast<PyObject*>(cmsg);
+ if (PyList_Append(self->child_messages, py_cmsg) < 0) {
Py_DECREF(py_cmsg);
return NULL;
}
- PyList_Append(self->child_messages, py_cmsg);
return py_cmsg;
}
@@ -283,20 +285,16 @@ static PyObject* AddToReleased(RepeatedCompositeContainer* self,
PyObject* kwargs) {
GOOGLE_CHECK_RELEASED(self);
- // Create the CMessage
- PyObject* py_cmsg = PyObject_CallObject(self->subclass_init, NULL);
+ // Create a new Message detached from the rest.
+ PyObject* py_cmsg = PyEval_CallObjectWithKeywords(
+ self->subclass_init, NULL, kwargs);
if (py_cmsg == NULL)
return NULL;
- CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg);
- if (cmessage::InitAttributes(cmsg, NULL, kwargs) < 0) {
+
+ if (PyList_Append(self->child_messages, py_cmsg) < 0) {
Py_DECREF(py_cmsg);
return NULL;
}
-
- // The Message got created by the call to subclass_init above and
- // it set self->owner to the newly allocated message.
-
- PyList_Append(self->child_messages, py_cmsg);
return py_cmsg;
}
@@ -354,35 +352,9 @@ PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice) {
if (UpdateChildMessages(self) < 0) {
return NULL;
}
- Py_ssize_t from;
- Py_ssize_t to;
- Py_ssize_t step;
- Py_ssize_t length = Length(self);
- Py_ssize_t slicelength;
- if (PySlice_Check(slice)) {
-#if PY_MAJOR_VERSION >= 3
- if (PySlice_GetIndicesEx(slice,
-#else
- if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(slice),
-#endif
- length, &from, &to, &step, &slicelength) == -1) {
- return NULL;
- }
- return PyList_GetSlice(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;
- }
- PyObject* result = PyList_GetItem(self->child_messages, from);
- if (result == NULL) {
- return NULL;
- }
- Py_INCREF(result);
- return result;
- }
- PyErr_SetString(PyExc_TypeError, "index must be an integer or slice");
- 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);
}
int AssignSubscript(RepeatedCompositeContainer* self,
@@ -399,7 +371,7 @@ int AssignSubscript(RepeatedCompositeContainer* self,
// Delete from the underlying Message, if any.
if (self->message != NULL) {
if (cmessage::InternalDeleteRepeatedField(self->message,
- self->parent_field->descriptor,
+ self->parent_field_descriptor,
slice,
self->child_messages) < 0) {
return -1;
@@ -512,7 +484,7 @@ static PyObject* SortAttached(RepeatedCompositeContainer* self,
if (reverse) {
google::protobuf::Message* message = self->message;
const google::protobuf::Reflection* reflection = message->GetReflection();
- const google::protobuf::FieldDescriptor* descriptor = self->parent_field->descriptor;
+ const google::protobuf::FieldDescriptor* descriptor = self->parent_field_descriptor;
// Reverse the Message array.
for (int i = 0; i < length / 2; ++i)
@@ -554,8 +526,9 @@ static PyObject* Sort(RepeatedCompositeContainer* self,
}
}
- if (UpdateChildMessages(self) < 0)
+ if (UpdateChildMessages(self) < 0) {
return NULL;
+ }
if (self->message == NULL) {
return SortReleased(self, args, kwds);
} else {
@@ -617,7 +590,7 @@ void ReleaseLastTo(const FieldDescriptor* field,
shared_ptr<Message> released_message(
ReleaseLast(field, cmessage->message->GetDescriptor(), message));
cmessage->parent = NULL;
- cmessage->parent_field = NULL;
+ cmessage->parent_field_descriptor = NULL;
cmessage->message = released_message.get();
cmessage->read_only = false;
cmessage::SetOwner(cmessage, released_message);
@@ -633,7 +606,7 @@ int Release(RepeatedCompositeContainer* self) {
}
Message* message = self->message;
- const FieldDescriptor* field = self->parent_field->descriptor;
+ 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
@@ -648,7 +621,7 @@ int Release(RepeatedCompositeContainer* self) {
// Detach from containing message.
self->parent = NULL;
- self->parent_field = NULL;
+ self->parent_field_descriptor = NULL;
self->message = NULL;
self->owner.reset();
@@ -670,22 +643,40 @@ int SetOwner(RepeatedCompositeContainer* self,
return 0;
}
-static int Init(RepeatedCompositeContainer* self,
- PyObject* args,
- PyObject* kwargs) {
- self->message = NULL;
- self->parent = NULL;
- self->parent_field = NULL;
- self->subclass_init = NULL;
+// The private constructor of RepeatedCompositeContainer objects.
+PyObject *NewContainer(
+ CMessage* parent,
+ const google::protobuf::FieldDescriptor* parent_field_descriptor,
+ PyObject *concrete_class) {
+ if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) {
+ return NULL;
+ }
+
+ RepeatedCompositeContainer* self =
+ reinterpret_cast<RepeatedCompositeContainer*>(
+ PyType_GenericAlloc(&RepeatedCompositeContainer_Type, 0));
+ if (self == NULL) {
+ return NULL;
+ }
+
+ self->message = parent->message;
+ self->parent = parent;
+ self->parent_field_descriptor = parent_field_descriptor;
+ self->owner = parent->owner;
+ Py_INCREF(concrete_class);
+ self->subclass_init = concrete_class;
self->child_messages = PyList_New(0);
- return 0;
+
+ return reinterpret_cast<PyObject*>(self);
}
static void Dealloc(RepeatedCompositeContainer* self) {
Py_CLEAR(self->child_messages);
+ Py_CLEAR(self->subclass_init);
// 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(reinterpret_cast<PyObject*>(self));
}
@@ -755,7 +746,7 @@ PyTypeObject RepeatedCompositeContainer_Type = {
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
- (initproc)repeated_composite_container::Init, // tp_init
+ 0, // tp_init
};
} // namespace python
diff --git a/python/google/protobuf/pyext/repeated_composite_container.h b/python/google/protobuf/pyext/repeated_composite_container.h
index 898ef5a7..0969af08 100644
--- a/python/google/protobuf/pyext/repeated_composite_container.h
+++ b/python/google/protobuf/pyext/repeated_composite_container.h
@@ -55,18 +55,17 @@ using internal::shared_ptr;
namespace python {
struct CMessage;
-struct CFieldDescriptor;
// 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 refered to by the children in
+// 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' are NULL.
+// 'parent_field_descriptor' are NULL.
typedef struct RepeatedCompositeContainer {
PyObject_HEAD;
@@ -82,7 +81,8 @@ typedef struct RepeatedCompositeContainer {
CMessage* parent;
// A descriptor used to modify the underlying 'message'.
- CFieldDescriptor* parent_field;
+ // The pointer is owned by the global DescriptorPool.
+ const google::protobuf::FieldDescriptor* parent_field_descriptor;
// Pointer to the C++ Message that contains this container. The
// RepeatedCompositeContainer does not own this pointer.
@@ -102,6 +102,13 @@ extern PyTypeObject RepeatedCompositeContainer_Type;
namespace repeated_composite_container {
+// Builds a RepeatedCompositeContainer object, from a parent message and a
+// field descriptor.
+PyObject *NewContainer(
+ CMessage* parent,
+ const google::protobuf::FieldDescriptor* parent_field_descriptor,
+ PyObject *concrete_class);
+
// Returns the number of items in this repeated composite container.
static Py_ssize_t Length(RepeatedCompositeContainer* self);
diff --git a/python/google/protobuf/pyext/repeated_scalar_container.cc b/python/google/protobuf/pyext/repeated_scalar_container.cc
index e627d37d..49d23fd6 100644
--- a/python/google/protobuf/pyext/repeated_scalar_container.cc
+++ b/python/google/protobuf/pyext/repeated_scalar_container.cc
@@ -52,7 +52,7 @@
#error "Python 3.0 - 3.2 are not supported."
#else
#define PyString_AsString(ob) \
- (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AS_STRING(ob))
+ (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob))
#endif
#endif
@@ -67,7 +67,7 @@ namespace repeated_scalar_container {
static int InternalAssignRepeatedField(
RepeatedScalarContainer* self, PyObject* list) {
self->message->GetReflection()->ClearField(self->message,
- self->parent_field->descriptor);
+ self->parent_field_descriptor);
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); ++i) {
PyObject* value = PyList_GET_ITEM(list, i);
if (Append(self, value) == NULL) {
@@ -80,7 +80,7 @@ static int InternalAssignRepeatedField(
static Py_ssize_t Len(RepeatedScalarContainer* self) {
google::protobuf::Message* message = self->message;
return message->GetReflection()->FieldSize(*message,
- self->parent_field->descriptor);
+ self->parent_field_descriptor);
}
static int AssignItem(RepeatedScalarContainer* self,
@@ -89,12 +89,7 @@ static int AssignItem(RepeatedScalarContainer* self,
cmessage::AssureWritable(self->parent);
google::protobuf::Message* message = self->message;
const google::protobuf::FieldDescriptor* field_descriptor =
- self->parent_field->descriptor;
- if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
- PyErr_SetString(
- PyExc_KeyError, "Field does not belong to message!");
- return -1;
- }
+ self->parent_field_descriptor;
const google::protobuf::Reflection* reflection = message->GetReflection();
int field_size = reflection->FieldSize(*message, field_descriptor);
@@ -175,7 +170,7 @@ static int AssignItem(RepeatedScalarContainer* self,
ScopedPyObjectPtr s(PyObject_Str(arg));
if (s != NULL) {
PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
- PyString_AsString(s.get()));
+ PyString_AsString(s));
}
return -1;
}
@@ -193,7 +188,7 @@ static int AssignItem(RepeatedScalarContainer* self,
static PyObject* Item(RepeatedScalarContainer* self, Py_ssize_t index) {
google::protobuf::Message* message = self->message;
const google::protobuf::FieldDescriptor* field_descriptor =
- self->parent_field->descriptor;
+ self->parent_field_descriptor;
const google::protobuf::Reflection* reflection = message->GetReflection();
int field_size = reflection->FieldSize(*message, field_descriptor);
@@ -358,13 +353,7 @@ PyObject* Append(RepeatedScalarContainer* self, PyObject* item) {
cmessage::AssureWritable(self->parent);
google::protobuf::Message* message = self->message;
const google::protobuf::FieldDescriptor* field_descriptor =
- self->parent_field->descriptor;
-
- if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
- PyErr_SetString(
- PyExc_KeyError, "Field does not belong to message!");
- return NULL;
- }
+ self->parent_field_descriptor;
const google::protobuf::Reflection* reflection = message->GetReflection();
switch (field_descriptor->cpp_type()) {
@@ -422,7 +411,7 @@ PyObject* Append(RepeatedScalarContainer* self, PyObject* item) {
ScopedPyObjectPtr s(PyObject_Str(item));
if (s != NULL) {
PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
- PyString_AsString(s.get()));
+ PyString_AsString(s));
}
return NULL;
}
@@ -451,7 +440,7 @@ static int AssSubscript(RepeatedScalarContainer* self,
cmessage::AssureWritable(self->parent);
google::protobuf::Message* message = self->message;
const google::protobuf::FieldDescriptor* field_descriptor =
- self->parent_field->descriptor;
+ self->parent_field_descriptor;
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(slice)) {
@@ -638,47 +627,25 @@ static PyObject* Sort(RepeatedScalarContainer* self,
Py_RETURN_NONE;
}
-static int Init(RepeatedScalarContainer* self,
- PyObject* args,
- PyObject* kwargs) {
- PyObject* py_parent;
- PyObject* py_parent_field;
- if (!PyArg_UnpackTuple(args, "__init__()", 2, 2, &py_parent,
- &py_parent_field)) {
- return -1;
- }
-
- if (!PyObject_TypeCheck(py_parent, &CMessage_Type)) {
- PyErr_Format(PyExc_TypeError,
- "expect %s, but got %s",
- CMessage_Type.tp_name,
- Py_TYPE(py_parent)->tp_name);
- return -1;
+// The private constructor of RepeatedScalarContainer objects.
+PyObject *NewContainer(
+ CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor) {
+ if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) {
+ return NULL;
}
- if (!PyObject_TypeCheck(py_parent_field, &CFieldDescriptor_Type)) {
- PyErr_Format(PyExc_TypeError,
- "expect %s, but got %s",
- CFieldDescriptor_Type.tp_name,
- Py_TYPE(py_parent_field)->tp_name);
- return -1;
+ RepeatedScalarContainer* self = reinterpret_cast<RepeatedScalarContainer*>(
+ PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0));
+ if (self == NULL) {
+ return NULL;
}
- CMessage* cmessage = reinterpret_cast<CMessage*>(py_parent);
- CFieldDescriptor* cdescriptor = reinterpret_cast<CFieldDescriptor*>(
- py_parent_field);
-
- if (!FIELD_BELONGS_TO_MESSAGE(cdescriptor->descriptor, cmessage->message)) {
- PyErr_SetString(
- PyExc_KeyError, "Field does not belong to message!");
- return -1;
- }
+ self->message = parent->message;
+ self->parent = parent;
+ self->parent_field_descriptor = parent_field_descriptor;
+ self->owner = parent->owner;
- self->message = cmessage->message;
- self->parent = cmessage;
- self->parent_field = cdescriptor;
- self->owner = cmessage->owner;
- return 0;
+ return reinterpret_cast<PyObject*>(self);
}
// Initializes the underlying Message object of "to" so it becomes a new parent
@@ -699,10 +666,7 @@ static int InitializeAndCopyToParentContainer(
google::protobuf::Message* new_message = global_message_factory->GetPrototype(
from->message->GetDescriptor())->New();
to->parent = NULL;
- // TODO(anuraag): Document why it's OK to hang on to parent_field,
- // even though it's a weak reference. It ought to be enough to
- // hold on to the FieldDescriptor only.
- to->parent_field = from->parent_field;
+ to->parent_field_descriptor = from->parent_field_descriptor;
to->message = new_message;
to->owner.reset(new_message);
if (InternalAssignRepeatedField(to, values) < 0) {
@@ -716,23 +680,17 @@ int Release(RepeatedScalarContainer* self) {
}
PyObject* DeepCopy(RepeatedScalarContainer* self, PyObject* arg) {
- ScopedPyObjectPtr init_args(
- PyTuple_Pack(2, self->parent, self->parent_field));
- PyObject* clone = PyObject_CallObject(
- reinterpret_cast<PyObject*>(&RepeatedScalarContainer_Type), init_args);
+ RepeatedScalarContainer* clone = reinterpret_cast<RepeatedScalarContainer*>(
+ PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0));
if (clone == NULL) {
return NULL;
}
- if (!PyObject_TypeCheck(clone, &RepeatedScalarContainer_Type)) {
- Py_DECREF(clone);
- return NULL;
- }
- if (InitializeAndCopyToParentContainer(
- self, reinterpret_cast<RepeatedScalarContainer*>(clone)) < 0) {
+
+ if (InitializeAndCopyToParentContainer(self, clone) < 0) {
Py_DECREF(clone);
return NULL;
}
- return clone;
+ return reinterpret_cast<PyObject*>(clone);
}
static void Dealloc(RepeatedScalarContainer* self) {
@@ -817,7 +775,7 @@ PyTypeObject RepeatedScalarContainer_Type = {
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
- (initproc)repeated_scalar_container::Init, // tp_init
+ 0, // tp_init
};
} // namespace python
diff --git a/python/google/protobuf/pyext/repeated_scalar_container.h b/python/google/protobuf/pyext/repeated_scalar_container.h
index 69d15d5c..513bfe48 100644
--- a/python/google/protobuf/pyext/repeated_scalar_container.h
+++ b/python/google/protobuf/pyext/repeated_scalar_container.h
@@ -41,6 +41,7 @@
#include <google/protobuf/stubs/shared_ptr.h>
#endif
+#include <google/protobuf/descriptor.h>
namespace google {
namespace protobuf {
@@ -51,7 +52,6 @@ using internal::shared_ptr;
namespace python {
-struct CFieldDescriptor;
struct CMessage;
typedef struct RepeatedScalarContainer {
@@ -73,16 +73,22 @@ typedef struct RepeatedScalarContainer {
// modifying the container.
CMessage* parent;
- // Weak reference to the parent's descriptor that describes this
+ // Pointer to the parent's descriptor that describes this
// field. Used together with the parent's message when making a
// default message instance mutable.
- CFieldDescriptor* parent_field;
+ // The pointer is owned by the global DescriptorPool.
+ const google::protobuf::FieldDescriptor* parent_field_descriptor;
} RepeatedScalarContainer;
extern PyTypeObject RepeatedScalarContainer_Type;
namespace repeated_scalar_container {
+// Builds a RepeatedScalarContainer object, from a parent message and a
+// field descriptor.
+extern PyObject *NewContainer(
+ CMessage* parent, const google::protobuf::FieldDescriptor* parent_field_descriptor);
+
// Appends the scalar 'item' to the end of the container 'self'.
//
// Returns None if successful; returns NULL and sets an exception if
diff --git a/python/google/protobuf/pyext/scoped_pyobject_ptr.h b/python/google/protobuf/pyext/scoped_pyobject_ptr.h
index 9f337c3c..18ddd5cd 100644
--- a/python/google/protobuf/pyext/scoped_pyobject_ptr.h
+++ b/python/google/protobuf/pyext/scoped_pyobject_ptr.h
@@ -33,12 +33,14 @@
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__
#define GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__
+#include <google/protobuf/stubs/common.h>
+
#include <Python.h>
namespace google {
class ScopedPyObjectPtr {
public:
- // Constructor. Defaults to intializing with NULL.
+ // Constructor. Defaults to initializing with NULL.
// There is no way to create an uninitialized ScopedPyObjectPtr.
explicit ScopedPyObjectPtr(PyObject* p = NULL) : ptr_(p) { }
diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py
index 2429fa59..fb54c50c 100755
--- a/python/google/protobuf/text_format.py
+++ b/python/google/protobuf/text_format.py
@@ -112,15 +112,17 @@ def PrintMessage(message, out, indent=0, as_utf8=False, as_one_line=False,
for element in value:
PrintField(field, element, out, indent, as_utf8, as_one_line,
pointy_brackets=pointy_brackets,
+ use_index_order=use_index_order,
float_format=float_format)
else:
PrintField(field, value, out, indent, as_utf8, as_one_line,
pointy_brackets=pointy_brackets,
+ use_index_order=use_index_order,
float_format=float_format)
def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
- pointy_brackets=False, float_format=None):
+ pointy_brackets=False, use_index_order=False, float_format=None):
"""Print a single field name/value pair. For repeated fields, the value
should be a single element."""
@@ -148,6 +150,7 @@ def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
PrintFieldValue(field, value, out, indent, as_utf8, as_one_line,
pointy_brackets=pointy_brackets,
+ use_index_order=use_index_order,
float_format=float_format)
if as_one_line:
out.write(' ')
@@ -157,6 +160,7 @@ def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
as_one_line=False, pointy_brackets=False,
+ use_index_order=False,
float_format=None):
"""Print a single field value (not including name). For repeated fields,
the value should be a single element."""
@@ -173,12 +177,14 @@ def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
out.write(' %s ' % openb)
PrintMessage(value, out, indent, as_utf8, as_one_line,
pointy_brackets=pointy_brackets,
+ use_index_order=use_index_order,
float_format=float_format)
out.write(closeb)
else:
out.write(' %s\n' % openb)
PrintMessage(value, out, indent + 2, as_utf8, as_one_line,
pointy_brackets=pointy_brackets,
+ use_index_order=use_index_order,
float_format=float_format)
out.write(' ' * indent + closeb)
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
@@ -211,24 +217,6 @@ def PrintFieldValue(field, value, out, indent=0, as_utf8=False,
out.write(str(value))
-def _ParseOrMerge(lines, message, allow_multiple_scalars):
- """Converts an ASCII representation of a protocol message into a message.
-
- Args:
- lines: Lines of a message's ASCII representation.
- message: A protocol buffer message to merge into.
- allow_multiple_scalars: Determines if repeated values for a non-repeated
- field are permitted, e.g., the string "foo: 1 foo: 2" for a
- required/optional field named "foo".
-
- Raises:
- ParseError: On ASCII parsing problems.
- """
- tokenizer = _Tokenizer(lines)
- while not tokenizer.AtEnd():
- _MergeField(tokenizer, message, allow_multiple_scalars)
-
-
def Parse(text, message):
"""Parses an ASCII representation of a protocol message into a message.
@@ -299,6 +287,24 @@ def MergeLines(lines, message):
return message
+def _ParseOrMerge(lines, message, allow_multiple_scalars):
+ """Converts an ASCII representation of a protocol message into a message.
+
+ Args:
+ lines: Lines of a message's ASCII representation.
+ message: A protocol buffer message to merge into.
+ allow_multiple_scalars: Determines if repeated values for a non-repeated
+ field are permitted, e.g., the string "foo: 1 foo: 2" for a
+ required/optional field named "foo".
+
+ Raises:
+ ParseError: On ASCII parsing problems.
+ """
+ tokenizer = _Tokenizer(lines)
+ while not tokenizer.AtEnd():
+ _MergeField(tokenizer, message, allow_multiple_scalars)
+
+
def _MergeField(tokenizer, message, allow_multiple_scalars):
"""Merges a single protocol message field into a message.
@@ -693,7 +699,7 @@ class _Tokenizer(object):
"""
text = self.token
if len(text) < 1 or text[0] not in ('\'', '"'):
- raise self._ParseError('Expected string.')
+ raise self._ParseError('Expected string but found: "%r"' % text)
if len(text) < 2 or text[-1] != text[0]:
raise self._ParseError('String missing ending quote.')
diff --git a/python/mox.py b/python/mox.py
index ce80ba50..257468e5 100755
--- a/python/mox.py
+++ b/python/mox.py
@@ -31,7 +31,7 @@ If an unexpected method (or an expected method with unexpected
parameters) is called, then an exception will be raised.
Once you are done interacting with the mock, you need to verify that
-all the expected interactions occured. (Maybe your code exited
+all the expected interactions occurred. (Maybe your code exited
prematurely without calling some cleanup method!) The verify phase
ensures that every expected method was called; otherwise, an exception
will be raised.
diff --git a/python/setup.py b/python/setup.py
index 17beac06..cfe25cc0 100755
--- a/python/setup.py
+++ b/python/setup.py
@@ -20,7 +20,12 @@ except ImportError:
"ez_setup installed.\n")
raise
from distutils.command.clean import clean as _clean
-from distutils.command.build_py import build_py as _build_py
+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
maintainer_email = "protobuf@googlegroups.com"
@@ -81,6 +86,8 @@ def GenerateUnittestProtos():
generate_proto("google/protobuf/internal/more_messages.proto")
generate_proto("google/protobuf/internal/factory_test1.proto")
generate_proto("google/protobuf/internal/factory_test2.proto")
+ generate_proto("google/protobuf/internal/import_test_package/inner.proto")
+ generate_proto("google/protobuf/internal/import_test_package/outer.proto")
generate_proto("google/protobuf/pyext/python.proto")
def MakeTestSuite():
@@ -150,13 +157,13 @@ if __name__ == '__main__':
"google/protobuf/pyext/repeated_scalar_container.cc",
"google/protobuf/pyext/repeated_composite_container.cc" ],
define_macros=[('GOOGLE_PROTOBUF_HAS_ONEOF', '1')],
- include_dirs = [ ".", "../src"],
+ include_dirs = [ ".", "..", "../src"],
libraries = [ "protobuf" ],
library_dirs = [ '../src/.libs' ],
))
setup(name = 'protobuf',
- version = '2.6.0',
+ version = '3.0.0-pre',
packages = [ 'google' ],
namespace_packages = [ 'google' ],
test_suite = 'setup.MakeTestSuite',
@@ -180,6 +187,7 @@ if __name__ == '__main__':
'google.protobuf.descriptor_database',
'google.protobuf.descriptor_pool',
'google.protobuf.message_factory',
+ 'google.protobuf.proto_builder',
'google.protobuf.pyext.cpp_message',
'google.protobuf.reflection',
'google.protobuf.service',
@@ -189,7 +197,11 @@ if __name__ == '__main__':
'google.protobuf.text_format'],
cmdclass = { 'clean': clean, 'build_py': build_py },
install_requires = ['setuptools'],
- setup_requires = ['google-apputils'],
+ # TODO: Restore dependency once a Python 3 compatible google-apputils
+ # is released.
+ setup_requires = (['google-apputils']
+ if sys.version_info[0] < 3 else
+ []),
ext_modules = ext_module_list,
url = 'https://developers.google.com/protocol-buffers/',
maintainer = maintainer_email,
diff --git a/ruby/README.md b/ruby/README.md
new file mode 100644
index 00000000..c966a103
--- /dev/null
+++ b/ruby/README.md
@@ -0,0 +1,34 @@
+This directory contains the Ruby extension that implements Protocol Buffers
+functionality in Ruby.
+
+The Ruby extension makes use of generated Ruby code that defines message and
+enum types in a Ruby DSL. You may write definitions in this DSL directly, but
+we recommend using protoc's Ruby generation support with .proto files. The
+build process in this directory only installs the extension; you need to
+install protoc as well to have Ruby code generation functionality.
+
+Installation
+------------
+
+To build this Ruby extension, you will need:
+
+* Rake
+* Bundler
+* Ruby development headers
+* a C compiler
+* the upb submodule
+
+First, ensure that upb/ is checked out:
+
+ $ cd .. # top level protobuf directory
+ $ git submodule init
+ $ git submodule update
+
+Then install the required Ruby gems:
+
+ $ sudo gem install bundler rake rake-compiler rspec rubygems-tasks
+
+Then build the Gem:
+
+ $ rake gem
+ $ gem install pkg/protobuf-$VERSION.gem
diff --git a/ruby/Rakefile b/ruby/Rakefile
new file mode 100644
index 00000000..ae7d8059
--- /dev/null
+++ b/ruby/Rakefile
@@ -0,0 +1,21 @@
+require "rake/extensiontask"
+require "rake/testtask"
+
+spec = Gem::Specification.load("google-protobuf.gemspec")
+
+Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
+ ext.ext_dir = "ext/google/protobuf_c"
+ ext.lib_dir = "lib/google"
+end
+
+Rake::TestTask.new(:test => :build) do |t|
+ t.test_files = FileList["tests/*.rb"]
+end
+
+Gem::PackageTask.new(spec) do |pkg|
+end
+
+task :build => [:clean, :compile]
+task :default => [:build]
+
+# vim:sw=2:et
diff --git a/ruby/ext/google/protobuf_c/defs.c b/ruby/ext/google/protobuf_c/defs.c
new file mode 100644
index 00000000..bb6f10e1
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/defs.c
@@ -0,0 +1,1286 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+// -----------------------------------------------------------------------------
+// Common utilities.
+// -----------------------------------------------------------------------------
+
+const char* kDescriptorInstanceVar = "descriptor";
+
+static const char* get_str(VALUE str) {
+ Check_Type(str, T_STRING);
+ return RSTRING_PTR(str);
+}
+
+static VALUE rb_str_maybe_null(const char* s) {
+ if (s == NULL) {
+ s = "";
+ }
+ return rb_str_new2(s);
+}
+
+static upb_def* check_notfrozen(const upb_def* def) {
+ if (upb_def_isfrozen(def)) {
+ rb_raise(rb_eRuntimeError,
+ "Attempt to modify a frozen descriptor. Once descriptors are "
+ "added to the descriptor pool, they may not be modified.");
+ }
+ return (upb_def*)def;
+}
+
+static upb_msgdef* check_msg_notfrozen(const upb_msgdef* def) {
+ return (upb_msgdef*)check_notfrozen((const upb_def*)def);
+}
+
+static upb_fielddef* check_field_notfrozen(const upb_fielddef* def) {
+ return (upb_fielddef*)check_notfrozen((const upb_def*)def);
+}
+
+static upb_enumdef* check_enum_notfrozen(const upb_enumdef* def) {
+ return (upb_enumdef*)check_notfrozen((const upb_def*)def);
+}
+
+// -----------------------------------------------------------------------------
+// DescriptorPool.
+// -----------------------------------------------------------------------------
+
+#define DEFINE_CLASS(name, string_name) \
+ VALUE c ## name; \
+ const rb_data_type_t _ ## name ## _type = { \
+ string_name, \
+ { name ## _mark, name ## _free, NULL }, \
+ }; \
+ name* ruby_to_ ## name(VALUE val) { \
+ name* ret; \
+ TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
+ return ret; \
+ } \
+
+#define DEFINE_SELF(type, var, rb_var) \
+ type* var = ruby_to_ ## type(rb_var);
+
+// Global singleton DescriptorPool. The user is free to create others, but this
+// is used by generated code.
+VALUE generated_pool;
+
+DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
+
+void DescriptorPool_mark(void* _self) {
+}
+
+void DescriptorPool_free(void* _self) {
+ DescriptorPool* self = _self;
+ upb_symtab_unref(self->symtab, &self->symtab);
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * DescriptorPool.new => pool
+ *
+ * Creates a new, empty, descriptor pool.
+ */
+VALUE DescriptorPool_alloc(VALUE klass) {
+ DescriptorPool* self = ALLOC(DescriptorPool);
+ self->symtab = upb_symtab_new(&self->symtab);
+ return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
+}
+
+void DescriptorPool_register(VALUE module) {
+ VALUE klass = rb_define_class_under(
+ 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, "lookup", DescriptorPool_lookup, 1);
+ rb_define_singleton_method(klass, "generated_pool",
+ DescriptorPool_generated_pool, 0);
+ cDescriptorPool = klass;
+ rb_gc_register_address(&cDescriptorPool);
+
+ generated_pool = rb_class_new_instance(0, NULL, klass);
+ rb_gc_register_address(&generated_pool);
+}
+
+static void add_descriptor_to_pool(DescriptorPool* self,
+ Descriptor* descriptor) {
+ CHECK_UPB(
+ upb_symtab_add(self->symtab, (upb_def**)&descriptor->msgdef, 1,
+ NULL, &status),
+ "Adding Descriptor to DescriptorPool failed");
+}
+
+static void add_enumdesc_to_pool(DescriptorPool* self,
+ EnumDescriptor* enumdesc) {
+ CHECK_UPB(
+ upb_symtab_add(self->symtab, (upb_def**)&enumdesc->enumdef, 1,
+ NULL, &status),
+ "Adding EnumDescriptor to DescriptorPool failed");
+}
+
+/*
+ * call-seq:
+ * DescriptorPool.add(descriptor)
+ *
+ * Adds the given Descriptor or EnumDescriptor to this pool. All references to
+ * other types in a Descriptor's fields must be resolvable within this pool or
+ * an exception will be raised.
+ */
+VALUE DescriptorPool_add(VALUE _self, VALUE def) {
+ DEFINE_SELF(DescriptorPool, self, _self);
+ VALUE def_klass = rb_obj_class(def);
+ if (def_klass == cDescriptor) {
+ add_descriptor_to_pool(self, ruby_to_Descriptor(def));
+ } else if (def_klass == cEnumDescriptor) {
+ add_enumdesc_to_pool(self, ruby_to_EnumDescriptor(def));
+ } else {
+ rb_raise(rb_eArgError,
+ "Second argument must be a Descriptor or EnumDescriptor.");
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * DescriptorPool.build(&block)
+ *
+ * Invokes the block with a Builder instance as self. All message and enum types
+ * added within the block are committed to the pool atomically, and may refer
+ * (co)recursively to each other. The user should call Builder#add_message and
+ * 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 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);
+ rb_funcall(ctx, rb_intern("finalize_to_pool"), 1, _self);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * DescriptorPool.lookup(name) => descriptor
+ *
+ * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
+ * exists with the given name.
+ */
+VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
+ DEFINE_SELF(DescriptorPool, self, _self);
+ const char* name_str = get_str(name);
+ const upb_def* def = upb_symtab_lookup(self->symtab, name_str);
+ if (!def) {
+ return Qnil;
+ }
+ return get_def_obj(def);
+}
+
+/*
+ * call-seq:
+ * DescriptorPool.generated_pool => descriptor_pool
+ *
+ * Class method that returns the global DescriptorPool. This is a singleton into
+ * which generated-code message and enum types are registered. The user may also
+ * register types in this pool for convenience so that they do not have to hold
+ * a reference to a private pool instance.
+ */
+VALUE DescriptorPool_generated_pool(VALUE _self) {
+ return generated_pool;
+}
+
+// -----------------------------------------------------------------------------
+// Descriptor.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
+
+void Descriptor_mark(void* _self) {
+ Descriptor* self = _self;
+ rb_gc_mark(self->klass);
+}
+
+void Descriptor_free(void* _self) {
+ Descriptor* self = _self;
+ upb_msgdef_unref(self->msgdef, &self->msgdef);
+ 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->pb_serialize_handlers) {
+ upb_handlers_unref(self->pb_serialize_handlers,
+ &self->pb_serialize_handlers);
+ }
+ if (self->json_serialize_handlers) {
+ upb_handlers_unref(self->pb_serialize_handlers,
+ &self->json_serialize_handlers);
+ }
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * Descriptor.new => descriptor
+ *
+ * Creates a new, empty, message type descriptor. At a minimum, its name must be
+ * set before it is added to a pool. It cannot be used to create messages until
+ * it is added to a pool, after which it becomes immutable (as part of a
+ * finalization process).
+ */
+VALUE Descriptor_alloc(VALUE klass) {
+ Descriptor* self = ALLOC(Descriptor);
+ VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
+ self->msgdef = upb_msgdef_new(&self->msgdef);
+ self->klass = Qnil;
+ self->layout = NULL;
+ self->fill_handlers = NULL;
+ self->fill_method = NULL;
+ self->pb_serialize_handlers = NULL;
+ self->json_serialize_handlers = NULL;
+ return ret;
+}
+
+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, "each", Descriptor_each, 0);
+ rb_define_method(klass, "lookup", Descriptor_lookup, 1);
+ rb_define_method(klass, "add_field", Descriptor_add_field, 1);
+ 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_include_module(klass, rb_mEnumerable);
+ cDescriptor = klass;
+ rb_gc_register_address(&cDescriptor);
+}
+
+/*
+ * call-seq:
+ * Descriptor.name => name
+ *
+ * Returns the name of this message type as a fully-qualfied string (e.g.,
+ * My.Package.MessageType).
+ */
+VALUE Descriptor_name(VALUE _self) {
+ DEFINE_SELF(Descriptor, self, _self);
+ return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
+}
+
+/*
+ * call-seq:
+ * Descriptor.name = name
+ *
+ * Assigns a name to this message type. The descriptor must not have been added
+ * to a pool yet.
+ */
+VALUE Descriptor_name_set(VALUE _self, VALUE str) {
+ DEFINE_SELF(Descriptor, self, _self);
+ upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
+ const char* name = get_str(str);
+ CHECK_UPB(
+ upb_msgdef_setfullname(mut_def, name, &status),
+ "Error setting Descriptor name");
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Descriptor.each(&block)
+ *
+ * Iterates over fields in this message type, yielding to the block on each one.
+ */
+VALUE Descriptor_each(VALUE _self) {
+ DEFINE_SELF(Descriptor, self, _self);
+
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, self->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ VALUE obj = get_def_obj(field);
+ rb_yield(obj);
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Descriptor.lookup(name) => FieldDescriptor
+ *
+ * Returns the field descriptor for the field with the given name, if present,
+ * or nil if none.
+ */
+VALUE Descriptor_lookup(VALUE _self, VALUE name) {
+ DEFINE_SELF(Descriptor, self, _self);
+ const char* s = get_str(name);
+ const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
+ if (field == NULL) {
+ return Qnil;
+ }
+ return get_def_obj(field);
+}
+
+/*
+ * call-seq:
+ * Descriptor.add_field(field) => nil
+ *
+ * Adds the given FieldDescriptor to this message type. The descriptor must not
+ * have been added to a pool yet. Raises an exception if a field with the same
+ * name or number already exists. Sub-type references (e.g. for fields of type
+ * message) are not resolved at this point.
+ */
+VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
+ DEFINE_SELF(Descriptor, self, _self);
+ upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
+ FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
+ upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
+ CHECK_UPB(
+ upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
+ "Adding field to Descriptor failed");
+ add_def_obj(def->fielddef, obj);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Descriptor.msgclass => message_klass
+ *
+ * Returns the Ruby class created for this message type. Valid only once the
+ * message type has been added to a pool.
+ */
+VALUE Descriptor_msgclass(VALUE _self) {
+ DEFINE_SELF(Descriptor, self, _self);
+ if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
+ rb_raise(rb_eRuntimeError,
+ "Cannot fetch message class from a Descriptor not yet in a pool.");
+ }
+ if (self->klass == Qnil) {
+ self->klass = build_class_from_descriptor(self);
+ }
+ return self->klass;
+}
+
+// -----------------------------------------------------------------------------
+// FieldDescriptor.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
+
+void FieldDescriptor_mark(void* _self) {
+}
+
+void FieldDescriptor_free(void* _self) {
+ FieldDescriptor* self = _self;
+ upb_fielddef_unref(self->fielddef, &self->fielddef);
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.new => field
+ *
+ * Returns a new field descriptor. Its name, type, etc. must be set before it is
+ * added to a message type.
+ */
+VALUE FieldDescriptor_alloc(VALUE klass) {
+ FieldDescriptor* self = ALLOC(FieldDescriptor);
+ VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
+ upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef);
+ upb_fielddef_setpacked(fielddef, false);
+ self->fielddef = fielddef;
+ return ret;
+}
+
+void FieldDescriptor_register(VALUE module) {
+ VALUE klass = rb_define_class_under(
+ module, "FieldDescriptor", rb_cObject);
+ rb_define_alloc_func(klass, FieldDescriptor_alloc);
+ rb_define_method(klass, "name", FieldDescriptor_name, 0);
+ 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, "label", FieldDescriptor_label, 0);
+ rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
+ rb_define_method(klass, "number", FieldDescriptor_number, 0);
+ rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
+ 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, "get", FieldDescriptor_get, 1);
+ rb_define_method(klass, "set", FieldDescriptor_set, 2);
+ cFieldDescriptor = klass;
+ rb_gc_register_address(&cFieldDescriptor);
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.name => name
+ *
+ * Returns the name of this field.
+ */
+VALUE FieldDescriptor_name(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.name = name
+ *
+ * Sets the name of this field. Cannot be called once the containing message
+ * type, if any, is added to a pool.
+ */
+VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
+ const char* name = get_str(str);
+ CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
+ "Error setting FieldDescriptor name");
+ return Qnil;
+}
+
+upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
+ if (TYPE(type) != T_SYMBOL) {
+ rb_raise(rb_eArgError, "Expected symbol for field type.");
+ }
+
+ upb_fieldtype_t upb_type = -1;
+
+#define CONVERT(upb, ruby) \
+ if (SYM2ID(type) == rb_intern( # ruby )) { \
+ upb_type = UPB_TYPE_ ## upb; \
+ }
+
+ CONVERT(FLOAT, float);
+ CONVERT(DOUBLE, double);
+ CONVERT(BOOL, bool);
+ CONVERT(STRING, string);
+ CONVERT(BYTES, bytes);
+ CONVERT(MESSAGE, message);
+ CONVERT(ENUM, enum);
+ CONVERT(INT32, int32);
+ CONVERT(INT64, int64);
+ CONVERT(UINT32, uint32);
+ CONVERT(UINT64, uint64);
+
+#undef CONVERT
+
+ if (upb_type == -1) {
+ rb_raise(rb_eArgError, "Unknown field type.");
+ }
+
+ return upb_type;
+}
+
+VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
+ switch (type) {
+#define CONVERT(upb, ruby) \
+ case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
+ CONVERT(FLOAT, float);
+ CONVERT(DOUBLE, double);
+ CONVERT(BOOL, bool);
+ CONVERT(STRING, string);
+ CONVERT(BYTES, bytes);
+ CONVERT(MESSAGE, message);
+ CONVERT(ENUM, enum);
+ CONVERT(INT32, int32);
+ CONVERT(INT64, int64);
+ CONVERT(UINT32, uint32);
+ CONVERT(UINT64, uint64);
+#undef CONVERT
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.type => type
+ *
+ * Returns this field's type, as a Ruby symbol, or nil if not yet set.
+ *
+ * Valid field types are:
+ * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
+ * :bytes, :message.
+ */
+VALUE FieldDescriptor_type(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ if (!upb_fielddef_typeisset(self->fielddef)) {
+ return Qnil;
+ }
+ return fieldtype_to_ruby(upb_fielddef_type(self->fielddef));
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.type = type
+ *
+ * Sets this field's type. Cannot be called if field is part of a message type
+ * already in a pool.
+ */
+VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
+ upb_fielddef_settype(mut_def, ruby_to_fieldtype(type));
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.label => label
+ *
+ * Returns this field's label (i.e., plurality), as a Ruby symbol.
+ *
+ * Valid field labels are:
+ * :optional, :repeated
+ */
+VALUE FieldDescriptor_label(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ switch (upb_fielddef_label(self->fielddef)) {
+#define CONVERT(upb, ruby) \
+ case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
+
+ CONVERT(OPTIONAL, optional);
+ CONVERT(REQUIRED, required);
+ CONVERT(REPEATED, repeated);
+
+#undef CONVERT
+ }
+
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.label = label
+ *
+ * Sets the label on this field. Cannot be called if field is part of a message
+ * type already in a pool.
+ */
+VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
+ if (TYPE(label) != T_SYMBOL) {
+ rb_raise(rb_eArgError, "Expected symbol for field label.");
+ }
+
+ upb_label_t upb_label = -1;
+
+#define CONVERT(upb, ruby) \
+ if (SYM2ID(label) == rb_intern( # ruby )) { \
+ upb_label = UPB_LABEL_ ## upb; \
+ }
+
+ CONVERT(OPTIONAL, optional);
+ CONVERT(REQUIRED, required);
+ CONVERT(REPEATED, repeated);
+
+#undef CONVERT
+
+ if (upb_label == -1) {
+ rb_raise(rb_eArgError, "Unknown field label.");
+ }
+
+ upb_fielddef_setlabel(mut_def, upb_label);
+
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.number => number
+ *
+ * Returns the tag number for this field.
+ */
+VALUE FieldDescriptor_number(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ return INT2NUM(upb_fielddef_number(self->fielddef));
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.number = number
+ *
+ * Sets the tag number for this field. Cannot be called if field is part of a
+ * message type already in a pool.
+ */
+VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
+ CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
+ "Error setting field number");
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.submsg_name => submsg_name
+ *
+ * Returns the name of the message or enum type corresponding to this field, if
+ * it is a message or enum field (respectively), or nil otherwise. This type
+ * name will be resolved within the context of the pool to which the containing
+ * message type is added.
+ */
+VALUE FieldDescriptor_submsg_name(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
+ return Qnil;
+ }
+ return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.submsg_name = submsg_name
+ *
+ * Sets the name of the message or enum type corresponding to this field, if it
+ * is a message or enum field (respectively). This type name will be resolved
+ * within the context of the pool to which the containing message type is added.
+ * Cannot be called on field that are not of message or enum type, or on fields
+ * that are part of a message type already added to a pool.
+ */
+VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
+ rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef.");
+ }
+ const char* str = get_str(value);
+ CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
+ "Error setting submessage name");
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.subtype => message_or_enum_descriptor
+ *
+ * Returns the message or enum descriptor corresponding to this field's type if
+ * it is a message or enum field, respectively, or nil otherwise. Cannot be
+ * called *until* the containing message type is added to a pool (and thus
+ * resolved).
+ */
+VALUE FieldDescriptor_subtype(VALUE _self) {
+ DEFINE_SELF(FieldDescriptor, self, _self);
+ if (!upb_fielddef_hassubdef(self->fielddef)) {
+ return Qnil;
+ }
+ const upb_def* def = upb_fielddef_subdef(self->fielddef);
+ if (def == NULL) {
+ return Qnil;
+ }
+ return get_def_obj(def);
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.get(message) => value
+ *
+ * Returns the value set for this field on the given message. Raises an
+ * exception if message is of the wrong type.
+ */
+VALUE FieldDescriptor_get(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(rb_eTypeError, "get method called on wrong message type");
+ }
+ return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
+}
+
+/*
+ * call-seq:
+ * FieldDescriptor.set(message, value)
+ *
+ * Sets the value corresponding to this field to the given value on the given
+ * message. Raises an exception if message is of the wrong type. Performs the
+ * ordinary type-checks for field setting.
+ */
+VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
+ 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(rb_eTypeError, "set method called on wrong message type");
+ }
+ layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
+ return Qnil;
+}
+
+// -----------------------------------------------------------------------------
+// EnumDescriptor.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
+
+void EnumDescriptor_mark(void* _self) {
+ EnumDescriptor* self = _self;
+ rb_gc_mark(self->module);
+}
+
+void EnumDescriptor_free(void* _self) {
+ EnumDescriptor* self = _self;
+ upb_enumdef_unref(self->enumdef, &self->enumdef);
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.new => enum_descriptor
+ *
+ * Creates a new, empty, enum descriptor. Must be added to a pool before the
+ * enum type can be used. The enum type may only be modified prior to adding to
+ * a pool.
+ */
+VALUE EnumDescriptor_alloc(VALUE klass) {
+ EnumDescriptor* self = ALLOC(EnumDescriptor);
+ VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
+ self->enumdef = upb_enumdef_new(&self->enumdef);
+ self->module = Qnil;
+ return ret;
+}
+
+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, "name", EnumDescriptor_name, 0);
+ rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
+ rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
+ rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
+ 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_include_module(klass, rb_mEnumerable);
+ cEnumDescriptor = klass;
+ rb_gc_register_address(&cEnumDescriptor);
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.name => name
+ *
+ * Returns the name of this enum type.
+ */
+VALUE EnumDescriptor_name(VALUE _self) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.name = name
+ *
+ * Sets the name of this enum type. Cannot be called if the enum type has
+ * already been added to a pool.
+ */
+VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
+ const char* name = get_str(str);
+ CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
+ "Error setting EnumDescriptor name");
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.add_value(key, value)
+ *
+ * Adds a new key => value mapping to this enum type. Key must be given as a
+ * Ruby symbol. Cannot be called if the enum type has already been added to a
+ * pool. Will raise an exception if the key or value is already in use.
+ */
+VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
+ const char* name_str = rb_id2name(SYM2ID(name));
+ int32_t val = NUM2INT(number);
+ CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
+ "Error adding value to enum");
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.lookup_name(name) => value
+ *
+ * Returns the numeric value corresponding to the given key name (as a Ruby
+ * symbol), or nil if none.
+ */
+VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ const char* name_str= rb_id2name(SYM2ID(name));
+ int32_t val = 0;
+ if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
+ return INT2NUM(val);
+ } else {
+ return Qnil;
+ }
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.lookup_value(name) => value
+ *
+ * Returns the key name (as a Ruby symbol) corresponding to the integer value,
+ * or nil if none.
+ */
+VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ int32_t val = NUM2INT(number);
+ const char* name = upb_enumdef_iton(self->enumdef, val);
+ if (name != NULL) {
+ return ID2SYM(rb_intern(name));
+ } else {
+ return Qnil;
+ }
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.each(&block)
+ *
+ * Iterates over key => value mappings in this enum's definition, yielding to
+ * the block with (key, value) arguments for each one.
+ */
+VALUE EnumDescriptor_each(VALUE _self) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+
+ upb_enum_iter it;
+ for (upb_enum_begin(&it, self->enumdef);
+ !upb_enum_done(&it);
+ upb_enum_next(&it)) {
+ VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
+ VALUE number = INT2NUM(upb_enum_iter_number(&it));
+ rb_yield_values(2, key, number);
+ }
+
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * EnumDescriptor.enummodule => module
+ *
+ * Returns the Ruby module corresponding to this enum type. Cannot be called
+ * until the enum descriptor has been added to a pool.
+ */
+VALUE EnumDescriptor_enummodule(VALUE _self) {
+ DEFINE_SELF(EnumDescriptor, self, _self);
+ if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
+ rb_raise(rb_eRuntimeError,
+ "Cannot fetch enum module from an EnumDescriptor not yet "
+ "in a pool.");
+ }
+ if (self->module == Qnil) {
+ self->module = build_module_from_enumdesc(self);
+ }
+ return self->module;
+}
+
+// -----------------------------------------------------------------------------
+// MessageBuilderContext.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(MessageBuilderContext,
+ "Google::Protobuf::Internal::MessageBuilderContext");
+
+void MessageBuilderContext_mark(void* _self) {
+ MessageBuilderContext* self = _self;
+ rb_gc_mark(self->descriptor);
+}
+
+void MessageBuilderContext_free(void* _self) {
+ MessageBuilderContext* self = _self;
+ xfree(self);
+}
+
+VALUE MessageBuilderContext_alloc(VALUE klass) {
+ MessageBuilderContext* self = ALLOC(MessageBuilderContext);
+ VALUE ret = TypedData_Wrap_Struct(
+ klass, &_MessageBuilderContext_type, self);
+ self->descriptor = Qnil;
+ return ret;
+}
+
+void MessageBuilderContext_register(VALUE module) {
+ VALUE klass = rb_define_class_under(
+ module, "MessageBuilderContext", rb_cObject);
+ rb_define_alloc_func(klass, MessageBuilderContext_alloc);
+ rb_define_method(klass, "initialize",
+ MessageBuilderContext_initialize, 1);
+ rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
+ rb_define_method(klass, "required", MessageBuilderContext_required, -1);
+ rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
+ cMessageBuilderContext = klass;
+ rb_gc_register_address(&cMessageBuilderContext);
+}
+
+/*
+ * call-seq:
+ * MessageBuilderContext.new(desc) => context
+ *
+ * Create a new builder context around the given message descriptor. This class
+ * is intended to serve as a DSL context to be used with #instance_eval.
+ */
+VALUE MessageBuilderContext_initialize(VALUE _self, VALUE msgdef) {
+ DEFINE_SELF(MessageBuilderContext, self, _self);
+ self->descriptor = msgdef;
+ return Qnil;
+}
+
+static VALUE msgdef_add_field(VALUE msgdef,
+ const char* label, VALUE name,
+ VALUE type, VALUE number,
+ VALUE type_class) {
+ VALUE fielddef = 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);
+
+ if (type_class != Qnil) {
+ if (TYPE(type_class) != T_STRING) {
+ rb_raise(rb_eArgError, "Expected string for type class");
+ }
+ // 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(msgdef, rb_intern("add_field"), 1, fielddef);
+ return fielddef;
+}
+
+/*
+ * call-seq:
+ * MessageBuilderContext.optional(name, type, number, type_class = 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
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
+ */
+VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
+ DEFINE_SELF(MessageBuilderContext, self, _self);
+
+ if (argc < 3) {
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
+ }
+ VALUE name = argv[0];
+ VALUE type = argv[1];
+ VALUE number = argv[2];
+ VALUE type_class = (argc > 3) ? argv[3] : Qnil;
+
+ return msgdef_add_field(self->descriptor, "optional",
+ name, type, number, type_class);
+}
+
+/*
+ * call-seq:
+ * MessageBuilderContext.required(name, type, number, type_class = 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
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
+ *
+ * Proto3 does not have required fields, but this method exists for
+ * completeness. Any attempt to add a message type with required fields to a
+ * pool will currently result in an error.
+ */
+VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
+ DEFINE_SELF(MessageBuilderContext, self, _self);
+
+ if (argc < 3) {
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
+ }
+ VALUE name = argv[0];
+ VALUE type = argv[1];
+ VALUE number = argv[2];
+ VALUE type_class = (argc > 3) ? argv[3] : Qnil;
+
+ return msgdef_add_field(self->descriptor, "required",
+ name, type, number, type_class);
+}
+
+/*
+ * call-seq:
+ * MessageBuilderContext.repeated(name, type, number, type_class = nil)
+ *
+ * Defines a new repeated 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
+ * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
+ * string, if present (as accepted by FieldDescriptor#submsg_name=).
+ */
+VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
+ DEFINE_SELF(MessageBuilderContext, self, _self);
+
+ if (argc < 3) {
+ rb_raise(rb_eArgError, "Expected at least 3 arguments.");
+ }
+ VALUE name = argv[0];
+ VALUE type = argv[1];
+ VALUE number = argv[2];
+ VALUE type_class = (argc > 3) ? argv[3] : Qnil;
+
+ return msgdef_add_field(self->descriptor, "repeated",
+ name, type, number, type_class);
+}
+
+// -----------------------------------------------------------------------------
+// EnumBuilderContext.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(EnumBuilderContext,
+ "Google::Protobuf::Internal::EnumBuilderContext");
+
+void EnumBuilderContext_mark(void* _self) {
+ EnumBuilderContext* self = _self;
+ rb_gc_mark(self->enumdesc);
+}
+
+void EnumBuilderContext_free(void* _self) {
+ EnumBuilderContext* self = _self;
+ xfree(self);
+}
+
+VALUE EnumBuilderContext_alloc(VALUE klass) {
+ EnumBuilderContext* self = ALLOC(EnumBuilderContext);
+ VALUE ret = TypedData_Wrap_Struct(
+ klass, &_EnumBuilderContext_type, self);
+ self->enumdesc = Qnil;
+ return ret;
+}
+
+void EnumBuilderContext_register(VALUE module) {
+ VALUE klass = rb_define_class_under(
+ module, "EnumBuilderContext", rb_cObject);
+ rb_define_alloc_func(klass, EnumBuilderContext_alloc);
+ rb_define_method(klass, "initialize",
+ EnumBuilderContext_initialize, 1);
+ rb_define_method(klass, "value", EnumBuilderContext_value, 2);
+ cEnumBuilderContext = klass;
+ rb_gc_register_address(&cEnumBuilderContext);
+}
+
+/*
+ * call-seq:
+ * EnumBuilderContext.new(enumdesc) => context
+ *
+ * Create a new builder context around the given enum descriptor. This class is
+ * intended to serve as a DSL context to be used with #instance_eval.
+ */
+VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
+ DEFINE_SELF(EnumBuilderContext, self, _self);
+ self->enumdesc = enumdef;
+ return Qnil;
+}
+
+static VALUE enumdef_add_value(VALUE enumdef,
+ VALUE name, VALUE number) {
+ rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * EnumBuilder.add_value(name, number)
+ *
+ * Adds the given name => number mapping to the enum type. Name must be a Ruby
+ * symbol.
+ */
+VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
+ DEFINE_SELF(EnumBuilderContext, self, _self);
+ return enumdef_add_value(self->enumdesc, name, number);
+}
+
+// -----------------------------------------------------------------------------
+// Builder.
+// -----------------------------------------------------------------------------
+
+DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
+
+void Builder_mark(void* _self) {
+ Builder* self = _self;
+ rb_gc_mark(self->pending_list);
+}
+
+void Builder_free(void* _self) {
+ Builder* self = _self;
+ xfree(self->defs);
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * Builder.new => builder
+ *
+ * Creates a new Builder. A Builder can accumulate a set of new message and enum
+ * descriptors and atomically register them into a pool in a way that allows for
+ * (co)recursive type references.
+ */
+VALUE Builder_alloc(VALUE klass) {
+ Builder* self = ALLOC(Builder);
+ VALUE ret = TypedData_Wrap_Struct(
+ klass, &_Builder_type, self);
+ self->pending_list = rb_ary_new();
+ self->defs = NULL;
+ 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, "add_message", Builder_add_message, 1);
+ rb_define_method(klass, "add_enum", Builder_add_enum, 1);
+ rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
+ cBuilder = klass;
+ rb_gc_register_address(&cBuilder);
+}
+
+/*
+ * 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.
+ *
+ * This is the recommended, idiomatic way to build message definitions.
+ */
+VALUE Builder_add_message(VALUE _self, VALUE name) {
+ DEFINE_SELF(Builder, self, _self);
+ VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor);
+ VALUE ctx = rb_class_new_instance(1, &msgdef, 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:
+ * 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.
+ *
+ * This is the recommended, idiomatic way to build enum definitions.
+ */
+VALUE Builder_add_enum(VALUE _self, VALUE name) {
+ DEFINE_SELF(Builder, self, _self);
+ VALUE enumdef = rb_class_new_instance(0, NULL, 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;
+}
+
+static void validate_msgdef(const upb_msgdef* msgdef) {
+ // Verify that no required fields exist. proto3 does not support these.
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, msgdef); !upb_msg_done(&it); upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
+ rb_raise(rb_eTypeError, "Required fields are unsupported in proto3.");
+ }
+ }
+}
+
+static void 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);
+ if (lookup == NULL) {
+ rb_raise(rb_eTypeError,
+ "Enum definition does not contain a value for '0'.");
+ }
+}
+
+/*
+ * call-seq:
+ * Builder.finalize_to_pool(pool)
+ *
+ * Adds all accumulated message and enum descriptors created in this builder
+ * context to the given pool. The operation occurs atomically, and all
+ * descriptors can refer to each other (including in cycles). This is the only
+ * way to build (co)recursive message definitions.
+ *
+ * This method is usually called automatically by DescriptorPool#build after it
+ * invokes the given user block in the context of the builder. The user should
+ * not normally need to call this manually because a Builder is not normally
+ * created manually.
+ */
+VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
+ DEFINE_SELF(Builder, self, _self);
+
+ DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
+
+ REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
+
+ for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
+ 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]);
+ } 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]);
+ }
+ }
+
+ CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
+ RARRAY_LEN(self->pending_list), NULL, &status),
+ "Unable to add defs to DescriptorPool");
+
+ for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
+ VALUE def_rb = rb_ary_entry(self->pending_list, i);
+ add_def_obj(self->defs[i], def_rb);
+ }
+
+ self->pending_list = rb_ary_new();
+ return Qnil;
+}
diff --git a/ruby/ext/google/protobuf_c/encode_decode.c b/ruby/ext/google/protobuf_c/encode_decode.c
new file mode 100644
index 00000000..8aba3c9e
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/encode_decode.c
@@ -0,0 +1,755 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+// -----------------------------------------------------------------------------
+// Parsing.
+// -----------------------------------------------------------------------------
+
+#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, free);
+ return hd_ofs;
+}
+
+typedef struct {
+ size_t ofs;
+ 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,
+ const upb_fielddef* f) {
+ submsg_handlerdata_t *hd = ALLOC(submsg_handlerdata_t);
+ hd->ofs = ofs;
+ hd->md = upb_fielddef_msgsubdef(f);
+ upb_handlers_addcleanup(h, hd, free);
+ return hd;
+}
+
+// A handler that starts a repeated field. Gets the Repeated*Field instance for
+// this field (such an instance always exists even in an empty message).
+static void *startseq_handler(void* closure, const void* hd) {
+ MessageHeader* msg = closure;
+ const size_t *ofs = hd;
+ return (void*)DEREF(Message_data(msg), *ofs, VALUE);
+}
+
+// Handlers that append primitive values to a repeated field (a regular Ruby
+// array for now).
+#define DEFINE_APPEND_HANDLER(type, ctype) \
+ static bool append##type##_handler(void *closure, const void *hd, \
+ ctype val) { \
+ VALUE ary = (VALUE)closure; \
+ RepeatedField_push_native(ary, &val); \
+ return true; \
+ }
+
+DEFINE_APPEND_HANDLER(bool, bool)
+DEFINE_APPEND_HANDLER(int32, int32_t)
+DEFINE_APPEND_HANDLER(uint32, uint32_t)
+DEFINE_APPEND_HANDLER(float, float)
+DEFINE_APPEND_HANDLER(int64, int64_t)
+DEFINE_APPEND_HANDLER(uint64, uint64_t)
+DEFINE_APPEND_HANDLER(double, double)
+
+// Appends a string to a repeated field (a regular Ruby array for now).
+static void* appendstr_handler(void *closure,
+ const void *hd,
+ size_t size_hint) {
+ VALUE ary = (VALUE)closure;
+ VALUE str = rb_str_new2("");
+ rb_enc_associate(str, kRubyStringUtf8Encoding);
+ RepeatedField_push(ary, str);
+ return (void*)str;
+}
+
+// Appends a 'bytes' string to a repeated field (a regular Ruby array for now).
+static void* appendbytes_handler(void *closure,
+ const void *hd,
+ size_t size_hint) {
+ VALUE ary = (VALUE)closure;
+ VALUE str = rb_str_new2("");
+ rb_enc_associate(str, kRubyString8bitEncoding);
+ RepeatedField_push(ary, str);
+ return (void*)str;
+}
+
+// Sets a non-repeated string 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;
+ VALUE str = rb_str_new2("");
+ rb_enc_associate(str, kRubyStringUtf8Encoding);
+ DEREF(Message_data(msg), *ofs, VALUE) = str;
+ return (void*)str;
+}
+
+// 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;
+ const size_t *ofs = hd;
+ VALUE str = rb_str_new2("");
+ rb_enc_associate(str, kRubyString8bitEncoding);
+ DEREF(Message_data(msg), *ofs, VALUE) = str;
+ return (void*)str;
+}
+
+static size_t stringdata_handler(void* closure, const void* hd,
+ const char* str, size_t len,
+ const upb_bufhandle* handle) {
+ VALUE rb_str = (VALUE)closure;
+ rb_str_cat(rb_str, str, len);
+ return len;
+}
+
+// Appends a submessage to a repeated field (a regular Ruby array for now).
+static void *appendsubmsg_handler(void *closure, const void *hd) {
+ VALUE ary = (VALUE)closure;
+ const submsg_handlerdata_t *submsgdata = hd;
+ VALUE subdesc =
+ get_def_obj((void*)submsgdata->md);
+ VALUE subklass = Descriptor_msgclass(subdesc);
+
+ VALUE submsg_rb = rb_class_new_instance(0, NULL, subklass);
+ RepeatedField_push(ary, submsg_rb);
+
+ MessageHeader* submsg;
+ TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
+ return submsg;
+}
+
+// Sets a non-repeated submessage field in a message.
+static void *submsg_handler(void *closure, const void *hd) {
+ MessageHeader* msg = closure;
+ const submsg_handlerdata_t* submsgdata = hd;
+ VALUE subdesc =
+ get_def_obj((void*)submsgdata->md);
+ VALUE subklass = Descriptor_msgclass(subdesc);
+
+ if (DEREF(Message_data(msg), submsgdata->ofs, VALUE) == Qnil) {
+ DEREF(Message_data(msg), submsgdata->ofs, VALUE) =
+ rb_class_new_instance(0, NULL, subklass);
+ }
+
+ VALUE submsg_rb = DEREF(Message_data(msg), submsgdata->ofs, VALUE);
+ MessageHeader* submsg;
+ TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
+ return submsg;
+}
+
+static void add_handlers_for_message(const void *closure, upb_handlers *h) {
+ Descriptor* desc = ruby_to_Descriptor(
+ get_def_obj((void*)upb_handlers_msgdef(h)));
+ // Ensure layout exists. We may be invoked to create handlers for a given
+ // message if we are included as a submsg of another message type before our
+ // class is actually built, so to work around this, we just create the layout
+ // (and handlers, in the class-building function) on-demand.
+ if (desc->layout == NULL) {
+ desc->layout = create_layout(desc->msgdef);
+ }
+
+ upb_msg_iter i;
+
+ for (upb_msg_begin(&i, desc->msgdef);
+ !upb_msg_done(&i);
+ upb_msg_next(&i)) {
+ const upb_fielddef *f = upb_msg_iter_field(&i);
+ size_t offset = desc->layout->offsets[upb_fielddef_index(f)];
+
+ if (upb_fielddef_isseq(f)) {
+ upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset));
+ upb_handlers_setstartseq(h, f, startseq_handler, &attr);
+ upb_handlerattr_uninit(&attr);
+
+ switch (upb_fielddef_type(f)) {
+
+#define SET_HANDLER(utype, ltype) \
+ case utype: \
+ upb_handlers_set##ltype(h, f, append##ltype##_handler, NULL); \
+ break;
+
+ SET_HANDLER(UPB_TYPE_BOOL, bool);
+ SET_HANDLER(UPB_TYPE_INT32, int32);
+ SET_HANDLER(UPB_TYPE_UINT32, uint32);
+ SET_HANDLER(UPB_TYPE_ENUM, int32);
+ SET_HANDLER(UPB_TYPE_FLOAT, float);
+ SET_HANDLER(UPB_TYPE_INT64, int64);
+ SET_HANDLER(UPB_TYPE_UINT64, uint64);
+ SET_HANDLER(UPB_TYPE_DOUBLE, double);
+
+#undef SET_HANDLER
+
+ 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);
+ upb_handlers_setstring(h, f, stringdata_handler, NULL);
+ }
+ case UPB_TYPE_MESSAGE: {
+ upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, f));
+ upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr);
+ upb_handlerattr_uninit(&attr);
+ break;
+ }
+ }
+ }
+
+ switch (upb_fielddef_type(f)) {
+ case UPB_TYPE_BOOL:
+ case UPB_TYPE_INT32:
+ case UPB_TYPE_UINT32:
+ case UPB_TYPE_ENUM:
+ case UPB_TYPE_FLOAT:
+ case UPB_TYPE_INT64:
+ case UPB_TYPE_UINT64:
+ case UPB_TYPE_DOUBLE:
+ // The shim writes directly at the given offset (instead of using
+ // DEREF()) so we need to add the msg overhead.
+ upb_shim_set(h, f, offset + sizeof(MessageHeader), -1);
+ 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_handlers_setstartstr(h, f,
+ is_bytes ? bytes_handler : str_handler,
+ &attr);
+ upb_handlers_setstring(h, f, stringdata_handler, &attr);
+ upb_handlerattr_uninit(&attr);
+ break;
+ }
+ case UPB_TYPE_MESSAGE: {
+ upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, offset, f));
+ upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr);
+ upb_handlerattr_uninit(&attr);
+ break;
+ }
+ }
+ }
+}
+
+// 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;
+}
+
+// Constructs the upb decoder method for parsing messages of this type.
+// This is called from the message class creation code.
+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);
+
+ const upb_pbdecodermethod *ret = upb_pbdecodermethod_new(&opts, owner);
+ return ret;
+}
+
+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;
+}
+
+/*
+ * call-seq:
+ * MessageClass.decode(data) => 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.
+ */
+VALUE Message_decode(VALUE klass, VALUE data) {
+ VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
+ Descriptor* desc = ruby_to_Descriptor(descriptor);
+ VALUE msgklass = Descriptor_msgclass(descriptor);
+
+ if (TYPE(data) != T_STRING) {
+ rb_raise(rb_eArgError, "Expected string for binary protobuf data.");
+ }
+
+ VALUE msg_rb = rb_class_new_instance(0, NULL, msgklass);
+ MessageHeader* msg;
+ TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
+
+ const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
+ const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
+ upb_pbdecoder decoder;
+ upb_sink sink;
+ upb_status status = UPB_STATUS_INIT;
+
+ upb_pbdecoder_init(&decoder, method, &status);
+ upb_sink_reset(&sink, h, msg);
+ upb_pbdecoder_resetoutput(&decoder, &sink);
+ upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
+ upb_pbdecoder_input(&decoder));
+
+ upb_pbdecoder_uninit(&decoder);
+ if (!upb_ok(&status)) {
+ rb_raise(rb_eRuntimeError, "Error occurred during parsing: %s.",
+ upb_status_errmsg(&status));
+ }
+
+ return msg_rb;
+}
+
+/*
+ * call-seq:
+ * MessageClass.decode_json(data) => 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.
+ */
+VALUE Message_decode_json(VALUE klass, VALUE data) {
+ VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
+ Descriptor* desc = ruby_to_Descriptor(descriptor);
+ VALUE msgklass = Descriptor_msgclass(descriptor);
+
+ if (TYPE(data) != T_STRING) {
+ rb_raise(rb_eArgError, "Expected string for JSON data.");
+ }
+ // TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
+ // convert, because string handlers pass data directly to message string
+ // fields.
+
+ VALUE msg_rb = rb_class_new_instance(0, NULL, msgklass);
+ MessageHeader* msg;
+ TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
+
+ upb_status status = UPB_STATUS_INIT;
+ upb_json_parser parser;
+ upb_json_parser_init(&parser, &status);
+
+ upb_sink sink;
+ upb_sink_reset(&sink, get_fill_handlers(desc), msg);
+ upb_json_parser_resetoutput(&parser, &sink);
+ upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
+ upb_json_parser_input(&parser));
+
+ upb_json_parser_uninit(&parser);
+ if (!upb_ok(&status)) {
+ rb_raise(rb_eRuntimeError, "Error occurred during parsing: %s.",
+ upb_status_errmsg(&status));
+ }
+
+ return msg_rb;
+}
+
+// -----------------------------------------------------------------------------
+// Serializing.
+// -----------------------------------------------------------------------------
+//
+// The code below also comes from upb's prototype Ruby binding, developed by
+// haberman@.
+
+/* stringsink *****************************************************************/
+
+// This should probably be factored into a common upb component.
+
+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) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+
+ stringsink *sink = _sink;
+ size_t new_size = sink->size;
+
+ 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 stringsink_init(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 stringsink_uninit(stringsink *sink) {
+ free(sink->ptr);
+}
+
+/* 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.
+//
+// Likewise, when implementing JSON serialization, we may need to have a
+// 'verbose' mode that outputs all fields and a 'concise' mode that outputs only
+// those with non-default values.
+
+static void putmsg(VALUE msg, const Descriptor* desc,
+ upb_sink *sink, int depth);
+
+static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
+ upb_selector_t ret;
+ bool ok = upb_handlers_getselector(f, type, &ret);
+ UPB_ASSERT_VAR(ok, ok);
+ return ret;
+}
+
+static void putstr(VALUE str, const upb_fielddef *f, upb_sink *sink) {
+ if (str == Qnil) return;
+
+ assert(BUILTIN_TYPE(str) == RUBY_T_STRING);
+ upb_sink subsink;
+
+ // 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.
+ native_slot_validate_string_encoding(upb_fielddef_type(f), str);
+
+ upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), RSTRING_LEN(str),
+ &subsink);
+ upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), RSTRING_PTR(str),
+ RSTRING_LEN(str), NULL);
+ upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
+}
+
+static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink,
+ int depth) {
+ if (submsg == Qnil) return;
+
+ upb_sink subsink;
+ VALUE descriptor = rb_iv_get(submsg, kDescriptorInstanceVar);
+ Descriptor* subdesc = ruby_to_Descriptor(descriptor);
+
+ upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
+ putmsg(submsg, subdesc, &subsink, depth + 1);
+ upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG));
+}
+
+static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink,
+ int depth) {
+ if (ary == Qnil) return;
+
+ upb_sink subsink;
+
+ upb_sink_startseq(sink, getsel(f, UPB_HANDLER_STARTSEQ), &subsink);
+
+ upb_fieldtype_t type = upb_fielddef_type(f);
+ upb_selector_t sel = 0;
+ if (upb_fielddef_isprimitive(f)) {
+ sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
+ }
+
+ int size = NUM2INT(RepeatedField_length(ary));
+ for (int i = 0; i < size; i++) {
+ void* memory = RepeatedField_index_native(ary, i);
+ switch (type) {
+#define T(upbtypeconst, upbtype, ctype) \
+ case upbtypeconst: \
+ upb_sink_put##upbtype(&subsink, sel, *((ctype *)memory)); \
+ break;
+
+ T(UPB_TYPE_FLOAT, float, float)
+ T(UPB_TYPE_DOUBLE, double, double)
+ T(UPB_TYPE_BOOL, bool, int8_t)
+ case UPB_TYPE_ENUM:
+ T(UPB_TYPE_INT32, int32, int32_t)
+ T(UPB_TYPE_UINT32, uint32, uint32_t)
+ T(UPB_TYPE_INT64, int64, int64_t)
+ T(UPB_TYPE_UINT64, uint64, uint64_t)
+
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ putstr(*((VALUE *)memory), f, &subsink);
+ break;
+ case UPB_TYPE_MESSAGE:
+ putsubmsg(*((VALUE *)memory), f, &subsink, depth);
+ break;
+
+#undef T
+
+ }
+ }
+ upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
+}
+
+static void putmsg(VALUE msg_rb, const Descriptor* desc,
+ upb_sink *sink, int depth) {
+ upb_sink_startmsg(sink);
+
+ // Protect against cycles (possible because users may freely reassign message
+ // and repeated fields) by imposing a maximum recursion depth.
+ if (depth > UPB_SINK_MAX_NESTING) {
+ rb_raise(rb_eRuntimeError,
+ "Maximum recursion depth exceeded during encoding.");
+ }
+
+ MessageHeader* msg;
+ TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
+ void* msg_data = Message_data(msg);
+
+ upb_msg_iter i;
+ for (upb_msg_begin(&i, desc->msgdef);
+ !upb_msg_done(&i);
+ upb_msg_next(&i)) {
+ upb_fielddef *f = upb_msg_iter_field(&i);
+ uint32_t offset = desc->layout->offsets[upb_fielddef_index(f)];
+
+ if (upb_fielddef_isseq(f)) {
+ VALUE ary = DEREF(msg_data, offset, VALUE);
+ if (ary != Qnil) {
+ putary(ary, f, sink, depth);
+ }
+ } else if (upb_fielddef_isstring(f)) {
+ VALUE str = DEREF(msg_data, offset, VALUE);
+ if (RSTRING_LEN(str) > 0) {
+ putstr(str, f, sink);
+ }
+ } else if (upb_fielddef_issubmsg(f)) {
+ putsubmsg(DEREF(msg_data, offset, VALUE), f, sink, depth);
+ } else {
+ upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
+
+#define T(upbtypeconst, upbtype, ctype, default_value) \
+ case upbtypeconst: { \
+ ctype value = DEREF(msg_data, offset, ctype); \
+ if (value != default_value) { \
+ upb_sink_put##upbtype(sink, sel, value); \
+ } \
+ } \
+ break;
+
+ switch (upb_fielddef_type(f)) {
+ T(UPB_TYPE_FLOAT, float, float, 0.0)
+ T(UPB_TYPE_DOUBLE, double, double, 0.0)
+ T(UPB_TYPE_BOOL, bool, uint8_t, 0)
+ case UPB_TYPE_ENUM:
+ T(UPB_TYPE_INT32, int32, int32_t, 0)
+ T(UPB_TYPE_UINT32, uint32, uint32_t, 0)
+ T(UPB_TYPE_INT64, int64, int64_t, 0)
+ T(UPB_TYPE_UINT64, uint64, uint64_t, 0)
+
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ case UPB_TYPE_MESSAGE: rb_raise(rb_eRuntimeError, "Internal error.");
+ }
+
+#undef T
+
+ }
+ }
+
+ upb_status status;
+ upb_sink_endmsg(sink, &status);
+}
+
+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;
+}
+
+static const upb_handlers* msgdef_json_serialize_handlers(Descriptor* desc) {
+ if (desc->json_serialize_handlers == NULL) {
+ desc->json_serialize_handlers =
+ upb_json_printer_newhandlers(
+ desc->msgdef, &desc->json_serialize_handlers);
+ }
+ return desc->json_serialize_handlers;
+}
+
+/*
+ * call-seq:
+ * MessageClass.encode(msg) => bytes
+ *
+ * Encodes the given message object to its serialized form in protocol buffers
+ * wire format.
+ */
+VALUE Message_encode(VALUE klass, VALUE msg_rb) {
+ VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
+ Descriptor* desc = ruby_to_Descriptor(descriptor);
+
+ stringsink sink;
+ stringsink_init(&sink);
+
+ const upb_handlers* serialize_handlers =
+ msgdef_pb_serialize_handlers(desc);
+
+ upb_pb_encoder encoder;
+ upb_pb_encoder_init(&encoder, serialize_handlers);
+ upb_pb_encoder_resetoutput(&encoder, &sink.sink);
+
+ putmsg(msg_rb, desc, upb_pb_encoder_input(&encoder), 0);
+
+ VALUE ret = rb_str_new(sink.ptr, sink.len);
+
+ upb_pb_encoder_uninit(&encoder);
+ stringsink_uninit(&sink);
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * MessageClass.encode_json(msg) => json_string
+ *
+ * Encodes the given message object into its serialized JSON representation.
+ */
+VALUE Message_encode_json(VALUE klass, VALUE msg_rb) {
+ VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
+ Descriptor* desc = ruby_to_Descriptor(descriptor);
+
+ stringsink sink;
+ stringsink_init(&sink);
+
+ const upb_handlers* serialize_handlers =
+ msgdef_json_serialize_handlers(desc);
+
+ upb_json_printer printer;
+ upb_json_printer_init(&printer, serialize_handlers);
+ upb_json_printer_resetoutput(&printer, &sink.sink);
+
+ putmsg(msg_rb, desc, upb_json_printer_input(&printer), 0);
+
+ VALUE ret = rb_str_new(sink.ptr, sink.len);
+
+ upb_json_printer_uninit(&printer);
+ stringsink_uninit(&sink);
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * Google::Protobuf.encode(msg) => bytes
+ *
+ * Encodes the given message object to protocol buffers wire format. This is an
+ * alternative to the #encode method on msg's class.
+ */
+VALUE Google_Protobuf_encode(VALUE self, VALUE msg_rb) {
+ VALUE klass = CLASS_OF(msg_rb);
+ return Message_encode(klass, msg_rb);
+}
+
+/*
+ * call-seq:
+ * Google::Protobuf.encode_json(msg) => json_string
+ *
+ * Encodes the given message object to its JSON representation. This is an
+ * alternative to the #encode_json method on msg's class.
+ */
+VALUE Google_Protobuf_encode_json(VALUE self, VALUE msg_rb) {
+ VALUE klass = CLASS_OF(msg_rb);
+ return Message_encode_json(klass, msg_rb);
+}
+
+/*
+ * call-seq:
+ * Google::Protobuf.decode(class, bytes) => msg
+ *
+ * Decodes the given bytes as protocol buffers wire format under the
+ * interpretation given by the given class's message definition. This is an
+ * alternative to the #decode method on the given class.
+ */
+VALUE Google_Protobuf_decode(VALUE self, VALUE klass, VALUE msg_rb) {
+ return Message_decode(klass, msg_rb);
+}
+
+/*
+ * call-seq:
+ * Google::Protobuf.decode_json(class, json_string) => msg
+ *
+ * Decodes the given JSON string under the interpretation given by the given
+ * class's message definition. This is an alternative to the #decode_json method
+ * on the given class.
+ */
+VALUE Google_Protobuf_decode_json(VALUE self, VALUE klass, VALUE msg_rb) {
+ return Message_decode_json(klass, msg_rb);
+}
diff --git a/ruby/ext/google/protobuf_c/extconf.rb b/ruby/ext/google/protobuf_c/extconf.rb
new file mode 100644
index 00000000..7cf7bf6a
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/extconf.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/ruby
+
+require 'mkmf'
+
+$CFLAGS += " -O3 -std=c99 -Wno-unused-function -DNDEBUG "
+
+$objs = ["protobuf.o", "defs.o", "storage.o", "message.o",
+ "repeated_field.o", "encode_decode.o", "upb.o"]
+
+create_makefile("google/protobuf_c")
diff --git a/ruby/ext/google/protobuf_c/message.c b/ruby/ext/google/protobuf_c/message.c
new file mode 100644
index 00000000..105b7807
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/message.c
@@ -0,0 +1,463 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+// -----------------------------------------------------------------------------
+// Class/module creation from msgdefs and enumdefs, respectively.
+// -----------------------------------------------------------------------------
+
+void* Message_data(void* msg) {
+ return ((uint8_t *)msg) + sizeof(MessageHeader);
+}
+
+void Message_mark(void* _self) {
+ MessageHeader* self = (MessageHeader *)_self;
+ layout_mark(self->descriptor->layout, Message_data(self));
+}
+
+void Message_free(void* self) {
+ xfree(self);
+}
+
+rb_data_type_t Message_type = {
+ "Message",
+ { Message_mark, Message_free, NULL },
+};
+
+VALUE Message_alloc(VALUE klass) {
+ VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
+ Descriptor* desc = ruby_to_Descriptor(descriptor);
+ MessageHeader* msg = (MessageHeader*)ALLOC_N(
+ uint8_t, sizeof(MessageHeader) + desc->layout->size);
+ memset(Message_data(msg), 0, desc->layout->size);
+
+ // We wrap first so that everything in the message object is GC-rooted in case
+ // a collection happens during object creation in layout_init().
+ VALUE ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
+ msg->descriptor = desc;
+ rb_iv_set(ret, kDescriptorInstanceVar, descriptor);
+
+ layout_init(desc->layout, Message_data(msg));
+
+ return ret;
+}
+
+/*
+ * 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
+ * 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:
+ *
+ * msg.foo = 42
+ * puts msg.foo
+ */
+VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+ if (argc < 1) {
+ rb_raise(rb_eArgError, "Expected method name as first argument.");
+ }
+ VALUE method_name = argv[0];
+ if (!SYMBOL_P(method_name)) {
+ rb_raise(rb_eArgError, "Expected symbol as method name.");
+ }
+ VALUE method_str = rb_id2str(SYM2ID(method_name));
+ char* name = RSTRING_PTR(method_str);
+ size_t name_len = RSTRING_LEN(method_str);
+ bool setter = false;
+
+ // Setters have names that end in '='.
+ if (name[name_len - 1] == '=') {
+ setter = true;
+ name_len--;
+ }
+
+ const upb_fielddef* f = upb_msgdef_ntof(self->descriptor->msgdef,
+ name, name_len);
+
+ if (f == NULL) {
+ rb_raise(rb_eArgError, "Unknown field");
+ }
+
+ if (setter) {
+ if (argc < 2) {
+ rb_raise(rb_eArgError, "No value provided to setter.");
+ }
+ layout_set(self->descriptor->layout, Message_data(self), f, argv[1]);
+ return Qnil;
+ } else {
+ return layout_get(self->descriptor->layout, Message_data(self), f);
+ }
+}
+
+int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ if (!SYMBOL_P(key)) {
+ rb_raise(rb_eArgError,
+ "Expected symbols as hash keys in initialization map.");
+ }
+
+ VALUE method_str = rb_id2str(SYM2ID(key));
+ char* name = RSTRING_PTR(method_str);
+ const upb_fielddef* f = upb_msgdef_ntofz(self->descriptor->msgdef, name);
+ if (f == NULL) {
+ rb_raise(rb_eArgError,
+ "Unknown field name in initialization map entry.");
+ }
+
+ if (upb_fielddef_label(f) == UPB_LABEL_REPEATED) {
+ if (TYPE(val) != T_ARRAY) {
+ rb_raise(rb_eArgError,
+ "Expected array as initializer value for repeated field.");
+ }
+ VALUE ary = layout_get(self->descriptor->layout, Message_data(self), f);
+ for (int i = 0; i < RARRAY_LEN(val); i++) {
+ RepeatedField_push(ary, rb_ary_entry(val, i));
+ }
+ } else {
+ layout_set(self->descriptor->layout, Message_data(self), f, val);
+ }
+ return 0;
+}
+
+/*
+ * call-seq:
+ * Message.new(kwargs) => new_message
+ *
+ * Creates a new instance of the given message class. Keyword arguments may be
+ * provided with keywords corresponding to field names.
+ *
+ * Note that no literal Message class exists. Only concrete classes per message
+ * type exist, as provided by the #msgclass method on Descriptors after they
+ * have been added to a pool. The method definitions described here on the
+ * Message class are provided on each concrete message class.
+ */
+VALUE Message_initialize(int argc, VALUE* argv, VALUE _self) {
+ if (argc == 0) {
+ return Qnil;
+ }
+ if (argc != 1) {
+ rb_raise(rb_eArgError, "Expected 0 or 1 arguments.");
+ }
+ VALUE hash_args = argv[0];
+ if (TYPE(hash_args) != T_HASH) {
+ rb_raise(rb_eArgError, "Expected hash arguments.");
+ }
+
+ rb_hash_foreach(hash_args, Message_initialize_kwarg, _self);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Message.dup => new_message
+ *
+ * Performs a shallow copy of this message and returns the new copy.
+ */
+VALUE Message_dup(VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
+ MessageHeader* new_msg_self;
+ TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
+
+ layout_dup(self->descriptor->layout,
+ Message_data(new_msg_self),
+ Message_data(self));
+
+ return new_msg;
+}
+
+// Internal only; used by Google::Protobuf.deep_copy.
+VALUE Message_deep_copy(VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ VALUE new_msg = rb_class_new_instance(0, NULL, CLASS_OF(_self));
+ MessageHeader* new_msg_self;
+ TypedData_Get_Struct(new_msg, MessageHeader, &Message_type, new_msg_self);
+
+ layout_deep_copy(self->descriptor->layout,
+ Message_data(new_msg_self),
+ Message_data(self));
+
+ return new_msg;
+}
+
+/*
+ * call-seq:
+ * Message.==(other) => boolean
+ *
+ * Performs a deep comparison of this message with another. Messages are equal
+ * if they have the same type and if each field is equal according to the :==
+ * method's semantics (a more efficient comparison may actually be done if the
+ * field is of a primitive type).
+ */
+VALUE Message_eq(VALUE _self, VALUE _other) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ MessageHeader* other;
+ TypedData_Get_Struct(_other, MessageHeader, &Message_type, other);
+
+ if (self->descriptor != other->descriptor) {
+ return Qfalse;
+ }
+
+ return layout_eq(self->descriptor->layout,
+ Message_data(self),
+ Message_data(other));
+}
+
+/*
+ * call-seq:
+ * Message.hash => hash_value
+ *
+ * Returns a hash value that represents this message's field values.
+ */
+VALUE Message_hash(VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ return layout_hash(self->descriptor->layout, Message_data(self));
+}
+
+/*
+ * call-seq:
+ * Message.inspect => string
+ *
+ * Returns a human-readable string representing this message. It will be
+ * formatted as "<MessageType: field1: value1, field2: value2, ...>". Each
+ * field's value is represented according to its own #inspect method.
+ */
+VALUE Message_inspect(VALUE _self) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+
+ VALUE str = rb_str_new2("<");
+ str = rb_str_append(str, rb_str_new2(rb_class2name(CLASS_OF(_self))));
+ str = rb_str_cat2(str, ": ");
+ str = rb_str_append(str, layout_inspect(
+ self->descriptor->layout, Message_data(self)));
+ str = rb_str_cat2(str, ">");
+ return str;
+}
+
+/*
+ * call-seq:
+ * Message.[](index) => value
+ *
+ * Accesses a field's value by field name. The provided field name should be a
+ * string.
+ */
+VALUE Message_index(VALUE _self, VALUE field_name) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+ Check_Type(field_name, T_STRING);
+ const upb_fielddef* field =
+ upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
+ if (field == NULL) {
+ return Qnil;
+ }
+ return layout_get(self->descriptor->layout, Message_data(self), field);
+}
+
+/*
+ * call-seq:
+ * Message.[]=(index, value)
+ *
+ * Sets a field's value by field name. The provided field name should be a
+ * string.
+ */
+VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
+ MessageHeader* self;
+ TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
+ Check_Type(field_name, T_STRING);
+ const upb_fielddef* field =
+ upb_msgdef_ntofz(self->descriptor->msgdef, RSTRING_PTR(field_name));
+ if (field == NULL) {
+ rb_raise(rb_eArgError, "Unknown field: %s", RSTRING_PTR(field_name));
+ }
+ layout_set(self->descriptor->layout, Message_data(self), field, value);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * Message.descriptor => descriptor
+ *
+ * Class method that returns the Descriptor instance corresponding to this
+ * message class's type.
+ */
+VALUE Message_descriptor(VALUE klass) {
+ return rb_iv_get(klass, kDescriptorInstanceVar);
+}
+
+VALUE build_class_from_descriptor(Descriptor* desc) {
+ if (desc->layout == NULL) {
+ desc->layout = create_layout(desc->msgdef);
+ }
+ if (desc->fill_method == NULL) {
+ desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
+ }
+
+ const char* name = upb_msgdef_fullname(desc->msgdef);
+ if (name == NULL) {
+ rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
+ }
+
+ VALUE klass = rb_define_class_id(
+ // Docs say this parameter is ignored. User will assign return value to
+ // their own toplevel constant class name.
+ rb_intern("Message"),
+ rb_cObject);
+ rb_iv_set(klass, kDescriptorInstanceVar, get_def_obj(desc->msgdef));
+ rb_define_alloc_func(klass, Message_alloc);
+ rb_define_method(klass, "method_missing",
+ Message_method_missing, -1);
+ rb_define_method(klass, "initialize", Message_initialize, -1);
+ rb_define_method(klass, "dup", Message_dup, 0);
+ // 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, "hash", Message_hash, 0);
+ rb_define_method(klass, "inspect", 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, "encode_json", Message_encode_json, 1);
+ rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
+ return klass;
+}
+
+/*
+ * call-seq:
+ * Enum.lookup(number) => name
+ *
+ * This module method, provided on each generated enum module, looks up an enum
+ * value by number and returns its name as a Ruby symbol, or nil if not found.
+ */
+VALUE enum_lookup(VALUE self, VALUE number) {
+ int32_t num = NUM2INT(number);
+ VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
+ EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
+
+ const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
+ if (name == NULL) {
+ return Qnil;
+ } else {
+ return ID2SYM(rb_intern(name));
+ }
+}
+
+/*
+ * call-seq:
+ * Enum.resolve(name) => number
+ *
+ * This module method, provided on each generated enum module, looks up an enum
+ * value by name (as a Ruby symbol) and returns its name, or nil if not found.
+ */
+VALUE enum_resolve(VALUE self, VALUE sym) {
+ const char* name = rb_id2name(SYM2ID(sym));
+ VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
+ EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
+
+ int32_t num = 0;
+ bool found = upb_enumdef_ntoiz(enumdesc->enumdef, name, &num);
+ if (!found) {
+ return Qnil;
+ } else {
+ return INT2NUM(num);
+ }
+}
+
+/*
+ * call-seq:
+ * Enum.descriptor
+ *
+ * This module method, provided on each generated enum module, returns the
+ * EnumDescriptor corresponding to this enum type.
+ */
+VALUE enum_descriptor(VALUE self) {
+ return rb_iv_get(self, kDescriptorInstanceVar);
+}
+
+VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
+ VALUE mod = rb_define_module_id(
+ rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
+
+ upb_enum_iter it;
+ for (upb_enum_begin(&it, enumdesc->enumdef);
+ !upb_enum_done(&it);
+ upb_enum_next(&it)) {
+ 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(rb_eTypeError,
+ "Enum value '%s' does not start with an uppercase letter "
+ "as is required for Ruby constants.",
+ name);
+ }
+ rb_define_const(mod, name, INT2NUM(value));
+ }
+
+ rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
+ rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
+ rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
+ rb_iv_set(mod, kDescriptorInstanceVar, get_def_obj(enumdesc->enumdef));
+
+ return mod;
+}
+
+/*
+ * call-seq:
+ * Google::Protobuf.deep_copy(obj) => copy_of_obj
+ *
+ * Performs a deep copy of either a RepeatedField instance or a message object,
+ * recursively copying its members.
+ */
+VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
+ VALUE klass = CLASS_OF(obj);
+ if (klass == cRepeatedField) {
+ return RepeatedField_deep_copy(obj);
+ } else {
+ return Message_deep_copy(obj);
+ }
+}
diff --git a/ruby/ext/google/protobuf_c/protobuf.c b/ruby/ext/google/protobuf_c/protobuf.c
new file mode 100644
index 00000000..d5862284
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/protobuf.c
@@ -0,0 +1,102 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+// -----------------------------------------------------------------------------
+// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
+// instances.
+// -----------------------------------------------------------------------------
+
+// This is a hash table from def objects (encoded by converting pointers to
+// Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
+VALUE upb_def_to_ruby_obj_map;
+
+void add_def_obj(const void* def, VALUE value) {
+ rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
+}
+
+VALUE get_def_obj(const void* def) {
+ return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
+}
+
+// -----------------------------------------------------------------------------
+// Utilities.
+// -----------------------------------------------------------------------------
+
+// Raises a Ruby error if |status| is not OK, using its error message.
+void check_upb_status(const upb_status* status, const char* msg) {
+ if (!upb_ok(status)) {
+ rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
+ }
+}
+
+// String encodings: we look these up once, at load time, and then cache them
+// here.
+rb_encoding* kRubyStringUtf8Encoding;
+rb_encoding* kRubyStringASCIIEncoding;
+rb_encoding* kRubyString8bitEncoding;
+
+// -----------------------------------------------------------------------------
+// Initialization/entry point.
+// -----------------------------------------------------------------------------
+
+// This must be named "Init_protobuf_c" because the Ruby module is named
+// "protobuf_c" -- the VM looks for this symbol in our .so.
+void Init_protobuf_c() {
+ VALUE google = rb_define_module("Google");
+ VALUE protobuf = rb_define_module_under(google, "Protobuf");
+ VALUE internal = rb_define_module_under(protobuf, "Internal");
+ DescriptorPool_register(protobuf);
+ Descriptor_register(protobuf);
+ FieldDescriptor_register(protobuf);
+ EnumDescriptor_register(protobuf);
+ MessageBuilderContext_register(internal);
+ EnumBuilderContext_register(internal);
+ Builder_register(internal);
+ RepeatedField_register(protobuf);
+
+ rb_define_singleton_method(protobuf, "encode", Google_Protobuf_encode, 1);
+ rb_define_singleton_method(protobuf, "decode", Google_Protobuf_decode, 2);
+ rb_define_singleton_method(protobuf, "encode_json",
+ Google_Protobuf_encode_json, 1);
+ rb_define_singleton_method(protobuf, "decode_json",
+ Google_Protobuf_decode_json, 2);
+
+ rb_define_singleton_method(protobuf, "deep_copy",
+ Google_Protobuf_deep_copy, 1);
+
+ kRubyStringUtf8Encoding = rb_utf8_encoding();
+ kRubyStringASCIIEncoding = rb_usascii_encoding();
+ kRubyString8bitEncoding = rb_ascii8bit_encoding();
+
+ upb_def_to_ruby_obj_map = rb_hash_new();
+ rb_gc_register_address(&upb_def_to_ruby_obj_map);
+}
diff --git a/ruby/ext/google/protobuf_c/protobuf.h b/ruby/ext/google/protobuf_c/protobuf.h
new file mode 100644
index 00000000..c3a5d653
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/protobuf.h
@@ -0,0 +1,396 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
+#define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
+
+#include <ruby/ruby.h>
+#include <ruby/vm.h>
+#include <ruby/encoding.h>
+
+#include "upb.h"
+
+// Forward decls.
+struct DescriptorPool;
+struct Descriptor;
+struct FieldDescriptor;
+struct EnumDescriptor;
+struct MessageLayout;
+struct MessageHeader;
+struct MessageBuilderContext;
+struct EnumBuilderContext;
+struct Builder;
+
+typedef struct DescriptorPool DescriptorPool;
+typedef struct Descriptor Descriptor;
+typedef struct FieldDescriptor FieldDescriptor;
+typedef struct EnumDescriptor EnumDescriptor;
+typedef struct MessageLayout MessageLayout;
+typedef struct MessageHeader MessageHeader;
+typedef struct MessageBuilderContext MessageBuilderContext;
+typedef struct EnumBuilderContext EnumBuilderContext;
+typedef struct Builder Builder;
+
+/*
+ It can be a bit confusing how the C structs defined below and the Ruby
+ objects interact and hold references to each other. First, a few principles:
+
+ - Ruby's "TypedData" abstraction lets a Ruby VALUE hold a pointer to a C
+ struct (or arbitrary memory chunk), own it, and free it when collected.
+ Thus, each struct below will have a corresponding Ruby object
+ wrapping/owning it.
+
+ - To get back from an underlying upb {msg,enum}def to the Ruby object, we
+ keep a global hashmap, accessed by get_def_obj/add_def_obj below.
+
+ The in-memory structure is then something like:
+
+ Ruby | upb
+ |
+ DescriptorPool ------------|-----------> upb_symtab____________________
+ | | (message types) \
+ | v \
+ Descriptor ---------------|-----------> upb_msgdef (enum types)|
+ |--> msgclass | | ^ |
+ | (dynamically built) | | | (submsg fields) |
+ |--> MessageLayout | | | /
+ |--------------------------|> decoder method| | /
+ \--------------------------|> serialize | | /
+ | handlers v | /
+ FieldDescriptor -----------|-----------> upb_fielddef /
+ | | /
+ | v (enum fields) /
+ EnumDescriptor ------------|-----------> upb_enumdef <----------'
+ |
+ |
+ ^ | \___/
+ `---------------|-----------------' (get_def_obj map)
+ */
+
+// -----------------------------------------------------------------------------
+// Ruby class structure definitions.
+// -----------------------------------------------------------------------------
+
+struct DescriptorPool {
+ upb_symtab* symtab;
+};
+
+struct Descriptor {
+ const upb_msgdef* msgdef;
+ MessageLayout* layout;
+ VALUE klass; // begins as nil
+ const upb_handlers* fill_handlers;
+ const upb_pbdecodermethod* fill_method;
+ const upb_handlers* pb_serialize_handlers;
+ const upb_handlers* json_serialize_handlers;
+};
+
+struct FieldDescriptor {
+ const upb_fielddef* fielddef;
+};
+
+struct EnumDescriptor {
+ const upb_enumdef* enumdef;
+ VALUE module; // begins as nil
+};
+
+struct MessageBuilderContext {
+ VALUE descriptor;
+};
+
+struct EnumBuilderContext {
+ VALUE enumdesc;
+};
+
+struct Builder {
+ VALUE pending_list;
+ upb_def** defs; // used only while finalizing
+};
+
+extern VALUE cDescriptorPool;
+extern VALUE cDescriptor;
+extern VALUE cFieldDescriptor;
+extern VALUE cEnumDescriptor;
+extern VALUE cMessageBuilderContext;
+extern VALUE cEnumBuilderContext;
+extern VALUE cBuilder;
+
+extern const char* kDescriptorInstanceVar;
+
+// We forward-declare all of the Ruby method implementations here because we
+// sometimes call the methods directly across .c files, rather than going
+// through Ruby's method dispatching (e.g. during message parse). It's cleaner
+// to keep the list of object methods together than to split them between
+// static-in-file definitions and header declarations.
+
+void DescriptorPool_mark(void* _self);
+void DescriptorPool_free(void* _self);
+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_lookup(VALUE _self, VALUE name);
+VALUE DescriptorPool_generated_pool(VALUE _self);
+
+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_name(VALUE _self);
+VALUE Descriptor_name_set(VALUE _self, VALUE str);
+VALUE Descriptor_each(VALUE _self);
+VALUE Descriptor_lookup(VALUE _self, VALUE name);
+VALUE Descriptor_add_field(VALUE _self, VALUE obj);
+VALUE Descriptor_msgclass(VALUE _self);
+extern const rb_data_type_t _Descriptor_type;
+
+void FieldDescriptor_mark(void* _self);
+void FieldDescriptor_free(void* _self);
+VALUE FieldDescriptor_alloc(VALUE klass);
+void FieldDescriptor_register(VALUE module);
+FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
+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_label(VALUE _self);
+VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
+VALUE FieldDescriptor_number(VALUE _self);
+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_get(VALUE _self, VALUE msg_rb);
+VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value);
+upb_fieldtype_t ruby_to_fieldtype(VALUE type);
+VALUE fieldtype_to_ruby(upb_fieldtype_t type);
+
+void EnumDescriptor_mark(void* _self);
+void EnumDescriptor_free(void* _self);
+VALUE EnumDescriptor_alloc(VALUE klass);
+void EnumDescriptor_register(VALUE module);
+EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
+VALUE EnumDescriptor_name(VALUE _self);
+VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
+VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
+VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
+VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
+VALUE EnumDescriptor_each(VALUE _self);
+VALUE EnumDescriptor_enummodule(VALUE _self);
+extern const rb_data_type_t _EnumDescriptor_type;
+
+void MessageBuilderContext_mark(void* _self);
+void MessageBuilderContext_free(void* _self);
+VALUE MessageBuilderContext_alloc(VALUE klass);
+void MessageBuilderContext_register(VALUE module);
+MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
+VALUE MessageBuilderContext_initialize(VALUE _self, VALUE descriptor);
+VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
+VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
+VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
+
+void EnumBuilderContext_mark(void* _self);
+void EnumBuilderContext_free(void* _self);
+VALUE EnumBuilderContext_alloc(VALUE klass);
+void EnumBuilderContext_register(VALUE module);
+EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
+VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
+VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
+
+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_add_message(VALUE _self, VALUE name);
+VALUE Builder_add_enum(VALUE _self, VALUE name);
+VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb);
+
+// -----------------------------------------------------------------------------
+// Native slot storage abstraction.
+// -----------------------------------------------------------------------------
+
+size_t native_slot_size(upb_fieldtype_t type);
+void native_slot_set(upb_fieldtype_t type,
+ VALUE type_class,
+ void* memory,
+ VALUE value);
+VALUE native_slot_get(upb_fieldtype_t type,
+ VALUE type_class,
+ void* memory);
+void native_slot_init(upb_fieldtype_t type, void* memory);
+void native_slot_mark(upb_fieldtype_t type, void* memory);
+void native_slot_dup(upb_fieldtype_t type, void* to, void* from);
+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);
+
+void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value);
+
+extern rb_encoding* kRubyStringUtf8Encoding;
+extern rb_encoding* kRubyStringASCIIEncoding;
+extern rb_encoding* kRubyString8bitEncoding;
+
+// -----------------------------------------------------------------------------
+// Repeated field container type.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+ upb_fieldtype_t field_type;
+ VALUE field_type_class;
+ void* elements;
+ int size;
+ int capacity;
+} RepeatedField;
+
+void RepeatedField_mark(void* self);
+void RepeatedField_free(void* self);
+VALUE RepeatedField_alloc(VALUE klass);
+VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self);
+void RepeatedField_register(VALUE module);
+
+extern const rb_data_type_t RepeatedField_type;
+extern VALUE cRepeatedField;
+
+RepeatedField* ruby_to_RepeatedField(VALUE value);
+
+void RepeatedField_register(VALUE module);
+VALUE RepeatedField_each(VALUE _self);
+VALUE RepeatedField_index(VALUE _self, VALUE _index);
+void* RepeatedField_index_native(VALUE _self, int index);
+VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
+void RepeatedField_reserve(RepeatedField* self, int new_size);
+VALUE RepeatedField_push(VALUE _self, VALUE val);
+void RepeatedField_push_native(VALUE _self, void* data);
+VALUE RepeatedField_pop(VALUE _self);
+VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
+VALUE RepeatedField_replace(VALUE _self, VALUE list);
+VALUE RepeatedField_clear(VALUE _self);
+VALUE RepeatedField_length(VALUE _self);
+VALUE RepeatedField_dup(VALUE _self);
+VALUE RepeatedField_deep_copy(VALUE _self);
+VALUE RepeatedField_eq(VALUE _self, VALUE _other);
+VALUE RepeatedField_hash(VALUE _self);
+VALUE RepeatedField_inspect(VALUE _self);
+VALUE RepeatedField_plus(VALUE _self, VALUE list);
+
+// -----------------------------------------------------------------------------
+// Message layout / storage.
+// -----------------------------------------------------------------------------
+
+struct MessageLayout {
+ const upb_msgdef* msgdef;
+ size_t* offsets;
+ size_t size;
+};
+
+MessageLayout* create_layout(const upb_msgdef* msgdef);
+void free_layout(MessageLayout* layout);
+VALUE layout_get(MessageLayout* layout,
+ void* storage,
+ const upb_fielddef* field);
+void layout_set(MessageLayout* layout,
+ void* storage,
+ const upb_fielddef* field,
+ VALUE val);
+void layout_init(MessageLayout* layout, void* storage);
+void layout_mark(MessageLayout* layout, void* storage);
+void layout_dup(MessageLayout* layout, void* to, void* from);
+void layout_deep_copy(MessageLayout* layout, void* to, void* from);
+VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2);
+VALUE layout_hash(MessageLayout* layout, void* storage);
+VALUE layout_inspect(MessageLayout* layout, void* storage);
+
+// -----------------------------------------------------------------------------
+// Message class creation.
+// -----------------------------------------------------------------------------
+
+struct MessageHeader {
+ Descriptor* descriptor; // kept alive by self.class.descriptor reference.
+ // Data comes after this.
+};
+
+extern rb_data_type_t Message_type;
+
+VALUE build_class_from_descriptor(Descriptor* descriptor);
+void* Message_data(void* msg);
+void Message_mark(void* self);
+void Message_free(void* self);
+VALUE Message_alloc(VALUE klass);
+VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self);
+VALUE Message_initialize(int argc, VALUE* argv, VALUE _self);
+VALUE Message_dup(VALUE _self);
+VALUE Message_deep_copy(VALUE _self);
+VALUE Message_eq(VALUE _self, VALUE _other);
+VALUE Message_hash(VALUE _self);
+VALUE Message_inspect(VALUE _self);
+VALUE Message_index(VALUE _self, VALUE field_name);
+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_encode_json(VALUE klass, VALUE msg_rb);
+
+VALUE Google_Protobuf_encode(VALUE self, VALUE msg_rb);
+VALUE Google_Protobuf_decode(VALUE self, VALUE klass, VALUE msg_rb);
+VALUE Google_Protobuf_encode_json(VALUE self, VALUE msg_rb);
+VALUE Google_Protobuf_decode_json(VALUE self, VALUE klass, VALUE msg_rb);
+
+VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
+
+VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
+VALUE enum_lookup(VALUE self, VALUE number);
+VALUE enum_resolve(VALUE self, VALUE sym);
+
+const upb_pbdecodermethod *new_fillmsg_decodermethod(
+ Descriptor* descriptor, const void *owner);
+
+// -----------------------------------------------------------------------------
+// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
+// instances.
+// -----------------------------------------------------------------------------
+void add_def_obj(const void* def, VALUE value);
+VALUE get_def_obj(const void* def);
+
+// -----------------------------------------------------------------------------
+// Utilities.
+// -----------------------------------------------------------------------------
+
+void check_upb_status(const upb_status* status, const char* msg);
+
+#define CHECK_UPB(code, msg) do { \
+ upb_status status = UPB_STATUS_INIT; \
+ code; \
+ check_upb_status(&status, msg); \
+} while (0)
+
+#endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
diff --git a/ruby/ext/google/protobuf_c/repeated_field.c b/ruby/ext/google/protobuf_c/repeated_field.c
new file mode 100644
index 00000000..6bd13b07
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/repeated_field.c
@@ -0,0 +1,597 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+// -----------------------------------------------------------------------------
+// Repeated field container type.
+// -----------------------------------------------------------------------------
+
+const rb_data_type_t RepeatedField_type = {
+ "Google::Protobuf::RepeatedField",
+ { RepeatedField_mark, RepeatedField_free, NULL },
+};
+
+VALUE cRepeatedField;
+
+RepeatedField* ruby_to_RepeatedField(VALUE _self) {
+ RepeatedField* self;
+ TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
+ return self;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.each(&block)
+ *
+ * Invokes the block once for each element of the repeated field. RepeatedField
+ * also includes Enumerable; combined with this method, the repeated field thus
+ * acts like an ordinary Ruby sequence.
+ */
+VALUE RepeatedField_each(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+ int element_size = native_slot_size(field_type);
+
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += element_size) {
+ void* memory = (void *) (((uint8_t *)self->elements) + off);
+ VALUE val = native_slot_get(field_type, field_type_class, memory);
+ rb_yield(val);
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.[](index) => value
+ *
+ * Accesses the element at the given index. Throws an exception on out-of-bounds
+ * errors.
+ */
+VALUE RepeatedField_index(VALUE _self, VALUE _index) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ int element_size = native_slot_size(self->field_type);
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+
+ int index = NUM2INT(_index);
+ if (index < 0 || index >= self->size) {
+ rb_raise(rb_eRangeError, "Index out of range");
+ }
+
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
+ return native_slot_get(field_type, field_type_class, memory);
+}
+
+/*
+ * call-seq:
+ * RepeatedField.[]=(index, value)
+ *
+ * Sets the element at the given index. On out-of-bounds assignments, extends
+ * the array and fills the hole (if any) with default values.
+ */
+VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+ int element_size = native_slot_size(field_type);
+
+ int index = NUM2INT(_index);
+ if (index < 0 || index >= (INT_MAX - 1)) {
+ rb_raise(rb_eRangeError, "Index out of range");
+ }
+ if (index >= self->size) {
+ RepeatedField_reserve(self, index + 1);
+ upb_fieldtype_t field_type = self->field_type;
+ int element_size = native_slot_size(field_type);
+ for (int i = self->size; i <= index; i++) {
+ void* elem = (void *)(((uint8_t *)self->elements) + i * element_size);
+ native_slot_init(field_type, elem);
+ }
+ self->size = index + 1;
+ }
+
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
+ native_slot_set(field_type, field_type_class, memory, val);
+ return Qnil;
+}
+
+static int kInitialSize = 8;
+
+void RepeatedField_reserve(RepeatedField* self, int new_size) {
+ if (new_size <= self->capacity) {
+ return;
+ }
+ if (self->capacity == 0) {
+ self->capacity = kInitialSize;
+ }
+ while (self->capacity < new_size) {
+ self->capacity *= 2;
+ }
+ void* old_elems = self->elements;
+ int elem_size = native_slot_size(self->field_type);
+ self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
+ if (old_elems != NULL) {
+ memcpy(self->elements, old_elems, self->size * elem_size);
+ xfree(old_elems);
+ }
+}
+
+/*
+ * call-seq:
+ * RepeatedField.push(value)
+ *
+ * Adds a new element to the repeated field.
+ */
+VALUE RepeatedField_push(VALUE _self, VALUE val) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ int element_size = native_slot_size(field_type);
+ RepeatedField_reserve(self, self->size + 1);
+ int index = self->size;
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
+ native_slot_set(field_type, self->field_type_class, memory, val);
+ // native_slot_set may raise an error; bump index only after set.
+ self->size++;
+ return _self;
+}
+
+// Used by parsing handlers.
+void RepeatedField_push_native(VALUE _self, void* data) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ int element_size = native_slot_size(field_type);
+ RepeatedField_reserve(self, self->size + 1);
+ int index = self->size;
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
+ memcpy(memory, data, element_size);
+ self->size++;
+}
+
+void* RepeatedField_index_native(VALUE _self, int index) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ int element_size = native_slot_size(field_type);
+ return ((uint8_t *)self->elements) + index * element_size;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.pop => value
+ *
+ * Removes the last element and returns it. Throws an exception if the repeated
+ * field is empty.
+ */
+VALUE RepeatedField_pop(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+ int element_size = native_slot_size(field_type);
+ if (self->size == 0) {
+ rb_raise(rb_eRangeError, "Pop from empty repeated field is not allowed.");
+ }
+ int index = self->size - 1;
+ void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
+ VALUE ret = native_slot_get(field_type, field_type_class, memory);
+ self->size--;
+ return ret;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.insert(*args)
+ *
+ * Pushes each arg in turn onto the end of the repeated field.
+ */
+VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self) {
+ for (int i = 0; i < argc; i++) {
+ RepeatedField_push(_self, argv[i]);
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.replace(list)
+ *
+ * Replaces the contents of the repeated field with the given list of elements.
+ */
+VALUE RepeatedField_replace(VALUE _self, VALUE list) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ Check_Type(list, T_ARRAY);
+ self->size = 0;
+ for (int i = 0; i < RARRAY_LEN(list); i++) {
+ RepeatedField_push(_self, rb_ary_entry(list, i));
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.clear
+ *
+ * Clears (removes all elements from) this repeated field.
+ */
+VALUE RepeatedField_clear(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ self->size = 0;
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.length
+ *
+ * Returns the length of this repeated field.
+ */
+VALUE RepeatedField_length(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ return INT2NUM(self->size);
+}
+
+static VALUE RepeatedField_new_this_type(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ VALUE new_rptfield = Qnil;
+ VALUE element_type = fieldtype_to_ruby(self->field_type);
+ if (self->field_type_class != Qnil) {
+ new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
+ element_type, self->field_type_class);
+ } else {
+ new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
+ element_type);
+ }
+ return new_rptfield;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.dup => repeated_field
+ *
+ * Duplicates this repeated field with a shallow copy. References to all
+ * non-primitive element objects (e.g., submessages) are shared.
+ */
+VALUE RepeatedField_dup(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ VALUE new_rptfield = RepeatedField_new_this_type(_self);
+ RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
+ RepeatedField_reserve(new_rptfield_self, self->size);
+ upb_fieldtype_t field_type = self->field_type;
+ size_t elem_size = native_slot_size(field_type);
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += elem_size) {
+ void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
+ void* from_mem = (uint8_t *)self->elements + off;
+ native_slot_dup(field_type, to_mem, from_mem);
+ new_rptfield_self->size++;
+ }
+
+ return new_rptfield;
+}
+
+// Internal only: used by Google::Protobuf.deep_copy.
+VALUE RepeatedField_deep_copy(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ VALUE new_rptfield = RepeatedField_new_this_type(_self);
+ RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
+ RepeatedField_reserve(new_rptfield_self, self->size);
+ upb_fieldtype_t field_type = self->field_type;
+ size_t elem_size = native_slot_size(field_type);
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += elem_size) {
+ void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
+ void* from_mem = (uint8_t *)self->elements + off;
+ native_slot_deep_copy(field_type, to_mem, from_mem);
+ new_rptfield_self->size++;
+ }
+
+ return new_rptfield;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.==(other) => boolean
+ *
+ * Compares this repeated field to another. Repeated fields are equal if their
+ * element types are equal, their lengths are equal, and each element is equal.
+ * Elements are compared as per normal Ruby semantics, by calling their :==
+ * methods (or performing a more efficient comparison for primitive types).
+ */
+VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
+ if (_self == _other) {
+ return Qtrue;
+ }
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+
+ // Inefficient but workable: to support comparison to a generic array, we
+ // build a temporary RepeatedField of our type.
+ if (TYPE(_other) == T_ARRAY) {
+ VALUE new_rptfield = RepeatedField_new_this_type(_self);
+ for (int i = 0; i < RARRAY_LEN(_other); i++) {
+ VALUE elem = rb_ary_entry(_other, i);
+ RepeatedField_push(new_rptfield, elem);
+ }
+ _other = new_rptfield;
+ }
+
+ RepeatedField* other = ruby_to_RepeatedField(_other);
+ if (self->field_type != other->field_type ||
+ self->field_type_class != other->field_type_class ||
+ self->size != other->size) {
+ return Qfalse;
+ }
+
+ upb_fieldtype_t field_type = self->field_type;
+ size_t elem_size = native_slot_size(field_type);
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += elem_size) {
+ void* self_mem = ((uint8_t *)self->elements) + off;
+ void* other_mem = ((uint8_t *)other->elements) + off;
+ if (!native_slot_eq(field_type, self_mem, other_mem)) {
+ return Qfalse;
+ }
+ }
+ return Qtrue;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.hash => hash_value
+ *
+ * Returns a hash value computed from this repeated field's elements.
+ */
+VALUE RepeatedField_hash(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+
+ VALUE hash = LL2NUM(0);
+
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+ size_t elem_size = native_slot_size(field_type);
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += elem_size) {
+ void* mem = ((uint8_t *)self->elements) + off;
+ VALUE elem = native_slot_get(field_type, field_type_class, mem);
+ hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2));
+ hash = rb_funcall(hash, rb_intern("^"), 1,
+ rb_funcall(elem, rb_intern("hash"), 0));
+ }
+
+ return hash;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.inspect => string
+ *
+ * Returns a string representing this repeated field's elements. It will be
+ * formated as "[<element>, <element>, ...]", with each element's string
+ * representation computed by its own #inspect method.
+ */
+VALUE RepeatedField_inspect(VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+
+ VALUE str = rb_str_new2("[");
+
+ bool first = true;
+
+ upb_fieldtype_t field_type = self->field_type;
+ VALUE field_type_class = self->field_type_class;
+ size_t elem_size = native_slot_size(field_type);
+ size_t off = 0;
+ for (int i = 0; i < self->size; i++, off += elem_size) {
+ void* mem = ((uint8_t *)self->elements) + off;
+ VALUE elem = native_slot_get(field_type, field_type_class, mem);
+ if (!first) {
+ str = rb_str_cat2(str, ", ");
+ } else {
+ first = false;
+ }
+ str = rb_str_append(str, rb_funcall(elem, rb_intern("inspect"), 0));
+ }
+
+ str = rb_str_cat2(str, "]");
+ return str;
+}
+
+/*
+ * call-seq:
+ * RepeatedField.+(other) => repeated field
+ *
+ * Returns a new repeated field that contains the concatenated list of this
+ * repeated field's elements and other's elements. The other (second) list may
+ * be either another repeated field or a Ruby array.
+ */
+VALUE RepeatedField_plus(VALUE _self, VALUE list) {
+ VALUE dupped = RepeatedField_dup(_self);
+
+ if (TYPE(list) == T_ARRAY) {
+ for (int i = 0; i < RARRAY_LEN(list); i++) {
+ VALUE elem = rb_ary_entry(list, i);
+ RepeatedField_push(dupped, elem);
+ }
+ } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
+ RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
+ if (self->field_type != list_rptfield->field_type ||
+ self->field_type_class != list_rptfield->field_type_class) {
+ rb_raise(rb_eArgError,
+ "Attempt to append RepeatedField with different element type.");
+ }
+ for (int i = 0; i < list_rptfield->size; i++) {
+ void* mem = RepeatedField_index_native(list, i);
+ RepeatedField_push_native(dupped, mem);
+ }
+ } else {
+ rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
+ }
+
+ return dupped;
+}
+
+static void validate_type_class(upb_fieldtype_t type, VALUE klass) {
+ if (rb_iv_get(klass, kDescriptorInstanceVar) == Qnil) {
+ rb_raise(rb_eArgError,
+ "Type class has no descriptor. Please pass a "
+ "class or enum as returned by the DescriptorPool.");
+ }
+ if (type == UPB_TYPE_MESSAGE) {
+ VALUE desc = rb_iv_get(klass, kDescriptorInstanceVar);
+ if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
+ RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
+ rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
+ }
+ if (rb_get_alloc_func(klass) != &Message_alloc) {
+ rb_raise(rb_eArgError,
+ "Message class was not returned by the DescriptorPool.");
+ }
+ } else if (type == UPB_TYPE_ENUM) {
+ VALUE enumdesc = rb_iv_get(klass, kDescriptorInstanceVar);
+ if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
+ RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
+ rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
+ }
+ }
+}
+
+void RepeatedField_init_args(int argc, VALUE* argv,
+ VALUE _self) {
+ RepeatedField* self = ruby_to_RepeatedField(_self);
+ VALUE ary = Qnil;
+ if (argc < 1) {
+ rb_raise(rb_eArgError, "Expected at least 1 argument.");
+ }
+ self->field_type = ruby_to_fieldtype(argv[0]);
+
+ if (self->field_type == UPB_TYPE_MESSAGE ||
+ self->field_type == UPB_TYPE_ENUM) {
+ if (argc < 2) {
+ rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
+ }
+ self->field_type_class = argv[1];
+ if (argc > 2) {
+ ary = argv[2];
+ }
+ validate_type_class(self->field_type, self->field_type_class);
+ } else {
+ if (argc > 2) {
+ rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
+ }
+ if (argc > 1) {
+ ary = argv[1];
+ }
+ }
+
+ if (ary != Qnil) {
+ if (!RB_TYPE_P(ary, T_ARRAY)) {
+ rb_raise(rb_eArgError, "Expected array as initialize argument");
+ }
+ for (int i = 0; i < RARRAY_LEN(ary); i++) {
+ RepeatedField_push(_self, rb_ary_entry(ary, i));
+ }
+ }
+}
+
+// Mark, free, alloc, init and class setup functions.
+
+void RepeatedField_mark(void* _self) {
+ RepeatedField* self = (RepeatedField*)_self;
+ rb_gc_mark(self->field_type_class);
+ upb_fieldtype_t field_type = self->field_type;
+ int element_size = native_slot_size(field_type);
+ for (int i = 0; i < self->size; i++) {
+ void* memory = (((uint8_t *)self->elements) + i * element_size);
+ native_slot_mark(self->field_type, memory);
+ }
+}
+
+void RepeatedField_free(void* _self) {
+ RepeatedField* self = (RepeatedField*)_self;
+ xfree(self->elements);
+ xfree(self);
+}
+
+/*
+ * call-seq:
+ * RepeatedField.new(type, type_class = nil, initial_elems = [])
+ *
+ * Creates a new repeated field. The provided type must be a Ruby symbol, and
+ * can take on the same values as those accepted by FieldDescriptor#type=. If
+ * the type is :message or :enum, type_class must be non-nil, and must be the
+ * Ruby class or module returned by Descriptor#msgclass or
+ * EnumDescriptor#enummodule, respectively. An initial list of elements may also
+ * be provided.
+ */
+VALUE RepeatedField_alloc(VALUE klass) {
+ RepeatedField* self = ALLOC(RepeatedField);
+ self->elements = NULL;
+ self->size = 0;
+ self->capacity = 0;
+ self->field_type = -1;
+ self->field_type_class = Qnil;
+ VALUE ret = TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
+ return ret;
+}
+
+VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
+ RepeatedField_init_args(argc, argv, self);
+ return Qnil;
+}
+
+void RepeatedField_register(VALUE module) {
+ VALUE klass = rb_define_class_under(
+ module, "RepeatedField", rb_cObject);
+ rb_define_alloc_func(klass, RepeatedField_alloc);
+ cRepeatedField = klass;
+ rb_gc_register_address(&cRepeatedField);
+
+ rb_define_method(klass, "initialize",
+ RepeatedField_init, -1);
+ rb_define_method(klass, "each", RepeatedField_each, 0);
+ rb_define_method(klass, "[]", RepeatedField_index, 1);
+ rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
+ rb_define_method(klass, "push", RepeatedField_push, 1);
+ rb_define_method(klass, "<<", RepeatedField_push, 1);
+ rb_define_method(klass, "pop", RepeatedField_pop, 0);
+ rb_define_method(klass, "insert", RepeatedField_insert, -1);
+ rb_define_method(klass, "replace", RepeatedField_replace, 1);
+ rb_define_method(klass, "clear", RepeatedField_clear, 0);
+ rb_define_method(klass, "length", RepeatedField_length, 0);
+ rb_define_method(klass, "dup", RepeatedField_dup, 0);
+ // Also define #clone so that we don't inherit Object#clone.
+ rb_define_method(klass, "clone", RepeatedField_dup, 0);
+ rb_define_method(klass, "==", RepeatedField_eq, 1);
+ rb_define_method(klass, "hash", RepeatedField_hash, 0);
+ rb_define_method(klass, "inspect", RepeatedField_inspect, 0);
+ rb_define_method(klass, "+", RepeatedField_plus, 1);
+ rb_include_module(klass, rb_mEnumerable);
+}
diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c
new file mode 100644
index 00000000..c4d801af
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/storage.c
@@ -0,0 +1,577 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "protobuf.h"
+
+#include <math.h>
+
+#include <ruby/encoding.h>
+
+// -----------------------------------------------------------------------------
+// Ruby <-> native slot management.
+// -----------------------------------------------------------------------------
+
+#define DEREF(memory, type) *(type*)(memory)
+
+size_t native_slot_size(upb_fieldtype_t type) {
+ switch (type) {
+ case UPB_TYPE_FLOAT: return 4;
+ case UPB_TYPE_DOUBLE: return 8;
+ case UPB_TYPE_BOOL: return 1;
+ case UPB_TYPE_STRING: return sizeof(VALUE);
+ case UPB_TYPE_BYTES: return sizeof(VALUE);
+ case UPB_TYPE_MESSAGE: return sizeof(VALUE);
+ case UPB_TYPE_ENUM: return 4;
+ case UPB_TYPE_INT32: return 4;
+ case UPB_TYPE_INT64: return 8;
+ case UPB_TYPE_UINT32: return 4;
+ case UPB_TYPE_UINT64: return 8;
+ default: return 0;
+ }
+}
+
+static void check_int_range_precision(upb_fieldtype_t type, VALUE val) {
+ // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
+ // bound; we just need to do precision checks (i.e., disallow rounding) and
+ // check for < 0 on unsigned types.
+ if (TYPE(val) == T_FLOAT) {
+ double dbl_val = NUM2DBL(val);
+ if (floor(dbl_val) != dbl_val) {
+ rb_raise(rb_eRangeError,
+ "Non-integral floating point value assigned to integer field.");
+ }
+ }
+ if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
+ if (NUM2DBL(val) < 0) {
+ rb_raise(rb_eRangeError,
+ "Assigning negative value to unsigned integer field.");
+ }
+ }
+}
+
+static bool is_ruby_num(VALUE value) {
+ return (TYPE(value) == T_FLOAT ||
+ TYPE(value) == T_FIXNUM ||
+ TYPE(value) == T_BIGNUM);
+}
+
+void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) {
+ bool bad_encoding = false;
+ rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value));
+ if (type == UPB_TYPE_STRING) {
+ bad_encoding =
+ string_encoding != kRubyStringUtf8Encoding &&
+ string_encoding != kRubyStringASCIIEncoding;
+ } else {
+ bad_encoding =
+ string_encoding != kRubyString8bitEncoding;
+ }
+ // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT
+ // (for bytes fields).
+ if (bad_encoding) {
+ rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)",
+ (type == UPB_TYPE_STRING) ? "string" : "bytes",
+ (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT",
+ rb_enc_name(string_encoding));
+ }
+}
+
+void native_slot_set(upb_fieldtype_t type, VALUE type_class,
+ void* memory, VALUE value) {
+ switch (type) {
+ case UPB_TYPE_FLOAT:
+ if (!is_ruby_num(value)) {
+ rb_raise(rb_eTypeError, "Expected number type for float field.");
+ }
+ DEREF(memory, float) = NUM2DBL(value);
+ break;
+ case UPB_TYPE_DOUBLE:
+ if (!is_ruby_num(value)) {
+ rb_raise(rb_eTypeError, "Expected number type for double field.");
+ }
+ DEREF(memory, double) = NUM2DBL(value);
+ break;
+ case UPB_TYPE_BOOL: {
+ int8_t val = -1;
+ if (value == Qtrue) {
+ val = 1;
+ } else if (value == Qfalse) {
+ val = 0;
+ } else {
+ rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
+ }
+ DEREF(memory, int8_t) = val;
+ break;
+ }
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES: {
+ if (CLASS_OF(value) != rb_cString) {
+ rb_raise(rb_eTypeError, "Invalid argument for string field.");
+ }
+ native_slot_validate_string_encoding(type, value);
+ DEREF(memory, VALUE) = value;
+ break;
+ }
+ case UPB_TYPE_MESSAGE: {
+ if (CLASS_OF(value) != type_class) {
+ rb_raise(rb_eTypeError,
+ "Invalid type %s to assign to submessage field.",
+ rb_class2name(CLASS_OF(value)));
+ }
+ DEREF(memory, VALUE) = value;
+ break;
+ }
+ case UPB_TYPE_ENUM: {
+ if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
+ rb_raise(rb_eTypeError,
+ "Expected number or symbol type for enum field.");
+ }
+ int32_t int_val = 0;
+ if (TYPE(value) == T_SYMBOL) {
+ // Ensure that the given symbol exists in the enum module.
+ VALUE lookup = rb_const_get(type_class, SYM2ID(value));
+ if (lookup == Qnil) {
+ rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
+ } else {
+ int_val = NUM2INT(lookup);
+ }
+ } else {
+ check_int_range_precision(UPB_TYPE_INT32, value);
+ int_val = NUM2INT(value);
+ }
+ DEREF(memory, int32_t) = int_val;
+ break;
+ }
+ case UPB_TYPE_INT32:
+ case UPB_TYPE_INT64:
+ case UPB_TYPE_UINT32:
+ case UPB_TYPE_UINT64:
+ if (!is_ruby_num(value)) {
+ rb_raise(rb_eTypeError, "Expected number type for integral field.");
+ }
+ check_int_range_precision(type, value);
+ switch (type) {
+ case UPB_TYPE_INT32:
+ DEREF(memory, int32_t) = NUM2INT(value);
+ break;
+ case UPB_TYPE_INT64:
+ DEREF(memory, int64_t) = NUM2LL(value);
+ break;
+ case UPB_TYPE_UINT32:
+ DEREF(memory, uint32_t) = NUM2UINT(value);
+ break;
+ case UPB_TYPE_UINT64:
+ DEREF(memory, uint64_t) = NUM2ULL(value);
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+VALUE native_slot_get(upb_fieldtype_t type, VALUE type_class, void* memory) {
+ switch (type) {
+ case UPB_TYPE_FLOAT:
+ return DBL2NUM(DEREF(memory, float));
+ case UPB_TYPE_DOUBLE:
+ return DBL2NUM(DEREF(memory, double));
+ case UPB_TYPE_BOOL:
+ return DEREF(memory, int8_t) ? Qtrue : Qfalse;
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ case UPB_TYPE_MESSAGE:
+ return *((VALUE *)memory);
+ case UPB_TYPE_ENUM: {
+ int32_t val = DEREF(memory, int32_t);
+ VALUE symbol = enum_lookup(type_class, INT2NUM(val));
+ if (symbol == Qnil) {
+ return INT2NUM(val);
+ } else {
+ return symbol;
+ }
+ }
+ case UPB_TYPE_INT32:
+ return INT2NUM(DEREF(memory, int32_t));
+ case UPB_TYPE_INT64:
+ return LL2NUM(DEREF(memory, int64_t));
+ case UPB_TYPE_UINT32:
+ return UINT2NUM(DEREF(memory, uint32_t));
+ case UPB_TYPE_UINT64:
+ return ULL2NUM(DEREF(memory, uint64_t));
+ default:
+ return Qnil;
+ }
+}
+
+void native_slot_init(upb_fieldtype_t type, void* memory) {
+ switch (type) {
+ case UPB_TYPE_FLOAT:
+ DEREF(memory, float) = 0.0;
+ break;
+ case UPB_TYPE_DOUBLE:
+ DEREF(memory, double) = 0.0;
+ break;
+ case UPB_TYPE_BOOL:
+ DEREF(memory, int8_t) = 0;
+ break;
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ // TODO(cfallin): set encoding appropriately
+ DEREF(memory, VALUE) = rb_str_new2("");
+ break;
+ case UPB_TYPE_MESSAGE:
+ DEREF(memory, VALUE) = Qnil;
+ break;
+ case UPB_TYPE_ENUM:
+ case UPB_TYPE_INT32:
+ DEREF(memory, int32_t) = 0;
+ break;
+ case UPB_TYPE_INT64:
+ DEREF(memory, int64_t) = 0;
+ break;
+ case UPB_TYPE_UINT32:
+ DEREF(memory, uint32_t) = 0;
+ break;
+ case UPB_TYPE_UINT64:
+ DEREF(memory, uint64_t) = 0;
+ break;
+ default:
+ break;
+ }
+}
+
+void native_slot_mark(upb_fieldtype_t type, void* memory) {
+ switch (type) {
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ case UPB_TYPE_MESSAGE:
+ rb_gc_mark(DEREF(memory, VALUE));
+ break;
+ default:
+ break;
+ }
+}
+
+void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
+ memcpy(to, from, native_slot_size(type));
+}
+
+void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
+ switch (type) {
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES: {
+ VALUE from_val = DEREF(from, VALUE);
+ DEREF(to, VALUE) = (from_val != Qnil) ?
+ rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
+ break;
+ }
+ case UPB_TYPE_MESSAGE: {
+ VALUE from_val = DEREF(from, VALUE);
+ DEREF(to, VALUE) = (from_val != Qnil) ?
+ Message_deep_copy(from_val) : Qnil;
+ break;
+ }
+ default:
+ memcpy(to, from, native_slot_size(type));
+ }
+}
+
+bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
+ switch (type) {
+ case UPB_TYPE_STRING:
+ case UPB_TYPE_BYTES:
+ case UPB_TYPE_MESSAGE: {
+ VALUE val1 = DEREF(mem1, VALUE);
+ VALUE val2 = DEREF(mem2, VALUE);
+ VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
+ return ret == Qtrue;
+ }
+ default:
+ return !memcmp(mem1, mem2, native_slot_size(type));
+ }
+}
+
+// -----------------------------------------------------------------------------
+// Memory layout management.
+// -----------------------------------------------------------------------------
+
+MessageLayout* create_layout(const upb_msgdef* msgdef) {
+ MessageLayout* layout = ALLOC(MessageLayout);
+ int nfields = upb_msgdef_numfields(msgdef);
+ layout->offsets = ALLOC_N(size_t, nfields);
+
+ upb_msg_iter it;
+ size_t off = 0;
+ for (upb_msg_begin(&it, msgdef); !upb_msg_done(&it); upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ size_t field_size =
+ (upb_fielddef_label(field) == UPB_LABEL_REPEATED) ?
+ sizeof(VALUE) : native_slot_size(upb_fielddef_type(field));
+ // align current offset
+ off = (off + field_size - 1) & ~(field_size - 1);
+ layout->offsets[upb_fielddef_index(field)] = off;
+ off += field_size;
+ }
+
+ layout->size = off;
+
+ layout->msgdef = msgdef;
+ upb_msgdef_ref(layout->msgdef, &layout->msgdef);
+
+ return layout;
+}
+
+void free_layout(MessageLayout* layout) {
+ xfree(layout->offsets);
+ upb_msgdef_unref(layout->msgdef, &layout->msgdef);
+ xfree(layout);
+}
+
+static VALUE get_type_class(const upb_fielddef* field) {
+ VALUE type_class = Qnil;
+ if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
+ VALUE submsgdesc =
+ get_def_obj(upb_fielddef_subdef(field));
+ type_class = Descriptor_msgclass(submsgdesc);
+ } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
+ VALUE subenumdesc =
+ get_def_obj(upb_fielddef_subdef(field));
+ type_class = EnumDescriptor_enummodule(subenumdesc);
+ }
+ return type_class;
+}
+
+VALUE layout_get(MessageLayout* layout,
+ void* storage,
+ const upb_fielddef* field) {
+ void* memory = ((uint8_t *)storage) +
+ layout->offsets[upb_fielddef_index(field)];
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ return *((VALUE *)memory);
+ } else {
+ return native_slot_get(upb_fielddef_type(field),
+ get_type_class(field),
+ memory);
+ }
+}
+
+static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
+ assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
+
+ if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
+ RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
+ rb_raise(rb_eTypeError, "Expected repeated field array");
+ }
+
+ RepeatedField* self = ruby_to_RepeatedField(val);
+ if (self->field_type != upb_fielddef_type(field)) {
+ rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
+ }
+
+ if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE ||
+ upb_fielddef_type(field) == UPB_TYPE_ENUM) {
+ RepeatedField* self = ruby_to_RepeatedField(val);
+ if (self->field_type_class !=
+ get_def_obj(upb_fielddef_subdef(field))) {
+ rb_raise(rb_eTypeError,
+ "Repeated field array has wrong message/enum class");
+ }
+ }
+}
+
+void layout_set(MessageLayout* layout,
+ void* storage,
+ const upb_fielddef* field,
+ VALUE val) {
+ void* memory = ((uint8_t *)storage) +
+ layout->offsets[upb_fielddef_index(field)];
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ check_repeated_field_type(val, field);
+ *((VALUE *)memory) = val;
+ } else {
+ native_slot_set(upb_fielddef_type(field), get_type_class(field),
+ memory, val);
+ }
+}
+
+void layout_init(MessageLayout* layout,
+ void* storage) {
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ void* memory = ((uint8_t *)storage) +
+ layout->offsets[upb_fielddef_index(field)];
+
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ VALUE ary = Qnil;
+ VALUE type_class = get_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);
+ }
+ *((VALUE *)memory) = ary;
+ } else {
+ native_slot_init(upb_fielddef_type(field), memory);
+ }
+ }
+}
+
+void layout_mark(MessageLayout* layout, void* storage) {
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ void* memory = ((uint8_t *)storage) +
+ layout->offsets[upb_fielddef_index(field)];
+
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ rb_gc_mark(*((VALUE *)memory));
+ } else {
+ native_slot_mark(upb_fielddef_type(field), memory);
+ }
+ }
+}
+
+void layout_dup(MessageLayout* layout, void* to, void* from) {
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ void* to_memory = ((uint8_t *)to) +
+ layout->offsets[upb_fielddef_index(field)];
+ void* from_memory = ((uint8_t *)from) +
+ layout->offsets[upb_fielddef_index(field)];
+
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ *((VALUE *)to_memory) = RepeatedField_dup(*((VALUE *)from_memory));
+ } else {
+ native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
+ }
+ }
+}
+
+void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ void* to_memory = ((uint8_t *)to) +
+ layout->offsets[upb_fielddef_index(field)];
+ void* from_memory = ((uint8_t *)from) +
+ layout->offsets[upb_fielddef_index(field)];
+
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ *((VALUE *)to_memory) = RepeatedField_deep_copy(*((VALUE *)from_memory));
+ } else {
+ native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
+ }
+ }
+}
+
+VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
+ upb_msg_iter it;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ void* msg1_memory = ((uint8_t *)msg1) +
+ layout->offsets[upb_fielddef_index(field)];
+ void* msg2_memory = ((uint8_t *)msg2) +
+ layout->offsets[upb_fielddef_index(field)];
+
+ if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
+ if (RepeatedField_eq(*((VALUE *)msg1_memory),
+ *((VALUE *)msg2_memory)) == Qfalse) {
+ return Qfalse;
+ }
+ } else {
+ if (!native_slot_eq(upb_fielddef_type(field),
+ msg1_memory, msg2_memory)) {
+ return Qfalse;
+ }
+ }
+ }
+ return Qtrue;
+}
+
+VALUE layout_hash(MessageLayout* layout, void* storage) {
+ upb_msg_iter it;
+ st_index_t h = rb_hash_start(0);
+ VALUE hash_sym = rb_intern("hash");
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ VALUE field_val = layout_get(layout, storage, field);
+ h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
+ }
+ h = rb_hash_end(h);
+
+ return INT2FIX(h);
+}
+
+VALUE layout_inspect(MessageLayout* layout, void* storage) {
+ VALUE str = rb_str_new2("");
+
+ upb_msg_iter it;
+ bool first = true;
+ for (upb_msg_begin(&it, layout->msgdef);
+ !upb_msg_done(&it);
+ upb_msg_next(&it)) {
+ const upb_fielddef* field = upb_msg_iter_field(&it);
+ VALUE field_val = layout_get(layout, storage, field);
+
+ if (!first) {
+ str = rb_str_cat2(str, ", ");
+ } else {
+ first = false;
+ }
+ str = rb_str_cat2(str, upb_fielddef_name(field));
+ str = rb_str_cat2(str, ": ");
+
+ str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
+ }
+
+ return str;
+}
diff --git a/ruby/ext/google/protobuf_c/upb.c b/ruby/ext/google/protobuf_c/upb.c
new file mode 100644
index 00000000..c9f47195
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/upb.c
@@ -0,0 +1,10078 @@
+// Amalgamated source file
+#include "upb.h"
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2008-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ size_t len;
+ char str[1]; // Null-terminated string data follows.
+} str_t;
+
+static str_t *newstr(const char *data, size_t len) {
+ str_t *ret = malloc(sizeof(*ret) + len);
+ if (!ret) return NULL;
+ ret->len = len;
+ memcpy(ret->str, data, len);
+ ret->str[len] = '\0';
+ return ret;
+}
+
+static void freestr(str_t *s) { free(s); }
+
+// isalpha() etc. from <ctype.h> are locale-dependent, which we don't want.
+static bool upb_isbetween(char c, char low, char high) {
+ return c >= low && c <= high;
+}
+
+static bool upb_isletter(char c) {
+ return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
+}
+
+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) {
+ bool start = true;
+ for (size_t i = 0; i < len; i++) {
+ char c = str[i];
+ if (c == '.') {
+ if (start || !full) {
+ upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
+ return false;
+ }
+ start = true;
+ } else if (start) {
+ if (!upb_isletter(c)) {
+ upb_status_seterrf(
+ s, "invalid name: path components must start with a letter (%s)",
+ str);
+ return false;
+ }
+ start = false;
+ } else {
+ if (!upb_isalphanum(c)) {
+ upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
+ str);
+ return false;
+ }
+ }
+ }
+ return !start;
+}
+
+
+/* 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; }
+
+bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s) {
+ assert(!upb_def_isfrozen(def));
+ if (!upb_isident(fullname, strlen(fullname), true, s)) return false;
+ free((void*)def->fullname);
+ def->fullname = upb_strdup(fullname);
+ return true;
+}
+
+upb_def *upb_def_dup(const upb_def *def, const void *o) {
+ switch (def->type) {
+ case UPB_DEF_MSG:
+ return UPB_UPCAST(upb_msgdef_dup(upb_downcast_msgdef(def), o));
+ case UPB_DEF_FIELD:
+ return UPB_UPCAST(upb_fielddef_dup(upb_downcast_fielddef(def), o));
+ case UPB_DEF_ENUM:
+ return UPB_UPCAST(upb_enumdef_dup(upb_downcast_enumdef(def), o));
+ default: assert(false); return NULL;
+ }
+}
+
+bool upb_def_isfrozen(const upb_def *def) {
+ return upb_refcounted_isfrozen(UPB_UPCAST(def));
+}
+
+void upb_def_ref(const upb_def *def, const void *owner) {
+ upb_refcounted_ref(UPB_UPCAST(def), owner);
+}
+
+void upb_def_unref(const upb_def *def, const void *owner) {
+ upb_refcounted_unref(UPB_UPCAST(def), owner);
+}
+
+void upb_def_donateref(const upb_def *def, const void *from, const void *to) {
+ upb_refcounted_donateref(UPB_UPCAST(def), from, to);
+}
+
+void upb_def_checkref(const upb_def *def, const void *owner) {
+ upb_refcounted_checkref(UPB_UPCAST(def), owner);
+}
+
+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_UPCAST(def), vtbl, owner)) return false;
+ def->type = type;
+ def->fullname = NULL;
+ def->came_from_user = false;
+ return true;
+}
+
+static void upb_def_uninit(upb_def *def) {
+ free((void*)def->fullname);
+}
+
+static const char *msgdef_name(const upb_msgdef *m) {
+ const char *name = upb_def_fullname(UPB_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)) {
+ 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;
+ }
+
+ const upb_def *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().
+ 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).
+ 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));
+ }
+
+ 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) {
+ uint32_t ret = upb_fielddef_number(f);
+ const uint32_t high_bit = 1 << 30;
+ assert(ret < high_bit);
+ if (!upb_fielddef_issubmsg(f))
+ ret |= high_bit;
+ return ret;
+}
+
+int cmp_fields(const void *p1, const void *p2) {
+ const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
+ const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
+ return field_rank(f1) - field_rank(f2);
+}
+
+static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
+ // Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
+ // lowest indexes, but we do not publicly guarantee this.
+ int n = upb_msgdef_numfields(m);
+ upb_fielddef **fields = malloc(n * sizeof(*fields));
+ if (!fields) return false;
+
+ upb_msg_iter j;
+ int i;
+ m->submsg_field_count = 0;
+ for(i = 0, upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j), i++) {
+ upb_fielddef *f = upb_msg_iter_field(&j);
+ assert(f->msg.def == m);
+ if (!upb_validate_field(f, s)) {
+ free(fields);
+ return false;
+ }
+ if (upb_fielddef_issubmsg(f)) {
+ m->submsg_field_count++;
+ }
+ fields[i] = f;
+ }
+
+ qsort(fields, n, sizeof(*fields), cmp_fields);
+
+ uint32_t selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
+ for (i = 0; i < n; i++) {
+ upb_fielddef *f = fields[i];
+ f->index_ = i;
+ f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
+ selector += upb_handlers_selectorcount(f);
+ }
+ m->selector_count = selector;
+
+#ifndef NDEBUG
+ // 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);
+
+ upb_inttable t;
+ upb_inttable_init(&t, UPB_CTYPE_BOOL);
+ upb_value v = upb_value_bool(true);
+ upb_selector_t sel;
+ upb_inttable_insert(&t, UPB_STARTMSG_SELECTOR, v);
+ upb_inttable_insert(&t, UPB_ENDMSG_SELECTOR, v);
+ for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j)) {
+ upb_fielddef *f = upb_msg_iter_field(&j);
+ // These calls will assert-fail in upb_table if the value already exists.
+ TRY(UPB_HANDLER_INT32);
+ TRY(UPB_HANDLER_INT64)
+ TRY(UPB_HANDLER_UINT32)
+ TRY(UPB_HANDLER_UINT64)
+ TRY(UPB_HANDLER_FLOAT)
+ TRY(UPB_HANDLER_DOUBLE)
+ TRY(UPB_HANDLER_BOOL)
+ TRY(UPB_HANDLER_STARTSTR)
+ TRY(UPB_HANDLER_STRING)
+ TRY(UPB_HANDLER_ENDSTR)
+ TRY(UPB_HANDLER_STARTSUBMSG)
+ TRY(UPB_HANDLER_ENDSUBMSG)
+ TRY(UPB_HANDLER_STARTSEQ)
+ TRY(UPB_HANDLER_ENDSEQ)
+ }
+ upb_inttable_uninit(&t);
+#undef TRY
+#endif
+
+ free(fields);
+ return true;
+}
+
+bool upb_def_freeze(upb_def *const* defs, int n, upb_status *s) {
+ upb_status_clear(s);
+
+ // First perform validation, in two passes so we can check that we have a
+ // transitive closure without needing to search.
+ for (int 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 if (def->type == UPB_DEF_ENUM) {
+ if (!upb_validate_enumdef(upb_dyncast_enumdef(def), s)) {
+ goto err;
+ }
+ } else {
+ // Set now to detect transitive closure in the second pass.
+ def->came_from_user = true;
+ }
+ }
+
+ // Second pass of validation. Also assign selector bases and indexes, and
+ // compact tables.
+ for (int i = 0; i < n; i++) {
+ upb_msgdef *m = upb_dyncast_msgdef_mutable(defs[i]);
+ upb_enumdef *e = upb_dyncast_enumdef_mutable(defs[i]);
+ if (m) {
+ upb_inttable_compact(&m->itof);
+ if (!assign_msg_indices(m, s)) {
+ goto err;
+ }
+ } else if (e) {
+ upb_inttable_compact(&e->iton);
+ }
+ }
+
+ // Def graph contains FieldDefs between each MessageDef, so double the limit.
+ int maxdepth = UPB_MAX_MESSAGE_DEPTH * 2;
+
+ // Validation all passed; freeze the defs.
+ bool ret =
+ upb_refcounted_freeze((upb_refcounted * const *)defs, n, s, maxdepth);
+ assert(!(s && ret != upb_ok(s)));
+ return ret;
+
+err:
+ for (int i = 0; i < n; i++) {
+ defs[i]->came_from_user = false;
+ }
+ assert(!(s && upb_ok(s)));
+ return false;
+}
+
+
+/* upb_enumdef ****************************************************************/
+
+static void upb_enumdef_free(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_strdup() from upb_enumdef_addval().
+ free(upb_value_getcstr(upb_inttable_iter_value(&i)));
+ }
+ upb_strtable_uninit(&e->ntoi);
+ upb_inttable_uninit(&e->iton);
+ upb_def_uninit(UPB_UPCAST(e));
+ free(e);
+}
+
+upb_enumdef *upb_enumdef_new(const void *owner) {
+ static const struct upb_refcounted_vtbl vtbl = {NULL, &upb_enumdef_free};
+ upb_enumdef *e = malloc(sizeof(*e));
+ if (!e) return NULL;
+ if (!upb_def_init(UPB_UPCAST(e), UPB_DEF_ENUM, &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:
+ free(e);
+ return NULL;
+}
+
+upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner) {
+ upb_enumdef *new_e = upb_enumdef_new(owner);
+ if (!new_e) return NULL;
+ upb_enum_iter i;
+ for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
+ bool success = upb_enumdef_addval(
+ new_e, upb_enum_iter_name(&i),upb_enum_iter_number(&i), NULL);
+ if (!success) {
+ upb_enumdef_unref(new_e, owner);
+ return NULL;
+ }
+ }
+ return new_e;
+}
+
+bool upb_enumdef_isfrozen(const upb_enumdef *e) {
+ return upb_def_isfrozen(UPB_UPCAST(e));
+}
+
+void upb_enumdef_ref(const upb_enumdef *e, const void *owner) {
+ upb_def_ref(UPB_UPCAST(e), owner);
+}
+
+void upb_enumdef_unref(const upb_enumdef *e, const void *owner) {
+ upb_def_unref(UPB_UPCAST(e), owner);
+}
+
+void upb_enumdef_donateref(
+ const upb_enumdef *e, const void *from, const void *to) {
+ upb_def_donateref(UPB_UPCAST(e), from, to);
+}
+
+void upb_enumdef_checkref(const upb_enumdef *e, const void *owner) {
+ upb_def_checkref(UPB_UPCAST(e), owner);
+}
+
+bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status) {
+ upb_def *d = UPB_UPCAST(e);
+ return upb_def_freeze(&d, 1, status);
+}
+
+const char *upb_enumdef_fullname(const upb_enumdef *e) {
+ return upb_def_fullname(UPB_UPCAST(e));
+}
+
+bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
+ upb_status *s) {
+ return upb_def_setfullname(UPB_UPCAST(e), fullname, s);
+}
+
+bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
+ upb_status *status) {
+ 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) &&
+ !upb_inttable_insert(&e->iton, num, upb_value_cstr(upb_strdup(name)))) {
+ 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_VAR(ok, ok);
+ }
+ return true;
+}
+
+int32_t upb_enumdef_default(const upb_enumdef *e) {
+ assert(upb_enumdef_iton(e, e->defaultval));
+ return e->defaultval;
+}
+
+bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) {
+ 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);
+}
+
+void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
+ // We iterate over the ntoi table, to account for duplicate numbers.
+ upb_strtable_begin(i, &e->ntoi);
+}
+
+void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
+bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
+
+bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
+ size_t len, int32_t *num) {
+ upb_value v;
+ if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
+ return false;
+ }
+ if (num) *num = upb_value_getint32(v);
+ return true;
+}
+
+const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
+ upb_value v;
+ return upb_inttable_lookup32(&def->iton, num, &v) ?
+ upb_value_getcstr(v) : NULL;
+}
+
+const char *upb_enum_iter_name(upb_enum_iter *iter) {
+ return upb_strtable_iter_key(iter);
+}
+
+int32_t upb_enum_iter_number(upb_enum_iter *iter) {
+ return upb_value_getint32(upb_strtable_iter_value(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);
+}
+
+static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit,
+ void *closure) {
+ const upb_fielddef *f = (const upb_fielddef*)r;
+ if (upb_fielddef_containingtype(f)) {
+ visit(r, UPB_UPCAST2(upb_fielddef_containingtype(f)), closure);
+ }
+ if (upb_fielddef_subdef(f)) {
+ visit(r, UPB_UPCAST(upb_fielddef_subdef(f)), closure);
+ }
+}
+
+static void freefield(upb_refcounted *r) {
+ upb_fielddef *f = (upb_fielddef*)r;
+ upb_fielddef_uninit_default(f);
+ if (f->subdef_is_symbolic)
+ free(f->sub.name);
+ upb_def_uninit(UPB_UPCAST(f));
+ free(f);
+}
+
+static const char *enumdefaultstr(const upb_fielddef *f) {
+ assert(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
+ const upb_enumdef *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));
+ assert(name);
+ return name;
+ }
+ }
+ }
+ return NULL;
+}
+
+static bool enumdefaultint32(const upb_fielddef *f, int32_t *val) {
+ assert(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
+ const upb_enumdef *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;
+}
+
+upb_fielddef *upb_fielddef_new(const void *owner) {
+ static const struct upb_refcounted_vtbl vtbl = {visitfield, freefield};
+ upb_fielddef *f = malloc(sizeof(*f));
+ if (!f) return NULL;
+ if (!upb_def_init(UPB_UPCAST(f), UPB_DEF_FIELD, &vtbl, owner)) {
+ free(f);
+ return NULL;
+ }
+ f->msg.def = NULL;
+ f->sub.def = 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;
+}
+
+upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner) {
+ upb_fielddef *newf = upb_fielddef_new(owner);
+ if (!newf) return NULL;
+ upb_fielddef_settype(newf, upb_fielddef_type(f));
+ upb_fielddef_setlabel(newf, upb_fielddef_label(f));
+ upb_fielddef_setnumber(newf, upb_fielddef_number(f), NULL);
+ upb_fielddef_setname(newf, upb_fielddef_name(f), NULL);
+ if (f->default_is_string && f->defaultval.bytes) {
+ str_t *s = f->defaultval.bytes;
+ upb_fielddef_setdefaultstr(newf, s->str, s->len, NULL);
+ } else {
+ newf->default_is_string = f->default_is_string;
+ newf->defaultval = f->defaultval;
+ }
+
+ const char *srcname;
+ if (f->subdef_is_symbolic) {
+ srcname = f->sub.name; // Might be NULL.
+ } else {
+ srcname = f->sub.def ? upb_def_fullname(f->sub.def) : NULL;
+ }
+ if (srcname) {
+ char *newname = malloc(strlen(f->sub.def->fullname) + 2);
+ if (!newname) {
+ upb_fielddef_unref(newf, owner);
+ return NULL;
+ }
+ strcpy(newname, ".");
+ strcat(newname, f->sub.def->fullname);
+ upb_fielddef_setsubdefname(newf, newname, NULL);
+ free(newname);
+ }
+
+ return newf;
+}
+
+bool upb_fielddef_isfrozen(const upb_fielddef *f) {
+ return upb_def_isfrozen(UPB_UPCAST(f));
+}
+
+void upb_fielddef_ref(const upb_fielddef *f, const void *owner) {
+ upb_def_ref(UPB_UPCAST(f), owner);
+}
+
+void upb_fielddef_unref(const upb_fielddef *f, const void *owner) {
+ upb_def_unref(UPB_UPCAST(f), owner);
+}
+
+void upb_fielddef_donateref(
+ const upb_fielddef *f, const void *from, const void *to) {
+ upb_def_donateref(UPB_UPCAST(f), from, to);
+}
+
+void upb_fielddef_checkref(const upb_fielddef *f, const void *owner) {
+ upb_def_checkref(UPB_UPCAST(f), owner);
+}
+
+bool upb_fielddef_typeisset(const upb_fielddef *f) {
+ return f->type_is_set_;
+}
+
+upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
+ assert(f->type_is_set_);
+ return f->type_;
+}
+
+uint32_t upb_fielddef_index(const upb_fielddef *f) {
+ return f->index_;
+}
+
+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_;
+}
+
+bool upb_fielddef_isextension(const upb_fielddef *f) {
+ return f->is_extension_;
+}
+
+bool upb_fielddef_lazy(const upb_fielddef *f) {
+ return f->lazy_;
+}
+
+bool upb_fielddef_packed(const upb_fielddef *f) {
+ return f->packed_;
+}
+
+const char *upb_fielddef_name(const upb_fielddef *f) {
+ return upb_def_fullname(UPB_UPCAST(f));
+}
+
+const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
+ return f->msg_is_symbolic ? NULL : f->msg.def;
+}
+
+upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f) {
+ return (upb_msgdef*)upb_fielddef_containingtype(f);
+}
+
+const char *upb_fielddef_containingtypename(upb_fielddef *f) {
+ return f->msg_is_symbolic ? f->msg.name : NULL;
+}
+
+static void release_containingtype(upb_fielddef *f) {
+ if (f->msg_is_symbolic) free(f->msg.name);
+}
+
+bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
+ upb_status *s) {
+ 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 ".").
+ release_containingtype(f);
+ f->msg.name = upb_strdup(name);
+ f->msg_is_symbolic = true;
+ return true;
+}
+
+bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s) {
+ return upb_def_setfullname(UPB_UPCAST(f), name, s);
+}
+
+static void chkdefaulttype(const upb_fielddef *f, upb_fieldtype_t type) {
+ UPB_UNUSED(f);
+ UPB_UNUSED(type);
+ 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_VAR(ok, ok);
+ return val;
+ } else {
+ chkdefaulttype(f, UPB_TYPE_INT32);
+ return f->defaultval.sint;
+ }
+}
+
+uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
+ chkdefaulttype(f, UPB_TYPE_UINT64);
+ return f->defaultval.uint;
+}
+
+uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
+ chkdefaulttype(f, UPB_TYPE_UINT32);
+ return f->defaultval.uint;
+}
+
+bool upb_fielddef_defaultbool(const upb_fielddef *f) {
+ chkdefaulttype(f, UPB_TYPE_BOOL);
+ return f->defaultval.uint;
+}
+
+float upb_fielddef_defaultfloat(const upb_fielddef *f) {
+ chkdefaulttype(f, UPB_TYPE_FLOAT);
+ return f->defaultval.flt;
+}
+
+double upb_fielddef_defaultdouble(const upb_fielddef *f) {
+ chkdefaulttype(f, UPB_TYPE_DOUBLE);
+ return f->defaultval.dbl;
+}
+
+const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
+ assert(f->type_is_set_);
+ 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);
+ 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 (len) *len = str->len;
+ return str->str;
+ }
+
+ return NULL;
+}
+
+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_def *upb_fielddef_subdef(const upb_fielddef *f) {
+ return f->subdef_is_symbolic ? NULL : f->sub.def;
+}
+
+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;
+}
+
+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;
+}
+
+upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) {
+ return (upb_def*)upb_fielddef_subdef(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_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;
+}
+
+void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) {
+ assert(!upb_fielddef_isfrozen(f));
+ assert(upb_fielddef_checktype(type));
+ upb_fielddef_uninit_default(f);
+ f->type_ = type;
+ f->type_is_set_ = true;
+ upb_fielddef_init_default(f);
+}
+
+void upb_fielddef_setdescriptortype(upb_fielddef *f, int type) {
+ 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: assert(false);
+ }
+
+ 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);
+ }
+
+ upb_fielddef_settagdelim(f, type == UPB_DESCRIPTOR_TYPE_GROUP);
+}
+
+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;
+}
+
+void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension) {
+ assert(!upb_fielddef_isfrozen(f));
+ f->is_extension_ = is_extension;
+}
+
+void upb_fielddef_setlazy(upb_fielddef *f, bool lazy) {
+ assert(!upb_fielddef_isfrozen(f));
+ f->lazy_ = lazy;
+}
+
+void upb_fielddef_setpacked(upb_fielddef *f, bool packed) {
+ assert(!upb_fielddef_isfrozen(f));
+ f->packed_ = packed;
+}
+
+void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) {
+ assert(!upb_fielddef_isfrozen(f));
+ assert(upb_fielddef_checklabel(label));
+ f->label_ = label;
+}
+
+void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt) {
+ assert(!upb_fielddef_isfrozen(f));
+ assert(upb_fielddef_checkintfmt(fmt));
+ f->intfmt = fmt;
+}
+
+void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim) {
+ assert(!upb_fielddef_isfrozen(f));
+ f->tagdelim = tag_delim;
+ f->tagdelim = tag_delim;
+}
+
+static bool checksetdefault(upb_fielddef *f, upb_fieldtype_t type) {
+ if (!f->type_is_set_ || upb_fielddef_isfrozen(f) ||
+ upb_fielddef_type(f) != type) {
+ assert(false);
+ return false;
+ }
+ if (f->default_is_string) {
+ str_t *s = f->defaultval.bytes;
+ assert(s || type == UPB_TYPE_ENUM);
+ if (s) freestr(s);
+ }
+ f->default_is_string = false;
+ return true;
+}
+
+void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t value) {
+ if (checksetdefault(f, UPB_TYPE_INT64))
+ f->defaultval.sint = value;
+}
+
+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;
+ }
+}
+
+void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t value) {
+ if (checksetdefault(f, UPB_TYPE_UINT64))
+ f->defaultval.uint = value;
+}
+
+void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t value) {
+ if (checksetdefault(f, UPB_TYPE_UINT32))
+ f->defaultval.uint = value;
+}
+
+void upb_fielddef_setdefaultbool(upb_fielddef *f, bool value) {
+ if (checksetdefault(f, UPB_TYPE_BOOL))
+ f->defaultval.uint = value;
+}
+
+void upb_fielddef_setdefaultfloat(upb_fielddef *f, float value) {
+ if (checksetdefault(f, UPB_TYPE_FLOAT))
+ f->defaultval.flt = value;
+}
+
+void upb_fielddef_setdefaultdouble(upb_fielddef *f, double value) {
+ if (checksetdefault(f, UPB_TYPE_DOUBLE))
+ f->defaultval.dbl = value;
+}
+
+bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
+ upb_status *s) {
+ assert(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE_ENUM);
+ if (f->type_ == UPB_TYPE_ENUM && !upb_isident(str, len, false, s))
+ return false;
+
+ if (f->default_is_string) {
+ str_t *s = f->defaultval.bytes;
+ assert(s || f->type_ == UPB_TYPE_ENUM);
+ if (s) freestr(s);
+ } else {
+ assert(f->type_ == UPB_TYPE_ENUM);
+ }
+
+ str_t *str2 = newstr(str, len);
+ f->defaultval.bytes = str2;
+ f->default_is_string = true;
+ return true;
+}
+
+void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
+ upb_status *s) {
+ assert(f->type_is_set_);
+ upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0, s);
+}
+
+bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f) {
+ assert(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
+ int32_t val;
+ return enumdefaultint32(f, &val);
+}
+
+bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f) {
+ assert(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
+ return enumdefaultstr(f) != NULL;
+}
+
+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;
+ }
+}
+
+static void release_subdef(upb_fielddef *f) {
+ if (f->subdef_is_symbolic) {
+ free(f->sub.name);
+ } else if (f->sub.def) {
+ upb_unref2(f->sub.def, f);
+ }
+}
+
+bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
+ upb_status *s) {
+ assert(!upb_fielddef_isfrozen(f));
+ 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_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
+ upb_status *s) {
+ return upb_fielddef_setsubdef(f, UPB_UPCAST(subdef), s);
+}
+
+bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
+ upb_status *s) {
+ return upb_fielddef_setsubdef(f, UPB_UPCAST(subdef), s);
+}
+
+bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
+ upb_status *s) {
+ assert(!upb_fielddef_isfrozen(f));
+ if (!upb_fielddef_hassubdef(f)) {
+ upb_status_seterrmsg(s, "field type does not accept a subdef");
+ return false;
+ }
+ // TODO: validate name (upb_isident() doesn't quite work atm because this name
+ // may have a leading ".").
+ release_subdef(f);
+ f->sub.name = upb_strdup(name);
+ f->subdef_is_symbolic = true;
+ return true;
+}
+
+bool upb_fielddef_issubmsg(const upb_fielddef *f) {
+ return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
+}
+
+bool upb_fielddef_isstring(const upb_fielddef *f) {
+ return upb_fielddef_type(f) == UPB_TYPE_STRING ||
+ upb_fielddef_type(f) == UPB_TYPE_BYTES;
+}
+
+bool upb_fielddef_isseq(const upb_fielddef *f) {
+ return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
+}
+
+bool upb_fielddef_isprimitive(const upb_fielddef *f) {
+ return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
+}
+
+bool upb_fielddef_hassubdef(const upb_fielddef *f) {
+ return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
+}
+
+static bool between(int32_t x, int32_t low, int32_t high) {
+ return x >= low && x <= high;
+}
+
+bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
+bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
+bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
+
+bool upb_fielddef_checkdescriptortype(int32_t type) {
+ return between(type, 1, 18);
+}
+
+/* upb_msgdef *****************************************************************/
+
+static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit,
+ void *closure) {
+ const upb_msgdef *m = (const upb_msgdef*)r;
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
+ upb_fielddef *f = upb_msg_iter_field(&i);
+ visit(r, UPB_UPCAST2(f), 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_UPCAST(m));
+ free(m);
+}
+
+upb_msgdef *upb_msgdef_new(const void *owner) {
+ static const struct upb_refcounted_vtbl vtbl = {visitmsg, freemsg};
+ upb_msgdef *m = malloc(sizeof(*m));
+ if (!m) return NULL;
+ if (!upb_def_init(UPB_UPCAST(m), UPB_DEF_MSG, &vtbl, owner)) goto err2;
+ if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2;
+ if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1;
+ return m;
+
+err1:
+ upb_inttable_uninit(&m->itof);
+err2:
+ free(m);
+ return NULL;
+}
+
+upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) {
+ upb_msgdef *newm = upb_msgdef_new(owner);
+ if (!newm) return NULL;
+ bool ok = upb_def_setfullname(UPB_UPCAST(newm),
+ upb_def_fullname(UPB_UPCAST(m)), NULL);
+ UPB_ASSERT_VAR(ok, ok);
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
+ upb_fielddef *f = upb_fielddef_dup(upb_msg_iter_field(&i), &f);
+ if (!f || !upb_msgdef_addfield(newm, f, &f, NULL)) {
+ upb_msgdef_unref(newm, owner);
+ return NULL;
+ }
+ }
+ return newm;
+}
+
+bool upb_msgdef_isfrozen(const upb_msgdef *m) {
+ return upb_def_isfrozen(UPB_UPCAST(m));
+}
+
+void upb_msgdef_ref(const upb_msgdef *m, const void *owner) {
+ upb_def_ref(UPB_UPCAST(m), owner);
+}
+
+void upb_msgdef_unref(const upb_msgdef *m, const void *owner) {
+ upb_def_unref(UPB_UPCAST(m), owner);
+}
+
+void upb_msgdef_donateref(
+ const upb_msgdef *m, const void *from, const void *to) {
+ upb_def_donateref(UPB_UPCAST(m), from, to);
+}
+
+void upb_msgdef_checkref(const upb_msgdef *m, const void *owner) {
+ upb_def_checkref(UPB_UPCAST(m), owner);
+}
+
+bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status) {
+ upb_def *d = UPB_UPCAST(m);
+ return upb_def_freeze(&d, 1, status);
+}
+
+const char *upb_msgdef_fullname(const upb_msgdef *m) {
+ return upb_def_fullname(UPB_UPCAST(m));
+}
+
+bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname,
+ upb_status *s) {
+ return upb_def_setfullname(UPB_UPCAST(m), fullname, s);
+}
+
+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.
+
+ // Check constraints for all fields before performing any action.
+ 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_msgdef_ntofz(m, upb_fielddef_name(f))) {
+ upb_status_seterrmsg(s, "duplicate field name or number");
+ return false;
+ }
+
+ // Constraint checks ok, perform the action.
+ 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);
+
+ 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;
+ return upb_strtable_lookup2(&m->ntof, name, len, &val) ?
+ upb_value_getptr(val) : NULL;
+}
+
+int upb_msgdef_numfields(const upb_msgdef *m) {
+ return upb_strtable_count(&m->ntof);
+}
+
+void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) {
+ upb_inttable_begin(iter, &m->itof);
+}
+
+void upb_msg_next(upb_msg_iter *iter) { upb_inttable_next(iter); }
+
+bool upb_msg_done(const upb_msg_iter *iter) { return upb_inttable_done(iter); }
+
+upb_fielddef *upb_msg_iter_field(const upb_msg_iter *iter) {
+ return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter));
+}
+
+void upb_msg_iter_setdone(upb_msg_iter *iter) {
+ upb_inttable_iter_setdone(iter);
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * TODO(haberman): it's unclear whether a lot of the consistency checks should
+ * assert() or return false.
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+
+
+// Defined for the sole purpose of having a unique pointer value for
+// 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);
+ free(h->sub);
+ free(h);
+}
+
+static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit,
+ void *closure) {
+ const upb_handlers *h = (const upb_handlers*)r;
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, h->msg); !upb_msg_done(&i); upb_msg_next(&i)) {
+ upb_fielddef *f = upb_msg_iter_field(&i);
+ if (!upb_fielddef_issubmsg(f)) continue;
+ const upb_handlers *sub = upb_handlers_getsubhandlers(h, f);
+ if (sub) visit(r, UPB_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_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.
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
+ upb_fielddef *f = upb_msg_iter_field(&i);
+ if (!upb_fielddef_issubmsg(f)) continue;
+
+ const upb_msgdef *subdef = upb_downcast_msgdef(upb_fielddef_subdef(f));
+ upb_value subm_ent;
+ 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_)
+
+static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f,
+ upb_handlertype_t type) {
+ upb_selector_t sel;
+ 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;
+ }
+ return sel;
+}
+
+static upb_selector_t handlers_getsel(upb_handlers *h, const upb_fielddef *f,
+ upb_handlertype_t type) {
+ int32_t sel = trygetsel(h, f, type);
+ assert(sel >= 0);
+ return sel;
+}
+
+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_;
+}
+
+static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f,
+ upb_handlertype_t type, upb_func *func,
+ upb_handlerattr *attr) {
+ 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_handlerattr set_attr = UPB_HANDLERATTR_INITIALIZER;
+ if (attr) {
+ set_attr = *attr;
+ }
+
+ // Check that the given closure type matches the closure type that has been
+ // established for this context (if any).
+ const void *closure_type = upb_handlerattr_closuretype(&set_attr);
+ const void **context_closure_type;
+
+ if (type == UPB_HANDLER_STRING) {
+ context_closure_type = returntype(h, f, UPB_HANDLER_STARTSTR);
+ } else if (f && upb_fielddef_isseq(f) &&
+ type != UPB_HANDLER_STARTSEQ &&
+ type != UPB_HANDLER_ENDSEQ) {
+ context_closure_type = returntype(h, f, UPB_HANDLER_STARTSEQ);
+ } else {
+ context_closure_type = &h->top_closure_type;
+ }
+
+ if (closure_type && *context_closure_type &&
+ closure_type != *context_closure_type) {
+ // TODO(haberman): better message for debugging.
+ upb_status_seterrmsg(&h->status_, "closure type does not match");
+ return false;
+ }
+
+ if (closure_type)
+ *context_closure_type = closure_type;
+
+ // 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);
+ 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);
+ }
+
+ h->table[sel].func = (upb_func*)func;
+ h->table[sel].attr = set_attr;
+ return true;
+}
+
+// Returns the effective closure type for this handler (which will propagate
+// from outer frames if this frame has no START* handler). Not implemented for
+// UPB_HANDLER_STRING at the moment since this is not needed. Returns NULL is
+// the effective closure type is unspecified (either no handler was registered
+// to specify it or the handler that was registered did not specify the closure
+// type).
+const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f,
+ upb_handlertype_t type) {
+ assert(type != UPB_HANDLER_STRING);
+ const void *ret = h->top_closure_type;
+ upb_selector_t sel;
+ if (upb_fielddef_isseq(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);
+ }
+
+ if (type == UPB_HANDLER_STRING &&
+ h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSTR)].func) {
+ ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
+ }
+
+ // 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);
+ // }
+
+ return ret;
+}
+
+// Checks whether the START* handler specified by f & type is missing even
+// though it is required to convert the established type of an outer frame
+// ("closure_type") into the established type of an inner frame (represented in
+// the return closure type of this handler's attr.
+bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type,
+ upb_status *status) {
+ upb_selector_t sel = handlers_getsel(h, f, type);
+ if (h->table[sel].func) return true;
+ const void *closure_type = effective_closure_type(h, f, type);
+ const upb_handlerattr *attr = &h->table[sel].attr;
+ const void *return_closure_type = upb_handlerattr_returnclosuretype(attr);
+ 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 ***********************************************************/
+
+bool upb_handlers_isfrozen(const upb_handlers *h) {
+ return upb_refcounted_isfrozen(UPB_UPCAST(h));
+}
+
+void upb_handlers_ref(const upb_handlers *h, const void *owner) {
+ upb_refcounted_ref(UPB_UPCAST(h), owner);
+}
+
+void upb_handlers_unref(const upb_handlers *h, const void *owner) {
+ upb_refcounted_unref(UPB_UPCAST(h), owner);
+}
+
+void upb_handlers_donateref(
+ const upb_handlers *h, const void *from, const void *to) {
+ upb_refcounted_donateref(UPB_UPCAST(h), from, to);
+}
+
+void upb_handlers_checkref(const upb_handlers *h, const void *owner) {
+ upb_refcounted_checkref(UPB_UPCAST(h), owner);
+}
+
+upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) {
+ assert(upb_msgdef_isfrozen(md));
+
+ int extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1);
+ upb_handlers *h = calloc(sizeof(*h) + extra, 1);
+ if (!h) return NULL;
+
+ h->msg = md;
+ upb_msgdef_ref(h->msg, h);
+ upb_status_clear(&h->status_);
+ h->sub = calloc(md->submsg_field_count, sizeof(*h->sub));
+ if (!h->sub) goto oom;
+ if (!upb_refcounted_init(UPB_UPCAST(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_UPCAST(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;
+ state.callback = callback;
+ state.closure = closure;
+ if (!upb_inttable_init(&state.tab, UPB_CTYPE_PTR)) return NULL;
+
+ upb_handlers *ret = newformsg(m, owner, &state);
+
+ upb_inttable_uninit(&state.tab);
+ if (!ret) return NULL;
+
+ upb_refcounted *r = UPB_UPCAST(ret);
+ bool ok = upb_refcounted_freeze(&r, 1, NULL, UPB_MAX_HANDLER_DEPTH);
+ UPB_ASSERT_VAR(ok, ok);
+
+ return ret;
+}
+
+const upb_status *upb_handlers_status(upb_handlers *h) {
+ assert(!upb_handlers_isfrozen(h));
+ return &h->status_;
+}
+
+void upb_handlers_clearerr(upb_handlers *h) {
+ assert(!upb_handlers_isfrozen(h));
+ upb_status_clear(&h->status_);
+}
+
+#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); \
+ }
+
+SETTER(int32, upb_int32_handlerfunc*, UPB_HANDLER_INT32);
+SETTER(int64, upb_int64_handlerfunc*, UPB_HANDLER_INT64);
+SETTER(uint32, upb_uint32_handlerfunc*, UPB_HANDLER_UINT32);
+SETTER(uint64, upb_uint64_handlerfunc*, UPB_HANDLER_UINT64);
+SETTER(float, upb_float_handlerfunc*, UPB_HANDLER_FLOAT);
+SETTER(double, upb_double_handlerfunc*, UPB_HANDLER_DOUBLE);
+SETTER(bool, upb_bool_handlerfunc*, UPB_HANDLER_BOOL);
+SETTER(startstr, upb_startstr_handlerfunc*, UPB_HANDLER_STARTSTR);
+SETTER(string, upb_string_handlerfunc*, UPB_HANDLER_STRING);
+SETTER(endstr, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSTR);
+SETTER(startseq, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSEQ);
+SETTER(startsubmsg, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSUBMSG);
+SETTER(endsubmsg, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSUBMSG);
+SETTER(endseq, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSEQ);
+
+#undef SETTER
+
+bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
+ 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) {
+ assert(!upb_handlers_isfrozen(h));
+ return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32,
+ (upb_func *)func, attr);
+}
+
+bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
+ const upb_handlers *sub) {
+ assert(sub);
+ assert(!upb_handlers_isfrozen(h));
+ assert(upb_fielddef_issubmsg(f));
+ if (SUBH_F(h, f)) return false; // Can't reset.
+ if (UPB_UPCAST(upb_handlers_msgdef(sub)) != upb_fielddef_subdef(f)) {
+ return false;
+ }
+ SUBH_F(h, f) = sub;
+ upb_ref2(sub, h);
+ return true;
+}
+
+const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
+ const upb_fielddef *f) {
+ assert(upb_fielddef_issubmsg(f));
+ return SUBH_F(h, f);
+}
+
+bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t sel,
+ upb_handlerattr *attr) {
+ if (!upb_handlers_gethandler(h, sel))
+ return false;
+ *attr = h->table[sel].attr;
+ return true;
+}
+
+const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
+ upb_selector_t sel) {
+ // STARTSUBMSG selector in sel is the field's selector base.
+ return SUBH(h, sel - UPB_STATIC_SELECTOR_COUNT);
+}
+
+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) {
+ if (upb_inttable_lookupptr(&h->cleanup_, p, NULL)) {
+ return false;
+ }
+ bool ok = upb_inttable_insertptr(&h->cleanup_, p, upb_value_fptr(func));
+ UPB_ASSERT_VAR(ok, 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.
+ for (int i = 0; i < n; i++) {
+ 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.
+ upb_msg_iter j;
+ for(upb_msg_begin(&j, h->msg); !upb_msg_done(&j); upb_msg_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;
+}
+
+upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) {
+ switch (upb_fielddef_type(f)) {
+ case UPB_TYPE_INT32:
+ case UPB_TYPE_ENUM: return UPB_HANDLER_INT32;
+ case UPB_TYPE_INT64: return UPB_HANDLER_INT64;
+ case UPB_TYPE_UINT32: return UPB_HANDLER_UINT32;
+ case UPB_TYPE_UINT64: return UPB_HANDLER_UINT64;
+ case UPB_TYPE_FLOAT: return UPB_HANDLER_FLOAT;
+ case UPB_TYPE_DOUBLE: return UPB_HANDLER_DOUBLE;
+ case UPB_TYPE_BOOL: return UPB_HANDLER_BOOL;
+ default: assert(false); return -1; // Invalid input.
+ }
+}
+
+bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
+ upb_selector_t *s) {
+ switch (type) {
+ case UPB_HANDLER_INT32:
+ case UPB_HANDLER_INT64:
+ case UPB_HANDLER_UINT32:
+ case UPB_HANDLER_UINT64:
+ case UPB_HANDLER_FLOAT:
+ case UPB_HANDLER_DOUBLE:
+ case UPB_HANDLER_BOOL:
+ if (!upb_fielddef_isprimitive(f) ||
+ upb_handlers_getprimitivehandlertype(f) != type)
+ return false;
+ *s = f->selector_base;
+ break;
+ case UPB_HANDLER_STRING:
+ if (upb_fielddef_isstring(f)) {
+ *s = f->selector_base;
+ } else if (upb_fielddef_lazy(f)) {
+ *s = f->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;
+ } else {
+ return false;
+ }
+ break;
+ case UPB_HANDLER_ENDSTR:
+ if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) {
+ *s = f->selector_base + 2;
+ } else {
+ return false;
+ }
+ break;
+ case UPB_HANDLER_STARTSEQ:
+ if (!upb_fielddef_isseq(f)) return false;
+ *s = f->selector_base - 2;
+ break;
+ case UPB_HANDLER_ENDSEQ:
+ if (!upb_fielddef_isseq(f)) return false;
+ *s = f->selector_base - 1;
+ break;
+ case UPB_HANDLER_STARTSUBMSG:
+ if (!upb_fielddef_issubmsg(f)) return false;
+ // Selectors for STARTSUBMSG are at the beginning of the table so that the
+ // 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;
+ break;
+ case UPB_HANDLER_ENDSUBMSG:
+ if (!upb_fielddef_issubmsg(f)) return false;
+ *s = f->selector_base;
+ break;
+ }
+ assert(*s < upb_fielddef_containingtype(f)->selector_count);
+ return true;
+}
+
+uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
+ return upb_fielddef_isseq(f) ? 2 : 0;
+}
+
+uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
+ uint32_t ret = 1;
+ if (upb_fielddef_isseq(f)) ret += 2; // STARTSEQ/ENDSEQ
+ if (upb_fielddef_isstring(f)) ret += 2; // [STRING]/STARTSTR/ENDSTR
+ if (upb_fielddef_issubmsg(f)) {
+ // ENDSUBMSG (STARTSUBMSG is at table beginning)
+ ret += 0;
+ if (upb_fielddef_lazy(f)) {
+ // STARTSTR/ENDSTR/STRING (for lazy)
+ ret += 3;
+ }
+ }
+ return ret;
+}
+
+
+/* upb_handlerattr ************************************************************/
+
+void upb_handlerattr_init(upb_handlerattr *attr) {
+ upb_handlerattr from = UPB_HANDLERATTR_INITIALIZER;
+ memcpy(attr, &from, sizeof(*attr));
+}
+
+void upb_handlerattr_uninit(upb_handlerattr *attr) {
+ UPB_UNUSED(attr);
+}
+
+bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd) {
+ attr->handler_data_ = hd;
+ return true;
+}
+
+bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type) {
+ attr->closure_type_ = type;
+ return true;
+}
+
+const void *upb_handlerattr_closuretype(const upb_handlerattr *attr) {
+ return attr->closure_type_;
+}
+
+bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
+ const void *type) {
+ attr->return_closure_type_ = type;
+ return true;
+}
+
+const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr) {
+ return attr->return_closure_type_;
+}
+
+bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok) {
+ attr->alwaysok_ = alwaysok;
+ return true;
+}
+
+bool upb_handlerattr_alwaysok(const upb_handlerattr *attr) {
+ return attr->alwaysok_;
+}
+
+/* upb_bufhandle **************************************************************/
+
+size_t upb_bufhandle_objofs(const upb_bufhandle *h) {
+ return h->objofs_;
+}
+
+/* upb_byteshandler ***********************************************************/
+
+void upb_byteshandler_init(upb_byteshandler* h) {
+ memset(h, 0, sizeof(*h));
+}
+
+// For when we support handlerfree callbacks.
+void upb_byteshandler_uninit(upb_byteshandler* h) {
+ UPB_UNUSED(h);
+}
+
+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;
+ 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;
+ 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;
+ return true;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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 <setjmp.h>
+#include <stdlib.h>
+
+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 <Windows.h>
+
+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 immediately fail.
+#define CHECK_OOM(predicate) if (!(predicate)) { assert(predicate); exit(1); }
+
+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 = malloc(sizeof(*ret));
+ CHECK_OOM(ret);
+ ret->count = 1;
+ ret->is_ref2 = is_ref2;
+ return ret;
+}
+
+static void track(const upb_refcounted *r, const void *owner, bool ref2) {
+ assert(owner);
+ if (owner == UPB_UNTRACKED_REF) return;
+
+ upb_lock();
+ upb_value v;
+ 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.
+ assert(ref2);
+ assert(ref->is_ref2);
+ ref->count++;
+ } else {
+ trackedref *ref = trackedref_new(ref2);
+ bool ok = upb_inttable_insertptr(r->refs, owner, upb_value_ptr(ref));
+ CHECK_OOM(ok);
+ 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;
+ assert(!upb_inttable_lookupptr(from->ref2s, r, NULL));
+ ok = upb_inttable_insertptr(from->ref2s, r, upb_value_ptr(NULL));
+ CHECK_OOM(ok);
+ }
+ }
+ upb_unlock();
+}
+
+static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
+ assert(owner);
+ if (owner == UPB_UNTRACKED_REF) return;
+
+ upb_lock();
+ upb_value v;
+ bool 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_VAR(found, found);
+ trackedref *ref = upb_value_getptr(v);
+ 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);
+ assert(removed);
+ }
+ }
+ upb_unlock();
+}
+
+static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
+ upb_lock();
+ upb_value v;
+ bool found = upb_inttable_lookupptr(r->refs, owner, &v);
+ UPB_ASSERT_VAR(found, found);
+ trackedref *ref = upb_value_getptr(v);
+ assert(ref->is_ref2 == ref2);
+ upb_unlock();
+}
+
+// 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_lock();
+ upb_inttable_iter i;
+ upb_inttable_begin(&i, owner->ref2s);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ upb_refcounted *to = (upb_refcounted*)upb_inttable_iter_key(&i);
+
+ // To get the count we need to look in the target's table.
+ upb_value v;
+ bool found = upb_inttable_lookupptr(to->refs, owner, &v);
+ assert(found);
+ trackedref *ref = upb_value_getptr(v);
+ upb_value count = upb_value_int32(ref->count);
+
+ bool ok = upb_inttable_insertptr(tab, to, count);
+ CHECK_OOM(ok);
+ }
+ upb_unlock();
+}
+
+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;
+ assert(obj == s->obj);
+ assert(subobj);
+ upb_inttable *ref2 = &s->ref2;
+ upb_value v;
+ bool 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.
+ assert(removed);
+ int32_t newcount = upb_value_getint32(v) - 1;
+ if (newcount > 0) {
+ upb_inttable_insert(ref2, (uintptr_t)subobj, upb_value_int32(newcount));
+ }
+}
+
+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;
+ bool ok = upb_inttable_init(&state.ref2, UPB_CTYPE_INT32);
+ CHECK_OOM(ok);
+ getref2s(r, &state.ref2);
+
+ // This should visit any children in the ref2 table.
+ if (r->vtbl->visit) r->vtbl->visit(r, visit_check, &state);
+
+ // This assertion will fail if the visit() function missed any children.
+ assert(upb_inttable_count(&state.ref2) == 0);
+ upb_inttable_uninit(&state.ref2);
+ if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
+}
+
+static bool trackinit(upb_refcounted *r) {
+ r->refs = malloc(sizeof(*r->refs));
+ r->ref2s = malloc(sizeof(*r->ref2s));
+ if (!r->refs || !r->ref2s) goto err1;
+
+ if (!upb_inttable_init(r->refs, UPB_CTYPE_PTR)) goto err1;
+ if (!upb_inttable_init(r->ref2s, UPB_CTYPE_PTR)) goto err2;
+ return true;
+
+err2:
+ upb_inttable_uninit(r->refs);
+err1:
+ free(r->refs);
+ free(r->ref2s);
+ return false;
+}
+
+static void trackfree(const upb_refcounted *r) {
+ upb_inttable_uninit(r->refs);
+ upb_inttable_uninit(r->ref2s);
+ free(r->refs);
+ free(r->ref2s);
+}
+
+#else
+
+static void track(const upb_refcounted *r, const void *owner, bool ref2) {
+ UPB_UNUSED(r);
+ UPB_UNUSED(owner);
+ UPB_UNUSED(ref2);
+}
+
+static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
+ UPB_UNUSED(r);
+ UPB_UNUSED(owner);
+ UPB_UNUSED(ref2);
+}
+
+static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
+ UPB_UNUSED(r);
+ UPB_UNUSED(owner);
+ UPB_UNUSED(ref2);
+}
+
+static bool trackinit(upb_refcounted *r) {
+ UPB_UNUSED(r);
+ return true;
+}
+
+static void trackfree(const upb_refcounted *r) {
+ UPB_UNUSED(r);
+}
+
+static void visit(const upb_refcounted *r, upb_refcounted_visit *v,
+ void *closure) {
+ if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
+}
+
+#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.
+
+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;
+
+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 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 uint64_t getattr(const tarjan *t, const upb_refcounted *r) {
+ upb_value v;
+ bool found = upb_inttable_lookupptr(&t->objattr, r, &v);
+ UPB_ASSERT_VAR(found, found);
+ return upb_value_getuint64(v);
+}
+
+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 color_t color(tarjan *t, const upb_refcounted *r) {
+ return trygetattr(t, r) & 0x3; // Color is always stored in the low 2 bits.
+}
+
+static void set_gray(tarjan *t, const upb_refcounted *r) {
+ assert(color(t, r) == BLACK);
+ setattr(t, r, GRAY);
+}
+
+// Pushes an obj onto the Tarjan stack and sets it to GREEN.
+static void push(tarjan *t, const upb_refcounted *r) {
+ 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);
+ }
+ upb_inttable_push(&t->stack, upb_value_ptr((void*)r));
+}
+
+// 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));
+ 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;
+}
+
+static void tarjan_newgroup(tarjan *t) {
+ uint32_t *group = malloc(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))) {
+ free(group);
+ oom(t);
+ }
+ *group = 0;
+}
+
+static uint32_t idx(tarjan *t, const upb_refcounted *r) {
+ assert(color(t, r) == GREEN);
+ return (getattr(t, r) >> 2) & 0x7FFFFFFF;
+}
+
+static uint32_t lowlink(tarjan *t, const upb_refcounted *r) {
+ if (color(t, r) == GREEN) {
+ return getattr(t, r) >> 33;
+ } else {
+ return UINT32_MAX;
+ }
+}
+
+static void set_lowlink(tarjan *t, const upb_refcounted *r, uint32_t lowlink) {
+ assert(color(t, r) == GREEN);
+ setattr(t, r, ((uint64_t)lowlink << 33) | (getattr(t, r) & 0x1FFFFFFFF));
+}
+
+static uint32_t *group(tarjan *t, upb_refcounted *r) {
+ assert(color(t, r) == WHITE);
+ uint64_t groupnum = getattr(t, r) >> 8;
+ upb_value v;
+ bool found = upb_inttable_lookup(&t->groups, groupnum, &v);
+ UPB_ASSERT_VAR(found, found);
+ return upb_value_getptr(v);
+}
+
+// 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) {
+ assert(color(t, r) == WHITE);
+ uint64_t leader_slot = (getattr(t, r) >> 8) + 1;
+ upb_value v;
+ bool found = upb_inttable_lookup(&t->groups, leader_slot, &v);
+ UPB_ASSERT_VAR(found, found);
+ if (upb_value_getptr(v)) {
+ return upb_value_getptr(v);
+ } else {
+ upb_inttable_remove(&t->groups, leader_slot, NULL);
+ upb_inttable_insert(&t->groups, leader_slot, upb_value_ptr(r));
+ return r;
+ }
+}
+
+
+// Tarjan's algorithm //////////////////////////////////////////////////////////
+
+// See:
+// http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
+static void do_tarjan(const upb_refcounted *obj, tarjan *t);
+
+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;
+}
+
+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);
+ }
+
+ push(t, obj);
+ visit(obj, tarjan_visit, t);
+ if (lowlink(t, obj) == idx(t, obj)) {
+ tarjan_newgroup(t);
+ while (pop(t) != obj)
+ ;
+ }
+}
+
+
+// freeze() ////////////////////////////////////////////////////////////////////
+
+static void crossref(const upb_refcounted *r, const upb_refcounted *subobj,
+ void *_t) {
+ tarjan *t = _t;
+ 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);
+ }
+}
+
+static bool freeze(upb_refcounted *const*roots, int n, upb_status *s,
+ int maxdepth) {
+ volatile bool ret = false;
+
+ // 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 (int 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_iter i;
+ upb_inttable_begin(&i, &t.objattr);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&i);
+ // 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) {
+ // Remove from old group.
+ upb_refcounted *move = obj->next;
+ if (obj == move) {
+ // Removing the last object from a group.
+ assert(*obj->group == obj->individual_count);
+ free(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.
+ assert(*move->group >= move->individual_count);
+ *move->group -= move->individual_count;
+ }
+
+ // Add to new group.
+ upb_refcounted *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.
+ 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(&i, &t.objattr);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&i);
+ visit(obj, crossref, &t);
+ }
+
+ // 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(&i, &t.objattr);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&i);
+ if (obj->group == NULL || *obj->group == 0) {
+ if (obj->group) {
+ // 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).
+ free(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()).
+ upb_refcounted *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);
+ }
+ freeobj(obj);
+ }
+ }
+
+err4:
+ if (!ret) {
+ upb_inttable_begin(&i, &t.groups);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i))
+ free(upb_value_getptr(upb_inttable_iter_value(&i)));
+ }
+ upb_inttable_uninit(&t.groups);
+err3:
+ upb_inttable_uninit(&t.stack);
+err2:
+ upb_inttable_uninit(&t.objattr);
+err1:
+ return ret;
+}
+
+
+/* Misc internal functions ***************************************************/
+
+static bool merged(const upb_refcounted *r, const upb_refcounted *r2) {
+ return r->group == r2->group;
+}
+
+static void merge(upb_refcounted *r, upb_refcounted *from) {
+ if (merged(r, from)) return;
+ *r->group += *from->group;
+ free(from->group);
+ upb_refcounted *base = from;
+
+ // 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:
+ // ftp://www.ncedc.org/outgoing/geomorph/dino/orals/p245-tarjan.pdf
+ do { from->group = r->group; } while ((from = from->next) != base);
+
+ // Merge the two circularly linked lists by swapping their next pointers.
+ upb_refcounted *tmp = r->next;
+ r->next = base->next;
+ base->next = tmp;
+}
+
+static void unref(const upb_refcounted *r);
+
+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)) {
+ assert(subobj->is_frozen);
+ unref(subobj);
+ }
+}
+
+static void unref(const upb_refcounted *r) {
+ if (unrefgroup(r->group)) {
+ free(r->group);
+
+ // In two passes, since release_ref2 needs a guarantee that any subobjs
+ // are alive.
+ const upb_refcounted *o = r;
+ do { visit(o, release_ref2, NULL); } while((o = o->next) != r);
+
+ o = r;
+ do {
+ const upb_refcounted *next = o->next;
+ assert(o->is_frozen || o->individual_count == 0);
+ freeobj((upb_refcounted*)o);
+ o = next;
+ } while(o != r);
+ }
+}
+
+static void freeobj(upb_refcounted *o) {
+ trackfree(o);
+ o->vtbl->free((upb_refcounted*)o);
+}
+
+
+/* Public interface ***********************************************************/
+
+bool upb_refcounted_init(upb_refcounted *r,
+ const struct upb_refcounted_vtbl *vtbl,
+ const void *owner) {
+ r->next = r;
+ r->vtbl = vtbl;
+ r->individual_count = 0;
+ r->is_frozen = false;
+ r->group = malloc(sizeof(*r->group));
+ if (!r->group) return false;
+ *r->group = 0;
+ if (!trackinit(r)) {
+ free(r->group);
+ return false;
+ }
+ upb_refcounted_ref(r, owner);
+ return true;
+}
+
+bool upb_refcounted_isfrozen(const upb_refcounted *r) {
+ return r->is_frozen;
+}
+
+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);
+}
+
+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_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from) {
+ assert(!from->is_frozen); // Non-const pointer implies this.
+ track(r, from, true);
+ if (r->is_frozen) {
+ refgroup(r->group);
+ } else {
+ merge((upb_refcounted*)r, from);
+ }
+}
+
+void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from) {
+ assert(!from->is_frozen); // Non-const pointer implies this.
+ untrack(r, from, true);
+ if (r->is_frozen) {
+ unref(r);
+ } else {
+ assert(merged(r, from));
+ }
+}
+
+void upb_refcounted_donateref(
+ const upb_refcounted *r, const void *from, const void *to) {
+ assert(from != to);
+ if (to != NULL)
+ upb_refcounted_ref(r, to);
+ if (from != NULL)
+ upb_refcounted_unref(r, from);
+}
+
+void upb_refcounted_checkref(const upb_refcounted *r, const void *owner) {
+ checkref(r, owner, false);
+}
+
+bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
+ int maxdepth) {
+ for (int i = 0; i < n; i++) {
+ assert(!roots[i]->is_frozen);
+ }
+ return freeze(roots, n, s, maxdepth);
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2013 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+
+#include <stdlib.h>
+
+// Fallback implementation if the shim is not specialized by the JIT.
+#define SHIM_WRITER(type, ctype) \
+ bool upb_shim_set ## type (void *c, const void *hd, ctype val) { \
+ uint8_t *m = c; \
+ const upb_shim_data *d = hd; \
+ if (d->hasbit > 0) \
+ *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \
+ *(ctype*)&m[d->offset] = val; \
+ return true; \
+ } \
+
+SHIM_WRITER(double, double)
+SHIM_WRITER(float, float)
+SHIM_WRITER(int32, int32_t)
+SHIM_WRITER(int64, int64_t)
+SHIM_WRITER(uint32, uint32_t)
+SHIM_WRITER(uint64, uint64_t)
+SHIM_WRITER(bool, bool)
+#undef SHIM_WRITER
+
+bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset,
+ int32_t hasbit) {
+ upb_shim_data *d = malloc(sizeof(*d));
+ if (!d) return false;
+ d->offset = offset;
+ d->hasbit = hasbit;
+
+ upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlerattr_sethandlerdata(&attr, d);
+ upb_handlerattr_setalwaysok(&attr, true);
+ upb_handlers_addcleanup(h, d, free);
+
+#define TYPE(u, l) \
+ case UPB_TYPE_##u: \
+ ok = upb_handlers_set##l(h, f, upb_shim_set##l, &attr); break;
+
+ bool 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: assert(false); break;
+ }
+#undef TYPE
+
+ upb_handlerattr_uninit(&attr);
+ return ok;
+}
+
+const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s,
+ upb_fieldtype_t *type) {
+ upb_func *f = upb_handlers_gethandler(h, s);
+
+ if ((upb_int64_handlerfunc*)f == upb_shim_setint64) {
+ *type = UPB_TYPE_INT64;
+ } else if ((upb_int32_handlerfunc*)f == upb_shim_setint32) {
+ *type = UPB_TYPE_INT32;
+ } else if ((upb_uint64_handlerfunc*)f == upb_shim_setuint64) {
+ *type = UPB_TYPE_UINT64;
+ } else if ((upb_uint32_handlerfunc*)f == upb_shim_setuint32) {
+ *type = UPB_TYPE_UINT32;
+ } else if ((upb_double_handlerfunc*)f == upb_shim_setdouble) {
+ *type = UPB_TYPE_DOUBLE;
+ } else if ((upb_float_handlerfunc*)f == upb_shim_setfloat) {
+ *type = UPB_TYPE_FLOAT;
+ } else if ((upb_bool_handlerfunc*)f == upb_shim_setbool) {
+ *type = UPB_TYPE_BOOL;
+ } else {
+ return NULL;
+ }
+
+ return (const upb_shim_data*)upb_handlers_gethandlerdata(h, s);
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2008-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+
+bool upb_symtab_isfrozen(const upb_symtab *s) {
+ return upb_refcounted_isfrozen(UPB_UPCAST(s));
+}
+
+void upb_symtab_ref(const upb_symtab *s, const void *owner) {
+ upb_refcounted_ref(UPB_UPCAST(s), owner);
+}
+
+void upb_symtab_unref(const upb_symtab *s, const void *owner) {
+ upb_refcounted_unref(UPB_UPCAST(s), owner);
+}
+
+void upb_symtab_donateref(
+ const upb_symtab *s, const void *from, const void *to) {
+ upb_refcounted_donateref(UPB_UPCAST(s), from, to);
+}
+
+void upb_symtab_checkref(const upb_symtab *s, const void *owner) {
+ upb_refcounted_checkref(UPB_UPCAST(s), owner);
+}
+
+static void upb_symtab_free(upb_refcounted *r) {
+ upb_symtab *s = (upb_symtab*)r;
+ 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);
+ free(s);
+}
+
+
+upb_symtab *upb_symtab_new(const void *owner) {
+ static const struct upb_refcounted_vtbl vtbl = {NULL, &upb_symtab_free};
+ upb_symtab *s = malloc(sizeof(*s));
+ upb_refcounted_init(UPB_UPCAST(s), &vtbl, owner);
+ upb_strtable_init(&s->symtab, UPB_CTYPE_PTR);
+ return s;
+}
+
+void upb_symtab_freeze(upb_symtab *s) {
+ assert(!upb_symtab_isfrozen(s));
+ upb_refcounted *r = UPB_UPCAST(s);
+ // The symtab does not take ref2's (see refcounted.h) on the defs, because
+ // defs cannot refer back to the table and therefore cannot create cycles. So
+ // 0 will suffice for maxdepth here.
+ bool ok = upb_refcounted_freeze(&r, 1, NULL, 0);
+ UPB_ASSERT_VAR(ok, ok);
+}
+
+const upb_def *upb_symtab_lookup(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;
+}
+
+const upb_msgdef *upb_symtab_lookupmsg(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_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) ?
+ 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;
+ assert(false);
+ return 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;
+}
+
+// Searches def and its children to find defs that have the same name as any
+// def in "addtab." Returns true if any where found, and as a side-effect adds
+// duplicates of these defs into addtab.
+//
+// We use a modified depth-first traversal that traverses each SCC (which we
+// already computed) as if it were a single node. This allows us to traverse
+// the possibly-cyclic graph as if it were a DAG and to dup the correct set of
+// nodes with O(n) time.
+static bool upb_resolve_dfs(const upb_def *def, upb_strtable *addtab,
+ const void *new_owner, upb_inttable *seen,
+ upb_status *s) {
+ // Memoize results of this function for efficiency (since we're traversing a
+ // DAG this is not needed to limit the depth of the search).
+ upb_value v;
+ if (upb_inttable_lookup(seen, (uintptr_t)def, &v))
+ return upb_value_getbool(v);
+
+ // Visit submessages for all messages in the SCC.
+ bool need_dup = false;
+ const upb_def *base = def;
+ do {
+ assert(upb_def_isfrozen(def));
+ if (def->type == UPB_DEF_FIELD) continue;
+ upb_value v;
+ if (upb_strtable_lookup(addtab, upb_def_fullname(def), &v)) {
+ need_dup = true;
+ }
+
+ // For messages, continue the recursion by visiting all subdefs.
+ const upb_msgdef *m = upb_dyncast_msgdef(def);
+ if (m) {
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
+ upb_fielddef *f = upb_msg_iter_field(&i);
+ if (!upb_fielddef_hassubdef(f)) continue;
+ // |= to avoid short-circuit; we need its side-effects.
+ need_dup |= upb_resolve_dfs(
+ upb_fielddef_subdef(f), addtab, new_owner, seen, s);
+ if (!upb_ok(s)) return false;
+ }
+ }
+ } while ((def = (upb_def*)def->base.next) != base);
+
+ if (need_dup) {
+ // Dup any defs that don't already have entries in addtab.
+ def = base;
+ do {
+ if (def->type == UPB_DEF_FIELD) continue;
+ const char *name = upb_def_fullname(def);
+ if (!upb_strtable_lookup(addtab, name, NULL)) {
+ upb_def *newdef = upb_def_dup(def, new_owner);
+ if (!newdef) goto oom;
+ newdef->came_from_user = false;
+ if (!upb_strtable_insert(addtab, name, upb_value_ptr(newdef)))
+ goto oom;
+ }
+ } while ((def = (upb_def*)def->base.next) != base);
+ }
+
+ upb_inttable_insert(seen, (uintptr_t)def, upb_value_bool(need_dup));
+ return need_dup;
+
+oom:
+ upb_status_seterrmsg(s, "out of memory");
+ return false;
+}
+
+// TODO(haberman): we need a lot more testing of error conditions.
+// The came_from_user stuff in particular is not tested.
+bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor,
+ upb_status *status) {
+ assert(!upb_symtab_isfrozen(s));
+ upb_def **add_defs = NULL;
+ upb_strtable addtab;
+ if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) {
+ upb_status_seterrmsg(status, "out of memory");
+ return false;
+ }
+
+ // Add new defs to our "add" set.
+ for (int i = 0; i < n; i++) {
+ upb_def *def = defs[i];
+ if (upb_def_isfrozen(def)) {
+ upb_status_seterrmsg(status, "added defs must be mutable");
+ goto err;
+ }
+ assert(!upb_def_isfrozen(def));
+ const char *fullname = upb_def_fullname(def);
+ if (!fullname) {
+ upb_status_seterrmsg(
+ status, "Anonymous defs cannot be added to a symtab");
+ goto err;
+ }
+
+ upb_fielddef *f = upb_dyncast_fielddef_mutable(def);
+
+ 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;
+ }
+ // We need this to back out properly, because if there is a failure we
+ // need to donate the ref back to the caller.
+ def->came_from_user = true;
+ upb_def_donateref(def, ref_donor, s);
+ if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def)))
+ goto oom_err;
+ }
+ }
+
+ // Add standalone fielddefs (ie. extensions) to the appropriate messages.
+ // If the appropriate message only exists in the existing symtab, duplicate
+ // it so we have a mutable copy we can add the fields to.
+ for (int i = 0; i < n; i++) {
+ upb_def *def = defs[i];
+ upb_fielddef *f = upb_dyncast_fielddef_mutable(def);
+ if (!f) continue;
+ const char *msgname = upb_fielddef_containingtypename(f);
+ // We validated this earlier in this function.
+ assert(msgname);
+
+ // If the extendee name is absolutely qualified, move past the initial ".".
+ // TODO(haberman): it is not obvious what it would mean if this was not
+ // absolutely qualified.
+ if (msgname[0] == '.') {
+ msgname++;
+ }
+
+ upb_value v;
+ upb_msgdef *m;
+ if (upb_strtable_lookup(&addtab, msgname, &v)) {
+ // Extendee is in the set of defs the user asked us to add.
+ m = upb_value_getptr(v);
+ } else {
+ // Need to find and dup the extendee from the existing symtab.
+ const upb_msgdef *frozen_m = upb_symtab_lookupmsg(s, msgname);
+ if (!frozen_m) {
+ upb_status_seterrf(status,
+ "Tried to extend message %s that does not exist "
+ "in this SymbolTable.",
+ msgname);
+ goto err;
+ }
+ m = upb_msgdef_dup(frozen_m, s);
+ if (!m) goto oom_err;
+ if (!upb_strtable_insert(&addtab, msgname, upb_value_ptr(m))) {
+ upb_msgdef_unref(m, s);
+ goto oom_err;
+ }
+ }
+
+ if (!upb_msgdef_addfield(m, f, ref_donor, status)) {
+ goto err;
+ }
+ }
+
+ // Add dups of any existing def that can reach a def with the same name as
+ // anything in our "add" set.
+ upb_inttable seen;
+ if (!upb_inttable_init(&seen, UPB_CTYPE_BOOL)) goto oom_err;
+ upb_strtable_iter i;
+ upb_strtable_begin(&i, &s->symtab);
+ for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i));
+ upb_resolve_dfs(def, &addtab, s, &seen, status);
+ if (!upb_ok(status)) goto err;
+ }
+ upb_inttable_uninit(&seen);
+
+ // Now using the table, resolve symbolic references for subdefs.
+ upb_strtable_begin(&i, &addtab);
+ for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i));
+ upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
+ if (!m) continue;
+ // Type names are resolved relative to the message in which they appear.
+ const char *base = upb_msgdef_fullname(m);
+
+ upb_msg_iter j;
+ for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_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;
+ }
+ }
+ }
+ }
+
+ // We need an array of the defs in addtab, for passing to upb_def_freeze.
+ add_defs = malloc(sizeof(void*) * upb_strtable_count(&addtab));
+ if (add_defs == NULL) goto oom_err;
+ upb_strtable_begin(&i, &addtab);
+ for (n = 0; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ add_defs[n++] = upb_value_getptr(upb_strtable_iter_value(&i));
+ }
+
+ if (!upb_def_freeze(add_defs, n, status)) 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 (int i = 0; i < n; i++) {
+ upb_def *def = add_defs[i];
+ const char *name = upb_def_fullname(def);
+ upb_value v;
+ if (upb_strtable_remove(&s->symtab, name, &v)) {
+ const upb_def *def = upb_value_getptr(v);
+ upb_def_unref(def, s);
+ }
+ bool success = upb_strtable_insert(&s->symtab, name, upb_value_ptr(def));
+ UPB_ASSERT_VAR(success, success == true);
+ }
+ free(add_defs);
+ return true;
+
+oom_err:
+ upb_status_seterrmsg(status, "out of memory");
+err: {
+ // For defs the user passed in, we need to donate the refs back. For defs
+ // we dup'd, we need to just unref them.
+ upb_strtable_iter i;
+ upb_strtable_begin(&i, &addtab);
+ for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i));
+ bool came_from_user = def->came_from_user;
+ def->came_from_user = false;
+ if (came_from_user) {
+ upb_def_donateref(def, s, ref_donor);
+ } else {
+ upb_def_unref(def, s);
+ }
+ }
+ }
+ upb_strtable_uninit(&addtab);
+ free(add_defs);
+ assert(!upb_ok(status));
+ return false;
+}
+
+// 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));
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Implementation is heavily inspired by Lua's ltable.c.
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+
+#define UPB_MAXARRSIZE 16 // 64k.
+
+// From Chromium.
+#define ARRAY_SIZE(x) \
+ ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
+
+static const double MAX_LOAD = 0.85;
+
+// 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;
+
+bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
+
+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);
+}
+
+char *upb_strdup(const char *s) {
+ size_t n = strlen(s) + 1;
+ char *p = malloc(n);
+ if (p) memcpy(p, s, n);
+ return p;
+}
+
+// A type to represent the lookup key of either a strtable or an inttable.
+// This is like upb_tabkey, but can carry a size also to allow lookups of
+// non-NULL-terminated strings (we don't store string lengths in the table).
+typedef struct {
+ upb_tabkey key;
+ uint32_t len; // For string keys only.
+} lookupkey_t;
+
+static lookupkey_t strkey(const char *str) {
+ lookupkey_t k;
+ k.key.str = (char*)str;
+ k.len = strlen(str);
+ return k;
+}
+
+static lookupkey_t strkey2(const char *str, size_t len) {
+ lookupkey_t k;
+ k.key.str = (char*)str;
+ k.len = len;
+ return k;
+}
+
+static lookupkey_t intkey(uintptr_t key) {
+ lookupkey_t k;
+ k.key = upb_intkey(key);
+ return k;
+}
+
+typedef uint32_t hashfunc_t(upb_tabkey key);
+typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
+
+/* Base table (shared code) ***************************************************/
+
+// For when we need to cast away const.
+static upb_tabent *mutable_entries(upb_table *t) {
+ return (upb_tabent*)t->entries;
+}
+
+static bool isfull(upb_table *t) {
+ 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) {
+ t->count = 0;
+ t->ctype = ctype;
+ t->size_lg2 = size_lg2;
+ t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
+ size_t bytes = upb_table_size(t) * sizeof(upb_tabent);
+ if (bytes > 0) {
+ t->entries = malloc(bytes);
+ if (!t->entries) return false;
+ memset(mutable_entries(t), 0, bytes);
+ } else {
+ t->entries = NULL;
+ }
+ return true;
+}
+
+static void uninit(upb_table *t) { free(mutable_entries(t)); }
+
+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; assert(e > t->entries); }
+}
+
+static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
+ return (upb_tabent*)upb_getentry(t, hash);
+}
+
+static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
+ uint32_t hash, eqlfunc_t *eql) {
+ if (t->size_lg2 == 0) return NULL;
+ const upb_tabent *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;
+ }
+}
+
+static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
+ uint32_t hash, eqlfunc_t *eql) {
+ return (upb_tabent*)findentry(t, key, hash, eql);
+}
+
+static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
+ uint32_t hash, eqlfunc_t *eql) {
+ const upb_tabent *e = findentry(t, key, hash, eql);
+ if (e) {
+ if (v) {
+ _upb_value_setval(v, e->val, t->ctype);
+ }
+ return true;
+ } else {
+ return false;
+ }
+}
+
+// The given key must not already exist in the table.
+static void insert(upb_table *t, lookupkey_t key, upb_value val,
+ uint32_t hash, hashfunc_t *hashfunc, eqlfunc_t *eql) {
+ UPB_UNUSED(eql);
+ assert(findentry(t, key, hash, eql) == NULL);
+ assert(val.ctype == t->ctype);
+ t->count++;
+ upb_tabent *mainpos_e = getentry_mutable(t, hash);
+ upb_tabent *our_e = mainpos_e;
+ if (upb_tabent_isempty(mainpos_e)) {
+ // Our main position is empty; use it.
+ our_e->next = NULL;
+ } else {
+ // Collision.
+ upb_tabent *new_e = emptyent(t);
+ // 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;
+ assert(chain);
+ }
+ chain->next = new_e;
+ our_e = mainpos_e;
+ our_e->next = NULL;
+ }
+ }
+ our_e->key = key.key;
+ our_e->val = val.val;
+ 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, t->ctype);
+ }
+ if (chain->next) {
+ upb_tabent *move = (upb_tabent*)chain->next;
+ *chain = *move;
+ if (removed) *removed = move->key;
+ move->key.num = 0; // Make the slot empty.
+ } else {
+ if (removed) *removed = chain->key;
+ chain->key.num = 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.
+ if (val) {
+ _upb_value_setval(val, chain->next->val, t->ctype);
+ }
+ upb_tabent *rm = (upb_tabent*)chain->next;
+ if (removed) *removed = rm->key;
+ rm->key.num = 0;
+ chain->next = rm->next;
+ t->count--;
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
+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]));
+
+ return i;
+}
+
+static size_t begin(const upb_table *t) {
+ return next(t, -1);
+}
+
+
+/* upb_strtable ***************************************************************/
+
+// A simple "subclass" of upb_table that only adds a hash function for strings.
+
+static uint32_t strhash(upb_tabkey key) {
+ return MurmurHash2(key.str, strlen(key.str), 0);
+}
+
+static bool streql(upb_tabkey k1, lookupkey_t k2) {
+ return strncmp(k1.str, k2.key.str, k2.len) == 0 && k1.str[k2.len] == '\0';
+}
+
+bool upb_strtable_init(upb_strtable *t, upb_ctype_t ctype) {
+ return init(&t->t, ctype, 2);
+}
+
+void upb_strtable_uninit(upb_strtable *t) {
+ for (size_t i = 0; i < upb_table_size(&t->t); i++)
+ free((void*)t->t.entries[i].key.str);
+ uninit(&t->t);
+}
+
+bool upb_strtable_resize(upb_strtable *t, size_t size_lg2) {
+ upb_strtable new_table;
+ if (!init(&new_table.t, t->t.ctype, size_lg2))
+ return false;
+ upb_strtable_iter i;
+ upb_strtable_begin(&i, t);
+ for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
+ upb_strtable_insert(
+ &new_table, upb_strtable_iter_key(&i), upb_strtable_iter_value(&i));
+ }
+ upb_strtable_uninit(t);
+ *t = new_table;
+ return true;
+}
+
+bool upb_strtable_insert(upb_strtable *t, const char *k, upb_value v) {
+ 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)) {
+ return false;
+ }
+ }
+ if ((k = upb_strdup(k)) == NULL) return false;
+
+ lookupkey_t key = strkey(k);
+ uint32_t hash = MurmurHash2(key.key.str, key.len, 0);
+ insert(&t->t, strkey(k), v, hash, &strhash, &streql);
+ return true;
+}
+
+bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
+ upb_value *v) {
+ uint32_t hash = MurmurHash2(key, len, 0);
+ return lookup(&t->t, strkey2(key, len), v, hash, &streql);
+}
+
+bool upb_strtable_remove(upb_strtable *t, const char *key, upb_value *val) {
+ uint32_t hash = MurmurHash2(key, strlen(key), 0);
+ upb_tabkey tabkey;
+ if (rm(&t->t, strkey(key), val, &tabkey, hash, &streql)) {
+ free((void*)tabkey.str);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+// Iteration
+
+static const upb_tabent *str_tabent(const upb_strtable_iter *i) {
+ return &i->t->t.entries[i->index];
+}
+
+void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
+ i->t = t;
+ i->index = begin(&t->t);
+}
+
+void upb_strtable_next(upb_strtable_iter *i) {
+ i->index = next(&i->t->t, i->index);
+}
+
+bool upb_strtable_done(const upb_strtable_iter *i) {
+ return i->index >= upb_table_size(&i->t->t) ||
+ upb_tabent_isempty(str_tabent(i));
+}
+
+const char *upb_strtable_iter_key(upb_strtable_iter *i) {
+ assert(!upb_strtable_done(i));
+ return str_tabent(i)->key.str;
+}
+
+upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
+ assert(!upb_strtable_done(i));
+ return _upb_value_val(str_tabent(i)->val, i->t->t.ctype);
+}
+
+void upb_strtable_iter_setdone(upb_strtable_iter *i) {
+ i->index = SIZE_MAX;
+}
+
+bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
+ const upb_strtable_iter *i2) {
+ if (upb_strtable_done(i1) && upb_strtable_done(i2))
+ return true;
+ return i1->t == i2->t && i1->index == i2->index;
+}
+
+
+/* upb_inttable ***************************************************************/
+
+// For inttables we use a hybrid structure where small keys are kept in an
+// array and large keys are put in the hash table.
+
+static uint32_t inthash(upb_tabkey key) { return upb_inthash(key.num); }
+
+static bool inteql(upb_tabkey k1, lookupkey_t k2) {
+ return k1.num == k2.key.num;
+}
+
+static _upb_value *mutable_array(upb_inttable *t) {
+ return (_upb_value*)t->array;
+}
+
+static _upb_value *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 const _upb_value *inttable_val_const(const upb_inttable *t,
+ uintptr_t key) {
+ return inttable_val((upb_inttable*)t, key);
+}
+
+size_t upb_inttable_count(const upb_inttable *t) {
+ return t->t.count + t->array_count;
+}
+
+static void check(upb_inttable *t) {
+ UPB_UNUSED(t);
+#if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
+ // This check is very expensive (makes inserts/deletes O(N)).
+ size_t count = 0;
+ upb_inttable_iter i;
+ upb_inttable_begin(&i, t);
+ for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
+ assert(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
+ }
+ assert(count == upb_inttable_count(t));
+#endif
+}
+
+bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype,
+ size_t asize, int hsize_lg2) {
+ if (!init(&t->t, ctype, hsize_lg2)) 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;
+ size_t array_bytes = t->array_size * sizeof(upb_value);
+ t->array = malloc(array_bytes);
+ if (!t->array) {
+ uninit(&t->t);
+ return false;
+ }
+ memset(mutable_array(t), 0xff, array_bytes);
+ check(t);
+ return true;
+}
+
+bool upb_inttable_init(upb_inttable *t, upb_ctype_t ctype) {
+ return upb_inttable_sizedinit(t, ctype, 0, 4);
+}
+
+void upb_inttable_uninit(upb_inttable *t) {
+ uninit(&t->t);
+ free(mutable_array(t));
+}
+
+bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val) {
+ assert(upb_arrhas(val.val));
+ if (key < t->array_size) {
+ assert(!upb_arrhas(t->array[key]));
+ t->array_count++;
+ mutable_array(t)[key] = val.val;
+ } else {
+ if (isfull(&t->t)) {
+ // Need to resize the hash part, but we re-use the array part.
+ upb_table new_table;
+ if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1))
+ return false;
+ size_t i;
+ for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
+ const upb_tabent *e = &t->t.entries[i];
+ upb_value v;
+ _upb_value_setval(&v, e->val, t->t.ctype);
+ uint32_t hash = upb_inthash(e->key.num);
+ insert(&new_table, intkey(e->key.num), v, hash, &inthash, &inteql);
+ }
+
+ assert(t->t.count == new_table.count);
+
+ uninit(&t->t);
+ t->t = new_table;
+ }
+ insert(&t->t, intkey(key), val, upb_inthash(key), &inthash, &inteql);
+ }
+ check(t);
+ return true;
+}
+
+bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
+ const _upb_value *table_v = inttable_val_const(t, key);
+ if (!table_v) return false;
+ if (v) _upb_value_setval(v, *table_v, t->t.ctype);
+ return true;
+}
+
+bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
+ _upb_value *table_v = inttable_val(t, key);
+ if (!table_v) return false;
+ *table_v = val.val;
+ return true;
+}
+
+bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
+ bool success;
+ if (key < t->array_size) {
+ if (upb_arrhas(t->array[key])) {
+ t->array_count--;
+ if (val) {
+ _upb_value_setval(val, t->array[key], t->t.ctype);
+ }
+ _upb_value empty = UPB_ARRAY_EMPTYENT;
+ mutable_array(t)[key] = empty;
+ success = true;
+ } else {
+ success = false;
+ }
+ } else {
+ upb_tabkey removed;
+ uint32_t hash = upb_inthash(key);
+ success = rm(&t->t, intkey(key), val, &removed, hash, &inteql);
+ }
+ check(t);
+ return success;
+}
+
+bool upb_inttable_push(upb_inttable *t, upb_value val) {
+ return upb_inttable_insert(t, upb_inttable_count(t), val);
+}
+
+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_VAR(ok, ok);
+ return val;
+}
+
+bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val) {
+ return upb_inttable_insert(t, (uintptr_t)key, val);
+}
+
+bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
+ upb_value *v) {
+ return upb_inttable_lookup(t, (uintptr_t)key, v);
+}
+
+bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
+ return upb_inttable_remove(t, (uintptr_t)key, val);
+}
+
+void upb_inttable_compact(upb_inttable *t) {
+ // Create a power-of-two histogram of the table keys.
+ int counts[UPB_MAXARRSIZE + 1] = {0};
+ uintptr_t max_key = 0;
+ 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);
+ if (key > max_key) {
+ max_key = key;
+ }
+ counts[log2ceil(key)]++;
+ }
+
+ int arr_size;
+ int arr_count = upb_inttable_count(t);
+
+ if (upb_inttable_count(t) >= max_key * MIN_DENSITY) {
+ // We can put 100% of the entries in the array part.
+ arr_size = max_key + 1;
+ } else {
+ // Find the largest power of two that satisfies the MIN_DENSITY definition.
+ for (int size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 1; size_lg2--) {
+ arr_size = 1 << size_lg2;
+ arr_count -= counts[size_lg2];
+ if (arr_count >= arr_size * MIN_DENSITY) {
+ break;
+ }
+ }
+ }
+
+ // Array part must always be at least 1 entry large to catch lookups of key
+ // 0. Key 0 must always be in the array part because "0" in the hash part
+ // denotes an empty entry.
+ arr_size = UPB_MAX(arr_size, 1);
+
+ // Insert all elements into new, perfectly-sized table.
+ int hash_count = upb_inttable_count(t) - arr_count;
+ int hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
+ int hashsize_lg2 = log2ceil(hash_size);
+ assert(hash_count >= 0);
+
+ upb_inttable new_t;
+ upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2);
+ upb_inttable_begin(&i, t);
+ for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
+ uintptr_t k = upb_inttable_iter_key(&i);
+ upb_inttable_insert(&new_t, k, upb_inttable_iter_value(&i));
+ }
+ assert(new_t.array_size == arr_size);
+ assert(new_t.t.size_lg2 == hashsize_lg2);
+ upb_inttable_uninit(t);
+ *t = new_t;
+}
+
+// Iteration.
+
+static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
+ assert(!i->array_part);
+ return &i->t->t.entries[i->index];
+}
+
+static _upb_value int_arrent(const upb_inttable_iter *i) {
+ assert(i->array_part);
+ return i->t->array[i->index];
+}
+
+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_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 {
+ iter->index = next(&t->t, iter->index);
+ }
+}
+
+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));
+ } else {
+ return i->index >= upb_table_size(&i->t->t) ||
+ upb_tabent_isempty(int_tabent(i));
+ }
+}
+
+uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
+ assert(!upb_inttable_done(i));
+ return i->array_part ? i->index : int_tabent(i)->key.num;
+}
+
+upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
+ assert(!upb_inttable_done(i));
+ return _upb_value_val(
+ i->array_part ? i->t->array[i->index] : int_tabent(i)->val,
+ i->t->t.ctype);
+}
+
+void upb_inttable_iter_setdone(upb_inttable_iter *i) {
+ i->index = SIZE_MAX;
+ i->array_part = false;
+}
+
+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;
+}
+
+#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;
+ }
+
+ // 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;
+}
+
+#else // !UPB_UNALIGNED_READS_OK
+
+//-----------------------------------------------------------------------------
+// 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;
+
+ switch(align) {
+ case 1: t |= data[2] << 16;
+ case 2: t |= data[1] << 8;
+ case 3: t |= data[0];
+ }
+
+ t <<= (8 * align);
+
+ data += 4-align;
+ len -= 4-align;
+
+ int32_t sl = 8 * (4-align);
+ int32_t sr = 8 * align;
+
+ // Mix
+
+ while(len >= 4) {
+ d = *(uint32_t *)data;
+ t = (t >> sr) | (d << sl);
+
+ uint32_t k = t;
+
+ MIX(h,k,m);
+
+ t = d;
+
+ data += 4;
+ len -= 4;
+ }
+
+ // Handle leftover data in temp registers
+
+ d = 0;
+
+ if(len >= align) {
+ switch(align) {
+ case 3: d |= data[2] << 16;
+ case 2: d |= data[1] << 8;
+ case 1: d |= data[0];
+ }
+
+ uint32_t 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;
+ }
+ }
+
+ 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;
+ }
+
+ //----------
+ // 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
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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);
+ assert(sizeof(status->msg) > len);
+ memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len);
+}
+
+void upb_status_clear(upb_status *status) {
+ upb_status blank = UPB_STATUS_INIT;
+ upb_status_copy(status, &blank);
+}
+
+bool upb_ok(const upb_status *status) { return status->ok_; }
+
+upb_errorspace *upb_status_errspace(const upb_status *status) {
+ return status->error_space_;
+}
+
+int upb_status_errcode(const upb_status *status) { return status->code_; }
+
+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);
+}
+
+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);
+}
+
+void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
+ if (!status) return;
+ status->ok_ = false;
+ vsnprintf(status->msg, sizeof(status->msg), fmt, args);
+ nullz(status);
+}
+
+void upb_status_seterrcode(upb_status *status, upb_errorspace *space,
+ int code) {
+ if (!status) return;
+ status->ok_ = false;
+ status->error_space_ = space;
+ status->code_ = code;
+ space->set_message(status, code);
+}
+
+void upb_status_copy(upb_status *to, const upb_status *from) {
+ if (!to) return;
+ *to = *from;
+}
+// This file was generated by upbc (the upb compiler).
+// Do not edit -- your changes will be discarded when the file is
+// regenerated.
+
+
+static const upb_msgdef msgs[20];
+static const upb_fielddef fields[81];
+static const upb_enumdef enums[4];
+static const upb_tabent strentries[236];
+static const upb_tabent intentries[14];
+static const _upb_value arrays[232];
+
+#ifdef UPB_DEBUG_REFS
+static upb_inttable reftables[212];
+#endif
+
+static const upb_msgdef msgs[20] = {
+ UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 27, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 8, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[0]),&reftables[0], &reftables[1]),
+ UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 4, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[8], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]),&reftables[2], &reftables[3]),
+ UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 11, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[20]),&reftables[4], &reftables[5]),
+ UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 7, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[15], 8, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[24]),&reftables[6], &reftables[7]),
+ UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 8, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[23], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]),&reftables[8], &reftables[9]),
+ UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 6, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[27], 4, 0), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[32]),&reftables[10], &reftables[11]),
+ UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 9, 8), UPB_STRTABLE_INIT(8, 15, UPB_CTYPE_PTR, 4, &strentries[36]),&reftables[12], &reftables[13]),
+ UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 14, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[40], 32, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[52]),&reftables[14], &reftables[15]),
+ UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 39, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[72], 12, 11), UPB_STRTABLE_INIT(11, 15, UPB_CTYPE_PTR, 4, &strentries[68]),&reftables[16], &reftables[17]),
+ UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 6, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[84], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[84]),&reftables[18], &reftables[19]),
+ UPB_MSGDEF_INIT("google.protobuf.FileOptions", 21, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[86], 64, 9), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[88]),&reftables[20], &reftables[21]),
+ UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[150], 16, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[104]),&reftables[22], &reftables[23]),
+ UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 13, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[166], 5, 4), UPB_STRTABLE_INIT(4, 7, UPB_CTYPE_PTR, 3, &strentries[108]),&reftables[24], &reftables[25]),
+ UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 6, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[10], &arrays[171], 4, 0), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[116]),&reftables[26], &reftables[27]),
+ UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 11, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[175], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[120]),&reftables[28], &reftables[29]),
+ UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 6, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[12], &arrays[179], 4, 0), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[124]),&reftables[30], &reftables[31]),
+ UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 6, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[183], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[128]),&reftables[32], &reftables[33]),
+ UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 14, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[185], 5, 4), UPB_STRTABLE_INIT(4, 7, UPB_CTYPE_PTR, 3, &strentries[132]),&reftables[34], &reftables[35]),
+ UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 18, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[190], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[140]),&reftables[36], &reftables[37]),
+ UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[199], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[156]),&reftables[38], &reftables[39]),
+};
+
+static const upb_fielddef fields[81] = {
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "aggregate_value", 8, &msgs[18], NULL, 15, 6, {0},&reftables[40], &reftables[41]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "allow_alias", 2, &msgs[3], NULL, 6, 1, {0},&reftables[42], &reftables[43]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_generic_services", 16, &msgs[10], NULL, 17, 6, {0},&reftables[44], &reftables[45]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "ctype", 1, &msgs[7], UPB_UPCAST(&enums[2]), 6, 1, {0},&reftables[46], &reftables[47]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "default_value", 7, &msgs[6], NULL, 16, 7, {0},&reftables[48], &reftables[49]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "dependency", 3, &msgs[8], NULL, 30, 8, {0},&reftables[50], &reftables[51]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[7], NULL, 8, 3, {0},&reftables[52], &reftables[53]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_DOUBLE, 0, false, false, false, false, "double_value", 6, &msgs[18], NULL, 11, 4, {0},&reftables[54], &reftables[55]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[1], NULL, 3, 1, {0},&reftables[56], &reftables[57]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 4, &msgs[0], UPB_UPCAST(&msgs[2]), 16, 2, {0},&reftables[58], &reftables[59]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 5, &msgs[8], UPB_UPCAST(&msgs[2]), 13, 1, {0},&reftables[60], &reftables[61]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "experimental_map_key", 9, &msgs[7], NULL, 10, 5, {0},&reftables[62], &reftables[63]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "extendee", 2, &msgs[6], NULL, 7, 2, {0},&reftables[64], &reftables[65]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 7, &msgs[8], UPB_UPCAST(&msgs[6]), 19, 3, {0},&reftables[66], &reftables[67]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 6, &msgs[0], UPB_UPCAST(&msgs[6]), 22, 4, {0},&reftables[68], &reftables[69]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension_range", 5, &msgs[0], UPB_UPCAST(&msgs[1]), 19, 3, {0},&reftables[70], &reftables[71]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "field", 2, &msgs[0], UPB_UPCAST(&msgs[6]), 10, 0, {0},&reftables[72], &reftables[73]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "file", 1, &msgs[9], UPB_UPCAST(&msgs[8]), 5, 0, {0},&reftables[74], &reftables[75]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "go_package", 11, &msgs[10], NULL, 14, 5, {0},&reftables[76], &reftables[77]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "identifier_value", 3, &msgs[18], NULL, 6, 1, {0},&reftables[78], &reftables[79]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "input_type", 2, &msgs[12], NULL, 7, 2, {0},&reftables[80], &reftables[81]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_BOOL, 0, false, false, false, false, "is_extension", 2, &msgs[19], NULL, 5, 1, {0},&reftables[82], &reftables[83]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generate_equals_and_hash", 20, &msgs[10], NULL, 20, 9, {0},&reftables[84], &reftables[85]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generic_services", 17, &msgs[10], NULL, 18, 7, {0},&reftables[86], &reftables[87]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_multiple_files", 10, &msgs[10], NULL, 13, 4, {0},&reftables[88], &reftables[89]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_outer_classname", 8, &msgs[10], NULL, 9, 2, {0},&reftables[90], &reftables[91]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_package", 1, &msgs[10], NULL, 6, 1, {0},&reftables[92], &reftables[93]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "label", 4, &msgs[6], UPB_UPCAST(&enums[0]), 11, 4, {0},&reftables[94], &reftables[95]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "lazy", 5, &msgs[7], NULL, 9, 4, {0},&reftables[96], &reftables[97]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "leading_comments", 3, &msgs[17], NULL, 8, 2, {0},&reftables[98], &reftables[99]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "location", 1, &msgs[16], UPB_UPCAST(&msgs[17]), 5, 0, {0},&reftables[100], &reftables[101]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "message_set_wire_format", 1, &msgs[11], NULL, 6, 1, {0},&reftables[102], &reftables[103]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "message_type", 4, &msgs[8], UPB_UPCAST(&msgs[0]), 10, 0, {0},&reftables[104], &reftables[105]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "method", 2, &msgs[14], UPB_UPCAST(&msgs[12]), 6, 0, {0},&reftables[106], &reftables[107]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[8], NULL, 22, 6, {0},&reftables[108], &reftables[109]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[14], NULL, 8, 2, {0},&reftables[110], &reftables[111]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "name", 2, &msgs[18], UPB_UPCAST(&msgs[19]), 5, 0, {0},&reftables[112], &reftables[113]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[4], NULL, 4, 1, {0},&reftables[114], &reftables[115]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[0], NULL, 24, 6, {0},&reftables[116], &reftables[117]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[12], NULL, 4, 1, {0},&reftables[118], &reftables[119]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[2], NULL, 8, 2, {0},&reftables[120], &reftables[121]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[6], NULL, 4, 1, {0},&reftables[122], &reftables[123]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_STRING, 0, false, false, false, false, "name_part", 1, &msgs[19], NULL, 2, 0, {0},&reftables[124], &reftables[125]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "negative_int_value", 5, &msgs[18], NULL, 10, 3, {0},&reftables[126], &reftables[127]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "nested_type", 3, &msgs[0], UPB_UPCAST(&msgs[0]), 13, 1, {0},&reftables[128], &reftables[129]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "no_standard_descriptor_accessor", 2, &msgs[11], NULL, 7, 2, {0},&reftables[130], &reftables[131]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 3, &msgs[6], NULL, 10, 3, {0},&reftables[132], &reftables[133]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 2, &msgs[4], NULL, 7, 2, {0},&reftables[134], &reftables[135]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optimize_for", 9, &msgs[10], UPB_UPCAST(&enums[3]), 12, 3, {0},&reftables[136], &reftables[137]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 7, &msgs[0], UPB_UPCAST(&msgs[11]), 23, 5, {0},&reftables[138], &reftables[139]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[2], UPB_UPCAST(&msgs[3]), 7, 1, {0},&reftables[140], &reftables[141]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[6], UPB_UPCAST(&msgs[7]), 3, 0, {0},&reftables[142], &reftables[143]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[4], UPB_UPCAST(&msgs[5]), 3, 0, {0},&reftables[144], &reftables[145]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[8], UPB_UPCAST(&msgs[10]), 20, 4, {0},&reftables[146], &reftables[147]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[14], UPB_UPCAST(&msgs[15]), 7, 1, {0},&reftables[148], &reftables[149]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 4, &msgs[12], UPB_UPCAST(&msgs[13]), 3, 0, {0},&reftables[150], &reftables[151]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "output_type", 3, &msgs[12], NULL, 10, 3, {0},&reftables[152], &reftables[153]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "package", 2, &msgs[8], NULL, 25, 7, {0},&reftables[154], &reftables[155]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "packed", 2, &msgs[7], NULL, 7, 2, {0},&reftables[156], &reftables[157]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "path", 1, &msgs[17], NULL, 4, 0, {0},&reftables[158], &reftables[159]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "positive_int_value", 4, &msgs[18], NULL, 9, 2, {0},&reftables[160], &reftables[161]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "public_dependency", 10, &msgs[8], NULL, 35, 9, {0},&reftables[162], &reftables[163]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "py_generic_services", 18, &msgs[10], NULL, 19, 8, {0},&reftables[164], &reftables[165]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "service", 6, &msgs[8], UPB_UPCAST(&msgs[14]), 16, 2, {0},&reftables[166], &reftables[167]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "source_code_info", 9, &msgs[8], UPB_UPCAST(&msgs[16]), 21, 5, {0},&reftables[168], &reftables[169]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "span", 2, &msgs[17], NULL, 7, 1, {0},&reftables[170], &reftables[171]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[1], NULL, 2, 0, {0},&reftables[172], &reftables[173]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "string_value", 7, &msgs[18], NULL, 12, 5, {0},&reftables[174], &reftables[175]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "trailing_comments", 4, &msgs[17], NULL, 11, 3, {0},&reftables[176], &reftables[177]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "type", 5, &msgs[6], UPB_UPCAST(&enums[1]), 12, 5, {0},&reftables[178], &reftables[179]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "type_name", 6, &msgs[6], NULL, 13, 6, {0},&reftables[180], &reftables[181]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[5], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[182], &reftables[183]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[15], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[184], &reftables[185]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[3], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[186], &reftables[187]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[13], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[188], &reftables[189]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[10], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[190], &reftables[191]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[11], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[192], &reftables[193]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[7], UPB_UPCAST(&msgs[18]), 5, 0, {0},&reftables[194], &reftables[195]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[2], UPB_UPCAST(&msgs[4]), 6, 0, {0},&reftables[196], &reftables[197]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "weak", 10, &msgs[7], NULL, 13, 6, {0},&reftables[198], &reftables[199]),
+ UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "weak_dependency", 11, &msgs[8], NULL, 38, 10, {0},&reftables[200], &reftables[201]),
+};
+
+static const upb_enumdef enums[4] = {
+ UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Label", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[160]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[202], 4, 3), 0, &reftables[202], &reftables[203]),
+ UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Type", UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_INT32, 5, &strentries[164]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[206], 19, 18), 0, &reftables[204], &reftables[205]),
+ UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.CType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[196]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[225], 3, 3), 0, &reftables[206], &reftables[207]),
+ UPB_ENUMDEF_INIT("google.protobuf.FileOptions.OptimizeMode", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[200]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[228], 4, 3), 0, &reftables[208], &reftables[209]),
+};
+
+static const upb_tabent strentries[236] = {
+ {UPB_TABKEY_STR("extension"), UPB_VALUE_INIT_CONSTPTR(&fields[14]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[38]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("field"), UPB_VALUE_INIT_CONSTPTR(&fields[16]), NULL},
+ {UPB_TABKEY_STR("extension_range"), UPB_VALUE_INIT_CONSTPTR(&fields[15]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("nested_type"), UPB_VALUE_INIT_CONSTPTR(&fields[44]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[49]), NULL},
+ {UPB_TABKEY_STR("enum_type"), UPB_VALUE_INIT_CONSTPTR(&fields[9]), &strentries[14]},
+ {UPB_TABKEY_STR("start"), UPB_VALUE_INIT_CONSTPTR(&fields[66]), NULL},
+ {UPB_TABKEY_STR("end"), UPB_VALUE_INIT_CONSTPTR(&fields[8]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("value"), UPB_VALUE_INIT_CONSTPTR(&fields[78]), NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[50]), NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[40]), &strentries[22]},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[73]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("allow_alias"), UPB_VALUE_INIT_CONSTPTR(&fields[1]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("number"), UPB_VALUE_INIT_CONSTPTR(&fields[47]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[52]), NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[37]), &strentries[30]},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[71]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("label"), UPB_VALUE_INIT_CONSTPTR(&fields[27]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[41]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("number"), UPB_VALUE_INIT_CONSTPTR(&fields[46]), &strentries[49]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("type_name"), UPB_VALUE_INIT_CONSTPTR(&fields[70]), NULL},
+ {UPB_TABKEY_STR("extendee"), UPB_VALUE_INIT_CONSTPTR(&fields[12]), NULL},
+ {UPB_TABKEY_STR("type"), UPB_VALUE_INIT_CONSTPTR(&fields[69]), &strentries[48]},
+ {UPB_TABKEY_STR("default_value"), UPB_VALUE_INIT_CONSTPTR(&fields[4]), NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[51]), NULL},
+ {UPB_TABKEY_STR("experimental_map_key"), UPB_VALUE_INIT_CONSTPTR(&fields[11]), &strentries[67]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("weak"), UPB_VALUE_INIT_CONSTPTR(&fields[79]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("packed"), UPB_VALUE_INIT_CONSTPTR(&fields[58]), NULL},
+ {UPB_TABKEY_STR("lazy"), UPB_VALUE_INIT_CONSTPTR(&fields[28]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("ctype"), UPB_VALUE_INIT_CONSTPTR(&fields[3]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("deprecated"), UPB_VALUE_INIT_CONSTPTR(&fields[6]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[77]), NULL},
+ {UPB_TABKEY_STR("extension"), UPB_VALUE_INIT_CONSTPTR(&fields[13]), NULL},
+ {UPB_TABKEY_STR("weak_dependency"), UPB_VALUE_INIT_CONSTPTR(&fields[80]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[34]), NULL},
+ {UPB_TABKEY_STR("service"), UPB_VALUE_INIT_CONSTPTR(&fields[63]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("source_code_info"), UPB_VALUE_INIT_CONSTPTR(&fields[64]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("dependency"), UPB_VALUE_INIT_CONSTPTR(&fields[5]), NULL},
+ {UPB_TABKEY_STR("message_type"), UPB_VALUE_INIT_CONSTPTR(&fields[32]), NULL},
+ {UPB_TABKEY_STR("package"), UPB_VALUE_INIT_CONSTPTR(&fields[57]), NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[53]), &strentries[82]},
+ {UPB_TABKEY_STR("enum_type"), UPB_VALUE_INIT_CONSTPTR(&fields[10]), NULL},
+ {UPB_TABKEY_STR("public_dependency"), UPB_VALUE_INIT_CONSTPTR(&fields[61]), &strentries[81]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("file"), UPB_VALUE_INIT_CONSTPTR(&fields[17]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[75]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("cc_generic_services"), UPB_VALUE_INIT_CONSTPTR(&fields[2]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("java_multiple_files"), UPB_VALUE_INIT_CONSTPTR(&fields[24]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("java_generic_services"), UPB_VALUE_INIT_CONSTPTR(&fields[23]), &strentries[102]},
+ {UPB_TABKEY_STR("java_generate_equals_and_hash"), UPB_VALUE_INIT_CONSTPTR(&fields[22]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("go_package"), UPB_VALUE_INIT_CONSTPTR(&fields[18]), NULL},
+ {UPB_TABKEY_STR("java_package"), UPB_VALUE_INIT_CONSTPTR(&fields[26]), NULL},
+ {UPB_TABKEY_STR("optimize_for"), UPB_VALUE_INIT_CONSTPTR(&fields[48]), NULL},
+ {UPB_TABKEY_STR("py_generic_services"), UPB_VALUE_INIT_CONSTPTR(&fields[62]), NULL},
+ {UPB_TABKEY_STR("java_outer_classname"), UPB_VALUE_INIT_CONSTPTR(&fields[25]), NULL},
+ {UPB_TABKEY_STR("message_set_wire_format"), UPB_VALUE_INIT_CONSTPTR(&fields[31]), &strentries[106]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[76]), NULL},
+ {UPB_TABKEY_STR("no_standard_descriptor_accessor"), UPB_VALUE_INIT_CONSTPTR(&fields[45]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[39]), NULL},
+ {UPB_TABKEY_STR("input_type"), UPB_VALUE_INIT_CONSTPTR(&fields[20]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("output_type"), UPB_VALUE_INIT_CONSTPTR(&fields[56]), NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[55]), NULL},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[74]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("options"), UPB_VALUE_INIT_CONSTPTR(&fields[54]), &strentries[122]},
+ {UPB_TABKEY_STR("method"), UPB_VALUE_INIT_CONSTPTR(&fields[33]), NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[35]), &strentries[121]},
+ {UPB_TABKEY_STR("uninterpreted_option"), UPB_VALUE_INIT_CONSTPTR(&fields[72]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("location"), UPB_VALUE_INIT_CONSTPTR(&fields[30]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("span"), UPB_VALUE_INIT_CONSTPTR(&fields[65]), &strentries[139]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("trailing_comments"), UPB_VALUE_INIT_CONSTPTR(&fields[68]), NULL},
+ {UPB_TABKEY_STR("leading_comments"), UPB_VALUE_INIT_CONSTPTR(&fields[29]), &strentries[137]},
+ {UPB_TABKEY_STR("path"), UPB_VALUE_INIT_CONSTPTR(&fields[59]), NULL},
+ {UPB_TABKEY_STR("double_value"), UPB_VALUE_INIT_CONSTPTR(&fields[7]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("name"), UPB_VALUE_INIT_CONSTPTR(&fields[36]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("negative_int_value"), UPB_VALUE_INIT_CONSTPTR(&fields[43]), NULL},
+ {UPB_TABKEY_STR("aggregate_value"), UPB_VALUE_INIT_CONSTPTR(&fields[0]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("positive_int_value"), UPB_VALUE_INIT_CONSTPTR(&fields[60]), NULL},
+ {UPB_TABKEY_STR("identifier_value"), UPB_VALUE_INIT_CONSTPTR(&fields[19]), NULL},
+ {UPB_TABKEY_STR("string_value"), UPB_VALUE_INIT_CONSTPTR(&fields[67]), &strentries[154]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("is_extension"), UPB_VALUE_INIT_CONSTPTR(&fields[21]), NULL},
+ {UPB_TABKEY_STR("name_part"), UPB_VALUE_INIT_CONSTPTR(&fields[42]), NULL},
+ {UPB_TABKEY_STR("LABEL_REQUIRED"), UPB_VALUE_INIT_INT32(2), &strentries[162]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("LABEL_REPEATED"), UPB_VALUE_INIT_INT32(3), NULL},
+ {UPB_TABKEY_STR("LABEL_OPTIONAL"), UPB_VALUE_INIT_INT32(1), NULL},
+ {UPB_TABKEY_STR("TYPE_FIXED64"), UPB_VALUE_INIT_INT32(6), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_STRING"), UPB_VALUE_INIT_INT32(9), NULL},
+ {UPB_TABKEY_STR("TYPE_FLOAT"), UPB_VALUE_INIT_INT32(2), &strentries[193]},
+ {UPB_TABKEY_STR("TYPE_DOUBLE"), UPB_VALUE_INIT_INT32(1), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_INT32"), UPB_VALUE_INIT_INT32(5), NULL},
+ {UPB_TABKEY_STR("TYPE_SFIXED32"), UPB_VALUE_INIT_INT32(15), NULL},
+ {UPB_TABKEY_STR("TYPE_FIXED32"), UPB_VALUE_INIT_INT32(7), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_MESSAGE"), UPB_VALUE_INIT_INT32(11), &strentries[194]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_INT64"), UPB_VALUE_INIT_INT32(3), &strentries[191]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_ENUM"), UPB_VALUE_INIT_INT32(14), NULL},
+ {UPB_TABKEY_STR("TYPE_UINT32"), UPB_VALUE_INIT_INT32(13), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_UINT64"), UPB_VALUE_INIT_INT32(4), &strentries[190]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("TYPE_SFIXED64"), UPB_VALUE_INIT_INT32(16), NULL},
+ {UPB_TABKEY_STR("TYPE_BYTES"), UPB_VALUE_INIT_INT32(12), NULL},
+ {UPB_TABKEY_STR("TYPE_SINT64"), UPB_VALUE_INIT_INT32(18), NULL},
+ {UPB_TABKEY_STR("TYPE_BOOL"), UPB_VALUE_INIT_INT32(8), NULL},
+ {UPB_TABKEY_STR("TYPE_GROUP"), UPB_VALUE_INIT_INT32(10), NULL},
+ {UPB_TABKEY_STR("TYPE_SINT32"), UPB_VALUE_INIT_INT32(17), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("CORD"), UPB_VALUE_INIT_INT32(1), NULL},
+ {UPB_TABKEY_STR("STRING"), UPB_VALUE_INIT_INT32(0), &strentries[197]},
+ {UPB_TABKEY_STR("STRING_PIECE"), UPB_VALUE_INIT_INT32(2), NULL},
+ {UPB_TABKEY_STR("CODE_SIZE"), UPB_VALUE_INIT_INT32(2), NULL},
+ {UPB_TABKEY_STR("SPEED"), UPB_VALUE_INIT_INT32(1), &strentries[203]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("LITE_RUNTIME"), UPB_VALUE_INIT_INT32(3), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("google.protobuf.SourceCodeInfo.Location"), UPB_VALUE_INIT_CONSTPTR(&msgs[17]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.UninterpretedOption"), UPB_VALUE_INIT_CONSTPTR(&msgs[18]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FileDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[8]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.MethodDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[12]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("google.protobuf.EnumValueOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[5]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("google.protobuf.DescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[0]), &strentries[228]},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("google.protobuf.SourceCodeInfo"), UPB_VALUE_INIT_CONSTPTR(&msgs[16]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FieldDescriptorProto.Type"), UPB_VALUE_INIT_CONSTPTR(&enums[1]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.DescriptorProto.ExtensionRange"), UPB_VALUE_INIT_CONSTPTR(&msgs[1]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_STR("google.protobuf.EnumValueDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[4]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FieldOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[7]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FileOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[10]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.EnumDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[2]), &strentries[233]},
+ {UPB_TABKEY_STR("google.protobuf.FieldDescriptorProto.Label"), UPB_VALUE_INIT_CONSTPTR(&enums[0]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.ServiceDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[14]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FieldOptions.CType"), UPB_VALUE_INIT_CONSTPTR(&enums[2]), &strentries[229]},
+ {UPB_TABKEY_STR("google.protobuf.FileDescriptorSet"), UPB_VALUE_INIT_CONSTPTR(&msgs[9]), &strentries[235]},
+ {UPB_TABKEY_STR("google.protobuf.EnumOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[3]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FieldDescriptorProto"), UPB_VALUE_INIT_CONSTPTR(&msgs[6]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.FileOptions.OptimizeMode"), UPB_VALUE_INIT_CONSTPTR(&enums[3]), &strentries[221]},
+ {UPB_TABKEY_STR("google.protobuf.ServiceOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[15]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.MessageOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[11]), NULL},
+ {UPB_TABKEY_STR("google.protobuf.MethodOptions"), UPB_VALUE_INIT_CONSTPTR(&msgs[13]), &strentries[226]},
+ {UPB_TABKEY_STR("google.protobuf.UninterpretedOption.NamePart"), UPB_VALUE_INIT_CONSTPTR(&msgs[19]), NULL},
+};
+
+static const upb_tabent intentries[14] = {
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[73]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[71]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[77]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[75]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[76]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[74]), NULL},
+ {UPB_TABKEY_NONE, UPB__VALUE_INIT_NONE, NULL},
+ {UPB_TABKEY_NUM(999), UPB_VALUE_INIT_CONSTPTR(&fields[72]), NULL},
+};
+
+static const _upb_value arrays[232] = {
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[38]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[16]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[44]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[9]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[15]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[14]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[49]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[66]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[8]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[40]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[78]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[50]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[1]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[37]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[47]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[52]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[41]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[12]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[46]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[27]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[69]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[70]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[4]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[51]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[3]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[58]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[6]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[28]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[11]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[79]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[34]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[57]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[5]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[32]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[10]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[63]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[13]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[53]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[64]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[61]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[80]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[17]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[26]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[25]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[48]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[24]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[18]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[2]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[23]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[62]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[22]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[31]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[45]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[39]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[20]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[56]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[55]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[35]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[33]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[54]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[30]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[59]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[65]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[29]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[68]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[36]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[19]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[60]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[43]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[7]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[67]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[0]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR(&fields[42]),
+ UPB_VALUE_INIT_CONSTPTR(&fields[21]),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR("LABEL_OPTIONAL"),
+ UPB_VALUE_INIT_CONSTPTR("LABEL_REQUIRED"),
+ UPB_VALUE_INIT_CONSTPTR("LABEL_REPEATED"),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR("TYPE_DOUBLE"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_FLOAT"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_INT64"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_UINT64"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_INT32"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_FIXED64"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_FIXED32"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_BOOL"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_STRING"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_GROUP"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_MESSAGE"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_BYTES"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_UINT32"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_ENUM"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_SFIXED32"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_SFIXED64"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_SINT32"),
+ UPB_VALUE_INIT_CONSTPTR("TYPE_SINT64"),
+ UPB_VALUE_INIT_CONSTPTR("STRING"),
+ UPB_VALUE_INIT_CONSTPTR("CORD"),
+ UPB_VALUE_INIT_CONSTPTR("STRING_PIECE"),
+ UPB_ARRAY_EMPTYENT,
+ UPB_VALUE_INIT_CONSTPTR("SPEED"),
+ UPB_VALUE_INIT_CONSTPTR("CODE_SIZE"),
+ UPB_VALUE_INIT_CONSTPTR("LITE_RUNTIME"),
+};
+
+static const upb_symtab symtab = UPB_SYMTAB_INIT(UPB_STRTABLE_INIT(24, 31, UPB_CTYPE_PTR, 5, &strentries[204]), &reftables[210], &reftables[211]);
+
+const upb_symtab *upbdefs_google_protobuf_descriptor(const void *owner) {
+ upb_symtab_ref(&symtab, owner);
+ return &symtab;
+}
+
+#ifdef UPB_DEBUG_REFS
+static upb_inttable reftables[212] = {
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
+ UPB_EMPTY_INTTABLE_INIT(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 - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2008-2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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.
+ */
+
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char *upb_strndup(const char *buf, size_t n) {
+ char *ret = malloc(n + 1);
+ if (!ret) return NULL;
+ memcpy(ret, buf, n);
+ ret[n] = '\0';
+ return ret;
+}
+
+// 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_strdup(name);
+ } else {
+ char *ret = malloc(strlen(base) + strlen(name) + 2);
+ ret[0] = '\0';
+ strcat(ret, base);
+ strcat(ret, ".");
+ strcat(ret, name);
+ return ret;
+ }
+}
+
+
+/* upb_deflist ****************************************************************/
+
+void upb_deflist_init(upb_deflist *l) {
+ l->size = 0;
+ l->defs = NULL;
+ l->len = 0;
+ l->owned = true;
+}
+
+void upb_deflist_uninit(upb_deflist *l) {
+ if (l->owned)
+ for(size_t i = 0; i < l->len; i++)
+ upb_def_unref(l->defs[i], l);
+ free(l->defs);
+}
+
+bool upb_deflist_push(upb_deflist *l, upb_def *d) {
+ if(++l->len >= l->size) {
+ size_t new_size = UPB_MAX(l->size, 4);
+ new_size *= 2;
+ l->defs = realloc(l->defs, new_size * sizeof(void *));
+ if (!l->defs) return false;
+ l->size = new_size;
+ }
+ l->defs[l->len - 1] = d;
+ return true;
+}
+
+void upb_deflist_donaterefs(upb_deflist *l, void *owner) {
+ assert(l->owned);
+ for (size_t i = 0; i < l->len; i++)
+ upb_def_donateref(l->defs[i], l, owner);
+ l->owned = false;
+}
+
+static upb_def *upb_deflist_last(upb_deflist *l) {
+ return l->defs[l->len-1];
+}
+
+// Qualify the defname for all defs starting with offset "start" with "str".
+static void upb_deflist_qualify(upb_deflist *l, char *str, int32_t start) {
+ for (uint32_t i = start; i < l->len; i++) {
+ upb_def *def = l->defs[i];
+ char *name = upb_join(str, upb_def_fullname(def));
+ upb_def_setfullname(def, name, NULL);
+ free(name);
+ }
+}
+
+
+/* upb_descreader ************************************************************/
+
+void upb_descreader_init(upb_descreader *r, const upb_handlers *handlers,
+ upb_status *status) {
+ UPB_UNUSED(status);
+ upb_deflist_init(&r->defs);
+ upb_sink_reset(upb_descreader_input(r), handlers, r);
+ r->stack_len = 0;
+ r->name = NULL;
+ r->default_string = NULL;
+}
+
+void upb_descreader_uninit(upb_descreader *r) {
+ free(r->name);
+ upb_deflist_uninit(&r->defs);
+ free(r->default_string);
+ while (r->stack_len > 0) {
+ upb_descreader_frame *f = &r->stack[--r->stack_len];
+ free(f->name);
+ }
+}
+
+upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n) {
+ *n = r->defs.len;
+ upb_deflist_donaterefs(&r->defs, owner);
+ return r->defs.defs;
+}
+
+upb_sink *upb_descreader_input(upb_descreader *r) {
+ return &r->sink;
+}
+
+static upb_msgdef *upb_descreader_top(upb_descreader *r) {
+ assert(r->stack_len > 1);
+ int index = r->stack[r->stack_len-1].start - 1;
+ assert(index >= 0);
+ return upb_downcast_msgdef_mutable(r->defs.defs[index]);
+}
+
+static upb_def *upb_descreader_last(upb_descreader *r) {
+ return upb_deflist_last(&r->defs);
+}
+
+// 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 = r->defs.len;
+ f->name = NULL;
+}
+
+void upb_descreader_endcontainer(upb_descreader *r) {
+ upb_descreader_frame *f = &r->stack[--r->stack_len];
+ upb_deflist_qualify(&r->defs, f->name, f->start);
+ free(f->name);
+ f->name = NULL;
+}
+
+void upb_descreader_setscopename(upb_descreader *r, char *str) {
+ upb_descreader_frame *f = &r->stack[r->stack_len-1];
+ free(f->name);
+ f->name = str;
+}
+
+// Handlers for google.protobuf.FileDescriptorProto.
+static bool file_startmsg(void *r, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader_startcontainer(r);
+ return true;
+}
+
+static bool file_endmsg(void *closure, const void *hd, upb_status *status) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(status);
+ upb_descreader *r = closure;
+ upb_descreader_endcontainer(r);
+ return true;
+}
+
+static size_t file_onpackage(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ upb_descreader_setscopename(r, upb_strndup(buf, n));
+ return n;
+}
+
+// Handlers for google.protobuf.EnumValueDescriptorProto.
+static bool enumval_startmsg(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ r->saw_number = false;
+ r->saw_name = false;
+ return true;
+}
+
+static size_t enumval_onname(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ free(r->name);
+ r->name = upb_strndup(buf, n);
+ r->saw_name = true;
+ return n;
+}
+
+static bool enumval_onnumber(void *closure, const void *hd, int32_t val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ r->number = val;
+ r->saw_number = true;
+ return true;
+}
+
+static bool enumval_endmsg(void *closure, const void *hd, upb_status *status) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ if(!r->saw_number || !r->saw_name) {
+ upb_status_seterrmsg(status, "Enum value missing name or number.");
+ return false;
+ }
+ upb_enumdef *e = upb_downcast_enumdef_mutable(upb_descreader_last(r));
+ upb_enumdef_addval(e, r->name, r->number, status);
+ free(r->name);
+ r->name = NULL;
+ return true;
+}
+
+
+// Handlers for google.protobuf.EnumDescriptorProto.
+static bool enum_startmsg(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_deflist_push(&r->defs, UPB_UPCAST(upb_enumdef_new(&r->defs)));
+ return true;
+}
+
+static bool enum_endmsg(void *closure, const void *hd, upb_status *status) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_enumdef *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;
+ }
+ return true;
+}
+
+static size_t enum_onname(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ char *fullname = upb_strndup(buf, n);
+ upb_def_setfullname(upb_descreader_last(r), fullname, NULL);
+ free(fullname);
+ return n;
+}
+
+// Handlers for google.protobuf.FieldDescriptorProto
+static bool field_startmsg(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ r->f = upb_fielddef_new(&r->defs);
+ free(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: {
+ long long val = strtoll(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: {
+ 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: {
+ unsigned long long val = strtoull(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: {
+ float val = strtof(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();
+ }
+ return success;
+}
+
+static bool field_endmsg(void *closure, const void *hd, upb_status *status) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_fielddef *f = r->f;
+ // TODO: verify that all required fields were present.
+ assert(upb_fielddef_number(f) != 0);
+ assert(upb_fielddef_name(f) != NULL);
+ assert((upb_fielddef_subdefname(f) != NULL) == upb_fielddef_hassubdef(f));
+
+ 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;
+ }
+ }
+ }
+ return true;
+}
+
+static bool field_onlazy(void *closure, const void *hd, bool val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_fielddef_setlazy(r->f, val);
+ return true;
+}
+
+static bool field_onpacked(void *closure, const void *hd, bool val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_fielddef_setpacked(r->f, val);
+ return true;
+}
+
+static bool field_ontype(void *closure, const void *hd, int32_t val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_fielddef_setdescriptortype(r->f, val);
+ return true;
+}
+
+static bool field_onlabel(void *closure, const void *hd, int32_t val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_fielddef_setlabel(r->f, val);
+ return true;
+}
+
+static bool field_onnumber(void *closure, const void *hd, int32_t val) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ bool ok = upb_fielddef_setnumber(r->f, val, NULL);
+ UPB_ASSERT_VAR(ok, ok);
+ return true;
+}
+
+static size_t field_onname(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ char *name = upb_strndup(buf, n);
+ upb_fielddef_setname(r->f, name, NULL);
+ free(name);
+ return n;
+}
+
+static size_t field_ontypename(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ char *name = upb_strndup(buf, n);
+ upb_fielddef_setsubdefname(r->f, name, NULL);
+ free(name);
+ return n;
+}
+
+static size_t field_onextendee(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // XXX: see comment at the top of the file.
+ char *name = upb_strndup(buf, n);
+ upb_fielddef_setcontainingtypename(r->f, name, NULL);
+ free(name);
+ return n;
+}
+
+static size_t field_ondefaultval(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ // 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.
+ free(r->default_string);
+ r->default_string = upb_strndup(buf, n);
+ return n;
+}
+
+// Handlers for google.protobuf.DescriptorProto (representing a message).
+static bool msg_startmsg(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_deflist_push(&r->defs, UPB_UPCAST(upb_msgdef_new(&r->defs)));
+ upb_descreader_startcontainer(r);
+ return true;
+}
+
+static bool msg_endmsg(void *closure, const void *hd, upb_status *status) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_msgdef *m = upb_descreader_top(r);
+ if(!upb_def_fullname(UPB_UPCAST(m))) {
+ upb_status_seterrmsg(status, "Encountered message with no name.");
+ return false;
+ }
+ upb_descreader_endcontainer(r);
+ return true;
+}
+
+static size_t msg_onname(void *closure, const void *hd, const char *buf,
+ size_t n, const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_descreader *r = closure;
+ upb_msgdef *m = upb_descreader_top(r);
+ // XXX: see comment at the top of the file.
+ char *name = upb_strndup(buf, n);
+ upb_def_setfullname(UPB_UPCAST(m), name, NULL);
+ upb_descreader_setscopename(r, name); // Passes ownership of name.
+ return n;
+}
+
+static bool msg_onendfield(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ upb_msgdef *m = upb_descreader_top(r);
+ upb_msgdef_addfield(m, r->f, &r->defs, NULL);
+ r->f = NULL;
+ return true;
+}
+
+static bool pushextension(void *closure, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_descreader *r = closure;
+ assert(upb_fielddef_containingtypename(r->f));
+ upb_fielddef_setisextension(r->f, true);
+ upb_deflist_push(&r->defs, UPB_UPCAST(r->f));
+ r->f = NULL;
+ return true;
+}
+
+#define D(name) upbdefs_google_protobuf_ ## name(s)
+
+static void reghandlers(const void *closure, upb_handlers *h) {
+ const upb_symtab *s = closure;
+ const upb_msgdef *m = upb_handlers_msgdef(h);
+
+ if (m == D(DescriptorProto)) {
+ upb_handlers_setstartmsg(h, &msg_startmsg, NULL);
+ upb_handlers_setendmsg(h, &msg_endmsg, NULL);
+ upb_handlers_setstring(h, D(DescriptorProto_name), &msg_onname, NULL);
+ upb_handlers_setendsubmsg(h, D(DescriptorProto_field), &msg_onendfield,
+ NULL);
+ upb_handlers_setendsubmsg(h, D(DescriptorProto_extension), &pushextension,
+ NULL);
+ } else if (m == D(FileDescriptorProto)) {
+ upb_handlers_setstartmsg(h, &file_startmsg, NULL);
+ upb_handlers_setendmsg(h, &file_endmsg, NULL);
+ upb_handlers_setstring(h, D(FileDescriptorProto_package), &file_onpackage,
+ NULL);
+ upb_handlers_setendsubmsg(h, D(FileDescriptorProto_extension), &pushextension,
+ NULL);
+ } else if (m == D(EnumValueDescriptorProto)) {
+ upb_handlers_setstartmsg(h, &enumval_startmsg, NULL);
+ upb_handlers_setendmsg(h, &enumval_endmsg, NULL);
+ upb_handlers_setstring(h, D(EnumValueDescriptorProto_name), &enumval_onname, NULL);
+ upb_handlers_setint32(h, D(EnumValueDescriptorProto_number), &enumval_onnumber,
+ NULL);
+ } else if (m == D(EnumDescriptorProto)) {
+ upb_handlers_setstartmsg(h, &enum_startmsg, NULL);
+ upb_handlers_setendmsg(h, &enum_endmsg, NULL);
+ upb_handlers_setstring(h, D(EnumDescriptorProto_name), &enum_onname, NULL);
+ } else if (m == D(FieldDescriptorProto)) {
+ upb_handlers_setstartmsg(h, &field_startmsg, NULL);
+ upb_handlers_setendmsg(h, &field_endmsg, NULL);
+ upb_handlers_setint32(h, D(FieldDescriptorProto_type), &field_ontype,
+ NULL);
+ upb_handlers_setint32(h, D(FieldDescriptorProto_label), &field_onlabel,
+ NULL);
+ upb_handlers_setint32(h, D(FieldDescriptorProto_number), &field_onnumber,
+ NULL);
+ upb_handlers_setstring(h, D(FieldDescriptorProto_name), &field_onname,
+ NULL);
+ upb_handlers_setstring(h, D(FieldDescriptorProto_type_name),
+ &field_ontypename, NULL);
+ upb_handlers_setstring(h, D(FieldDescriptorProto_extendee),
+ &field_onextendee, NULL);
+ upb_handlers_setstring(h, D(FieldDescriptorProto_default_value),
+ &field_ondefaultval, NULL);
+ } else if (m == D(FieldOptions)) {
+ upb_handlers_setbool(h, D(FieldOptions_lazy), &field_onlazy, NULL);
+ upb_handlers_setbool(h, D(FieldOptions_packed), &field_onpacked, NULL);
+ }
+}
+
+#undef D
+
+const upb_handlers *upb_descreader_newhandlers(const void *owner) {
+ const upb_symtab *s = upbdefs_google_protobuf_descriptor(&s);
+ const upb_handlers *h = upb_handlers_newfrozen(
+ upbdefs_google_protobuf_FileDescriptorSet(s), owner, reghandlers, s);
+ upb_symtab_unref(s, &s);
+ return h;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2013 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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 <stdarg.h>
+
+#ifdef UPB_DUMP_BYTECODE
+#include <stdio.h>
+#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
+ free(g->bytecode);
+ free(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_UPCAST(method), closure);
+ }
+}
+
+mgroup *newgroup(const void *owner) {
+ mgroup *g = malloc(sizeof(*g));
+ static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup};
+ upb_refcounted_init(UPB_UPCAST(g), &vtbl, owner);
+ upb_inttable_init(&g->methods, UPB_CTYPE_PTR);
+ g->bytecode = NULL;
+ g->bytecode_end = NULL;
+ return g;
+}
+
+
+/* upb_pbdecodermethod ********************************************************/
+
+static void freemethod(upb_refcounted *r) {
+ upb_pbdecodermethod *method = (upb_pbdecodermethod*)r;
+ upb_byteshandler_uninit(&method->input_handler_);
+
+ if (method->dest_handlers_) {
+ upb_handlers_unref(method->dest_handlers_, method);
+ }
+
+ upb_inttable_uninit(&method->dispatch);
+ free(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 = malloc(sizeof(*ret));
+ upb_refcounted_init(UPB_UPCAST(ret), &vtbl, &ret);
+ upb_byteshandler_init(&ret->input_handler_);
+
+ // 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_refcounted_unref(UPB_UPCAST(ret), &ret);
+
+ ret->group = UPB_UPCAST(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);
+
+ if (ret->dest_handlers_) {
+ upb_handlers_ref(ret->dest_handlers_, ret);
+ }
+ return ret;
+}
+
+void upb_pbdecodermethod_ref(const upb_pbdecodermethod *m, const void *owner) {
+ upb_refcounted_ref(UPB_UPCAST(m), owner);
+}
+
+void upb_pbdecodermethod_unref(const upb_pbdecodermethod *m,
+ const void *owner) {
+ upb_refcounted_unref(UPB_UPCAST(m), owner);
+}
+
+void upb_pbdecodermethod_donateref(const upb_pbdecodermethod *m,
+ const void *from, const void *to) {
+ upb_refcounted_donateref(UPB_UPCAST(m), from, to);
+}
+
+void upb_pbdecodermethod_checkref(const upb_pbdecodermethod *m,
+ const void *owner) {
+ upb_refcounted_checkref(UPB_UPCAST(m), owner);
+}
+
+const upb_handlers *upb_pbdecodermethod_desthandlers(
+ const upb_pbdecodermethod *m) {
+ return m->dest_handlers_;
+}
+
+const upb_byteshandler *upb_pbdecodermethod_inputhandler(
+ const upb_pbdecodermethod *m) {
+ return &m->input_handler_;
+}
+
+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) {
+ upb_pbcodecache cache;
+ upb_pbcodecache_init(&cache);
+ const upb_pbdecodermethod *ret =
+ upb_pbcodecache_getdecodermethod(&cache, opts);
+ upb_pbdecodermethod_ref(ret, owner);
+ upb_pbcodecache_uninit(&cache);
+ return ret;
+}
+
+
+/* bytecode compiler **********************************************************/
+
+// Data used only at compilation time.
+typedef struct {
+ mgroup *group;
+
+ 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 = malloc(sizeof(*ret));
+ ret->group = group;
+ ret->lazy = lazy;
+ for (int i = 0; i < MAXLABEL; i++) {
+ ret->fwd_labels[i] = EMPTYLABEL;
+ ret->back_labels[i] = EMPTYLABEL;
+ }
+ return ret;
+}
+
+static void freecompiler(compiler *c) {
+ free(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:
+ 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);
+ }
+ 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) {
+ assert(label < MAXLABEL);
+ int val = c->fwd_labels[label];
+ uint32_t *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) {
+ 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 = realloc(g->bytecode, newsize * sizeof(uint32_t));
+ g->bytecode_end = g->bytecode + newsize;
+ c->pc = g->bytecode + ofs;
+ }
+ *c->pc++ = v;
+}
+
+static void putop(compiler *c, opcode 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:
+ 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);
+ 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 OP(op) [OP_ ## op] = "OP_" #op
+#define T(op) OP(PARSE_##op)
+ static const char *names[] = {
+ "<no opcode>",
+ 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(HALT),
+ };
+ return op > OP_HALT ? names[0] : names[op];
+#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_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.
+ 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_VAR(ok, 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);
+ 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);
+
+ if (!sub_m) {
+ // Don't emit any code for this field at all; it will be parsed as an
+ // unknown field.
+ return;
+ }
+
+ label(c, LABEL_FIELD);
+
+ int 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) {
+ label(c, LABEL_FIELD);
+
+ const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
+ upb_descriptortype_t descriptor_type = upb_fielddef_descriptortype(f);
+
+ // From a decoding perspective, ENUM is the same as INT32.
+ if (descriptor_type == UPB_DESCRIPTOR_TYPE_ENUM)
+ descriptor_type = UPB_DESCRIPTOR_TYPE_INT32;
+
+ opcode 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.
+
+ assert((int)parse_type >= 0 && parse_type <= OP_MAX);
+ upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
+ int 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) {
+ assert(method);
+
+ // Clear all entries in the dispatch table.
+ upb_inttable_uninit(&method->dispatch);
+ upb_inttable_init(&method->dispatch, UPB_CTYPE_UINT64);
+
+ const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
+ const upb_msgdef *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);
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, md); !upb_msg_done(&i); upb_msg_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);
+ }
+ }
+
+ // 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);
+ upb_value 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;
+ if (upb_inttable_lookupptr(&c->group->methods, h, &v))
+ return;
+ newmethod(h, c->group);
+
+ // Find submethods.
+ upb_msg_iter i;
+ const upb_msgdef *md = upb_handlers_msgdef(h);
+ for(upb_msg_begin(&i, md); !upb_msg_done(&i); upb_msg_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) {
+ // Start over at the beginning of the bytecode.
+ c->pc = c->group->bytecode;
+
+ upb_inttable_iter i;
+ 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));
+
+ m->code_base.ptr = g->bytecode + m->code_base.ofs;
+
+ upb_byteshandler *h = &m->input_handler_;
+ 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) {
+ UPB_UNUSED(allowjit);
+ assert(upb_handlers_isfrozen(dest));
+
+ mgroup *g = newgroup(owner);
+ compiler *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", "wb");
+ assert(f);
+ dumpbc(g->bytecode, g->bytecode_end, stderr);
+ dumpbc(g->bytecode, g->bytecode_end, 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));
+ upb_refcounted_unref(UPB_UPCAST(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) {
+ // 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));
+
+ upb_value v;
+ bool ok = upb_inttable_lookupptr(&g->methods, opts->handlers, &v);
+ UPB_ASSERT_VAR(ok, 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 - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2008-2013 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * This file implements a VM for the interpreted (bytecode) decoder.
+ *
+ * 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 <inttypes.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#ifdef UPB_DUMP_BYTECODE
+#include <stdio.h>
+#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.";
+
+// Error messages shared within this file.
+static const char *kUnterminatedVarint = "Unterminated varint.";
+
+/* upb_pbdecoder **************************************************************/
+
+static opcode halt = OP_HALT;
+
+// 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 bool in_residual_buf(const upb_pbdecoder *d, const char *p);
+
+// It's unfortunate that we have to micro-manage the compiler this way,
+// especially since this tuning is necessarily specific to one hardware
+// configuration. But emperically 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.
+#define FORCEINLINE static inline __attribute__((always_inline))
+#define NOINLINE __attribute__((noinline))
+
+static void seterr(upb_pbdecoder *d, const char *msg) {
+ // TODO(haberman): encapsulate this access to pipeline->status, but not sure
+ // exactly what that interface should look like.
+ upb_status_seterrmsg(d->status, msg);
+}
+
+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) {
+ assert(d->data_end >= d->ptr);
+ return d->data_end - d->ptr;
+}
+
+// Overall stream offset of d->ptr.
+uint64_t offset(const upb_pbdecoder *d) {
+ return d->bufstart_ofs + (d->ptr - d->buf);
+}
+
+// Advances d->ptr.
+static void advance(upb_pbdecoder *d, size_t len) {
+ 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 <= (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) {
+ 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.
+ assert(d->checkpoint != d->ptr);
+ d->checkpoint = d->ptr;
+}
+
+// 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->buf_param = buf;
+ d->size_param = size;
+ d->handle = handle;
+ if (d->residual_end > d->residual) {
+ // We have residual bytes from the last buffer.
+ assert(d->ptr == d->residual);
+ } else {
+ switchtobuf(d, buf, buf + size);
+ }
+ d->checkpoint = d->ptr;
+ if (d->top->groupnum < 0) {
+ CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0));
+ d->checkpoint = d->ptr;
+ }
+ 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 {
+ assert(!in_residual_buf(d, d->checkpoint));
+ assert(d->buf == d->buf_param);
+ size_t consumed = d->checkpoint - d->buf;
+ d->bufstart_ofs += consumed;
+ d->residual_end = d->residual;
+ switchtobuf(d, d->residual, d->residual_end);
+ return consumed;
+ }
+}
+
+// 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.
+ 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.
+ assert(!in_residual_buf(d, d->checkpoint));
+ d->ptr = d->checkpoint;
+ size_t save = curbufleft(d);
+ assert(save <= sizeof(d->residual));
+ memcpy(d->residual, d->ptr, save);
+ d->residual_end = d->residual + save;
+ d->bufstart_ofs = offset(d);
+ }
+
+ switchtobuf(d, d->residual, d->residual_end);
+ return d->size_param;
+}
+
+// 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 the caller should skip before passing a new buffer.
+static int32_t skip(upb_pbdecoder *d, size_t bytes) {
+ assert(!in_residual_buf(d, d->ptr) || d->size_param == 0);
+ if (curbufleft(d) >= bytes) {
+ // Skipped data is all in current buffer.
+ advance(d, bytes);
+ return DECODE_OK;
+ } else {
+ // Skipped data extends beyond currently available buffers.
+ d->pc = d->last;
+ size_t skip = bytes - curbufleft(d);
+ d->bufstart_ofs += (d->end - d->buf) + skip;
+ d->residual_end = d->residual;
+ switchtobuf(d, d->residual, d->residual_end);
+ return d->size_param + skip;
+ }
+}
+
+// Copies the next "bytes" bytes into "buf" and advances the stream.
+// Requires that this many bytes are available in the current buffer.
+FORCEINLINE void consumebytes(upb_pbdecoder *d, void *buf, size_t bytes) {
+ 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.
+static NOINLINE int32_t getbytes_slow(upb_pbdecoder *d, void *buf,
+ size_t bytes) {
+ const size_t avail = curbufleft(d);
+ consumebytes(d, buf, avail);
+ bytes -= avail;
+ assert(bytes > 0);
+ if (in_residual_buf(d, d->ptr)) {
+ advancetobuf(d, d->buf_param, d->size_param);
+ }
+ if (curbufleft(d) >= bytes) {
+ consumebytes(d, 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.
+FORCEINLINE 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 NOINLINE 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(buf + ret, d->buf_param, copy);
+ ret += copy;
+ }
+ return ret;
+}
+
+FORCEINLINE 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);
+ }
+}
+
+
+/* Decoding of wire types *****************************************************/
+
+// Slow path for decoding a varint from the current buffer position.
+// Returns a status code as described in decoder.int.h.
+NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d,
+ uint64_t *u64) {
+ *u64 = 0;
+ uint8_t byte = 0x80;
+ int bitpos;
+ for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
+ int32_t ret = getbytes(d, &byte, 1);
+ if (ret >= 0) return ret;
+ *u64 |= (uint64_t)(byte & 0x7F) << bitpos;
+ }
+ if(bitpos == 70 && (byte & 0x80)) {
+ seterr(d, kUnterminatedVarint);
+ return upb_pbdecoder_suspend(d);
+ }
+ return DECODE_OK;
+}
+
+// Decodes a varint from the current buffer position.
+// Returns a status code as described in decoder.int.h.
+FORCEINLINE 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);
+ }
+}
+
+// Decodes a 32-bit varint from the current buffer position.
+// Returns a status code as described in decoder.int.h.
+FORCEINLINE 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);
+ }
+ *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.
+FORCEINLINE int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) {
+ return getbytes(d, u32, 4);
+}
+
+// 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.
+FORCEINLINE int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) {
+ return getbytes(d, u64, 8);
+}
+
+// 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);
+}
+
+int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) {
+ return decode_fixed64(d, u64);
+}
+
+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; }
+
+// 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, "Submessage end extends past enclosing submessage.");
+ return false;
+ } else if ((fr + 1) == d->limit) {
+ seterr(d, kPbDecoderStackOverflow);
+ return false;
+ }
+
+ fr++;
+ fr->end_ofs = end;
+ fr->dispatch = NULL;
+ fr->groupnum = 0;
+ d->top = fr;
+ return true;
+}
+
+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--; }
+
+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_VAR(ok, ok < 0);
+ return DECODE_OK;
+ } else if (read < bytes && memcmp(&data, &expected, read) == 0) {
+ return suspend_save(d);
+ } else {
+ return DECODE_MISMATCH;
+ }
+}
+
+int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
+ uint8_t wire_type) {
+ if (fieldnum >= 0)
+ goto have_tag;
+
+ while (true) {
+ uint32_t tag;
+ CHECK_RETURN(decode_v32(d, &tag));
+ wire_type = tag & 0x7;
+ fieldnum = tag >> 3;
+
+have_tag:
+ if (fieldnum == 0) {
+ seterr(d, "Saw invalid field number (0)");
+ return upb_pbdecoder_suspend(d);
+ }
+
+ // TODO: deliver to unknown field callback.
+ 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 (d->top->groupnum >= 0) {
+ return DECODE_OK;
+ }
+
+ if (d->ptr == d->delim_end) {
+ seterr(d, "Enclosing submessage ended in the middle of value or group");
+ // Unlike most errors we notice during parsing, right now we have consumed
+ // all of the user's input.
+ //
+ // There are three different options for how to handle this case:
+ //
+ // 1. decode() = short count, error = set
+ // 2. decode() = full count, error = set
+ // 3. decode() = full count, error NOT set, short count and error will
+ // be reported on next call to decode() (or end())
+ //
+ // (1) and (3) have the advantage that they preserve the invariant that an
+ // error occurs iff decode() returns a short count.
+ //
+ // (2) and (3) have the advantage of reflecting the fact that all of the
+ // bytes were in fact parsed (and possibly delivered to the unknown field
+ // handler, in the future when that is supported).
+ //
+ // (3) requires extra state in the decode (a place to store the "permanent
+ // error" that we should return for all subsequent attempts to decode).
+ // But we likely want this anyway.
+ //
+ // Right now we do (1), thanks to the fact that we checkpoint *after* this
+ // check. (3) may be a better choice long term; unclear at the moment.
+ return upb_pbdecoder_suspend(d);
+ }
+
+ checkpoint(d);
+ }
+}
+
+static void goto_endmsg(upb_pbdecoder *d) {
+ upb_value v;
+ bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v);
+ UPB_ASSERT_VAR(found, 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;
+
+ // Decode tag.
+ uint32_t tag;
+ CHECK_RETURN(decode_v32(d, &tag));
+ uint8_t wire_type = tag & 0x7;
+ uint32_t fieldnum = tag >> 3;
+
+ // Lookup tag. Because of packed/non-packed compatibility, we have to
+ // check the wire type against two possibilities.
+ upb_value val;
+ 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_VAR(found, found);
+ d->pc = d->top->base + upb_value_getuint64(val);
+ return DECODE_OK;
+ }
+ }
+
+ // Unknown field or ENDGROUP.
+ int32_t ret = upb_pbdecoder_skipunknown(d, fieldnum, wire_type);
+
+ if (ret == DECODE_ENDGROUP) {
+ goto_endmsg(d);
+ return DECODE_OK;
+ } else {
+ d->pc = d->last - 1; // Rewind to CHECKDELIM.
+ return ret;
+ }
+}
+
+// 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) {
+ assert(d->top != d->stack);
+ return d->top - 1;
+}
+
+
+/* The main decoding loop *****************************************************/
+
+// The main decoder VM function. Uses traditional bytecode dispatch loop with a
+// switch() statement.
+size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
+ size_t size, const upb_bufhandle *handle) {
+ upb_pbdecoder *d = closure;
+ const mgroup *group = hd;
+ assert(buf);
+ int32_t result = upb_pbdecoder_resume(d, NULL, buf, size, handle);
+ if (result == DECODE_ENDGROUP) {
+ goto_endmsg(d);
+ }
+ CHECK_RETURN(result);
+ UPB_UNUSED(group);
+
+#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)); \
+ })
+
+ while(1) {
+ d->last = d->pc;
+ int32_t instruction = *d->pc++;
+ opcode op = getop(instruction);
+ uint32_t arg = instruction >> 8;
+ int32_t longofs = arg;
+ assert(d->ptr != d->residual_end);
+#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)
+
+ 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 = d->top->end_ofs - offset(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 > d->top->end_ofs - offset(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.
+ 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,
+ 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.
+ 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,
+ assert(d->call_len > 0);
+ d->pc = d->callstack[--d->call_len];
+ )
+ VMCASE(OP_BRANCH,
+ d->pc += longofs;
+ )
+ VMCASE(OP_TAG1,
+ CHECK_SUSPEND(curbufleft(d) > 0);
+ uint8_t 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,
+ CHECK_SUSPEND(curbufleft(d) > 0);
+ uint16_t 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;
+ memcpy(&expected, d->pc, 8);
+ d->pc += 2;
+ int32_t result = upb_pbdecoder_checktag_slow(d, expected);
+ if (result == DECODE_MISMATCH) goto badtag;
+ if (result >= 0) return result;
+ })
+ VMCASE(OP_HALT, {
+ return size;
+ })
+ }
+ }
+}
+
+void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
+ upb_pbdecoder *d = closure;
+ UPB_UNUSED(size_hint);
+ d->call_len = 1;
+ d->pc = pc;
+ return d;
+}
+
+void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(size_hint);
+ upb_pbdecoder *d = closure;
+ d->call_len = 0;
+ return d;
+}
+
+bool upb_pbdecoder_end(void *closure, const void *handler_data) {
+ upb_pbdecoder *d = closure;
+ const upb_pbdecodermethod *method = handler_data;
+
+ if (d->residual_end > d->residual) {
+ seterr(d, "Unexpected EOF");
+ return false;
+ }
+
+ if (d->top->end_ofs != UINT64_MAX) {
+ seterr(d, "Unexpected EOF inside delimited string");
+ return false;
+ }
+
+ // Message ends here.
+ uint64_t end = offset(d);
+ d->top->end_ofs = end;
+
+ char dummy;
+#ifdef UPB_USE_JIT_X64
+ const mgroup *group = (const mgroup*)method->group;
+ if (group->jit_code) {
+ if (d->top != d->stack)
+ d->stack->end_ofs = 0;
+ group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
+ } else {
+#endif
+ d->stack->end_ofs = end;
+ const uint32_t *p = d->pc;
+ // 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.
+ assert(getop(*d->pc) == OP_TAG1 ||
+ getop(*d->pc) == OP_TAG2 ||
+ getop(*d->pc) == OP_TAGN);
+ d->pc = p;
+ }
+ upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL);
+#ifdef UPB_USE_JIT_X64
+ }
+#endif
+
+ if (d->call_len != 0) {
+ seterr(d, "Unexpected EOF");
+ return false;
+ }
+
+ return true;
+}
+
+void upb_pbdecoder_init(upb_pbdecoder *d, const upb_pbdecodermethod *m,
+ upb_status *s) {
+ d->limit = &d->stack[UPB_DECODER_MAX_NESTING];
+ upb_bytessink_reset(&d->input_, &m->input_handler_, d);
+ d->method_ = m;
+ d->callstack[0] = &halt;
+ d->status = s;
+ upb_pbdecoder_reset(d);
+}
+
+void upb_pbdecoder_reset(upb_pbdecoder *d) {
+ d->top = d->stack;
+ d->top->end_ofs = UINT64_MAX;
+ d->top->groupnum = 0;
+ d->bufstart_ofs = 0;
+ d->ptr = d->residual;
+ d->buf = d->residual;
+ d->end = d->residual;
+ d->residual_end = d->residual;
+ d->call_len = 1;
+}
+
+uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d) {
+ return offset(d);
+}
+
+// Not currently required, but to support outgrowing the static stack we need
+// this.
+void upb_pbdecoder_uninit(upb_pbdecoder *d) {
+ UPB_UNUSED(d);
+}
+
+const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) {
+ return d->method_;
+}
+
+bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink* sink) {
+ // TODO(haberman): do we need to test whether the decoder is already on the
+ // stack (like calling this from within a callback)? Should we support
+ // rebinding the output at all?
+ assert(sink);
+ if (d->method_->dest_handlers_) {
+ if (sink->handlers != d->method_->dest_handlers_)
+ return false;
+ }
+ upb_sink_reset(&d->top->sink, sink->handlers, sink->closure);
+ return true;
+}
+
+upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) {
+ return &d->input_;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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.
+ */
+
+
+#include <stdlib.h>
+
+/* low-level buffering ********************************************************/
+
+// Low-level functions for interacting with the output buffer.
+
+// 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_VAR(n, n == len);
+}
+
+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 ((e->limit - e->ptr) < bytes) {
+ size_t needed = bytes + (e->ptr - e->buf);
+ size_t old_size = e->limit - e->buf;
+ size_t new_size = old_size;
+ while (new_size < needed) {
+ new_size *= 2;
+ }
+
+ char *realloc_from = (e->buf == e->initbuf) ? NULL : e->buf;
+ char *new_buf = realloc(realloc_from, new_size);
+
+ if (new_buf == NULL) {
+ return false;
+ }
+
+ if (realloc_from == NULL) {
+ memcpy(new_buf, e->initbuf, old_size);
+ }
+
+ 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;
+ }
+
+ 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) {
+ assert((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;
+}
+
+// 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;
+ }
+
+ memcpy(e->ptr, data, len);
+ encoder_advance(e, len);
+ return true;
+}
+
+// Finish the current run by adding the run totals to the segment and message
+// length.
+static void accumulate(upb_pb_encoder *e) {
+ assert(e->ptr >= e->runbegin);
+ size_t run_len = e->ptr - e->runbegin;
+ e->segptr->seglen += run_len;
+ top(e)->msglen += run_len;
+ e->runbegin = e->ptr;
+}
+
+// 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) {
+ upb_pb_encoder_segment *realloc_from =
+ (e->segbuf == e->seginitbuf) ? NULL : e->segbuf;
+ 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 = realloc(realloc_from, new_size);
+
+ if (new_buf == NULL) {
+ return false;
+ }
+
+ if (realloc_from == NULL) {
+ memcpy(new_buf, e->seginitbuf, old_size);
+ }
+
+ 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;
+ }
+
+ *e->top = e->segptr - e->segbuf;
+ e->segptr->seglen = 0;
+ e->segptr->msglen = 0;
+
+ 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) {
+ accumulate(e);
+ size_t 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;
+ }
+
+ 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);
+ }
+
+ 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 = malloc(sizeof(tag_t));
+ tag->bytes = upb_vencode64((n << 3) | wt, tag->tag);
+
+ upb_handlerattr_init(attr);
+ upb_handlerattr_sethandlerdata(attr, tag);
+ upb_handlers_addcleanup(h, tag, free);
+}
+
+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;
+}
+
+static uint64_t dbl2uint64(double d) {
+ uint64_t ret;
+ memcpy(&ret, &d, sizeof(uint64_t));
+ return ret;
+}
+
+static uint32_t flt2uint32(float d) {
+ uint32_t ret;
+ memcpy(&ret, &d, sizeof(uint32_t));
+ return ret;
+}
+
+
+/* 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_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);
+}
+
+static void *encode_startstr(void *c, const void *hd, size_t size_hint) {
+ UPB_UNUSED(size_hint);
+ return encode_startdelimfield(c, hd);
+}
+
+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;
+}
+
+#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, uint32_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);
+
+#undef T
+
+
+/* code to build the handlers *************************************************/
+
+static void newhandlers_callback(const void *closure, upb_handlers *h) {
+ UPB_UNUSED(closure);
+
+ upb_handlers_setstartmsg(h, startmsg, NULL);
+ upb_handlers_setendmsg(h, endmsg, NULL);
+
+ const upb_msgdef *m = upb_handlers_msgdef(h);
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_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)];
+
+ // Pre-encode the tag for this field.
+ new_tag(h, f, wt, &attr);
+
+ if (packed) {
+ upb_handlers_setstartseq(h, f, encode_startdelimfield, &attr);
+ upb_handlers_setendseq(h, f, encode_enddelimfield, &attr);
+ }
+
+#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;
+
+ 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);
+
+ upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr);
+ upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2);
+
+ upb_handlerattr_uninit(&attr2);
+ break;
+ }
+ }
+
+#undef T
+
+ upb_handlerattr_uninit(&attr);
+ }
+}
+
+
+/* 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);
+}
+
+#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
+
+void upb_pb_encoder_init(upb_pb_encoder *e, const upb_handlers *h) {
+ e->output_ = NULL;
+ e->subc = NULL;
+ e->buf = e->initbuf;
+ e->ptr = e->buf;
+ e->limit = e->buf + ARRAYSIZE(e->initbuf);
+ e->segbuf = e->seginitbuf;
+ e->seglimit = e->segbuf + ARRAYSIZE(e->seginitbuf);
+ e->stacklimit = e->stack + ARRAYSIZE(e->stack);
+ upb_sink_reset(&e->input_, h, e);
+}
+
+void upb_pb_encoder_uninit(upb_pb_encoder *e) {
+ if (e->buf != e->initbuf) {
+ free(e->buf);
+ }
+
+ if (e->segbuf != e->seginitbuf) {
+ free(e->segbuf);
+ }
+}
+
+void upb_pb_encoder_resetoutput(upb_pb_encoder *e, upb_bytessink *output) {
+ upb_pb_encoder_reset(e);
+ e->output_ = output;
+ e->subc = output->closure;
+}
+
+void upb_pb_encoder_reset(upb_pb_encoder *e) {
+ e->segptr = NULL;
+ e->top = NULL;
+ e->depth = 0;
+}
+
+upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; }
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n,
+ void *owner, upb_status *status) {
+ // Create handlers.
+ const upb_handlers *reader_h = upb_descreader_newhandlers(&reader_h);
+ upb_pbdecodermethodopts opts;
+ upb_pbdecodermethodopts_init(&opts, reader_h);
+ const upb_pbdecodermethod *decoder_m =
+ upb_pbdecodermethod_new(&opts, &decoder_m);
+
+ upb_pbdecoder decoder;
+ upb_descreader reader;
+
+ upb_pbdecoder_init(&decoder, decoder_m, status);
+ upb_descreader_init(&reader, reader_h, status);
+ upb_pbdecoder_resetoutput(&decoder, upb_descreader_input(&reader));
+
+ // Push input data.
+ bool ok = upb_bufsrc_putbuf(str, len, upb_pbdecoder_input(&decoder));
+
+ upb_def **ret = NULL;
+
+ if (!ok) goto cleanup;
+ upb_def **defs = upb_descreader_getdefs(&reader, owner, n);
+ ret = malloc(sizeof(upb_def*) * (*n));
+ memcpy(ret, defs, sizeof(upb_def*) * (*n));
+
+cleanup:
+ upb_pbdecoder_uninit(&decoder);
+ upb_descreader_uninit(&reader);
+ upb_handlers_unref(reader_h, &reader_h);
+ upb_pbdecodermethod_unref(decoder_m, &decoder_m);
+ return ret;
+}
+
+bool upb_load_descriptor_into_symtab(upb_symtab *s, const char *str, size_t len,
+ upb_status *status) {
+ int n;
+ upb_def **defs = upb_load_defs_from_descriptor(str, len, &n, &defs, status);
+ if (!defs) return false;
+ bool success = upb_symtab_add(s, defs, n, &defs, status);
+ free(defs);
+ return success;
+}
+
+char *upb_readfile(const char *filename, size_t *len) {
+ FILE *f = fopen(filename, "rb");
+ if(!f) return NULL;
+ if(fseek(f, 0, SEEK_END) != 0) goto error;
+ long size = ftell(f);
+ if(size < 0) goto error;
+ if(fseek(f, 0, SEEK_SET) != 0) goto error;
+ char *buf = malloc(size + 1);
+ if(size && fread(buf, size, 1, f) != 1) goto error;
+ fclose(f);
+ if (len) *len = size;
+ return buf;
+
+error:
+ fclose(f);
+ return NULL;
+}
+
+bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname,
+ upb_status *status) {
+ size_t len;
+ char *data = upb_readfile(fname, &len);
+ if (!data) {
+ if (status) upb_status_seterrf(status, "Couldn't read file: %s", fname);
+ return false;
+ }
+ bool success = upb_load_descriptor_into_symtab(symtab, data, len, status);
+ free(data);
+ return success;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * OPT: This is not optimized at all. It uses printf() which parses the format
+ * string every time, and it allocates memory for every put.
+ */
+
+
+#include <ctype.h>
+#include <float.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#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 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;
+}
+
+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 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;
+
+ // 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
+
+ for (; buf < end; buf++) {
+ if (dstend - dst < 4) {
+ upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
+ dst = dstbuf;
+ }
+
+ bool 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;
+}
+
+bool putf(upb_textprinter *p, const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+
+ // Run once to get the length of the string.
+ va_list args_copy;
+ va_copy(args_copy, args);
+ int len = vsnprintf(NULL, 0, fmt, args_copy);
+ va_end(args_copy);
+
+ // + 1 for NULL terminator (vsnprintf() requires it even if we don't).
+ char *str = malloc(len + 1);
+ if (!str) return false;
+ int written = vsnprintf(str, len + 1, fmt, args);
+ va_end(args);
+ UPB_ASSERT_VAR(written, written == len);
+
+ bool ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL);
+ free(str);
+ return ok;
+}
+
+
+/* handlers *******************************************************************/
+
+static bool textprinter_startmsg(void *c, const void *hd) {
+ UPB_UNUSED(hd);
+ upb_textprinter *p = c;
+ if (p->indent_depth_ == 0) {
+ upb_bytessink_start(p->output_, 0, &p->subc);
+ }
+ return true;
+}
+
+static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(s);
+ upb_textprinter *p = c;
+ if (p->indent_depth_ == 0) {
+ upb_bytessink_end(p->output_);
+ }
+ return true;
+}
+
+#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;
+}
+
+#define STRINGIFY_HELPER(x) #x
+#define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x)
+
+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")
+
+#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_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;
+}
+
+static void *textprinter_startstr(void *closure, const void *handler_data,
+ size_t size_hint) {
+ const upb_fielddef *f = handler_data;
+ UPB_UNUSED(size_hint);
+ upb_textprinter *p = closure;
+ indent(p);
+ putf(p, "%s: \"", upb_fielddef_name(f));
+ return p;
+}
+
+static bool textprinter_endstr(void *closure, const void *handler_data) {
+ UPB_UNUSED(handler_data);
+ upb_textprinter *p = closure;
+ putf(p, "\"");
+ endfield(p);
+ return true;
+}
+
+static size_t textprinter_putstr(void *closure, const void *hd, const char *buf,
+ size_t len, const upb_bufhandle *handle) {
+ UPB_UNUSED(handle);
+ upb_textprinter *p = closure;
+ const upb_fielddef *f = hd;
+ CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING));
+ return len;
+err:
+ return 0;
+}
+
+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 textprinter_endsubmsg(void *closure, const void *handler_data) {
+ UPB_UNUSED(handler_data);
+ upb_textprinter *p = closure;
+ p->indent_depth_--;
+ CHECK(indent(p));
+ upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL);
+ CHECK(endfield(p));
+ return true;
+err:
+ return false;
+}
+
+
+/* Public API *****************************************************************/
+
+void upb_textprinter_init(upb_textprinter *p, const upb_handlers *h) {
+ p->single_line_ = false;
+ p->indent_depth_ = 0;
+ upb_sink_reset(&p->input_, h, p);
+}
+
+void upb_textprinter_uninit(upb_textprinter *p) {
+ UPB_UNUSED(p);
+}
+
+void upb_textprinter_reset(upb_textprinter *p, bool single_line) {
+ p->single_line_ = single_line;
+ p->indent_depth_ = 0;
+}
+
+static void onmreg(const void *c, upb_handlers *h) {
+ UPB_UNUSED(c);
+ const upb_msgdef *m = upb_handlers_msgdef(h);
+
+ upb_handlers_setstartmsg(h, textprinter_startmsg, NULL);
+ upb_handlers_setendmsg(h, textprinter_endmsg, NULL);
+
+ upb_msg_iter i;
+ for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_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;
+ }
+ }
+}
+
+const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
+ const void *owner) {
+ return upb_handlers_newfrozen(m, owner, &onmreg, NULL);
+}
+
+upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; }
+
+bool upb_textprinter_resetoutput(upb_textprinter *p, upb_bytessink *output) {
+ p->output_ = output;
+ return true;
+}
+
+void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) {
+ p->single_line_ = single_line;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+
+// 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;
+
+done:
+ r.val = ((uint64_t)high << 32) | low;
+ r.p = p;
+ return r;
+}
+
+// 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;
+}
+
+// Given an encoded varint v, returns an integer with a single bit set that
+// indicates the end of the varint. Subtracting one from this value will
+// yield a mask that leaves only bits that are part of the varint. Returns
+// 0 if the varint is unterminated.
+static uint64_t upb_get_vstopbit(uint64_t v) {
+ uint64_t cbits = v | 0x7f7f7f7f7f7f7f7fULL;
+ return ~cbits & (cbits+1);
+}
+
+// A branchless decoder. Credit to Pascal Massimino for the bit-twiddling.
+upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r) {
+ uint64_t b;
+ memcpy(&b, r.p, sizeof(b));
+ uint64_t stop_bit = upb_get_vstopbit(b);
+ b = (b & 0x7f7f7f7f7f7f7f7fULL) & (stop_bit - 1);
+ b += b & 0x007f007f007f007fULL;
+ b += 3 * (b & 0x0000ffff0000ffffULL);
+ b += 15 * (b & 0x00000000ffffffffULL);
+ if (stop_bit == 0) {
+ // Error: unterminated varint.
+ upb_decoderet err_r = {(void*)0, 0};
+ return err_r;
+ }
+ upb_decoderet my_r = {r.p + ((__builtin_ctzll(stop_bit) + 1) / 8),
+ r.val | (b << 7)};
+ return my_r;
+}
+
+// A branchless decoder. Credit to Daniel Wright for the bit-twiddling.
+upb_decoderet upb_vdecode_max8_wright(upb_decoderet r) {
+ uint64_t b;
+ memcpy(&b, r.p, sizeof(b));
+ uint64_t stop_bit = upb_get_vstopbit(b);
+ b &= (stop_bit - 1);
+ b = ((b & 0x7f007f007f007f00ULL) >> 1) | (b & 0x007f007f007f007fULL);
+ b = ((b & 0xffff0000ffff0000ULL) >> 2) | (b & 0x0000ffff0000ffffULL);
+ b = ((b & 0xffffffff00000000ULL) >> 4) | (b & 0x00000000ffffffffULL);
+ if (stop_bit == 0) {
+ // Error: unterminated varint.
+ upb_decoderet err_r = {(void*)0, 0};
+ return err_r;
+ }
+ upb_decoderet my_r = {r.p + ((__builtin_ctzll(stop_bit) + 1) / 8),
+ r.val | (b << 14)};
+ return my_r;
+}
+
+#line 1 "upb/json/parser.rl"
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+
+#define PARSER_CHECK_RETURN(x) if (!(x)) return false
+
+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_VAR(ok, ok);
+ return sel;
+}
+
+static upb_selector_t parser_getsel(upb_json_parser *p) {
+ return getsel_for_handlertype(
+ p, upb_handlers_getprimitivehandlertype(p->top->f));
+}
+
+static void start_member(upb_json_parser *p) {
+ assert(!p->top->f);
+ assert(!p->accumulated);
+ p->accumulated_len = 0;
+}
+
+static bool end_member(upb_json_parser *p) {
+ // TODO(haberman): support keys that span buffers or have escape sequences.
+ assert(!p->top->f);
+ assert(p->accumulated);
+ const upb_fielddef *f =
+ upb_msgdef_ntof(p->top->m, p->accumulated, p->accumulated_len);
+
+ if (!f) {
+ // TODO(haberman): Ignore unknown fields if requested/configured to do so.
+ upb_status_seterrf(p->status, "No such field: %.*s\n",
+ (int)p->accumulated_len, p->accumulated);
+ return false;
+ }
+
+ p->top->f = f;
+ p->accumulated = NULL;
+
+ return true;
+}
+
+static void start_object(upb_json_parser *p) {
+ upb_sink_startmsg(&p->top->sink);
+}
+
+static void end_object(upb_json_parser *p) {
+ upb_status status;
+ upb_sink_endmsg(&p->top->sink, &status);
+}
+
+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 true;
+}
+
+static bool start_subobject(upb_json_parser *p) {
+ assert(p->top->f);
+
+ if (!upb_fielddef_issubmsg(p->top->f)) {
+ upb_status_seterrf(p->status,
+ "Object specified for non-message/group field: %s",
+ upb_fielddef_name(p->top->f));
+ return false;
+ }
+
+ if (!check_stack(p)) return false;
+
+ upb_jsonparser_frame *inner = p->top + 1;
+
+ upb_selector_t 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);
+ inner->f = NULL;
+ p->top = inner;
+
+ return true;
+}
+
+static void end_subobject(upb_json_parser *p) {
+ p->top--;
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
+ upb_sink_endsubmsg(&p->top->sink, sel);
+}
+
+static bool start_array(upb_json_parser *p) {
+ assert(p->top->f);
+
+ if (!upb_fielddef_isseq(p->top->f)) {
+ upb_status_seterrf(p->status,
+ "Array specified for non-repeated field: %s",
+ upb_fielddef_name(p->top->f));
+ return false;
+ }
+
+ if (!check_stack(p)) return false;
+
+ upb_jsonparser_frame *inner = p->top + 1;
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
+ upb_sink_startseq(&p->top->sink, sel, &inner->sink);
+ inner->m = p->top->m;
+ inner->f = p->top->f;
+ p->top = inner;
+
+ return true;
+}
+
+static void end_array(upb_json_parser *p) {
+ assert(p->top > p->stack);
+
+ p->top--;
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
+ upb_sink_endseq(&p->top->sink, sel);
+}
+
+static void clear_member(upb_json_parser *p) { p->top->f = NULL; }
+
+static bool parser_putbool(upb_json_parser *p, bool val) {
+ 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;
+ }
+
+ bool ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val);
+ UPB_ASSERT_VAR(ok, ok);
+ return true;
+}
+
+static void start_text(upb_json_parser *p, const char *ptr) {
+ p->text_begin = ptr;
+}
+
+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 != '='; }
+
+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) {
+ 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;
+ }
+
+ uint32_t val = b64lookup(ptr[0]) << 18 |
+ b64lookup(ptr[1]) << 12 |
+ b64lookup(ptr[2]) << 6 |
+ b64lookup(ptr[3]);
+
+ // Test the upper bit; returns true if any of the characters returned -1.
+ if (val & 0x80000000) {
+ goto otherchar;
+ }
+
+ char output[3];
+ 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;
+
+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] == '=') {
+ // Last group contains only two input bytes, one output byte.
+ if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
+ goto badpadding;
+ }
+
+ uint32_t val = b64lookup(ptr[0]) << 18 |
+ b64lookup(ptr[1]) << 12;
+
+ assert(!(val & 0x80000000));
+ char output = val >> 16;
+ upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL);
+ return true;
+ } else {
+ // Last group contains only three input bytes, two output bytes.
+ if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
+ goto badpadding;
+ }
+
+ uint32_t val = b64lookup(ptr[0]) << 18 |
+ b64lookup(ptr[1]) << 12 |
+ b64lookup(ptr[2]) << 6;
+
+ char output[2];
+ output[0] = val >> 16;
+ output[1] = (val >> 8) & 0xff;
+ upb_sink_putstring(&p->top->sink, sel, output, 2, NULL);
+ return true;
+ }
+
+badpadding:
+ upb_status_seterrf(p->status,
+ "Incorrect base64 padding for field: %s (%.*s)",
+ upb_fielddef_name(p->top->f),
+ 4, ptr);
+ return false;
+}
+
+static bool end_text(upb_json_parser *p, const char *ptr, bool is_num) {
+ assert(!p->accumulated); // TODO: handle this case.
+ p->accumulated = p->text_begin;
+ p->accumulated_len = ptr - p->text_begin;
+
+ if (p->top->f && upb_fielddef_isstring(p->top->f)) {
+ // This is a string field (as opposed to a member name).
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
+ if (upb_fielddef_type(p->top->f) == UPB_TYPE_BYTES) {
+ PARSER_CHECK_RETURN(base64_push(p, sel, p->accumulated,
+ p->accumulated_len));
+ } else {
+ upb_sink_putstring(&p->top->sink, sel, p->accumulated, p->accumulated_len, NULL);
+ }
+ p->accumulated = NULL;
+ } else if (p->top->f &&
+ upb_fielddef_type(p->top->f) == UPB_TYPE_ENUM &&
+ !is_num) {
+
+ // Enum case: resolve enum symbolic name to integer value.
+ const upb_enumdef *enumdef =
+ (const upb_enumdef*)upb_fielddef_subdef(p->top->f);
+
+ int32_t int_val = 0;
+ if (upb_enumdef_ntoi(enumdef, p->accumulated, p->accumulated_len,
+ &int_val)) {
+ upb_selector_t sel = parser_getsel(p);
+ upb_sink_putint32(&p->top->sink, sel, int_val);
+ } else {
+ upb_status_seterrmsg(p->status, "Enum value name unknown");
+ return false;
+ }
+ p->accumulated = NULL;
+ }
+
+ return true;
+}
+
+static bool start_stringval(upb_json_parser *p) {
+ assert(p->top->f);
+
+ if (upb_fielddef_isstring(p->top->f)) {
+ 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.
+ upb_jsonparser_frame *inner = p->top + 1;
+ upb_selector_t 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;
+
+ return true;
+ } else if (upb_fielddef_type(p->top->f) == UPB_TYPE_ENUM) {
+ // Do nothing -- symbolic enum names in quotes remain in the
+ // current parser frame.
+ return true;
+ } else {
+ upb_status_seterrf(p->status,
+ "String specified for non-string/non-enum field: %s",
+ upb_fielddef_name(p->top->f));
+ return false;
+ }
+
+}
+
+static void end_stringval(upb_json_parser *p) {
+ if (upb_fielddef_isstring(p->top->f)) {
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
+ upb_sink_endstr(&p->top->sink, sel);
+ p->top--;
+ }
+}
+
+static void start_number(upb_json_parser *p, const char *ptr) {
+ start_text(p, ptr);
+ assert(p->accumulated == NULL);
+}
+
+static void end_number(upb_json_parser *p, const char *ptr) {
+ end_text(p, ptr, true);
+ const char *myend = p->accumulated + p->accumulated_len;
+ char *end;
+
+ switch (upb_fielddef_type(p->top->f)) {
+ case UPB_TYPE_ENUM:
+ case UPB_TYPE_INT32: {
+ long val = strtol(p->accumulated, &end, 0);
+ if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putint32(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ case UPB_TYPE_INT64: {
+ long long val = strtoll(p->accumulated, &end, 0);
+ if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putint64(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ case UPB_TYPE_UINT32: {
+ unsigned long val = strtoul(p->accumulated, &end, 0);
+ if (val > UINT32_MAX || errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putuint32(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ case UPB_TYPE_UINT64: {
+ unsigned long long val = strtoull(p->accumulated, &end, 0);
+ if (val > UINT64_MAX || errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putuint64(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ case UPB_TYPE_DOUBLE: {
+ double val = strtod(p->accumulated, &end);
+ if (errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putdouble(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ case UPB_TYPE_FLOAT: {
+ float val = strtof(p->accumulated, &end);
+ if (errno == ERANGE || end != myend)
+ assert(false);
+ else
+ upb_sink_putfloat(&p->top->sink, parser_getsel(p), val);
+ break;
+ }
+ default:
+ assert(false);
+ }
+
+ p->accumulated = 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:
+ assert(0);
+ return 'x';
+ }
+}
+
+static void escape(upb_json_parser *p, const char *ptr) {
+ char ch = escape_char(*ptr);
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
+ upb_sink_putstring(&p->top->sink, sel, &ch, 1, NULL);
+}
+
+static uint8_t hexdigit(char ch) {
+ if (ch >= '0' && ch <= '9') {
+ return ch - '0';
+ } else if (ch >= 'a' && ch <= 'f') {
+ return ch - 'a' + 10;
+ } else {
+ assert(ch >= 'A' && ch <= 'F');
+ return ch - 'A' + 10;
+ }
+}
+
+static void start_hex(upb_json_parser *p, const char *ptr) {
+ start_text(p, ptr);
+}
+
+static void hex(upb_json_parser *p, const char *end) {
+ const char *start = p->text_begin;
+ UPB_ASSERT_VAR(end, end - start == 4);
+ uint16_t codepoint =
+ (hexdigit(start[0]) << 12) |
+ (hexdigit(start[1]) << 8) |
+ (hexdigit(start[2]) << 4) |
+ hexdigit(start[3]);
+ // 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).
+
+ upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
+ upb_sink_putstring(&p->top->sink, sel, utf8, length, NULL);
+}
+
+#define CHECK_RETURN_TOP(x) if (!(x)) goto error
+
+// What follows is the Ragel parser itself. The language is specified in Ragel
+// and the actions call our C functions above.
+
+#line 596 "upb/json/parser.rl"
+
+
+
+#line 514 "upb/json/parser.c"
+static const char _json_actions[] = {
+ 0, 1, 0, 1, 2, 1, 3, 1,
+ 4, 1, 5, 1, 6, 1, 7, 1,
+ 9, 1, 11, 1, 12, 1, 13, 1,
+ 14, 1, 15, 1, 16, 1, 24, 1,
+ 26, 2, 3, 7, 2, 5, 2, 2,
+ 5, 7, 2, 10, 8, 2, 12, 14,
+ 2, 13, 14, 2, 17, 1, 2, 18,
+ 26, 2, 19, 8, 2, 20, 26, 2,
+ 21, 26, 2, 22, 26, 2, 23, 26,
+ 2, 25, 26, 3, 13, 10, 8
+};
+
+static const unsigned char _json_key_offsets[] = {
+ 0, 0, 4, 9, 14, 18, 22, 27,
+ 32, 37, 41, 45, 48, 51, 53, 57,
+ 61, 63, 65, 70, 72, 74, 83, 89,
+ 95, 101, 107, 109, 118, 118, 118, 123,
+ 128, 133, 133, 134, 135, 136, 137, 137,
+ 138, 139, 140, 140, 141, 142, 143, 143,
+ 148, 153, 157, 161, 166, 171, 176, 180,
+ 180, 183, 183, 183
+};
+
+static const char _json_trans_keys[] = {
+ 32, 123, 9, 13, 32, 34, 125, 9,
+ 13, 32, 34, 125, 9, 13, 32, 58,
+ 9, 13, 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, 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, 32, 58, 9, 13, 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
+};
+
+static const char _json_single_lengths[] = {
+ 0, 2, 3, 3, 2, 2, 3, 3,
+ 3, 2, 2, 1, 3, 0, 2, 2,
+ 0, 0, 3, 2, 2, 9, 0, 0,
+ 0, 0, 2, 7, 0, 0, 3, 3,
+ 3, 0, 1, 1, 1, 1, 0, 1,
+ 1, 1, 0, 1, 1, 1, 0, 3,
+ 3, 2, 2, 3, 3, 3, 2, 0,
+ 1, 0, 0, 0
+};
+
+static const char _json_range_lengths[] = {
+ 0, 1, 1, 1, 1, 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, 1, 1,
+ 1, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 1,
+ 1, 1, 1, 1, 1, 1, 1, 0,
+ 1, 0, 0, 0
+};
+
+static const short _json_index_offsets[] = {
+ 0, 0, 4, 9, 14, 18, 22, 27,
+ 32, 37, 41, 45, 48, 52, 54, 58,
+ 62, 64, 66, 71, 74, 77, 87, 91,
+ 95, 99, 103, 106, 115, 116, 117, 122,
+ 127, 132, 133, 135, 137, 139, 141, 142,
+ 144, 146, 148, 149, 151, 153, 155, 156,
+ 161, 166, 170, 174, 179, 184, 189, 193,
+ 194, 197, 198, 199
+};
+
+static const char _json_indicies[] = {
+ 0, 2, 0, 1, 3, 4, 5, 3,
+ 1, 6, 7, 8, 6, 1, 9, 10,
+ 9, 1, 11, 12, 11, 1, 12, 1,
+ 1, 12, 13, 14, 15, 16, 14, 1,
+ 17, 18, 8, 17, 1, 18, 7, 18,
+ 1, 19, 20, 21, 1, 20, 21, 1,
+ 23, 24, 24, 22, 25, 1, 24, 24,
+ 25, 22, 26, 26, 27, 1, 27, 1,
+ 27, 22, 23, 24, 24, 21, 22, 29,
+ 30, 28, 32, 33, 31, 34, 34, 34,
+ 34, 34, 34, 34, 34, 35, 1, 36,
+ 36, 36, 1, 37, 37, 37, 1, 38,
+ 38, 38, 1, 39, 39, 39, 1, 41,
+ 42, 40, 43, 44, 45, 46, 47, 48,
+ 49, 44, 1, 50, 51, 53, 54, 1,
+ 53, 52, 55, 56, 54, 55, 1, 56,
+ 1, 1, 56, 52, 57, 58, 1, 59,
+ 1, 60, 1, 61, 1, 62, 63, 1,
+ 64, 1, 65, 1, 66, 67, 1, 68,
+ 1, 69, 1, 70, 71, 72, 73, 71,
+ 1, 74, 75, 76, 74, 1, 77, 78,
+ 77, 1, 79, 80, 79, 1, 80, 1,
+ 1, 80, 81, 82, 83, 84, 82, 1,
+ 85, 86, 76, 85, 1, 86, 75, 86,
+ 1, 87, 88, 88, 1, 1, 1, 1,
+ 0
+};
+
+static const char _json_trans_targs[] = {
+ 1, 0, 2, 3, 4, 56, 3, 4,
+ 56, 5, 6, 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, 29, 30, 34, 39,
+ 43, 47, 59, 59, 31, 30, 33, 31,
+ 32, 59, 35, 36, 37, 38, 59, 40,
+ 41, 42, 59, 44, 45, 46, 59, 48,
+ 49, 55, 48, 49, 55, 50, 51, 50,
+ 51, 52, 53, 54, 55, 53, 54, 59,
+ 56
+};
+
+static const char _json_trans_actions[] = {
+ 0, 0, 0, 21, 75, 48, 0, 42,
+ 23, 17, 17, 0, 0, 15, 19, 19,
+ 45, 0, 0, 0, 0, 0, 1, 0,
+ 0, 0, 0, 0, 3, 13, 0, 0,
+ 33, 5, 11, 0, 7, 0, 0, 0,
+ 36, 39, 9, 57, 51, 25, 0, 0,
+ 0, 29, 60, 54, 15, 0, 27, 0,
+ 0, 31, 0, 0, 0, 0, 66, 0,
+ 0, 0, 69, 0, 0, 0, 63, 21,
+ 75, 48, 0, 42, 23, 17, 17, 0,
+ 0, 15, 19, 19, 45, 0, 0, 72,
+ 0
+};
+
+static const int json_start = 1;
+static const int json_first_final = 56;
+static const int json_error = 0;
+
+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_main = 1;
+
+
+#line 599 "upb/json/parser.rl"
+
+size_t parse(void *closure, const void *hd, const char *buf, size_t size,
+ const upb_bufhandle *handle) {
+ UPB_UNUSED(hd);
+ UPB_UNUSED(handle);
+ upb_json_parser *parser = closure;
+
+ // Variables used by Ragel's generated code.
+ int cs = parser->current_state;
+ int *stack = parser->parser_stack;
+ int top = parser->parser_top;
+
+ const char *p = buf;
+ const char *pe = buf + size;
+
+
+#line 684 "upb/json/parser.c"
+ {
+ int _klen;
+ unsigned int _trans;
+ const char *_acts;
+ unsigned int _nacts;
+ const char *_keys;
+
+ if ( p == pe )
+ goto _test_eof;
+ if ( cs == 0 )
+ goto _out;
+_resume:
+ _keys = _json_trans_keys + _json_key_offsets[cs];
+ _trans = _json_index_offsets[cs];
+
+ _klen = _json_single_lengths[cs];
+ if ( _klen > 0 ) {
+ const char *_lower = _keys;
+ const char *_mid;
+ const char *_upper = _keys + _klen - 1;
+ while (1) {
+ if ( _upper < _lower )
+ break;
+
+ _mid = _lower + ((_upper-_lower) >> 1);
+ if ( (*p) < *_mid )
+ _upper = _mid - 1;
+ else if ( (*p) > *_mid )
+ _lower = _mid + 1;
+ else {
+ _trans += (unsigned int)(_mid - _keys);
+ goto _match;
+ }
+ }
+ _keys += _klen;
+ _trans += _klen;
+ }
+
+ _klen = _json_range_lengths[cs];
+ if ( _klen > 0 ) {
+ const char *_lower = _keys;
+ const char *_mid;
+ const char *_upper = _keys + (_klen<<1) - 2;
+ while (1) {
+ if ( _upper < _lower )
+ break;
+
+ _mid = _lower + (((_upper-_lower) >> 1) & ~1);
+ if ( (*p) < _mid[0] )
+ _upper = _mid - 2;
+ else if ( (*p) > _mid[1] )
+ _lower = _mid + 2;
+ else {
+ _trans += (unsigned int)((_mid - _keys)>>1);
+ goto _match;
+ }
+ }
+ _trans += _klen;
+ }
+
+_match:
+ _trans = _json_indicies[_trans];
+ cs = _json_trans_targs[_trans];
+
+ if ( _json_trans_actions[_trans] == 0 )
+ goto _again;
+
+ _acts = _json_actions + _json_trans_actions[_trans];
+ _nacts = (unsigned int) *_acts++;
+ while ( _nacts-- > 0 )
+ {
+ switch ( *_acts++ )
+ {
+ case 0:
+#line 517 "upb/json/parser.rl"
+ { p--; {cs = stack[--top]; goto _again;} }
+ break;
+ case 1:
+#line 518 "upb/json/parser.rl"
+ { p--; {stack[top++] = cs; cs = 10; goto _again;} }
+ break;
+ case 2:
+#line 522 "upb/json/parser.rl"
+ { start_text(parser, p); }
+ break;
+ case 3:
+#line 523 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(end_text(parser, p, false)); }
+ break;
+ case 4:
+#line 529 "upb/json/parser.rl"
+ { start_hex(parser, p); }
+ break;
+ case 5:
+#line 530 "upb/json/parser.rl"
+ { hex(parser, p); }
+ break;
+ case 6:
+#line 536 "upb/json/parser.rl"
+ { escape(parser, p); }
+ break;
+ case 7:
+#line 539 "upb/json/parser.rl"
+ { {cs = stack[--top]; goto _again;} }
+ break;
+ case 8:
+#line 540 "upb/json/parser.rl"
+ { {stack[top++] = cs; cs = 19; goto _again;} }
+ break;
+ case 9:
+#line 542 "upb/json/parser.rl"
+ { p--; {stack[top++] = cs; cs = 27; goto _again;} }
+ break;
+ case 10:
+#line 547 "upb/json/parser.rl"
+ { start_member(parser); }
+ break;
+ case 11:
+#line 548 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(end_member(parser)); }
+ break;
+ case 12:
+#line 551 "upb/json/parser.rl"
+ { clear_member(parser); }
+ break;
+ case 13:
+#line 557 "upb/json/parser.rl"
+ { start_object(parser); }
+ break;
+ case 14:
+#line 560 "upb/json/parser.rl"
+ { end_object(parser); }
+ break;
+ case 15:
+#line 566 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(start_array(parser)); }
+ break;
+ case 16:
+#line 570 "upb/json/parser.rl"
+ { end_array(parser); }
+ break;
+ case 17:
+#line 575 "upb/json/parser.rl"
+ { start_number(parser, p); }
+ break;
+ case 18:
+#line 576 "upb/json/parser.rl"
+ { end_number(parser, p); }
+ break;
+ case 19:
+#line 578 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(start_stringval(parser)); }
+ break;
+ case 20:
+#line 579 "upb/json/parser.rl"
+ { end_stringval(parser); }
+ break;
+ case 21:
+#line 581 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(parser_putbool(parser, true)); }
+ break;
+ case 22:
+#line 583 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(parser_putbool(parser, false)); }
+ break;
+ case 23:
+#line 585 "upb/json/parser.rl"
+ { /* null value */ }
+ break;
+ case 24:
+#line 587 "upb/json/parser.rl"
+ { CHECK_RETURN_TOP(start_subobject(parser)); }
+ break;
+ case 25:
+#line 588 "upb/json/parser.rl"
+ { end_subobject(parser); }
+ break;
+ case 26:
+#line 593 "upb/json/parser.rl"
+ { p--; {cs = stack[--top]; goto _again;} }
+ break;
+#line 866 "upb/json/parser.c"
+ }
+ }
+
+_again:
+ if ( cs == 0 )
+ goto _out;
+ if ( ++p != pe )
+ goto _resume;
+ _test_eof: {}
+ _out: {}
+ }
+
+#line 615 "upb/json/parser.rl"
+
+ if (p != pe) {
+ upb_status_seterrf(parser->status, "Parse error at %s\n", 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);
+ return true;
+}
+
+void upb_json_parser_init(upb_json_parser *p, upb_status *status) {
+ p->limit = p->stack + UPB_JSON_MAX_DEPTH;
+ upb_byteshandler_init(&p->input_handler_);
+ upb_byteshandler_setstring(&p->input_handler_, parse, NULL);
+ upb_byteshandler_setendstr(&p->input_handler_, end, NULL);
+ upb_bytessink_reset(&p->input_, &p->input_handler_, p);
+ p->status = status;
+}
+
+void upb_json_parser_uninit(upb_json_parser *p) {
+ upb_byteshandler_uninit(&p->input_handler_);
+}
+
+void upb_json_parser_reset(upb_json_parser *p) {
+ p->top = p->stack;
+ p->top->f = NULL;
+
+ int cs;
+ int top;
+ // Emit Ragel initialization of the parser.
+
+#line 920 "upb/json/parser.c"
+ {
+ cs = json_start;
+ top = 0;
+ }
+
+#line 655 "upb/json/parser.rl"
+ p->current_state = cs;
+ p->parser_top = top;
+ p->text_begin = NULL;
+ p->accumulated = NULL;
+ p->accumulated_len = 0;
+}
+
+void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *sink) {
+ upb_json_parser_reset(p);
+ upb_sink_reset(&p->top->sink, sink->handlers, sink->closure);
+ p->top->m = upb_handlers_msgdef(sink->handlers);
+ p->accumulated = NULL;
+}
+
+upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
+ return &p->input_;
+}
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * This currently uses snprintf() to format primitives, and could be optimized
+ * further.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+// StringPiece; a pointer plus a length.
+typedef struct {
+ const char *ptr;
+ size_t len;
+} strpc;
+
+strpc *newstrpc(upb_handlers *h, const upb_fielddef *f) {
+ strpc *ret = malloc(sizeof(*ret));
+ ret->ptr = upb_fielddef_name(f);
+ ret->len = strlen(ret->ptr);
+ upb_handlers_addcleanup(h, ret, free);
+ return ret;
+}
+
+// ------------ JSON string printing: values, maps, arrays --------------------
+
+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_VAR(n, n == len);
+}
+
+static void print_comma(upb_json_printer *p) {
+ if (!p->first_elem_[p->depth_]) {
+ print_data(p, ",", 1);
+ }
+ p->first_elem_[p->depth_] = false;
+}
+
+// Helpers that print properly formatted elements to the JSON output stream.
+
+// Used for escaping control chars in strings.
+static const char kControlCharLimit = 0x20;
+
+static inline bool is_json_escaped(char c) {
+ // See RFC 4627.
+ unsigned char uc = (unsigned char)c;
+ return uc < kControlCharLimit || uc == '"' || uc == '\\';
+}
+
+static inline 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;
+ }
+}
+
+// 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;
+ for (unsigned int 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;
+ snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
+ escape = escape_buf;
+ }
+
+ // 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.
+
+ // 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];
+ }
+ }
+ }
+
+ // 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);
+ }
+}
+
+#define CHKLENGTH(x) if (!(x)) return -1;
+
+// 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 fmt_double(double val, char* buf, size_t length) {
+ size_t n = snprintf(buf, length, "%.17g", val);
+ CHKLENGTH(n > 0 && n < length);
+ return n;
+}
+
+static size_t fmt_float(float val, char* buf, size_t length) {
+ size_t n = snprintf(buf, length, "%.8g", val);
+ CHKLENGTH(n > 0 && n < length);
+ return n;
+}
+
+static size_t fmt_bool(bool val, char* buf, size_t length) {
+ size_t n = snprintf(buf, length, "%s", (val ? "true" : "false"));
+ CHKLENGTH(n > 0 && n < length);
+ return n;
+}
+
+static size_t fmt_int64(long val, char* buf, size_t length) {
+ size_t n = snprintf(buf, length, "%ld", val);
+ CHKLENGTH(n > 0 && n < length);
+ return n;
+}
+
+static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) {
+ size_t n = 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) {
+ 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;
+}
+
+#define CHKFMT(val) if ((val) == -1) return false;
+#define CHK(val) if (!(val)) return false;
+
+#define TYPE_HANDLERS(type, fmt_func) \
+ static bool put##type(void *closure, const void *handler_data, type val) { \
+ upb_json_printer *p = closure; \
+ UPB_UNUSED(handler_data); \
+ char data[64]; \
+ size_t length = fmt_func(val, data, sizeof(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; \
+ }
+
+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);
+
+#undef TYPE_HANDLERS
+
+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;
+ CHK(putkey(closure, hd->keyname));
+
+ const char *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);
+ }
+
+ return true;
+}
+
+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);
+
+ const char *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);
+ }
+
+ 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_UNUSED(handler_data);
+ upb_json_printer *p = closure;
+ print_comma(p);
+ return closure;
+}
+
+static bool startmap(void *closure, const void *handler_data) {
+ UPB_UNUSED(handler_data);
+ upb_json_printer *p = closure;
+ if (p->depth_++ == 0) {
+ upb_bytessink_start(p->output_, 0, &p->subc_);
+ }
+ p->first_elem_[p->depth_] = true;
+ print_data(p, "{", 1);
+ return true;
+}
+
+static bool endmap(void *closure, const void *handler_data, upb_status *s) {
+ UPB_UNUSED(handler_data);
+ UPB_UNUSED(s);
+ upb_json_printer *p = closure;
+ if (--p->depth_ == 0) {
+ upb_bytessink_end(p->output_);
+ }
+ print_data(p, "}", 1);
+ 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_UNUSED(handler_data);
+ upb_json_printer *p = closure;
+ 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_UNUSED(handler_data);
+ UPB_UNUSED(handle);
+ upb_json_printer *p = closure;
+ 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_UNUSED(handler_data);
+ UPB_UNUSED(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;
+ while (remaining > 2) {
+ // TODO(haberman): handle encoded lengths > sizeof(data)
+ UPB_ASSERT_VAR(limit, (limit - to) >= 4);
+
+ 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;
+ }
+
+ size_t bytes = to - data;
+ print_data(p, "\"", 1);
+ 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_UNUSED(handler_data);
+ UPB_UNUSED(size_hint);
+ upb_json_printer *p = closure;
+ 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_UNUSED(handler_data);
+ upb_json_printer *p = closure;
+ print_data(p, "\"", 1);
+ return true;
+}
+
+static void *repeated_startstr(void *closure, const void *handler_data,
+ size_t size_hint) {
+ UPB_UNUSED(handler_data);
+ UPB_UNUSED(size_hint);
+ upb_json_printer *p = closure;
+ 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_UNUSED(handler_data);
+ upb_json_printer *p = closure;
+ 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;
+}
+
+void printer_sethandlers(const void *closure, upb_handlers *h) {
+ UPB_UNUSED(closure);
+
+ upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlers_setstartmsg(h, startmap, &empty_attr);
+ upb_handlers_setendmsg(h, endmap, &empty_attr);
+
+#define TYPE(type, name, ctype) \
+ case type: \
+ if (upb_fielddef_isseq(f)) { \
+ upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
+ } else { \
+ upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
+ } \
+ break;
+
+ upb_msg_iter i;
+ upb_msg_begin(&i, upb_handlers_msgdef(h));
+ for(; !upb_msg_done(&i); upb_msg_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));
+
+ if (upb_fielddef_isseq(f)) {
+ upb_handlers_setstartseq(h, f, startseq, &name_attr);
+ upb_handlers_setendseq(h, f, endseq, &empty_attr);
+ }
+
+ switch (upb_fielddef_type(f)) {
+ TYPE(UPB_TYPE_FLOAT, float, float);
+ TYPE(UPB_TYPE_DOUBLE, double, double);
+ TYPE(UPB_TYPE_BOOL, bool, bool);
+ TYPE(UPB_TYPE_INT32, int32, int32_t);
+ TYPE(UPB_TYPE_UINT32, uint32, uint32_t);
+ TYPE(UPB_TYPE_INT64, int64, int64_t);
+ TYPE(UPB_TYPE_UINT64, uint64, uint64_t);
+ case UPB_TYPE_ENUM: {
+ // 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.
+ EnumHandlerData *hd = malloc(sizeof(EnumHandlerData));
+ hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f);
+ hd->keyname = newstrpc(h, f);
+ upb_handlers_addcleanup(h, hd, free);
+ upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER;
+ upb_handlerattr_sethandlerdata(&enum_attr, hd);
+
+ if (upb_fielddef_isseq(f)) {
+ upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
+ } else {
+ upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
+ }
+
+ upb_handlerattr_uninit(&enum_attr);
+ break;
+ }
+ case UPB_TYPE_STRING:
+ if (upb_fielddef_isseq(f)) {
+ upb_handlers_setstartstr(h, f, repeated_startstr, &empty_attr);
+ upb_handlers_setstring(h, f, repeated_str, &empty_attr);
+ upb_handlers_setendstr(h, f, repeated_endstr, &empty_attr);
+ } else {
+ upb_handlers_setstartstr(h, f, scalar_startstr, &name_attr);
+ upb_handlers_setstring(h, f, scalar_str, &empty_attr);
+ upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
+ }
+ break;
+ case UPB_TYPE_BYTES:
+ // XXX: this doesn't support strings that span buffers yet. The base64
+ // encoder will need to be made resumable for this to work properly.
+ if (upb_fielddef_isseq(f)) {
+ upb_handlers_setstring(h, f, repeated_bytes, &empty_attr);
+ } else {
+ upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
+ }
+ break;
+ case UPB_TYPE_MESSAGE:
+ if (upb_fielddef_isseq(f)) {
+ upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
+ } else {
+ upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
+ }
+ break;
+ }
+
+ upb_handlerattr_uninit(&name_attr);
+ }
+
+ upb_handlerattr_uninit(&empty_attr);
+#undef TYPE
+}
+
+/* Public API *****************************************************************/
+
+void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h) {
+ p->output_ = NULL;
+ p->depth_ = 0;
+ upb_sink_reset(&p->input_, h, p);
+}
+
+void upb_json_printer_uninit(upb_json_printer *p) {
+ UPB_UNUSED(p);
+}
+
+void upb_json_printer_reset(upb_json_printer *p) {
+ p->depth_ = 0;
+}
+
+void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output) {
+ upb_json_printer_reset(p);
+ p->output_ = output;
+}
+
+upb_sink *upb_json_printer_input(upb_json_printer *p) {
+ return &p->input_;
+}
+
+const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
+ const void *owner) {
+ return upb_handlers_newfrozen(md, owner, printer_sethandlers, NULL);
+}
diff --git a/ruby/ext/google/protobuf_c/upb.h b/ruby/ext/google/protobuf_c/upb.h
new file mode 100644
index 00000000..150aef10
--- /dev/null
+++ b/ruby/ext/google/protobuf_c/upb.h
@@ -0,0 +1,7439 @@
+// Amalgamated source file
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Defs are upb's internal representation of the constructs that can appear
+ * in a .proto file:
+ *
+ * - upb_msgdef: describes a "message" construct.
+ * - upb_fielddef: describes a message field.
+ * - upb_enumdef: describes an enum.
+ * (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 - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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 - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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 <assert.h>
+#include <stdint.h>
+#include <string.h>
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * This file contains shared definitions that are widely used across upb.
+ *
+ * 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_H_
+#define UPB_H_
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+// inline if possible, emit standalone code if required.
+#ifdef __cplusplus
+#define UPB_INLINE inline
+#else
+#define UPB_INLINE static inline
+#endif
+
+#if __STDC_VERSION__ >= 199901L
+#define UPB_C99
+#endif
+
+#if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
+ defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
+#define UPB_CXX11
+#endif
+
+#ifdef UPB_CXX11
+#include <type_traits>
+#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; \
+ /* Friend Pointer<T> so it can access base class. */ \
+ friend class Pointer<full_class_name>; \
+ friend class Pointer<const full_class_name>; \
+ UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
+#define UPB_ASSERT_STDLAYOUT(type) \
+ static_assert(std::is_standard_layout<type>::value, \
+ #type " must be standard layout");
+#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(); \
+ /* Friend Pointer<T> so it can access base class. */ \
+ friend class Pointer<full_class_name>; \
+ friend class Pointer<const full_class_name>; \
+ UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
+#define UPB_ASSERT_STDLAYOUT(type)
+#endif
+
+
+#ifdef __cplusplus
+
+#define UPB_PRIVATE_FOR_CPP private:
+#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
+#define UPB_BEGIN_EXTERN_C extern "C" {
+#define UPB_END_EXTERN_C }
+#define UPB_DEFINE_STRUCT0(cname, members) members;
+#define UPB_DEFINE_STRUCT(cname, cbase, members) \
+ public: \
+ cbase* base() { return &base_; } \
+ const cbase* base() const { return &base_; } \
+ \
+ private: \
+ cbase base_; \
+ members;
+#define UPB_DEFINE_CLASS0(cppname, cppmethods, members) \
+ class cppname { \
+ cppmethods \
+ members \
+ }; \
+ UPB_ASSERT_STDLAYOUT(cppname);
+#define UPB_DEFINE_CLASS1(cppname, cppbase, cppmethods, members) \
+ UPB_DEFINE_CLASS0(cppname, cppmethods, members) \
+ namespace upb { \
+ template <> \
+ class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
+ public: \
+ explicit Pointer(cppname* ptr) : PointerBase(ptr) {} \
+ }; \
+ template <> \
+ class Pointer<const cppname> \
+ : public PointerBase<const cppname, const cppbase> { \
+ public: \
+ explicit Pointer(const cppname* ptr) : PointerBase(ptr) {} \
+ }; \
+ }
+#define UPB_DEFINE_CLASS2(cppname, cppbase, cppbase2, cppmethods, members) \
+ UPB_DEFINE_CLASS0(cppname, UPB_QUOTE(cppmethods), members) \
+ namespace upb { \
+ template <> \
+ class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
+ public: \
+ explicit Pointer(cppname* ptr) : PointerBase2(ptr) {} \
+ }; \
+ template <> \
+ class Pointer<const cppname> \
+ : public PointerBase2<const cppname, const cppbase, const cppbase2> { \
+ public: \
+ explicit Pointer(const cppname* ptr) : PointerBase2(ptr) {} \
+ }; \
+ }
+
+#else // !defined(__cplusplus)
+
+#define UPB_PRIVATE_FOR_CPP
+#define UPB_DECLARE_TYPE(cppname, cname) \
+ struct cname; \
+ typedef struct cname cname;
+#define UPB_BEGIN_EXTERN_C
+#define UPB_END_EXTERN_C
+#define UPB_DEFINE_STRUCT0(cname, members) \
+ struct cname { \
+ members; \
+ };
+#define UPB_DEFINE_STRUCT(cname, cbase, members) \
+ struct cname { \
+ cbase base; \
+ members; \
+ };
+#define UPB_DEFINE_CLASS0(cppname, cppmethods, members) members
+#define UPB_DEFINE_CLASS1(cppname, cppbase, cppmethods, members) members
+#define UPB_DEFINE_CLASS2(cppname, cppbase, cppbase2, cppmethods, members) \
+ members
+
+#endif // defined(__cplusplus)
+
+#ifdef __GNUC__
+#define UPB_NORETURN __attribute__((__noreturn__))
+#else
+#define UPB_NORETURN
+#endif
+
+#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
+#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
+
+#define UPB_UNUSED(var) (void)var
+
+// Code with commas confuses the preprocessor when passed as arguments, whether
+// C++ type names with commas (eg. Foo<int, int>) or code blocks that declare
+// variables (ie. int foo, bar).
+#define UPB_QUOTE(...) __VA_ARGS__
+
+// For asserting something about a variable when the variable is not used for
+// anything else. This prevents "unused variable" warnings when compiling in
+// debug mode.
+#define UPB_ASSERT_VAR(var, predicate) UPB_UNUSED(var); assert(predicate)
+
+// Generic function type.
+typedef void upb_func();
+
+/* Casts **********************************************************************/
+
+// Upcasts for C. For downcasts see the definitions of the subtypes.
+#define UPB_UPCAST(obj) (&(obj)->base)
+#define UPB_UPCAST2(obj) UPB_UPCAST(UPB_UPCAST(obj))
+
+#ifdef __cplusplus
+
+// Downcasts for C++. We can't use C++ inheritance directly and maintain
+// compatibility with C. So our inheritance is undeclared in C++.
+// Specializations of these casting functions are defined for appropriate type
+// pairs, and perform the necessary checks.
+//
+// Example:
+// upb::Def* def = <...>;
+// upb::MessageDef* = upb::dyn_cast<upb::MessageDef*>(def);
+
+namespace upb {
+
+// Casts to a direct subclass. The caller must know that cast is correct; an
+// incorrect cast will throw an assertion failure in debug mode.
+template<class To, class From> To down_cast(From* f);
+
+// Casts to a direct subclass. If the class does not actually match the given
+// subtype, returns NULL.
+template<class To, class From> To dyn_cast(From* f);
+
+// Pointer<T> is a simple wrapper around a T*. It is only constructed for
+// upcast() below, and its sole purpose is to be implicitly convertable to T* or
+// pointers to base classes, just as a pointer would be in regular C++ if the
+// inheritance were directly expressed as C++ inheritance.
+template <class T> class Pointer;
+
+// Casts to any base class, or the type itself (ie. can be a no-op).
+template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
+
+template <class T, class Base>
+class PointerBase {
+ public:
+ explicit PointerBase(T* ptr) : ptr_(ptr) {}
+ operator T*() { return ptr_; }
+ operator Base*() { return ptr_->base(); }
+
+ private:
+ T* ptr_;
+};
+
+template <class T, class Base, class Base2>
+class PointerBase2 : public PointerBase<T, Base> {
+ public:
+ explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
+ operator Base2*() { return Pointer<Base>(*this); }
+};
+
+}
+
+#endif
+
+
+/* upb::reffed_ptr ************************************************************/
+
+#ifdef __cplusplus
+
+#include <algorithm> // For std::swap().
+
+namespace upb {
+
+// Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a
+// ref on whatever object it points to (if any).
+template <class T> class reffed_ptr {
+ public:
+ reffed_ptr() : ptr_(NULL) {}
+
+ // If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor.
+ template <class U>
+ reffed_ptr(U* val, const void* ref_donor = NULL)
+ : ptr_(upb::upcast(val)) {
+ if (ref_donor) {
+ assert(ptr_);
+ ptr_->DonateRef(ref_donor, this);
+ } else if (ptr_) {
+ ptr_->Ref(this);
+ }
+ }
+
+ template <class U>
+ reffed_ptr(const reffed_ptr<U>& other)
+ : ptr_(upb::upcast(other.get())) {
+ if (ptr_) ptr_->Ref(this);
+ }
+
+ ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
+
+ template <class U>
+ reffed_ptr& operator=(const reffed_ptr<U>& 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 {
+ assert(ptr_);
+ return *ptr_;
+ }
+
+ T* operator->() const {
+ assert(ptr_);
+ return ptr_;
+ }
+
+ T* get() const { return ptr_; }
+
+ // If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor.
+ template <class U>
+ void reset(U* ptr = NULL, const void* ref_donor = NULL) {
+ reffed_ptr(ptr, ref_donor).swap(*this);
+ }
+
+ template <class U>
+ reffed_ptr<U> down_cast() {
+ return reffed_ptr<U>(upb::down_cast<U*>(get()));
+ }
+
+ template <class U>
+ reffed_ptr<U> dyn_cast() {
+ return reffed_ptr<U>(upb::dyn_cast<U*>(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_;
+};
+
+} // namespace upb
+
+#endif // __cplusplus
+
+
+/* upb::Status ****************************************************************/
+
+#ifdef __cplusplus
+namespace upb {
+class ErrorSpace;
+class Status;
+}
+#endif
+
+UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace);
+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
+
+// An error callback function is used to report errors from some component.
+// The function can return "true" to indicate that the component should try
+// to recover and proceed, but this is not always possible.
+typedef bool upb_errcb_t(void *closure, const upb_status* status);
+
+UPB_DEFINE_CLASS0(upb::ErrorSpace,
+,
+UPB_DEFINE_STRUCT0(upb_errorspace,
+ const char *name;
+ // Should the error message in the status object according to this code.
+ void (*set_message)(upb_status* status, int code);
+));
+
+// Object representing a success or failure status.
+// It owns no resources and allocates no memory, so it should work
+// even in OOM situations.
+UPB_DEFINE_CLASS0(upb::Status,
+ public:
+ Status();
+
+ // Returns true if there is no error.
+ bool ok() const;
+
+ // Optional error space and code, useful if the caller wants to
+ // programmatically check the specific kind of error.
+ ErrorSpace* error_space();
+ int code() const;
+
+ const char *error_message() const;
+
+ // The error message will be truncated if it is longer than
+ // UPB_STATUS_MAX_MESSAGE-4.
+ void SetErrorMessage(const char* msg);
+ void SetFormattedErrorMessage(const char* fmt, ...);
+
+ // If there is no error message already, this will use the ErrorSpace to
+ // populate the error message for this code. The caller can still call
+ // SetErrorMessage() to give a more specific message.
+ void SetErrorCode(ErrorSpace* space, int code);
+
+ // Resets the status to a successful state with no message.
+ void Clear();
+
+ void CopyFrom(const Status& other);
+
+ private:
+ UPB_DISALLOW_COPY_AND_ASSIGN(Status);
+,
+UPB_DEFINE_STRUCT0(upb_status,
+ bool ok_;
+
+ // Specific status code defined by some error space (optional).
+ int code_;
+ upb_errorspace *error_space_;
+
+ // Error message; NULL-terminated.
+ char msg[UPB_STATUS_MAX_MESSAGE];
+));
+
+#define UPB_STATUS_INIT {true, 0, NULL, {0}}
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// The returned string is invalidated by any other call into the status.
+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
+// status message.
+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_seterrcode(upb_status *status, upb_errorspace *space, int code);
+void upb_status_copy(upb_status *to, const upb_status *from);
+
+#ifdef __cplusplus
+} // extern "C"
+
+namespace upb {
+
+// C++ Wrappers
+inline Status::Status() { Clear(); }
+inline bool Status::ok() const { return upb_ok(this); }
+inline const char* Status::error_message() const {
+ return upb_status_errmsg(this);
+}
+inline void Status::SetErrorMessage(const char* msg) {
+ upb_status_seterrmsg(this, msg);
+}
+inline void Status::SetFormattedErrorMessage(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ upb_status_vseterrf(this, fmt, args);
+ va_end(args);
+}
+inline void Status::SetErrorCode(ErrorSpace* space, int code) {
+ upb_status_seterrcode(this, space, code);
+}
+inline void Status::Clear() { upb_status_clear(this); }
+inline void Status::CopyFrom(const Status& other) {
+ upb_status_copy(this, &other);
+}
+
+} // namespace upb
+
+#endif
+
+#endif /* UPB_H_ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* upb_value ******************************************************************/
+
+// 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_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_t;
+
+typedef union {
+ int32_t int32;
+ int64_t int64;
+ uint64_t uint64;
+ uint32_t uint32;
+ bool _bool;
+ char *cstr;
+ void *ptr;
+ const void *constptr;
+ upb_func *fptr;
+} _upb_value;
+
+typedef struct {
+ _upb_value 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 UPB_C99
+#define UPB_VALUE_INIT(v, member) {.member = v}
+#endif
+#define UPB__VALUE_INIT_NONE UPB_VALUE_INIT(NULL, ptr)
+
+#ifdef NDEBUG
+#define SET_TYPE(dest, val) UPB_UNUSED(val)
+#define UPB_VALUE_INIT_NONE {UPB__VALUE_INIT_NONE}
+#else
+#define SET_TYPE(dest, val) dest = val
+// Non-existent type, all reads will fail.
+#define UPB_VALUE_INIT_NONE {UPB__VALUE_INIT_NONE, -1}
+#endif
+
+#define UPB_VALUE_INIT_INT32(v) UPB_VALUE_INIT(v, int32)
+#define UPB_VALUE_INIT_INT64(v) UPB_VALUE_INIT(v, int64)
+#define UPB_VALUE_INIT_UINT32(v) UPB_VALUE_INIT(v, uint32)
+#define UPB_VALUE_INIT_UINT64(v) UPB_VALUE_INIT(v, uint64)
+#define UPB_VALUE_INIT_BOOL(v) UPB_VALUE_INIT(v, _bool)
+#define UPB_VALUE_INIT_CSTR(v) UPB_VALUE_INIT(v, cstr)
+#define UPB_VALUE_INIT_PTR(v) UPB_VALUE_INIT(v, ptr)
+#define UPB_VALUE_INIT_CONSTPTR(v) UPB_VALUE_INIT(v, constptr)
+#define UPB_VALUE_INIT_FPTR(v) UPB_VALUE_INIT(v, fptr)
+
+// Like strdup(), which isn't always available since it's not ANSI C.
+char *upb_strdup(const char *s);
+
+UPB_INLINE void _upb_value_setval(upb_value *v, _upb_value val,
+ upb_ctype_t ctype) {
+ v->val = val;
+ SET_TYPE(v->ctype, ctype);
+}
+
+UPB_INLINE upb_value _upb_value_val(_upb_value 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, proto_type) \
+ UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
+ val->val.uint64 = 0; \
+ SET_TYPE(val->ctype, proto_type); \
+ val->val.membername = cval; \
+ } \
+ UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
+ upb_value ret; \
+ upb_value_set ## name(&ret, val); \
+ return ret; \
+ } \
+ UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
+ assert(val.ctype == proto_type); \
+ return val.val.membername; \
+ }
+
+FUNCS(int32, int32, int32_t, UPB_CTYPE_INT32);
+FUNCS(int64, int64, int64_t, UPB_CTYPE_INT64);
+FUNCS(uint32, uint32, uint32_t, UPB_CTYPE_UINT32);
+FUNCS(uint64, uint64, uint64_t, UPB_CTYPE_UINT64);
+FUNCS(bool, _bool, bool, UPB_CTYPE_BOOL);
+FUNCS(cstr, cstr, char*, UPB_CTYPE_CSTR);
+FUNCS(ptr, ptr, void*, UPB_CTYPE_PTR);
+FUNCS(constptr, constptr, const void*, UPB_CTYPE_CONSTPTR);
+FUNCS(fptr, fptr, upb_func*, UPB_CTYPE_FPTR);
+
+#undef FUNCS
+
+
+/* upb_table ******************************************************************/
+
+typedef union {
+ uintptr_t num;
+ const char *str; // We own, nullz.
+} upb_tabkey;
+
+#define UPB_TABKEY_NUM(n) {n}
+#ifdef UPB_C99
+#define UPB_TABKEY_STR(s) {.str = s}
+#endif
+// TODO(haberman): C++
+#define UPB_TABKEY_NONE {0}
+
+typedef struct _upb_tabent {
+ upb_tabkey key;
+ _upb_value val;
+ // 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 {
+ 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 hash table part is 2^size_lg2 entries.
+
+ // 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;
+} upb_table;
+
+typedef struct {
+ upb_table t;
+} upb_strtable;
+
+#define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
+ {{count, mask, ctype, size_lg2, entries}}
+
+typedef struct {
+ upb_table t; // For entries that don't fit in the array part.
+ const _upb_value *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;
+
+#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
+ {{count, mask, ctype, size_lg2, ent}, a, asize, acount}
+
+#define UPB_EMPTY_INTTABLE_INIT(ctype) \
+ UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
+
+#define UPB_ARRAY_EMPTYVAL -1
+#define UPB_ARRAY_EMPTYENT UPB_VALUE_INIT_INT64(UPB_ARRAY_EMPTYVAL)
+
+UPB_INLINE size_t upb_table_size(const upb_table *t) {
+ if (t->size_lg2 == 0)
+ return 0;
+ else
+ return 1 << t->size_lg2;
+}
+
+// Internal-only functions, in .h file only out of necessity.
+UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
+ return e->key.num == 0;
+}
+
+// Used by some of the unit tests for generic hashing functionality.
+uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
+
+UPB_INLINE upb_tabkey upb_intkey(uintptr_t key) {
+ upb_tabkey k;
+ k.num = key;
+ return k;
+}
+
+UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
+ return (uint32_t)key;
+}
+
+static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
+ return t->entries + (hash & t->mask);
+}
+
+UPB_INLINE bool upb_arrhas(_upb_value v) {
+ return v.uint64 != (uint64_t)UPB_ARRAY_EMPTYVAL;
+}
+
+// Initialize and uninitialize a table, respectively. If memory allocation
+// failed, false is returned that the table is uninitialized.
+bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype);
+bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype);
+void upb_inttable_uninit(upb_inttable *table);
+void upb_strtable_uninit(upb_strtable *table);
+
+// 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;
+}
+
+// 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_insert(upb_inttable *t, uintptr_t key, upb_value val);
+bool upb_strtable_insert(upb_strtable *t, const char *key, upb_value 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_remove(upb_strtable *t, const char *key, upb_value *val);
+
+// Updates an existing entry in an inttable. If the entry does not exist,
+// returns false and does nothing. Unlike insert/remove, this does not
+// invalidate iterators.
+bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
+
+// Handy routines for treating an inttable like a stack. May not be mixed with
+// other insert/remove calls.
+bool upb_inttable_push(upb_inttable *t, upb_value val);
+upb_value upb_inttable_pop(upb_inttable *t);
+
+// Convenience routines for inttables with pointer keys.
+bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val);
+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);
+
+// 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_compact(upb_inttable *t);
+
+// 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_value arrval = t->array[key];
+ if (upb_arrhas(arrval)) {
+ _upb_value_setval(v, arrval, 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.num == key) {
+ _upb_value_setval(v, e->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);
+
+/* 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(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);
+// // ...
+// }
+
+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
+
+#endif /* UPB_TABLE_H_ */
+
+// 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).
+#ifndef NDEBUG
+#define UPB_DEBUG_REFS
+#endif
+
+#ifdef __cplusplus
+namespace upb { class RefCounted; }
+#endif
+
+UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted);
+
+struct upb_refcounted_vtbl;
+
+UPB_DEFINE_CLASS0(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);
+,
+UPB_DEFINE_STRUCT0(upb_refcounted,
+ // 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
+));
+
+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);
+
+
+// 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 // }
+
+#ifdef UPB_DEBUG_REFS
+#define UPB_REFCOUNT_INIT(refs, ref2s) \
+ {&static_refcount, NULL, NULL, 0, true, refs, ref2s}
+#else
+#define UPB_REFCOUNT_INIT(refs, ref2s) {&static_refcount, NULL, NULL, 0, true}
+#endif
+
+#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
+#endif
+
+#endif // UPB_REFCOUNT_H_
+
+#ifdef __cplusplus
+#include <cstring>
+#include <string>
+#include <vector>
+
+namespace upb {
+class Def;
+class EnumDef;
+class FieldDef;
+class MessageDef;
+}
+#endif
+
+UPB_DECLARE_TYPE(upb::Def, upb_def);
+UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef);
+UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef);
+UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef);
+
+// Maximum field number allowed for FieldDefs. This is an inherent limit of the
+// protobuf wire format.
+#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
+
+// 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 defs *********************************************/
+
+// All the different kind of defs we support. These correspond 1:1 with
+// declarations in a .proto file.
+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;
+
+// The base class of all defs. Its base is upb::RefCounted (use upb::upcast()
+// to convert).
+UPB_DEFINE_CLASS1(upb::Def, upb::RefCounted,
+ public:
+ typedef upb_deftype_t Type;
+
+ Def* Dup(const void *owner) const;
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void* from, const void* to) const;
+ void CheckRef(const void* owner) const;
+
+ Type def_type() const;
+
+ // "fullname" is the def's fully-qualified name (eg. foo.bar.Message).
+ const char *full_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);
+
+ // 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, int n, Status* status);
+ static bool Freeze(const std::vector<Def*>& defs, Status* status);
+
+ private:
+ UPB_DISALLOW_POD_OPS(Def, upb::Def);
+,
+UPB_DEFINE_STRUCT(upb_def, upb_refcounted,
+ const char *fullname;
+ upb_deftype_t type : 8;
+ // 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;
+));
+
+#define UPB_DEF_INIT(name, type, refs, ref2s) \
+ { UPB_REFCOUNT_INIT(refs, ref2s), name, type, false }
+
+UPB_BEGIN_EXTERN_C // {
+
+// Native C API.
+upb_def *upb_def_dup(const upb_def *def, const void *owner);
+
+// From upb_refcounted.
+bool upb_def_isfrozen(const upb_def *def);
+void upb_def_ref(const upb_def *def, const void *owner);
+void upb_def_unref(const upb_def *def, const void *owner);
+void upb_def_donateref(const upb_def *def, const void *from, const void *to);
+void upb_def_checkref(const upb_def *def, const void *owner);
+
+upb_deftype_t upb_def_type(const upb_def *d);
+const char *upb_def_fullname(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, int 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<cpptype *, Def>(Def * def) { \
+ return upb_downcast_##cname##_mutable(def); \
+ } \
+ template <> \
+ inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \
+ return upb_dyncast_##cname##_mutable(def); \
+ } \
+ template <> \
+ inline const cpptype *down_cast<const cpptype *, const Def>( \
+ const Def *def) { \
+ return upb_downcast_##cname(def); \
+ } \
+ template <> \
+ inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
+ return upb_dyncast_##cname(def); \
+ } \
+ template <> \
+ inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \
+ return upb_downcast_##cname(def); \
+ } \
+ template <> \
+ inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \
+ return upb_dyncast_##cname(def); \
+ } \
+ } // namespace upb
+#else
+#define UPB_CPP_CASTS(cname, cpptype)
+#endif
+
+// 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) { \
+ 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, UPB_QUOTE(cppmethods), \
+ members) \
+ UPB_DEF_CASTS(lower, upper, cppname)
+
+
+/* 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 {
+ UPB_TYPE_FLOAT = 1,
+ UPB_TYPE_DOUBLE = 2,
+ UPB_TYPE_BOOL = 3,
+ UPB_TYPE_STRING = 4,
+ UPB_TYPE_BYTES = 5,
+ UPB_TYPE_MESSAGE = 6,
+ UPB_TYPE_ENUM = 7, // Enum values are int32.
+ UPB_TYPE_INT32 = 8,
+ UPB_TYPE_UINT32 = 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;
+
+
+// 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).
+UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD,
+ 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<FieldDef> New();
+
+ // Duplicates the given field, returning NULL if memory allocation failed.
+ // When a fielddef is duplicated, the subdef (if any) is made symbolic if it
+ // wasn't already. If the subdef is set but has no name (which is possible
+ // since msgdefs are not required to have a name) the new fielddef's subdef
+ // will be unset.
+ FieldDef* Dup(const void* owner) const;
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void* from, const void* to) const;
+ void CheckRef(const void* owner) const;
+
+ // Functionality from upb::Def.
+ const char* full_name() const;
+
+ bool type_is_set() const; // Whether 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;
+
+ // 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.
+ int 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 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;
+
+ // 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);
+
+ 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);
+,
+UPB_DEFINE_STRUCT(upb_fielddef, upb_def,
+ 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;
+ 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_;
+));
+
+#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, refs, ref2s), defaultval, {msgdef}, \
+ {subdef}, false, false, \
+ type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
+ lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
+ }
+
+UPB_BEGIN_EXTERN_C // {
+
+// Native C API.
+upb_fielddef *upb_fielddef_new(const void *owner);
+upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner);
+
+// From upb_refcounted.
+bool upb_fielddef_isfrozen(const upb_fielddef *f);
+void upb_fielddef_ref(const upb_fielddef *f, const void *owner);
+void upb_fielddef_unref(const upb_fielddef *f, const void *owner);
+void upb_fielddef_donateref(const upb_fielddef *f, const void *from,
+ const void *to);
+void upb_fielddef_checkref(const upb_fielddef *f, const void *owner);
+
+// 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);
+const upb_msgdef *upb_fielddef_containingtype(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);
+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_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_iter;
+
+// Structure that describes a single .proto message type.
+//
+// Its base class is upb::Def (use upb::upcast() to convert).
+UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
+ public:
+ // Returns NULL if memory allocation failed.
+ static reffed_ptr<MessageDef> New();
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void* from, const void* to) const;
+ void CheckRef(const void* owner) const;
+
+ // Functionality from upb::Def.
+ const char* full_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;
+
+ // 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.
+ bool AddField(FieldDef* f, Status* s);
+ bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
+
+ // 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 <class T>
+ FieldDef* FindFieldByName(const T& str) {
+ return FindFieldByName(str.c_str(), str.size());
+ }
+ template <class T>
+ const FieldDef* FindFieldByName(const T& str) const {
+ return FindFieldByName(str.c_str(), str.size());
+ }
+
+ // Returns a new msgdef that is a copy of the given msgdef (and a copy of all
+ // the fields) but with any references to submessages broken and replaced
+ // with just the name of the submessage. Returns NULL if memory allocation
+ // failed.
+ //
+ // TODO(haberman): which is more useful, keeping fields resolved or
+ // unresolving them? If there's no obvious answer, Should this functionality
+ // just be moved into symtab.c?
+ MessageDef* Dup(const void* owner) const;
+
+ // Iteration over fields. The order is undefined.
+ class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
+ public:
+ explicit iterator(MessageDef* md);
+ static iterator end(MessageDef* md);
+
+ void operator++();
+ FieldDef* operator*() const;
+ bool operator!=(const iterator& other) const;
+ bool operator==(const iterator& other) const;
+
+ private:
+ upb_msg_iter iter_;
+ };
+
+ class const_iterator
+ : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
+ public:
+ explicit const_iterator(const MessageDef* md);
+ static const_iterator end(const MessageDef* md);
+
+ void operator++();
+ const FieldDef* operator*() const;
+ bool operator!=(const const_iterator& other) const;
+ bool operator==(const const_iterator& other) const;
+
+ private:
+ upb_msg_iter iter_;
+ };
+
+ iterator begin();
+ iterator end();
+ const_iterator begin() const;
+ const_iterator end() const;
+
+ private:
+ UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef);
+),
+UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
+ 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
+
+ // TODO(haberman): proper extension ranges (there can be multiple).
+));
+
+#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
+ refs, ref2s) \
+ { \
+ UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
+ submsg_field_count, itof, ntof \
+ }
+
+UPB_BEGIN_EXTERN_C // {
+
+// Returns NULL if memory allocation failed.
+upb_msgdef *upb_msgdef_new(const void *owner);
+
+// From upb_refcounted.
+bool upb_msgdef_isfrozen(const upb_msgdef *m);
+void upb_msgdef_ref(const upb_msgdef *m, const void *owner);
+void upb_msgdef_unref(const upb_msgdef *m, const void *owner);
+void upb_msgdef_donateref(const upb_msgdef *m, const void *from,
+ const void *to);
+void upb_msgdef_checkref(const upb_msgdef *m, const void *owner);
+bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
+
+// From upb_def.
+const char *upb_msgdef_fullname(const upb_msgdef *m);
+bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
+
+upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner);
+bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
+ upb_status *s);
+
+// 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);
+}
+
+// upb_msg_iter i;
+// for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_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_begin(upb_msg_iter *iter, const upb_msgdef *m);
+void upb_msg_next(upb_msg_iter *iter);
+bool upb_msg_done(const upb_msg_iter *iter);
+upb_fielddef *upb_msg_iter_field(const upb_msg_iter *iter);
+void upb_msg_iter_setdone(upb_msg_iter *iter);
+
+UPB_END_EXTERN_C // }
+
+
+/* upb::EnumDef ***************************************************************/
+
+typedef upb_strtable_iter upb_enum_iter;
+
+// Class that represents an enum. Its base class is upb::Def (convert with
+// upb::upcast()).
+UPB_DEFINE_DEF(upb::EnumDef, enumdef, ENUM,
+ public:
+ // Returns NULL if memory allocation failed.
+ static reffed_ptr<EnumDef> New();
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void* from, const void* to) const;
+ void CheckRef(const void* owner) const;
+
+ // Functionality from upb::Def.
+ const char* full_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;
+
+ // Returns a new EnumDef with all the same values. The new EnumDef will be
+ // owned by the given owner.
+ EnumDef* Dup(const void* owner) 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);
+,
+UPB_DEFINE_STRUCT(upb_enumdef, upb_def,
+ upb_strtable ntoi;
+ upb_inttable iton;
+ int32_t defaultval;
+));
+
+#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
+ { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval }
+
+UPB_BEGIN_EXTERN_C // {
+
+// Native C API.
+upb_enumdef *upb_enumdef_new(const void *owner);
+upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner);
+
+// From upb_refcounted.
+void upb_enumdef_unref(const upb_enumdef *e, const void *owner);
+bool upb_enumdef_isfrozen(const upb_enumdef *e);
+void upb_enumdef_ref(const upb_enumdef *e, const void *owner);
+void upb_enumdef_donateref(const upb_enumdef *m, const void *from,
+ const void *to);
+void upb_enumdef_checkref(const upb_enumdef *e, const void *owner);
+bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
+
+// From upb_def.
+const char *upb_enumdef_fullname(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 // }
+
+
+#ifdef __cplusplus
+
+UPB_INLINE const char* upb_safecstr(const std::string& str) {
+ assert(str.size() == std::strlen(str.c_str()));
+ return str.c_str();
+}
+
+// Inline C++ wrappers.
+namespace upb {
+
+inline Def* Def::Dup(const void* owner) const {
+ return upb_def_dup(this, owner);
+}
+inline bool Def::IsFrozen() const { return upb_def_isfrozen(this); }
+inline void Def::Ref(const void* owner) const { upb_def_ref(this, owner); }
+inline void Def::Unref(const void* owner) const { upb_def_unref(this, owner); }
+inline void Def::DonateRef(const void* from, const void* to) const {
+ upb_def_donateref(this, from, to);
+}
+inline void Def::CheckRef(const void* owner) const {
+ upb_def_checkref(this, owner);
+}
+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 bool Def::set_full_name(const char* fullname, Status* s) {
+ return upb_def_setfullname(this, fullname, s);
+}
+inline bool Def::set_full_name(const std::string& fullname, Status* s) {
+ return upb_def_setfullname(this, upb_safecstr(fullname), s);
+}
+inline bool Def::Freeze(Def* const* defs, int n, Status* status) {
+ return upb_def_freeze(defs, n, status);
+}
+inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
+ return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
+}
+
+inline bool FieldDef::CheckType(int32_t val) {
+ return upb_fielddef_checktype(val);
+}
+inline bool FieldDef::CheckLabel(int32_t val) {
+ return upb_fielddef_checklabel(val);
+}
+inline bool FieldDef::CheckDescriptorType(int32_t val) {
+ return upb_fielddef_checkdescriptortype(val);
+}
+inline bool FieldDef::CheckIntegerFormat(int32_t val) {
+ return upb_fielddef_checkintfmt(val);
+}
+inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
+ assert(CheckType(val));
+ return static_cast<FieldDef::Type>(val);
+}
+inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
+ assert(CheckLabel(val));
+ return static_cast<FieldDef::Label>(val);
+}
+inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
+ assert(CheckDescriptorType(val));
+ return static_cast<FieldDef::DescriptorType>(val);
+}
+inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
+ assert(CheckIntegerFormat(val));
+ return static_cast<FieldDef::IntegerFormat>(val);
+}
+
+inline reffed_ptr<FieldDef> FieldDef::New() {
+ upb_fielddef *f = upb_fielddef_new(&f);
+ return reffed_ptr<FieldDef>(f, &f);
+}
+inline FieldDef* FieldDef::Dup(const void* owner) const {
+ return upb_fielddef_dup(this, owner);
+}
+inline bool FieldDef::IsFrozen() const { return upb_fielddef_isfrozen(this); }
+inline void FieldDef::Ref(const void* owner) const {
+ upb_fielddef_ref(this, owner);
+}
+inline void FieldDef::Unref(const void* owner) const {
+ upb_fielddef_unref(this, owner);
+}
+inline void FieldDef::DonateRef(const void* from, const void* to) const {
+ upb_fielddef_donateref(this, from, to);
+}
+inline void FieldDef::CheckRef(const void* owner) const {
+ upb_fielddef_checkref(this, owner);
+}
+inline const char* FieldDef::full_name() const {
+ return upb_fielddef_fullname(this);
+}
+inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
+ return upb_fielddef_setfullname(this, fullname, s);
+}
+inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
+ return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
+}
+inline bool FieldDef::type_is_set() const {
+ return upb_fielddef_typeisset(this);
+}
+inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
+inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
+ return upb_fielddef_descriptortype(this);
+}
+inline FieldDef::Label FieldDef::label() const {
+ return upb_fielddef_label(this);
+}
+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);
+}
+inline bool FieldDef::lazy() const {
+ return upb_fielddef_lazy(this);
+}
+inline void FieldDef::set_lazy(bool lazy) {
+ upb_fielddef_setlazy(this, lazy);
+}
+inline bool FieldDef::packed() const {
+ return upb_fielddef_packed(this);
+}
+inline void FieldDef::set_packed(bool packed) {
+ upb_fielddef_setpacked(this, packed);
+}
+inline const MessageDef* FieldDef::containing_type() const {
+ return upb_fielddef_containingtype(this);
+}
+inline const char* FieldDef::containing_type_name() {
+ return upb_fielddef_containingtypename(this);
+}
+inline bool FieldDef::set_number(uint32_t number, Status* s) {
+ return upb_fielddef_setnumber(this, number, s);
+}
+inline bool FieldDef::set_name(const char *name, Status* s) {
+ return upb_fielddef_setname(this, name, s);
+}
+inline bool FieldDef::set_name(const std::string& name, Status* s) {
+ return upb_fielddef_setname(this, upb_safecstr(name), s);
+}
+inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
+ return upb_fielddef_setcontainingtypename(this, name, s);
+}
+inline bool FieldDef::set_containing_type_name(const std::string &name,
+ Status *s) {
+ return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
+}
+inline void FieldDef::set_type(upb_fieldtype_t type) {
+ upb_fielddef_settype(this, type);
+}
+inline void FieldDef::set_is_extension(bool is_extension) {
+ upb_fielddef_setisextension(this, is_extension);
+}
+inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
+ upb_fielddef_setdescriptortype(this, type);
+}
+inline void FieldDef::set_label(upb_label_t label) {
+ upb_fielddef_setlabel(this, label);
+}
+inline bool FieldDef::IsSubMessage() const {
+ return upb_fielddef_issubmsg(this);
+}
+inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
+inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
+inline int64_t FieldDef::default_int64() const {
+ return upb_fielddef_defaultint64(this);
+}
+inline int32_t FieldDef::default_int32() const {
+ return upb_fielddef_defaultint32(this);
+}
+inline uint64_t FieldDef::default_uint64() const {
+ return upb_fielddef_defaultuint64(this);
+}
+inline uint32_t FieldDef::default_uint32() const {
+ return upb_fielddef_defaultuint32(this);
+}
+inline bool FieldDef::default_bool() const {
+ return upb_fielddef_defaultbool(this);
+}
+inline float FieldDef::default_float() const {
+ return upb_fielddef_defaultfloat(this);
+}
+inline double FieldDef::default_double() const {
+ return upb_fielddef_defaultdouble(this);
+}
+inline const char* FieldDef::default_string(size_t* len) const {
+ return upb_fielddef_defaultstr(this, len);
+}
+inline void FieldDef::set_default_int64(int64_t value) {
+ upb_fielddef_setdefaultint64(this, value);
+}
+inline void FieldDef::set_default_int32(int32_t value) {
+ upb_fielddef_setdefaultint32(this, value);
+}
+inline void FieldDef::set_default_uint64(uint64_t value) {
+ upb_fielddef_setdefaultuint64(this, value);
+}
+inline void FieldDef::set_default_uint32(uint32_t value) {
+ upb_fielddef_setdefaultuint32(this, value);
+}
+inline void FieldDef::set_default_bool(bool value) {
+ upb_fielddef_setdefaultbool(this, value);
+}
+inline void FieldDef::set_default_float(float value) {
+ upb_fielddef_setdefaultfloat(this, value);
+}
+inline void FieldDef::set_default_double(double value) {
+ upb_fielddef_setdefaultdouble(this, value);
+}
+inline bool FieldDef::set_default_string(const void *str, size_t len,
+ Status *s) {
+ return upb_fielddef_setdefaultstr(this, str, len, s);
+}
+inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
+ return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
+}
+inline void FieldDef::set_default_cstr(const char* str, Status* s) {
+ return upb_fielddef_setdefaultcstr(this, str, s);
+}
+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);
+}
+inline const EnumDef *FieldDef::enum_subdef() const {
+ return upb_fielddef_enumsubdef(this);
+}
+inline const char* FieldDef::subdef_name() const {
+ return upb_fielddef_subdefname(this);
+}
+inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
+ return upb_fielddef_setsubdef(this, subdef, s);
+}
+inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
+ return upb_fielddef_setenumsubdef(this, subdef, s);
+}
+inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
+ return upb_fielddef_setmsgsubdef(this, subdef, s);
+}
+inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
+ return upb_fielddef_setsubdefname(this, name, s);
+}
+inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
+ return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
+}
+
+inline reffed_ptr<MessageDef> MessageDef::New() {
+ upb_msgdef *m = upb_msgdef_new(&m);
+ return reffed_ptr<MessageDef>(m, &m);
+}
+inline bool MessageDef::IsFrozen() const { return upb_msgdef_isfrozen(this); }
+inline void MessageDef::Ref(const void* owner) const {
+ return upb_msgdef_ref(this, owner);
+}
+inline void MessageDef::Unref(const void* owner) const {
+ return upb_msgdef_unref(this, owner);
+}
+inline void MessageDef::DonateRef(const void* from, const void* to) const {
+ return upb_msgdef_donateref(this, from, to);
+}
+inline void MessageDef::CheckRef(const void* owner) const {
+ return upb_msgdef_checkref(this, owner);
+}
+inline const char *MessageDef::full_name() const {
+ return upb_msgdef_fullname(this);
+}
+inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
+ return upb_msgdef_setfullname(this, fullname, s);
+}
+inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
+ return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
+}
+inline bool MessageDef::Freeze(Status* status) {
+ return upb_msgdef_freeze(this, status);
+}
+inline int MessageDef::field_count() const {
+ return upb_msgdef_numfields(this);
+}
+inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
+ return upb_msgdef_addfield(this, f, NULL, s);
+}
+inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
+ return upb_msgdef_addfield(this, f.get(), NULL, s);
+}
+inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
+ return upb_msgdef_itof_mutable(this, number);
+}
+inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
+ return upb_msgdef_ntof_mutable(this, name, len);
+}
+inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
+ return upb_msgdef_itof(this, number);
+}
+inline const FieldDef *MessageDef::FindFieldByName(const char *name,
+ size_t len) const {
+ return upb_msgdef_ntof(this, name, len);
+}
+inline MessageDef* MessageDef::Dup(const void *owner) const {
+ return upb_msgdef_dup(this, owner);
+}
+inline MessageDef::iterator MessageDef::begin() { return iterator(this); }
+inline MessageDef::iterator MessageDef::end() { return iterator::end(this); }
+inline MessageDef::const_iterator MessageDef::begin() const {
+ return const_iterator(this);
+}
+inline MessageDef::const_iterator MessageDef::end() const {
+ return const_iterator::end(this);
+}
+
+inline MessageDef::iterator::iterator(MessageDef* md) {
+ upb_msg_begin(&iter_, md);
+}
+inline MessageDef::iterator MessageDef::iterator::end(MessageDef* md) {
+ MessageDef::iterator iter(md);
+ upb_msg_iter_setdone(&iter.iter_);
+ return iter;
+}
+inline FieldDef* MessageDef::iterator::operator*() const {
+ return upb_msg_iter_field(&iter_);
+}
+inline void MessageDef::iterator::operator++() { return upb_msg_next(&iter_); }
+inline bool MessageDef::iterator::operator==(const iterator &other) const {
+ return upb_inttable_iter_isequal(&iter_, &other.iter_);
+}
+inline bool MessageDef::iterator::operator!=(const iterator &other) const {
+ return !(*this == other);
+}
+
+inline MessageDef::const_iterator::const_iterator(const MessageDef* md) {
+ upb_msg_begin(&iter_, md);
+}
+inline MessageDef::const_iterator MessageDef::const_iterator::end(
+ const MessageDef *md) {
+ MessageDef::const_iterator iter(md);
+ upb_msg_iter_setdone(&iter.iter_);
+ return iter;
+}
+inline const FieldDef* MessageDef::const_iterator::operator*() const {
+ return upb_msg_iter_field(&iter_);
+}
+inline void MessageDef::const_iterator::operator++() {
+ return upb_msg_next(&iter_);
+}
+inline bool MessageDef::const_iterator::operator==(
+ const const_iterator &other) const {
+ return upb_inttable_iter_isequal(&iter_, &other.iter_);
+}
+inline bool MessageDef::const_iterator::operator!=(
+ const const_iterator &other) const {
+ return !(*this == other);
+}
+
+inline reffed_ptr<EnumDef> EnumDef::New() {
+ upb_enumdef *e = upb_enumdef_new(&e);
+ return reffed_ptr<EnumDef>(e, &e);
+}
+inline bool EnumDef::IsFrozen() const { return upb_enumdef_isfrozen(this); }
+inline void EnumDef::Ref(const void* owner) const {
+ return upb_enumdef_ref(this, owner);
+}
+inline void EnumDef::Unref(const void* owner) const {
+ return upb_enumdef_unref(this, owner);
+}
+inline void EnumDef::DonateRef(const void* from, const void* to) const {
+ return upb_enumdef_donateref(this, from, to);
+}
+inline void EnumDef::CheckRef(const void* owner) const {
+ return upb_enumdef_checkref(this, owner);
+}
+inline const char* EnumDef::full_name() const {
+ return upb_enumdef_fullname(this);
+}
+inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
+ return upb_enumdef_setfullname(this, fullname, s);
+}
+inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
+ return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
+}
+inline bool EnumDef::Freeze(Status* status) {
+ return upb_enumdef_freeze(this, status);
+}
+inline int32_t EnumDef::default_value() const {
+ return upb_enumdef_default(this);
+}
+inline bool EnumDef::set_default_value(int32_t val, Status* status) {
+ return upb_enumdef_setdefault(this, val, status);
+}
+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);
+}
+inline bool EnumDef::AddValue(const std::string& name, int32_t num,
+ Status* status) {
+ return upb_enumdef_addval(this, upb_safecstr(name), num, status);
+}
+inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
+ return upb_enumdef_ntoiz(this, name, num);
+}
+inline const char* EnumDef::FindValueByNumber(int32_t num) const {
+ return upb_enumdef_iton(this, num);
+}
+inline EnumDef* EnumDef::Dup(const void* owner) const {
+ return upb_enumdef_dup(this, owner);
+}
+
+inline EnumDef::Iterator::Iterator(const EnumDef* e) {
+ upb_enum_begin(&iter_, e);
+}
+inline int32_t EnumDef::Iterator::number() {
+ return upb_enum_iter_number(&iter_);
+}
+inline const char* EnumDef::Iterator::name() {
+ return upb_enum_iter_name(&iter_);
+}
+inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
+inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
+} // namespace upb
+#endif
+
+#undef UPB_DEFINE_DEF
+#undef UPB_DEF_CASTS
+#undef UPB_CPP_CASTS
+
+#endif /* UPB_DEF_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).
+// Do not edit -- your changes will be discarded when the file is
+// regenerated.
+
+#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
+#define GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
+
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * A symtab (symbol table) stores a name->def map of upb_defs. Clients could
+ * always create such tables themselves, but upb_symtab has logic for resolving
+ * symbolic references, and in particular, for keeping a whole set of consistent
+ * defs when replacing some subset of those defs. This logic is nontrivial.
+ *
+ * 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_SYMTAB_H_
+#define UPB_SYMTAB_H_
+
+
+#ifdef __cplusplus
+#include <vector>
+namespace upb { class SymbolTable; }
+#endif
+
+UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab);
+
+typedef struct {
+ UPB_PRIVATE_FOR_CPP
+ upb_strtable_iter iter;
+ upb_deftype_t type;
+} upb_symtab_iter;
+
+// Non-const methods in upb::SymbolTable are NOT thread-safe.
+UPB_DEFINE_CLASS1(upb::SymbolTable, upb::RefCounted,
+ public:
+ // Returns a new symbol table with a single ref owned by "owner."
+ // Returns NULL if memory allocation failed.
+ static reffed_ptr<SymbolTable> New();
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void *from, const void *to) const;
+ void CheckRef(const void *owner) const;
+
+ // 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;
+
+ // 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, but defs can replace existing defs 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.
+ //
+ // Any existing defs that can reach defs that are being replaced will
+ // themselves be replaced also, so that the resulting set of defs is fully
+ // consistent.
+ //
+ // This logic implemented in this method is a convenience; ultimately it
+ // calls some combination of upb_fielddef_setsubdef(), upb_def_dup(), and
+ // upb_freeze(), any of which the client could call themself. However, since
+ // the logic for doing so is nontrivial, we provide it here.
+ //
+ // 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.
+ //
+ // TODO(haberman): since the defs must be mutable, refining a frozen def
+ // requires making mutable copies of the entire tree. This is wasteful if
+ // only a few messages are changing. We may want to add a way of adding a
+ // tree of frozen defs to the symtab (perhaps an alternate constructor where
+ // you pass the root of the tree?)
+ bool Add(Def*const* defs, int n, void* ref_donor, upb_status* status);
+
+ bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
+ return Add((Def*const*)&defs[0], defs.size(), owner, status);
+ }
+
+ private:
+ UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable);
+,
+UPB_DEFINE_STRUCT(upb_symtab, upb_refcounted,
+ upb_strtable symtab;
+));
+
+#define UPB_SYMTAB_INIT(symtab, refs, ref2s) \
+ { UPB_REFCOUNT_INIT(refs, ref2s), symtab }
+
+UPB_BEGIN_EXTERN_C // {
+
+// Native C API.
+// From upb_refcounted.
+bool upb_symtab_isfrozen(const upb_symtab *s);
+void upb_symtab_ref(const upb_symtab *s, const void *owner);
+void upb_symtab_unref(const upb_symtab *s, const void *owner);
+void upb_symtab_donateref(
+ const upb_symtab *s, const void *from, const void *to);
+void upb_symtab_checkref(const upb_symtab *s, const void *owner);
+
+upb_symtab *upb_symtab_new(const void *owner);
+void upb_symtab_freeze(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, int n, void *ref_donor,
+ 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 // }
+
+#ifdef __cplusplus
+// C++ inline wrappers.
+namespace upb {
+inline reffed_ptr<SymbolTable> SymbolTable::New() {
+ upb_symtab *s = upb_symtab_new(&s);
+ return reffed_ptr<SymbolTable>(s, &s);
+}
+
+inline bool SymbolTable::IsFrozen() const {
+ return upb_symtab_isfrozen(this);
+}
+inline void SymbolTable::Ref(const void *owner) const {
+ upb_symtab_ref(this, owner);
+}
+inline void SymbolTable::Unref(const void *owner) const {
+ upb_symtab_unref(this, owner);
+}
+inline void SymbolTable::DonateRef(const void *from, const void *to) const {
+ upb_symtab_donateref(this, from, to);
+}
+inline void SymbolTable::CheckRef(const void *owner) const {
+ upb_symtab_checkref(this, owner);
+}
+
+inline void SymbolTable::Freeze() {
+ return upb_symtab_freeze(this);
+}
+inline const Def *SymbolTable::Resolve(const char *base,
+ const char *sym) const {
+ return upb_symtab_resolve(this, base, sym);
+}
+inline const Def* SymbolTable::Lookup(const char *sym) const {
+ return upb_symtab_lookup(this, sym);
+}
+inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
+ return upb_symtab_lookupmsg(this, sym);
+}
+inline bool SymbolTable::Add(
+ Def*const* defs, int n, void* ref_donor, upb_status* status) {
+ return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
+}
+} // namespace upb
+#endif
+
+#endif /* UPB_SYMTAB_H_ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// 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_FILEOPTIONS_SPEED = 1,
+ GOOGLE_PROTOBUF_FILEOPTIONS_CODE_SIZE = 2,
+ GOOGLE_PROTOBUF_FILEOPTIONS_LITE_RUNTIME = 3,
+} google_protobuf_FileOptions_OptimizeMode;
+
+// Selectors
+
+// google.protobuf.DescriptorProto
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 4
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSUBMSG 6
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_STARTSUBMSG 7
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSEQ 8
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSEQ 9
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSUBMSG 10
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSEQ 11
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSEQ 12
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSUBMSG 13
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 14
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 15
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 16
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSEQ 17
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSEQ 18
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSUBMSG 19
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSEQ 20
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSEQ 21
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSUBMSG 22
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_ENDSUBMSG 23
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STRING 24
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STARTSTR 25
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_ENDSTR 26
+
+// google.protobuf.DescriptorProto.ExtensionRange
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_START_INT32 2
+#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_END_INT32 3
+
+// google.protobuf.EnumDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSEQ 4
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSEQ 5
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSUBMSG 6
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STRING 8
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STARTSTR 9
+#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_ENDSTR 10
+
+// google.protobuf.EnumOptions
+#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_ALLOW_ALIAS_BOOL 6
+
+// google.protobuf.EnumValueDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STRING 4
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STARTSTR 5
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_ENDSTR 6
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NUMBER_INT32 7
+
+// google.protobuf.EnumValueOptions
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+
+// google.protobuf.FieldDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STRING 4
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STARTSTR 5
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_ENDSTR 6
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STRING 7
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STARTSTR 8
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_ENDSTR 9
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NUMBER_INT32 10
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_INT32 11
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 12
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STRING 13
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STARTSTR 14
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_ENDSTR 15
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STRING 16
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STARTSTR 17
+#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_ENDSTR 18
+
+// google.protobuf.FieldOptions
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_INT32 6
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_PACKED_BOOL 7
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_DEPRECATED_BOOL 8
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_LAZY_BOOL 9
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_STRING 10
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_STARTSTR 11
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_ENDSTR 12
+#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_WEAK_BOOL 13
+
+// google.protobuf.FileDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSUBMSG 4
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 6
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_STARTSUBMSG 7
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSEQ 8
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSEQ 9
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSUBMSG 10
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 11
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 12
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 13
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSEQ 14
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSEQ 15
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSUBMSG 16
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSEQ 17
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSEQ 18
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSUBMSG 19
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 20
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_ENDSUBMSG 21
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STRING 22
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STARTSTR 23
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_ENDSTR 24
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STRING 25
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STARTSTR 26
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_ENDSTR 27
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSEQ 28
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSEQ 29
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STRING 30
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSTR 31
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSTR 32
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_STARTSEQ 33
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_ENDSEQ 34
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_INT32 35
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_STARTSEQ 36
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_ENDSEQ 37
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_INT32 38
+
+// google.protobuf.FileDescriptorSet
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSUBMSG 5
+
+// google.protobuf.FileOptions
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STRING 6
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STARTSTR 7
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_ENDSTR 8
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STRING 9
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STARTSTR 10
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_ENDSTR 11
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZE_FOR_INT32 12
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_MULTIPLE_FILES_BOOL 13
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STRING 14
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STARTSTR 15
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_ENDSTR 16
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_GENERIC_SERVICES_BOOL 17
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERIC_SERVICES_BOOL 18
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_PY_GENERIC_SERVICES_BOOL 19
+#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERATE_EQUALS_AND_HASH_BOOL 20
+
+// google.protobuf.MessageOptions
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MESSAGE_SET_WIRE_FORMAT_BOOL 6
+#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_NO_STANDARD_DESCRIPTOR_ACCESSOR_BOOL 7
+
+// google.protobuf.MethodDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STRING 4
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STARTSTR 5
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_ENDSTR 6
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STRING 7
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STARTSTR 8
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_ENDSTR 9
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STRING 10
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STARTSTR 11
+#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_ENDSTR 12
+
+// google.protobuf.MethodOptions
+#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+
+// google.protobuf.ServiceDescriptorProto
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSEQ 4
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSEQ 5
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSUBMSG 6
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STRING 8
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STARTSTR 9
+#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_ENDSTR 10
+
+// google.protobuf.ServiceOptions
+#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
+
+// google.protobuf.SourceCodeInfo
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSUBMSG 5
+
+// google.protobuf.SourceCodeInfo.Location
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_STARTSEQ 2
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_ENDSEQ 3
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_INT32 4
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_STARTSEQ 5
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_ENDSEQ 6
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_INT32 7
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STRING 8
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STARTSTR 9
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_ENDSTR 10
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STRING 11
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STARTSTR 12
+#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_ENDSTR 13
+
+// google.protobuf.UninterpretedOption
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSUBMSG 2
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSEQ 3
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSEQ 4
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSUBMSG 5
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STRING 6
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STARTSTR 7
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_ENDSTR 8
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_POSITIVE_INT_VALUE_UINT64 9
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NEGATIVE_INT_VALUE_INT64 10
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_DOUBLE_VALUE_DOUBLE 11
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STRING 12
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STARTSTR 13
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_ENDSTR 14
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STRING 15
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STARTSTR 16
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_ENDSTR 17
+
+// google.protobuf.UninterpretedOption.NamePart
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STRING 2
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STARTSTR 3
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_ENDSTR 4
+#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_IS_EXTENSION_BOOL 5
+
+const upb_symtab *upbdefs_google_protobuf_descriptor(const void *owner);
+
+// MessageDefs
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ExtensionRange");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorSet");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MessageOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MessageOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceDescriptorProto");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceOptions(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceOptions");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo.Location");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption");
+ assert(m);
+ return m;
+}
+UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart(const upb_symtab *s) {
+ const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption.NamePart");
+ assert(m);
+ return m;
+}
+
+
+// EnumDefs
+UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label(const upb_symtab *s) {
+ const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Label");
+ assert(e);
+ return e;
+}
+UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type(const upb_symtab *s) {
+ const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Type");
+ assert(e);
+ return e;
+}
+UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType(const upb_symtab *s) {
+ const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.CType");
+ assert(e);
+ return e;
+}
+UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode(const upb_symtab *s) {
+ const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FileOptions.OptimizeMode");
+ assert(e);
+ return e;
+}
+
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 6); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 5); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_field(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_nested_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 7); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_allow_alias(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_default_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 7); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_extendee(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_label(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 8); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 5); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 6); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_ctype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_experimental_map_key(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 9); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_lazy(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 5); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_packed(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_weak(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 10); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 5); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 7); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_message_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 8); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_public_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 10); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_service(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 6); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_source_code_info(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 9); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_weak_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 11); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_file(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorSet(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 16); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_go_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 11); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 20); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 17); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_multiple_files(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 10); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_outer_classname(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 8); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_optimize_for(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 9); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_py_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 18); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_message_set_wire_format(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_input_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_output_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_method(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 999); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_path(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_span(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_location(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 1); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_aggregate_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 8); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_double_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 6); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_identifier_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 3); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 2); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_negative_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 5); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_positive_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 4); }
+UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_string_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 7); }
+
+#ifdef __cplusplus
+}; // extern "C"
+#endif
+
+#ifdef __cplusplus
+
+namespace upbdefs {
+namespace google {
+namespace protobuf {
+namespace descriptor {
+inline upb::reffed_ptr<const upb::SymbolTable> SymbolTable() {
+ const upb::SymbolTable* s = upbdefs_google_protobuf_descriptor(&s);
+ return upb::reffed_ptr<const upb::SymbolTable>(s, &s);
+}
+} // namespace descriptor
+} // namespace protobuf
+} // namespace google
+
+#define RETURN_REFFED(type, func) \
+ const type* obj = func(upbdefs::google::protobuf::descriptor::SymbolTable().get()); \
+ return upb::reffed_ptr<const type>(obj);
+
+namespace google {
+namespace protobuf {
+namespace DescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_enum_type) }
+inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension) }
+inline upb::reffed_ptr<const upb::FieldDef> extension_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension_range) }
+inline upb::reffed_ptr<const upb::FieldDef> field() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_field) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> nested_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_nested_type) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_options) }
+} // namespace DescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace DescriptorProto {
+namespace ExtensionRange {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange) }
+inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end) }
+inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start) }
+} // namespace ExtensionRange
+} // namespace DescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace EnumDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_options) }
+inline upb::reffed_ptr<const upb::FieldDef> value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_value) }
+} // namespace EnumDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace EnumOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> allow_alias() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_allow_alias) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_uninterpreted_option) }
+} // namespace EnumOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace EnumValueDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_number) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_options) }
+} // namespace EnumValueDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace EnumValueOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option) }
+} // namespace EnumValueOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace FieldDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> default_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_default_value) }
+inline upb::reffed_ptr<const upb::FieldDef> extendee() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_extendee) }
+inline upb::reffed_ptr<const upb::FieldDef> label() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_label) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_number) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_options) }
+inline upb::reffed_ptr<const upb::FieldDef> type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type) }
+inline upb::reffed_ptr<const upb::FieldDef> type_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type_name) }
+inline upb::reffed_ptr<const upb::EnumDef> Label() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Label) }
+inline upb::reffed_ptr<const upb::EnumDef> Type() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Type) }
+} // namespace FieldDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace FieldOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> ctype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_ctype) }
+inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_deprecated) }
+inline upb::reffed_ptr<const upb::FieldDef> experimental_map_key() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_experimental_map_key) }
+inline upb::reffed_ptr<const upb::FieldDef> lazy() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_lazy) }
+inline upb::reffed_ptr<const upb::FieldDef> packed() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_packed) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_uninterpreted_option) }
+inline upb::reffed_ptr<const upb::FieldDef> weak() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_weak) }
+inline upb::reffed_ptr<const upb::EnumDef> CType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_CType) }
+} // namespace FieldOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace FileDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_dependency) }
+inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_enum_type) }
+inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_extension) }
+inline upb::reffed_ptr<const upb::FieldDef> message_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_message_type) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_options) }
+inline upb::reffed_ptr<const upb::FieldDef> package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_package) }
+inline upb::reffed_ptr<const upb::FieldDef> public_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_public_dependency) }
+inline upb::reffed_ptr<const upb::FieldDef> service() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_service) }
+inline upb::reffed_ptr<const upb::FieldDef> source_code_info() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_source_code_info) }
+inline upb::reffed_ptr<const upb::FieldDef> weak_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_weak_dependency) }
+} // namespace FileDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace FileDescriptorSet {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorSet) }
+inline upb::reffed_ptr<const upb::FieldDef> file() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorSet_file) }
+} // namespace FileDescriptorSet
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace FileOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> cc_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_generic_services) }
+inline upb::reffed_ptr<const upb::FieldDef> go_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_go_package) }
+inline upb::reffed_ptr<const upb::FieldDef> java_generate_equals_and_hash() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash) }
+inline upb::reffed_ptr<const upb::FieldDef> java_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generic_services) }
+inline upb::reffed_ptr<const upb::FieldDef> java_multiple_files() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_multiple_files) }
+inline upb::reffed_ptr<const upb::FieldDef> java_outer_classname() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_outer_classname) }
+inline upb::reffed_ptr<const upb::FieldDef> java_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_package) }
+inline upb::reffed_ptr<const upb::FieldDef> optimize_for() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_optimize_for) }
+inline upb::reffed_ptr<const upb::FieldDef> py_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_py_generic_services) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_uninterpreted_option) }
+inline upb::reffed_ptr<const upb::EnumDef> OptimizeMode() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FileOptions_OptimizeMode) }
+} // namespace FileOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace MessageOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MessageOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> message_set_wire_format() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_message_set_wire_format) }
+inline upb::reffed_ptr<const upb::FieldDef> no_standard_descriptor_accessor() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_uninterpreted_option) }
+} // namespace MessageOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace MethodDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> input_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_input_type) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_options) }
+inline upb::reffed_ptr<const upb::FieldDef> output_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_output_type) }
+} // namespace MethodDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace MethodOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_uninterpreted_option) }
+} // namespace MethodOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace ServiceDescriptorProto {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceDescriptorProto) }
+inline upb::reffed_ptr<const upb::FieldDef> method() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_method) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_name) }
+inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_options) }
+} // namespace ServiceDescriptorProto
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace ServiceOptions {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceOptions) }
+inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_uninterpreted_option) }
+} // namespace ServiceOptions
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace SourceCodeInfo {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo) }
+inline upb::reffed_ptr<const upb::FieldDef> location() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_location) }
+} // namespace SourceCodeInfo
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace SourceCodeInfo {
+namespace Location {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo_Location) }
+inline upb::reffed_ptr<const upb::FieldDef> leading_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments) }
+inline upb::reffed_ptr<const upb::FieldDef> path() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_path) }
+inline upb::reffed_ptr<const upb::FieldDef> span() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_span) }
+inline upb::reffed_ptr<const upb::FieldDef> trailing_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments) }
+} // namespace Location
+} // namespace SourceCodeInfo
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace UninterpretedOption {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption) }
+inline upb::reffed_ptr<const upb::FieldDef> aggregate_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_aggregate_value) }
+inline upb::reffed_ptr<const upb::FieldDef> double_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_double_value) }
+inline upb::reffed_ptr<const upb::FieldDef> identifier_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_identifier_value) }
+inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_name) }
+inline upb::reffed_ptr<const upb::FieldDef> negative_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_negative_int_value) }
+inline upb::reffed_ptr<const upb::FieldDef> positive_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_positive_int_value) }
+inline upb::reffed_ptr<const upb::FieldDef> string_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_string_value) }
+} // namespace UninterpretedOption
+} // namespace protobuf
+} // namespace google
+
+namespace google {
+namespace protobuf {
+namespace UninterpretedOption {
+namespace NamePart {
+inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption_NamePart) }
+inline upb::reffed_ptr<const upb::FieldDef> is_extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension) }
+inline upb::reffed_ptr<const upb::FieldDef> name_part() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part) }
+} // namespace NamePart
+} // namespace UninterpretedOption
+} // namespace protobuf
+} // namespace google
+
+} // namespace upbdefs
+
+
+#undef RETURN_REFFED
+#endif // __cplusplus
+
+#endif // GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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.
+ */
+
+#ifndef UPB_HANDLERS_H
+#define UPB_HANDLERS_H
+
+
+#ifdef __cplusplus
+namespace upb {
+class BufferHandle;
+class BytesHandler;
+class HandlerAttributes;
+class Handlers;
+template <class T> class Handler;
+template <class T> struct CanonicalType;
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle);
+UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler);
+UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr);
+UPB_DECLARE_TYPE(upb::Handlers, upb_handlers);
+
+// 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
+
+// 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;
+
+#define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
+
+#define UPB_BREAK NULL
+
+// A convenient definition for when no closure is needed.
+extern char _upb_noclosure;
+#define UPB_NO_CLOSURE &_upb_noclosure
+
+// 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;
+
+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_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);
+
+UPB_END_EXTERN_C
+
+
+// Static selectors for upb::Handlers.
+#define UPB_STARTMSG_SELECTOR 0
+#define UPB_ENDMSG_SELECTOR 1
+#define UPB_STATIC_SELECTOR_COUNT 2
+
+// Static selectors for upb::BytesHandler.
+#define UPB_STARTSTR_SELECTOR 0
+#define UPB_STRING_SELECTOR 1
+#define UPB_ENDSTR_SELECTOR 2
+
+typedef void upb_handlerfree(void *d);
+
+// A set of attributes that accompanies a handler's function pointer.
+UPB_DEFINE_CLASS0(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;
+
+ private:
+ friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
+ const upb_handlerattr *attr);
+,
+UPB_DEFINE_STRUCT0(upb_handlerattr,
+ const void *handler_data_;
+ const void *closure_type_;
+ const void *return_closure_type_;
+ bool alwaysok_;
+));
+
+#define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
+
+typedef struct {
+ upb_func *func;
+ // 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;
+
+// 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.
+UPB_DEFINE_CLASS0(upb::BufferHandle,
+ public:
+ BufferHandle();
+ ~BufferHandle();
+
+ // 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;
+
+ // The offset within the attached object where this buffer begins. Only
+ // meaningful if there is an attached object.
+ size_t object_offset() const;
+
+ // Note that object_offset is the offset of "buf" within the attached object.
+ void SetBuffer(const char* buf, size_t object_offset);
+
+ // The BufferHandle can have an "attached object", which can be used to
+ // tunnel through a pointer to the buffer's underlying representation.
+ template <class T>
+ void SetAttachedObject(const T* obj);
+
+ // Returns NULL if the attached object is not of this type.
+ template <class T>
+ const T* GetAttachedObject() const;
+
+ 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);
+,
+UPB_DEFINE_STRUCT0(upb_bufhandle,
+ const char *buf_;
+ const void *obj_;
+ const void *objtype_;
+ size_t objofs_;
+));
+
+// 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.
+UPB_DEFINE_CLASS1(upb::Handlers, upb::RefCounted,
+ public:
+ typedef upb_selector_t Selector;
+ typedef upb_handlertype_t Type;
+
+ typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
+ typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
+ typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
+ typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
+ typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
+ typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
+ const BufferHandle *)> StringHandler;
+
+ template <class T> struct ValueHandler {
+ typedef Handler<bool(*)(void *, const void *, T)> H;
+ };
+
+ typedef ValueHandler<int32_t>::H Int32Handler;
+ typedef ValueHandler<int64_t>::H Int64Handler;
+ typedef ValueHandler<uint32_t>::H UInt32Handler;
+ typedef ValueHandler<uint64_t>::H UInt64Handler;
+ typedef ValueHandler<float>::H FloatHandler;
+ typedef ValueHandler<double>::H DoubleHandler;
+ typedef ValueHandler<bool>::H BoolHandler;
+
+ // Any function pointer can be converted to this and converted back to its
+ // correct type.
+ typedef void GenericFunction();
+
+ typedef void HandlersCallback(const void *closure, upb_handlers *h);
+
+ // Returns a new handlers object for the given frozen msgdef.
+ // Returns NULL if memory allocation failed.
+ static reffed_ptr<Handlers> 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<const Handlers> NewFrozen(const MessageDef *m,
+ HandlersCallback *callback,
+ const void *closure);
+
+ // Functionality from upb::RefCounted.
+ bool IsFrozen() const;
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void *from, const void *to) const;
+ void CheckRef(const void *owner) const;
+
+ // 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*>& handlers, Status* s);
+
+ // Returns the msgdef associated with this handlers object.
+ const MessageDef* message_def() const;
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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<T>(f, UpbBind(MyHandler<T>, MyData));
+ template <class T>
+ bool SetValueHandler(
+ const FieldDef *f,
+ const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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;
+
+ // Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
+ // field.
+ const Handlers* GetSubHandlers(Selector startsubmsg) const;
+
+ // 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);
+
+ // Given a START selector of any kind, returns the corresponding END selector.
+ static Selector GetEndSelector(Selector start_selector);
+
+ // 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);
+
+ // Sets the given attributes to the attributes for this selector.
+ bool GetAttributes(Selector selector, HandlerAttributes* attr);
+
+ // Returns the handler data that was registered with this handler.
+ const void* GetHandlerData(Selector selector);
+
+ // Could add any of the following functions as-needed, with some minor
+ // implementation changes:
+ //
+ // const FieldDef* GetFieldDef(Selector selector);
+ // static bool IsSequence(Selector selector);
+
+ private:
+ UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers);
+
+ 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);
+
+,
+UPB_DEFINE_STRUCT(upb_handlers, upb_refcounted,
+ 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.
+));
+
+
+#ifdef __cplusplus
+
+namespace upb {
+
+// 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 ...
+// }
+//
+// // Handler that doesn't need any data bound to it.
+// void OnValue2(MyClosure* c, int32_t val) {
+// // do stuff ...
+// }
+//
+// // Handler that returns bool so it can return failure if necessary.
+// bool OnValue3(MyClosure* c, int32_t val) {
+// // do stuff ...
+// return ok;
+// }
+//
+// // 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));
+
+#ifdef UPB_CXX11
+
+// In C++11, the "template" disambiguator can appear even outside templates,
+// so all calls can safely use this pair of macros.
+
+#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
+
+// We have to be careful to only evaluate "d" once.
+#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
+
+#else
+
+// Prior to C++11, the "template" disambiguator may only appear inside a
+// template, so the regular macro must not use "template"
+
+#define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
+
+#define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
+
+#endif // UPB_CXX11
+
+// 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<f>()
+
+// We have to be careful to only evaluate "d" once.
+#define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
+
+// 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 T> class Handler {
+ public:
+ // The underlying, handler function signature that upb uses internally.
+ typedef T FuncPtr;
+
+ // Intentionally implicit.
+ template <class F> Handler(F func);
+ ~Handler();
+
+ private:
+ void AddCleanup(Handlers* h) const {
+ if (cleanup_func_) {
+ bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
+ UPB_ASSERT_VAR(ok, ok);
+ }
+ }
+
+ UPB_DISALLOW_COPY_AND_ASSIGN(Handler);
+ friend class Handlers;
+ FuncPtr handler_;
+ mutable HandlerAttributes attr_;
+ mutable bool registered_;
+ void *cleanup_data_;
+ upb_handlerfree *cleanup_func_;
+};
+
+} // namespace upb
+
+#endif // __cplusplus
+
+UPB_BEGIN_EXTERN_C
+
+// Native C API.
+
+// Handler function typedefs.
+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);
+
+// upb_bufhandle
+size_t upb_bufhandle_objofs(const upb_bufhandle *h);
+
+// upb_handlerattr
+void upb_handlerattr_init(upb_handlerattr *attr);
+void upb_handlerattr_uninit(upb_handlerattr *attr);
+
+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);
+
+UPB_INLINE const void *upb_handlerattr_handlerdata(
+ const upb_handlerattr *attr) {
+ return attr->handler_data_;
+}
+
+// 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);
+bool upb_handlers_isfrozen(const upb_handlers *h);
+void upb_handlers_ref(const upb_handlers *h, const void *owner);
+void upb_handlers_unref(const upb_handlers *h, const void *owner);
+void upb_handlers_donateref(const upb_handlers *h, const void *from,
+ const void *to);
+void upb_handlers_checkref(const upb_handlers *h, const void *owner);
+
+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_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);
+
+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);
+
+UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
+ upb_selector_t s) {
+ return (upb_func *)h->table[s].func;
+}
+
+bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
+ upb_handlerattr *attr);
+
+UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
+ upb_selector_t s) {
+ return upb_handlerattr_handlerdata(&h->table[s].attr);
+}
+
+// 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.
+UPB_DEFINE_CLASS0(upb::BytesHandler,
+ public:
+ BytesHandler();
+ ~BytesHandler();
+,
+UPB_DEFINE_STRUCT0(upb_byteshandler,
+ upb_handlers_tabent table[3];
+));
+
+void upb_byteshandler_init(upb_byteshandler *h);
+void upb_byteshandler_uninit(upb_byteshandler *h);
+
+// Caller must ensure that "d" outlives the handlers.
+// TODO(haberman): support handlerfree function for the data.
+// 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);
+
+// "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;
+}
+
+// Internal-only.
+uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
+uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
+
+UPB_END_EXTERN_C
+
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Inline definitions for handlers.h, which are particularly long and a bit
+ * tricky.
+ */
+
+#ifndef UPB_HANDLERS_INL_H_
+#define UPB_HANDLERS_INL_H_
+
+#include <limits.h>
+
+// 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.
+
+// 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
+
+#if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
+#define UPB_LONG_IS_32BITS 1
+#endif
+
+#if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
+#define UPB_LONG_IS_64BITS 1
+#endif
+
+#if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
+#define UPB_LLONG_IS_64BITS 1
+#endif
+
+// 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
+
+#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
+
+
+#if UPB_LONG_IS_64BITS
+#define UPB_INT64_T long
+#define UPB_UINT64_T unsigned long
+
+#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
+
+#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
+
+// C inline methods.
+
+// 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_;
+}
+
+
+#ifdef __cplusplus
+
+namespace upb {
+
+typedef void CleanupFunc(void *ptr);
+
+// 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 <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
+template <class T> struct remove_constptr<const T *> { typedef T *type; };
+
+// Template that we use below to remove a template specialization from
+// consideration if it matches a specific type.
+template <class T, class U> struct disable_if_same { typedef void Type; };
+template <class T> struct disable_if_same<T, T> {};
+
+template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
+
+template <class T1, class T2>
+struct FirstUnlessVoid {
+ typedef T1 value;
+};
+
+template <class T2>
+struct FirstUnlessVoid<void, T2> {
+ typedef T2 value;
+};
+
+template<class T, class U>
+struct is_same {
+ static bool value;
+};
+
+template<class T>
+struct is_same<T, T> {
+ static bool value;
+};
+
+template<class T, class U>
+bool is_same<T, U>::value = false;
+
+template<class T>
+bool is_same<T, T>::value = true;
+
+// FuncInfo ////////////////////////////////////////////////////////////////////
+
+// Info about the user's original, pre-wrapped function.
+template <class C, class R = void>
+struct FuncInfo {
+ // The type of the closure that the function takes (its first param).
+ typedef C Closure;
+
+ // The return type.
+ typedef R Return;
+};
+
+// Func ////////////////////////////////////////////////////////////////////////
+
+// 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; }
+};
+
+template <class R, class P1, R F(P1), class I>
+struct Func1 : public UnboundFunc {
+ typedef R Return;
+ typedef I FuncInfo;
+ static R Call(P1 p1) { return F(p1); }
+};
+
+template <class R, class P1, class P2, R F(P1, P2), class I>
+struct Func2 : public UnboundFunc {
+ typedef R Return;
+ typedef I FuncInfo;
+ static R Call(P1 p1, P2 p2) { return F(p1, p2); }
+};
+
+template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
+struct Func3 : public UnboundFunc {
+ typedef R Return;
+ typedef I FuncInfo;
+ static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
+};
+
+template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
+ class I>
+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); }
+};
+
+template <class R, class P1, class P2, class P3, class P4, class P5,
+ R F(P1, P2, P3, P4, P5), class I>
+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);
+ }
+};
+
+// BoundFunc ///////////////////////////////////////////////////////////////////
+
+// 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 <class T>
+struct BoundFunc {
+ typedef typename remove_constptr<T>::type MutableP2;
+ explicit BoundFunc(MutableP2 data_) : data(data_) {}
+ CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
+ MutableP2 GetData() { return data; }
+ MutableP2 data;
+};
+
+template <class R, class P1, class P2, R F(P1, P2), class I>
+struct BoundFunc2 : public BoundFunc<P2> {
+ typedef BoundFunc<P2> Base;
+ typedef I FuncInfo;
+ explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
+};
+
+template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
+struct BoundFunc3 : public BoundFunc<P2> {
+ typedef BoundFunc<P2> Base;
+ typedef I FuncInfo;
+ explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
+};
+
+template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
+ class I>
+struct BoundFunc4 : public BoundFunc<P2> {
+ typedef BoundFunc<P2> Base;
+ typedef I FuncInfo;
+ explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
+};
+
+template <class R, class P1, class P2, class P3, class P4, class P5,
+ R F(P1, P2, P3, P4, P5), class I>
+struct BoundFunc5 : public BoundFunc<P2> {
+ typedef BoundFunc<P2> Base;
+ typedef I FuncInfo;
+ explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
+};
+
+// FuncSig /////////////////////////////////////////////////////////////////////
+
+// 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 <class R, class P1>
+struct FuncSig1 {
+ template <R F(P1)>
+ Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
+ return Func1<R, P1, F, FuncInfo<P1, R> >();
+ }
+};
+
+template <class R, class P1, class P2>
+struct FuncSig2 {
+ template <R F(P1, P2)>
+ Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
+ return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
+ }
+
+ template <R F(P1, P2)>
+ BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
+ typename remove_constptr<P2>::type param2) {
+ return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
+ }
+};
+
+template <class R, class P1, class P2, class P3>
+struct FuncSig3 {
+ template <R F(P1, P2, P3)>
+ Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
+ return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
+ }
+
+ template <R F(P1, P2, P3)>
+ BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
+ typename remove_constptr<P2>::type param2) {
+ return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
+ }
+};
+
+template <class R, class P1, class P2, class P3, class P4>
+struct FuncSig4 {
+ template <R F(P1, P2, P3, P4)>
+ Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
+ return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
+ }
+
+ template <R F(P1, P2, P3, P4)>
+ BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
+ typename remove_constptr<P2>::type param2) {
+ return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
+ }
+};
+
+template <class R, class P1, class P2, class P3, class P4, class P5>
+struct FuncSig5 {
+ template <R F(P1, P2, P3, P4, P5)>
+ Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
+ return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
+ }
+
+ template <R F(P1, P2, P3, P4, P5)>
+ BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
+ typename remove_constptr<P2>::type param2) {
+ return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
+ }
+};
+
+// Overloaded template function that can construct the appropriate FuncSig*
+// class given a function pointer by deducing the template parameters.
+template <class R, class P1>
+inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return FuncSig1<R, P1>();
+}
+
+template <class R, class P1, class P2>
+inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return FuncSig2<R, P1, P2>();
+}
+
+template <class R, class P1, class P2, class P3>
+inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return FuncSig3<R, P1, P2, P3>();
+}
+
+template <class R, class P1, class P2, class P3, class P4>
+inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return FuncSig4<R, P1, P2, P3, P4>();
+}
+
+template <class R, class P1, class P2, class P3, class P4, class P5>
+inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return FuncSig5<R, P1, P2, P3, P4, P5>();
+}
+
+// MethodSig ///////////////////////////////////////////////////////////////////
+
+// CallMethod*: a function template that calls a given method.
+template <class R, class C, R (C::*F)()>
+R CallMethod0(C *obj) {
+ return ((*obj).*F)();
+}
+
+template <class R, class C, class P1, R (C::*F)(P1)>
+R CallMethod1(C *obj, P1 arg1) {
+ return ((*obj).*F)(arg1);
+}
+
+template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
+R CallMethod2(C *obj, P1 arg1, P2 arg2) {
+ return ((*obj).*F)(arg1, arg2);
+}
+
+template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
+R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
+ return ((*obj).*F)(arg1, arg2, arg3);
+}
+
+template <class R, class C, class P1, class P2, class P3, class P4,
+ R (C::*F)(P1, P2, P3, P4)>
+R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
+ return ((*obj).*F)(arg1, arg2, arg3, arg4);
+}
+
+// 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 <class R, class C>
+struct MethodSig0 {
+ template <R (C::*F)()>
+ Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
+ return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
+ }
+};
+
+template <class R, class C, class P1>
+struct MethodSig1 {
+ template <R (C::*F)(P1)>
+ Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
+ return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
+ }
+
+ template <R (C::*F)(P1)>
+ BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
+ typename remove_constptr<P1>::type param1) {
+ return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
+ param1);
+ }
+};
+
+template <class R, class C, class P1, class P2>
+struct MethodSig2 {
+ template <R (C::*F)(P1, P2)>
+ Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
+ GetFunc() {
+ return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
+ FuncInfo<C *, R> >();
+ }
+
+ template <R (C::*F)(P1, P2)>
+ BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
+ GetFunc(typename remove_constptr<P1>::type param1) {
+ return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
+ FuncInfo<C *, R> >(param1);
+ }
+};
+
+template <class R, class C, class P1, class P2, class P3>
+struct MethodSig3 {
+ template <R (C::*F)(P1, P2, P3)>
+ Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
+ GetFunc() {
+ return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
+ FuncInfo<C *, R> >();
+ }
+
+ template <R (C::*F)(P1, P2, P3)>
+ BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
+ FuncInfo<C *, R> >
+ GetFunc(typename remove_constptr<P1>::type param1) {
+ return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
+ FuncInfo<C *, R> >(param1);
+ }
+};
+
+template <class R, class C, class P1, class P2, class P3, class P4>
+struct MethodSig4 {
+ template <R (C::*F)(P1, P2, P3, P4)>
+ Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
+ FuncInfo<C *, R> >
+ GetFunc() {
+ return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
+ FuncInfo<C *, R> >();
+ }
+
+ template <R (C::*F)(P1, P2, P3, P4)>
+ BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
+ FuncInfo<C *, R> >
+ GetFunc(typename remove_constptr<P1>::type param1) {
+ return BoundFunc5<R, C *, P1, P2, P3, P4,
+ CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
+ param1);
+ }
+};
+
+template <class R, class C>
+inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return MethodSig0<R, C>();
+}
+
+template <class R, class C, class P1>
+inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return MethodSig1<R, C, P1>();
+}
+
+template <class R, class C, class P1, class P2>
+inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return MethodSig2<R, C, P1, P2>();
+}
+
+template <class R, class C, class P1, class P2, class P3>
+inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return MethodSig3<R, C, P1, P2, P3>();
+}
+
+template <class R, class C, class P1, class P2, class P3, class P4>
+inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
+ UPB_UNUSED(f); // Only used for template parameter deduction.
+ return MethodSig4<R, C, P1, P2, P3, P4>();
+}
+
+// MaybeWrapReturn /////////////////////////////////////////////////////////////
+
+// 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 is expected to return void* but doesn't, wrap it so it
+// does (either by returning the closure param if the wrapped function
+// returns void or by casting a different pointer type to void* for
+// return).
+
+// Template parameters are FuncN type and desired return type.
+template <class F, class R, class Enable = void>
+struct MaybeWrapReturn;
+
+// If the return type matches, return the given function unwrapped.
+template <class F>
+struct MaybeWrapReturn<F, typename F::Return> {
+ typedef F Func;
+};
+
+// Function wrapper that munges the return value from void to (bool)true.
+template <class P1, class P2, void F(P1, P2)>
+bool ReturnTrue2(P1 p1, P2 p2) {
+ F(p1, p2);
+ return true;
+}
+
+template <class P1, class P2, class P3, void F(P1, P2, P3)>
+bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
+ F(p1, p2, p3);
+ return true;
+}
+
+// Function wrapper that munges the return value from void to (void*)arg1
+template <class P1, class P2, void F(P1, P2)>
+void *ReturnClosure2(P1 p1, P2 p2) {
+ F(p1, p2);
+ return p1;
+}
+
+template <class P1, class P2, class P3, void F(P1, P2, P3)>
+void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
+ F(p1, p2, p3);
+ return p1;
+}
+
+// Function wrapper that munges the return value from R to void*.
+template <class R, class P1, class P2, R F(P1, P2)>
+void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
+ return F(p1, p2);
+}
+
+template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
+void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
+ return F(p1, p2, p3);
+}
+
+// Function wrapper that munges the return value from bool to void*.
+template <class P1, class P2, bool F(P1, P2)>
+void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
+ return F(p1, p2) ? p1 : UPB_BREAK;
+}
+
+template <class P1, class P2, class P3, bool F(P1, P2, P3)>
+void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
+ return F(p1, p2, p3) ? p1 : UPB_BREAK;
+}
+
+// For the string callback, which takes five params, returns the size param.
+template <class P1, class P2,
+ void F(P1, P2, const char *, size_t, const BufferHandle *)>
+size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
+ const BufferHandle *p5) {
+ F(p1, p2, p3, p4, p5);
+ return p4;
+}
+
+// For the string callback, which takes five params, returns the size param or
+// zero.
+template <class P1, class P2,
+ bool F(P1, P2, const char *, size_t, const BufferHandle *)>
+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;
+}
+
+// If we have a function returning void but want a function returning bool, wrap
+// it in a function that returns true.
+template <class P1, class P2, void F(P1, P2), class I>
+struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
+ typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
+};
+
+template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
+struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
+ typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
+};
+
+// If our function returns void but we want one returning void*, wrap it in a
+// function that returns the first argument.
+template <class P1, class P2, void F(P1, P2), class I>
+struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
+ typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
+};
+
+template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
+struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
+ typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
+};
+
+// If our function returns R* but we want one returning void*, wrap it in a
+// function that casts to void*.
+template <class R, class P1, class P2, R *F(P1, P2), class I>
+struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
+ typename disable_if_same<R *, void *>::Type> {
+ typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
+};
+
+template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
+struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
+ typename disable_if_same<R *, void *>::Type> {
+ typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
+ Func;
+};
+
+// 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 <class P1, class P2, bool F(P1, P2), class I>
+struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
+ typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
+};
+
+template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
+struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
+ typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, 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 <class P1, class P2,
+ void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
+struct MaybeWrapReturn<
+ Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
+ size_t> {
+ typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
+ ReturnStringLen<P1, P2, F>, I> Func;
+};
+
+// 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 <class P1, class P2,
+ bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
+struct MaybeWrapReturn<
+ Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
+ size_t> {
+ typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
+ ReturnNOr0<P1, P2, F>, I> Func;
+};
+
+// 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 <class F, class T>
+struct ConvertParams;
+
+// Function that discards the handler data parameter.
+template <class R, class P1, R F(P1)>
+R IgnoreHandlerData2(void *p1, const void *hd) {
+ UPB_UNUSED(hd);
+ return F(static_cast<P1>(p1));
+}
+
+template <class R, class P1, class P2Wrapper, class P2Wrapped,
+ R F(P1, P2Wrapped)>
+R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
+ UPB_UNUSED(hd);
+ return F(static_cast<P1>(p1), p2);
+}
+
+template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
+R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
+ UPB_UNUSED(hd);
+ return F(static_cast<P1>(p1), p2, p3);
+}
+
+template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
+R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
+ UPB_UNUSED(hd);
+ return F(static_cast<P1>(p1), p2, p3, p4);
+}
+
+template <class R, class P1, R F(P1, const char*, size_t)>
+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>(p1), p2, p3);
+}
+
+// Function that casts the handler data parameter.
+template <class R, class P1, class P2, R F(P1, P2)>
+R CastHandlerData2(void *c, const void *hd) {
+ return F(static_cast<P1>(c), static_cast<P2>(hd));
+}
+
+template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
+ R F(P1, P2, P3Wrapped)>
+R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
+ return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
+}
+
+template <class R, class P1, class P2, class P3, class P4, class P5,
+ R F(P1, P2, P3, P4, P5)>
+R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
+ return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
+}
+
+template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
+R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
+ size_t p4, const BufferHandle *handle) {
+ UPB_UNUSED(handle);
+ return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
+}
+
+// For unbound functions, ignore the handler data.
+template <class R, class P1, R F(P1), class I, class T>
+struct ConvertParams<Func1<R, P1, F, I>, T> {
+ typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
+};
+
+template <class R, class P1, class P2, R F(P1, P2), class I,
+ class R2, class P1_2, class P2_2, class P3_2>
+struct ConvertParams<Func2<R, P1, P2, F, I>,
+ R2 (*)(P1_2, P2_2, P3_2)> {
+ typedef Func3<R, void *, const void *, P3_2,
+ IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
+};
+
+// For StringBuffer only; this ignores both the handler data and the
+// BufferHandle.
+template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
+struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
+ typedef Func5<R, void *, const void *, const char *, size_t,
+ const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
+ I> Func;
+};
+
+template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
+ class I, class T>
+struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
+ typedef Func5<R, void *, const void *, P2, P3, P4,
+ IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
+};
+
+// For bound functions, cast the handler data.
+template <class R, class P1, class P2, R F(P1, P2), class I, class T>
+struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
+ typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
+ Func;
+};
+
+template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
+ class R2, class P1_2, class P2_2, class P3_2>
+struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
+ R2 (*)(P1_2, P2_2, P3_2)> {
+ typedef Func3<R, void *, const void *, P3_2,
+ CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
+};
+
+// For StringBuffer only; this ignores the BufferHandle.
+template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
+ class I, class T>
+struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
+ typedef Func5<R, void *, const void *, const char *, size_t,
+ const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
+ I> Func;
+};
+
+template <class R, class P1, class P2, class P3, class P4, class P5,
+ R F(P1, P2, P3, P4, P5), class I, class T>
+struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
+ typedef Func5<R, void *, const void *, P3, P4, P5,
+ CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
+};
+
+// 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<vtype> { \
+ typedef ctype Type; \
+ }; \
+ template <> \
+ inline bool Handlers::SetValueHandler<vtype>( \
+ const FieldDef *f, \
+ const Handlers::utype ## Handler& handler) { \
+ assert(!handler.registered_); \
+ handler.AddCleanup(this); \
+ handler.registered_ = true; \
+ return upb_handlers_set##ltype(this, f, 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);
+
+#ifdef UPB_TWO_32BIT_TYPES
+TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T);
+TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T);
+#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<Status*> {
+ typedef Status* Type;
+};
+
+// 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<ctype>(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
+
+template <class F> struct ReturnOf;
+
+template <class R, class P1, class P2>
+struct ReturnOf<R (*)(P1, P2)> {
+ typedef R Return;
+};
+
+template <class R, class P1, class P2, class P3>
+struct ReturnOf<R (*)(P1, P2, P3)> {
+ typedef R Return;
+};
+
+template <class R, class P1, class P2, class P3, class P4>
+struct ReturnOf<R (*)(P1, P2, P3, P4)> {
+ typedef R Return;
+};
+
+template <class R, class P1, class P2, class P3, class P4, class P5>
+struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
+ typedef R Return;
+};
+
+template<class T> const void *UniquePtrForType() {
+ static const char ch = 0;
+ return &ch;
+}
+
+template <class T>
+template <class F>
+inline Handler<T>::Handler(F func)
+ : registered_(false),
+ cleanup_data_(func.GetData()),
+ cleanup_func_(func.GetCleanup()) {
+ upb_handlerattr_sethandlerdata(&attr_, func.GetData());
+ typedef typename ReturnOf<T>::Return Return;
+ typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
+ typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
+ ReturnWrappedFunc;
+ handler_ = ReturnWrappedFunc().Call;
+
+ // Set attributes based on what templates can statically tell us about the
+ // user's function.
+
+ // If the original function returns void, then we know that we wrapped it to
+ // always return ok.
+ bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
+ attr_.SetAlwaysOk(always_ok);
+
+ // Closure parameter and return type.
+ attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
+
+ // We use the closure type (from the first parameter) if the return type is
+ // void. This is all nonsense for non START* handlers, but it doesn't matter
+ // because in that case the value will be ignored.
+ typedef typename FirstUnlessVoid<typename F::FuncInfo::Return,
+ typename F::FuncInfo::Closure>::value
+ EffectiveReturn;
+ attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
+}
+
+template <class T>
+inline Handler<T>::~Handler() {
+ assert(registered_);
+}
+
+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);
+}
+
+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 <class T>
+void BufferHandle::SetAttachedObject(const T* obj) {
+ upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
+}
+template <class T>
+const T* BufferHandle::GetAttachedObject() const {
+ return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
+ ? static_cast<const T *>(upb_bufhandle_obj(this))
+ : NULL;
+}
+
+inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
+ upb_handlers *h = upb_handlers_new(m, &h);
+ return reffed_ptr<Handlers>(h, &h);
+}
+inline reffed_ptr<const Handlers> 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<const Handlers>(h, &h);
+}
+inline bool Handlers::IsFrozen() const { return upb_handlers_isfrozen(this); }
+inline void Handlers::Ref(const void *owner) const {
+ upb_handlers_ref(this, owner);
+}
+inline void Handlers::Unref(const void *owner) const {
+ upb_handlers_unref(this, owner);
+}
+inline void Handlers::DonateRef(const void *from, const void *to) const {
+ upb_handlers_donateref(this, from, to);
+}
+inline void Handlers::CheckRef(const void *owner) const {
+ upb_handlers_checkref(this, owner);
+}
+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<Handlers*>& 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ 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() {
+ upb_byteshandler_uninit(this);
+}
+
+} // namespace upb
+
+#endif // __cplusplus
+
+
+#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
+
+#endif // UPB_HANDLERS_INL_H_
+
+#endif // UPB_HANDLERS_H
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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).
+ */
+
+#ifndef UPB_SINK_H
+#define UPB_SINK_H
+
+
+#ifdef __cplusplus
+namespace upb {
+class BufferSource;
+class BytesSink;
+class Sink;
+}
+#endif
+
+UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc);
+UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink);
+UPB_DECLARE_TYPE(upb::Sink, upb_sink);
+
+// Internal-only struct for the sink.
+struct upb_sinkframe {
+ UPB_PRIVATE_FOR_CPP
+ const upb_handlers *h;
+ void *closure;
+
+ // For any frames besides the top, this is the END* callback that will run
+ // when the subframe is popped (for example, for a "sequence" frame the frame
+ // above it will be a UPB_HANDLER_ENDSEQ handler). But this is only
+ // necessary for assertion checking inside upb_sink and can be omitted if the
+ // sink has only one caller.
+ //
+ // TODO(haberman): have a mechanism for ensuring that a sink only has one
+ // caller.
+ upb_selector_t selector;
+};
+
+// The maximum nesting depth that upb::Sink will allow. Matches proto2's limit.
+// TODO: make this a runtime-settable property of Sink.
+#define UPB_SINK_MAX_NESTING 64
+
+// 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?
+UPB_DEFINE_CLASS0(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 <class T> Sink(const Handlers* handlers, T* closure);
+
+ // Resets the value of the sink.
+ template <class T> void Reset(const Handlers* handlers, T* closure);
+
+ // 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 <class T> T* GetObject() const;
+
+ // 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).
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+
+ // 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.
+,
+UPB_DEFINE_STRUCT0(upb_sink,
+ const upb_handlers *handlers;
+ void *closure;
+));
+
+UPB_DEFINE_CLASS0(upb::BytesSink,
+ public:
+ BytesSink() {}
+
+ // 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 <class T> BytesSink(const BytesHandler* handler, T* closure);
+
+ // Resets the value of the sink.
+ template <class T> void Reset(const BytesHandler* handler, T* closure);
+
+ bool Start(size_t size_hint, void **subc);
+ size_t PutBuffer(void *subc, const char *buf, size_t len,
+ const BufferHandle *handle);
+ bool End();
+,
+UPB_DEFINE_STRUCT0(upb_bytessink,
+ const upb_byteshandler *handler;
+ void *closure;
+));
+
+// 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.
+UPB_DEFINE_CLASS0(upb::BufferSource,
+ public:
+ BufferSource();
+ BufferSource(const char* buf, size_t len, BytesSink* sink);
+
+ // 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();
+
+ // 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);
+
+ template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
+ return PutBuffer(str.c_str(), str.size(), sink);
+ }
+,
+UPB_DEFINE_STRUCT0(upb_bufsrc,
+));
+
+UPB_BEGIN_EXTERN_C // {
+
+// Inline definitions.
+
+UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
+ void *closure) {
+ s->handler = h;
+ s->closure = closure;
+}
+
+UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
+ void **subc) {
+ *subc = s->closure;
+ if (!s->handler) return true;
+ upb_startstr_handlerfunc *start =
+ (upb_startstr_handlerfunc *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
+
+ if (!start) return true;
+ *subc = start(s->closure, upb_handlerattr_handlerdata(
+ &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
+ size_hint);
+ return *subc != NULL;
+}
+
+UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
+ const char *buf, size_t size,
+ const upb_bufhandle* handle) {
+ if (!s->handler) return true;
+ upb_string_handlerfunc *putbuf =
+ (upb_string_handlerfunc *)s->handler->table[UPB_STRING_SELECTOR].func;
+
+ if (!putbuf) return true;
+ return putbuf(subc, upb_handlerattr_handlerdata(
+ &s->handler->table[UPB_STRING_SELECTOR].attr),
+ buf, size, handle);
+}
+
+UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
+ if (!s->handler) return true;
+ upb_endfield_handlerfunc *end =
+ (upb_endfield_handlerfunc *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
+
+ if (!end) return true;
+ return end(s->closure,
+ upb_handlerattr_handlerdata(
+ &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
+}
+
+UPB_INLINE bool upb_bufsrc_putbuf(const char *buf, size_t len,
+ upb_bytessink *sink) {
+ void *subc;
+ upb_bufhandle handle;
+ upb_bufhandle_init(&handle);
+ upb_bufhandle_setbuf(&handle, buf, 0);
+ bool 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;
+}
+
+#define PUTVAL(type, ctype) \
+ UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \
+ ctype val) { \
+ if (!s->handlers) return true; \
+ upb_##type##_handlerfunc *func = \
+ (upb_##type##_handlerfunc *)upb_handlers_gethandler(s->handlers, sel); \
+ if (!func) return true; \
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel); \
+ return func(s->closure, hd, val); \
+ }
+
+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
+
+UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
+ s->handlers = h;
+ s->closure = c;
+}
+
+UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
+ const char *buf, size_t n,
+ const upb_bufhandle *handle) {
+ if (!s->handlers) return n;
+ upb_string_handlerfunc *handler =
+ (upb_string_handlerfunc *)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!handler) return n;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ return handler(s->closure, hd, buf, n, handle);
+}
+
+UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
+ if (!s->handlers) return true;
+ upb_startmsg_handlerfunc *startmsg =
+ (upb_startmsg_handlerfunc *)upb_handlers_gethandler(s->handlers,
+ UPB_STARTMSG_SELECTOR);
+ if (!startmsg) return true;
+ const void *hd =
+ upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
+ return startmsg(s->closure, hd);
+}
+
+UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
+ if (!s->handlers) return true;
+ upb_endmsg_handlerfunc *endmsg =
+ (upb_endmsg_handlerfunc *)upb_handlers_gethandler(s->handlers,
+ UPB_ENDMSG_SELECTOR);
+
+ if (!endmsg) return true;
+ const void *hd =
+ upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
+ return endmsg(s->closure, hd, status);
+}
+
+UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
+ upb_sink *sub) {
+ sub->closure = s->closure;
+ sub->handlers = s->handlers;
+ if (!s->handlers) return true;
+ upb_startfield_handlerfunc *startseq =
+ (upb_startfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!startseq) return true;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ sub->closure = startseq(s->closure, hd);
+ return sub->closure ? true : false;
+}
+
+UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
+ if (!s->handlers) return true;
+ upb_endfield_handlerfunc *endseq =
+ (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!endseq) return true;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ return endseq(s->closure, hd);
+}
+
+UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
+ size_t size_hint, upb_sink *sub) {
+ sub->closure = s->closure;
+ sub->handlers = s->handlers;
+ if (!s->handlers) return true;
+ upb_startstr_handlerfunc *startstr =
+ (upb_startstr_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!startstr) return true;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ sub->closure = startstr(s->closure, hd, size_hint);
+ return sub->closure ? true : false;
+}
+
+UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
+ if (!s->handlers) return true;
+ upb_endfield_handlerfunc *endstr =
+ (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!endstr) return true;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ return endstr(s->closure, hd);
+}
+
+UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
+ upb_sink *sub) {
+ sub->closure = s->closure;
+ if (!s->handlers) {
+ sub->handlers = NULL;
+ return true;
+ }
+ sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
+ upb_startfield_handlerfunc *startsubmsg =
+ (upb_startfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!startsubmsg) return true;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ sub->closure = startsubmsg(s->closure, hd);
+ return sub->closure ? true : false;
+}
+
+UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
+ if (!s->handlers) return true;
+ upb_endfield_handlerfunc *endsubmsg =
+ (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
+
+ if (!endsubmsg) return s->closure;
+ const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
+ return endsubmsg(s->closure, hd);
+}
+
+UPB_END_EXTERN_C // }
+
+#ifdef __cplusplus
+
+namespace upb {
+
+template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
+ upb_sink_reset(this, handlers, closure);
+}
+template <class T>
+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);
+}
+
+template <class T>
+BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
+ Reset(handler, closure);
+}
+
+template <class T>
+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);
+}
+
+inline bool BufferSource::PutBuffer(const char *buf, size_t len,
+ BytesSink *sink) {
+ return upb_bufsrc_putbuf(buf, len, sink);
+}
+
+} // namespace upb
+#endif
+
+#endif
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2013 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * For handlers that do very tiny, very simple operations, the function call
+ * overhead of calling a handler can be significant. This file allows the
+ * user to define handlers that do something very simple like store the value
+ * to memory and/or set a hasbit. JIT compilers can then special-case these
+ * handlers and emit specialized code for them instead of actually calling the
+ * handler.
+ *
+ * The functionality is very simple/limited right now but may expand to be able
+ * to call another function.
+ */
+
+#ifndef UPB_SHIM_H
+#define UPB_SHIM_H
+
+
+typedef struct {
+ size_t offset;
+ int32_t hasbit;
+} upb_shim_data;
+
+#ifdef __cplusplus
+
+namespace upb {
+
+struct Shim {
+ typedef upb_shim_data Data;
+
+ // Sets a handler for the given field that writes the value to the given
+ // offset and, if hasbit >= 0, sets a bit at the given bit offset. Returns
+ // true if the handler was set successfully.
+ static bool Set(Handlers *h, const FieldDef *f, size_t ofs, int32_t hasbit);
+
+ // If this handler is a shim, returns the corresponding upb::Shim::Data and
+ // stores the type in "type". Otherwise returns NULL.
+ static const Data* GetData(const Handlers* h, Handlers::Selector s,
+ FieldDef::Type* type);
+};
+
+} // namespace upb
+
+#endif
+
+UPB_BEGIN_EXTERN_C // {
+
+// C API.
+bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset,
+ int32_t hasbit);
+const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s,
+ upb_fieldtype_t *type);
+
+UPB_END_EXTERN_C // }
+
+#ifdef __cplusplus
+// C++ Wrappers.
+namespace upb {
+inline bool Shim::Set(Handlers* h, const FieldDef* f, size_t ofs,
+ int32_t hasbit) {
+ return upb_shim_set(h, f, ofs, hasbit);
+}
+inline const Shim::Data* Shim::GetData(const Handlers* h, Handlers::Selector s,
+ FieldDef::Type* type) {
+ return upb_shim_getdata(h, s, type);
+}
+} // namespace upb
+#endif
+
+#endif // UPB_SHIM_H
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * upb::descriptor::Reader provides a way of building upb::Defs from
+ * data in descriptor.proto format.
+ */
+
+#ifndef UPB_DESCRIPTOR_H
+#define UPB_DESCRIPTOR_H
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace descriptor {
+class Reader;
+} // namespace descriptor
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader);
+
+// Internal-only structs used by Reader.
+
+// upb_deflist is an internal-only dynamic array for storing a growing list of
+// upb_defs.
+typedef struct {
+ UPB_PRIVATE_FOR_CPP
+ upb_def **defs;
+ size_t len;
+ size_t size;
+ bool owned;
+} upb_deflist;
+
+// 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 {
+ UPB_PRIVATE_FOR_CPP
+ char *name;
+ // Index of the first def that is under this scope. For msgdefs, the
+ // msgdef itself is at start-1.
+ int start;
+} 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
+
+// Class that receives descriptor data according to the descriptor.proto schema
+// and use it to build upb::Defs corresponding to that schema.
+UPB_DEFINE_CLASS0(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.
+ Reader(const Handlers* handlers, Status* status);
+ ~Reader();
+
+ // Resets the reader's state and discards any defs it may have built.
+ void Reset();
+
+ // The reader's input; this is where descriptor.proto data should be sent.
+ Sink* input();
+
+ // Returns an array of all defs that have been parsed, and transfers ownership
+ // of them to "owner". The number of defs is stored in *n. Ownership of the
+ // returned array is retained and is invalidated by any other call into
+ // Reader.
+ //
+ // These defs are not frozen or resolved; they are ready to be added to a
+ // symtab.
+ upb::Def** GetDefs(void* owner, int* n);
+
+ // Builds and returns handlers for the reader, owned by "owner."
+ static Handlers* NewHandlers(const void* owner);
+,
+UPB_DEFINE_STRUCT0(upb_descreader,
+ upb_sink sink;
+ upb_deflist defs;
+ upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING];
+ int stack_len;
+
+ uint32_t number;
+ char *name;
+ bool saw_number;
+ bool saw_name;
+
+ char *default_string;
+
+ upb_fielddef *f;
+));
+
+UPB_BEGIN_EXTERN_C // {
+
+// C API.
+void upb_descreader_init(upb_descreader *r, const upb_handlers *handlers,
+ upb_status *status);
+void upb_descreader_uninit(upb_descreader *r);
+void upb_descreader_reset(upb_descreader *r);
+upb_sink *upb_descreader_input(upb_descreader *r);
+upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n);
+const upb_handlers *upb_descreader_newhandlers(const void *owner);
+
+UPB_END_EXTERN_C // }
+
+#ifdef __cplusplus
+// C++ implementation details. /////////////////////////////////////////////////
+namespace upb {
+namespace descriptor {
+inline Reader::Reader(const Handlers *h, Status *s) {
+ upb_descreader_init(this, h, s);
+}
+inline Reader::~Reader() { upb_descreader_uninit(this); }
+inline void Reader::Reset() { upb_descreader_reset(this); }
+inline Sink* Reader::input() { return upb_descreader_input(this); }
+inline upb::Def** Reader::GetDefs(void* owner, int* n) {
+ return upb_descreader_getdefs(this, owner, n);
+}
+} // namespace descriptor
+} // namespace upb
+#endif
+
+#endif // UPB_DESCRIPTOR_H
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Internal-only definitions for the decoder.
+ */
+
+#ifndef UPB_DECODER_INT_H_
+#define UPB_DECODER_INT_H_
+
+#include <stdlib.h>
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * upb::pb::Decoder implements a high performance, streaming, resumable decoder
+ * for the binary protobuf format.
+ *
+ * This interface works the same regardless of what decoder backend is being
+ * used. A client of this class does not need to know whether decoding is using
+ * a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
+ * it will always use the fastest available decoder. However, you can call
+ * set_allow_jit(false) to disable any JIT decoder that might be available.
+ * This is primarily useful for testing purposes.
+ */
+
+#ifndef UPB_DECODER_H_
+#define UPB_DECODER_H_
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace pb {
+class CodeCache;
+class Decoder;
+class DecoderMethod;
+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::DecoderMethod, upb_pbdecodermethod);
+UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts);
+
+// 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
+// setting it high will cause the upb::pb::Decoder object to be larger.
+//
+// If necessary we can add a runtime-settable property to Decoder that allow
+// this to be larger than the compile-time setting, but this would add
+// complexity, particularly since we would have to decide how/if to give users
+// the ability to set a custom memory allocation function.
+#define UPB_DECODER_MAX_NESTING 64
+
+// Internal-only struct used by the decoder.
+typedef struct {
+ UPB_PRIVATE_FOR_CPP
+ // Space optimization note: we store two pointers here that the JIT
+ // doesn't need at all; the upb_handlers* inside the sink and
+ // the dispatch table pointer. We can optimze so that the JIT uses
+ // smaller stack frames than the interpreter. The only thing we need
+ // to guarantee is that the fallback routines can find end_ofs.
+ upb_sink sink;
+
+ // The absolute stream offset of the end-of-frame delimiter.
+ // Non-delimited frames (groups and non-packed repeated fields) reuse the
+ // delimiter of their parent, even though the frame may not end there.
+ //
+ // NOTE: the JIT stores a slightly different value here for non-top frames.
+ // It stores the value relative to the end of the enclosed message. But the
+ // top frame is still stored the same way, which is important for ensuring
+ // that calls from the JIT into C work correctly.
+ uint64_t end_ofs;
+ const uint32_t *base;
+
+ // 0 indicates a length-delimited field.
+ // A positive number indicates a known group.
+ // A negative number indicates an unknown group.
+ int32_t groupnum;
+ upb_inttable *dispatch; // Not used by the JIT.
+} upb_pbdecoder_frame;
+
+// The parameters one uses to construct a DecoderMethod.
+// TODO(haberman): move allowjit here? Seems more convenient for users.
+UPB_DEFINE_CLASS0(upb::pb::DecoderMethodOptions,
+ public:
+ // Parameter represents the destination handlers that this method will push
+ // to.
+ explicit DecoderMethodOptions(const Handlers* dest_handlers);
+
+ // 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_DEFINE_STRUCT0(upb_pbdecodermethodopts,
+ const upb_handlers *handlers;
+ bool lazy;
+));
+
+// Represents the code to parse a protobuf according to a destination Handlers.
+UPB_DEFINE_CLASS1(upb::pb::DecoderMethod, upb::RefCounted,
+ public:
+ // From upb::ReferenceCounted.
+ void Ref(const void* owner) const;
+ void Unref(const void* owner) const;
+ void DonateRef(const void* from, const void* to) const;
+ void CheckRef(const void* owner) const;
+
+ // 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;
+
+ // The input handlers for this decoder method.
+ const BytesHandler* input_handler() const;
+
+ // Whether this method is native.
+ bool is_native() const;
+
+ // Convenience method for generating a DecoderMethod without explicitly
+ // creating a CodeCache.
+ static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
+
+ private:
+ UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod);
+,
+UPB_DEFINE_STRUCT(upb_pbdecodermethod, upb_refcounted,
+ // While compiling, the base is relative in "ofs", after compiling it is
+ // absolute in "ptr".
+ union {
+ uint32_t ofs; // PC offset of method.
+ 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;
+
+ // Whether this method is native code or bytecode.
+ bool is_native_;
+
+ // The handler one calls to invoke this method.
+ upb_byteshandler input_handler_;
+
+ // The destination handlers this method is bound to. We own a ref.
+ const upb_handlers *dest_handlers_;
+
+ // Dispatch table -- used by both bytecode decoder and JIT when encountering a
+ // field number that wasn't the one we were expecting to see. See
+ // decoder.int.h for the layout of this table.
+ upb_inttable dispatch;
+));
+
+// A Decoder receives binary protobuf data on its input sink and pushes the
+// decoded data to its output sink.
+UPB_DEFINE_CLASS0(upb::pb::Decoder,
+ public:
+ // 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.
+ Decoder(const DecoderMethod* method, Status* status);
+ ~Decoder();
+
+ // Returns the DecoderMethod this decoder is parsing from.
+ // TODO(haberman): Do users need to be able to rebind this?
+ const DecoderMethod* method() const;
+
+ // Resets the state of the decoder.
+ void Reset();
+
+ // Returns number of bytes successfully parsed.
+ //
+ // This can be useful for determining the stream position where an error
+ // occurred.
+ //
+ // This value may not be up-to-date when called from inside a parsing
+ // callback.
+ uint64_t BytesParsed() const;
+
+ // Resets the output sink of the Decoder.
+ // The given sink must match method()->dest_handlers().
+ //
+ // This must be called at least once before the decoder can be used. It may
+ // only be called with the decoder is in a state where it was just created or
+ // reset with pipeline.Reset(). The given sink must be from the same pipeline
+ // as this decoder.
+ bool ResetOutput(Sink* sink);
+
+ // The sink on which this decoder receives input.
+ BytesSink* input();
+
+ private:
+ UPB_DISALLOW_COPY_AND_ASSIGN(Decoder);
+,
+UPB_DEFINE_STRUCT0(upb_pbdecoder, UPB_QUOTE(
+ // Our input sink.
+ upb_bytessink input_;
+
+ // The decoder method we are parsing with (owned).
+ const upb_pbdecodermethod *method_;
+
+ size_t call_len;
+ const uint32_t *pc, *last;
+
+ // Current input buffer and its stream offset.
+ const char *buf, *ptr, *end, *checkpoint;
+
+ // End of the delimited region, relative to ptr, or NULL if not in this buf.
+ const char *delim_end;
+
+ // End of the delimited region, relative to ptr, or end if not in this buf.
+ const char *data_end;
+
+ // Overall stream offset of "buf."
+ uint64_t bufstart_ofs;
+
+ // Buffer for residual bytes not parsed from the previous buffer.
+ // The maximum number of residual bytes we require is 12; a five-byte
+ // unknown tag plus an eight-byte value, less one because the value
+ // is only a partial value.
+ char residual[12];
+ char *residual_end;
+
+ // Stores the user buffer passed to our decode function.
+ const char *buf_param;
+ size_t size_param;
+ const upb_bufhandle *handle;
+
+#ifdef UPB_USE_JIT_X64
+ // Used momentarily by the generated code to store a value while a user
+ // function is called.
+ uint32_t tmp_len;
+
+ const void *saved_rsp;
+#endif
+
+ upb_status *status;
+
+ // Our internal stack.
+ upb_pbdecoder_frame *top, *limit;
+ upb_pbdecoder_frame stack[UPB_DECODER_MAX_NESTING];
+#ifdef UPB_USE_JIT_X64
+ // Each native stack frame needs two pointers, plus we need a few frames for
+ // the enter/exit trampolines.
+ const uint32_t *callstack[(UPB_DECODER_MAX_NESTING * 2) + 10];
+#else
+ const uint32_t *callstack[UPB_DECODER_MAX_NESTING];
+#endif
+)));
+
+// 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.
+UPB_DEFINE_CLASS0(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);
+
+ // 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);
+
+ // If/when someone needs to explicitly create a dynamically-bound
+ // DecoderMethod*, we can add a method to get it here.
+
+ private:
+ UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache);
+,
+UPB_DEFINE_STRUCT0(upb_pbcodecache,
+ bool allow_jit_;
+
+ // Array of mgroups.
+ upb_inttable groups;
+));
+
+UPB_BEGIN_EXTERN_C // {
+
+void upb_pbdecoder_init(upb_pbdecoder *d, const upb_pbdecodermethod *method,
+ upb_status *status);
+void upb_pbdecoder_uninit(upb_pbdecoder *d);
+void upb_pbdecoder_reset(upb_pbdecoder *d);
+const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
+bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink *sink);
+upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
+uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
+
+void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
+ const upb_handlers *h);
+void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
+
+void upb_pbdecodermethod_ref(const upb_pbdecodermethod *m, const void *owner);
+void upb_pbdecodermethod_unref(const upb_pbdecodermethod *m, const void *owner);
+void upb_pbdecodermethod_donateref(const upb_pbdecodermethod *m,
+ const void *from, const void *to);
+void upb_pbdecodermethod_checkref(const upb_pbdecodermethod *m,
+ const void *owner);
+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);
+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 // }
+
+#ifdef __cplusplus
+
+namespace upb {
+
+namespace pb {
+
+inline Decoder::Decoder(const DecoderMethod* m, Status* s) {
+ upb_pbdecoder_init(this, m, s);
+}
+inline Decoder::~Decoder() {
+ upb_pbdecoder_uninit(this);
+}
+inline const DecoderMethod* Decoder::method() const {
+ return upb_pbdecoder_method(this);
+}
+inline void Decoder::Reset() {
+ upb_pbdecoder_reset(this);
+}
+inline uint64_t Decoder::BytesParsed() const {
+ return upb_pbdecoder_bytesparsed(this);
+}
+inline bool Decoder::ResetOutput(Sink* sink) {
+ return upb_pbdecoder_resetoutput(this, sink);
+}
+inline BytesSink* Decoder::input() {
+ return upb_pbdecoder_input(this);
+}
+
+inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
+ upb_pbdecodermethodopts_init(this, h);
+}
+inline void DecoderMethodOptions::set_lazy(bool lazy) {
+ upb_pbdecodermethodopts_setlazy(this, lazy);
+}
+
+inline void DecoderMethod::Ref(const void *owner) const {
+ upb_pbdecodermethod_ref(this, owner);
+}
+inline void DecoderMethod::Unref(const void *owner) const {
+ upb_pbdecodermethod_unref(this, owner);
+}
+inline void DecoderMethod::DonateRef(const void *from, const void *to) const {
+ upb_pbdecodermethod_donateref(this, from, to);
+}
+inline void DecoderMethod::CheckRef(const void *owner) const {
+ upb_pbdecodermethod_checkref(this, owner);
+}
+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<const DecoderMethod> DecoderMethod::New(
+ const DecoderMethodOptions &opts) {
+ const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
+ return reffed_ptr<const DecoderMethod>(m, &m);
+}
+
+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);
+}
+
+} // namespace pb
+} // namespace upb
+
+#endif // __cplusplus
+
+#endif /* UPB_DECODER_H_ */
+
+// Opcode definitions. The canonical meaning of each opcode is its
+// implementation in the interpreter (the JIT is written to match this).
+//
+// All instructions have the opcode in the low byte.
+// Instruction format for most instructions is:
+//
+// +-------------------+--------+
+// | arg (24) | op (8) |
+// +-------------------+--------+
+//
+// Exceptions are indicated below. A few opcodes are multi-word.
+typedef enum {
+ // Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
+ // Arg for all of these is the upb selector for this field.
+#define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
+ 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),
+#undef T
+ OP_STARTMSG = 9, // No arg.
+ OP_ENDMSG = 10, // No arg.
+ OP_STARTSEQ = 11,
+ OP_ENDSEQ = 12,
+ OP_STARTSUBMSG = 14,
+ OP_ENDSUBMSG = 19,
+ OP_STARTSTR = 20,
+ OP_STRING = 21,
+ OP_ENDSTR = 22,
+
+ OP_PUSHTAGDELIM = 23, // No arg.
+ OP_PUSHLENDELIM = 24, // No arg.
+ OP_POP = 25, // No arg.
+ OP_SETDELIM = 26, // No arg.
+ OP_SETBIGGROUPNUM = 27, // two words: | unused (24) | opc || groupnum (32) |
+ OP_CHECKDELIM = 28,
+ OP_CALL = 29,
+ OP_RET = 30,
+ OP_BRANCH = 31,
+
+ // Different opcodes depending on how many bytes expected.
+ OP_TAG1 = 32, // | expected tag (16) | jump target (8) | opc (8) |
+ OP_TAG2 = 33, // | expected tag (16) | jump target (8) | opc (8) |
+ OP_TAGN = 34, // three words:
+ // | unused (16) | jump target(8) | opc (8) |
+ // | expected tag 1 (32) |
+ // | expected tag 2 (32) |
+
+ OP_SETDISPATCH = 35, // N words:
+ // | unused (24) | opc |
+ // | upb_inttable* (32 or 64) |
+
+ OP_HALT = 36, // No arg.
+} opcode;
+
+#define OP_MAX OP_HALT
+
+UPB_INLINE opcode getop(uint32_t instr) { return 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
+// +----------+
+typedef struct {
+ upb_refcounted base;
+
+ // Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the
+ // methods.
+ upb_inttable methods;
+
+ // 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.
+
+ // The bytecode for our methods, if any exists. Owned by us.
+ uint32_t *bytecode;
+ uint32_t *bytecode_end;
+
+#ifdef UPB_USE_JIT_X64
+ // JIT-generated machine code, if any.
+ upb_string_handlerfunc *jit_code;
+ // The size of the jit_code (required to munmap()).
+ size_t jit_size;
+ char *debug_info;
+ void *dl;
+#endif
+} mgroup;
+
+// Decoder entry points; used as handlers.
+void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
+void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
+size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
+ size_t size, const upb_bufhandle *handle);
+bool upb_pbdecoder_end(void *closure, const void *handler_data);
+
+// Decoder-internal functions that the JIT calls to handle fallback paths.
+int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
+ size_t size, const upb_bufhandle *handle);
+size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
+int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
+ uint8_t wire_type);
+int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
+int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
+int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
+int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
+void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
+
+// Error messages that are shared between the bytecode and JIT decoders.
+extern const char *kPbDecoderStackOverflow;
+
+// Access to decoderplan members needed by the decoder.
+const char *upb_pbdecoder_getopname(unsigned int op);
+
+// JIT codegen entry point.
+void upb_pbdecoder_jit(mgroup *group);
+void upb_pbdecoder_freejit(mgroup *group);
+
+// A special label that means "do field dispatch for this message and branch to
+// wherever that takes you."
+#define LABEL_DISPATCH 0
+
+// A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
+// RET) for branching to when we find an appropriate ENDGROUP tag.
+#define DISPATCH_ENDMSG 0
+
+// It's important to use this invalid wire type instead of 0 (which is a valid
+// wire type).
+#define NO_WIRE_TYPE 0xff
+
+// The dispatch table layout is:
+// [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
+//
+// If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
+// (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
+//
+// We need two wire types because of packed/non-packed compatibility. A
+// primitive repeated field can use either wire type and be valid. While we
+// could key the table on fieldnum+wiretype, the table would be 8x sparser.
+//
+// Storing two wire types in the primary value allows us to quickly rule out
+// the second wire type without needing to do a separate lookup (this case is
+// less common than an unknown field).
+UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
+ uint8_t wt2) {
+ return (ofs << 16) | (wt2 << 8) | wt1;
+}
+
+UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
+ uint8_t *wt1, uint8_t *wt2) {
+ *wt1 = (uint8_t)dispatch;
+ *wt2 = (uint8_t)(dispatch >> 8);
+ *ofs = dispatch >> 16;
+}
+
+// All of the functions in decoder.c that return int32_t return values according
+// to the following scheme:
+// 1. negative values indicate a return code from the following list.
+// 2. positive values indicate that error or end of buffer was hit, and
+// that the decode function should immediately return the given value
+// (the decoder state has already been suspended and is ready to be
+// resumed).
+#define DECODE_OK -1
+#define DECODE_MISMATCH -2 // Used only from checktag_slow().
+#define DECODE_ENDGROUP -3 // Used only from checkunknown().
+
+#define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
+
+#endif // UPB_DECODER_INT_H_
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * A number of routines for varint manipulation (we keep them all around to
+ * have multiple approaches available for benchmarking).
+ */
+
+#ifndef UPB_VARINT_DECODER_H_
+#define UPB_VARINT_DECODER_H_
+
+#include <assert.h>
+#include <stdint.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#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;
+
+#define UPB_MAX_WIRE_TYPE 5
+
+// The maximum number of bytes that it takes to encode a 64-bit varint.
+// Note that with a better encoding this could be 9 (TODO: write up a
+// wiki document about this).
+#define UPB_PB_VARINT_MAX_LEN 10
+
+// Array of the "native" (ie. non-packed-repeated) wire type for the given a
+// descriptor type (upb_descriptortype_t).
+extern const uint8_t upb_pb_native_wire_types[];
+
+/* Zig-zag encoding/decoding **************************************************/
+
+UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
+ return (n >> 1) ^ -(int32_t)(n & 1);
+}
+UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
+ return (n >> 1) ^ -(int64_t)(n & 1);
+}
+UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
+UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
+
+/* Decoding *******************************************************************/
+
+// All decoding functions return this struct by value.
+typedef struct {
+ const char *p; // NULL if the varint was unterminated.
+ uint64_t val;
+} upb_decoderet;
+
+// Four functions for decoding a varint of at most eight bytes. They are all
+// functionally identical, but are implemented in different ways and likely have
+// different performance profiles. We keep them around for performance testing.
+//
+// Note that these functions may not read byte-by-byte, so they must not be used
+// unless there are at least eight bytes left in the buffer!
+upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
+upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
+upb_decoderet upb_vdecode_max8_wright(upb_decoderet r);
+upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r);
+
+// Template for a function that checks the first two bytes with branching
+// and dispatches 2-10 bytes with a separate function. Note that this may read
+// up to 10 bytes, so it must not be used unless there are at least ten bytes
+// left in the buffer!
+#define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
+UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
+ uint8_t *p = (uint8_t*)_p; \
+ if ((*p & 0x80) == 0) { upb_decoderet r = {_p + 1, *p & 0x7fU}; return r; } \
+ upb_decoderet r = {_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)}; \
+ if ((*(p + 1) & 0x80) == 0) return r; \
+ return decode_max8_function(r); \
+}
+
+UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32);
+UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64);
+UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright);
+UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino);
+#undef UPB_VARINT_DECODER_CHECK2
+
+// Our canonical functions for decoding varints, based on the currently
+// favored best-performing implementations.
+UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
+ if (sizeof(long) == 8)
+ return upb_vdecode_check2_branch64(p);
+ else
+ return upb_vdecode_check2_branch32(p);
+}
+
+UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) {
+ return upb_vdecode_max8_massimino(r);
+}
+
+
+/* Encoding *******************************************************************/
+
+UPB_INLINE int upb_value_size(uint64_t val) {
+#ifdef __GNUC__
+ int high_bit = 63 - __builtin_clzll(val); // 0-based, undef if val == 0.
+#else
+ int high_bit = 0;
+ uint64_t tmp = val;
+ while(tmp >>= 1) high_bit++;
+#endif
+ return val == 0 ? 1 : high_bit / 8 + 1;
+}
+
+// Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
+// bytes long), returning how many bytes were used.
+//
+// TODO: benchmark and optimize if necessary.
+UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
+ if (val == 0) { buf[0] = 0; return 1; }
+ size_t i = 0;
+ while (val) {
+ uint8_t byte = val & 0x7fU;
+ val >>= 7;
+ if (val) byte |= 0x80U;
+ buf[i++] = byte;
+ }
+ return i;
+}
+
+UPB_INLINE size_t upb_varint_size(uint64_t val) {
+ char buf[UPB_PB_VARINT_MAX_LEN];
+ return upb_vencode64(val, buf);
+}
+
+// Encodes a 32-bit varint, *not* sign-extended.
+UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
+ char buf[UPB_PB_VARINT_MAX_LEN];
+ size_t bytes = upb_vencode64(val, buf);
+ uint64_t ret = 0;
+ assert(bytes <= 5);
+ memcpy(&ret, buf, bytes);
+ assert(ret <= 0xffffffffffU);
+ return ret;
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UPB_VARINT_DECODER_H_ */
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009-2010 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * Implements a set of upb_handlers that write protobuf data to the binary wire
+ * format.
+ *
+ * This encoder implementation does not have any access to any out-of-band or
+ * precomputed lengths for submessages, so it must buffer submessages internally
+ * before it can emit the first byte.
+ */
+
+#ifndef UPB_ENCODER_H_
+#define UPB_ENCODER_H_
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace pb {
+class Encoder;
+} // namespace pb
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder);
+
+#define UPB_PBENCODER_MAX_NESTING 100
+
+/* upb::pb::Encoder ***********************************************************/
+
+// 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 {
+ UPB_PRIVATE_FOR_CPP
+ uint32_t msglen; // The length to varint-encode before this segment.
+ uint32_t seglen; // Length of the segment.
+} upb_pb_encoder_segment;
+
+UPB_DEFINE_CLASS0(upb::pb::Encoder,
+ public:
+ Encoder(const upb::Handlers* handlers);
+ ~Encoder();
+
+ static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* msg);
+
+ // Resets the state of the printer, so that it will expect to begin a new
+ // document.
+ void Reset();
+
+ // Resets the output pointer which will serve as our closure.
+ void ResetOutput(BytesSink* output);
+
+ // The input to the encoder.
+ Sink* input();
+
+ private:
+ UPB_DISALLOW_COPY_AND_ASSIGN(Encoder);
+,
+UPB_DEFINE_STRUCT0(upb_pb_encoder, UPB_QUOTE(
+ // 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;
+
+ // The stack of enclosing submessages. Each entry in the stack points to the
+ // segment where this submessage's length is being accumulated.
+ int stack[UPB_PBENCODER_MAX_NESTING], *top, *stacklimit;
+
+ // Depth of startmsg/endmsg calls.
+ int depth;
+
+ // Initial buffers for the output buffer and segment buffer. If we outgrow
+ // these we will dynamically allocate bigger ones.
+ char initbuf[256];
+ upb_pb_encoder_segment seginitbuf[32];
+)));
+
+UPB_BEGIN_EXTERN_C
+
+const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
+ const void *owner);
+void upb_pb_encoder_reset(upb_pb_encoder *e);
+upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
+void upb_pb_encoder_init(upb_pb_encoder *e, const upb_handlers *h);
+void upb_pb_encoder_resetoutput(upb_pb_encoder *e, upb_bytessink *output);
+void upb_pb_encoder_uninit(upb_pb_encoder *e);
+
+UPB_END_EXTERN_C
+
+#ifdef __cplusplus
+
+namespace upb {
+namespace pb {
+inline Encoder::Encoder(const upb::Handlers* handlers) {
+ upb_pb_encoder_init(this, handlers);
+}
+inline Encoder::~Encoder() {
+ upb_pb_encoder_uninit(this);
+}
+inline void Encoder::Reset() {
+ upb_pb_encoder_reset(this);
+}
+inline void Encoder::ResetOutput(BytesSink* output) {
+ upb_pb_encoder_resetoutput(this, output);
+}
+inline Sink* Encoder::input() {
+ return upb_pb_encoder_input(this);
+}
+inline reffed_ptr<const Handlers> Encoder::NewHandlers(
+ const upb::MessageDef *md) {
+ const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
+ return reffed_ptr<const Handlers>(h, &h);
+}
+} // namespace pb
+} // namespace upb
+
+#endif
+
+#endif /* UPB_ENCODER_H_ */
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * 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
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Loads all defs from the given protobuf binary descriptor, setting default
+// accessors and a default layout on all messages. The caller owns the
+// returned array of defs, which will be of length *n. On error NULL is
+// returned and status is set (if non-NULL).
+upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n,
+ void *owner, upb_status *status);
+
+// Like the previous but also adds the loaded defs to the given symtab.
+bool upb_load_descriptor_into_symtab(upb_symtab *symtab, const char *str,
+ size_t len, upb_status *status);
+
+// Like the previous but also reads the descriptor from the given filename.
+bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname,
+ upb_status *status);
+
+// Reads the given filename into a character string, returning NULL if there
+// was an error.
+char *upb_readfile(const char *filename, size_t *len);
+
+#ifdef __cplusplus
+} /* extern "C" */
+
+namespace upb {
+
+// All routines that load descriptors expect the descriptor to be a
+// FileDescriptorSet.
+inline bool LoadDescriptorFileIntoSymtab(SymbolTable* s, const char *fname,
+ Status* status) {
+ return upb_load_descriptor_file_into_symtab(s, fname, status);
+}
+
+inline bool LoadDescriptorIntoSymtab(SymbolTable* s, const char* str,
+ size_t len, Status* status) {
+ return upb_load_descriptor_into_symtab(s, str, len, status);
+}
+
+// Templated so it can accept both string and std::string.
+template <typename T>
+bool LoadDescriptorIntoSymtab(SymbolTable* s, const T& desc, Status* status) {
+ return upb_load_descriptor_into_symtab(s, desc.c_str(), desc.size(), status);
+}
+
+} // namespace upb
+
+#endif
+
+#endif
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2009 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ */
+
+#ifndef UPB_TEXT_H_
+#define UPB_TEXT_H_
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace pb {
+class TextPrinter;
+} // namespace pb
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter);
+
+UPB_DEFINE_CLASS0(upb::pb::TextPrinter,
+ public:
+ // The given handlers must have come from NewHandlers(). It must outlive the
+ // TextPrinter.
+ explicit TextPrinter(const upb::Handlers* handlers);
+
+ void SetSingleLineMode(bool single_line);
+
+ bool ResetOutput(BytesSink* output);
+ Sink* input();
+
+ // If handler caching becomes a requirement we can add a code cache as in
+ // decoder.h
+ static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
+
+ private:
+,
+UPB_DEFINE_STRUCT0(upb_textprinter,
+ upb_sink input_;
+ upb_bytessink *output_;
+ int indent_depth_;
+ bool single_line_;
+ void *subc;
+));
+
+UPB_BEGIN_EXTERN_C // {
+
+// C API.
+void upb_textprinter_init(upb_textprinter *p, const upb_handlers *h);
+void upb_textprinter_uninit(upb_textprinter *p);
+bool upb_textprinter_resetoutput(upb_textprinter *p, 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
+
+namespace upb {
+namespace pb {
+inline TextPrinter::TextPrinter(const upb::Handlers* handlers) {
+ upb_textprinter_init(this, handlers);
+}
+inline void TextPrinter::SetSingleLineMode(bool single_line) {
+ upb_textprinter_setsingleline(this, single_line);
+}
+inline bool TextPrinter::ResetOutput(BytesSink* output) {
+ return upb_textprinter_resetoutput(this, output);
+}
+inline Sink* TextPrinter::input() {
+ return upb_textprinter_input(this);
+}
+inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
+ const MessageDef *md) {
+ const Handlers* h = upb_textprinter_newhandlers(md, &h);
+ return reffed_ptr<const Handlers>(h, &h);
+}
+} // namespace pb
+} // namespace upb
+
+#endif
+
+#endif /* UPB_TEXT_H_ */
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * upb::json::Parser can parse JSON according to a specific schema.
+ * Support for parsing arbitrary JSON (schema-less) will be added later.
+ */
+
+#ifndef UPB_JSON_PARSER_H_
+#define UPB_JSON_PARSER_H_
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace json {
+class Parser;
+} // namespace json
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
+
+// Internal-only struct used by the parser.
+typedef struct {
+ UPB_PRIVATE_FOR_CPP
+ upb_sink sink;
+ const upb_msgdef *m;
+ const upb_fielddef *f;
+} upb_jsonparser_frame;
+
+
+/* upb::json::Parser **********************************************************/
+
+#define UPB_JSON_MAX_DEPTH 64
+
+// Parses an incoming BytesStream, pushing the results to the destination sink.
+UPB_DEFINE_CLASS0(upb::json::Parser,
+ public:
+ Parser(Status* status);
+ ~Parser();
+
+ // Resets the state of the printer, so that it will expect to begin a new
+ // document.
+ void Reset();
+
+ // Resets the output pointer which will serve as our closure. Implies
+ // Reset().
+ void ResetOutput(Sink* output);
+
+ // The input to the printer.
+ BytesSink* input();
+,
+UPB_DEFINE_STRUCT0(upb_json_parser,
+ upb_byteshandler input_handler_;
+ upb_bytessink input_;
+
+ // 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;
+
+ upb_status *status;
+
+ // Ragel's internal parsing stack for the parsing state machine.
+ int current_state;
+ int parser_stack[UPB_JSON_MAX_DEPTH];
+ int parser_top;
+
+ // A pointer to the beginning of whatever text we are currently parsing.
+ const char *text_begin;
+
+ // We have to accumulate text for member names, integers, unicode escapes, and
+ // base64 partial results.
+ const char *accumulated;
+ size_t accumulated_len;
+ // TODO: add members and code for allocating a buffer when necessary (when the
+ // member spans input buffers or contains escapes).
+));
+
+UPB_BEGIN_EXTERN_C
+
+void upb_json_parser_init(upb_json_parser *p, upb_status *status);
+void upb_json_parser_uninit(upb_json_parser *p);
+void upb_json_parser_reset(upb_json_parser *p);
+void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *output);
+upb_bytessink *upb_json_parser_input(upb_json_parser *p);
+
+UPB_END_EXTERN_C
+
+#ifdef __cplusplus
+
+namespace upb {
+namespace json {
+inline Parser::Parser(Status* status) { upb_json_parser_init(this, status); }
+inline Parser::~Parser() { upb_json_parser_uninit(this); }
+inline void Parser::Reset() { upb_json_parser_reset(this); }
+inline void Parser::ResetOutput(Sink* output) {
+ upb_json_parser_resetoutput(this, output);
+}
+inline BytesSink* Parser::input() {
+ return upb_json_parser_input(this);
+}
+} // namespace json
+} // namespace upb
+
+#endif
+
+
+#endif // UPB_JSON_PARSER_H_
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2014 Google Inc. See LICENSE for details.
+ * Author: Josh Haberman <jhaberman@gmail.com>
+ *
+ * upb::json::Printer allows you to create handlers that emit JSON
+ * according to a specific protobuf schema.
+ */
+
+#ifndef UPB_JSON_TYPED_PRINTER_H_
+#define UPB_JSON_TYPED_PRINTER_H_
+
+
+#ifdef __cplusplus
+namespace upb {
+namespace json {
+class Printer;
+} // namespace json
+} // namespace upb
+#endif
+
+UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer);
+
+
+/* upb::json::Printer *********************************************************/
+
+// Prints an incoming stream of data to a BytesSink in JSON format.
+UPB_DEFINE_CLASS0(upb::json::Printer,
+ public:
+ Printer(const upb::Handlers* handlers);
+ ~Printer();
+
+ // Resets the state of the printer, so that it will expect to begin a new
+ // document.
+ void Reset();
+
+ // Resets the output pointer which will serve as our closure. Implies
+ // Reset().
+ void ResetOutput(BytesSink* output);
+
+ // The input to the printer.
+ Sink* input();
+
+ // Returns handlers for printing according to the specified schema.
+ static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
+,
+UPB_DEFINE_STRUCT0(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];
+));
+
+UPB_BEGIN_EXTERN_C // {
+
+// Native C API.
+
+void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h);
+void upb_json_printer_uninit(upb_json_printer *p);
+void upb_json_printer_reset(upb_json_printer *p);
+void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output);
+upb_sink *upb_json_printer_input(upb_json_printer *p);
+const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
+ const void *owner);
+
+UPB_END_EXTERN_C // }
+
+#ifdef __cplusplus
+
+namespace upb {
+namespace json {
+inline Printer::Printer(const upb::Handlers* handlers) {
+ upb_json_printer_init(this, handlers);
+}
+inline Printer::~Printer() { upb_json_printer_uninit(this); }
+inline void Printer::Reset() { upb_json_printer_reset(this); }
+inline void Printer::ResetOutput(BytesSink* output) {
+ upb_json_printer_resetoutput(this, output);
+}
+inline Sink* Printer::input() { return upb_json_printer_input(this); }
+inline reffed_ptr<const Handlers> Printer::NewHandlers(
+ const upb::MessageDef *md) {
+ const Handlers* h = upb_json_printer_newhandlers(md, &h);
+ return reffed_ptr<const Handlers>(h, &h);
+}
+} // namespace json
+} // namespace upb
+
+#endif
+
+#endif // UPB_JSON_TYPED_PRINTER_H_
diff --git a/ruby/google-protobuf.gemspec b/ruby/google-protobuf.gemspec
new file mode 100644
index 00000000..87033ac4
--- /dev/null
+++ b/ruby/google-protobuf.gemspec
@@ -0,0 +1,22 @@
+class << Gem::Specification
+ def find_c_source(dir)
+ `cd #{dir}; git ls-files "*.c" "*.h" extconf.rb Makefile`.split
+ .map{|f| "#{dir}/#{f}"}
+ end
+end
+
+Gem::Specification.new do |s|
+ s.name = "google-protobuf"
+ s.version = "3.0.0.alpha.2"
+ s.licenses = ["BSD"]
+ s.summary = "Protocol Buffers"
+ s.description = "Protocol Buffers are Google's data interchange format."
+ s.authors = ["Protobuf Authors"]
+ s.email = "protobuf@googlegroups.com"
+ s.require_paths = ["lib"]
+ s.extensions = ["ext/google/protobuf_c/extconf.rb"]
+ s.files = ["lib/google/protobuf.rb"] +
+ # extension C source
+ find_c_source("ext/google/protobuf_c")
+ s.test_files = `git ls-files -- tests`.split
+end
diff --git a/ruby/lib/google/protobuf.rb b/ruby/lib/google/protobuf.rb
new file mode 100644
index 00000000..74053332
--- /dev/null
+++ b/ruby/lib/google/protobuf.rb
@@ -0,0 +1,31 @@
+# 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.
+
+require 'google/protobuf_c'
diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb
new file mode 100644
index 00000000..05b3a0fa
--- /dev/null
+++ b/ruby/tests/basic.rb
@@ -0,0 +1,633 @@
+#!/usr/bin/ruby
+
+require 'google/protobuf'
+require 'test/unit'
+
+# ------------- generated code --------------
+
+module BasicTest
+ pool = Google::Protobuf::DescriptorPool.new
+ pool.build do
+ 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 "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
+ end
+
+ TestMessage = pool.lookup("TestMessage").msgclass
+ TestMessage2 = pool.lookup("TestMessage2").msgclass
+ Recursive1 = pool.lookup("Recursive1").msgclass
+ Recursive2 = pool.lookup("Recursive2").msgclass
+ TestEnum = pool.lookup("TestEnum").enummodule
+ BadFieldNames = pool.lookup("BadFieldNames").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_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)
+ 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_inspect
+ m = TestMessage.new(:optional_int32 => -42,
+ :optional_enum => :A,
+ :optional_msg => TestMessage2.new,
+ :repeated_string => ["hello", "there", "world"])
+ expected = '<BasicTest::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: <BasicTest::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 m.inspect == expected
+ end
+
+ def test_hash
+ m1 = TestMessage.new(:optional_int32 => 42)
+ m2 = TestMessage.new(:optional_int32 => 102)
+ assert m1.hash != 0
+ assert m2.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
+ end
+
+ def test_type_errors
+ m = TestMessage.new
+ assert_raise TypeError do
+ m.optional_int32 = "hello"
+ end
+ assert_raise TypeError do
+ m.optional_string = 42
+ end
+ assert_raise TypeError do
+ m.optional_string = nil
+ end
+ assert_raise TypeError do
+ m.optional_bool = 42
+ end
+ assert_raise TypeError do
+ m.optional_msg = TestMessage.new # expects TestMessage2
+ end
+
+ assert_raise TypeError do
+ m.repeated_int32 = [] # needs RepeatedField
+ end
+
+ assert_raise TypeError do
+ m.repeated_int32.push "hello"
+ end
+
+ assert_raise TypeError do
+ m.repeated_msg.push TestMessage.new
+ 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, raises an error.
+ assert_raise TypeError do
+ m.optional_bytes = "Test string ASCII".encode!('ASCII')
+ end
+ assert_raise TypeError do
+ m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8')
+ end
+ assert_raise TypeError do
+ m.optional_string = ["FFFF"].pack('H*')
+ end
+
+ # "Ordinary" use case.
+ m.optional_bytes = ["FFFF"].pack('H*')
+ m.optional_string = "\u0100"
+
+ # strings are mutable so we can do this, but serialize should catch it.
+ m.optional_string = "asdf".encode!('UTF-8')
+ m.optional_string.encode!('ASCII-8BIT')
+ assert_raise TypeError do
+ data = TestMessage.encode(m)
+ 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 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.insert(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 TypeError do
+ m = TestMessage.new
+ l.push m
+ 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]
+ 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_rptfield_msg
+ l = Google::Protobuf::RepeatedField.new(:message, TestMessage)
+ l.push TestMessage.new
+ assert l.count == 1
+ assert_raise TypeError do
+ l.push TestMessage2.new
+ end
+ assert_raise 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 = 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 NameError 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
+ end
+ assert_raise ArgumentError do
+ l = Google::Protobuf::RepeatedField.new(:message)
+ end
+ assert_raise ArgumentError do
+ l = Google::Protobuf::RepeatedField.new([1, 2, 3])
+ end
+ assert_raise ArgumentError do
+ l = Google::Protobuf::RepeatedField.new(:message, [TestMessage2.new])
+ end
+ end
+
+ def test_enum_field
+ m = TestMessage.new
+ assert m.optional_enum == :Default
+ m.optional_enum = :A
+ assert m.optional_enum == :A
+ assert_raise NameError 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 = TestMessage.new
+ m.optional_string = "hello"
+ m.optional_int32 = 42
+ m.repeated_msg.push TestMessage2.new(:foo => 100)
+ m.repeated_msg.push TestMessage2.new(:foo => 200)
+
+ 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_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_def_errors
+ s = Google::Protobuf::DescriptorPool.new
+ assert_raise TypeError do
+ s.build do
+ # enum with no default (integer value 0)
+ add_enum "MyEnum" do
+ value :A, 1
+ end
+ end
+ end
+ assert_raise 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
+ 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)
+ assert m == m2
+ end
+ end
+end
diff --git a/ruby/tests/stress.rb b/ruby/tests/stress.rb
new file mode 100644
index 00000000..1bd768ed
--- /dev/null
+++ b/ruby/tests/stress.rb
@@ -0,0 +1,38 @@
+#!/usr/bin/ruby
+
+require 'google/protobuf'
+require 'test/unit'
+
+module StressTest
+ pool = Google::Protobuf::DescriptorPool.new
+ pool.build do
+ add_message "TestMessage" do
+ optional :a, :int32, 1
+ repeated :b, :message, 2, "M"
+ end
+ add_message "M" do
+ optional :foo, :string, 1
+ end
+ end
+
+ TestMessage = pool.lookup("TestMessage").msgclass
+ M = pool.lookup("M").msgclass
+
+ class StressTest < Test::Unit::TestCase
+ def get_msg
+ TestMessage.new(:a => 1000,
+ :b => [M.new(:foo => "hello"),
+ M.new(:foo => "world")])
+ end
+ def test_stress
+ m = get_msg
+ data = TestMessage.encode(m)
+ 100_000.times do
+ mnew = TestMessage.decode(data)
+ mnew = mnew.dup
+ assert mnew.inspect == m.inspect
+ assert TestMessage.encode(mnew) == data
+ end
+ end
+ end
+end
diff --git a/src/Makefile.am b/src/Makefile.am
index b32635da..ac0fcc41 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -38,36 +38,52 @@ MAINTAINERCLEANFILES = \
Makefile.in
nobase_include_HEADERS = \
+ google/protobuf/stubs/atomic_sequence_num.h \
google/protobuf/stubs/atomicops.h \
- google/protobuf/stubs/atomicops_internals_arm_gcc.h \
+ google/protobuf/stubs/atomicops_internals_aix.h \
google/protobuf/stubs/atomicops_internals_arm64_gcc.h \
+ google/protobuf/stubs/atomicops_internals_arm_gcc.h \
google/protobuf/stubs/atomicops_internals_arm_qnx.h \
google/protobuf/stubs/atomicops_internals_atomicword_compat.h \
google/protobuf/stubs/atomicops_internals_generic_gcc.h \
google/protobuf/stubs/atomicops_internals_macosx.h \
google/protobuf/stubs/atomicops_internals_mips_gcc.h \
google/protobuf/stubs/atomicops_internals_pnacl.h \
+ google/protobuf/stubs/atomicops_internals_solaris.h \
google/protobuf/stubs/atomicops_internals_tsan.h \
google/protobuf/stubs/atomicops_internals_x86_gcc.h \
google/protobuf/stubs/atomicops_internals_x86_msvc.h \
+ google/protobuf/stubs/casts.h \
+ google/protobuf/stubs/singleton.h \
google/protobuf/stubs/common.h \
- google/protobuf/stubs/platform_macros.h \
+ google/protobuf/stubs/fastmem.h \
google/protobuf/stubs/once.h \
+ google/protobuf/stubs/platform_macros.h \
google/protobuf/stubs/stl_util.h \
google/protobuf/stubs/template_util.h \
google/protobuf/stubs/type_traits.h \
+ google/protobuf/arena.h \
+ google/protobuf/arenastring.h \
+ google/protobuf/descriptor_database.h \
google/protobuf/descriptor.h \
google/protobuf/descriptor.pb.h \
- google/protobuf/descriptor_database.h \
google/protobuf/dynamic_message.h \
google/protobuf/extension_set.h \
google/protobuf/generated_enum_reflection.h \
- google/protobuf/generated_message_util.h \
google/protobuf/generated_message_reflection.h \
+ google/protobuf/generated_message_util.h \
+ google/protobuf/map_entry.h \
+ google/protobuf/map_field.h \
+ google/protobuf/map_field_inl.h \
+ google/protobuf/map.h \
+ google/protobuf/map_type_handler.h \
google/protobuf/message.h \
google/protobuf/message_lite.h \
+ google/protobuf/metadata.h \
+ google/protobuf/reflection.h \
google/protobuf/reflection_ops.h \
google/protobuf/repeated_field.h \
+ google/protobuf/repeated_field_reflection.h \
google/protobuf/service.h \
google/protobuf/text_format.h \
google/protobuf/unknown_field_set.h \
@@ -90,12 +106,14 @@ nobase_include_HEADERS = \
google/protobuf/compiler/plugin.pb.h \
google/protobuf/compiler/cpp/cpp_generator.h \
google/protobuf/compiler/java/java_generator.h \
- google/protobuf/compiler/python/python_generator.h
+ google/protobuf/compiler/javanano/javanano_generator.h \
+ google/protobuf/compiler/python/python_generator.h \
+ google/protobuf/compiler/ruby/ruby_generator.h
lib_LTLIBRARIES = libprotobuf-lite.la libprotobuf.la libprotoc.la
libprotobuf_lite_la_LIBADD = $(PTHREAD_LIBS)
-libprotobuf_lite_la_LDFLAGS = -version-info 9:0:0 -export-dynamic -no-undefined
+libprotobuf_lite_la_LDFLAGS = -version-info 10:0:0 -export-dynamic -no-undefined
libprotobuf_lite_la_SOURCES = \
google/protobuf/stubs/atomicops_internals_x86_gcc.cc \
google/protobuf/stubs/atomicops_internals_x86_msvc.cc \
@@ -106,6 +124,8 @@ libprotobuf_lite_la_SOURCES = \
google/protobuf/stubs/shared_ptr.h \
google/protobuf/stubs/stringprintf.cc \
google/protobuf/stubs/stringprintf.h \
+ google/protobuf/arena.cc \
+ google/protobuf/arenastring.cc \
google/protobuf/extension_set.cc \
google/protobuf/generated_message_util.cc \
google/protobuf/message_lite.cc \
@@ -117,7 +137,7 @@ libprotobuf_lite_la_SOURCES = \
google/protobuf/io/zero_copy_stream_impl_lite.cc
libprotobuf_la_LIBADD = $(PTHREAD_LIBS)
-libprotobuf_la_LDFLAGS = -version-info 9:0:0 -export-dynamic -no-undefined
+libprotobuf_la_LDFLAGS = -version-info 10:0:0 -export-dynamic -no-undefined
libprotobuf_la_SOURCES = \
$(libprotobuf_lite_la_SOURCES) \
google/protobuf/stubs/strutil.cc \
@@ -126,12 +146,14 @@ libprotobuf_la_SOURCES = \
google/protobuf/stubs/substitute.h \
google/protobuf/stubs/structurally_valid.cc \
google/protobuf/descriptor.cc \
- google/protobuf/descriptor.pb.cc \
google/protobuf/descriptor_database.cc \
+ google/protobuf/descriptor.pb.cc \
google/protobuf/dynamic_message.cc \
google/protobuf/extension_set_heavy.cc \
google/protobuf/generated_message_reflection.cc \
+ google/protobuf/map_field.cc \
google/protobuf/message.cc \
+ google/protobuf/reflection_internal.h \
google/protobuf/reflection_ops.cc \
google/protobuf/service.cc \
google/protobuf/text_format.cc \
@@ -146,7 +168,7 @@ libprotobuf_la_SOURCES = \
google/protobuf/compiler/parser.cc
libprotoc_la_LIBADD = $(PTHREAD_LIBS) libprotobuf.la
-libprotoc_la_LDFLAGS = -version-info 9:0:0 -export-dynamic -no-undefined
+libprotoc_la_LDFLAGS = -version-info 10:0:0 -export-dynamic -no-undefined
libprotoc_la_SOURCES = \
google/protobuf/compiler/code_generator.cc \
google/protobuf/compiler/command_line_interface.cc \
@@ -169,6 +191,8 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/cpp/cpp_generator.cc \
google/protobuf/compiler/cpp/cpp_helpers.cc \
google/protobuf/compiler/cpp/cpp_helpers.h \
+ google/protobuf/compiler/cpp/cpp_map_field.cc \
+ google/protobuf/compiler/cpp/cpp_map_field.h \
google/protobuf/compiler/cpp/cpp_message.cc \
google/protobuf/compiler/cpp/cpp_message.h \
google/protobuf/compiler/cpp/cpp_message_field.cc \
@@ -183,9 +207,9 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/java/java_context.cc \
google/protobuf/compiler/java/java_context.h \
google/protobuf/compiler/java/java_enum.cc \
- google/protobuf/compiler/java/java_enum.h \
google/protobuf/compiler/java/java_enum_field.cc \
google/protobuf/compiler/java/java_enum_field.h \
+ google/protobuf/compiler/java/java_enum.h \
google/protobuf/compiler/java/java_extension.cc \
google/protobuf/compiler/java/java_extension.h \
google/protobuf/compiler/java/java_field.cc \
@@ -199,10 +223,12 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/java/java_helpers.h \
google/protobuf/compiler/java/java_lazy_message_field.cc \
google/protobuf/compiler/java/java_lazy_message_field.h \
+ google/protobuf/compiler/java/java_map_field.cc \
+ google/protobuf/compiler/java/java_map_field.h \
google/protobuf/compiler/java/java_message.cc \
- google/protobuf/compiler/java/java_message.h \
google/protobuf/compiler/java/java_message_field.cc \
google/protobuf/compiler/java/java_message_field.h \
+ google/protobuf/compiler/java/java_message.h \
google/protobuf/compiler/java/java_name_resolver.cc \
google/protobuf/compiler/java/java_name_resolver.h \
google/protobuf/compiler/java/java_primitive_field.cc \
@@ -215,7 +241,29 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/java/java_string_field.h \
google/protobuf/compiler/java/java_doc_comment.cc \
google/protobuf/compiler/java/java_doc_comment.h \
- google/protobuf/compiler/python/python_generator.cc
+ google/protobuf/compiler/javanano/javanano_enum.cc \
+ google/protobuf/compiler/javanano/javanano_enum_field.h \
+ google/protobuf/compiler/javanano/javanano_extension.cc \
+ google/protobuf/compiler/javanano/javanano_field.cc \
+ google/protobuf/compiler/javanano/javanano_file.cc \
+ google/protobuf/compiler/javanano/javanano_generator.cc \
+ google/protobuf/compiler/javanano/javanano_helpers.cc \
+ google/protobuf/compiler/javanano/javanano_message.cc \
+ google/protobuf/compiler/javanano/javanano_message_field.h \
+ google/protobuf/compiler/javanano/javanano_params.h \
+ google/protobuf/compiler/javanano/javanano_primitive_field.h \
+ google/protobuf/compiler/javanano/javanano_enum_field.cc \
+ google/protobuf/compiler/javanano/javanano_enum.h \
+ google/protobuf/compiler/javanano/javanano_extension.h \
+ google/protobuf/compiler/javanano/javanano_field.h \
+ google/protobuf/compiler/javanano/javanano_file.h \
+ google/protobuf/compiler/javanano/javanano_generator.h \
+ google/protobuf/compiler/javanano/javanano_helpers.h \
+ google/protobuf/compiler/javanano/javanano_message_field.cc \
+ google/protobuf/compiler/javanano/javanano_message.h \
+ google/protobuf/compiler/javanano/javanano_primitive_field.cc \
+ google/protobuf/compiler/python/python_generator.cc \
+ google/protobuf/compiler/ruby/ruby_generator.cc
bin_PROGRAMS = protoc
protoc_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la
@@ -224,19 +272,29 @@ protoc_SOURCES = google/protobuf/compiler/main.cc
# Tests ==============================================================
protoc_inputs = \
+ google/protobuf/map_lite_unittest.proto \
+ google/protobuf/map_proto2_unittest.proto \
+ google/protobuf/map_unittest.proto \
google/protobuf/unittest.proto \
- google/protobuf/unittest_empty.proto \
- google/protobuf/unittest_import.proto \
- google/protobuf/unittest_import_public.proto \
- google/protobuf/unittest_mset.proto \
- google/protobuf/unittest_optimize_for.proto \
- google/protobuf/unittest_embed_optimize_for.proto \
+ google/protobuf/unittest_arena.proto \
google/protobuf/unittest_custom_options.proto \
- google/protobuf/unittest_lite.proto \
+ google/protobuf/unittest_drop_unknown_fields.proto \
+ google/protobuf/unittest_embed_optimize_for.proto \
+ google/protobuf/unittest_empty.proto \
google/protobuf/unittest_import_lite.proto \
+ google/protobuf/unittest_import.proto \
google/protobuf/unittest_import_public_lite.proto \
+ google/protobuf/unittest_import_public.proto \
google/protobuf/unittest_lite_imports_nonlite.proto \
+ google/protobuf/unittest_lite.proto \
+ google/protobuf/unittest_mset.proto \
+ google/protobuf/unittest_no_arena_import.proto \
+ google/protobuf/unittest_no_arena.proto \
+ google/protobuf/unittest_no_field_presence.proto \
google/protobuf/unittest_no_generic_services.proto \
+ google/protobuf/unittest_optimize_for.proto \
+ google/protobuf/unittest_preserve_unknown_enum.proto \
+ google/protobuf/unittest_proto3_arena.proto \
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
EXTRA_DIST = \
@@ -270,26 +328,46 @@ protoc_lite_outputs = \
protoc_outputs = \
$(protoc_lite_outputs) \
+ google/protobuf/map_lite_unittest.pb.cc \
+ google/protobuf/map_lite_unittest.pb.h \
+ google/protobuf/map_proto2_unittest.pb.cc \
+ google/protobuf/map_proto2_unittest.pb.h \
+ google/protobuf/map_unittest.pb.cc \
+ google/protobuf/map_unittest.pb.h \
google/protobuf/unittest.pb.cc \
google/protobuf/unittest.pb.h \
+ google/protobuf/unittest_arena.pb.cc \
+ google/protobuf/unittest_arena.pb.h \
+ google/protobuf/unittest_custom_options.pb.cc \
+ google/protobuf/unittest_custom_options.pb.h \
+ google/protobuf/unittest_drop_unknown_fields.pb.cc \
+ google/protobuf/unittest_drop_unknown_fields.pb.h \
+ google/protobuf/unittest_embed_optimize_for.pb.cc \
+ google/protobuf/unittest_embed_optimize_for.pb.h \
google/protobuf/unittest_empty.pb.cc \
google/protobuf/unittest_empty.pb.h \
google/protobuf/unittest_import.pb.cc \
google/protobuf/unittest_import.pb.h \
google/protobuf/unittest_import_public.pb.cc \
google/protobuf/unittest_import_public.pb.h \
- google/protobuf/unittest_mset.pb.cc \
- google/protobuf/unittest_mset.pb.h \
- google/protobuf/unittest_optimize_for.pb.cc \
- google/protobuf/unittest_optimize_for.pb.h \
- google/protobuf/unittest_embed_optimize_for.pb.cc \
- google/protobuf/unittest_embed_optimize_for.pb.h \
- google/protobuf/unittest_custom_options.pb.cc \
- google/protobuf/unittest_custom_options.pb.h \
google/protobuf/unittest_lite_imports_nonlite.pb.cc \
google/protobuf/unittest_lite_imports_nonlite.pb.h \
+ google/protobuf/unittest_mset.pb.cc \
+ google/protobuf/unittest_mset.pb.h \
+ google/protobuf/unittest_no_arena.pb.cc \
+ google/protobuf/unittest_no_arena.pb.h \
+ google/protobuf/unittest_no_arena_import.pb.cc \
+ google/protobuf/unittest_no_arena_import.pb.h \
+ google/protobuf/unittest_no_field_presence.pb.cc \
+ google/protobuf/unittest_no_field_presence.pb.h \
google/protobuf/unittest_no_generic_services.pb.cc \
google/protobuf/unittest_no_generic_services.pb.h \
+ google/protobuf/unittest_optimize_for.pb.cc \
+ google/protobuf/unittest_optimize_for.pb.h \
+ google/protobuf/unittest_preserve_unknown_enum.pb.cc \
+ google/protobuf/unittest_preserve_unknown_enum.pb.h \
+ google/protobuf/unittest_proto3_arena.pb.cc \
+ google/protobuf/unittest_proto3_arena.pb.h \
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.cc \
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h
@@ -315,6 +393,8 @@ endif
$(protoc_outputs): unittest_proto_middleman
COMMON_TEST_SOURCES = \
+ google/protobuf/map_test_util.cc \
+ google/protobuf/map_test_util.h \
google/protobuf/test_util.cc \
google/protobuf/test_util.h \
google/protobuf/testing/googletest.cc \
@@ -341,15 +421,23 @@ protobuf_test_SOURCES = \
google/protobuf/stubs/stringprintf_unittest.cc \
google/protobuf/stubs/template_util_unittest.cc \
google/protobuf/stubs/type_traits_unittest.cc \
+ google/protobuf/arenastring_unittest.cc \
+ google/protobuf/arena_unittest.cc \
google/protobuf/descriptor_database_unittest.cc \
google/protobuf/descriptor_unittest.cc \
+ google/protobuf/drop_unknown_fields_test.cc \
google/protobuf/dynamic_message_unittest.cc \
google/protobuf/extension_set_unittest.cc \
google/protobuf/generated_message_reflection_unittest.cc \
+ google/protobuf/map_field_test.cc \
+ google/protobuf/map_test.cc \
google/protobuf/message_unittest.cc \
+ google/protobuf/no_field_presence_test.cc \
+ google/protobuf/preserve_unknown_enum_test.cc \
+ google/protobuf/proto3_arena_unittest.cc \
google/protobuf/reflection_ops_unittest.cc \
- google/protobuf/repeated_field_unittest.cc \
google/protobuf/repeated_field_reflection_unittest.cc \
+ google/protobuf/repeated_field_unittest.cc \
google/protobuf/text_format_unittest.cc \
google/protobuf/unknown_field_set_unittest.cc \
google/protobuf/wire_format_unittest.cc \
diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc
new file mode 100644
index 00000000..18536781
--- /dev/null
+++ b/src/google/protobuf/arena.cc
@@ -0,0 +1,258 @@
+// 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 <google/protobuf/arena.h>
+
+#ifdef ADDRESS_SANITIZER
+#include <sanitizer/asan_interface.h>
+#endif
+
+namespace google {
+namespace protobuf {
+
+google::protobuf::internal::SequenceNumber Arena::lifecycle_id_generator_;
+#ifdef PROTOBUF_USE_DLLS
+Arena::ThreadCache& Arena::thread_cache() {
+ static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL };
+ return thread_cache_;
+}
+#else
+GOOGLE_THREAD_LOCAL Arena::ThreadCache Arena::thread_cache_ = { -1, NULL };
+#endif
+
+void Arena::Init(const ArenaOptions& options) {
+ lifecycle_id_ = lifecycle_id_generator_.GetNext();
+ start_block_size_ = options.start_block_size;
+ max_block_size_ = options.max_block_size;
+ block_alloc = options.block_alloc;
+ block_dealloc = options.block_dealloc;
+ blocks_ = 0;
+ hint_ = 0;
+ owns_first_block_ = true;
+ cleanup_list_ = 0;
+
+ if (options.initial_block != NULL && options.initial_block_size > 0) {
+ // Add first unowned block to list.
+ Block* first_block = reinterpret_cast<Block*>(options.initial_block);
+ first_block->size = options.initial_block_size;
+ first_block->pos = kHeaderSize;
+ first_block->next = NULL;
+ first_block->owner = &first_block->owner;
+ AddBlock(first_block);
+ owns_first_block_ = false;
+ }
+}
+
+uint64 Arena::Reset() {
+ CleanupList();
+ uint64 space_used = FreeBlocks();
+ // Invalidate any ThreadCaches pointing to any blocks we just destroyed.
+ lifecycle_id_ = lifecycle_id_generator_.GetNext();
+ return space_used;
+}
+
+Arena::Block* Arena::NewBlock(void* me, Block* my_last_block, size_t n,
+ size_t start_block_size, size_t max_block_size) {
+ size_t size;
+ if (my_last_block != NULL) {
+ // Double the current block size, up to a limit.
+ size = 2 * (my_last_block->size);
+ if (size > max_block_size) size = max_block_size;
+ } else {
+ size = start_block_size;
+ }
+ if (n > size - kHeaderSize) {
+ // TODO(sanjay): Check if n + kHeaderSize would overflow
+ size = kHeaderSize + n;
+ }
+
+ Block* b = reinterpret_cast<Block*>(block_alloc(size));
+ b->pos = kHeaderSize + n;
+ b->size = size;
+ if (b->avail() == 0) {
+ // Do not attempt to reuse this block.
+ b->owner = NULL;
+ } else {
+ b->owner = me;
+ }
+#ifdef ADDRESS_SANITIZER
+ // Poison the rest of the block for ASAN. It was unpoisoned by the underlying
+ // malloc but it's not yet usable until we return it as part of an allocation.
+ ASAN_POISON_MEMORY_REGION(
+ reinterpret_cast<char*>(b) + b->pos, b->size - b->pos);
+#endif
+ return b;
+}
+
+void Arena::AddBlock(Block* b) {
+ MutexLock l(&blocks_lock_);
+ b->next = reinterpret_cast<Block*>(google::protobuf::internal::NoBarrier_Load(&blocks_));
+ google::protobuf::internal::Release_Store(&blocks_, reinterpret_cast<google::protobuf::internal::AtomicWord>(b));
+ if (b->avail() != 0) {
+ // Direct future allocations to this block.
+ google::protobuf::internal::Release_Store(&hint_, reinterpret_cast<google::protobuf::internal::AtomicWord>(b));
+ }
+}
+
+void Arena::AddListNode(void* elem, void (*cleanup)(void*)) {
+ Node* node = reinterpret_cast<Node*>(AllocateAligned(sizeof(Node)));
+ node->elem = elem;
+ node->cleanup = cleanup;
+ node->next = reinterpret_cast<Node*>(
+ google::protobuf::internal::NoBarrier_AtomicExchange(&cleanup_list_,
+ reinterpret_cast<google::protobuf::internal::AtomicWord>(node)));
+}
+
+void* Arena::AllocateAligned(size_t n) {
+ // Align n to next multiple of 8 (from Hacker's Delight, Chapter 3.)
+ n = (n + 7) & -8;
+
+ // 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.
+ if (thread_cache().last_lifecycle_id_seen == lifecycle_id_ &&
+ thread_cache().last_block_used_ != NULL) {
+ if (thread_cache().last_block_used_->avail() < n) {
+ return SlowAlloc(n);
+ }
+ return AllocFromBlock(thread_cache().last_block_used_, n);
+ }
+
+ // Check whether we own the last accessed block on this arena.
+ // This fast path optimizes the case where a single thread uses multiple
+ // arenas.
+ void* me = &thread_cache();
+ Block* b = reinterpret_cast<Block*>(google::protobuf::internal::Acquire_Load(&hint_));
+ if (!b || b->owner != me || b->avail() < n) {
+ // If the next block to allocate from is the first block, try to claim it
+ // for this thread.
+ if (!owns_first_block_ && b->next == NULL) {
+ MutexLock l(&blocks_lock_);
+ if (b->owner == &b->owner && b->avail() >= n) {
+ b->owner = me;
+ SetThreadCacheBlock(b);
+ return AllocFromBlock(b, n);
+ }
+ }
+ return SlowAlloc(n);
+ }
+ return AllocFromBlock(b, n);
+}
+
+void* Arena::AllocFromBlock(Block* b, size_t n) {
+ size_t p = b->pos;
+ b->pos = p + n;
+#ifdef ADDRESS_SANITIZER
+ ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<char*>(b) + p, n);
+#endif
+ return reinterpret_cast<char*>(b) + p;
+}
+
+void* Arena::SlowAlloc(size_t n) {
+ void* me = &thread_cache();
+ Block* b = FindBlock(me); // Find block owned by me.
+ // See if allocation fits in my latest block.
+ if (b != NULL && b->avail() >= n) {
+ SetThreadCacheBlock(b);
+ google::protobuf::internal::NoBarrier_Store(&hint_, reinterpret_cast<google::protobuf::internal::AtomicWord>(b));
+ return AllocFromBlock(b, n);
+ }
+ b = NewBlock(me, b, n, start_block_size_, max_block_size_);
+ AddBlock(b);
+ if (b->owner == me) { // If this block can be reused (see NewBlock()).
+ SetThreadCacheBlock(b);
+ }
+ return reinterpret_cast<char*>(b) + kHeaderSize;
+}
+
+uint64 Arena::SpaceUsed() const {
+ uint64 space_used = 0;
+ Block* b = reinterpret_cast<Block*>(google::protobuf::internal::NoBarrier_Load(&blocks_));
+ while (b != NULL) {
+ space_used += (b->size);
+ b = b->next;
+ }
+ return space_used;
+}
+
+
+uint64 Arena::FreeBlocks() {
+ uint64 space_used = 0;
+ Block* b = reinterpret_cast<Block*>(google::protobuf::internal::NoBarrier_Load(&blocks_));
+ Block* first_block = NULL;
+ while (b != NULL) {
+ space_used += (b->size);
+ Block* next = b->next;
+ if (next != NULL) {
+ block_dealloc(b, b->size);
+ } else {
+ if (owns_first_block_) {
+ block_dealloc(b, b->size);
+ } else {
+ // User passed in the first block, skip free'ing the memory.
+ first_block = b;
+ }
+ }
+ b = next;
+ }
+ blocks_ = 0;
+ hint_ = 0;
+ if (!owns_first_block_) {
+ // Make the first block that was passed in through ArenaOptions
+ // available for reuse.
+ first_block->pos = kHeaderSize;
+ first_block->owner = &first_block->owner;
+ AddBlock(first_block);
+ }
+ return space_used;
+}
+
+void Arena::CleanupList() {
+ Node* head =
+ reinterpret_cast<Node*>(google::protobuf::internal::NoBarrier_Load(&cleanup_list_));
+ while (head != NULL) {
+ head->cleanup(head->elem);
+ head = head->next;
+ }
+ cleanup_list_ = 0;
+}
+
+Arena::Block* Arena::FindBlock(void* me) {
+ // TODO(sanjay): We might want to keep a separate list with one
+ // entry per thread.
+ Block* b = reinterpret_cast<Block*>(google::protobuf::internal::Acquire_Load(&blocks_));
+ while (b != NULL && b->owner != me) {
+ b = b->next;
+ }
+ return b;
+}
+
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h
new file mode 100644
index 00000000..d0cb163c
--- /dev/null
+++ b/src/google/protobuf/arena.h
@@ -0,0 +1,484 @@
+// 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 header is logically internal, but is made public because it is used
+// from protocol-compiler-generated code, which may reside in other components.
+
+#ifndef GOOGLE_PROTOBUF_ARENA_H__
+#define GOOGLE_PROTOBUF_ARENA_H__
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/atomic_sequence_num.h>
+#include <google/protobuf/stubs/atomicops.h>
+#include <google/protobuf/stubs/type_traits.h>
+
+namespace google {
+namespace protobuf {
+
+class Arena; // defined below
+class Message; // message.h
+
+namespace internal {
+class ArenaString; // arenastring.h
+class LazyField; // lazy_field.h
+
+template<typename Type>
+class GenericTypeHandler; // repeated_field.h
+
+// Templated cleanup methods.
+template<typename T> void arena_destruct_object(void* object) {
+ reinterpret_cast<T*>(object)->~T();
+}
+template<typename T> void arena_delete_object(void* object) {
+ delete reinterpret_cast<T*>(object);
+}
+inline void arena_free(void* object, size_t size) {
+ free(object);
+}
+
+} // namespace internal
+
+// ArenaOptions provides optional additional parameters to arena construction
+// that control its block-allocation behavior.
+struct ArenaOptions {
+ // This defines the size of the first block requested from the system malloc.
+ // Subsequent block sizes will increase in a geometric series up to a maximum.
+ size_t start_block_size;
+
+ // This defines the maximum block size requested from system malloc (unless an
+ // individual arena allocation request occurs with a size larger than this
+ // maximum). Requested block sizes increase up to this value, then remain
+ // here.
+ size_t max_block_size;
+
+ // An initial block of memory for the arena to use, or NULL for none. If
+ // provided, the block must live at least as long as the arena itself. The
+ // creator of the Arena retains ownership of the block after the Arena is
+ // destroyed.
+ char* initial_block;
+
+ // The size of the initial block, if provided.
+ size_t initial_block_size;
+
+ // A function pointer to an alloc method that returns memory blocks of size
+ // requested. By default, it contains a ptr to the malloc function.
+ void* (*block_alloc)(size_t);
+ // A function pointer to a dealloc method that takes ownership of the blocks
+ // from the arena. By default, it contains a ptr to a wrapper function that
+ // calls free.
+ void (*block_dealloc)(void*, size_t);
+
+ ArenaOptions()
+ : start_block_size(kDefaultStartBlockSize),
+ max_block_size(kDefaultMaxBlockSize),
+ initial_block(NULL),
+ initial_block_size(0),
+ block_alloc(&malloc),
+ block_dealloc(&internal::arena_free) {}
+
+ private:
+ // Constants define default starting block size and max block size for
+ // arena allocator behavior -- see descriptions above.
+ static const size_t kDefaultStartBlockSize = 256;
+ static const size_t kDefaultMaxBlockSize = 8192;
+};
+
+// Arena allocator. Arena allocation replaces ordinary (heap-based) allocation
+// with new/delete, and improves performance by aggregating allocations into
+// larger blocks and freeing allocations all at once. Protocol messages are
+// allocated on an arena by using Arena::CreateMessage<T>(Arena*), below, and
+// are automatically freed when the arena is destroyed.
+//
+// This is a thread-safe implementation: multiple threads may allocate from the
+// arena concurrently. Destruction is not thread-safe and the destructing
+// thread must synchronize with users of the arena first.
+class LIBPROTOBUF_EXPORT Arena {
+ public:
+ // Arena constructor taking custom options. See ArenaOptions below for
+ // descriptions of the options available.
+ explicit Arena(const ArenaOptions& options) {
+ Init(options);
+ }
+
+ // Default constructor with sensible default options, tuned for average
+ // use-cases.
+ Arena() {
+ Init(ArenaOptions());
+ }
+
+ // Destructor deletes all owned heap allocated objects, and destructs objects
+ // that have non-trivial destructors, except for proto2 message objects whose
+ // destructors can be skipped. Also, frees all blocks except the initial block
+ // if it was passed in.
+ ~Arena() {
+ Reset();
+ }
+
+ // API to create proto2 message objects on the arena. If the arena passed in
+ // is NULL, then a heap allocated object is returned. Type T must be a message
+ // defined in a .proto file with cc_enable_arenas set to true, otherwise a
+ // compilation error will occur.
+ //
+ // RepeatedField and RepeatedPtrField may also be instantiated directly on an
+ // arena with this method: they act as "arena-capable message types" for the
+ // purposes of the Arena API.
+ template <typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* CreateMessage(::google::protobuf::Arena* arena) {
+ if (arena == NULL) {
+ return new T;
+ } else {
+ return arena->CreateMessageInternal<T>(static_cast<T*>(0));
+ }
+ }
+
+ // API to create any objects on the arena. Note that only the object will
+ // be created on the arena; the underlying ptrs (in case of a proto2 message)
+ // will be still heap allocated. Proto messages should usually be allocated
+ // with CreateMessage<T>() instead.
+ template <typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* Create(::google::protobuf::Arena* arena) {
+ if (arena == NULL) {
+ return new T();
+ } else {
+ return arena->CreateInternal<T>(
+ SkipDeleteList<T>(static_cast<T*>(0)));
+ }
+ }
+
+ // Version of the above with one constructor argument for the created object.
+ template <typename T, typename Arg> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* Create(::google::protobuf::Arena* arena, const Arg& arg) {
+ if (arena == NULL) {
+ return new T(arg);
+ } else {
+ return arena->CreateInternal<T>(SkipDeleteList<T>(static_cast<T*>(0)),
+ arg);
+ }
+ }
+
+ // Version of the above with two constructor arguments for the created object.
+ template <typename T, typename Arg1, typename Arg2> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* Create(::google::protobuf::Arena* arena, const Arg1& arg1, const Arg2& arg2) {
+ if (arena == NULL) {
+ return new T(arg1, arg2);
+ } else {
+ return arena->CreateInternal<T>(SkipDeleteList<T>(static_cast<T*>(0)),
+ arg1,
+ arg2);
+ }
+ }
+
+ // Create an array of object type T on the arena. Type T must have a trivial
+ // constructor, as it will not be invoked when created on the arena.
+ template <typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* CreateArray(::google::protobuf::Arena* arena, size_t num_elements) {
+ if (arena == NULL) {
+ return new T[num_elements];
+ } else {
+ return static_cast<T*>(
+ arena->AllocateAligned(num_elements * sizeof(T)));
+ }
+ }
+
+ // Returns the total space used by the arena, which is the sums of the sizes
+ // of the underlying blocks. The total space used may not include the new
+ // blocks that are allocated by this arena from other threads concurrently
+ // with the call to this method.
+ uint64 SpaceUsed() const GOOGLE_ATTRIBUTE_NOINLINE;
+
+ // Frees all storage allocated by this arena after calling destructors
+ // registered with OwnDestructor() and freeing objects registered with Own().
+ // Any objects allocated on this arena are unusable after this call. It also
+ // returns the total space used by the arena which is the sums of the sizes
+ // of the allocated blocks. This method is not thread-safe.
+ uint64 Reset() GOOGLE_ATTRIBUTE_NOINLINE;
+
+ // Adds |object| to a list of heap-allocated objects to be freed with |delete|
+ // when the arena is destroyed or reset.
+ template <typename T> GOOGLE_ATTRIBUTE_NOINLINE
+ void Own(T* object) {
+ OwnInternal(object, google::protobuf::internal::is_convertible<T*, ::google::protobuf::Message*>());
+ }
+
+ // Adds |object| to a list of objects whose destructors will be manually
+ // called when the arena is destroyed or reset. This differs from Own() in
+ // that it does not free the underlying memory with |delete|; hence, it is
+ // normally only used for objects that are placement-newed into
+ // arena-allocated memory.
+ template <typename T> GOOGLE_ATTRIBUTE_NOINLINE
+ void OwnDestructor(T* object) {
+ if (object != NULL) {
+ AddListNode(object, &internal::arena_destruct_object<T>);
+ }
+ }
+
+ // Adds a custom member function on an object to the list of destructors that
+ // will be manually called when the arena is destroyed or reset. This differs
+ // from OwnDestructor() in that any member function may be specified, not only
+ // the class destructor.
+ void OwnCustomDestructor(void* object, void (*destruct)(void*))
+ GOOGLE_ATTRIBUTE_NOINLINE {
+ AddListNode(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.
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static inline ::google::protobuf::Arena* GetArena(T* value) {
+ return GetArenaInternal(value, static_cast<T*>(0));
+ }
+
+ // Helper typetrait that indicates support for arenas in a type T at compile
+ // time. This is public only to allow construction of higher-level templated
+ // utilities. is_arena_constructable<T>::value is an instance of
+ // google::protobuf::internal::true_type if the message type T has arena support enabled, and
+ // google::protobuf::internal::false_type otherwise.
+ //
+ // This is inside Arena because only Arena has the friend relationships
+ // necessary to see the underlying generated code traits.
+ template<typename T>
+ struct is_arena_constructable {
+ template<typename U>
+ static char ArenaConstructable(
+ const typename U::InternalArenaConstructable_*);
+ template<typename U>
+ static double ArenaConstructable(...);
+
+ // This will resolve to either google::protobuf::internal::true_type or google::protobuf::internal::false_type.
+ typedef google::protobuf::internal::integral_constant<bool,
+ sizeof(ArenaConstructable<const T>(static_cast<const T*>(0))) ==
+ sizeof(char)> type;
+ static const type value;
+ };
+
+ private:
+ // Blocks are variable length malloc-ed objects. The following structure
+ // describes the common header for all blocks.
+ struct Block {
+ void* owner; // &ThreadCache of thread that owns this block, or
+ // &this->owner if not yet owned by a thread.
+ Block* next; // Next block in arena (may have different owner)
+ // ((char*) &block) + pos is next available byte. It is always
+ // aligned at a multiple of 8 bytes.
+ size_t pos;
+ size_t size; // total size of the block.
+ size_t avail() const GOOGLE_ATTRIBUTE_ALWAYS_INLINE { return size - pos; }
+ // data follows
+ };
+
+ void* (*block_alloc)(size_t); // Allocates a free block of a particular size.
+ void (*block_dealloc)(void*, size_t); // Deallocates the given block.
+
+ template<typename Type> friend class ::google::protobuf::internal::GenericTypeHandler;
+ friend class MockArena; // For unit-testing.
+ friend class internal::ArenaString; // For AllocateAligned.
+ friend class internal::LazyField; // For CreateMaybeMessage.
+
+ struct ThreadCache {
+ // The ThreadCache is considered valid as long as this matches the
+ // lifecycle_id of the arena being used.
+ int64 last_lifecycle_id_seen;
+ Block* last_block_used_;
+ };
+
+ static const size_t kHeaderSize = sizeof(Block);
+ static google::protobuf::internal::SequenceNumber lifecycle_id_generator_;
+#ifdef PROTOBUF_USE_DLLS
+ static ThreadCache& thread_cache();
+#else
+ static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_;
+ static ThreadCache& thread_cache() { return thread_cache_; }
+#endif
+
+ // SFINAE for skipping addition to delete list for a Type. This is mainly to
+ // skip proto2/proto1 message objects with cc_enable_arenas=true from being
+ // part of the delete list. Also, note, compiler will optimize out the branch
+ // in CreateInternal<T>.
+ //
+ template<typename T>
+ static inline bool SkipDeleteList(typename T::DestructorSkippable_*) {
+ return true;
+ }
+
+ // For non message objects, we skip addition to delete list if the object has
+ // a trivial destructor.
+ template<typename T>
+ static inline bool SkipDeleteList(...) {
+ return google::protobuf::internal::has_trivial_destructor<T>::value;
+ }
+
+ // CreateMessage<T> requires that T supports arenas, but this private method
+ // works whether or not T supports arenas. These are not exposed to user code
+ // as it can cause confusing API usages, and end up having double free in
+ // user code. These are used only internally from LazyField and Repeated
+ // fields, since they are designed to work in all mode combinations.
+ template<typename Msg> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static Msg* CreateMaybeMessage(
+ Arena* arena, typename Msg::InternalArenaConstructable_*) {
+ return CreateMessage<Msg>(arena);
+ }
+
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static T* CreateMaybeMessage(Arena* arena, ...) {
+ return Create<T>(arena);
+ }
+
+ template <typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ inline T* CreateInternal(
+ bool skip_explicit_ownership) {
+ T* t = new (AllocateAligned(sizeof(T))) T();
+ if (!skip_explicit_ownership) {
+ AddListNode(t, &internal::arena_destruct_object<T>);
+ }
+ return t;
+ }
+
+ template <typename T, typename Arg> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ inline T* CreateInternal(
+ bool skip_explicit_ownership, const Arg& arg) {
+ T* t = new (AllocateAligned(sizeof(T))) T(arg);
+ if (!skip_explicit_ownership) {
+ AddListNode(t, &internal::arena_destruct_object<T>);
+ }
+ return t;
+ }
+
+ template <typename T, typename Arg1, typename Arg2> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ inline T* CreateInternal(
+ bool skip_explicit_ownership, const Arg1& arg1, const Arg2& arg2) {
+ T* t = new (AllocateAligned(sizeof(T))) T(arg1, arg2);
+ if (!skip_explicit_ownership) {
+ AddListNode(t, &internal::arena_destruct_object<T>);
+ }
+ return t;
+ }
+
+ template <typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ inline T* CreateMessageInternal(typename T::InternalArenaConstructable_*) {
+ return CreateInternal<T, Arena*>(SkipDeleteList<T>(static_cast<T*>(0)),
+ this);
+ }
+
+ // These implement Own(), which registers an object for deletion (destructor
+ // call and operator delete()). The second parameter has type 'true_type' if T
+ // is a subtype of ::google::protobuf::Message and 'false_type' otherwise. Collapsing
+ // all template instantiations to one for generic Message reduces code size,
+ // using the virtual destructor instead.
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ void OwnInternal(T* object, google::protobuf::internal::true_type) {
+ if (object != NULL) {
+ AddListNode(object, &internal::arena_delete_object< ::google::protobuf::Message >);
+ }
+ }
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ void OwnInternal(T* object, google::protobuf::internal::false_type) {
+ if (object != NULL) {
+ AddListNode(object, &internal::arena_delete_object<T>);
+ }
+ }
+
+ // Implementation for GetArena(). Only message objects with
+ // InternalArenaConstructable_ tags can be associated with an arena, and such
+ // objects must implement a GetArenaNoVirtual() method.
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static inline ::google::protobuf::Arena* GetArenaInternal(T* value,
+ typename T::InternalArenaConstructable_*) {
+ return value->GetArenaNoVirtual();
+ }
+
+ template<typename T> GOOGLE_ATTRIBUTE_ALWAYS_INLINE
+ static inline ::google::protobuf::Arena* GetArenaInternal(T* value, ...) {
+ return NULL;
+ }
+
+
+ void* AllocateAligned(size_t size);
+
+ void Init(const ArenaOptions& options);
+
+ // Free all blocks and return the total space used which is the sums of sizes
+ // of the all the allocated blocks.
+ uint64 FreeBlocks();
+
+ // Add object pointer and cleanup function pointer to the list.
+ // TODO(rohananil, cfallin): We could pass in a sub-arena into this method
+ // to avoid polluting blocks of this arena with list nodes. This would help in
+ // mixed mode (where many protobufs have cc_enable_arenas=false), and is an
+ // alternative to a chunked linked-list, but with extra overhead of *next.
+ void AddListNode(void* elem, void (*cleanup)(void*));
+ // Delete or Destruct all objects owned by the arena.
+ void CleanupList();
+
+ inline void SetThreadCacheBlock(Block* block) {
+ thread_cache().last_block_used_ = block;
+ thread_cache().last_lifecycle_id_seen = lifecycle_id_;
+ }
+
+ int64 lifecycle_id_; // Unique for each arena. Changes on Reset().
+ size_t start_block_size_; // Starting block size of the arena.
+ size_t max_block_size_; // Max block size of the arena.
+
+ google::protobuf::internal::AtomicWord blocks_; // Head of linked list of all allocated blocks
+ google::protobuf::internal::AtomicWord hint_; // Fast thread-local block access
+
+ // Node contains the ptr of the object to be cleaned up and the associated
+ // cleanup function ptr.
+ struct Node {
+ void* elem; // Pointer to the object to be cleaned up.
+ void (*cleanup)(void*); // Function pointer to the destructor or deleter.
+ Node* next; // Next node in the list.
+ };
+
+ google::protobuf::internal::AtomicWord cleanup_list_; // Head of a linked list of nodes containing object
+ // ptrs and cleanup methods.
+
+ bool owns_first_block_; // Indicates that arena owns the first block
+ Mutex blocks_lock_;
+
+ void AddBlock(Block* b);
+ void* SlowAlloc(size_t n);
+ Block* FindBlock(void* me);
+ Block* NewBlock(void* me, Block* my_last_block, size_t n,
+ size_t start_block_size, size_t max_block_size);
+ static void* AllocFromBlock(Block* b, size_t n);
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Arena);
+};
+
+template<typename T>
+const typename Arena::is_arena_constructable<T>::type
+ Arena::is_arena_constructable<T>::value =
+ typename Arena::is_arena_constructable<T>::type();
+
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_ARENA_H__
diff --git a/src/google/protobuf/arena_nc.cc b/src/google/protobuf/arena_nc.cc
new file mode 100644
index 00000000..f2f08427
--- /dev/null
+++ b/src/google/protobuf/arena_nc.cc
@@ -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.
+
+// Negative compilation test for arena usage.
+
+#include <google/protobuf/arena.h>
+#include <google/protobuf/unittest.pb.h>
+
+#ifdef TEST_ARENA_PRIVATE_CONSTRUCTOR
+
+namespace google {
+void ArenaPrivateConstructor() {
+ google::protobuf::Arena arena;
+ protobuf_unittest::TestAllTypes message(&arena);
+}
+
+#endif
+} // namespace google
diff --git a/src/google/protobuf/arena_nc_test.py b/src/google/protobuf/arena_nc_test.py
new file mode 100644
index 00000000..41b1b5f8
--- /dev/null
+++ b/src/google/protobuf/arena_nc_test.py
@@ -0,0 +1,59 @@
+#! /usr/bin/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.
+
+"""Negative compilation unit tests for arena API."""
+
+from google3.testing.pybase import fake_target_util
+from google.apputils import basetest
+
+
+class ArenaNcTest(basetest.TestCase):
+
+ def testCompilerErrors(self):
+ """Runs a list of tests to verify compiler error messages."""
+
+ # Defines a list of test specs, where each element is a tuple
+ # (test name, list of regexes for matching the compiler errors).
+ test_specs = [
+ ('ARENA_PRIVATE_CONSTRUCTOR',
+ [r'calling a protected constructor']),
+ ('SANITY', None)]
+
+ fake_target_util.AssertCcCompilerErrors(
+ self, # The current test case.
+ 'google3/google/protobuf/arena_nc', # The fake target file.
+ 'arena_nc.o', # The sub-target to build.
+ test_specs # List of test specifications.
+ )
+
+if __name__ == '__main__':
+ basetest.main()
diff --git a/src/google/protobuf/arena_unittest.cc b/src/google/protobuf/arena_unittest.cc
new file mode 100644
index 00000000..9d3d3e3e
--- /dev/null
+++ b/src/google/protobuf/arena_unittest.cc
@@ -0,0 +1,1009 @@
+// 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 <google/protobuf/arena.h>
+
+#include <algorithm>
+#include <cstring>
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+#include <string>
+#include <vector>
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/test_util.h>
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/unittest_arena.pb.h>
+#include <google/protobuf/unittest_no_arena.pb.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/extension_set.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/message_lite.h>
+#include <google/protobuf/repeated_field.h>
+#include <google/protobuf/unknown_field_set.h>
+#include <gtest/gtest.h>
+
+
+namespace google {
+using proto2_arena_unittest::ArenaMessage;
+using protobuf_unittest::TestAllTypes;
+using protobuf_unittest::TestAllExtensions;
+using protobuf_unittest::TestOneof2;
+using protobuf_unittest::TestEmptyMessage;
+
+namespace protobuf {
+namespace {
+
+class Notifier {
+ public:
+ Notifier() : count_(0) {}
+ void Notify() {
+ count_++;
+ }
+ int GetCount() {
+ return count_;
+ }
+
+ private:
+ int count_;
+};
+
+class SimpleDataType {
+ public:
+ SimpleDataType() : notifier_(NULL) {}
+ void SetNotifier(Notifier* notifier) {
+ notifier_ = notifier;
+ }
+ virtual ~SimpleDataType() {
+ if (notifier_ != NULL) {
+ notifier_->Notify();
+ }
+ };
+ private:
+ Notifier* notifier_;
+};
+} // namespace
+
+TEST(ArenaTest, ArenaConstructable) {
+ EXPECT_TRUE(Arena::is_arena_constructable<TestAllTypes>::type::value);
+ EXPECT_TRUE(Arena::is_arena_constructable<const TestAllTypes>::type::value);
+ EXPECT_FALSE(Arena::is_arena_constructable<Arena>::type::value);
+}
+
+TEST(ArenaTest, BasicCreate) {
+ Arena arena;
+ EXPECT_TRUE(Arena::Create<int32>(&arena) != NULL);
+ EXPECT_TRUE(Arena::Create<int64>(&arena) != NULL);
+ EXPECT_TRUE(Arena::Create<float>(&arena) != NULL);
+ EXPECT_TRUE(Arena::Create<double>(&arena) != NULL);
+ EXPECT_TRUE(Arena::Create<string>(&arena) != NULL);
+ arena.Own(new int32);
+ arena.Own(new int64);
+ arena.Own(new float);
+ arena.Own(new double);
+ arena.Own(new string);
+ arena.Own<int>(NULL);
+ Notifier notifier;
+ SimpleDataType* data = Arena::Create<SimpleDataType>(&arena);
+ data->SetNotifier(&notifier);
+ data = new SimpleDataType;
+ data->SetNotifier(&notifier);
+ arena.Own(data);
+ arena.Reset();
+ EXPECT_EQ(2, notifier.GetCount());
+}
+
+TEST(ArenaTest, InitialBlockTooSmall) {
+ // Construct a small (64 byte) initial block of memory to be used by the
+ // arena allocator; then, allocate an object which will not fit in the
+ // initial block.
+ std::vector<char> arena_block(64);
+ ArenaOptions options;
+ options.initial_block = &arena_block[0];
+ options.initial_block_size = arena_block.size();
+ Arena arena(options);
+
+ char* p = ::google::protobuf::Arena::CreateArray<char>(&arena, 96);
+ uintptr_t allocation = reinterpret_cast<uintptr_t>(p);
+
+ // Ensure that the arena allocator did not return memory pointing into the
+ // initial block of memory.
+ uintptr_t arena_start = reinterpret_cast<uintptr_t>(&arena_block[0]);
+ uintptr_t arena_end = arena_start + arena_block.size();
+ EXPECT_FALSE(allocation >= arena_start && allocation < arena_end);
+
+ // Write to the memory we allocated; this should (but is not guaranteed to)
+ // trigger a check for heap corruption if the object was allocated from the
+ // initially-provided block.
+ memset(p, '\0', 96);
+}
+
+TEST(ArenaTest, Parsing) {
+ TestAllTypes original;
+ TestUtil::SetAllFields(&original);
+
+ // Test memory leak.
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->ParseFromString(original.SerializeAsString());
+ TestUtil::ExpectAllFieldsSet(*arena_message);
+
+ // Test that string fields have null terminator bytes (earlier bug).
+ EXPECT_EQ(strlen(original.optional_string().c_str()),
+ strlen(arena_message->optional_string().c_str()));
+}
+
+TEST(ArenaTest, UnknownFields) {
+ TestAllTypes original;
+ TestUtil::SetAllFields(&original);
+
+ // Test basic parsing into (populating) and reading out of unknown fields on
+ // an arena.
+ Arena arena;
+ TestEmptyMessage* arena_message =
+ Arena::CreateMessage<TestEmptyMessage>(&arena);
+ arena_message->ParseFromString(original.SerializeAsString());
+
+ TestAllTypes copied;
+ copied.ParseFromString(arena_message->SerializeAsString());
+ TestUtil::ExpectAllFieldsSet(copied);
+
+ // Exercise UFS manual manipulation (setters).
+ arena_message = Arena::CreateMessage<TestEmptyMessage>(&arena);
+ arena_message->mutable_unknown_fields()->AddVarint(
+ TestAllTypes::kOptionalInt32FieldNumber, 42);
+ copied.Clear();
+ copied.ParseFromString(arena_message->SerializeAsString());
+ EXPECT_TRUE(copied.has_optional_int32());
+ EXPECT_EQ(42, copied.optional_int32());
+
+ // Exercise UFS swap path.
+ TestEmptyMessage* arena_message_2 =
+ Arena::CreateMessage<TestEmptyMessage>(&arena);
+ arena_message_2->Swap(arena_message);
+ copied.Clear();
+ copied.ParseFromString(arena_message_2->SerializeAsString());
+ EXPECT_TRUE(copied.has_optional_int32());
+ EXPECT_EQ(42, copied.optional_int32());
+
+ // Test field manipulation.
+ TestEmptyMessage* arena_message_3 =
+ Arena::CreateMessage<TestEmptyMessage>(&arena);
+ arena_message_3->mutable_unknown_fields()->AddVarint(1000, 42);
+ arena_message_3->mutable_unknown_fields()->AddFixed32(1001, 42);
+ arena_message_3->mutable_unknown_fields()->AddFixed64(1002, 42);
+ arena_message_3->mutable_unknown_fields()->AddLengthDelimited(1003);
+ arena_message_3->mutable_unknown_fields()->DeleteSubrange(0, 2);
+ arena_message_3->mutable_unknown_fields()->DeleteByNumber(1002);
+ arena_message_3->mutable_unknown_fields()->DeleteByNumber(1003);
+ EXPECT_TRUE(arena_message_3->unknown_fields().empty());
+}
+
+TEST(ArenaTest, Swap) {
+ Arena arena1;
+ Arena arena2;
+ TestAllTypes* arena1_message;
+ TestAllTypes* arena2_message;
+
+ // Case 1: Swap(), no UFS on either message, both messages on different
+ // arenas. Arena pointers should remain the same after swap.
+ arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ arena1_message->Swap(arena2_message);
+ EXPECT_EQ(&arena1, arena1_message->GetArena());
+ EXPECT_EQ(&arena2, arena2_message->GetArena());
+
+ // Case 2: Swap(), UFS on one message, both messages on different arenas.
+ arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ arena1_message->mutable_unknown_fields()->AddVarint(1, 42);
+ arena1_message->Swap(arena2_message);
+ EXPECT_EQ(&arena1, arena1_message->GetArena());
+ EXPECT_EQ(&arena2, arena2_message->GetArena());
+ EXPECT_EQ(0, arena1_message->unknown_fields().field_count());
+ EXPECT_EQ(1, arena2_message->unknown_fields().field_count());
+ EXPECT_EQ(42, arena2_message->unknown_fields().field(0).varint());
+
+ // Case 3: Swap(), UFS on both messages, both messages on different arenas.
+ arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ arena1_message->mutable_unknown_fields()->AddVarint(1, 42);
+ arena2_message->mutable_unknown_fields()->AddVarint(2, 84);
+ arena1_message->Swap(arena2_message);
+ EXPECT_EQ(&arena1, arena1_message->GetArena());
+ EXPECT_EQ(&arena2, arena2_message->GetArena());
+ EXPECT_EQ(1, arena1_message->unknown_fields().field_count());
+ EXPECT_EQ(1, arena2_message->unknown_fields().field_count());
+ EXPECT_EQ(84, arena1_message->unknown_fields().field(0).varint());
+ EXPECT_EQ(42, arena2_message->unknown_fields().field(0).varint());
+}
+
+TEST(ArenaTest, SetAllocatedMessage) {
+ Arena arena;
+ TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&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;
+ EXPECT_FALSE(no_arena_message.has_arena_message());
+ no_arena_message.set_allocated_arena_message(NULL);
+ EXPECT_FALSE(no_arena_message.has_arena_message());
+ no_arena_message.set_allocated_arena_message(new ArenaMessage);
+ EXPECT_TRUE(no_arena_message.has_arena_message());
+}
+
+TEST(ArenaTest, ReleaseMessage) {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->mutable_optional_nested_message()->set_bb(118);
+ scoped_ptr<TestAllTypes::NestedMessage> nested(
+ arena_message->release_optional_nested_message());
+ EXPECT_EQ(118, nested->bb());
+
+ TestAllTypes::NestedMessage* released_null =
+ arena_message->release_optional_nested_message();
+ EXPECT_EQ(NULL, released_null);
+}
+
+TEST(ArenaTest, SetAllocatedString) {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ string* allocated_str = new string("hello");
+ arena_message->set_allocated_optional_string(allocated_str);
+ EXPECT_EQ("hello", arena_message->optional_string());
+}
+
+TEST(ArenaTest, ReleaseString) {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->set_optional_string("hello");
+ scoped_ptr<string> released_str(
+ arena_message->release_optional_string());
+ EXPECT_EQ("hello", *released_str);
+
+ // Test default value.
+}
+
+
+TEST(ArenaTest, SwapBetweenArenasWithAllFieldsSet) {
+ Arena arena1;
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ {
+ Arena arena2;
+ TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ TestUtil::SetAllFields(arena2_message);
+ arena2_message->Swap(arena1_message);
+ string output;
+ arena2_message->SerializeToString(&output);
+ EXPECT_EQ(0, output.size());
+ }
+ TestUtil::ExpectAllFieldsSet(*arena1_message);
+}
+
+TEST(ArenaTest, SwapBetweenArenaAndNonArenaWithAllFieldsSet) {
+ TestAllTypes non_arena_message;
+ TestUtil::SetAllFields(&non_arena_message);
+ {
+ Arena arena2;
+ TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ TestUtil::SetAllFields(arena2_message);
+ arena2_message->Swap(&non_arena_message);
+ TestUtil::ExpectAllFieldsSet(*arena2_message);
+ TestUtil::ExpectAllFieldsSet(non_arena_message);
+ }
+}
+
+TEST(ArenaTest, UnsafeArenaSwap) {
+ Arena shared_arena;
+ TestAllTypes* message1 = Arena::CreateMessage<TestAllTypes>(&shared_arena);
+ TestAllTypes* message2 = Arena::CreateMessage<TestAllTypes>(&shared_arena);
+ TestUtil::SetAllFields(message1);
+ message1->UnsafeArenaSwap(message2);
+ TestUtil::ExpectAllFieldsSet(*message2);
+}
+
+TEST(ArenaTest, SwapBetweenArenasUsingReflection) {
+ Arena arena1;
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ {
+ Arena arena2;
+ TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ TestUtil::SetAllFields(arena2_message);
+ const Reflection* r = arena2_message->GetReflection();
+ r->Swap(arena1_message, arena2_message);
+ string output;
+ arena2_message->SerializeToString(&output);
+ EXPECT_EQ(0, output.size());
+ }
+ TestUtil::ExpectAllFieldsSet(*arena1_message);
+}
+
+TEST(ArenaTest, SwapBetweenArenaAndNonArenaUsingReflection) {
+ TestAllTypes non_arena_message;
+ TestUtil::SetAllFields(&non_arena_message);
+ {
+ Arena arena2;
+ TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ TestUtil::SetAllFields(arena2_message);
+ const Reflection* r = arena2_message->GetReflection();
+ r->Swap(&non_arena_message, arena2_message);
+ TestUtil::ExpectAllFieldsSet(*arena2_message);
+ TestUtil::ExpectAllFieldsSet(non_arena_message);
+ }
+}
+
+TEST(ArenaTest, ReleaseFromArenaMessageMakesCopy) {
+ TestAllTypes::NestedMessage* nested_msg = NULL;
+ string* nested_string = NULL;
+ {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->mutable_optional_nested_message()->set_bb(42);
+ *arena_message->mutable_optional_string() = "Hello";
+ nested_msg = arena_message->release_optional_nested_message();
+ nested_string = arena_message->release_optional_string();
+ }
+ EXPECT_EQ(42, nested_msg->bb());
+ EXPECT_EQ("Hello", *nested_string);
+ delete nested_msg;
+ delete nested_string;
+}
+
+TEST(ArenaTest, ReleaseFromArenaMessageUsingReflectionMakesCopy) {
+ TestAllTypes::NestedMessage* nested_msg = NULL;
+ // Note: no string: reflection API only supports releasing submessages.
+ {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->mutable_optional_nested_message()->set_bb(42);
+ const Reflection* r = arena_message->GetReflection();
+ const FieldDescriptor* f = arena_message->GetDescriptor()->FindFieldByName(
+ "optional_nested_message");
+ nested_msg = dynamic_cast<TestAllTypes::NestedMessage*>(
+ r->ReleaseMessage(arena_message, f));
+ }
+ EXPECT_EQ(42, nested_msg->bb());
+ delete nested_msg;
+}
+
+TEST(ArenaTest, UnsafeArenaReleaseDoesNotMakeCopy) {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ TestAllTypes::NestedMessage* nested_msg = NULL;
+ TestAllTypes::NestedMessage* orig_nested_msg = NULL;
+ string* nested_string = NULL;
+ string* orig_nested_string = NULL;
+ arena_message->mutable_optional_nested_message()->set_bb(42);
+ *arena_message->mutable_optional_string() = "Hello";
+ orig_nested_msg = arena_message->mutable_optional_nested_message();
+ orig_nested_string = arena_message->mutable_optional_string();
+ nested_msg = arena_message->unsafe_arena_release_optional_nested_message();
+ nested_string = arena_message->unsafe_arena_release_optional_string();
+
+ EXPECT_EQ(orig_nested_msg, nested_msg);
+ EXPECT_EQ(orig_nested_string, nested_string);
+ // Released pointers still on arena; no 'delete' calls needed here.
+}
+
+TEST(ArenaTest, SetAllocatedAcrossArenas) {
+ Arena arena1;
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ TestAllTypes::NestedMessage* heap_submessage =
+ new TestAllTypes::NestedMessage();
+ 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());
+ {
+ Arena arena2;
+ TestAllTypes::NestedMessage* arena2_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
+ arena2_submessage->set_bb(42);
+ arena1_message->set_allocated_optional_nested_message(arena2_submessage);
+ EXPECT_NE(arena2_submessage,
+ arena1_message->mutable_optional_nested_message());
+ }
+
+ TestAllTypes::NestedMessage* arena1_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena1);
+ 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());
+ delete heap_message;
+}
+
+TEST(ArenaTest, SetAllocatedAcrossArenasWithReflection) {
+ // Same as above, with reflection.
+ Arena arena1;
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ const Reflection* r = arena1_message->GetReflection();
+ const Descriptor* d = arena1_message->GetDescriptor();
+ 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());
+ {
+ Arena arena2;
+ TestAllTypes::NestedMessage* arena2_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
+ arena2_submessage->set_bb(42);
+ r->SetAllocatedMessage(arena1_message, arena2_submessage, msg_field);
+ EXPECT_NE(arena2_submessage,
+ arena1_message->mutable_optional_nested_message());
+ }
+
+ TestAllTypes::NestedMessage* arena1_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena1);
+ 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());
+ delete heap_message;
+}
+
+TEST(ArenaTest, AddAllocatedWithReflection) {
+ Arena arena1;
+ ArenaMessage* arena1_message = Arena::CreateMessage<ArenaMessage>(&arena1);
+ const Reflection* r = arena1_message->GetReflection();
+ const Descriptor* d = arena1_message->GetDescriptor();
+ const FieldDescriptor* fd =
+ d->FindFieldByName("repeated_import_no_arena_message");
+ // Message with cc_enable_arenas = false;
+ r->AddMessage(arena1_message, fd);
+ r->AddMessage(arena1_message, fd);
+ r->AddMessage(arena1_message, fd);
+ EXPECT_EQ(3, r->FieldSize(*arena1_message, fd));
+ // Message with cc_enable_arenas = true;
+ fd = d->FindFieldByName("repeated_nested_message");
+ r->AddMessage(arena1_message, fd);
+ r->AddMessage(arena1_message, fd);
+ r->AddMessage(arena1_message, fd);
+ EXPECT_EQ(3, r->FieldSize(*arena1_message, fd));
+}
+
+TEST(ArenaTest, RepeatedPtrFieldAddClearedTest) {
+ {
+ RepeatedPtrField<TestAllTypes> repeated_field;
+ EXPECT_TRUE(repeated_field.empty());
+ EXPECT_EQ(0, repeated_field.size());
+ // Ownership is passed to repeated_field.
+ TestAllTypes* cleared = new TestAllTypes();
+ repeated_field.AddCleared(cleared);
+ EXPECT_TRUE(repeated_field.empty());
+ EXPECT_EQ(0, repeated_field.size());
+ }
+ {
+ RepeatedPtrField<TestAllTypes> repeated_field;
+ EXPECT_TRUE(repeated_field.empty());
+ EXPECT_EQ(0, repeated_field.size());
+ // Ownership is passed to repeated_field.
+ TestAllTypes* cleared = new TestAllTypes();
+ repeated_field.AddAllocated(cleared);
+ EXPECT_FALSE(repeated_field.empty());
+ EXPECT_EQ(1, repeated_field.size());
+ }
+}
+
+// N.B.: no reflection version of this test because all the arena-specific code
+// is in RepeatedPtrField, and the reflection works implicitly based on that.
+TEST(ArenaTest, AddAllocatedToRepeatedField) {
+ // Heap->arena case.
+ Arena arena1;
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ for (int i = 0; i < 10; i++) {
+ TestAllTypes::NestedMessage* heap_submessage =
+ new TestAllTypes::NestedMessage();
+ heap_submessage->set_bb(42);
+ 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(42, arena1_message->repeated_nested_message(i).bb());
+ }
+
+ // Arena1->Arena2 case.
+ arena1_message->Clear();
+ for (int i = 0; i < 10; i++) {
+ Arena arena2;
+ TestAllTypes::NestedMessage* arena2_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
+ arena2_submessage->set_bb(42);
+ arena1_message->mutable_repeated_nested_message()->
+ AddAllocated(arena2_submessage);
+ // Should copy object.
+ EXPECT_NE(arena2_submessage,
+ &arena1_message->repeated_nested_message(i));
+ EXPECT_EQ(42, arena1_message->repeated_nested_message(i).bb());
+ }
+
+ // Arena->heap case.
+ TestAllTypes* heap_message = new TestAllTypes();
+ for (int i = 0; i < 10; i++) {
+ Arena arena2;
+ TestAllTypes::NestedMessage* arena2_submessage =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena2);
+ arena2_submessage->set_bb(42);
+ heap_message->mutable_repeated_nested_message()->
+ AddAllocated(arena2_submessage);
+ // Should copy object.
+ EXPECT_NE(arena2_submessage,
+ &heap_message->repeated_nested_message(i));
+ EXPECT_EQ(42, heap_message->repeated_nested_message(i).bb());
+ }
+ delete heap_message;
+
+ // 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);
+ // Should not copy.
+ EXPECT_EQ(s, &arena1_message->repeated_string(i));
+ EXPECT_EQ("Test", arena1_message->repeated_string(i));
+ }
+}
+
+TEST(ArenaTest, ReleaseLastRepeatedField) {
+ // Release from arena-allocated repeated field and ensure that returned object
+ // is heap-allocated.
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ for (int i = 0; i < 10; i++) {
+ TestAllTypes::NestedMessage* nested =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena);
+ nested->set_bb(42);
+ arena_message->mutable_repeated_nested_message()->AddAllocated(nested);
+ }
+
+ for (int i = 0; i < 10; i++) {
+ const TestAllTypes::NestedMessage *orig_submessage =
+ &arena_message->repeated_nested_message(10 - 1 - i); // last element
+ TestAllTypes::NestedMessage *released =
+ arena_message->mutable_repeated_nested_message()->ReleaseLast();
+ EXPECT_NE(released, orig_submessage);
+ EXPECT_EQ(42, released->bb());
+ delete released;
+ }
+
+ // Test UnsafeArenaReleaseLast().
+ for (int i = 0; i < 10; i++) {
+ TestAllTypes::NestedMessage* nested =
+ Arena::CreateMessage<TestAllTypes::NestedMessage>(&arena);
+ nested->set_bb(42);
+ arena_message->mutable_repeated_nested_message()->AddAllocated(nested);
+ }
+
+ for (int i = 0; i < 10; i++) {
+ 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();
+ 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).
+ arena_message->Clear();
+ for (int i = 0; i < 10; i++) {
+ string* s = new 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();
+ EXPECT_NE(released, orig_element);
+ EXPECT_EQ("Test", *released);
+ delete released;
+ }
+}
+
+TEST(ArenaTest, UnsafeArenaReleaseAdd) {
+ // Use unsafe_arena_release() and unsafe_arena_set_allocated() to transfer an
+ // arena-allocated string from one message to another.
+ Arena arena;
+ TestAllTypes* message1 = Arena::CreateMessage<TestAllTypes>(&arena);
+ TestAllTypes* message2 = Arena::CreateMessage<TestAllTypes>(&arena);
+ string* arena_string = Arena::Create<string>(&arena);
+ *arena_string = "Test content";
+
+ message1->unsafe_arena_set_allocated_optional_string(arena_string);
+ EXPECT_EQ(arena_string, message1->mutable_optional_string());
+ message2->unsafe_arena_set_allocated_optional_string(
+ message1->unsafe_arena_release_optional_string());
+ EXPECT_EQ(arena_string, message2->mutable_optional_string());
+}
+
+TEST(ArenaTest, UnsafeArenaAddAllocated) {
+ Arena arena;
+ TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
+ for (int i = 0; i < 10; i++) {
+ string* arena_string = Arena::Create<string>(&arena);
+ message->mutable_repeated_string()->UnsafeArenaAddAllocated(arena_string);
+ EXPECT_EQ(arena_string, message->mutable_repeated_string(i));
+ }
+}
+
+namespace {
+void TestSwapRepeatedField(Arena* arena1, Arena* arena2) {
+ // Test "safe" (copying) semantics for direct Swap() on RepeatedPtrField
+ // between arenas.
+ RepeatedPtrField<TestAllTypes> field1(arena1);
+ RepeatedPtrField<TestAllTypes> field2(arena2);
+ for (int i = 0; i < 10; i++) {
+ TestAllTypes* t = Arena::CreateMessage<TestAllTypes>(arena1);
+ t->set_optional_string("field1");
+ t->set_optional_int32(i);
+ if (arena1 != NULL) {
+ field1.UnsafeArenaAddAllocated(t);
+ } else {
+ field1.AddAllocated(t);
+ }
+ }
+ for (int i = 0; i < 5; i++) {
+ TestAllTypes* t = Arena::CreateMessage<TestAllTypes>(arena2);
+ t->set_optional_string("field2");
+ t->set_optional_int32(i);
+ if (arena2 != NULL) {
+ field2.UnsafeArenaAddAllocated(t);
+ } else {
+ field2.AddAllocated(t);
+ }
+ }
+ 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());
+ // Ensure that fields retained their original order:
+ for (int i = 0; i < field1.size(); i++) {
+ EXPECT_EQ(i, field1.Get(i).optional_int32());
+ }
+ for (int i = 0; i < field2.size(); i++) {
+ EXPECT_EQ(i, field2.Get(i).optional_int32());
+ }
+}
+} // namespace
+
+TEST(ArenaTest, SwapRepeatedField) {
+ Arena arena;
+ TestSwapRepeatedField(&arena, &arena);
+}
+
+TEST(ArenaTest, SwapRepeatedFieldWithDifferentArenas) {
+ Arena arena1;
+ Arena arena2;
+ TestSwapRepeatedField(&arena1, &arena2);
+}
+
+TEST(ArenaTest, SwapRepeatedFieldWithNoArenaOnRightHandSide) {
+ Arena arena;
+ TestSwapRepeatedField(&arena, NULL);
+}
+
+TEST(ArenaTest, SwapRepeatedFieldWithNoArenaOnLeftHandSide) {
+ Arena arena;
+ TestSwapRepeatedField(NULL, &arena);
+}
+
+TEST(ArenaTest, ExtensionsOnArena) {
+ Arena arena;
+ // Ensure no leaks.
+ TestAllExtensions* message_ext =
+ Arena::CreateMessage<TestAllExtensions>(&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);
+}
+
+class NoHeapChecker {
+ public:
+ NoHeapChecker() {
+ capture_alloc.Hook();
+ }
+ ~NoHeapChecker() {
+ capture_alloc.Unhook();
+ EXPECT_EQ(0, capture_alloc.alloc_count());
+ EXPECT_EQ(0, capture_alloc.free_count());
+ }
+ private:
+ class NewDeleteCapture {
+ public:
+ // TOOD(xiaofeng): Implement this for opensource protobuf.
+ void Hook() {}
+ void Unhook() {}
+ int alloc_count() { return 0; }
+ int free_count() { return 0; }
+ } capture_alloc;
+};
+
+TEST(ArenaTest, RepeatedFieldOnArena) {
+ // Preallocate an initial arena block to avoid mallocs during hooked region.
+ std::vector<char> arena_block(1024 * 1024);
+ ArenaOptions options;
+ options.initial_block = &arena_block[0];
+ options.initial_block_size = arena_block.size();
+ Arena arena(options);
+
+ {
+ NoHeapChecker no_heap;
+
+ // Fill some repeated fields on the arena to test for leaks. Also verify no
+ // memory allocations.
+ RepeatedField<int32> repeated_int32(&arena);
+ RepeatedPtrField<TestAllTypes> repeated_message(&arena);
+ for (int i = 0; i < 100; i++) {
+ repeated_int32.Add(42);
+ repeated_message.Add()->set_optional_int32(42);
+ EXPECT_EQ(&arena, repeated_message.Get(0).GetArena());
+ const TestAllTypes* msg_in_repeated_field = &repeated_message.Get(0);
+ TestAllTypes* msg = repeated_message.UnsafeArenaReleaseLast();
+ EXPECT_EQ(msg_in_repeated_field, msg);
+ }
+
+ // UnsafeArenaExtractSubrange (i) should not leak and (ii) should return
+ // on-arena pointers.
+ for (int i = 0; i < 10; i++) {
+ repeated_message.Add()->set_optional_int32(42);
+ }
+ TestAllTypes* extracted_messages[5];
+ repeated_message.UnsafeArenaExtractSubrange(0, 5, extracted_messages);
+ EXPECT_EQ(&arena, repeated_message.Get(0).GetArena());
+ EXPECT_EQ(5, repeated_message.size());
+ }
+
+ // Now, outside the scope of the NoHeapChecker, test ExtractSubrange's copying
+ // semantics.
+ {
+ RepeatedPtrField<TestAllTypes> repeated_message(&arena);
+ for (int i = 0; i < 100; i++) {
+ repeated_message.Add()->set_optional_int32(42);
+ }
+
+ TestAllTypes* extracted_messages[5];
+ // ExtractSubrange should copy to the heap.
+ repeated_message.ExtractSubrange(0, 5, extracted_messages);
+ EXPECT_EQ(NULL, extracted_messages[0]->GetArena());
+ // We need to free the heap-allocated messages to prevent a leak.
+ for (int i = 0; i < 5; i++) {
+ delete extracted_messages[i];
+ extracted_messages[i] = NULL;
+ }
+ }
+
+ // Now check that we can create RepeatedFields/RepeatedPtrFields themselves on
+ // the arena. They have the necessary type traits so that they can behave like
+ // messages in this way. This is useful for higher-level generic templated
+ // code that may allocate messages or repeated fields of messages on an arena.
+ {
+ RepeatedPtrField<TestAllTypes>* repeated_ptr_on_arena =
+ Arena::CreateMessage< RepeatedPtrField<TestAllTypes> >(&arena);
+ for (int i = 0; i < 10; i++) {
+ // Add some elements and let the leak-checker ensure that everything is
+ // freed.
+ repeated_ptr_on_arena->Add();
+ }
+
+ RepeatedField<int>* repeated_int_on_arena =
+ Arena::CreateMessage< RepeatedField<int> >(&arena);
+ for (int i = 0; i < 100; i++) {
+ repeated_int_on_arena->Add(i);
+ }
+
+ }
+
+ arena.Reset();
+}
+
+
+TEST(ArenaTest, MutableMessageReflection) {
+ Arena arena;
+ TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
+ const Reflection* r = message->GetReflection();
+ const Descriptor* d = message->GetDescriptor();
+ const FieldDescriptor* field = d->FindFieldByName("optional_nested_message");
+ TestAllTypes::NestedMessage* submessage =
+ dynamic_cast<TestAllTypes::NestedMessage*>(
+ r->MutableMessage(message, field));
+ TestAllTypes::NestedMessage* submessage_expected =
+ message->mutable_optional_nested_message();
+
+ EXPECT_EQ(submessage_expected, submessage);
+ EXPECT_EQ(&arena, submessage->GetArena());
+
+ const FieldDescriptor* oneof_field = d->FindFieldByName("oneof_nested_message");
+ submessage = dynamic_cast<TestAllTypes::NestedMessage*>(
+ r->MutableMessage(message, oneof_field));
+ submessage_expected = message->mutable_oneof_nested_message();
+
+ EXPECT_EQ(submessage_expected, submessage);
+ EXPECT_EQ(&arena, submessage->GetArena());
+}
+
+
+namespace {
+
+void FillArenaAwareFields(TestAllTypes* message) {
+ string test_string = "hello world";
+ message->set_optional_int32(42);
+ message->set_optional_string(test_string);
+ message->set_optional_bytes(test_string);
+ message->mutable_optional_nested_message()->set_bb(42);
+
+ message->set_oneof_uint32(42);
+ message->mutable_oneof_nested_message()->set_bb(42);
+ message->set_oneof_string(test_string);
+ message->set_oneof_bytes(test_string);
+
+ message->add_repeated_int32(42);
+ // No repeated string: not yet arena-aware.
+ message->add_repeated_nested_message()->set_bb(42);
+ message->mutable_optional_lazy_message()->set_bb(42);
+}
+
+}
+
+// Test: no allocations occur on heap while touching all supported field types.
+TEST(ArenaTest, NoHeapAllocationsTest) {
+ // Allocate a large initial block to avoid mallocs during hooked test.
+ std::vector<char> arena_block(128 * 1024);
+ ArenaOptions options;
+ options.initial_block = &arena_block[0];
+ options.initial_block_size = arena_block.size();
+ Arena arena(options);
+
+ {
+
+ TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
+ FillArenaAwareFields(message);
+ }
+
+ arena.Reset();
+}
+
+
+// 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.
+TEST(ArenaTest, MessageLiteOnArena) {
+ std::vector<char> arena_block(128 * 1024);
+ ArenaOptions options;
+ options.initial_block = &arena_block[0];
+ options.initial_block_size = arena_block.size();
+ Arena arena(options);
+ const google::protobuf::MessageLite* prototype = dynamic_cast<
+ const google::protobuf::MessageLite*>(&TestAllTypes::default_instance());
+
+ TestAllTypes initial_message;
+ FillArenaAwareFields(&initial_message);
+ string serialized;
+ initial_message.SerializeToString(&serialized);
+
+ {
+
+ google::protobuf::MessageLite* generic_message = prototype->New(&arena);
+ EXPECT_TRUE(generic_message != NULL);
+ EXPECT_EQ(&arena, generic_message->GetArena());
+ EXPECT_TRUE(generic_message->ParseFromString(serialized));
+ TestAllTypes* deserialized = dynamic_cast<TestAllTypes*>(generic_message);
+ EXPECT_TRUE(deserialized != NULL);
+ EXPECT_EQ(42, deserialized->optional_int32());
+ }
+
+ arena.Reset();
+}
+
+
+// RepeatedField should support non-POD types, and invoke constructors and
+// destructors appropriately, because it's used this way by lots of other code
+// (even if this was not its original intent).
+TEST(ArenaTest, RepeatedFieldWithNonPODType) {
+ {
+ RepeatedField<string> 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<string> field_on_arena(&arena);
+ for (int i = 0; i < 100; i++) {
+ *field_on_arena.Add() = "test string long enough to exceed inline buffer";
+ }
+ }
+}
+
+TEST(ArenaTest, SpaceUsed) {
+ ArenaOptions options;
+ options.start_block_size = 256;
+ options.max_block_size = 8192;
+ Arena arena_1(options);
+ EXPECT_EQ(0, arena_1.SpaceUsed());
+ EXPECT_EQ(0, arena_1.Reset());
+ ::google::protobuf::Arena::CreateArray<char>(&arena_1, 320);
+ // Arena will allocate slightly more than 320 for the block headers.
+ EXPECT_LE(320, arena_1.SpaceUsed());
+ EXPECT_LE(320, arena_1.Reset());
+
+ // Test with initial block.
+ std::vector<char> arena_block(1024);
+ options.initial_block = &arena_block[0];
+ options.initial_block_size = arena_block.size();
+ Arena arena_2(options);
+ EXPECT_EQ(1024, arena_2.SpaceUsed());
+ EXPECT_EQ(1024, arena_2.Reset());
+ ::google::protobuf::Arena::CreateArray<char>(&arena_2, 55);
+ EXPECT_EQ(1024, arena_2.SpaceUsed());
+ EXPECT_EQ(1024, arena_2.Reset());
+
+ // Reset options to test doubling policy explicitly.
+ options.initial_block = NULL;
+ options.initial_block_size = 0;
+ Arena arena_3(options);
+ ::google::protobuf::Arena::CreateArray<char>(&arena_3, 190);
+ EXPECT_EQ(256, arena_3.SpaceUsed());
+ ::google::protobuf::Arena::CreateArray<char>(&arena_3, 70);
+ EXPECT_EQ(256 + 512, arena_3.SpaceUsed());
+ EXPECT_EQ(256 + 512, arena_3.Reset());
+}
+
+TEST(ArenaTest, Alignment) {
+ ::google::protobuf::Arena arena;
+ for (int i = 0; i < 200; i++) {
+ void* p = ::google::protobuf::Arena::CreateArray<char>(&arena, i);
+ GOOGLE_CHECK_EQ(reinterpret_cast<uintptr_t>(p) % 8, 0) << i << ": " << p;
+ }
+}
+
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/arenastring.cc b/src/google/protobuf/arenastring.cc
new file mode 100644
index 00000000..cce61d74
--- /dev/null
+++ b/src/google/protobuf/arenastring.cc
@@ -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.
+
+// The ArenaString implementation is not included in the open-source release. Do
+// not include this file in the distribution.
+
+#include <google/protobuf/arenastring.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+
+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.
+ if (me != other) {
+ SetNoArena(default_value, value.GetNoArena(default_value));
+ }
+}
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/arenastring.h b/src/google/protobuf/arenastring.h
new file mode 100755
index 00000000..d829ed91
--- /dev/null
+++ b/src/google/protobuf/arenastring.h
@@ -0,0 +1,315 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_ARENASTRING_H__
+#define GOOGLE_PROTOBUF_ARENASTRING_H__
+
+#include <string>
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/fastmem.h>
+
+#include <google/protobuf/arena.h>
+#include <google/protobuf/generated_message_util.h>
+
+
+
+// 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.
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+struct LIBPROTOBUF_EXPORT ArenaStringPtr {
+ inline void Set(const ::std::string* default_value,
+ const ::std::string& value, ::google::protobuf::Arena* arena) {
+ if (ptr_ == default_value) {
+ CreateInstance(arena, &value);
+ } else {
+ *ptr_ = value;
+ }
+ }
+
+ // Basic accessors.
+ inline const ::std::string& Get(const ::std::string* default_value) const {
+ return *ptr_;
+ }
+
+ inline ::std::string* Mutable(const ::std::string* default_value,
+ ::google::protobuf::Arena* arena) {
+ if (ptr_ == default_value) {
+ CreateInstance(arena, default_value);
+ }
+ return ptr_;
+ }
+
+ // Release returns a ::std::string* instance that is heap-allocated and is not
+ // Own()'d by any arena. If the field was not set, it returns NULL. The caller
+ // retains ownership. Clears this field back to NULL state. Used to implement
+ // release_<field>() methods on generated classes.
+ inline ::std::string* Release(const ::std::string* default_value,
+ ::google::protobuf::Arena* arena) {
+ if (ptr_ == default_value) {
+ return NULL;
+ }
+ ::std::string* released = NULL;
+ if (arena != NULL) {
+ // ptr_ is owned by the arena -- we need to return a copy.
+ released = new ::std::string(*ptr_);
+ } else {
+ released = ptr_;
+ }
+ 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_<field>() methods on
+ // generated classes.
+ inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value,
+ ::google::protobuf::Arena* arena) {
+ if (ptr_ == default_value) {
+ return NULL;
+ }
+ ::std::string* released = ptr_;
+ ptr_ = const_cast< ::std::string* >(default_value);
+ return released;
+ }
+
+ // Takes a string that is heap-allocated, and takes ownership. The string's
+ // destructor is registered with the arena. Used to implement
+ // set_allocated_<field> in generated classes.
+ inline void SetAllocated(const ::std::string* default_value,
+ ::std::string* value, ::google::protobuf::Arena* arena) {
+ if (arena == NULL && ptr_ != default_value) {
+ Destroy(default_value, arena);
+ }
+ if (value != NULL) {
+ ptr_ = value;
+ if (arena != NULL) {
+ arena->Own(value);
+ }
+ } else {
+ ptr_ = const_cast< ::std::string* >(default_value);
+ }
+ }
+
+ // Takes a string that has lifetime equal to the arena's lifetime. The arena
+ // must be non-null. It is safe only to pass this method a value returned by
+ // UnsafeArenaRelease() on another field of a message in the same arena. Used
+ // to implement unsafe_arena_set_allocated_<field> in generated classes.
+ inline void UnsafeArenaSetAllocated(const ::std::string* default_value,
+ ::std::string* value, ::google::protobuf::Arena* arena) {
+ if (value != NULL) {
+ ptr_ = value;
+ } else {
+ 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.
+ inline void Swap(ArenaStringPtr* other) GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ std::swap(ptr_, other->ptr_);
+ }
+
+ // Frees storage (if not on an arena) and sets field to default value.
+ inline void Destroy(const ::std::string* default_value,
+ ::google::protobuf::Arena* arena) {
+ if (arena == NULL && ptr_ != default_value) {
+ delete ptr_;
+ }
+ ptr_ = const_cast< ::std::string* >(default_value);
+ }
+
+ // 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 the empty string. Assumes that |default_value|
+ // is an empty string.
+ inline void ClearToEmpty(const ::std::string* default_value,
+ ::google::protobuf::Arena* arena) {
+ if (ptr_ == default_value) {
+ // Already set to default (which is empty) -- do nothing.
+ } else {
+ 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) {
+ if (ptr_ == default_value) {
+ // Already set to default -- do nothing.
+ } else {
+ // Have another allocated string -- rather than throwing this away and
+ // resetting ptr_ to the canonical default string instance, we just reuse
+ // this instance.
+ *ptr_ = *default_value;
+ }
+ }
+
+ // Called from generated code / reflection runtime only. Resets value to point
+ // to a default string pointer, with the semantics that this ArenaStringPtr
+ // does not own the pointed-to memory. Disregards initial value of ptr_ (so
+ // this is the *ONLY* safe method to call after construction or when
+ // reinitializing after becoming the active field in a oneof union).
+ inline void UnsafeSetDefault(const ::std::string* default_value) {
+ // 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);
+ }
+
+ // The 'NoArena' variants of methods below assume arena == NULL and are
+ // optimized to provide very little overhead relative to a raw string pointer
+ // (while still being in-memory compatible with other code that assumes
+ // ArenaStringPtr). Note the invariant that a class instance that has only
+ // ever been mutated by NoArena methods must *only* be in the String state
+ // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all
+ // tagged-pointer manipulations to be avoided.
+ inline void SetNoArena(const ::std::string* default_value,
+ const ::std::string& value) {
+ if (ptr_ == default_value) {
+ CreateInstanceNoArena(&value);
+ } else {
+ *ptr_ = value;
+ }
+ }
+
+ void AssignWithDefault(const ::std::string* default_value, ArenaStringPtr value);
+
+ inline const ::std::string& GetNoArena(const ::std::string* default_value) const {
+ return *ptr_;
+ }
+
+ inline ::std::string* MutableNoArena(const ::std::string* default_value) {
+ if (ptr_ == default_value) {
+ CreateInstanceNoArena(default_value);
+ }
+ return ptr_;
+ }
+
+ inline ::std::string* ReleaseNoArena(const ::std::string* default_value) {
+ if (ptr_ == default_value) {
+ return NULL;
+ } else {
+ ::std::string* released = ptr_;
+ ptr_ = const_cast< ::std::string* >(default_value);
+ return released;
+ }
+ }
+
+ inline void SetAllocatedNoArena(const ::std::string* default_value,
+ ::std::string* value) {
+ if (ptr_ != default_value) {
+ delete ptr_;
+ }
+ if (value != NULL) {
+ ptr_ = value;
+ } else {
+ ptr_ = const_cast< ::std::string* >(default_value);
+ }
+ }
+
+ inline void DestroyNoArena(const ::std::string* default_value) {
+ if (ptr_ != default_value) {
+ delete ptr_;
+ }
+ ptr_ = NULL;
+ }
+
+ inline void ClearToEmptyNoArena(const ::std::string* default_value) {
+ if (ptr_ == default_value) {
+ // Nothing: already equal to default (which is the empty string).
+ } else {
+ ptr_->clear();
+ }
+ }
+
+ inline void ClearToDefaultNoArena(const ::std::string* default_value) {
+ if (ptr_ == default_value) {
+ // Nothing: already set to default.
+ } else {
+ // Reuse existing allocated instance.
+ *ptr_ = *default_value;
+ }
+ }
+
+ // Internal accessor used only at parse time to provide direct access to the
+ // 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_;
+ }
+
+ private:
+ ::std::string* ptr_;
+
+ inline void CreateInstance(::google::protobuf::Arena* arena,
+ const ::std::string* initial_value)
+ GOOGLE_ATTRIBUTE_NOINLINE {
+ // Assumes ptr_ is not NULL.
+ if (initial_value != NULL) {
+ ptr_ = new ::std::string(*initial_value);
+ } else {
+ ptr_ = new ::std::string();
+ }
+ if (arena != NULL) {
+ arena->Own(ptr_);
+ }
+ }
+ inline void CreateInstanceNoArena(const ::std::string* initial_value)
+ GOOGLE_ATTRIBUTE_NOINLINE {
+ if (initial_value != NULL) {
+ ptr_ = new ::std::string(*initial_value);
+ } else {
+ ptr_ = new ::std::string();
+ }
+ }
+};
+
+} // namespace internal
+} // namespace protobuf
+
+
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_ARENASTRING_H__
diff --git a/src/google/protobuf/arenastring_unittest.cc b/src/google/protobuf/arenastring_unittest.cc
new file mode 100644
index 00000000..8ebd4b9c
--- /dev/null
+++ b/src/google/protobuf/arenastring_unittest.cc
@@ -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.
+
+// arenastring_unittest.cc is not open-sourced. Do not include in open-source
+// distribution.
+
+// Based on mvels@'s frankenstring.
+
+#include <google/protobuf/arenastring.h>
+
+#include <string>
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+#include <cstdlib>
+
+#include <google/protobuf/stubs/common.h>
+#include <gtest/gtest.h>
+
+namespace google {
+using google::protobuf::internal::ArenaString;
+using google::protobuf::internal::ArenaStringPtr;
+
+namespace protobuf {
+
+
+static string WrapString(const char* value) {
+ return value;
+}
+
+// Test ArenaStringPtr with arena == NULL.
+TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) {
+ ArenaStringPtr field;
+ ::std::string default_value = "default";
+ field.UnsafeSetDefault(&default_value);
+ EXPECT_EQ(string("default"), field.Get(&default_value));
+ field.Set(&default_value, WrapString("Test short"), NULL);
+ EXPECT_EQ(string("Test short"), field.Get(&default_value));
+ field.Set(&default_value, WrapString("Test long long long long value"), NULL);
+ EXPECT_EQ(string("Test long long long long value"), field.Get(&default_value));
+ field.Set(&default_value, string(""), NULL);
+ field.Destroy(&default_value, NULL);
+
+ ArenaStringPtr field2;
+ field2.UnsafeSetDefault(&default_value);
+ ::std::string* mut = field2.Mutable(&default_value, NULL);
+ EXPECT_EQ(mut, field2.Mutable(&default_value, NULL));
+ EXPECT_EQ(mut, &field2.Get(&default_value));
+ EXPECT_NE(&default_value, mut);
+ EXPECT_EQ(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(&default_value));
+ field2.Destroy(&default_value, NULL);
+}
+
+TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) {
+ google::protobuf::Arena arena;
+ ArenaStringPtr field;
+ ::std::string default_value = "default";
+ field.UnsafeSetDefault(&default_value);
+ EXPECT_EQ(string("default"), field.Get(&default_value));
+ field.Set(&default_value, WrapString("Test short"), &arena);
+ EXPECT_EQ(string("Test short"), field.Get(&default_value));
+ field.Set(&default_value, WrapString("Test long long long long value"), &arena);
+ EXPECT_EQ(string("Test long long long long value"),
+ field.Get(&default_value));
+ field.Set(&default_value, string(""), &arena);
+ field.Destroy(&default_value, &arena);
+
+ ArenaStringPtr field2;
+ field2.UnsafeSetDefault(&default_value);
+ ::std::string* mut = field2.Mutable(&default_value, &arena);
+ EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
+ EXPECT_EQ(mut, &field2.Get(&default_value));
+ EXPECT_NE(&default_value, mut);
+ EXPECT_EQ(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(&default_value));
+ field2.Destroy(&default_value, &arena);
+}
+
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc
index f9f27bd8..13250702 100644
--- a/src/google/protobuf/compiler/command_line_interface.cc
+++ b/src/google/protobuf/compiler/command_line_interface.cc
@@ -50,6 +50,9 @@
#include <google/protobuf/stubs/hash.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stringprintf.h>
@@ -297,7 +300,7 @@ class CommandLineInterface::MemoryOutputStream
bool append_mode_;
// StringOutputStream writing to data_.
- scoped_ptr<io::StringOutputStream> inner_;
+ google::protobuf::scoped_ptr<io::StringOutputStream> inner_;
};
// -------------------------------------------------------------------
@@ -1318,7 +1321,7 @@ bool CommandLineInterface::GeneratePluginOutput(
// Write the files. We do this even if there was a generator error in order
// to match the behavior of a compiled-in generator.
- scoped_ptr<io::ZeroCopyOutputStream> current_output;
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> current_output;
for (int i = 0; i < response.file_size(); i++) {
const CodeGeneratorResponse::File& output_file = response.file(i);
@@ -1367,7 +1370,7 @@ bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) {
}
DynamicMessageFactory dynamic_factory(pool);
- scoped_ptr<Message> message(dynamic_factory.GetPrototype(type)->New());
+ google::protobuf::scoped_ptr<Message> message(dynamic_factory.GetPrototype(type)->New());
if (mode_ == MODE_ENCODE) {
SetFdToTextMode(STDIN_FILENO);
diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h
index 47f28919..74a0adb4 100644
--- a/src/google/protobuf/compiler/command_line_interface.h
+++ b/src/google/protobuf/compiler/command_line_interface.h
@@ -214,7 +214,7 @@ class LIBPROTOC_EXPORT CommandLineInterface {
// true if the next argument in the argv should be used as the value,
// false otherwise.
//
- // Exmaples:
+ // Examples:
// "-Isrc/protos" ->
// name = "-I", value = "src/protos"
// "--cpp_out=src/foo.pb2.cc" ->
diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc
index 6bf7357b..dbaaa405 100644
--- a/src/google/protobuf/compiler/command_line_interface_unittest.cc
+++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc
@@ -41,6 +41,9 @@
#include <unistd.h>
#endif
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <vector>
#include <google/protobuf/descriptor.pb.h>
@@ -48,6 +51,7 @@
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/code_generator.h>
+#include <google/protobuf/testing/file.h>
#include <google/protobuf/compiler/mock_code_generator.h>
#include <google/protobuf/compiler/subprocess.h>
#include <google/protobuf/io/printer.h>
@@ -59,6 +63,7 @@
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
+
namespace google {
namespace protobuf {
namespace compiler {
@@ -77,6 +82,10 @@ namespace compiler {
namespace {
+bool FileExists(const string& path) {
+ return File::Exists(path);
+}
+
class CommandLineInterfaceTest : public testing::Test {
protected:
virtual void SetUp();
@@ -226,7 +235,7 @@ void CommandLineInterfaceTest::SetUp() {
// If the temp directory already exists, it must be left over from a
// previous run. Delete it.
- if (File::Exists(temp_directory_)) {
+ if (FileExists(temp_directory_)) {
File::DeleteRecursively(temp_directory_, NULL, NULL);
}
@@ -252,7 +261,9 @@ void CommandLineInterfaceTest::SetUp() {
void CommandLineInterfaceTest::TearDown() {
// Delete the temp directory.
- File::DeleteRecursively(temp_directory_, NULL, NULL);
+ if (FileExists(temp_directory_)) {
+ File::DeleteRecursively(temp_directory_, NULL, NULL);
+ }
// Delete all the MockCodeGenerators.
for (int i = 0; i < mock_generators_to_delete_.size(); i++) {
@@ -301,7 +312,7 @@ void CommandLineInterfaceTest::Run(const string& command) {
}
}
- scoped_array<const char*> argv(new const char* [args.size()]);
+ google::protobuf::scoped_array<const char * > argv(new const char* [args.size()]);
for (int i = 0; i < args.size(); i++) {
args[i] = StringReplace(args[i], "$tmpdir", temp_directory_, true);
@@ -333,7 +344,7 @@ void CommandLineInterfaceTest::CreateTempFile(
string::size_type slash_pos = name.find_last_of('/');
if (slash_pos != string::npos) {
string dir = name.substr(0, slash_pos);
- if (!File::Exists(temp_directory_ + "/" + dir)) {
+ if (!FileExists(temp_directory_ + "/" + dir)) {
GOOGLE_CHECK_OK(File::RecursivelyCreateDir(temp_directory_ + "/" + dir,
0777));
}
@@ -714,7 +725,7 @@ TEST_F(CommandLineInterfaceTest, ColonDelimitedPath) {
#endif
Run("protocol_compiler --test_out=$tmpdir "
- "--proto_path=$tmpdir/a"PATH_SEPARATOR"$tmpdir/b foo.proto");
+ "--proto_path=$tmpdir/a" PATH_SEPARATOR "$tmpdir/b foo.proto");
#undef PATH_SEPARATOR
@@ -1539,7 +1550,7 @@ class EncodeDecodeTest : public testing::Test {
SplitStringUsing(command, " ", &args);
args.push_back("--proto_path=" + TestSourceDir());
- scoped_array<const char*> argv(new const char* [args.size()]);
+ google::protobuf::scoped_array<const char * > argv(new const char* [args.size()]);
for (int i = 0; i < args.size(); i++) {
argv[i] = args[i].c_str();
}
diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc
index c31cb5b3..3ce1f120 100644
--- a/src/google/protobuf/compiler/cpp/cpp_enum.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc
@@ -101,6 +101,15 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
}
}
+ if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ // For new enum semantics: generate min and max sentinel values equal to
+ // INT32_MIN and INT32_MAX
+ if (descriptor_->value_count() > 0) printer->Print(",\n");
+ printer->Print(vars,
+ "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32min,\n"
+ "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32max");
+ }
+
printer->Outdent();
printer->Print("\n};\n");
diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc
index 20b18ade..17926135 100644
--- a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc
@@ -87,9 +87,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const {
" // @@protoc_insertion_point(field_get:$full_name$)\n"
" return static_cast< $type$ >($name$_);\n"
"}\n"
- "inline void $classname$::set_$name$($type$ value) {\n"
- " assert($type$_IsValid(value));\n"
- " set_has_$name$();\n"
+ "inline void $classname$::set_$name$($type$ value) {\n");
+ if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " assert($type$_IsValid(value));\n");
+ }
+ printer->Print(variables_,
+ " $set_hasbit$\n"
" $name$_ = value;\n"
" // @@protoc_insertion_point(field_set:$full_name$)\n"
"}\n");
@@ -121,21 +125,27 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
"int value;\n"
"DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
" int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
- " input, &value)));\n"
- "if ($type$_IsValid(value)) {\n"
- " set_$name$(static_cast< $type$ >(value));\n");
- if (UseUnknownFieldSet(descriptor_->file())) {
+ " input, &value)));\n");
+ if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
printer->Print(variables_,
- "} else {\n"
- " mutable_unknown_fields()->AddVarint($number$, value);\n");
+ "set_$name$(static_cast< $type$ >(value));\n");
} else {
- printer->Print(
- "} else {\n"
- " unknown_fields_stream.WriteVarint32(tag);\n"
- " unknown_fields_stream.WriteVarint32(value);\n");
+ printer->Print(variables_,
+ "if ($type$_IsValid(value)) {\n"
+ " set_$name$(static_cast< $type$ >(value));\n");
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(variables_,
+ "} else {\n"
+ " mutable_unknown_fields()->AddVarint($number$, value);\n");
+ } else {
+ printer->Print(
+ "} else {\n"
+ " unknown_fields_stream.WriteVarint32(tag);\n"
+ " unknown_fields_stream.WriteVarint32(value);\n");
+ }
+ printer->Print(variables_,
+ "}\n");
}
- printer->Print(variables_,
- "}\n");
}
void EnumFieldGenerator::
@@ -174,18 +184,24 @@ void EnumOneofFieldGenerator::
GenerateInlineAccessorDefinitions(io::Printer* printer) const {
printer->Print(variables_,
"inline $type$ $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
" if (has_$name$()) {\n"
" return static_cast< $type$ >($oneof_prefix$$name$_);\n"
" }\n"
" return static_cast< $type$ >($default$);\n"
"}\n"
- "inline void $classname$::set_$name$($type$ value) {\n"
- " assert($type$_IsValid(value));\n"
+ "inline void $classname$::set_$name$($type$ value) {\n");
+ if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " assert($type$_IsValid(value));\n");
+ }
+ printer->Print(variables_,
" if (!has_$name$()) {\n"
" clear_$oneof_name$();\n"
" set_has_$name$();\n"
" }\n"
" $oneof_prefix$$name$_ = value;\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
"}\n");
}
@@ -245,13 +261,21 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const {
" // @@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"
- " assert($type$_IsValid(value));\n"
+ "inline void $classname$::set_$name$(int index, $type$ value) {\n");
+ if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " assert($type$_IsValid(value));\n");
+ }
+ printer->Print(variables_,
" $name$_.Set(index, value);\n"
" // @@protoc_insertion_point(field_set:$full_name$)\n"
"}\n"
- "inline void $classname$::add_$name$($type$ value) {\n"
- " assert($type$_IsValid(value));\n"
+ "inline void $classname$::add_$name$($type$ value) {\n");
+ if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " assert($type$_IsValid(value));\n");
+ }
+ printer->Print(variables_,
" $name$_.Add(value);\n"
" // @@protoc_insertion_point(field_add:$full_name$)\n"
"}\n");
@@ -280,7 +304,7 @@ GenerateMergingCode(io::Printer* printer) const {
void RepeatedEnumFieldGenerator::
GenerateSwappingCode(io::Printer* printer) const {
- printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
+ printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
}
void RepeatedEnumFieldGenerator::
@@ -295,20 +319,26 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
"int value;\n"
"DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
" int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
- " input, &value)));\n"
- "if ($type$_IsValid(value)) {\n"
- " add_$name$(static_cast< $type$ >(value));\n");
- if (UseUnknownFieldSet(descriptor_->file())) {
+ " input, &value)));\n");
+ if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
printer->Print(variables_,
- "} else {\n"
- " mutable_unknown_fields()->AddVarint($number$, value);\n");
+ "add_$name$(static_cast< $type$ >(value));\n");
} else {
- printer->Print(
- "} else {\n"
- " unknown_fields_stream.WriteVarint32(tag);\n"
- " unknown_fields_stream.WriteVarint32(value);\n");
+ printer->Print(variables_,
+ "if ($type$_IsValid(value)) {\n"
+ " add_$name$(static_cast< $type$ >(value));\n");
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(variables_,
+ "} else {\n"
+ " mutable_unknown_fields()->AddVarint($number$, value);\n");
+ } else {
+ printer->Print(
+ "} else {\n"
+ " unknown_fields_stream.WriteVarint32(tag);\n"
+ " unknown_fields_stream.WriteVarint32(value);\n");
+ }
+ printer->Print("}\n");
}
- printer->Print("}\n");
}
void RepeatedEnumFieldGenerator::
@@ -318,8 +348,15 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
// rarely be executed.
printer->Print(variables_,
"DO_((::google::protobuf::internal::WireFormatLite::ReadPackedEnumNoInline(\n"
- " input,\n"
- " &$type$_IsValid,\n"
+ " input,\n");
+ if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " NULL,\n");
+ } else {
+ printer->Print(variables_,
+ " &$type$_IsValid,\n");
+ }
+ printer->Print(variables_,
" this->mutable_$name$())));\n");
} else {
printer->Print(variables_,
@@ -331,10 +368,17 @@ GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
" int value;\n"
" DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
" int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
- " input, &value)));\n"
+ " input, &value)));\n");
+ if (HasPreservingUnknownEnumSemantics(descriptor_->file())) {
+ printer->Print(variables_,
+ " add_$name$(static_cast< $type$ >(value));\n");
+ } else {
+ printer->Print(variables_,
" if ($type$_IsValid(value)) {\n"
" add_$name$(static_cast< $type$ >(value));\n"
- " }\n"
+ " }\n");
+ }
+ printer->Print(variables_,
"}\n"
"input->PopLimit(limit);\n");
}
diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.h b/src/google/protobuf/compiler/cpp/cpp_extension.h
index d354c16a..1c1caf1f 100644
--- a/src/google/protobuf/compiler/cpp/cpp_extension.h
+++ b/src/google/protobuf/compiler/cpp/cpp_extension.h
@@ -57,7 +57,7 @@ namespace cpp {
class ExtensionGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
- explicit ExtensionGenerator(const FieldDescriptor* desycriptor,
+ explicit ExtensionGenerator(const FieldDescriptor* descriptor,
const Options& options);
~ExtensionGenerator();
diff --git a/src/google/protobuf/compiler/cpp/cpp_field.cc b/src/google/protobuf/compiler/cpp/cpp_field.cc
index 1e9a40ac..43df1d88 100644
--- a/src/google/protobuf/compiler/cpp/cpp_field.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_field.cc
@@ -34,11 +34,15 @@
#include <google/protobuf/compiler/cpp/cpp_field.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
#include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
#include <google/protobuf/compiler/cpp/cpp_string_field.h>
#include <google/protobuf/compiler/cpp/cpp_enum_field.h>
+#include <google/protobuf/compiler/cpp/cpp_map_field.h>
#include <google/protobuf/compiler/cpp/cpp_message_field.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/wire_format.h>
@@ -62,18 +66,41 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor,
(*variables)["classname"] = ClassName(FieldScope(descriptor), false);
(*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
+ // non_null_ptr_to_name is usable only if has_$name$ is true. It yields a
+ // pointer that will not be NULL. Subclasses of FieldGenerator may set
+ // (*variables)["non_null_ptr_to_name"] differently.
+ (*variables)["non_null_ptr_to_name"] =
+ StrCat("&this->", FieldName(descriptor), "()");
+
(*variables)["tag_size"] = SimpleItoa(
WireFormat::TagSize(descriptor->number(), descriptor->type()));
(*variables)["deprecation"] = descriptor->options().deprecated()
? " PROTOBUF_DEPRECATED" : "";
(*variables)["cppget"] = "Get";
+
+ if (HasFieldPresence(descriptor->file())) {
+ (*variables)["set_hasbit"] =
+ "set_has_" + FieldName(descriptor) + "();";
+ (*variables)["clear_hasbit"] =
+ "clear_has_" + FieldName(descriptor) + "();";
+ } else {
+ (*variables)["set_hasbit"] = "";
+ (*variables)["clear_hasbit"] = "";
+ }
+
+ // By default, empty string, so that generic code used for both oneofs and
+ // singular fields can be written.
+ (*variables)["oneof_prefix"] = "";
}
void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor,
map<string, string>* variables) {
- (*variables)["oneof_prefix"] = descriptor->containing_oneof()->name() + "_.";
+ const string prefix = descriptor->containing_oneof()->name() + "_.";
+ (*variables)["oneof_prefix"] = prefix;
(*variables)["oneof_name"] = descriptor->containing_oneof()->name();
+ (*variables)["non_null_ptr_to_name"] =
+ StrCat(prefix, (*variables)["name"], "_");
}
FieldGenerator::~FieldGenerator() {}
@@ -94,7 +121,7 @@ FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor,
const Options& options)
: descriptor_(descriptor),
field_generators_(
- new scoped_ptr<FieldGenerator>[descriptor->field_count()]) {
+ new google::protobuf::scoped_ptr<FieldGenerator>[descriptor->field_count()]) {
// Construct all the FieldGenerators.
for (int i = 0; i < descriptor->field_count(); i++) {
field_generators_[i].reset(MakeGenerator(descriptor->field(i), options));
@@ -106,7 +133,11 @@ FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field,
if (field->is_repeated()) {
switch (field->cpp_type()) {
case FieldDescriptor::CPPTYPE_MESSAGE:
- return new RepeatedMessageFieldGenerator(field, options);
+ if (field->is_map()) {
+ return new MapFieldGenerator(field, options);
+ } else {
+ return new RepeatedMessageFieldGenerator(field, options);
+ }
case FieldDescriptor::CPPTYPE_STRING:
switch (field->options().ctype()) {
default: // RepeatedStringFieldGenerator handles unknown ctypes.
diff --git a/src/google/protobuf/compiler/cpp/cpp_field.h b/src/google/protobuf/compiler/cpp/cpp_field.h
index 96c29633..c37fe0be 100644
--- a/src/google/protobuf/compiler/cpp/cpp_field.h
+++ b/src/google/protobuf/compiler/cpp/cpp_field.h
@@ -37,6 +37,9 @@
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <google/protobuf/descriptor.h>
@@ -77,7 +80,7 @@ class FieldGenerator {
// Generate static default variable for this field. These are placed inside
// the message class. Most field types don't need this, so the default
// implementation is empty.
- virtual void GenerateStaticMembers(io::Printer* printer) const {}
+ virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
// Generate prototypes for all of the accessor functions related to this
// field. These are placed inside the class definition.
@@ -92,7 +95,7 @@ class FieldGenerator {
// placed somewhere in the .cc file.
// Most field types don't need this, so the default implementation is empty.
virtual void GenerateNonInlineAccessorDefinitions(
- io::Printer* printer) const {}
+ io::Printer* /*printer*/) const {}
// Generate lines of code (statements, not declarations) which clear the
// field. This is used to define the clear_$name$() method as well as
@@ -122,14 +125,25 @@ class FieldGenerator {
// Generate any code that needs to go in the class's SharedDtor() method,
// invoked by the destructor.
// Most field types don't need this, so the default implementation is empty.
- virtual void GenerateDestructorCode(io::Printer* printer) const {}
+ virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
+
+ // Generate a manual destructor invocation for use when the message is on an
+ // arena. The code that this method generates will be executed inside a
+ // shared-for-the-whole-message-class method registered with OwnDestructor().
+ // The method should return |true| if it generated any code that requires a
+ // call; this allows the message generator to eliminate the OwnDestructor()
+ // registration if no fields require it.
+ virtual bool GenerateArenaDestructorCode(io::Printer* printer) const {
+ return false;
+ }
// Generate code that allocates the fields's default instance.
- virtual void GenerateDefaultInstanceAllocator(io::Printer* printer) const {}
+ virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/)
+ const {}
// Generate code that should be run when ShutdownProtobufLibrary() is called,
// to delete all dynamically-allocated objects.
- virtual void GenerateShutdownCode(io::Printer* printer) const {}
+ virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {}
// Generate lines to decode this field, which will be placed inside the
// message's MergeFromCodedStream() method.
@@ -168,7 +182,7 @@ class FieldGeneratorMap {
private:
const Descriptor* descriptor_;
- scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGenerator> > field_generators_;
static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
const Options& options);
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc
index fa194273..fae4df40 100644
--- a/src/google/protobuf/compiler/cpp/cpp_file.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_file.cc
@@ -34,6 +34,9 @@
#include <google/protobuf/compiler/cpp/cpp_file.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <set>
#include <google/protobuf/compiler/cpp/cpp_enum.h>
@@ -56,13 +59,13 @@ namespace cpp {
FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options)
: file_(file),
message_generators_(
- new scoped_ptr<MessageGenerator>[file->message_type_count()]),
+ new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_count()]),
enum_generators_(
- new scoped_ptr<EnumGenerator>[file->enum_type_count()]),
+ new google::protobuf::scoped_ptr<EnumGenerator>[file->enum_type_count()]),
service_generators_(
- new scoped_ptr<ServiceGenerator>[file->service_count()]),
+ new google::protobuf::scoped_ptr<ServiceGenerator>[file->service_count()]),
extension_generators_(
- new scoped_ptr<ExtensionGenerator>[file->extension_count()]),
+ new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_count()]),
options_(options) {
for (int i = 0; i < file->message_type_count(); i++) {
@@ -131,7 +134,10 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
// OK, it's now safe to #include other files.
printer->Print(
- "#include <google/protobuf/generated_message_util.h>\n");
+ "#include <google/protobuf/arena.h>\n"
+ "#include <google/protobuf/arenastring.h>\n"
+ "#include <google/protobuf/generated_message_util.h>\n"
+ "#include <google/protobuf/metadata.h>\n");
if (file_->message_type_count() > 0) {
if (HasDescriptorMethods(file_)) {
printer->Print(
@@ -144,6 +150,11 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
printer->Print(
"#include <google/protobuf/repeated_field.h>\n"
"#include <google/protobuf/extension_set.h>\n");
+ if (HasMapFields(file_)) {
+ printer->Print(
+ "#include <google/protobuf/map.h>\n"
+ "#include <google/protobuf/map_field_inl.h>\n");
+ }
if (HasDescriptorMethods(file_) && HasEnumDefinitions(file_)) {
printer->Print(
@@ -181,6 +192,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
"// @@protoc_insertion_point(includes)\n");
+
// Open namespace.
GenerateNamespaceOpeners(printer);
@@ -189,9 +201,10 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
printer->Print(
"\n"
"// Internal implementation detail -- do not call these.\n"
- "void $dllexport_decl$ $adddescriptorsname$();\n",
+ "void $dllexport_decl$$adddescriptorsname$();\n",
"adddescriptorsname", GlobalAddDescriptorsName(file_->name()),
- "dllexport_decl", options_.dllexport_decl);
+ "dllexport_decl",
+ options_.dllexport_decl.empty() ? "" : options_.dllexport_decl + " ");
printer->Print(
// Note that we don't put dllexport_decl on these because they are only
@@ -295,7 +308,7 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
}
printer->Print(
"\n"
- "} // namespace google\n} // namespace protobuf\n"
+ "} // namespace protobuf\n} // namespace google\n"
"#endif // SWIG\n");
}
@@ -387,6 +400,19 @@ void FileGenerator::GenerateSource(io::Printer* printer) {
// Generate classes.
for (int i = 0; i < file_->message_type_count(); i++) {
+ if (i == 0 && HasGeneratedMethods(file_)) {
+ printer->Print(
+ "\n"
+ "namespace {\n"
+ "\n"
+ "static void MergeFromFail(int line) GOOGLE_ATTRIBUTE_COLD;\n"
+ "static void MergeFromFail(int line) {\n"
+ " GOOGLE_CHECK(false) << __FILE__ << \":\" << line;\n"
+ "}\n"
+ "\n"
+ "} // namespace\n"
+ "\n");
+ }
printer->Print("\n");
printer->Print(kThickSeparator);
printer->Print("\n");
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.h b/src/google/protobuf/compiler/cpp/cpp_file.h
index 4e4d8b6a..0e06547d 100644
--- a/src/google/protobuf/compiler/cpp/cpp_file.h
+++ b/src/google/protobuf/compiler/cpp/cpp_file.h
@@ -36,6 +36,9 @@
#define GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <vector>
#include <google/protobuf/stubs/common.h>
@@ -79,10 +82,10 @@ class FileGenerator {
const FileDescriptor* file_;
- scoped_array<scoped_ptr<MessageGenerator> > message_generators_;
- scoped_array<scoped_ptr<EnumGenerator> > enum_generators_;
- scoped_array<scoped_ptr<ServiceGenerator> > service_generators_;
- scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<ServiceGenerator> > service_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
// E.g. if the package is foo.bar, package_parts_ is {"foo", "bar"}.
vector<string> package_parts_;
diff --git a/src/google/protobuf/compiler/cpp/cpp_generator.cc b/src/google/protobuf/compiler/cpp/cpp_generator.cc
index 75d558ea..c999b93f 100644
--- a/src/google/protobuf/compiler/cpp/cpp_generator.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_generator.cc
@@ -36,6 +36,9 @@
#include <vector>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <utility>
#include <google/protobuf/compiler/cpp/cpp_file.h>
@@ -102,7 +105,7 @@ bool CppGenerator::Generate(const FileDescriptor* file,
// Generate header.
{
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(basename + ".h"));
io::Printer printer(output.get(), '$');
file_generator.GenerateHeader(&printer);
@@ -110,7 +113,7 @@ bool CppGenerator::Generate(const FileDescriptor* file,
// Generate cc file.
{
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(basename + ".cc"));
io::Printer printer(output.get(), '$');
file_generator.GenerateSource(&printer);
diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc
index b7a47acb..28c4dd54 100644
--- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc
@@ -60,16 +60,17 @@ string DotsToColons(const string& name) {
}
const char* const kKeywordList[] = {
- "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case",
- "catch", "char", "class", "compl", "const", "const_cast", "continue",
- "default", "delete", "do", "double", "dynamic_cast", "else", "enum",
- "explicit", "extern", "false", "float", "for", "friend", "goto", "if",
- "inline", "int", "long", "mutable", "namespace", "new", "not", "not_eq",
- "operator", "or", "or_eq", "private", "protected", "public", "register",
- "reinterpret_cast", "return", "short", "signed", "sizeof", "static",
- "static_cast", "struct", "switch", "template", "this", "throw", "true", "try",
- "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual",
- "void", "volatile", "wchar_t", "while", "xor", "xor_eq"
+ "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", "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"
};
hash_set<string> MakeKeywordsMap() {
@@ -452,6 +453,25 @@ void PrintHandlingOptionalStaticInitializers(
}
+static bool HasMapFields(const Descriptor* descriptor) {
+ for (int i = 0; i < descriptor->field_count(); ++i) {
+ if (descriptor->field(i)->is_map()) {
+ return true;
+ }
+ }
+ for (int i = 0; i < descriptor->nested_type_count(); ++i) {
+ if (HasMapFields(descriptor->nested_type(i))) return true;
+ }
+ return false;
+}
+
+bool HasMapFields(const FileDescriptor* file) {
+ for (int i = 0; i < file->message_type_count(); ++i) {
+ if (HasMapFields(file->message_type(i))) return true;
+ }
+ return false;
+}
+
static bool HasEnumDefinitions(const Descriptor* message_type) {
if (message_type->enum_type_count() > 0) return true;
for (int i = 0; i < message_type->nested_type_count(); ++i) {
diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.h b/src/google/protobuf/compiler/cpp/cpp_helpers.h
index 5d30240c..e60fa7c2 100644
--- a/src/google/protobuf/compiler/cpp/cpp_helpers.h
+++ b/src/google/protobuf/compiler/cpp/cpp_helpers.h
@@ -135,13 +135,24 @@ string SafeFunctionName(const Descriptor* descriptor,
const FieldDescriptor* field,
const string& prefix);
-// Do message classes in this file use UnknownFieldSet?
-// Otherwise, messages will store unknown fields in a string
+// Returns true if unknown fields are preseved after parsing.
+inline bool PreserveUnknownFields(const Descriptor* message) {
+ return message->file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
+}
+
+// If PreserveUnknownFields() is true, determines whether unknown
+// fields will be stored in an UnknownFieldSet or a string.
+// If PreserveUnknownFields() is false, this method will not be
+// used.
inline bool UseUnknownFieldSet(const FileDescriptor* file) {
return file->options().optimize_for() != FileOptions::LITE_RUNTIME;
}
+// Does the file have any map fields, necessitating the file to include
+// map_field_inl.h and map.h.
+bool HasMapFields(const FileDescriptor* file);
+
// Does this file have any enum type definitions?
bool HasEnumDefinitions(const FileDescriptor* file);
@@ -193,11 +204,37 @@ void PrintHandlingOptionalStaticInitializers(
const char* without_static_init);
+inline bool IsMapEntryMessage(const Descriptor* descriptor) {
+ return descriptor->options().map_entry();
+}
+
// 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);
+inline bool HasFieldPresence(const FileDescriptor* file) {
+ return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
+}
+
+// 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 SupportsArenas(const FileDescriptor* file) {
+ return file->options().cc_enable_arenas();
+}
+
+inline bool SupportsArenas(const Descriptor* desc) {
+ return SupportsArenas(desc->file());
+}
+
+inline bool SupportsArenas(const FieldDescriptor* field) {
+ return SupportsArenas(field->file());
+}
+
} // namespace cpp
} // namespace compiler
} // namespace protobuf
diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc
new file mode 100644
index 00000000..0154eeb8
--- /dev/null
+++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc
@@ -0,0 +1,255 @@
+// 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 <google/protobuf/compiler/cpp/cpp_map_field.h>
+#include <google/protobuf/compiler/cpp/cpp_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace cpp {
+
+bool IsProto3Field(const FieldDescriptor* field_descriptor) {
+ const FileDescriptor* file_descriptor = field_descriptor->file();
+ return file_descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
+}
+
+void SetMessageVariables(const FieldDescriptor* descriptor,
+ map<string, string>* variables,
+ const Options& options) {
+ SetCommonFieldVariables(descriptor, variables, options);
+ (*variables)["type"] = FieldMessageTypeName(descriptor);
+ (*variables)["stream_writer"] = (*variables)["declared_type"] +
+ (HasFastArraySerialization(descriptor->message_type()->file()) ?
+ "MaybeToArray" :
+ "");
+ (*variables)["full_name"] = descriptor->full_name();
+
+ const FieldDescriptor* key =
+ descriptor->message_type()->FindFieldByName("key");
+ const FieldDescriptor* val =
+ descriptor->message_type()->FindFieldByName("value");
+ (*variables)["key_cpp"] = PrimitiveTypeName(key->cpp_type());
+ switch (val->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ (*variables)["val_cpp"] = FieldMessageTypeName(val);
+ (*variables)["wrapper"] = "EntryWrapper";
+ break;
+ case FieldDescriptor::CPPTYPE_ENUM:
+ (*variables)["val_cpp"] = ClassName(val->enum_type(), false);
+ (*variables)["wrapper"] = "EnumEntryWrapper";
+ break;
+ default:
+ (*variables)["val_cpp"] = PrimitiveTypeName(val->cpp_type());
+ (*variables)["wrapper"] = "EntryWrapper";
+ }
+ (*variables)["key_type"] =
+ "::google::protobuf::FieldDescriptor::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(key->type()));
+ (*variables)["val_type"] =
+ "::google::protobuf::FieldDescriptor::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(val->type()));
+ (*variables)["map_classname"] = ClassName(descriptor->message_type(), false);
+ (*variables)["number"] = Int32ToString(descriptor->number());
+
+ 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 {
+ (*variables)["default_enum_value"] = "0";
+ }
+}
+
+MapFieldGenerator::
+MapFieldGenerator(const FieldDescriptor* descriptor,
+ const Options& options)
+ : descriptor_(descriptor) {
+ SetMessageVariables(descriptor, &variables_, options);
+}
+
+MapFieldGenerator::~MapFieldGenerator() {}
+
+void MapFieldGenerator::
+GeneratePrivateMembers(io::Printer* printer) const {
+ printer->Print(variables_,
+ "typedef ::google::protobuf::internal::MapEntry<\n"
+ " $key_cpp$, $val_cpp$,\n"
+ " $key_type$,\n"
+ " $val_type$, $default_enum_value$>\n"
+ " $map_classname$;\n"
+ "::google::protobuf::internal::MapField< $key_cpp$, $val_cpp$,"
+ "$key_type$, $val_type$, $default_enum_value$ > $name$_;\n");
+}
+
+void MapFieldGenerator::
+GenerateAccessorDeclarations(io::Printer* printer) const {
+ printer->Print(variables_,
+ "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n"
+ " $name$() const$deprecation$;\n"
+ "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n"
+ " mutable_$name$()$deprecation$;\n");
+}
+
+void MapFieldGenerator::
+GenerateInlineAccessorDefinitions(io::Printer* printer) const {
+ printer->Print(variables_,
+ "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n"
+ "$classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_map:$full_name$)\n"
+ " return $name$_.GetMap();\n"
+ "}\n"
+ "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n"
+ "$classname$::mutable_$name$() {\n"
+ " // @@protoc_insertion_point(field_mutable_map:$full_name$)\n"
+ " return $name$_.MutableMap();\n"
+ "}\n");
+}
+
+void MapFieldGenerator::
+GenerateClearingCode(io::Printer* printer) const {
+ printer->Print(variables_, "$name$_.Clear();\n");
+}
+
+void MapFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
+}
+
+void MapFieldGenerator::
+GenerateSwappingCode(io::Printer* printer) const {
+ printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
+}
+
+void MapFieldGenerator::
+GenerateConstructorCode(io::Printer* printer) const {
+ if (HasDescriptorMethods(descriptor_->file())) {
+ printer->Print(variables_,
+ "$name$_.SetAssignDescriptorCallback(\n"
+ " protobuf_AssignDescriptorsOnce);\n"
+ "$name$_.SetEntryDescriptor(\n"
+ " &$type$_descriptor_);\n");
+ }
+}
+
+void MapFieldGenerator::
+GenerateMergeFromCodedStream(io::Printer* printer) const {
+ const FieldDescriptor* value_field =
+ descriptor_->message_type()->FindFieldByName("value");
+ printer->Print(variables_,
+ "::google::protobuf::scoped_ptr<$map_classname$> entry($name$_.NewEntry());\n");
+
+ if (IsProto3Field(descriptor_) ||
+ value_field->type() != FieldDescriptor::TYPE_ENUM) {
+ printer->Print(variables_,
+ "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n"
+ " input, entry.get()));\n");
+ switch (value_field->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ printer->Print(variables_,
+ "(*mutable_$name$())[entry->key()].Swap("
+ "entry->mutable_value());\n");
+ break;
+ case FieldDescriptor::CPPTYPE_ENUM:
+ printer->Print(variables_,
+ "(*mutable_$name$())[entry->key()] =\n"
+ " static_cast<$val_cpp$>(*entry->mutable_value());\n");
+ break;
+ default:
+ printer->Print(variables_,
+ "(*mutable_$name$())[entry->key()] = *entry->mutable_value();\n");
+ break;
+ }
+ } else {
+ printer->Print(variables_,
+ "{\n"
+ " ::std::string data;\n"
+ " DO_(::google::protobuf::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"
+ " mutable_unknown_fields()->AddLengthDelimited($number$, data);\n"
+ " }\n"
+ "}\n");
+ }
+}
+
+void MapFieldGenerator::
+GenerateSerializeWithCachedSizes(io::Printer* printer) const {
+ printer->Print(variables_,
+ "{\n"
+ " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
+ " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
+ " it = $name$().begin(); it != $name$().end(); ++it) {\n"
+ " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
+ " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
+ " $number$, *entry, output);\n"
+ " }\n"
+ "}\n");
+}
+
+void MapFieldGenerator::
+GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
+ printer->Print(variables_,
+ "{\n"
+ " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
+ " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
+ " it = $name$().begin(); it != $name$().end(); ++it) {\n"
+ " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
+ " target = ::google::protobuf::internal::WireFormatLite::\n"
+ " Write$declared_type$NoVirtualToArray(\n"
+ " $number$, *entry, target);\n"
+ " }\n"
+ "}\n");
+}
+
+void MapFieldGenerator::
+GenerateByteSize(io::Printer* printer) const {
+ printer->Print(variables_,
+ "total_size += $tag_size$ * this->$name$_size();\n"
+ "{\n"
+ " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n"
+ " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n"
+ " it = $name$().begin(); it != $name$().end(); ++it) {\n"
+ " entry.reset($name$_.New$wrapper$(it->first, it->second));\n"
+ " total_size += ::google::protobuf::internal::WireFormatLite::\n"
+ " $declared_type$SizeNoVirtual(*entry);\n"
+ " }\n"
+ "}\n");
+}
+
+} // namespace cpp
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.h b/src/google/protobuf/compiler/cpp/cpp_map_field.h
new file mode 100644
index 00000000..0ff032fd
--- /dev/null
+++ b/src/google/protobuf/compiler/cpp/cpp_map_field.h
@@ -0,0 +1,75 @@
+// 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_COMPILER_CPP_MAP_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_CPP_MAP_FIELD_H__
+
+#include <map>
+#include <string>
+
+#include <google/protobuf/compiler/cpp/cpp_message_field.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace cpp {
+
+class MapFieldGenerator : public FieldGenerator {
+ public:
+ explicit MapFieldGenerator(const FieldDescriptor* descriptor,
+ const Options& options);
+ ~MapFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GeneratePrivateMembers(io::Printer* printer) const;
+ void GenerateAccessorDeclarations(io::Printer* printer) const;
+ void GenerateInlineAccessorDefinitions(io::Printer* printer) const;
+ void GenerateClearingCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSwappingCode(io::Printer* printer) const;
+ void GenerateConstructorCode(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:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapFieldGenerator);
+};
+
+} // namespace cpp
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_CPP_MAP_FIELD_H__
diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc
index 3a9d2639..e71d35fa 100644
--- a/src/google/protobuf/compiler/cpp/cpp_message.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_message.cc
@@ -36,6 +36,9 @@
#include <google/protobuf/stubs/hash.h>
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <set>
#include <utility>
#include <vector>
@@ -281,8 +284,92 @@ string MessageTypeProtoName(const FieldDescriptor* field) {
return field->message_type()->full_name();
}
+// Emits an if-statement with a condition that evaluates to true if |field| is
+// 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,
+ const FieldDescriptor* field) {
+ // Merge and serialize semantics: primitive fields are merged/serialized only
+ // if non-zero (numeric) or non-empty (string).
+ if (!field->is_repeated() && !field->containing_oneof()) {
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
+ printer->Print(
+ "if ($prefix$$name$().size() > 0) {\n",
+ "prefix", prefix,
+ "name", FieldName(field));
+ } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ // Message fields still have has_$name$() methods.
+ printer->Print(
+ "if ($prefix$has_$name$()) {\n",
+ "prefix", prefix,
+ "name", FieldName(field));
+ } else {
+ printer->Print(
+ "if ($prefix$$name$() != 0) {\n",
+ "prefix", prefix,
+ "name", FieldName(field));
+ }
+ printer->Indent();
+ return true;
+ } else if (field->containing_oneof()) {
+ printer->Print(
+ "if (has_$name$()) {\n",
+ "name", FieldName(field));
+ printer->Indent();
+ return true;
+ }
+ return false;
+}
+
+// Does the given field have a has_$name$() method?
+bool HasHasMethod(const FieldDescriptor* field) {
+ if (HasFieldPresence(field->file())) {
+ // In proto1/proto2, every field has a has_$name$() method.
+ return true;
+ }
+ // For message types without true field presence, only fields with a message
+ // type have a has_$name$() method.
+ return field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE;
+}
+
+// Collects map entry message type information.
+void CollectMapInfo(const Descriptor* descriptor,
+ map<string, string>* variables) {
+ GOOGLE_CHECK(IsMapEntryMessage(descriptor));
+ const FieldDescriptor* key = descriptor->FindFieldByName("key");
+ const FieldDescriptor* val = descriptor->FindFieldByName("value");
+ (*variables)["key"] = PrimitiveTypeName(key->cpp_type());
+ switch (val->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ (*variables)["val"] = FieldMessageTypeName(val);
+ break;
+ case FieldDescriptor::CPPTYPE_ENUM:
+ (*variables)["val"] = ClassName(val->enum_type(), false);
+ break;
+ default:
+ (*variables)["val"] = PrimitiveTypeName(val->cpp_type());
+ }
+ (*variables)["key_type"] =
+ "::google::protobuf::FieldDescriptor::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(key->type()));
+ (*variables)["val_type"] =
+ "::google::protobuf::FieldDescriptor::TYPE_" +
+ ToUpper(DeclaredTypeMethodName(val->type()));
}
+// Does the given field have a private (internal helper only) has_$name$()
+// method?
+bool HasPrivateHasMethod(const FieldDescriptor* field) {
+ // Only for oneofs in message types with no field presence. has_$name$(),
+ // based on the oneof case, is still useful internally for generated code.
+ return (!HasFieldPresence(field->file()) &&
+ field->containing_oneof() != NULL);
+}
+
+} // anonymous namespace
+
// ===================================================================
MessageGenerator::MessageGenerator(const Descriptor* descriptor,
@@ -291,11 +378,11 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
classname_(ClassName(descriptor, false)),
options_(options),
field_generators_(descriptor, options),
- nested_generators_(new scoped_ptr<
+ nested_generators_(new google::protobuf::scoped_ptr<
MessageGenerator>[descriptor->nested_type_count()]),
enum_generators_(
- new scoped_ptr<EnumGenerator>[descriptor->enum_type_count()]),
- extension_generators_(new scoped_ptr<
+ new google::protobuf::scoped_ptr<EnumGenerator>[descriptor->enum_type_count()]),
+ extension_generators_(new google::protobuf::scoped_ptr<
ExtensionGenerator>[descriptor->extension_count()]) {
for (int i = 0; i < descriptor->nested_type_count(); i++) {
@@ -312,6 +399,13 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
extension_generators_[i].reset(
new ExtensionGenerator(descriptor->extension(i), options));
}
+
+ num_required_fields_ = 0;
+ for (int i = 0; i < descriptor->field_count(); i++) {
+ if (descriptor->field(i)->is_required()) {
+ ++num_required_fields_;
+ }
+ }
}
MessageGenerator::~MessageGenerator() {}
@@ -322,6 +416,10 @@ GenerateForwardDeclaration(io::Printer* printer) {
"classname", classname_);
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // map entry message doesn't need forward declaration. Since map entry
+ // message cannot be a top level class, we just need to avoid calling
+ // GenerateForwardDeclaration here.
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateForwardDeclaration(printer);
}
}
@@ -360,8 +458,13 @@ GenerateFieldAccessorDeclarations(io::Printer* printer) {
if (field->is_repeated()) {
printer->Print(vars, "inline int $name$_size() const$deprecation$;\n");
- } else {
+ } else if (HasHasMethod(field)) {
printer->Print(vars, "inline bool has_$name$() const$deprecation$;\n");
+ } else if (HasPrivateHasMethod(field)) {
+ printer->Print(vars,
+ "private:\n"
+ "inline bool has_$name$() const$deprecation$;\n"
+ "public:\n");
}
printer->Print(vars, "inline void clear_$name$()$deprecation$;\n");
@@ -410,32 +513,59 @@ GenerateFieldAccessorDefinitions(io::Printer* printer) {
"}\n");
} else if (field->containing_oneof()) {
// Singular field in a oneof
+ // N.B.: Without field presence, we do not use has-bits or generate
+ // has_$name$() methods, but oneofs still have set_has_$name$().
+ // Oneofs also have has_$name$() but only as a private helper
+ // method, so that generated code is slightly cleaner (vs. comparing
+ // _oneof_case_[index] against a constant everywhere).
vars["field_name"] = UnderscoresToCamelCase(field->name(), true);
vars["oneof_name"] = field->containing_oneof()->name();
vars["oneof_index"] = SimpleItoa(field->containing_oneof()->index());
printer->Print(vars,
"inline bool $classname$::has_$name$() const {\n"
" return $oneof_name$_case() == k$field_name$;\n"
- "}\n"
+ "}\n");
+ printer->Print(vars,
"inline void $classname$::set_has_$name$() {\n"
" _oneof_case_[$oneof_index$] = k$field_name$;\n"
"}\n");
} else {
// Singular field.
- char buffer[kFastToBufferSize];
- vars["has_array_index"] = SimpleItoa(field->index() / 32);
- vars["has_mask"] = FastHex32ToBuffer(1u << (field->index() % 32), buffer);
- printer->Print(vars,
- "inline bool $classname$::has_$name$() const {\n"
- " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n"
- "}\n"
- "inline void $classname$::set_has_$name$() {\n"
- " _has_bits_[$has_array_index$] |= 0x$has_mask$u;\n"
- "}\n"
- "inline void $classname$::clear_has_$name$() {\n"
- " _has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n"
- "}\n"
- );
+ if (HasFieldPresence(descriptor_->file())) {
+ // N.B.: without field presence, we do not use has-bits or generate
+ // has_$name$() methods.
+ char buffer[kFastToBufferSize];
+ vars["has_array_index"] = SimpleItoa(field->index() / 32);
+ vars["has_mask"] = FastHex32ToBuffer(1u << (field->index() % 32),
+ buffer);
+ printer->Print(vars,
+ "inline bool $classname$::has_$name$() const {\n"
+ " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n"
+ "}\n"
+ "inline void $classname$::set_has_$name$() {\n"
+ " _has_bits_[$has_array_index$] |= 0x$has_mask$u;\n"
+ "}\n"
+ "inline void $classname$::clear_has_$name$() {\n"
+ " _has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n"
+ "}\n"
+ );
+ } else {
+ // Message fields have a has_$name$() method.
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ bool is_lazy = false;
+ if (is_lazy) {
+ printer->Print(vars,
+ "inline bool $classname$::has_$name$() const {\n"
+ " return !$name$_.IsCleared();\n"
+ "}\n");
+ } else {
+ printer->Print(vars,
+ "inline bool $classname$::has_$name$() const {\n"
+ " return !_is_default_instance_ && $name$_ != NULL;\n"
+ "}\n");
+ }
+ }
+ }
}
// Generate clear_$name$()
@@ -457,9 +587,11 @@ GenerateFieldAccessorDefinitions(io::Printer* printer) {
printer->Print("}\n");
} else {
field_generators_.get(field).GenerateClearingCode(printer);
- if (!field->is_repeated()) {
- printer->Print(vars,
- "clear_has_$name$();\n");
+ if (HasFieldPresence(descriptor_->file())) {
+ if (!field->is_repeated()) {
+ printer->Print(vars,
+ "clear_has_$name$();\n");
+ }
}
}
@@ -482,7 +614,7 @@ GenerateFieldAccessorDefinitions(io::Printer* printer) {
vars["classname"] = classname_;
printer->Print(
vars,
- "inline bool $classname$::has_$oneof_name$() {\n"
+ "inline bool $classname$::has_$oneof_name$() const {\n"
" return $oneof_name$_case() != $cap_oneof_name$_NOT_SET;\n"
"}\n"
"inline void $classname$::clear_has_$oneof_name$() {\n"
@@ -519,6 +651,10 @@ static bool CanClearByZeroing(const FieldDescriptor* field) {
void MessageGenerator::
GenerateClassDefinition(io::Printer* printer) {
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // map entry message doesn't need class definition. Since map entry message
+ // cannot be a top level class, we just need to avoid calling
+ // GenerateClassDefinition here.
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateClassDefinition(printer);
printer->Print("\n");
printer->Print(kThinSeparator);
@@ -553,26 +689,41 @@ GenerateClassDefinition(io::Printer* printer) {
"}\n"
"\n");
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
- " return _unknown_fields_;\n"
- "}\n"
- "\n"
- "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
- " return &_unknown_fields_;\n"
- "}\n"
- "\n");
- } else {
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {\n"
+ " return _internal_metadata_.unknown_fields();\n"
+ "}\n"
+ "\n"
+ "inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {\n"
+ " return _internal_metadata_.mutable_unknown_fields();\n"
+ "}\n"
+ "\n");
+ } else {
+ printer->Print(
+ "inline const ::std::string& unknown_fields() const {\n"
+ " return _unknown_fields_;\n"
+ "}\n"
+ "\n"
+ "inline ::std::string* mutable_unknown_fields() {\n"
+ " return &_unknown_fields_;\n"
+ "}\n"
+ "\n");
+ }
+ }
+
+ // N.B.: We exclude GetArena() when arena support is disabled, falling back on
+ // MessageLite's implementation which returns NULL rather than generating our
+ // own method which returns NULL, in order to reduce code size.
+ if (SupportsArenas(descriptor_)) {
+ // virtual method version of GetArenaNoVirtual(), required for generic dispatch given a
+ // MessageLite* (e.g., in RepeatedField::AddAllocated()).
printer->Print(
- "inline const ::std::string& unknown_fields() const {\n"
- " return _unknown_fields_;\n"
- "}\n"
- "\n"
- "inline ::std::string* mutable_unknown_fields() {\n"
- " return &_unknown_fields_;\n"
- "}\n"
- "\n");
+ "inline ::google::protobuf::Arena* GetArena() const { return GetArenaNoVirtual(); }\n"
+ "inline void* GetMaybeArenaPointer() const {\n"
+ " return MaybeArenaPtr();\n"
+ "}\n");
}
// Only generate this member if it's not disabled.
@@ -628,12 +779,18 @@ GenerateClassDefinition(io::Printer* printer) {
}
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(vars,
+ "void UnsafeArenaSwap($classname$* other);\n");
+ }
printer->Print(vars,
"void Swap($classname$* other);\n"
"\n"
"// implements Message ----------------------------------------------\n"
"\n"
- "$classname$* New() const;\n");
+ "inline $classname$* New() const { return New(NULL); }\n"
+ "\n"
+ "$classname$* New(::google::protobuf::Arena* arena) const;\n");
if (HasGeneratedMethods(descriptor_->file())) {
if (HasDescriptorMethods(descriptor_->file())) {
@@ -699,7 +856,41 @@ GenerateClassDefinition(io::Printer* printer) {
"void SharedCtor();\n"
"void SharedDtor();\n"
"void SetCachedSize(int size) const;\n"
- "public:\n");
+ "void InternalSwap($classname$* other);\n",
+ "classname", classname_);
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(
+ "protected:\n"
+ "explicit $classname$(::google::protobuf::Arena* arena);\n"
+ "private:\n"
+ "static void ArenaDtor(void* object);\n"
+ "inline void RegisterArenaDtor(::google::protobuf::Arena* arena);\n",
+ "classname", classname_);
+ }
+
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "private:\n"
+ "inline ::google::protobuf::Arena* GetArenaNoVirtual() const {\n"
+ " return _internal_metadata_.arena();\n"
+ "}\n"
+ "inline void* MaybeArenaPtr() const {\n"
+ " return _internal_metadata_.raw_arena_ptr();\n"
+ "}\n"
+ "public:\n"
+ "\n");
+ } else {
+ printer->Print(
+ "private:\n"
+ "inline ::google::protobuf::Arena* GetArenaNoVirtual() const {\n"
+ " return _arena_ptr_;\n"
+ "}\n"
+ "inline ::google::protobuf::Arena* MaybeArenaPtr() const {\n"
+ " return _arena_ptr_;\n"
+ "}\n"
+ "public:\n"
+ "\n");
+ }
if (HasDescriptorMethods(descriptor_->file())) {
printer->Print(
@@ -718,9 +909,11 @@ GenerateClassDefinition(io::Printer* printer) {
// Import all nested message classes into this class's scope with typedefs.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
const Descriptor* nested_type = descriptor_->nested_type(i);
- printer->Print("typedef $nested_full_name$ $nested_name$;\n",
- "nested_name", nested_type->name(),
- "nested_full_name", ClassName(nested_type, false));
+ if (!IsMapEntryMessage(nested_type)) {
+ printer->Print("typedef $nested_full_name$ $nested_name$;\n",
+ "nested_name", nested_type->name(),
+ "nested_full_name", ClassName(nested_type, false));
+ }
}
if (descriptor_->nested_type_count() > 0) {
@@ -759,10 +952,18 @@ GenerateClassDefinition(io::Printer* printer) {
for (int i = 0; i < descriptor_->field_count(); i++) {
if (!descriptor_->field(i)->is_repeated()) {
- printer->Print(
- "inline void set_has_$name$();\n",
- "name", FieldName(descriptor_->field(i)));
- if (!descriptor_->field(i)->containing_oneof()) {
+ // set_has_***() generated in all proto1/2 code and in oneofs (only) for
+ // messages without true field presence.
+ if (HasFieldPresence(descriptor_->file()) ||
+ descriptor_->field(i)->containing_oneof()) {
+ printer->Print(
+ "inline void set_has_$name$();\n",
+ "name", FieldName(descriptor_->field(i)));
+ }
+ // clear_has_***() generated only for non-oneof fields
+ // in proto1/2.
+ if (!descriptor_->field(i)->containing_oneof() &&
+ HasFieldPresence(descriptor_->file())) {
printer->Print(
"inline void clear_has_$name$();\n",
"name", FieldName(descriptor_->field(i)));
@@ -774,12 +975,20 @@ GenerateClassDefinition(io::Printer* printer) {
// Generate oneof function declarations
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
- "inline bool has_$oneof_name$();\n"
+ "inline bool has_$oneof_name$() const;\n"
"void clear_$oneof_name$();\n"
"inline void clear_has_$oneof_name$();\n\n",
"oneof_name", descriptor_->oneof_decl(i)->name());
}
+ if (HasGeneratedMethods(descriptor_->file()) &&
+ !descriptor_->options().message_set_wire_format() &&
+ num_required_fields_ > 1) {
+ printer->Print(
+ "// helper for ByteSize()\n"
+ "int RequiredFieldsByteSizeFallback() const;\n\n");
+ }
+
// Prepare decls for _cached_size_ and _has_bits_. Their position in the
// output will be determined later.
@@ -816,23 +1025,48 @@ GenerateClassDefinition(io::Printer* printer) {
if (UseUnknownFieldSet(descriptor_->file())) {
printer->Print(
- "::google::protobuf::UnknownFieldSet _unknown_fields_;\n"
- "\n");
+ "::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;\n");
} else {
printer->Print(
"::std::string _unknown_fields_;\n"
+ "::google::protobuf::Arena* _arena_ptr_;\n"
"\n");
}
- // _has_bits_ is frequently accessed, so to reduce code size and improve
- // speed, it should be close to the start of the object. But, try not to
- // waste space:_has_bits_ by itself always makes sense if its size is a
- // multiple of 8, but, otherwise, maybe _has_bits_ and cached_size_ together
- // will work well.
- printer->Print(has_bits_decl.c_str());
- if ((sizeof_has_bits % 8) != 0) {
- printer->Print(cached_size_decl.c_str());
- need_to_emit_cached_size = false;
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(
+ "friend class ::google::protobuf::Arena;\n"
+ "typedef void InternalArenaConstructable_;\n"
+ "typedef void DestructorSkippable_;\n");
+ }
+
+ if (HasFieldPresence(descriptor_->file())) {
+ // _has_bits_ is frequently accessed, so to reduce code size and improve
+ // speed, it should be close to the start of the object. But, try not to
+ // waste space:_has_bits_ by itself always makes sense if its size is a
+ // multiple of 8, but, otherwise, maybe _has_bits_ and cached_size_ together
+ // will work well.
+ printer->Print(has_bits_decl.c_str());
+ if ((sizeof_has_bits % 8) != 0) {
+ printer->Print(cached_size_decl.c_str());
+ need_to_emit_cached_size = false;
+ }
+ } else {
+ // Without field presence, we need another way to disambiguate the default
+ // instance, because the default instance's submessage fields (if any) store
+ // pointers to the default instances of the submessages even when they
+ // aren't present. Alternatives to this approach might be to (i) use a
+ // tagged pointer on all message fields, setting a tag bit for "not really
+ // present, just default instance"; or (ii) comparing |this| against the
+ // return value from GeneratedMessageFactory::GetPrototype() in all
+ // has_$field$() calls. However, both of these options are much more
+ // expensive (in code size and CPU overhead) than just checking a field in
+ // the message. Long-term, the best solution would be to rearchitect the
+ // default instance design not to store pointers to submessage default
+ // instances, and have reflection get those some other way; but that change
+ // would have too much impact on proto2.
+ printer->Print(
+ "bool _is_default_instance_;\n");
}
// Field members:
@@ -871,7 +1105,10 @@ GenerateClassDefinition(io::Printer* printer) {
// For each oneof generate a union
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
- "union $camel_oneof_name$Union {\n",
+ "union $camel_oneof_name$Union {\n"
+ // explicit empty constructor is needed when union contains
+ // ArenaStringPtr members for string fields.
+ " $camel_oneof_name$Union() {}\n",
"camel_oneof_name",
UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true));
printer->Indent();
@@ -938,6 +1175,10 @@ GenerateClassDefinition(io::Printer* printer) {
void MessageGenerator::
GenerateInlineMethods(io::Printer* printer) {
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // map entry message doesn't need inline methods. Since map entry message
+ // cannot be a top level class, we just need to avoid calling
+ // GenerateInlineMethods here.
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateInlineMethods(printer);
printer->Print(kThinSeparator);
printer->Print("\n");
@@ -965,11 +1206,17 @@ GenerateInlineMethods(io::Printer* printer) {
void MessageGenerator::
GenerateDescriptorDeclarations(io::Printer* printer) {
- printer->Print(
- "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n"
- "const ::google::protobuf::internal::GeneratedMessageReflection*\n"
- " $name$_reflection_ = NULL;\n",
- "name", classname_);
+ if (!IsMapEntryMessage(descriptor_)) {
+ printer->Print(
+ "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n"
+ "const ::google::protobuf::internal::GeneratedMessageReflection*\n"
+ " $name$_reflection_ = NULL;\n",
+ "name", classname_);
+ } else {
+ printer->Print(
+ "const ::google::protobuf::Descriptor* $name$_descriptor_ = NULL;\n",
+ "name", classname_);
+ }
// Generate oneof default instance for reflection usage.
if (descriptor_->oneof_decl_count() > 0) {
@@ -979,7 +1226,7 @@ GenerateDescriptorDeclarations(io::Printer* printer) {
for (int j = 0; j < descriptor_->oneof_decl(i)->field_count(); j++) {
const FieldDescriptor* field = descriptor_->oneof_decl(i)->field(j);
printer->Print(" ");
- if (IsStringOrMessage(field)) {
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
printer->Print("const ");
}
field_generators_.get(field).GeneratePrivateMembers(printer);
@@ -1020,19 +1267,44 @@ GenerateDescriptorInitializer(io::Printer* printer, int index) {
"$parent$_descriptor_->nested_type($index$);\n");
}
+ if (IsMapEntryMessage(descriptor_)) return;
+
// Generate the offsets.
GenerateOffsets(printer);
+ const bool pass_pool_and_factory = false;
+ vars["fn"] = pass_pool_and_factory ?
+ "new ::google::protobuf::internal::GeneratedMessageReflection" :
+ "::google::protobuf::internal::GeneratedMessageReflection"
+ "::NewGeneratedMessageReflection";
// Construct the reflection object.
printer->Print(vars,
"$classname$_reflection_ =\n"
- " new ::google::protobuf::internal::GeneratedMessageReflection(\n"
+ " $fn$(\n"
" $classname$_descriptor_,\n"
" $classname$::default_instance_,\n"
- " $classname$_offsets_,\n"
- " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, _has_bits_[0]),\n"
+ " $classname$_offsets_,\n");
+ if (!HasFieldPresence(descriptor_->file())) {
+ // If we don't have field presence, then _has_bits_ does not exist.
+ printer->Print(vars,
+ " -1,\n");
+ } else {
+ printer->Print(vars,
+ " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, _has_bits_[0]),\n");
+ }
+
+ // Unknown field offset: either points to the unknown field set if embedded
+ // directly, or indicates that the unknown field set is stored as part of the
+ // internal metadata if not.
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(vars,
+ " -1,\n");
+ } else {
+ printer->Print(vars,
" GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
"$classname$, _unknown_fields_),\n");
+ }
+
if (descriptor_->extension_range_count() > 0) {
printer->Print(vars,
" GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
@@ -1050,12 +1322,39 @@ GenerateDescriptorInitializer(io::Printer* printer, int index) {
"$classname$, _oneof_case_[0]),\n");
}
- printer->Print(
- " ::google::protobuf::DescriptorPool::generated_pool(),\n");
- printer->Print(vars,
- " ::google::protobuf::MessageFactory::generated_factory(),\n");
+ if (pass_pool_and_factory) {
+ printer->Print(
+ " ::google::protobuf::DescriptorPool::generated_pool(),\n");
+ printer->Print(vars,
+ " ::google::protobuf::MessageFactory::generated_factory(),\n");
+ }
+
printer->Print(vars,
- " sizeof($classname$));\n");
+ " sizeof($classname$),\n");
+
+ // Arena offset: either an offset to the metadata struct that contains the
+ // arena pointer and unknown field set (in a space-efficient way) if we use
+ // that implementation strategy, or an offset directly to the arena pointer if
+ // not (because e.g. we don't have an unknown field set).
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(vars,
+ " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "$classname$, _internal_metadata_),\n");
+ } else {
+ printer->Print(vars,
+ " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "$classname$, _arena_),\n");
+ }
+
+ // is_default_instance_ offset.
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(vars,
+ " -1);\n");
+ } else {
+ printer->Print(vars,
+ " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET("
+ "$classname$, _is_default_instance_));\n");
+ }
// Handle nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
@@ -1070,10 +1369,37 @@ GenerateDescriptorInitializer(io::Printer* printer, int index) {
void MessageGenerator::
GenerateTypeRegistrations(io::Printer* printer) {
// Register this message type with the message factory.
- printer->Print(
- "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
- " $classname$_descriptor_, &$classname$::default_instance());\n",
- "classname", classname_);
+ if (!IsMapEntryMessage(descriptor_)) {
+ printer->Print(
+ "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
+ " $classname$_descriptor_, &$classname$::default_instance());\n",
+ "classname", classname_);
+ }
+ else {
+ map<string, string> vars;
+ CollectMapInfo(descriptor_, &vars);
+ vars["classname"] = classname_;
+
+ const FieldDescriptor* val = descriptor_->FindFieldByName("value");
+ if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO2 &&
+ val->type() == FieldDescriptor::TYPE_ENUM) {
+ const EnumValueDescriptor* default_value = val->default_value_enum();
+ vars["default_enum_value"] = Int32ToString(default_value->number());
+ } else {
+ vars["default_enum_value"] = "0";
+ }
+
+ printer->Print(vars,
+ "::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(\n"
+ " $classname$_descriptor_,\n"
+ " ::google::protobuf::internal::MapEntry<\n"
+ " $key$,\n"
+ " $val$,\n"
+ " $key_type$,\n"
+ " $val_type$,\n"
+ " $default_enum_value$>::CreateDefaultInstance(\n"
+ " $classname$_descriptor_));\n");
+ }
// Handle nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
@@ -1090,6 +1416,8 @@ GenerateDefaultInstanceAllocator(io::Printer* printer) {
.GenerateDefaultInstanceAllocator(printer);
}
+ if (IsMapEntryMessage(descriptor_)) return;
+
// Construct the default instance. We can't call InitAsDefaultInstance() yet
// because we need to make sure all default instances that this one might
// depend on are constructed first.
@@ -1100,7 +1428,7 @@ GenerateDefaultInstanceAllocator(io::Printer* printer) {
if ((descriptor_->oneof_decl_count() > 0) &&
HasDescriptorMethods(descriptor_->file())) {
printer->Print(
- "$classname$_default_oneof_instance_ = new $classname$OneofInstance;\n",
+ "$classname$_default_oneof_instance_ = new $classname$OneofInstance();\n",
"classname", classname_);
}
@@ -1124,6 +1452,10 @@ GenerateDefaultInstanceInitializer(io::Printer* printer) {
// Handle nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // map entry message doesn't need to initialize default instance manually.
+ // Since map entry message cannot be a top level class, we just need to
+ // avoid calling DefaultInstanceInitializer here.
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateDefaultInstanceInitializer(printer);
}
}
@@ -1153,6 +1485,7 @@ GenerateShutdownCode(io::Printer* printer) {
// Handle nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateShutdownCode(printer);
}
}
@@ -1164,6 +1497,10 @@ GenerateClassMethods(io::Printer* printer) {
}
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // map entry message doesn't need class methods. Since map entry message
+ // cannot be a top level class, we just need to avoid calling
+ // GenerateClassMethods here.
+ if (IsMapEntryMessage(descriptor_->nested_type(i))) continue;
nested_generators_[i]->GenerateClassMethods(printer);
printer->Print("\n");
printer->Print(kThinSeparator);
@@ -1301,6 +1638,11 @@ GenerateSharedConstructorCode(io::Printer* printer) {
"classname", classname_);
printer->Indent();
+ if (!HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ " _is_default_instance_ = false;\n");
+ }
+
printer->Print(StrCat(
uses_string_ ? "::google::protobuf::internal::GetEmptyString();\n" : "",
"_cached_size_ = 0;\n").c_str());
@@ -1312,8 +1654,10 @@ GenerateSharedConstructorCode(io::Printer* printer) {
}
}
- printer->Print(
- "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
+ }
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print(
@@ -1331,6 +1675,14 @@ GenerateSharedDestructorCode(io::Printer* printer) {
"void $classname$::SharedDtor() {\n",
"classname", classname_);
printer->Indent();
+ if (SupportsArenas(descriptor_)) {
+ // Do nothing when the message is allocated in an arena.
+ printer->Print(
+ "if (GetArenaNoVirtual() != NULL) {\n"
+ " return;\n"
+ "}\n"
+ "\n");
+ }
// Write the destructors for each field except oneof members.
for (int i = 0; i < descriptor_->field_count(); i++) {
if (!descriptor_->field(i)->containing_oneof()) {
@@ -1381,25 +1733,118 @@ GenerateSharedDestructorCode(io::Printer* printer) {
}
void MessageGenerator::
+GenerateArenaDestructorCode(io::Printer* printer) {
+ // Generate the ArenaDtor() method. Track whether any fields actually produced
+ // code that needs to be called.
+ printer->Print(
+ "void $classname$::ArenaDtor(void* object) {\n",
+ "classname", classname_);
+ printer->Indent();
+
+ // This code is placed inside a static method, rather than an ordinary one,
+ // since that simplifies Arena's destructor list (ordinary function pointers
+ // rather than member function pointers). _this is the object being
+ // destructed.
+ printer->Print(
+ "$classname$* _this = reinterpret_cast< $classname$* >(object);\n"
+ // avoid an "unused variable" warning in case no fields have dtor code.
+ "(void)_this;\n",
+ "classname", classname_);
+
+ bool need_registration = false;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ if (field_generators_.get(descriptor_->field(i))
+ .GenerateArenaDestructorCode(printer)) {
+ need_registration = true;
+ }
+ }
+ printer->Outdent();
+ printer->Print(
+ "}\n");
+
+ if (need_registration) {
+ printer->Print(
+ "inline void $classname$::RegisterArenaDtor(::google::protobuf::Arena* arena) {\n"
+ " if (arena != NULL) {"
+ " arena->OwnCustomDestructor(this, &$classname$::ArenaDtor);\n"
+ " }\n"
+ "}\n",
+ "classname", classname_);
+ } else {
+ printer->Print(
+ "void $classname$::RegisterArenaDtor(::google::protobuf::Arena* arena) {\n"
+ "}\n",
+ "classname", classname_);
+ }
+}
+
+void MessageGenerator::
GenerateStructors(io::Printer* printer) {
string superclass = SuperClassName(descriptor_);
+ string initializer_with_arena;
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ initializer_with_arena = "_internal_metadata_(arena)";
+ } else {
+ initializer_with_arena = "_arena_ptr_(arena)";
+ }
+ if (descriptor_->extension_range_count() > 0) {
+ initializer_with_arena = string("\n _extensions_(arena)") +
+ (!initializer_with_arena.empty() ? ", " : "") + initializer_with_arena;
+ } else {
+ initializer_with_arena = "\n " + initializer_with_arena;
+ }
+
+ // Initialize member variables with arena constructor.
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ bool has_arena_constructor = descriptor_->field(i)->is_repeated();
+ if (has_arena_constructor) {
+ initializer_with_arena += string(",\n ") +
+ FieldName(descriptor_->field(i)) + string("_(arena)");
+ }
+ }
+ initializer_with_arena = superclass + "()" +
+ (!initializer_with_arena.empty() ? "," : " ") + initializer_with_arena;
+
+ string initializer_null;
+ initializer_null = (UseUnknownFieldSet(descriptor_->file()) ?
+ ", _internal_metadata_(NULL) " : ", _arena_ptr_(NULL)");
- // Generate the default constructor.
printer->Print(
- "$classname$::$classname$()\n"
- " : $superclass$() {\n"
- " SharedCtor();\n"
- " // @@protoc_insertion_point(constructor:$full_name$)\n"
- "}\n",
- "classname", classname_,
- "superclass", superclass,
- "full_name", descriptor_->full_name());
+ "$classname$::$classname$()\n"
+ " : $superclass$() $initializer$ {\n"
+ " SharedCtor();\n"
+ " // @@protoc_insertion_point(constructor:$full_name$)\n"
+ "}\n",
+ "classname", classname_,
+ "superclass", superclass,
+ "full_name", descriptor_->full_name(),
+ "initializer", initializer_null);
+
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(
+ "\n"
+ "$classname$::$classname$(::google::protobuf::Arena* arena)\n"
+ " : $initializer$ {\n"
+ " SharedCtor();\n"
+ " RegisterArenaDtor(arena);\n"
+ " // @@protoc_insertion_point(arena_constructor:$full_name$)\n"
+ "}\n",
+ "initializer", initializer_with_arena,
+ "classname", classname_,
+ "superclass", superclass,
+ "full_name", descriptor_->full_name());
+ }
printer->Print(
"\n"
"void $classname$::InitAsDefaultInstance() {\n",
"classname", classname_);
+ if (!HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ " _is_default_instance_ = true;\n");
+ }
+
// The default instance needs all of its embedded message pointers
// cross-linked to other default instances. We can't do this initialization
// in the constructor because some other default instances may not have been
@@ -1441,7 +1886,17 @@ GenerateStructors(io::Printer* printer) {
// Generate the copy constructor.
printer->Print(
"$classname$::$classname$(const $classname$& from)\n"
- " : $superclass$() {\n"
+ " : $superclass$()",
+ "classname", classname_,
+ "superclass", superclass,
+ "full_name", descriptor_->full_name());
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ ",\n _internal_metadata_(NULL) {\n");
+ } else if (!UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(",\n _arena_ptr_(NULL) {\n");
+ }
+ printer->Print(
" SharedCtor();\n"
" MergeFrom(from);\n"
" // @@protoc_insertion_point(copy_constructor:$full_name$)\n"
@@ -1467,6 +1922,11 @@ GenerateStructors(io::Printer* printer) {
// Generate the shared destructor code.
GenerateSharedDestructorCode(printer);
+ // Generate the arena-specific destructor code.
+ if (SupportsArenas(descriptor_)) {
+ GenerateArenaDestructorCode(printer);
+ }
+
// Generate SetCachedSize.
printer->Print(
"void $classname$::SetCachedSize(int size) const {\n"
@@ -1512,11 +1972,23 @@ GenerateStructors(io::Printer* printer) {
"\n",
"classname", classname_);
- printer->Print(
- "$classname$* $classname$::New() const {\n"
- " return new $classname$;\n"
- "}\n",
- "classname", classname_);
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(
+ "$classname$* $classname$::New(::google::protobuf::Arena* arena) const {\n"
+ " return ::google::protobuf::Arena::CreateMessage<$classname$>(arena);\n"
+ "}\n",
+ "classname", classname_);
+ } else {
+ printer->Print(
+ "$classname$* $classname$::New(::google::protobuf::Arena* arena) const {\n"
+ " $classname$* n = new $classname$;\n"
+ " if (arena != NULL) {\n"
+ " arena->Own(n);\n"
+ " }\n"
+ " return n;\n"
+ "}\n",
+ "classname", classname_);
+ }
}
@@ -1616,16 +2088,19 @@ GenerateClear(io::Printer* printer) {
const string& memsets = memsets_for_chunk[i / 8];
uint32 mask = fields_mask_for_chunk[i / 8];
int count = popcnt(mask);
+ GOOGLE_DCHECK_GE(count, 1);
if (count == 1 ||
(count <= 4 && count == memset_field_count_for_chunk[i / 8])) {
// No "if" here because the chunk is trivial.
} else {
- printer->Print(
- "if (_has_bits_[$index$ / 32] & $mask$) {\n",
- "index", SimpleItoa(i / 8 * 8),
- "mask", SimpleItoa(mask));
- printer->Indent();
- chunk_block_in_progress = true;
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ "if (_has_bits_[$index$ / 32] & $mask$) {\n",
+ "index", SimpleItoa(i / 8 * 8),
+ "mask", SimpleItoa(mask));
+ printer->Indent();
+ chunk_block_in_progress = true;
+ }
}
printer->Print(memsets.c_str());
}
@@ -1639,14 +2114,18 @@ GenerateClear(io::Printer* printer) {
field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
field->cpp_type() == FieldDescriptor::CPPTYPE_STRING;
- if (should_check_bit) {
+ bool have_enclosing_if = false;
+ if (should_check_bit &&
+ // If no field presence, then always clear strings/messages as well.
+ HasFieldPresence(descriptor_->file())) {
printer->Print("if (has_$name$()) {\n", "name", fieldname);
printer->Indent();
+ have_enclosing_if = true;
}
field_generators_.get(field).GenerateClearingCode(printer);
- if (should_check_bit) {
+ if (have_enclosing_if) {
printer->Outdent();
printer->Print("}\n");
}
@@ -1678,16 +2157,22 @@ GenerateClear(io::Printer* printer) {
"oneof_name", descriptor_->oneof_decl(i)->name());
}
- // Step 5: Everything else.
- printer->Print(
- "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
-
- if (UseUnknownFieldSet(descriptor_->file())) {
+ if (HasFieldPresence(descriptor_->file())) {
+ // Step 5: Everything else.
printer->Print(
- "mutable_unknown_fields()->Clear();\n");
- } else {
- printer->Print(
- "mutable_unknown_fields()->clear();\n");
+ "::memset(_has_bits_, 0, sizeof(_has_bits_));\n");
+ }
+
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "if (_internal_metadata_.have_unknown_fields()) {\n"
+ " mutable_unknown_fields()->Clear();\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "mutable_unknown_fields()->clear();\n");
+ }
}
printer->Outdent();
@@ -1748,12 +2233,42 @@ GenerateOneofClear(io::Printer* printer) {
void MessageGenerator::
GenerateSwap(io::Printer* printer) {
- // Generate the Swap member function.
- printer->Print("void $classname$::Swap($classname$* other) {\n",
+ 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.
+ printer->Print(
+ "void $classname$::Swap($classname$* other) {\n"
+ " if (other == this) return;\n"
+ " if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {\n"
+ " InternalSwap(other);\n"
+ " } else {\n"
+ " $classname$ temp;\n"
+ " temp.MergeFrom(*this);\n"
+ " CopyFrom(*other);\n"
+ " other->CopyFrom(temp);\n"
+ " }\n"
+ "}\n"
+ "void $classname$::UnsafeArenaSwap($classname$* other) {\n"
+ " if (other == this) return;\n"
+ " GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual());\n"
+ " InternalSwap(other);\n"
+ "}\n",
+ "classname", classname_);
+ } else {
+ printer->Print(
+ "void $classname$::Swap($classname$* other) {\n"
+ " if (other == this) return;\n"
+ " InternalSwap(other);\n"
+ "}\n",
+ "classname", classname_);
+ }
+
+ // Generate the UnsafeArenaSwap member function.
+ printer->Print("void $classname$::InternalSwap($classname$* other) {\n",
"classname", classname_);
printer->Indent();
- printer->Print("if (other != this) {\n");
- printer->Indent();
if (HasGeneratedMethods(descriptor_->file())) {
for (int i = 0; i < descriptor_->field_count(); i++) {
@@ -1769,15 +2284,25 @@ GenerateSwap(io::Printer* printer) {
"i", SimpleItoa(i));
}
- for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) {
- printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
- "i", SimpleItoa(i));
+ if (HasFieldPresence(descriptor_->file())) {
+ for (int i = 0; i < (descriptor_->field_count() + 31) / 32; ++i) {
+ printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n",
+ "i", SimpleItoa(i));
+ }
}
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print("_unknown_fields_.Swap(&other->_unknown_fields_);\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "_internal_metadata_.Swap(&other->_internal_metadata_);\n");
+ } else {
+ printer->Print("_unknown_fields_.swap(other->_unknown_fields_);\n");
+ }
} else {
- printer->Print("_unknown_fields_.swap(other->_unknown_fields_);\n");
+ // Still swap internal_metadata as it may contain more than just
+ // unknown fields.
+ printer->Print(
+ "_internal_metadata_.Swap(&other->_internal_metadata_);\n");
}
printer->Print("std::swap(_cached_size_, other->_cached_size_);\n");
if (descriptor_->extension_range_count() > 0) {
@@ -1789,8 +2314,6 @@ GenerateSwap(io::Printer* printer) {
printer->Outdent();
printer->Print("}\n");
- printer->Outdent();
- printer->Print("}\n");
}
void MessageGenerator::
@@ -1800,7 +2323,7 @@ GenerateMergeFrom(io::Printer* printer) {
// base class as a parameter).
printer->Print(
"void $classname$::MergeFrom(const ::google::protobuf::Message& from) {\n"
- " GOOGLE_CHECK_NE(&from, this);\n",
+ " if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);\n",
"classname", classname_);
printer->Indent();
@@ -1835,7 +2358,7 @@ GenerateMergeFrom(io::Printer* printer) {
// Generate the class-specific MergeFrom, which avoids the GOOGLE_CHECK and cast.
printer->Print(
"void $classname$::MergeFrom(const $classname$& from) {\n"
- " GOOGLE_CHECK_NE(&from, this);\n",
+ " if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);\n",
"classname", classname_);
printer->Indent();
@@ -1886,33 +2409,48 @@ GenerateMergeFrom(io::Printer* printer) {
const FieldDescriptor* field = descriptor_->field(i);
if (!field->is_repeated() && !field->containing_oneof()) {
- // See above in GenerateClear for an explanation of this.
- if (i / 8 != last_index / 8 || last_index < 0) {
- if (last_index >= 0) {
- printer->Outdent();
- printer->Print("}\n");
+ if (HasFieldPresence(descriptor_->file())) {
+ // See above in GenerateClear for an explanation of this.
+ if (i / 8 != last_index / 8 || last_index < 0) {
+ if (last_index >= 0) {
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+ printer->Print(
+ "if (from._has_bits_[$index$ / 32] & "
+ "(0xffu << ($index$ % 32))) {\n",
+ "index", SimpleItoa(field->index()));
+ printer->Indent();
}
- printer->Print(
- "if (from._has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n",
- "index", SimpleItoa(field->index()));
- printer->Indent();
}
last_index = i;
- printer->Print(
- "if (from.has_$name$()) {\n",
- "name", FieldName(field));
- printer->Indent();
+ bool have_enclosing_if = false;
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ "if (from.has_$name$()) {\n",
+ "name", FieldName(field));
+ printer->Indent();
+ have_enclosing_if = true;
+ } else {
+ // Merge semantics without true field presence: primitive fields are
+ // merged only if non-zero (numeric) or non-empty (string).
+ have_enclosing_if = EmitFieldNonDefaultCondition(
+ printer, "from.", field);
+ }
field_generators_.get(field).GenerateMergingCode(printer);
- printer->Outdent();
- printer->Print("}\n");
+ if (have_enclosing_if) {
+ printer->Outdent();
+ printer->Print("}\n");
+ }
}
}
- if (last_index >= 0) {
+ if (HasFieldPresence(descriptor_->file()) &&
+ last_index >= 0) {
printer->Outdent();
printer->Print("}\n");
}
@@ -1921,12 +2459,16 @@ GenerateMergeFrom(io::Printer* printer) {
printer->Print("_extensions_.MergeFrom(from._extensions_);\n");
}
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n");
- } else {
- printer->Print(
- "mutable_unknown_fields()->append(from.unknown_fields());\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "if (from._internal_metadata_.have_unknown_fields()) {\n"
+ " mutable_unknown_fields()->MergeFrom(from.unknown_fields());\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "mutable_unknown_fields()->append(from.unknown_fields());\n");
+ }
}
printer->Outdent();
@@ -2015,7 +2557,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
printer->Print("for (;;) {\n");
printer->Indent();
- scoped_array<const FieldDescriptor*> ordered_fields(
+ google::protobuf::scoped_array<const FieldDescriptor * > ordered_fields(
SortFieldsByNumber(descriptor_));
uint32 maxtag = descriptor_->field_count() == 0 ? 0 :
WireFormat::MakeTag(ordered_fields[descriptor_->field_count() - 1]);
@@ -2171,24 +2713,33 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
}
}
printer->Print(") {\n");
- if (UseUnknownFieldSet(descriptor_->file())) {
- PrintHandlingOptionalStaticInitializers(
- descriptor_->file(), printer,
- // With static initializers.
- " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
- " mutable_unknown_fields()));\n",
- // Without.
- " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
- " mutable_unknown_fields()));\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ PrintHandlingOptionalStaticInitializers(
+ descriptor_->file(), printer,
+ // With static initializers.
+ " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
+ " mutable_unknown_fields()));\n",
+ // Without.
+ " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
+ " mutable_unknown_fields()));\n");
+ } else {
+ PrintHandlingOptionalStaticInitializers(
+ descriptor_->file(), printer,
+ // With static initializers.
+ " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
+ " &unknown_fields_stream));\n",
+ // Without.
+ " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
+ " &unknown_fields_stream));\n");
+ }
} else {
PrintHandlingOptionalStaticInitializers(
descriptor_->file(), printer,
// With static initializers.
- " DO_(_extensions_.ParseField(tag, input, default_instance_,\n"
- " &unknown_fields_stream));\n",
+ " DO_(_extensions_.ParseField(tag, input, default_instance_);\n",
// Without.
- " DO_(_extensions_.ParseField(tag, input, &default_instance(),\n"
- " &unknown_fields_stream));\n");
+ " DO_(_extensions_.ParseField(tag, input, &default_instance());\n");
}
printer->Print(
" continue;\n"
@@ -2196,14 +2747,19 @@ GenerateMergeFromCodedStream(io::Printer* printer) {
}
// We really don't recognize this tag. Skip it.
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print(
- "DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
- " input, tag, mutable_unknown_fields()));\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "DO_(::google::protobuf::internal::WireFormat::SkipField(\n"
+ " input, tag, mutable_unknown_fields()));\n");
+ } else {
+ printer->Print(
+ "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n"
+ " input, tag, &unknown_fields_stream));\n");
+ }
} else {
printer->Print(
- "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n"
- " input, tag, &unknown_fields_stream));\n");
+ "DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag));\n");
}
if (descriptor_->field_count() > 0) {
@@ -2232,11 +2788,15 @@ void MessageGenerator::GenerateSerializeOneField(
io::Printer* printer, const FieldDescriptor* field, bool to_array) {
PrintFieldComment(printer, field);
- if (!field->is_repeated()) {
+ bool have_enclosing_if = false;
+ if (!field->is_repeated() && HasFieldPresence(descriptor_->file())) {
printer->Print(
"if (has_$name$()) {\n",
"name", FieldName(field));
printer->Indent();
+ have_enclosing_if = true;
+ } else if (!HasFieldPresence(descriptor_->file())) {
+ have_enclosing_if = EmitFieldNonDefaultCondition(printer, "this->", field);
}
if (to_array) {
@@ -2246,7 +2806,7 @@ void MessageGenerator::GenerateSerializeOneField(
field_generators_.get(field).GenerateSerializeWithCachedSizes(printer);
}
- if (!field->is_repeated()) {
+ if (have_enclosing_if) {
printer->Outdent();
printer->Print("}\n");
}
@@ -2356,7 +2916,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) {
void MessageGenerator::
GenerateSerializeWithCachedSizesBody(io::Printer* printer, bool to_array) {
- scoped_array<const FieldDescriptor*> ordered_fields(
+ google::protobuf::scoped_array<const FieldDescriptor * > ordered_fields(
SortFieldsByNumber(descriptor_));
vector<const Descriptor::ExtensionRange*> sorted_extensions;
@@ -2386,28 +2946,69 @@ GenerateSerializeWithCachedSizesBody(io::Printer* printer, bool to_array) {
}
}
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print("if (!unknown_fields().empty()) {\n");
- printer->Indent();
- if (to_array) {
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print("if (_internal_metadata_.have_unknown_fields()) {\n");
+ printer->Indent();
+ if (to_array) {
+ printer->Print(
+ "target = "
+ "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
+ " unknown_fields(), target);\n");
+ } else {
+ printer->Print(
+ "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
+ " unknown_fields(), output);\n");
+ }
+ printer->Outdent();
+
printer->Print(
- "target = "
- "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n"
- " unknown_fields(), target);\n");
+ "}\n");
} else {
printer->Print(
- "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n"
- " unknown_fields(), output);\n");
+ "output->WriteRaw(unknown_fields().data(),\n"
+ " unknown_fields().size());\n");
}
- printer->Outdent();
+ }
+}
- printer->Print(
- "}\n");
- } else {
- printer->Print(
- "output->WriteRaw(unknown_fields().data(),\n"
- " unknown_fields().size());\n");
+static vector<uint32> RequiredFieldsBitMask(const Descriptor* desc) {
+ vector<uint32> result;
+ uint32 mask = 0;
+ for (int i = 0; i < desc->field_count(); i++) {
+ if (i > 0 && i % 32 == 0) {
+ result.push_back(mask);
+ mask = 0;
+ }
+ if (desc->field(i)->is_required()) {
+ mask |= (1 << (i & 31));
+ }
+ }
+ if (mask != 0) {
+ result.push_back(mask);
}
+ return result;
+}
+
+// Create an expression that evaluates to
+// "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 vector<uint32>& masks) {
+ vector<string> parts;
+ for (int i = 0; i < masks.size(); i++) {
+ if (masks[i] == 0) continue;
+ char buffer[kFastToBufferSize];
+ FastHex32ToBuffer(masks[i], buffer);
+ string m = StrCat("0x", buffer);
+ // 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 = parts.size() == 1 ? parts[0] :
+ StrCat("(", Join(parts, "\n | "), ")");
+ return result + " == 0";
}
void MessageGenerator::
@@ -2420,8 +3021,10 @@ GenerateByteSize(io::Printer* printer) {
"classname", classname_);
GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file()));
printer->Print(
+ "if (_internal_metadata_.have_unknown_fields()) {\n"
" total_size += ::google::protobuf::internal::WireFormat::\n"
- " ComputeUnknownMessageSetItemsSize(unknown_fields());\n");
+ " ComputeUnknownMessageSetItemsSize(unknown_fields());\n"
+ "}\n");
printer->Print(
" GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
" _cached_size_ = total_size;\n"
@@ -2431,6 +3034,33 @@ GenerateByteSize(io::Printer* printer) {
return;
}
+ if (num_required_fields_ > 1 && HasFieldPresence(descriptor_->file())) {
+ // Emit a function (rarely used, we hope) that handles the required fields
+ // by checking for each one individually.
+ printer->Print(
+ "int $classname$::RequiredFieldsByteSizeFallback() const {\n",
+ "classname", classname_);
+ printer->Indent();
+ printer->Print("int total_size = 0;\n");
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (field->is_required()) {
+ printer->Print("\n"
+ "if (has_$name$()) {\n",
+ "name", FieldName(field));
+ printer->Indent();
+ PrintFieldComment(printer, field);
+ field_generators_.get(field).GenerateByteSize(printer);
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+ }
+ printer->Print("\n"
+ "return total_size;\n");
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+
printer->Print(
"int $classname$::ByteSize() const {\n",
"classname", classname_);
@@ -2439,45 +3069,121 @@ GenerateByteSize(io::Printer* printer) {
"int total_size = 0;\n"
"\n");
- int last_index = -1;
+ // 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
+ // present then the fast path executes; otherwise the slow path executes.
+ if (num_required_fields_ > 1 && HasFieldPresence(descriptor_->file())) {
+ // The fast path works if all required fields are present.
+ vector<uint32> masks_for_has_bits = RequiredFieldsBitMask(descriptor_);
+ printer->Print((string("if (") +
+ ConditionalToCheckBitmasks(masks_for_has_bits) +
+ ") { // All required fields are present.\n").c_str());
+ printer->Indent();
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (!field->is_required()) continue;
+ PrintFieldComment(printer, field);
+ field_generators_.get(field).GenerateByteSize(printer);
+ printer->Print("\n");
+ }
+ printer->Outdent();
+ printer->Print("} else {\n" // the slow path
+ " total_size += RequiredFieldsByteSizeFallback();\n"
+ "}\n");
+ } else {
+ // num_required_fields_ <= 1: no need to be tricky
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (!field->is_required()) continue;
+ PrintFieldComment(printer, field);
+ printer->Print("if (has_$name$()) {\n",
+ "name", FieldName(field));
+ printer->Indent();
+ field_generators_.get(field).GenerateByteSize(printer);
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+ }
+ // Handle optional fields (worry below about repeateds, oneofs, etc.).
+ // These are handled in chunks of 8. The first chunk is
+ // the non-requireds-non-repeateds-non-unions-non-extensions in
+ // descriptor_->field(0), descriptor_->field(1), ... descriptor_->field(7),
+ // and the second chunk is the same for
+ // descriptor_->field(8), descriptor_->field(9), ... descriptor_->field(15),
+ // etc.
+ hash_map<int, uint32> fields_mask_for_chunk;
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
+ if (!field->is_required() && !field->is_repeated() &&
+ !field->containing_oneof()) {
+ fields_mask_for_chunk[i / 8] |= static_cast<uint32>(1) << (i % 32);
+ }
+ }
- if (!field->is_repeated() && !field->containing_oneof()) {
+ int last_index = -1;
+ bool chunk_block_in_progress = false;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (!field->is_required() && !field->is_repeated() &&
+ !field->containing_oneof()) {
// See above in GenerateClear for an explanation of this.
// TODO(kenton): Share code? Unclear how to do so without
// over-engineering.
- if ((i / 8) != (last_index / 8) ||
- last_index < 0) {
- if (last_index >= 0) {
+ if (i / 8 != last_index / 8 || last_index < 0) {
+ // End previous chunk, if there was one.
+ if (chunk_block_in_progress) {
printer->Outdent();
printer->Print("}\n");
+ chunk_block_in_progress = false;
+ }
+ // Start chunk.
+ uint32 mask = fields_mask_for_chunk[i / 8];
+ int count = popcnt(mask);
+ GOOGLE_DCHECK_GE(count, 1);
+ if (count == 1) {
+ // No "if" here because the chunk is trivial.
+ } else {
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ "if (_has_bits_[$index$ / 32] & $mask$) {\n",
+ "index", SimpleItoa(i),
+ "mask", SimpleItoa(mask));
+ printer->Indent();
+ chunk_block_in_progress = true;
+ }
}
- printer->Print(
- "if (_has_bits_[$index$ / 32] & (0xffu << ($index$ % 32))) {\n",
- "index", SimpleItoa(field->index()));
- printer->Indent();
}
last_index = i;
PrintFieldComment(printer, field);
- printer->Print(
- "if (has_$name$()) {\n",
- "name", FieldName(field));
- printer->Indent();
+ bool have_enclosing_if = false;
+ if (HasFieldPresence(descriptor_->file())) {
+ printer->Print(
+ "if (has_$name$()) {\n",
+ "name", FieldName(field));
+ printer->Indent();
+ have_enclosing_if = true;
+ } else {
+ // Without field presence: field is serialized only if it has a
+ // non-default value.
+ have_enclosing_if = EmitFieldNonDefaultCondition(
+ printer, "this->", field);
+ }
field_generators_.get(field).GenerateByteSize(printer);
- printer->Outdent();
- printer->Print(
- "}\n"
- "\n");
+ if (have_enclosing_if) {
+ printer->Outdent();
+ printer->Print(
+ "}\n"
+ "\n");
+ }
}
}
- if (last_index >= 0) {
+ if (chunk_block_in_progress) {
printer->Outdent();
printer->Print("}\n");
}
@@ -2532,19 +3238,19 @@ GenerateByteSize(io::Printer* printer) {
"\n");
}
- if (UseUnknownFieldSet(descriptor_->file())) {
- printer->Print("if (!unknown_fields().empty()) {\n");
- printer->Indent();
- printer->Print(
- "total_size +=\n"
- " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
- " unknown_fields());\n");
- printer->Outdent();
- printer->Print("}\n");
- } else {
- printer->Print(
- "total_size += unknown_fields().size();\n"
- "\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (UseUnknownFieldSet(descriptor_->file())) {
+ printer->Print(
+ "if (_internal_metadata_.have_unknown_fields()) {\n"
+ " total_size +=\n"
+ " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n"
+ " unknown_fields());\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "total_size += unknown_fields().size();\n"
+ "\n");
+ }
}
// We update _cached_size_ even though this is a const method. In theory,
@@ -2569,27 +3275,29 @@ GenerateIsInitialized(io::Printer* printer) {
"classname", classname_);
printer->Indent();
- // Check that all required fields in this message are set. We can do this
- // most efficiently by checking 32 "has bits" at a time.
- int has_bits_array_size = (descriptor_->field_count() + 31) / 32;
- for (int i = 0; i < has_bits_array_size; i++) {
- uint32 mask = 0;
- for (int bit = 0; bit < 32; bit++) {
- int index = i * 32 + bit;
- if (index >= descriptor_->field_count()) break;
- const FieldDescriptor* field = descriptor_->field(index);
-
- if (field->is_required()) {
- mask |= 1 << bit;
+ if (HasFieldPresence(descriptor_->file())) {
+ // Check that all required fields in this message are set. We can do this
+ // most efficiently by checking 32 "has bits" at a time.
+ int has_bits_array_size = (descriptor_->field_count() + 31) / 32;
+ for (int i = 0; i < has_bits_array_size; i++) {
+ uint32 mask = 0;
+ for (int bit = 0; bit < 32; bit++) {
+ int index = i * 32 + bit;
+ if (index >= descriptor_->field_count()) break;
+ const FieldDescriptor* field = descriptor_->field(index);
+
+ if (field->is_required()) {
+ mask |= 1 << bit;
+ }
}
- }
- if (mask != 0) {
- char buffer[kFastToBufferSize];
- printer->Print(
- "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n",
- "i", SimpleItoa(i),
- "mask", FastHex32ToBuffer(mask, buffer));
+ if (mask != 0) {
+ char buffer[kFastToBufferSize];
+ printer->Print(
+ "if ((_has_bits_[$i$] & 0x$mask$) != 0x$mask$) return false;\n",
+ "i", SimpleItoa(i),
+ "mask", FastHex32ToBuffer(mask, buffer));
+ }
}
}
@@ -2606,14 +3314,14 @@ GenerateIsInitialized(io::Printer* printer) {
" return false;\n",
"name", FieldName(field));
} else {
- if (field->options().weak()) {
- // For weak fields, use the data member (google::protobuf::Message*) instead
+ if (field->options().weak() || !field->containing_oneof()) {
+ // For weak fields, use the data member (::google::protobuf::Message*) instead
// of the getter to avoid a link dependency on the weak message type
// which is only forward declared.
printer->Print(
- "if (has_$name$()) {\n"
- " if (!this->$name$_->IsInitialized()) return false;\n"
- "}\n",
+ "if (has_$name$()) {\n"
+ " if (!this->$name$_->IsInitialized()) return false;\n"
+ "}\n",
"name", FieldName(field));
} else {
printer->Print(
diff --git a/src/google/protobuf/compiler/cpp/cpp_message.h b/src/google/protobuf/compiler/cpp/cpp_message.h
index bfd3cec1..dfbc9af5 100644
--- a/src/google/protobuf/compiler/cpp/cpp_message.h
+++ b/src/google/protobuf/compiler/cpp/cpp_message.h
@@ -36,6 +36,9 @@
#define GOOGLE_PROTOBUF_COMPILER_CPP_MESSAGE_H__
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <vector>
#include <google/protobuf/compiler/cpp/cpp_field.h>
@@ -58,8 +61,7 @@ class ExtensionGenerator; // extension.h
class MessageGenerator {
public:
// See generator.cc for the meaning of dllexport_decl.
- explicit MessageGenerator(const Descriptor* descriptor,
- const Options& options);
+ MessageGenerator(const Descriptor* descriptor, const Options& options);
~MessageGenerator();
// Header stuff.
@@ -130,6 +132,8 @@ class MessageGenerator {
void GenerateSharedConstructorCode(io::Printer* printer);
// Generate the shared destructor code.
void GenerateSharedDestructorCode(io::Printer* printer);
+ // Generate the arena-specific destructor code.
+ void GenerateArenaDestructorCode(io::Printer* printer);
// Generate standard Message methods.
void GenerateClear(io::Printer* printer);
@@ -159,9 +163,10 @@ class MessageGenerator {
Options options_;
FieldGeneratorMap field_generators_;
vector< vector<string> > runs_of_fields_; // that might be trivially cleared
- scoped_array<scoped_ptr<MessageGenerator> > nested_generators_;
- scoped_array<scoped_ptr<EnumGenerator> > enum_generators_;
- scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > nested_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<EnumGenerator> > enum_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
+ int num_required_fields_;
bool uses_string_;
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 6ac15a5a..b3cd0ba1 100644
--- a/src/google/protobuf/compiler/cpp/cpp_message_field.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_message_field.cc
@@ -49,6 +49,10 @@ void SetMessageVariables(const FieldDescriptor* descriptor,
const Options& options) {
SetCommonFieldVariables(descriptor, variables, options);
(*variables)["type"] = FieldMessageTypeName(descriptor);
+ if (descriptor->options().weak() || !descriptor->containing_oneof()) {
+ (*variables)["non_null_ptr_to_name"] =
+ StrCat("this->", (*variables)["name"], "_");
+ }
(*variables)["stream_writer"] = (*variables)["declared_type"] +
(HasFastArraySerialization(descriptor->message_type()->file()) ?
"MaybeToArray" :
@@ -86,6 +90,12 @@ GenerateAccessorDeclarations(io::Printer* printer) const {
"inline $type$* mutable_$name$()$deprecation$;\n"
"inline $type$* $release_name$()$deprecation$;\n"
"inline void set_allocated_$name$($type$* $name$)$deprecation$;\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "inline $type$* unsafe_arena_release_$name$()$deprecation$;\n"
+ "inline void unsafe_arena_set_allocated_$name$(\n"
+ " $type$* $name$)$deprecation$;\n");
+ }
}
void MessageFieldGenerator::
@@ -101,36 +111,157 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const {
// Without.
" return $name$_ != NULL ? *$name$_ : *default_instance().$name$_;\n");
- printer->Print(variables_,
- "}\n"
- "inline $type$* $classname$::mutable_$name$() {\n"
- " set_has_$name$();\n"
- " if ($name$_ == NULL) $name$_ = new $type$;\n"
- " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
- " return $name$_;\n"
- "}\n"
- "inline $type$* $classname$::$release_name$() {\n"
- " clear_has_$name$();\n"
- " $type$* temp = $name$_;\n"
- " $name$_ = NULL;\n"
- " return temp;\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
- " delete $name$_;\n"
- " $name$_ = $name$;\n"
- " if ($name$) {\n"
- " set_has_$name$();\n"
- " } else {\n"
- " clear_has_$name$();\n"
- " }\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "}\n"
+ "inline $type$* $classname$::mutable_$name$() {\n"
+ " $set_hasbit$\n"
+ " if ($name$_ == NULL) {\n");
+ if (SupportsArenas(descriptor_->message_type())) {
+ printer->Print(variables_,
+ " $name$_ = ::google::protobuf::Arena::CreateMessage< $type$ >(\n"
+ " GetArenaNoVirtual());\n");
+ } else {
+ printer->Print(variables_,
+ " $name$_ = ::google::protobuf::Arena::Create< $type$ >(\n"
+ " GetArenaNoVirtual());\n");
+ }
+ printer->Print(variables_, " }\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $name$_;\n"
+ "}\n"
+ "inline $type$* $classname$::$release_name$() {\n"
+ " $clear_hasbit$\n"
+ " if (GetArenaNoVirtual() != NULL) {\n"
+ " if ($name$_ == NULL) {\n"
+ " return NULL;\n"
+ " } else {\n"
+ " $type$* temp = new $type$;\n"
+ " temp->MergeFrom(*$name$_);\n"
+ " $name$_ = NULL;\n"
+ " return temp;\n"
+ " }\n"
+ " } else {\n"
+ " $type$* temp = $name$_;\n"
+ " $name$_ = NULL;\n"
+ " return temp;\n"
+ " }\n"
+ "}\n"
+ "inline $type$* $classname$::unsafe_arena_release_$name$() {\n"
+ " $clear_hasbit$\n"
+ " $type$* temp = $name$_;\n"
+ " $name$_ = NULL;\n"
+ " return temp;\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
+ " if (GetArenaNoVirtual() == NULL) {\n"
+ " delete $name$_;\n"
+ " }\n"
+ " if ($name$ != NULL) {\n");
+ if (SupportsArenas(descriptor_->message_type())) {
+ // If we're on an arena and the incoming message is not, simply Own() it
+ // rather than copy to the arena -- either way we need a heap dealloc,
+ // so we might as well defer it. Otherwise, if incoming message is on a
+ // different ownership domain (specific arena, or the heap) than we are,
+ // copy to our arena (or heap, as the case may be).
+ printer->Print(variables_,
+ " if (GetArenaNoVirtual() != NULL && \n"
+ " ::google::protobuf::Arena::GetArena($name$) == NULL) {\n"
+ " GetArenaNoVirtual()->Own($name$);\n"
+ " } else if (GetArenaNoVirtual() !=\n"
+ " ::google::protobuf::Arena::GetArena($name$)) {\n"
+ " $type$* new_$name$ = \n"
+ " ::google::protobuf::Arena::CreateMessage< $type$ >(\n"
+ " GetArenaNoVirtual());\n"
+ " new_$name$->CopyFrom(*$name$);\n"
+ " $name$ = new_$name$;\n"
+ " }\n");
+ } else {
+ printer->Print(variables_,
+ " if (GetArenaNoVirtual() != NULL) {\n"
+ " GetArenaNoVirtual()->Own($name$);\n"
+ " }\n");
+ }
+
+ printer->Print(variables_,
+ " }\n"
+ " $name$_ = $name$;\n"
+ " if ($name$) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::unsafe_arena_set_allocated_$name$(\n"
+ " $type$* $name$) {\n"
+ // 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"
+ " delete $name$_;\n"
+ " }\n"
+ " $name$_ = $name$;\n"
+ " if ($name$) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_unsafe_arena_set_allocated"
+ ":$full_name$)\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "}\n"
+ "inline $type$* $classname$::mutable_$name$() {\n"
+ " $set_hasbit$\n"
+ " if ($name$_ == NULL) {\n"
+ " $name$_ = new $type$;\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $name$_;\n"
+ "}\n"
+ "inline $type$* $classname$::$release_name$() {\n"
+ " $clear_hasbit$\n"
+ " $type$* temp = $name$_;\n"
+ " $name$_ = NULL;\n"
+ " return temp;\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
+ " delete $name$_;\n");
+
+ if (SupportsArenas(descriptor_->message_type())) {
+ printer->Print(variables_,
+ " if ($name$ != NULL && $name$->GetArena() != NULL) {\n"
+ " $type$* new_$name$ = new $type$;\n"
+ " new_$name$->CopyFrom(*$name$);\n"
+ " $name$ = new_$name$;\n"
+ " }\n");
+ }
+
+ printer->Print(variables_,
+ " $name$_ = $name$;\n"
+ " if ($name$) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
+ }
}
void MessageFieldGenerator::
GenerateClearingCode(io::Printer* printer) const {
- printer->Print(variables_,
- "if ($name$_ != NULL) $name$_->$type$::Clear();\n");
+ 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.
+ printer->Print(variables_,
+ "if ($name$_ != NULL) delete $name$_;\n"
+ "$name$_ = NULL;\n");
+ } else {
+ printer->Print(variables_,
+ "if ($name$_ != NULL) $name$_->$type$::Clear();\n");
+ }
}
void MessageFieldGenerator::
@@ -166,7 +297,7 @@ void MessageFieldGenerator::
GenerateSerializeWithCachedSizes(io::Printer* printer) const {
printer->Print(variables_,
"::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
- " $number$, this->$name$(), output);\n");
+ " $number$, *$non_null_ptr_to_name$, output);\n");
}
void MessageFieldGenerator::
@@ -174,7 +305,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
printer->Print(variables_,
"target = ::google::protobuf::internal::WireFormatLite::\n"
" Write$declared_type$NoVirtualToArray(\n"
- " $number$, this->$name$(), target);\n");
+ " $number$, *$non_null_ptr_to_name$, target);\n");
}
void MessageFieldGenerator::
@@ -182,7 +313,7 @@ GenerateByteSize(io::Printer* printer) const {
printer->Print(variables_,
"total_size += $tag_size$ +\n"
" ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n"
- " this->$name$());\n");
+ " *$non_null_ptr_to_name$);\n");
}
// ===================================================================
@@ -198,43 +329,165 @@ MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {}
void MessageOneofFieldGenerator::
GenerateInlineAccessorDefinitions(io::Printer* printer) const {
- printer->Print(variables_,
- "inline const $type$& $classname$::$name$() const {\n"
- " return has_$name$() ? *$oneof_prefix$$name$_\n"
- " : $type$::default_instance();\n"
- "}\n"
- "inline $type$* $classname$::mutable_$name$() {\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = new $type$;\n"
- " }\n"
- " return $oneof_prefix$$name$_;\n"
- "}\n"
- "inline $type$* $classname$::$release_name$() {\n"
- " if (has_$name$()) {\n"
- " clear_has_$oneof_name$();\n"
- " $type$* temp = $oneof_prefix$$name$_;\n"
- " $oneof_prefix$$name$_ = NULL;\n"
- " return temp;\n"
- " } else {\n"
- " return NULL;\n"
- " }\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
- " clear_$oneof_name$();\n"
- " if ($name$) {\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = $name$;\n"
- " }\n"
- "}\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "inline const $type$& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " return has_$name$() ? *$oneof_prefix$$name$_\n"
+ " : $type$::default_instance();\n"
+ "}\n"
+ "inline $type$* $classname$::mutable_$name$() {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n");
+ if (SupportsArenas(descriptor_->message_type())) {
+ printer->Print(variables_,
+ " $oneof_prefix$$name$_ = \n"
+ " ::google::protobuf::Arena::CreateMessage< $type$ >(\n"
+ " GetArenaNoVirtual());\n");
+ } else {
+ printer->Print(variables_,
+ " $oneof_prefix$$name$_ = \n"
+ " ::google::protobuf::Arena::Create< $type$ >(\n"
+ " GetArenaNoVirtual());\n");
+ }
+ printer->Print(variables_,
+ " }\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $oneof_prefix$$name$_;\n"
+ "}\n"
+ "inline $type$* $classname$::$release_name$() {\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " if (GetArenaNoVirtual() != NULL) {\n"
+ // N.B.: safe to use the underlying field pointer here because we are sure
+ // that it is non-NULL (because has_$name$() returned true).
+ " $type$* temp = new $type$;\n"
+ " temp->MergeFrom(*$oneof_prefix$$name$_);\n"
+ " $oneof_prefix$$name$_ = NULL;\n"
+ " return temp;\n"
+ " } else {\n"
+ " $type$* temp = $oneof_prefix$$name$_;\n"
+ " $oneof_prefix$$name$_ = NULL;\n"
+ " return temp;\n"
+ " }\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline $type$* $classname$::unsafe_arena_release_$name$() {\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " $type$* temp = $oneof_prefix$$name$_;\n"
+ " $oneof_prefix$$name$_ = NULL;\n"
+ " return temp;\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
+ " clear_$oneof_name$();\n"
+ " if ($name$) {\n");
+
+ if (SupportsArenas(descriptor_->message_type())) {
+ printer->Print(variables_,
+ // If incoming message is on the heap and we are on an arena, just Own()
+ // it (see above). If it's on a different arena than we are or one of us
+ // is on the heap, we make a copy to our arena/heap.
+ " if (GetArenaNoVirtual() != NULL &&\n"
+ " ::google::protobuf::Arena::GetArena($name$) == NULL) {\n"
+ " GetArenaNoVirtual()->Own($name$);\n"
+ " } else if (GetArenaNoVirtual() !=\n"
+ " ::google::protobuf::Arena::GetArena($name$)) {\n"
+ " $type$* new_$name$ = \n"
+ " ::google::protobuf::Arena::CreateMessage< $type$ >(\n"
+ " GetArenaNoVirtual());\n"
+ " new_$name$->CopyFrom(*$name$);\n"
+ " $name$ = new_$name$;\n"
+ " }\n");
+ } else {
+ printer->Print(variables_,
+ " if (GetArenaNoVirtual() != NULL) {\n"
+ " GetArenaNoVirtual()->Own($name$);\n"
+ " }\n");
+ }
+
+ printer->Print(variables_,
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_ = $name$;\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::unsafe_arena_set_allocated_$name$("
+ "$type$* $name$) {\n"
+ // 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.
+ " clear_$oneof_name$();\n"
+ " if ($name$) {\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_ = $name$;\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
+ "$full_name$)\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "inline const $type$& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " return has_$name$() ? *$oneof_prefix$$name$_\n"
+ " : $type$::default_instance();\n"
+ "}\n"
+ "inline $type$* $classname$::mutable_$name$() {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_ = new $type$;\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $oneof_prefix$$name$_;\n"
+ "}\n"
+ "inline $type$* $classname$::$release_name$() {\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " $type$* temp = $oneof_prefix$$name$_;\n"
+ " $oneof_prefix$$name$_ = NULL;\n"
+ " return temp;\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
+ " clear_$oneof_name$();\n"
+ " if ($name$) {\n");
+ if (SupportsArenas(descriptor_->message_type())) {
+ printer->Print(variables_,
+ " if ($name$->GetArena() != NULL) {\n"
+ " $type$* new_$name$ = new $type$;\n"
+ " new_$name$->CopyFrom(*$name$);\n"
+ " $name$ = new_$name$;\n"
+ " }\n");
+ }
+ printer->Print(variables_,
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_ = $name$;\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
+ }
}
void MessageOneofFieldGenerator::
GenerateClearingCode(io::Printer* printer) const {
- // if it is the active field, it cannot be NULL.
- printer->Print(variables_,
- "delete $oneof_prefix$$name$_;\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "if (GetArenaNoVirtual() == NULL) {\n"
+ " delete $oneof_prefix$$name$_;\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "delete $oneof_prefix$$name$_;\n");
+ }
}
void MessageOneofFieldGenerator::
@@ -318,7 +571,7 @@ GenerateMergingCode(io::Printer* printer) const {
void RepeatedMessageFieldGenerator::
GenerateSwappingCode(io::Printer* printer) const {
- printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
+ printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
}
void RepeatedMessageFieldGenerator::
@@ -342,7 +595,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
void RepeatedMessageFieldGenerator::
GenerateSerializeWithCachedSizes(io::Printer* printer) const {
printer->Print(variables_,
- "for (int i = 0; i < this->$name$_size(); i++) {\n"
+ "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n"
" ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
" $number$, this->$name$(i), output);\n"
"}\n");
@@ -351,7 +604,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const {
void RepeatedMessageFieldGenerator::
GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
printer->Print(variables_,
- "for (int i = 0; i < this->$name$_size(); i++) {\n"
+ "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n"
" target = ::google::protobuf::internal::WireFormatLite::\n"
" Write$declared_type$NoVirtualToArray(\n"
" $number$, this->$name$(i), target);\n"
diff --git a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc
index 2dbf14ce..d1efbfe6 100644
--- a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc
@@ -35,6 +35,9 @@
// worth.
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/cpp/cpp_generator.h>
#include <google/protobuf/compiler/command_line_interface.h>
@@ -69,12 +72,106 @@ class TestGenerator : public CodeGenerator {
TryInsert("test.pb.cc", "includes", context);
TryInsert("test.pb.cc", "namespace_scope", context);
TryInsert("test.pb.cc", "global_scope", context);
+
+ // Check field accessors for an optional int32:
+ TryInsert("test.pb.h", "field_get:foo.Bar.optInt", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.optInt", context);
+
+ // Check field accessors for a repeated int32:
+ TryInsert("test.pb.h", "field_get:foo.Bar.repeatedInt", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.repeatedInt", context);
+
+ // Check field accessors for a required string:
+ TryInsert("test.pb.h", "field_get:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_set_allocated:foo.Bar.requiredString",
+ context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.requiredString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.requiredString", context);
+
+ // Check field accessors for a repeated string:
+ TryInsert("test.pb.h", "field_get:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.repeatedString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.repeatedString", context);
+
+ // Check field accessors for an int inside oneof{}:
+ TryInsert("test.pb.h", "field_get:foo.Bar.oneOfInt", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.oneOfInt", context);
+
+ // Check field accessors for a string inside oneof{}:
+ TryInsert("test.pb.h", "field_get:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set_allocated:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set_char:foo.Bar.oneOfString", context);
+ TryInsert("test.pb.h", "field_set_pointer:foo.Bar.oneOfString", context);
+
+ // Check field accessors for an optional message:
+ TryInsert("test.pb.h", "field_get:foo.Bar.optMessage", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.optMessage", context);
+ TryInsert("test.pb.h", "field_set_allocated:foo.Bar.optMessage", context);
+
+ // Check field accessors for a repeated message:
+ TryInsert("test.pb.h", "field_add:foo.Bar.repeatedMessage", context);
+ TryInsert("test.pb.h", "field_get:foo.Bar.repeatedMessage", context);
+ TryInsert("test.pb.h", "field_list:foo.Bar.repeatedMessage", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedMessage", context);
+ TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedMessage",
+ context);
+
+ // 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.h", "field_set_allocated:foo.Bar.oneOfMessage", context);
+
+ // Check field accessors for an optional enum:
+ TryInsert("test.pb.h", "field_get:foo.Bar.optEnum", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.optEnum", context);
+
+ // Check field accessors for a repeated enum:
+ TryInsert("test.pb.h", "field_get:foo.Bar.repeatedEnum", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.repeatedEnum", context);
+ TryInsert("test.pb.h", "field_add:foo.Bar.repeatedEnum", context);
+ TryInsert("test.pb.h", "field_list:foo.Bar.repeatedEnum", context);
+ TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedEnum", context);
+
+ // Check field accessors for an enum inside oneof{}:
+ TryInsert("test.pb.h", "field_get:foo.Bar.oneOfEnum", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.oneOfEnum", context);
+
+ // Check field accessors for a required cord:
+ TryInsert("test.pb.h", "field_get:foo.Bar.requiredCord", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.requiredCord", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.requiredCord", context);
+
+ // Check field accessors for a repeated cord:
+ TryInsert("test.pb.h", "field_get:foo.Bar.repeatedCord", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.repeatedCord", context);
+ TryInsert("test.pb.h", "field_add:foo.Bar.repeatedCord", context);
+ TryInsert("test.pb.h", "field_list:foo.Bar.repeatedCord", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.repeatedCord", context);
+ TryInsert("test.pb.h", "field_mutable_list:foo.Bar.repeatedCord", context);
+
+ // Check field accessors for a cord inside oneof{}:
+ TryInsert("test.pb.h", "field_get:foo.Bar.oneOfCord", context);
+ TryInsert("test.pb.h", "field_set:foo.Bar.oneOfCord", context);
+ TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfCord", context);
+
return true;
}
void TryInsert(const string& filename, const string& insertion_point,
GeneratorContext* context) const {
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->OpenForInsert(filename, insertion_point));
io::Printer printer(output.get(), '$');
printer.Print("// inserted $name$\n", "name", insertion_point);
@@ -88,8 +185,39 @@ TEST(CppPluginTest, PluginTest) {
GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
"syntax = \"proto2\";\n"
"package foo;\n"
+ "\n"
+ "enum Thud { VALUE = 0; }\n"
+ "\n"
"message Bar {\n"
" message Baz {}\n"
+ " optional int32 optInt = 1;\n"
+ " repeated int32 repeatedInt = 2;\n"
+ "\n"
+ " required string requiredString = 3;\n"
+ " repeated string repeatedString = 4;\n"
+ "\n"
+ " optional Baz optMessage = 6;\n"
+ " repeated Baz repeatedMessage = 7;\n"
+ "\n"
+ " optional Thud optEnum = 8;\n"
+ " repeated Thud repeatedEnum = 9;\n"
+ "\n"
+ " required string requiredCord = 10 [\n"
+ " ctype = CORD\n"
+ " ];\n"
+ " repeated string repeatedCord = 11 [\n"
+ " ctype = CORD\n"
+ " ];\n"
+ "\n"
+ " oneof Qux {\n"
+ " int64 oneOfInt = 20;\n"
+ " string oneOfString = 21;\n"
+ " Baz oneOfMessage = 22;\n"
+ " Thud oneOfEnum = 23;"
+ " string oneOfCord = 24 [\n"
+ " ctype = CORD\n"
+ " ];\n"
+ " }\n"
"}\n",
true));
diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc
index 44290a31..9a2c930e 100644
--- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc
@@ -129,7 +129,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const {
" return $name$_;\n"
"}\n"
"inline void $classname$::set_$name$($type$ value) {\n"
- " set_has_$name$();\n"
+ " $set_hasbit$\n"
" $name$_ = value;\n"
" // @@protoc_insertion_point(field_set:$full_name$)\n"
"}\n");
@@ -161,7 +161,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
"DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
" $type$, $wire_format_field_type$>(\n"
" input, &$name$_)));\n"
- "set_has_$name$();\n");
+ "$set_hasbit$\n");
}
void PrimitiveFieldGenerator::
@@ -207,6 +207,7 @@ void PrimitiveOneofFieldGenerator::
GenerateInlineAccessorDefinitions(io::Printer* printer) const {
printer->Print(variables_,
"inline $type$ $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
" if (has_$name$()) {\n"
" return $oneof_prefix$$name$_;\n"
" }\n"
@@ -218,6 +219,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const {
" set_has_$name$();\n"
" }\n"
" $oneof_prefix$$name$_ = value;\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
"}\n");
}
@@ -330,7 +332,7 @@ GenerateMergingCode(io::Printer* printer) const {
void RepeatedPrimitiveFieldGenerator::
GenerateSwappingCode(io::Printer* printer) const {
- printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
+ printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
}
void RepeatedPrimitiveFieldGenerator::
diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc
index 180d236b..04ed08c9 100644
--- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc
@@ -52,9 +52,14 @@ void SetStringVariables(const FieldDescriptor* descriptor,
(*variables)["default"] = DefaultValue(descriptor);
(*variables)["default_length"] =
SimpleItoa(descriptor->default_value_string().length());
- (*variables)["default_variable"] = descriptor->default_value_string().empty()
- ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()"
- : "_default_" + FieldName(descriptor) + "_";
+ string default_variable_string =
+ descriptor->default_value_string().empty()
+ ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()"
+ : "_default_" + FieldName(descriptor) + "_";
+ (*variables)["default_variable"] = default_variable_string;
+ (*variables)["default_value_init"] =
+ descriptor->default_value_string().empty()
+ ? "" : "*" + default_variable_string;
(*variables)["pointer_type"] =
descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
// NOTE: Escaped here to unblock proto1->proto2 migration.
@@ -63,6 +68,8 @@ void SetStringVariables(const FieldDescriptor* descriptor,
SafeFunctionName(descriptor->containing_type(),
descriptor, "release_");
(*variables)["full_name"] = descriptor->full_name();
+
+ (*variables)["string_piece"] = "::std::string";
}
} // namespace
@@ -80,7 +87,19 @@ StringFieldGenerator::~StringFieldGenerator() {}
void StringFieldGenerator::
GeneratePrivateMembers(io::Printer* printer) const {
- printer->Print(variables_, "::std::string* $name$_;\n");
+ // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for
+ // string fields, even when SupportArenas(descriptor_) == false. Why?
+ // The simple answer is to avoid unmaintainable complexity. The reflection
+ // code assumes ArenaStringPtrs. These are *almost* in-memory-compatible with
+ // string*, except for the pointer tags and related ownership semantics. We
+ // could modify the runtime code to use string* for the not-supporting-arenas
+ // case, but this would require a way to detect which type of class was
+ // generated (adding overhead and complexity to GeneratedMessageReflection)
+ // and littering the runtime code paths with conditionals. It's simpler to
+ // stick with this but use lightweight accessors that assume arena == NULL.
+ // There should be very little overhead anyway because it's just a tagged
+ // pointer in-memory.
+ printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name$_;\n");
}
void StringFieldGenerator::
@@ -125,6 +144,12 @@ GenerateAccessorDeclarations(io::Printer* printer) const {
"inline ::std::string* mutable_$name$()$deprecation$;\n"
"inline ::std::string* $release_name$()$deprecation$;\n"
"inline void set_allocated_$name$(::std::string* $name$)$deprecation$;\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "inline ::std::string* unsafe_arena_release_$name$()$deprecation$;\n"
+ "inline void unsafe_arena_set_allocated_$name$(\n"
+ " ::std::string* $name$)$deprecation$;\n");
+ }
if (descriptor_->options().ctype() != FieldOptions::STRING) {
@@ -136,74 +161,113 @@ GenerateAccessorDeclarations(io::Printer* printer) const {
void StringFieldGenerator::
GenerateInlineAccessorDefinitions(io::Printer* printer) const {
- printer->Print(variables_,
- "inline const ::std::string& $classname$::$name$() const {\n"
- " // @@protoc_insertion_point(field_get:$full_name$)\n"
- " return *$name$_;\n"
- "}\n"
- "inline void $classname$::set_$name$(const ::std::string& value) {\n"
- " set_has_$name$();\n"
- " if ($name$_ == $default_variable$) {\n"
- " $name$_ = new ::std::string;\n"
- " }\n"
- " $name$_->assign(value);\n"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- " set_has_$name$();\n"
- " if ($name$_ == $default_variable$) {\n"
- " $name$_ = new ::std::string;\n"
- " }\n"
- " $name$_->assign(value);\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n"
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n"
- " set_has_$name$();\n"
- " if ($name$_ == $default_variable$) {\n"
- " $name$_ = new ::std::string;\n"
- " }\n"
- " $name$_->assign(reinterpret_cast<const char*>(value), size);\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline ::std::string* $classname$::mutable_$name$() {\n"
- " set_has_$name$();\n"
- " if ($name$_ == $default_variable$) {\n");
- if (descriptor_->default_value_string().empty()) {
+ if (SupportsArenas(descriptor_)) {
printer->Print(variables_,
- " $name$_ = new ::std::string;\n");
+ "inline const ::std::string& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " return $name$_.Get($default_variable$);\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const ::std::string& value) {\n"
+ " $set_hasbit$\n"
+ " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const char* value) {\n"
+ " $set_hasbit$\n"
+ " $name$_.Set($default_variable$, $string_piece$(value),\n"
+ " GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
+ "}\n"
+ "inline "
+ "void $classname$::set_$name$(const $pointer_type$* value,\n"
+ " size_t size) {\n"
+ " $set_hasbit$\n"
+ " $name$_.Set($default_variable$, $string_piece$(\n"
+ " reinterpret_cast<const char*>(value), size), GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
+ "}\n"
+ "inline ::std::string* $classname$::mutable_$name$() {\n"
+ " $set_hasbit$\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n"
+ "}\n"
+ "inline ::std::string* $classname$::$release_name$() {\n"
+ " $clear_hasbit$\n"
+ " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n"
+ "}\n"
+ "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
+ " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
+ " $clear_hasbit$\n"
+ " return $name$_.UnsafeArenaRelease($default_variable$,\n"
+ " GetArenaNoVirtual());\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
+ " if ($name$ != NULL) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " $name$_.SetAllocated($default_variable$, $name$,\n"
+ " GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::unsafe_arena_set_allocated_$name$(\n"
+ " ::std::string* $name$) {\n"
+ " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
+ " if ($name$ != NULL) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " $set_hasbit$\n"
+ " $name$_.UnsafeArenaSetAllocated($default_variable$,\n"
+ " $name$, GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
} else {
+ // No-arena case.
printer->Print(variables_,
- " $name$_ = new ::std::string(*$default_variable$);\n");
+ "inline const ::std::string& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " return $name$_.GetNoArena($default_variable$);\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const ::std::string& value) {\n"
+ " $set_hasbit$\n"
+ " $name$_.SetNoArena($default_variable$, value);\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const char* value) {\n"
+ " $set_hasbit$\n"
+ " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n"
+ " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
+ "}\n"
+ "inline "
+ "void $classname$::set_$name$(const $pointer_type$* value, "
+ "size_t size) {\n"
+ " $set_hasbit$\n"
+ " $name$_.SetNoArena($default_variable$,\n"
+ " $string_piece$(reinterpret_cast<const char*>(value), size));\n"
+ " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
+ "}\n"
+ "inline ::std::string* $classname$::mutable_$name$() {\n"
+ " $set_hasbit$\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $name$_.MutableNoArena($default_variable$);\n"
+ "}\n"
+ "inline ::std::string* $classname$::$release_name$() {\n"
+ " $clear_hasbit$\n"
+ " return $name$_.ReleaseNoArena($default_variable$);\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
+ " if ($name$ != NULL) {\n"
+ " $set_hasbit$\n"
+ " } else {\n"
+ " $clear_hasbit$\n"
+ " }\n"
+ " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
}
- printer->Print(variables_,
- " }\n"
- " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
- " return $name$_;\n"
- "}\n"
- "inline ::std::string* $classname$::$release_name$() {\n"
- " clear_has_$name$();\n"
- " if ($name$_ == $default_variable$) {\n"
- " return NULL;\n"
- " } else {\n"
- " ::std::string* temp = $name$_;\n"
- " $name$_ = const_cast< ::std::string*>($default_variable$);\n"
- " return temp;\n"
- " }\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
- " if ($name$_ != $default_variable$) {\n"
- " delete $name$_;\n"
- " }\n"
- " if ($name$) {\n"
- " set_has_$name$();\n"
- " $name$_ = $name$;\n"
- " } else {\n"
- " clear_has_$name$();\n"
- " $name$_ = const_cast< ::std::string*>($default_variable$);\n"
- " }\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
}
void StringFieldGenerator::
@@ -217,41 +281,61 @@ GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
void StringFieldGenerator::
GenerateClearingCode(io::Printer* printer) const {
- if (descriptor_->default_value_string().empty()) {
- printer->Print(variables_,
- "if ($name$_ != $default_variable$) {\n"
- " $name$_->clear();\n"
- "}\n");
+ // Two-dimension specialization here: supporting arenas or not, and default
+ // value is the empty string or not. Complexity here ensures the minimal
+ // number of branches / amount of extraneous code at runtime (given that the
+ // below methods are inlined one-liners)!
+ if (SupportsArenas(descriptor_)) {
+ if (descriptor_->default_value_string().empty()) {
+ printer->Print(variables_,
+ "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
+ } else {
+ printer->Print(variables_,
+ "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
+ }
} else {
- printer->Print(variables_,
- "if ($name$_ != $default_variable$) {\n"
- " $name$_->assign(*$default_variable$);\n"
- "}\n");
+ if (descriptor_->default_value_string().empty()) {
+ printer->Print(variables_,
+ "$name$_.ClearToEmptyNoArena($default_variable$);\n");
+ } else {
+ printer->Print(variables_,
+ "$name$_.ClearToDefaultNoArena($default_variable$);\n");
+ }
}
}
void StringFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
- printer->Print(variables_, "set_$name$(from.$name$());\n");
+ if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
+ // TODO(gpike): improve this
+ printer->Print(variables_, "set_$name$(from.$name$());\n");
+ } else {
+ printer->Print(variables_,
+ "$set_hasbit$\n"
+ "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
+ }
}
void StringFieldGenerator::
GenerateSwappingCode(io::Printer* printer) const {
- printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
+ printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
}
void StringFieldGenerator::
GenerateConstructorCode(io::Printer* printer) const {
printer->Print(variables_,
- "$name$_ = const_cast< ::std::string*>($default_variable$);\n");
+ "$name$_.UnsafeSetDefault($default_variable$);\n");
}
void StringFieldGenerator::
GenerateDestructorCode(io::Printer* printer) const {
- printer->Print(variables_,
- "if ($name$_ != $default_variable$) {\n"
- " delete $name$_;\n"
- "}\n");
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "$name$_.Destroy($default_variable$, GetArenaNoVirtual());\n");
+ } else {
+ printer->Print(variables_,
+ "$name$_.DestroyNoArena($default_variable$);\n");
+ }
}
void StringFieldGenerator::
@@ -276,13 +360,14 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
printer->Print(variables_,
"DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
" input, this->mutable_$name$()));\n");
+
if (HasUtf8Verification(descriptor_->file()) &&
descriptor_->type() == FieldDescriptor::TYPE_STRING) {
printer->Print(variables_,
"::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
" this->$name$().data(), this->$name$().length(),\n"
" ::google::protobuf::internal::WireFormat::PARSE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
}
@@ -294,7 +379,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const {
"::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
" this->$name$().data(), this->$name$().length(),\n"
" ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
printer->Print(variables_,
"::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAliased(\n"
@@ -309,7 +394,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
"::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
" this->$name$().data(), this->$name$().length(),\n"
" ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
printer->Print(variables_,
"target =\n"
@@ -338,84 +423,186 @@ StringOneofFieldGenerator::~StringOneofFieldGenerator() {}
void StringOneofFieldGenerator::
GenerateInlineAccessorDefinitions(io::Printer* printer) const {
- printer->Print(variables_,
- "inline const ::std::string& $classname$::$name$() const {\n"
- " if (has_$name$()) {\n"
- " return *$oneof_prefix$$name$_;\n"
- " }\n");
- if (descriptor_->default_value_string().empty()) {
+ if (SupportsArenas(descriptor_)) {
printer->Print(variables_,
- " return ::google::protobuf::internal::GetEmptyStringAlreadyInited();\n");
- } else {
- printer->Print(variables_,
- " return *$default_variable$;\n");
- }
- printer->Print(variables_,
- "}\n"
- "inline void $classname$::set_$name$(const ::std::string& value) {\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = new ::std::string;\n"
- " }\n"
- " $oneof_prefix$$name$_->assign(value);\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = new ::std::string;\n"
- " }\n"
- " $oneof_prefix$$name$_->assign(value);\n"
- "}\n"
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = new ::std::string;\n"
- " }\n"
- " $oneof_prefix$$name$_->assign(\n"
- " reinterpret_cast<const char*>(value), size);\n"
- "}\n"
- "inline ::std::string* $classname$::mutable_$name$() {\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n");
- if (descriptor_->default_value_string().empty()) {
- printer->Print(variables_,
- " $oneof_prefix$$name$_ = new ::std::string;\n");
+ "inline const ::std::string& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " if (has_$name$()) {\n"
+ " return $oneof_prefix$$name$_.Get($default_variable$);\n"
+ " }\n"
+ " return *$default_variable$;\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const ::std::string& value) {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.Set($default_variable$, value,\n"
+ " GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const char* value) {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.Set($default_variable$,\n"
+ " $string_piece$(value), GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
+ "}\n"
+ "inline "
+ "void $classname$::set_$name$(const $pointer_type$* value,\n"
+ " size_t size) {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.Set($default_variable$, $string_piece$(\n"
+ " reinterpret_cast<const char*>(value), size),\n"
+ " GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
+ "}\n"
+ "inline ::std::string* $classname$::mutable_$name$() {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " return $oneof_prefix$$name$_.Mutable($default_variable$,\n"
+ " GetArenaNoVirtual());\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ "}\n"
+ "inline ::std::string* $classname$::$release_name$() {\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " return $oneof_prefix$$name$_.Release($default_variable$,\n"
+ " GetArenaNoVirtual());\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
+ " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n"
+ " $default_variable$, GetArenaNoVirtual());\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
+ " if (!has_$name$()) {\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " clear_$oneof_name$();\n"
+ " if ($name$ != NULL) {\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.SetAllocated($default_variable$, $name$,\n"
+ " GetArenaNoVirtual());\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::unsafe_arena_set_allocated_$name$("
+ "::std::string* $name$) {\n"
+ " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
+ " if (!has_$name$()) {\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " clear_$oneof_name$();\n"
+ " if ($name$) {\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeArenaSetAllocated($default_variable$, "
+ "$name$, GetArenaNoVirtual());\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
} else {
+ // No-arena case.
printer->Print(variables_,
- " $oneof_prefix$$name$_ = new ::std::string(*$default_variable$);\n");
+ "inline const ::std::string& $classname$::$name$() const {\n"
+ " // @@protoc_insertion_point(field_get:$full_name$)\n"
+ " if (has_$name$()) {\n"
+ " return $oneof_prefix$$name$_.GetNoArena($default_variable$);\n"
+ " }\n"
+ " return *$default_variable$;\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const ::std::string& value) {\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.SetNoArena($default_variable$, value);\n"
+ " // @@protoc_insertion_point(field_set:$full_name$)\n"
+ "}\n"
+ "inline void $classname$::set_$name$(const char* value) {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.SetNoArena($default_variable$,\n"
+ " $string_piece$(value));\n"
+ " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
+ "}\n"
+ "inline "
+ "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " $oneof_prefix$$name$_.SetNoArena($default_variable$, $string_piece$(\n"
+ " reinterpret_cast<const char*>(value), size));\n"
+ " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
+ "}\n"
+ "inline ::std::string* $classname$::mutable_$name$() {\n"
+ " if (!has_$name$()) {\n"
+ " clear_$oneof_name$();\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
+ " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n"
+ "}\n"
+ "inline ::std::string* $classname$::$release_name$() {\n"
+ " if (has_$name$()) {\n"
+ " clear_has_$oneof_name$();\n"
+ " return $oneof_prefix$$name$_.ReleaseNoArena($default_variable$);\n"
+ " } else {\n"
+ " return NULL;\n"
+ " }\n"
+ "}\n"
+ "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
+ " if (!has_$name$()) {\n"
+ " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
+ " }\n"
+ " clear_$oneof_name$();\n"
+ " if ($name$ != NULL) {\n"
+ " set_has_$name$();\n"
+ " $oneof_prefix$$name$_.SetAllocatedNoArena($default_variable$,\n"
+ " $name$);\n"
+ " }\n"
+ " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
+ "}\n");
}
- printer->Print(variables_,
- " }\n"
- " return $oneof_prefix$$name$_;\n"
- "}\n"
- "inline ::std::string* $classname$::$release_name$() {\n"
- " if (has_$name$()) {\n"
- " clear_has_$oneof_name$();\n"
- " ::std::string* temp = $oneof_prefix$$name$_;\n"
- " $oneof_prefix$$name$_ = NULL;\n"
- " return temp;\n"
- " } else {\n"
- " return NULL;\n"
- " }\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
- " clear_$oneof_name$();\n"
- " if ($name$) {\n"
- " set_has_$name$();\n"
- " $oneof_prefix$$name$_ = $name$;\n"
- " }\n"
- "}\n");
}
void StringOneofFieldGenerator::
GenerateClearingCode(io::Printer* printer) const {
+ if (SupportsArenas(descriptor_)) {
+ printer->Print(variables_,
+ "$oneof_prefix$$name$_.Destroy($default_variable$,\n"
+ " GetArenaNoVirtual());\n");
+ } else {
printer->Print(variables_,
- "delete $oneof_prefix$$name$_;\n");
+ "$oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n");
+ }
}
void StringOneofFieldGenerator::
@@ -425,25 +612,44 @@ GenerateSwappingCode(io::Printer* printer) const {
void StringOneofFieldGenerator::
GenerateConstructorCode(io::Printer* printer) const {
- if (!descriptor_->default_value_string().empty()) {
+ printer->Print(variables_,
+ " $classname$_default_oneof_instance_->$name$_.UnsafeSetDefault("
+ "$default_variable$);\n");
+}
+
+void StringOneofFieldGenerator::
+GenerateDestructorCode(io::Printer* printer) const {
+ if (SupportsArenas(descriptor_)) {
printer->Print(variables_,
- " $classname$_default_oneof_instance_->$name$_ = "
- "$classname$::$default_variable$;\n");
+ "if (has_$name$()) {\n"
+ " $oneof_prefix$$name$_.Destroy($default_variable$,\n"
+ " GetArenaNoVirtual());\n"
+ "}\n");
} else {
printer->Print(variables_,
- " $classname$_default_oneof_instance_->$name$_ = "
- "$default_variable$;\n");
+ "if (has_$name$()) {\n"
+ " $oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n"
+ "}\n");
}
}
void StringOneofFieldGenerator::
-GenerateDestructorCode(io::Printer* printer) const {
- printer->Print(variables_,
- "if (has_$name$()) {\n"
- " delete $oneof_prefix$$name$_;\n"
- "}\n");
+GenerateMergeFromCodedStream(io::Printer* printer) const {
+ printer->Print(variables_,
+ "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
+ " input, this->mutable_$name$()));\n");
+
+ if (HasUtf8Verification(descriptor_->file()) &&
+ descriptor_->type() == FieldDescriptor::TYPE_STRING) {
+ printer->Print(variables_,
+ "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
+ " this->$name$().data(), this->$name$().length(),\n"
+ " ::google::protobuf::internal::WireFormat::PARSE,\n"
+ " \"$full_name$\");\n");
+ }
}
+
// ===================================================================
RepeatedStringFieldGenerator::
@@ -566,7 +772,7 @@ GenerateMergingCode(io::Printer* printer) const {
void RepeatedStringFieldGenerator::
GenerateSwappingCode(io::Printer* printer) const {
- printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
+ printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
}
void RepeatedStringFieldGenerator::
@@ -586,7 +792,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const {
" this->$name$(this->$name$_size() - 1).data(),\n"
" this->$name$(this->$name$_size() - 1).length(),\n"
" ::google::protobuf::internal::WireFormat::PARSE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
}
@@ -600,7 +806,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const {
"::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
" this->$name$(i).data(), this->$name$(i).length(),\n"
" ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
printer->Print(variables_,
" ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
@@ -618,7 +824,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
" ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
" this->$name$(i).data(), this->$name$(i).length(),\n"
" ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
- " \"$name$\");\n");
+ " \"$full_name$\");\n");
}
printer->Print(variables_,
" target = ::google::protobuf::internal::WireFormatLite::\n"
diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.h b/src/google/protobuf/compiler/cpp/cpp_string_field.h
index 86da38f2..0a5ca440 100644
--- a/src/google/protobuf/compiler/cpp/cpp_string_field.h
+++ b/src/google/protobuf/compiler/cpp/cpp_string_field.h
@@ -88,6 +88,7 @@ class StringOneofFieldGenerator : public StringFieldGenerator {
void GenerateSwappingCode(io::Printer* printer) const;
void GenerateConstructorCode(io::Printer* printer) const;
void GenerateDestructorCode(io::Printer* printer) const;
+ void GenerateMergeFromCodedStream(io::Printer* printer) const;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOneofFieldGenerator);
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 6b7f8308..4fa3c144 100644
--- a/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
+++ b/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
@@ -35,6 +35,7 @@
// 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.
+syntax = "proto2";
// Some generic_services option(s) added automatically.
// See: http://go/proto2-generic-services-default
@@ -58,7 +59,7 @@ message TestConflictingSymbolNames {
optional int32 total_size = 6;
optional int32 tag = 7;
- enum TestEnum { FOO = 1; }
+ enum TestEnum { FOO = 0; }
message Data1 { repeated int32 data = 1; }
message Data2 { repeated TestEnum data = 1; }
message Data3 { repeated string data = 1; }
@@ -99,6 +100,8 @@ message TestConflictingSymbolNames {
optional uint32 int = 30;
optional uint32 friend = 31;
optional uint32 class = 37;
+ optional uint32 typedecl = 39;
+ optional uint32 auto = 40;
// The generator used to #define a macro called "DO" inside the .cc file.
message DO {}
@@ -116,15 +119,18 @@ message TestConflictingSymbolNames {
// names.
optional DO release_do = 36;
- extensions 1000 to max;
-}
+ // For clashing local variables in Serialize and ByteSize calculation.
+ optional string target = 38;
-message TestConflictingSymbolNamesExtension {
- extend TestConflictingSymbolNames {
- repeated int32 repeated_int32_ext = 20423638 [packed=true];
- }
+ 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 DummyMessage {}
service TestConflictingMethodNames {
diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_unittest.cc
index 93e1c3f1..2a04b293 100644
--- a/src/google/protobuf/compiler/cpp/cpp_unittest.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_unittest.cc
@@ -47,6 +47,9 @@
#include <google/protobuf/compiler/cpp/cpp_unittest.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <vector>
#include <google/protobuf/unittest.pb.h>
@@ -150,6 +153,7 @@ TEST(GeneratedMessageTest, Defaults) {
&message.optional_import_message());
}
+#ifndef PROTOBUF_USE_DLLS
TEST(GeneratedMessageTest, Int32StringConversion) {
EXPECT_EQ("971", Int32ToString(971));
EXPECT_EQ("(~0x7fffffff)", Int32ToString(kint32min));
@@ -162,6 +166,7 @@ TEST(GeneratedMessageTest, Int64StringConversion) {
EXPECT_EQ("GOOGLE_LONGLONG(~0x7fffffffffffffff)", Int64ToString(kint64min));
EXPECT_EQ("GOOGLE_LONGLONG(9223372036854775807)", Int64ToString(kint64max));
}
+#endif // !PROTOBUF_USE_DLLS
TEST(GeneratedMessageTest, FloatingPointDefaults) {
const unittest::TestExtremeDefaultValues& extreme_default =
@@ -248,7 +253,7 @@ TEST(GeneratedMessageTest, ReleaseString) {
message.set_default_string("blah");
EXPECT_TRUE(message.has_default_string());
- scoped_ptr<string> str(message.release_default_string());
+ google::protobuf::scoped_ptr<string> str(message.release_default_string());
EXPECT_FALSE(message.has_default_string());
ASSERT_TRUE(str != NULL);
EXPECT_EQ("blah", *str);
@@ -267,7 +272,7 @@ TEST(GeneratedMessageTest, ReleaseMessage) {
EXPECT_FALSE(message.has_optional_nested_message());
message.mutable_optional_nested_message()->set_bb(1);
- scoped_ptr<unittest::TestAllTypes::NestedMessage> nest(
+ google::protobuf::scoped_ptr<unittest::TestAllTypes::NestedMessage> nest(
message.release_optional_nested_message());
EXPECT_FALSE(message.has_optional_nested_message());
ASSERT_TRUE(nest != NULL);
@@ -531,7 +536,7 @@ TEST(GeneratedMessageTest, DynamicMessageCopyFrom) {
// Construct a new version of the dynamic message via the factory.
DynamicMessageFactory factory;
- scoped_ptr<Message> message1;
+ google::protobuf::scoped_ptr<Message> message1;
message1.reset(factory.GetPrototype(
unittest::TestAllTypes::descriptor())->New());
@@ -578,9 +583,9 @@ TEST(GeneratedMessageTest, NonEmptyMergeFrom) {
TEST(GeneratedMessageTest, MergeFromSelf) {
unittest::TestAllTypes message;
- EXPECT_DEATH(message.MergeFrom(message), "&from");
+ EXPECT_DEATH(message.MergeFrom(message), "Check failed:.*pb[.]cc");
EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)),
- "&from");
+ "Check failed:.*pb[.]cc");
}
#endif // PROTOBUF_HAS_DEATH_TEST
@@ -1218,7 +1223,7 @@ class GeneratedServiceTest : public testing::Test {
unittest::FooResponse foo_response_;
unittest::BarRequest bar_request_;
unittest::BarResponse bar_response_;
- scoped_ptr<Closure> done_;
+ google::protobuf::scoped_ptr<Closure> done_;
};
TEST_F(GeneratedServiceTest, GetDescriptor) {
@@ -1516,7 +1521,7 @@ TEST_F(OneofTest, ReleaseString) {
message.set_foo_string("blah");
EXPECT_TRUE(message.has_foo_string());
- scoped_ptr<string> str(message.release_foo_string());
+ google::protobuf::scoped_ptr<string> str(message.release_foo_string());
EXPECT_FALSE(message.has_foo_string());
ASSERT_TRUE(str != NULL);
EXPECT_EQ("blah", *str);
@@ -1570,7 +1575,7 @@ TEST_F(OneofTest, ReleaseMessage) {
message.mutable_foo_message()->set_qux_int(1);
EXPECT_TRUE(message.has_foo_message());
- scoped_ptr<unittest::TestOneof2_NestedMessage> mes(
+ google::protobuf::scoped_ptr<unittest::TestOneof2_NestedMessage> mes(
message.release_foo_message());
EXPECT_FALSE(message.has_foo_message());
ASSERT_TRUE(mes != NULL);
@@ -1687,7 +1692,7 @@ TEST_F(OneofTest, SwapBothHasFields) {
EXPECT_EQ(message2.foo_string(), "FOO");
}
-TEST_F(OneofTest, CopyContructor) {
+TEST_F(OneofTest, CopyConstructor) {
unittest::TestOneof2 message1;
message1.set_foo_bytes("FOO");
diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc
index e6c446af..8333684e 100644
--- a/src/google/protobuf/compiler/importer.cc
+++ b/src/google/protobuf/compiler/importer.cc
@@ -44,6 +44,9 @@
#include <algorithm>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/importer.h>
@@ -122,7 +125,7 @@ SourceTreeDescriptorDatabase::~SourceTreeDescriptorDatabase() {}
bool SourceTreeDescriptorDatabase::FindFileByName(
const string& filename, FileDescriptorProto* output) {
- scoped_ptr<io::ZeroCopyInputStream> input(source_tree_->Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> input(source_tree_->Open(filename));
if (input == NULL) {
if (error_collector_ != NULL) {
error_collector_->AddError(filename, -1, 0,
@@ -400,7 +403,7 @@ DiskSourceTree::DiskFileToVirtualFile(
// Verify that we can open the file. Note that this also has the side-effect
// of verifying that we are not canonicalizing away any non-existent
// directories.
- scoped_ptr<io::ZeroCopyInputStream> stream(OpenDiskFile(disk_file));
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> stream(OpenDiskFile(disk_file));
if (stream == NULL) {
return CANNOT_OPEN;
}
@@ -410,7 +413,7 @@ DiskSourceTree::DiskFileToVirtualFile(
bool DiskSourceTree::VirtualFileToDiskFile(const string& virtual_file,
string* disk_file) {
- scoped_ptr<io::ZeroCopyInputStream> stream(
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> stream(
OpenVirtualFile(virtual_file, disk_file));
return stream != NULL;
}
diff --git a/src/google/protobuf/compiler/importer_unittest.cc b/src/google/protobuf/compiler/importer_unittest.cc
index 387f135d..43eb0ed5 100644
--- a/src/google/protobuf/compiler/importer_unittest.cc
+++ b/src/google/protobuf/compiler/importer_unittest.cc
@@ -34,9 +34,13 @@
#include <google/protobuf/stubs/hash.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/descriptor.h>
+#include <google/protobuf/testing/file.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/stubs/map_util.h>
@@ -53,6 +57,10 @@ namespace compiler {
namespace {
+bool FileExists(const string& path) {
+ return File::Exists(path);
+}
+
#define EXPECT_SUBSTRING(needle, haystack) \
EXPECT_PRED_FORMAT2(testing::IsSubstring, (needle), (haystack))
@@ -215,120 +223,6 @@ TEST_F(ImporterTest, RecursiveImport) {
error_collector_.text_);
}
-// TODO(sanjay): The MapField tests below more properly belong in
-// descriptor_unittest, but are more convenient to test here.
-TEST_F(ImporterTest, MapFieldValid) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Item {\n"
- " required string key = 1;\n"
- "}\n"
- "message Map {\n"
- " repeated Item items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- const FileDescriptor* file = importer_.Import("map.proto");
- ASSERT_TRUE(file != NULL) << error_collector_.text_;
- EXPECT_EQ("", error_collector_.text_);
-
- // Check that Map::items points to Item::key
- const Descriptor* item_type = file->FindMessageTypeByName("Item");
- ASSERT_TRUE(item_type != NULL);
- const Descriptor* map_type = file->FindMessageTypeByName("Map");
- ASSERT_TRUE(map_type != NULL);
- const FieldDescriptor* key_field = item_type->FindFieldByName("key");
- ASSERT_TRUE(key_field != NULL);
- const FieldDescriptor* items_field = map_type->FindFieldByName("items");
- ASSERT_TRUE(items_field != NULL);
- EXPECT_EQ(items_field->experimental_map_key(), key_field);
-}
-
-TEST_F(ImporterTest, MapFieldNotRepeated) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Item {\n"
- " required string key = 1;\n"
- "}\n"
- "message Map {\n"
- " required Item items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("only allowed for repeated fields", error());
-}
-
-TEST_F(ImporterTest, MapFieldNotMessageType) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Map {\n"
- " repeated int32 items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("only allowed for fields with a message type", error());
-}
-
-TEST_F(ImporterTest, MapFieldTypeNotFound) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Map {\n"
- " repeated Unknown items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("not defined", error());
-}
-
-TEST_F(ImporterTest, MapFieldKeyNotFound) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Item {\n"
- " required string key = 1;\n"
- "}\n"
- "message Map {\n"
- " repeated Item items = 1 [experimental_map_key = \"badkey\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("Could not find field", error());
-}
-
-TEST_F(ImporterTest, MapFieldKeyRepeated) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message Item {\n"
- " repeated string key = 1;\n"
- "}\n"
- "message Map {\n"
- " repeated Item items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("must not name a repeated field", error());
-}
-
-TEST_F(ImporterTest, MapFieldKeyNotScalar) {
- AddFile(
- "map.proto",
- "syntax = \"proto2\";\n"
- "message ItemKey { }\n"
- "message Item {\n"
- " required ItemKey key = 1;\n"
- "}\n"
- "message Map {\n"
- " repeated Item items = 1 [experimental_map_key = \"key\"];\n"
- "}\n"
- );
- EXPECT_TRUE(importer_.Import("map.proto") == NULL);
- EXPECT_SUBSTRING("must name a scalar or string", error());
-}
-
// ===================================================================
@@ -339,7 +233,7 @@ class DiskSourceTreeTest : public testing::Test {
dirnames_.push_back(TestTempDir() + "/test_proto2_import_path_2");
for (int i = 0; i < dirnames_.size(); i++) {
- if (File::Exists(dirnames_[i])) {
+ if (FileExists(dirnames_[i])) {
File::DeleteRecursively(dirnames_[i], NULL, NULL);
}
GOOGLE_CHECK_OK(File::CreateDir(dirnames_[i], 0777));
@@ -348,7 +242,9 @@ class DiskSourceTreeTest : public testing::Test {
virtual void TearDown() {
for (int i = 0; i < dirnames_.size(); i++) {
- File::DeleteRecursively(dirnames_[i], NULL, NULL);
+ if (FileExists(dirnames_[i])) {
+ File::DeleteRecursively(dirnames_[i], NULL, NULL);
+ }
}
}
@@ -362,7 +258,7 @@ class DiskSourceTreeTest : public testing::Test {
void ExpectFileContents(const string& filename,
const char* expected_contents) {
- scoped_ptr<io::ZeroCopyInputStream> input(source_tree_.Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> input(source_tree_.Open(filename));
ASSERT_FALSE(input == NULL);
@@ -379,7 +275,7 @@ class DiskSourceTreeTest : public testing::Test {
void ExpectCannotOpenFile(const string& filename,
const string& error_message) {
- scoped_ptr<io::ZeroCopyInputStream> input(source_tree_.Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> input(source_tree_.Open(filename));
EXPECT_TRUE(input == NULL);
EXPECT_EQ(error_message, source_tree_.GetLastErrorMessage());
}
diff --git a/src/google/protobuf/compiler/java/java_context.h b/src/google/protobuf/compiler/java/java_context.h
index 57914450..5b595d07 100644
--- a/src/google/protobuf/compiler/java/java_context.h
+++ b/src/google/protobuf/compiler/java/java_context.h
@@ -33,6 +33,9 @@
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <vector>
#include <google/protobuf/stubs/common.h>
@@ -81,7 +84,7 @@ class Context {
void InitializeFieldGeneratorInfoForFields(
const vector<const FieldDescriptor*>& fields);
- scoped_ptr<ClassNameResolver> name_resolver_;
+ google::protobuf::scoped_ptr<ClassNameResolver> name_resolver_;
map<const FieldDescriptor*, FieldGeneratorInfo> field_generator_info_map_;
map<const OneofDescriptor*, OneofGeneratorInfo> oneof_generator_info_map_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Context);
diff --git a/src/google/protobuf/compiler/java/java_enum.cc b/src/google/protobuf/compiler/java/java_enum.cc
index 668377a3..0353b607 100644
--- a/src/google/protobuf/compiler/java/java_enum.cc
+++ b/src/google/protobuf/compiler/java/java_enum.cc
@@ -111,6 +111,10 @@ void EnumGenerator::Generate(io::Printer* printer) {
"$name$($index$, $number$),\n");
}
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ printer->Print("UNRECOGNIZED(-1, -1),\n");
+ }
+
printer->Print(
";\n"
"\n");
@@ -141,7 +145,17 @@ void EnumGenerator::Generate(io::Printer* printer) {
printer->Print(
"\n"
- "public final int getNumber() { return value; }\n"
+ "public final int getNumber() {\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ printer->Print(
+ " 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"
"public static $classname$ valueOf(int value) {\n"
" switch (value) {\n",
@@ -231,11 +245,12 @@ void EnumGenerator::Generate(io::Printer* printer) {
"index", SimpleItoa(descriptor_->index()));
}
printer->Print(
- "return $immutable_package$.$descriptor_class$.getDescriptor()\n"
+ "return $immutable_package$.$descriptor_class$.$descriptor$\n"
" .getEnumTypes().get($index$);\n",
"immutable_package", FileJavaPackage(descriptor_->file(), true),
"descriptor_class",
name_resolver_->GetDescriptorClassName(descriptor_->file()),
+ "descriptor", "getDescriptor()",
"index", SimpleItoa(descriptor_->index()));
printer->Outdent();
}
@@ -283,11 +298,18 @@ void EnumGenerator::Generate(io::Printer* printer) {
" if (desc.getType() != getDescriptor()) {\n"
" throw new java.lang.IllegalArgumentException(\n"
" \"EnumValueDescriptor is not for this type.\");\n"
- " }\n"
+ " }\n",
+ "classname", descriptor_->name());
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ printer->Print(
+ " if (desc.getIndex() == -1) {\n"
+ " return UNRECOGNIZED;\n"
+ " }\n");
+ }
+ printer->Print(
" return VALUES[desc.getIndex()];\n"
"}\n"
- "\n",
- "classname", descriptor_->name());
+ "\n");
// index is only used for reflection; lite implementation does not need it
printer->Print("private final int index;\n");
diff --git a/src/google/protobuf/compiler/java/java_enum_field.cc b/src/google/protobuf/compiler/java/java_enum_field.cc
index 1f0c4af0..71a2ba4b 100644
--- a/src/google/protobuf/compiler/java/java_enum_field.cc
+++ b/src/google/protobuf/compiler/java/java_enum_field.cc
@@ -65,6 +65,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor,
(*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->enum_type());
(*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver);
+ (*variables)["default_number"] = SimpleItoa(
+ descriptor->default_value_enum()->number());
(*variables)["tag"] = SimpleItoa(internal::WireFormat::MakeTag(descriptor));
(*variables)["tag_size"] = SimpleItoa(
internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
@@ -95,7 +97,8 @@ void SetEnumVariables(const FieldDescriptor* descriptor,
(*variables)["clear_has_field_bit_builder"] = "";
(*variables)["is_field_present_message"] =
- (*variables)["name"] + "_ != " + (*variables)["default"];
+ (*variables)["name"] + "_ != " +
+ (*variables)["default"] + ".getNumber()";
}
// For repated builders, one bit is used for whether the array is immutable.
@@ -114,6 +117,12 @@ void SetEnumVariables(const FieldDescriptor* descriptor,
GenerateGetBitFromLocal(builderBitIndex);
(*variables)["set_has_field_bit_to_local"] =
GenerateSetBitToLocal(messageBitIndex);
+
+ if (SupportUnknownEnumValue(descriptor->file())) {
+ (*variables)["unknown"] = (*variables)["type"] + ".UNRECOGNIZED";
+ } else {
+ (*variables)["unknown"] = (*variables)["default"];
+ }
}
} // namespace
@@ -150,6 +159,11 @@ GenerateInterfaceMembers(io::Printer* printer) const {
printer->Print(variables_,
"$deprecation$boolean has$capitalized_name$();\n");
}
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$int get$capitalized_name$Value();\n");
+ }
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$$type$ get$capitalized_name$();\n");
@@ -158,7 +172,7 @@ GenerateInterfaceMembers(io::Printer* printer) const {
void ImmutableEnumFieldGenerator::
GenerateMembers(io::Printer* printer) const {
printer->Print(variables_,
- "private $type$ $name$_;\n");
+ "private int $name$_;\n");
PrintExtraFieldInfo(variables_, printer);
if (SupportFieldPresence(descriptor_->file())) {
WriteFieldDocComment(printer, descriptor_);
@@ -167,17 +181,25 @@ GenerateMembers(io::Printer* printer) const {
" return $get_has_field_bit_message$;\n"
"}\n");
}
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public int get$capitalized_name$Value() {\n"
+ " return $name$_;\n"
+ "}\n");
+ }
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$() {\n"
- " return $name$_;\n"
+ " $type$ result = $type$.valueOf($name$_);\n"
+ " return result == null ? $unknown$ : result;\n"
"}\n");
}
void ImmutableEnumFieldGenerator::
GenerateBuilderMembers(io::Printer* printer) const {
printer->Print(variables_,
- "private $type$ $name$_ = $default$;\n");
+ "private int $name$_ = $default_number$;\n");
if (SupportFieldPresence(descriptor_->file())) {
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
@@ -185,10 +207,25 @@ GenerateBuilderMembers(io::Printer* printer) const {
" return $get_has_field_bit_builder$;\n"
"}\n");
}
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public int get$capitalized_name$Value() {\n"
+ " return $name$_;\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public Builder set$capitalized_name$Value(int value) {\n"
+ " $name$_ = value;\n"
+ " $on_changed$\n"
+ " return this;\n"
+ "}\n");
+ }
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$() {\n"
- " return $name$_;\n"
+ " $type$ result = $type$.valueOf($name$_);\n"
+ " return result == null ? $unknown$ : result;\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
@@ -197,7 +234,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
" throw new NullPointerException();\n"
" }\n"
" $set_has_field_bit_builder$\n"
- " $name$_ = value;\n"
+ " $name$_ = value.getNumber();\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -205,7 +242,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
printer->Print(variables_,
"$deprecation$public Builder clear$capitalized_name$() {\n"
" $clear_has_field_bit_builder$\n"
- " $name$_ = $default$;\n"
+ " $name$_ = $default_number$;\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -218,13 +255,13 @@ GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
void ImmutableEnumFieldGenerator::
GenerateInitializationCode(io::Printer* printer) const {
- printer->Print(variables_, "$name$_ = $default$;\n");
+ printer->Print(variables_, "$name$_ = $default_number$;\n");
}
void ImmutableEnumFieldGenerator::
GenerateBuilderClearCode(io::Printer* printer) const {
printer->Print(variables_,
- "$name$_ = $default$;\n"
+ "$name$_ = $default_number$;\n"
"$clear_has_field_bit_builder$\n");
}
@@ -235,11 +272,13 @@ GenerateMergingCode(io::Printer* printer) const {
"if (other.has$capitalized_name$()) {\n"
" set$capitalized_name$(other.get$capitalized_name$());\n"
"}\n");
- } else {
+ } else if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- "if (other.get$capitalized_name$() != $default$) {\n"
- " set$capitalized_name$(other.get$capitalized_name$());\n"
+ "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.";
}
}
@@ -257,23 +296,26 @@ GenerateBuildingCode(io::Printer* printer) const {
void ImmutableEnumFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
- printer->Print(variables_,
- "int rawValue = input.readEnum();\n"
- "$type$ value = $type$.valueOf(rawValue);\n"
- "if (value == null) {\n");
- if (UseUnknownFieldSet(descriptor_->containing_type())) {
+ if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- " unknownFields.mergeVarintField($number$, rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "$set_has_field_bit_message$\n"
+ "$name$_ = rawValue;\n");
} else {
printer->Print(variables_,
- " unknownFieldsCodedOutput.writeRawVarint32(tag);\n"
- " unknownFieldsCodedOutput.writeRawVarint32(rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "$type$ value = $type$.valueOf(rawValue);\n"
+ "if (value == null) {\n");
+ if (PreserveUnknownFields(descriptor_->containing_type())) {
+ printer->Print(variables_,
+ " unknownFields.mergeVarintField($number$, rawValue);\n");
+ }
+ printer->Print(variables_,
+ "} else {\n"
+ " $set_has_field_bit_message$\n"
+ " $name$_ = rawValue;\n"
+ "}\n");
}
- printer->Print(variables_,
- "} else {\n"
- " $set_has_field_bit_message$\n"
- " $name$_ = value;\n"
- "}\n");
}
void ImmutableEnumFieldGenerator::
@@ -285,7 +327,7 @@ void ImmutableEnumFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($is_field_present_message$) {\n"
- " output.writeEnum($number$, $name$_.getNumber());\n"
+ " output.writeEnum($number$, $name$_);\n"
"}\n");
}
@@ -294,23 +336,21 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($is_field_present_message$) {\n"
" size += com.google.protobuf.CodedOutputStream\n"
- " .computeEnumSize($number$, $name$_.getNumber());\n"
+ " .computeEnumSize($number$, $name$_);\n"
"}\n");
}
void ImmutableEnumFieldGenerator::
GenerateEqualsCode(io::Printer* printer) const {
printer->Print(variables_,
- "result = result &&\n"
- " (get$capitalized_name$() == other.get$capitalized_name$());\n");
+ "result = result && $name$_ == other.$name$_;\n");
}
void ImmutableEnumFieldGenerator::
GenerateHashCode(io::Printer* printer) const {
printer->Print(variables_,
"hash = (37 * hash) + $constant_name$;\n"
- "hash = (53 * hash) + com.google.protobuf.Internal.hashEnum(\n"
- " get$capitalized_name$());\n");
+ "hash = (53 * hash) + $name$_;\n");
}
string ImmutableEnumFieldGenerator::GetBoxedType() const {
@@ -344,11 +384,22 @@ GenerateMembers(io::Printer* printer) const {
" return $has_oneof_case_message$;\n"
"}\n");
}
+ 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");
+ }
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$() {\n"
" if ($has_oneof_case_message$) {\n"
- " return ($type$) $oneof_name$_;\n"
+ " $type$ result = $type$.valueOf((java.lang.Integer) $oneof_name$_);\n"
+ " return result == null ? $unknown$ : result;\n"
" }\n"
" return $default$;\n"
"}\n");
@@ -363,11 +414,30 @@ GenerateBuilderMembers(io::Printer* printer) const {
" return $has_oneof_case_message$;\n"
"}\n");
}
+ 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");
+ WriteFieldDocComment(printer, descriptor_);
+ 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");
+ }
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$() {\n"
" if ($has_oneof_case_message$) {\n"
- " return ($type$) $oneof_name$_;\n"
+ " $type$ result = $type$.valueOf((java.lang.Integer) $oneof_name$_);\n"
+ " return result == null ? $unknown$ : result;\n"
" }\n"
" return $default$;\n"
"}\n");
@@ -378,7 +448,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
" throw new NullPointerException();\n"
" }\n"
" $set_oneof_case_message$;\n"
- " $oneof_name$_ = value;\n"
+ " $oneof_name$_ = value.getNumber();\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -404,36 +474,44 @@ GenerateBuildingCode(io::Printer* printer) const {
void ImmutableEnumOneofFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
- printer->Print(variables_,
- "set$capitalized_name$(other.get$capitalized_name$());\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ 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");
+ }
}
void ImmutableEnumOneofFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
- printer->Print(variables_,
- "int rawValue = input.readEnum();\n"
- "$type$ value = $type$.valueOf(rawValue);\n"
- "if (value == null) {\n");
- if (UseUnknownFieldSet(descriptor_->containing_type())) {
+ if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- " unknownFields.mergeVarintField($number$, rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "$set_oneof_case_message$;\n"
+ "$oneof_name$_ = rawValue;\n");
} else {
printer->Print(variables_,
- " unknownFieldsCodedOutput.writeRawVarint32(tag);\n"
- " unknownFieldsCodedOutput.writeRawVarint32(rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "$type$ value = $type$.valueOf(rawValue);\n"
+ "if (value == null) {\n");
+ if (PreserveUnknownFields(descriptor_->containing_type())) {
+ printer->Print(variables_,
+ " unknownFields.mergeVarintField($number$, rawValue);\n");
+ }
+ printer->Print(variables_,
+ "} else {\n"
+ " $set_oneof_case_message$;\n"
+ " $oneof_name$_ = rawValue;\n"
+ "}\n");
}
- printer->Print(variables_,
- "} else {\n"
- " $set_oneof_case_message$;\n"
- " $oneof_name$_ = value;\n"
- "}\n");
}
void ImmutableEnumOneofFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($has_oneof_case_message$) {\n"
- " output.writeEnum($number$, (($type$) $oneof_name$_).getNumber());\n"
+ " output.writeEnum($number$, ((java.lang.Integer) $oneof_name$_));\n"
"}\n");
}
@@ -442,10 +520,36 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($has_oneof_case_message$) {\n"
" size += com.google.protobuf.CodedOutputStream\n"
- " .computeEnumSize($number$, (($type$) $oneof_name$_).getNumber());\n"
+ " .computeEnumSize($number$, ((java.lang.Integer) $oneof_name$_));\n"
"}\n");
}
+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");
+ } else {
+ printer->Print(variables_,
+ "result = result && get$capitalized_name$()\n"
+ " .equals(other.get$capitalized_name$());\n");
+ }
+}
+
+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");
+ } else {
+ printer->Print(variables_,
+ "hash = (37 * hash) + $constant_name$;\n"
+ "hash = (53 * hash) + get$capitalized_name$().getNumber();\n");
+ }
+}
+
// ===================================================================
RepeatedImmutableEnumFieldGenerator::
@@ -482,17 +586,36 @@ GenerateInterfaceMembers(io::Printer* printer) const {
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$$type$ get$capitalized_name$(int index);\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$java.util.List<java.lang.Integer>\n"
+ "get$capitalized_name$ValueList();\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$int get$capitalized_name$Value(int index);\n");
+ }
}
void RepeatedImmutableEnumFieldGenerator::
GenerateMembers(io::Printer* printer) const {
printer->Print(variables_,
- "private java.util.List<$type$> $name$_;\n");
+ "private java.util.List<java.lang.Integer> $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"
+ " $type$ result = $type$.valueOf(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 $name$_;\n" // note: unmodifiable list
+ " return new com.google.protobuf.Internal.ListAdapter<\n"
+ " java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
@@ -502,8 +625,21 @@ GenerateMembers(io::Printer* printer) const {
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$(int index) {\n"
- " return $name$_.get(index);\n"
+ " return $name$_converter_.convert($name$_.get(index));\n"
"}\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public java.util.List<java.lang.Integer>\n"
+ "get$capitalized_name$ValueList() {\n"
+ " return $name$_;\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public int get$capitalized_name$Value(int index) {\n"
+ " return $name$_.get(index);\n"
+ "}\n");
+ }
if (descriptor_->options().packed() &&
HasGeneratedMethods(descriptor_->containing_type())) {
@@ -524,12 +660,12 @@ 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.
- "private java.util.List<$type$> $name$_ =\n"
+ "private java.util.List<java.lang.Integer> $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"
+ " $name$_ = new java.util.ArrayList<java.lang.Integer>($name$_);\n"
" $set_mutable_bit_builder$;\n"
" }\n"
"}\n");
@@ -541,7 +677,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
// 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 java.util.Collections.unmodifiableList($name$_);\n"
+ " return new com.google.protobuf.Internal.ListAdapter<\n"
+ " java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
@@ -551,7 +688,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$(int index) {\n"
- " return $name$_.get(index);\n"
+ " return $name$_converter_.convert($name$_.get(index));\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
@@ -561,7 +698,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
" throw new NullPointerException();\n"
" }\n"
" ensure$capitalized_name$IsMutable();\n"
- " $name$_.set(index, value);\n"
+ " $name$_.set(index, value.getNumber());\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -572,7 +709,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
" throw new NullPointerException();\n"
" }\n"
" ensure$capitalized_name$IsMutable();\n"
- " $name$_.add(value);\n"
+ " $name$_.add(value.getNumber());\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -581,8 +718,9 @@ GenerateBuilderMembers(io::Printer* printer) const {
"$deprecation$public Builder addAll$capitalized_name$(\n"
" java.lang.Iterable<? extends $type$> values) {\n"
" ensure$capitalized_name$IsMutable();\n"
- " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n"
- " values, $name$_);\n"
+ " for ($type$ value : values) {\n"
+ " $name$_.add(value.getNumber());\n"
+ " }\n"
" $on_changed$\n"
" return this;\n"
"}\n");
@@ -594,6 +732,48 @@ GenerateBuilderMembers(io::Printer* printer) const {
" $on_changed$\n"
" return this;\n"
"}\n");
+
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public java.util.List<java.lang.Integer>\n"
+ "get$capitalized_name$ValueList() {\n"
+ " return java.util.Collections.unmodifiableList($name$_);\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public int get$capitalized_name$Value(int index) {\n"
+ " return $name$_.get(index);\n"
+ "}\n");
+ 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");
+ WriteFieldDocComment(printer, descriptor_);
+ 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");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(variables_,
+ "$deprecation$public Builder addAll$capitalized_name$Value(\n"
+ " java.lang.Iterable<java.lang.Integer> values) {\n"
+ " ensure$capitalized_name$IsMutable();\n"
+ " for (int value : values) {\n"
+ " $name$_.add(value);\n"
+ " }\n"
+ " $on_changed$\n"
+ " return this;\n"
+ "}\n");
+ }
}
void RepeatedImmutableEnumFieldGenerator::
@@ -648,26 +828,32 @@ GenerateBuildingCode(io::Printer* printer) const {
void RepeatedImmutableEnumFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
// Read and store the enum
- printer->Print(variables_,
- "int rawValue = input.readEnum();\n"
- "$type$ value = $type$.valueOf(rawValue);\n"
- "if (value == null) {\n");
- if (UseUnknownFieldSet(descriptor_->containing_type())) {
+ if (SupportUnknownEnumValue(descriptor_->file())) {
printer->Print(variables_,
- " unknownFields.mergeVarintField($number$, rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "if (!$get_mutable_bit_parser$) {\n"
+ " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n"
+ " $set_mutable_bit_parser$;\n"
+ "}\n"
+ "$name$_.add(rawValue);\n");
} else {
printer->Print(variables_,
- " unknownFieldsCodedOutput.writeRawVarint32(tag);\n"
- " unknownFieldsCodedOutput.writeRawVarint32(rawValue);\n");
+ "int rawValue = input.readEnum();\n"
+ "$type$ value = $type$.valueOf(rawValue);\n"
+ "if (value == null) {\n");
+ if (PreserveUnknownFields(descriptor_->containing_type())) {
+ printer->Print(variables_,
+ " unknownFields.mergeVarintField($number$, rawValue);\n");
+ }
+ printer->Print(variables_,
+ "} else {\n"
+ " if (!$get_mutable_bit_parser$) {\n"
+ " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n"
+ " $set_mutable_bit_parser$;\n"
+ " }\n"
+ " $name$_.add(rawValue);\n"
+ "}\n");
}
- printer->Print(variables_,
- " } else {\n"
- " if (!$get_mutable_bit_parser$) {\n"
- " $name$_ = new java.util.ArrayList<$type$>();\n"
- " $set_mutable_bit_parser$;\n"
- " }\n"
- " $name$_.add(value);\n"
- "}\n");
}
void RepeatedImmutableEnumFieldGenerator::
@@ -705,12 +891,12 @@ GenerateSerializationCode(io::Printer* printer) const {
" output.writeRawVarint32($name$MemoizedSerializedSize);\n"
"}\n"
"for (int i = 0; i < $name$_.size(); i++) {\n"
- " output.writeEnumNoTag($name$_.get(i).getNumber());\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).getNumber());\n"
+ " output.writeEnum($number$, $name$_.get(i));\n"
"}\n");
}
}
@@ -725,7 +911,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"for (int i = 0; i < $name$_.size(); i++) {\n"
" dataSize += com.google.protobuf.CodedOutputStream\n"
- " .computeEnumSizeNoTag($name$_.get(i).getNumber());\n"
+ " .computeEnumSizeNoTag($name$_.get(i));\n"
"}\n");
printer->Print(
"size += dataSize;\n");
@@ -754,8 +940,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
void RepeatedImmutableEnumFieldGenerator::
GenerateEqualsCode(io::Printer* printer) const {
printer->Print(variables_,
- "result = result && get$capitalized_name$List()\n"
- " .equals(other.get$capitalized_name$List());\n");
+ "result = result && $name$_.equals(other.$name$_);\n");
}
void RepeatedImmutableEnumFieldGenerator::
@@ -763,8 +948,7 @@ GenerateHashCode(io::Printer* printer) const {
printer->Print(variables_,
"if (get$capitalized_name$Count() > 0) {\n"
" hash = (37 * hash) + $constant_name$;\n"
- " hash = (53 * hash) + com.google.protobuf.Internal.hashEnumList(\n"
- " get$capitalized_name$List());\n"
+ " hash = (53 * hash) + $name$_.hashCode();\n"
"}\n");
}
diff --git a/src/google/protobuf/compiler/java/java_enum_field.h b/src/google/protobuf/compiler/java/java_enum_field.h
index 6a9535aa..b8ff7343 100644
--- a/src/google/protobuf/compiler/java/java_enum_field.h
+++ b/src/google/protobuf/compiler/java/java_enum_field.h
@@ -106,6 +106,8 @@ class ImmutableEnumOneofFieldGenerator : public ImmutableEnumFieldGenerator {
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;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableEnumOneofFieldGenerator);
diff --git a/src/google/protobuf/compiler/java/java_field.cc b/src/google/protobuf/compiler/java/java_field.cc
index d7b0f3fc..af9978e2 100644
--- a/src/google/protobuf/compiler/java/java_field.cc
+++ b/src/google/protobuf/compiler/java/java_field.cc
@@ -35,12 +35,16 @@
#include <google/protobuf/compiler/java/java_field.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/compiler/java/java_context.h>
#include <google/protobuf/compiler/java/java_enum_field.h>
#include <google/protobuf/compiler/java/java_helpers.h>
#include <google/protobuf/compiler/java/java_lazy_message_field.h>
+#include <google/protobuf/compiler/java/java_map_field.h>
#include <google/protobuf/compiler/java/java_message_field.h>
#include <google/protobuf/compiler/java/java_primitive_field.h>
#include <google/protobuf/compiler/java/java_string_field.h>
@@ -48,6 +52,7 @@
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
+
namespace google {
namespace protobuf {
namespace compiler {
@@ -61,12 +66,17 @@ ImmutableFieldGenerator* MakeImmutableGenerator(
if (field->is_repeated()) {
switch (GetJavaType(field)) {
case JAVATYPE_MESSAGE:
- if (IsLazy(field)) {
- return new RepeatedImmutableLazyMessageFieldGenerator(
+ if (IsMapEntry(field->message_type())) {
+ return new ImmutableMapFieldGenerator(
field, messageBitIndex, builderBitIndex, context);
} else {
- return new RepeatedImmutableMessageFieldGenerator(
- field, messageBitIndex, builderBitIndex, context);
+ if (IsLazy(field)) {
+ return new RepeatedImmutableLazyMessageFieldGenerator(
+ field, messageBitIndex, builderBitIndex, context);
+ } else {
+ return new RepeatedImmutableMessageFieldGenerator(
+ field, messageBitIndex, builderBitIndex, context);
+ }
}
case JAVATYPE_ENUM:
return new RepeatedImmutableEnumFieldGenerator(
@@ -149,7 +159,7 @@ template <>
FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
const Descriptor* descriptor, Context* context)
: descriptor_(descriptor),
- field_generators_(new scoped_ptr<
+ field_generators_(new google::protobuf::scoped_ptr<
ImmutableFieldGenerator>[descriptor->field_count()]) {
// Construct all the FieldGenerators and assign them bit indices for their
diff --git a/src/google/protobuf/compiler/java/java_field.h b/src/google/protobuf/compiler/java/java_field.h
index db339d5b..e8d6f3a2 100644
--- a/src/google/protobuf/compiler/java/java_field.h
+++ b/src/google/protobuf/compiler/java/java_field.h
@@ -37,6 +37,9 @@
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <google/protobuf/stubs/common.h>
@@ -105,7 +108,7 @@ class FieldGeneratorMap {
const Descriptor* descriptor_;
Context* context_;
ClassNameResolver* name_resolver_;
- scoped_array<scoped_ptr<FieldGeneratorType> > field_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<FieldGeneratorType> > field_generators_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
};
diff --git a/src/google/protobuf/compiler/java/java_file.cc b/src/google/protobuf/compiler/java/java_file.cc
index 231b1445..f1e3cf67 100644
--- a/src/google/protobuf/compiler/java/java_file.cc
+++ b/src/google/protobuf/compiler/java/java_file.cc
@@ -35,6 +35,9 @@
#include <google/protobuf/compiler/java/java_file.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/java/java_context.h>
#include <google/protobuf/compiler/java/java_enum.h>
@@ -118,7 +121,7 @@ void CollectExtensions(const FileDescriptorProto& file_proto,
"descriptor.proto is not in the transitive dependencies. "
"This normally should not happen. Please report a bug.";
DynamicMessageFactory factory;
- scoped_ptr<Message> dynamic_file_proto(
+ google::protobuf::scoped_ptr<Message> dynamic_file_proto(
factory.GetPrototype(file_proto_desc)->New());
GOOGLE_CHECK(dynamic_file_proto.get() != NULL);
GOOGLE_CHECK(dynamic_file_proto->ParseFromString(file_data));
@@ -143,9 +146,9 @@ FileGenerator::FileGenerator(const FileDescriptor* file, bool immutable_api)
: file_(file),
java_package_(FileJavaPackage(file, immutable_api)),
message_generators_(
- new scoped_ptr<MessageGenerator>[file->message_type_count()]),
+ new google::protobuf::scoped_ptr<MessageGenerator>[file->message_type_count()]),
extension_generators_(
- new scoped_ptr<ExtensionGenerator>[file->extension_count()]),
+ new google::protobuf::scoped_ptr<ExtensionGenerator>[file->extension_count()]),
context_(new Context(file)),
name_resolver_(context_->GetNameResolver()),
immutable_api_(immutable_api) {
@@ -180,29 +183,6 @@ bool FileGenerator::Validate(string* error) {
"option to specify a different outer class name for the .proto file.");
return false;
}
- // If java_outer_classname option is not set and the default outer class name
- // conflicts with a type defined in the message, we will append a suffix to
- // avoid the conflict. This allows proto1 API protos to be dual-compiled into
- // proto2 API without code change. When this happens we'd like to issue an
- // warning to let the user know that the outer class name has been changed.
- // Although we only do this automatic naming fix for immutable API, mutable
- // outer class name will also be affected as it's contructed from immutable
- // outer class name with an additional "Mutable" prefix. Since the naming
- // change in mutable API is not caused by a naming conflict, we generate the
- // warning for immutable API only.
- if (immutable_api_ && !file_->options().has_java_outer_classname()) {
- string default_classname =
- name_resolver_->GetFileDefaultImmutableClassName(file_);
- if (default_classname != classname_) {
- GOOGLE_LOG(WARNING) << file_->name() << ": The default outer class name, \""
- << default_classname << "\", conflicts with a type "
- << "declared in the proto file and an alternative outer "
- << "class name is used: \"" << classname_ << "\". To avoid "
- << "this warning, please use the java_outer_classname "
- << "option to specify a different outer class name for "
- << "the .proto file.";
- }
- }
return true;
}
@@ -260,7 +240,7 @@ void FileGenerator::Generate(io::Printer* printer) {
}
if (HasGenericServices(file_)) {
for (int i = 0; i < file_->service_count(); i++) {
- scoped_ptr<ServiceGenerator> generator(
+ google::protobuf::scoped_ptr<ServiceGenerator> generator(
generator_factory_->NewServiceGenerator(file_->service(i)));
generator->Generate(printer);
}
@@ -323,6 +303,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable(
SharedCodeGenerator shared_code_generator(file_);
shared_code_generator.GenerateDescriptors(printer);
+
for (int i = 0; i < file_->message_type_count(); i++) {
message_generators_[i]->GenerateStaticVariableInitializers(printer);
}
@@ -359,7 +340,7 @@ void FileGenerator::GenerateDescriptorInitializationCodeForImmutable(
"com.google.protobuf.ExtensionRegistry registry =\n"
" com.google.protobuf.ExtensionRegistry.newInstance();\n");
for (int i = 0; i < extensions.size(); i++) {
- scoped_ptr<ExtensionGenerator> generator(
+ google::protobuf::scoped_ptr<ExtensionGenerator> generator(
generator_factory_->NewExtensionGenerator(extensions[i]));
generator->GenerateRegistrationCode(printer);
}
@@ -463,7 +444,7 @@ static void GenerateSibling(const string& package_dir,
string filename = package_dir + descriptor->name() + name_suffix + ".java";
file_list->push_back(filename);
- scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
io::Printer printer(output.get(), '$');
printer.Print(
@@ -511,7 +492,7 @@ void FileGenerator::GenerateSiblings(const string& package_dir,
}
if (HasGenericServices(file_)) {
for (int i = 0; i < file_->service_count(); i++) {
- scoped_ptr<ServiceGenerator> generator(
+ google::protobuf::scoped_ptr<ServiceGenerator> generator(
generator_factory_->NewServiceGenerator(file_->service(i)));
GenerateSibling<ServiceGenerator>(package_dir, java_package_,
file_->service(i),
diff --git a/src/google/protobuf/compiler/java/java_file.h b/src/google/protobuf/compiler/java/java_file.h
index c805b9a6..080b3424 100644
--- a/src/google/protobuf/compiler/java/java_file.h
+++ b/src/google/protobuf/compiler/java/java_file.h
@@ -36,6 +36,9 @@
#define GOOGLE_PROTOBUF_COMPILER_JAVA_FILE_H__
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <vector>
#include <google/protobuf/stubs/common.h>
@@ -96,10 +99,10 @@ class FileGenerator {
string java_package_;
string classname_;
- scoped_array<scoped_ptr<MessageGenerator> > message_generators_;
- scoped_array<scoped_ptr<ExtensionGenerator> > extension_generators_;
- scoped_ptr<GeneratorFactory> generator_factory_;
- scoped_ptr<Context> context_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<MessageGenerator> > message_generators_;
+ google::protobuf::scoped_array<google::protobuf::scoped_ptr<ExtensionGenerator> > extension_generators_;
+ google::protobuf::scoped_ptr<GeneratorFactory> generator_factory_;
+ google::protobuf::scoped_ptr<Context> context_;
ClassNameResolver* name_resolver_;
bool immutable_api_;
diff --git a/src/google/protobuf/compiler/java/java_generator.cc b/src/google/protobuf/compiler/java/java_generator.cc
index c3a47e3e..6c6f7286 100644
--- a/src/google/protobuf/compiler/java/java_generator.cc
+++ b/src/google/protobuf/compiler/java/java_generator.cc
@@ -35,6 +35,9 @@
#include <google/protobuf/compiler/java/java_generator.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/java/java_file.h>
#include <google/protobuf/compiler/java/java_generator_factory.h>
@@ -99,10 +102,14 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
vector<string> all_files;
+
vector<FileGenerator*> file_generators;
if (generate_immutable_code) {
file_generators.push_back(new FileGenerator(file, /* immutable = */ true));
}
+ if (generate_mutable_code) {
+ file_generators.push_back(new FileGenerator(file, /* mutable = */ false));
+ }
for (int i = 0; i < file_generators.size(); ++i) {
if (!file_generators[i]->Validate(error)) {
for (int j = 0; j < file_generators.size(); ++j) {
@@ -123,7 +130,7 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
all_files.push_back(java_filename);
// Generate main java file.
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->Open(java_filename));
io::Printer printer(output.get(), '$');
file_generator->Generate(&printer);
@@ -141,7 +148,7 @@ bool JavaGenerator::Generate(const FileDescriptor* file,
if (!output_list_file.empty()) {
// Generate output list. This is just a simple text file placed in a
// deterministic location which lists the .java files being generated.
- scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
context->Open(output_list_file));
io::Printer srclist_printer(srclist_raw_output.get(), '$');
for (int i = 0; i < all_files.size(); i++) {
diff --git a/src/google/protobuf/compiler/java/java_helpers.cc b/src/google/protobuf/compiler/java/java_helpers.cc
index 3efd7edb..23685385 100644
--- a/src/google/protobuf/compiler/java/java_helpers.cc
+++ b/src/google/protobuf/compiler/java/java_helpers.cc
@@ -306,6 +306,26 @@ JavaType GetJavaType(const FieldDescriptor* field) {
return JAVATYPE_INT;
}
+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.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return NULL;
+}
+
const char* BoxedPrimitiveTypeName(JavaType type) {
switch (type) {
case JAVATYPE_INT : return "java.lang.Integer";
diff --git a/src/google/protobuf/compiler/java/java_helpers.h b/src/google/protobuf/compiler/java/java_helpers.h
index 93f23a93..d957fdc4 100644
--- a/src/google/protobuf/compiler/java/java_helpers.h
+++ b/src/google/protobuf/compiler/java/java_helpers.h
@@ -132,7 +132,7 @@ string FieldConstantName(const FieldDescriptor *field);
// Returns the type of the FieldDescriptor.
// This does nothing interesting for the open source release, but is used for
-// hacks that improve compatability with version 1 protocol buffers at Google.
+// hacks that improve compatibility with version 1 protocol buffers at Google.
FieldDescriptor::Type GetType(const FieldDescriptor* field);
enum JavaType {
@@ -149,6 +149,8 @@ enum JavaType {
JavaType GetJavaType(const FieldDescriptor* field);
+const char* PrimitiveTypeName(JavaType type);
+
// Get the fully-qualified class name for a boxed primitive type, e.g.
// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
// types.
@@ -168,13 +170,6 @@ inline string ImmutableDefaultValue(const FieldDescriptor* field,
}
bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
-// Does this message class use UnknownFieldSet?
-// Otherwise, unknown fields will be stored in a ByteString object
-inline bool UseUnknownFieldSet(const Descriptor* descriptor) {
- return descriptor->file()->options().optimize_for() !=
- FileOptions::LITE_RUNTIME;
-}
-
// Does this message class have generated parsing, serialization, and other
// standard methods for which reflection-based fallback implementations exist?
inline bool HasGeneratedMethods(const Descriptor* descriptor) {
@@ -308,12 +303,27 @@ bool HasRequiredFields(const Descriptor* descriptor);
// Whether a .proto file supports field presence test for non-message types.
inline bool SupportFieldPresence(const FileDescriptor* descriptor) {
- return true;
+ return descriptor->syntax() != FileDescriptor::SYNTAX_PROTO3;
+}
+
+// Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet
+// but in the message and can be queried using additional getters that return
+// ints.
+inline bool SupportUnknownEnumValue(const FileDescriptor* descriptor) {
+ return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO3;
}
// Check whether a mesasge has repeated fields.
bool HasRepeatedFields(const Descriptor* descriptor);
+inline bool IsMapEntry(const Descriptor* descriptor) {
+ return descriptor->options().map_entry();
+}
+
+inline bool PreserveUnknownFields(const Descriptor* descriptor) {
+ return descriptor->file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
+}
+
} // namespace java
} // namespace compiler
} // namespace protobuf
diff --git a/src/google/protobuf/compiler/java/java_map_field.cc b/src/google/protobuf/compiler/java/java_map_field.cc
new file mode 100644
index 00000000..2986f51f
--- /dev/null
+++ b/src/google/protobuf/compiler/java/java_map_field.cc
@@ -0,0 +1,455 @@
+// 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 <google/protobuf/compiler/java/java_map_field.h>
+
+#include <google/protobuf/compiler/java/java_context.h>
+#include <google/protobuf/compiler/java/java_doc_comment.h>
+#include <google/protobuf/compiler/java/java_helpers.h>
+#include <google/protobuf/compiler/java/java_name_resolver.h>
+#include <google/protobuf/io/printer.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace java {
+
+namespace {
+
+const FieldDescriptor* KeyField(const FieldDescriptor* descriptor) {
+ GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
+ const Descriptor* message = descriptor->message_type();
+ GOOGLE_CHECK(message->options().map_entry());
+ return message->FindFieldByName("key");
+}
+
+const FieldDescriptor* ValueField(const FieldDescriptor* descriptor) {
+ GOOGLE_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
+ const Descriptor* message = descriptor->message_type();
+ GOOGLE_CHECK(message->options().map_entry());
+ return message->FindFieldByName("value");
+}
+
+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) {
+ return name_resolver->GetImmutableClassName(field->enum_type());
+ } else {
+ return boxed ? BoxedPrimitiveTypeName(GetJavaType(field))
+ : PrimitiveTypeName(GetJavaType(field));
+ }
+}
+
+string WireType(const FieldDescriptor* field) {
+ return "com.google.protobuf.WireFormat.FieldType." +
+ string(FieldTypeName(field->type()));
+}
+
+void SetMessageVariables(const FieldDescriptor* descriptor,
+ int messageBitIndex,
+ int builderBitIndex,
+ const FieldGeneratorInfo* info,
+ ClassNameResolver* name_resolver,
+ map<string, string>* variables) {
+ SetCommonFieldVariables(descriptor, info, variables);
+
+ (*variables)["type"] =
+ name_resolver->GetImmutableClassName(descriptor->message_type());
+ const FieldDescriptor* key = KeyField(descriptor);
+ const FieldDescriptor* value = ValueField(descriptor);
+ (*variables)["key_type"] = TypeName(key, name_resolver, false);
+ (*variables)["boxed_key_type"] = TypeName(key, name_resolver, true);
+ (*variables)["key_wire_type"] = WireType(key);
+ (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver);
+ if (GetJavaType(value) == JAVATYPE_ENUM) {
+ // We store enums as Integers internally.
+ (*variables)["value_type"] = "int";
+ (*variables)["boxed_value_type"] = "java.lang.Integer";
+ (*variables)["value_wire_type"] = WireType(value);
+ (*variables)["value_default_value"] =
+ DefaultValue(value, true, name_resolver) + ".getNumber()";
+
+ (*variables)["value_enum_type"] = TypeName(value, name_resolver, false);
+
+ if (SupportUnknownEnumValue(descriptor->file())) {
+ // Map unknown values to a special UNRECOGNIZED value if supported.
+ (*variables)["unrecognized_value"] =
+ (*variables)["value_enum_type"] + ".UNRECOGNIZED";
+ } else {
+ // Map unknown values to the default value if we don't have UNRECOGNIZED.
+ (*variables)["unrecognized_value"] =
+ DefaultValue(value, true, name_resolver);
+ }
+ } else {
+ (*variables)["value_type"] = TypeName(value, name_resolver, false);
+ (*variables)["boxed_value_type"] = TypeName(value, name_resolver, true);
+ (*variables)["value_wire_type"] = WireType(value);
+ (*variables)["value_default_value"] =
+ DefaultValue(value, true, name_resolver);
+ }
+ (*variables)["type_parameters"] =
+ (*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)["on_changed"] =
+ HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
+
+ // For repeated fields, one bit is used for whether the array is immutable
+ // in the parsing constructor.
+ (*variables)["get_mutable_bit_parser"] =
+ GenerateGetBitMutableLocal(builderBitIndex);
+ (*variables)["set_mutable_bit_parser"] =
+ GenerateSetBitMutableLocal(builderBitIndex);
+
+ if (HasDescriptorMethods(descriptor->file())) {
+ (*variables)["lite"] = "";
+ (*variables)["map_field_parameter"] = (*variables)["name"] + "DefaultEntry";
+ (*variables)["descriptor"] =
+ name_resolver->GetImmutableClassName(descriptor->file()) +
+ ".internal_" + UniqueFileScopeIdentifier(descriptor->message_type()) +
+ "_descriptor, ";
+ } else {
+ (*variables)["lite"] = "Lite";
+ (*variables)["map_field_parameter"] = "";
+ (*variables)["descriptor"] = "";
+ }
+}
+
+} // namespace
+
+ImmutableMapFieldGenerator::
+ImmutableMapFieldGenerator(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_);
+}
+
+ImmutableMapFieldGenerator::
+~ImmutableMapFieldGenerator() {}
+
+int ImmutableMapFieldGenerator::GetNumBitsForMessage() const {
+ return 0;
+}
+
+int ImmutableMapFieldGenerator::GetNumBitsForBuilder() const {
+ return 1;
+}
+
+void ImmutableMapFieldGenerator::
+GenerateInterfaceMembers(io::Printer* printer) const {
+ if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
+ "get$capitalized_name$();\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$java.util.Map<$type_parameters$>\n"
+ "get$capitalized_name$Value();\n");
+ }
+ } else {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$java.util.Map<$type_parameters$>\n"
+ "get$capitalized_name$();\n");
+ }
+}
+
+void ImmutableMapFieldGenerator::
+GenerateMembers(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "private static final com.google.protobuf.MapEntry$lite$<\n"
+ " $type_parameters$> $name$DefaultEntry =\n"
+ " com.google.protobuf.MapEntry$lite$\n"
+ " .<$type_parameters$>newDefaultInstance(\n"
+ " $descriptor$\n"
+ " $key_wire_type$,\n"
+ " $key_default_value$,\n"
+ " $value_wire_type$,\n"
+ " $value_default_value$);\n");
+ printer->Print(
+ variables_,
+ "private com.google.protobuf.MapField$lite$<\n"
+ " $type_parameters$> $name$_ =\n"
+ " com.google.protobuf.MapField$lite$.emptyMapField(\n"
+ " $map_field_parameter$);\n"
+ "\n");
+ if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
+ printer->Print(
+ variables_,
+ "private static final\n"
+ "com.google.protobuf.Internal.MapAdapter.Converter<\n"
+ " java.lang.Integer, $value_enum_type$> $name$ValueConverter =\n"
+ " com.google.protobuf.Internal.MapAdapter.newEnumConverter(\n"
+ " $value_enum_type$.internalGetValueMap(),\n"
+ " $unrecognized_value$);\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
+ "get$capitalized_name$Value() {\n"
+ " return $name$_.getMap();\n"
+ "}\n");
+ }
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
+ "get$capitalized_name$() {\n"
+ " return new com.google.protobuf.Internal.MapAdapter<\n"
+ " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
+ " $name$_.getMap(), $name$ValueConverter);\n"
+ "}\n");
+ } else {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n"
+ " return $name$_.getMap();\n"
+ "}\n");
+ }
+}
+
+void ImmutableMapFieldGenerator::
+GenerateBuilderMembers(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "private com.google.protobuf.MapField$lite$<\n"
+ " $type_parameters$> $name$_ =\n"
+ " com.google.protobuf.MapField$lite$.newMapField(\n"
+ " $map_field_parameter$);\n"
+ "\n");
+ if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
+ "get$capitalized_name$() {\n"
+ " return new com.google.protobuf.Internal.MapAdapter<\n"
+ " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
+ " $name$_.getMap(), $name$ValueConverter);\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n"
+ "getMutable$capitalized_name$() {\n"
+ " $on_changed$\n"
+ " return new com.google.protobuf.Internal.MapAdapter<\n"
+ " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n"
+ " $name$_.getMutableMap(), $name$ValueConverter);\n"
+ "}\n");
+ if (SupportUnknownEnumValue(descriptor_->file())) {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
+ "get$capitalized_name$Value() {\n"
+ " return $name$_.getMap();\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "$deprecation$\n"
+ "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n"
+ "getMutable$capitalized_name$Value() {\n"
+ " $on_changed$\n"
+ " return $name$_.getMutableMap();\n"
+ "}\n");
+ }
+ } else {
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n"
+ " return $name$_.getMap();\n"
+ "}\n");
+ WriteFieldDocComment(printer, descriptor_);
+ printer->Print(
+ variables_,
+ "public java.util.Map<$type_parameters$>\n"
+ "getMutable$capitalized_name$() {\n"
+ " $on_changed$\n"
+ " return $name$_.getMutableMap();\n"
+ "}\n");
+ }
+}
+
+void ImmutableMapFieldGenerator::
+GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
+ // Nothing to initialize.
+}
+
+void ImmutableMapFieldGenerator::
+GenerateInitializationCode(io::Printer* printer) const {
+ // Nothing to initialize.
+}
+
+void ImmutableMapFieldGenerator::
+GenerateBuilderClearCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "$name$_.clear();\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "$name$_.mergeFrom(other.$name$_);\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateBuildingCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ // We do a copy of the map field to ensure that the built result is
+ // immutable. Implementation of this copy() method can do copy-on-write
+ // to defer this copy until further modifications are made on the field.
+ "result.$name$_ = $name$_.copy();\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateParsingCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "if (!$get_mutable_bit_parser$) {\n"
+ " $name$_ = com.google.protobuf.MapField$lite$.newMapField(\n"
+ " $map_field_parameter$);\n"
+ " $set_mutable_bit_parser$;\n"
+ "}\n");
+ if (!SupportUnknownEnumValue(descriptor_->file()) &&
+ GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) {
+ printer->Print(
+ variables_,
+ "com.google.protobuf.ByteString bytes = input.readBytes();\n"
+ "com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
+ "$name$ = $name$DefaultEntry.getParserForType().parseFrom(bytes);\n");
+ printer->Print(
+ variables_,
+ "if ($value_enum_type$.valueOf($name$.getValue()) == null) {\n"
+ " unknownFields.mergeLengthDelimitedField($number$, bytes);\n"
+ "} else {\n"
+ " $name$_.getMutableMap().put($name$.getKey(), $name$.getValue());\n"
+ "}\n");
+ } else {
+ printer->Print(
+ variables_,
+ "com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
+ "$name$ = input.readMessage(\n"
+ " $name$DefaultEntry.getParserForType(), extensionRegistry);\n"
+ "$name$_.getMutableMap().put($name$.getKey(), $name$.getValue());\n");
+ }
+}
+
+void ImmutableMapFieldGenerator::
+GenerateParsingDoneCode(io::Printer* printer) const {
+ // Nothing to do here.
+}
+
+void ImmutableMapFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "for (java.util.Map.Entry<$type_parameters$> entry\n"
+ " : $name$_.getMap().entrySet()) {\n"
+ " com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
+ " $name$ = $name$DefaultEntry.newBuilderForType()\n"
+ " .setKey(entry.getKey())\n"
+ " .setValue(entry.getValue())\n"
+ " .build();\n"
+ " output.writeMessage($number$, $name$);\n"
+ "}\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "for (java.util.Map.Entry<$type_parameters$> entry\n"
+ " : $name$_.getMap().entrySet()) {\n"
+ " com.google.protobuf.MapEntry$lite$<$type_parameters$>\n"
+ " $name$ = $name$DefaultEntry.newBuilderForType()\n"
+ " .setKey(entry.getKey())\n"
+ " .setValue(entry.getValue())\n"
+ " .build();\n"
+ " size += com.google.protobuf.CodedOutputStream\n"
+ " .computeMessageSize($number$, $name$);\n"
+ "}\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "result = result && $name$_.equals(other.$name$_);\n");
+}
+
+void ImmutableMapFieldGenerator::
+GenerateHashCode(io::Printer* printer) const {
+ printer->Print(
+ variables_,
+ "if (!$name$_.getMap().isEmpty()) {\n"
+ " hash = (37 * hash) + $constant_name$;\n"
+ " hash = (53 * hash) + $name$_.hashCode();\n"
+ "}\n");
+}
+
+string ImmutableMapFieldGenerator::GetBoxedType() const {
+ return name_resolver_->GetImmutableClassName(descriptor_->message_type());
+}
+
+} // namespace java
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/java/java_map_field.h b/src/google/protobuf/compiler/java/java_map_field.h
new file mode 100644
index 00000000..80a94f45
--- /dev/null
+++ b/src/google/protobuf/compiler/java/java_map_field.h
@@ -0,0 +1,82 @@
+// 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_COMPILER_JAVA_MAP_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__
+
+#include <google/protobuf/compiler/java/java_field.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace java {
+
+class ImmutableMapFieldGenerator : public ImmutableFieldGenerator {
+ public:
+ explicit ImmutableMapFieldGenerator(
+ const FieldDescriptor* descriptor, int messageBitIndex,
+ int builderBitIndex, Context* context);
+ ~ImmutableMapFieldGenerator();
+
+ // implements ImmutableFieldGenerator ---------------------------------------
+ int GetNumBitsForMessage() const;
+ int GetNumBitsForBuilder() const;
+ void GenerateInterfaceMembers(io::Printer* printer) const;
+ 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 GenerateMergingCode(io::Printer* printer) const;
+ void GenerateBuildingCode(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;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+ const int messageBitIndex_;
+ const int builderBitIndex_;
+ Context* context_;
+ ClassNameResolver* name_resolver_;
+};
+
+} // namespace java
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__
diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc
index 8aa89ac7..1171b718 100644
--- a/src/google/protobuf/compiler/java/java_message.cc
+++ b/src/google/protobuf/compiler/java/java_message.cc
@@ -38,6 +38,9 @@
#include <google/protobuf/stubs/hash.h>
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <vector>
#include <google/protobuf/compiler/java/java_context.h>
@@ -67,6 +70,13 @@ 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
// ===================================================================
@@ -264,108 +274,75 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
descriptor_->containing_type() == NULL &&
MultipleJavaFiles(descriptor_->file(), /* immutable = */ true);
+ map<string, string> variables;
+ variables["static"] = is_own_file ? " " : " static ";
+ variables["classname"] = descriptor_->name();
+ variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
+ variables["lite"] = HasDescriptorMethods(descriptor_) ? "" : "Lite";
+
WriteMessageDocComment(printer, descriptor_);
// The builder_type stores the super type name of the nested Builder class.
string builder_type;
if (descriptor_->extension_range_count() > 0) {
- if (HasDescriptorMethods(descriptor_)) {
- printer->Print(
- "public$static$final class $classname$ extends\n"
- " com.google.protobuf.GeneratedMessage.ExtendableMessage<\n"
- " $classname$> implements\n"
- " $extra_interfaces$\n"
- " $classname$OrBuilder {\n",
- "static", is_own_file ? " " : " static ",
- "classname", descriptor_->name(),
- "extra_interfaces", ExtraMessageInterfaces(descriptor_));
- builder_type = strings::Substitute(
- "com.google.protobuf.GeneratedMessage.ExtendableBuilder<$0, ?>",
- name_resolver_->GetImmutableClassName(descriptor_));
- } else {
- printer->Print(
- "public$static$final class $classname$ extends\n"
- " com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n"
- " $classname$> implements\n"
- " $extra_interfaces$\n"
- " $classname$OrBuilder {\n",
- "static", is_own_file ? " " : " static ",
- "classname", descriptor_->name(),
- "extra_interfaces", ExtraMessageInterfaces(descriptor_));
- builder_type = strings::Substitute(
- "com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>",
- name_resolver_->GetImmutableClassName(descriptor_));
- }
+ printer->Print(variables,
+ "public $static$final class $classname$ extends\n"
+ " com.google.protobuf.GeneratedMessage$lite$.ExtendableMessage<\n"
+ " $classname$> implements\n"
+ " $extra_interfaces$\n"
+ " $classname$OrBuilder {\n");
+ builder_type = strings::Substitute(
+ "com.google.protobuf.GeneratedMessage$1.ExtendableBuilder<$0, ?>",
+ name_resolver_->GetImmutableClassName(descriptor_),
+ variables["lite"]);
} else {
- if (HasDescriptorMethods(descriptor_)) {
- printer->Print(
- "public$static$final class $classname$ extends\n"
- " com.google.protobuf.GeneratedMessage implements\n"
- " $extra_interfaces$\n"
- " $classname$OrBuilder {\n",
- "static", is_own_file ? " " : " static ",
- "classname", descriptor_->name(),
- "extra_interfaces", ExtraMessageInterfaces(descriptor_));
- builder_type = "com.google.protobuf.GeneratedMessage.Builder<?>";
- } else {
- printer->Print(
- "public$static$final class $classname$ extends\n"
- " com.google.protobuf.GeneratedMessageLite implements\n"
- " $extra_interfaces$\n"
- " $classname$OrBuilder {\n",
- "static", is_own_file ? " " : " static ",
- "classname", descriptor_->name(),
- "extra_interfaces", ExtraMessageInterfaces(descriptor_));
- builder_type = "com.google.protobuf.GeneratedMessageLite.Builder";
- }
+ printer->Print(variables,
+ "public $static$final class $classname$ extends\n"
+ " com.google.protobuf.GeneratedMessage$lite$ implements\n"
+ " $extra_interfaces$\n"
+ " $classname$OrBuilder {\n");
+ builder_type = strings::Substitute(
+ "com.google.protobuf.GeneratedMessage$0.Builder",
+ variables["lite"]);
}
printer->Indent();
- // Using builder_type, instead of Builder, prevents the Builder class from
- // being loaded into PermGen space when the default instance is created.
- // 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"
- "$set_unknown_fields$\n"
- "}\n",
- "classname", descriptor_->name(),
- "buildertype", builder_type,
- "set_unknown_fields",
- " this.unknownFields = builder.getUnknownFields();");
- printer->Print(
- // Used when constructing the default instance, which cannot be initialized
- // immediately because it may cyclically refer to other default instances.
- "private $classname$(boolean noInit) {$set_default_unknown_fields$}\n"
- "\n"
- "private static final $classname$ defaultInstance;\n"
- "public static $classname$ getDefaultInstance() {\n"
- " return defaultInstance;\n"
- "}\n"
- "\n"
- "public $classname$ getDefaultInstanceForType() {\n"
- " return defaultInstance;\n"
- "}\n"
- "\n",
- "classname", descriptor_->name(),
- "set_default_unknown_fields", UseUnknownFieldSet(descriptor_)
- ? " this.unknownFields ="
- " com.google.protobuf.UnknownFieldSet.getDefaultInstance(); "
- : " this.unknownFields = com.google.protobuf.ByteString.EMPTY;");
+ if (HasDescriptorMethods(descriptor_)) {
+ // Using builder_type, instead of Builder, prevents the Builder class from
+ // being loaded into PermGen space when the default instance is created.
+ // 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());
+ printer->Indent();
+ GenerateInitializers(printer);
+ printer->Outdent();
+ printer->Print(
+ "}\n"
+ "\n");
+ }
- if (UseUnknownFieldSet(descriptor_)) {
+ if (HasDescriptorMethods(descriptor_)) {
printer->Print(
- "private final com.google.protobuf.UnknownFieldSet unknownFields;\n"
- ""
- "@java.lang.Override\n"
- "public final com.google.protobuf.UnknownFieldSet\n"
- " getUnknownFields() {\n"
- " return this.unknownFields;\n"
- "}\n");
- } else {
+ "@java.lang.Override\n"
+ "public final com.google.protobuf.UnknownFieldSet\n"
+ "getUnknownFields() {\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ printer->Print(
+ " return this.unknownFields;\n");
+ } else {
+ printer->Print(
+ " return com.google.protobuf.UnknownFieldSet.getDefaultInstance();\n");
+ }
printer->Print(
- "private final com.google.protobuf.ByteString unknownFields;\n");
+ "}\n");
}
if (HasGeneratedMethods(descriptor_)) {
@@ -382,6 +359,8 @@ void ImmutableMessageGenerator::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;
ImmutableMessageGenerator messageGenerator(
descriptor_->nested_type(i), context_);
messageGenerator.GenerateInterface(printer);
@@ -480,20 +459,6 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
printer->Print("\n");
}
- // Called by the constructor, except in the case of the default instance,
- // in which case this is called by static init code later on.
- printer->Print("private void initFields() {\n");
- printer->Indent();
- for (int i = 0; i < descriptor_->field_count(); i++) {
- if (!descriptor_->field(i)->containing_oneof()) {
- field_generators_.get(descriptor_->field(i))
- .GenerateInitializationCode(printer);
- }
- }
-
- printer->Outdent();
- printer->Print("}\n");
-
if (HasGeneratedMethods(descriptor_)) {
GenerateIsInitialized(printer, MEMOIZE);
GenerateMessageSerializationMethods(printer);
@@ -507,19 +472,52 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
GenerateParseFromMethods(printer);
GenerateBuilder(printer);
- // Carefully initialize the default instance in such a way that it doesn't
- // conflict with other initialization.
printer->Print(
"\n"
- "static {\n"
- " defaultInstance = new $classname$(true);\n"
- " defaultInstance.initFields();\n"
- "}\n"
- "\n"
"// @@protoc_insertion_point(class_scope:$full_name$)\n",
- "classname", descriptor_->name(),
"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$ defaultInstance;",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ if (HasDescriptorMethods(descriptor_)) {
+ printer->Print(
+ "static {\n"
+ " defaultInstance = new $classname$();\n"
+ "}\n"
+ "\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ } else {
+ // LITE_RUNTIME only has one constructor.
+ printer->Print(
+ "static {\n"
+ " try {\n"
+ " defaultInstance = new $classname$(\n"
+ " com.google.protobuf.Internal\n"
+ " .EMPTY_CODED_INPUT_STREAM,\n"
+ " com.google.protobuf.ExtensionRegistryLite\n"
+ " .getEmptyRegistry());\n"
+ " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
+ " throw new ExceptionInInitializerError(e);\n"
+ " }\n"
+ "}\n"
+ "\n",
+ "classname", descriptor_->name());
+ }
+ printer->Print(
+ "public static $classname$ getDefaultInstance() {\n"
+ " return defaultInstance;\n"
+ "}\n"
+ "\n"
+ "public $classname$ getDefaultInstanceForType() {\n"
+ " return defaultInstance;\n"
+ "}\n"
+ "\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+
// Extensions must be declared after the defaultInstance is initialized
// because the defaultInstance is used by the extension to lazily retrieve
// the outer class's FileDescriptor.
@@ -537,7 +535,7 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) {
void ImmutableMessageGenerator::
GenerateMessageSerializationMethods(io::Printer* printer) {
- scoped_array<const FieldDescriptor*> sorted_fields(
+ google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields(
SortFieldsByNumber(descriptor_));
vector<const Descriptor::ExtensionRange*> sorted_extensions;
@@ -593,17 +591,15 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
}
}
- if (UseUnknownFieldSet(descriptor_)) {
- if (descriptor_->options().message_set_wire_format()) {
+ if (PreserveUnknownFields(descriptor_)) {
+ if (descriptor_->options().message_set_wire_format()
+ && HasDescriptorMethods(descriptor_)) {
printer->Print(
- "getUnknownFields().writeAsMessageSetTo(output);\n");
+ "unknownFields.writeAsMessageSetTo(output);\n");
} else {
printer->Print(
- "getUnknownFields().writeTo(output);\n");
+ "unknownFields.writeTo(output);\n");
}
- } else {
- printer->Print(
- "output.writeRawBytes(unknownFields);\n");
}
printer->Outdent();
@@ -632,17 +628,15 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
}
}
- if (UseUnknownFieldSet(descriptor_)) {
- if (descriptor_->options().message_set_wire_format()) {
+ if (PreserveUnknownFields(descriptor_)) {
+ if (descriptor_->options().message_set_wire_format()
+ && HasDescriptorMethods(descriptor_)) {
printer->Print(
- "size += getUnknownFields().getSerializedSizeAsMessageSet();\n");
+ "size += unknownFields.getSerializedSizeAsMessageSet();\n");
} else {
printer->Print(
- "size += getUnknownFields().getSerializedSize();\n");
+ "size += unknownFields.getSerializedSize();\n");
}
- } else {
- printer->Print(
- "size += unknownFields.size();\n");
}
printer->Outdent();
@@ -653,13 +647,7 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
"\n");
printer->Print(
- "private static final long serialVersionUID = 0L;\n"
- "@java.lang.Override\n"
- "protected java.lang.Object writeReplace()\n"
- " throws java.io.ObjectStreamException {\n"
- " return super.writeReplace();\n"
- "}\n"
- "\n");
+ "private static final long serialVersionUID = 0L;\n");
}
void ImmutableMessageGenerator::
@@ -740,7 +728,7 @@ void ImmutableMessageGenerator::GenerateSerializeOneExtensionRange(
void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) {
printer->Print(
- "public static Builder newBuilder() { return Builder.create(); }\n"
+ "public static Builder newBuilder() { return new Builder(); }\n"
"public Builder newBuilderForType() { return newBuilder(); }\n"
"public static Builder newBuilder($classname$ prototype) {\n"
" return newBuilder().mergeFrom(prototype);\n"
@@ -864,6 +852,20 @@ void ImmutableMessageGenerator::GenerateBuilder(io::Printer* printer) {
.GenerateBuilderMembers(printer);
}
+ if (!PreserveUnknownFields(descriptor_)) {
+ printer->Print(
+ "public final Builder setUnknownFields(\n"
+ " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
+ " return this;\n"
+ "}\n"
+ "\n"
+ "public final Builder mergeUnknownFields(\n"
+ " final com.google.protobuf.UnknownFieldSet unknownFields) {\n"
+ " return this;\n"
+ "}\n"
+ "\n");
+ }
+
printer->Print(
"\n"
"// @@protoc_insertion_point(builder_scope:$full_name$)\n",
@@ -886,6 +888,41 @@ GenerateDescriptorMethods(io::Printer* printer) {
"fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
"identifier", UniqueFileScopeIdentifier(descriptor_));
}
+ vector<const FieldDescriptor*> map_fields;
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ if (GetJavaType(field) == JAVATYPE_MESSAGE &&
+ IsMapEntry(field->message_type())) {
+ map_fields.push_back(field);
+ }
+ }
+ if (!map_fields.empty()) {
+ printer->Print(
+ "@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) {
+ const FieldDescriptor* field = map_fields[i];
+ const FieldGeneratorInfo* info = context_->GetFieldGeneratorInfo(field);
+ printer->Print(
+ "case $number$:\n"
+ " return $name$_;\n",
+ "number", SimpleItoa(field->number()),
+ "name", info->name);
+ }
+ printer->Print(
+ "default:\n"
+ " throw new RuntimeException(\n"
+ " \"Invalid map field number: \" + number);\n");
+ printer->Outdent();
+ printer->Outdent();
+ printer->Print(
+ " }\n"
+ "}\n");
+ }
printer->Print(
"protected com.google.protobuf.GeneratedMessage.FieldAccessorTable\n"
" internalGetFieldAccessorTable() {\n"
@@ -904,22 +941,33 @@ GenerateDescriptorMethods(io::Printer* printer) {
void ImmutableMessageGenerator::
GenerateCommonBuilderMethods(io::Printer* printer) {
- printer->Print(
- "// Construct using $classname$.newBuilder()\n"
- "private Builder() {\n"
- " maybeForceBuilderInitialization();\n"
- "}\n"
- "\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
-
if (HasDescriptorMethods(descriptor_)) {
printer->Print(
+ "// Construct using $classname$.newBuilder()\n"
+ "private Builder() {\n"
+ " maybeForceBuilderInitialization();\n"
+ "}\n"
+ "\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+
+ printer->Print(
"private Builder(\n"
" com.google.protobuf.GeneratedMessage.BuilderParent parent) {\n"
" super(parent);\n"
" maybeForceBuilderInitialization();\n"
"}\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
+ } else {
+ // LITE runtime passes along the default instance to implement
+ // getDefaultInstanceForType() at the GneratedMessageLite level.
+ printer->Print(
+ "// Construct using $classname$.newBuilder()\n"
+ "private Builder() {\n"
+ " super(defaultInstance);\n"
+ " maybeForceBuilderInitialization();\n"
+ "}\n"
+ "\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
}
@@ -949,13 +997,8 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
}
printer->Print(
- "private static Builder create() {\n"
- " return new Builder();\n"
- "}\n"
- "\n"
"public Builder clear() {\n"
- " super.clear();\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ " super.clear();\n");
printer->Indent();
@@ -979,12 +1022,8 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
printer->Print(
" return this;\n"
"}\n"
- "\n"
- "public Builder clone() {\n"
- " return create().mergeFrom(buildPartial());\n"
- "}\n"
- "\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ "\n");
+
if (HasDescriptorMethods(descriptor_)) {
printer->Print(
"public com.google.protobuf.Descriptors.Descriptor\n"
@@ -994,28 +1033,59 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
"\n",
"fileclass", name_resolver_->GetImmutableClassName(descriptor_->file()),
"identifier", UniqueFileScopeIdentifier(descriptor_));
+
+ // LITE runtime implements this in GeneratedMessageLite.
+ printer->Print(
+ "public $classname$ getDefaultInstanceForType() {\n"
+ " return $classname$.getDefaultInstance();\n"
+ "}\n"
+ "\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
}
- printer->Print(
- "public $classname$ getDefaultInstanceForType() {\n"
- " return $classname$.getDefaultInstance();\n"
- "}\n"
- "\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
// -----------------------------------------------------------------
- printer->Print(
- "public $classname$ build() {\n"
- " $classname$ result = buildPartial();\n"
- " if (!result.isInitialized()) {\n"
- " throw newUninitializedMessageException(result);\n"
- " }\n"
- " return result;\n"
- "}\n"
- "\n"
- "public $classname$ buildPartial() {\n"
- " $classname$ result = new $classname$(this);\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ if (HasDescriptorMethods(descriptor_)) {
+ // LITE implements build in GeneratedMessageLite to save methods.
+ printer->Print(
+ "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_));
+ }
+
+ if (HasDescriptorMethods(descriptor_)) {
+ printer->Print(
+ "public $classname$ buildPartial() {\n"
+ " $classname$ result = new $classname$(this);\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ } else {
+ // LITE_RUNTIME only provides a single message constructor.
+ printer->Print(
+ "public $classname$ buildPartial() {\n"
+ " $classname$ result = null;\n"
+ " try {\n"
+ " result = new $classname$(\n"
+ " com.google.protobuf.Internal\n"
+ " .EMPTY_CODED_INPUT_STREAM,\n"
+ " com.google.protobuf.ExtensionRegistryLite\n"
+ " .getEmptyRegistry());\n"
+ " } catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
+ " throw new RuntimeException(e);\n"
+ " }\n"
+ " result.unknownFields = this.unknownFields;\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+
+ if (descriptor_->extension_range_count() > 0) {
+ printer->Print(
+ " result.extensions = this.buildExtensions();\n");
+ }
+ }
printer->Indent();
@@ -1153,13 +1223,13 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
" this.mergeExtensionFields(other);\n");
}
- if (UseUnknownFieldSet(descriptor_)) {
+ if (PreserveUnknownFields(descriptor_)) {
printer->Print(
- " this.mergeUnknownFields(other.getUnknownFields());\n");
- } else {
- printer->Print(
- " setUnknownFields(\n"
- " getUnknownFields().concat(other.unknownFields));\n");
+ " this.mergeUnknownFields(other.unknownFields);\n");
+ }
+
+ if (HasDescriptorMethods(descriptor_)) {
+ printer->Print(" onChanged();\n");
}
printer->Print(
@@ -1173,25 +1243,28 @@ GenerateCommonBuilderMethods(io::Printer* printer) {
void ImmutableMessageGenerator::
GenerateBuilderParsingMethods(io::Printer* printer) {
- printer->Print(
- "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;\n"
- " } finally {\n"
- " if (parsedMessage != null) {\n"
- " mergeFrom(parsedMessage);\n"
- " }\n"
- " }\n"
- " return this;\n"
- "}\n",
- "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ if (HasDescriptorMethods(descriptor_)) {
+ // LITE_RUNTIME implements this at the GeneratedMessageLite level.
+ printer->Print(
+ "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;\n"
+ " } finally {\n"
+ " if (parsedMessage != null) {\n"
+ " mergeFrom(parsedMessage);\n"
+ " }\n"
+ " }\n"
+ " return this;\n"
+ "}\n",
+ "classname", name_resolver_->GetImmutableClassName(descriptor_));
+ }
}
// ===================================================================
@@ -1256,30 +1329,55 @@ void ImmutableMessageGenerator::GenerateIsInitialized(
"memoize", memoization ? "memoizedIsInitialized = 0;" : "");
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 (has$name$()) {\n"
" if (!get$name$().isInitialized()) {\n"
" $memoize$\n"
" return false;\n"
" }\n"
"}\n",
- "type", name_resolver_->GetImmutableClassName(
- field->message_type()),
"name", info->capitalized_name,
"memoize", memoization ? "memoizedIsInitialized = 0;" : "");
break;
case FieldDescriptor::LABEL_REPEATED:
- printer->Print(
- "for (int i = 0; i < get$name$Count(); i++) {\n"
- " if (!get$name$(i).isInitialized()) {\n"
- " $memoize$\n"
- " return false;\n"
- " }\n"
- "}\n",
- "type", name_resolver_->GetImmutableClassName(
- field->message_type()),
- "name", info->capitalized_name,
- "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
+ if (IsMapEntry(field->message_type())) {
+ printer->Print(
+ "for ($type$ item : get$name$().values()) {\n"
+ " if (!item.isInitialized()) {\n"
+ " $memoize$\n"
+ " return false;\n"
+ " }\n"
+ "}\n",
+ "type", MapValueImmutableClassdName(field->message_type(),
+ name_resolver_),
+ "name", info->capitalized_name,
+ "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
+ } else {
+ printer->Print(
+ "for (int i = 0; i < get$name$Count(); i++) {\n"
+ " if (!get$name$(i).isInitialized()) {\n"
+ " $memoize$\n"
+ " return false;\n"
+ " }\n"
+ "}\n",
+ "type", name_resolver_->GetImmutableClassName(
+ field->message_type()),
+ "name", info->capitalized_name,
+ "memoize", memoization ? "memoizedIsInitialized = 0;" : "");
+ }
break;
}
}
@@ -1358,10 +1456,14 @@ GenerateEqualsAndHashCode(io::Printer* printer) {
"}\n");
}
}
- if (HasDescriptorMethods(descriptor_)) {
+ if (PreserveUnknownFields(descriptor_)) {
+ // Always consider unknown fields for equality. This will sometimes return
+ // false for non-canonical ordering when running in LITE_RUNTIME but it's
+ // the best we can do.
printer->Print(
- "result = result &&\n"
- " getUnknownFields().equals(other.getUnknownFields());\n");
+ "result = result && unknownFields.equals(other.unknownFields);\n");
+ }
+ if (HasDescriptorMethods(descriptor_)) {
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"result = result &&\n"
@@ -1421,13 +1523,8 @@ GenerateEqualsAndHashCode(io::Printer* printer) {
}
}
- if (UseUnknownFieldSet(descriptor_)) {
- printer->Print(
- "hash = (29 * hash) + getUnknownFields().hashCode();\n");
- } else {
- printer->Print(
- "hash = (29 * hash) + unknownFields.hashCode();\n");
- }
+ printer->Print(
+ "hash = (29 * hash) + unknownFields.hashCode();\n");
printer->Print(
"memoizedHashCode = hash;\n"
"return hash;\n");
@@ -1455,7 +1552,7 @@ GenerateExtensionRegistrationCode(io::Printer* printer) {
// ===================================================================
void ImmutableMessageGenerator::
GenerateParsingConstructor(io::Printer* printer) {
- scoped_array<const FieldDescriptor*> sorted_fields(
+ google::protobuf::scoped_array<const FieldDescriptor * > sorted_fields(
SortFieldsByNumber(descriptor_));
printer->Print(
@@ -1467,8 +1564,13 @@ GenerateParsingConstructor(io::Printer* printer) {
printer->Indent();
// Initialize all fields to default.
- printer->Print(
- "initFields();\n");
+ if (HasDescriptorMethods(descriptor_)) {
+ printer->Print(
+ "this();\n");
+ } else {
+ // LITE_RUNTIME only has one constructor.
+ GenerateInitializers(printer);
+ }
// Use builder bits to track mutable repeated fields.
int totalBuilderBits = 0;
@@ -1483,17 +1585,16 @@ GenerateParsingConstructor(io::Printer* printer) {
"bit_field_name", GetBitFieldName(i));
}
- if (UseUnknownFieldSet(descriptor_)) {
- printer->Print(
- "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n"
- " com.google.protobuf.UnknownFieldSet.newBuilder();\n");
- } else {
- printer->Print(
- "com.google.protobuf.ByteString.Output unknownFieldsOutput =\n"
- " com.google.protobuf.ByteString.newOutput();\n"
- "com.google.protobuf.CodedOutputStream unknownFieldsCodedOutput =\n"
- " com.google.protobuf.CodedOutputStream.newInstance(\n"
- " unknownFieldsOutput);\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ if (HasDescriptorMethods(descriptor_)) {
+ printer->Print(
+ "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n"
+ " com.google.protobuf.UnknownFieldSet.newBuilder();\n");
+ } else {
+ printer->Print(
+ "com.google.protobuf.UnknownFieldSetLite.Builder unknownFields =\n"
+ " com.google.protobuf.UnknownFieldSetLite.newBuilder();\n");
+ }
}
printer->Print(
@@ -1513,16 +1614,41 @@ GenerateParsingConstructor(io::Printer* printer) {
printer->Print(
"case 0:\n" // zero signals EOF / limit reached
" done = true;\n"
- " break;\n"
- "default: {\n"
- " if (!parseUnknownField(input,$unknown_fields$\n"
- " extensionRegistry, tag)) {\n"
- " done = true;\n" // it's an endgroup tag
- " }\n"
- " break;\n"
- "}\n",
- "unknown_fields", UseUnknownFieldSet(descriptor_)
- ? " unknownFields," : " unknownFieldsCodedOutput,");
+ " break;\n");
+
+ if (PreserveUnknownFields(descriptor_)) {
+ if (!HasDescriptorMethods(descriptor_)
+ && descriptor_->extension_range_count() > 0) {
+ // Lite runtime directly invokes parseUnknownField to reduce method
+ // counts.
+ printer->Print(
+ "default: {\n"
+ " if (!parseUnknownField(extensions, getDefaultInstanceForType(),\n"
+ " input, unknownFields,\n"
+ " extensionRegistry, tag)) {\n"
+ " done = true;\n" // it's an endgroup tag
+ " }\n"
+ " break;\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "default: {\n"
+ " if (!parseUnknownField(input, unknownFields,\n"
+ " extensionRegistry, tag)) {\n"
+ " done = true;\n" // it's an endgroup tag
+ " }\n"
+ " break;\n"
+ "}\n");
+ }
+ } else {
+ printer->Print(
+ "default: {\n"
+ " if (!input.skipField(tag)) {\n"
+ " done = true;\n" // it's an endgroup tag
+ " }\n"
+ " break;\n"
+ "}\n");
+ }
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = sorted_fields[i];
@@ -1582,25 +1708,24 @@ GenerateParsingConstructor(io::Printer* printer) {
field_generators_.get(field).GenerateParsingDoneCode(printer);
}
- // Make unknown fields immutable.
- if (UseUnknownFieldSet(descriptor_)) {
- printer->Print(
- "this.unknownFields = unknownFields.build();\n");
+ if (PreserveUnknownFields(descriptor_)) {
+ // Make unknown fields immutable.
+ printer->Print("this.unknownFields = unknownFields.build();\n");
+ }
+
+ if (!HasDescriptorMethods(descriptor_)) {
+ // LITE runtime uses a static method to reduce method count.
+ if (descriptor_->extension_range_count() > 0) {
+ // Make extensions immutable.
+ printer->Print(
+ "makeExtensionsImmutable(extensions);\n");
+ }
} else {
+ // Make extensions immutable.
printer->Print(
- "try {\n"
- " unknownFieldsCodedOutput.flush();\n"
- "} catch (java.io.IOException e) {\n"
- "// Should not happen\n"
- "} finally {\n"
- " unknownFields = unknownFieldsOutput.toByteString();\n"
- "}\n");
+ "makeExtensionsImmutable();\n");
}
- // Make extensions immutable.
- printer->Print(
- "makeExtensionsImmutable();\n");
-
printer->Outdent();
printer->Outdent();
printer->Print(
@@ -1611,7 +1736,7 @@ GenerateParsingConstructor(io::Printer* printer) {
// ===================================================================
void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) {
printer->Print(
- "public static com.google.protobuf.Parser<$classname$> PARSER =\n"
+ "public static final com.google.protobuf.Parser<$classname$> PARSER =\n"
" new com.google.protobuf.AbstractParser<$classname$>() {\n",
"classname", descriptor_->name());
printer->Indent();
@@ -1659,6 +1784,16 @@ void ImmutableMessageGenerator::GenerateParser(io::Printer* printer) {
"classname", descriptor_->name());
}
+// ===================================================================
+void ImmutableMessageGenerator::GenerateInitializers(io::Printer* printer) {
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ if (!descriptor_->field(i)->containing_oneof()) {
+ field_generators_.get(descriptor_->field(i))
+ .GenerateInitializationCode(printer);
+ }
+ }
+}
+
} // namespace java
} // namespace compiler
diff --git a/src/google/protobuf/compiler/java/java_message.h b/src/google/protobuf/compiler/java/java_message.h
index fece1c21..91eb2876 100644
--- a/src/google/protobuf/compiler/java/java_message.h
+++ b/src/google/protobuf/compiler/java/java_message.h
@@ -121,6 +121,7 @@ class ImmutableMessageGenerator : public MessageGenerator {
void GenerateBuilderParsingMethods(io::Printer* printer);
void GenerateIsInitialized(io::Printer* printer,
UseMemoization useMemoization);
+ void GenerateInitializers(io::Printer* printer);
void GenerateEqualsAndHashCode(io::Printer* printer);
void GenerateParser(io::Printer* printer);
void GenerateParsingConstructor(io::Printer* printer);
diff --git a/src/google/protobuf/compiler/java/java_message_field.cc b/src/google/protobuf/compiler/java/java_message_field.cc
index e681314e..538f1248 100644
--- a/src/google/protobuf/compiler/java/java_message_field.cc
+++ b/src/google/protobuf/compiler/java/java_message_field.cc
@@ -179,7 +179,7 @@ GenerateMembers(io::Printer* printer) const {
WriteFieldDocComment(printer, descriptor_);
printer->Print(variables_,
"$deprecation$public $type$ get$capitalized_name$() {\n"
- " return $name$_;\n"
+ " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n"
"}\n");
if (HasNestedBuilders(descriptor_->containing_type())) {
@@ -187,7 +187,7 @@ GenerateMembers(io::Printer* printer) const {
printer->Print(variables_,
"$deprecation$public $type$OrBuilder "
"get$capitalized_name$OrBuilder() {\n"
- " return $name$_;\n"
+ " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n"
"}\n");
}
} else {
@@ -257,14 +257,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
bool support_field_presence = SupportFieldPresence(descriptor_->file());
- if (support_field_presence) {
- printer->Print(variables_,
- // Used when the builder is null.
- "private $type$ $name$_ = $type$.getDefaultInstance();\n");
- } else {
- printer->Print(variables_,
- "private $type$ $name$_ = null;\n");
- }
+ printer->Print(variables_,
+ "private $type$ $name$_ = null;\n");
if (HasNestedBuilders(descriptor_->containing_type())) {
printer->Print(variables_,
@@ -296,13 +290,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
WriteFieldDocComment(printer, descriptor_);
PrintNestedBuilderFunction(printer,
"$deprecation$public $type$ get$capitalized_name$()",
-
- support_field_presence
- ? "return $name$_;\n"
- : "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n",
-
+ "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n",
"return $name$Builder_.getMessage();\n",
-
NULL);
// Field.Builder setField(Field value)
@@ -342,6 +331,7 @@ GenerateBuilderMembers(io::Printer* printer) const {
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"
@@ -367,11 +357,8 @@ GenerateBuilderMembers(io::Printer* printer) const {
PrintNestedBuilderFunction(printer,
"$deprecation$public Builder clear$capitalized_name$()",
- support_field_presence
- ? "$name$_ = $type$.getDefaultInstance();\n"
- "$on_changed$\n"
- : "$name$_ = null;\n"
- "$on_changed$\n",
+ "$name$_ = null;\n"
+ "$on_changed$\n",
support_field_presence
? "$name$Builder_.clear();\n"
@@ -394,16 +381,9 @@ GenerateBuilderMembers(io::Printer* printer) const {
"$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n"
" if ($name$Builder_ != null) {\n"
" return $name$Builder_.getMessageOrBuilder();\n"
- " } else {\n");
- if (support_field_presence) {
- printer->Print(variables_,
- " return $name$_;\n");
- } else {
- printer->Print(variables_,
- " return $name$_ == null ?\n"
- " $type$.getDefaultInstance() : $name$_;\n");
- }
- printer->Print(variables_,
+ " } else {\n"
+ " return $name$_ == null ?\n"
+ " $type$.getDefaultInstance() : $name$_;\n"
" }\n"
"}\n");
WriteFieldDocComment(printer, descriptor_);
@@ -434,17 +414,13 @@ GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
void ImmutableMessageFieldGenerator::
-GenerateInitializationCode(io::Printer* printer) const {
- if (SupportFieldPresence(descriptor_->file())) {
- printer->Print(variables_, "$name$_ = $type$.getDefaultInstance();\n");
- }
-}
+GenerateInitializationCode(io::Printer* printer) const {}
void ImmutableMessageFieldGenerator::
GenerateBuilderClearCode(io::Printer* printer) const {
if (SupportFieldPresence(descriptor_->file())) {
PrintNestedBuilderCondition(printer,
- "$name$_ = $type$.getDefaultInstance();\n",
+ "$name$_ = null;\n",
"$name$Builder_.clear();\n");
printer->Print(variables_, "$clear_has_field_bit_builder$\n");
@@ -514,7 +490,7 @@ void ImmutableMessageFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($is_field_present_message$) {\n"
- " output.write$group_or_message$($number$, $name$_);\n"
+ " output.write$group_or_message$($number$, get$capitalized_name$());\n"
"}\n");
}
@@ -523,7 +499,7 @@ 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$, $name$_);\n"
+ " .compute$group_or_message$Size($number$, get$capitalized_name$());\n"
"}\n");
}
diff --git a/src/google/protobuf/compiler/java/java_plugin_unittest.cc b/src/google/protobuf/compiler/java/java_plugin_unittest.cc
index c4d6995d..fe527623 100644
--- a/src/google/protobuf/compiler/java/java_plugin_unittest.cc
+++ b/src/google/protobuf/compiler/java/java_plugin_unittest.cc
@@ -35,6 +35,9 @@
// worth.
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/java/java_generator.h>
#include <google/protobuf/compiler/command_line_interface.h>
@@ -72,7 +75,7 @@ class TestGenerator : public CodeGenerator {
void TryInsert(const string& filename, const string& insertion_point,
GeneratorContext* context) const {
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->OpenForInsert(filename, insertion_point));
io::Printer printer(output.get(), '$');
printer.Print("// inserted $name$\n", "name", insertion_point);
diff --git a/src/google/protobuf/compiler/java/java_primitive_field.cc b/src/google/protobuf/compiler/java/java_primitive_field.cc
index 6713d29a..e331d7a4 100644
--- a/src/google/protobuf/compiler/java/java_primitive_field.cc
+++ b/src/google/protobuf/compiler/java/java_primitive_field.cc
@@ -55,26 +55,6 @@ using internal::WireFormatLite;
namespace {
-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.
- }
-
- GOOGLE_LOG(FATAL) << "Can't get here.";
- return NULL;
-}
-
void SetPrimitiveVariables(const FieldDescriptor* descriptor,
int messageBitIndex,
int builderBitIndex,
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 ea77f792..2e61ea8a 100644
--- a/src/google/protobuf/compiler/java/java_shared_code_generator.cc
+++ b/src/google/protobuf/compiler/java/java_shared_code_generator.cc
@@ -33,6 +33,9 @@
#include <google/protobuf/compiler/java/java_shared_code_generator.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/java/java_helpers.h>
#include <google/protobuf/compiler/java/java_name_resolver.h>
@@ -65,8 +68,8 @@ void SharedCodeGenerator::Generate(GeneratorContext* context,
string classname = name_resolver_->GetDescriptorClassName(file_);
string filename = package_dir + classname + ".java";
file_list->push_back(filename);
- scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
- scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$'));
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
+ google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get(), '$'));
printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
@@ -176,7 +179,8 @@ void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) {
// Invoke internalBuildGeneratedFileFrom() to build the file.
printer->Print(
"com.google.protobuf.Descriptors.FileDescriptor\n"
- " .internalBuildGeneratedFileFrom(descriptorData,\n"
+ " .internalBuildGeneratedFileFrom(descriptorData,\n");
+ printer->Print(
" new com.google.protobuf.Descriptors.FileDescriptor[] {\n");
for (int i = 0; i < dependencies.size(); i++) {
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 1bb2f3dc..38a32fc2 100644
--- a/src/google/protobuf/compiler/java/java_shared_code_generator.h
+++ b/src/google/protobuf/compiler/java/java_shared_code_generator.h
@@ -36,6 +36,9 @@
#define GOOGLE_PROTOBUF_COMPILER_JAVA_SHARED_CODE_GENERATOR_H__
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <vector>
@@ -68,6 +71,7 @@ class SharedCodeGenerator {
void Generate(GeneratorContext* generator_context,
vector<string>* file_list);
+
void GenerateDescriptors(io::Printer* printer);
private:
@@ -76,7 +80,7 @@ class SharedCodeGenerator {
// improve compatibility with version 1 of protocol buffers.
bool ShouldIncludeDependency(const FileDescriptor* descriptor);
- scoped_ptr<ClassNameResolver> name_resolver_;
+ google::protobuf::scoped_ptr<ClassNameResolver> name_resolver_;
const FileDescriptor* file_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SharedCodeGenerator);
};
diff --git a/src/google/protobuf/compiler/java/java_string_field.cc b/src/google/protobuf/compiler/java/java_string_field.cc
index 8889a744..1c9302af 100644
--- a/src/google/protobuf/compiler/java/java_string_field.cc
+++ b/src/google/protobuf/compiler/java/java_string_field.cc
@@ -407,6 +407,15 @@ GenerateParsingCode(io::Printer* printer) const {
"String s = input.readStringRequireUtf8();\n"
"$set_has_field_bit_message$\n"
"$name$_ = s;\n");
+ } else if (!HasDescriptorMethods(descriptor_->file())) {
+ // 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_,
+ "String s = input.readString();\n"
+ "$set_has_field_bit_message$\n"
+ "$name$_ = s;\n");
} else {
printer->Print(variables_,
"com.google.protobuf.ByteString bs = input.readBytes();\n"
@@ -659,6 +668,15 @@ GenerateParsingCode(io::Printer* printer) const {
"String s = input.readStringRequireUtf8();\n"
"$set_oneof_case_message$;\n"
"$oneof_name$_ = s;\n}\n");
+ } else if (!HasDescriptorMethods(descriptor_->file())) {
+ // 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_,
+ "String s = input.readString();\n"
+ "$set_oneof_case_message$;\n"
+ "$oneof_name$_ = s;\n");
} else {
printer->Print(variables_,
"com.google.protobuf.ByteString bs = input.readBytes();\n"
@@ -923,6 +941,13 @@ GenerateParsingCode(io::Printer* printer) const {
if (CheckUtf8(descriptor_)) {
printer->Print(variables_,
"String s = input.readStringRequireUtf8();\n");
+ } else if (!HasDescriptorMethods(descriptor_->file())) {
+ // 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_,
+ "String s = input.readString();\n");
} else {
printer->Print(variables_,
"com.google.protobuf.ByteString bs = input.readBytes();\n");
@@ -932,7 +957,7 @@ GenerateParsingCode(io::Printer* printer) const {
" $name$_ = new com.google.protobuf.LazyStringArrayList();\n"
" $set_mutable_bit_parser$;\n"
"}\n");
- if (CheckUtf8(descriptor_)) {
+ if (CheckUtf8(descriptor_) || !HasDescriptorMethods(descriptor_->file())) {
printer->Print(variables_,
"$name$_.add(s);\n");
} else {
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum.cc b/src/google/protobuf/compiler/javanano/javanano_enum.cc
new file mode 100644
index 00000000..f934b05f
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_enum.cc
@@ -0,0 +1,111 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <map>
+#include <string>
+
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/compiler/javanano/javanano_enum.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, const Params& params)
+ : params_(params), descriptor_(descriptor) {
+ for (int i = 0; i < descriptor_->value_count(); i++) {
+ const EnumValueDescriptor* value = descriptor_->value(i);
+ const EnumValueDescriptor* canonical_value =
+ descriptor_->FindValueByNumber(value->number());
+
+ if (value == canonical_value) {
+ canonical_values_.push_back(value);
+ } else {
+ Alias alias;
+ alias.value = value;
+ alias.canonical_value = canonical_value;
+ aliases_.push_back(alias);
+ }
+ }
+}
+
+EnumGenerator::~EnumGenerator() {}
+
+void EnumGenerator::Generate(io::Printer* printer) {
+ printer->Print(
+ "\n"
+ "// enum $classname$\n",
+ "classname", descriptor_->name());
+
+ // Start of container interface
+ bool use_shell_class = params_.java_enum_style();
+ if (use_shell_class) {
+ printer->Print(
+ "public interface $classname$ {\n",
+ "classname", RenameJavaKeywords(descriptor_->name()));
+ printer->Indent();
+ }
+
+ // Canonical values
+ for (int i = 0; i < canonical_values_.size(); i++) {
+ printer->Print(
+ "public static final int $name$ = $canonical_value$;\n",
+ "name", RenameJavaKeywords(canonical_values_[i]->name()),
+ "canonical_value", SimpleItoa(canonical_values_[i]->number()));
+ }
+
+ // Aliases
+ for (int i = 0; i < aliases_.size(); i++) {
+ printer->Print(
+ "public static final int $name$ = $canonical_name$;\n",
+ "name", RenameJavaKeywords(aliases_[i].value->name()),
+ "canonical_name", RenameJavaKeywords(aliases_[i].canonical_value->name()));
+ }
+
+ // End of container interface
+ if (use_shell_class) {
+ printer->Outdent();
+ printer->Print("}\n");
+ }
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum.h b/src/google/protobuf/compiler/javanano/javanano_enum.h
new file mode 100644
index 00000000..10dd3648
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_enum.h
@@ -0,0 +1,87 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_H__
+
+#include <string>
+#include <vector>
+
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/descriptor.h>
+
+namespace google {
+namespace protobuf {
+ namespace io {
+ class Printer; // printer.h
+ }
+}
+
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class EnumGenerator {
+ public:
+ explicit EnumGenerator(const EnumDescriptor* descriptor, const Params& params);
+ ~EnumGenerator();
+
+ void Generate(io::Printer* printer);
+
+ private:
+ const Params& params_;
+ const EnumDescriptor* descriptor_;
+
+ // The proto language allows multiple enum constants to have the same numeric
+ // value. Java, however, does not allow multiple enum constants to be
+ // considered equivalent. We treat the first defined constant for any
+ // given numeric value as "canonical" and the rest as aliases of that
+ // canonical value.
+ vector<const EnumValueDescriptor*> canonical_values_;
+
+ struct Alias {
+ const EnumValueDescriptor* value;
+ const EnumValueDescriptor* canonical_value;
+ };
+ vector<Alias> aliases_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
new file mode 100644
index 00000000..8a59d323
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
@@ -0,0 +1,520 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <map>
+#include <string>
+
+#include <google/protobuf/compiler/javanano/javanano_enum_field.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/wire_format.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+namespace {
+
+// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
+// repeat code between this and the other field types.
+void SetEnumVariables(const Params& params,
+ const FieldDescriptor* descriptor, map<string, string>* variables) {
+ (*variables)["name"] =
+ RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
+ (*variables)["capitalized_name"] =
+ RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
+ (*variables)["number"] = SimpleItoa(descriptor->number());
+ if (params.use_reference_types_for_primitives()
+ && !params.reftypes_primitive_enums()
+ && !descriptor->is_repeated()) {
+ (*variables)["type"] = "java.lang.Integer";
+ (*variables)["default"] = "null";
+ } else {
+ (*variables)["type"] = "int";
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ }
+ (*variables)["repeated_default"] =
+ "com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY";
+ (*variables)["tag"] = SimpleItoa(internal::WireFormat::MakeTag(descriptor));
+ (*variables)["tag_size"] = SimpleItoa(
+ internal::WireFormat::TagSize(descriptor->number(), descriptor->type()));
+ (*variables)["non_packed_tag"] = SimpleItoa(
+ internal::WireFormatLite::MakeTag(descriptor->number(),
+ internal::WireFormat::WireTypeForFieldType(descriptor->type())));
+ (*variables)["message_name"] = descriptor->containing_type()->name();
+}
+
+void LoadEnumValues(const Params& params,
+ const EnumDescriptor* enum_descriptor, vector<string>* canonical_values) {
+ string enum_class_name = ClassName(params, enum_descriptor);
+ for (int i = 0; i < enum_descriptor->value_count(); i++) {
+ const EnumValueDescriptor* value = enum_descriptor->value(i);
+ const EnumValueDescriptor* canonical_value =
+ enum_descriptor->FindValueByNumber(value->number());
+ if (value == canonical_value) {
+ canonical_values->push_back(
+ enum_class_name + "." + RenameJavaKeywords(value->name()));
+ }
+ }
+}
+
+void PrintCaseLabels(
+ io::Printer* printer, const vector<string>& canonical_values) {
+ for (int i = 0; i < canonical_values.size(); i++) {
+ printer->Print(
+ " case $value$:\n",
+ "value", canonical_values[i]);
+ }
+}
+
+} // namespace
+
+// ===================================================================
+
+EnumFieldGenerator::
+EnumFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetEnumVariables(params, descriptor, &variables_);
+ LoadEnumValues(params, descriptor->enum_type(), &canonical_values_);
+}
+
+EnumFieldGenerator::~EnumFieldGenerator() {}
+
+void EnumFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
+ printer->Print(variables_,
+ "public $type$ $name$;\n");
+
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "public boolean has$capitalized_name$;\n");
+ }
+}
+
+void EnumFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = $default$;\n");
+
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "has$capitalized_name$ = false;\n");
+ }
+}
+
+void EnumFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "int value = input.readInt32();\n"
+ "switch (value) {\n");
+ PrintCaseLabels(printer, canonical_values_);
+ printer->Print(variables_,
+ " this.$name$ = value;\n");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ " has$capitalized_name$ = true;\n");
+ }
+ printer->Print(
+ " break;\n"
+ "}\n");
+ // No default case: in case of invalid value from the wire, preserve old
+ // field value. Also we are not storing the invalid value into the unknown
+ // fields, because there is no way to get the value out.
+}
+
+void EnumFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ if (descriptor_->is_required() && !params_.generate_has()) {
+ // Always serialize a required field if we don't have the 'has' signal.
+ printer->Print(variables_,
+ "output.writeInt32($number$, this.$name$);\n");
+ } else {
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "if (this.$name$ != $default$ || has$capitalized_name$) {\n");
+ } else {
+ printer->Print(variables_,
+ "if (this.$name$ != $default$) {\n");
+ }
+ printer->Print(variables_,
+ " output.writeInt32($number$, this.$name$);\n"
+ "}\n");
+ }
+}
+
+void EnumFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ if (descriptor_->is_required() && !params_.generate_has()) {
+ printer->Print(variables_,
+ "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeInt32Size($number$, this.$name$);\n");
+ } else {
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "if (this.$name$ != $default$ || has$capitalized_name$) {\n");
+ } else {
+ printer->Print(variables_,
+ "if (this.$name$ != $default$) {\n");
+ }
+ printer->Print(variables_,
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeInt32Size($number$, this.$name$);\n"
+ "}\n");
+ }
+}
+
+void EnumFieldGenerator::GenerateEqualsCode(io::Printer* printer) const {
+ if (params_.use_reference_types_for_primitives()
+ && !params_.reftypes_primitive_enums()) {
+ printer->Print(variables_,
+ "if (this.$name$ == null) {\n"
+ " if (other.$name$ != null) {\n"
+ " return false;\n"
+ " }\n"
+ "} else if (!this.$name$.equals(other.$name$)) {\n"
+ " return false;"
+ "}\n");
+ } else {
+ // We define equality as serialized form equality. If generate_has(),
+ // then if the field value equals the default value in both messages,
+ // but one's 'has' field is set and the other's is not, the serialized
+ // forms are different and we should return false.
+ printer->Print(variables_,
+ "if (this.$name$ != other.$name$");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (this.$name$ == $default$\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ "}\n");
+ }
+}
+
+void EnumFieldGenerator::GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(
+ "result = 31 * result + ");
+ if (params_.use_reference_types_for_primitives()
+ && !params_.reftypes_primitive_enums()) {
+ printer->Print(variables_,
+ "(this.$name$ == null ? 0 : this.$name$)");
+ } else {
+ printer->Print(variables_,
+ "this.$name$");
+ }
+ printer->Print(";\n");
+}
+
+// ===================================================================
+
+AccessorEnumFieldGenerator::
+AccessorEnumFieldGenerator(const FieldDescriptor* descriptor,
+ const Params& params, int has_bit_index)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetEnumVariables(params, descriptor, &variables_);
+ LoadEnumValues(params, descriptor->enum_type(), &canonical_values_);
+ SetBitOperationVariables("has", has_bit_index, &variables_);
+}
+
+AccessorEnumFieldGenerator::~AccessorEnumFieldGenerator() {}
+
+void AccessorEnumFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
+ printer->Print(variables_,
+ "private int $name$_;\n"
+ "public int get$capitalized_name$() {\n"
+ " return $name$_;\n"
+ "}\n"
+ "public $message_name$ set$capitalized_name$(int value) {\n"
+ " $name$_ = value;\n"
+ " $set_has$;\n"
+ " return this;\n"
+ "}\n"
+ "public boolean has$capitalized_name$() {\n"
+ " return $get_has$;\n"
+ "}\n"
+ "public $message_name$ clear$capitalized_name$() {\n"
+ " $name$_ = $default$;\n"
+ " $clear_has$;\n"
+ " return this;\n"
+ "}\n");
+}
+
+void AccessorEnumFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$_ = $default$;\n");
+}
+
+void AccessorEnumFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "int value = input.readInt32();\n"
+ "switch (value) {\n");
+ PrintCaseLabels(printer, canonical_values_);
+ printer->Print(variables_,
+ " $name$_ = value;\n"
+ " $set_has$;\n"
+ " break;\n"
+ "}\n");
+ // No default case: in case of invalid value from the wire, preserve old
+ // field value. Also we are not storing the invalid value into the unknown
+ // fields, because there is no way to get the value out.
+}
+
+void AccessorEnumFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if ($get_has$) {\n"
+ " output.writeInt32($number$, $name$_);\n"
+ "}\n");
+}
+
+void AccessorEnumFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if ($get_has$) {\n"
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeInt32Size($number$, $name$_);\n"
+ "}\n");
+}
+
+void AccessorEnumFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || $name$_ != other.$name$_) {\n"
+ " return false;\n"
+ "}\n");
+}
+
+void AccessorEnumFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "result = 31 * result + $name$_;\n");
+}
+
+// ===================================================================
+
+RepeatedEnumFieldGenerator::
+RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetEnumVariables(params, descriptor, &variables_);
+ LoadEnumValues(params, descriptor->enum_type(), &canonical_values_);
+}
+
+RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
+
+void RepeatedEnumFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
+ printer->Print(variables_,
+ "public $type$[] $name$;\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = $repeated_default$;\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ // First, figure out the maximum length of the array, then parse,
+ // and finally copy the valid values to the field.
+ printer->Print(variables_,
+ "int length = com.google.protobuf.nano.WireFormatNano\n"
+ " .getRepeatedFieldArrayLength(input, $non_packed_tag$);\n"
+ "int[] validValues = new int[length];\n"
+ "int validCount = 0;\n"
+ "for (int i = 0; i < length; i++) {\n"
+ " if (i != 0) { // tag for first value already consumed.\n"
+ " input.readTag();\n"
+ " }\n"
+ " int value = input.readInt32();\n"
+ " switch (value) {\n");
+ printer->Indent();
+ PrintCaseLabels(printer, canonical_values_);
+ printer->Outdent();
+ printer->Print(variables_,
+ " validValues[validCount++] = value;\n"
+ " break;\n"
+ " }\n"
+ "}\n"
+ "if (validCount != 0) {\n"
+ " int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ " if (i == 0 && validCount == validValues.length) {\n"
+ " this.$name$ = validValues;\n"
+ " } else {\n"
+ " int[] newArray = new int[i + validCount];\n"
+ " if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ " }\n"
+ " java.lang.System.arraycopy(validValues, 0, newArray, i, validCount);\n"
+ " this.$name$ = newArray;\n"
+ " }\n"
+ "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateMergingCodeFromPacked(io::Printer* printer) const {
+ printer->Print(variables_,
+ "int bytes = input.readRawVarint32();\n"
+ "int limit = input.pushLimit(bytes);\n"
+ "// First pass to compute array length.\n"
+ "int arrayLength = 0;\n"
+ "int startPos = input.getPosition();\n"
+ "while (input.getBytesUntilLimit() > 0) {\n"
+ " switch (input.readInt32()) {\n");
+ printer->Indent();
+ PrintCaseLabels(printer, canonical_values_);
+ printer->Outdent();
+ printer->Print(variables_,
+ " arrayLength++;\n"
+ " break;\n"
+ " }\n"
+ "}\n"
+ "if (arrayLength != 0) {\n"
+ " input.rewindToPosition(startPos);\n"
+ " int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ " int[] newArray = new int[i + arrayLength];\n"
+ " if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ " }\n"
+ " while (input.getBytesUntilLimit() > 0) {\n"
+ " int value = input.readInt32();\n"
+ " switch (value) {\n");
+ printer->Indent();
+ printer->Indent();
+ PrintCaseLabels(printer, canonical_values_);
+ printer->Outdent();
+ printer->Outdent();
+ printer->Print(variables_,
+ " newArray[i++] = value;\n"
+ " break;\n"
+ " }\n"
+ " }\n"
+ " this.$name$ = newArray;\n"
+ "}\n"
+ "input.popLimit(limit);\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateRepeatedDataSizeCode(io::Printer* printer) const {
+ // Creates a variable dataSize and puts the serialized size in there.
+ printer->Print(variables_,
+ "int dataSize = 0;\n"
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " int element = this.$name$[i];\n"
+ " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeInt32SizeNoTag(element);\n"
+ "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n");
+ printer->Indent();
+
+ if (descriptor_->options().packed()) {
+ GenerateRepeatedDataSizeCode(printer);
+ printer->Print(variables_,
+ "output.writeRawVarint32($tag$);\n"
+ "output.writeRawVarint32(dataSize);\n"
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " output.writeRawVarint32(this.$name$[i]);\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " output.writeInt32($number$, this.$name$[i]);\n"
+ "}\n");
+ }
+
+ printer->Outdent();
+ printer->Print(variables_,
+ "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n");
+ printer->Indent();
+
+ GenerateRepeatedDataSizeCode(printer);
+
+ printer->Print(
+ "size += dataSize;\n");
+ if (descriptor_->options().packed()) {
+ printer->Print(variables_,
+ "size += $tag_size$;\n"
+ "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeRawVarint32Size(dataSize);\n");
+ } else {
+ printer->Print(variables_,
+ "size += $tag_size$ * this.$name$.length;\n");
+ }
+
+ printer->Outdent();
+
+ printer->Print(
+ "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (!com.google.protobuf.nano.InternalNano.equals(\n"
+ " this.$name$, other.$name$)) {\n"
+ " return false;\n"
+ "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.h b/src/google/protobuf/compiler/javanano/javanano_enum_field.h
new file mode 100644
index 00000000..00adc61f
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.h
@@ -0,0 +1,125 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_FIELD_H__
+
+#include <map>
+#include <string>
+#include <vector>
+#include <google/protobuf/compiler/javanano/javanano_field.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class EnumFieldGenerator : public FieldGenerator {
+ public:
+ explicit EnumFieldGenerator(
+ const FieldDescriptor* descriptor, const Params& params);
+ ~EnumFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+ vector<string> canonical_values_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator);
+};
+
+class AccessorEnumFieldGenerator : public FieldGenerator {
+ public:
+ explicit AccessorEnumFieldGenerator(const FieldDescriptor* descriptor,
+ const Params& params, int has_bit_index);
+ ~AccessorEnumFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+ vector<string> canonical_values_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AccessorEnumFieldGenerator);
+};
+
+class RepeatedEnumFieldGenerator : public FieldGenerator {
+ public:
+ explicit RepeatedEnumFieldGenerator(
+ const FieldDescriptor* descriptor, const Params& params);
+ ~RepeatedEnumFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateMergingCodeFromPacked(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ void GenerateRepeatedDataSizeCode(io::Printer* printer) const;
+
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+ vector<string> canonical_values_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_ENUM_FIELD_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_extension.cc b/src/google/protobuf/compiler/javanano/javanano_extension.cc
new file mode 100644
index 00000000..754ed550
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_extension.cc
@@ -0,0 +1,150 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: bduff@google.com (Brian Duff)
+
+#include <google/protobuf/compiler/javanano/javanano_extension.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/wire_format.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+using internal::WireFormat;
+using internal::WireFormatLite;
+
+namespace {
+
+const char* GetTypeConstantName(const FieldDescriptor::Type type) {
+ switch (type) {
+ case FieldDescriptor::TYPE_INT32 : return "TYPE_INT32" ;
+ case FieldDescriptor::TYPE_UINT32 : return "TYPE_UINT32" ;
+ case FieldDescriptor::TYPE_SINT32 : return "TYPE_SINT32" ;
+ case FieldDescriptor::TYPE_FIXED32 : return "TYPE_FIXED32" ;
+ case FieldDescriptor::TYPE_SFIXED32: return "TYPE_SFIXED32";
+ case FieldDescriptor::TYPE_INT64 : return "TYPE_INT64" ;
+ case FieldDescriptor::TYPE_UINT64 : return "TYPE_UINT64" ;
+ case FieldDescriptor::TYPE_SINT64 : return "TYPE_SINT64" ;
+ case FieldDescriptor::TYPE_FIXED64 : return "TYPE_FIXED64" ;
+ case FieldDescriptor::TYPE_SFIXED64: return "TYPE_SFIXED64";
+ case FieldDescriptor::TYPE_FLOAT : return "TYPE_FLOAT" ;
+ case FieldDescriptor::TYPE_DOUBLE : return "TYPE_DOUBLE" ;
+ case FieldDescriptor::TYPE_BOOL : return "TYPE_BOOL" ;
+ case FieldDescriptor::TYPE_STRING : return "TYPE_STRING" ;
+ case FieldDescriptor::TYPE_BYTES : return "TYPE_BYTES" ;
+ case FieldDescriptor::TYPE_ENUM : return "TYPE_ENUM" ;
+ case FieldDescriptor::TYPE_GROUP : return "TYPE_GROUP" ;
+ case FieldDescriptor::TYPE_MESSAGE : return "TYPE_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;
+}
+
+} // namespace
+
+void SetVariables(const FieldDescriptor* descriptor, const Params params,
+ map<string, string>* variables) {
+ (*variables)["extends"] = ClassName(params, descriptor->containing_type());
+ (*variables)["name"] = RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
+ bool repeated = descriptor->is_repeated();
+ (*variables)["repeated"] = repeated ? "Repeated" : "";
+ (*variables)["type"] = GetTypeConstantName(descriptor->type());
+ JavaType java_type = GetJavaType(descriptor->type());
+ string tag = SimpleItoa(WireFormat::MakeTag(descriptor));
+ if (java_type == JAVATYPE_MESSAGE) {
+ (*variables)["ext_type"] = "MessageTyped";
+ string message_type = ClassName(params, descriptor->message_type());
+ if (repeated) {
+ message_type += "[]";
+ }
+ (*variables)["class"] = message_type;
+ // For message typed extensions, tags_params contains a single tag
+ // for both singular and repeated cases.
+ (*variables)["tag_params"] = tag;
+ } else {
+ (*variables)["ext_type"] = "PrimitiveTyped";
+ if (!repeated) {
+ (*variables)["class"] = BoxedPrimitiveTypeName(java_type);
+ (*variables)["tag_params"] = tag;
+ } else {
+ (*variables)["class"] = PrimitiveTypeName(java_type) + "[]";
+ if (!descriptor->is_packable()) {
+ // Non-packable: nonPackedTag == tag, packedTag == 0
+ (*variables)["tag_params"] = tag + ", " + tag + ", 0";
+ } else if (descriptor->options().packed()) {
+ // Packable and packed: tag == packedTag
+ string non_packed_tag = SimpleItoa(WireFormatLite::MakeTag(
+ descriptor->number(),
+ WireFormat::WireTypeForFieldType(descriptor->type())));
+ (*variables)["tag_params"] = tag + ", " + non_packed_tag + ", " + tag;
+ } else {
+ // Packable and not packed: tag == nonPackedTag
+ string packed_tag = SimpleItoa(WireFormatLite::MakeTag(
+ descriptor->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
+ (*variables)["tag_params"] = tag + ", " + tag + ", " + packed_tag;
+ }
+ }
+ }
+}
+
+ExtensionGenerator::
+ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : params_(params), descriptor_(descriptor) {
+ SetVariables(descriptor, params, &variables_);
+}
+
+ExtensionGenerator::~ExtensionGenerator() {}
+
+void ExtensionGenerator::Generate(io::Printer* printer) const {
+ printer->Print("\n");
+ PrintFieldComment(printer, descriptor_);
+ printer->Print(variables_,
+ "public static final com.google.protobuf.nano.Extension<\n"
+ " $extends$,\n"
+ " $class$> $name$ =\n"
+ " com.google.protobuf.nano.Extension.create$repeated$$ext_type$(\n"
+ " com.google.protobuf.nano.Extension.$type$,\n"
+ " $class$.class,\n"
+ " $tag_params$);\n");
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
+
diff --git a/src/google/protobuf/compiler/javanano/javanano_extension.h b/src/google/protobuf/compiler/javanano/javanano_extension.h
new file mode 100644
index 00000000..4843e296
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_extension.h
@@ -0,0 +1,74 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: bduff@google.com (Brian Duff)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_EXTENSION_H_
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_EXTENSION_H_
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/descriptor.pb.h>
+
+
+namespace google {
+namespace protobuf {
+ namespace io {
+ class Printer; // printer.h
+ }
+}
+
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class ExtensionGenerator {
+ public:
+ explicit ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params);
+ ~ExtensionGenerator();
+
+ void Generate(io::Printer* printer) const;
+
+ private:
+ const Params& params_;
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_EXTENSION_H_
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.cc b/src/google/protobuf/compiler/javanano/javanano_field.cc
new file mode 100644
index 00000000..e3e4cefe
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_field.cc
@@ -0,0 +1,143 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <google/protobuf/compiler/javanano/javanano_field.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/compiler/javanano/javanano_primitive_field.h>
+#include <google/protobuf/compiler/javanano/javanano_enum_field.h>
+#include <google/protobuf/compiler/javanano/javanano_message_field.h>
+#include <google/protobuf/stubs/common.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+FieldGenerator::~FieldGenerator() {}
+
+bool FieldGenerator::SavedDefaultNeeded() const {
+ // No saved default for this field by default.
+ // Subclasses whose instances may need saved defaults will override this
+ // and return the appropriate value.
+ return false;
+}
+
+void FieldGenerator::GenerateInitSavedDefaultCode(io::Printer* printer) const {
+ // No saved default for this field by default.
+ // Subclasses whose instances may need saved defaults will override this
+ // and generate the appropriate init code to the printer.
+}
+
+void FieldGenerator::GenerateMergingCodeFromPacked(io::Printer* printer) const {
+ // Reaching here indicates a bug. Cases are:
+ // - This FieldGenerator should support packing, but this method should be
+ // overridden.
+ // - This FieldGenerator doesn't support packing, and this method should
+ // never have been called.
+ GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
+ << "called on field generator that does not support packing.";
+}
+
+// =============================================
+
+FieldGeneratorMap::FieldGeneratorMap(
+ const Descriptor* descriptor, const Params &params)
+ : descriptor_(descriptor),
+ field_generators_(
+ new scoped_ptr<FieldGenerator>[descriptor->field_count()]) {
+
+ int next_has_bit_index = 0;
+ bool saved_defaults_needed = false;
+ // Construct all the FieldGenerators.
+ for (int i = 0; i < descriptor->field_count(); i++) {
+ FieldGenerator* field_generator = MakeGenerator(
+ descriptor->field(i), params, &next_has_bit_index);
+ saved_defaults_needed = saved_defaults_needed
+ || field_generator->SavedDefaultNeeded();
+ field_generators_[i].reset(field_generator);
+ }
+ total_bits_ = next_has_bit_index;
+ saved_defaults_needed_ = saved_defaults_needed;
+}
+
+FieldGenerator* FieldGeneratorMap::MakeGenerator(const FieldDescriptor* field,
+ const Params &params, int* next_has_bit_index) {
+ JavaType java_type = GetJavaType(field);
+ if (field->is_repeated()) {
+ switch (java_type) {
+ case JAVATYPE_MESSAGE:
+ return new RepeatedMessageFieldGenerator(field, params);
+ case JAVATYPE_ENUM:
+ return new RepeatedEnumFieldGenerator(field, params);
+ default:
+ return new RepeatedPrimitiveFieldGenerator(field, params);
+ }
+ } else if (params.optional_field_accessors() && field->is_optional()
+ && java_type != JAVATYPE_MESSAGE) {
+ // We need a has-bit for each primitive/enum field because their default
+ // values could be same as explicitly set values. But we don't need it
+ // for a message field because they have no defaults and Nano uses 'null'
+ // for unset messages, which cannot be set explicitly.
+ switch (java_type) {
+ case JAVATYPE_ENUM:
+ return new AccessorEnumFieldGenerator(
+ field, params, (*next_has_bit_index)++);
+ default:
+ return new AccessorPrimitiveFieldGenerator(
+ field, params, (*next_has_bit_index)++);
+ }
+ } else {
+ switch (java_type) {
+ case JAVATYPE_MESSAGE:
+ return new MessageFieldGenerator(field, params);
+ case JAVATYPE_ENUM:
+ return new EnumFieldGenerator(field, params);
+ default:
+ return new PrimitiveFieldGenerator(field, params);
+ }
+ }
+}
+
+FieldGeneratorMap::~FieldGeneratorMap() {}
+
+const FieldGenerator& FieldGeneratorMap::get(
+ const FieldDescriptor* field) const {
+ GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
+ return *field_generators_[field->index()];
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.h b/src/google/protobuf/compiler/javanano/javanano_field.h
new file mode 100644
index 00000000..6170c2c0
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_field.h
@@ -0,0 +1,119 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_FIELD_H__
+
+#include <string>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+
+namespace google {
+namespace protobuf {
+ namespace io {
+ class Printer; // printer.h
+ }
+}
+
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class FieldGenerator {
+ public:
+ FieldGenerator(const Params& params) : params_(params) {}
+ virtual ~FieldGenerator();
+
+ virtual bool SavedDefaultNeeded() const;
+ virtual void GenerateInitSavedDefaultCode(io::Printer* printer) const;
+
+ // Generates code for Java fields and methods supporting this field.
+ // If this field needs a saved default (SavedDefaultNeeded() is true),
+ // then @lazy_init controls how the static field for that default value
+ // and its initialization code should be generated. If @lazy_init is
+ // true, the static field is not declared final and the initialization
+ // code is generated only when GenerateInitSavedDefaultCode is called;
+ // otherwise, the static field is declared final and initialized inline.
+ // GenerateInitSavedDefaultCode will not be called in the latter case.
+ virtual void GenerateMembers(
+ io::Printer* printer, bool lazy_init) const = 0;
+
+ virtual void GenerateClearCode(io::Printer* printer) const = 0;
+ virtual void GenerateMergingCode(io::Printer* printer) const = 0;
+
+ // Generates code to merge from packed serialized form. The default
+ // implementation will fail; subclasses which can handle packed serialized
+ // forms will override this and print appropriate code to the printer.
+ virtual void GenerateMergingCodeFromPacked(io::Printer* printer) const;
+
+ virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
+ virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
+ virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
+ virtual void GenerateHashCodeCode(io::Printer* printer) const = 0;
+
+ protected:
+ const Params& params_;
+ private:
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
+};
+
+// Convenience class which constructs FieldGenerators for a Descriptor.
+class FieldGeneratorMap {
+ public:
+ explicit FieldGeneratorMap(const Descriptor* descriptor, const Params &params);
+ ~FieldGeneratorMap();
+
+ const FieldGenerator& get(const FieldDescriptor* field) const;
+ int total_bits() const { return total_bits_; }
+ bool saved_defaults_needed() const { return saved_defaults_needed_; }
+
+ private:
+ const Descriptor* descriptor_;
+ scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
+ int total_bits_;
+ bool saved_defaults_needed_;
+
+ static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
+ const Params &params, int* next_has_bit_index);
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_FIELD_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_file.cc b/src/google/protobuf/compiler/javanano/javanano_file.cc
new file mode 100644
index 00000000..3676ab9d
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_file.cc
@@ -0,0 +1,263 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <iostream>
+
+#include <google/protobuf/compiler/javanano/javanano_file.h>
+#include <google/protobuf/compiler/javanano/javanano_enum.h>
+#include <google/protobuf/compiler/javanano/javanano_extension.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/compiler/javanano/javanano_message.h>
+#include <google/protobuf/compiler/code_generator.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+namespace {
+
+// Recursively searches the given message to see if it contains any extensions.
+bool UsesExtensions(const Message& message) {
+ const Reflection* reflection = message.GetReflection();
+
+ // We conservatively assume that unknown fields are extensions.
+ if (reflection->GetUnknownFields(message).field_count() > 0) return true;
+
+ vector<const FieldDescriptor*> fields;
+ reflection->ListFields(message, &fields);
+
+ for (int i = 0; i < fields.size(); i++) {
+ if (fields[i]->is_extension()) return true;
+
+ if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ if (fields[i]->is_repeated()) {
+ int size = reflection->FieldSize(message, fields[i]);
+ for (int j = 0; j < size; j++) {
+ const Message& sub_message =
+ reflection->GetRepeatedMessage(message, fields[i], j);
+ if (UsesExtensions(sub_message)) return true;
+ }
+ } else {
+ const Message& sub_message = reflection->GetMessage(message, fields[i]);
+ if (UsesExtensions(sub_message)) return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+} // namespace
+
+FileGenerator::FileGenerator(const FileDescriptor* file, const Params& params)
+ : file_(file),
+ params_(params),
+ java_package_(FileJavaPackage(params, file)),
+ classname_(FileClassName(params, file)) {}
+
+FileGenerator::~FileGenerator() {}
+
+bool FileGenerator::Validate(string* error) {
+ // Check for extensions
+ FileDescriptorProto file_proto;
+ file_->CopyTo(&file_proto);
+ if (UsesExtensions(file_proto) && !params_.store_unknown_fields()) {
+ error->assign(file_->name());
+ error->append(
+ ": Java NANO_RUNTIME only supports extensions when the "
+ "'store_unknown_fields' generator option is 'true'.");
+ return false;
+ }
+
+ if (file_->service_count() != 0 && !params_.ignore_services()) {
+ error->assign(file_->name());
+ error->append(
+ ": Java NANO_RUNTIME does not support services\"");
+ return false;
+ }
+
+ if (!IsOuterClassNeeded(params_, file_)) {
+ return true;
+ }
+
+ // Check whether legacy javanano generator would omit the outer class.
+ if (!params_.has_java_outer_classname(file_->name())
+ && file_->message_type_count() == 1
+ && file_->enum_type_count() == 0 && file_->extension_count() == 0) {
+ cout << "INFO: " << file_->name() << ":" << endl;
+ cout << "Javanano generator has changed to align with java generator. "
+ "An outer class will be created for this file and the single message "
+ "in the file will become a nested class. Use java_multiple_files to "
+ "skip generating the outer class, or set an explicit "
+ "java_outer_classname to suppress this message." << endl;
+ }
+
+ // 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.
+ bool found_conflict = false;
+ for (int i = 0; !found_conflict && i < file_->message_type_count(); i++) {
+ if (file_->message_type(i)->name() == classname_) {
+ found_conflict = true;
+ }
+ }
+ if (params_.java_enum_style()) {
+ for (int i = 0; !found_conflict && i < file_->enum_type_count(); i++) {
+ if (file_->enum_type(i)->name() == classname_) {
+ found_conflict = true;
+ }
+ }
+ }
+ if (found_conflict) {
+ error->assign(file_->name());
+ error->append(
+ ": 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.");
+ return false;
+ }
+ return true;
+}
+
+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");
+ if (!java_package_.empty()) {
+ printer->Print(
+ "\n"
+ "package $package$;\n",
+ "package", java_package_);
+ }
+
+ // Note: constants (from enums, emitted in the loop below) may have the same names as constants
+ // in the nested classes. This causes Java warnings, but is not fatal, so we suppress those
+ // warnings here in the top-most class declaration.
+ printer->Print(
+ "\n"
+ "@SuppressWarnings(\"hiding\")\n"
+ "public interface $classname$ {\n",
+ "classname", classname_);
+ printer->Indent();
+
+ // -----------------------------------------------------------------
+
+ // Extensions.
+ for (int i = 0; i < file_->extension_count(); i++) {
+ ExtensionGenerator(file_->extension(i), params_).Generate(printer);
+ }
+
+ // Enums.
+ for (int i = 0; i < file_->enum_type_count(); i++) {
+ EnumGenerator(file_->enum_type(i), params_).Generate(printer);
+ }
+
+ // Messages.
+ if (!params_.java_multiple_files(file_->name())) {
+ for (int i = 0; i < file_->message_type_count(); i++) {
+ MessageGenerator(file_->message_type(i), params_).Generate(printer);
+ }
+ }
+
+ // Static variables.
+ for (int i = 0; i < file_->message_type_count(); i++) {
+ // TODO(kenton): Reuse MessageGenerator objects?
+ MessageGenerator(file_->message_type(i), params_).GenerateStaticVariables(printer);
+ }
+
+ printer->Outdent();
+ printer->Print(
+ "}\n");
+}
+
+template<typename GeneratorClass, typename DescriptorClass>
+static void GenerateSibling(const string& package_dir,
+ const string& java_package,
+ const DescriptorClass* descriptor,
+ GeneratorContext* output_directory,
+ vector<string>* file_list,
+ const Params& params) {
+ string filename = package_dir + descriptor->name() + ".java";
+ file_list->push_back(filename);
+
+ scoped_ptr<io::ZeroCopyOutputStream> output(
+ output_directory->Open(filename));
+ io::Printer printer(output.get(), '$');
+
+ printer.Print(
+ "// Generated by the protocol buffer compiler. DO NOT EDIT!\n");
+ if (!java_package.empty()) {
+ printer.Print(
+ "\n"
+ "package $package$;\n",
+ "package", java_package);
+ }
+
+ GeneratorClass(descriptor, params).Generate(&printer);
+}
+
+void FileGenerator::GenerateSiblings(const string& package_dir,
+ GeneratorContext* output_directory,
+ vector<string>* file_list) {
+ if (params_.java_multiple_files(file_->name())) {
+ for (int i = 0; i < file_->message_type_count(); i++) {
+ GenerateSibling<MessageGenerator>(package_dir, java_package_,
+ file_->message_type(i),
+ output_directory, file_list, params_);
+ }
+
+ if (params_.java_enum_style()) {
+ for (int i = 0; i < file_->enum_type_count(); i++) {
+ GenerateSibling<EnumGenerator>(package_dir, java_package_,
+ file_->enum_type(i),
+ output_directory, file_list, params_);
+ }
+ }
+ }
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_file.h b/src/google/protobuf/compiler/javanano/javanano_file.h
new file mode 100644
index 00000000..217eafe2
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_file.h
@@ -0,0 +1,94 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_FILE_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_FILE_H__
+
+#include <string>
+#include <vector>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+
+namespace google {
+namespace protobuf {
+ class FileDescriptor; // descriptor.h
+ namespace io {
+ class Printer; // printer.h
+ }
+ namespace compiler {
+ class GeneratorContext; // code_generator.h
+ }
+}
+
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class FileGenerator {
+ public:
+ explicit FileGenerator(const FileDescriptor* file, const Params& params);
+ ~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);
+
+ 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,
+ GeneratorContext* output_directory,
+ vector<string>* file_list);
+
+ const string& java_package() { return java_package_; }
+ const string& classname() { return classname_; }
+
+ private:
+ const FileDescriptor* file_;
+ const Params& params_;
+ string java_package_;
+ string classname_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_FILE_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_generator.cc b/src/google/protobuf/compiler/javanano/javanano_generator.cc
new file mode 100644
index 00000000..b5fbcd5f
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_generator.cc
@@ -0,0 +1,219 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/compiler/javanano/javanano_generator.h>
+#include <google/protobuf/compiler/javanano/javanano_file.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+namespace {
+
+string TrimString(const string& s) {
+ string::size_type start = s.find_first_not_of(" \n\r\t");
+ if (start == string::npos) {
+ return "";
+ }
+ string::size_type end = s.find_last_not_of(" \n\r\t") + 1;
+ return s.substr(start, end - start);
+}
+
+} // namespace
+
+void UpdateParamsRecursively(Params& params,
+ const FileDescriptor* file) {
+ // Add any parameters for this file
+ if (file->options().has_java_outer_classname()) {
+ params.set_java_outer_classname(
+ file->name(), file->options().java_outer_classname());
+ }
+ if (file->options().has_java_package()) {
+ params.set_java_package(
+ file->name(), file->options().java_package());
+ }
+ if (file->options().has_java_multiple_files()) {
+ params.set_java_multiple_files(
+ file->name(), file->options().java_multiple_files());
+ }
+
+ // Loop through all dependent files recursively
+ // adding dep
+ for (int i = 0; i < file->dependency_count(); i++) {
+ UpdateParamsRecursively(params, file->dependency(i));
+ }
+}
+
+JavaNanoGenerator::JavaNanoGenerator() {}
+JavaNanoGenerator::~JavaNanoGenerator() {}
+
+bool JavaNanoGenerator::Generate(const FileDescriptor* file,
+ const string& parameter,
+ GeneratorContext* output_directory,
+ string* error) const {
+ vector<pair<string, string> > options;
+
+ ParseGeneratorParameter(parameter, &options);
+
+ // -----------------------------------------------------------------
+ // parse generator options
+
+ // Name a file where we will write a list of generated file names, one
+ // per line.
+ string output_list_file;
+ Params params(file->name());
+
+ // Update per file params
+ UpdateParamsRecursively(params, file);
+
+ // Replace any existing options with ones from command line
+ for (int i = 0; i < options.size(); i++) {
+ string option_name = TrimString(options[i].first);
+ string option_value = TrimString(options[i].second);
+ if (option_name == "output_list_file") {
+ output_list_file = option_value;
+ } else if (option_name == "java_package") {
+ vector<string> parts;
+ SplitStringUsing(option_value, "|", &parts);
+ if (parts.size() != 2) {
+ *error = "Bad java_package, expecting filename|PackageName found '"
+ + option_value + "'";
+ return false;
+ }
+ params.set_java_package(parts[0], parts[1]);
+ } else if (option_name == "java_outer_classname") {
+ vector<string> parts;
+ SplitStringUsing(option_value, "|", &parts);
+ if (parts.size() != 2) {
+ *error = "Bad java_outer_classname, "
+ "expecting filename|ClassName found '"
+ + option_value + "'";
+ return false;
+ }
+ params.set_java_outer_classname(parts[0], parts[1]);
+ } else if (option_name == "store_unknown_fields") {
+ params.set_store_unknown_fields(option_value == "true");
+ } else if (option_name == "java_multiple_files") {
+ params.set_override_java_multiple_files(option_value == "true");
+ } else if (option_name == "java_nano_generate_has") {
+ params.set_generate_has(option_value == "true");
+ } else if (option_name == "enum_style") {
+ params.set_java_enum_style(option_value == "java");
+ } else if (option_name == "optional_field_style") {
+ params.set_optional_field_accessors(option_value == "accessors");
+ params.set_use_reference_types_for_primitives(option_value == "reftypes"
+ || option_value == "reftypes_compat_mode");
+ params.set_reftypes_primitive_enums(
+ option_value == "reftypes_compat_mode");
+ if (option_value == "reftypes_compat_mode") {
+ params.set_generate_clear(false);
+ }
+ } else if (option_name == "generate_equals") {
+ params.set_generate_equals(option_value == "true");
+ } else if (option_name == "ignore_services") {
+ params.set_ignore_services(option_value == "true");
+ } else if (option_name == "parcelable_messages") {
+ params.set_parcelable_messages(option_value == "true");
+ } else {
+ *error = "Ignore unknown javanano generator option: " + option_name;
+ }
+ }
+
+ // Check illegal parameter combinations
+ // Note: the enum-like optional_field_style generator param ensures
+ // that we can never have illegal combinations of field styles
+ // (e.g. reftypes and accessors can't be on at the same time).
+ if (params.generate_has()
+ && (params.optional_field_accessors()
+ || params.use_reference_types_for_primitives())) {
+ error->assign("java_nano_generate_has=true cannot be used in conjunction"
+ " with optional_field_style=accessors or optional_field_style=reftypes");
+ return false;
+ }
+
+ // -----------------------------------------------------------------
+
+ FileGenerator file_generator(file, params);
+ if (!file_generator.Validate(error)) {
+ return false;
+ }
+
+ string package_dir =
+ StringReplace(file_generator.java_package(), ".", "/", true);
+ if (!package_dir.empty()) package_dir += "/";
+
+ vector<string> all_files;
+
+ if (IsOuterClassNeeded(params, file)) {
+ string java_filename = package_dir;
+ java_filename += file_generator.classname();
+ java_filename += ".java";
+ all_files.push_back(java_filename);
+
+ // Generate main java file.
+ scoped_ptr<io::ZeroCopyOutputStream> output(
+ output_directory->Open(java_filename));
+ io::Printer printer(output.get(), '$');
+ file_generator.Generate(&printer);
+ }
+
+ // Generate sibling files.
+ file_generator.GenerateSiblings(package_dir, output_directory, &all_files);
+
+ // Generate output list if requested.
+ if (!output_list_file.empty()) {
+ // Generate output list. This is just a simple text file placed in a
+ // deterministic location which lists the .java files being generated.
+ scoped_ptr<io::ZeroCopyOutputStream> srclist_raw_output(
+ output_directory->Open(output_list_file));
+ io::Printer srclist_printer(srclist_raw_output.get(), '$');
+ for (int i = 0; i < all_files.size(); i++) {
+ srclist_printer.Print("$filename$\n", "filename", all_files[i]);
+ }
+ }
+
+ return true;
+}
+
+} // namespace java
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_generator.h b/src/google/protobuf/compiler/javanano/javanano_generator.h
new file mode 100644
index 00000000..6f9f7f2a
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_generator.h
@@ -0,0 +1,72 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+//
+// Generates Java nano code for a given .proto file.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_NANO_GENERATOR_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_NANO_GENERATOR_H__
+
+#include <string>
+#include <google/protobuf/compiler/code_generator.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+// CodeGenerator implementation which generates Java nano code. If you create your
+// own protocol compiler binary and you want it to support Java output for the
+// nano runtime, you can do so by registering an instance of this CodeGenerator with
+// the CommandLineInterface in your main() function.
+class LIBPROTOC_EXPORT JavaNanoGenerator : public CodeGenerator {
+ public:
+ JavaNanoGenerator();
+ ~JavaNanoGenerator();
+
+ // implements CodeGenerator ----------------------------------------
+ bool Generate(const FileDescriptor* file,
+ const string& parameter,
+ GeneratorContext* output_directory,
+ string* error) const;
+
+ private:
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(JavaNanoGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_NANO_GENERATOR_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_helpers.cc b/src/google/protobuf/compiler/javanano/javanano_helpers.cc
new file mode 100644
index 00000000..2149418a
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_helpers.cc
@@ -0,0 +1,566 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <limits>
+#include <vector>
+
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/stubs/hash.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/stubs/substitute.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+const char kThickSeparator[] =
+ "// ===================================================================\n";
+const char kThinSeparator[] =
+ "// -------------------------------------------------------------------\n";
+
+class RenameKeywords {
+ private:
+ hash_set<string> java_keywords_set_;
+
+ public:
+ RenameKeywords() {
+ static const char* kJavaKeywordsList[] = {
+ // Reserved Java Keywords
+ "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",
+
+ // Reserved Keywords for Literals
+ "false", "null", "true"
+ };
+
+ for (int i = 0; i < GOOGLE_ARRAYSIZE(kJavaKeywordsList); i++) {
+ java_keywords_set_.insert(kJavaKeywordsList[i]);
+ }
+ }
+
+ // Used to rename the a field name if it's a java keyword. Specifically
+ // this is used to rename the ["name"] or ["capitalized_name"] field params.
+ // (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html)
+ string RenameJavaKeywordsImpl(const string& input) {
+ string result = input;
+
+ if (java_keywords_set_.find(result) != java_keywords_set_.end()) {
+ result += "_";
+ }
+
+ return result;
+ }
+
+};
+
+static RenameKeywords sRenameKeywords;
+
+namespace {
+
+const char* kDefaultPackage = "";
+
+const string& FieldName(const FieldDescriptor* field) {
+ // 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.
+ if (field->type() == FieldDescriptor::TYPE_GROUP) {
+ return field->message_type()->name();
+ } else {
+ return field->name();
+ }
+}
+
+string UnderscoresToCamelCaseImpl(const string& input, bool cap_next_letter) {
+ 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') {
+ if (cap_next_letter) {
+ result += input[i] + ('A' - 'a');
+ } else {
+ result += input[i];
+ }
+ cap_next_letter = false;
+ } else if ('A' <= input[i] && input[i] <= 'Z') {
+ if (i == 0 && !cap_next_letter) {
+ // Force first letter to lower-case unless explicitly told to
+ // capitalize it.
+ result += input[i] + ('a' - 'A');
+ } else {
+ // Capital letters after the first are left as-is.
+ result += input[i];
+ }
+ cap_next_letter = false;
+ } else if ('0' <= input[i] && input[i] <= '9') {
+ result += input[i];
+ cap_next_letter = true;
+ } else {
+ cap_next_letter = true;
+ }
+ }
+ return result;
+}
+
+} // namespace
+
+string UnderscoresToCamelCase(const FieldDescriptor* field) {
+ return UnderscoresToCamelCaseImpl(FieldName(field), false);
+}
+
+string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field) {
+ return UnderscoresToCamelCaseImpl(FieldName(field), true);
+}
+
+string UnderscoresToCamelCase(const MethodDescriptor* method) {
+ return UnderscoresToCamelCaseImpl(method->name(), false);
+}
+
+string RenameJavaKeywords(const string& input) {
+ return sRenameKeywords.RenameJavaKeywordsImpl(input);
+}
+
+string StripProto(const string& filename) {
+ if (HasSuffixString(filename, ".protodevel")) {
+ return StripSuffixString(filename, ".protodevel");
+ } else {
+ return StripSuffixString(filename, ".proto");
+ }
+}
+
+string FileClassName(const Params& params, const FileDescriptor* file) {
+ if (params.has_java_outer_classname(file->name())) {
+ return params.java_outer_classname(file->name());
+ } else {
+ // Use the filename itself with underscores removed
+ // and a CamelCase style name.
+ string basename;
+ string::size_type last_slash = file->name().find_last_of('/');
+ if (last_slash == string::npos) {
+ basename = file->name();
+ } else {
+ basename = file->name().substr(last_slash + 1);
+ }
+ return UnderscoresToCamelCaseImpl(StripProto(basename), true);
+ }
+}
+
+string FileJavaPackage(const Params& params, const FileDescriptor* file) {
+ if (params.has_java_package(file->name())) {
+ return params.java_package(file->name());
+ } else {
+ string result = kDefaultPackage;
+ if (!file->package().empty()) {
+ if (!result.empty()) result += '.';
+ result += file->package();
+ }
+ return result;
+ }
+}
+
+bool IsOuterClassNeeded(const Params& params, const FileDescriptor* file) {
+ // If java_multiple_files is false, the outer class is always needed.
+ if (!params.java_multiple_files(file->name())) {
+ return true;
+ }
+
+ // File-scope extensions need the outer class as the scope.
+ if (file->extension_count() != 0) {
+ return true;
+ }
+
+ // If container interfaces are not generated, file-scope enums need the
+ // outer class as the scope.
+ if (file->enum_type_count() != 0 && !params.java_enum_style()) {
+ return true;
+ }
+
+ return false;
+}
+
+string ToJavaName(const Params& params, const string& name, bool is_class,
+ const Descriptor* parent, const FileDescriptor* file) {
+ string result;
+ if (parent != NULL) {
+ result.append(ClassName(params, parent));
+ } else if (is_class && params.java_multiple_files(file->name())) {
+ result.append(FileJavaPackage(params, file));
+ } else {
+ result.append(ClassName(params, file));
+ }
+ if (!result.empty()) result.append(1, '.');
+ result.append(RenameJavaKeywords(name));
+ return result;
+}
+
+string ClassName(const Params& params, const FileDescriptor* descriptor) {
+ string result = FileJavaPackage(params, descriptor);
+ if (!result.empty()) result += '.';
+ result += FileClassName(params, descriptor);
+ return result;
+}
+
+string ClassName(const Params& params, const EnumDescriptor* descriptor) {
+ const Descriptor* parent = descriptor->containing_type();
+ // When using Java enum style, an enum's class name contains the enum name.
+ // Use the standard ToJavaName translation.
+ if (params.java_enum_style()) {
+ return ToJavaName(params, descriptor->name(), true, parent,
+ descriptor->file());
+ }
+ // Otherwise the enum members are accessed from the enclosing class.
+ if (parent != NULL) {
+ return ClassName(params, parent);
+ } else {
+ return ClassName(params, descriptor->file());
+ }
+}
+
+string FieldConstantName(const FieldDescriptor *field) {
+ string name = field->name() + "_FIELD_NUMBER";
+ UpperString(&name);
+ return name;
+}
+
+string FieldDefaultConstantName(const FieldDescriptor *field) {
+ return "_" + RenameJavaKeywords(UnderscoresToCamelCase(field)) + "Default";
+}
+
+void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field) {
+ // We don't want to print group bodies so we cut off after the first line
+ // (the second line for extensions).
+ string def = field->DebugString();
+ string::size_type first_line_end = def.find_first_of('\n');
+ printer->Print("// $def$\n",
+ "def", def.substr(0, first_line_end));
+ if (field->is_extension()) {
+ string::size_type second_line_start = first_line_end + 1;
+ string::size_type second_line_length =
+ def.find('\n', second_line_start) - second_line_start;
+ printer->Print("// $def$\n",
+ "def", def.substr(second_line_start, second_line_length));
+ }
+}
+
+JavaType GetJavaType(FieldDescriptor::Type field_type) {
+ switch (field_type) {
+ case FieldDescriptor::TYPE_INT32:
+ case FieldDescriptor::TYPE_UINT32:
+ case FieldDescriptor::TYPE_SINT32:
+ case FieldDescriptor::TYPE_FIXED32:
+ case FieldDescriptor::TYPE_SFIXED32:
+ return JAVATYPE_INT;
+
+ case FieldDescriptor::TYPE_INT64:
+ case FieldDescriptor::TYPE_UINT64:
+ case FieldDescriptor::TYPE_SINT64:
+ case FieldDescriptor::TYPE_FIXED64:
+ case FieldDescriptor::TYPE_SFIXED64:
+ return JAVATYPE_LONG;
+
+ case FieldDescriptor::TYPE_FLOAT:
+ return JAVATYPE_FLOAT;
+
+ case FieldDescriptor::TYPE_DOUBLE:
+ return JAVATYPE_DOUBLE;
+
+ case FieldDescriptor::TYPE_BOOL:
+ return JAVATYPE_BOOLEAN;
+
+ case FieldDescriptor::TYPE_STRING:
+ return JAVATYPE_STRING;
+
+ case FieldDescriptor::TYPE_BYTES:
+ return JAVATYPE_BYTES;
+
+ case FieldDescriptor::TYPE_ENUM:
+ return JAVATYPE_ENUM;
+
+ case FieldDescriptor::TYPE_GROUP:
+ case FieldDescriptor::TYPE_MESSAGE:
+ return JAVATYPE_MESSAGE;
+
+ // No default because we want the compiler to complain if any new
+ // types are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return JAVATYPE_INT;
+}
+
+string 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 "byte[]";
+ case JAVATYPE_ENUM : return "int";
+ case JAVATYPE_MESSAGE: return "";
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return "";
+}
+
+string 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 "byte[]";
+ case JAVATYPE_ENUM : return "java.lang.Integer";
+ case JAVATYPE_MESSAGE: return "";
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return "";
+}
+
+string EmptyArrayName(const Params& params, const FieldDescriptor* field) {
+ switch (GetJavaType(field)) {
+ case JAVATYPE_INT : return "com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY";
+ case JAVATYPE_LONG : return "com.google.protobuf.nano.WireFormatNano.EMPTY_LONG_ARRAY";
+ case JAVATYPE_FLOAT : return "com.google.protobuf.nano.WireFormatNano.EMPTY_FLOAT_ARRAY";
+ case JAVATYPE_DOUBLE : return "com.google.protobuf.nano.WireFormatNano.EMPTY_DOUBLE_ARRAY";
+ case JAVATYPE_BOOLEAN: return "com.google.protobuf.nano.WireFormatNano.EMPTY_BOOLEAN_ARRAY";
+ case JAVATYPE_STRING : return "com.google.protobuf.nano.WireFormatNano.EMPTY_STRING_ARRAY";
+ case JAVATYPE_BYTES : return "com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES_ARRAY";
+ case JAVATYPE_ENUM : return "com.google.protobuf.nano.WireFormatNano.EMPTY_INT_ARRAY";
+ case JAVATYPE_MESSAGE: return ClassName(params, field->message_type()) + ".EMPTY_ARRAY";
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return "";
+}
+
+string DefaultValue(const Params& params, const FieldDescriptor* field) {
+ if (field->label() == FieldDescriptor::LABEL_REPEATED) {
+ return EmptyArrayName(params, field);
+ }
+
+ if (params.use_reference_types_for_primitives()) {
+ if (params.reftypes_primitive_enums()
+ && field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
+ return "Integer.MIN_VALUE";
+ }
+ return "null";
+ }
+
+ // Switch on cpp_type 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());
+ case FieldDescriptor::CPPTYPE_UINT32:
+ // Need to print as a signed int since Java has no unsigned.
+ return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
+ case FieldDescriptor::CPPTYPE_INT64:
+ return SimpleItoa(field->default_value_int64()) + "L";
+ case FieldDescriptor::CPPTYPE_UINT64:
+ return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
+ "L";
+ case FieldDescriptor::CPPTYPE_DOUBLE: {
+ double value = field->default_value_double();
+ if (value == numeric_limits<double>::infinity()) {
+ return "Double.POSITIVE_INFINITY";
+ } else if (value == -numeric_limits<double>::infinity()) {
+ return "Double.NEGATIVE_INFINITY";
+ } else if (value != value) {
+ return "Double.NaN";
+ } else {
+ return SimpleDtoa(value) + "D";
+ }
+ }
+ case FieldDescriptor::CPPTYPE_FLOAT: {
+ float value = field->default_value_float();
+ if (value == numeric_limits<float>::infinity()) {
+ return "Float.POSITIVE_INFINITY";
+ } else if (value == -numeric_limits<float>::infinity()) {
+ return "Float.NEGATIVE_INFINITY";
+ } else if (value != value) {
+ return "Float.NaN";
+ } else {
+ return SimpleFtoa(value) + "F";
+ }
+ }
+ case FieldDescriptor::CPPTYPE_BOOL:
+ return field->default_value_bool() ? "true" : "false";
+ case FieldDescriptor::CPPTYPE_STRING:
+ if (!field->default_value_string().empty()) {
+ // Point it to the static final in the generated code.
+ return FieldDefaultConstantName(field);
+ } else {
+ if (field->type() == FieldDescriptor::TYPE_BYTES) {
+ return "com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES";
+ } else {
+ return "\"\"";
+ }
+ }
+
+ case FieldDescriptor::CPPTYPE_ENUM:
+ return ClassName(params, field->enum_type()) + "." +
+ RenameJavaKeywords(field->default_value_enum()->name());
+
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ return "null";
+
+ // No default because we want the compiler to complain if any new
+ // types are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return "";
+}
+
+
+static const char* kBitMasks[] = {
+ "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 var_name = "bitField";
+ var_name += SimpleItoa(index);
+ var_name += "_";
+ return var_name;
+}
+
+string GetBitFieldNameForBit(int bit_index) {
+ return GetBitFieldName(bit_index / 32);
+}
+
+string GenerateGetBit(int bit_index) {
+ string var_name = GetBitFieldNameForBit(bit_index);
+ int bit_in_var_index = bit_index % 32;
+
+ string mask = kBitMasks[bit_in_var_index];
+ string result = "((" + var_name + " & " + mask + ") != 0)";
+ return result;
+}
+
+string GenerateSetBit(int bit_index) {
+ string var_name = GetBitFieldNameForBit(bit_index);
+ int bit_in_var_index = bit_index % 32;
+
+ string mask = kBitMasks[bit_in_var_index];
+ string result = var_name + " |= " + mask;
+ return result;
+}
+
+string GenerateClearBit(int bit_index) {
+ string var_name = GetBitFieldNameForBit(bit_index);
+ int bit_in_var_index = bit_index % 32;
+
+ string mask = kBitMasks[bit_in_var_index];
+ string result = var_name + " = (" + var_name + " & ~" + mask + ")";
+ return result;
+}
+
+string GenerateDifferentBit(int bit_index) {
+ string var_name = GetBitFieldNameForBit(bit_index);
+ int bit_in_var_index = bit_index % 32;
+
+ string mask = kBitMasks[bit_in_var_index];
+ string result = "((" + var_name + " & " + mask
+ + ") != (other." + var_name + " & " + mask + "))";
+ return result;
+}
+
+void SetBitOperationVariables(const string name,
+ int bitIndex, map<string, string>* variables) {
+ (*variables)["get_" + name] = GenerateGetBit(bitIndex);
+ (*variables)["set_" + name] = GenerateSetBit(bitIndex);
+ (*variables)["clear_" + name] = GenerateClearBit(bitIndex);
+ (*variables)["different_" + name] = GenerateDifferentBit(bitIndex);
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_helpers.h b/src/google/protobuf/compiler/javanano/javanano_helpers.h
new file mode 100644
index 00000000..29310743
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_helpers.h
@@ -0,0 +1,189 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_HELPERS_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_HELPERS_H__
+
+#include <string>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/io/printer.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+// Commonly-used separator comments. Thick is a line of '=', thin is a line
+// of '-'.
+extern const char kThickSeparator[];
+extern const char kThinSeparator[];
+
+// 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);
+
+// Appends an "_" to the end of a field where the name is a reserved java
+// keyword. For example int32 public = 1 will generate int public_.
+string RenameJavaKeywords(const string& input);
+
+// 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);
+
+// Strips ".proto" or ".protodevel" from the end of a filename.
+string StripProto(const string& filename);
+
+// Gets the unqualified class name for the file. Each .proto file becomes a
+// single Java class, with all its contents nested in that class.
+string FileClassName(const Params& params, const FileDescriptor* file);
+
+// Returns the file's Java package name.
+string FileJavaPackage(const Params& params, const FileDescriptor* file);
+
+// Returns whether the Java outer class is needed, i.e. whether the option
+// java_multiple_files is false, or the proto file contains any file-scope
+// enums/extensions.
+bool IsOuterClassNeeded(const Params& params, const FileDescriptor* file);
+
+// Converts the given simple name of a proto entity to its fully-qualified name
+// in the Java namespace, given that it is in the given file enclosed in the
+// given parent message (or NULL for file-scope entities). Whether the file's
+// outer class name should be included in the return value depends on factors
+// inferrable from the given arguments, including is_class which indicates
+// whether the entity translates to a Java class.
+string ToJavaName(const Params& params, const string& name, bool is_class,
+ const Descriptor* parent, const FileDescriptor* file);
+
+// These return the fully-qualified class name corresponding to the given
+// descriptor.
+inline string ClassName(const Params& params, const Descriptor* descriptor) {
+ return ToJavaName(params, descriptor->name(), true,
+ descriptor->containing_type(), descriptor->file());
+}
+string ClassName(const Params& params, const EnumDescriptor* descriptor);
+inline string ClassName(const Params& params,
+ const ServiceDescriptor* descriptor) {
+ return ToJavaName(params, descriptor->name(), true, NULL, descriptor->file());
+}
+inline string ExtensionIdentifierName(const Params& params,
+ const FieldDescriptor* descriptor) {
+ return ToJavaName(params, descriptor->name(), false,
+ descriptor->extension_scope(), descriptor->file());
+}
+string ClassName(const Params& params, const FileDescriptor* descriptor);
+
+// Get the unqualified name that should be used for a field's field
+// number constant.
+string FieldConstantName(const FieldDescriptor *field);
+
+string FieldDefaultConstantName(const FieldDescriptor *field);
+
+// Print the field's proto-syntax definition as a comment.
+void PrintFieldComment(io::Printer* printer, const FieldDescriptor* field);
+
+enum JavaType {
+ JAVATYPE_INT,
+ JAVATYPE_LONG,
+ JAVATYPE_FLOAT,
+ JAVATYPE_DOUBLE,
+ JAVATYPE_BOOLEAN,
+ JAVATYPE_STRING,
+ JAVATYPE_BYTES,
+ JAVATYPE_ENUM,
+ JAVATYPE_MESSAGE
+};
+
+JavaType GetJavaType(FieldDescriptor::Type field_type);
+
+inline JavaType GetJavaType(const FieldDescriptor* field) {
+ return GetJavaType(field->type());
+}
+
+string PrimitiveTypeName(JavaType type);
+
+// Get the fully-qualified class name for a boxed primitive type, e.g.
+// "java.lang.Integer" for JAVATYPE_INT. Returns NULL for enum and message
+// types.
+string BoxedPrimitiveTypeName(JavaType type);
+
+string EmptyArrayName(const Params& params, const FieldDescriptor* field);
+
+string DefaultValue(const Params& params, const FieldDescriptor* field);
+
+
+// Methods for shared bitfields.
+
+// Gets the name of the shared bitfield for the given field index.
+string GetBitFieldName(int index);
+
+// Gets the name of the shared bitfield for the given bit index.
+// Effectively, GetBitFieldName(bit_index / 32)
+string GetBitFieldNameForBit(int bit_index);
+
+// Generates the java code for the expression that returns whether the bit at
+// the given bit index is set.
+// Example: "((bitField1_ & 0x04000000) != 0)"
+string GenerateGetBit(int bit_index);
+
+// Generates the java code for the expression that sets the bit at the given
+// bit index.
+// Example: "bitField1_ |= 0x04000000"
+string GenerateSetBit(int bit_index);
+
+// Generates the java code for the expression that clears the bit at the given
+// bit index.
+// Example: "bitField1_ = (bitField1_ & ~0x04000000)"
+string GenerateClearBit(int bit_index);
+
+// Generates the java code for the expression that returns whether the bit at
+// the given bit index contains different values in the current object and
+// another object accessible via the variable 'other'.
+// Example: "((bitField1_ & 0x04000000) != (other.bitField1_ & 0x04000000))"
+string GenerateDifferentBit(int bit_index);
+
+// Sets the 'get_*', 'set_*', 'clear_*' and 'different_*' variables, where * is
+// the given name of the bit, to the appropriate Java expressions for the given
+// bit index.
+void SetBitOperationVariables(const string name,
+ int bitIndex, map<string, string>* variables);
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_HELPERS_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_message.cc b/src/google/protobuf/compiler/javanano/javanano_message.cc
new file mode 100644
index 00000000..7c52ca31
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_message.cc
@@ -0,0 +1,555 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <algorithm>
+#include <google/protobuf/stubs/hash.h>
+#include <google/protobuf/compiler/javanano/javanano_message.h>
+#include <google/protobuf/compiler/javanano/javanano_enum.h>
+#include <google/protobuf/compiler/javanano/javanano_extension.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/wire_format.h>
+#include <google/protobuf/descriptor.pb.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+using internal::WireFormat;
+using internal::WireFormatLite;
+
+namespace {
+
+struct FieldOrderingByNumber {
+ inline bool operator()(const FieldDescriptor* a,
+ const FieldDescriptor* b) const {
+ return a->number() < b->number();
+ }
+};
+
+// Sort the fields of the given Descriptor by number into a new[]'d array
+// and return it.
+const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
+ const FieldDescriptor** fields =
+ new const FieldDescriptor*[descriptor->field_count()];
+ for (int i = 0; i < descriptor->field_count(); i++) {
+ fields[i] = descriptor->field(i);
+ }
+ sort(fields, fields + descriptor->field_count(),
+ FieldOrderingByNumber());
+ return fields;
+}
+
+} // namespace
+
+// ===================================================================
+
+MessageGenerator::MessageGenerator(const Descriptor* descriptor, const Params& params)
+ : params_(params),
+ descriptor_(descriptor),
+ field_generators_(descriptor, params) {
+}
+
+MessageGenerator::~MessageGenerator() {}
+
+void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
+ // Generate static members for all nested types.
+ for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // TODO(kenton): Reuse MessageGenerator objects?
+ MessageGenerator(descriptor_->nested_type(i), params_)
+ .GenerateStaticVariables(printer);
+ }
+}
+
+void MessageGenerator::GenerateStaticVariableInitializers(
+ io::Printer* printer) {
+ // Generate static member initializers for all nested types.
+ for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ // TODO(kenton): Reuse MessageGenerator objects?
+ MessageGenerator(descriptor_->nested_type(i), params_)
+ .GenerateStaticVariableInitializers(printer);
+ }
+}
+
+void MessageGenerator::Generate(io::Printer* printer) {
+ if (!params_.store_unknown_fields() &&
+ (descriptor_->extension_count() != 0 || descriptor_->extension_range_count() != 0)) {
+ GOOGLE_LOG(FATAL) << "Extensions are only supported in NANO_RUNTIME if the "
+ "'store_unknown_fields' generator option is 'true'\n";
+ }
+
+ const string& file_name = descriptor_->file()->name();
+ bool is_own_file =
+ params_.java_multiple_files(file_name)
+ && descriptor_->containing_type() == NULL;
+
+ if (is_own_file) {
+ // Note: constants (from enums and fields requiring stored defaults, emitted in the loop below)
+ // may have the same names as constants in the nested classes. This causes Java warnings, but
+ // is not fatal, so we suppress those warnings here in the top-most class declaration.
+ printer->Print(
+ "\n"
+ "@SuppressWarnings(\"hiding\")\n"
+ "public final class $classname$ extends\n",
+ "classname", descriptor_->name());
+ } else {
+ printer->Print(
+ "\n"
+ "public static final class $classname$ extends\n",
+ "classname", descriptor_->name());
+ }
+ if (params_.store_unknown_fields() && params_.parcelable_messages()) {
+ printer->Print(
+ " com.google.protobuf.nano.android.ParcelableExtendableMessageNano<$classname$> {\n",
+ "classname", descriptor_->name());
+ } else if (params_.store_unknown_fields()) {
+ printer->Print(
+ " com.google.protobuf.nano.ExtendableMessageNano<$classname$> {\n",
+ "classname", descriptor_->name());
+ } else if (params_.parcelable_messages()) {
+ printer->Print(
+ " com.google.protobuf.nano.android.ParcelableMessageNano {\n");
+ } else {
+ printer->Print(
+ " com.google.protobuf.nano.MessageNano {\n");
+ }
+ printer->Indent();
+
+ // Nested types and extensions
+ for (int i = 0; i < descriptor_->extension_count(); i++) {
+ ExtensionGenerator(descriptor_->extension(i), params_).Generate(printer);
+ }
+
+ for (int i = 0; i < descriptor_->enum_type_count(); i++) {
+ EnumGenerator(descriptor_->enum_type(i), params_).Generate(printer);
+ }
+
+ for (int i = 0; i < descriptor_->nested_type_count(); i++) {
+ MessageGenerator(descriptor_->nested_type(i), params_).Generate(printer);
+ }
+
+ // Lazy initialization of otherwise static final fields can help prevent the
+ // class initializer from being generated. We want to prevent it because it
+ // stops ProGuard from inlining any methods in this class into call sites and
+ // therefore reducing the method count. However, extensions are best kept as
+ // public static final fields with initializers, so with their existence we
+ // won't bother with lazy initialization.
+ bool lazy_init = descriptor_->extension_count() == 0;
+
+ // Empty array
+ if (lazy_init) {
+ printer->Print(
+ "\n"
+ "private static volatile $classname$[] _emptyArray;\n"
+ "public static $classname$[] emptyArray() {\n"
+ " // Lazily initializes the empty array\n"
+ " if (_emptyArray == null) {\n"
+ " synchronized (\n"
+ " com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {\n"
+ " if (_emptyArray == null) {\n"
+ " _emptyArray = new $classname$[0];\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " return _emptyArray;\n"
+ "}\n",
+ "classname", descriptor_->name());
+ } else {
+ printer->Print(
+ "\n"
+ "private static final $classname$[] EMPTY_ARRAY = {};\n"
+ "public static $classname$[] emptyArray() {\n"
+ " return EMPTY_ARRAY;\n"
+ "}\n",
+ "classname", descriptor_->name());
+ }
+
+ // Integers for bit fields
+ int totalInts = (field_generators_.total_bits() + 31) / 32;
+ if (totalInts > 0) {
+ printer->Print("\n");
+ for (int i = 0; i < totalInts; i++) {
+ printer->Print("private int $bit_field_name$;\n",
+ "bit_field_name", GetBitFieldName(i));
+ }
+ }
+
+ // Fields and maybe their default values
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ printer->Print("\n");
+ PrintFieldComment(printer, descriptor_->field(i));
+ field_generators_.get(descriptor_->field(i)).GenerateMembers(
+ printer, lazy_init);
+ }
+
+ // Constructor, with lazy init code if needed
+ if (lazy_init && field_generators_.saved_defaults_needed()) {
+ printer->Print(
+ "\n"
+ "private static volatile boolean _classInitialized;\n"
+ "\n"
+ "public $classname$() {\n"
+ " // Lazily initializes the field defaults\n"
+ " if (!_classInitialized) {\n"
+ " synchronized (\n"
+ " com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {\n"
+ " if (!_classInitialized) {\n",
+ "classname", descriptor_->name());
+ printer->Indent();
+ printer->Indent();
+ printer->Indent();
+ printer->Indent();
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ field_generators_.get(descriptor_->field(i))
+ .GenerateInitSavedDefaultCode(printer);
+ }
+ printer->Outdent();
+ printer->Outdent();
+ printer->Outdent();
+ printer->Outdent();
+ printer->Print(
+ " _classInitialized = true;\n"
+ " }\n"
+ " }\n"
+ " }\n");
+ if (params_.generate_clear()) {
+ printer->Print(" clear();\n");
+ }
+ printer->Print("}\n");
+ } else {
+ if (params_.generate_clear()) {
+ printer->Print(
+ "\n"
+ "public $classname$() {\n"
+ " clear();\n"
+ "}\n",
+ "classname", descriptor_->name());
+ }
+ }
+
+ // Other methods in this class
+
+ GenerateClear(printer);
+
+ if (params_.generate_equals()) {
+ GenerateEquals(printer);
+ GenerateHashCode(printer);
+ }
+
+ GenerateMessageSerializationMethods(printer);
+ GenerateMergeFromMethods(printer);
+ GenerateParseFromMethods(printer);
+
+ printer->Outdent();
+ printer->Print("}\n");
+}
+
+// ===================================================================
+
+void MessageGenerator::
+GenerateMessageSerializationMethods(io::Printer* printer) {
+ // Rely on the parent implementations of writeTo() and getSerializedSize()
+ // if there are no fields to serialize in this message.
+ if (descriptor_->field_count() == 0) {
+ return;
+ }
+
+ scoped_array<const FieldDescriptor*> sorted_fields(
+ SortFieldsByNumber(descriptor_));
+
+ printer->Print(
+ "\n"
+ "@Override\n"
+ "public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)\n"
+ " throws java.io.IOException {\n");
+ printer->Indent();
+
+ // Output the fields in sorted order
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ GenerateSerializeOneField(printer, sorted_fields[i]);
+ }
+
+ // The parent implementation will write any unknown fields if necessary.
+ printer->Print(
+ "super.writeTo(output);\n");
+
+ printer->Outdent();
+ printer->Print("}\n");
+
+ // The parent implementation will get the serialized size for unknown
+ // fields if necessary.
+ printer->Print(
+ "\n"
+ "@Override\n"
+ "protected int computeSerializedSize() {\n"
+ " int size = super.computeSerializedSize();\n");
+ printer->Indent();
+
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ field_generators_.get(sorted_fields[i]).GenerateSerializedSizeCode(printer);
+ }
+
+ printer->Outdent();
+ printer->Print(
+ " return size;\n"
+ "}\n");
+}
+
+void MessageGenerator::GenerateMergeFromMethods(io::Printer* printer) {
+ scoped_array<const FieldDescriptor*> sorted_fields(
+ SortFieldsByNumber(descriptor_));
+
+ printer->Print(
+ "\n"
+ "@Override\n"
+ "public $classname$ mergeFrom(\n"
+ " com.google.protobuf.nano.CodedInputByteBufferNano input)\n"
+ " throws java.io.IOException {\n",
+ "classname", descriptor_->name());
+
+ printer->Indent();
+
+ printer->Print(
+ "while (true) {\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
+ " return this;\n"
+ "default: {\n");
+
+ printer->Indent();
+ if (params_.store_unknown_fields()) {
+ printer->Print(
+ "if (!storeUnknownField(input, tag)) {\n"
+ " return this;\n"
+ "}\n");
+ } else {
+ printer->Print(
+ "if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {\n"
+ " return this;\n" // it's an endgroup tag
+ "}\n");
+ }
+ printer->Print("break;\n");
+ printer->Outdent();
+ printer->Print("}\n");
+
+ 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(tag));
+ printer->Indent();
+
+ field_generators_.get(field).GenerateMergingCode(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(packed_tag));
+ printer->Indent();
+
+ field_generators_.get(field).GenerateMergingCodeFromPacked(printer);
+
+ printer->Outdent();
+ printer->Print(
+ " break;\n"
+ "}\n");
+ }
+ }
+
+ printer->Outdent();
+ printer->Outdent();
+ printer->Outdent();
+ printer->Print(
+ " }\n" // switch (tag)
+ " }\n" // while (true)
+ "}\n");
+}
+
+void MessageGenerator::
+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(
+ "\n"
+ "public static $classname$ parseFrom(byte[] data)\n"
+ " throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {\n"
+ " return com.google.protobuf.nano.MessageNano.mergeFrom(new $classname$(), data);\n"
+ "}\n"
+ "\n"
+ "public static $classname$ parseFrom(\n"
+ " com.google.protobuf.nano.CodedInputByteBufferNano input)\n"
+ " throws java.io.IOException {\n"
+ " return new $classname$().mergeFrom(input);\n"
+ "}\n",
+ "classname", descriptor_->name());
+}
+
+void MessageGenerator::GenerateSerializeOneField(
+ io::Printer* printer, const FieldDescriptor* field) {
+ field_generators_.get(field).GenerateSerializationCode(printer);
+}
+
+void MessageGenerator::GenerateClear(io::Printer* printer) {
+ if (!params_.generate_clear()) {
+ return;
+ }
+ printer->Print(
+ "\n"
+ "public $classname$ clear() {\n",
+ "classname", descriptor_->name());
+ printer->Indent();
+
+ // Clear bit fields.
+ int totalInts = (field_generators_.total_bits() + 31) / 32;
+ for (int i = 0; i < totalInts; i++) {
+ printer->Print("$bit_field_name$ = 0;\n",
+ "bit_field_name", GetBitFieldName(i));
+ }
+
+ // Call clear for all of the fields.
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ field_generators_.get(field).GenerateClearCode(printer);
+ }
+
+ // Clear unknown fields.
+ if (params_.store_unknown_fields()) {
+ printer->Print("unknownFieldData = null;\n");
+ }
+
+ printer->Outdent();
+ printer->Print(
+ " cachedSize = -1;\n"
+ " return this;\n"
+ "}\n");
+}
+
+void MessageGenerator::GenerateEquals(io::Printer* printer) {
+ // Don't override if there are no fields. We could generate an
+ // equals method that compares types, but often empty messages
+ // are used as namespaces.
+ if (descriptor_->field_count() == 0 && !params_.store_unknown_fields()) {
+ return;
+ }
+
+ printer->Print(
+ "\n"
+ "@Override\n"
+ "public boolean equals(Object o) {\n");
+ printer->Indent();
+ printer->Print(
+ "if (o == this) {\n"
+ " return true;\n"
+ "}\n"
+ "if (!(o instanceof $classname$)) {\n"
+ " return false;\n"
+ "}\n"
+ "$classname$ other = ($classname$) o;\n",
+ "classname", descriptor_->name());
+
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ field_generators_.get(field).GenerateEqualsCode(printer);
+ }
+
+ if (params_.store_unknown_fields()) {
+ printer->Print(
+ "return unknownFieldDataEquals(other);\n");
+ } else {
+ printer->Print(
+ "return true;\n");
+ }
+
+ printer->Outdent();
+ printer->Print("}\n");
+}
+
+void MessageGenerator::GenerateHashCode(io::Printer* printer) {
+ if (descriptor_->field_count() == 0 && !params_.store_unknown_fields()) {
+ return;
+ }
+
+ printer->Print(
+ "\n"
+ "@Override\n"
+ "public int hashCode() {\n");
+ printer->Indent();
+
+ printer->Print("int result = 17;\n");
+ printer->Print("result = 31 * result + getClass().getName().hashCode();\n");
+ for (int i = 0; i < descriptor_->field_count(); i++) {
+ const FieldDescriptor* field = descriptor_->field(i);
+ field_generators_.get(field).GenerateHashCodeCode(printer);
+ }
+
+ if (params_.store_unknown_fields()) {
+ printer->Print(
+ "result = 31 * result + unknownFieldDataHashCode();\n");
+ }
+
+ printer->Print("return result;\n");
+
+ printer->Outdent();
+ printer->Print("}\n");
+}
+
+// ===================================================================
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_message.h b/src/google/protobuf/compiler/javanano/javanano_message.h
new file mode 100644
index 00000000..6f25a3a0
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_message.h
@@ -0,0 +1,95 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_H__
+
+#include <string>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/compiler/javanano/javanano_field.h>
+#include <google/protobuf/compiler/javanano/javanano_params.h>
+#include <google/protobuf/stubs/common.h>
+
+namespace google {
+namespace protobuf {
+ namespace io {
+ class Printer; // printer.h
+ }
+}
+
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class MessageGenerator {
+ public:
+ explicit MessageGenerator(const Descriptor* descriptor, const Params& params);
+ ~MessageGenerator();
+
+ // All static variables have to be declared at the top-level of the file
+ // so that we can control initialization order, which is important for
+ // DescriptorProto bootstrapping to work.
+ void GenerateStaticVariables(io::Printer* printer);
+
+ // Output code which initializes the static variables generated by
+ // GenerateStaticVariables().
+ void GenerateStaticVariableInitializers(io::Printer* printer);
+
+ // Generate the class itself.
+ void Generate(io::Printer* printer);
+
+ private:
+ void GenerateMessageSerializationMethods(io::Printer* printer);
+ void GenerateMergeFromMethods(io::Printer* printer);
+ void GenerateParseFromMethods(io::Printer* printer);
+ void GenerateSerializeOneField(io::Printer* printer,
+ const FieldDescriptor* field);
+
+ void GenerateClear(io::Printer* printer);
+ void GenerateEquals(io::Printer* printer);
+ void GenerateHashCode(io::Printer* printer);
+
+ const Params& params_;
+ const Descriptor* descriptor_;
+ FieldGeneratorMap field_generators_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
new file mode 100644
index 00000000..a46081d0
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
@@ -0,0 +1,259 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <map>
+#include <string>
+
+#include <google/protobuf/compiler/javanano/javanano_message_field.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/wire_format.h>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+using internal::WireFormat;
+using internal::WireFormatLite;
+
+namespace {
+
+// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
+// repeat code between this and the other field types.
+void SetMessageVariables(const Params& params,
+ const FieldDescriptor* descriptor, map<string, string>* variables) {
+ (*variables)["name"] =
+ RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
+ (*variables)["capitalized_name"] =
+ RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
+ (*variables)["number"] = SimpleItoa(descriptor->number());
+ (*variables)["type"] = ClassName(params, descriptor->message_type());
+ (*variables)["group_or_message"] =
+ (descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
+ "Group" : "Message";
+ (*variables)["message_name"] = descriptor->containing_type()->name();
+ //(*variables)["message_type"] = descriptor->message_type()->name();
+ (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
+}
+
+} // namespace
+
+// ===================================================================
+
+MessageFieldGenerator::
+MessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetMessageVariables(params, descriptor, &variables_);
+}
+
+MessageFieldGenerator::~MessageFieldGenerator() {}
+
+void MessageFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
+ printer->Print(variables_,
+ "public $type$ $name$;\n");
+}
+
+void MessageFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = null;\n");
+}
+
+void MessageFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ == null) {\n"
+ " this.$name$ = new $type$();\n"
+ "}\n");
+
+ if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
+ printer->Print(variables_,
+ "input.readGroup(this.$name$, $number$);\n");
+ } else {
+ printer->Print(variables_,
+ "input.readMessage(this.$name$);\n");
+ }
+}
+
+void MessageFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null) {\n"
+ " output.write$group_or_message$($number$, this.$name$);\n"
+ "}\n");
+}
+
+void MessageFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null) {\n"
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$group_or_message$Size($number$, this.$name$);\n"
+ "}\n");
+}
+
+void MessageFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ == null) { \n"
+ " if (other.$name$ != null) {\n"
+ " return false;\n"
+ " }\n"
+ "} else {\n"
+ " if (!this.$name$.equals(other.$name$)) {\n"
+ " return false;\n"
+ " }\n"
+ "}\n");
+}
+
+void MessageFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "result = 31 * result +\n"
+ " (this.$name$ == null ? 0 : this.$name$.hashCode());\n");
+}
+
+// ===================================================================
+
+RepeatedMessageFieldGenerator::
+RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetMessageVariables(params, descriptor, &variables_);
+}
+
+RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
+
+void RepeatedMessageFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /* unused lazy_init */) const {
+ printer->Print(variables_,
+ "public $type$[] $name$;\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = $type$.emptyArray();\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ // First, figure out the length of the array, then parse.
+ printer->Print(variables_,
+ "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
+ " .getRepeatedFieldArrayLength(input, $tag$);\n"
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ "$type$[] newArray =\n"
+ " new $type$[i + arrayLength];\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length - 1; i++) {\n"
+ " newArray[i] = new $type$();\n");
+
+ if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
+ printer->Print(variables_,
+ " input.readGroup(newArray[i], $number$);\n");
+ } else {
+ printer->Print(variables_,
+ " input.readMessage(newArray[i]);\n");
+ }
+
+ printer->Print(variables_,
+ " input.readTag();\n"
+ "}\n"
+ "// Last one without readTag.\n"
+ "newArray[i] = new $type$();\n");
+
+ if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
+ printer->Print(variables_,
+ "input.readGroup(newArray[i], $number$);\n");
+ } else {
+ printer->Print(variables_,
+ "input.readMessage(newArray[i]);\n");
+ }
+
+ printer->Print(variables_,
+ "this.$name$ = newArray;\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n"
+ " for (int i = 0; i < this.$name$.length; i++) {\n"
+ " $type$ element = this.$name$[i];\n"
+ " if (element != null) {\n"
+ " output.write$group_or_message$($number$, element);\n"
+ " }\n"
+ " }\n"
+ "}\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n"
+ " for (int i = 0; i < this.$name$.length; i++) {\n"
+ " $type$ element = this.$name$[i];\n"
+ " if (element != null) {\n"
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$group_or_message$Size($number$, element);\n"
+ " }\n"
+ " }\n"
+ "}\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (!com.google.protobuf.nano.InternalNano.equals(\n"
+ " this.$name$, other.$name$)) {\n"
+ " return false;\n"
+ "}\n");
+}
+
+void RepeatedMessageFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.h b/src/google/protobuf/compiler/javanano/javanano_message_field.h
new file mode 100644
index 00000000..5d35fd24
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_message_field.h
@@ -0,0 +1,96 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_FIELD_H__
+
+#include <map>
+#include <string>
+#include <google/protobuf/compiler/javanano/javanano_field.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class MessageFieldGenerator : public FieldGenerator {
+ public:
+ explicit MessageFieldGenerator(
+ const FieldDescriptor* descriptor, const Params& params);
+ ~MessageFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator);
+};
+
+class RepeatedMessageFieldGenerator : public FieldGenerator {
+ public:
+ explicit RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor,
+ const Params& params);
+ ~RepeatedMessageFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_MESSAGE_FIELD_H__
diff --git a/src/google/protobuf/compiler/javanano/javanano_params.h b/src/google/protobuf/compiler/javanano/javanano_params.h
new file mode 100644
index 00000000..4691f360
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_params.h
@@ -0,0 +1,240 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2010 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: wink@google.com (Wink Saville)
+
+#ifndef PROTOBUF_COMPILER_JAVANANO_JAVANANO_PARAMS_H_
+#define PROTOBUF_COMPILER_JAVANANO_JAVANANO_PARAMS_H_
+
+#include <map>
+#include <set>
+#include <google/protobuf/stubs/strutil.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+enum eMultipleFiles { JAVANANO_MUL_UNSET, JAVANANO_MUL_FALSE, JAVANANO_MUL_TRUE };
+
+// Parameters for used by the generators
+class Params {
+ public:
+ typedef map<string, string> NameMap;
+ typedef set<string> NameSet;
+ private:
+ string empty_;
+ string base_name_;
+ eMultipleFiles override_java_multiple_files_;
+ bool store_unknown_fields_;
+ NameMap java_packages_;
+ NameMap java_outer_classnames_;
+ NameSet java_multiple_files_;
+ bool generate_has_;
+ bool java_enum_style_;
+ bool optional_field_accessors_;
+ bool use_reference_types_for_primitives_;
+ bool generate_equals_;
+ bool ignore_services_;
+ bool parcelable_messages_;
+ bool reftypes_primitive_enums_;
+ bool generate_clear_;
+
+ public:
+ Params(const string & base_name) :
+ empty_(""),
+ base_name_(base_name),
+ override_java_multiple_files_(JAVANANO_MUL_UNSET),
+ store_unknown_fields_(false),
+ generate_has_(false),
+ java_enum_style_(false),
+ optional_field_accessors_(false),
+ use_reference_types_for_primitives_(false),
+ generate_equals_(false),
+ ignore_services_(false),
+ parcelable_messages_(false),
+ reftypes_primitive_enums_(false),
+ generate_clear_(true) {
+ }
+
+ const string& base_name() const {
+ return base_name_;
+ }
+
+ bool has_java_package(const string& file_name) const {
+ return java_packages_.find(file_name)
+ != java_packages_.end();
+ }
+ void set_java_package(const string& file_name,
+ const string& java_package) {
+ java_packages_[file_name] = java_package;
+ }
+ const string& java_package(const string& file_name) const {
+ NameMap::const_iterator itr;
+
+ itr = java_packages_.find(file_name);
+ if (itr == java_packages_.end()) {
+ return empty_;
+ } else {
+ return itr->second;
+ }
+ }
+ const NameMap& java_packages() {
+ return java_packages_;
+ }
+
+ bool has_java_outer_classname(const string& file_name) const {
+ return java_outer_classnames_.find(file_name)
+ != java_outer_classnames_.end();
+ }
+ void set_java_outer_classname(const string& file_name,
+ const string& java_outer_classname) {
+ java_outer_classnames_[file_name] = java_outer_classname;
+ }
+ const string& java_outer_classname(const string& file_name) const {
+ NameMap::const_iterator itr;
+
+ itr = java_outer_classnames_.find(file_name);
+ if (itr == java_outer_classnames_.end()) {
+ return empty_;
+ } else {
+ return itr->second;
+ }
+ }
+ const NameMap& java_outer_classnames() {
+ return java_outer_classnames_;
+ }
+
+ void set_override_java_multiple_files(bool java_multiple_files) {
+ if (java_multiple_files) {
+ override_java_multiple_files_ = JAVANANO_MUL_TRUE;
+ } else {
+ override_java_multiple_files_ = JAVANANO_MUL_FALSE;
+ }
+ }
+ void clear_override_java_multiple_files() {
+ override_java_multiple_files_ = JAVANANO_MUL_UNSET;
+ }
+
+ void set_java_multiple_files(const string& file_name, bool value) {
+ if (value) {
+ java_multiple_files_.insert(file_name);
+ } else {
+ java_multiple_files_.erase(file_name);
+ }
+ }
+ bool java_multiple_files(const string& file_name) const {
+ switch (override_java_multiple_files_) {
+ case JAVANANO_MUL_FALSE:
+ return false;
+ case JAVANANO_MUL_TRUE:
+ return true;
+ default:
+ return java_multiple_files_.find(file_name)
+ != java_multiple_files_.end();
+ }
+ }
+
+ void set_store_unknown_fields(bool value) {
+ store_unknown_fields_ = value;
+ }
+ bool store_unknown_fields() const {
+ return store_unknown_fields_;
+ }
+
+ void set_generate_has(bool value) {
+ generate_has_ = value;
+ }
+ bool generate_has() const {
+ return generate_has_;
+ }
+
+ void set_java_enum_style(bool value) {
+ java_enum_style_ = value;
+ }
+ bool java_enum_style() const {
+ return java_enum_style_;
+ }
+
+ void set_optional_field_accessors(bool value) {
+ optional_field_accessors_ = value;
+ }
+ bool optional_field_accessors() const {
+ return optional_field_accessors_;
+ }
+
+ void set_use_reference_types_for_primitives(bool value) {
+ use_reference_types_for_primitives_ = value;
+ }
+ bool use_reference_types_for_primitives() const {
+ return use_reference_types_for_primitives_;
+ }
+
+ void set_generate_equals(bool value) {
+ generate_equals_ = value;
+ }
+ bool generate_equals() const {
+ return generate_equals_;
+ }
+
+ void set_ignore_services(bool value) {
+ ignore_services_ = value;
+ }
+ bool ignore_services() const {
+ return ignore_services_;
+ }
+
+ void set_parcelable_messages(bool value) {
+ parcelable_messages_ = value;
+ }
+ bool parcelable_messages() const {
+ return parcelable_messages_;
+ }
+
+ void set_reftypes_primitive_enums(bool value) {
+ reftypes_primitive_enums_ = value;
+ }
+ bool reftypes_primitive_enums() const {
+ return reftypes_primitive_enums_;
+ }
+
+ void set_generate_clear(bool value) {
+ generate_clear_ = value;
+ }
+ bool generate_clear() const {
+ return generate_clear_;
+ }
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
+#endif // PROTOBUF_COMPILER_JAVANANO_JAVANANO_PARAMS_H_
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
new file mode 100644
index 00000000..a3bc3a84
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
@@ -0,0 +1,910 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#include <map>
+#include <math.h>
+#include <string>
+
+#include <google/protobuf/compiler/javanano/javanano_primitive_field.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/compiler/javanano/javanano_helpers.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/wire_format.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/stubs/substitute.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+using internal::WireFormat;
+using internal::WireFormatLite;
+
+namespace {
+
+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 false;
+ case JAVATYPE_MESSAGE: return true;
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return false;
+}
+
+bool IsArrayType(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 false;
+ case JAVATYPE_BYTES : return true;
+ case JAVATYPE_ENUM : return false;
+ case JAVATYPE_MESSAGE: return false;
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return false;
+}
+
+const char* GetCapitalizedType(const FieldDescriptor* field) {
+ 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.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return NULL;
+}
+
+// For encodings with fixed sizes, returns that size in bytes. Otherwise
+// 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.
+ }
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return -1;
+}
+
+// Return true if the type is a that has variable length
+// for instance String's.
+bool IsVariableLenType(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 false;
+ case JAVATYPE_MESSAGE: return true;
+
+ // No default because we want the compiler to complain if any new
+ // JavaTypes are added.
+ }
+
+ GOOGLE_LOG(FATAL) << "Can't get here.";
+ return false;
+}
+
+bool AllAscii(const string& text) {
+ for (int i = 0; i < text.size(); i++) {
+ if ((text[i] & 0x80) != 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params params,
+ map<string, string>* variables) {
+ (*variables)["name"] =
+ RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
+ (*variables)["capitalized_name"] =
+ RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
+ (*variables)["number"] = SimpleItoa(descriptor->number());
+ if (params.use_reference_types_for_primitives()
+ && !descriptor->is_repeated()) {
+ (*variables)["type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
+ } else {
+ (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
+ }
+ // Deals with defaults. For C++-string types (string and bytes),
+ // we might need to have the generated code do the unicode decoding
+ // (see comments in InternalNano.java for gory details.). We would
+ // like to do this once into a static field and re-use that from
+ // then on.
+ if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
+ !descriptor->default_value_string().empty() &&
+ !params.use_reference_types_for_primitives()) {
+ if (descriptor->type() == FieldDescriptor::TYPE_BYTES) {
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
+ (*variables)["default_constant_value"] = strings::Substitute(
+ "com.google.protobuf.nano.InternalNano.bytesDefaultValue(\"$0\")",
+ CEscape(descriptor->default_value_string()));
+ (*variables)["default_copy_if_needed"] =
+ (*variables)["default"] + ".clone()";
+ } else if (AllAscii(descriptor->default_value_string())) {
+ // All chars are ASCII. In this case directly referencing a
+ // CEscape()'d string literal works fine.
+ (*variables)["default"] =
+ "\"" + CEscape(descriptor->default_value_string()) + "\"";
+ (*variables)["default_copy_if_needed"] = (*variables)["default"];
+ } else {
+ // Strings where some chars are non-ASCII. We need to save the
+ // default value.
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
+ (*variables)["default_constant_value"] = strings::Substitute(
+ "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")",
+ CEscape(descriptor->default_value_string()));
+ (*variables)["default_copy_if_needed"] = (*variables)["default"];
+ }
+ } else {
+ // Non-string, non-bytes field. Defaults are literals.
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ (*variables)["default_copy_if_needed"] = (*variables)["default"];
+ }
+ (*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
+ (*variables)["capitalized_type"] = GetCapitalizedType(descriptor);
+ (*variables)["tag"] = SimpleItoa(WireFormat::MakeTag(descriptor));
+ (*variables)["tag_size"] = SimpleItoa(
+ WireFormat::TagSize(descriptor->number(), descriptor->type()));
+ (*variables)["non_packed_tag"] = SimpleItoa(
+ internal::WireFormatLite::MakeTag(descriptor->number(),
+ internal::WireFormat::WireTypeForFieldType(descriptor->type())));
+ int fixed_size = FixedSize(descriptor->type());
+ if (fixed_size != -1) {
+ (*variables)["fixed_size"] = SimpleItoa(fixed_size);
+ }
+ (*variables)["message_name"] = descriptor->containing_type()->name();
+ (*variables)["empty_array_name"] = EmptyArrayName(params, descriptor);
+}
+} // namespace
+
+// ===================================================================
+
+PrimitiveFieldGenerator::
+PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetPrimitiveVariables(descriptor, params, &variables_);
+}
+
+PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
+
+bool PrimitiveFieldGenerator::SavedDefaultNeeded() const {
+ return variables_.find("default_constant") != variables_.end();
+}
+
+void PrimitiveFieldGenerator::GenerateInitSavedDefaultCode(io::Printer* printer) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ printer->Print(variables_,
+ "$default_constant$ = $default_constant_value$;\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateMembers(io::Printer* printer, bool lazy_init) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ // Those primitive types that need a saved default.
+ if (lazy_init) {
+ printer->Print(variables_,
+ "private static $type$ $default_constant$;\n");
+ } else {
+ printer->Print(variables_,
+ "private static final $type$ $default_constant$ =\n"
+ " $default_constant_value$;\n");
+ }
+ }
+
+ printer->Print(variables_,
+ "public $type$ $name$;\n");
+
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "public boolean has$capitalized_name$;\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = $default_copy_if_needed$;\n");
+
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "has$capitalized_name$ = false;\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "this.$name$ = input.read$capitalized_type$();\n");
+
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "has$capitalized_name$ = true;\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateSerializationConditional(io::Printer* printer) const {
+ if (params_.use_reference_types_for_primitives()) {
+ // For reference type mode, serialize based on equality
+ // to null.
+ printer->Print(variables_,
+ "if (this.$name$ != null) {\n");
+ return;
+ }
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "if (has$capitalized_name$ || ");
+ } else {
+ printer->Print(variables_,
+ "if (");
+ }
+ JavaType java_type = GetJavaType(descriptor_);
+ if (IsArrayType(java_type)) {
+ printer->Print(variables_,
+ "!java.util.Arrays.equals(this.$name$, $default$)) {\n");
+ } else if (IsReferenceType(java_type)) {
+ printer->Print(variables_,
+ "!this.$name$.equals($default$)) {\n");
+ } else if (java_type == JAVATYPE_FLOAT) {
+ printer->Print(variables_,
+ "java.lang.Float.floatToIntBits(this.$name$)\n"
+ " != java.lang.Float.floatToIntBits($default$)) {\n");
+ } else if (java_type == JAVATYPE_DOUBLE) {
+ printer->Print(variables_,
+ "java.lang.Double.doubleToLongBits(this.$name$)\n"
+ " != java.lang.Double.doubleToLongBits($default$)) {\n");
+ } else {
+ printer->Print(variables_,
+ "this.$name$ != $default$) {\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ if (descriptor_->is_required() && !params_.generate_has()) {
+ // Always serialize a required field if we don't have the 'has' signal.
+ printer->Print(variables_,
+ "output.write$capitalized_type$($number$, this.$name$);\n");
+ } else {
+ GenerateSerializationConditional(printer);
+ printer->Print(variables_,
+ " output.write$capitalized_type$($number$, this.$name$);\n"
+ "}\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ if (descriptor_->is_required() && !params_.generate_has()) {
+ printer->Print(variables_,
+ "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$capitalized_type$Size($number$, this.$name$);\n");
+ } else {
+ GenerateSerializationConditional(printer);
+ printer->Print(variables_,
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$capitalized_type$Size($number$, this.$name$);\n"
+ "}\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ // We define equality as serialized form equality. If generate_has(),
+ // then if the field value equals the default value in both messages,
+ // but one's 'has' field is set and the other's is not, the serialized
+ // forms are different and we should return false.
+ JavaType java_type = GetJavaType(descriptor_);
+ if (java_type == JAVATYPE_BYTES) {
+ printer->Print(variables_,
+ "if (!java.util.Arrays.equals(this.$name$, other.$name$)");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (java.util.Arrays.equals(this.$name$, $default$)\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ "}\n");
+ } else if (java_type == JAVATYPE_STRING
+ || params_.use_reference_types_for_primitives()) {
+ printer->Print(variables_,
+ "if (this.$name$ == null) {\n"
+ " if (other.$name$ != null) {\n"
+ " return false;\n"
+ " }\n"
+ "} else if (!this.$name$.equals(other.$name$)");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (this.$name$.equals($default$)\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ "}\n");
+ } else if (java_type == JAVATYPE_FLOAT) {
+ printer->Print(variables_,
+ "{\n"
+ " int bits = java.lang.Float.floatToIntBits(this.$name$);\n"
+ " if (bits != java.lang.Float.floatToIntBits(other.$name$)");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (bits == java.lang.Float.floatToIntBits($default$)\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ " }\n"
+ "}\n");
+ } else if (java_type == JAVATYPE_DOUBLE) {
+ printer->Print(variables_,
+ "{\n"
+ " long bits = java.lang.Double.doubleToLongBits(this.$name$);\n"
+ " if (bits != java.lang.Double.doubleToLongBits(other.$name$)");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (bits == java.lang.Double.doubleToLongBits($default$)\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ " }\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "if (this.$name$ != other.$name$");
+ if (params_.generate_has()) {
+ printer->Print(variables_,
+ "\n"
+ " || (this.$name$ == $default$\n"
+ " && this.has$capitalized_name$ != other.has$capitalized_name$)");
+ }
+ printer->Print(") {\n"
+ " return false;\n"
+ "}\n");
+ }
+}
+
+void PrimitiveFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ JavaType java_type = GetJavaType(descriptor_);
+ if (java_type == JAVATYPE_BYTES) {
+ printer->Print(variables_,
+ "result = 31 * result + java.util.Arrays.hashCode(this.$name$);\n");
+ } else if (java_type == JAVATYPE_STRING
+ || params_.use_reference_types_for_primitives()) {
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + (this.$name$ == null ? 0 : this.$name$.hashCode());\n");
+ } else {
+ switch (java_type) {
+ // For all Java primitive types below, the hash codes match the
+ // results of BoxedType.valueOf(primitiveValue).hashCode().
+ case JAVATYPE_INT:
+ printer->Print(variables_,
+ "result = 31 * result + this.$name$;\n");
+ break;
+ case JAVATYPE_LONG:
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + (int) (this.$name$ ^ (this.$name$ >>> 32));\n");
+ break;
+ case JAVATYPE_FLOAT:
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + java.lang.Float.floatToIntBits(this.$name$);\n");
+ break;
+ case JAVATYPE_DOUBLE:
+ printer->Print(variables_,
+ "{\n"
+ " long v = java.lang.Double.doubleToLongBits(this.$name$);\n"
+ " result = 31 * result + (int) (v ^ (v >>> 32));\n"
+ "}\n");
+ break;
+ case JAVATYPE_BOOLEAN:
+ printer->Print(variables_,
+ "result = 31 * result + (this.$name$ ? 1231 : 1237);\n");
+ break;
+ default:
+ GOOGLE_LOG(ERROR) << "unknown java type for primitive field";
+ break;
+ }
+ }
+}
+
+// ===================================================================
+
+AccessorPrimitiveFieldGenerator::
+AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
+ const Params& params, int has_bit_index)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetPrimitiveVariables(descriptor, params, &variables_);
+ SetBitOperationVariables("has", has_bit_index, &variables_);
+}
+
+AccessorPrimitiveFieldGenerator::~AccessorPrimitiveFieldGenerator() {}
+
+bool AccessorPrimitiveFieldGenerator::SavedDefaultNeeded() const {
+ return variables_.find("default_constant") != variables_.end();
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateInitSavedDefaultCode(io::Printer* printer) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ printer->Print(variables_,
+ "$default_constant$ = $default_constant_value$;\n");
+ }
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateMembers(io::Printer* printer, bool lazy_init) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ // Those primitive types that need a saved default.
+ if (lazy_init) {
+ printer->Print(variables_,
+ "private static $type$ $default_constant$;\n");
+ } else {
+ printer->Print(variables_,
+ "private static final $type$ $default_constant$ =\n"
+ " $default_constant_value$;\n");
+ }
+ }
+ printer->Print(variables_,
+ "private $type$ $name$_;\n"
+ "public $type$ get$capitalized_name$() {\n"
+ " return $name$_;\n"
+ "}\n"
+ "public $message_name$ set$capitalized_name$($type$ value) {\n");
+ if (IsReferenceType(GetJavaType(descriptor_))) {
+ printer->Print(variables_,
+ " if (value == null) {\n"
+ " throw new java.lang.NullPointerException();\n"
+ " }\n");
+ }
+ printer->Print(variables_,
+ " $name$_ = value;\n"
+ " $set_has$;\n"
+ " return this;\n"
+ "}\n"
+ "public boolean has$capitalized_name$() {\n"
+ " return $get_has$;\n"
+ "}\n"
+ "public $message_name$ clear$capitalized_name$() {\n"
+ " $name$_ = $default_copy_if_needed$;\n"
+ " $clear_has$;\n"
+ " return this;\n"
+ "}\n");
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$_ = $default_copy_if_needed$;\n");
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$_ = input.read$capitalized_type$();\n"
+ "$set_has$;\n");
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if ($get_has$) {\n"
+ " output.write$capitalized_type$($number$, $name$_);\n"
+ "}\n");
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if ($get_has$) {\n"
+ " size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$capitalized_type$Size($number$, $name$_);\n"
+ "}\n");
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ switch (GetJavaType(descriptor_)) {
+ // For all Java primitive types below, the equality checks match the
+ // results of BoxedType.valueOf(primitiveValue).equals(otherValue).
+ case JAVATYPE_FLOAT:
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || java.lang.Float.floatToIntBits($name$_)\n"
+ " != java.lang.Float.floatToIntBits(other.$name$_)) {\n"
+ " return false;\n"
+ "}\n");
+ break;
+ case JAVATYPE_DOUBLE:
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || java.lang.Double.doubleToLongBits($name$_)\n"
+ " != java.lang.Double.doubleToLongBits(other.$name$_)) {\n"
+ " return false;\n"
+ "}\n");
+ break;
+ case JAVATYPE_INT:
+ case JAVATYPE_LONG:
+ case JAVATYPE_BOOLEAN:
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || $name$_ != other.$name$_) {\n"
+ " return false;\n"
+ "}\n");
+ break;
+ case JAVATYPE_STRING:
+ // Accessor style would guarantee $name$_ non-null
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || !$name$_.equals(other.$name$_)) {\n"
+ " return false;\n"
+ "}\n");
+ break;
+ case JAVATYPE_BYTES:
+ // Accessor style would guarantee $name$_ non-null
+ printer->Print(variables_,
+ "if ($different_has$\n"
+ " || !java.util.Arrays.equals($name$_, other.$name$_)) {\n"
+ " return false;\n"
+ "}\n");
+ break;
+ default:
+ GOOGLE_LOG(ERROR) << "unknown java type for primitive field";
+ break;
+ }
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ switch (GetJavaType(descriptor_)) {
+ // For all Java primitive types below, the hash codes match the
+ // results of BoxedType.valueOf(primitiveValue).hashCode().
+ case JAVATYPE_INT:
+ printer->Print(variables_,
+ "result = 31 * result + $name$_;\n");
+ break;
+ case JAVATYPE_LONG:
+ printer->Print(variables_,
+ "result = 31 * result + (int) ($name$_ ^ ($name$_ >>> 32));\n");
+ break;
+ case JAVATYPE_FLOAT:
+ printer->Print(variables_,
+ "result = 31 * result +\n"
+ " java.lang.Float.floatToIntBits($name$_);\n");
+ break;
+ case JAVATYPE_DOUBLE:
+ printer->Print(variables_,
+ "{\n"
+ " long v = java.lang.Double.doubleToLongBits($name$_);\n"
+ " result = 31 * result + (int) (v ^ (v >>> 32));\n"
+ "}\n");
+ break;
+ case JAVATYPE_BOOLEAN:
+ printer->Print(variables_,
+ "result = 31 * result + ($name$_ ? 1231 : 1237);\n");
+ break;
+ case JAVATYPE_STRING:
+ // Accessor style would guarantee $name$_ non-null
+ printer->Print(variables_,
+ "result = 31 * result + $name$_.hashCode();\n");
+ break;
+ case JAVATYPE_BYTES:
+ // Accessor style would guarantee $name$_ non-null
+ printer->Print(variables_,
+ "result = 31 * result + java.util.Arrays.hashCode($name$_);\n");
+ break;
+ default:
+ GOOGLE_LOG(ERROR) << "unknown java type for primitive field";
+ break;
+ }
+}
+
+// ===================================================================
+
+RepeatedPrimitiveFieldGenerator::
+RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
+ : FieldGenerator(params), descriptor_(descriptor) {
+ SetPrimitiveVariables(descriptor, params, &variables_);
+}
+
+RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateMembers(io::Printer* printer, bool /*unused init_defaults*/) const {
+ printer->Print(variables_,
+ "public $type$[] $name$;\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateClearCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "$name$ = $default$;\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateMergingCode(io::Printer* printer) const {
+ // First, figure out the length of the array, then parse.
+ printer->Print(variables_,
+ "int arrayLength = com.google.protobuf.nano.WireFormatNano\n"
+ " .getRepeatedFieldArrayLength(input, $non_packed_tag$);\n"
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n");
+
+ if (GetJavaType(descriptor_) == JAVATYPE_BYTES) {
+ printer->Print(variables_,
+ "byte[][] newArray = new byte[i + arrayLength][];\n");
+ } else {
+ printer->Print(variables_,
+ "$type$[] newArray = new $type$[i + arrayLength];\n");
+ }
+ printer->Print(variables_,
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length - 1; i++) {\n"
+ " newArray[i] = input.read$capitalized_type$();\n"
+ " input.readTag();\n"
+ "}\n"
+ "// Last one without readTag.\n"
+ "newArray[i] = input.read$capitalized_type$();\n"
+ "this.$name$ = newArray;\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateMergingCodeFromPacked(io::Printer* printer) const {
+ printer->Print(
+ "int length = input.readRawVarint32();\n"
+ "int limit = input.pushLimit(length);\n");
+
+ // If we know the elements will all be of the same size, the arrayLength
+ // can be calculated much more easily. However, FixedSize() returns 1 for
+ // repeated bool fields, which are guaranteed to have the fixed size of
+ // 1 byte per value only if we control the output. On the wire they can
+ // legally appear as variable-size integers, so we need to use the slow
+ // way for repeated bool fields.
+ if (descriptor_->type() == FieldDescriptor::TYPE_BOOL
+ || FixedSize(descriptor_->type()) == -1) {
+ printer->Print(variables_,
+ "// First pass to compute array length.\n"
+ "int arrayLength = 0;\n"
+ "int startPos = input.getPosition();\n"
+ "while (input.getBytesUntilLimit() > 0) {\n"
+ " input.read$capitalized_type$();\n"
+ " arrayLength++;\n"
+ "}\n"
+ "input.rewindToPosition(startPos);\n");
+ } else {
+ printer->Print(variables_,
+ "int arrayLength = length / $fixed_size$;\n");
+ }
+
+ printer->Print(variables_,
+ "int i = this.$name$ == null ? 0 : this.$name$.length;\n"
+ "$type$[] newArray = new $type$[i + arrayLength];\n"
+ "if (i != 0) {\n"
+ " java.lang.System.arraycopy(this.$name$, 0, newArray, 0, i);\n"
+ "}\n"
+ "for (; i < newArray.length; i++) {\n"
+ " newArray[i] = input.read$capitalized_type$();\n"
+ "}\n"
+ "this.$name$ = newArray;\n"
+ "input.popLimit(limit);\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateRepeatedDataSizeCode(io::Printer* printer) const {
+ // Creates a variable dataSize and puts the serialized size in there.
+ // If the element type is a Java reference type, also generates
+ // dataCount which stores the number of non-null elements in the field.
+ if (IsReferenceType(GetJavaType(descriptor_))) {
+ printer->Print(variables_,
+ "int dataCount = 0;\n"
+ "int dataSize = 0;\n"
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " $type$ element = this.$name$[i];\n"
+ " if (element != null) {\n"
+ " dataCount++;\n"
+ " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$capitalized_type$SizeNoTag(element);\n"
+ " }\n"
+ "}\n");
+ } else if (FixedSize(descriptor_->type()) == -1) {
+ printer->Print(variables_,
+ "int dataSize = 0;\n"
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " $type$ element = this.$name$[i];\n"
+ " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .compute$capitalized_type$SizeNoTag(element);\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "int dataSize = $fixed_size$ * this.$name$.length;\n");
+ }
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateSerializationCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n");
+ printer->Indent();
+
+ if (descriptor_->is_packable() && descriptor_->options().packed()) {
+ GenerateRepeatedDataSizeCode(printer);
+ printer->Print(variables_,
+ "output.writeRawVarint32($tag$);\n"
+ "output.writeRawVarint32(dataSize);\n"
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " output.write$capitalized_type$NoTag(this.$name$[i]);\n"
+ "}\n");
+ } else if (IsReferenceType(GetJavaType(descriptor_))) {
+ printer->Print(variables_,
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " $type$ element = this.$name$[i];\n"
+ " if (element != null) {\n"
+ " output.write$capitalized_type$($number$, element);\n"
+ " }\n"
+ "}\n");
+ } else {
+ printer->Print(variables_,
+ "for (int i = 0; i < this.$name$.length; i++) {\n"
+ " output.write$capitalized_type$($number$, this.$name$[i]);\n"
+ "}\n");
+ }
+
+ printer->Outdent();
+ printer->Print("}\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateSerializedSizeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (this.$name$ != null && this.$name$.length > 0) {\n");
+ printer->Indent();
+
+ GenerateRepeatedDataSizeCode(printer);
+
+ printer->Print(
+ "size += dataSize;\n");
+ if (descriptor_->is_packable() && descriptor_->options().packed()) {
+ printer->Print(variables_,
+ "size += $tag_size$;\n"
+ "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
+ " .computeRawVarint32Size(dataSize);\n");
+ } else if (IsReferenceType(GetJavaType(descriptor_))) {
+ printer->Print(variables_,
+ "size += $tag_size$ * dataCount;\n");
+ } else {
+ printer->Print(variables_,
+ "size += $tag_size$ * this.$name$.length;\n");
+ }
+
+ printer->Outdent();
+
+ printer->Print(
+ "}\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateEqualsCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "if (!com.google.protobuf.nano.InternalNano.equals(\n"
+ " this.$name$, other.$name$)) {\n"
+ " return false;\n"
+ "}\n");
+}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateHashCodeCode(io::Printer* printer) const {
+ printer->Print(variables_,
+ "result = 31 * result\n"
+ " + com.google.protobuf.nano.InternalNano.hashCode(this.$name$);\n");
+}
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h
new file mode 100644
index 00000000..c04a19b7
--- /dev/null
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h
@@ -0,0 +1,126 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://code.google.com/p/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.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+
+#ifndef GOOGLE_PROTOBUF_COMPILER_JAVANANO_PRIMITIVE_FIELD_H__
+#define GOOGLE_PROTOBUF_COMPILER_JAVANANO_PRIMITIVE_FIELD_H__
+
+#include <map>
+#include <string>
+#include <google/protobuf/compiler/javanano/javanano_field.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace javanano {
+
+class PrimitiveFieldGenerator : public FieldGenerator {
+ public:
+ explicit PrimitiveFieldGenerator(
+ const FieldDescriptor* descriptor, const Params &params);
+ ~PrimitiveFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ bool SavedDefaultNeeded() const;
+ void GenerateInitSavedDefaultCode(io::Printer* printer) const;
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ void GenerateSerializationConditional(io::Printer* printer) const;
+
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator);
+};
+
+class AccessorPrimitiveFieldGenerator : public FieldGenerator {
+ public:
+ explicit AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
+ const Params &params, int has_bit_index);
+ ~AccessorPrimitiveFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ bool SavedDefaultNeeded() const;
+ void GenerateInitSavedDefaultCode(io::Printer* printer) const;
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AccessorPrimitiveFieldGenerator);
+};
+
+class RepeatedPrimitiveFieldGenerator : public FieldGenerator {
+ public:
+ explicit RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params);
+ ~RepeatedPrimitiveFieldGenerator();
+
+ // implements FieldGenerator ---------------------------------------
+ void GenerateMembers(io::Printer* printer, bool lazy_init) const;
+ void GenerateClearCode(io::Printer* printer) const;
+ void GenerateMergingCode(io::Printer* printer) const;
+ void GenerateMergingCodeFromPacked(io::Printer* printer) const;
+ void GenerateSerializationCode(io::Printer* printer) const;
+ void GenerateSerializedSizeCode(io::Printer* printer) const;
+ void GenerateEqualsCode(io::Printer* printer) const;
+ void GenerateHashCodeCode(io::Printer* printer) const;
+
+ private:
+ void GenerateRepeatedDataSizeCode(io::Printer* printer) const;
+
+ const FieldDescriptor* descriptor_;
+ map<string, string> variables_;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator);
+};
+
+} // namespace javanano
+} // namespace compiler
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_COMPILER_JAVANANO_PRIMITIVE_FIELD_H__
diff --git a/src/google/protobuf/compiler/main.cc b/src/google/protobuf/compiler/main.cc
index 2f5bdaf7..931b8fa3 100644
--- a/src/google/protobuf/compiler/main.cc
+++ b/src/google/protobuf/compiler/main.cc
@@ -34,7 +34,8 @@
#include <google/protobuf/compiler/cpp/cpp_generator.h>
#include <google/protobuf/compiler/python/python_generator.h>
#include <google/protobuf/compiler/java/java_generator.h>
-
+#include <google/protobuf/compiler/javanano/javanano_generator.h>
+#include <google/protobuf/compiler/ruby/ruby_generator.h>
int main(int argc, char* argv[]) {
@@ -57,5 +58,15 @@ int main(int argc, char* argv[]) {
cli.RegisterGenerator("--python_out", &py_generator,
"Generate Python source file.");
+ // Java Nano
+ google::protobuf::compiler::javanano::JavaNanoGenerator javanano_generator;
+ cli.RegisterGenerator("--javanano_out", &javanano_generator,
+ "Generate Java Nano source file.");
+
+ // Ruby
+ google::protobuf::compiler::ruby::Generator rb_generator;
+ cli.RegisterGenerator("--ruby_out", &rb_generator,
+ "Generate Ruby source file.");
+
return cli.Run(argc, argv);
}
diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc
index 916b0ccd..e7d5117e 100644
--- a/src/google/protobuf/compiler/mock_code_generator.cc
+++ b/src/google/protobuf/compiler/mock_code_generator.cc
@@ -33,6 +33,9 @@
#include <google/protobuf/compiler/mock_code_generator.h>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/testing/file.h>
#include <google/protobuf/descriptor.pb.h>
@@ -157,7 +160,7 @@ bool MockCodeGenerator::Generate(
for (int i = 0; i < insert_into.size(); i++) {
{
- scoped_ptr<io::ZeroCopyOutputStream> output(context->OpenForInsert(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->OpenForInsert(
GetOutputFileName(insert_into[i], file), kFirstInsertionPointName));
io::Printer printer(output.get(), '$');
printer.PrintRaw(GetOutputFileContent(name_, "first_insert",
@@ -169,7 +172,7 @@ bool MockCodeGenerator::Generate(
}
{
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->OpenForInsert(GetOutputFileName(insert_into[i], file),
kSecondInsertionPointName));
io::Printer printer(output.get(), '$');
@@ -182,7 +185,7 @@ bool MockCodeGenerator::Generate(
}
}
} else {
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->Open(GetOutputFileName(name_, file)));
io::Printer printer(output.get(), '$');
diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc
index 474a8f80..fe697acf 100644
--- a/src/google/protobuf/compiler/parser.cc
+++ b/src/google/protobuf/compiler/parser.cc
@@ -84,6 +84,32 @@ TypeNameMap MakeTypeNameTable() {
const TypeNameMap kTypeNames = MakeTypeNameTable();
+// Camel-case the field name and append "Entry" for generated map entry name.
+// e.g. map<KeyType, ValueType> foo_map => FooMapEntry
+string MapEntryName(const string& field_name) {
+ string result;
+ static const char kSuffix[] = "Entry";
+ result.reserve(field_name.size() + sizeof(kSuffix));
+ bool cap_next = true;
+ for (int i = 0; i < field_name.size(); ++i) {
+ if (field_name[i] == '_') {
+ cap_next = true;
+ } else if (cap_next) {
+ // Note: Do not use ctype.h due to locales.
+ if ('a' <= field_name[i] && field_name[i] <= 'z') {
+ result.push_back(field_name[i] - 'a' + 'A');
+ } else {
+ result.push_back(field_name[i]);
+ }
+ cap_next = false;
+ } else {
+ result.push_back(field_name[i]);
+ }
+ }
+ result.append(kSuffix);
+ return result;
+}
+
} // anonymous namespace
// Makes code slightly more readable. The meaning of "DO(foo)" is
@@ -439,7 +465,13 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) {
// identifier.
return false;
}
+ // 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. "
+ << "Please use 'syntax = \"proto2\";' or "
+ << "'syntax = \"proto3\";' to specify a syntax "
+ << "version. (Defaulted to proto2 syntax.)";
syntax_identifier_ = "proto2";
}
@@ -467,7 +499,9 @@ bool Parser::Parse(io::Tokenizer* input, FileDescriptorProto* file) {
}
bool Parser::ParseSyntaxIdentifier() {
- DO(Consume("syntax", "File must begin with 'syntax = \"proto2\";'."));
+ DO(Consume(
+ "syntax",
+ "File must begin with a syntax statement, e.g. 'syntax = \"proto2\";'."));
DO(Consume("="));
io::Tokenizer::Token syntax_token = input_->current();
string syntax;
@@ -476,10 +510,11 @@ bool Parser::ParseSyntaxIdentifier() {
syntax_identifier_ = syntax;
- if (syntax != "proto2" && !stop_after_syntax_identifier_) {
+ 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\".");
+ "only recognizes \"proto2\" and \"proto3\".");
return false;
}
@@ -673,8 +708,9 @@ bool Parser::ParseMessageField(FieldDescriptorProto* field,
LocationRecorder location(field_location,
FieldDescriptorProto::kLabelFieldNumber);
FieldDescriptorProto::Label label;
- DO(ParseLabel(&label, containing_file));
- field->set_label(label);
+ if (ParseLabel(&label, containing_file)) {
+ field->set_label(label);
+ }
}
return ParseMessageFieldNoLabel(field, messages, parent_location,
@@ -690,20 +726,75 @@ bool Parser::ParseMessageFieldNoLabel(
int location_field_number_for_nested_type,
const LocationRecorder& field_location,
const FileDescriptorProto* containing_file) {
+ MapField map_field;
// Parse type.
{
LocationRecorder location(field_location); // add path later
location.RecordLegacyLocation(field, DescriptorPool::ErrorCollector::TYPE);
+ bool type_parsed = false;
FieldDescriptorProto::Type type = FieldDescriptorProto::TYPE_INT32;
string type_name;
- DO(ParseType(&type, &type_name));
- if (type_name.empty()) {
- location.AddPath(FieldDescriptorProto::kTypeFieldNumber);
- field->set_type(type);
- } else {
+
+ // 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 "<".
+ if (TryConsume("map")) {
+ if (LookingAt("<")) {
+ map_field.is_map_field = true;
+ } else {
+ // False positive
+ type_parsed = true;
+ type_name = "map";
+ }
+ }
+ if (map_field.is_map_field) {
+ if (field->has_oneof_index()) {
+ AddError("Map fields are not allowed in oneofs.");
+ return false;
+ }
+ if (field->has_label()) {
+ AddError(
+ "Field labels (required/optional/repeated) are not allowed on "
+ "map fields.");
+ return false;
+ }
+ if (field->has_extendee()) {
+ AddError("Map fields are not allowed to be extensions.");
+ return false;
+ }
+ field->set_label(FieldDescriptorProto::LABEL_REPEATED);
+ DO(Consume("<"));
+ DO(ParseType(&map_field.key_type, &map_field.key_type_name));
+ DO(Consume(","));
+ DO(ParseType(&map_field.value_type, &map_field.value_type_name));
+ DO(Consume(">"));
+ // Defer setting of the type name of the map field until the
+ // field name is parsed. Add the source location though.
location.AddPath(FieldDescriptorProto::kTypeNameFieldNumber);
- field->set_type_name(type_name);
+ } else {
+ // Handle the case where no explicit label is given for a non-map field.
+ if (!field->has_label() && DefaultToOptionalFields()) {
+ field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
+ }
+ if (!field->has_label()) {
+ AddError("Expected \"required\", \"optional\", or \"repeated\".");
+ // We can actually reasonably recover here by just assuming the user
+ // forgot the label altogether.
+ field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
+ }
+
+ // Handle the case where the actual type is a message or enum named "map",
+ // which we already consumed in the code above.
+ if (!type_parsed) {
+ DO(ParseType(&type, &type_name));
+ }
+ if (type_name.empty()) {
+ location.AddPath(FieldDescriptorProto::kTypeFieldNumber);
+ field->set_type(type);
+ } else {
+ location.AddPath(FieldDescriptorProto::kTypeNameFieldNumber);
+ field->set_type_name(type_name);
+ }
}
}
@@ -781,9 +872,42 @@ bool Parser::ParseMessageFieldNoLabel(
DO(ConsumeEndOfDeclaration(";", &field_location));
}
+ // Create a map entry type if this is a map field.
+ if (map_field.is_map_field) {
+ GenerateMapEntry(map_field, field, messages);
+ }
+
return true;
}
+void Parser::GenerateMapEntry(const MapField& map_field,
+ FieldDescriptorProto* field,
+ RepeatedPtrField<DescriptorProto>* messages) {
+ DescriptorProto* entry = messages->Add();
+ string entry_name = MapEntryName(field->name());
+ field->set_type_name(entry_name);
+ entry->set_name(entry_name);
+ entry->mutable_options()->set_map_entry(true);
+ FieldDescriptorProto* key_field = entry->add_field();
+ key_field->set_name("key");
+ key_field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
+ key_field->set_number(1);
+ if (map_field.key_type_name.empty()) {
+ key_field->set_type(map_field.key_type);
+ } else {
+ key_field->set_type_name(map_field.key_type_name);
+ }
+ FieldDescriptorProto* value_field = entry->add_field();
+ value_field->set_name("value");
+ value_field->set_label(FieldDescriptorProto::LABEL_OPTIONAL);
+ value_field->set_number(2);
+ if (map_field.value_type_name.empty()) {
+ value_field->set_type(map_field.value_type);
+ } else {
+ value_field->set_type_name(map_field.value_type_name);
+ }
+}
+
bool Parser::ParseFieldOptions(FieldDescriptorProto* field,
const LocationRecorder& field_location,
const FileDescriptorProto* containing_file) {
@@ -1512,6 +1636,9 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method,
// Parse input type.
DO(Consume("("));
{
+ if (TryConsume("stream")) {
+ method->set_client_streaming(true);
+ }
LocationRecorder location(method_location,
MethodDescriptorProto::kInputTypeFieldNumber);
location.RecordLegacyLocation(
@@ -1524,6 +1651,9 @@ bool Parser::ParseServiceMethod(MethodDescriptorProto* method,
DO(Consume("returns"));
DO(Consume("("));
{
+ if (TryConsume("stream")) {
+ method->set_server_streaming(true);
+ }
LocationRecorder location(method_location,
MethodDescriptorProto::kOutputTypeFieldNumber);
location.RecordLegacyLocation(
@@ -1588,13 +1718,8 @@ bool Parser::ParseLabel(FieldDescriptorProto::Label* label,
} else if (TryConsume("required")) {
*label = FieldDescriptorProto::LABEL_REQUIRED;
return true;
- } else {
- AddError("Expected \"required\", \"optional\", or \"repeated\".");
- // We can actually reasonably recover here by just assuming the user
- // forgot the label altogether.
- *label = FieldDescriptorProto::LABEL_OPTIONAL;
- return true;
}
+ return false;
}
bool Parser::ParseType(FieldDescriptorProto::Type* type,
diff --git a/src/google/protobuf/compiler/parser.h b/src/google/protobuf/compiler/parser.h
index d0a2359b..a15cc705 100644
--- a/src/google/protobuf/compiler/parser.h
+++ b/src/google/protobuf/compiler/parser.h
@@ -403,7 +403,7 @@ class LIBPROTOBUF_EXPORT Parser {
Message* mutable_options);
// Parse "required", "optional", or "repeated" and fill in "label"
- // with the value.
+ // with the value. Returns true if shuch a label is consumed.
bool ParseLabel(FieldDescriptorProto::Label* label,
const FileDescriptorProto* containing_file);
@@ -460,6 +460,28 @@ class LIBPROTOBUF_EXPORT Parser {
// the ending brace.
bool ParseUninterpretedBlock(string* value);
+ struct MapField {
+ // Whether the field is a map field.
+ bool is_map_field;
+ // The types of the key and value if they are primitive types.
+ 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;
+
+ MapField() : is_map_field(false) {}
+ };
+ // Desugar the map syntax to generate a nested map entry message.
+ void GenerateMapEntry(const MapField& map_field, FieldDescriptorProto* field,
+ RepeatedPtrField<DescriptorProto>* messages);
+
+ // Whether fields without label default to optional fields.
+ bool DefaultToOptionalFields() const {
+ return syntax_identifier_ == "proto3";
+ }
+
+
// =================================================================
io::Tokenizer* input_;
diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc
index 9ec29a48..638a83b9 100644
--- a/src/google/protobuf/compiler/parser_unittest.cc
+++ b/src/google/protobuf/compiler/parser_unittest.cc
@@ -33,6 +33,9 @@
// Sanjay Ghemawat, Jeff Dean, and others.
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <vector>
#include <algorithm>
#include <map>
@@ -175,9 +178,9 @@ class ParserTest : public testing::Test {
MockErrorCollector error_collector_;
DescriptorPool pool_;
- scoped_ptr<io::ZeroCopyInputStream> raw_input_;
- scoped_ptr<io::Tokenizer> input_;
- scoped_ptr<Parser> parser_;
+ google::protobuf::scoped_ptr<io::ZeroCopyInputStream> raw_input_;
+ google::protobuf::scoped_ptr<io::Tokenizer> input_;
+ google::protobuf::scoped_ptr<Parser> parser_;
bool require_syntax_identifier_;
};
@@ -213,6 +216,15 @@ TEST_F(ParserTest, StopAfterSyntaxIdentifierWithErrors) {
EXPECT_EQ("1:9: Expected syntax identifier.\n", error_collector_.text_);
}
+TEST_F(ParserTest, WarnIfSyntaxIdentifierOmmitted) {
+ SetupParser("message A {}");
+ FileDescriptorProto file;
+ CaptureTestStderr();
+ EXPECT_TRUE(parser_->Parse(input_.get(), &file));
+ EXPECT_TRUE(
+ GetCapturedTestStderr().find("No syntax specified") != string::npos);
+}
+
// ===================================================================
typedef ParserTest ParseMessageTest;
@@ -250,6 +262,7 @@ TEST_F(ParseMessageTest, ExplicitSyntaxIdentifier) {
" required int32 foo = 1;\n"
"}\n",
+ "syntax: 'proto2' "
"message_type {"
" name: \"TestMessage\""
" field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }"
@@ -265,6 +278,7 @@ TEST_F(ParseMessageTest, ExplicitRequiredSyntaxIdentifier) {
" required int32 foo = 1;\n"
"}\n",
+ "syntax: 'proto2' "
"message_type {"
" name: \"TestMessage\""
" field { name:\"foo\" label:LABEL_REQUIRED type:TYPE_INT32 number:1 }"
@@ -365,35 +379,35 @@ TEST_F(ParseMessageTest, FieldDefaults) {
#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" }"
+ " 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
}
@@ -500,6 +514,54 @@ TEST_F(ParseMessageTest, MultipleOneofs) {
"}");
}
+TEST_F(ParseMessageTest, Maps) {
+ ExpectParsesTo(
+ "message TestMessage {\n"
+ " map<int32, string> primitive_type_map = 1;\n"
+ " map<KeyType, ValueType> 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"
@@ -639,6 +701,20 @@ TEST_F(ParseMessageTest, MultipleExtensionsOneExtendee) {
" type_name:\"TestMessage\" extendee: \"Extendee1\" }");
}
+TEST_F(ParseMessageTest, OptionalOptionalLabelProto3) {
+ ExpectParsesTo(
+ "syntax = \"proto3\";\n"
+ "message TestMessage {\n"
+ " int32 foo = 1;\n"
+ " optional int32 bar = 2;\n"
+ "}\n",
+
+ "syntax: \"proto3\" "
+ "message_type {"
+ " name: \"TestMessage\""
+ " field { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:1 }"
+ " field { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:2 } }");
+}
// ===================================================================
@@ -828,9 +904,9 @@ typedef ParserTest ParseErrorTest;
TEST_F(ParseErrorTest, MissingSyntaxIdentifier) {
require_syntax_identifier_ = true;
- ExpectHasEarlyExitErrors(
- "message TestMessage {}",
- "0:0: File must begin with 'syntax = \"proto2\";'.\n");
+ ExpectHasEarlyExitErrors("message TestMessage {}",
+ "0:0: File must begin with a syntax statement, e.g. "
+ "'syntax = \"proto2\";'.\n");
EXPECT_EQ("", parser_->GetSyntaxIdentifier());
}
@@ -838,7 +914,7 @@ TEST_F(ParseErrorTest, UnknownSyntaxIdentifier) {
ExpectHasEarlyExitErrors(
"syntax = \"no_such_syntax\";",
"0:9: Unrecognized syntax identifier \"no_such_syntax\". This parser "
- "only recognizes \"proto2\".\n");
+ "only recognizes \"proto2\" and \"proto3\".\n");
EXPECT_EQ("no_such_syntax", parser_->GetSyntaxIdentifier());
}
@@ -1041,6 +1117,55 @@ TEST_F(ParseErrorTest, LabelInOneof) {
"/ repeated).\n");
}
+TEST_F(ParseErrorTest, MapInOneof) {
+ ExpectHasErrors(
+ "message TestMessage {\n"
+ " oneof foo {\n"
+ " map<int32, int32> 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<int32, int32> int_map = 1;\n"
+ " required map<int32, int32> int_map2 = 2;\n"
+ " repeated map<int32, int32> 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<string> str_map = 2;\n"
+ " map<string,> str_map2 = 3;\n"
+ " map<,string> str_map3 = 4;\n"
+ " map<> empty_map = 5;\n"
+ " map<string,string str_map6 = 6;\n"
+ "}"
+ "extend SomeMessage {\n"
+ " map<int32, int32> 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"
@@ -1413,7 +1538,7 @@ TEST_F(ParserValidationErrorTest, ResovledUndefinedOptionError) {
// definitions again afoter parsing (note, however, that the order of messages
// cannot be guaranteed to be the same)
-typedef ParserTest ParseDecriptorDebugTest;
+typedef ParserTest ParseDescriptorDebugTest;
class CompareDescriptorNames {
public:
@@ -1447,7 +1572,28 @@ void SortMessages(FileDescriptorProto *file_descriptor_proto) {
sort(data, data + size, CompareDescriptorNames());
}
-TEST_F(ParseDecriptorDebugTest, TestAllDescriptorTypes) {
+// 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) {
+ proto->mutable_field(i)->mutable_type_name()->assign(
+ type_name.begin() + pos + 1, type_name.end());
+ }
+ }
+ for (int i = 0; i < proto->nested_type_size(); ++i) {
+ StripFieldTypeName(proto->mutable_nested_type(i));
+ }
+}
+
+void StripFieldTypeName(FileDescriptorProto* file_proto) {
+ for (int i = 0; i < file_proto->message_type_size(); ++i) {
+ StripFieldTypeName(file_proto->mutable_message_type(i));
+ }
+}
+
+TEST_F(ParseDescriptorDebugTest, TestAllDescriptorTypes) {
const FileDescriptor* original_file =
protobuf_unittest::TestAllTypes::descriptor()->file();
FileDescriptorProto expected;
@@ -1499,7 +1645,7 @@ TEST_F(ParseDecriptorDebugTest, TestAllDescriptorTypes) {
EXPECT_EQ(expected.DebugString(), parsed.DebugString());
}
-TEST_F(ParseDecriptorDebugTest, TestCustomOptions) {
+TEST_F(ParseDescriptorDebugTest, TestCustomOptions) {
const FileDescriptor* original_file =
protobuf_unittest::AggregateMessage::descriptor()->file();
FileDescriptorProto expected;
@@ -1538,6 +1684,106 @@ TEST_F(ParseDecriptorDebugTest, TestCustomOptions) {
EXPECT_EQ(expected.DebugString(), parsed.DebugString());
}
+// Ensure that DebugStringWithOptions(), with |include_comments| set to true,
+// includes comments from the original parser input in all of the appropriate
+// places.
+TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) {
+ SetupParser(
+ "// Message comment.\n"
+ "message TestMessage1 {\n"
+ " // Field comment.\n"
+ " optional int32 foo = 1;\n"
+ "\n"
+ " // Nested-message comment.\n"
+ " message NestedMessage {\n"
+ " optional int32 bar = 1;\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "// Enum comment.\n"
+ "enum MyEnumType {\n"
+ " // Enum-value comment.\n"
+ " ASDF = 1;\n"
+ "}\n"
+ "\n"
+ "// Service comment.\n"
+ "service MyService {\n"
+ " // RPC comment.\n"
+ " rpc MyRPCCall(TestMessage1) returns (TestMessage1) { }\n"
+ "}\n");
+
+ FileDescriptorProto parsed_desc;
+ parsed_desc.set_name("foo.proto");
+ SourceLocationTable source_locations;
+ parser_->RecordSourceLocationsTo(&source_locations);
+ parser_->Parse(input_.get(), &parsed_desc);
+ EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type);
+ ASSERT_EQ("", error_collector_.text_);
+
+ // We need to import the FileDescriptorProto to get a FileDescriptor.
+ MockValidationErrorCollector collector(source_locations, &error_collector_);
+ const FileDescriptor* descriptor =
+ pool_.BuildFileCollectingErrors(parsed_desc, &collector);
+ ASSERT_TRUE(descriptor != NULL);
+
+ DebugStringOptions debug_string_options;
+ debug_string_options.include_comments = true;
+ const string debug_string =
+ descriptor->DebugStringWithOptions(debug_string_options);
+
+ // Ensure that each of the comments appears somewhere in the DebugString(),
+ // and that these comments appear in order. We don't test the exact comment
+ // placement or formatting, because we do not want to be too fragile here.
+ const char* expected_comments[] = {
+ "Message comment.",
+ "Field comment",
+ "Nested-message comment",
+ "Enum comment",
+ "Enum-value comment",
+ "Service comment",
+ "RPC comment",
+ };
+
+ for (int i = 0; i < GOOGLE_ARRAYSIZE(expected_comments); ++i) {
+ string::size_type found_pos = debug_string.find(expected_comments[i]);
+ ASSERT_TRUE(found_pos != string::npos);
+ }
+}
+
+TEST_F(ParseDescriptorDebugTest, TestMaps) {
+ SetupParser(
+ "syntax = \"proto3\"; "
+ "message Foo { "
+ " message Bar { } "
+ " map<int32, Bar> enum_message_map = 1; "
+ " map<string, float> primitive_map = 2; "
+ "} ");
+ FileDescriptorProto original;
+ EXPECT_TRUE(parser_->Parse(input_.get(), &original));
+ original.set_name("foo.proto");
+ const FileDescriptor* file = pool_.BuildFile(original);
+ ASSERT_TRUE(file != NULL);
+
+ // Make sure the debug string uses map syntax and does not have the auto
+ // generated entry.
+ 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);
+
+ // Make sure the descriptor debug string is parsable.
+ FileDescriptorProto parsed;
+ SetupParser(debug_string.c_str());
+ parsed.set_name("foo.proto");
+ ASSERT_TRUE(parser_->Parse(input_.get(), &parsed));
+
+ original.clear_source_code_info();
+ parsed.clear_source_code_info();
+ StripFieldTypeName(&original);
+ StripFieldTypeName(&parsed);
+ EXPECT_EQ(original.DebugString(), parsed.DebugString());
+}
+
// ===================================================================
// SourceCodeInfo tests.
diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc
index b5cd01b5..5118de15 100644
--- a/src/google/protobuf/compiler/plugin.pb.cc
+++ b/src/google/protobuf/compiler/plugin.pb.cc
@@ -48,32 +48,32 @@ void protobuf_AssignDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorRequest, proto_file_),
};
CodeGeneratorRequest_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
CodeGeneratorRequest_descriptor_,
CodeGeneratorRequest::default_instance_,
CodeGeneratorRequest_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorRequest, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorRequest, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(CodeGeneratorRequest));
+ -1,
+ sizeof(CodeGeneratorRequest),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorRequest, _internal_metadata_),
+ -1);
CodeGeneratorResponse_descriptor_ = file->message_type(1);
static const int CodeGeneratorResponse_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse, error_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse, file_),
};
CodeGeneratorResponse_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
CodeGeneratorResponse_descriptor_,
CodeGeneratorResponse::default_instance_,
CodeGeneratorResponse_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(CodeGeneratorResponse));
+ -1,
+ sizeof(CodeGeneratorResponse),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse, _internal_metadata_),
+ -1);
CodeGeneratorResponse_File_descriptor_ = CodeGeneratorResponse_descriptor_->nested_type(0);
static const int CodeGeneratorResponse_File_offsets_[3] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse_File, name_),
@@ -81,16 +81,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse_File, content_),
};
CodeGeneratorResponse_File_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
CodeGeneratorResponse_File_descriptor_,
CodeGeneratorResponse_File::default_instance_,
CodeGeneratorResponse_File_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse_File, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse_File, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(CodeGeneratorResponse_File));
+ -1,
+ sizeof(CodeGeneratorResponse_File),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(CodeGeneratorResponse_File, _internal_metadata_),
+ -1);
}
namespace {
@@ -104,11 +104,11 @@ inline void protobuf_AssignDescriptorsOnce() {
void protobuf_RegisterTypes(const ::std::string&) {
protobuf_AssignDescriptorsOnce();
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- CodeGeneratorRequest_descriptor_, &CodeGeneratorRequest::default_instance());
+ CodeGeneratorRequest_descriptor_, &CodeGeneratorRequest::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- CodeGeneratorResponse_descriptor_, &CodeGeneratorResponse::default_instance());
+ CodeGeneratorResponse_descriptor_, &CodeGeneratorResponse::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- CodeGeneratorResponse_File_descriptor_, &CodeGeneratorResponse_File::default_instance());
+ CodeGeneratorResponse_File_descriptor_, &CodeGeneratorResponse_File::default_instance());
}
} // namespace
@@ -160,6 +160,16 @@ struct StaticDescriptorInitializer_google_2fprotobuf_2fcompiler_2fplugin_2eproto
}
} static_descriptor_initializer_google_2fprotobuf_2fcompiler_2fplugin_2eproto_;
+namespace {
+
+static void MergeFromFail(int line) GOOGLE_ATTRIBUTE_COLD;
+static void MergeFromFail(int line) {
+ GOOGLE_CHECK(false) << __FILE__ << ":" << line;
+}
+
+} // namespace
+
+
// ===================================================================
#ifndef _MSC_VER
@@ -169,7 +179,7 @@ const int CodeGeneratorRequest::kProtoFileFieldNumber;
#endif // !_MSC_VER
CodeGeneratorRequest::CodeGeneratorRequest()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorRequest)
}
@@ -178,7 +188,8 @@ void CodeGeneratorRequest::InitAsDefaultInstance() {
}
CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorRequest)
@@ -187,7 +198,7 @@ CodeGeneratorRequest::CodeGeneratorRequest(const CodeGeneratorRequest& from)
void CodeGeneratorRequest::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- parameter_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ parameter_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -197,9 +208,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() {
}
void CodeGeneratorRequest::SharedDtor() {
- if (parameter_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete parameter_;
- }
+ parameter_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -221,20 +230,24 @@ const CodeGeneratorRequest& CodeGeneratorRequest::default_instance() {
CodeGeneratorRequest* CodeGeneratorRequest::default_instance_ = NULL;
-CodeGeneratorRequest* CodeGeneratorRequest::New() const {
- return new CodeGeneratorRequest;
+CodeGeneratorRequest* CodeGeneratorRequest::New(::google::protobuf::Arena* arena) const {
+ CodeGeneratorRequest* n = new CodeGeneratorRequest;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void CodeGeneratorRequest::Clear() {
if (has_parameter()) {
- if (parameter_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_->clear();
- }
+ parameter_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
file_to_generate_.Clear();
proto_file_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool CodeGeneratorRequest::MergePartialFromCodedStream(
@@ -257,7 +270,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream(
this->file_to_generate(this->file_to_generate_size() - 1).data(),
this->file_to_generate(this->file_to_generate_size() - 1).length(),
::google::protobuf::internal::WireFormat::PARSE,
- "file_to_generate");
+ "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate");
} else {
goto handle_unusual;
}
@@ -275,7 +288,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->parameter().data(), this->parameter().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "parameter");
+ "google.protobuf.compiler.CodeGeneratorRequest.parameter");
} else {
goto handle_unusual;
}
@@ -327,7 +340,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->file_to_generate(i).data(), this->file_to_generate(i).length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "file_to_generate");
+ "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate");
::google::protobuf::internal::WireFormatLite::WriteString(
1, this->file_to_generate(i), output);
}
@@ -337,18 +350,18 @@ void CodeGeneratorRequest::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->parameter().data(), this->parameter().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "parameter");
+ "google.protobuf.compiler.CodeGeneratorRequest.parameter");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->parameter(), output);
}
// repeated .google.protobuf.FileDescriptorProto proto_file = 15;
- for (int i = 0; i < this->proto_file_size(); i++) {
+ for (unsigned int i = 0, n = this->proto_file_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
15, this->proto_file(i), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -363,7 +376,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->file_to_generate(i).data(), this->file_to_generate(i).length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "file_to_generate");
+ "google.protobuf.compiler.CodeGeneratorRequest.file_to_generate");
target = ::google::protobuf::internal::WireFormatLite::
WriteStringToArray(1, this->file_to_generate(i), target);
}
@@ -373,20 +386,20 @@ void CodeGeneratorRequest::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->parameter().data(), this->parameter().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "parameter");
+ "google.protobuf.compiler.CodeGeneratorRequest.parameter");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->parameter(), target);
}
// repeated .google.protobuf.FileDescriptorProto proto_file = 15;
- for (int i = 0; i < this->proto_file_size(); i++) {
+ for (unsigned int i = 0, n = this->proto_file_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
15, this->proto_file(i), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -397,15 +410,13 @@ void CodeGeneratorRequest::SerializeWithCachedSizes(
int CodeGeneratorRequest::ByteSize() const {
int total_size = 0;
- if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) {
- // optional string parameter = 2;
- if (has_parameter()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::StringSize(
- this->parameter());
- }
-
+ // optional string parameter = 2;
+ if (has_parameter()) {
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->parameter());
}
+
// repeated string file_to_generate = 1;
total_size += 1 * this->file_to_generate_size();
for (int i = 0; i < this->file_to_generate_size(); i++) {
@@ -421,7 +432,7 @@ int CodeGeneratorRequest::ByteSize() const {
this->proto_file(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -433,7 +444,7 @@ int CodeGeneratorRequest::ByteSize() const {
}
void CodeGeneratorRequest::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const CodeGeneratorRequest* source =
::google::protobuf::internal::dynamic_cast_if_available<const CodeGeneratorRequest*>(
&from);
@@ -445,15 +456,18 @@ void CodeGeneratorRequest::MergeFrom(const ::google::protobuf::Message& from) {
}
void CodeGeneratorRequest::MergeFrom(const CodeGeneratorRequest& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
file_to_generate_.MergeFrom(from.file_to_generate_);
proto_file_.MergeFrom(from.proto_file_);
if (from._has_bits_[1 / 32] & (0xffu << (1 % 32))) {
if (from.has_parameter()) {
- set_parameter(from.parameter());
+ set_has_parameter();
+ parameter_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.parameter_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void CodeGeneratorRequest::CopyFrom(const ::google::protobuf::Message& from) {
@@ -475,14 +489,16 @@ bool CodeGeneratorRequest::IsInitialized() const {
}
void CodeGeneratorRequest::Swap(CodeGeneratorRequest* other) {
- if (other != this) {
- file_to_generate_.Swap(&other->file_to_generate_);
- std::swap(parameter_, other->parameter_);
- proto_file_.Swap(&other->proto_file_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void CodeGeneratorRequest::InternalSwap(CodeGeneratorRequest* other) {
+ file_to_generate_.UnsafeArenaSwap(&other->file_to_generate_);
+ parameter_.Swap(&other->parameter_);
+ proto_file_.UnsafeArenaSwap(&other->proto_file_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata CodeGeneratorRequest::GetMetadata() const {
@@ -503,7 +519,7 @@ const int CodeGeneratorResponse_File::kContentFieldNumber;
#endif // !_MSC_VER
CodeGeneratorResponse_File::CodeGeneratorResponse_File()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorResponse.File)
}
@@ -512,7 +528,8 @@ void CodeGeneratorResponse_File::InitAsDefaultInstance() {
}
CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorResponse_File& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorResponse.File)
@@ -521,9 +538,9 @@ CodeGeneratorResponse_File::CodeGeneratorResponse_File(const CodeGeneratorRespon
void CodeGeneratorResponse_File::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- content_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ insertion_point_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ content_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -533,15 +550,9 @@ CodeGeneratorResponse_File::~CodeGeneratorResponse_File() {
}
void CodeGeneratorResponse_File::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (insertion_point_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete insertion_point_;
- }
- if (content_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete content_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ insertion_point_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ content_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -563,30 +574,30 @@ const CodeGeneratorResponse_File& CodeGeneratorResponse_File::default_instance()
CodeGeneratorResponse_File* CodeGeneratorResponse_File::default_instance_ = NULL;
-CodeGeneratorResponse_File* CodeGeneratorResponse_File::New() const {
- return new CodeGeneratorResponse_File;
+CodeGeneratorResponse_File* CodeGeneratorResponse_File::New(::google::protobuf::Arena* arena) const {
+ CodeGeneratorResponse_File* n = new CodeGeneratorResponse_File;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void CodeGeneratorResponse_File::Clear() {
if (_has_bits_[0 / 32] & 7) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_insertion_point()) {
- if (insertion_point_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_->clear();
- }
+ insertion_point_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_content()) {
- if (content_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_->clear();
- }
+ content_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool CodeGeneratorResponse_File::MergePartialFromCodedStream(
@@ -607,7 +618,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.name");
} else {
goto handle_unusual;
}
@@ -624,7 +635,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->insertion_point().data(), this->insertion_point().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "insertion_point");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point");
} else {
goto handle_unusual;
}
@@ -641,7 +652,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->content().data(), this->content().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "content");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.content");
} else {
goto handle_unusual;
}
@@ -679,7 +690,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
@@ -689,7 +700,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->insertion_point().data(), this->insertion_point().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "insertion_point");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->insertion_point(), output);
}
@@ -699,12 +710,12 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->content().data(), this->content().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "content");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.content");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
15, this->content(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -719,7 +730,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
@@ -730,7 +741,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->insertion_point().data(), this->insertion_point().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "insertion_point");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->insertion_point(), target);
@@ -741,13 +752,13 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->content().data(), this->content().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "content");
+ "google.protobuf.compiler.CodeGeneratorResponse.File.content");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
15, this->content(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -758,7 +769,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes(
int CodeGeneratorResponse_File::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 7) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -781,7 +792,7 @@ int CodeGeneratorResponse_File::ByteSize() const {
}
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -793,7 +804,7 @@ int CodeGeneratorResponse_File::ByteSize() const {
}
void CodeGeneratorResponse_File::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const CodeGeneratorResponse_File* source =
::google::protobuf::internal::dynamic_cast_if_available<const CodeGeneratorResponse_File*>(
&from);
@@ -805,19 +816,24 @@ void CodeGeneratorResponse_File::MergeFrom(const ::google::protobuf::Message& fr
}
void CodeGeneratorResponse_File::MergeFrom(const CodeGeneratorResponse_File& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_insertion_point()) {
- set_insertion_point(from.insertion_point());
+ set_has_insertion_point();
+ insertion_point_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.insertion_point_);
}
if (from.has_content()) {
- set_content(from.content());
+ set_has_content();
+ content_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.content_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void CodeGeneratorResponse_File::CopyFrom(const ::google::protobuf::Message& from) {
@@ -838,14 +854,16 @@ bool CodeGeneratorResponse_File::IsInitialized() const {
}
void CodeGeneratorResponse_File::Swap(CodeGeneratorResponse_File* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(insertion_point_, other->insertion_point_);
- std::swap(content_, other->content_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void CodeGeneratorResponse_File::InternalSwap(CodeGeneratorResponse_File* other) {
+ name_.Swap(&other->name_);
+ insertion_point_.Swap(&other->insertion_point_);
+ content_.Swap(&other->content_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata CodeGeneratorResponse_File::GetMetadata() const {
@@ -865,7 +883,7 @@ const int CodeGeneratorResponse::kFileFieldNumber;
#endif // !_MSC_VER
CodeGeneratorResponse::CodeGeneratorResponse()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.compiler.CodeGeneratorResponse)
}
@@ -874,7 +892,8 @@ void CodeGeneratorResponse::InitAsDefaultInstance() {
}
CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.compiler.CodeGeneratorResponse)
@@ -883,7 +902,7 @@ CodeGeneratorResponse::CodeGeneratorResponse(const CodeGeneratorResponse& from)
void CodeGeneratorResponse::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- error_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ error_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -893,9 +912,7 @@ CodeGeneratorResponse::~CodeGeneratorResponse() {
}
void CodeGeneratorResponse::SharedDtor() {
- if (error_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete error_;
- }
+ error_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -917,19 +934,23 @@ const CodeGeneratorResponse& CodeGeneratorResponse::default_instance() {
CodeGeneratorResponse* CodeGeneratorResponse::default_instance_ = NULL;
-CodeGeneratorResponse* CodeGeneratorResponse::New() const {
- return new CodeGeneratorResponse;
+CodeGeneratorResponse* CodeGeneratorResponse::New(::google::protobuf::Arena* arena) const {
+ CodeGeneratorResponse* n = new CodeGeneratorResponse;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void CodeGeneratorResponse::Clear() {
if (has_error()) {
- if (error_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_->clear();
- }
+ error_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
file_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool CodeGeneratorResponse::MergePartialFromCodedStream(
@@ -950,7 +971,7 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->error().data(), this->error().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "error");
+ "google.protobuf.compiler.CodeGeneratorResponse.error");
} else {
goto handle_unusual;
}
@@ -1002,18 +1023,18 @@ void CodeGeneratorResponse::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->error().data(), this->error().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "error");
+ "google.protobuf.compiler.CodeGeneratorResponse.error");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->error(), output);
}
// repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15;
- for (int i = 0; i < this->file_size(); i++) {
+ for (unsigned int i = 0, n = this->file_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
15, this->file(i), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -1028,20 +1049,20 @@ void CodeGeneratorResponse::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->error().data(), this->error().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "error");
+ "google.protobuf.compiler.CodeGeneratorResponse.error");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->error(), target);
}
// repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15;
- for (int i = 0; i < this->file_size(); i++) {
+ for (unsigned int i = 0, n = this->file_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
15, this->file(i), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -1052,15 +1073,13 @@ void CodeGeneratorResponse::SerializeWithCachedSizes(
int CodeGeneratorResponse::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
- // optional string error = 1;
- if (has_error()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::StringSize(
- this->error());
- }
-
+ // optional string error = 1;
+ if (has_error()) {
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->error());
}
+
// repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15;
total_size += 1 * this->file_size();
for (int i = 0; i < this->file_size(); i++) {
@@ -1069,7 +1088,7 @@ int CodeGeneratorResponse::ByteSize() const {
this->file(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -1081,7 +1100,7 @@ int CodeGeneratorResponse::ByteSize() const {
}
void CodeGeneratorResponse::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const CodeGeneratorResponse* source =
::google::protobuf::internal::dynamic_cast_if_available<const CodeGeneratorResponse*>(
&from);
@@ -1093,14 +1112,17 @@ void CodeGeneratorResponse::MergeFrom(const ::google::protobuf::Message& from) {
}
void CodeGeneratorResponse::MergeFrom(const CodeGeneratorResponse& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
file_.MergeFrom(from.file_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_error()) {
- set_error(from.error());
+ set_has_error();
+ error_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.error_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void CodeGeneratorResponse::CopyFrom(const ::google::protobuf::Message& from) {
@@ -1121,13 +1143,15 @@ bool CodeGeneratorResponse::IsInitialized() const {
}
void CodeGeneratorResponse::Swap(CodeGeneratorResponse* other) {
- if (other != this) {
- std::swap(error_, other->error_);
- file_.Swap(&other->file_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void CodeGeneratorResponse::InternalSwap(CodeGeneratorResponse* other) {
+ error_.Swap(&other->error_);
+ file_.UnsafeArenaSwap(&other->file_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata CodeGeneratorResponse::GetMetadata() const {
diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h
index 815addd6..ad017005 100644
--- a/src/google/protobuf/compiler/plugin.pb.h
+++ b/src/google/protobuf/compiler/plugin.pb.h
@@ -8,18 +8,21 @@
#include <google/protobuf/stubs/common.h>
-#if GOOGLE_PROTOBUF_VERSION < 2006000
+#if GOOGLE_PROTOBUF_VERSION < 3000000
#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 2006000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
+#if 3000000 < GOOGLE_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 <google/protobuf/arena.h>
+#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_util.h>
+#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
@@ -55,11 +58,11 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -69,7 +72,9 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message
// implements Message ----------------------------------------------
- CodeGeneratorRequest* New() const;
+ inline CodeGeneratorRequest* New() const { return New(NULL); }
+
+ CodeGeneratorRequest* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const CodeGeneratorRequest& from);
@@ -88,7 +93,16 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(CodeGeneratorRequest* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -140,12 +154,11 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message
inline void set_has_parameter();
inline void clear_has_parameter();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::std::string> file_to_generate_;
- ::std::string* parameter_;
+ ::google::protobuf::internal::ArenaStringPtr parameter_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto > proto_file_;
friend void LIBPROTOC_EXPORT protobuf_AddDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
@@ -169,11 +182,11 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -183,7 +196,9 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M
// implements Message ----------------------------------------------
- CodeGeneratorResponse_File* New() const;
+ inline CodeGeneratorResponse_File* New() const { return New(NULL); }
+
+ CodeGeneratorResponse_File* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const CodeGeneratorResponse_File& from);
@@ -202,7 +217,16 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(CodeGeneratorResponse_File* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -254,13 +278,12 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M
inline void set_has_content();
inline void clear_has_content();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
- ::std::string* insertion_point_;
- ::std::string* content_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
+ ::google::protobuf::internal::ArenaStringPtr insertion_point_;
+ ::google::protobuf::internal::ArenaStringPtr content_;
friend void LIBPROTOC_EXPORT protobuf_AddDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
@@ -283,11 +306,11 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -297,7 +320,9 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag
// implements Message ----------------------------------------------
- CodeGeneratorResponse* New() const;
+ inline CodeGeneratorResponse* New() const { return New(NULL); }
+
+ CodeGeneratorResponse* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const CodeGeneratorResponse& from);
@@ -316,7 +341,16 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(CodeGeneratorResponse* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -354,11 +388,10 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag
inline void set_has_error();
inline void clear_has_error();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* error_;
+ ::google::protobuf::internal::ArenaStringPtr error_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::compiler::CodeGeneratorResponse_File > file_;
friend void LIBPROTOC_EXPORT protobuf_AddDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fcompiler_2fplugin_2eproto();
@@ -439,68 +472,45 @@ inline void CodeGeneratorRequest::clear_has_parameter() {
_has_bits_[0] &= ~0x00000002u;
}
inline void CodeGeneratorRequest::clear_parameter() {
- if (parameter_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_->clear();
- }
+ parameter_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_parameter();
}
inline const ::std::string& CodeGeneratorRequest::parameter() const {
// @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.parameter)
- return *parameter_;
+ return parameter_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorRequest::set_parameter(const ::std::string& value) {
set_has_parameter();
- if (parameter_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_ = new ::std::string;
- }
- parameter_->assign(value);
+ parameter_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.parameter)
}
inline void CodeGeneratorRequest::set_parameter(const char* value) {
set_has_parameter();
- if (parameter_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_ = new ::std::string;
- }
- parameter_->assign(value);
+ parameter_.SetNoArena(&::google::protobuf::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) {
set_has_parameter();
- if (parameter_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_ = new ::std::string;
- }
- parameter_->assign(reinterpret_cast<const char*>(value), size);
+ parameter_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorRequest.parameter)
}
inline ::std::string* CodeGeneratorRequest::mutable_parameter() {
set_has_parameter();
- if (parameter_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- parameter_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorRequest.parameter)
- return parameter_;
+ return parameter_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* CodeGeneratorRequest::release_parameter() {
clear_has_parameter();
- if (parameter_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = parameter_;
- parameter_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return parameter_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorRequest::set_allocated_parameter(::std::string* parameter) {
- if (parameter_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete parameter_;
- }
- if (parameter) {
+ if (parameter != NULL) {
set_has_parameter();
- parameter_ = parameter;
} else {
clear_has_parameter();
- parameter_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ parameter_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), parameter);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorRequest.parameter)
}
@@ -549,68 +559,45 @@ inline void CodeGeneratorResponse_File::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void CodeGeneratorResponse_File::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& CodeGeneratorResponse_File::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.name)
}
inline void CodeGeneratorResponse_File::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::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) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.name)
}
inline ::std::string* CodeGeneratorResponse_File::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* CodeGeneratorResponse_File::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.name)
}
@@ -625,68 +612,45 @@ inline void CodeGeneratorResponse_File::clear_has_insertion_point() {
_has_bits_[0] &= ~0x00000002u;
}
inline void CodeGeneratorResponse_File::clear_insertion_point() {
- if (insertion_point_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_->clear();
- }
+ insertion_point_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_insertion_point();
}
inline const ::std::string& CodeGeneratorResponse_File::insertion_point() const {
// @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point)
- return *insertion_point_;
+ return insertion_point_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_insertion_point(const ::std::string& value) {
set_has_insertion_point();
- if (insertion_point_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_ = new ::std::string;
- }
- insertion_point_->assign(value);
+ insertion_point_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point)
}
inline void CodeGeneratorResponse_File::set_insertion_point(const char* value) {
set_has_insertion_point();
- if (insertion_point_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_ = new ::std::string;
- }
- insertion_point_->assign(value);
+ insertion_point_.SetNoArena(&::google::protobuf::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) {
set_has_insertion_point();
- if (insertion_point_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_ = new ::std::string;
- }
- insertion_point_->assign(reinterpret_cast<const char*>(value), size);
+ insertion_point_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point)
}
inline ::std::string* CodeGeneratorResponse_File::mutable_insertion_point() {
set_has_insertion_point();
- if (insertion_point_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- insertion_point_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point)
- return insertion_point_;
+ return insertion_point_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* CodeGeneratorResponse_File::release_insertion_point() {
clear_has_insertion_point();
- if (insertion_point_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = insertion_point_;
- insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return insertion_point_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_allocated_insertion_point(::std::string* insertion_point) {
- if (insertion_point_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete insertion_point_;
- }
- if (insertion_point) {
+ if (insertion_point != NULL) {
set_has_insertion_point();
- insertion_point_ = insertion_point;
} else {
clear_has_insertion_point();
- insertion_point_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ insertion_point_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), insertion_point);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.insertion_point)
}
@@ -701,68 +665,45 @@ inline void CodeGeneratorResponse_File::clear_has_content() {
_has_bits_[0] &= ~0x00000004u;
}
inline void CodeGeneratorResponse_File::clear_content() {
- if (content_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_->clear();
- }
+ content_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_content();
}
inline const ::std::string& CodeGeneratorResponse_File::content() const {
// @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.File.content)
- return *content_;
+ return content_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_content(const ::std::string& value) {
set_has_content();
- if (content_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_ = new ::std::string;
- }
- content_->assign(value);
+ content_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.File.content)
}
inline void CodeGeneratorResponse_File::set_content(const char* value) {
set_has_content();
- if (content_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_ = new ::std::string;
- }
- content_->assign(value);
+ content_.SetNoArena(&::google::protobuf::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) {
set_has_content();
- if (content_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_ = new ::std::string;
- }
- content_->assign(reinterpret_cast<const char*>(value), size);
+ content_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.File.content)
}
inline ::std::string* CodeGeneratorResponse_File::mutable_content() {
set_has_content();
- if (content_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- content_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.File.content)
- return content_;
+ return content_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* CodeGeneratorResponse_File::release_content() {
clear_has_content();
- if (content_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = content_;
- content_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return content_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse_File::set_allocated_content(::std::string* content) {
- if (content_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete content_;
- }
- if (content) {
+ if (content != NULL) {
set_has_content();
- content_ = content;
} else {
clear_has_content();
- content_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ content_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), content);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.File.content)
}
@@ -781,68 +722,45 @@ inline void CodeGeneratorResponse::clear_has_error() {
_has_bits_[0] &= ~0x00000001u;
}
inline void CodeGeneratorResponse::clear_error() {
- if (error_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_->clear();
- }
+ error_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_error();
}
inline const ::std::string& CodeGeneratorResponse::error() const {
// @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorResponse.error)
- return *error_;
+ return error_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse::set_error(const ::std::string& value) {
set_has_error();
- if (error_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_ = new ::std::string;
- }
- error_->assign(value);
+ error_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorResponse.error)
}
inline void CodeGeneratorResponse::set_error(const char* value) {
set_has_error();
- if (error_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_ = new ::std::string;
- }
- error_->assign(value);
+ error_.SetNoArena(&::google::protobuf::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) {
set_has_error();
- if (error_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_ = new ::std::string;
- }
- error_->assign(reinterpret_cast<const char*>(value), size);
+ error_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.compiler.CodeGeneratorResponse.error)
}
inline ::std::string* CodeGeneratorResponse::mutable_error() {
set_has_error();
- if (error_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- error_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.compiler.CodeGeneratorResponse.error)
- return error_;
+ return error_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* CodeGeneratorResponse::release_error() {
clear_has_error();
- if (error_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = error_;
- error_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return error_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void CodeGeneratorResponse::set_allocated_error(::std::string* error) {
- if (error_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete error_;
- }
- if (error) {
+ if (error != NULL) {
set_has_error();
- error_ = error;
} else {
clear_has_error();
- error_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ error_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), error);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.compiler.CodeGeneratorResponse.error)
}
@@ -888,8 +806,8 @@ namespace google {
namespace protobuf {
-} // namespace google
} // namespace protobuf
+} // namespace google
#endif // SWIG
// @@protoc_insertion_point(global_scope)
diff --git a/src/google/protobuf/compiler/plugin.proto b/src/google/protobuf/compiler/plugin.proto
index b65379d7..e627289b 100644
--- a/src/google/protobuf/compiler/plugin.proto
+++ b/src/google/protobuf/compiler/plugin.proto
@@ -44,6 +44,7 @@
// plugin should be named "protoc-gen-$NAME", and will then be used when the
// 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 15e05da9..b30d1972 100644
--- a/src/google/protobuf/compiler/python/python_generator.cc
+++ b/src/google/protobuf/compiler/python/python_generator.cc
@@ -44,11 +44,15 @@
// performance-minded Python code leverage the fast C++ implementation
// directly.
+#include <google/protobuf/stubs/hash.h>
#include <limits>
#include <map>
-#include <utility>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
+#include <utility>
#include <vector>
#include <google/protobuf/compiler/python/python_generator.h>
@@ -88,6 +92,37 @@ string ModuleName(const string& filename) {
}
+// Returns the alias we assign to the module of the given .proto filename
+// when importing. See testPackageInitializationImport in
+// google/protobuf/python/reflection_test.py
+// to see why we need the alias.
+string ModuleAlias(const string& filename) {
+ 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.
+ GlobalReplaceSubstring("_", "__", &module_name);
+ GlobalReplaceSubstring(".", "_dot_", &module_name);
+ return module_name;
+}
+
+
+// Returns an import statement of form "from X.Y.Z import T" for the given
+// .proto filename.
+string ModuleImportStatement(const string& filename) {
+ string module_name = ModuleName(filename);
+ int last_dot_pos = module_name.rfind('.');
+ if (last_dot_pos == 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.
+ return "import " + module_name;
+ } else {
+ return "from " + module_name.substr(0, last_dot_pos) + " import " +
+ module_name.substr(last_dot_pos + 1);
+ }
+}
+
+
// 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|.
@@ -273,7 +308,7 @@ bool Generator::Generate(const FileDescriptor* file,
fdp.SerializeToString(&file_descriptor_serialized_);
- scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
GOOGLE_CHECK(output.get());
io::Printer printer(output.get(), '$');
printer_ = &printer;
@@ -309,9 +344,12 @@ bool Generator::Generate(const FileDescriptor* file,
// Prints Python imports for all modules imported by |file|.
void Generator::PrintImports() const {
for (int i = 0; i < file_->dependency_count(); ++i) {
- string module_name = ModuleName(file_->dependency(i)->name());
- printer_->Print("import $module$\n", "module",
- module_name);
+ const string& filename = file_->dependency(i)->name();
+ string import_statement = ModuleImportStatement(filename);
+ string module_alias = ModuleAlias(filename);
+ printer_->Print("$statement$ as $alias$\n", "statement",
+ import_statement, "alias", module_alias);
+ CopyPublicDependenciesAliases(module_alias, file_->dependency(i));
}
printer_->Print("\n");
@@ -342,8 +380,9 @@ void Generator::PrintFileDescriptor() const {
if (file_->dependency_count() != 0) {
printer_->Print(",\ndependencies=[");
for (int i = 0; i < file_->dependency_count(); ++i) {
- string module_name = ModuleName(file_->dependency(i)->name());
- printer_->Print("$module_name$.DESCRIPTOR,", "module_name", module_name);
+ string module_alias = ModuleAlias(file_->dependency(i)->name());
+ printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias",
+ module_alias);
}
printer_->Print("]");
}
@@ -541,19 +580,24 @@ void Generator::PrintServiceDescriptor(
printer_->Print("])\n\n");
}
-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_->Indent();
+
+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()));
+ "module_name", ModuleName(file_->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_->Indent();
+ Generator::PrintDescriptorKeyAndModuleName(descriptor);
printer_->Print("))\n\n");
printer_->Outdent();
}
@@ -565,13 +609,7 @@ void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const {
"'$class_name$_Stub', ($class_name$,), dict(\n",
"class_name", descriptor.name());
printer_->Indent();
- 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()));
+ Generator::PrintDescriptorKeyAndModuleName(descriptor);
printer_->Print("))\n\n");
printer_->Outdent();
}
@@ -1084,7 +1122,7 @@ string Generator::ModuleLevelDescriptorName(
// We now have the name relative to its own module. Also qualify with
// the module name iff this descriptor is from a different .proto file.
if (descriptor.file() != file_) {
- name = ModuleName(descriptor.file()->name()) + "." + name;
+ name = ModuleAlias(descriptor.file()->name()) + "." + name;
}
return name;
}
@@ -1096,7 +1134,7 @@ string Generator::ModuleLevelDescriptorName(
string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const {
string name = NamePrefixedWithNestedTypes(descriptor, ".");
if (descriptor.file() != file_) {
- name = ModuleName(descriptor.file()->name()) + "." + name;
+ name = ModuleAlias(descriptor.file()->name()) + "." + name;
}
return name;
}
@@ -1109,7 +1147,7 @@ string Generator::ModuleLevelServiceDescriptorName(
UpperString(&name);
name = "_" + name;
if (descriptor.file() != file_) {
- name = ModuleName(descriptor.file()->name()) + "." + name;
+ name = ModuleAlias(descriptor.file()->name()) + "." + name;
}
return name;
}
@@ -1256,6 +1294,18 @@ 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 {
+ for (int i = 0; i < file->public_dependency_count(); ++i) {
+ string module_alias = ModuleAlias(file->public_dependency(i)->name());
+ printer_->Print("$alias$ = $copy_from$.$alias$\n", "alias", module_alias,
+ "copy_from", copy_from);
+ CopyPublicDependenciesAliases(copy_from, file->public_dependency(i));
+ }
+}
+
} // namespace python
} // namespace compiler
} // namespace protobuf
diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h
index f86e9ea2..ee68ad72 100644
--- a/src/google/protobuf/compiler/python/python_generator.h
+++ b/src/google/protobuf/compiler/python/python_generator.h
@@ -127,6 +127,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
void PrintServiceClass(const ServiceDescriptor& descriptor) const;
void PrintServiceStub(const ServiceDescriptor& descriptor) const;
+ void PrintDescriptorKeyAndModuleName(const ServiceDescriptor& descriptor) const ;
void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
string OptionsValue(const string& class_name,
@@ -148,6 +149,9 @@ 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;
+
// Very coarse-grained lock to ensure that Generate() is reentrant.
// Guards file_, printer_ and file_descriptor_serialized_.
mutable Mutex mutex_;
diff --git a/src/google/protobuf/compiler/python/python_plugin_unittest.cc b/src/google/protobuf/compiler/python/python_plugin_unittest.cc
index 09dbc654..e82bbae7 100644
--- a/src/google/protobuf/compiler/python/python_plugin_unittest.cc
+++ b/src/google/protobuf/compiler/python/python_plugin_unittest.cc
@@ -35,6 +35,9 @@
// worth.
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/compiler/python/python_generator.h>
#include <google/protobuf/compiler/command_line_interface.h>
@@ -69,7 +72,7 @@ class TestGenerator : public CodeGenerator {
void TryInsert(const string& filename, const string& insertion_point,
GeneratorContext* context) const {
- scoped_ptr<io::ZeroCopyOutputStream> output(
+ google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(
context->OpenForInsert(filename, insertion_point));
io::Printer printer(output.get(), '$');
printer.Print("// inserted $name$\n", "name", insertion_point);
diff --git a/src/google/protobuf/compiler/ruby/ruby_generator.cc b/src/google/protobuf/compiler/ruby/ruby_generator.cc
new file mode 100644
index 00000000..c5687903
--- /dev/null
+++ b/src/google/protobuf/compiler/ruby/ruby_generator.cc
@@ -0,0 +1,321 @@
+// 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 <sstream>
+
+#include <google/protobuf/compiler/code_generator.h>
+#include <google/protobuf/compiler/plugin.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/io/zero_copy_stream.h>
+
+#include <google/protobuf/compiler/ruby/ruby_generator.h>
+
+using google::protobuf::internal::scoped_ptr;
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace ruby {
+
+// Forward decls.
+std::string IntToString(uint32_t value);
+std::string StripDotProto(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(uint32_t value) {
+ std::ostringstream os;
+ os << value;
+ return os.str();
+}
+
+std::string StripDotProto(const std::string& proto_file) {
+ int lastindex = proto_file.find_last_of(".");
+ return proto_file.substr(0, lastindex);
+}
+
+std::string LabelForField(const google::protobuf::FieldDescriptor* field) {
+ switch (field->label()) {
+ case FieldDescriptor::LABEL_OPTIONAL: return "optional";
+ case FieldDescriptor::LABEL_REQUIRED: return "required";
+ case FieldDescriptor::LABEL_REPEATED: return "repeated";
+ default: assert(false); return "";
+ }
+}
+
+std::string TypeName(const google::protobuf::FieldDescriptor* field) {
+ switch (field->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_INT32: return "int32";
+ case FieldDescriptor::CPPTYPE_INT64: return "int64";
+ case FieldDescriptor::CPPTYPE_UINT32: return "uint32";
+ case FieldDescriptor::CPPTYPE_UINT64: return "uint64";
+ case FieldDescriptor::CPPTYPE_DOUBLE: return "double";
+ case FieldDescriptor::CPPTYPE_FLOAT: return "float";
+ case FieldDescriptor::CPPTYPE_BOOL: return "bool";
+ case FieldDescriptor::CPPTYPE_ENUM: return "enum";
+ case FieldDescriptor::CPPTYPE_STRING: return "string";
+ case FieldDescriptor::CPPTYPE_MESSAGE: return "message";
+ default: assert(false); return "";
+ }
+}
+
+void GenerateMessage(const google::protobuf::Descriptor* message,
+ google::protobuf::io::Printer* printer) {
+ printer->Print(
+ "add_message \"$name$\" do\n",
+ "name", message->full_name());
+ printer->Indent();
+
+ for (int i = 0; i < message->field_count(); i++) {
+ const FieldDescriptor* field = message->field(i);
+ printer->Print(
+ "$label$ :$name$, ",
+ "label", LabelForField(field),
+ "name", field->name());
+ printer->Print(
+ ":$type$, $number$",
+ "type", TypeName(field),
+ "number", IntToString(field->number()));
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ printer->Print(
+ ", \"$subtype$\"\n",
+ "subtype", field->message_type()->full_name());
+ } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
+ printer->Print(
+ ", \"$subtype$\"\n",
+ "subtype", field->enum_type()->full_name());
+ } else {
+ printer->Print("\n");
+ }
+ }
+
+ printer->Outdent();
+ printer->Print("end\n");
+
+ for (int i = 0; i < message->nested_type_count(); i++) {
+ GenerateMessage(message->nested_type(i), printer);
+ }
+ for (int i = 0; i < message->enum_type_count(); i++) {
+ GenerateEnum(message->enum_type(i), printer);
+ }
+}
+
+void GenerateEnum(const google::protobuf::EnumDescriptor* en,
+ google::protobuf::io::Printer* printer) {
+ printer->Print(
+ "add_enum \"$name$\" do\n",
+ "name", en->full_name());
+ printer->Indent();
+
+ for (int i = 0; i < en->value_count(); i++) {
+ const EnumValueDescriptor* value = en->value(i);
+ printer->Print(
+ "value :$name$, $number$\n",
+ "name", value->name(),
+ "number", IntToString(value->number()));
+ }
+
+ printer->Outdent();
+ printer->Print(
+ "end\n");
+}
+
+// Module names, class names, and enum value names need to be Ruby constants,
+// which must start with a capital letter.
+std::string RubifyConstant(const std::string& name) {
+ std::string ret = name;
+ if (!ret.empty()) {
+ if (ret[0] >= 'a' && ret[0] <= 'z') {
+ // If it starts with a lowercase letter, capitalize it.
+ ret[0] = ret[0] - 'a' + 'A';
+ } else if (ret[0] < 'A' || ret[0] > 'Z') {
+ // 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
+ // here, e.g. try to strip leading underscores, but this may cause other
+ // problems if the user really intended the name. So let's just prepend a
+ // well-known suffix.
+ ret = "PB_" + ret;
+ }
+ }
+ return ret;
+}
+
+void GenerateMessageAssignment(
+ const std::string& prefix,
+ const google::protobuf::Descriptor* message,
+ google::protobuf::io::Printer* printer) {
+ printer->Print(
+ "$prefix$$name$ = ",
+ "prefix", prefix,
+ "name", RubifyConstant(message->name()));
+ printer->Print(
+ "Google::Protobuf::DescriptorPool.generated_pool."
+ "lookup(\"$full_name$\").msgclass\n",
+ "full_name", message->full_name());
+
+ std::string nested_prefix = prefix + message->name() + "::";
+ for (int i = 0; i < message->nested_type_count(); i++) {
+ GenerateMessageAssignment(nested_prefix, message->nested_type(i), printer);
+ }
+ for (int i = 0; i < message->enum_type_count(); i++) {
+ GenerateEnumAssignment(nested_prefix, message->enum_type(i), printer);
+ }
+}
+
+void GenerateEnumAssignment(
+ const std::string& prefix,
+ const google::protobuf::EnumDescriptor* en,
+ google::protobuf::io::Printer* printer) {
+ printer->Print(
+ "$prefix$$name$ = ",
+ "prefix", prefix,
+ "name", RubifyConstant(en->name()));
+ printer->Print(
+ "Google::Protobuf::DescriptorPool.generated_pool."
+ "lookup(\"$full_name$\").enummodule\n",
+ "full_name", en->full_name());
+}
+
+int GeneratePackageModules(
+ std::string package_name,
+ google::protobuf::io::Printer* printer) {
+ int levels = 0;
+ while (!package_name.empty()) {
+ size_t dot_index = package_name.find(".");
+ 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);
+ }
+ component = RubifyConstant(component);
+ printer->Print(
+ "module $name$\n",
+ "name", component);
+ printer->Indent();
+ levels++;
+ }
+ return levels;
+}
+
+void EndPackageModules(
+ int levels,
+ google::protobuf::io::Printer* printer) {
+ while (levels > 0) {
+ levels--;
+ printer->Outdent();
+ printer->Print(
+ "end\n");
+ }
+}
+
+void GenerateFile(const google::protobuf::FileDescriptor* file,
+ google::protobuf::io::Printer* printer) {
+ printer->Print(
+ "# Generated by the protocol buffer compiler. DO NOT EDIT!\n"
+ "# source: $filename$\n"
+ "\n",
+ "filename", file->name());
+
+ printer->Print(
+ "require 'google/protobuf'\n\n");
+
+ for (int i = 0; i < file->dependency_count(); i++) {
+ const std::string& name = file->dependency(i)->name();
+ printer->Print(
+ "require '$name$'\n", "name", StripDotProto(name));
+ }
+
+ printer->Print(
+ "Google::Protobuf::DescriptorPool.generated_pool.build do\n");
+ printer->Indent();
+ for (int i = 0; i < file->message_type_count(); i++) {
+ GenerateMessage(file->message_type(i), printer);
+ }
+ for (int i = 0; i < file->enum_type_count(); i++) {
+ GenerateEnum(file->enum_type(i), printer);
+ }
+ printer->Outdent();
+ printer->Print(
+ "end\n\n");
+
+ int levels = GeneratePackageModules(file->package(), printer);
+ for (int i = 0; i < file->message_type_count(); i++) {
+ GenerateMessageAssignment("", file->message_type(i), printer);
+ }
+ for (int i = 0; i < file->enum_type_count(); i++) {
+ GenerateEnumAssignment("", file->enum_type(i), printer);
+ }
+ EndPackageModules(levels, printer);
+}
+
+bool Generator::Generate(
+ const FileDescriptor* file,
+ const string& parameter,
+ 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";
+ return false;
+ }
+
+ std::string filename =
+ StripDotProto(file->name()) + ".rb";
+ scoped_ptr<io::ZeroCopyOutputStream> output(generator_context->Open(filename));
+ io::Printer printer(output.get(), '$');
+
+ GenerateFile(file, &printer);
+
+ return true;
+}
+
+} // namespace ruby
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/compiler/ruby/ruby_generator.h b/src/google/protobuf/compiler/ruby/ruby_generator.h
new file mode 100644
index 00000000..48dbefd1
--- /dev/null
+++ b/src/google/protobuf/compiler/ruby/ruby_generator.h
@@ -0,0 +1,57 @@
+// 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_COMPILER_RUBY_GENERATOR_H__
+#define GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H__
+
+#include <string>
+
+#include <google/protobuf/compiler/code_generator.h>
+
+namespace google {
+namespace protobuf {
+namespace compiler {
+namespace ruby {
+
+class Generator : public google::protobuf::compiler::CodeGenerator {
+ virtual bool Generate(
+ const FileDescriptor* file,
+ const string& parameter,
+ GeneratorContext* generator_context,
+ string* error) const;
+};
+
+} // namespace ruby
+} // namespace compiler
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_COMPILER_RUBY_GENERATOR_H__
+
diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc
index 21dda598..b8dd198d 100644
--- a/src/google/protobuf/descriptor.cc
+++ b/src/google/protobuf/descriptor.cc
@@ -54,6 +54,7 @@
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/once.h>
+#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/stubs/map_util.h>
@@ -134,6 +135,19 @@ const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
"repeated", // LABEL_REPEATED
};
+const char* FileDescriptor::SyntaxName(FileDescriptor::Syntax syntax) {
+ switch (syntax) {
+ case SYNTAX_PROTO2:
+ return "proto2";
+ case SYNTAX_PROTO3:
+ return "proto3";
+ case SYNTAX_UNKNOWN:
+ return "unknown";
+ }
+ GOOGLE_LOG(FATAL) << "can't reach here.";
+ return NULL;
+}
+
static const char * const kNonLinkedWeakMessageReplacementName = "google.protobuf.Empty";
#ifndef _MSC_VER // MSVC doesn't need these and won't even accept them.
@@ -144,8 +158,8 @@ const int FieldDescriptor::kLastReservedNumber;
namespace {
-string ToCamelCase(const string& input) {
- bool capitalize_next = false;
+string ToCamelCase(const string& input, bool lower_first) {
+ bool capitalize_next = !lower_first;
string result;
result.reserve(input.size());
@@ -166,8 +180,8 @@ string ToCamelCase(const string& input) {
}
// Lower-case the first letter.
- if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') {
- result[0] = result[0] - 'A' + 'a';
+ if (lower_first && !result.empty() && 'A' <= result[0] && result[0] <= 'Z') {
+ result[0] = result[0] - 'A' + 'a';
}
return result;
@@ -327,6 +341,36 @@ typedef hash_map<EnumIntPair, const EnumValueDescriptor*,
typedef map<DescriptorIntPair, const FieldDescriptor*>
ExtensionsGroupedByDescriptorMap;
typedef hash_map<string, const SourceCodeInfo_Location*> LocationsByPathMap;
+
+set<string>* allowed_proto3_extendees_ = NULL;
+GOOGLE_PROTOBUF_DECLARE_ONCE(allowed_proto3_extendees_init_);
+
+void DeleteAllowedProto3Extendee() {
+ delete allowed_proto3_extendees_;
+}
+
+void InitAllowedProto3Extendee() {
+ allowed_proto3_extendees_ = new set<string>;
+ allowed_proto3_extendees_->insert("google.protobuf.FileOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.MessageOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.FieldOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.EnumOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.EnumValueOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.ServiceOptions");
+ allowed_proto3_extendees_->insert("google.protobuf.MethodOptions");
+ google::protobuf::internal::OnShutdown(&DeleteAllowedProto3Extendee);
+}
+
+// Checks whether the extendee type is allowed in proto3.
+// 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) {
+ ::google::protobuf::GoogleOnceInit(&allowed_proto3_extendees_init_, &InitAllowedProto3Extendee);
+ return allowed_proto3_extendees_->find(name) !=
+ allowed_proto3_extendees_->end();
+}
+
} // anonymous namespace
// ===================================================================
@@ -532,6 +576,9 @@ class FileDescriptorTables {
const void* parent, const string& camelcase_name) const;
inline const EnumValueDescriptor* FindEnumValueByNumber(
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;
// -----------------------------------------------------------------
// Adding items.
@@ -567,10 +614,16 @@ class FileDescriptorTables {
FieldsByNameMap fields_by_camelcase_name_;
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_);
// Populated on first request to save space, hence constness games.
mutable GoogleOnceDynamic locations_by_path_once_;
mutable LocationsByPathMap locations_by_path_;
+
+ // Mutex to protect the unknown-enum-value map due to dynamic
+ // EnumValueDescriptor creation on unknown values.
+ mutable Mutex unknown_enum_values_mu_;
};
DescriptorPool::Tables::Tables()
@@ -600,7 +653,8 @@ FileDescriptorTables::FileDescriptorTables()
fields_by_lowercase_name_(3),
fields_by_camelcase_name_(3),
fields_by_number_(3),
- enum_values_by_number_(3) {
+ enum_values_by_number_(3),
+ unknown_enum_values_by_number_(3) {
}
FileDescriptorTables::~FileDescriptorTables() {}
@@ -749,6 +803,60 @@ inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber(
return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number));
}
+inline const EnumValueDescriptor*
+FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown(
+ const EnumDescriptor* parent, int number) const {
+ // First try, with map of compiled-in values.
+ {
+ const EnumValueDescriptor* desc = FindPtrOrNull(
+ enum_values_by_number_, make_pair(parent, number));
+ if (desc != NULL) {
+ return desc;
+ }
+ }
+ // Second try, with reader lock held on unknown enum values: common case.
+ {
+ ReaderMutexLock l(&unknown_enum_values_mu_);
+ const EnumValueDescriptor* desc = FindPtrOrNull(
+ unknown_enum_values_by_number_, make_pair(parent, number));
+ if (desc != NULL) {
+ return desc;
+ }
+ }
+ // If not found, try again with writer lock held, and create new descriptor if
+ // necessary.
+ {
+ WriterMutexLock l(&unknown_enum_values_mu_);
+ const EnumValueDescriptor* desc = FindPtrOrNull(
+ unknown_enum_values_by_number_, make_pair(parent, number));
+ if (desc != NULL) {
+ return desc;
+ }
+
+ // Create an EnumValueDescriptor dynamically. We don't insert it into the
+ // 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::Tables*>(DescriptorPool::generated_pool()->
+ tables_.get());
+ EnumValueDescriptor* result = tables->Allocate<EnumValueDescriptor>();
+ result->name_ = tables->AllocateString(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();
+ InsertIfNotPresent(&unknown_enum_values_by_number_,
+ make_pair(parent, number),
+ result);
+ return result;
+ }
+}
+
+
inline const FieldDescriptor* DescriptorPool::Tables::FindExtension(
const Descriptor* extendee, int number) {
return FindPtrOrNull(extensions_, make_pair(extendee, number));
@@ -1311,6 +1419,11 @@ EnumDescriptor::FindValueByNumber(int key) const {
return file()->tables_->FindEnumValueByNumber(this, key);
}
+const EnumValueDescriptor*
+EnumDescriptor::FindValueByNumberCreatingIfUnknown(int key) const {
+ return file()->tables_->FindEnumValueByNumberCreatingIfUnknown(this, key);
+}
+
const MethodDescriptor*
ServiceDescriptor::FindMethodByName(const string& key) const {
Symbol result =
@@ -1511,6 +1624,10 @@ bool DescriptorPool::TryFindExtensionInFallbackDatabase(
// ===================================================================
+bool FieldDescriptor::is_map() const {
+ return type() == TYPE_MESSAGE && message_type()->options().map_entry();
+}
+
string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const {
GOOGLE_CHECK(has_default_value()) << "No default value";
switch (cpp_type()) {
@@ -1562,6 +1679,8 @@ string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const {
void FileDescriptor::CopyTo(FileDescriptorProto* proto) const {
proto->set_name(name());
if (!package().empty()) proto->set_package(package());
+ // TODO(liujisi): Also populate when syntax="proto2".
+ if (syntax() == SYNTAX_PROTO3) proto->set_syntax(SyntaxName(syntax()));
for (int i = 0; i < dependency_count(); i++) {
proto->add_dependency(dependency(i)->name());
@@ -1594,7 +1713,8 @@ void FileDescriptor::CopyTo(FileDescriptorProto* proto) const {
}
void FileDescriptor::CopySourceCodeInfoTo(FileDescriptorProto* proto) const {
- if (source_code_info_ != &SourceCodeInfo::default_instance()) {
+ if (source_code_info_ &&
+ source_code_info_ != &SourceCodeInfo::default_instance()) {
proto->mutable_source_code_info()->CopyFrom(*source_code_info_);
}
}
@@ -1730,6 +1850,13 @@ void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const {
if (&options() != &MethodOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
+
+ if (client_streaming_) {
+ proto->set_client_streaming(true);
+ }
+ if (server_streaming_) {
+ proto->set_server_streaming(true);
+ }
}
// DebugString methods ===============================================
@@ -1802,10 +1929,67 @@ bool FormatLineOptions(int depth, const Message &options, string *output) {
return !all_options.empty();
}
+template<typename DescType>
+class SourceLocationCommentPrinter {
+ public:
+ SourceLocationCommentPrinter(const DescType* desc,
+ const 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_);
+ }
+ void AddPreComment(string* output) {
+ if (have_source_loc_ && source_loc_.leading_comments.size() > 0) {
+ *output += FormatComment(source_loc_.leading_comments);
+ }
+ }
+ void AddPostComment(string* output) {
+ if (have_source_loc_ && source_loc_.trailing_comments.size() > 0) {
+ *output += FormatComment(source_loc_.trailing_comments);
+ }
+ }
+
+ // 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;
+ StripWhitespace(&stripped_comment);
+ vector<string> lines = Split(stripped_comment, "\n");
+ string output;
+ for (int i = 0; i < lines.size(); ++i) {
+ const string& line = lines[i];
+ strings::SubstituteAndAppend(&output, "$0// $1\n", prefix_, line);
+ }
+ return output;
+ }
+
+ private:
+ const DescType* desc_;
+ bool have_source_loc_;
+ SourceLocation source_loc_;
+ DebugStringOptions options_;
+ string prefix_;
+};
+
} // anonymous namespace
string FileDescriptor::DebugString() const {
- string contents = "syntax = \"proto2\";\n\n";
+ DebugStringOptions options; // default options
+ return DebugStringWithOptions(options);
+}
+
+string FileDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& debug_string_options) const {
+ string contents;
+ strings::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n",
+ SyntaxName(syntax()));
+
+ SourceLocationCommentPrinter<FileDescriptor>
+ comment_printer(this, "", debug_string_options);
+ comment_printer.AddPreComment(&contents);
set<int> public_dependencies;
set<int> weak_dependencies;
@@ -1836,7 +2020,7 @@ string FileDescriptor::DebugString() const {
}
for (int i = 0; i < enum_type_count(); i++) {
- enum_type(i)->DebugString(0, &contents);
+ enum_type(i)->DebugString(0, &contents, debug_string_options);
contents.append("\n");
}
@@ -1851,15 +2035,14 @@ string FileDescriptor::DebugString() const {
for (int i = 0; i < message_type_count(); i++) {
if (groups.count(message_type(i)) == 0) {
- strings::SubstituteAndAppend(&contents, "message $0",
- message_type(i)->name());
- message_type(i)->DebugString(0, &contents);
+ message_type(i)->DebugString(0, &contents, debug_string_options,
+ /* include_opening_clause */ true);
contents.append("\n");
}
}
for (int i = 0; i < service_count(); i++) {
- service(i)->DebugString(&contents);
+ service(i)->DebugString(&contents, debug_string_options);
contents.append("\n");
}
@@ -1871,23 +2054,46 @@ string FileDescriptor::DebugString() const {
strings::SubstituteAndAppend(&contents, "extend .$0 {\n",
containing_type->full_name());
}
- extension(i)->DebugString(1, FieldDescriptor::PRINT_LABEL, &contents);
+ extension(i)->DebugString(1, FieldDescriptor::PRINT_LABEL, &contents,
+ debug_string_options);
}
if (extension_count() > 0) contents.append("}\n\n");
+ comment_printer.AddPostComment(&contents);
+
return contents;
}
string Descriptor::DebugString() const {
+ DebugStringOptions options; // default options
+ return DebugStringWithOptions(options);
+}
+
+string Descriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- strings::SubstituteAndAppend(&contents, "message $0", name());
- DebugString(0, &contents);
+ DebugString(0, &contents, options, /* include_opening_clause */ true);
return contents;
}
-void Descriptor::DebugString(int depth, string *contents) const {
+void Descriptor::DebugString(int depth, 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, ' ');
++depth;
+
+ SourceLocationCommentPrinter<Descriptor>
+ comment_printer(this, prefix, debug_string_options);
+ comment_printer.AddPreComment(contents);
+
+ if (include_opening_clause) {
+ strings::SubstituteAndAppend(contents, "$0message $1", prefix, name());
+ }
contents->append(" {\n");
FormatLineOptions(depth, options(), contents);
@@ -1909,20 +2115,21 @@ void Descriptor::DebugString(int depth, string *contents) const {
for (int i = 0; i < nested_type_count(); i++) {
if (groups.count(nested_type(i)) == 0) {
- strings::SubstituteAndAppend(contents, "$0 message $1",
- prefix, nested_type(i)->name());
- nested_type(i)->DebugString(depth, contents);
+ nested_type(i)->DebugString(depth, contents, debug_string_options,
+ /* include_opening_clause */ true);
}
}
for (int i = 0; i < enum_type_count(); i++) {
- enum_type(i)->DebugString(depth, contents);
+ enum_type(i)->DebugString(depth, contents, debug_string_options);
}
for (int i = 0; i < field_count(); i++) {
if (field(i)->containing_oneof() == NULL) {
- field(i)->DebugString(depth, FieldDescriptor::PRINT_LABEL, contents);
+ field(i)->DebugString(depth, FieldDescriptor::PRINT_LABEL, contents,
+ debug_string_options);
} else if (field(i)->containing_oneof()->field(0) == field(i)) {
// This is the first field in this oneof, so print the whole oneof.
- field(i)->containing_oneof()->DebugString(depth, contents);
+ field(i)->containing_oneof()->DebugString(depth, contents,
+ debug_string_options);
}
}
@@ -1943,15 +2150,23 @@ void Descriptor::DebugString(int depth, string *contents) const {
prefix, containing_type->full_name());
}
extension(i)->DebugString(
- depth + 1, FieldDescriptor::PRINT_LABEL, contents);
+ depth + 1, FieldDescriptor::PRINT_LABEL, contents,
+ debug_string_options);
}
if (extension_count() > 0)
strings::SubstituteAndAppend(contents, "$0 }\n", prefix);
strings::SubstituteAndAppend(contents, "$0}\n", prefix);
+ comment_printer.AddPostComment(contents);
}
string FieldDescriptor::DebugString() const {
+ DebugStringOptions options; // default options
+ return DebugStringWithOptions(options);
+}
+
+string FieldDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& debug_string_options) const {
string contents;
int depth = 0;
if (is_extension()) {
@@ -1959,35 +2174,53 @@ string FieldDescriptor::DebugString() const {
containing_type()->full_name());
depth = 1;
}
- DebugString(depth, PRINT_LABEL, &contents);
+ DebugString(depth, PRINT_LABEL, &contents, debug_string_options);
if (is_extension()) {
contents.append("}\n");
}
return contents;
}
+// The field type string used in FieldDescriptor::DebugString()
+string FieldDescriptor::FieldTypeNameDebugString() const {
+ switch(type()) {
+ case TYPE_MESSAGE:
+ return "." + message_type()->full_name();
+ case TYPE_ENUM:
+ return "." + enum_type()->full_name();
+ default:
+ return kTypeToName[type()];
+ }
+}
+
void FieldDescriptor::DebugString(int depth,
PrintLabelFlag print_label_flag,
- string *contents) const {
+ string *contents,
+ const DebugStringOptions&
+ debug_string_options) const {
string prefix(depth * 2, ' ');
string field_type;
- switch (type()) {
- case TYPE_MESSAGE:
- field_type = "." + message_type()->full_name();
- break;
- case TYPE_ENUM:
- field_type = "." + enum_type()->full_name();
- break;
- default:
- field_type = kTypeToName[type()];
+
+ // Special case map fields.
+ if (is_map()) {
+ strings::SubstituteAndAppend(
+ &field_type, "map<$0, $1>",
+ message_type()->field(0)->FieldTypeNameDebugString(),
+ message_type()->field(1)->FieldTypeNameDebugString());
+ } else {
+ field_type = FieldTypeNameDebugString();
}
string label;
- if (print_label_flag == PRINT_LABEL) {
+ if (print_label_flag == PRINT_LABEL && !is_map()) {
label = kLabelToName[this->label()];
label.push_back(' ');
}
+ SourceLocationCommentPrinter<FieldDescriptor>
+ comment_printer(this, prefix, debug_string_options);
+ comment_printer.AddPreComment(contents);
+
strings::SubstituteAndAppend(contents, "$0$1$2 $3 = $4",
prefix,
label,
@@ -2015,57 +2248,101 @@ void FieldDescriptor::DebugString(int depth,
}
if (type() == TYPE_GROUP) {
- message_type()->DebugString(depth, contents);
+ message_type()->DebugString(depth, contents, debug_string_options,
+ /* include_opening_clause */ false);
} else {
contents->append(";\n");
}
+
+ comment_printer.AddPostComment(contents);
}
string OneofDescriptor::DebugString() const {
+ DebugStringOptions options; // default values
+ return DebugStringWithOptions(options);
+}
+
+string OneofDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- DebugString(0, &contents);
+ DebugString(0, &contents, options);
return contents;
}
-void OneofDescriptor::DebugString(int depth, string* contents) const {
+void OneofDescriptor::DebugString(int depth, string* contents,
+ const DebugStringOptions&
+ debug_string_options) const {
string prefix(depth * 2, ' ');
++depth;
+ SourceLocationCommentPrinter<OneofDescriptor>
+ comment_printer(this, prefix, debug_string_options);
+ comment_printer.AddPreComment(contents);
strings::SubstituteAndAppend(
contents, "$0 oneof $1 {\n", prefix, name());
for (int i = 0; i < field_count(); i++) {
- field(i)->DebugString(depth, FieldDescriptor::OMIT_LABEL, contents);
+ field(i)->DebugString(depth, FieldDescriptor::OMIT_LABEL, contents,
+ debug_string_options);
}
strings::SubstituteAndAppend(contents, "$0}\n", prefix);
+ comment_printer.AddPostComment(contents);
}
string EnumDescriptor::DebugString() const {
+ DebugStringOptions options; // default values
+ return DebugStringWithOptions(options);
+}
+
+string EnumDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- DebugString(0, &contents);
+ DebugString(0, &contents, options);
return contents;
}
-void EnumDescriptor::DebugString(int depth, string *contents) const {
+void EnumDescriptor::DebugString(int depth, string *contents,
+ const DebugStringOptions&
+ debug_string_options) const {
string prefix(depth * 2, ' ');
++depth;
+
+ SourceLocationCommentPrinter<EnumDescriptor>
+ comment_printer(this, prefix, debug_string_options);
+ comment_printer.AddPreComment(contents);
+
strings::SubstituteAndAppend(contents, "$0enum $1 {\n",
prefix, name());
FormatLineOptions(depth, options(), contents);
for (int i = 0; i < value_count(); i++) {
- value(i)->DebugString(depth, contents);
+ value(i)->DebugString(depth, contents, debug_string_options);
}
strings::SubstituteAndAppend(contents, "$0}\n", prefix);
+
+ comment_printer.AddPostComment(contents);
}
string EnumValueDescriptor::DebugString() const {
+ DebugStringOptions options; // default values
+ return DebugStringWithOptions(options);
+}
+
+string EnumValueDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- DebugString(0, &contents);
+ DebugString(0, &contents, options);
return contents;
}
-void EnumValueDescriptor::DebugString(int depth, string *contents) const {
+void EnumValueDescriptor::DebugString(int depth, string *contents,
+ const DebugStringOptions&
+ debug_string_options) const {
string prefix(depth * 2, ' ');
+
+ SourceLocationCommentPrinter<EnumValueDescriptor>
+ comment_printer(this, prefix, debug_string_options);
+ comment_printer.AddPreComment(contents);
+
strings::SubstituteAndAppend(contents, "$0$1 = $2",
prefix, name(), number());
@@ -2074,39 +2351,70 @@ void EnumValueDescriptor::DebugString(int depth, string *contents) const {
strings::SubstituteAndAppend(contents, " [$0]", formatted_options);
}
contents->append(";\n");
+
+ comment_printer.AddPostComment(contents);
}
string ServiceDescriptor::DebugString() const {
+ DebugStringOptions options; // default values
+ return DebugStringWithOptions(options);
+}
+
+string ServiceDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- DebugString(&contents);
+ DebugString(&contents, options);
return contents;
}
-void ServiceDescriptor::DebugString(string *contents) const {
+void ServiceDescriptor::DebugString(string *contents,
+ const DebugStringOptions&
+ debug_string_options) const {
+ SourceLocationCommentPrinter<ServiceDescriptor>
+ comment_printer(this, /* prefix */ "", debug_string_options);
+ comment_printer.AddPreComment(contents);
+
strings::SubstituteAndAppend(contents, "service $0 {\n", name());
FormatLineOptions(1, options(), contents);
for (int i = 0; i < method_count(); i++) {
- method(i)->DebugString(1, contents);
+ method(i)->DebugString(1, contents, debug_string_options);
}
contents->append("}\n");
+
+ comment_printer.AddPostComment(contents);
}
string MethodDescriptor::DebugString() const {
+ DebugStringOptions options; // default values
+ return DebugStringWithOptions(options);
+}
+
+string MethodDescriptor::DebugStringWithOptions(
+ const DebugStringOptions& options) const {
string contents;
- DebugString(0, &contents);
+ DebugString(0, &contents, options);
return contents;
}
-void MethodDescriptor::DebugString(int depth, string *contents) const {
+void MethodDescriptor::DebugString(int depth, string *contents,
+ const DebugStringOptions&
+ debug_string_options) const {
string prefix(depth * 2, ' ');
++depth;
- strings::SubstituteAndAppend(contents, "$0rpc $1(.$2) returns (.$3)",
+
+ SourceLocationCommentPrinter<MethodDescriptor>
+ 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());
+ output_type()->full_name(),
+ client_streaming() ? "stream " : "",
+ server_streaming() ? "stream " : "");
string formatted_options;
if (FormatLineOptions(depth, options(), &formatted_options)) {
@@ -2115,6 +2423,8 @@ void MethodDescriptor::DebugString(int depth, string *contents) const {
} else {
contents->append(";\n");
}
+
+ comment_printer.AddPostComment(contents);
}
@@ -2142,6 +2452,11 @@ bool FileDescriptor::GetSourceLocation(const vector<int>& path,
return false;
}
+bool FileDescriptor::GetSourceLocation(SourceLocation* out_location) const {
+ vector<int> path; // empty path for root FileDescriptor
+ return GetSourceLocation(path, out_location);
+}
+
bool FieldDescriptor::is_packed() const {
return is_packable() && (options_ != NULL) && options_->packed();
}
@@ -2409,7 +2724,7 @@ class DescriptorBuilder {
// Creates a placeholder file. Never returns NULL. This is used when an
// import is not found and AllowUnknownDependencies() is enabled.
- const FileDescriptor* NewPlaceholderFile(const string& name);
+ FileDescriptor* NewPlaceholderFile(const string& name);
// Calls tables_->AddSymbol() and records an error if it fails. Returns
// true if successful or false if failed, though most callers can ignore
@@ -2667,9 +2982,24 @@ class DescriptorBuilder {
const ServiceDescriptorProto& proto);
void ValidateMethodOptions(MethodDescriptor* method,
const MethodDescriptorProto& proto);
-
- void ValidateMapKey(FieldDescriptor* field,
- const FieldDescriptorProto& 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,
+ const EnumDescriptorProto& proto);
+
+ // Returns true if the map entry message is compatible with the
+ // auto-generated entry message from map fields syntax.
+ bool ValidateMapEntry(FieldDescriptor* field,
+ const FieldDescriptorProto& proto);
+
+ // Recursively detects naming conflicts with map entry types for a
+ // better error message.
+ void DetectMapConflicts(const Descriptor* message,
+ const DescriptorProto& proto);
};
@@ -2992,19 +3322,9 @@ Symbol DescriptorBuilder::NewPlaceholder(const string& name,
}
// Create the placeholders.
- FileDescriptor* placeholder_file = tables_->Allocate<FileDescriptor>();
- memset(placeholder_file, 0, sizeof(*placeholder_file));
-
- placeholder_file->source_code_info_ = &SourceCodeInfo::default_instance();
-
- placeholder_file->name_ =
- tables_->AllocateString(*placeholder_full_name + ".placeholder.proto");
+ FileDescriptor* placeholder_file = NewPlaceholderFile(
+ *placeholder_full_name + ".placeholder.proto");
placeholder_file->package_ = placeholder_package;
- placeholder_file->pool_ = pool_;
- placeholder_file->options_ = &FileOptions::default_instance();
- placeholder_file->tables_ = &FileDescriptorTables::kEmpty;
- placeholder_file->is_placeholder_ = true;
- // All other fields are zero or NULL.
if (placeholder_type == PLACEHOLDER_ENUM) {
placeholder_file->enum_type_count_ = 1;
@@ -3068,7 +3388,7 @@ Symbol DescriptorBuilder::NewPlaceholder(const string& name,
}
}
-const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
+FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
const string& name) {
FileDescriptor* placeholder = tables_->Allocate<FileDescriptor>();
memset(placeholder, 0, sizeof(*placeholder));
@@ -3078,7 +3398,9 @@ const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
placeholder->pool_ = pool_;
placeholder->options_ = &FileOptions::default_instance();
placeholder->tables_ = &FileDescriptorTables::kEmpty;
+ placeholder->source_code_info_ = &SourceCodeInfo::default_instance();
placeholder->is_placeholder_ = true;
+ placeholder->syntax_ = FileDescriptor::SYNTAX_PROTO2;
// All other fields are zero or NULL.
return placeholder;
@@ -3296,7 +3618,7 @@ const FileDescriptor* DescriptorBuilder::BuildFile(
// Note: This only works if the input is canonical -- that is, it
// fully-qualifies all type names, has no UninterpretedOptions, etc.
// This is fine, because this idempotency "feature" really only exists to
- // accomodate one hack in the proto1->proto2 migration layer.
+ // accommodate one hack in the proto1->proto2 migration layer.
const FileDescriptor* existing_file = tables_->FindFile(filename_);
if (existing_file != NULL) {
// File already in pool. Compare the existing one to the input.
@@ -3363,6 +3685,19 @@ const FileDescriptor* DescriptorBuilder::BuildFile(
"Missing field: FileDescriptorProto.name.");
}
+ // TODO(liujisi): Report error when the syntax is empty after all the protos
+ // have added the syntax statement.
+ if (proto.syntax().empty() || proto.syntax() == "proto2") {
+ file_->syntax_ = FileDescriptor::SYNTAX_PROTO2;
+ } else if (proto.syntax() == "proto3") {
+ file_->syntax_ = FileDescriptor::SYNTAX_PROTO3;
+ } else {
+ file_->syntax_ = FileDescriptor::SYNTAX_UNKNOWN;
+ AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
+ "Unrecognized syntax: " + proto.syntax());
+ }
+
+
result->name_ = tables_->AllocateString(proto.name());
if (proto.has_package()) {
result->package_ = tables_->AllocateString(proto.package());
@@ -3506,6 +3841,14 @@ const FileDescriptor* DescriptorBuilder::BuildFile(
ValidateFileOptions(result, proto);
}
+ // Additional naming conflict check for map entry types. Only need to check
+ // this if there are already errors.
+ if (had_errors_) {
+ for (int i = 0; i < proto.message_type_size(); ++i) {
+ DetectMapConflicts(result->message_type(i), proto.message_type(i));
+ }
+ }
+
if (!unused_dependency_.empty()) {
LogUnusedDependency(result);
@@ -3621,7 +3964,9 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
// Don't bother with the above optimization for camel-case names since
// .proto files that follow the guide shouldn't be using names in this
// format, so the optimization wouldn't help much.
- result->camelcase_name_ = tables_->AllocateString(ToCamelCase(proto.name()));
+ result->camelcase_name_ =
+ tables_->AllocateString(ToCamelCase(proto.name(),
+ /* lower_first = */ true));
// Some compilers do not allow static_cast directly between two enum types,
// so we must cast to int first.
@@ -3646,7 +3991,6 @@ 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->experimental_map_key_ = NULL;
result->message_type_ = NULL;
result->enum_type_ = NULL;
@@ -4065,6 +4409,9 @@ void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto,
AllocateOptions(proto.options(), result);
}
+ result->client_streaming_ = proto.client_streaming();
+ result->server_streaming_ = proto.server_streaming();
+
AddSymbol(result->full_name(), parent, result->name(),
proto, Symbol(result));
}
@@ -4472,8 +4819,98 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file,
}
}
}
+ if (file->syntax() == FileDescriptor::SYNTAX_PROTO3) {
+ ValidateProto3(file, 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));
+ }
+ for (int i = 0; i < file->message_type_count(); ++i) {
+ ValidateProto3Message(file->message_types_ + i, proto.message_type(i));
+ }
+ for (int i = 0; i < file->enum_type_count(); ++i) {
+ ValidateProto3Enum(file->enum_types_ + i, proto.enum_type(i));
+ }
+ if (IsLite(file)) {
+ AddError(file->name(), proto,
+ DescriptorPool::ErrorCollector::OTHER,
+ "Lite runtime is not supported in proto3.");
+ }
+}
+
+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));
+ }
+ for (int i = 0; i < message->enum_type_count(); ++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));
+ }
+ if (message->extension_range_count() > 0) {
+ AddError(message->full_name(), proto,
+ DescriptorPool::ErrorCollector::OTHER,
+ "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,
+ "MessageSet is not supported in proto3.");
+ }
+}
+
+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,
+ "Extensions in proto3 are only allowed for defining options.");
+ }
+ if (field->is_required()) {
+ AddError(field->full_name(), proto,
+ DescriptorPool::ErrorCollector::OTHER,
+ "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.");
+ }
+ 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,
+ "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.");
+ }
+}
+
+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.");
+ }
+}
void DescriptorBuilder::ValidateMessageOptions(Descriptor* message,
const DescriptorProto& proto) {
@@ -4499,10 +4936,6 @@ void DescriptorBuilder::ValidateMessageOptions(Descriptor* message,
void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field,
const FieldDescriptorProto& proto) {
- if (field->options().has_experimental_map_key()) {
- ValidateMapKey(field, proto);
- }
-
// Only message type fields may be lazy.
if (field->options().lazy()) {
if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
@@ -4551,6 +4984,16 @@ void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field,
"a lite type, but the reverse is allowed.");
}
+ // Validate map types.
+ if (field->is_map()) {
+ if (!ValidateMapEntry(field, proto)) {
+ AddError(field->full_name(), proto,
+ DescriptorPool::ErrorCollector::OTHER,
+ "map_entry should not be set explicitly. Use map<KeyType, "
+ "ValueType> instead.");
+ }
+ }
+
}
void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm,
@@ -4608,57 +5051,127 @@ void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* /* method */,
// Nothing to do so far.
}
-void DescriptorBuilder::ValidateMapKey(FieldDescriptor* field,
- const FieldDescriptorProto& proto) {
- if (!field->is_repeated()) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "map type is only allowed for repeated fields.");
- return;
+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
+ message->extension_count() != 0 ||
+ field->label() != FieldDescriptor::LABEL_REPEATED ||
+ message->extension_range_count() != 0 ||
+ message->nested_type_count() != 0 || message->enum_type_count() != 0 ||
+ // Must contain exactly two fields
+ message->field_count() != 2 ||
+ // Field name and message name must match
+ message->name() != ToCamelCase(field->name(), false) + "Entry" ||
+ // Entry message must be in the same containing type of the field.
+ field->containing_type() != message->containing_type()) {
+ return false;
}
- if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "map type is only allowed for fields with a message type.");
- return;
+ const FieldDescriptor* key = message->field(0);
+ const FieldDescriptor* value = message->field(1);
+ if (key->label() != FieldDescriptor::LABEL_OPTIONAL || key->number() != 1 ||
+ key->name() != "key") {
+ return false;
+ }
+ if (value->label() != FieldDescriptor::LABEL_OPTIONAL ||
+ value->number() != 2 || value->name() != "value") {
+ return false;
}
- const Descriptor* item_type = field->message_type();
- if (item_type == NULL) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "Could not find field type.");
- return;
+ // 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.");
+ break;
+ case FieldDescriptor::TYPE_FLOAT:
+ case FieldDescriptor::TYPE_DOUBLE:
+ case FieldDescriptor::TYPE_MESSAGE:
+ case FieldDescriptor::TYPE_GROUP:
+ case FieldDescriptor::TYPE_BYTES:
+ AddError(
+ field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
+ "Key in map fields cannot be float/double, bytes or message types.");
+ break;
+ case FieldDescriptor::TYPE_BOOL:
+ case FieldDescriptor::TYPE_INT32:
+ case FieldDescriptor::TYPE_INT64:
+ case FieldDescriptor::TYPE_SINT32:
+ case FieldDescriptor::TYPE_SINT64:
+ case FieldDescriptor::TYPE_STRING:
+ case FieldDescriptor::TYPE_UINT32:
+ case FieldDescriptor::TYPE_UINT64:
+ case FieldDescriptor::TYPE_FIXED32:
+ case FieldDescriptor::TYPE_FIXED64:
+ case FieldDescriptor::TYPE_SFIXED32:
+ case FieldDescriptor::TYPE_SFIXED64:
+ // Legal cases
+ break;
+ // Do not add a default, so that the compiler will complian when new types
+ // are added.
}
- // Find the field in item_type named by "experimental_map_key"
- const string& key_name = field->options().experimental_map_key();
- const Symbol key_symbol = LookupSymbol(
- key_name,
- // We append ".key_name" to the containing type's name since
- // LookupSymbol() searches for peers of the supplied name, not
- // children of the supplied name.
- item_type->full_name() + "." + key_name);
+ return true;
+}
- if (key_symbol.IsNull() || key_symbol.field_descriptor->is_extension()) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "Could not find field named \"" + key_name + "\" in type \"" +
- item_type->full_name() + "\".");
- return;
+void DescriptorBuilder::DetectMapConflicts(const Descriptor* message,
+ const DescriptorProto& proto) {
+ map<string, const Descriptor*> seen_types;
+ for (int i = 0; i < message->nested_type_count(); ++i) {
+ const Descriptor* nested = message->nested_type(i);
+ pair<map<string, const Descriptor*>::iterator, bool> result =
+ seen_types.insert(make_pair(nested->name(), nested));
+ if (!result.second) {
+ if (result.first->second->options().map_entry() ||
+ nested->options().map_entry()) {
+ AddError(message->full_name(), proto,
+ DescriptorPool::ErrorCollector::NAME,
+ "Expanded map entry type " + nested->name() +
+ " conflicts with an existing nested message type.");
+ }
+ }
+ // Recursively test on the nested types.
+ DetectMapConflicts(message->nested_type(i), proto.nested_type(i));
+ }
+ // Check for conflicted field names.
+ for (int i = 0; i < message->field_count(); ++i) {
+ const FieldDescriptor* field = message->field(i);
+ map<string, const Descriptor*>::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.");
+ }
}
- const FieldDescriptor* key_field = key_symbol.field_descriptor;
-
- if (key_field->is_repeated()) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "map_key must not name a repeated field.");
- return;
+ // Check for conflicted enum names.
+ for (int i = 0; i < message->enum_type_count(); ++i) {
+ const EnumDescriptor* enum_desc = message->enum_type(i);
+ map<string, const Descriptor*>::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.");
+ }
}
-
- if (key_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
- AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
- "map key must name a scalar or string field.");
- return;
+ // Check for conflicted oneof names.
+ for (int i = 0; i < message->oneof_decl_count(); ++i) {
+ const OneofDescriptor* oneof_desc = message->oneof_decl(i);
+ map<string, const Descriptor*>::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.");
+ }
}
-
- field->experimental_map_key_ = key_field;
}
diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h
index 67afc774..52df47f3 100644
--- a/src/google/protobuf/descriptor.h
+++ b/src/google/protobuf/descriptor.h
@@ -104,6 +104,11 @@ class FileDescriptorTables;
// Defined in unknown_field_set.h.
class UnknownField;
+// Defined in generated_message_reflection.h.
+namespace internal {
+ class GeneratedMessageReflection;
+}
+
// NB, all indices are zero-based.
struct SourceLocation {
int start_line;
@@ -112,12 +117,24 @@ struct SourceLocation {
int end_column;
// Doc comments found at the source location.
- // TODO(kenton): Maybe this struct should have been named SourceInfo or
- // something instead. Oh well.
string leading_comments;
string trailing_comments;
};
+// Options when generating machine-parsable output from a descriptor with
+// DebugString().
+struct DebugStringOptions {
+ // include original user comments as recorded in SourceLocation entries. N.B.
+ // that this must be |false| by default: several other pieces of code (for
+ // example, the C++ code generation for fields in the proto compiler) rely on
+ // DebugString() output being unobstructed by user comments.
+ bool include_comments;
+
+ DebugStringOptions()
+ : include_comments(false)
+ {}
+};
+
// Describes a type of protocol message, or a particular group within a
// message. To obtain the Descriptor for a given message object, call
// Message::GetDescriptor(). Generated message classes also have a
@@ -162,6 +179,10 @@ class LIBPROTOBUF_EXPORT Descriptor {
// will be suitable for re-parsing.
string DebugString() const;
+ // Similar to DebugString(), but additionally takes options (e.g.,
+ // include original user comments in output).
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
// Returns true if this is a placeholder for an unknown type. This will
// only be the case if this descriptor comes from a DescriptorPool
// with AllowUnknownDependencies() set.
@@ -282,8 +303,12 @@ class LIBPROTOBUF_EXPORT Descriptor {
typedef MessageOptions OptionsType;
// Internal version of DebugString; controls the level of indenting for
- // correct depth
- void DebugString(int depth, string *contents) const;
+ // 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,
+ const DebugStringOptions& options,
+ bool include_opening_clause) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -450,6 +475,8 @@ class LIBPROTOBUF_EXPORT FieldDescriptor {
// 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.
@@ -513,12 +540,6 @@ class LIBPROTOBUF_EXPORT FieldDescriptor {
// returns null.
const EnumDescriptor* enum_type() const;
- // EXPERIMENTAL; DO NOT USE.
- // If this field is a map field, experimental_map_key() is the field
- // that is the key for this map.
- // experimental_map_key()->containing_type() is the same as message_type().
- const FieldDescriptor* experimental_map_key() const;
-
// Get the FieldOptions for this field. This includes things listed in
// square brackets after the field definition. E.g., the field:
// optional string text = 1 [ctype=CORD];
@@ -533,6 +554,9 @@ class LIBPROTOBUF_EXPORT FieldDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
// Helper method to get the CppType for a particular Type.
static CppType TypeToCppType(Type type);
@@ -558,13 +582,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;
+ 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;
+ // Helper function that returns the field type name for DebugString.
+ string FieldTypeNameDebugString() const;
+
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
void GetLocationPath(vector<int>* output) const;
@@ -584,7 +611,6 @@ class LIBPROTOBUF_EXPORT FieldDescriptor {
const Descriptor* extension_scope_;
const Descriptor* message_type_;
const EnumDescriptor* enum_type_;
- const FieldDescriptor* experimental_map_key_;
const FieldOptions* options_;
// IMPORTANT: If you add a new field, make sure to search for all instances
// of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
@@ -645,6 +671,9 @@ class LIBPROTOBUF_EXPORT OneofDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
@@ -654,7 +683,8 @@ class LIBPROTOBUF_EXPORT OneofDescriptor {
private:
// See Descriptor::DebugString().
- void DebugString(int depth, string* contents) const;
+ void DebugString(int depth, string* contents,
+ const DebugStringOptions& options) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -723,6 +753,10 @@ class LIBPROTOBUF_EXPORT EnumDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
+
// Returns true if this is a placeholder for an unknown enum. This will
// only be the case if this descriptor comes from a DescriptorPool
// with AllowUnknownDependencies() set.
@@ -738,8 +772,21 @@ class LIBPROTOBUF_EXPORT EnumDescriptor {
private:
typedef EnumOptions OptionsType;
+ // Looks up a value by number. If the value does not exist, dynamically
+ // creates a new EnumValueDescriptor for that value, assuming that it was
+ // unknown. If a new descriptor is created, this is done in a thread-safe way,
+ // and future calls will return the same value descriptor pointer.
+ //
+ // This is private but is used by GeneratedMessageReflection (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) const;
+ void DebugString(int depth, string *contents,
+ const DebugStringOptions& options) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -769,6 +816,7 @@ class LIBPROTOBUF_EXPORT EnumDescriptor {
friend class FieldDescriptor;
friend class EnumValueDescriptor;
friend class FileDescriptor;
+ friend class LIBPROTOBUF_EXPORT internal::GeneratedMessageReflection;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
};
@@ -806,6 +854,10 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
+
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
@@ -817,7 +869,8 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor {
typedef EnumValueOptions OptionsType;
// See Descriptor::DebugString().
- void DebugString(int depth, string *contents) const;
+ void DebugString(int depth, string *contents,
+ const DebugStringOptions& options) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -836,6 +889,7 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor {
EnumValueDescriptor() {}
friend class DescriptorBuilder;
friend class EnumDescriptor;
+ friend class FileDescriptorTables;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
};
@@ -876,6 +930,10 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
+
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
@@ -887,7 +945,7 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor {
typedef ServiceOptions OptionsType;
// See Descriptor::DebugString().
- void DebugString(string *contents) const;
+ void DebugString(string *contents, const DebugStringOptions& options) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -932,6 +990,11 @@ class LIBPROTOBUF_EXPORT MethodDescriptor {
// Gets the type of protocol message which this message produces as output.
const Descriptor* output_type() const;
+ // Gets whether the client streams multiple requests.
+ bool client_streaming() const;
+ // Gets whether the server streams multiple responses.
+ bool server_streaming() const;
+
// Get options for this method. These are specified in the .proto file by
// placing lines like "option foo = 1234;" in curly-braces after a method
// declaration. Allowed options are defined by MethodOptions in
@@ -945,6 +1008,10 @@ class LIBPROTOBUF_EXPORT MethodDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
+
// Source Location ---------------------------------------------------
// Updates |*out_location| to the source location of the complete
@@ -956,7 +1023,8 @@ class LIBPROTOBUF_EXPORT MethodDescriptor {
typedef MethodOptions OptionsType;
// See Descriptor::DebugString().
- void DebugString(int depth, string *contents) const;
+ void DebugString(int depth, string *contents,
+ const DebugStringOptions& options) const;
// Walks up the descriptor tree to generate the source location path
// to this descriptor from the file root.
@@ -968,6 +1036,8 @@ class LIBPROTOBUF_EXPORT MethodDescriptor {
const Descriptor* input_type_;
const Descriptor* output_type_;
const MethodOptions* options_;
+ bool client_streaming_;
+ bool server_streaming_;
// IMPORTANT: If you add a new field, make sure to search for all instances
// of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
// descriptor.cc and update them to initialize the field.
@@ -1052,6 +1122,15 @@ class LIBPROTOBUF_EXPORT FileDescriptor {
// message.
const FileOptions& options() const;
+ // Syntax of this file.
+ enum Syntax {
+ SYNTAX_UNKNOWN = 0,
+ SYNTAX_PROTO2 = 2,
+ SYNTAX_PROTO3 = 3,
+ };
+ Syntax syntax() const;
+ static const char* SyntaxName(Syntax syntax);
+
// Find a top-level message type by name. Returns NULL if not found.
const Descriptor* FindMessageTypeByName(const string& name) const;
// Find a top-level enum type by name. Returns NULL if not found.
@@ -1082,11 +1161,18 @@ class LIBPROTOBUF_EXPORT FileDescriptor {
// See Descriptor::DebugString().
string DebugString() const;
+ // See Descriptor::DebugStringWithOptions().
+ string DebugStringWithOptions(const DebugStringOptions& options) const;
+
// Returns true if this is a placeholder for an unknown file. This will
// only be the case if this descriptor comes from a DescriptorPool
// with AllowUnknownDependencies() set.
bool is_placeholder() const;
+ // Updates |*out_location| to the source location of the complete extent of
+ // this file declaration (namely, the empty path).
+ bool GetSourceLocation(SourceLocation* out_location) const;
+
private:
// Source Location ---------------------------------------------------
@@ -1116,6 +1202,7 @@ class LIBPROTOBUF_EXPORT FileDescriptor {
int service_count_;
ServiceDescriptor* services_;
int extension_count_;
+ Syntax syntax_;
bool is_placeholder_;
FieldDescriptor* extensions_;
const FileOptions* options_;
@@ -1268,7 +1355,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool {
};
// Reports an error in the FileDescriptorProto. Use this function if the
- // problem occured should interrupt building the FileDescriptorProto.
+ // 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.
@@ -1278,13 +1365,13 @@ class LIBPROTOBUF_EXPORT DescriptorPool {
) = 0;
// Reports a warning in the FileDescriptorProto. Use this function if the
- // problem occured should NOT interrupt building the FileDescriptorProto.
+ // 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 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.
) {}
private:
@@ -1394,6 +1481,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool {
friend class ServiceDescriptor;
friend class FileDescriptor;
friend class DescriptorBuilder;
+ friend class FileDescriptorTables;
// Return true if the given name is a sub-symbol of any non-package
// descriptor that already exists in the descriptor pool. (The full
@@ -1494,8 +1582,6 @@ PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int)
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
-PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
- const FieldDescriptor*)
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions)
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
@@ -1544,6 +1630,9 @@ PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
+PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool)
+PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool)
+
PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
@@ -1679,6 +1768,10 @@ inline const FileDescriptor* FileDescriptor::weak_dependency(
return dependencies_[weak_dependencies_[index]];
}
+inline FileDescriptor::Syntax FileDescriptor::syntax() const {
+ return syntax_;
+}
+
// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array
// of pointers rather than the usual array of objects.
inline const FieldDescriptor* OneofDescriptor::field(int index) const {
diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc
index c3aa2fb6..97121fa9 100644
--- a/src/google/protobuf/descriptor.pb.cc
+++ b/src/google/protobuf/descriptor.pb.cc
@@ -103,18 +103,18 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorSet, file_),
};
FileDescriptorSet_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
FileDescriptorSet_descriptor_,
FileDescriptorSet::default_instance_,
FileDescriptorSet_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorSet, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorSet, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(FileDescriptorSet));
+ -1,
+ sizeof(FileDescriptorSet),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorSet, _internal_metadata_),
+ -1);
FileDescriptorProto_descriptor_ = file->message_type(1);
- static const int FileDescriptorProto_offsets_[11] = {
+ static const int FileDescriptorProto_offsets_[12] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, name_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, package_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, dependency_),
@@ -126,18 +126,19 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, extension_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, options_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, source_code_info_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, syntax_),
};
FileDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
FileDescriptorProto_descriptor_,
FileDescriptorProto::default_instance_,
FileDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(FileDescriptorProto));
+ -1,
+ sizeof(FileDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileDescriptorProto, _internal_metadata_),
+ -1);
DescriptorProto_descriptor_ = file->message_type(2);
static const int DescriptorProto_offsets_[8] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto, name_),
@@ -150,32 +151,32 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto, options_),
};
DescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
DescriptorProto_descriptor_,
DescriptorProto::default_instance_,
DescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(DescriptorProto));
+ -1,
+ sizeof(DescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto, _internal_metadata_),
+ -1);
DescriptorProto_ExtensionRange_descriptor_ = DescriptorProto_descriptor_->nested_type(0);
static const int DescriptorProto_ExtensionRange_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto_ExtensionRange, start_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto_ExtensionRange, end_),
};
DescriptorProto_ExtensionRange_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
DescriptorProto_ExtensionRange_descriptor_,
DescriptorProto_ExtensionRange::default_instance_,
DescriptorProto_ExtensionRange_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto_ExtensionRange, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto_ExtensionRange, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(DescriptorProto_ExtensionRange));
+ -1,
+ sizeof(DescriptorProto_ExtensionRange),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(DescriptorProto_ExtensionRange, _internal_metadata_),
+ -1);
FieldDescriptorProto_descriptor_ = file->message_type(3);
static const int FieldDescriptorProto_offsets_[9] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldDescriptorProto, name_),
@@ -189,16 +190,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldDescriptorProto, options_),
};
FieldDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
FieldDescriptorProto_descriptor_,
FieldDescriptorProto::default_instance_,
FieldDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(FieldDescriptorProto));
+ -1,
+ sizeof(FieldDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldDescriptorProto, _internal_metadata_),
+ -1);
FieldDescriptorProto_Type_descriptor_ = FieldDescriptorProto_descriptor_->enum_type(0);
FieldDescriptorProto_Label_descriptor_ = FieldDescriptorProto_descriptor_->enum_type(1);
OneofDescriptorProto_descriptor_ = file->message_type(4);
@@ -206,16 +207,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OneofDescriptorProto, name_),
};
OneofDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
OneofDescriptorProto_descriptor_,
OneofDescriptorProto::default_instance_,
OneofDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OneofDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OneofDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(OneofDescriptorProto));
+ -1,
+ sizeof(OneofDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(OneofDescriptorProto, _internal_metadata_),
+ -1);
EnumDescriptorProto_descriptor_ = file->message_type(5);
static const int EnumDescriptorProto_offsets_[3] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumDescriptorProto, name_),
@@ -223,16 +224,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumDescriptorProto, options_),
};
EnumDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
EnumDescriptorProto_descriptor_,
EnumDescriptorProto::default_instance_,
EnumDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(EnumDescriptorProto));
+ -1,
+ sizeof(EnumDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumDescriptorProto, _internal_metadata_),
+ -1);
EnumValueDescriptorProto_descriptor_ = file->message_type(6);
static const int EnumValueDescriptorProto_offsets_[3] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueDescriptorProto, name_),
@@ -240,16 +241,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueDescriptorProto, options_),
};
EnumValueDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
EnumValueDescriptorProto_descriptor_,
EnumValueDescriptorProto::default_instance_,
EnumValueDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(EnumValueDescriptorProto));
+ -1,
+ sizeof(EnumValueDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueDescriptorProto, _internal_metadata_),
+ -1);
ServiceDescriptorProto_descriptor_ = file->message_type(7);
static const int ServiceDescriptorProto_offsets_[3] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceDescriptorProto, name_),
@@ -257,36 +258,38 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceDescriptorProto, options_),
};
ServiceDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
ServiceDescriptorProto_descriptor_,
ServiceDescriptorProto::default_instance_,
ServiceDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(ServiceDescriptorProto));
+ -1,
+ sizeof(ServiceDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceDescriptorProto, _internal_metadata_),
+ -1);
MethodDescriptorProto_descriptor_ = file->message_type(8);
- static const int MethodDescriptorProto_offsets_[4] = {
+ static const int MethodDescriptorProto_offsets_[6] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, name_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, input_type_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, output_type_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, options_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, client_streaming_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, server_streaming_),
};
MethodDescriptorProto_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
MethodDescriptorProto_descriptor_,
MethodDescriptorProto::default_instance_,
MethodDescriptorProto_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(MethodDescriptorProto));
+ -1,
+ sizeof(MethodDescriptorProto),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodDescriptorProto, _internal_metadata_),
+ -1);
FileOptions_descriptor_ = file->message_type(9);
- static const int FileOptions_offsets_[12] = {
+ static const int FileOptions_offsets_[13] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, java_package_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, java_outer_classname_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, java_multiple_files_),
@@ -298,59 +301,60 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, java_generic_services_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, py_generic_services_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, deprecated_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, cc_enable_arenas_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, uninterpreted_option_),
};
FileOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
FileOptions_descriptor_,
FileOptions::default_instance_,
FileOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(FileOptions));
+ sizeof(FileOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FileOptions, _internal_metadata_),
+ -1);
FileOptions_OptimizeMode_descriptor_ = FileOptions_descriptor_->enum_type(0);
MessageOptions_descriptor_ = file->message_type(10);
- static const int MessageOptions_offsets_[4] = {
+ static const int MessageOptions_offsets_[5] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, message_set_wire_format_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, no_standard_descriptor_accessor_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, deprecated_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, map_entry_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, uninterpreted_option_),
};
MessageOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
MessageOptions_descriptor_,
MessageOptions::default_instance_,
MessageOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(MessageOptions));
+ sizeof(MessageOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MessageOptions, _internal_metadata_),
+ -1);
FieldOptions_descriptor_ = file->message_type(11);
- static const int FieldOptions_offsets_[7] = {
+ static const int FieldOptions_offsets_[6] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, ctype_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, packed_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, lazy_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, deprecated_),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, experimental_map_key_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, weak_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, uninterpreted_option_),
};
FieldOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
FieldOptions_descriptor_,
FieldOptions::default_instance_,
FieldOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(FieldOptions));
+ sizeof(FieldOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(FieldOptions, _internal_metadata_),
+ -1);
FieldOptions_CType_descriptor_ = FieldOptions_descriptor_->enum_type(0);
EnumOptions_descriptor_ = file->message_type(12);
static const int EnumOptions_offsets_[3] = {
@@ -359,64 +363,64 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumOptions, uninterpreted_option_),
};
EnumOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
EnumOptions_descriptor_,
EnumOptions::default_instance_,
EnumOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(EnumOptions));
+ sizeof(EnumOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumOptions, _internal_metadata_),
+ -1);
EnumValueOptions_descriptor_ = file->message_type(13);
static const int EnumValueOptions_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, deprecated_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, uninterpreted_option_),
};
EnumValueOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
EnumValueOptions_descriptor_,
EnumValueOptions::default_instance_,
EnumValueOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(EnumValueOptions));
+ sizeof(EnumValueOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(EnumValueOptions, _internal_metadata_),
+ -1);
ServiceOptions_descriptor_ = file->message_type(14);
static const int ServiceOptions_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, deprecated_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, uninterpreted_option_),
};
ServiceOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
ServiceOptions_descriptor_,
ServiceOptions::default_instance_,
ServiceOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(ServiceOptions));
+ sizeof(ServiceOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(ServiceOptions, _internal_metadata_),
+ -1);
MethodOptions_descriptor_ = file->message_type(15);
static const int MethodOptions_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, deprecated_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, uninterpreted_option_),
};
MethodOptions_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
MethodOptions_descriptor_,
MethodOptions::default_instance_,
MethodOptions_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, _unknown_fields_),
+ -1,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, _extensions_),
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(MethodOptions));
+ sizeof(MethodOptions),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MethodOptions, _internal_metadata_),
+ -1);
UninterpretedOption_descriptor_ = file->message_type(16);
static const int UninterpretedOption_offsets_[7] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption, name_),
@@ -428,47 +432,47 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption, aggregate_value_),
};
UninterpretedOption_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
UninterpretedOption_descriptor_,
UninterpretedOption::default_instance_,
UninterpretedOption_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(UninterpretedOption));
+ -1,
+ sizeof(UninterpretedOption),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption, _internal_metadata_),
+ -1);
UninterpretedOption_NamePart_descriptor_ = UninterpretedOption_descriptor_->nested_type(0);
static const int UninterpretedOption_NamePart_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption_NamePart, name_part_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption_NamePart, is_extension_),
};
UninterpretedOption_NamePart_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
UninterpretedOption_NamePart_descriptor_,
UninterpretedOption_NamePart::default_instance_,
UninterpretedOption_NamePart_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption_NamePart, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption_NamePart, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(UninterpretedOption_NamePart));
+ -1,
+ sizeof(UninterpretedOption_NamePart),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(UninterpretedOption_NamePart, _internal_metadata_),
+ -1);
SourceCodeInfo_descriptor_ = file->message_type(17);
static const int SourceCodeInfo_offsets_[1] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo, location_),
};
SourceCodeInfo_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
SourceCodeInfo_descriptor_,
SourceCodeInfo::default_instance_,
SourceCodeInfo_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(SourceCodeInfo));
+ -1,
+ sizeof(SourceCodeInfo),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo, _internal_metadata_),
+ -1);
SourceCodeInfo_Location_descriptor_ = SourceCodeInfo_descriptor_->nested_type(0);
static const int SourceCodeInfo_Location_offsets_[4] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo_Location, path_),
@@ -477,16 +481,16 @@ void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto() {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo_Location, trailing_comments_),
};
SourceCodeInfo_Location_reflection_ =
- new ::google::protobuf::internal::GeneratedMessageReflection(
+ ::google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(
SourceCodeInfo_Location_descriptor_,
SourceCodeInfo_Location::default_instance_,
SourceCodeInfo_Location_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo_Location, _has_bits_[0]),
- GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo_Location, _unknown_fields_),
-1,
- ::google::protobuf::DescriptorPool::generated_pool(),
- ::google::protobuf::MessageFactory::generated_factory(),
- sizeof(SourceCodeInfo_Location));
+ -1,
+ sizeof(SourceCodeInfo_Location),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(SourceCodeInfo_Location, _internal_metadata_),
+ -1);
}
namespace {
@@ -500,47 +504,47 @@ inline void protobuf_AssignDescriptorsOnce() {
void protobuf_RegisterTypes(const ::std::string&) {
protobuf_AssignDescriptorsOnce();
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- FileDescriptorSet_descriptor_, &FileDescriptorSet::default_instance());
+ FileDescriptorSet_descriptor_, &FileDescriptorSet::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- FileDescriptorProto_descriptor_, &FileDescriptorProto::default_instance());
+ FileDescriptorProto_descriptor_, &FileDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- DescriptorProto_descriptor_, &DescriptorProto::default_instance());
+ DescriptorProto_descriptor_, &DescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- DescriptorProto_ExtensionRange_descriptor_, &DescriptorProto_ExtensionRange::default_instance());
+ DescriptorProto_ExtensionRange_descriptor_, &DescriptorProto_ExtensionRange::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- FieldDescriptorProto_descriptor_, &FieldDescriptorProto::default_instance());
+ FieldDescriptorProto_descriptor_, &FieldDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- OneofDescriptorProto_descriptor_, &OneofDescriptorProto::default_instance());
+ OneofDescriptorProto_descriptor_, &OneofDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- EnumDescriptorProto_descriptor_, &EnumDescriptorProto::default_instance());
+ EnumDescriptorProto_descriptor_, &EnumDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- EnumValueDescriptorProto_descriptor_, &EnumValueDescriptorProto::default_instance());
+ EnumValueDescriptorProto_descriptor_, &EnumValueDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- ServiceDescriptorProto_descriptor_, &ServiceDescriptorProto::default_instance());
+ ServiceDescriptorProto_descriptor_, &ServiceDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- MethodDescriptorProto_descriptor_, &MethodDescriptorProto::default_instance());
+ MethodDescriptorProto_descriptor_, &MethodDescriptorProto::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- FileOptions_descriptor_, &FileOptions::default_instance());
+ FileOptions_descriptor_, &FileOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- MessageOptions_descriptor_, &MessageOptions::default_instance());
+ MessageOptions_descriptor_, &MessageOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- FieldOptions_descriptor_, &FieldOptions::default_instance());
+ FieldOptions_descriptor_, &FieldOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- EnumOptions_descriptor_, &EnumOptions::default_instance());
+ EnumOptions_descriptor_, &EnumOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- EnumValueOptions_descriptor_, &EnumValueOptions::default_instance());
+ EnumValueOptions_descriptor_, &EnumValueOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- ServiceOptions_descriptor_, &ServiceOptions::default_instance());
+ ServiceOptions_descriptor_, &ServiceOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- MethodOptions_descriptor_, &MethodOptions::default_instance());
+ MethodOptions_descriptor_, &MethodOptions::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- UninterpretedOption_descriptor_, &UninterpretedOption::default_instance());
+ UninterpretedOption_descriptor_, &UninterpretedOption::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- UninterpretedOption_NamePart_descriptor_, &UninterpretedOption_NamePart::default_instance());
+ UninterpretedOption_NamePart_descriptor_, &UninterpretedOption_NamePart::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- SourceCodeInfo_descriptor_, &SourceCodeInfo::default_instance());
+ SourceCodeInfo_descriptor_, &SourceCodeInfo::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
- SourceCodeInfo_Location_descriptor_, &SourceCodeInfo_Location::default_instance());
+ SourceCodeInfo_Location_descriptor_, &SourceCodeInfo_Location::default_instance());
}
} // namespace
@@ -600,7 +604,7 @@ void protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto() {
"\n google/protobuf/descriptor.proto\022\017goog"
"le.protobuf\"G\n\021FileDescriptorSet\0222\n\004file"
"\030\001 \003(\0132$.google.protobuf.FileDescriptorP"
- "roto\"\313\003\n\023FileDescriptorProto\022\014\n\004name\030\001 \001"
+ "roto\"\333\003\n\023FileDescriptorProto\022\014\n\004name\030\001 \001"
"(\t\022\017\n\007package\030\002 \001(\t\022\022\n\ndependency\030\003 \003(\t\022"
"\031\n\021public_dependency\030\n \003(\005\022\027\n\017weak_depen"
"dency\030\013 \003(\005\0226\n\014message_type\030\004 \003(\0132 .goog"
@@ -611,104 +615,106 @@ void protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto() {
"\0132%.google.protobuf.FieldDescriptorProto"
"\022-\n\007options\030\010 \001(\0132\034.google.protobuf.File"
"Options\0229\n\020source_code_info\030\t \001(\0132\037.goog"
- "le.protobuf.SourceCodeInfo\"\344\003\n\017Descripto"
- "rProto\022\014\n\004name\030\001 \001(\t\0224\n\005field\030\002 \003(\0132%.go"
- "ogle.protobuf.FieldDescriptorProto\0228\n\tex"
- "tension\030\006 \003(\0132%.google.protobuf.FieldDes"
- "criptorProto\0225\n\013nested_type\030\003 \003(\0132 .goog"
- "le.protobuf.DescriptorProto\0227\n\tenum_type"
- "\030\004 \003(\0132$.google.protobuf.EnumDescriptorP"
- "roto\022H\n\017extension_range\030\005 \003(\0132/.google.p"
- "rotobuf.DescriptorProto.ExtensionRange\0229"
- "\n\noneof_decl\030\010 \003(\0132%.google.protobuf.One"
- "ofDescriptorProto\0220\n\007options\030\007 \001(\0132\037.goo"
- "gle.protobuf.MessageOptions\032,\n\016Extension"
- "Range\022\r\n\005start\030\001 \001(\005\022\013\n\003end\030\002 \001(\005\"\251\005\n\024Fi"
- "eldDescriptorProto\022\014\n\004name\030\001 \001(\t\022\016\n\006numb"
- "er\030\003 \001(\005\022:\n\005label\030\004 \001(\0162+.google.protobu"
- "f.FieldDescriptorProto.Label\0228\n\004type\030\005 \001"
- "(\0162*.google.protobuf.FieldDescriptorProt"
- "o.Type\022\021\n\ttype_name\030\006 \001(\t\022\020\n\010extendee\030\002 "
- "\001(\t\022\025\n\rdefault_value\030\007 \001(\t\022\023\n\013oneof_inde"
- "x\030\t \001(\005\022.\n\007options\030\010 \001(\0132\035.google.protob"
- "uf.FieldOptions\"\266\002\n\004Type\022\017\n\013TYPE_DOUBLE\020"
- "\001\022\016\n\nTYPE_FLOAT\020\002\022\016\n\nTYPE_INT64\020\003\022\017\n\013TYP"
- "E_UINT64\020\004\022\016\n\nTYPE_INT32\020\005\022\020\n\014TYPE_FIXED"
- "64\020\006\022\020\n\014TYPE_FIXED32\020\007\022\r\n\tTYPE_BOOL\020\010\022\017\n"
- "\013TYPE_STRING\020\t\022\016\n\nTYPE_GROUP\020\n\022\020\n\014TYPE_M"
- "ESSAGE\020\013\022\016\n\nTYPE_BYTES\020\014\022\017\n\013TYPE_UINT32\020"
- "\r\022\r\n\tTYPE_ENUM\020\016\022\021\n\rTYPE_SFIXED32\020\017\022\021\n\rT"
- "YPE_SFIXED64\020\020\022\017\n\013TYPE_SINT32\020\021\022\017\n\013TYPE_"
- "SINT64\020\022\"C\n\005Label\022\022\n\016LABEL_OPTIONAL\020\001\022\022\n"
- "\016LABEL_REQUIRED\020\002\022\022\n\016LABEL_REPEATED\020\003\"$\n"
- "\024OneofDescriptorProto\022\014\n\004name\030\001 \001(\t\"\214\001\n\023"
- "EnumDescriptorProto\022\014\n\004name\030\001 \001(\t\0228\n\005val"
- "ue\030\002 \003(\0132).google.protobuf.EnumValueDesc"
- "riptorProto\022-\n\007options\030\003 \001(\0132\034.google.pr"
- "otobuf.EnumOptions\"l\n\030EnumValueDescripto"
- "rProto\022\014\n\004name\030\001 \001(\t\022\016\n\006number\030\002 \001(\005\0222\n\007"
- "options\030\003 \001(\0132!.google.protobuf.EnumValu"
- "eOptions\"\220\001\n\026ServiceDescriptorProto\022\014\n\004n"
- "ame\030\001 \001(\t\0226\n\006method\030\002 \003(\0132&.google.proto"
- "buf.MethodDescriptorProto\0220\n\007options\030\003 \001"
- "(\0132\037.google.protobuf.ServiceOptions\"\177\n\025M"
- "ethodDescriptorProto\022\014\n\004name\030\001 \001(\t\022\022\n\nin"
- "put_type\030\002 \001(\t\022\023\n\013output_type\030\003 \001(\t\022/\n\007o"
- "ptions\030\004 \001(\0132\036.google.protobuf.MethodOpt"
- "ions\"\253\004\n\013FileOptions\022\024\n\014java_package\030\001 \001"
- "(\t\022\034\n\024java_outer_classname\030\010 \001(\t\022\"\n\023java"
- "_multiple_files\030\n \001(\010:\005false\022,\n\035java_gen"
- "erate_equals_and_hash\030\024 \001(\010:\005false\022%\n\026ja"
- "va_string_check_utf8\030\033 \001(\010:\005false\022F\n\014opt"
- "imize_for\030\t \001(\0162).google.protobuf.FileOp"
- "tions.OptimizeMode:\005SPEED\022\022\n\ngo_package\030"
- "\013 \001(\t\022\"\n\023cc_generic_services\030\020 \001(\010:\005fals"
- "e\022$\n\025java_generic_services\030\021 \001(\010:\005false\022"
- "\"\n\023py_generic_services\030\022 \001(\010:\005false\022\031\n\nd"
- "eprecated\030\027 \001(\010:\005false\022C\n\024uninterpreted_"
- "option\030\347\007 \003(\0132$.google.protobuf.Uninterp"
- "retedOption\":\n\014OptimizeMode\022\t\n\005SPEED\020\001\022\r"
- "\n\tCODE_SIZE\020\002\022\020\n\014LITE_RUNTIME\020\003*\t\010\350\007\020\200\200\200"
- "\200\002\"\323\001\n\016MessageOptions\022&\n\027message_set_wir"
- "e_format\030\001 \001(\010:\005false\022.\n\037no_standard_des"
- "criptor_accessor\030\002 \001(\010:\005false\022\031\n\ndepreca"
- "ted\030\003 \001(\010:\005false\022C\n\024uninterpreted_option"
- "\030\347\007 \003(\0132$.google.protobuf.UninterpretedO"
- "ption*\t\010\350\007\020\200\200\200\200\002\"\276\002\n\014FieldOptions\022:\n\005cty"
- "pe\030\001 \001(\0162#.google.protobuf.FieldOptions."
- "CType:\006STRING\022\016\n\006packed\030\002 \001(\010\022\023\n\004lazy\030\005 "
- "\001(\010:\005false\022\031\n\ndeprecated\030\003 \001(\010:\005false\022\034\n"
- "\024experimental_map_key\030\t \001(\t\022\023\n\004weak\030\n \001("
- "\010:\005false\022C\n\024uninterpreted_option\030\347\007 \003(\0132"
- "$.google.protobuf.UninterpretedOption\"/\n"
- "\005CType\022\n\n\006STRING\020\000\022\010\n\004CORD\020\001\022\020\n\014STRING_P"
- "IECE\020\002*\t\010\350\007\020\200\200\200\200\002\"\215\001\n\013EnumOptions\022\023\n\013all"
- "ow_alias\030\002 \001(\010\022\031\n\ndeprecated\030\003 \001(\010:\005fals"
- "e\022C\n\024uninterpreted_option\030\347\007 \003(\0132$.googl"
- "e.protobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200"
- "\002\"}\n\020EnumValueOptions\022\031\n\ndeprecated\030\001 \001("
- "\010:\005false\022C\n\024uninterpreted_option\030\347\007 \003(\0132"
- "$.google.protobuf.UninterpretedOption*\t\010"
- "\350\007\020\200\200\200\200\002\"{\n\016ServiceOptions\022\031\n\ndeprecated"
- "\030! \001(\010:\005false\022C\n\024uninterpreted_option\030\347\007"
- " \003(\0132$.google.protobuf.UninterpretedOpti"
- "on*\t\010\350\007\020\200\200\200\200\002\"z\n\rMethodOptions\022\031\n\ndeprec"
- "ated\030! \001(\010:\005false\022C\n\024uninterpreted_optio"
- "n\030\347\007 \003(\0132$.google.protobuf.Uninterpreted"
- "Option*\t\010\350\007\020\200\200\200\200\002\"\236\002\n\023UninterpretedOptio"
- "n\022;\n\004name\030\002 \003(\0132-.google.protobuf.Uninte"
- "rpretedOption.NamePart\022\030\n\020identifier_val"
- "ue\030\003 \001(\t\022\032\n\022positive_int_value\030\004 \001(\004\022\032\n\022"
- "negative_int_value\030\005 \001(\003\022\024\n\014double_value"
- "\030\006 \001(\001\022\024\n\014string_value\030\007 \001(\014\022\027\n\017aggregat"
- "e_value\030\010 \001(\t\0323\n\010NamePart\022\021\n\tname_part\030\001"
- " \002(\t\022\024\n\014is_extension\030\002 \002(\010\"\261\001\n\016SourceCod"
- "eInfo\022:\n\010location\030\001 \003(\0132(.google.protobu"
- "f.SourceCodeInfo.Location\032c\n\010Location\022\020\n"
- "\004path\030\001 \003(\005B\002\020\001\022\020\n\004span\030\002 \003(\005B\002\020\001\022\030\n\020lea"
- "ding_comments\030\003 \001(\t\022\031\n\021trailing_comments"
- "\030\004 \001(\tB)\n\023com.google.protobufB\020Descripto"
- "rProtosH\001", 4449);
+ "le.protobuf.SourceCodeInfo\022\016\n\006syntax\030\014 \001"
+ "(\t\"\344\003\n\017DescriptorProto\022\014\n\004name\030\001 \001(\t\0224\n\005"
+ "field\030\002 \003(\0132%.google.protobuf.FieldDescr"
+ "iptorProto\0228\n\textension\030\006 \003(\0132%.google.p"
+ "rotobuf.FieldDescriptorProto\0225\n\013nested_t"
+ "ype\030\003 \003(\0132 .google.protobuf.DescriptorPr"
+ "oto\0227\n\tenum_type\030\004 \003(\0132$.google.protobuf"
+ ".EnumDescriptorProto\022H\n\017extension_range\030"
+ "\005 \003(\0132/.google.protobuf.DescriptorProto."
+ "ExtensionRange\0229\n\noneof_decl\030\010 \003(\0132%.goo"
+ "gle.protobuf.OneofDescriptorProto\0220\n\007opt"
+ "ions\030\007 \001(\0132\037.google.protobuf.MessageOpti"
+ "ons\032,\n\016ExtensionRange\022\r\n\005start\030\001 \001(\005\022\013\n\003"
+ "end\030\002 \001(\005\"\251\005\n\024FieldDescriptorProto\022\014\n\004na"
+ "me\030\001 \001(\t\022\016\n\006number\030\003 \001(\005\022:\n\005label\030\004 \001(\0162"
+ "+.google.protobuf.FieldDescriptorProto.L"
+ "abel\0228\n\004type\030\005 \001(\0162*.google.protobuf.Fie"
+ "ldDescriptorProto.Type\022\021\n\ttype_name\030\006 \001("
+ "\t\022\020\n\010extendee\030\002 \001(\t\022\025\n\rdefault_value\030\007 \001"
+ "(\t\022\023\n\013oneof_index\030\t \001(\005\022.\n\007options\030\010 \001(\013"
+ "2\035.google.protobuf.FieldOptions\"\266\002\n\004Type"
+ "\022\017\n\013TYPE_DOUBLE\020\001\022\016\n\nTYPE_FLOAT\020\002\022\016\n\nTYP"
+ "E_INT64\020\003\022\017\n\013TYPE_UINT64\020\004\022\016\n\nTYPE_INT32"
+ "\020\005\022\020\n\014TYPE_FIXED64\020\006\022\020\n\014TYPE_FIXED32\020\007\022\r"
+ "\n\tTYPE_BOOL\020\010\022\017\n\013TYPE_STRING\020\t\022\016\n\nTYPE_G"
+ "ROUP\020\n\022\020\n\014TYPE_MESSAGE\020\013\022\016\n\nTYPE_BYTES\020\014"
+ "\022\017\n\013TYPE_UINT32\020\r\022\r\n\tTYPE_ENUM\020\016\022\021\n\rTYPE"
+ "_SFIXED32\020\017\022\021\n\rTYPE_SFIXED64\020\020\022\017\n\013TYPE_S"
+ "INT32\020\021\022\017\n\013TYPE_SINT64\020\022\"C\n\005Label\022\022\n\016LAB"
+ "EL_OPTIONAL\020\001\022\022\n\016LABEL_REQUIRED\020\002\022\022\n\016LAB"
+ "EL_REPEATED\020\003\"$\n\024OneofDescriptorProto\022\014\n"
+ "\004name\030\001 \001(\t\"\214\001\n\023EnumDescriptorProto\022\014\n\004n"
+ "ame\030\001 \001(\t\0228\n\005value\030\002 \003(\0132).google.protob"
+ "uf.EnumValueDescriptorProto\022-\n\007options\030\003"
+ " \001(\0132\034.google.protobuf.EnumOptions\"l\n\030En"
+ "umValueDescriptorProto\022\014\n\004name\030\001 \001(\t\022\016\n\006"
+ "number\030\002 \001(\005\0222\n\007options\030\003 \001(\0132!.google.p"
+ "rotobuf.EnumValueOptions\"\220\001\n\026ServiceDesc"
+ "riptorProto\022\014\n\004name\030\001 \001(\t\0226\n\006method\030\002 \003("
+ "\0132&.google.protobuf.MethodDescriptorProt"
+ "o\0220\n\007options\030\003 \001(\0132\037.google.protobuf.Ser"
+ "viceOptions\"\301\001\n\025MethodDescriptorProto\022\014\n"
+ "\004name\030\001 \001(\t\022\022\n\ninput_type\030\002 \001(\t\022\023\n\013outpu"
+ "t_type\030\003 \001(\t\022/\n\007options\030\004 \001(\0132\036.google.p"
+ "rotobuf.MethodOptions\022\037\n\020client_streamin"
+ "g\030\005 \001(\010:\005false\022\037\n\020server_streaming\030\006 \001(\010"
+ ":\005false\"\314\004\n\013FileOptions\022\024\n\014java_package\030"
+ "\001 \001(\t\022\034\n\024java_outer_classname\030\010 \001(\t\022\"\n\023j"
+ "ava_multiple_files\030\n \001(\010:\005false\022,\n\035java_"
+ "generate_equals_and_hash\030\024 \001(\010:\005false\022%\n"
+ "\026java_string_check_utf8\030\033 \001(\010:\005false\022F\n\014"
+ "optimize_for\030\t \001(\0162).google.protobuf.Fil"
+ "eOptions.OptimizeMode:\005SPEED\022\022\n\ngo_packa"
+ "ge\030\013 \001(\t\022\"\n\023cc_generic_services\030\020 \001(\010:\005f"
+ "alse\022$\n\025java_generic_services\030\021 \001(\010:\005fal"
+ "se\022\"\n\023py_generic_services\030\022 \001(\010:\005false\022\031"
+ "\n\ndeprecated\030\027 \001(\010:\005false\022\037\n\020cc_enable_a"
+ "renas\030\037 \001(\010:\005false\022C\n\024uninterpreted_opti"
+ "on\030\347\007 \003(\0132$.google.protobuf.Uninterprete"
+ "dOption\":\n\014OptimizeMode\022\t\n\005SPEED\020\001\022\r\n\tCO"
+ "DE_SIZE\020\002\022\020\n\014LITE_RUNTIME\020\003*\t\010\350\007\020\200\200\200\200\002\"\346"
+ "\001\n\016MessageOptions\022&\n\027message_set_wire_fo"
+ "rmat\030\001 \001(\010:\005false\022.\n\037no_standard_descrip"
+ "tor_accessor\030\002 \001(\010:\005false\022\031\n\ndeprecated\030"
+ "\003 \001(\010:\005false\022\021\n\tmap_entry\030\007 \001(\010\022C\n\024unint"
+ "erpreted_option\030\347\007 \003(\0132$.google.protobuf"
+ ".UninterpretedOption*\t\010\350\007\020\200\200\200\200\002\"\240\002\n\014Fiel"
+ "dOptions\022:\n\005ctype\030\001 \001(\0162#.google.protobu"
+ "f.FieldOptions.CType:\006STRING\022\016\n\006packed\030\002"
+ " \001(\010\022\023\n\004lazy\030\005 \001(\010:\005false\022\031\n\ndeprecated\030"
+ "\003 \001(\010:\005false\022\023\n\004weak\030\n \001(\010:\005false\022C\n\024uni"
+ "nterpreted_option\030\347\007 \003(\0132$.google.protob"
+ "uf.UninterpretedOption\"/\n\005CType\022\n\n\006STRIN"
+ "G\020\000\022\010\n\004CORD\020\001\022\020\n\014STRING_PIECE\020\002*\t\010\350\007\020\200\200\200"
+ "\200\002\"\215\001\n\013EnumOptions\022\023\n\013allow_alias\030\002 \001(\010\022"
+ "\031\n\ndeprecated\030\003 \001(\010:\005false\022C\n\024uninterpre"
+ "ted_option\030\347\007 \003(\0132$.google.protobuf.Unin"
+ "terpretedOption*\t\010\350\007\020\200\200\200\200\002\"}\n\020EnumValueO"
+ "ptions\022\031\n\ndeprecated\030\001 \001(\010:\005false\022C\n\024uni"
+ "nterpreted_option\030\347\007 \003(\0132$.google.protob"
+ "uf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002\"{\n\016Ser"
+ "viceOptions\022\031\n\ndeprecated\030! \001(\010:\005false\022C"
+ "\n\024uninterpreted_option\030\347\007 \003(\0132$.google.p"
+ "rotobuf.UninterpretedOption*\t\010\350\007\020\200\200\200\200\002\"z"
+ "\n\rMethodOptions\022\031\n\ndeprecated\030! \001(\010:\005fal"
+ "se\022C\n\024uninterpreted_option\030\347\007 \003(\0132$.goog"
+ "le.protobuf.UninterpretedOption*\t\010\350\007\020\200\200\200"
+ "\200\002\"\236\002\n\023UninterpretedOption\022;\n\004name\030\002 \003(\013"
+ "2-.google.protobuf.UninterpretedOption.N"
+ "amePart\022\030\n\020identifier_value\030\003 \001(\t\022\032\n\022pos"
+ "itive_int_value\030\004 \001(\004\022\032\n\022negative_int_va"
+ "lue\030\005 \001(\003\022\024\n\014double_value\030\006 \001(\001\022\024\n\014strin"
+ "g_value\030\007 \001(\014\022\027\n\017aggregate_value\030\010 \001(\t\0323"
+ "\n\010NamePart\022\021\n\tname_part\030\001 \002(\t\022\024\n\014is_exte"
+ "nsion\030\002 \002(\010\"\261\001\n\016SourceCodeInfo\022:\n\010locati"
+ "on\030\001 \003(\0132(.google.protobuf.SourceCodeInf"
+ "o.Location\032c\n\010Location\022\020\n\004path\030\001 \003(\005B\002\020\001"
+ "\022\020\n\004span\030\002 \003(\005B\002\020\001\022\030\n\020leading_comments\030\003"
+ " \001(\t\022\031\n\021trailing_comments\030\004 \001(\tB)\n\023com.g"
+ "oogle.protobufB\020DescriptorProtosH\001", 4554);
::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
"google/protobuf/descriptor.proto", &protobuf_RegisterTypes);
FileDescriptorSet::default_instance_ = new FileDescriptorSet();
@@ -763,6 +769,16 @@ struct StaticDescriptorInitializer_google_2fprotobuf_2fdescriptor_2eproto {
}
} static_descriptor_initializer_google_2fprotobuf_2fdescriptor_2eproto_;
+namespace {
+
+static void MergeFromFail(int line) GOOGLE_ATTRIBUTE_COLD;
+static void MergeFromFail(int line) {
+ GOOGLE_CHECK(false) << __FILE__ << ":" << line;
+}
+
+} // namespace
+
+
// ===================================================================
#ifndef _MSC_VER
@@ -770,7 +786,7 @@ const int FileDescriptorSet::kFileFieldNumber;
#endif // !_MSC_VER
FileDescriptorSet::FileDescriptorSet()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.FileDescriptorSet)
}
@@ -779,7 +795,8 @@ void FileDescriptorSet::InitAsDefaultInstance() {
}
FileDescriptorSet::FileDescriptorSet(const FileDescriptorSet& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.FileDescriptorSet)
@@ -817,14 +834,20 @@ const FileDescriptorSet& FileDescriptorSet::default_instance() {
FileDescriptorSet* FileDescriptorSet::default_instance_ = NULL;
-FileDescriptorSet* FileDescriptorSet::New() const {
- return new FileDescriptorSet;
+FileDescriptorSet* FileDescriptorSet::New(::google::protobuf::Arena* arena) const {
+ FileDescriptorSet* n = new FileDescriptorSet;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void FileDescriptorSet::Clear() {
file_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool FileDescriptorSet::MergePartialFromCodedStream(
@@ -877,12 +900,12 @@ void FileDescriptorSet::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// @@protoc_insertion_point(serialize_start:google.protobuf.FileDescriptorSet)
// repeated .google.protobuf.FileDescriptorProto file = 1;
- for (int i = 0; i < this->file_size(); i++) {
+ for (unsigned int i = 0, n = this->file_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
1, this->file(i), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -893,13 +916,13 @@ void FileDescriptorSet::SerializeWithCachedSizes(
::google::protobuf::uint8* target) const {
// @@protoc_insertion_point(serialize_to_array_start:google.protobuf.FileDescriptorSet)
// repeated .google.protobuf.FileDescriptorProto file = 1;
- for (int i = 0; i < this->file_size(); i++) {
+ for (unsigned int i = 0, n = this->file_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
1, this->file(i), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -918,7 +941,7 @@ int FileDescriptorSet::ByteSize() const {
this->file(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -930,7 +953,7 @@ int FileDescriptorSet::ByteSize() const {
}
void FileDescriptorSet::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const FileDescriptorSet* source =
::google::protobuf::internal::dynamic_cast_if_available<const FileDescriptorSet*>(
&from);
@@ -942,9 +965,11 @@ void FileDescriptorSet::MergeFrom(const ::google::protobuf::Message& from) {
}
void FileDescriptorSet::MergeFrom(const FileDescriptorSet& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
file_.MergeFrom(from.file_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void FileDescriptorSet::CopyFrom(const ::google::protobuf::Message& from) {
@@ -966,12 +991,14 @@ bool FileDescriptorSet::IsInitialized() const {
}
void FileDescriptorSet::Swap(FileDescriptorSet* other) {
- if (other != this) {
- file_.Swap(&other->file_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void FileDescriptorSet::InternalSwap(FileDescriptorSet* other) {
+ file_.UnsafeArenaSwap(&other->file_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata FileDescriptorSet::GetMetadata() const {
@@ -997,10 +1024,11 @@ const int FileDescriptorProto::kServiceFieldNumber;
const int FileDescriptorProto::kExtensionFieldNumber;
const int FileDescriptorProto::kOptionsFieldNumber;
const int FileDescriptorProto::kSourceCodeInfoFieldNumber;
+const int FileDescriptorProto::kSyntaxFieldNumber;
#endif // !_MSC_VER
FileDescriptorProto::FileDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.FileDescriptorProto)
}
@@ -1011,7 +1039,8 @@ void FileDescriptorProto::InitAsDefaultInstance() {
}
FileDescriptorProto::FileDescriptorProto(const FileDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.FileDescriptorProto)
@@ -1020,10 +1049,11 @@ FileDescriptorProto::FileDescriptorProto(const FileDescriptorProto& from)
void FileDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
options_ = NULL;
source_code_info_ = NULL;
+ syntax_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -1033,12 +1063,9 @@ FileDescriptorProto::~FileDescriptorProto() {
}
void FileDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete package_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ syntax_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
delete source_code_info_;
@@ -1062,30 +1089,33 @@ const FileDescriptorProto& FileDescriptorProto::default_instance() {
FileDescriptorProto* FileDescriptorProto::default_instance_ = NULL;
-FileDescriptorProto* FileDescriptorProto::New() const {
- return new FileDescriptorProto;
+FileDescriptorProto* FileDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ FileDescriptorProto* n = new FileDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void FileDescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 3) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_package()) {
- if (package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_->clear();
- }
+ package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
}
- if (_has_bits_[8 / 32] & 1536) {
+ if (_has_bits_[8 / 32] & 3584) {
if (has_options()) {
if (options_ != NULL) options_->::google::protobuf::FileOptions::Clear();
}
if (has_source_code_info()) {
if (source_code_info_ != NULL) source_code_info_->::google::protobuf::SourceCodeInfo::Clear();
}
+ if (has_syntax()) {
+ syntax_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ }
}
dependency_.Clear();
public_dependency_.Clear();
@@ -1095,7 +1125,9 @@ void FileDescriptorProto::Clear() {
service_.Clear();
extension_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool FileDescriptorProto::MergePartialFromCodedStream(
@@ -1116,7 +1148,7 @@ bool FileDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.FileDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -1133,7 +1165,7 @@ bool FileDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->package().data(), this->package().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "package");
+ "google.protobuf.FileDescriptorProto.package");
} else {
goto handle_unusual;
}
@@ -1151,7 +1183,7 @@ bool FileDescriptorProto::MergePartialFromCodedStream(
this->dependency(this->dependency_size() - 1).data(),
this->dependency(this->dependency_size() - 1).length(),
::google::protobuf::internal::WireFormat::PARSE,
- "dependency");
+ "google.protobuf.FileDescriptorProto.dependency");
} else {
goto handle_unusual;
}
@@ -1276,6 +1308,23 @@ bool FileDescriptorProto::MergePartialFromCodedStream(
goto handle_unusual;
}
if (input->ExpectTag(88)) goto parse_weak_dependency;
+ if (input->ExpectTag(98)) goto parse_syntax;
+ break;
+ }
+
+ // optional string syntax = 12;
+ case 12: {
+ if (tag == 98) {
+ parse_syntax:
+ DO_(::google::protobuf::internal::WireFormatLite::ReadString(
+ input, this->mutable_syntax()));
+ ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+ this->syntax().data(), this->syntax().length(),
+ ::google::protobuf::internal::WireFormat::PARSE,
+ "google.protobuf.FileDescriptorProto.syntax");
+ } else {
+ goto handle_unusual;
+ }
if (input->ExpectAtEnd()) goto success;
break;
}
@@ -1310,7 +1359,7 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.FileDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
@@ -1320,7 +1369,7 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->package().data(), this->package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "package");
+ "google.protobuf.FileDescriptorProto.package");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->package(), output);
}
@@ -1330,31 +1379,31 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->dependency(i).data(), this->dependency(i).length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "dependency");
+ "google.protobuf.FileDescriptorProto.dependency");
::google::protobuf::internal::WireFormatLite::WriteString(
3, this->dependency(i), output);
}
// repeated .google.protobuf.DescriptorProto message_type = 4;
- for (int i = 0; i < this->message_type_size(); i++) {
+ for (unsigned int i = 0, n = this->message_type_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
4, this->message_type(i), output);
}
// repeated .google.protobuf.EnumDescriptorProto enum_type = 5;
- for (int i = 0; i < this->enum_type_size(); i++) {
+ for (unsigned int i = 0, n = this->enum_type_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
5, this->enum_type(i), output);
}
// repeated .google.protobuf.ServiceDescriptorProto service = 6;
- for (int i = 0; i < this->service_size(); i++) {
+ for (unsigned int i = 0, n = this->service_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
6, this->service(i), output);
}
// repeated .google.protobuf.FieldDescriptorProto extension = 7;
- for (int i = 0; i < this->extension_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
7, this->extension(i), output);
}
@@ -1362,13 +1411,13 @@ void FileDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.FileOptions options = 8;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 8, this->options(), output);
+ 8, *this->options_, output);
}
// optional .google.protobuf.SourceCodeInfo source_code_info = 9;
if (has_source_code_info()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 9, this->source_code_info(), output);
+ 9, *this->source_code_info_, output);
}
// repeated int32 public_dependency = 10;
@@ -1383,7 +1432,17 @@ void FileDescriptorProto::SerializeWithCachedSizes(
11, this->weak_dependency(i), output);
}
- if (!unknown_fields().empty()) {
+ // optional string syntax = 12;
+ if (has_syntax()) {
+ ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+ this->syntax().data(), 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(
unknown_fields(), output);
}
@@ -1398,7 +1457,7 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.FileDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
@@ -1409,7 +1468,7 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->package().data(), this->package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "package");
+ "google.protobuf.FileDescriptorProto.package");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->package(), target);
@@ -1420,34 +1479,34 @@ void FileDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->dependency(i).data(), this->dependency(i).length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "dependency");
+ "google.protobuf.FileDescriptorProto.dependency");
target = ::google::protobuf::internal::WireFormatLite::
WriteStringToArray(3, this->dependency(i), target);
}
// repeated .google.protobuf.DescriptorProto message_type = 4;
- for (int i = 0; i < this->message_type_size(); i++) {
+ for (unsigned int i = 0, n = this->message_type_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
4, this->message_type(i), target);
}
// repeated .google.protobuf.EnumDescriptorProto enum_type = 5;
- for (int i = 0; i < this->enum_type_size(); i++) {
+ for (unsigned int i = 0, n = this->enum_type_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
5, this->enum_type(i), target);
}
// repeated .google.protobuf.ServiceDescriptorProto service = 6;
- for (int i = 0; i < this->service_size(); i++) {
+ for (unsigned int i = 0, n = this->service_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
6, this->service(i), target);
}
// repeated .google.protobuf.FieldDescriptorProto extension = 7;
- for (int i = 0; i < this->extension_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
7, this->extension(i), target);
@@ -1457,14 +1516,14 @@ void FileDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 8, this->options(), target);
+ 8, *this->options_, target);
}
// optional .google.protobuf.SourceCodeInfo source_code_info = 9;
if (has_source_code_info()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 9, this->source_code_info(), target);
+ 9, *this->source_code_info_, target);
}
// repeated int32 public_dependency = 10;
@@ -1479,7 +1538,18 @@ void FileDescriptorProto::SerializeWithCachedSizes(
WriteInt32ToArray(11, this->weak_dependency(i), target);
}
- if (!unknown_fields().empty()) {
+ // optional string syntax = 12;
+ if (has_syntax()) {
+ ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
+ this->syntax().data(), this->syntax().length(),
+ ::google::protobuf::internal::WireFormat::SERIALIZE,
+ "google.protobuf.FileDescriptorProto.syntax");
+ target =
+ ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
+ 12, this->syntax(), target);
+ }
+
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -1490,7 +1560,7 @@ void FileDescriptorProto::SerializeWithCachedSizes(
int FileDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 3) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -1506,19 +1576,26 @@ int FileDescriptorProto::ByteSize() const {
}
}
- if (_has_bits_[9 / 32] & (0xffu << (9 % 32))) {
+ if (_has_bits_[9 / 32] & 3584) {
// optional .google.protobuf.FileOptions options = 8;
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
}
// optional .google.protobuf.SourceCodeInfo source_code_info = 9;
if (has_source_code_info()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->source_code_info());
+ *this->source_code_info_);
+ }
+
+ // optional string syntax = 12;
+ if (has_syntax()) {
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->syntax());
}
}
@@ -1581,7 +1658,7 @@ int FileDescriptorProto::ByteSize() const {
this->extension(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -1593,7 +1670,7 @@ int FileDescriptorProto::ByteSize() const {
}
void FileDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const FileDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const FileDescriptorProto*>(
&from);
@@ -1605,7 +1682,7 @@ void FileDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void FileDescriptorProto::MergeFrom(const FileDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
dependency_.MergeFrom(from.dependency_);
public_dependency_.MergeFrom(from.public_dependency_);
weak_dependency_.MergeFrom(from.weak_dependency_);
@@ -1615,10 +1692,12 @@ void FileDescriptorProto::MergeFrom(const FileDescriptorProto& from) {
extension_.MergeFrom(from.extension_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_package()) {
- set_package(from.package());
+ set_has_package();
+ package_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.package_);
}
}
if (from._has_bits_[9 / 32] & (0xffu << (9 % 32))) {
@@ -1628,8 +1707,14 @@ void FileDescriptorProto::MergeFrom(const FileDescriptorProto& from) {
if (from.has_source_code_info()) {
mutable_source_code_info()->::google::protobuf::SourceCodeInfo::MergeFrom(from.source_code_info());
}
+ if (from.has_syntax()) {
+ set_has_syntax();
+ syntax_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.syntax_);
+ }
+ }
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
}
void FileDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -1651,28 +1736,31 @@ bool FileDescriptorProto::IsInitialized() const {
if (!::google::protobuf::internal::AllAreInitialized(this->service())) return false;
if (!::google::protobuf::internal::AllAreInitialized(this->extension())) return false;
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void FileDescriptorProto::Swap(FileDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(package_, other->package_);
- dependency_.Swap(&other->dependency_);
- public_dependency_.Swap(&other->public_dependency_);
- weak_dependency_.Swap(&other->weak_dependency_);
- message_type_.Swap(&other->message_type_);
- enum_type_.Swap(&other->enum_type_);
- service_.Swap(&other->service_);
- extension_.Swap(&other->extension_);
- std::swap(options_, other->options_);
- std::swap(source_code_info_, other->source_code_info_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void FileDescriptorProto::InternalSwap(FileDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ package_.Swap(&other->package_);
+ dependency_.UnsafeArenaSwap(&other->dependency_);
+ public_dependency_.UnsafeArenaSwap(&other->public_dependency_);
+ weak_dependency_.UnsafeArenaSwap(&other->weak_dependency_);
+ message_type_.UnsafeArenaSwap(&other->message_type_);
+ enum_type_.UnsafeArenaSwap(&other->enum_type_);
+ service_.UnsafeArenaSwap(&other->service_);
+ extension_.UnsafeArenaSwap(&other->extension_);
+ std::swap(options_, other->options_);
+ std::swap(source_code_info_, other->source_code_info_);
+ syntax_.Swap(&other->syntax_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata FileDescriptorProto::GetMetadata() const {
@@ -1692,7 +1780,7 @@ const int DescriptorProto_ExtensionRange::kEndFieldNumber;
#endif // !_MSC_VER
DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.DescriptorProto.ExtensionRange)
}
@@ -1701,7 +1789,8 @@ void DescriptorProto_ExtensionRange::InitAsDefaultInstance() {
}
DescriptorProto_ExtensionRange::DescriptorProto_ExtensionRange(const DescriptorProto_ExtensionRange& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.DescriptorProto.ExtensionRange)
@@ -1741,8 +1830,12 @@ const DescriptorProto_ExtensionRange& DescriptorProto_ExtensionRange::default_in
DescriptorProto_ExtensionRange* DescriptorProto_ExtensionRange::default_instance_ = NULL;
-DescriptorProto_ExtensionRange* DescriptorProto_ExtensionRange::New() const {
- return new DescriptorProto_ExtensionRange;
+DescriptorProto_ExtensionRange* DescriptorProto_ExtensionRange::New(::google::protobuf::Arena* arena) const {
+ DescriptorProto_ExtensionRange* n = new DescriptorProto_ExtensionRange;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void DescriptorProto_ExtensionRange::Clear() {
@@ -1762,7 +1855,9 @@ void DescriptorProto_ExtensionRange::Clear() {
#undef ZR_
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool DescriptorProto_ExtensionRange::MergePartialFromCodedStream(
@@ -1839,7 +1934,7 @@ void DescriptorProto_ExtensionRange::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteInt32(2, this->end(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -1859,7 +1954,7 @@ void DescriptorProto_ExtensionRange::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this->end(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -1870,7 +1965,7 @@ void DescriptorProto_ExtensionRange::SerializeWithCachedSizes(
int DescriptorProto_ExtensionRange::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 3) {
// optional int32 start = 1;
if (has_start()) {
total_size += 1 +
@@ -1886,7 +1981,7 @@ int DescriptorProto_ExtensionRange::ByteSize() const {
}
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -1898,7 +1993,7 @@ int DescriptorProto_ExtensionRange::ByteSize() const {
}
void DescriptorProto_ExtensionRange::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const DescriptorProto_ExtensionRange* source =
::google::protobuf::internal::dynamic_cast_if_available<const DescriptorProto_ExtensionRange*>(
&from);
@@ -1910,7 +2005,7 @@ void DescriptorProto_ExtensionRange::MergeFrom(const ::google::protobuf::Message
}
void DescriptorProto_ExtensionRange::MergeFrom(const DescriptorProto_ExtensionRange& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_start()) {
set_start(from.start());
@@ -1919,7 +2014,9 @@ void DescriptorProto_ExtensionRange::MergeFrom(const DescriptorProto_ExtensionRa
set_end(from.end());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void DescriptorProto_ExtensionRange::CopyFrom(const ::google::protobuf::Message& from) {
@@ -1940,13 +2037,15 @@ bool DescriptorProto_ExtensionRange::IsInitialized() const {
}
void DescriptorProto_ExtensionRange::Swap(DescriptorProto_ExtensionRange* other) {
- if (other != this) {
- std::swap(start_, other->start_);
- std::swap(end_, other->end_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void DescriptorProto_ExtensionRange::InternalSwap(DescriptorProto_ExtensionRange* other) {
+ std::swap(start_, other->start_);
+ std::swap(end_, other->end_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata DescriptorProto_ExtensionRange::GetMetadata() const {
@@ -1972,7 +2071,7 @@ const int DescriptorProto::kOptionsFieldNumber;
#endif // !_MSC_VER
DescriptorProto::DescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.DescriptorProto)
}
@@ -1982,7 +2081,8 @@ void DescriptorProto::InitAsDefaultInstance() {
}
DescriptorProto::DescriptorProto(const DescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.DescriptorProto)
@@ -1991,7 +2091,7 @@ DescriptorProto::DescriptorProto(const DescriptorProto& from)
void DescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
options_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -2002,9 +2102,7 @@ DescriptorProto::~DescriptorProto() {
}
void DescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -2027,16 +2125,18 @@ const DescriptorProto& DescriptorProto::default_instance() {
DescriptorProto* DescriptorProto::default_instance_ = NULL;
-DescriptorProto* DescriptorProto::New() const {
- return new DescriptorProto;
+DescriptorProto* DescriptorProto::New(::google::protobuf::Arena* arena) const {
+ DescriptorProto* n = new DescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void DescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 129) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_options()) {
if (options_ != NULL) options_->::google::protobuf::MessageOptions::Clear();
@@ -2049,7 +2149,9 @@ void DescriptorProto::Clear() {
extension_range_.Clear();
oneof_decl_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool DescriptorProto::MergePartialFromCodedStream(
@@ -2070,7 +2172,7 @@ bool DescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.DescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -2205,37 +2307,37 @@ void DescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.DescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
// repeated .google.protobuf.FieldDescriptorProto field = 2;
- for (int i = 0; i < this->field_size(); i++) {
+ for (unsigned int i = 0, n = this->field_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
2, this->field(i), output);
}
// repeated .google.protobuf.DescriptorProto nested_type = 3;
- for (int i = 0; i < this->nested_type_size(); i++) {
+ for (unsigned int i = 0, n = this->nested_type_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
3, this->nested_type(i), output);
}
// repeated .google.protobuf.EnumDescriptorProto enum_type = 4;
- for (int i = 0; i < this->enum_type_size(); i++) {
+ for (unsigned int i = 0, n = this->enum_type_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
4, this->enum_type(i), output);
}
// repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5;
- for (int i = 0; i < this->extension_range_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_range_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
5, this->extension_range(i), output);
}
// repeated .google.protobuf.FieldDescriptorProto extension = 6;
- for (int i = 0; i < this->extension_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
6, this->extension(i), output);
}
@@ -2243,16 +2345,16 @@ void DescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.MessageOptions options = 7;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 7, this->options(), output);
+ 7, *this->options_, output);
}
// repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8;
- for (int i = 0; i < this->oneof_decl_size(); i++) {
+ for (unsigned int i = 0, n = this->oneof_decl_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
8, this->oneof_decl(i), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -2267,42 +2369,42 @@ void DescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.DescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
}
// repeated .google.protobuf.FieldDescriptorProto field = 2;
- for (int i = 0; i < this->field_size(); i++) {
+ for (unsigned int i = 0, n = this->field_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
2, this->field(i), target);
}
// repeated .google.protobuf.DescriptorProto nested_type = 3;
- for (int i = 0; i < this->nested_type_size(); i++) {
+ for (unsigned int i = 0, n = this->nested_type_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
3, this->nested_type(i), target);
}
// repeated .google.protobuf.EnumDescriptorProto enum_type = 4;
- for (int i = 0; i < this->enum_type_size(); i++) {
+ for (unsigned int i = 0, n = this->enum_type_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
4, this->enum_type(i), target);
}
// repeated .google.protobuf.DescriptorProto.ExtensionRange extension_range = 5;
- for (int i = 0; i < this->extension_range_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_range_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
5, this->extension_range(i), target);
}
// repeated .google.protobuf.FieldDescriptorProto extension = 6;
- for (int i = 0; i < this->extension_size(); i++) {
+ for (unsigned int i = 0, n = this->extension_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
6, this->extension(i), target);
@@ -2312,17 +2414,17 @@ void DescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 7, this->options(), target);
+ 7, *this->options_, target);
}
// repeated .google.protobuf.OneofDescriptorProto oneof_decl = 8;
- for (int i = 0; i < this->oneof_decl_size(); i++) {
+ for (unsigned int i = 0, n = this->oneof_decl_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
8, this->oneof_decl(i), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -2333,7 +2435,7 @@ void DescriptorProto::SerializeWithCachedSizes(
int DescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 129) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -2345,7 +2447,7 @@ int DescriptorProto::ByteSize() const {
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
}
}
@@ -2397,7 +2499,7 @@ int DescriptorProto::ByteSize() const {
this->oneof_decl(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -2409,7 +2511,7 @@ int DescriptorProto::ByteSize() const {
}
void DescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const DescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const DescriptorProto*>(
&from);
@@ -2421,7 +2523,7 @@ void DescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void DescriptorProto::MergeFrom(const DescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
field_.MergeFrom(from.field_);
extension_.MergeFrom(from.extension_);
nested_type_.MergeFrom(from.nested_type_);
@@ -2430,13 +2532,16 @@ void DescriptorProto::MergeFrom(const DescriptorProto& from) {
oneof_decl_.MergeFrom(from.oneof_decl_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_options()) {
mutable_options()->::google::protobuf::MessageOptions::MergeFrom(from.options());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void DescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -2458,25 +2563,27 @@ bool DescriptorProto::IsInitialized() const {
if (!::google::protobuf::internal::AllAreInitialized(this->nested_type())) return false;
if (!::google::protobuf::internal::AllAreInitialized(this->enum_type())) return false;
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void DescriptorProto::Swap(DescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- field_.Swap(&other->field_);
- extension_.Swap(&other->extension_);
- nested_type_.Swap(&other->nested_type_);
- enum_type_.Swap(&other->enum_type_);
- extension_range_.Swap(&other->extension_range_);
- oneof_decl_.Swap(&other->oneof_decl_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void DescriptorProto::InternalSwap(DescriptorProto* other) {
+ name_.Swap(&other->name_);
+ field_.UnsafeArenaSwap(&other->field_);
+ extension_.UnsafeArenaSwap(&other->extension_);
+ nested_type_.UnsafeArenaSwap(&other->nested_type_);
+ enum_type_.UnsafeArenaSwap(&other->enum_type_);
+ extension_range_.UnsafeArenaSwap(&other->extension_range_);
+ oneof_decl_.UnsafeArenaSwap(&other->oneof_decl_);
+ std::swap(options_, other->options_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata DescriptorProto::GetMetadata() const {
@@ -2579,7 +2686,7 @@ const int FieldDescriptorProto::kOptionsFieldNumber;
#endif // !_MSC_VER
FieldDescriptorProto::FieldDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.FieldDescriptorProto)
}
@@ -2589,7 +2696,8 @@ void FieldDescriptorProto::InitAsDefaultInstance() {
}
FieldDescriptorProto::FieldDescriptorProto(const FieldDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.FieldDescriptorProto)
@@ -2598,13 +2706,13 @@ FieldDescriptorProto::FieldDescriptorProto(const FieldDescriptorProto& from)
void FieldDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
number_ = 0;
label_ = 1;
type_ = 1;
- type_name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- extendee_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- default_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ type_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ extendee_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ default_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
oneof_index_ = 0;
options_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
@@ -2616,18 +2724,10 @@ FieldDescriptorProto::~FieldDescriptorProto() {
}
void FieldDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (type_name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete type_name_;
- }
- if (extendee_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete extendee_;
- }
- if (default_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete default_value_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ type_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ extendee_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ default_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -2650,34 +2750,30 @@ const FieldDescriptorProto& FieldDescriptorProto::default_instance() {
FieldDescriptorProto* FieldDescriptorProto::default_instance_ = NULL;
-FieldDescriptorProto* FieldDescriptorProto::New() const {
- return new FieldDescriptorProto;
+FieldDescriptorProto* FieldDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ FieldDescriptorProto* n = new FieldDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void FieldDescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 255) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
number_ = 0;
label_ = 1;
type_ = 1;
if (has_type_name()) {
- if (type_name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_->clear();
- }
+ type_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_extendee()) {
- if (extendee_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_->clear();
- }
+ extendee_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_default_value()) {
- if (default_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_->clear();
- }
+ default_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
oneof_index_ = 0;
}
@@ -2685,7 +2781,9 @@ void FieldDescriptorProto::Clear() {
if (options_ != NULL) options_->::google::protobuf::FieldOptions::Clear();
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool FieldDescriptorProto::MergePartialFromCodedStream(
@@ -2706,7 +2804,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.FieldDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -2723,7 +2821,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->extendee().data(), this->extendee().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "extendee");
+ "google.protobuf.FieldDescriptorProto.extendee");
} else {
goto handle_unusual;
}
@@ -2795,7 +2893,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->type_name().data(), this->type_name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "type_name");
+ "google.protobuf.FieldDescriptorProto.type_name");
} else {
goto handle_unusual;
}
@@ -2812,7 +2910,7 @@ bool FieldDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->default_value().data(), this->default_value().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "default_value");
+ "google.protobuf.FieldDescriptorProto.default_value");
} else {
goto handle_unusual;
}
@@ -2878,7 +2976,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.FieldDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
@@ -2888,7 +2986,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->extendee().data(), this->extendee().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "extendee");
+ "google.protobuf.FieldDescriptorProto.extendee");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->extendee(), output);
}
@@ -2915,7 +3013,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->type_name().data(), this->type_name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "type_name");
+ "google.protobuf.FieldDescriptorProto.type_name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
6, this->type_name(), output);
}
@@ -2925,7 +3023,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->default_value().data(), this->default_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "default_value");
+ "google.protobuf.FieldDescriptorProto.default_value");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
7, this->default_value(), output);
}
@@ -2933,7 +3031,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.FieldOptions options = 8;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 8, this->options(), output);
+ 8, *this->options_, output);
}
// optional int32 oneof_index = 9;
@@ -2941,7 +3039,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteInt32(9, this->oneof_index(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -2956,7 +3054,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.FieldDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
@@ -2967,7 +3065,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->extendee().data(), this->extendee().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "extendee");
+ "google.protobuf.FieldDescriptorProto.extendee");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->extendee(), target);
@@ -2995,7 +3093,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->type_name().data(), this->type_name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "type_name");
+ "google.protobuf.FieldDescriptorProto.type_name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
6, this->type_name(), target);
@@ -3006,7 +3104,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->default_value().data(), this->default_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "default_value");
+ "google.protobuf.FieldDescriptorProto.default_value");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
7, this->default_value(), target);
@@ -3016,7 +3114,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 8, this->options(), target);
+ 8, *this->options_, target);
}
// optional int32 oneof_index = 9;
@@ -3024,7 +3122,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(9, this->oneof_index(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -3035,7 +3133,7 @@ void FieldDescriptorProto::SerializeWithCachedSizes(
int FieldDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 255) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -3091,16 +3189,14 @@ int FieldDescriptorProto::ByteSize() const {
}
}
- if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) {
- // optional .google.protobuf.FieldOptions options = 8;
- if (has_options()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
- }
-
+ // optional .google.protobuf.FieldOptions options = 8;
+ if (has_options()) {
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
+ *this->options_);
}
- if (!unknown_fields().empty()) {
+
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -3112,7 +3208,7 @@ int FieldDescriptorProto::ByteSize() const {
}
void FieldDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const FieldDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const FieldDescriptorProto*>(
&from);
@@ -3124,10 +3220,11 @@ void FieldDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_number()) {
set_number(from.number());
@@ -3139,13 +3236,16 @@ void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) {
set_type(from.type());
}
if (from.has_type_name()) {
- set_type_name(from.type_name());
+ set_has_type_name();
+ type_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.type_name_);
}
if (from.has_extendee()) {
- set_extendee(from.extendee());
+ set_has_extendee();
+ extendee_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.extendee_);
}
if (from.has_default_value()) {
- set_default_value(from.default_value());
+ set_has_default_value();
+ default_value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.default_value_);
}
if (from.has_oneof_index()) {
set_oneof_index(from.oneof_index());
@@ -3156,7 +3256,9 @@ void FieldDescriptorProto::MergeFrom(const FieldDescriptorProto& from) {
mutable_options()->::google::protobuf::FieldOptions::MergeFrom(from.options());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void FieldDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -3174,26 +3276,28 @@ void FieldDescriptorProto::CopyFrom(const FieldDescriptorProto& from) {
bool FieldDescriptorProto::IsInitialized() const {
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void FieldDescriptorProto::Swap(FieldDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(number_, other->number_);
- std::swap(label_, other->label_);
- std::swap(type_, other->type_);
- std::swap(type_name_, other->type_name_);
- std::swap(extendee_, other->extendee_);
- std::swap(default_value_, other->default_value_);
- std::swap(oneof_index_, other->oneof_index_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void FieldDescriptorProto::InternalSwap(FieldDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ std::swap(number_, other->number_);
+ std::swap(label_, other->label_);
+ std::swap(type_, other->type_);
+ type_name_.Swap(&other->type_name_);
+ extendee_.Swap(&other->extendee_);
+ default_value_.Swap(&other->default_value_);
+ std::swap(oneof_index_, other->oneof_index_);
+ std::swap(options_, other->options_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata FieldDescriptorProto::GetMetadata() const {
@@ -3212,7 +3316,7 @@ const int OneofDescriptorProto::kNameFieldNumber;
#endif // !_MSC_VER
OneofDescriptorProto::OneofDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.OneofDescriptorProto)
}
@@ -3221,7 +3325,8 @@ void OneofDescriptorProto::InitAsDefaultInstance() {
}
OneofDescriptorProto::OneofDescriptorProto(const OneofDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.OneofDescriptorProto)
@@ -3230,7 +3335,7 @@ OneofDescriptorProto::OneofDescriptorProto(const OneofDescriptorProto& from)
void OneofDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -3240,9 +3345,7 @@ OneofDescriptorProto::~OneofDescriptorProto() {
}
void OneofDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -3264,18 +3367,22 @@ const OneofDescriptorProto& OneofDescriptorProto::default_instance() {
OneofDescriptorProto* OneofDescriptorProto::default_instance_ = NULL;
-OneofDescriptorProto* OneofDescriptorProto::New() const {
- return new OneofDescriptorProto;
+OneofDescriptorProto* OneofDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ OneofDescriptorProto* n = new OneofDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void OneofDescriptorProto::Clear() {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool OneofDescriptorProto::MergePartialFromCodedStream(
@@ -3296,7 +3403,7 @@ bool OneofDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.OneofDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -3334,12 +3441,12 @@ void OneofDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.OneofDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -3354,13 +3461,13 @@ void OneofDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.OneofDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -3371,16 +3478,14 @@ void OneofDescriptorProto::SerializeWithCachedSizes(
int OneofDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
- // optional string name = 1;
- if (has_name()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::StringSize(
- this->name());
- }
-
+ // optional string name = 1;
+ if (has_name()) {
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->name());
}
- if (!unknown_fields().empty()) {
+
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -3392,7 +3497,7 @@ int OneofDescriptorProto::ByteSize() const {
}
void OneofDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const OneofDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const OneofDescriptorProto*>(
&from);
@@ -3404,13 +3509,16 @@ void OneofDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void OneofDescriptorProto::MergeFrom(const OneofDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void OneofDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -3431,12 +3539,14 @@ bool OneofDescriptorProto::IsInitialized() const {
}
void OneofDescriptorProto::Swap(OneofDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void OneofDescriptorProto::InternalSwap(OneofDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata OneofDescriptorProto::GetMetadata() const {
@@ -3457,7 +3567,7 @@ const int EnumDescriptorProto::kOptionsFieldNumber;
#endif // !_MSC_VER
EnumDescriptorProto::EnumDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.EnumDescriptorProto)
}
@@ -3467,7 +3577,8 @@ void EnumDescriptorProto::InitAsDefaultInstance() {
}
EnumDescriptorProto::EnumDescriptorProto(const EnumDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.EnumDescriptorProto)
@@ -3476,7 +3587,7 @@ EnumDescriptorProto::EnumDescriptorProto(const EnumDescriptorProto& from)
void EnumDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
options_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -3487,9 +3598,7 @@ EnumDescriptorProto::~EnumDescriptorProto() {
}
void EnumDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -3512,16 +3621,18 @@ const EnumDescriptorProto& EnumDescriptorProto::default_instance() {
EnumDescriptorProto* EnumDescriptorProto::default_instance_ = NULL;
-EnumDescriptorProto* EnumDescriptorProto::New() const {
- return new EnumDescriptorProto;
+EnumDescriptorProto* EnumDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ EnumDescriptorProto* n = new EnumDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void EnumDescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 5) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_options()) {
if (options_ != NULL) options_->::google::protobuf::EnumOptions::Clear();
@@ -3529,7 +3640,9 @@ void EnumDescriptorProto::Clear() {
}
value_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool EnumDescriptorProto::MergePartialFromCodedStream(
@@ -3550,7 +3663,7 @@ bool EnumDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.EnumDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -3615,13 +3728,13 @@ void EnumDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.EnumDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
// repeated .google.protobuf.EnumValueDescriptorProto value = 2;
- for (int i = 0; i < this->value_size(); i++) {
+ for (unsigned int i = 0, n = this->value_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
2, this->value(i), output);
}
@@ -3629,10 +3742,10 @@ void EnumDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.EnumOptions options = 3;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 3, this->options(), output);
+ 3, *this->options_, output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -3647,14 +3760,14 @@ void EnumDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.EnumDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
}
// repeated .google.protobuf.EnumValueDescriptorProto value = 2;
- for (int i = 0; i < this->value_size(); i++) {
+ for (unsigned int i = 0, n = this->value_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
2, this->value(i), target);
@@ -3664,10 +3777,10 @@ void EnumDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 3, this->options(), target);
+ 3, *this->options_, target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -3678,7 +3791,7 @@ void EnumDescriptorProto::SerializeWithCachedSizes(
int EnumDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 5) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -3690,7 +3803,7 @@ int EnumDescriptorProto::ByteSize() const {
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
}
}
@@ -3702,7 +3815,7 @@ int EnumDescriptorProto::ByteSize() const {
this->value(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -3714,7 +3827,7 @@ int EnumDescriptorProto::ByteSize() const {
}
void EnumDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const EnumDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const EnumDescriptorProto*>(
&from);
@@ -3726,17 +3839,20 @@ void EnumDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void EnumDescriptorProto::MergeFrom(const EnumDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
value_.MergeFrom(from.value_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_options()) {
mutable_options()->::google::protobuf::EnumOptions::MergeFrom(from.options());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void EnumDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -3755,20 +3871,22 @@ bool EnumDescriptorProto::IsInitialized() const {
if (!::google::protobuf::internal::AllAreInitialized(this->value())) return false;
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void EnumDescriptorProto::Swap(EnumDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- value_.Swap(&other->value_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void EnumDescriptorProto::InternalSwap(EnumDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ value_.UnsafeArenaSwap(&other->value_);
+ std::swap(options_, other->options_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata EnumDescriptorProto::GetMetadata() const {
@@ -3789,7 +3907,7 @@ const int EnumValueDescriptorProto::kOptionsFieldNumber;
#endif // !_MSC_VER
EnumValueDescriptorProto::EnumValueDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.EnumValueDescriptorProto)
}
@@ -3799,7 +3917,8 @@ void EnumValueDescriptorProto::InitAsDefaultInstance() {
}
EnumValueDescriptorProto::EnumValueDescriptorProto(const EnumValueDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.EnumValueDescriptorProto)
@@ -3808,7 +3927,7 @@ EnumValueDescriptorProto::EnumValueDescriptorProto(const EnumValueDescriptorProt
void EnumValueDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
number_ = 0;
options_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
@@ -3820,9 +3939,7 @@ EnumValueDescriptorProto::~EnumValueDescriptorProto() {
}
void EnumValueDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -3845,16 +3962,18 @@ const EnumValueDescriptorProto& EnumValueDescriptorProto::default_instance() {
EnumValueDescriptorProto* EnumValueDescriptorProto::default_instance_ = NULL;
-EnumValueDescriptorProto* EnumValueDescriptorProto::New() const {
- return new EnumValueDescriptorProto;
+EnumValueDescriptorProto* EnumValueDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ EnumValueDescriptorProto* n = new EnumValueDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void EnumValueDescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 7) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
number_ = 0;
if (has_options()) {
@@ -3862,7 +3981,9 @@ void EnumValueDescriptorProto::Clear() {
}
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool EnumValueDescriptorProto::MergePartialFromCodedStream(
@@ -3883,7 +4004,7 @@ bool EnumValueDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.EnumValueDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -3949,7 +4070,7 @@ void EnumValueDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.EnumValueDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
@@ -3962,10 +4083,10 @@ void EnumValueDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.EnumValueOptions options = 3;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 3, this->options(), output);
+ 3, *this->options_, output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -3980,7 +4101,7 @@ void EnumValueDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.EnumValueDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
@@ -3995,10 +4116,10 @@ void EnumValueDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 3, this->options(), target);
+ 3, *this->options_, target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -4009,7 +4130,7 @@ void EnumValueDescriptorProto::SerializeWithCachedSizes(
int EnumValueDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 7) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -4028,11 +4149,11 @@ int EnumValueDescriptorProto::ByteSize() const {
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
}
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -4044,7 +4165,7 @@ int EnumValueDescriptorProto::ByteSize() const {
}
void EnumValueDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const EnumValueDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const EnumValueDescriptorProto*>(
&from);
@@ -4056,10 +4177,11 @@ void EnumValueDescriptorProto::MergeFrom(const ::google::protobuf::Message& from
}
void EnumValueDescriptorProto::MergeFrom(const EnumValueDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_number()) {
set_number(from.number());
@@ -4068,7 +4190,9 @@ void EnumValueDescriptorProto::MergeFrom(const EnumValueDescriptorProto& from) {
mutable_options()->::google::protobuf::EnumValueOptions::MergeFrom(from.options());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void EnumValueDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -4086,20 +4210,22 @@ void EnumValueDescriptorProto::CopyFrom(const EnumValueDescriptorProto& from) {
bool EnumValueDescriptorProto::IsInitialized() const {
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void EnumValueDescriptorProto::Swap(EnumValueDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(number_, other->number_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void EnumValueDescriptorProto::InternalSwap(EnumValueDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ std::swap(number_, other->number_);
+ std::swap(options_, other->options_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata EnumValueDescriptorProto::GetMetadata() const {
@@ -4120,7 +4246,7 @@ const int ServiceDescriptorProto::kOptionsFieldNumber;
#endif // !_MSC_VER
ServiceDescriptorProto::ServiceDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.ServiceDescriptorProto)
}
@@ -4130,7 +4256,8 @@ void ServiceDescriptorProto::InitAsDefaultInstance() {
}
ServiceDescriptorProto::ServiceDescriptorProto(const ServiceDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.ServiceDescriptorProto)
@@ -4139,7 +4266,7 @@ ServiceDescriptorProto::ServiceDescriptorProto(const ServiceDescriptorProto& fro
void ServiceDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
options_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -4150,9 +4277,7 @@ ServiceDescriptorProto::~ServiceDescriptorProto() {
}
void ServiceDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -4175,16 +4300,18 @@ const ServiceDescriptorProto& ServiceDescriptorProto::default_instance() {
ServiceDescriptorProto* ServiceDescriptorProto::default_instance_ = NULL;
-ServiceDescriptorProto* ServiceDescriptorProto::New() const {
- return new ServiceDescriptorProto;
+ServiceDescriptorProto* ServiceDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ ServiceDescriptorProto* n = new ServiceDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void ServiceDescriptorProto::Clear() {
if (_has_bits_[0 / 32] & 5) {
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_options()) {
if (options_ != NULL) options_->::google::protobuf::ServiceOptions::Clear();
@@ -4192,7 +4319,9 @@ void ServiceDescriptorProto::Clear() {
}
method_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool ServiceDescriptorProto::MergePartialFromCodedStream(
@@ -4213,7 +4342,7 @@ bool ServiceDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.ServiceDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -4278,13 +4407,13 @@ void ServiceDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.ServiceDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
// repeated .google.protobuf.MethodDescriptorProto method = 2;
- for (int i = 0; i < this->method_size(); i++) {
+ for (unsigned int i = 0, n = this->method_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
2, this->method(i), output);
}
@@ -4292,10 +4421,10 @@ void ServiceDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.ServiceOptions options = 3;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 3, this->options(), output);
+ 3, *this->options_, output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -4310,14 +4439,14 @@ void ServiceDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.ServiceDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
}
// repeated .google.protobuf.MethodDescriptorProto method = 2;
- for (int i = 0; i < this->method_size(); i++) {
+ for (unsigned int i = 0, n = this->method_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
2, this->method(i), target);
@@ -4327,10 +4456,10 @@ void ServiceDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 3, this->options(), target);
+ 3, *this->options_, target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -4341,7 +4470,7 @@ void ServiceDescriptorProto::SerializeWithCachedSizes(
int ServiceDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 5) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -4353,7 +4482,7 @@ int ServiceDescriptorProto::ByteSize() const {
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
}
}
@@ -4365,7 +4494,7 @@ int ServiceDescriptorProto::ByteSize() const {
this->method(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -4377,7 +4506,7 @@ int ServiceDescriptorProto::ByteSize() const {
}
void ServiceDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const ServiceDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const ServiceDescriptorProto*>(
&from);
@@ -4389,17 +4518,20 @@ void ServiceDescriptorProto::MergeFrom(const ::google::protobuf::Message& from)
}
void ServiceDescriptorProto::MergeFrom(const ServiceDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
method_.MergeFrom(from.method_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_options()) {
mutable_options()->::google::protobuf::ServiceOptions::MergeFrom(from.options());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void ServiceDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -4418,20 +4550,22 @@ bool ServiceDescriptorProto::IsInitialized() const {
if (!::google::protobuf::internal::AllAreInitialized(this->method())) return false;
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void ServiceDescriptorProto::Swap(ServiceDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- method_.Swap(&other->method_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void ServiceDescriptorProto::InternalSwap(ServiceDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ method_.UnsafeArenaSwap(&other->method_);
+ std::swap(options_, other->options_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata ServiceDescriptorProto::GetMetadata() const {
@@ -4450,10 +4584,12 @@ 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 // !_MSC_VER
MethodDescriptorProto::MethodDescriptorProto()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.MethodDescriptorProto)
}
@@ -4463,7 +4599,8 @@ void MethodDescriptorProto::InitAsDefaultInstance() {
}
MethodDescriptorProto::MethodDescriptorProto(const MethodDescriptorProto& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.MethodDescriptorProto)
@@ -4472,10 +4609,12 @@ MethodDescriptorProto::MethodDescriptorProto(const MethodDescriptorProto& from)
void MethodDescriptorProto::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- input_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- output_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ input_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ output_type_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
options_ = NULL;
+ client_streaming_ = false;
+ server_streaming_ = false;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -4485,15 +4624,9 @@ MethodDescriptorProto::~MethodDescriptorProto() {
}
void MethodDescriptorProto::SharedDtor() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (input_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete input_type_;
- }
- if (output_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete output_type_;
- }
+ name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ input_type_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ output_type_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
delete options_;
}
@@ -4516,33 +4649,48 @@ const MethodDescriptorProto& MethodDescriptorProto::default_instance() {
MethodDescriptorProto* MethodDescriptorProto::default_instance_ = NULL;
-MethodDescriptorProto* MethodDescriptorProto::New() const {
- return new MethodDescriptorProto;
+MethodDescriptorProto* MethodDescriptorProto::New(::google::protobuf::Arena* arena) const {
+ MethodDescriptorProto* n = new MethodDescriptorProto;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void MethodDescriptorProto::Clear() {
- if (_has_bits_[0 / 32] & 15) {
+#define OFFSET_OF_FIELD_(f) (reinterpret_cast<char*>( \
+ &reinterpret_cast<MethodDescriptorProto*>(16)->f) - \
+ reinterpret_cast<char*>(16))
+
+#define ZR_(first, last) do { \
+ size_t f = OFFSET_OF_FIELD_(first); \
+ size_t n = OFFSET_OF_FIELD_(last) - f + sizeof(last); \
+ ::memset(&first, 0, n); \
+ } while (0)
+
+ if (_has_bits_[0 / 32] & 63) {
+ ZR_(client_streaming_, server_streaming_);
if (has_name()) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_input_type()) {
- if (input_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_->clear();
- }
+ input_type_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_output_type()) {
- if (output_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_->clear();
- }
+ output_type_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_options()) {
if (options_ != NULL) options_->::google::protobuf::MethodOptions::Clear();
}
}
+
+#undef OFFSET_OF_FIELD_
+#undef ZR_
+
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool MethodDescriptorProto::MergePartialFromCodedStream(
@@ -4563,7 +4711,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name");
+ "google.protobuf.MethodDescriptorProto.name");
} else {
goto handle_unusual;
}
@@ -4580,7 +4728,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->input_type().data(), this->input_type().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "input_type");
+ "google.protobuf.MethodDescriptorProto.input_type");
} else {
goto handle_unusual;
}
@@ -4597,7 +4745,7 @@ bool MethodDescriptorProto::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->output_type().data(), this->output_type().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "output_type");
+ "google.protobuf.MethodDescriptorProto.output_type");
} else {
goto handle_unusual;
}
@@ -4614,6 +4762,36 @@ bool MethodDescriptorProto::MergePartialFromCodedStream(
} else {
goto handle_unusual;
}
+ if (input->ExpectTag(40)) goto parse_client_streaming;
+ break;
+ }
+
+ // optional bool client_streaming = 5 [default = false];
+ case 5: {
+ if (tag == 40) {
+ parse_client_streaming:
+ DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+ bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>(
+ input, &client_streaming_)));
+ set_has_client_streaming();
+ } else {
+ goto handle_unusual;
+ }
+ if (input->ExpectTag(48)) goto parse_server_streaming;
+ break;
+ }
+
+ // optional bool server_streaming = 6 [default = false];
+ case 6: {
+ if (tag == 48) {
+ parse_server_streaming:
+ DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+ bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>(
+ input, &server_streaming_)));
+ set_has_server_streaming();
+ } else {
+ goto handle_unusual;
+ }
if (input->ExpectAtEnd()) goto success;
break;
}
@@ -4648,7 +4826,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.MethodDescriptorProto.name");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name(), output);
}
@@ -4658,7 +4836,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->input_type().data(), this->input_type().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "input_type");
+ "google.protobuf.MethodDescriptorProto.input_type");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
2, this->input_type(), output);
}
@@ -4668,7 +4846,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->output_type().data(), this->output_type().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "output_type");
+ "google.protobuf.MethodDescriptorProto.output_type");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
3, this->output_type(), output);
}
@@ -4676,10 +4854,20 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
// optional .google.protobuf.MethodOptions options = 4;
if (has_options()) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
- 4, this->options(), output);
+ 4, *this->options_, output);
+ }
+
+ // optional bool client_streaming = 5 [default = false];
+ if (has_client_streaming()) {
+ ::google::protobuf::internal::WireFormatLite::WriteBool(5, this->client_streaming(), output);
+ }
+
+ // optional bool server_streaming = 6 [default = false];
+ if (has_server_streaming()) {
+ ::google::protobuf::internal::WireFormatLite::WriteBool(6, this->server_streaming(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -4694,7 +4882,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name().data(), this->name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name");
+ "google.protobuf.MethodDescriptorProto.name");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name(), target);
@@ -4705,7 +4893,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->input_type().data(), this->input_type().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "input_type");
+ "google.protobuf.MethodDescriptorProto.input_type");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
2, this->input_type(), target);
@@ -4716,7 +4904,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->output_type().data(), this->output_type().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "output_type");
+ "google.protobuf.MethodDescriptorProto.output_type");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
3, this->output_type(), target);
@@ -4726,10 +4914,20 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
if (has_options()) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
- 4, this->options(), target);
+ 4, *this->options_, target);
+ }
+
+ // optional bool client_streaming = 5 [default = false];
+ if (has_client_streaming()) {
+ target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(5, this->client_streaming(), target);
+ }
+
+ // optional bool server_streaming = 6 [default = false];
+ if (has_server_streaming()) {
+ target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(6, this->server_streaming(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -4740,7 +4938,7 @@ void MethodDescriptorProto::SerializeWithCachedSizes(
int MethodDescriptorProto::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 63) {
// optional string name = 1;
if (has_name()) {
total_size += 1 +
@@ -4766,11 +4964,21 @@ int MethodDescriptorProto::ByteSize() const {
if (has_options()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
- this->options());
+ *this->options_);
+ }
+
+ // optional bool client_streaming = 5 [default = false];
+ if (has_client_streaming()) {
+ total_size += 1 + 1;
+ }
+
+ // optional bool server_streaming = 6 [default = false];
+ if (has_server_streaming()) {
+ total_size += 1 + 1;
}
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -4782,7 +4990,7 @@ int MethodDescriptorProto::ByteSize() const {
}
void MethodDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const MethodDescriptorProto* source =
::google::protobuf::internal::dynamic_cast_if_available<const MethodDescriptorProto*>(
&from);
@@ -4794,22 +5002,33 @@ void MethodDescriptorProto::MergeFrom(const ::google::protobuf::Message& from) {
}
void MethodDescriptorProto::MergeFrom(const MethodDescriptorProto& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name()) {
- set_name(from.name());
+ set_has_name();
+ name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
}
if (from.has_input_type()) {
- set_input_type(from.input_type());
+ set_has_input_type();
+ input_type_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.input_type_);
}
if (from.has_output_type()) {
- set_output_type(from.output_type());
+ set_has_output_type();
+ output_type_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.output_type_);
}
if (from.has_options()) {
mutable_options()->::google::protobuf::MethodOptions::MergeFrom(from.options());
}
+ if (from.has_client_streaming()) {
+ set_client_streaming(from.client_streaming());
+ }
+ if (from.has_server_streaming()) {
+ set_server_streaming(from.server_streaming());
+ }
+ }
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
}
void MethodDescriptorProto::CopyFrom(const ::google::protobuf::Message& from) {
@@ -4827,21 +5046,25 @@ void MethodDescriptorProto::CopyFrom(const MethodDescriptorProto& from) {
bool MethodDescriptorProto::IsInitialized() const {
if (has_options()) {
- if (!this->options().IsInitialized()) return false;
+ if (!this->options_->IsInitialized()) return false;
}
return true;
}
void MethodDescriptorProto::Swap(MethodDescriptorProto* other) {
- if (other != this) {
- std::swap(name_, other->name_);
- std::swap(input_type_, other->input_type_);
- std::swap(output_type_, other->output_type_);
- std::swap(options_, other->options_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void MethodDescriptorProto::InternalSwap(MethodDescriptorProto* other) {
+ name_.Swap(&other->name_);
+ input_type_.Swap(&other->input_type_);
+ output_type_.Swap(&other->output_type_);
+ std::swap(options_, other->options_);
+ std::swap(client_streaming_, other->client_streaming_);
+ std::swap(server_streaming_, other->server_streaming_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata MethodDescriptorProto::GetMetadata() const {
@@ -4890,11 +5113,12 @@ const int FileOptions::kCcGenericServicesFieldNumber;
const int FileOptions::kJavaGenericServicesFieldNumber;
const int FileOptions::kPyGenericServicesFieldNumber;
const int FileOptions::kDeprecatedFieldNumber;
+const int FileOptions::kCcEnableArenasFieldNumber;
const int FileOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
FileOptions::FileOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.FileOptions)
}
@@ -4903,7 +5127,8 @@ void FileOptions::InitAsDefaultInstance() {
}
FileOptions::FileOptions(const FileOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.FileOptions)
@@ -4912,17 +5137,18 @@ FileOptions::FileOptions(const FileOptions& from)
void FileOptions::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- java_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- java_outer_classname_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ java_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ java_outer_classname_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
java_multiple_files_ = false;
java_generate_equals_and_hash_ = false;
java_string_check_utf8_ = false;
optimize_for_ = 1;
- go_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ go_package_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
cc_generic_services_ = false;
java_generic_services_ = false;
py_generic_services_ = false;
deprecated_ = false;
+ cc_enable_arenas_ = false;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -4932,15 +5158,9 @@ FileOptions::~FileOptions() {
}
void FileOptions::SharedDtor() {
- if (java_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete java_package_;
- }
- if (java_outer_classname_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete java_outer_classname_;
- }
- if (go_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete go_package_;
- }
+ java_package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ java_outer_classname_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ go_package_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -4962,8 +5182,12 @@ const FileOptions& FileOptions::default_instance() {
FileOptions* FileOptions::default_instance_ = NULL;
-FileOptions* FileOptions::New() const {
- return new FileOptions;
+FileOptions* FileOptions::New(::google::protobuf::Arena* arena) const {
+ FileOptions* n = new FileOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void FileOptions::Clear() {
@@ -4981,30 +5205,26 @@ void FileOptions::Clear() {
if (_has_bits_[0 / 32] & 255) {
ZR_(java_multiple_files_, cc_generic_services_);
if (has_java_package()) {
- if (java_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_->clear();
- }
+ java_package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_java_outer_classname()) {
- if (java_outer_classname_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_->clear();
- }
+ java_outer_classname_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
optimize_for_ = 1;
if (has_go_package()) {
- if (go_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_->clear();
- }
+ go_package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
}
- ZR_(java_generic_services_, deprecated_);
+ ZR_(java_generic_services_, cc_enable_arenas_);
#undef OFFSET_OF_FIELD_
#undef ZR_
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool FileOptions::MergePartialFromCodedStream(
@@ -5025,7 +5245,7 @@ bool FileOptions::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_package().data(), this->java_package().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "java_package");
+ "google.protobuf.FileOptions.java_package");
} else {
goto handle_unusual;
}
@@ -5042,7 +5262,7 @@ bool FileOptions::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_outer_classname().data(), this->java_outer_classname().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "java_outer_classname");
+ "google.protobuf.FileOptions.java_outer_classname");
} else {
goto handle_unusual;
}
@@ -5094,7 +5314,7 @@ bool FileOptions::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->go_package().data(), this->go_package().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "go_package");
+ "google.protobuf.FileOptions.go_package");
} else {
goto handle_unusual;
}
@@ -5188,6 +5408,21 @@ bool FileOptions::MergePartialFromCodedStream(
} else {
goto handle_unusual;
}
+ if (input->ExpectTag(248)) goto parse_cc_enable_arenas;
+ break;
+ }
+
+ // optional bool cc_enable_arenas = 31 [default = false];
+ case 31: {
+ if (tag == 248) {
+ parse_cc_enable_arenas:
+ DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+ bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>(
+ input, &cc_enable_arenas_)));
+ set_has_cc_enable_arenas();
+ } else {
+ goto handle_unusual;
+ }
if (input->ExpectTag(7994)) goto parse_uninterpreted_option;
break;
}
@@ -5241,7 +5476,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_package().data(), this->java_package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "java_package");
+ "google.protobuf.FileOptions.java_package");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->java_package(), output);
}
@@ -5251,7 +5486,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_outer_classname().data(), this->java_outer_classname().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "java_outer_classname");
+ "google.protobuf.FileOptions.java_outer_classname");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
8, this->java_outer_classname(), output);
}
@@ -5272,7 +5507,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->go_package().data(), this->go_package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "go_package");
+ "google.protobuf.FileOptions.go_package");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
11, this->go_package(), output);
}
@@ -5307,8 +5542,13 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteBool(27, this->java_string_check_utf8(), output);
}
+ // optional bool cc_enable_arenas = 31 [default = false];
+ if (has_cc_enable_arenas()) {
+ ::google::protobuf::internal::WireFormatLite::WriteBool(31, this->cc_enable_arenas(), output);
+ }
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -5317,7 +5557,7 @@ void FileOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -5332,7 +5572,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_package().data(), this->java_package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "java_package");
+ "google.protobuf.FileOptions.java_package");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->java_package(), target);
@@ -5343,7 +5583,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->java_outer_classname().data(), this->java_outer_classname().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "java_outer_classname");
+ "google.protobuf.FileOptions.java_outer_classname");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
8, this->java_outer_classname(), target);
@@ -5365,7 +5605,7 @@ void FileOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->go_package().data(), this->go_package().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "go_package");
+ "google.protobuf.FileOptions.go_package");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
11, this->go_package(), target);
@@ -5401,8 +5641,13 @@ void FileOptions::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(27, this->java_string_check_utf8(), target);
}
+ // optional bool cc_enable_arenas = 31 [default = false];
+ if (has_cc_enable_arenas()) {
+ target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(31, this->cc_enable_arenas(), target);
+ }
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -5412,7 +5657,7 @@ void FileOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -5423,7 +5668,7 @@ void FileOptions::SerializeWithCachedSizes(
int FileOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 255) {
// optional string java_package = 1;
if (has_java_package()) {
total_size += 1 +
@@ -5472,7 +5717,7 @@ int FileOptions::ByteSize() const {
}
}
- if (_has_bits_[8 / 32] & (0xffu << (8 % 32))) {
+ if (_has_bits_[8 / 32] & 3840) {
// optional bool java_generic_services = 17 [default = false];
if (has_java_generic_services()) {
total_size += 2 + 1;
@@ -5488,6 +5733,11 @@ int FileOptions::ByteSize() const {
total_size += 2 + 1;
}
+ // optional bool cc_enable_arenas = 31 [default = false];
+ if (has_cc_enable_arenas()) {
+ total_size += 2 + 1;
+ }
+
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
total_size += 2 * this->uninterpreted_option_size();
@@ -5499,7 +5749,7 @@ int FileOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -5511,7 +5761,7 @@ int FileOptions::ByteSize() const {
}
void FileOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const FileOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const FileOptions*>(
&from);
@@ -5523,14 +5773,16 @@ void FileOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void FileOptions::MergeFrom(const FileOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_java_package()) {
- set_java_package(from.java_package());
+ set_has_java_package();
+ java_package_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.java_package_);
}
if (from.has_java_outer_classname()) {
- set_java_outer_classname(from.java_outer_classname());
+ set_has_java_outer_classname();
+ java_outer_classname_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.java_outer_classname_);
}
if (from.has_java_multiple_files()) {
set_java_multiple_files(from.java_multiple_files());
@@ -5545,7 +5797,8 @@ void FileOptions::MergeFrom(const FileOptions& from) {
set_optimize_for(from.optimize_for());
}
if (from.has_go_package()) {
- set_go_package(from.go_package());
+ set_has_go_package();
+ go_package_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.go_package_);
}
if (from.has_cc_generic_services()) {
set_cc_generic_services(from.cc_generic_services());
@@ -5561,9 +5814,14 @@ void FileOptions::MergeFrom(const FileOptions& from) {
if (from.has_deprecated()) {
set_deprecated(from.deprecated());
}
+ if (from.has_cc_enable_arenas()) {
+ set_cc_enable_arenas(from.cc_enable_arenas());
+ }
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void FileOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -5586,24 +5844,27 @@ bool FileOptions::IsInitialized() const {
}
void FileOptions::Swap(FileOptions* other) {
- if (other != this) {
- std::swap(java_package_, other->java_package_);
- std::swap(java_outer_classname_, other->java_outer_classname_);
- std::swap(java_multiple_files_, other->java_multiple_files_);
- std::swap(java_generate_equals_and_hash_, other->java_generate_equals_and_hash_);
- std::swap(java_string_check_utf8_, other->java_string_check_utf8_);
- std::swap(optimize_for_, other->optimize_for_);
- std::swap(go_package_, other->go_package_);
- std::swap(cc_generic_services_, other->cc_generic_services_);
- std::swap(java_generic_services_, other->java_generic_services_);
- std::swap(py_generic_services_, other->py_generic_services_);
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void FileOptions::InternalSwap(FileOptions* other) {
+ java_package_.Swap(&other->java_package_);
+ java_outer_classname_.Swap(&other->java_outer_classname_);
+ std::swap(java_multiple_files_, other->java_multiple_files_);
+ std::swap(java_generate_equals_and_hash_, other->java_generate_equals_and_hash_);
+ std::swap(java_string_check_utf8_, other->java_string_check_utf8_);
+ std::swap(optimize_for_, other->optimize_for_);
+ go_package_.Swap(&other->go_package_);
+ std::swap(cc_generic_services_, other->cc_generic_services_);
+ std::swap(java_generic_services_, other->java_generic_services_);
+ std::swap(py_generic_services_, other->py_generic_services_);
+ std::swap(deprecated_, other->deprecated_);
+ std::swap(cc_enable_arenas_, other->cc_enable_arenas_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata FileOptions::GetMetadata() const {
@@ -5621,11 +5882,12 @@ void FileOptions::Swap(FileOptions* other) {
const int MessageOptions::kMessageSetWireFormatFieldNumber;
const int MessageOptions::kNoStandardDescriptorAccessorFieldNumber;
const int MessageOptions::kDeprecatedFieldNumber;
+const int MessageOptions::kMapEntryFieldNumber;
const int MessageOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
MessageOptions::MessageOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.MessageOptions)
}
@@ -5634,7 +5896,8 @@ void MessageOptions::InitAsDefaultInstance() {
}
MessageOptions::MessageOptions(const MessageOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.MessageOptions)
@@ -5645,6 +5908,7 @@ void MessageOptions::SharedCtor() {
message_set_wire_format_ = false;
no_standard_descriptor_accessor_ = false;
deprecated_ = false;
+ map_entry_ = false;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -5675,8 +5939,12 @@ const MessageOptions& MessageOptions::default_instance() {
MessageOptions* MessageOptions::default_instance_ = NULL;
-MessageOptions* MessageOptions::New() const {
- return new MessageOptions;
+MessageOptions* MessageOptions::New(::google::protobuf::Arena* arena) const {
+ MessageOptions* n = new MessageOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void MessageOptions::Clear() {
@@ -5691,14 +5959,16 @@ void MessageOptions::Clear() {
::memset(&first, 0, n); \
} while (0)
- ZR_(message_set_wire_format_, deprecated_);
+ ZR_(message_set_wire_format_, map_entry_);
#undef OFFSET_OF_FIELD_
#undef ZR_
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool MessageOptions::MergePartialFromCodedStream(
@@ -5751,6 +6021,21 @@ bool MessageOptions::MergePartialFromCodedStream(
} else {
goto handle_unusual;
}
+ if (input->ExpectTag(56)) goto parse_map_entry;
+ break;
+ }
+
+ // optional bool map_entry = 7;
+ case 7: {
+ if (tag == 56) {
+ parse_map_entry:
+ DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
+ bool, ::google::protobuf::internal::WireFormatLite::TYPE_BOOL>(
+ input, &map_entry_)));
+ set_has_map_entry();
+ } else {
+ goto handle_unusual;
+ }
if (input->ExpectTag(7994)) goto parse_uninterpreted_option;
break;
}
@@ -5814,8 +6099,13 @@ void MessageOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteBool(3, this->deprecated(), output);
}
+ // optional bool map_entry = 7;
+ if (has_map_entry()) {
+ ::google::protobuf::internal::WireFormatLite::WriteBool(7, this->map_entry(), output);
+ }
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -5824,7 +6114,7 @@ void MessageOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -5849,8 +6139,13 @@ void MessageOptions::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(3, this->deprecated(), target);
}
+ // optional bool map_entry = 7;
+ if (has_map_entry()) {
+ target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(7, this->map_entry(), target);
+ }
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -5860,7 +6155,7 @@ void MessageOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -5871,7 +6166,7 @@ void MessageOptions::SerializeWithCachedSizes(
int MessageOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 15) {
// optional bool message_set_wire_format = 1 [default = false];
if (has_message_set_wire_format()) {
total_size += 1 + 1;
@@ -5887,6 +6182,11 @@ int MessageOptions::ByteSize() const {
total_size += 1 + 1;
}
+ // optional bool map_entry = 7;
+ if (has_map_entry()) {
+ total_size += 1 + 1;
+ }
+
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
total_size += 2 * this->uninterpreted_option_size();
@@ -5898,7 +6198,7 @@ int MessageOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -5910,7 +6210,7 @@ int MessageOptions::ByteSize() const {
}
void MessageOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const MessageOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const MessageOptions*>(
&from);
@@ -5922,7 +6222,7 @@ void MessageOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void MessageOptions::MergeFrom(const MessageOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_message_set_wire_format()) {
@@ -5934,9 +6234,14 @@ void MessageOptions::MergeFrom(const MessageOptions& from) {
if (from.has_deprecated()) {
set_deprecated(from.deprecated());
}
+ if (from.has_map_entry()) {
+ set_map_entry(from.map_entry());
+ }
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void MessageOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -5959,16 +6264,19 @@ bool MessageOptions::IsInitialized() const {
}
void MessageOptions::Swap(MessageOptions* other) {
- if (other != this) {
- std::swap(message_set_wire_format_, other->message_set_wire_format_);
- std::swap(no_standard_descriptor_accessor_, other->no_standard_descriptor_accessor_);
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void MessageOptions::InternalSwap(MessageOptions* other) {
+ std::swap(message_set_wire_format_, other->message_set_wire_format_);
+ std::swap(no_standard_descriptor_accessor_, other->no_standard_descriptor_accessor_);
+ std::swap(deprecated_, other->deprecated_);
+ std::swap(map_entry_, other->map_entry_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata MessageOptions::GetMetadata() const {
@@ -6010,13 +6318,12 @@ const int FieldOptions::kCtypeFieldNumber;
const int FieldOptions::kPackedFieldNumber;
const int FieldOptions::kLazyFieldNumber;
const int FieldOptions::kDeprecatedFieldNumber;
-const int FieldOptions::kExperimentalMapKeyFieldNumber;
const int FieldOptions::kWeakFieldNumber;
const int FieldOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
FieldOptions::FieldOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.FieldOptions)
}
@@ -6025,20 +6332,19 @@ void FieldOptions::InitAsDefaultInstance() {
}
FieldOptions::FieldOptions(const FieldOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.FieldOptions)
}
void FieldOptions::SharedCtor() {
- ::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
ctype_ = 0;
packed_ = false;
lazy_ = false;
deprecated_ = false;
- experimental_map_key_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
weak_ = false;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -6049,9 +6355,6 @@ FieldOptions::~FieldOptions() {
}
void FieldOptions::SharedDtor() {
- if (experimental_map_key_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete experimental_map_key_;
- }
if (this != default_instance_) {
}
}
@@ -6073,8 +6376,12 @@ const FieldOptions& FieldOptions::default_instance() {
FieldOptions* FieldOptions::default_instance_ = NULL;
-FieldOptions* FieldOptions::New() const {
- return new FieldOptions;
+FieldOptions* FieldOptions::New(::google::protobuf::Arena* arena) const {
+ FieldOptions* n = new FieldOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void FieldOptions::Clear() {
@@ -6089,13 +6396,8 @@ void FieldOptions::Clear() {
::memset(&first, 0, n); \
} while (0)
- if (_has_bits_[0 / 32] & 63) {
+ if (_has_bits_[0 / 32] & 31) {
ZR_(ctype_, weak_);
- if (has_experimental_map_key()) {
- if (experimental_map_key_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_->clear();
- }
- }
}
#undef OFFSET_OF_FIELD_
@@ -6103,7 +6405,9 @@ void FieldOptions::Clear() {
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool FieldOptions::MergePartialFromCodedStream(
@@ -6176,23 +6480,6 @@ bool FieldOptions::MergePartialFromCodedStream(
} else {
goto handle_unusual;
}
- if (input->ExpectTag(74)) goto parse_experimental_map_key;
- break;
- }
-
- // optional string experimental_map_key = 9;
- case 9: {
- if (tag == 74) {
- parse_experimental_map_key:
- DO_(::google::protobuf::internal::WireFormatLite::ReadString(
- input, this->mutable_experimental_map_key()));
- ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
- this->experimental_map_key().data(), this->experimental_map_key().length(),
- ::google::protobuf::internal::WireFormat::PARSE,
- "experimental_map_key");
- } else {
- goto handle_unusual;
- }
if (input->ExpectTag(80)) goto parse_weak;
break;
}
@@ -6277,23 +6564,13 @@ void FieldOptions::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteBool(5, this->lazy(), output);
}
- // optional string experimental_map_key = 9;
- if (has_experimental_map_key()) {
- ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
- this->experimental_map_key().data(), this->experimental_map_key().length(),
- ::google::protobuf::internal::WireFormat::SERIALIZE,
- "experimental_map_key");
- ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
- 9, this->experimental_map_key(), output);
- }
-
// optional bool weak = 10 [default = false];
if (has_weak()) {
::google::protobuf::internal::WireFormatLite::WriteBool(10, this->weak(), output);
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -6302,7 +6579,7 @@ void FieldOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -6333,24 +6610,13 @@ void FieldOptions::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(5, this->lazy(), target);
}
- // optional string experimental_map_key = 9;
- if (has_experimental_map_key()) {
- ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
- this->experimental_map_key().data(), this->experimental_map_key().length(),
- ::google::protobuf::internal::WireFormat::SERIALIZE,
- "experimental_map_key");
- target =
- ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
- 9, this->experimental_map_key(), target);
- }
-
// optional bool weak = 10 [default = false];
if (has_weak()) {
target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(10, this->weak(), target);
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -6360,7 +6626,7 @@ void FieldOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -6371,7 +6637,7 @@ void FieldOptions::SerializeWithCachedSizes(
int FieldOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 31) {
// optional .google.protobuf.FieldOptions.CType ctype = 1 [default = STRING];
if (has_ctype()) {
total_size += 1 +
@@ -6393,13 +6659,6 @@ int FieldOptions::ByteSize() const {
total_size += 1 + 1;
}
- // optional string experimental_map_key = 9;
- if (has_experimental_map_key()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::StringSize(
- this->experimental_map_key());
- }
-
// optional bool weak = 10 [default = false];
if (has_weak()) {
total_size += 1 + 1;
@@ -6416,7 +6675,7 @@ int FieldOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -6428,7 +6687,7 @@ int FieldOptions::ByteSize() const {
}
void FieldOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const FieldOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const FieldOptions*>(
&from);
@@ -6440,7 +6699,7 @@ void FieldOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void FieldOptions::MergeFrom(const FieldOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_ctype()) {
@@ -6455,15 +6714,14 @@ void FieldOptions::MergeFrom(const FieldOptions& from) {
if (from.has_deprecated()) {
set_deprecated(from.deprecated());
}
- if (from.has_experimental_map_key()) {
- set_experimental_map_key(from.experimental_map_key());
- }
if (from.has_weak()) {
set_weak(from.weak());
}
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void FieldOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -6486,19 +6744,20 @@ bool FieldOptions::IsInitialized() const {
}
void FieldOptions::Swap(FieldOptions* other) {
- if (other != this) {
- std::swap(ctype_, other->ctype_);
- std::swap(packed_, other->packed_);
- std::swap(lazy_, other->lazy_);
- std::swap(deprecated_, other->deprecated_);
- std::swap(experimental_map_key_, other->experimental_map_key_);
- std::swap(weak_, other->weak_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void FieldOptions::InternalSwap(FieldOptions* other) {
+ std::swap(ctype_, other->ctype_);
+ std::swap(packed_, other->packed_);
+ std::swap(lazy_, other->lazy_);
+ std::swap(deprecated_, other->deprecated_);
+ std::swap(weak_, other->weak_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata FieldOptions::GetMetadata() const {
@@ -6519,7 +6778,7 @@ const int EnumOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
EnumOptions::EnumOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.EnumOptions)
}
@@ -6528,7 +6787,8 @@ void EnumOptions::InitAsDefaultInstance() {
}
EnumOptions::EnumOptions(const EnumOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.EnumOptions)
@@ -6568,8 +6828,12 @@ const EnumOptions& EnumOptions::default_instance() {
EnumOptions* EnumOptions::default_instance_ = NULL;
-EnumOptions* EnumOptions::New() const {
- return new EnumOptions;
+EnumOptions* EnumOptions::New(::google::protobuf::Arena* arena) const {
+ EnumOptions* n = new EnumOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void EnumOptions::Clear() {
@@ -6591,7 +6855,9 @@ void EnumOptions::Clear() {
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool EnumOptions::MergePartialFromCodedStream(
@@ -6688,7 +6954,7 @@ void EnumOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -6697,7 +6963,7 @@ void EnumOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -6718,7 +6984,7 @@ void EnumOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -6728,7 +6994,7 @@ void EnumOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -6739,7 +7005,7 @@ void EnumOptions::SerializeWithCachedSizes(
int EnumOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (_has_bits_[0 / 32] & 3) {
// optional bool allow_alias = 2;
if (has_allow_alias()) {
total_size += 1 + 1;
@@ -6761,7 +7027,7 @@ int EnumOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -6773,7 +7039,7 @@ int EnumOptions::ByteSize() const {
}
void EnumOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const EnumOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const EnumOptions*>(
&from);
@@ -6785,7 +7051,7 @@ void EnumOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void EnumOptions::MergeFrom(const EnumOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_allow_alias()) {
@@ -6796,7 +7062,9 @@ void EnumOptions::MergeFrom(const EnumOptions& from) {
}
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void EnumOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -6819,15 +7087,17 @@ bool EnumOptions::IsInitialized() const {
}
void EnumOptions::Swap(EnumOptions* other) {
- if (other != this) {
- std::swap(allow_alias_, other->allow_alias_);
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void EnumOptions::InternalSwap(EnumOptions* other) {
+ std::swap(allow_alias_, other->allow_alias_);
+ std::swap(deprecated_, other->deprecated_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata EnumOptions::GetMetadata() const {
@@ -6847,7 +7117,7 @@ const int EnumValueOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
EnumValueOptions::EnumValueOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.EnumValueOptions)
}
@@ -6856,7 +7126,8 @@ void EnumValueOptions::InitAsDefaultInstance() {
}
EnumValueOptions::EnumValueOptions(const EnumValueOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.EnumValueOptions)
@@ -6895,8 +7166,12 @@ const EnumValueOptions& EnumValueOptions::default_instance() {
EnumValueOptions* EnumValueOptions::default_instance_ = NULL;
-EnumValueOptions* EnumValueOptions::New() const {
- return new EnumValueOptions;
+EnumValueOptions* EnumValueOptions::New(::google::protobuf::Arena* arena) const {
+ EnumValueOptions* n = new EnumValueOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void EnumValueOptions::Clear() {
@@ -6904,7 +7179,9 @@ void EnumValueOptions::Clear() {
deprecated_ = false;
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool EnumValueOptions::MergePartialFromCodedStream(
@@ -6981,7 +7258,7 @@ void EnumValueOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -6990,7 +7267,7 @@ void EnumValueOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -7006,7 +7283,7 @@ void EnumValueOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -7016,7 +7293,7 @@ void EnumValueOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -7027,13 +7304,11 @@ void EnumValueOptions::SerializeWithCachedSizes(
int EnumValueOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
- // optional bool deprecated = 1 [default = false];
- if (has_deprecated()) {
- total_size += 1 + 1;
- }
-
+ // optional bool deprecated = 1 [default = false];
+ if (has_deprecated()) {
+ total_size += 1 + 1;
}
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
total_size += 2 * this->uninterpreted_option_size();
for (int i = 0; i < this->uninterpreted_option_size(); i++) {
@@ -7044,7 +7319,7 @@ int EnumValueOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -7056,7 +7331,7 @@ int EnumValueOptions::ByteSize() const {
}
void EnumValueOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const EnumValueOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const EnumValueOptions*>(
&from);
@@ -7068,7 +7343,7 @@ void EnumValueOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void EnumValueOptions::MergeFrom(const EnumValueOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_deprecated()) {
@@ -7076,7 +7351,9 @@ void EnumValueOptions::MergeFrom(const EnumValueOptions& from) {
}
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void EnumValueOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -7099,14 +7376,16 @@ bool EnumValueOptions::IsInitialized() const {
}
void EnumValueOptions::Swap(EnumValueOptions* other) {
- if (other != this) {
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void EnumValueOptions::InternalSwap(EnumValueOptions* other) {
+ std::swap(deprecated_, other->deprecated_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata EnumValueOptions::GetMetadata() const {
@@ -7126,7 +7405,7 @@ const int ServiceOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
ServiceOptions::ServiceOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.ServiceOptions)
}
@@ -7135,7 +7414,8 @@ void ServiceOptions::InitAsDefaultInstance() {
}
ServiceOptions::ServiceOptions(const ServiceOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.ServiceOptions)
@@ -7174,8 +7454,12 @@ const ServiceOptions& ServiceOptions::default_instance() {
ServiceOptions* ServiceOptions::default_instance_ = NULL;
-ServiceOptions* ServiceOptions::New() const {
- return new ServiceOptions;
+ServiceOptions* ServiceOptions::New(::google::protobuf::Arena* arena) const {
+ ServiceOptions* n = new ServiceOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void ServiceOptions::Clear() {
@@ -7183,7 +7467,9 @@ void ServiceOptions::Clear() {
deprecated_ = false;
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool ServiceOptions::MergePartialFromCodedStream(
@@ -7260,7 +7546,7 @@ void ServiceOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -7269,7 +7555,7 @@ void ServiceOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -7285,7 +7571,7 @@ void ServiceOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -7295,7 +7581,7 @@ void ServiceOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -7306,13 +7592,11 @@ void ServiceOptions::SerializeWithCachedSizes(
int ServiceOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
- // optional bool deprecated = 33 [default = false];
- if (has_deprecated()) {
- total_size += 2 + 1;
- }
-
+ // optional bool deprecated = 33 [default = false];
+ if (has_deprecated()) {
+ total_size += 2 + 1;
}
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
total_size += 2 * this->uninterpreted_option_size();
for (int i = 0; i < this->uninterpreted_option_size(); i++) {
@@ -7323,7 +7607,7 @@ int ServiceOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -7335,7 +7619,7 @@ int ServiceOptions::ByteSize() const {
}
void ServiceOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const ServiceOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const ServiceOptions*>(
&from);
@@ -7347,7 +7631,7 @@ void ServiceOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void ServiceOptions::MergeFrom(const ServiceOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_deprecated()) {
@@ -7355,7 +7639,9 @@ void ServiceOptions::MergeFrom(const ServiceOptions& from) {
}
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void ServiceOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -7378,14 +7664,16 @@ bool ServiceOptions::IsInitialized() const {
}
void ServiceOptions::Swap(ServiceOptions* other) {
- if (other != this) {
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void ServiceOptions::InternalSwap(ServiceOptions* other) {
+ std::swap(deprecated_, other->deprecated_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata ServiceOptions::GetMetadata() const {
@@ -7405,7 +7693,7 @@ const int MethodOptions::kUninterpretedOptionFieldNumber;
#endif // !_MSC_VER
MethodOptions::MethodOptions()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.MethodOptions)
}
@@ -7414,7 +7702,8 @@ void MethodOptions::InitAsDefaultInstance() {
}
MethodOptions::MethodOptions(const MethodOptions& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.MethodOptions)
@@ -7453,8 +7742,12 @@ const MethodOptions& MethodOptions::default_instance() {
MethodOptions* MethodOptions::default_instance_ = NULL;
-MethodOptions* MethodOptions::New() const {
- return new MethodOptions;
+MethodOptions* MethodOptions::New(::google::protobuf::Arena* arena) const {
+ MethodOptions* n = new MethodOptions;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void MethodOptions::Clear() {
@@ -7462,7 +7755,9 @@ void MethodOptions::Clear() {
deprecated_ = false;
uninterpreted_option_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool MethodOptions::MergePartialFromCodedStream(
@@ -7539,7 +7834,7 @@ void MethodOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
999, this->uninterpreted_option(i), output);
}
@@ -7548,7 +7843,7 @@ void MethodOptions::SerializeWithCachedSizes(
_extensions_.SerializeWithCachedSizes(
1000, 536870912, output);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -7564,7 +7859,7 @@ void MethodOptions::SerializeWithCachedSizes(
}
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
- for (int i = 0; i < this->uninterpreted_option_size(); i++) {
+ for (unsigned int i = 0, n = this->uninterpreted_option_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
999, this->uninterpreted_option(i), target);
@@ -7574,7 +7869,7 @@ void MethodOptions::SerializeWithCachedSizes(
target = _extensions_.SerializeWithCachedSizesToArray(
1000, 536870912, target);
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -7585,13 +7880,11 @@ void MethodOptions::SerializeWithCachedSizes(
int MethodOptions::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
- // optional bool deprecated = 33 [default = false];
- if (has_deprecated()) {
- total_size += 2 + 1;
- }
-
+ // optional bool deprecated = 33 [default = false];
+ if (has_deprecated()) {
+ total_size += 2 + 1;
}
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
total_size += 2 * this->uninterpreted_option_size();
for (int i = 0; i < this->uninterpreted_option_size(); i++) {
@@ -7602,7 +7895,7 @@ int MethodOptions::ByteSize() const {
total_size += _extensions_.ByteSize();
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -7614,7 +7907,7 @@ int MethodOptions::ByteSize() const {
}
void MethodOptions::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const MethodOptions* source =
::google::protobuf::internal::dynamic_cast_if_available<const MethodOptions*>(
&from);
@@ -7626,7 +7919,7 @@ void MethodOptions::MergeFrom(const ::google::protobuf::Message& from) {
}
void MethodOptions::MergeFrom(const MethodOptions& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
uninterpreted_option_.MergeFrom(from.uninterpreted_option_);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_deprecated()) {
@@ -7634,7 +7927,9 @@ void MethodOptions::MergeFrom(const MethodOptions& from) {
}
}
_extensions_.MergeFrom(from._extensions_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void MethodOptions::CopyFrom(const ::google::protobuf::Message& from) {
@@ -7657,14 +7952,16 @@ bool MethodOptions::IsInitialized() const {
}
void MethodOptions::Swap(MethodOptions* other) {
- if (other != this) {
- std::swap(deprecated_, other->deprecated_);
- uninterpreted_option_.Swap(&other->uninterpreted_option_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- _extensions_.Swap(&other->_extensions_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void MethodOptions::InternalSwap(MethodOptions* other) {
+ std::swap(deprecated_, other->deprecated_);
+ uninterpreted_option_.UnsafeArenaSwap(&other->uninterpreted_option_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
+ _extensions_.Swap(&other->_extensions_);
}
::google::protobuf::Metadata MethodOptions::GetMetadata() const {
@@ -7684,7 +7981,7 @@ const int UninterpretedOption_NamePart::kIsExtensionFieldNumber;
#endif // !_MSC_VER
UninterpretedOption_NamePart::UninterpretedOption_NamePart()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.UninterpretedOption.NamePart)
}
@@ -7693,7 +7990,8 @@ void UninterpretedOption_NamePart::InitAsDefaultInstance() {
}
UninterpretedOption_NamePart::UninterpretedOption_NamePart(const UninterpretedOption_NamePart& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.UninterpretedOption.NamePart)
@@ -7702,7 +8000,7 @@ UninterpretedOption_NamePart::UninterpretedOption_NamePart(const UninterpretedOp
void UninterpretedOption_NamePart::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- name_part_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ name_part_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
is_extension_ = false;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -7713,9 +8011,7 @@ UninterpretedOption_NamePart::~UninterpretedOption_NamePart() {
}
void UninterpretedOption_NamePart::SharedDtor() {
- if (name_part_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_part_;
- }
+ name_part_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -7737,21 +8033,25 @@ const UninterpretedOption_NamePart& UninterpretedOption_NamePart::default_instan
UninterpretedOption_NamePart* UninterpretedOption_NamePart::default_instance_ = NULL;
-UninterpretedOption_NamePart* UninterpretedOption_NamePart::New() const {
- return new UninterpretedOption_NamePart;
+UninterpretedOption_NamePart* UninterpretedOption_NamePart::New(::google::protobuf::Arena* arena) const {
+ UninterpretedOption_NamePart* n = new UninterpretedOption_NamePart;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void UninterpretedOption_NamePart::Clear() {
if (_has_bits_[0 / 32] & 3) {
if (has_name_part()) {
- if (name_part_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_->clear();
- }
+ name_part_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
is_extension_ = false;
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool UninterpretedOption_NamePart::MergePartialFromCodedStream(
@@ -7772,7 +8072,7 @@ bool UninterpretedOption_NamePart::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name_part().data(), this->name_part().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "name_part");
+ "google.protobuf.UninterpretedOption.NamePart.name_part");
} else {
goto handle_unusual;
}
@@ -7825,7 +8125,7 @@ void UninterpretedOption_NamePart::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name_part().data(), this->name_part().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name_part");
+ "google.protobuf.UninterpretedOption.NamePart.name_part");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
1, this->name_part(), output);
}
@@ -7835,7 +8135,7 @@ void UninterpretedOption_NamePart::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormatLite::WriteBool(2, this->is_extension(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -7850,7 +8150,7 @@ void UninterpretedOption_NamePart::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->name_part().data(), this->name_part().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "name_part");
+ "google.protobuf.UninterpretedOption.NamePart.name_part");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this->name_part(), target);
@@ -7861,7 +8161,7 @@ void UninterpretedOption_NamePart::SerializeWithCachedSizes(
target = ::google::protobuf::internal::WireFormatLite::WriteBoolToArray(2, this->is_extension(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -7869,24 +8169,39 @@ void UninterpretedOption_NamePart::SerializeWithCachedSizes(
return target;
}
+int UninterpretedOption_NamePart::RequiredFieldsByteSizeFallback() const {
+ int total_size = 0;
+
+ if (has_name_part()) {
+ // required string name_part = 1;
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->name_part());
+ }
+
+ if (has_is_extension()) {
+ // required bool is_extension = 2;
+ total_size += 1 + 1;
+ }
+
+ return total_size;
+}
int UninterpretedOption_NamePart::ByteSize() const {
int total_size = 0;
- if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
+ if (((_has_bits_[0] & 0x00000003) ^ 0x00000003) == 0) { // All required fields are present.
// required string name_part = 1;
- if (has_name_part()) {
- total_size += 1 +
- ::google::protobuf::internal::WireFormatLite::StringSize(
- this->name_part());
- }
+ total_size += 1 +
+ ::google::protobuf::internal::WireFormatLite::StringSize(
+ this->name_part());
// required bool is_extension = 2;
- if (has_is_extension()) {
- total_size += 1 + 1;
- }
+ total_size += 1 + 1;
+ } else {
+ total_size += RequiredFieldsByteSizeFallback();
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -7898,7 +8213,7 @@ int UninterpretedOption_NamePart::ByteSize() const {
}
void UninterpretedOption_NamePart::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const UninterpretedOption_NamePart* source =
::google::protobuf::internal::dynamic_cast_if_available<const UninterpretedOption_NamePart*>(
&from);
@@ -7910,16 +8225,19 @@ void UninterpretedOption_NamePart::MergeFrom(const ::google::protobuf::Message&
}
void UninterpretedOption_NamePart::MergeFrom(const UninterpretedOption_NamePart& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
if (from.has_name_part()) {
- set_name_part(from.name_part());
+ set_has_name_part();
+ name_part_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_part_);
}
if (from.has_is_extension()) {
set_is_extension(from.is_extension());
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void UninterpretedOption_NamePart::CopyFrom(const ::google::protobuf::Message& from) {
@@ -7941,13 +8259,15 @@ bool UninterpretedOption_NamePart::IsInitialized() const {
}
void UninterpretedOption_NamePart::Swap(UninterpretedOption_NamePart* other) {
- if (other != this) {
- std::swap(name_part_, other->name_part_);
- std::swap(is_extension_, other->is_extension_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void UninterpretedOption_NamePart::InternalSwap(UninterpretedOption_NamePart* other) {
+ name_part_.Swap(&other->name_part_);
+ std::swap(is_extension_, other->is_extension_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata UninterpretedOption_NamePart::GetMetadata() const {
@@ -7972,7 +8292,7 @@ const int UninterpretedOption::kAggregateValueFieldNumber;
#endif // !_MSC_VER
UninterpretedOption::UninterpretedOption()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.UninterpretedOption)
}
@@ -7981,7 +8301,8 @@ void UninterpretedOption::InitAsDefaultInstance() {
}
UninterpretedOption::UninterpretedOption(const UninterpretedOption& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.UninterpretedOption)
@@ -7990,12 +8311,12 @@ UninterpretedOption::UninterpretedOption(const UninterpretedOption& from)
void UninterpretedOption::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- identifier_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ identifier_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
positive_int_value_ = GOOGLE_ULONGLONG(0);
negative_int_value_ = GOOGLE_LONGLONG(0);
double_value_ = 0;
- string_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- aggregate_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ string_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ aggregate_value_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -8005,15 +8326,9 @@ UninterpretedOption::~UninterpretedOption() {
}
void UninterpretedOption::SharedDtor() {
- if (identifier_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete identifier_value_;
- }
- if (string_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete string_value_;
- }
- if (aggregate_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete aggregate_value_;
- }
+ identifier_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ string_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ aggregate_value_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -8035,8 +8350,12 @@ const UninterpretedOption& UninterpretedOption::default_instance() {
UninterpretedOption* UninterpretedOption::default_instance_ = NULL;
-UninterpretedOption* UninterpretedOption::New() const {
- return new UninterpretedOption;
+UninterpretedOption* UninterpretedOption::New(::google::protobuf::Arena* arena) const {
+ UninterpretedOption* n = new UninterpretedOption;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void UninterpretedOption::Clear() {
@@ -8053,19 +8372,13 @@ void UninterpretedOption::Clear() {
if (_has_bits_[0 / 32] & 126) {
ZR_(positive_int_value_, double_value_);
if (has_identifier_value()) {
- if (identifier_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_->clear();
- }
+ identifier_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_string_value()) {
- if (string_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_->clear();
- }
+ string_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_aggregate_value()) {
- if (aggregate_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_->clear();
- }
+ aggregate_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
}
@@ -8074,7 +8387,9 @@ void UninterpretedOption::Clear() {
name_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool UninterpretedOption::MergePartialFromCodedStream(
@@ -8110,7 +8425,7 @@ bool UninterpretedOption::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->identifier_value().data(), this->identifier_value().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "identifier_value");
+ "google.protobuf.UninterpretedOption.identifier_value");
} else {
goto handle_unusual;
}
@@ -8185,7 +8500,7 @@ bool UninterpretedOption::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->aggregate_value().data(), this->aggregate_value().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "aggregate_value");
+ "google.protobuf.UninterpretedOption.aggregate_value");
} else {
goto handle_unusual;
}
@@ -8219,7 +8534,7 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// @@protoc_insertion_point(serialize_start:google.protobuf.UninterpretedOption)
// repeated .google.protobuf.UninterpretedOption.NamePart name = 2;
- for (int i = 0; i < this->name_size(); i++) {
+ for (unsigned int i = 0, n = this->name_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
2, this->name(i), output);
}
@@ -8229,7 +8544,7 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->identifier_value().data(), this->identifier_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "identifier_value");
+ "google.protobuf.UninterpretedOption.identifier_value");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
3, this->identifier_value(), output);
}
@@ -8260,12 +8575,12 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->aggregate_value().data(), this->aggregate_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "aggregate_value");
+ "google.protobuf.UninterpretedOption.aggregate_value");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
8, this->aggregate_value(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -8276,7 +8591,7 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::uint8* target) const {
// @@protoc_insertion_point(serialize_to_array_start:google.protobuf.UninterpretedOption)
// repeated .google.protobuf.UninterpretedOption.NamePart name = 2;
- for (int i = 0; i < this->name_size(); i++) {
+ for (unsigned int i = 0, n = this->name_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
2, this->name(i), target);
@@ -8287,7 +8602,7 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->identifier_value().data(), this->identifier_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "identifier_value");
+ "google.protobuf.UninterpretedOption.identifier_value");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
3, this->identifier_value(), target);
@@ -8320,13 +8635,13 @@ void UninterpretedOption::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->aggregate_value().data(), this->aggregate_value().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "aggregate_value");
+ "google.protobuf.UninterpretedOption.aggregate_value");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
8, this->aggregate_value(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -8337,7 +8652,7 @@ void UninterpretedOption::SerializeWithCachedSizes(
int UninterpretedOption::ByteSize() const {
int total_size = 0;
- if (_has_bits_[1 / 32] & (0xffu << (1 % 32))) {
+ if (_has_bits_[1 / 32] & 126) {
// optional string identifier_value = 3;
if (has_identifier_value()) {
total_size += 1 +
@@ -8387,7 +8702,7 @@ int UninterpretedOption::ByteSize() const {
this->name(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -8399,7 +8714,7 @@ int UninterpretedOption::ByteSize() const {
}
void UninterpretedOption::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const UninterpretedOption* source =
::google::protobuf::internal::dynamic_cast_if_available<const UninterpretedOption*>(
&from);
@@ -8411,11 +8726,12 @@ void UninterpretedOption::MergeFrom(const ::google::protobuf::Message& from) {
}
void UninterpretedOption::MergeFrom(const UninterpretedOption& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
name_.MergeFrom(from.name_);
if (from._has_bits_[1 / 32] & (0xffu << (1 % 32))) {
if (from.has_identifier_value()) {
- set_identifier_value(from.identifier_value());
+ set_has_identifier_value();
+ identifier_value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.identifier_value_);
}
if (from.has_positive_int_value()) {
set_positive_int_value(from.positive_int_value());
@@ -8427,13 +8743,17 @@ void UninterpretedOption::MergeFrom(const UninterpretedOption& from) {
set_double_value(from.double_value());
}
if (from.has_string_value()) {
- set_string_value(from.string_value());
+ set_has_string_value();
+ string_value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.string_value_);
}
if (from.has_aggregate_value()) {
- set_aggregate_value(from.aggregate_value());
+ set_has_aggregate_value();
+ aggregate_value_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.aggregate_value_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void UninterpretedOption::CopyFrom(const ::google::protobuf::Message& from) {
@@ -8455,18 +8775,20 @@ bool UninterpretedOption::IsInitialized() const {
}
void UninterpretedOption::Swap(UninterpretedOption* other) {
- if (other != this) {
- name_.Swap(&other->name_);
- std::swap(identifier_value_, other->identifier_value_);
- std::swap(positive_int_value_, other->positive_int_value_);
- std::swap(negative_int_value_, other->negative_int_value_);
- std::swap(double_value_, other->double_value_);
- std::swap(string_value_, other->string_value_);
- std::swap(aggregate_value_, other->aggregate_value_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void UninterpretedOption::InternalSwap(UninterpretedOption* other) {
+ name_.UnsafeArenaSwap(&other->name_);
+ identifier_value_.Swap(&other->identifier_value_);
+ std::swap(positive_int_value_, other->positive_int_value_);
+ std::swap(negative_int_value_, other->negative_int_value_);
+ std::swap(double_value_, other->double_value_);
+ string_value_.Swap(&other->string_value_);
+ aggregate_value_.Swap(&other->aggregate_value_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata UninterpretedOption::GetMetadata() const {
@@ -8488,7 +8810,7 @@ const int SourceCodeInfo_Location::kTrailingCommentsFieldNumber;
#endif // !_MSC_VER
SourceCodeInfo_Location::SourceCodeInfo_Location()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.SourceCodeInfo.Location)
}
@@ -8497,7 +8819,8 @@ void SourceCodeInfo_Location::InitAsDefaultInstance() {
}
SourceCodeInfo_Location::SourceCodeInfo_Location(const SourceCodeInfo_Location& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.SourceCodeInfo.Location)
@@ -8506,8 +8829,8 @@ SourceCodeInfo_Location::SourceCodeInfo_Location(const SourceCodeInfo_Location&
void SourceCodeInfo_Location::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
- leading_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- trailing_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ leading_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ trailing_comments_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
@@ -8517,12 +8840,8 @@ SourceCodeInfo_Location::~SourceCodeInfo_Location() {
}
void SourceCodeInfo_Location::SharedDtor() {
- if (leading_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete leading_comments_;
- }
- if (trailing_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete trailing_comments_;
- }
+ leading_comments_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ trailing_comments_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
if (this != default_instance_) {
}
}
@@ -8544,27 +8863,29 @@ const SourceCodeInfo_Location& SourceCodeInfo_Location::default_instance() {
SourceCodeInfo_Location* SourceCodeInfo_Location::default_instance_ = NULL;
-SourceCodeInfo_Location* SourceCodeInfo_Location::New() const {
- return new SourceCodeInfo_Location;
+SourceCodeInfo_Location* SourceCodeInfo_Location::New(::google::protobuf::Arena* arena) const {
+ SourceCodeInfo_Location* n = new SourceCodeInfo_Location;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void SourceCodeInfo_Location::Clear() {
if (_has_bits_[0 / 32] & 12) {
if (has_leading_comments()) {
- if (leading_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_->clear();
- }
+ leading_comments_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
if (has_trailing_comments()) {
- if (trailing_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_->clear();
- }
+ trailing_comments_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
}
path_.Clear();
span_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool SourceCodeInfo_Location::MergePartialFromCodedStream(
@@ -8621,7 +8942,7 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->leading_comments().data(), this->leading_comments().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "leading_comments");
+ "google.protobuf.SourceCodeInfo.Location.leading_comments");
} else {
goto handle_unusual;
}
@@ -8638,7 +8959,7 @@ bool SourceCodeInfo_Location::MergePartialFromCodedStream(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->trailing_comments().data(), this->trailing_comments().length(),
::google::protobuf::internal::WireFormat::PARSE,
- "trailing_comments");
+ "google.protobuf.SourceCodeInfo.Location.trailing_comments");
} else {
goto handle_unusual;
}
@@ -8696,7 +9017,7 @@ void SourceCodeInfo_Location::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->leading_comments().data(), this->leading_comments().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "leading_comments");
+ "google.protobuf.SourceCodeInfo.Location.leading_comments");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
3, this->leading_comments(), output);
}
@@ -8706,12 +9027,12 @@ void SourceCodeInfo_Location::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->trailing_comments().data(), this->trailing_comments().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "trailing_comments");
+ "google.protobuf.SourceCodeInfo.Location.trailing_comments");
::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
4, this->trailing_comments(), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -8754,7 +9075,7 @@ void SourceCodeInfo_Location::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->leading_comments().data(), this->leading_comments().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "leading_comments");
+ "google.protobuf.SourceCodeInfo.Location.leading_comments");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
3, this->leading_comments(), target);
@@ -8765,13 +9086,13 @@ void SourceCodeInfo_Location::SerializeWithCachedSizes(
::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
this->trailing_comments().data(), this->trailing_comments().length(),
::google::protobuf::internal::WireFormat::SERIALIZE,
- "trailing_comments");
+ "google.protobuf.SourceCodeInfo.Location.trailing_comments");
target =
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
4, this->trailing_comments(), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -8782,7 +9103,7 @@ void SourceCodeInfo_Location::SerializeWithCachedSizes(
int SourceCodeInfo_Location::ByteSize() const {
int total_size = 0;
- if (_has_bits_[2 / 32] & (0xffu << (2 % 32))) {
+ if (_has_bits_[2 / 32] & 12) {
// optional string leading_comments = 3;
if (has_leading_comments()) {
total_size += 1 +
@@ -8832,7 +9153,7 @@ int SourceCodeInfo_Location::ByteSize() const {
total_size += data_size;
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -8844,7 +9165,7 @@ int SourceCodeInfo_Location::ByteSize() const {
}
void SourceCodeInfo_Location::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const SourceCodeInfo_Location* source =
::google::protobuf::internal::dynamic_cast_if_available<const SourceCodeInfo_Location*>(
&from);
@@ -8856,18 +9177,22 @@ void SourceCodeInfo_Location::MergeFrom(const ::google::protobuf::Message& from)
}
void SourceCodeInfo_Location::MergeFrom(const SourceCodeInfo_Location& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
path_.MergeFrom(from.path_);
span_.MergeFrom(from.span_);
if (from._has_bits_[2 / 32] & (0xffu << (2 % 32))) {
if (from.has_leading_comments()) {
- set_leading_comments(from.leading_comments());
+ set_has_leading_comments();
+ leading_comments_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.leading_comments_);
}
if (from.has_trailing_comments()) {
- set_trailing_comments(from.trailing_comments());
+ set_has_trailing_comments();
+ trailing_comments_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.trailing_comments_);
}
}
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void SourceCodeInfo_Location::CopyFrom(const ::google::protobuf::Message& from) {
@@ -8888,15 +9213,17 @@ bool SourceCodeInfo_Location::IsInitialized() const {
}
void SourceCodeInfo_Location::Swap(SourceCodeInfo_Location* other) {
- if (other != this) {
- path_.Swap(&other->path_);
- span_.Swap(&other->span_);
- std::swap(leading_comments_, other->leading_comments_);
- std::swap(trailing_comments_, other->trailing_comments_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void SourceCodeInfo_Location::InternalSwap(SourceCodeInfo_Location* other) {
+ path_.UnsafeArenaSwap(&other->path_);
+ span_.UnsafeArenaSwap(&other->span_);
+ leading_comments_.Swap(&other->leading_comments_);
+ trailing_comments_.Swap(&other->trailing_comments_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata SourceCodeInfo_Location::GetMetadata() const {
@@ -8915,7 +9242,7 @@ const int SourceCodeInfo::kLocationFieldNumber;
#endif // !_MSC_VER
SourceCodeInfo::SourceCodeInfo()
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message() , _internal_metadata_(NULL) {
SharedCtor();
// @@protoc_insertion_point(constructor:google.protobuf.SourceCodeInfo)
}
@@ -8924,7 +9251,8 @@ void SourceCodeInfo::InitAsDefaultInstance() {
}
SourceCodeInfo::SourceCodeInfo(const SourceCodeInfo& from)
- : ::google::protobuf::Message() {
+ : ::google::protobuf::Message(),
+ _internal_metadata_(NULL) {
SharedCtor();
MergeFrom(from);
// @@protoc_insertion_point(copy_constructor:google.protobuf.SourceCodeInfo)
@@ -8962,14 +9290,20 @@ const SourceCodeInfo& SourceCodeInfo::default_instance() {
SourceCodeInfo* SourceCodeInfo::default_instance_ = NULL;
-SourceCodeInfo* SourceCodeInfo::New() const {
- return new SourceCodeInfo;
+SourceCodeInfo* SourceCodeInfo::New(::google::protobuf::Arena* arena) const {
+ SourceCodeInfo* n = new SourceCodeInfo;
+ if (arena != NULL) {
+ arena->Own(n);
+ }
+ return n;
}
void SourceCodeInfo::Clear() {
location_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
- mutable_unknown_fields()->Clear();
+ if (_internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->Clear();
+ }
}
bool SourceCodeInfo::MergePartialFromCodedStream(
@@ -9022,12 +9356,12 @@ void SourceCodeInfo::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// @@protoc_insertion_point(serialize_start:google.protobuf.SourceCodeInfo)
// repeated .google.protobuf.SourceCodeInfo.Location location = 1;
- for (int i = 0; i < this->location_size(); i++) {
+ for (unsigned int i = 0, n = this->location_size(); i < n; i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
1, this->location(i), output);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
}
@@ -9038,13 +9372,13 @@ void SourceCodeInfo::SerializeWithCachedSizes(
::google::protobuf::uint8* target) const {
// @@protoc_insertion_point(serialize_to_array_start:google.protobuf.SourceCodeInfo)
// repeated .google.protobuf.SourceCodeInfo.Location location = 1;
- for (int i = 0; i < this->location_size(); i++) {
+ for (unsigned int i = 0, n = this->location_size(); i < n; i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
1, this->location(i), target);
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
}
@@ -9063,7 +9397,7 @@ int SourceCodeInfo::ByteSize() const {
this->location(i));
}
- if (!unknown_fields().empty()) {
+ if (_internal_metadata_.have_unknown_fields()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
@@ -9075,7 +9409,7 @@ int SourceCodeInfo::ByteSize() const {
}
void SourceCodeInfo::MergeFrom(const ::google::protobuf::Message& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
const SourceCodeInfo* source =
::google::protobuf::internal::dynamic_cast_if_available<const SourceCodeInfo*>(
&from);
@@ -9087,9 +9421,11 @@ void SourceCodeInfo::MergeFrom(const ::google::protobuf::Message& from) {
}
void SourceCodeInfo::MergeFrom(const SourceCodeInfo& from) {
- GOOGLE_CHECK_NE(&from, this);
+ if (GOOGLE_PREDICT_FALSE(&from == this)) MergeFromFail(__LINE__);
location_.MergeFrom(from.location_);
- mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ if (from._internal_metadata_.have_unknown_fields()) {
+ mutable_unknown_fields()->MergeFrom(from.unknown_fields());
+ }
}
void SourceCodeInfo::CopyFrom(const ::google::protobuf::Message& from) {
@@ -9110,12 +9446,14 @@ bool SourceCodeInfo::IsInitialized() const {
}
void SourceCodeInfo::Swap(SourceCodeInfo* other) {
- if (other != this) {
- location_.Swap(&other->location_);
- std::swap(_has_bits_[0], other->_has_bits_[0]);
- _unknown_fields_.Swap(&other->_unknown_fields_);
- std::swap(_cached_size_, other->_cached_size_);
- }
+ if (other == this) return;
+ InternalSwap(other);
+}
+void SourceCodeInfo::InternalSwap(SourceCodeInfo* other) {
+ location_.UnsafeArenaSwap(&other->location_);
+ std::swap(_has_bits_[0], other->_has_bits_[0]);
+ _internal_metadata_.Swap(&other->_internal_metadata_);
+ std::swap(_cached_size_, other->_cached_size_);
}
::google::protobuf::Metadata SourceCodeInfo::GetMetadata() const {
diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h
index d5221e7f..cda25982 100644
--- a/src/google/protobuf/descriptor.pb.h
+++ b/src/google/protobuf/descriptor.pb.h
@@ -8,18 +8,21 @@
#include <google/protobuf/stubs/common.h>
-#if GOOGLE_PROTOBUF_VERSION < 2006000
+#if GOOGLE_PROTOBUF_VERSION < 3000000
#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 2006000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
+#if 3000000 < GOOGLE_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 <google/protobuf/arena.h>
+#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_util.h>
+#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
@@ -167,11 +170,11 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -181,7 +184,9 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message
// implements Message ----------------------------------------------
- FileDescriptorSet* New() const;
+ inline FileDescriptorSet* New() const { return New(NULL); }
+
+ FileDescriptorSet* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const FileDescriptorSet& from);
@@ -200,7 +205,16 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(FileDescriptorSet* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -222,8 +236,7 @@ class LIBPROTOBUF_EXPORT FileDescriptorSet : public ::google::protobuf::Message
// @@protoc_insertion_point(class_scope:google.protobuf.FileDescriptorSet)
private:
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::FileDescriptorProto > file_;
@@ -249,11 +262,11 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -263,7 +276,9 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
// implements Message ----------------------------------------------
- FileDescriptorProto* New() const;
+ inline FileDescriptorProto* New() const { return New(NULL); }
+
+ FileDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const FileDescriptorProto& from);
@@ -282,7 +297,16 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(FileDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -419,6 +443,18 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
inline ::google::protobuf::SourceCodeInfo* release_source_code_info();
inline void set_allocated_source_code_info(::google::protobuf::SourceCodeInfo* source_code_info);
+ // optional string syntax = 12;
+ inline bool has_syntax() const;
+ inline void clear_syntax();
+ static const int kSyntaxFieldNumber = 12;
+ inline const ::std::string& syntax() const;
+ inline void set_syntax(const ::std::string& value);
+ inline void set_syntax(const char* value);
+ inline void set_syntax(const char* value, size_t size);
+ inline ::std::string* mutable_syntax();
+ inline ::std::string* release_syntax();
+ inline void set_allocated_syntax(::std::string* syntax);
+
// @@protoc_insertion_point(class_scope:google.protobuf.FileDescriptorProto)
private:
inline void set_has_name();
@@ -429,13 +465,14 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
inline void clear_has_options();
inline void set_has_source_code_info();
inline void clear_has_source_code_info();
+ inline void set_has_syntax();
+ inline void clear_has_syntax();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
- ::std::string* package_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
+ ::google::protobuf::internal::ArenaStringPtr package_;
::google::protobuf::RepeatedPtrField< ::std::string> dependency_;
::google::protobuf::RepeatedField< ::google::protobuf::int32 > public_dependency_;
::google::protobuf::RepeatedField< ::google::protobuf::int32 > weak_dependency_;
@@ -445,6 +482,7 @@ class LIBPROTOBUF_EXPORT FileDescriptorProto : public ::google::protobuf::Messag
::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > extension_;
::google::protobuf::FileOptions* options_;
::google::protobuf::SourceCodeInfo* source_code_info_;
+ ::google::protobuf::internal::ArenaStringPtr syntax_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -467,11 +505,11 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -481,7 +519,9 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto
// implements Message ----------------------------------------------
- DescriptorProto_ExtensionRange* New() const;
+ inline DescriptorProto_ExtensionRange* New() const { return New(NULL); }
+
+ DescriptorProto_ExtensionRange* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const DescriptorProto_ExtensionRange& from);
@@ -500,7 +540,16 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(DescriptorProto_ExtensionRange* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -528,8 +577,7 @@ class LIBPROTOBUF_EXPORT DescriptorProto_ExtensionRange : public ::google::proto
inline void set_has_end();
inline void clear_has_end();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::int32 start_;
@@ -556,11 +604,11 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -570,7 +618,9 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- DescriptorProto* New() const;
+ inline DescriptorProto* New() const { return New(NULL); }
+
+ DescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const DescriptorProto& from);
@@ -589,7 +639,16 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(DescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -698,11 +757,10 @@ class LIBPROTOBUF_EXPORT DescriptorProto : public ::google::protobuf::Message {
inline void set_has_options();
inline void clear_has_options();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > field_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::FieldDescriptorProto > extension_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::DescriptorProto > nested_type_;
@@ -732,11 +790,11 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -746,7 +804,9 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa
// implements Message ----------------------------------------------
- FieldDescriptorProto* New() const;
+ inline FieldDescriptorProto* New() const { return New(NULL); }
+
+ FieldDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const FieldDescriptorProto& from);
@@ -765,7 +825,16 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(FieldDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -943,18 +1012,17 @@ class LIBPROTOBUF_EXPORT FieldDescriptorProto : public ::google::protobuf::Messa
inline void set_has_options();
inline void clear_has_options();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
::google::protobuf::int32 number_;
int label_;
- ::std::string* type_name_;
- ::std::string* extendee_;
+ ::google::protobuf::internal::ArenaStringPtr type_name_;
+ ::google::protobuf::internal::ArenaStringPtr extendee_;
int type_;
::google::protobuf::int32 oneof_index_;
- ::std::string* default_value_;
+ ::google::protobuf::internal::ArenaStringPtr default_value_;
::google::protobuf::FieldOptions* options_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -978,11 +1046,11 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -992,7 +1060,9 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa
// implements Message ----------------------------------------------
- OneofDescriptorProto* New() const;
+ inline OneofDescriptorProto* New() const { return New(NULL); }
+
+ OneofDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const OneofDescriptorProto& from);
@@ -1011,7 +1081,16 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(OneofDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1035,11 +1114,10 @@ class LIBPROTOBUF_EXPORT OneofDescriptorProto : public ::google::protobuf::Messa
inline void set_has_name();
inline void clear_has_name();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1062,11 +1140,11 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1076,7 +1154,9 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag
// implements Message ----------------------------------------------
- EnumDescriptorProto* New() const;
+ inline EnumDescriptorProto* New() const { return New(NULL); }
+
+ EnumDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const EnumDescriptorProto& from);
@@ -1095,7 +1175,16 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(EnumDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1142,11 +1231,10 @@ class LIBPROTOBUF_EXPORT EnumDescriptorProto : public ::google::protobuf::Messag
inline void set_has_options();
inline void clear_has_options();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::EnumValueDescriptorProto > value_;
::google::protobuf::EnumOptions* options_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1171,11 +1259,11 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1185,7 +1273,9 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M
// implements Message ----------------------------------------------
- EnumValueDescriptorProto* New() const;
+ inline EnumValueDescriptorProto* New() const { return New(NULL); }
+
+ EnumValueDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const EnumValueDescriptorProto& from);
@@ -1204,7 +1294,16 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(EnumValueDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1248,11 +1347,10 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptorProto : public ::google::protobuf::M
inline void set_has_options();
inline void clear_has_options();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
::google::protobuf::EnumValueOptions* options_;
::google::protobuf::int32 number_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1277,11 +1375,11 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1291,7 +1389,9 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes
// implements Message ----------------------------------------------
- ServiceDescriptorProto* New() const;
+ inline ServiceDescriptorProto* New() const { return New(NULL); }
+
+ ServiceDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const ServiceDescriptorProto& from);
@@ -1310,7 +1410,16 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(ServiceDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1357,11 +1466,10 @@ class LIBPROTOBUF_EXPORT ServiceDescriptorProto : public ::google::protobuf::Mes
inline void set_has_options();
inline void clear_has_options();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::MethodDescriptorProto > method_;
::google::protobuf::ServiceOptions* options_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1386,11 +1494,11 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1400,7 +1508,9 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess
// implements Message ----------------------------------------------
- MethodDescriptorProto* New() const;
+ inline MethodDescriptorProto* New() const { return New(NULL); }
+
+ MethodDescriptorProto* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const MethodDescriptorProto& from);
@@ -1419,7 +1529,16 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(MethodDescriptorProto* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1471,6 +1590,20 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess
inline ::google::protobuf::MethodOptions* release_options();
inline void set_allocated_options(::google::protobuf::MethodOptions* options);
+ // optional bool client_streaming = 5 [default = false];
+ inline bool has_client_streaming() const;
+ inline void clear_client_streaming();
+ static const int kClientStreamingFieldNumber = 5;
+ inline bool client_streaming() const;
+ inline void set_client_streaming(bool value);
+
+ // optional bool server_streaming = 6 [default = false];
+ inline bool has_server_streaming() const;
+ inline void clear_server_streaming();
+ static const int kServerStreamingFieldNumber = 6;
+ inline bool server_streaming() const;
+ inline void set_server_streaming(bool value);
+
// @@protoc_insertion_point(class_scope:google.protobuf.MethodDescriptorProto)
private:
inline void set_has_name();
@@ -1481,15 +1614,20 @@ class LIBPROTOBUF_EXPORT MethodDescriptorProto : public ::google::protobuf::Mess
inline void clear_has_output_type();
inline void set_has_options();
inline void clear_has_options();
+ inline void set_has_client_streaming();
+ inline void clear_has_client_streaming();
+ inline void set_has_server_streaming();
+ inline void clear_has_server_streaming();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_;
- ::std::string* input_type_;
- ::std::string* output_type_;
+ ::google::protobuf::internal::ArenaStringPtr name_;
+ ::google::protobuf::internal::ArenaStringPtr input_type_;
+ ::google::protobuf::internal::ArenaStringPtr output_type_;
::google::protobuf::MethodOptions* options_;
+ bool client_streaming_;
+ bool server_streaming_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1512,11 +1650,11 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1526,7 +1664,9 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- FileOptions* New() const;
+ inline FileOptions* New() const { return New(NULL); }
+
+ FileOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const FileOptions& from);
@@ -1545,7 +1685,16 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(FileOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1669,6 +1818,13 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message {
inline bool deprecated() const;
inline void set_deprecated(bool value);
+ // optional bool cc_enable_arenas = 31 [default = false];
+ inline bool has_cc_enable_arenas() const;
+ inline void clear_cc_enable_arenas();
+ static const int kCcEnableArenasFieldNumber = 31;
+ inline bool cc_enable_arenas() const;
+ inline void set_cc_enable_arenas(bool value);
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
inline int uninterpreted_option_size() const;
inline void clear_uninterpreted_option();
@@ -1706,25 +1862,27 @@ class LIBPROTOBUF_EXPORT FileOptions : public ::google::protobuf::Message {
inline void clear_has_py_generic_services();
inline void set_has_deprecated();
inline void clear_has_deprecated();
+ inline void set_has_cc_enable_arenas();
+ inline void clear_has_cc_enable_arenas();
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* java_package_;
- ::std::string* java_outer_classname_;
+ ::google::protobuf::internal::ArenaStringPtr java_package_;
+ ::google::protobuf::internal::ArenaStringPtr java_outer_classname_;
bool java_multiple_files_;
bool java_generate_equals_and_hash_;
bool java_string_check_utf8_;
bool cc_generic_services_;
int optimize_for_;
- ::std::string* go_package_;
+ ::google::protobuf::internal::ArenaStringPtr go_package_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
bool java_generic_services_;
bool py_generic_services_;
bool deprecated_;
+ bool cc_enable_arenas_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1747,11 +1905,11 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1761,7 +1919,9 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- MessageOptions* New() const;
+ inline MessageOptions* New() const { return New(NULL); }
+
+ MessageOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const MessageOptions& from);
@@ -1780,7 +1940,16 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(MessageOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1808,6 +1977,13 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message {
inline bool deprecated() const;
inline void set_deprecated(bool value);
+ // optional bool map_entry = 7;
+ inline bool has_map_entry() const;
+ inline void clear_map_entry();
+ static const int kMapEntryFieldNumber = 7;
+ inline bool map_entry() const;
+ inline void set_map_entry(bool value);
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
inline int uninterpreted_option_size() const;
inline void clear_uninterpreted_option();
@@ -1829,17 +2005,19 @@ class LIBPROTOBUF_EXPORT MessageOptions : public ::google::protobuf::Message {
inline void clear_has_no_standard_descriptor_accessor();
inline void set_has_deprecated();
inline void clear_has_deprecated();
+ inline void set_has_map_entry();
+ inline void clear_has_map_entry();
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
bool message_set_wire_format_;
bool no_standard_descriptor_accessor_;
bool deprecated_;
+ bool map_entry_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -1862,11 +2040,11 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -1876,7 +2054,9 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- FieldOptions* New() const;
+ inline FieldOptions* New() const { return New(NULL); }
+
+ FieldOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const FieldOptions& from);
@@ -1895,7 +2075,16 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(FieldOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -1955,18 +2144,6 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
inline bool deprecated() const;
inline void set_deprecated(bool value);
- // optional string experimental_map_key = 9;
- inline bool has_experimental_map_key() const;
- inline void clear_experimental_map_key();
- static const int kExperimentalMapKeyFieldNumber = 9;
- inline const ::std::string& experimental_map_key() const;
- inline void set_experimental_map_key(const ::std::string& value);
- inline void set_experimental_map_key(const char* value);
- inline void set_experimental_map_key(const char* value, size_t size);
- inline ::std::string* mutable_experimental_map_key();
- inline ::std::string* release_experimental_map_key();
- inline void set_allocated_experimental_map_key(::std::string* experimental_map_key);
-
// optional bool weak = 10 [default = false];
inline bool has_weak() const;
inline void clear_weak();
@@ -1997,15 +2174,12 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
inline void clear_has_lazy();
inline void set_has_deprecated();
inline void clear_has_deprecated();
- inline void set_has_experimental_map_key();
- inline void clear_has_experimental_map_key();
inline void set_has_weak();
inline void clear_has_weak();
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
int ctype_;
@@ -2013,7 +2187,6 @@ class LIBPROTOBUF_EXPORT FieldOptions : public ::google::protobuf::Message {
bool lazy_;
bool deprecated_;
bool weak_;
- ::std::string* experimental_map_key_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -2037,11 +2210,11 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2051,7 +2224,9 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- EnumOptions* New() const;
+ inline EnumOptions* New() const { return New(NULL); }
+
+ EnumOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const EnumOptions& from);
@@ -2070,7 +2245,16 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(EnumOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2113,8 +2297,7 @@ class LIBPROTOBUF_EXPORT EnumOptions : public ::google::protobuf::Message {
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
@@ -2142,11 +2325,11 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2156,7 +2339,9 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- EnumValueOptions* New() const;
+ inline EnumValueOptions* New() const { return New(NULL); }
+
+ EnumValueOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const EnumValueOptions& from);
@@ -2175,7 +2360,16 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(EnumValueOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2209,8 +2403,7 @@ class LIBPROTOBUF_EXPORT EnumValueOptions : public ::google::protobuf::Message {
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
@@ -2237,11 +2430,11 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2251,7 +2444,9 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- ServiceOptions* New() const;
+ inline ServiceOptions* New() const { return New(NULL); }
+
+ ServiceOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const ServiceOptions& from);
@@ -2270,7 +2465,16 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(ServiceOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2304,8 +2508,7 @@ class LIBPROTOBUF_EXPORT ServiceOptions : public ::google::protobuf::Message {
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
@@ -2332,11 +2535,11 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2346,7 +2549,9 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- MethodOptions* New() const;
+ inline MethodOptions* New() const { return New(NULL); }
+
+ MethodOptions* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const MethodOptions& from);
@@ -2365,7 +2570,16 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(MethodOptions* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2399,8 +2613,7 @@ class LIBPROTOBUF_EXPORT MethodOptions : public ::google::protobuf::Message {
::google::protobuf::internal::ExtensionSet _extensions_;
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption > uninterpreted_option_;
@@ -2427,11 +2640,11 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2441,7 +2654,9 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu
// implements Message ----------------------------------------------
- UninterpretedOption_NamePart* New() const;
+ inline UninterpretedOption_NamePart* New() const { return New(NULL); }
+
+ UninterpretedOption_NamePart* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const UninterpretedOption_NamePart& from);
@@ -2460,7 +2675,16 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(UninterpretedOption_NamePart* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2493,11 +2717,13 @@ class LIBPROTOBUF_EXPORT UninterpretedOption_NamePart : public ::google::protobu
inline void set_has_is_extension();
inline void clear_has_is_extension();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
+ // helper for ByteSize()
+ int RequiredFieldsByteSizeFallback() const;
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
- ::std::string* name_part_;
+ ::google::protobuf::internal::ArenaStringPtr name_part_;
bool is_extension_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
@@ -2521,11 +2747,11 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2535,7 +2761,9 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag
// implements Message ----------------------------------------------
- UninterpretedOption* New() const;
+ inline UninterpretedOption* New() const { return New(NULL); }
+
+ UninterpretedOption* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const UninterpretedOption& from);
@@ -2554,7 +2782,16 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(UninterpretedOption* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2647,17 +2884,16 @@ class LIBPROTOBUF_EXPORT UninterpretedOption : public ::google::protobuf::Messag
inline void set_has_aggregate_value();
inline void clear_has_aggregate_value();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::UninterpretedOption_NamePart > name_;
- ::std::string* identifier_value_;
+ ::google::protobuf::internal::ArenaStringPtr identifier_value_;
::google::protobuf::uint64 positive_int_value_;
::google::protobuf::int64 negative_int_value_;
double double_value_;
- ::std::string* string_value_;
- ::std::string* aggregate_value_;
+ ::google::protobuf::internal::ArenaStringPtr string_value_;
+ ::google::protobuf::internal::ArenaStringPtr aggregate_value_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -2680,11 +2916,11 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2694,7 +2930,9 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me
// implements Message ----------------------------------------------
- SourceCodeInfo_Location* New() const;
+ inline SourceCodeInfo_Location* New() const { return New(NULL); }
+
+ SourceCodeInfo_Location* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const SourceCodeInfo_Location& from);
@@ -2713,7 +2951,16 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(SourceCodeInfo_Location* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2775,16 +3022,15 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo_Location : public ::google::protobuf::Me
inline void set_has_trailing_comments();
inline void clear_has_trailing_comments();
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedField< ::google::protobuf::int32 > path_;
mutable int _path_cached_byte_size_;
::google::protobuf::RepeatedField< ::google::protobuf::int32 > span_;
mutable int _span_cached_byte_size_;
- ::std::string* leading_comments_;
- ::std::string* trailing_comments_;
+ ::google::protobuf::internal::ArenaStringPtr leading_comments_;
+ ::google::protobuf::internal::ArenaStringPtr trailing_comments_;
friend void LIBPROTOBUF_EXPORT protobuf_AddDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_AssignDesc_google_2fprotobuf_2fdescriptor_2eproto();
friend void protobuf_ShutdownFile_google_2fprotobuf_2fdescriptor_2eproto();
@@ -2807,11 +3053,11 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message {
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
- return _unknown_fields_;
+ return _internal_metadata_.unknown_fields();
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
- return &_unknown_fields_;
+ return _internal_metadata_.mutable_unknown_fields();
}
static const ::google::protobuf::Descriptor* descriptor();
@@ -2821,7 +3067,9 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message {
// implements Message ----------------------------------------------
- SourceCodeInfo* New() const;
+ inline SourceCodeInfo* New() const { return New(NULL); }
+
+ SourceCodeInfo* New(::google::protobuf::Arena* arena) const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const SourceCodeInfo& from);
@@ -2840,7 +3088,16 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message {
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
+ void InternalSwap(SourceCodeInfo* other);
+ private:
+ inline ::google::protobuf::Arena* GetArenaNoVirtual() const {
+ return _internal_metadata_.arena();
+ }
+ inline void* MaybeArenaPtr() const {
+ return _internal_metadata_.raw_arena_ptr();
+ }
public:
+
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
@@ -2864,8 +3121,7 @@ class LIBPROTOBUF_EXPORT SourceCodeInfo : public ::google::protobuf::Message {
// @@protoc_insertion_point(class_scope:google.protobuf.SourceCodeInfo)
private:
- ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
+ ::google::protobuf::internal::InternalMetadataWithArena _internal_metadata_;
::google::protobuf::uint32 _has_bits_[1];
mutable int _cached_size_;
::google::protobuf::RepeatedPtrField< ::google::protobuf::SourceCodeInfo_Location > location_;
@@ -2928,68 +3184,45 @@ inline void FileDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void FileDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& FileDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.name)
}
inline void FileDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.name)
}
inline void FileDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.name)
}
inline ::std::string* FileDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FileDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.name)
}
@@ -3004,68 +3237,45 @@ inline void FileDescriptorProto::clear_has_package() {
_has_bits_[0] &= ~0x00000002u;
}
inline void FileDescriptorProto::clear_package() {
- if (package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_->clear();
- }
+ package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_package();
}
inline const ::std::string& FileDescriptorProto::package() const {
// @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.package)
- return *package_;
+ return package_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileDescriptorProto::set_package(const ::std::string& value) {
set_has_package();
- if (package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_ = new ::std::string;
- }
- package_->assign(value);
+ package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.package)
}
inline void FileDescriptorProto::set_package(const char* value) {
set_has_package();
- if (package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_ = new ::std::string;
- }
- package_->assign(value);
+ package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.package)
}
inline void FileDescriptorProto::set_package(const char* value, size_t size) {
set_has_package();
- if (package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_ = new ::std::string;
- }
- package_->assign(reinterpret_cast<const char*>(value), size);
+ package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.package)
}
inline ::std::string* FileDescriptorProto::mutable_package() {
set_has_package();
- if (package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- package_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.package)
- return package_;
+ return package_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FileDescriptorProto::release_package() {
clear_has_package();
- if (package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = package_;
- package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return package_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileDescriptorProto::set_allocated_package(::std::string* package) {
- if (package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete package_;
- }
- if (package) {
+ if (package != NULL) {
set_has_package();
- package_ = package;
} else {
clear_has_package();
- package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ package_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), package);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.package)
}
@@ -3323,7 +3533,9 @@ inline const ::google::protobuf::FileOptions& FileDescriptorProto::options() con
}
inline ::google::protobuf::FileOptions* FileDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::FileOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::FileOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.options)
return options_;
}
@@ -3364,7 +3576,9 @@ inline const ::google::protobuf::SourceCodeInfo& FileDescriptorProto::source_cod
}
inline ::google::protobuf::SourceCodeInfo* FileDescriptorProto::mutable_source_code_info() {
set_has_source_code_info();
- if (source_code_info_ == NULL) source_code_info_ = new ::google::protobuf::SourceCodeInfo;
+ if (source_code_info_ == NULL) {
+ source_code_info_ = new ::google::protobuf::SourceCodeInfo;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.source_code_info)
return source_code_info_;
}
@@ -3385,6 +3599,59 @@ inline void FileDescriptorProto::set_allocated_source_code_info(::google::protob
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.source_code_info)
}
+// optional string syntax = 12;
+inline bool FileDescriptorProto::has_syntax() const {
+ return (_has_bits_[0] & 0x00000800u) != 0;
+}
+inline void FileDescriptorProto::set_has_syntax() {
+ _has_bits_[0] |= 0x00000800u;
+}
+inline void FileDescriptorProto::clear_has_syntax() {
+ _has_bits_[0] &= ~0x00000800u;
+}
+inline void FileDescriptorProto::clear_syntax() {
+ syntax_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+ clear_has_syntax();
+}
+inline const ::std::string& FileDescriptorProto::syntax() const {
+ // @@protoc_insertion_point(field_get:google.protobuf.FileDescriptorProto.syntax)
+ return syntax_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+inline void FileDescriptorProto::set_syntax(const ::std::string& value) {
+ set_has_syntax();
+ syntax_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
+ // @@protoc_insertion_point(field_set:google.protobuf.FileDescriptorProto.syntax)
+}
+inline void FileDescriptorProto::set_syntax(const char* value) {
+ set_has_syntax();
+ syntax_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
+ // @@protoc_insertion_point(field_set_char:google.protobuf.FileDescriptorProto.syntax)
+}
+inline void FileDescriptorProto::set_syntax(const char* value, size_t size) {
+ set_has_syntax();
+ syntax_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
+ // @@protoc_insertion_point(field_set_pointer:google.protobuf.FileDescriptorProto.syntax)
+}
+inline ::std::string* FileDescriptorProto::mutable_syntax() {
+ set_has_syntax();
+ // @@protoc_insertion_point(field_mutable:google.protobuf.FileDescriptorProto.syntax)
+ return syntax_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+inline ::std::string* FileDescriptorProto::release_syntax() {
+ clear_has_syntax();
+ return syntax_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
+}
+inline void FileDescriptorProto::set_allocated_syntax(::std::string* syntax) {
+ if (syntax != NULL) {
+ set_has_syntax();
+ } else {
+ clear_has_syntax();
+ }
+ syntax_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), syntax);
+ // @@protoc_insertion_point(field_set_allocated:google.protobuf.FileDescriptorProto.syntax)
+}
+
// -------------------------------------------------------------------
// DescriptorProto_ExtensionRange
@@ -3452,68 +3719,45 @@ inline void DescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void DescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& DescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.DescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void DescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.DescriptorProto.name)
}
inline void DescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.DescriptorProto.name)
}
inline void DescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.DescriptorProto.name)
}
inline ::std::string* DescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* DescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void DescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.DescriptorProto.name)
}
@@ -3717,7 +3961,9 @@ inline const ::google::protobuf::MessageOptions& DescriptorProto::options() cons
}
inline ::google::protobuf::MessageOptions* DescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::MessageOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::MessageOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.DescriptorProto.options)
return options_;
}
@@ -3753,68 +3999,45 @@ inline void FieldDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void FieldDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& FieldDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.name)
}
inline void FieldDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.name)
}
inline void FieldDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.name)
}
inline ::std::string* FieldDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FieldDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.name)
}
@@ -3903,68 +4126,45 @@ inline void FieldDescriptorProto::clear_has_type_name() {
_has_bits_[0] &= ~0x00000010u;
}
inline void FieldDescriptorProto::clear_type_name() {
- if (type_name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_->clear();
- }
+ type_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_type_name();
}
inline const ::std::string& FieldDescriptorProto::type_name() const {
// @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.type_name)
- return *type_name_;
+ return type_name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_type_name(const ::std::string& value) {
set_has_type_name();
- if (type_name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_ = new ::std::string;
- }
- type_name_->assign(value);
+ type_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.type_name)
}
inline void FieldDescriptorProto::set_type_name(const char* value) {
set_has_type_name();
- if (type_name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_ = new ::std::string;
- }
- type_name_->assign(value);
+ type_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.type_name)
}
inline void FieldDescriptorProto::set_type_name(const char* value, size_t size) {
set_has_type_name();
- if (type_name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_ = new ::std::string;
- }
- type_name_->assign(reinterpret_cast<const char*>(value), size);
+ type_name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.type_name)
}
inline ::std::string* FieldDescriptorProto::mutable_type_name() {
set_has_type_name();
- if (type_name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- type_name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.type_name)
- return type_name_;
+ return type_name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FieldDescriptorProto::release_type_name() {
clear_has_type_name();
- if (type_name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = type_name_;
- type_name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return type_name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_allocated_type_name(::std::string* type_name) {
- if (type_name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete type_name_;
- }
- if (type_name) {
+ if (type_name != NULL) {
set_has_type_name();
- type_name_ = type_name;
} else {
clear_has_type_name();
- type_name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ type_name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), type_name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.type_name)
}
@@ -3979,68 +4179,45 @@ inline void FieldDescriptorProto::clear_has_extendee() {
_has_bits_[0] &= ~0x00000020u;
}
inline void FieldDescriptorProto::clear_extendee() {
- if (extendee_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_->clear();
- }
+ extendee_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_extendee();
}
inline const ::std::string& FieldDescriptorProto::extendee() const {
// @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.extendee)
- return *extendee_;
+ return extendee_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_extendee(const ::std::string& value) {
set_has_extendee();
- if (extendee_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_ = new ::std::string;
- }
- extendee_->assign(value);
+ extendee_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.extendee)
}
inline void FieldDescriptorProto::set_extendee(const char* value) {
set_has_extendee();
- if (extendee_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_ = new ::std::string;
- }
- extendee_->assign(value);
+ extendee_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.extendee)
}
inline void FieldDescriptorProto::set_extendee(const char* value, size_t size) {
set_has_extendee();
- if (extendee_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_ = new ::std::string;
- }
- extendee_->assign(reinterpret_cast<const char*>(value), size);
+ extendee_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.extendee)
}
inline ::std::string* FieldDescriptorProto::mutable_extendee() {
set_has_extendee();
- if (extendee_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- extendee_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.extendee)
- return extendee_;
+ return extendee_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FieldDescriptorProto::release_extendee() {
clear_has_extendee();
- if (extendee_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = extendee_;
- extendee_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return extendee_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_allocated_extendee(::std::string* extendee) {
- if (extendee_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete extendee_;
- }
- if (extendee) {
+ if (extendee != NULL) {
set_has_extendee();
- extendee_ = extendee;
} else {
clear_has_extendee();
- extendee_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ extendee_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), extendee);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.extendee)
}
@@ -4055,68 +4232,45 @@ inline void FieldDescriptorProto::clear_has_default_value() {
_has_bits_[0] &= ~0x00000040u;
}
inline void FieldDescriptorProto::clear_default_value() {
- if (default_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_->clear();
- }
+ default_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_default_value();
}
inline const ::std::string& FieldDescriptorProto::default_value() const {
// @@protoc_insertion_point(field_get:google.protobuf.FieldDescriptorProto.default_value)
- return *default_value_;
+ return default_value_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_default_value(const ::std::string& value) {
set_has_default_value();
- if (default_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_ = new ::std::string;
- }
- default_value_->assign(value);
+ default_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FieldDescriptorProto.default_value)
}
inline void FieldDescriptorProto::set_default_value(const char* value) {
set_has_default_value();
- if (default_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_ = new ::std::string;
- }
- default_value_->assign(value);
+ default_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FieldDescriptorProto.default_value)
}
inline void FieldDescriptorProto::set_default_value(const char* value, size_t size) {
set_has_default_value();
- if (default_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_ = new ::std::string;
- }
- default_value_->assign(reinterpret_cast<const char*>(value), size);
+ default_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldDescriptorProto.default_value)
}
inline ::std::string* FieldDescriptorProto::mutable_default_value() {
set_has_default_value();
- if (default_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- default_value_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.default_value)
- return default_value_;
+ return default_value_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FieldDescriptorProto::release_default_value() {
clear_has_default_value();
- if (default_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = default_value_;
- default_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return default_value_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FieldDescriptorProto::set_allocated_default_value(::std::string* default_value) {
- if (default_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete default_value_;
- }
- if (default_value) {
+ if (default_value != NULL) {
set_has_default_value();
- default_value_ = default_value;
} else {
clear_has_default_value();
- default_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ default_value_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), default_value);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldDescriptorProto.default_value)
}
@@ -4164,7 +4318,9 @@ inline const ::google::protobuf::FieldOptions& FieldDescriptorProto::options() c
}
inline ::google::protobuf::FieldOptions* FieldDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::FieldOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::FieldOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.FieldDescriptorProto.options)
return options_;
}
@@ -4200,68 +4356,45 @@ inline void OneofDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void OneofDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& OneofDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.OneofDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void OneofDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.OneofDescriptorProto.name)
}
inline void OneofDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.OneofDescriptorProto.name)
}
inline void OneofDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.OneofDescriptorProto.name)
}
inline ::std::string* OneofDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.OneofDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* OneofDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void OneofDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.OneofDescriptorProto.name)
}
@@ -4280,68 +4413,45 @@ inline void EnumDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void EnumDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& EnumDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.EnumDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void EnumDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.EnumDescriptorProto.name)
}
inline void EnumDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.EnumDescriptorProto.name)
}
inline void EnumDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.EnumDescriptorProto.name)
}
inline ::std::string* EnumDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* EnumDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void EnumDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.EnumDescriptorProto.name)
}
@@ -4395,7 +4505,9 @@ inline const ::google::protobuf::EnumOptions& EnumDescriptorProto::options() con
}
inline ::google::protobuf::EnumOptions* EnumDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::EnumOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::EnumOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.EnumDescriptorProto.options)
return options_;
}
@@ -4431,68 +4543,45 @@ inline void EnumValueDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void EnumValueDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& EnumValueDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.EnumValueDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void EnumValueDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.EnumValueDescriptorProto.name)
}
inline void EnumValueDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.EnumValueDescriptorProto.name)
}
inline void EnumValueDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.EnumValueDescriptorProto.name)
}
inline ::std::string* EnumValueDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.EnumValueDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* EnumValueDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void EnumValueDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.EnumValueDescriptorProto.name)
}
@@ -4540,7 +4629,9 @@ inline const ::google::protobuf::EnumValueOptions& EnumValueDescriptorProto::opt
}
inline ::google::protobuf::EnumValueOptions* EnumValueDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::EnumValueOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::EnumValueOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.EnumValueDescriptorProto.options)
return options_;
}
@@ -4576,68 +4667,45 @@ inline void ServiceDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void ServiceDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& ServiceDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.ServiceDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void ServiceDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.ServiceDescriptorProto.name)
}
inline void ServiceDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.ServiceDescriptorProto.name)
}
inline void ServiceDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.ServiceDescriptorProto.name)
}
inline ::std::string* ServiceDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.ServiceDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* ServiceDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void ServiceDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.ServiceDescriptorProto.name)
}
@@ -4691,7 +4759,9 @@ inline const ::google::protobuf::ServiceOptions& ServiceDescriptorProto::options
}
inline ::google::protobuf::ServiceOptions* ServiceDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::ServiceOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::ServiceOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.ServiceDescriptorProto.options)
return options_;
}
@@ -4727,68 +4797,45 @@ inline void MethodDescriptorProto::clear_has_name() {
_has_bits_[0] &= ~0x00000001u;
}
inline void MethodDescriptorProto::clear_name() {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_->clear();
- }
+ name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name();
}
inline const ::std::string& MethodDescriptorProto::name() const {
// @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.name)
- return *name_;
+ return name_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_name(const ::std::string& value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.name)
}
inline void MethodDescriptorProto::set_name(const char* value) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(value);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.name)
}
inline void MethodDescriptorProto::set_name(const char* value, size_t size) {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
- name_->assign(reinterpret_cast<const char*>(value), size);
+ name_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.name)
}
inline ::std::string* MethodDescriptorProto::mutable_name() {
set_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.name)
- return name_;
+ return name_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* MethodDescriptorProto::release_name() {
clear_has_name();
- if (name_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_;
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_allocated_name(::std::string* name) {
- if (name_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_;
- }
- if (name) {
+ if (name != NULL) {
set_has_name();
- name_ = name;
} else {
clear_has_name();
- name_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.name)
}
@@ -4803,68 +4850,45 @@ inline void MethodDescriptorProto::clear_has_input_type() {
_has_bits_[0] &= ~0x00000002u;
}
inline void MethodDescriptorProto::clear_input_type() {
- if (input_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_->clear();
- }
+ input_type_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_input_type();
}
inline const ::std::string& MethodDescriptorProto::input_type() const {
// @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.input_type)
- return *input_type_;
+ return input_type_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_input_type(const ::std::string& value) {
set_has_input_type();
- if (input_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_ = new ::std::string;
- }
- input_type_->assign(value);
+ input_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.input_type)
}
inline void MethodDescriptorProto::set_input_type(const char* value) {
set_has_input_type();
- if (input_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_ = new ::std::string;
- }
- input_type_->assign(value);
+ input_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.input_type)
}
inline void MethodDescriptorProto::set_input_type(const char* value, size_t size) {
set_has_input_type();
- if (input_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_ = new ::std::string;
- }
- input_type_->assign(reinterpret_cast<const char*>(value), size);
+ input_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.input_type)
}
inline ::std::string* MethodDescriptorProto::mutable_input_type() {
set_has_input_type();
- if (input_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- input_type_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.input_type)
- return input_type_;
+ return input_type_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* MethodDescriptorProto::release_input_type() {
clear_has_input_type();
- if (input_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = input_type_;
- input_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return input_type_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_allocated_input_type(::std::string* input_type) {
- if (input_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete input_type_;
- }
- if (input_type) {
+ if (input_type != NULL) {
set_has_input_type();
- input_type_ = input_type;
} else {
clear_has_input_type();
- input_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ input_type_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), input_type);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.input_type)
}
@@ -4879,68 +4903,45 @@ inline void MethodDescriptorProto::clear_has_output_type() {
_has_bits_[0] &= ~0x00000004u;
}
inline void MethodDescriptorProto::clear_output_type() {
- if (output_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_->clear();
- }
+ output_type_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_output_type();
}
inline const ::std::string& MethodDescriptorProto::output_type() const {
// @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.output_type)
- return *output_type_;
+ return output_type_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_output_type(const ::std::string& value) {
set_has_output_type();
- if (output_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_ = new ::std::string;
- }
- output_type_->assign(value);
+ output_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.output_type)
}
inline void MethodDescriptorProto::set_output_type(const char* value) {
set_has_output_type();
- if (output_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_ = new ::std::string;
- }
- output_type_->assign(value);
+ output_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.MethodDescriptorProto.output_type)
}
inline void MethodDescriptorProto::set_output_type(const char* value, size_t size) {
set_has_output_type();
- if (output_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_ = new ::std::string;
- }
- output_type_->assign(reinterpret_cast<const char*>(value), size);
+ output_type_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.MethodDescriptorProto.output_type)
}
inline ::std::string* MethodDescriptorProto::mutable_output_type() {
set_has_output_type();
- if (output_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- output_type_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.output_type)
- return output_type_;
+ return output_type_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* MethodDescriptorProto::release_output_type() {
clear_has_output_type();
- if (output_type_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = output_type_;
- output_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return output_type_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void MethodDescriptorProto::set_allocated_output_type(::std::string* output_type) {
- if (output_type_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete output_type_;
- }
- if (output_type) {
+ if (output_type != NULL) {
set_has_output_type();
- output_type_ = output_type;
} else {
clear_has_output_type();
- output_type_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ output_type_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), output_type);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.output_type)
}
@@ -4964,7 +4965,9 @@ inline const ::google::protobuf::MethodOptions& MethodDescriptorProto::options()
}
inline ::google::protobuf::MethodOptions* MethodDescriptorProto::mutable_options() {
set_has_options();
- if (options_ == NULL) options_ = new ::google::protobuf::MethodOptions;
+ if (options_ == NULL) {
+ options_ = new ::google::protobuf::MethodOptions;
+ }
// @@protoc_insertion_point(field_mutable:google.protobuf.MethodDescriptorProto.options)
return options_;
}
@@ -4985,6 +4988,54 @@ inline void MethodDescriptorProto::set_allocated_options(::google::protobuf::Met
// @@protoc_insertion_point(field_set_allocated:google.protobuf.MethodDescriptorProto.options)
}
+// optional bool client_streaming = 5 [default = false];
+inline bool MethodDescriptorProto::has_client_streaming() const {
+ return (_has_bits_[0] & 0x00000010u) != 0;
+}
+inline void MethodDescriptorProto::set_has_client_streaming() {
+ _has_bits_[0] |= 0x00000010u;
+}
+inline void MethodDescriptorProto::clear_has_client_streaming() {
+ _has_bits_[0] &= ~0x00000010u;
+}
+inline void MethodDescriptorProto::clear_client_streaming() {
+ client_streaming_ = false;
+ clear_has_client_streaming();
+}
+inline bool MethodDescriptorProto::client_streaming() const {
+ // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.client_streaming)
+ return client_streaming_;
+}
+inline void MethodDescriptorProto::set_client_streaming(bool value) {
+ set_has_client_streaming();
+ client_streaming_ = value;
+ // @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.client_streaming)
+}
+
+// optional bool server_streaming = 6 [default = false];
+inline bool MethodDescriptorProto::has_server_streaming() const {
+ return (_has_bits_[0] & 0x00000020u) != 0;
+}
+inline void MethodDescriptorProto::set_has_server_streaming() {
+ _has_bits_[0] |= 0x00000020u;
+}
+inline void MethodDescriptorProto::clear_has_server_streaming() {
+ _has_bits_[0] &= ~0x00000020u;
+}
+inline void MethodDescriptorProto::clear_server_streaming() {
+ server_streaming_ = false;
+ clear_has_server_streaming();
+}
+inline bool MethodDescriptorProto::server_streaming() const {
+ // @@protoc_insertion_point(field_get:google.protobuf.MethodDescriptorProto.server_streaming)
+ return server_streaming_;
+}
+inline void MethodDescriptorProto::set_server_streaming(bool value) {
+ set_has_server_streaming();
+ server_streaming_ = value;
+ // @@protoc_insertion_point(field_set:google.protobuf.MethodDescriptorProto.server_streaming)
+}
+
// -------------------------------------------------------------------
// FileOptions
@@ -5000,68 +5051,45 @@ inline void FileOptions::clear_has_java_package() {
_has_bits_[0] &= ~0x00000001u;
}
inline void FileOptions::clear_java_package() {
- if (java_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_->clear();
- }
+ java_package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_java_package();
}
inline const ::std::string& FileOptions::java_package() const {
// @@protoc_insertion_point(field_get:google.protobuf.FileOptions.java_package)
- return *java_package_;
+ return java_package_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_java_package(const ::std::string& value) {
set_has_java_package();
- if (java_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_ = new ::std::string;
- }
- java_package_->assign(value);
+ java_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FileOptions.java_package)
}
inline void FileOptions::set_java_package(const char* value) {
set_has_java_package();
- if (java_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_ = new ::std::string;
- }
- java_package_->assign(value);
+ java_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.java_package)
}
inline void FileOptions::set_java_package(const char* value, size_t size) {
set_has_java_package();
- if (java_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_ = new ::std::string;
- }
- java_package_->assign(reinterpret_cast<const char*>(value), size);
+ java_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.java_package)
}
inline ::std::string* FileOptions::mutable_java_package() {
set_has_java_package();
- if (java_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_package_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.java_package)
- return java_package_;
+ return java_package_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FileOptions::release_java_package() {
clear_has_java_package();
- if (java_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = java_package_;
- java_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return java_package_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_allocated_java_package(::std::string* java_package) {
- if (java_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete java_package_;
- }
- if (java_package) {
+ if (java_package != NULL) {
set_has_java_package();
- java_package_ = java_package;
} else {
clear_has_java_package();
- java_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ java_package_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), java_package);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.java_package)
}
@@ -5076,68 +5104,45 @@ inline void FileOptions::clear_has_java_outer_classname() {
_has_bits_[0] &= ~0x00000002u;
}
inline void FileOptions::clear_java_outer_classname() {
- if (java_outer_classname_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_->clear();
- }
+ java_outer_classname_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_java_outer_classname();
}
inline const ::std::string& FileOptions::java_outer_classname() const {
// @@protoc_insertion_point(field_get:google.protobuf.FileOptions.java_outer_classname)
- return *java_outer_classname_;
+ return java_outer_classname_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_java_outer_classname(const ::std::string& value) {
set_has_java_outer_classname();
- if (java_outer_classname_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_ = new ::std::string;
- }
- java_outer_classname_->assign(value);
+ java_outer_classname_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FileOptions.java_outer_classname)
}
inline void FileOptions::set_java_outer_classname(const char* value) {
set_has_java_outer_classname();
- if (java_outer_classname_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_ = new ::std::string;
- }
- java_outer_classname_->assign(value);
+ java_outer_classname_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@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) {
set_has_java_outer_classname();
- if (java_outer_classname_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_ = new ::std::string;
- }
- java_outer_classname_->assign(reinterpret_cast<const char*>(value), size);
+ java_outer_classname_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.java_outer_classname)
}
inline ::std::string* FileOptions::mutable_java_outer_classname() {
set_has_java_outer_classname();
- if (java_outer_classname_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- java_outer_classname_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.java_outer_classname)
- return java_outer_classname_;
+ return java_outer_classname_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FileOptions::release_java_outer_classname() {
clear_has_java_outer_classname();
- if (java_outer_classname_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = java_outer_classname_;
- java_outer_classname_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return java_outer_classname_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_allocated_java_outer_classname(::std::string* java_outer_classname) {
- if (java_outer_classname_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete java_outer_classname_;
- }
- if (java_outer_classname) {
+ if (java_outer_classname != NULL) {
set_has_java_outer_classname();
- java_outer_classname_ = java_outer_classname;
} else {
clear_has_java_outer_classname();
- java_outer_classname_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ java_outer_classname_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), java_outer_classname);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.java_outer_classname)
}
@@ -5249,68 +5254,45 @@ inline void FileOptions::clear_has_go_package() {
_has_bits_[0] &= ~0x00000040u;
}
inline void FileOptions::clear_go_package() {
- if (go_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_->clear();
- }
+ go_package_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_go_package();
}
inline const ::std::string& FileOptions::go_package() const {
// @@protoc_insertion_point(field_get:google.protobuf.FileOptions.go_package)
- return *go_package_;
+ return go_package_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_go_package(const ::std::string& value) {
set_has_go_package();
- if (go_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_ = new ::std::string;
- }
- go_package_->assign(value);
+ go_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.FileOptions.go_package)
}
inline void FileOptions::set_go_package(const char* value) {
set_has_go_package();
- if (go_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_ = new ::std::string;
- }
- go_package_->assign(value);
+ go_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.FileOptions.go_package)
}
inline void FileOptions::set_go_package(const char* value, size_t size) {
set_has_go_package();
- if (go_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_ = new ::std::string;
- }
- go_package_->assign(reinterpret_cast<const char*>(value), size);
+ go_package_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.FileOptions.go_package)
}
inline ::std::string* FileOptions::mutable_go_package() {
set_has_go_package();
- if (go_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- go_package_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.FileOptions.go_package)
- return go_package_;
+ return go_package_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* FileOptions::release_go_package() {
clear_has_go_package();
- if (go_package_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = go_package_;
- go_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return go_package_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void FileOptions::set_allocated_go_package(::std::string* go_package) {
- if (go_package_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete go_package_;
- }
- if (go_package) {
+ if (go_package != NULL) {
set_has_go_package();
- go_package_ = go_package;
} else {
clear_has_go_package();
- go_package_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ go_package_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), go_package);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.FileOptions.go_package)
}
@@ -5410,6 +5392,30 @@ inline void FileOptions::set_deprecated(bool value) {
// @@protoc_insertion_point(field_set:google.protobuf.FileOptions.deprecated)
}
+// optional bool cc_enable_arenas = 31 [default = false];
+inline bool FileOptions::has_cc_enable_arenas() const {
+ return (_has_bits_[0] & 0x00000800u) != 0;
+}
+inline void FileOptions::set_has_cc_enable_arenas() {
+ _has_bits_[0] |= 0x00000800u;
+}
+inline void FileOptions::clear_has_cc_enable_arenas() {
+ _has_bits_[0] &= ~0x00000800u;
+}
+inline void FileOptions::clear_cc_enable_arenas() {
+ cc_enable_arenas_ = false;
+ clear_has_cc_enable_arenas();
+}
+inline bool FileOptions::cc_enable_arenas() const {
+ // @@protoc_insertion_point(field_get:google.protobuf.FileOptions.cc_enable_arenas)
+ return cc_enable_arenas_;
+}
+inline void FileOptions::set_cc_enable_arenas(bool value) {
+ set_has_cc_enable_arenas();
+ cc_enable_arenas_ = value;
+ // @@protoc_insertion_point(field_set:google.protobuf.FileOptions.cc_enable_arenas)
+}
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
inline int FileOptions::uninterpreted_option_size() const {
return uninterpreted_option_.size();
@@ -5516,6 +5522,30 @@ inline void MessageOptions::set_deprecated(bool value) {
// @@protoc_insertion_point(field_set:google.protobuf.MessageOptions.deprecated)
}
+// optional bool map_entry = 7;
+inline bool MessageOptions::has_map_entry() const {
+ return (_has_bits_[0] & 0x00000008u) != 0;
+}
+inline void MessageOptions::set_has_map_entry() {
+ _has_bits_[0] |= 0x00000008u;
+}
+inline void MessageOptions::clear_has_map_entry() {
+ _has_bits_[0] &= ~0x00000008u;
+}
+inline void MessageOptions::clear_map_entry() {
+ map_entry_ = false;
+ clear_has_map_entry();
+}
+inline bool MessageOptions::map_entry() const {
+ // @@protoc_insertion_point(field_get:google.protobuf.MessageOptions.map_entry)
+ return map_entry_;
+}
+inline void MessageOptions::set_map_entry(bool value) {
+ set_has_map_entry();
+ map_entry_ = value;
+ // @@protoc_insertion_point(field_set:google.protobuf.MessageOptions.map_entry)
+}
+
// repeated .google.protobuf.UninterpretedOption uninterpreted_option = 999;
inline int MessageOptions::uninterpreted_option_size() const {
return uninterpreted_option_.size();
@@ -5647,91 +5677,15 @@ inline void FieldOptions::set_deprecated(bool value) {
// @@protoc_insertion_point(field_set:google.protobuf.FieldOptions.deprecated)
}
-// optional string experimental_map_key = 9;
-inline bool FieldOptions::has_experimental_map_key() const {
- return (_has_bits_[0] & 0x00000010u) != 0;
-}
-inline void FieldOptions::set_has_experimental_map_key() {
- _has_bits_[0] |= 0x00000010u;
-}
-inline void FieldOptions::clear_has_experimental_map_key() {
- _has_bits_[0] &= ~0x00000010u;
-}
-inline void FieldOptions::clear_experimental_map_key() {
- if (experimental_map_key_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_->clear();
- }
- clear_has_experimental_map_key();
-}
-inline const ::std::string& FieldOptions::experimental_map_key() const {
- // @@protoc_insertion_point(field_get:google.protobuf.FieldOptions.experimental_map_key)
- return *experimental_map_key_;
-}
-inline void FieldOptions::set_experimental_map_key(const ::std::string& value) {
- set_has_experimental_map_key();
- if (experimental_map_key_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_ = new ::std::string;
- }
- experimental_map_key_->assign(value);
- // @@protoc_insertion_point(field_set:google.protobuf.FieldOptions.experimental_map_key)
-}
-inline void FieldOptions::set_experimental_map_key(const char* value) {
- set_has_experimental_map_key();
- if (experimental_map_key_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_ = new ::std::string;
- }
- experimental_map_key_->assign(value);
- // @@protoc_insertion_point(field_set_char:google.protobuf.FieldOptions.experimental_map_key)
-}
-inline void FieldOptions::set_experimental_map_key(const char* value, size_t size) {
- set_has_experimental_map_key();
- if (experimental_map_key_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_ = new ::std::string;
- }
- experimental_map_key_->assign(reinterpret_cast<const char*>(value), size);
- // @@protoc_insertion_point(field_set_pointer:google.protobuf.FieldOptions.experimental_map_key)
-}
-inline ::std::string* FieldOptions::mutable_experimental_map_key() {
- set_has_experimental_map_key();
- if (experimental_map_key_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- experimental_map_key_ = new ::std::string;
- }
- // @@protoc_insertion_point(field_mutable:google.protobuf.FieldOptions.experimental_map_key)
- return experimental_map_key_;
-}
-inline ::std::string* FieldOptions::release_experimental_map_key() {
- clear_has_experimental_map_key();
- if (experimental_map_key_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = experimental_map_key_;
- experimental_map_key_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
-}
-inline void FieldOptions::set_allocated_experimental_map_key(::std::string* experimental_map_key) {
- if (experimental_map_key_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete experimental_map_key_;
- }
- if (experimental_map_key) {
- set_has_experimental_map_key();
- experimental_map_key_ = experimental_map_key;
- } else {
- clear_has_experimental_map_key();
- experimental_map_key_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- }
- // @@protoc_insertion_point(field_set_allocated:google.protobuf.FieldOptions.experimental_map_key)
-}
-
// optional bool weak = 10 [default = false];
inline bool FieldOptions::has_weak() const {
- return (_has_bits_[0] & 0x00000020u) != 0;
+ return (_has_bits_[0] & 0x00000010u) != 0;
}
inline void FieldOptions::set_has_weak() {
- _has_bits_[0] |= 0x00000020u;
+ _has_bits_[0] |= 0x00000010u;
}
inline void FieldOptions::clear_has_weak() {
- _has_bits_[0] &= ~0x00000020u;
+ _has_bits_[0] &= ~0x00000010u;
}
inline void FieldOptions::clear_weak() {
weak_ = false;
@@ -6048,68 +6002,45 @@ inline void UninterpretedOption_NamePart::clear_has_name_part() {
_has_bits_[0] &= ~0x00000001u;
}
inline void UninterpretedOption_NamePart::clear_name_part() {
- if (name_part_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_->clear();
- }
+ name_part_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_name_part();
}
inline const ::std::string& UninterpretedOption_NamePart::name_part() const {
// @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.NamePart.name_part)
- return *name_part_;
+ return name_part_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption_NamePart::set_name_part(const ::std::string& value) {
set_has_name_part();
- if (name_part_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_ = new ::std::string;
- }
- name_part_->assign(value);
+ name_part_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.NamePart.name_part)
}
inline void UninterpretedOption_NamePart::set_name_part(const char* value) {
set_has_name_part();
- if (name_part_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_ = new ::std::string;
- }
- name_part_->assign(value);
+ name_part_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@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) {
set_has_name_part();
- if (name_part_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_ = new ::std::string;
- }
- name_part_->assign(reinterpret_cast<const char*>(value), size);
+ name_part_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.NamePart.name_part)
}
inline ::std::string* UninterpretedOption_NamePart::mutable_name_part() {
set_has_name_part();
- if (name_part_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- name_part_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.NamePart.name_part)
- return name_part_;
+ return name_part_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* UninterpretedOption_NamePart::release_name_part() {
clear_has_name_part();
- if (name_part_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = name_part_;
- name_part_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return name_part_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption_NamePart::set_allocated_name_part(::std::string* name_part) {
- if (name_part_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete name_part_;
- }
- if (name_part) {
+ if (name_part != NULL) {
set_has_name_part();
- name_part_ = name_part;
} else {
clear_has_name_part();
- name_part_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ name_part_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), name_part);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.NamePart.name_part)
}
@@ -6182,68 +6113,45 @@ inline void UninterpretedOption::clear_has_identifier_value() {
_has_bits_[0] &= ~0x00000002u;
}
inline void UninterpretedOption::clear_identifier_value() {
- if (identifier_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_->clear();
- }
+ identifier_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_identifier_value();
}
inline const ::std::string& UninterpretedOption::identifier_value() const {
// @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.identifier_value)
- return *identifier_value_;
+ return identifier_value_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_identifier_value(const ::std::string& value) {
set_has_identifier_value();
- if (identifier_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_ = new ::std::string;
- }
- identifier_value_->assign(value);
+ identifier_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.identifier_value)
}
inline void UninterpretedOption::set_identifier_value(const char* value) {
set_has_identifier_value();
- if (identifier_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_ = new ::std::string;
- }
- identifier_value_->assign(value);
+ identifier_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.identifier_value)
}
inline void UninterpretedOption::set_identifier_value(const char* value, size_t size) {
set_has_identifier_value();
- if (identifier_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_ = new ::std::string;
- }
- identifier_value_->assign(reinterpret_cast<const char*>(value), size);
+ identifier_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.identifier_value)
}
inline ::std::string* UninterpretedOption::mutable_identifier_value() {
set_has_identifier_value();
- if (identifier_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- identifier_value_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.identifier_value)
- return identifier_value_;
+ return identifier_value_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* UninterpretedOption::release_identifier_value() {
clear_has_identifier_value();
- if (identifier_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = identifier_value_;
- identifier_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return identifier_value_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_allocated_identifier_value(::std::string* identifier_value) {
- if (identifier_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete identifier_value_;
- }
- if (identifier_value) {
+ if (identifier_value != NULL) {
set_has_identifier_value();
- identifier_value_ = identifier_value;
} else {
clear_has_identifier_value();
- identifier_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ identifier_value_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), identifier_value);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.identifier_value)
}
@@ -6330,68 +6238,45 @@ inline void UninterpretedOption::clear_has_string_value() {
_has_bits_[0] &= ~0x00000020u;
}
inline void UninterpretedOption::clear_string_value() {
- if (string_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_->clear();
- }
+ string_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_string_value();
}
inline const ::std::string& UninterpretedOption::string_value() const {
// @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.string_value)
- return *string_value_;
+ return string_value_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_string_value(const ::std::string& value) {
set_has_string_value();
- if (string_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_ = new ::std::string;
- }
- string_value_->assign(value);
+ string_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.string_value)
}
inline void UninterpretedOption::set_string_value(const char* value) {
set_has_string_value();
- if (string_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_ = new ::std::string;
- }
- string_value_->assign(value);
+ string_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.string_value)
}
inline void UninterpretedOption::set_string_value(const void* value, size_t size) {
set_has_string_value();
- if (string_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_ = new ::std::string;
- }
- string_value_->assign(reinterpret_cast<const char*>(value), size);
+ string_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.string_value)
}
inline ::std::string* UninterpretedOption::mutable_string_value() {
set_has_string_value();
- if (string_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- string_value_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.string_value)
- return string_value_;
+ return string_value_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* UninterpretedOption::release_string_value() {
clear_has_string_value();
- if (string_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = string_value_;
- string_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return string_value_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_allocated_string_value(::std::string* string_value) {
- if (string_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete string_value_;
- }
- if (string_value) {
+ if (string_value != NULL) {
set_has_string_value();
- string_value_ = string_value;
} else {
clear_has_string_value();
- string_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ string_value_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), string_value);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.string_value)
}
@@ -6406,68 +6291,45 @@ inline void UninterpretedOption::clear_has_aggregate_value() {
_has_bits_[0] &= ~0x00000040u;
}
inline void UninterpretedOption::clear_aggregate_value() {
- if (aggregate_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_->clear();
- }
+ aggregate_value_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_aggregate_value();
}
inline const ::std::string& UninterpretedOption::aggregate_value() const {
// @@protoc_insertion_point(field_get:google.protobuf.UninterpretedOption.aggregate_value)
- return *aggregate_value_;
+ return aggregate_value_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_aggregate_value(const ::std::string& value) {
set_has_aggregate_value();
- if (aggregate_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_ = new ::std::string;
- }
- aggregate_value_->assign(value);
+ aggregate_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.UninterpretedOption.aggregate_value)
}
inline void UninterpretedOption::set_aggregate_value(const char* value) {
set_has_aggregate_value();
- if (aggregate_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_ = new ::std::string;
- }
- aggregate_value_->assign(value);
+ aggregate_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@protoc_insertion_point(field_set_char:google.protobuf.UninterpretedOption.aggregate_value)
}
inline void UninterpretedOption::set_aggregate_value(const char* value, size_t size) {
set_has_aggregate_value();
- if (aggregate_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_ = new ::std::string;
- }
- aggregate_value_->assign(reinterpret_cast<const char*>(value), size);
+ aggregate_value_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.UninterpretedOption.aggregate_value)
}
inline ::std::string* UninterpretedOption::mutable_aggregate_value() {
set_has_aggregate_value();
- if (aggregate_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- aggregate_value_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.UninterpretedOption.aggregate_value)
- return aggregate_value_;
+ return aggregate_value_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* UninterpretedOption::release_aggregate_value() {
clear_has_aggregate_value();
- if (aggregate_value_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = aggregate_value_;
- aggregate_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return aggregate_value_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void UninterpretedOption::set_allocated_aggregate_value(::std::string* aggregate_value) {
- if (aggregate_value_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete aggregate_value_;
- }
- if (aggregate_value) {
+ if (aggregate_value != NULL) {
set_has_aggregate_value();
- aggregate_value_ = aggregate_value;
} else {
clear_has_aggregate_value();
- aggregate_value_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ aggregate_value_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), aggregate_value);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.UninterpretedOption.aggregate_value)
}
@@ -6546,68 +6408,45 @@ inline void SourceCodeInfo_Location::clear_has_leading_comments() {
_has_bits_[0] &= ~0x00000004u;
}
inline void SourceCodeInfo_Location::clear_leading_comments() {
- if (leading_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_->clear();
- }
+ leading_comments_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_leading_comments();
}
inline const ::std::string& SourceCodeInfo_Location::leading_comments() const {
// @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.leading_comments)
- return *leading_comments_;
+ return leading_comments_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void SourceCodeInfo_Location::set_leading_comments(const ::std::string& value) {
set_has_leading_comments();
- if (leading_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_ = new ::std::string;
- }
- leading_comments_->assign(value);
+ leading_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.leading_comments)
}
inline void SourceCodeInfo_Location::set_leading_comments(const char* value) {
set_has_leading_comments();
- if (leading_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_ = new ::std::string;
- }
- leading_comments_->assign(value);
+ leading_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@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) {
set_has_leading_comments();
- if (leading_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_ = new ::std::string;
- }
- leading_comments_->assign(reinterpret_cast<const char*>(value), size);
+ leading_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceCodeInfo.Location.leading_comments)
}
inline ::std::string* SourceCodeInfo_Location::mutable_leading_comments() {
set_has_leading_comments();
- if (leading_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- leading_comments_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.Location.leading_comments)
- return leading_comments_;
+ return leading_comments_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* SourceCodeInfo_Location::release_leading_comments() {
clear_has_leading_comments();
- if (leading_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = leading_comments_;
- leading_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return leading_comments_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void SourceCodeInfo_Location::set_allocated_leading_comments(::std::string* leading_comments) {
- if (leading_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete leading_comments_;
- }
- if (leading_comments) {
+ if (leading_comments != NULL) {
set_has_leading_comments();
- leading_comments_ = leading_comments;
} else {
clear_has_leading_comments();
- leading_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ leading_comments_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), leading_comments);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.SourceCodeInfo.Location.leading_comments)
}
@@ -6622,68 +6461,45 @@ inline void SourceCodeInfo_Location::clear_has_trailing_comments() {
_has_bits_[0] &= ~0x00000008u;
}
inline void SourceCodeInfo_Location::clear_trailing_comments() {
- if (trailing_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_->clear();
- }
+ trailing_comments_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
clear_has_trailing_comments();
}
inline const ::std::string& SourceCodeInfo_Location::trailing_comments() const {
// @@protoc_insertion_point(field_get:google.protobuf.SourceCodeInfo.Location.trailing_comments)
- return *trailing_comments_;
+ return trailing_comments_.GetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void SourceCodeInfo_Location::set_trailing_comments(const ::std::string& value) {
set_has_trailing_comments();
- if (trailing_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_ = new ::std::string;
- }
- trailing_comments_->assign(value);
+ trailing_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), value);
// @@protoc_insertion_point(field_set:google.protobuf.SourceCodeInfo.Location.trailing_comments)
}
inline void SourceCodeInfo_Location::set_trailing_comments(const char* value) {
set_has_trailing_comments();
- if (trailing_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_ = new ::std::string;
- }
- trailing_comments_->assign(value);
+ trailing_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), ::std::string(value));
// @@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) {
set_has_trailing_comments();
- if (trailing_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_ = new ::std::string;
- }
- trailing_comments_->assign(reinterpret_cast<const char*>(value), size);
+ trailing_comments_.SetNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(),
+ ::std::string(reinterpret_cast<const char*>(value), size));
// @@protoc_insertion_point(field_set_pointer:google.protobuf.SourceCodeInfo.Location.trailing_comments)
}
inline ::std::string* SourceCodeInfo_Location::mutable_trailing_comments() {
set_has_trailing_comments();
- if (trailing_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- trailing_comments_ = new ::std::string;
- }
// @@protoc_insertion_point(field_mutable:google.protobuf.SourceCodeInfo.Location.trailing_comments)
- return trailing_comments_;
+ return trailing_comments_.MutableNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline ::std::string* SourceCodeInfo_Location::release_trailing_comments() {
clear_has_trailing_comments();
- if (trailing_comments_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- return NULL;
- } else {
- ::std::string* temp = trailing_comments_;
- trailing_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
- return temp;
- }
+ return trailing_comments_.ReleaseNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
inline void SourceCodeInfo_Location::set_allocated_trailing_comments(::std::string* trailing_comments) {
- if (trailing_comments_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
- delete trailing_comments_;
- }
- if (trailing_comments) {
+ if (trailing_comments != NULL) {
set_has_trailing_comments();
- trailing_comments_ = trailing_comments;
} else {
clear_has_trailing_comments();
- trailing_comments_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}
+ trailing_comments_.SetAllocatedNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), trailing_comments);
// @@protoc_insertion_point(field_set_allocated:google.protobuf.SourceCodeInfo.Location.trailing_comments)
}
@@ -6752,8 +6568,8 @@ inline const EnumDescriptor* GetEnumDescriptor< ::google::protobuf::FieldOptions
return ::google::protobuf::FieldOptions_CType_descriptor();
}
-} // namespace google
} // namespace protobuf
+} // namespace google
#endif // SWIG
// @@protoc_insertion_point(global_scope)
diff --git a/src/google/protobuf/descriptor.proto b/src/google/protobuf/descriptor.proto
index a753601f..e17c0cc8 100644
--- a/src/google/protobuf/descriptor.proto
+++ b/src/google/protobuf/descriptor.proto
@@ -37,6 +37,7 @@
// without any other information (e.g. without reading its imports).
+syntax = "proto2";
package google.protobuf;
option java_package = "com.google.protobuf";
@@ -74,10 +75,14 @@ message FileDescriptorProto {
optional FileOptions options = 8;
// This field contains optional information about the original source code.
- // You may safely remove this entire field whithout harming runtime
+ // You may safely remove this entire field without harming runtime
// functionality of the descriptors -- the information is needed only by
// development tools.
optional SourceCodeInfo source_code_info = 9;
+
+ // The syntax of the proto file.
+ // The supported values are "proto2" and "proto3".
+ optional string syntax = 12;
}
// Describes a message type.
@@ -215,6 +220,11 @@ message MethodDescriptorProto {
optional string output_type = 3;
optional MethodOptions options = 4;
+
+ // Identifies if client streams multiple client messages
+ optional bool client_streaming = 5 [default=false];
+ // Identifies if server streams multiple server messages
+ optional bool server_streaming = 6 [default=false];
}
@@ -306,7 +316,10 @@ message FileOptions {
optional OptimizeMode optimize_for = 9 [default=SPEED];
// Sets the Go package where structs generated from this .proto will be
- // placed. There is no default.
+ // placed. If omitted, the Go package will be derived from the following:
+ // - The basename of the package import path, if provided.
+ // - Otherwise, the package statement in the .proto file, if present.
+ // - Otherwise, the basename of the .proto file, without extension.
optional string go_package = 11;
@@ -315,7 +328,7 @@ message FileOptions {
// are not specific to any particular RPC system. They are generated by the
// main code generators in each language (without additional plugins).
// Generic services were the only kind of service generation supported by
- // early versions of proto2.
+ // early versions of google.protobuf.
//
// Generic services are now considered deprecated in favor of using plugins
// that generate code specific to your particular RPC system. Therefore,
@@ -332,6 +345,11 @@ message FileOptions {
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];
+
+
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
@@ -371,6 +389,29 @@ message MessageOptions {
// this is a formalization for deprecating messages.
optional bool deprecated = 3 [default=false];
+ // Whether the message is an automatically generated map entry type for the
+ // maps field.
+ //
+ // For maps fields:
+ // map<KeyType, ValueType> map_field = 1;
+ // The parsed descriptor looks like:
+ // message MapFieldEntry {
+ // option map_entry = true;
+ // optional KeyType key = 1;
+ // optional ValueType value = 2;
+ // }
+ // 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
+ // 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
+ // parser.
+ optional bool map_entry = 7;
+
// The parser stores options it doesn't recognize here. See above.
repeated UninterpretedOption uninterpreted_option = 999;
@@ -436,20 +477,6 @@ message FieldOptions {
// is a formalization for deprecating fields.
optional bool deprecated = 3 [default=false];
- // EXPERIMENTAL. DO NOT USE.
- // For "map" fields, the name of the field in the enclosed type that
- // is the key for this map. For example, suppose we have:
- // message Item {
- // required string name = 1;
- // required string value = 2;
- // }
- // message Config {
- // repeated Item items = 1 [experimental_map_key="name"];
- // }
- // In this situation, the map key for Item will be set to "name".
- // TODO: Fully-implement this, then remove the "experimental_" prefix.
- optional string experimental_map_key = 9;
-
// For Google-internal migration only. Do not use.
optional bool weak = 10 [default=false];
diff --git a/src/google/protobuf/descriptor_pb2_test.py b/src/google/protobuf/descriptor_pb2_test.py
index 48a0d48a..c5a8d8a2 100644
--- a/src/google/protobuf/descriptor_pb2_test.py
+++ b/src/google/protobuf/descriptor_pb2_test.py
@@ -33,14 +33,14 @@
# Verify that prebuild and checkedin version of descriptor_pb2.py is up to date.
from google3.pyglib import resources
-import unittest
+from google.apputils import basetest
_DESC = 'google3/net/proto2/proto/descriptor_pb2.'
_OLD = _DESC + 'py-prebuilt'
_NEW = _DESC + 'compiled'
-class PregeneratedFileChanged(unittest.TestCase):
+class PregeneratedFileChanged(basetest.TestCase):
def testSameText(self):
generated = resources.GetResource(_NEW)
@@ -51,4 +51,4 @@ class PregeneratedFileChanged(unittest.TestCase):
'regnerate a new version of %s and add it to your CL' % _OLD)
if __name__ == '__main__':
- unittest.main()
+ basetest.main()
diff --git a/src/google/protobuf/descriptor_unittest.cc b/src/google/protobuf/descriptor_unittest.cc
index ac96160c..2ceb41e5 100644
--- a/src/google/protobuf/descriptor_unittest.cc
+++ b/src/google/protobuf/descriptor_unittest.cc
@@ -360,6 +360,34 @@ TEST_F(FileDescriptorTest, BuildAgain) {
EXPECT_TRUE(pool_.BuildFile(file) == NULL);
}
+TEST_F(FileDescriptorTest, Syntax) {
+ FileDescriptorProto proto;
+ proto.set_name("foo");
+ // Enable the test when we also populate the syntax for proto2.
+#if 0
+ {
+ proto.set_syntax("proto2");
+ DescriptorPool pool;
+ const FileDescriptor* file = pool.BuildFile(proto);
+ EXPECT_TRUE(file != NULL);
+ EXPECT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax());
+ FileDescriptorProto other;
+ file->CopyTo(&other);
+ EXPECT_EQ("proto2", other.syntax());
+ }
+#endif
+ {
+ proto.set_syntax("proto3");
+ DescriptorPool pool;
+ const FileDescriptor* file = pool.BuildFile(proto);
+ EXPECT_TRUE(file != NULL);
+ EXPECT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax());
+ FileDescriptorProto other;
+ file->CopyTo(&other);
+ EXPECT_EQ("proto3", other.syntax());
+ }
+}
+
// ===================================================================
// Test simple flat messages and fields.
@@ -387,6 +415,11 @@ class DescriptorTest : public testing::Test {
// required string quux = 6;
// }
//
+ // // in "map.proto"
+ // message TestMessage3 {
+ // map<int32, int32> map_int32_int32 = 1;
+ // }
+ //
// We cheat and use TestForeign as the type for qux rather than create
// an actual nested type.
//
@@ -434,6 +467,24 @@ class DescriptorTest : public testing::Test {
FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
+ FileDescriptorProto map_file;
+ map_file.set_name("map.proto");
+ DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3");
+
+ DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry");
+ AddField(entry, "key", 1,
+ FieldDescriptorProto::LABEL_OPTIONAL,
+ FieldDescriptorProto::TYPE_INT32);
+ AddField(entry, "value", 2,
+ FieldDescriptorProto::LABEL_OPTIONAL,
+ FieldDescriptorProto::TYPE_INT32);
+ entry->mutable_options()->set_map_entry(true);
+
+ AddField(message3, "map_int32_int32", 1,
+ FieldDescriptorProto::LABEL_REPEATED,
+ FieldDescriptorProto::TYPE_MESSAGE)
+ ->set_type_name("MapInt32Int32Entry");
+
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
@@ -441,6 +492,9 @@ class DescriptorTest : public testing::Test {
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
+ map_file_ = pool_.BuildFile(map_file);
+ ASSERT_TRUE(map_file_ != NULL);
+
ASSERT_EQ(1, foo_file_->enum_type_count());
enum_ = foo_file_->enum_type(0);
@@ -461,15 +515,23 @@ class DescriptorTest : public testing::Test {
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);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
+ const FileDescriptor* map_file_;
const Descriptor* message_;
const Descriptor* message2_;
+ const Descriptor* message3_;
const Descriptor* foreign_;
const EnumDescriptor* enum_;
@@ -481,6 +543,8 @@ class DescriptorTest : public testing::Test {
const FieldDescriptor* foo2_;
const FieldDescriptor* bar2_;
const FieldDescriptor* quux2_;
+
+ const FieldDescriptor* map_;
};
TEST_F(DescriptorTest, Name) {
@@ -610,6 +674,12 @@ TEST_F(DescriptorTest, FieldLabel) {
EXPECT_TRUE (baz_->is_repeated());
}
+TEST_F(DescriptorTest, IsMap) {
+ EXPECT_TRUE(map_->is_map());
+ EXPECT_FALSE(baz_->is_map());
+ EXPECT_TRUE(map_->message_type()->options().map_entry());
+}
+
TEST_F(DescriptorTest, FieldHasDefault) {
EXPECT_FALSE(foo_->has_default_value());
EXPECT_FALSE(bar_->has_default_value());
@@ -2157,6 +2227,12 @@ 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);
+
+ // Copy*To should not crash for placeholder files.
+ FileDescriptorProto baz_file_proto;
+ baz_file->CopyTo(&baz_file_proto);
+ baz_file->CopySourceCodeInfoTo(&baz_file_proto);
+ EXPECT_FALSE(baz_file_proto.has_source_code_info());
}
TEST_P(AllowUnknownDependenciesTest, PlaceholderTypes) {
@@ -4736,6 +4812,553 @@ TEST_F(ValidationErrorTest, UnusedImportWarning) {
"}");
}
+namespace {
+void FillValidMapEntry(FileDescriptorProto* file_proto) {
+ ASSERT_TRUE(TextFormat::ParseFromString(
+ "name: 'foo.proto' "
+ "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:TYPE_INT32 label:LABEL_OPTIONAL "
+ " } "
+ " } "
+ "} "
+ "message_type { "
+ " name: 'Bar' "
+ " extension_range { start: 1 end: 10 }"
+ "} ",
+ file_proto));
+}
+static const char* kMapEntryErrorMessage =
+ "foo.proto: Foo.foo_map: OTHER: map_entry should not be set explicitly. "
+ "Use map<KeyType, ValueType> instead.\n";
+static const char* kMapEntryKeyTypeErrorMessage =
+ "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot be float/double, "
+ "bytes or message types.\n";
+
+} // namespace
+
+TEST_F(ValidationErrorTest, MapEntryBase) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ BuildFile(file_proto.DebugString());
+}
+
+TEST_F(ValidationErrorTest, MapEntryExtensionRange) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "extension_range { "
+ " start: 10 end: 20 "
+ "} ",
+ file_proto.mutable_message_type(0)->mutable_nested_type(0));
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryExtension) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "extension { "
+ " name: 'foo_ext' extendee: '.Bar' number: 5"
+ "} ",
+ file_proto.mutable_message_type(0)->mutable_nested_type(0));
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryNestedType) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "nested_type { "
+ " name: 'Bar' "
+ "} ",
+ file_proto.mutable_message_type(0)->mutable_nested_type(0));
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryEnumTypes) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "enum_type { "
+ " name: 'BarEnum' "
+ " value { name: 'BAR_BAR' number:0 } "
+ "} ",
+ file_proto.mutable_message_type(0)->mutable_nested_type(0));
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryExtraField) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "field { "
+ " name: 'other_field' "
+ " label: LABEL_OPTIONAL "
+ " type: TYPE_INT32 "
+ " number: 3 "
+ "} ",
+ file_proto.mutable_message_type(0)->mutable_nested_type(0));
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryMessageName) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ file_proto.mutable_message_type(0)->mutable_nested_type(0)->set_name(
+ "OtherMapEntry");
+ file_proto.mutable_message_type(0)->mutable_field(0)->set_type_name(
+ "OtherMapEntry");
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryNoneRepeatedMapEntry) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ file_proto.mutable_message_type(0)->mutable_field(0)->set_label(
+ FieldDescriptorProto::LABEL_OPTIONAL);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ // Move the nested MapEntry message into the top level, which should not pass
+ // the validation.
+ file_proto.mutable_message_type()->AddAllocated(
+ file_proto.mutable_message_type(0)->mutable_nested_type()->ReleaseLast());
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ key->set_name("Key");
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ key->set_label(FieldDescriptorProto::LABEL_REQUIRED);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ key->set_number(3);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ value->set_name("Value");
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ value->set_label(FieldDescriptorProto::LABEL_REQUIRED);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ value->set_number(3);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
+}
+
+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);
+ key->set_type(FieldDescriptorProto::TYPE_FLOAT);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
+}
+
+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);
+ key->set_type(FieldDescriptorProto::TYPE_DOUBLE);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
+}
+
+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);
+ key->set_type(FieldDescriptorProto::TYPE_BYTES);
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
+}
+
+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);
+ key->clear_type();
+ key->set_type_name("BarEnum");
+ EnumDescriptorProto* enum_proto = file_proto.add_enum_type();
+ enum_proto->set_name("BarEnum");
+ EnumValueDescriptorProto* enum_value_proto = enum_proto->add_value();
+ enum_value_proto->set_name("BAR_VALUE0");
+ enum_value_proto->set_number(0);
+ BuildFileWithErrors(file_proto.DebugString(),
+ "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
+ "be enum types.\n");
+ // Enum keys are not allowed in proto3 as well.
+ // Get rid of extensions for proto3 to make it proto3 compatible.
+ file_proto.mutable_message_type()->RemoveLast();
+ file_proto.set_syntax("proto3");
+ BuildFileWithErrors(file_proto.DebugString(),
+ "foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
+ "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);
+ key->clear_type();
+ key->set_type_name(".Bar");
+ BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
+}
+
+TEST_F(ValidationErrorTest, MapEntryConflictsWithField) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "field { "
+ " name: 'FooMapEntry' "
+ " type: TYPE_INT32 "
+ " label: LABEL_OPTIONAL "
+ " number: 100 "
+ "}",
+ file_proto.mutable_message_type(0));
+ BuildFileWithErrors(
+ file_proto.DebugString(),
+ "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
+ "\"Foo\".\n"
+ "foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
+ "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
+ "with an existing field.\n");
+}
+
+TEST_F(ValidationErrorTest, MapEntryConflictsWithMessage) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "nested_type { "
+ " name: 'FooMapEntry' "
+ "}",
+ file_proto.mutable_message_type(0));
+ BuildFileWithErrors(
+ file_proto.DebugString(),
+ "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
+ "\"Foo\".\n"
+ "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
+ "with an existing nested message type.\n");
+}
+
+TEST_F(ValidationErrorTest, MapEntryConflictsWithEnum) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "enum_type { "
+ " name: 'FooMapEntry' "
+ " value { name: 'ENTRY_FOO' number: 0 }"
+ "}",
+ file_proto.mutable_message_type(0));
+ BuildFileWithErrors(
+ file_proto.DebugString(),
+ "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
+ "\"Foo\".\n"
+ "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
+ "with an existing enum type.\n");
+}
+
+TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) {
+ FileDescriptorProto file_proto;
+ FillValidMapEntry(&file_proto);
+ TextFormat::MergeFromString(
+ "oneof_decl { "
+ " name: 'FooMapEntry' "
+ "}"
+ "field { "
+ " name: 'int_field' "
+ " type: TYPE_INT32 "
+ " label: LABEL_OPTIONAL "
+ " oneof_index: 0 "
+ " number: 100 "
+ "} ",
+ file_proto.mutable_message_type(0));
+ BuildFileWithErrors(
+ file_proto.DebugString(),
+ "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
+ "\"Foo\".\n"
+ "foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
+ "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
+ "with an existing oneof type.\n");
+}
+
+TEST_F(ValidationErrorTest, Proto3RequiredFields) {
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " field { name:'foo' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } "
+ "}",
+ "foo.proto: Foo.foo: OTHER: Required fields are not allowed in "
+ "proto3.\n");
+
+ // applied to nested types as well.
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " nested_type { "
+ " name : 'Bar' "
+ " field { name:'bar' number:1 label:LABEL_REQUIRED type:TYPE_INT32 } "
+ " } "
+ "}",
+ "foo.proto: Foo.Bar.bar: OTHER: Required fields are not allowed in "
+ "proto3.\n");
+
+ // optional and repeated fields are OK.
+ BuildFile(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " field { name:'foo' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } "
+ " field { name:'bar' number:2 label:LABEL_REPEATED type:TYPE_INT32 } "
+ "}");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3DefaultValue) {
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " 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");
+
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " nested_type { "
+ " name : 'Bar' "
+ " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 "
+ " default_value: '1' }"
+ " } "
+ "}",
+ "foo.proto: Foo.Bar.bar: OTHER: Explicit default values are not allowed "
+ "in proto3.\n");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3ExtensionRange) {
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " 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 "
+ "proto3.\n");
+
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " nested_type { "
+ " name : 'Bar' "
+ " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 } "
+ " extension_range { start:10 end:100 } "
+ " } "
+ "}",
+ "foo.proto: Foo.Bar: OTHER: Extension ranges are not allowed in "
+ "proto3.\n");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3MessageSetWireFormat) {
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " options { message_set_wire_format: true } "
+ "}",
+ "foo.proto: Foo: OTHER: MessageSet is not supported "
+ "in proto3.\n");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3Enum) {
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "enum_type { "
+ " name: 'FooEnum' "
+ " value { name: 'FOO_FOO' number:1 } "
+ "}",
+ "foo.proto: FooEnum: OTHER: The first enum value must be "
+ "zero in proto3.\n");
+
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " enum_type { "
+ " name: 'FooEnum' "
+ " value { name: 'FOO_FOO' number:1 } "
+ " } "
+ "}",
+ "foo.proto: Foo.FooEnum: OTHER: The first enum value must be "
+ "zero in proto3.\n");
+
+ // valid case.
+ BuildFile(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "enum_type { "
+ " name: 'FooEnum' "
+ " value { name: 'FOO_FOO' number:0 } "
+ "}");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3LiteRuntime) {
+ // Lite runtime is not supported in proto3.
+ BuildFileWithErrors(
+ "name: 'foo.proto' "
+ "syntax: 'proto3' "
+ "options { "
+ " optimize_for: LITE_RUNTIME "
+ "} ",
+ "foo.proto: foo.proto: OTHER: Lite runtime is not supported "
+ "in proto3.\n");
+}
+
+
+TEST_F(ValidationErrorTest, ValidateProto3EnumFromProto2) {
+ // Define an enum in a proto2 file.
+ BuildFile(
+ "name: 'foo.proto' "
+ "package: 'foo' "
+ "syntax: 'proto2' "
+ "enum_type { "
+ " name: 'FooEnum' "
+ " value { name: 'DEFAULT_OPTION' number:0 } "
+ "}");
+
+ // Now try to refer to it. (All tests in the fixture use the same pool, so we
+ // can refer to the enum above in this definition.)
+ BuildFileWithErrors(
+ "name: 'bar.proto' "
+ "dependency: 'foo.proto' "
+ "syntax: 'proto3' "
+ "message_type { "
+ " name: 'Foo' "
+ " field { name:'bar' number:1 label:LABEL_OPTIONAL type:TYPE_ENUM "
+ " type_name: 'foo.FooEnum' }"
+ "}",
+ "bar.proto: Foo.bar: TYPE: Enum type \"foo.FooEnum\" is not a proto3 "
+ "enum, but is used in \"Foo\" which is a proto3 message type.\n");
+}
+
+TEST_F(ValidationErrorTest, ValidateProto3Extension) {
+ // Valid for options.
+ DescriptorPool pool;
+ FileDescriptorProto file_proto;
+ // Add "google/protobuf/descriptor.proto".
+ FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
+ ASSERT_TRUE(pool.BuildFile(file_proto) != NULL);
+ // Add "foo.proto":
+ // import "google/protobuf/descriptor.proto";
+ // extend google.protobuf.FieldOptions {
+ // optional int32 option1 = 1000;
+ // }
+ file_proto.Clear();
+ file_proto.set_name("foo.proto");
+ file_proto.set_syntax("proto3");
+ file_proto.add_dependency("google/protobuf/descriptor.proto");
+ AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000,
+ FieldDescriptorProto::LABEL_OPTIONAL,
+ FieldDescriptorProto::TYPE_INT32);
+ ASSERT_TRUE(pool.BuildFile(file_proto) != NULL);
+
+ // Copy and change the package of the descriptor.proto
+ BuildFile(
+ "name: 'google.protobuf.proto' "
+ "syntax: 'proto2' "
+ "message_type { "
+ " name: 'Container' extension_range { start: 1 end: 1000 } "
+ "}");
+ BuildFileWithErrors(
+ "name: 'bar.proto' "
+ "syntax: 'proto3' "
+ "dependency: 'google.protobuf.proto' "
+ "extension { "
+ " name: 'bar' number: 1 label: LABEL_OPTIONAL type: TYPE_INT32 "
+ " extendee: 'Container' "
+ "}",
+ "bar.proto: bar: OTHER: Extensions in proto3 are only allowed for "
+ "defining options.\n");
+}
// ===================================================================
// DescriptorDatabase
@@ -5260,8 +5883,8 @@ class AbortingErrorCollector : public DescriptorPool::ErrorCollector {
const Message *message,
ErrorLocation location,
const string &error_message) {
- GOOGLE_LOG(FATAL) << "AddError() called unexpectedly: " << filename << ": "
- << error_message;
+ GOOGLE_LOG(FATAL) << "AddError() called unexpectedly: " << filename << " ["
+ << element_name << "]: " << error_message;
}
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AbortingErrorCollector);
diff --git a/src/google/protobuf/drop_unknown_fields_test.cc b/src/google/protobuf/drop_unknown_fields_test.cc
new file mode 100644
index 00000000..6f16dc53
--- /dev/null
+++ b/src/google/protobuf/drop_unknown_fields_test.cc
@@ -0,0 +1,88 @@
+// 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 <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+
+#include <google/protobuf/unittest_drop_unknown_fields.pb.h>
+#include <google/protobuf/dynamic_message.h>
+#include <gtest/gtest.h>
+
+namespace google {
+using unittest_drop_unknown_fields::Foo;
+using unittest_drop_unknown_fields::FooWithExtraFields;
+
+namespace protobuf {
+
+TEST(DropUnknownFieldsTest, GeneratedMessage) {
+ FooWithExtraFields foo_with_extra_fields;
+ foo_with_extra_fields.set_int32_value(1);
+ foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
+ foo_with_extra_fields.set_extra_int32_value(2);
+
+ Foo foo;
+ ASSERT_TRUE(foo.ParseFromString(foo_with_extra_fields.SerializeAsString()));
+ EXPECT_EQ(1, foo.int32_value());
+ EXPECT_EQ(static_cast<int>(FooWithExtraFields::QUX),
+ static_cast<int>(foo.enum_value()));
+ // We don't generate unknown field accessors but the UnknownFieldSet is
+ // still exposed through reflection API.
+ EXPECT_TRUE(foo.GetReflection()->GetUnknownFields(foo).empty());
+
+ ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo.SerializeAsString()));
+ EXPECT_EQ(1, foo_with_extra_fields.int32_value());
+ EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
+ // The "extra_int32_value" field should be lost.
+ EXPECT_EQ(0, foo_with_extra_fields.extra_int32_value());
+}
+
+TEST(DropUnknownFieldsTest, DynamicMessage) {
+ FooWithExtraFields foo_with_extra_fields;
+ foo_with_extra_fields.set_int32_value(1);
+ foo_with_extra_fields.set_enum_value(FooWithExtraFields::QUX);
+ foo_with_extra_fields.set_extra_int32_value(2);
+
+ google::protobuf::DynamicMessageFactory factory;
+ google::protobuf::scoped_ptr<google::protobuf::Message> foo(
+ factory.GetPrototype(Foo::descriptor())->New());
+ ASSERT_TRUE(foo->ParseFromString(foo_with_extra_fields.SerializeAsString()));
+ EXPECT_TRUE(foo->GetReflection()->GetUnknownFields(*foo).empty());
+
+ ASSERT_TRUE(foo_with_extra_fields.ParseFromString(foo->SerializeAsString()));
+ EXPECT_EQ(1, foo_with_extra_fields.int32_value());
+ EXPECT_EQ(FooWithExtraFields::QUX, foo_with_extra_fields.enum_value());
+ // The "extra_int32_value" field should be lost.
+ EXPECT_EQ(0, foo_with_extra_fields.extra_int32_value());
+}
+
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/dynamic_message.cc b/src/google/protobuf/dynamic_message.cc
index 4cca9869..318ce6f9 100644
--- a/src/google/protobuf/dynamic_message.cc
+++ b/src/google/protobuf/dynamic_message.cc
@@ -72,8 +72,11 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/generated_message_reflection.h>
+#include <google/protobuf/arenastring.h>
+#include <google/protobuf/map_field_inl.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/repeated_field.h>
+#include <google/protobuf/map_type_handler.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/wire_format.h>
@@ -83,13 +86,21 @@ namespace protobuf {
using internal::WireFormat;
using internal::ExtensionSet;
using internal::GeneratedMessageReflection;
+using internal::MapField;
+using internal::MapFieldBase;
+using internal::ArenaStringPtr;
+
// ===================================================================
// Some helper tables and functions...
namespace {
+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
@@ -103,7 +114,12 @@ int FieldSpaceUsed(const FieldDescriptor* field) {
case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField<float >);
case FD::CPPTYPE_BOOL : return sizeof(RepeatedField<bool >);
case FD::CPPTYPE_ENUM : return sizeof(RepeatedField<int >);
- case FD::CPPTYPE_MESSAGE: return sizeof(RepeatedPtrField<Message>);
+ case FD::CPPTYPE_MESSAGE:
+ if (IsMapFieldInApi(field)) {
+ return sizeof(MapFieldBase);
+ } else {
+ return sizeof(RepeatedPtrField<Message>);
+ }
case FD::CPPTYPE_STRING:
switch (field->options().ctype()) {
@@ -131,7 +147,7 @@ int FieldSpaceUsed(const FieldDescriptor* field) {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING:
- return sizeof(string*);
+ return sizeof(ArenaStringPtr);
}
break;
}
@@ -162,7 +178,7 @@ int OneofFieldSpaceUsed(const FieldDescriptor* field) {
switch (field->options().ctype()) {
default:
case FieldOptions::STRING:
- return sizeof(string*);
+ return sizeof(ArenaStringPtr);
}
break;
}
@@ -202,6 +218,7 @@ class DynamicMessage : public Message {
int oneof_case_offset;
int unknown_fields_offset;
int extensions_offset;
+ int is_default_instance_offset;
// Not owned by the TypeInfo.
DynamicMessageFactory* factory; // The factory that created this object.
@@ -236,6 +253,8 @@ class DynamicMessage : public Message {
// implements Message ----------------------------------------------
Message* New() const;
+ Message* New(::google::protobuf::Arena* arena) const;
+ ::google::protobuf::Arena* GetArena() const { return NULL; };
int GetCachedSize() const;
void SetCachedSize(int size) const;
@@ -245,6 +264,8 @@ class DynamicMessage : public Message {
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessage);
+ DynamicMessage(const TypeInfo* type_info, ::google::protobuf::Arena* arena);
+ void SharedCtor();
inline bool is_prototype() const {
return type_info_->prototype == this ||
@@ -261,7 +282,6 @@ class DynamicMessage : public Message {
}
const TypeInfo* type_info_;
-
// TODO(kenton): Make this an atomic<int> when C++ supports it.
mutable int cached_byte_size_;
};
@@ -269,6 +289,17 @@ class DynamicMessage : public Message {
DynamicMessage::DynamicMessage(const TypeInfo* type_info)
: type_info_(type_info),
cached_byte_size_(0) {
+ SharedCtor();
+}
+
+DynamicMessage::DynamicMessage(const TypeInfo* type_info,
+ ::google::protobuf::Arena* arena)
+ : type_info_(type_info),
+ cached_byte_size_(0) {
+ SharedCtor();
+}
+
+void DynamicMessage::SharedCtor() {
// We need to call constructors for various fields manually and set
// default values where appropriate. We use placement new to call
// constructors. If you haven't heard of placement new, I suggest Googling
@@ -286,6 +317,11 @@ DynamicMessage::DynamicMessage(const TypeInfo* type_info)
uint32(0);
}
+ if (type_info_->is_default_instance_offset != -1) {
+ *reinterpret_cast<bool*>(
+ OffsetToPointer(type_info_->is_default_instance_offset)) = false;
+ }
+
new(OffsetToPointer(type_info_->unknown_fields_offset)) UnknownFieldSet;
if (type_info_->extensions_offset != -1) {
@@ -330,15 +366,17 @@ DynamicMessage::DynamicMessage(const TypeInfo* type_info)
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING:
if (!field->is_repeated()) {
+ const string* default_value;
if (is_prototype()) {
- new(field_ptr) const string*(&field->default_value_string());
+ default_value = &field->default_value_string();
} else {
- string* default_value =
- *reinterpret_cast<string* const*>(
+ default_value =
+ &(reinterpret_cast<const ArenaStringPtr*>(
type_info_->prototype->OffsetToPointer(
- type_info_->offsets[i]));
- new(field_ptr) string*(default_value);
+ type_info_->offsets[i]))->Get(NULL));
}
+ ArenaStringPtr* asp = new(field_ptr) ArenaStringPtr();
+ asp->UnsafeSetDefault(default_value);
} else {
new(field_ptr) RepeatedPtrField<string>();
}
@@ -350,7 +388,11 @@ DynamicMessage::DynamicMessage(const TypeInfo* type_info)
if (!field->is_repeated()) {
new(field_ptr) Message*(NULL);
} else {
- new(field_ptr) RepeatedPtrField<Message>();
+ if (IsMapFieldInApi(field)) {
+ new (field_ptr) MapFieldBase();
+ } else {
+ new (field_ptr) RepeatedPtrField<Message>();
+ }
}
break;
}
@@ -390,9 +432,15 @@ DynamicMessage::~DynamicMessage() {
if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
switch (field->options().ctype()) {
default:
- case FieldOptions::STRING:
- delete *reinterpret_cast<string**>(field_ptr);
+ case FieldOptions::STRING: {
+ const ::std::string* default_value =
+ &(reinterpret_cast<const ArenaStringPtr*>(
+ type_info_->prototype->OffsetToPointer(
+ type_info_->offsets[i]))->Get(NULL));
+ reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy(default_value,
+ NULL);
break;
+ }
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
delete *reinterpret_cast<Message**>(field_ptr);
@@ -431,8 +479,12 @@ DynamicMessage::~DynamicMessage() {
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
- reinterpret_cast<RepeatedPtrField<Message>*>(field_ptr)
- ->~RepeatedPtrField<Message>();
+ if (IsMapFieldInApi(field)) {
+ reinterpret_cast<MapFieldBase*>(field_ptr)->~MapFieldBase();
+ } else {
+ reinterpret_cast<RepeatedPtrField<Message>*>(field_ptr)
+ ->~RepeatedPtrField<Message>();
+ }
break;
}
@@ -440,10 +492,12 @@ DynamicMessage::~DynamicMessage() {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING: {
- string* ptr = *reinterpret_cast<string**>(field_ptr);
- if (ptr != &field->default_value_string()) {
- delete ptr;
- }
+ const ::std::string* default_value =
+ &(reinterpret_cast<const ArenaStringPtr*>(
+ type_info_->prototype->OffsetToPointer(
+ type_info_->offsets[i]))->Get(NULL));
+ reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy(default_value,
+ NULL);
break;
}
}
@@ -484,6 +538,14 @@ void DynamicMessage::CrossLinkPrototypes() {
factory->GetPrototypeNoLock(field->message_type());
}
}
+
+ // Set as the default instance -- this affects field-presence semantics for
+ // proto3.
+ if (type_info_->is_default_instance_offset != -1) {
+ void* is_default_instance_ptr =
+ OffsetToPointer(type_info_->is_default_instance_offset);
+ *reinterpret_cast<bool*>(is_default_instance_ptr) = true;
+ }
}
Message* DynamicMessage::New() const {
@@ -492,6 +554,16 @@ Message* DynamicMessage::New() const {
return new(new_base) DynamicMessage(type_info_);
}
+Message* DynamicMessage::New(::google::protobuf::Arena* arena) const {
+ if (arena != NULL) {
+ Message* message = New();
+ arena->Own(message);
+ return message;
+ } else {
+ return New();
+ }
+}
+
int DynamicMessage::GetCachedSize() const {
return cached_byte_size_;
}
@@ -583,11 +655,24 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
size = AlignOffset(size);
// Next the has_bits, which is an array of uint32s.
- type_info->has_bits_offset = size;
- int has_bits_array_size =
- DivideRoundingUp(type->field_count(), bitsizeof(uint32));
- size += has_bits_array_size * sizeof(uint32);
- size = AlignOffset(size);
+ if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
+ type_info->has_bits_offset = -1;
+ } else {
+ type_info->has_bits_offset = size;
+ int has_bits_array_size =
+ DivideRoundingUp(type->field_count(), bitsizeof(uint32));
+ size += has_bits_array_size * sizeof(uint32);
+ size = AlignOffset(size);
+ }
+
+ // The is_default_instance member, if any.
+ if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
+ type_info->is_default_instance_offset = size;
+ size += sizeof(bool);
+ size = AlignOffset(size);
+ } else {
+ type_info->is_default_instance_offset = -1;
+ }
// The oneof_case, if any. It is an array of uint32s.
if (type->oneof_decl_count() > 0) {
@@ -672,7 +757,9 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
type_info->oneof_case_offset,
type_info->pool,
this,
- type_info->size));
+ type_info->size,
+ -1 /* arena_offset */,
+ type_info->is_default_instance_offset));
} else {
type_info->reflection.reset(
new GeneratedMessageReflection(
@@ -684,7 +771,9 @@ const Message* DynamicMessageFactory::GetPrototypeNoLock(
type_info->extensions_offset,
type_info->pool,
this,
- type_info->size));
+ type_info->size,
+ -1 /* arena_offset */,
+ type_info->is_default_instance_offset));
}
// Cross link prototypes.
prototype->CrossLinkPrototypes();
@@ -723,12 +812,8 @@ void DynamicMessageFactory::ConstructDefaultOneofInstance(
switch (field->options().ctype()) {
default:
case FieldOptions::STRING:
- if (field->has_default_value()) {
- new(field_ptr) const string*(&field->default_value_string());
- } else {
- new(field_ptr) string*(
- const_cast<string*>(&internal::GetEmptyString()));
- }
+ ArenaStringPtr* asp = new (field_ptr) ArenaStringPtr();
+ asp->UnsafeSetDefault(&field->default_value_string());
break;
}
break;
diff --git a/src/google/protobuf/dynamic_message.h b/src/google/protobuf/dynamic_message.h
index 10ed7005..b5928a52 100644
--- a/src/google/protobuf/dynamic_message.h
+++ b/src/google/protobuf/dynamic_message.h
@@ -39,6 +39,9 @@
#define GOOGLE_PROTOBUF_DYNAMIC_MESSAGE_H__
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <google/protobuf/message.h>
#include <google/protobuf/stubs/common.h>
@@ -123,7 +126,7 @@ class LIBPROTOBUF_EXPORT DynamicMessageFactory : public MessageFactory {
// public header (for good reason), but dynamic_message.h is, and public
// headers may only #include other public headers.
struct PrototypeMap;
- scoped_ptr<PrototypeMap> prototypes_;
+ google::protobuf::scoped_ptr<PrototypeMap> prototypes_;
mutable Mutex prototypes_mutex_;
friend class DynamicMessage;
diff --git a/src/google/protobuf/dynamic_message_unittest.cc b/src/google/protobuf/dynamic_message_unittest.cc
index a1c1661b..522a092a 100644
--- a/src/google/protobuf/dynamic_message_unittest.cc
+++ b/src/google/protobuf/dynamic_message_unittest.cc
@@ -46,6 +46,7 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/test_util.h>
#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/unittest_no_field_presence.pb.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
@@ -65,6 +66,8 @@ class DynamicMessageTest : public testing::Test {
const Message* packed_prototype_;
const Descriptor* oneof_descriptor_;
const Message* oneof_prototype_;
+ const Descriptor* proto3_descriptor_;
+ const Message* proto3_prototype_;
DynamicMessageTest(): factory_(&pool_) {}
@@ -76,16 +79,20 @@ class DynamicMessageTest : public testing::Test {
FileDescriptorProto unittest_file;
FileDescriptorProto unittest_import_file;
FileDescriptorProto unittest_import_public_file;
+ FileDescriptorProto unittest_no_field_presence_file;
unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
unittest_import::ImportMessage::descriptor()->file()->CopyTo(
&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);
ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL);
ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
+ ASSERT_TRUE(pool_.BuildFile(unittest_no_field_presence_file) != NULL);
descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
ASSERT_TRUE(descriptor_ != NULL);
@@ -105,6 +112,12 @@ class DynamicMessageTest : public testing::Test {
pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2");
ASSERT_TRUE(oneof_descriptor_ != NULL);
oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_);
+
+ proto3_descriptor_ =
+ pool_.FindMessageTypeByName(
+ "proto2_nofieldpresence_unittest.TestAllTypes");
+ ASSERT_TRUE(proto3_descriptor_ != NULL);
+ proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_);
}
};
@@ -226,5 +239,47 @@ TEST_F(DynamicMessageTest, SpaceUsed) {
EXPECT_LT(initial_space_used, message->SpaceUsed());
}
+TEST_F(DynamicMessageTest, Arena) {
+ Arena arena;
+ Message* message = prototype_->New(&arena);
+ (void)message; // avoid unused-variable error.
+ // Return without freeing: should not leak.
+}
+
+TEST_F(DynamicMessageTest, Proto3) {
+ Message* message = proto3_prototype_->New();
+ const Reflection* refl = message->GetReflection();
+ const Descriptor* desc = message->GetDescriptor();
+
+ // Just test a single primtive and single message field here to make sure we
+ // are getting the no-field-presence semantics elsewhere. DynamicMessage uses
+ // GeneratedMessageReflection under the hood, so the rest should be fine as
+ // long as GMR recognizes that we're using a proto3 message.
+ const FieldDescriptor* optional_int32 =
+ desc->FindFieldByName("optional_int32");
+ const FieldDescriptor* optional_msg =
+ desc->FindFieldByName("optional_nested_message");
+ EXPECT_TRUE(optional_int32 != NULL);
+ EXPECT_TRUE(optional_msg != NULL);
+
+ EXPECT_EQ(false, refl->HasField(*message, optional_int32));
+ refl->SetInt32(message, optional_int32, 42);
+ EXPECT_EQ(true, refl->HasField(*message, optional_int32));
+ refl->SetInt32(message, optional_int32, 0);
+ EXPECT_EQ(false, refl->HasField(*message, optional_int32));
+
+ EXPECT_EQ(false, refl->HasField(*message, optional_msg));
+ refl->MutableMessage(message, optional_msg);
+ EXPECT_EQ(true, refl->HasField(*message, optional_msg));
+ delete refl->ReleaseMessage(message, optional_msg);
+ EXPECT_EQ(false, refl->HasField(*message, optional_msg));
+
+ // Also ensure that the default instance handles field presence properly.
+ EXPECT_EQ(false, refl->HasField(*proto3_prototype_, optional_msg));
+
+ delete message;
+}
+
+
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/extension_set.cc b/src/google/protobuf/extension_set.cc
index 274554b5..49087131 100644
--- a/src/google/protobuf/extension_set.cc
+++ b/src/google/protobuf/extension_set.cc
@@ -71,6 +71,8 @@ inline bool is_packable(WireFormatLite::WireType 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;
}
// Registry stuff.
@@ -174,12 +176,21 @@ void ExtensionSet::RegisterMessageExtension(const MessageLite* containing_type,
// ===================================================================
// Constructors and basic methods.
-ExtensionSet::ExtensionSet() {}
+ExtensionSet::ExtensionSet(::google::protobuf::Arena* arena) : arena_(arena) {
+ if (arena_ != NULL) {
+ arena_->OwnDestructor(&extensions_);
+ }
+}
+
+ExtensionSet::ExtensionSet() : arena_(NULL) {}
ExtensionSet::~ExtensionSet() {
- for (map<int, Extension>::iterator iter = extensions_.begin();
- iter != extensions_.end(); ++iter) {
- iter->second.Free();
+ // Deletes all allocated extensions.
+ if (arena_ == NULL) {
+ for (map<int, Extension>::iterator iter = extensions_.begin();
+ iter != extensions_.end(); ++iter) {
+ iter->second.Free();
+ }
}
}
@@ -301,7 +312,8 @@ void ExtensionSet::Add##CAMELCASE(int number, FieldType type, \
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_##UPPERCASE); \
extension->is_repeated = true; \
extension->is_packed = packed; \
- extension->repeated_##LOWERCASE##_value = new RepeatedField<LOWERCASE>(); \
+ extension->repeated_##LOWERCASE##_value = \
+ Arena::Create<RepeatedField<LOWERCASE> >(arena_, arena_); \
} else { \
GOOGLE_DCHECK_TYPE(*extension, REPEATED, UPPERCASE); \
GOOGLE_DCHECK_EQ(extension->is_packed, packed); \
@@ -345,34 +357,44 @@ void* ExtensionSet::MutableRawRepeatedField(int number, FieldType field_type,
switch (WireFormatLite::FieldTypeToCppType(
static_cast<WireFormatLite::FieldType>(field_type))) {
case WireFormatLite::CPPTYPE_INT32:
- extension->repeated_int32_value = new RepeatedField<int32>();
+ extension->repeated_int32_value =
+ Arena::Create<RepeatedField<int32> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_INT64:
- extension->repeated_int64_value = new RepeatedField<int64>();
+ extension->repeated_int64_value =
+ Arena::Create<RepeatedField<int64> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_UINT32:
- extension->repeated_uint32_value = new RepeatedField<uint32>();
+ extension->repeated_uint32_value =
+ Arena::Create<RepeatedField<uint32> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_UINT64:
- extension->repeated_uint64_value = new RepeatedField<uint64>();
+ extension->repeated_uint64_value =
+ Arena::Create<RepeatedField<uint64> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_DOUBLE:
- extension->repeated_double_value = new RepeatedField<double>();
+ extension->repeated_double_value =
+ Arena::Create<RepeatedField<double> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_FLOAT:
- extension->repeated_float_value = new RepeatedField<float>();
+ extension->repeated_float_value =
+ Arena::Create<RepeatedField<float> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_BOOL:
- extension->repeated_bool_value = new RepeatedField<bool>();
+ extension->repeated_bool_value =
+ Arena::Create<RepeatedField<bool> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_ENUM:
- extension->repeated_enum_value = new RepeatedField<int>();
+ extension->repeated_enum_value =
+ Arena::Create<RepeatedField<int> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_STRING:
- extension->repeated_string_value = new RepeatedPtrField< ::std::string>();
+ extension->repeated_string_value =
+ Arena::Create<RepeatedPtrField< ::std::string> >(arena_, arena_);
break;
case WireFormatLite::CPPTYPE_MESSAGE:
- extension->repeated_message_value = new RepeatedPtrField<MessageLite>();
+ extension->repeated_message_value =
+ Arena::Create<RepeatedPtrField<MessageLite> >(arena_, arena_);
break;
}
}
@@ -444,7 +466,8 @@ void ExtensionSet::AddEnum(int number, FieldType type,
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_ENUM);
extension->is_repeated = true;
extension->is_packed = packed;
- extension->repeated_enum_value = new RepeatedField<int>();
+ extension->repeated_enum_value =
+ Arena::Create<RepeatedField<int> >(arena_, arena_);
} else {
GOOGLE_DCHECK_TYPE(*extension, REPEATED, ENUM);
GOOGLE_DCHECK_EQ(extension->is_packed, packed);
@@ -474,7 +497,7 @@ string* ExtensionSet::MutableString(int number, FieldType type,
extension->type = type;
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
extension->is_repeated = false;
- extension->string_value = new string;
+ extension->string_value = Arena::Create<string>(arena_);
} else {
GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, STRING);
}
@@ -504,7 +527,8 @@ string* ExtensionSet::AddString(int number, FieldType type,
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_STRING);
extension->is_repeated = true;
extension->is_packed = false;
- extension->repeated_string_value = new RepeatedPtrField<string>();
+ extension->repeated_string_value =
+ Arena::Create<RepeatedPtrField<string> >(arena_, arena_);
} else {
GOOGLE_DCHECK_TYPE(*extension, REPEATED, STRING);
}
@@ -544,7 +568,7 @@ MessageLite* ExtensionSet::MutableMessage(int number, FieldType type,
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
extension->is_repeated = false;
extension->is_lazy = false;
- extension->message_value = prototype.New();
+ extension->message_value = prototype.New(arena_);
extension->is_cleared = false;
return extension->message_value;
} else {
@@ -576,14 +600,32 @@ void ExtensionSet::SetAllocatedMessage(int number, FieldType type,
GOOGLE_DCHECK_EQ(cpp_type(extension->type), WireFormatLite::CPPTYPE_MESSAGE);
extension->is_repeated = false;
extension->is_lazy = false;
- extension->message_value = message;
+ if (message->GetArena() == arena_) {
+ extension->message_value = message;
+ } else {
+ extension->message_value = message->New(arena_);
+ extension->message_value->CheckTypeAndMergeFrom(*message);
+ if (message->GetArena() == NULL) {
+ delete message;
+ }
+ }
} else {
GOOGLE_DCHECK_TYPE(*extension, OPTIONAL, MESSAGE);
if (extension->is_lazy) {
extension->lazymessage_value->SetAllocatedMessage(message);
} else {
- delete extension->message_value;
- extension->message_value = message;
+ if (arena_ == NULL) {
+ delete extension->message_value;
+ }
+ if (message->GetArena() == arena_) {
+ extension->message_value = message;
+ } else {
+ extension->message_value = message->New(arena_);
+ extension->message_value->CheckTypeAndMergeFrom(*message);
+ if (message->GetArena() == NULL) {
+ delete message;
+ }
+ }
}
}
extension->is_cleared = false;
@@ -600,7 +642,39 @@ MessageLite* ExtensionSet::ReleaseMessage(int number,
MessageLite* ret = NULL;
if (iter->second.is_lazy) {
ret = iter->second.lazymessage_value->ReleaseMessage(prototype);
- delete iter->second.lazymessage_value;
+ if (arena_ == NULL) {
+ delete iter->second.lazymessage_value;
+ }
+ } else {
+ if (arena_ == NULL) {
+ ret = iter->second.message_value;
+ } else {
+ // ReleaseMessage() always returns a heap-allocated message, and we are
+ // on an arena, so we need to make a copy of this message to return.
+ ret = (iter->second.message_value)->New();
+ ret->CheckTypeAndMergeFrom(*iter->second.message_value);
+ }
+ }
+ extensions_.erase(number);
+ return ret;
+ }
+}
+
+MessageLite* ExtensionSet::UnsafeArenaReleaseMessage(
+ int number, const MessageLite& prototype) {
+ map<int, Extension>::iterator iter = extensions_.find(number);
+ if (iter == extensions_.end()) {
+ // Not present. Return NULL.
+ return NULL;
+ } else {
+ GOOGLE_DCHECK_TYPE(iter->second, OPTIONAL, MESSAGE);
+ MessageLite* ret = NULL;
+ if (iter->second.is_lazy) {
+ ret =
+ iter->second.lazymessage_value->UnsafeArenaReleaseMessage(prototype);
+ if (arena_ == NULL) {
+ delete iter->second.lazymessage_value;
+ }
} else {
ret = iter->second.message_value;
}
@@ -637,7 +711,7 @@ 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 =
- new RepeatedPtrField<MessageLite>();
+ Arena::Create<RepeatedPtrField<MessageLite> >(arena_, arena_);
} else {
GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
}
@@ -647,7 +721,7 @@ MessageLite* ExtensionSet::AddMessage(int number, FieldType type,
MessageLite* result = extension->repeated_message_value
->AddFromCleared<GenericTypeHandler<MessageLite> >();
if (result == NULL) {
- result = prototype.New();
+ result = prototype.New(arena_);
extension->repeated_message_value->AddAllocated(result);
}
return result;
@@ -765,138 +839,142 @@ void ExtensionSet::MergeFrom(const ExtensionSet& other) {
for (map<int, Extension>::const_iterator iter = other.extensions_.begin();
iter != other.extensions_.end(); ++iter) {
const Extension& other_extension = iter->second;
+ InternalExtensionMergeFrom(iter->first, other_extension);
+ }
+}
- if (other_extension.is_repeated) {
- Extension* extension;
- bool is_new = MaybeNewExtension(iter->first, other_extension.descriptor,
- &extension);
- if (is_new) {
- // Extension did not already exist in set.
- extension->type = other_extension.type;
- extension->is_packed = other_extension.is_packed;
- extension->is_repeated = true;
- } else {
- GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
- GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
- GOOGLE_DCHECK(extension->is_repeated);
- }
+void ExtensionSet::InternalExtensionMergeFrom(
+ int number, const Extension& other_extension) {
+ if (other_extension.is_repeated) {
+ Extension* extension;
+ bool is_new = MaybeNewExtension(number, other_extension.descriptor,
+ &extension);
+ if (is_new) {
+ // Extension did not already exist in set.
+ extension->type = other_extension.type;
+ extension->is_packed = other_extension.is_packed;
+ extension->is_repeated = true;
+ } else {
+ GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
+ GOOGLE_DCHECK_EQ(extension->is_packed, other_extension.is_packed);
+ GOOGLE_DCHECK(extension->is_repeated);
+ }
- switch (cpp_type(other_extension.type)) {
-#define HANDLE_TYPE(UPPERCASE, LOWERCASE, REPEATED_TYPE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- if (is_new) { \
- extension->repeated_##LOWERCASE##_value = \
- new REPEATED_TYPE; \
- } \
- extension->repeated_##LOWERCASE##_value->MergeFrom( \
- *other_extension.repeated_##LOWERCASE##_value); \
- break;
+ 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::Create<REPEATED_TYPE >(arena_, 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>);
+ 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>);
#undef HANDLE_TYPE
- case WireFormatLite::CPPTYPE_MESSAGE:
- if (is_new) {
- extension->repeated_message_value =
- new RepeatedPtrField<MessageLite>();
- }
- // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
- // it would attempt to allocate new objects.
- RepeatedPtrField<MessageLite>* other_repeated_message =
- other_extension.repeated_message_value;
- for (int i = 0; i < other_repeated_message->size(); i++) {
- const MessageLite& other_message = other_repeated_message->Get(i);
- MessageLite* target = extension->repeated_message_value
- ->AddFromCleared<GenericTypeHandler<MessageLite> >();
- if (target == NULL) {
- target = other_message.New();
- extension->repeated_message_value->AddAllocated(target);
- }
- target->CheckTypeAndMergeFrom(other_message);
+ case WireFormatLite::CPPTYPE_MESSAGE:
+ if (is_new) {
+ extension->repeated_message_value =
+ Arena::Create<RepeatedPtrField<MessageLite> >(arena_, arena_);
+ }
+ // We can't call RepeatedPtrField<MessageLite>::MergeFrom() because
+ // it would attempt to allocate new objects.
+ RepeatedPtrField<MessageLite>* other_repeated_message =
+ other_extension.repeated_message_value;
+ for (int i = 0; i < other_repeated_message->size(); i++) {
+ const MessageLite& other_message = other_repeated_message->Get(i);
+ MessageLite* target = extension->repeated_message_value
+ ->AddFromCleared<GenericTypeHandler<MessageLite> >();
+ if (target == NULL) {
+ target = other_message.New(arena_);
+ extension->repeated_message_value->AddAllocated(target);
}
+ target->CheckTypeAndMergeFrom(other_message);
+ }
+ break;
+ }
+ } else {
+ if (!other_extension.is_cleared) {
+ switch (cpp_type(other_extension.type)) {
+#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
+ case WireFormatLite::CPPTYPE_##UPPERCASE: \
+ Set##CAMELCASE(number, other_extension.type, \
+ other_extension.LOWERCASE##_value, \
+ other_extension.descriptor); \
break;
- }
- } else {
- if (!other_extension.is_cleared) {
- switch (cpp_type(other_extension.type)) {
-#define HANDLE_TYPE(UPPERCASE, LOWERCASE, CAMELCASE) \
- case WireFormatLite::CPPTYPE_##UPPERCASE: \
- Set##CAMELCASE(iter->first, other_extension.type, \
- other_extension.LOWERCASE##_value, \
- other_extension.descriptor); \
- break;
-
- HANDLE_TYPE( INT32, int32, Int32);
- HANDLE_TYPE( INT64, int64, Int64);
- HANDLE_TYPE(UINT32, uint32, UInt32);
- HANDLE_TYPE(UINT64, uint64, UInt64);
- HANDLE_TYPE( FLOAT, float, Float);
- HANDLE_TYPE(DOUBLE, double, Double);
- HANDLE_TYPE( BOOL, bool, Bool);
- HANDLE_TYPE( ENUM, enum, Enum);
+
+ HANDLE_TYPE( INT32, int32, Int32);
+ HANDLE_TYPE( INT64, int64, Int64);
+ HANDLE_TYPE(UINT32, uint32, UInt32);
+ HANDLE_TYPE(UINT64, uint64, UInt64);
+ HANDLE_TYPE( FLOAT, float, Float);
+ HANDLE_TYPE(DOUBLE, double, Double);
+ HANDLE_TYPE( BOOL, bool, Bool);
+ HANDLE_TYPE( ENUM, enum, Enum);
#undef HANDLE_TYPE
- case WireFormatLite::CPPTYPE_STRING:
- SetString(iter->first, other_extension.type,
- *other_extension.string_value,
- other_extension.descriptor);
- break;
- case WireFormatLite::CPPTYPE_MESSAGE: {
- Extension* extension;
- bool is_new = MaybeNewExtension(iter->first,
- other_extension.descriptor,
- &extension);
- if (is_new) {
- extension->type = other_extension.type;
- extension->is_packed = other_extension.is_packed;
- extension->is_repeated = false;
- if (other_extension.is_lazy) {
- extension->is_lazy = true;
- extension->lazymessage_value =
- other_extension.lazymessage_value->New();
+ case WireFormatLite::CPPTYPE_STRING:
+ SetString(number, other_extension.type,
+ *other_extension.string_value,
+ other_extension.descriptor);
+ break;
+ case WireFormatLite::CPPTYPE_MESSAGE: {
+ Extension* extension;
+ bool is_new = MaybeNewExtension(number,
+ other_extension.descriptor,
+ &extension);
+ if (is_new) {
+ extension->type = other_extension.type;
+ extension->is_packed = other_extension.is_packed;
+ extension->is_repeated = false;
+ if (other_extension.is_lazy) {
+ extension->is_lazy = true;
+ extension->lazymessage_value =
+ other_extension.lazymessage_value->New(arena_);
+ extension->lazymessage_value->MergeFrom(
+ *other_extension.lazymessage_value);
+ } else {
+ extension->is_lazy = false;
+ extension->message_value =
+ other_extension.message_value->New(arena_);
+ extension->message_value->CheckTypeAndMergeFrom(
+ *other_extension.message_value);
+ }
+ } else {
+ GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
+ GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed);
+ GOOGLE_DCHECK(!extension->is_repeated);
+ if (other_extension.is_lazy) {
+ if (extension->is_lazy) {
extension->lazymessage_value->MergeFrom(
*other_extension.lazymessage_value);
} else {
- extension->is_lazy = false;
- extension->message_value =
- other_extension.message_value->New();
extension->message_value->CheckTypeAndMergeFrom(
- *other_extension.message_value);
+ other_extension.lazymessage_value->GetMessage(
+ *extension->message_value));
}
} else {
- GOOGLE_DCHECK_EQ(extension->type, other_extension.type);
- GOOGLE_DCHECK_EQ(extension->is_packed,other_extension.is_packed);
- GOOGLE_DCHECK(!extension->is_repeated);
- if (other_extension.is_lazy) {
- if (extension->is_lazy) {
- extension->lazymessage_value->MergeFrom(
- *other_extension.lazymessage_value);
- } else {
- extension->message_value->CheckTypeAndMergeFrom(
- other_extension.lazymessage_value->GetMessage(
- *extension->message_value));
- }
+ if (extension->is_lazy) {
+ extension->lazymessage_value->MutableMessage(
+ *other_extension.message_value)->CheckTypeAndMergeFrom(
+ *other_extension.message_value);
} else {
- if (extension->is_lazy) {
- extension->lazymessage_value->MutableMessage(
- *other_extension.message_value)->CheckTypeAndMergeFrom(
- *other_extension.message_value);
- } else {
- extension->message_value->CheckTypeAndMergeFrom(
- *other_extension.message_value);
- }
+ extension->message_value->CheckTypeAndMergeFrom(
+ *other_extension.message_value);
}
}
- extension->is_cleared = false;
- break;
}
+ extension->is_cleared = false;
+ break;
}
}
}
@@ -904,7 +982,19 @@ void ExtensionSet::MergeFrom(const ExtensionSet& other) {
}
void ExtensionSet::Swap(ExtensionSet* x) {
- extensions_.swap(x->extensions_);
+ if (GetArenaNoVirtual() == x->GetArenaNoVirtual()) {
+ extensions_.swap(x->extensions_);
+ } else {
+ // TODO(cfallin, rohananil): We maybe able to optimize a case where we are
+ // swapping from heap to arena-allocated extension set, by just Own()'ing
+ // the extensions.
+ ExtensionSet extension_set;
+ extension_set.MergeFrom(*x);
+ x->Clear();
+ x->MergeFrom(*this);
+ Clear();
+ MergeFrom(extension_set);
+ }
}
void ExtensionSet::SwapExtension(ExtensionSet* other,
@@ -920,18 +1010,42 @@ void ExtensionSet::SwapExtension(ExtensionSet* other,
if (this_iter != extensions_.end() &&
other_iter != other->extensions_.end()) {
- std::swap(this_iter->second, other_iter->second);
+ if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
+ using std::swap;
+ swap(this_iter->second, other_iter->second);
+ } else {
+ // TODO(cfallin, rohananil): We could further optimize these cases,
+ // especially avoid creation of ExtensionSet, and move MergeFrom logic
+ // into Extensions itself (which takes arena as an argument).
+ // We do it this way to reuse the copy-across-arenas logic already
+ // implemented in ExtensionSet's MergeFrom.
+ ExtensionSet temp;
+ temp.InternalExtensionMergeFrom(number, other_iter->second);
+ map<int, Extension>::iterator temp_iter = temp.extensions_.find(number);
+ other_iter->second.Clear();
+ other->InternalExtensionMergeFrom(number, this_iter->second);
+ this_iter->second.Clear();
+ InternalExtensionMergeFrom(number, temp_iter->second);
+ }
return;
}
if (this_iter == extensions_.end()) {
- extensions_.insert(make_pair(number, other_iter->second));
+ if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
+ extensions_.insert(make_pair(number, other_iter->second));
+ } else {
+ InternalExtensionMergeFrom(number, other_iter->second);
+ }
other->extensions_.erase(number);
return;
}
if (other_iter == other->extensions_.end()) {
- other->extensions_.insert(make_pair(number, this_iter->second));
+ if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
+ other->extensions_.insert(make_pair(number, this_iter->second));
+ } else {
+ other->InternalExtensionMergeFrom(number, this_iter->second);
+ }
extensions_.erase(number);
return;
}
@@ -1223,7 +1337,7 @@ bool ExtensionSet::MaybeNewExtension(int number,
const FieldDescriptor* descriptor,
Extension** result) {
pair<map<int, Extension>::iterator, bool> insert_result =
- extensions_.insert(make_pair(number, Extension()));
+ extensions_.insert(make_pair(number, Extension()));
*result = &insert_result.first->second;
(*result)->descriptor = descriptor;
return insert_result.second;
@@ -1554,6 +1668,8 @@ int ExtensionSet::Extension::GetSize() const {
return 0;
}
+// This function deletes all allocated objects. This function should be only
+// called if the Extension was created with an arena.
void ExtensionSet::Extension::Free() {
if (is_repeated) {
switch (cpp_type(type)) {
diff --git a/src/google/protobuf/extension_set.h b/src/google/protobuf/extension_set.h
index d7ec5192..6d6702b3 100644
--- a/src/google/protobuf/extension_set.h
+++ b/src/google/protobuf/extension_set.h
@@ -51,6 +51,7 @@
namespace google {
namespace protobuf {
+ class Arena;
class Descriptor; // descriptor.h
class FieldDescriptor; // descriptor.h
class DescriptorPool; // descriptor.h
@@ -157,6 +158,7 @@ class MessageSetFieldSkipper;
class LIBPROTOBUF_EXPORT ExtensionSet {
public:
ExtensionSet();
+ explicit ExtensionSet(::google::protobuf::Arena* arena);
~ExtensionSet();
// These are called at startup by protocol-compiler-generated code to
@@ -182,7 +184,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
// is useful to implement Reflection::ListFields().
void AppendToList(const Descriptor* containing_type,
const DescriptorPool* pool,
- vector<const FieldDescriptor*>* output) const;
+ std::vector<const FieldDescriptor*>* output) const;
// =================================================================
// Accessors
@@ -261,9 +263,13 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
const FieldDescriptor* descriptor,
MessageLite* message);
MessageLite* ReleaseMessage(int number, const MessageLite& prototype);
+ MessageLite* UnsafeArenaReleaseMessage(
+ int number, const MessageLite& prototype);
+
MessageLite* ReleaseMessage(const FieldDescriptor* descriptor,
MessageFactory* factory);
#undef desc
+ ::google::protobuf::Arena* GetArenaNoVirtual() const { return arena_; }
// repeated fields -------------------------------------------------
@@ -421,12 +427,14 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
LazyMessageExtension() {}
virtual ~LazyMessageExtension() {}
- virtual LazyMessageExtension* New() const = 0;
+ virtual LazyMessageExtension* New(::google::protobuf::Arena* arena) const = 0;
virtual const MessageLite& GetMessage(
const MessageLite& prototype) const = 0;
virtual MessageLite* MutableMessage(const MessageLite& prototype) = 0;
virtual void SetAllocatedMessage(MessageLite *message) = 0;
virtual MessageLite* ReleaseMessage(const MessageLite& prototype) = 0;
+ virtual MessageLite* UnsafeArenaReleaseMessage(
+ const MessageLite& prototype) = 0;
virtual bool IsInitialized() const = 0;
virtual int ByteSize() const = 0;
@@ -524,6 +532,9 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
};
+ // Merges existing Extension from other_extension
+ void InternalExtensionMergeFrom(int number, const Extension& other_extension);
+
// Returns true and fills field_number and extension if extension is found.
// Note to support packed repeated field compatibility, it also fills whether
// the tag on wire is packed, which can be different from
@@ -569,7 +580,6 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
ExtensionFinder* extension_finder,
MessageSetFieldSkipper* field_skipper);
-
// 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
@@ -587,7 +597,7 @@ class LIBPROTOBUF_EXPORT ExtensionSet {
// for 100 elements or more. Also, we want AppendToList() to order fields
// by field number.
std::map<int, Extension> extensions_;
-
+ ::google::protobuf::Arena* arena_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionSet);
};
diff --git a/src/google/protobuf/extension_set_heavy.cc b/src/google/protobuf/extension_set_heavy.cc
index eae4d574..5f4ca1c3 100644
--- a/src/google/protobuf/extension_set_heavy.cc
+++ b/src/google/protobuf/extension_set_heavy.cc
@@ -169,7 +169,7 @@ MessageLite* ExtensionSet::MutableMessage(const FieldDescriptor* descriptor,
const MessageLite* prototype =
factory->GetPrototype(descriptor->message_type());
extension->is_lazy = false;
- extension->message_value = prototype->New();
+ extension->message_value = prototype->New(arena_);
extension->is_cleared = false;
return extension->message_value;
} else {
@@ -196,9 +196,16 @@ MessageLite* ExtensionSet::ReleaseMessage(const FieldDescriptor* descriptor,
if (iter->second.is_lazy) {
ret = iter->second.lazymessage_value->ReleaseMessage(
*factory->GetPrototype(descriptor->message_type()));
- delete iter->second.lazymessage_value;
+ if (arena_ == NULL) {
+ delete iter->second.lazymessage_value;
+ }
} else {
- ret = iter->second.message_value;
+ if (arena_ != NULL) {
+ ret = (iter->second.message_value)->New();
+ ret->CheckTypeAndMergeFrom(*(iter->second.message_value));
+ } else {
+ ret = iter->second.message_value;
+ }
}
extensions_.erase(descriptor->number());
return ret;
@@ -213,7 +220,7 @@ MessageLite* ExtensionSet::AddMessage(const FieldDescriptor* descriptor,
GOOGLE_DCHECK_EQ(cpp_type(extension->type), FieldDescriptor::CPPTYPE_MESSAGE);
extension->is_repeated = true;
extension->repeated_message_value =
- new RepeatedPtrField<MessageLite>();
+ ::google::protobuf::Arena::Create<RepeatedPtrField<MessageLite> >(arena_, arena_);
} else {
GOOGLE_DCHECK_TYPE(*extension, REPEATED, MESSAGE);
}
@@ -230,7 +237,7 @@ MessageLite* ExtensionSet::AddMessage(const FieldDescriptor* descriptor,
} else {
prototype = &extension->repeated_message_value->Get(0);
}
- result = prototype->New();
+ result = prototype->New(arena_);
extension->repeated_message_value->AddAllocated(result);
}
return result;
diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc
index 8a00bafd..684ce002 100644
--- a/src/google/protobuf/extension_set_unittest.cc
+++ b/src/google/protobuf/extension_set_unittest.cc
@@ -37,6 +37,7 @@
#include <google/protobuf/unittest_mset.pb.h>
#include <google/protobuf/test_util.h>
#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/arena.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/wire_format.h>
@@ -319,6 +320,117 @@ TEST(ExtensionSetTest, SwapExtensionBothFull) {
TestUtil::ExpectAllExtensionsSet(message2);
}
+TEST(ExtensionSetTest, ArenaSetAllExtension) {
+ ::google::protobuf::Arena arena1;
+ unittest::TestAllExtensions* message1 =
+ ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
+ TestUtil::SetAllExtensions(message1);
+ TestUtil::ExpectAllExtensionsSet(*message1);
+}
+
+TEST(ExtensionSetTest, ArenaCopyConstructor) {
+ ::google::protobuf::Arena arena1;
+ unittest::TestAllExtensions* message1 =
+ ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
+ TestUtil::SetAllExtensions(message1);
+ unittest::TestAllExtensions message2(*message1);
+ arena1.Reset();
+ TestUtil::ExpectAllExtensionsSet(message2);
+}
+
+TEST(ExtensionSetTest, ArenaMergeFrom) {
+ ::google::protobuf::Arena arena1;
+ unittest::TestAllExtensions* message1 =
+ ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
+ TestUtil::SetAllExtensions(message1);
+ unittest::TestAllExtensions message2;
+ message2.MergeFrom(*message1);
+ arena1.Reset();
+ TestUtil::ExpectAllExtensionsSet(message2);
+}
+
+TEST(ExtensionSetTest, ArenaSetAllocatedMessageAndRelease) {
+ ::google::protobuf::Arena arena;
+ unittest::TestAllExtensions* message =
+ ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
+ 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 copied underneath, as foreign_message is on heap
+ // and extension_set is on an arena.
+ EXPECT_NE(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);
+ delete released_message;
+ EXPECT_FALSE(message->HasExtension(
+ unittest::optional_foreign_message_extension));
+}
+
+TEST(ExtensionSetTest, SwapExtensionBothFullWithArena) {
+ ::google::protobuf::Arena arena1;
+ google::protobuf::scoped_ptr<google::protobuf::Arena> arena2(new ::google::protobuf::Arena());
+
+ unittest::TestAllExtensions* message1 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
+ unittest::TestAllExtensions* message2 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(arena2.get());
+
+ TestUtil::SetAllExtensions(message1);
+ TestUtil::SetAllExtensions(message2);
+ message1->SetExtension(unittest::optional_int32_extension, 1);
+ message2->SetExtension(unittest::optional_int32_extension, 2);
+ message1->Swap(message2);
+ EXPECT_EQ(2, message1->GetExtension(unittest::optional_int32_extension));
+ EXPECT_EQ(1, message2->GetExtension(unittest::optional_int32_extension));
+ // Re-set the original values so ExpectAllExtensionsSet is happy.
+ message1->SetExtension(unittest::optional_int32_extension, 101);
+ message2->SetExtension(unittest::optional_int32_extension, 101);
+ TestUtil::ExpectAllExtensionsSet(*message1);
+ TestUtil::ExpectAllExtensionsSet(*message2);
+ arena2.reset(NULL);
+ TestUtil::ExpectAllExtensionsSet(*message1);
+ // Test corner cases, when one is empty and other is not.
+ ::google::protobuf::Arena arena3, arena4;
+
+ unittest::TestAllExtensions* message3 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(&arena3);
+ unittest::TestAllExtensions* message4 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(&arena4);
+ TestUtil::SetAllExtensions(message3);
+ message3->Swap(message4);
+ arena3.Reset();
+ TestUtil::ExpectAllExtensionsSet(*message4);
+}
+
+TEST(ExtensionSetTest, SwapFieldsOfExtensionBothFullWithArena) {
+ google::protobuf::Arena arena1;
+ google::protobuf::Arena* arena2 = new ::google::protobuf::Arena();
+
+ unittest::TestAllExtensions* message1 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(&arena1);
+ unittest::TestAllExtensions* message2 =
+ Arena::CreateMessage<unittest::TestAllExtensions>(arena2);
+
+ TestUtil::SetAllExtensions(message1);
+ TestUtil::SetAllExtensions(message2);
+
+ const Reflection* reflection = message1->GetReflection();
+ vector<const FieldDescriptor*> fields;
+ reflection->ListFields(*message1, &fields);
+ reflection->SwapFields(message1, message2, fields);
+ TestUtil::ExpectAllExtensionsSet(*message1);
+ TestUtil::ExpectAllExtensionsSet(*message2);
+ delete arena2;
+ TestUtil::ExpectAllExtensionsSet(*message1);
+}
+
TEST(ExtensionSetTest, SwapExtensionWithSelf) {
unittest::TestAllExtensions message1;
diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc
index 536de7d9..b500b9c5 100644
--- a/src/google/protobuf/generated_message_reflection.cc
+++ b/src/google/protobuf/generated_message_reflection.cc
@@ -34,13 +34,16 @@
#include <algorithm>
#include <set>
+
+#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.pb.h>
-#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/descriptor.h>
-#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
+#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/generated_message_util.h>
-#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/map_field.h>
+#include <google/protobuf/repeated_field.h>
+
#define GOOGLE_PROTOBUF_HAS_ONEOF
@@ -48,6 +51,12 @@ namespace google {
namespace protobuf {
namespace internal {
+namespace {
+bool IsMapFieldInApi(const FieldDescriptor* field) {
+ return field->is_map();
+}
+} // anonymous namespace
+
int StringSpaceUsedExcludingSelf(const string& str) {
const void* start = &str;
const void* end = &str + 1;
@@ -74,6 +83,12 @@ const string& NameOfEnum(const EnumDescriptor* descriptor, int value) {
return (d == NULL ? GetEmptyString() : d->name());
}
+namespace {
+inline bool SupportsArenas(const Descriptor* descriptor) {
+ return descriptor->file()->options().cc_enable_arenas();
+}
+} // anonymous namespace
+
// ===================================================================
// Helpers for reporting usage errors (e.g. trying to use GetInt32() on
// a string field).
@@ -177,13 +192,17 @@ GeneratedMessageReflection::GeneratedMessageReflection(
int extensions_offset,
const DescriptorPool* descriptor_pool,
MessageFactory* factory,
- int object_size)
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset)
: descriptor_ (descriptor),
default_instance_ (default_instance),
offsets_ (offsets),
has_bits_offset_ (has_bits_offset),
unknown_fields_offset_(unknown_fields_offset),
extensions_offset_(extensions_offset),
+ arena_offset_ (arena_offset),
+ is_default_instance_offset_(is_default_instance_offset),
object_size_ (object_size),
descriptor_pool_ ((descriptor_pool == NULL) ?
DescriptorPool::generated_pool() :
@@ -202,7 +221,9 @@ GeneratedMessageReflection::GeneratedMessageReflection(
int oneof_case_offset,
const DescriptorPool* descriptor_pool,
MessageFactory* factory,
- int object_size)
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset)
: descriptor_ (descriptor),
default_instance_ (default_instance),
default_oneof_instance_ (default_oneof_instance),
@@ -211,6 +232,8 @@ GeneratedMessageReflection::GeneratedMessageReflection(
oneof_case_offset_(oneof_case_offset),
unknown_fields_offset_(unknown_fields_offset),
extensions_offset_(extensions_offset),
+ arena_offset_ (arena_offset),
+ is_default_instance_offset_(is_default_instance_offset),
object_size_ (object_size),
descriptor_pool_ ((descriptor_pool == NULL) ?
DescriptorPool::generated_pool() :
@@ -220,14 +243,39 @@ GeneratedMessageReflection::GeneratedMessageReflection(
GeneratedMessageReflection::~GeneratedMessageReflection() {}
+namespace {
+UnknownFieldSet* empty_unknown_field_set_ = NULL;
+GOOGLE_PROTOBUF_DECLARE_ONCE(empty_unknown_field_set_once_);
+
+void InitEmptyUnknownFieldSet() {
+ empty_unknown_field_set_ = new UnknownFieldSet;
+}
+
+const UnknownFieldSet& GetEmptyUnknownFieldSet() {
+ ::google::protobuf::GoogleOnceInit(&empty_unknown_field_set_once_, &InitEmptyUnknownFieldSet);
+ return *empty_unknown_field_set_;
+}
+} // namespace
+
const UnknownFieldSet& GeneratedMessageReflection::GetUnknownFields(
const Message& message) const {
+ if (descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
+ return GetEmptyUnknownFieldSet();
+ }
+ if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
+ return GetInternalMetadataWithArena(message).unknown_fields();
+ }
const void* ptr = reinterpret_cast<const uint8*>(&message) +
unknown_fields_offset_;
return *reinterpret_cast<const UnknownFieldSet*>(ptr);
}
+
UnknownFieldSet* GeneratedMessageReflection::MutableUnknownFields(
Message* message) const {
+ if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
+ return MutableInternalMetadataWithArena(message)->
+ mutable_unknown_fields();
+ }
void* ptr = reinterpret_cast<uint8*>(message) + unknown_fields_offset_;
return reinterpret_cast<UnknownFieldSet*>(ptr);
}
@@ -276,11 +324,17 @@ int GeneratedMessageReflection::SpaceUsed(const Message& message) const {
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
- // We don't know which subclass of RepeatedPtrFieldBase the type is,
- // so we use RepeatedPtrFieldBase directly.
- total_size +=
- GetRaw<RepeatedPtrFieldBase>(message, field)
- .SpaceUsedExcludingSelf<GenericTypeHandler<Message> >();
+ if (IsMapFieldInApi(field)) {
+ total_size +=
+ GetRaw<MapFieldBase>(message, field).SpaceUsedExcludingSelf();
+ } else {
+ // We don't know which subclass of RepeatedPtrFieldBase the type is,
+ // so we use RepeatedPtrFieldBase directly.
+ total_size +=
+ GetRaw<RepeatedPtrFieldBase>(message, field)
+ .SpaceUsedExcludingSelf<GenericTypeHandler<Message> >();
+ }
+
break;
}
} else {
@@ -303,12 +357,13 @@ int GeneratedMessageReflection::SpaceUsed(const Message& message) const {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING: {
- const string* ptr = GetField<const string*>(message, field);
-
// 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 = DefaultRaw<const string*>(field);
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ const string* ptr =
+ &GetField<ArenaStringPtr>(message, field).Get(default_ptr);
if (ptr != default_ptr) {
// string fields are represented by just a pointer, so also
@@ -363,8 +418,17 @@ void GeneratedMessageReflection::SwapField(
case FieldDescriptor::CPPTYPE_STRING:
case FieldDescriptor::CPPTYPE_MESSAGE:
- MutableRaw<RepeatedPtrFieldBase>(message1, field)->Swap(
- MutableRaw<RepeatedPtrFieldBase>(message2, field));
+ if (IsMapFieldInApi(field)) {
+ MutableRaw<MapFieldBase>(message1, field)->
+ MutableRepeatedField()->
+ Swap<GenericTypeHandler<google::protobuf::Message> >(
+ MutableRaw<MapFieldBase>(message2, field)->
+ MutableRepeatedField());
+ } else {
+ MutableRaw<RepeatedPtrFieldBase>(message1, field)->
+ Swap<GenericTypeHandler<google::protobuf::Message> >(
+ MutableRaw<RepeatedPtrFieldBase>(message2, field));
+ }
break;
default:
@@ -396,8 +460,8 @@ void GeneratedMessageReflection::SwapField(
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING:
- std::swap(*MutableRaw<string*>(message1, field),
- *MutableRaw<string*>(message2, field));
+ MutableRaw<ArenaStringPtr>(message1, field)->Swap(
+ MutableRaw<ArenaStringPtr>(message2, field));
break;
}
break;
@@ -423,11 +487,11 @@ void GeneratedMessageReflection::SwapOneofField(
double temp_double;
bool temp_bool;
int temp_int;
- Message* temp_message;
+ Message* temp_message = NULL;
string temp_string;
// Stores message1's oneof field to a temp variable.
- const FieldDescriptor* field1;
+ const FieldDescriptor* field1 = NULL;
if (oneof_case1 > 0) {
field1 = descriptor_->FindFieldByNumber(oneof_case1);
//oneof_descriptor->field(oneof_case1);
@@ -549,12 +613,29 @@ void GeneratedMessageReflection::Swap(
<< "\"). Note that the exact same class is required; not just the same "
"descriptor.";
- uint32* has_bits1 = MutableHasBits(message1);
- uint32* has_bits2 = MutableHasBits(message2);
- int has_bits_size = (descriptor_->field_count() + 31) / 32;
+ // 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.
+ if (GetArena(message1) != GetArena(message2)) {
+ // Slow copy path.
+ // Use our arena as temp space, if available.
+ Message* temp = message1->New(GetArena(message1));
+ temp->MergeFrom(*message1);
+ message1->CopyFrom(*message2);
+ message2->CopyFrom(*temp);
+ if (GetArena(message1) == NULL) {
+ delete temp;
+ }
+ return;
+ }
+
+ if (has_bits_offset_ != -1) {
+ uint32* has_bits1 = MutableHasBits(message1);
+ uint32* has_bits2 = MutableHasBits(message2);
+ int has_bits_size = (descriptor_->field_count() + 31) / 32;
- for (int i = 0; i < has_bits_size; i++) {
- std::swap(has_bits1[i], has_bits2[i]);
+ for (int i = 0; i < has_bits_size; i++) {
+ std::swap(has_bits1[i], has_bits2[i]);
+ }
}
for (int i = 0; i < descriptor_->field_count(); i++) {
@@ -667,7 +748,11 @@ int GeneratedMessageReflection::FieldSize(const Message& message,
case FieldDescriptor::CPPTYPE_STRING:
case FieldDescriptor::CPPTYPE_MESSAGE:
- return GetRaw<RepeatedPtrFieldBase>(message, field).size();
+ if (IsMapFieldInApi(field)) {
+ return GetRaw<MapFieldBase>(message, field).GetRepeatedField().size();
+ } else {
+ return GetRaw<RepeatedPtrFieldBase>(message, field).size();
+ }
}
GOOGLE_LOG(FATAL) << "Can't get here.";
@@ -715,17 +800,13 @@ void GeneratedMessageReflection::ClearField(
case FieldDescriptor::CPPTYPE_STRING: {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
- case FieldOptions::STRING:
- const string* default_ptr = DefaultRaw<const string*>(field);
- string** value = MutableRaw<string*>(message, field);
- if (*value != default_ptr) {
- if (field->has_default_value()) {
- (*value)->assign(field->default_value_string());
- } else {
- (*value)->clear();
- }
- }
+ case FieldOptions::STRING: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ MutableRaw<ArenaStringPtr>(message, field)->Destroy(default_ptr,
+ GetArena(message));
break;
+ }
}
break;
}
@@ -763,10 +844,16 @@ void GeneratedMessageReflection::ClearField(
}
case FieldDescriptor::CPPTYPE_MESSAGE: {
- // We don't know which subclass of RepeatedPtrFieldBase the type is,
- // so we use RepeatedPtrFieldBase directly.
- MutableRaw<RepeatedPtrFieldBase>(message, field)
- ->Clear<GenericTypeHandler<Message> >();
+ if (IsMapFieldInApi(field)) {
+ MutableRaw<MapFieldBase>(message, field)
+ ->MutableRepeatedField()
+ ->Clear<GenericTypeHandler<Message> >();
+ } else {
+ // We don't know which subclass of RepeatedPtrFieldBase the type is,
+ // so we use RepeatedPtrFieldBase directly.
+ MutableRaw<RepeatedPtrFieldBase>(message, field)
+ ->Clear<GenericTypeHandler<Message> >();
+ }
break;
}
}
@@ -808,8 +895,14 @@ void GeneratedMessageReflection::RemoveLast(
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
- MutableRaw<RepeatedPtrFieldBase>(message, field)
+ if (IsMapFieldInApi(field)) {
+ MutableRaw<MapFieldBase>(message, field)
+ ->MutableRepeatedField()
+ ->RemoveLast<GenericTypeHandler<Message> >();
+ } else {
+ MutableRaw<RepeatedPtrFieldBase>(message, field)
->RemoveLast<GenericTypeHandler<Message> >();
+ }
break;
}
}
@@ -824,8 +917,14 @@ Message* GeneratedMessageReflection::ReleaseLast(
return static_cast<Message*>(
MutableExtensionSet(message)->ReleaseLast(field->number()));
} else {
- return MutableRaw<RepeatedPtrFieldBase>(message, field)
+ if (IsMapFieldInApi(field)) {
+ return MutableRaw<MapFieldBase>(message, field)
+ ->MutableRepeatedField()
+ ->ReleaseLast<GenericTypeHandler<Message> >();
+ } else {
+ return MutableRaw<RepeatedPtrFieldBase>(message, field)
->ReleaseLast<GenericTypeHandler<Message> >();
+ }
}
}
@@ -859,8 +958,14 @@ void GeneratedMessageReflection::SwapElements(
case FieldDescriptor::CPPTYPE_STRING:
case FieldDescriptor::CPPTYPE_MESSAGE:
- MutableRaw<RepeatedPtrFieldBase>(message, field)
+ if (IsMapFieldInApi(field)) {
+ MutableRaw<MapFieldBase>(message, field)
+ ->MutableRepeatedField()
+ ->SwapElements(index1, index2);
+ } else {
+ MutableRaw<RepeatedPtrFieldBase>(message, field)
->SwapElements(index1, index2);
+ }
break;
}
}
@@ -994,8 +1099,11 @@ string GeneratedMessageReflection::GetString(
} else {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
- case FieldOptions::STRING:
- return *GetField<const string*>(message, field);
+ case FieldOptions::STRING: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ return GetField<ArenaStringPtr>(message, field).Get(default_ptr);
+ }
}
GOOGLE_LOG(FATAL) << "Can't get here.";
@@ -1013,8 +1121,11 @@ const string& GeneratedMessageReflection::GetStringReference(
} else {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
- case FieldOptions::STRING:
- return *GetField<const string*>(message, field);
+ case FieldOptions::STRING: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ return GetField<ArenaStringPtr>(message, field).Get(default_ptr);
+ }
}
GOOGLE_LOG(FATAL) << "Can't get here.";
@@ -1034,16 +1145,15 @@ void GeneratedMessageReflection::SetString(
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
case FieldOptions::STRING: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
if (field->containing_oneof() && !HasOneofField(*message, field)) {
ClearOneof(message, field->containing_oneof());
- *MutableField<string*>(message, field) = new string;
- }
- string** ptr = MutableField<string*>(message, field);
- if (*ptr == DefaultRaw<const string*>(field)) {
- *ptr = new string(value);
- } else {
- (*ptr)->assign(value);
+ MutableField<ArenaStringPtr>(message, field)->UnsafeSetDefault(
+ default_ptr);
}
+ MutableField<ArenaStringPtr>(message, field)->Set(default_ptr,
+ value, GetArena(message));
break;
}
}
@@ -1125,42 +1235,80 @@ void GeneratedMessageReflection::AddString(
// -------------------------------------------------------------------
+inline bool CreateUnknownEnumValues(const FileDescriptor* file) {
+ return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
+}
+
const EnumValueDescriptor* GeneratedMessageReflection::GetEnum(
const Message& message, const FieldDescriptor* field) const {
- USAGE_CHECK_ALL(GetEnum, SINGULAR, ENUM);
+ // Usage checked by GetEnumValue.
+ int value = GetEnumValue(message, field);
+ return field->enum_type()->FindValueByNumberCreatingIfUnknown(value);
+}
- int value;
+int GeneratedMessageReflection::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());
} else {
value = GetField<int>(message, field);
}
- const EnumValueDescriptor* result =
- field->enum_type()->FindValueByNumber(value);
- GOOGLE_CHECK(result != NULL) << "Value " << value << " is not valid for field "
- << field->full_name() << " of type "
- << field->enum_type()->full_name() << ".";
- return result;
+ return value;
}
void GeneratedMessageReflection::SetEnum(
Message* message, const FieldDescriptor* field,
const EnumValueDescriptor* value) const {
- USAGE_CHECK_ALL(SetEnum, SINGULAR, ENUM);
+ // 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 {
+ 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) {
+ GOOGLE_LOG(DFATAL) << "SetEnumValue accepts only valid integer values: value "
+ << value << " unexpected for field " << field->full_name();
+ // In production builds, DFATAL will not terminate the program, so we have
+ // to do something reasonable: just set the default value.
+ value = field->default_value_enum()->number();
+ }
+ }
+ SetEnumValueInternal(message, field, value);
+}
+void GeneratedMessageReflection::SetEnumValueInternal(
+ Message* message, const FieldDescriptor* field,
+ int value) const {
if (field->is_extension()) {
MutableExtensionSet(message)->SetEnum(field->number(), field->type(),
- value->number(), field);
+ value, field);
} else {
- SetField<int>(message, field, value->number());
+ SetField<int>(message, field, value);
}
}
const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum(
const Message& message, const FieldDescriptor* field, int index) const {
- USAGE_CHECK_ALL(GetRepeatedEnum, REPEATED, ENUM);
+ // 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 {
+ USAGE_CHECK_ALL(GetRepeatedEnumValue, REPEATED, ENUM);
int value;
if (field->is_extension()) {
@@ -1168,41 +1316,89 @@ const EnumValueDescriptor* GeneratedMessageReflection::GetRepeatedEnum(
} else {
value = GetRepeatedField<int>(message, field, index);
}
- const EnumValueDescriptor* result =
- field->enum_type()->FindValueByNumber(value);
- GOOGLE_CHECK(result != NULL) << "Value " << value << " is not valid for field "
- << field->full_name() << " of type "
- << field->enum_type()->full_name() << ".";
- return result;
+ return value;
}
void GeneratedMessageReflection::SetRepeatedEnum(
Message* message,
const FieldDescriptor* field, int index,
const EnumValueDescriptor* value) const {
- USAGE_CHECK_ALL(SetRepeatedEnum, REPEATED, ENUM);
+ // 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 {
+ 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) {
+ GOOGLE_LOG(DFATAL) << "SetRepeatedEnumValue accepts only valid integer values: "
+ << "value " << value << " unexpected for field "
+ << field->full_name();
+ // In production builds, DFATAL will not terminate the program, so we have
+ // to do something reasonable: just set the default value.
+ value = field->default_value_enum()->number();
+ }
+ }
+ SetRepeatedEnumValueInternal(message, field, index, value);
+}
+
+void GeneratedMessageReflection::SetRepeatedEnumValueInternal(
+ Message* message,
+ const FieldDescriptor* field, int index,
+ int value) const {
if (field->is_extension()) {
MutableExtensionSet(message)->SetRepeatedEnum(
- field->number(), index, value->number());
+ field->number(), index, value);
} else {
- SetRepeatedField<int>(message, field, index, value->number());
+ SetRepeatedField<int>(message, field, index, value);
}
}
void GeneratedMessageReflection::AddEnum(
Message* message, const FieldDescriptor* field,
const EnumValueDescriptor* value) const {
- USAGE_CHECK_ALL(AddEnum, REPEATED, ENUM);
+ // 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 {
+ 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) {
+ GOOGLE_LOG(DFATAL) << "AddEnumValue accepts only valid integer values: value "
+ << value << " unexpected for field " << field->full_name();
+ // In production builds, DFATAL will not terminate the program, so we have
+ // to do something reasonable: just set the default value.
+ value = field->default_value_enum()->number();
+ }
+ }
+ AddEnumValueInternal(message, field, value);
+}
+void GeneratedMessageReflection::AddEnumValueInternal(
+ Message* message, const FieldDescriptor* field,
+ int value) const {
if (field->is_extension()) {
MutableExtensionSet(message)->AddEnum(field->number(), field->type(),
field->options().packed(),
- value->number(), field);
+ value, field);
} else {
- AddField<int>(message, field, value->number());
+ AddField<int>(message, field, value);
}
}
@@ -1246,7 +1442,7 @@ Message* GeneratedMessageReflection::MutableMessage(
ClearOneof(message, field->containing_oneof());
result_holder = MutableField<Message*>(message, field);
const Message* default_message = DefaultRaw<const Message*>(field);
- *result_holder = default_message->New();
+ *result_holder = default_message->New(message->GetArena());
}
} else {
SetBit(message, field);
@@ -1254,14 +1450,14 @@ Message* GeneratedMessageReflection::MutableMessage(
if (*result_holder == NULL) {
const Message* default_message = DefaultRaw<const Message*>(field);
- *result_holder = default_message->New();
+ *result_holder = default_message->New(message->GetArena());
}
result = *result_holder;
return result;
}
}
-void GeneratedMessageReflection::SetAllocatedMessage(
+void GeneratedMessageReflection::UnsafeArenaSetAllocatedMessage(
Message* message,
Message* sub_message,
const FieldDescriptor* field) const {
@@ -1288,12 +1484,42 @@ void GeneratedMessageReflection::SetAllocatedMessage(
SetBit(message, field);
}
Message** sub_message_holder = MutableRaw<Message*>(message, field);
- delete *sub_message_holder;
+ if (GetArena(message) == NULL) {
+ delete *sub_message_holder;
+ }
*sub_message_holder = sub_message;
}
}
-Message* GeneratedMessageReflection::ReleaseMessage(
+void GeneratedMessageReflection::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 &&
+ sub_message->GetArena() != message->GetArena()) {
+ if (sub_message->GetArena() == NULL && message->GetArena() != NULL) {
+ // 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.
+ message->GetArena()->Own(sub_message);
+ UnsafeArenaSetAllocatedMessage(message, sub_message, field);
+ } else {
+ // Case 2: all other cases. We need to make a copy. MutableMessage() will
+ // either get the existing message object, or instantiate a new one as
+ // appropriate w.r.t. our arena.
+ Message* sub_message_copy = MutableMessage(message, field);
+ sub_message_copy->CopyFrom(*sub_message);
+ }
+ } else {
+ // Same memory ownership domains.
+ UnsafeArenaSetAllocatedMessage(message, sub_message, field);
+ }
+}
+
+Message* GeneratedMessageReflection::UnsafeArenaReleaseMessage(
Message* message,
const FieldDescriptor* field,
MessageFactory* factory) const {
@@ -1320,6 +1546,19 @@ Message* GeneratedMessageReflection::ReleaseMessage(
}
}
+Message* GeneratedMessageReflection::ReleaseMessage(
+ Message* message,
+ const FieldDescriptor* field,
+ MessageFactory* factory) const {
+ Message* released = UnsafeArenaReleaseMessage(message, field, factory);
+ if (GetArena(message) != NULL && released != NULL) {
+ Message* copy_from_arena = released->New();
+ copy_from_arena->CopyFrom(*released);
+ released = copy_from_arena;
+ }
+ return released;
+}
+
const Message& GeneratedMessageReflection::GetRepeatedMessage(
const Message& message, const FieldDescriptor* field, int index) const {
USAGE_CHECK_ALL(GetRepeatedMessage, REPEATED, MESSAGE);
@@ -1328,8 +1567,14 @@ const Message& GeneratedMessageReflection::GetRepeatedMessage(
return static_cast<const Message&>(
GetExtensionSet(message).GetRepeatedMessage(field->number(), index));
} else {
- return GetRaw<RepeatedPtrFieldBase>(message, field)
- .Get<GenericTypeHandler<Message> >(index);
+ if (IsMapFieldInApi(field)) {
+ return GetRaw<MapFieldBase>(message, field)
+ .GetRepeatedField()
+ .Get<GenericTypeHandler<Message> >(index);
+ } else {
+ return GetRaw<RepeatedPtrFieldBase>(message, field)
+ .Get<GenericTypeHandler<Message> >(index);
+ }
}
}
@@ -1342,8 +1587,14 @@ Message* GeneratedMessageReflection::MutableRepeatedMessage(
MutableExtensionSet(message)->MutableRepeatedMessage(
field->number(), index));
} else {
- return MutableRaw<RepeatedPtrFieldBase>(message, field)
+ if (IsMapFieldInApi(field)) {
+ return MutableRaw<MapFieldBase>(message, field)
+ ->MutableRepeatedField()
+ ->Mutable<GenericTypeHandler<Message> >(index);
+ } else {
+ return MutableRaw<RepeatedPtrFieldBase>(message, field)
->Mutable<GenericTypeHandler<Message> >(index);
+ }
}
}
@@ -1358,11 +1609,18 @@ Message* GeneratedMessageReflection::AddMessage(
return static_cast<Message*>(
MutableExtensionSet(message)->AddMessage(field, factory));
} else {
+ Message* result = NULL;
+
// We can't use AddField<Message>() because RepeatedPtrFieldBase doesn't
// know how to allocate one.
- RepeatedPtrFieldBase* repeated =
- MutableRaw<RepeatedPtrFieldBase>(message, field);
- Message* result = repeated->AddFromCleared<GenericTypeHandler<Message> >();
+ RepeatedPtrFieldBase* repeated = NULL;
+ if (IsMapFieldInApi(field)) {
+ repeated =
+ MutableRaw<MapFieldBase>(message, field)->MutableRepeatedField();
+ } else {
+ repeated = MutableRaw<RepeatedPtrFieldBase>(message, field);
+ }
+ result = repeated->AddFromCleared<GenericTypeHandler<Message> >();
if (result == NULL) {
// We must allocate a new object.
const Message* prototype;
@@ -1371,9 +1629,13 @@ Message* GeneratedMessageReflection::AddMessage(
} else {
prototype = &repeated->Get<GenericTypeHandler<Message> >(0);
}
- result = prototype->New();
- repeated->AddAllocated<GenericTypeHandler<Message> >(result);
+ result = prototype->New(message->GetArena());
+ // We can guarantee here that repeated and result are either both heap
+ // allocated or arena owned. So it is safe to call the unsafe version
+ // of AddAllocated.
+ repeated->UnsafeArenaAddAllocated<GenericTypeHandler<Message> >(result);
}
+
return result;
}
}
@@ -1390,11 +1652,18 @@ void* GeneratedMessageReflection::MutableRawRepeatedField(
GOOGLE_CHECK_EQ(field->options().ctype(), ctype) << "subtype mismatch";
if (desc != NULL)
GOOGLE_CHECK_EQ(field->message_type(), desc) << "wrong submessage type";
- if (field->is_extension())
+ if (field->is_extension()) {
return MutableExtensionSet(message)->MutableRawRepeatedField(
field->number(), field->type(), field->is_packed(), field);
- else
+ } else {
+ // Trigger transform for MapField
+ if (IsMapFieldInApi(field)) {
+ return reinterpret_cast<MapFieldBase*>(reinterpret_cast<uint8*>(message) +
+ offsets_[field->index()])
+ ->MutableRepeatedField();
+ }
return reinterpret_cast<uint8*>(message) + offsets_[field->index()];
+ }
}
const FieldDescriptor* GeneratedMessageReflection::GetOneofFieldDescriptor(
@@ -1445,6 +1714,10 @@ const FieldDescriptor* GeneratedMessageReflection::FindKnownExtensionByNumber(
return descriptor_pool_->FindExtensionByNumber(descriptor_, number);
}
+bool GeneratedMessageReflection::SupportsUnknownEnumValues() const {
+ return CreateUnknownEnumValues(descriptor_->file());
+}
+
// ===================================================================
// Some private helpers.
@@ -1487,11 +1760,17 @@ inline const Type& GeneratedMessageReflection::DefaultRaw(
inline const uint32* GeneratedMessageReflection::GetHasBits(
const Message& message) const {
+ if (has_bits_offset_ == -1) { // proto3 with no has-bits.
+ return NULL;
+ }
const void* ptr = reinterpret_cast<const uint8*>(&message) + has_bits_offset_;
return reinterpret_cast<const uint32*>(ptr);
}
inline uint32* GeneratedMessageReflection::MutableHasBits(
Message* message) const {
+ if (has_bits_offset_ == -1) {
+ return NULL;
+ }
void* ptr = reinterpret_cast<uint8*>(message) + has_bits_offset_;
return reinterpret_cast<uint32*>(ptr);
}
@@ -1525,25 +1804,125 @@ inline ExtensionSet* GeneratedMessageReflection::MutableExtensionSet(
return reinterpret_cast<ExtensionSet*>(ptr);
}
+inline Arena* GeneratedMessageReflection::GetArena(Message* message) const {
+ if (arena_offset_ == kNoArenaPointer) {
+ return NULL;
+ }
+
+ if (unknown_fields_offset_ == kUnknownFieldSetInMetadata) {
+ // zero-overhead arena pointer overloading UnknownFields
+ return GetInternalMetadataWithArena(*message).arena();
+ }
+
+ // Baseline case: message class has a dedicated arena pointer.
+ void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_;
+ return *reinterpret_cast<Arena**>(ptr);
+}
+
+inline const InternalMetadataWithArena&
+GeneratedMessageReflection::GetInternalMetadataWithArena(
+ const Message& message) const {
+ const void* ptr = reinterpret_cast<const uint8*>(&message) + arena_offset_;
+ return *reinterpret_cast<const InternalMetadataWithArena*>(ptr);
+}
+
+inline InternalMetadataWithArena*
+GeneratedMessageReflection::MutableInternalMetadataWithArena(
+ Message* message) const {
+ void* ptr = reinterpret_cast<uint8*>(message) + arena_offset_;
+ return reinterpret_cast<InternalMetadataWithArena*>(ptr);
+}
+
+inline bool
+GeneratedMessageReflection::GetIsDefaultInstance(
+ const Message& message) const {
+ if (is_default_instance_offset_ == kHasNoDefaultInstanceField) {
+ return false;
+ }
+ const void* ptr = reinterpret_cast<const uint8*>(&message) +
+ is_default_instance_offset_;
+ return *reinterpret_cast<const bool*>(ptr);
+}
+
// Simple accessors for manipulating has_bits_.
inline bool GeneratedMessageReflection::HasBit(
const Message& message, const FieldDescriptor* field) const {
+ if (has_bits_offset_ == -1) {
+ // proto3: no has-bits. All fields present except messages, which are
+ // present only if their message-field pointer is non-NULL.
+ if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
+ return !GetIsDefaultInstance(message) &&
+ GetRaw<const Message*>(message, field) != NULL;
+ } 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.:
+ // 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
+ // (which uses HasField()) needs to be consistent with this.
+ switch (field->cpp_type()) {
+ case FieldDescriptor::CPPTYPE_STRING:
+ switch (field->options().ctype()) {
+ default: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ return GetField<ArenaStringPtr>(message, field).Get(
+ default_ptr).size() > 0;
+ }
+ }
+ return false;
+ case FieldDescriptor::CPPTYPE_BOOL:
+ return GetRaw<bool>(message, field) != false;
+ case FieldDescriptor::CPPTYPE_INT32:
+ return GetRaw<int32>(message, field) != 0;
+ case FieldDescriptor::CPPTYPE_INT64:
+ return GetRaw<int64>(message, field) != 0;
+ case FieldDescriptor::CPPTYPE_UINT32:
+ return GetRaw<uint32>(message, field) != 0;
+ case FieldDescriptor::CPPTYPE_UINT64:
+ return GetRaw<uint64>(message, field) != 0;
+ case FieldDescriptor::CPPTYPE_FLOAT:
+ return GetRaw<float>(message, field) != 0.0;
+ case FieldDescriptor::CPPTYPE_DOUBLE:
+ return GetRaw<double>(message, field) != 0.0;
+ case FieldDescriptor::CPPTYPE_ENUM:
+ return GetRaw<int>(message, field) != 0;
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ // handled above; avoid warning
+ GOOGLE_LOG(FATAL) << "Reached impossible case in HasBit().";
+ break;
+ }
+ }
+ }
return GetHasBits(message)[field->index() / 32] &
(1 << (field->index() % 32));
}
inline void GeneratedMessageReflection::SetBit(
Message* message, const FieldDescriptor* field) const {
+ if (has_bits_offset_ == -1) {
+ return;
+ }
MutableHasBits(message)[field->index() / 32] |= (1 << (field->index() % 32));
}
inline void GeneratedMessageReflection::ClearBit(
Message* message, const FieldDescriptor* field) const {
+ if (has_bits_offset_ == -1) {
+ return;
+ }
MutableHasBits(message)[field->index() / 32] &= ~(1 << (field->index() % 32));
}
inline void GeneratedMessageReflection::SwapBit(
Message* message1, Message* message2, const FieldDescriptor* field) const {
+ if (has_bits_offset_ == -1) {
+ return;
+ }
bool temp_has_bit = HasBit(*message1, field);
if (HasBit(*message2, field)) {
SetBit(message1, field);
@@ -1591,9 +1970,13 @@ inline void GeneratedMessageReflection::ClearOneof(
case FieldDescriptor::CPPTYPE_STRING: {
switch (field->options().ctype()) {
default: // TODO(kenton): Support other string reps.
- case FieldOptions::STRING:
- delete *MutableRaw<string*>(message, field);
+ case FieldOptions::STRING: {
+ const string* default_ptr =
+ &DefaultRaw<ArenaStringPtr>(field).Get(NULL);
+ MutableField<ArenaStringPtr>(message, field)->
+ Destroy(default_ptr, GetArena(message));
break;
+ }
}
break;
}
@@ -1678,6 +2061,84 @@ inline Type* GeneratedMessageReflection::AddField(
return repeated->Add();
}
+MessageFactory* GeneratedMessageReflection::GetMessageFactory() const {
+ return message_factory_;
+}
+
+void* GeneratedMessageReflection::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 &&
+ cpp_type == FieldDescriptor::CPPTYPE_INT32))
+ << "The type parameter T in RepeatedFieldRef<T> API doesn't match "
+ << "the actual field type (for enums T should be the generated enum "
+ << "type or int32).";
+ if (message_type != NULL) {
+ GOOGLE_CHECK_EQ(message_type, field->message_type());
+ }
+ if (field->is_extension()) {
+ return MutableExtensionSet(message)->MutableRawRepeatedField(
+ field->number(), field->type(), field->is_packed(), field);
+ } else {
+ return reinterpret_cast<uint8*>(message) + offsets_[field->index()];
+ }
+}
+
+GeneratedMessageReflection*
+GeneratedMessageReflection::NewGeneratedMessageReflection(
+ const Descriptor* descriptor,
+ const Message* default_instance,
+ const int offsets[],
+ int has_bits_offset,
+ int unknown_fields_offset,
+ int extensions_offset,
+ const void* default_oneof_instance,
+ int oneof_case_offset,
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset) {
+ return new GeneratedMessageReflection(descriptor,
+ default_instance,
+ offsets,
+ has_bits_offset,
+ unknown_fields_offset,
+ extensions_offset,
+ default_oneof_instance,
+ oneof_case_offset,
+ DescriptorPool::generated_pool(),
+ MessageFactory::generated_factory(),
+ object_size,
+ arena_offset,
+ is_default_instance_offset);
+}
+
+GeneratedMessageReflection*
+GeneratedMessageReflection::NewGeneratedMessageReflection(
+ const Descriptor* descriptor,
+ const Message* default_instance,
+ const int offsets[],
+ int has_bits_offset,
+ int unknown_fields_offset,
+ int extensions_offset,
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset) {
+ return new GeneratedMessageReflection(descriptor,
+ default_instance,
+ offsets,
+ has_bits_offset,
+ unknown_fields_offset,
+ extensions_offset,
+ DescriptorPool::generated_pool(),
+ MessageFactory::generated_factory(),
+ object_size,
+ arena_offset,
+ is_default_instance_offset);
+}
+
} // namespace internal
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/generated_message_reflection.h b/src/google/protobuf/generated_message_reflection.h
index b6671ad0..4dddf6c7 100644
--- a/src/google/protobuf/generated_message_reflection.h
+++ b/src/google/protobuf/generated_message_reflection.h
@@ -45,6 +45,7 @@
// is released to components.
#include <google/protobuf/generated_enum_reflection.h>
#include <google/protobuf/message.h>
+#include <google/protobuf/metadata.h>
#include <google/protobuf/unknown_field_set.h>
@@ -134,7 +135,9 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
int extensions_offset,
const DescriptorPool* pool,
MessageFactory* factory,
- int object_size);
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset = -1);
// Similar with the construction above. Call this construction if the
// message has oneof definition.
@@ -170,9 +173,39 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
int oneof_case_offset,
const DescriptorPool* pool,
MessageFactory* factory,
- int object_size);
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset = -1);
~GeneratedMessageReflection();
+ // Shorter-to-call helpers for the above two constructions that work if the
+ // pool and factory are the usual, namely, DescriptorPool::generated_pool()
+ // and MessageFactory::generated_factory().
+
+ static GeneratedMessageReflection* NewGeneratedMessageReflection(
+ const Descriptor* descriptor,
+ const Message* default_instance,
+ const int offsets[],
+ int has_bits_offset,
+ int unknown_fields_offset,
+ int extensions_offset,
+ const void* default_oneof_instance,
+ int oneof_case_offset,
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset = -1);
+
+ static GeneratedMessageReflection* NewGeneratedMessageReflection(
+ const Descriptor* descriptor,
+ const Message* default_instance,
+ const int offsets[],
+ int has_bits_offset,
+ int unknown_fields_offset,
+ int extensions_offset,
+ int object_size,
+ int arena_offset,
+ int is_default_instance_offset = -1);
+
// implements Reflection -------------------------------------------
const UnknownFieldSet& GetUnknownFields(const Message& message) const;
@@ -217,6 +250,8 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
string* scratch) const;
const EnumValueDescriptor* GetEnum(const Message& message,
const FieldDescriptor* field) const;
+ int GetEnumValue(const Message& message,
+ const FieldDescriptor* field) const;
const Message& GetMessage(const Message& message,
const FieldDescriptor* field,
MessageFactory* factory = NULL) const;
@@ -245,6 +280,8 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
const string& value) const;
void SetEnum (Message* message, const FieldDescriptor* field,
const EnumValueDescriptor* value) const;
+ void SetEnumValue(Message* message, const FieldDescriptor* field,
+ int value) const;
Message* MutableMessage(Message* message, const FieldDescriptor* field,
MessageFactory* factory = NULL) const;
void SetAllocatedMessage(Message* message,
@@ -275,6 +312,9 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
const FieldDescriptor* field,
int index) const;
+ int GetRepeatedEnumValue(const Message& message,
+ const FieldDescriptor* field,
+ int index) const;
const Message& GetRepeatedMessage(const Message& message,
const FieldDescriptor* field,
int index) const;
@@ -299,6 +339,8 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
const string& value) const;
void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
int index, const EnumValueDescriptor* value) const;
+ void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
+ int index, int value) const;
// Get a mutable pointer to a field with a message type.
Message* MutableRepeatedMessage(Message* message,
const FieldDescriptor* field,
@@ -323,17 +365,42 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
void AddEnum(Message* message,
const FieldDescriptor* field,
const EnumValueDescriptor* value) const;
+ void AddEnumValue(Message* message,
+ const FieldDescriptor* field,
+ int value) const;
Message* AddMessage(Message* message, const FieldDescriptor* field,
MessageFactory* factory = NULL) const;
const FieldDescriptor* FindKnownExtensionByName(const string& name) const;
const FieldDescriptor* FindKnownExtensionByNumber(int number) const;
+ bool SupportsUnknownEnumValues() const;
+
+ // 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:
virtual void* MutableRawRepeatedField(
Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
int ctype, const Descriptor* desc) const;
+ virtual MessageFactory* GetMessageFactory() const;
+
+ virtual void* RepeatedFieldData(
+ Message* message, const FieldDescriptor* field,
+ FieldDescriptor::CppType cpp_type,
+ const Descriptor* message_type) const;
+
private:
friend class GeneratedMessage;
@@ -350,8 +417,12 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
int oneof_case_offset_;
int unknown_fields_offset_;
int extensions_offset_;
+ int arena_offset_;
+ int is_default_instance_offset_;
int object_size_;
+ static const int kHasNoDefaultInstanceField = -1;
+
const DescriptorPool* descriptor_pool_;
MessageFactory* message_factory_;
@@ -376,6 +447,13 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
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 internal::InternalMetadataWithArena&
+ GetInternalMetadataWithArena(const Message& message) const;
+ inline internal::InternalMetadataWithArena*
+ MutableInternalMetadataWithArena(Message* message) const;
+
+ inline bool GetIsDefaultInstance(const Message& message) const;
inline bool HasBit(const Message& message,
const FieldDescriptor* field) const;
@@ -438,6 +516,28 @@ class LIBPROTOBUF_EXPORT GeneratedMessageReflection : public Reflection {
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;
+
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection);
};
diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc
index 0c20d81c..53cae8b5 100644
--- a/src/google/protobuf/generated_message_util.cc
+++ b/src/google/protobuf/generated_message_util.cc
@@ -36,6 +36,7 @@
#include <limits>
+
namespace google {
namespace protobuf {
namespace internal {
diff --git a/src/google/protobuf/io/coded_stream.cc b/src/google/protobuf/io/coded_stream.cc
index 53449755..df88205c 100644
--- a/src/google/protobuf/io/coded_stream.cc
+++ b/src/google/protobuf/io/coded_stream.cc
@@ -40,8 +40,10 @@
#include <google/protobuf/io/coded_stream_inl.h>
#include <algorithm>
+#include <utility>
#include <limits.h>
#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/arena.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stl_util.h>
@@ -149,6 +151,19 @@ void CodedInputStream::PopLimit(Limit limit) {
legitimate_message_end_ = false;
}
+std::pair<CodedInputStream::Limit, int>
+CodedInputStream::IncrementRecursionDepthAndPushLimit(int byte_limit) {
+ return make_pair(PushLimit(byte_limit), --recursion_budget_);
+}
+
+bool CodedInputStream::DecrementRecursionDepthAndPopLimit(Limit limit) {
+ bool result = ConsumedEntireMessage();
+ PopLimit(limit);
+ GOOGLE_DCHECK_LT(recursion_budget_, recursion_limit_);
+ ++recursion_budget_;
+ return result;
+}
+
int CodedInputStream::BytesUntilLimit() const {
if (current_limit_ == INT_MAX) return -1;
int current_position = CurrentPosition();
diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h
index 81fabb1d..978cc19d 100644
--- a/src/google/protobuf/io/coded_stream.h
+++ b/src/google/protobuf/io/coded_stream.h
@@ -49,7 +49,7 @@
// // to identify the file type, then write a length-delimited string. The
// // string is composed of a varint giving the length followed by the raw
// // bytes.
-// int fd = open("myfile", O_WRONLY);
+// int fd = open("myfile", O_CREAT | O_WRONLY);
// ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
// CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
//
@@ -110,6 +110,7 @@
#define GOOGLE_PROTOBUF_IO_CODED_STREAM_H__
#include <string>
+#include <utility>
#ifdef _MSC_VER
#if defined(_M_IX86) && \
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
@@ -122,15 +123,16 @@
#endif
#else
#include <sys/param.h> // __BYTE_ORDER
- #if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN && \
+ #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 <google/protobuf/stubs/common.h>
-
namespace google {
+
namespace protobuf {
class DescriptorPool;
@@ -388,6 +390,23 @@ class LIBPROTOBUF_EXPORT CodedInputStream {
// Decrements the recursion depth.
void DecrementRecursionDepth();
+ // Shorthand for make_pair(PushLimit(byte_limit), --recursion_budget_).
+ // Using this can reduce code size and complexity in some cases. The caller
+ // is expected to check that the second part of the result is non-negative (to
+ // bail out if the depth of recursion is too high) and, if all is well, to
+ // later pass the first part of the result to PopLimit() or similar.
+ std::pair<CodedInputStream::Limit, int> IncrementRecursionDepthAndPushLimit(
+ int byte_limit);
+
+ // Helper that is equivalent to: {
+ // bool result = ConsumedEntireMessage();
+ // PopLimit(limit);
+ // DecrementRecursionDepth();
+ // return result; }
+ // Using this can reduce code size and complexity in some cases.
+ // Do not use unless the current recursion depth is greater than zero.
+ bool DecrementRecursionDepthAndPopLimit(Limit limit);
+
// Extension Registry ----------------------------------------------
// ADVANCED USAGE: 99.9% of people can ignore this section.
//
@@ -470,9 +489,9 @@ class LIBPROTOBUF_EXPORT CodedInputStream {
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodedInputStream);
- ZeroCopyInputStream* input_;
const uint8* buffer_;
const uint8* buffer_end_; // pointer to the end of the buffer.
+ ZeroCopyInputStream* input_;
int total_bytes_read_; // total bytes read from input_, including
// the current buffer
@@ -513,9 +532,10 @@ class LIBPROTOBUF_EXPORT CodedInputStream {
// If -2: Internal: Limit has been reached, print full size when destructing.
int total_bytes_warning_threshold_;
- // Current recursion depth, controlled by IncrementRecursionDepth() and
- // DecrementRecursionDepth().
- int recursion_depth_;
+ // Current recursion budget, controlled by IncrementRecursionDepth() and
+ // similar. Starts at recursion_limit_ and goes down: if this reaches
+ // -1 we are over budget.
+ int recursion_budget_;
// Recursion depth limit, set by SetRecursionLimit().
int recursion_limit_;
@@ -1132,16 +1152,17 @@ inline void CodedOutputStream::Advance(int amount) {
}
inline void CodedInputStream::SetRecursionLimit(int limit) {
+ recursion_budget_ += limit - recursion_limit_;
recursion_limit_ = limit;
}
inline bool CodedInputStream::IncrementRecursionDepth() {
- ++recursion_depth_;
- return recursion_depth_ <= recursion_limit_;
+ --recursion_budget_;
+ return recursion_budget_ >= 0;
}
inline void CodedInputStream::DecrementRecursionDepth() {
- if (recursion_depth_ > 0) --recursion_depth_;
+ if (recursion_budget_ < recursion_limit_) ++recursion_budget_;
}
inline void CodedInputStream::SetExtensionRegistry(const DescriptorPool* pool,
@@ -1163,9 +1184,9 @@ inline int CodedInputStream::BufferSize() const {
}
inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
- : input_(input),
- buffer_(NULL),
+ : buffer_(NULL),
buffer_end_(NULL),
+ input_(input),
total_bytes_read_(0),
overflow_bytes_(0),
last_tag_(0),
@@ -1175,7 +1196,7 @@ inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
buffer_size_after_limit_(0),
total_bytes_limit_(kDefaultTotalBytesLimit),
total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
- recursion_depth_(0),
+ recursion_budget_(default_recursion_limit_),
recursion_limit_(default_recursion_limit_),
extension_pool_(NULL),
extension_factory_(NULL) {
@@ -1184,9 +1205,9 @@ inline CodedInputStream::CodedInputStream(ZeroCopyInputStream* input)
}
inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
- : input_(NULL),
- buffer_(buffer),
+ : buffer_(buffer),
buffer_end_(buffer + size),
+ input_(NULL),
total_bytes_read_(size),
overflow_bytes_(0),
last_tag_(0),
@@ -1196,7 +1217,7 @@ inline CodedInputStream::CodedInputStream(const uint8* buffer, int size)
buffer_size_after_limit_(0),
total_bytes_limit_(kDefaultTotalBytesLimit),
total_bytes_warning_threshold_(kDefaultTotalBytesWarningThreshold),
- recursion_depth_(0),
+ recursion_budget_(default_recursion_limit_),
recursion_limit_(default_recursion_limit_),
extension_pool_(NULL),
extension_factory_(NULL) {
diff --git a/src/google/protobuf/io/coded_stream_inl.h b/src/google/protobuf/io/coded_stream_inl.h
index 88c14cab..cd8d1746 100644
--- a/src/google/protobuf/io/coded_stream_inl.h
+++ b/src/google/protobuf/io/coded_stream_inl.h
@@ -36,6 +36,7 @@
#ifndef GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__
#define GOOGLE_PROTOBUF_IO_CODED_STREAM_INL_H__
+#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <string>
@@ -51,10 +52,12 @@ inline bool CodedInputStream::InternalReadStringInline(string* buffer,
if (BufferSize() >= size) {
STLStringResizeUninitialized(buffer, size);
- // When buffer is empty, string_as_array(buffer) will return NULL but memcpy
- // requires non-NULL pointers even when size is 0. Hench this check.
- if (size > 0) {
- memcpy(mutable_string_data(buffer), buffer_, size);
+ std::pair<char*, bool> z = as_string_data(buffer);
+ if (z.second) {
+ // Oddly enough, memcpy() requires its first two args to be non-NULL even
+ // if we copy 0 bytes. So, we have ensured that z.first is non-NULL here.
+ GOOGLE_DCHECK(z.first != NULL);
+ memcpy(z.first, buffer_, size);
Advance(size);
}
return true;
diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc
index f4cb5ea1..bbe5e399 100644
--- a/src/google/protobuf/io/coded_stream_unittest.cc
+++ b/src/google/protobuf/io/coded_stream_unittest.cc
@@ -144,6 +144,7 @@ uint8 CodedStreamTest::buffer_[CodedStreamTest::kBufferSize];
// checks.
const int kBlockSizes[] = {1, 2, 3, 5, 7, 13, 32, 1024};
+
// -------------------------------------------------------------------
// Varint tests.
diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc
index ee286961..e6037863 100644
--- a/src/google/protobuf/io/gzip_stream.cc
+++ b/src/google/protobuf/io/gzip_stream.cc
@@ -48,7 +48,7 @@ static const int kDefaultBufferSize = 65536;
GzipInputStream::GzipInputStream(
ZeroCopyInputStream* sub_stream, Format format, int buffer_size)
- : format_(format), sub_stream_(sub_stream), zerror_(Z_OK) {
+ : format_(format), sub_stream_(sub_stream), zerror_(Z_OK), byte_count_(0) {
zcontext_.zalloc = Z_NULL;
zcontext_.zfree = Z_NULL;
zcontext_.opaque = Z_NULL;
@@ -134,6 +134,7 @@ bool GzipInputStream::Next(const void** data, int* size) {
if (zcontext_.next_out != NULL) {
// sub_stream_ may have concatenated streams to follow
zerror_ = inflateEnd(&zcontext_);
+ byte_count_ += zcontext_.total_out;
if (zerror_ != Z_OK) {
return false;
}
@@ -178,8 +179,12 @@ bool GzipInputStream::Skip(int count) {
return ok;
}
int64 GzipInputStream::ByteCount() const {
- return zcontext_.total_out +
- (((uintptr_t)zcontext_.next_out) - ((uintptr_t)output_position_));
+ int64 ret = byte_count_ + zcontext_.total_out;
+ if (zcontext_.next_out != NULL && output_position_ != NULL) {
+ ret += reinterpret_cast<uintptr_t>(zcontext_.next_out) -
+ reinterpret_cast<uintptr_t>(output_position_);
+ }
+ return ret;
}
// =========================================================================
diff --git a/src/google/protobuf/io/gzip_stream.h b/src/google/protobuf/io/gzip_stream.h
index c7ccc260..82445000 100644
--- a/src/google/protobuf/io/gzip_stream.h
+++ b/src/google/protobuf/io/gzip_stream.h
@@ -99,6 +99,7 @@ class LIBPROTOBUF_EXPORT GzipInputStream : public ZeroCopyInputStream {
void* output_buffer_;
void* output_position_;
size_t output_buffer_length_;
+ int64 byte_count_;
int Inflate(int flush);
void DoNextOutput(const void** data, int* size);
diff --git a/src/google/protobuf/io/printer.cc b/src/google/protobuf/io/printer.cc
index c8df4177..e621ba1d 100644
--- a/src/google/protobuf/io/printer.cc
+++ b/src/google/protobuf/io/printer.cc
@@ -142,6 +142,19 @@ void Printer::Print(const char* text,
Print(vars, text);
}
+void Printer::Print(const char* text,
+ const char* variable1, const string& value1,
+ const char* variable2, const string& value2,
+ const char* variable3, const string& value3,
+ const char* variable4, const string& value4) {
+ map<string, string> vars;
+ vars[variable1] = value1;
+ vars[variable2] = value2;
+ vars[variable3] = value3;
+ vars[variable4] = value4;
+ Print(vars, text);
+}
+
void Printer::Indent() {
indent_ += " ";
}
diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h
index f06cbf2f..92ce3409 100644
--- a/src/google/protobuf/io/printer.h
+++ b/src/google/protobuf/io/printer.h
@@ -86,6 +86,11 @@ class LIBPROTOBUF_EXPORT Printer {
void Print(const char* text, const char* variable1, const string& value1,
const char* variable2, const string& value2,
const char* variable3, const string& value3);
+ // Like the first Print(), except the substitutions are given as parameters.
+ void Print(const char* text, const char* variable1, const string& value1,
+ const char* variable2, const string& value2,
+ const char* variable3, const string& value3,
+ const char* variable4, const string& value4);
// TODO(kenton): Overloaded versions with more variables? Three seems
// to be enough.
diff --git a/src/google/protobuf/io/tokenizer.h b/src/google/protobuf/io/tokenizer.h
index 8c6220a1..98576f56 100644
--- a/src/google/protobuf/io/tokenizer.h
+++ b/src/google/protobuf/io/tokenizer.h
@@ -350,7 +350,7 @@ class LIBPROTOBUF_EXPORT Tokenizer {
// -----------------------------------------------------------------
// These helper methods make the parsing code more readable. The
- // "character classes" refered to are defined at the top of the .cc file.
+ // "character classes" referred to are defined at the top of the .cc file.
// Basically it is a C++ class with one method:
// static bool InClass(char c);
// The method returns true if c is a member of this "class", like "Letter"
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 58aff0e2..97b73b88 100644
--- a/src/google/protobuf/io/zero_copy_stream_impl_lite.cc
+++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.cc
@@ -37,6 +37,7 @@
#include <algorithm>
#include <limits>
+#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stl_util.h>
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 e18da72c..3e25edfa 100644
--- a/src/google/protobuf/io/zero_copy_stream_impl_lite.h
+++ b/src/google/protobuf/io/zero_copy_stream_impl_lite.h
@@ -127,8 +127,10 @@ class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
public:
// Create a StringOutputStream which appends bytes to the given string.
- // The string remains property of the caller, but it MUST NOT be accessed
- // in any way until the stream is destroyed.
+ // The string remains property of the caller, but it is mutated in arbitrary
+ // ways and MUST NOT be accessed in any way until you're done with the
+ // stream. Either be sure there's no further usage, or (safest) destroy the
+ // stream before using the contents.
//
// Hint: If you call target->reserve(n) before creating the stream,
// the first call to Next() will return at least n bytes of buffer
@@ -334,6 +336,18 @@ class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStrea
// ===================================================================
+// mutable_string_data() and as_string_data() are workarounds to improve
+// the performance of writing new data to an existing string. Unfortunately
+// the methods provided by the string class are suboptimal, and using memcpy()
+// is mildly annoying because it requires its pointer args to be non-NULL even
+// if we ask it to copy 0 bytes. Furthermore, string_as_array() has the
+// property that it always returns NULL if its arg is the empty string, exactly
+// what we want to avoid if we're using it in conjunction with memcpy()!
+// With C++11, the desired memcpy() boils down to memcpy(..., &(*s)[0], size),
+// where s is a string*. Without C++11, &(*s)[0] is not guaranteed to be safe,
+// so we use string_as_array(), and live with the extra logic that tests whether
+// *s is empty.
+
// Return a pointer to mutable characters underlying the given string. The
// return value is valid until the next time the string is resized. We
// trust the caller to treat the return value as an array of length s->size().
@@ -347,6 +361,19 @@ inline char* mutable_string_data(string* 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<char*, bool> as_string_data(string* s) {
+ char *p = mutable_string_data(s);
+#ifdef LANG_CXX11
+ return make_pair(p, true);
+#else
+ return make_pair(p, p != NULL);
+#endif
+}
+
} // namespace io
} // namespace protobuf
diff --git a/src/google/protobuf/io/zero_copy_stream_unittest.cc b/src/google/protobuf/io/zero_copy_stream_unittest.cc
index bf978cc8..dd3d1285 100644
--- a/src/google/protobuf/io/zero_copy_stream_unittest.cc
+++ b/src/google/protobuf/io/zero_copy_stream_unittest.cc
@@ -656,6 +656,43 @@ TEST_F(IoTest, TwoSessionWriteGzip) {
delete [] temp_buffer;
delete [] buffer;
}
+
+TEST_F(IoTest, GzipInputByteCountAfterClosed) {
+ string golden = "abcdefghijklmnopqrstuvwxyz";
+ string compressed = Compress(golden, GzipOutputStream::Options());
+
+ for (int i = 0; i < kBlockSizeCount; i++) {
+ ArrayInputStream arr_input(compressed.data(), compressed.size(),
+ kBlockSizes[i]);
+ GzipInputStream gz_input(&arr_input);
+ const void* buffer;
+ int size;
+ while (gz_input.Next(&buffer, &size)) {
+ EXPECT_LE(gz_input.ByteCount(), golden.size());
+ }
+ EXPECT_EQ(golden.size(), gz_input.ByteCount());
+ }
+}
+
+TEST_F(IoTest, GzipInputByteCountAfterClosedConcatenatedStreams) {
+ string golden1 = "abcdefghijklmnopqrstuvwxyz";
+ 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());
+
+ for (int i = 0; i < kBlockSizeCount; i++) {
+ ArrayInputStream arr_input(compressed.data(), compressed.size(),
+ kBlockSizes[i]);
+ GzipInputStream gz_input(&arr_input);
+ const void* buffer;
+ int size;
+ while (gz_input.Next(&buffer, &size)) {
+ EXPECT_LE(gz_input.ByteCount(), total_size);
+ }
+ EXPECT_EQ(total_size, gz_input.ByteCount());
+ }
+}
#endif
// There is no string input, only string output. Also, it doesn't support
diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h
new file mode 100644
index 00000000..6d8a9d03
--- /dev/null
+++ b/src/google/protobuf/map.h
@@ -0,0 +1,311 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_MAP_H__
+#define GOOGLE_PROTOBUF_MAP_H__
+
+#include <iterator>
+#include <google/protobuf/stubs/hash.h>
+
+#include <google/protobuf/map_type_handler.h>
+
+namespace google {
+namespace protobuf {
+
+template <typename Key, typename T>
+class Map;
+
+template <typename Enum> struct is_proto_enum;
+
+namespace internal {
+template <typename K, typename V, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+class MapField;
+} // namespace internal
+
+// This is the class for google::protobuf::Map's internal value_type. Instead of using
+// std::pair as value_type, we use this class which provides us more control of
+// its process of construction and destruction.
+template <typename Key, typename T>
+class MapPair {
+ public:
+ typedef const Key first_type;
+ typedef T second_type;
+
+ MapPair(const Key& other_first, const T& other_second)
+ : first(other_first), second(other_second) {}
+
+ explicit MapPair(const Key& other_first) : first(other_first), second() {}
+
+ MapPair(const MapPair& other)
+ : first(other.first), second(other.second) {}
+
+ ~MapPair() {}
+
+ // Implicitly convertible to std::pair.
+ operator std::pair<const Key, T>() const {
+ return std::pair<const Key, T>(first, second);
+ }
+
+ const Key first;
+ T second;
+
+ private:
+ friend class Map<Key, T>;
+};
+
+// google::protobuf::Map is an associative container type used to store protobuf map
+// fields. Its interface is similar to std::unordered_map. Users should use this
+// interface directly to visit or change map fields.
+template <typename Key, typename T>
+class Map {
+ typedef internal::MapCppTypeHandler<T> ValueTypeHandler;
+
+ public:
+ typedef Key key_type;
+ typedef T mapped_type;
+ typedef MapPair<Key, T> value_type;
+
+ typedef value_type* pointer;
+ typedef const value_type* const_pointer;
+ typedef value_type& reference;
+ typedef const value_type& const_reference;
+
+ typedef size_t size_type;
+ typedef hash<Key> hasher;
+
+ Map() : default_enum_value_(0) {}
+
+ Map(const Map& other) {
+ insert(other.begin(), other.end());
+ }
+
+ ~Map() { clear(); }
+
+ // Iterators
+ class const_iterator
+ : public std::iterator<std::forward_iterator_tag, value_type, ptrdiff_t,
+ const value_type*, const value_type&> {
+ typedef typename hash_map<Key, value_type*>::const_iterator InnerIt;
+
+ public:
+ const_iterator() {}
+ explicit const_iterator(const InnerIt& it) : it_(it) {}
+
+ const_reference operator*() const { return *it_->second; }
+ const_pointer operator->() const { return it_->second; }
+
+ const_iterator& operator++() {
+ ++it_;
+ return *this;
+ }
+ const_iterator operator++(int) { return const_iterator(it_++); }
+
+ friend bool operator==(const const_iterator& a, const const_iterator& b) {
+ return a.it_ == b.it_;
+ }
+ friend bool operator!=(const const_iterator& a, const const_iterator& b) {
+ return a.it_ != b.it_;
+ }
+
+ private:
+ InnerIt it_;
+ };
+
+ class iterator : public std::iterator<std::forward_iterator_tag, value_type> {
+ typedef typename hash_map<Key, value_type*>::iterator InnerIt;
+
+ public:
+ iterator() {}
+ explicit iterator(const InnerIt& it) : it_(it) {}
+
+ reference operator*() const { return *it_->second; }
+ pointer operator->() const { return it_->second; }
+
+ iterator& operator++() {
+ ++it_;
+ return *this;
+ }
+ iterator operator++(int) { return iterator(it_++); }
+
+ // Implicitly convertible to const_iterator.
+ operator const_iterator() const { return const_iterator(it_); }
+
+ friend bool operator==(const iterator& a, const iterator& b) {
+ return a.it_ == b.it_;
+ }
+ friend bool operator!=(const iterator& a, const iterator& b) {
+ return a.it_ != b.it_;
+ }
+
+ private:
+ friend class Map;
+ InnerIt it_;
+ };
+
+ iterator begin() { return iterator(elements_.begin()); }
+ iterator end() { return iterator(elements_.end()); }
+ const_iterator begin() const { return const_iterator(elements_.begin()); }
+ const_iterator end() const { return const_iterator(elements_.end()); }
+ const_iterator cbegin() const { return begin(); }
+ const_iterator cend() const { return end(); }
+
+ // Capacity
+ size_type size() const { return elements_.size(); }
+ bool empty() const { return elements_.empty(); }
+
+ // Element access
+ T& operator[](const key_type& key) {
+ value_type** value = &elements_[key];
+ if (*value == NULL) {
+ *value = new value_type(key);
+ internal::MapValueInitializer<google::protobuf::is_proto_enum<T>::value,
+ T>::Initialize((*value)->second,
+ default_enum_value_);
+ }
+ return (*value)->second;
+ }
+ const T& at(const key_type& key) const {
+ const_iterator it = find(key);
+ GOOGLE_CHECK(it != end());
+ return it->second;
+ }
+ T& at(const key_type& key) {
+ iterator it = find(key);
+ GOOGLE_CHECK(it != end());
+ return it->second;
+ }
+
+ // Lookup
+ size_type count(const key_type& key) const {
+ return elements_.count(key);
+ }
+ const_iterator find(const key_type& key) const {
+ return const_iterator(elements_.find(key));
+ }
+ iterator find(const key_type& key) {
+ return iterator(elements_.find(key));
+ }
+ std::pair<const_iterator, const_iterator> equal_range(
+ const key_type& key) const {
+ const_iterator it = find(key);
+ if (it == end()) {
+ return std::pair<const_iterator, const_iterator>(it, it);
+ } else {
+ const_iterator begin = it++;
+ return std::pair<const_iterator, const_iterator>(begin, it);
+ }
+ }
+ std::pair<iterator, iterator> equal_range(const key_type& key) {
+ iterator it = find(key);
+ if (it == end()) {
+ return std::pair<iterator, iterator>(it, it);
+ } else {
+ iterator begin = it++;
+ return std::pair<iterator, iterator>(begin, it);
+ }
+ }
+
+ // insert
+ std::pair<iterator, bool> insert(const value_type& value) {
+ iterator it = find(value.first);
+ if (it != end()) {
+ return std::pair<iterator, bool>(it, false);
+ } else {
+ return std::pair<iterator, bool>(
+ iterator(elements_.insert(std::pair<Key, value_type*>(
+ value.first, new value_type(value))).first), true);
+ }
+ }
+ template <class InputIt>
+ void insert(InputIt first, InputIt last) {
+ for (InputIt it = first; it != last; ++it) {
+ iterator exist_it = find(it->first);
+ if (exist_it == end()) {
+ operator[](it->first) = it->second;
+ }
+ }
+ }
+
+ // Erase
+ size_type erase(const key_type& key) {
+ typename hash_map<Key, value_type*>::iterator it = elements_.find(key);
+ if (it == elements_.end()) {
+ return 0;
+ } else {
+ delete it->second;
+ elements_.erase(it);
+ return 1;
+ }
+ }
+ void erase(iterator pos) {
+ delete pos.it_->second;
+ elements_.erase(pos.it_);
+ }
+ void erase(iterator first, iterator last) {
+ for (iterator it = first; it != last;) {
+ delete it.it_->second;
+ elements_.erase((it++).it_);
+ }
+ }
+ void clear() {
+ for (iterator it = begin(); it != end(); ++it) {
+ delete it.it_->second;
+ }
+ elements_.clear();
+ }
+
+ // Assign
+ Map& operator=(const Map& other) {
+ if (this != &other) {
+ clear();
+ insert(other.begin(), other.end());
+ }
+ return *this;
+ }
+
+ private:
+ // Set default enum value only for proto2 map field whose value is enum type.
+ void SetDefaultEnumValue(int default_enum_value) {
+ default_enum_value_ = default_enum_value;
+ }
+
+ hash_map<Key, value_type*> elements_;
+ int default_enum_value_;
+
+ template <typename K, typename V, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum>
+ friend class internal::MapField;
+};
+
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_MAP_H__
diff --git a/src/google/protobuf/map_entry.h b/src/google/protobuf/map_entry.h
new file mode 100644
index 00000000..217b15f6
--- /dev/null
+++ b/src/google/protobuf/map_entry.h
@@ -0,0 +1,454 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_MAP_ENTRY_H__
+#define GOOGLE_PROTOBUF_MAP_ENTRY_H__
+
+#include <google/protobuf/reflection_ops.h>
+#include <google/protobuf/map_type_handler.h>
+#include <google/protobuf/wire_format_lite_inl.h>
+
+namespace google {
+namespace protobuf {
+class Arena;
+}
+
+namespace protobuf {
+namespace internal {
+
+// Register all MapEntry default instances so we can delete them in
+// ShutdownProtobufLibrary().
+void LIBPROTOBUF_EXPORT RegisterMapEntryDefaultInstance(MessageLite* default_instance);
+
+// This is the common base class for MapEntry. It is used by MapFieldBase in
+// reflection api, in which the static type of key and value is unknown.
+class LIBPROTOBUF_EXPORT MapEntryBase : public Message {
+ public:
+ ::google::protobuf::Metadata GetMetadata() const {
+ ::google::protobuf::Metadata metadata;
+ metadata.descriptor = descriptor_;
+ metadata.reflection = reflection_;
+ return metadata;
+ }
+
+ protected:
+ MapEntryBase() : descriptor_(NULL), reflection_(NULL) { }
+ virtual ~MapEntryBase() {}
+
+ const Descriptor* descriptor_;
+ const Reflection* reflection_;
+};
+
+// MapEntry is the returned google::protobuf::Message when calling AddMessage of
+// google::protobuf::Reflection. In order to let it work with generated message
+// reflection, its internal layout is the same as generated message with the
+// same fields. However, in order to decide the internal layout of key/value, we
+// need to know both their cpp type in generated api and proto type.
+//
+// cpp type | proto type | internal layout
+// int32 TYPE_INT32 int32
+// int32 TYPE_FIXED32 int32
+// FooEnum TYPE_ENUM int
+// FooMessage TYPE_MESSAGE FooMessage*
+//
+// The internal layouts of primitive types can be inferred from its proto type,
+// while we need to explicitly tell cpp type if proto type is TYPE_MESSAGE to
+// get internal layout.
+// Moreover, default_enum_value is used to initialize enum field in proto2.
+template <typename Key, typename Value, FieldDescriptor::Type KeyProtoType,
+ FieldDescriptor::Type ValueProtoType, int default_enum_value>
+class MapEntry : public MapEntryBase {
+ // Handlers for key/value's proto field type. Used to infer internal layout
+ // and provide parsing/serialization support.
+ typedef MapProtoTypeHandler<KeyProtoType> KeyProtoHandler;
+ typedef MapProtoTypeHandler<ValueProtoType> ValueProtoHandler;
+
+ // Define key/value's internal stored type. Message is the only one whose
+ // internal stored type cannot be inferred from its proto type.
+ typedef typename KeyProtoHandler::CppType KeyProtoHandlerCppType;
+ typedef typename ValueProtoHandler::CppType ValueProtoHandlerCppType;
+ static const bool kIsKeyMessage = KeyProtoHandler::kIsMessage;
+ static const bool kIsValueMessage = ValueProtoHandler::kIsMessage;
+ typedef typename MapIf<kIsKeyMessage, Key, KeyProtoHandlerCppType>::type
+ KeyCppType;
+ typedef typename MapIf<kIsValueMessage, Value, ValueProtoHandlerCppType>::type
+ ValCppType;
+
+ // Handlers for key/value's internal stored type. Provide utilities to
+ // manipulate internal stored type. We need it because some types are stored
+ // as values and others are stored as pointers (Message and string), but we
+ // need to keep the code in MapEntry unified instead of providing different
+ // codes for each type.
+ typedef MapCppTypeHandler<KeyCppType> KeyCppHandler;
+ typedef MapCppTypeHandler<ValCppType> ValueCppHandler;
+
+ // Define internal memory layout. Strings and messages are stored as
+ // pointers, while other types are stored as values.
+ static const bool kKeyIsStringOrMessage = KeyCppHandler::kIsStringOrMessage;
+ static const bool kValIsStringOrMessage = ValueCppHandler::kIsStringOrMessage;
+ typedef typename MapIf<kKeyIsStringOrMessage, KeyCppType*, KeyCppType>::type
+ KeyBase;
+ typedef typename MapIf<kValIsStringOrMessage, ValCppType*, ValCppType>::type
+ ValueBase;
+
+ // Abbreviation for MapEntry
+ typedef typename google::protobuf::internal::MapEntry<
+ Key, Value, KeyProtoType, ValueProtoType, default_enum_value> EntryType;
+
+ // 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, KeyProtoHandler::kWireType);
+ static const uint8 kValueTag = GOOGLE_PROTOBUF_WIRE_FORMAT_MAKE_TAG(
+ kValueFieldNumber, ValueProtoHandler::kWireType);
+ static const int kTagSize = 1;
+
+ public:
+ ~MapEntry() {
+ if (this == default_instance_) {
+ delete reflection_;
+ } else {
+ KeyCppHandler::Delete(key_);
+ ValueCppHandler::Delete(value_);
+ }
+ }
+
+ // accessors ======================================================
+
+ inline void set_key(const KeyCppType& key) {
+ KeyCppHandler::EnsureMutable(&key_);
+ KeyCppHandler::Merge(key, &key_);
+ set_has_key();
+ }
+ virtual inline const KeyCppType& key() const {
+ return KeyCppHandler::Reference(key_);
+ }
+ inline KeyCppType* mutable_key() {
+ set_has_key();
+ KeyCppHandler::EnsureMutable(&key_);
+ return KeyCppHandler::Pointer(key_);
+ }
+ inline void set_value(const ValCppType& value) {
+ ValueCppHandler::EnsureMutable(&value_);
+ ValueCppHandler::Merge(value, &value_);
+ set_has_value();
+ }
+ virtual inline const ValCppType& value() const {
+ GOOGLE_CHECK(default_instance_ != NULL);
+ return ValueCppHandler::DefaultIfNotInitialized(value_,
+ default_instance_->value_);
+ }
+ inline ValCppType* mutable_value() {
+ set_has_value();
+ ValueCppHandler::EnsureMutable(&value_);
+ return ValueCppHandler::Pointer(value_);
+ }
+
+ // implements Message =============================================
+
+ bool MergePartialFromCodedStream(::google::protobuf::io::CodedInputStream* input) {
+ uint32 tag;
+
+ for (;;) {
+ // 1) corrupted data: return false;
+ // 2) unknown field: skip without putting into unknown field set;
+ // 3) unknown enum value: keep it in parsing. In proto2, caller should
+ // check the value and put this entry into containing message's unknown
+ // field set if the value is an unknown enum. In proto3, caller doesn't
+ // need to care whether the value is unknown enum;
+ // 4) missing key/value: missed key/value will have default value. caller
+ // should take this entry as if key/value is set to default value.
+ tag = input->ReadTag();
+ switch (tag) {
+ case kKeyTag:
+ if (!KeyProtoHandler::Read(input, mutable_key())) return false;
+ set_has_key();
+ if (!input->ExpectTag(kValueTag)) break;
+ GOOGLE_FALLTHROUGH_INTENDED;
+
+ case kValueTag:
+ if (!ValueProtoHandler::Read(input, mutable_value())) return false;
+ set_has_value();
+ if (input->ExpectAtEnd()) return true;
+ break;
+
+ default:
+ if (tag == 0 ||
+ WireFormatLite::GetTagWireType(tag) ==
+ WireFormatLite::WIRETYPE_END_GROUP) {
+ return true;
+ }
+ if (!WireFormatLite::SkipField(input, tag)) return false;
+ break;
+ }
+ }
+ }
+
+ int ByteSize() const {
+ int size = 0;
+ size += has_key() ? kTagSize + KeyProtoHandler::ByteSize(key()) : 0;
+ size += has_value() ? kTagSize + ValueProtoHandler::ByteSize(value()) : 0;
+ return size;
+ }
+
+ void SerializeWithCachedSizes(::google::protobuf::io::CodedOutputStream* output) const {
+ KeyProtoHandler::Write(kKeyFieldNumber, key(), output);
+ ValueProtoHandler::Write(kValueFieldNumber, value(), output);
+ }
+
+ ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const {
+ output = KeyProtoHandler::WriteToArray(kKeyFieldNumber, key(), output);
+ output =
+ ValueProtoHandler::WriteToArray(kValueFieldNumber, value(), output);
+ return output;
+ }
+
+ int GetCachedSize() const {
+ int size = 0;
+ size += has_key() ? kTagSize + KeyProtoHandler::GetCachedSize(key()) : 0;
+ size +=
+ has_value() ? kTagSize + ValueProtoHandler::GetCachedSize(value()) : 0;
+ return size;
+ }
+
+ bool IsInitialized() const { return ValueCppHandler::IsInitialized(value_); }
+
+ Message* New() const {
+ MapEntry* entry = new MapEntry;
+ entry->descriptor_ = descriptor_;
+ entry->reflection_ = reflection_;
+ entry->default_instance_ = default_instance_;
+ return entry;
+ }
+
+ int SpaceUsed() const {
+ int size = sizeof(MapEntry);
+ size += KeyCppHandler::SpaceUsedInMapEntry(key_);
+ size += ValueCppHandler::SpaceUsedInMapEntry(value_);
+ return size;
+ }
+
+ void CopyFrom(const ::google::protobuf::Message& from) {
+ Clear();
+ MergeFrom(from);
+ }
+
+ void MergeFrom(const ::google::protobuf::Message& from) {
+ GOOGLE_CHECK_NE(&from, this);
+ const MapEntry* source = dynamic_cast_if_available<const MapEntry*>(&from);
+ if (source == NULL) {
+ ReflectionOps::Merge(from, this);
+ } else {
+ MergeFrom(*source);
+ }
+ }
+
+ void CopyFrom(const MapEntry& from) {
+ Clear();
+ MergeFrom(from);
+ }
+
+ void MergeFrom(const MapEntry& from) {
+ if (from._has_bits_[0]) {
+ if (from.has_key()) {
+ KeyCppHandler::EnsureMutable(&key_);
+ KeyCppHandler::Merge(from.key(), &key_);
+ set_has_key();
+ }
+ if (from.has_value()) {
+ ValueCppHandler::EnsureMutable(&value_);
+ ValueCppHandler::Merge(from.value(), &value_);
+ set_has_value();
+ }
+ }
+ }
+
+ void Clear() {
+ KeyCppHandler::Clear(&key_);
+ ValueCppHandler::ClearMaybeByDefaultEnum(&value_, default_enum_value);
+ clear_has_key();
+ clear_has_value();
+ }
+
+ void InitAsDefaultInstance() {
+ KeyCppHandler::AssignDefaultValue(&key_);
+ ValueCppHandler::AssignDefaultValue(&value_);
+ }
+
+ // Create default MapEntry instance for given descriptor. Descriptor has to be
+ // given when creating default MapEntry instance because different map field
+ // may have the same type and MapEntry class. The given descriptor is needed
+ // to distinguish instances of the same MapEntry class.
+ static MapEntry* CreateDefaultInstance(const Descriptor* descriptor) {
+ MapEntry* entry = new MapEntry();
+ const Reflection* reflection = new GeneratedMessageReflection(
+ descriptor, entry, offsets_,
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MapEntry, _has_bits_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MapEntry, _unknown_fields_), -1,
+ DescriptorPool::generated_pool(),
+ ::google::protobuf::MessageFactory::generated_factory(), sizeof(MapEntry), -1);
+ entry->descriptor_ = descriptor;
+ entry->reflection_ = reflection;
+ entry->default_instance_ = entry;
+ entry->InitAsDefaultInstance();
+ RegisterMapEntryDefaultInstance(entry);
+ return entry;
+ }
+
+ // Create a MapEntry for given key and value from google::protobuf::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
+ // google::protobuf::Map is enum. We cannot create a reference to int from an enum.
+ static MapEntry* EnumWrap(const Key& key, const Value value) {
+ return new MapEnumEntryWrapper<Key, Value, KeyProtoType, ValueProtoType,
+ default_enum_value>(key, value);
+ }
+
+ // Like above, but for all the other types. This avoids value copy to create
+ // MapEntry from google::protobuf::Map in serialization.
+ static MapEntry* Wrap(const Key& key, const Value& value) {
+ return new MapEntryWrapper<Key, Value, KeyProtoType, ValueProtoType,
+ default_enum_value>(key, value);
+ }
+
+ protected:
+ void set_has_key() { _has_bits_[0] |= 0x00000001u; }
+ bool has_key() const { return (_has_bits_[0] & 0x00000001u) != 0; }
+ void clear_has_key() { _has_bits_[0] &= ~0x00000001u; }
+ void set_has_value() { _has_bits_[0] |= 0x00000002u; }
+ bool has_value() const { return (_has_bits_[0] & 0x00000002u) != 0; }
+ void clear_has_value() { _has_bits_[0] &= ~0x00000002u; }
+
+ private:
+ // Serializing a generated message containing map field involves serializing
+ // key-value pairs from google::protobuf::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, google::protobuf::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.
+ template <typename KeyNested, typename ValueNested,
+ FieldDescriptor::Type KeyProtoNested,
+ FieldDescriptor::Type ValueProtoNested, int default_enum>
+ class MapEntryWrapper
+ : public MapEntry<KeyNested, ValueNested, KeyProtoNested,
+ ValueProtoNested, default_enum> {
+ typedef MapEntry<KeyNested, ValueNested, KeyProtoNested, ValueProtoNested,
+ default_enum_value> Base;
+ typedef typename Base::KeyCppType KeyCppType;
+ typedef typename Base::ValCppType ValCppType;
+
+ public:
+ MapEntryWrapper(const KeyNested& key, const ValueNested& value)
+ : key_(key), value_(value) {
+ Base::set_has_key();
+ Base::set_has_value();
+ }
+ inline const KeyCppType& key() const { return key_; }
+ inline const ValCppType& value() const { return value_; }
+
+ private:
+ const Key& key_;
+ const Value& value_;
+ };
+
+ // 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.
+ template <typename KeyNested, typename ValueNested,
+ FieldDescriptor::Type KeyProtoNested,
+ FieldDescriptor::Type ValueProtoNested, int default_enum>
+ class MapEnumEntryWrapper
+ : public MapEntry<KeyNested, ValueNested, KeyProtoNested,
+ ValueProtoNested, default_enum> {
+ typedef MapEntry<KeyNested, ValueNested, KeyProtoNested, ValueProtoNested,
+ default_enum> Base;
+ typedef typename Base::KeyCppType KeyCppType;
+ typedef typename Base::ValCppType ValCppType;
+
+ public:
+ MapEnumEntryWrapper(const KeyNested& key, const ValueNested& value)
+ : key_(key), value_(value) {
+ Base::set_has_key();
+ Base::set_has_value();
+ }
+ inline const KeyCppType& key() const { return key_; }
+ inline const ValCppType& value() const { return value_; }
+
+ private:
+ const KeyCppType& key_;
+ const ValCppType value_;
+ };
+
+ MapEntry() : default_instance_(NULL) {
+ KeyCppHandler::Initialize(&key_);
+ ValueCppHandler::InitializeMaybeByDefaultEnum(&value_, default_enum_value);
+ _has_bits_[0] = 0;
+ }
+
+ KeyBase key_;
+ ValueBase value_;
+ static int offsets_[2];
+ UnknownFieldSet _unknown_fields_;
+ uint32 _has_bits_[1];
+ MapEntry* default_instance_;
+
+ friend class ::google::protobuf::Arena;
+ template <typename K, typename V,
+ FieldDescriptor::Type KType,
+ FieldDescriptor::Type VType, int default_enum>
+ friend class internal::MapField;
+ friend class LIBPROTOBUF_EXPORT internal::GeneratedMessageReflection;
+
+ GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapEntry);
+};
+
+template <typename Key, typename Value, FieldDescriptor::Type KeyProtoType,
+ FieldDescriptor::Type ValueProtoType, int default_enum_value>
+int MapEntry<Key, Value, KeyProtoType, ValueProtoType,
+ default_enum_value>::offsets_[2] = {
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MapEntry, key_),
+ GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(MapEntry, value_),
+};
+
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_MAP_ENTRY_H__
diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc
new file mode 100644
index 00000000..b535ec28
--- /dev/null
+++ b/src/google/protobuf/map_field.cc
@@ -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.
+
+#include <google/protobuf/map_field.h>
+
+#include <vector>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+ProtobufOnceType map_entry_default_instances_once_;
+Mutex* map_entry_default_instances_mutex_;
+vector<MessageLite*>* map_entry_default_instances_;
+
+void DeleteMapEntryDefaultInstances() {
+ for (int i = 0; i < map_entry_default_instances_->size(); ++i) {
+ delete map_entry_default_instances_->at(i);
+ }
+ delete map_entry_default_instances_mutex_;
+ delete map_entry_default_instances_;
+}
+
+void InitMapEntryDefaultInstances() {
+ map_entry_default_instances_mutex_ = new Mutex();
+ map_entry_default_instances_ = new vector<MessageLite*>();
+ OnShutdown(&DeleteMapEntryDefaultInstances);
+}
+
+void RegisterMapEntryDefaultInstance(MessageLite* default_instance) {
+ GoogleOnceInit(&map_entry_default_instances_once_,
+ &InitMapEntryDefaultInstances);
+ MutexLock lock(map_entry_default_instances_mutex_);
+ map_entry_default_instances_->push_back(default_instance);
+}
+
+MapFieldBase::~MapFieldBase() {
+ if (repeated_field_ != NULL) delete repeated_field_;
+}
+
+const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const {
+ SyncRepeatedFieldWithMap();
+ return *repeated_field_;
+}
+
+RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() {
+ SyncRepeatedFieldWithMap();
+ SetRepeatedDirty();
+ return repeated_field_;
+}
+
+int MapFieldBase::SpaceUsedExcludingSelf() const {
+ mutex_.Lock();
+ int size = SpaceUsedExcludingSelfNoLock();
+ mutex_.Unlock();
+ return size;
+}
+
+int MapFieldBase::SpaceUsedExcludingSelfNoLock() const {
+ if (repeated_field_ != NULL) {
+ return repeated_field_->SpaceUsedExcludingSelf();
+ } else {
+ return 0;
+ }
+}
+
+void MapFieldBase::InitMetadataOnce() const {
+ GOOGLE_CHECK(entry_descriptor_ != NULL);
+ GOOGLE_CHECK(assign_descriptor_callback_ != NULL);
+ (*assign_descriptor_callback_)();
+}
+
+void MapFieldBase::SetMapDirty() { state_ = STATE_MODIFIED_MAP; }
+
+void MapFieldBase::SetRepeatedDirty() { state_ = STATE_MODIFIED_REPEATED; }
+
+void* MapFieldBase::MutableRepeatedPtrField() const { return repeated_field_; }
+
+void MapFieldBase::SyncRepeatedFieldWithMap() const {
+ Atomic32 state = google::protobuf::internal::NoBarrier_Load(&state_);
+ if (state == 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_ == STATE_MODIFIED_MAP) {
+ SyncRepeatedFieldWithMapNoLock();
+ google::protobuf::internal::NoBarrier_Store(&state_, CLEAN);
+ }
+ mutex_.Unlock();
+ }
+}
+
+void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const {
+ if (repeated_field_ == NULL) repeated_field_ = new RepeatedPtrField<Message>;
+}
+
+void MapFieldBase::SyncMapWithRepeatedField() const {
+ Atomic32 state = google::protobuf::internal::NoBarrier_Load(&state_);
+ if (state == STATE_MODIFIED_REPEATED) {
+ mutex_.Lock();
+ // Double check state, because another thread may have seen the same state
+ // and done the synchronization before the current thread.
+ if (state_ == STATE_MODIFIED_REPEATED) {
+ SyncMapWithRepeatedFieldNoLock();
+ google::protobuf::internal::NoBarrier_Store(&state_, CLEAN);
+ }
+ mutex_.Unlock();
+ }
+}
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h
new file mode 100644
index 00000000..8516d74e
--- /dev/null
+++ b/src/google/protobuf/map_field.h
@@ -0,0 +1,227 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_MAP_FIELD_H__
+#define GOOGLE_PROTOBUF_MAP_FIELD_H__
+
+#include <google/protobuf/stubs/atomicops.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/generated_message_reflection.h>
+#include <google/protobuf/map.h>
+#include <google/protobuf/map_entry.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/repeated_field.h>
+#include <google/protobuf/unknown_field_set.h>
+
+
+namespace google {
+namespace protobuf {
+
+namespace internal {
+
+class ContendedMapCleanTest;
+class GeneratedMessageReflection;
+class MapFieldAccessor;
+
+// This class provides accesss to map field using reflection, which is the same
+// as those provided for RepeatedPtrField<Message>. It is used for internal
+// reflection implentation only. Users should never use this directly.
+class LIBPROTOBUF_EXPORT MapFieldBase {
+ public:
+ MapFieldBase()
+ : base_map_(NULL),
+ repeated_field_(NULL),
+ entry_descriptor_(NULL),
+ assign_descriptor_callback_(NULL),
+ state_(STATE_MODIFIED_MAP) {}
+ virtual ~MapFieldBase();
+
+ // Returns reference to internal repeated field. Data written using
+ // google::protobuf::Map's api prior to calling this function is guarantted to be
+ // included in repeated field.
+ const RepeatedPtrFieldBase& GetRepeatedField() const;
+
+ // Like above. Returns mutable pointer to the internal repeated field.
+ RepeatedPtrFieldBase* MutableRepeatedField();
+
+ // Returns the number of bytes used by the repeated field, excluding
+ // sizeof(*this)
+ int SpaceUsedExcludingSelf() const;
+
+ protected:
+ // Gets the size of space used by map field.
+ virtual int SpaceUsedExcludingSelfNoLock() const;
+
+ // Synchronizes the content in Map to RepeatedPtrField if there is any change
+ // to Map after last synchronization.
+ void SyncRepeatedFieldWithMap() const;
+ virtual void SyncRepeatedFieldWithMapNoLock() const;
+
+ // Synchronizes the content in RepeatedPtrField to Map if there is any change
+ // to RepeatedPtrField after last synchronization.
+ void SyncMapWithRepeatedField() const;
+ virtual void SyncMapWithRepeatedFieldNoLock() const {}
+
+ // Tells MapFieldBase that there is new change to Map.
+ void SetMapDirty();
+
+ // Tells MapFieldBase that there is new change to RepeatedPTrField.
+ void SetRepeatedDirty();
+
+ // Provides derived class the access to repeated field.
+ void* MutableRepeatedPtrField() const;
+
+ // Creates descriptor for only one time.
+ void InitMetadataOnce() const;
+
+ enum State {
+ STATE_MODIFIED_MAP = 0, // map has newly added data that has not been
+ // 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
+ };
+
+ mutable void* base_map_;
+ mutable RepeatedPtrField<Message>* repeated_field_;
+ // MapEntry can only be created from MapField. To create MapEntry, MapField
+ // needs to know its descriptor, because MapEntry is not generated class which
+ // cannot initialize its own descriptor by calling generated
+ // descriptor-assign-function. Thus, we need to register a callback to
+ // initialize MapEntry's descriptor.
+ const Descriptor** entry_descriptor_;
+ void (*assign_descriptor_callback_)();
+
+ mutable Mutex mutex_; // The thread to synchronize map and repeated field
+ // needs to get lock first;
+ mutable volatile Atomic32 state_; // 0: STATE_MODIFIED_MAP
+ // 1: STATE_MODIFIED_REPEATED
+ // 2: CLEAN
+
+ private:
+ friend class ContendedMapCleanTest;
+ friend class GeneratedMessageReflection;
+ friend class MapFieldAccessor;
+};
+
+// This class provides accesss to map field using generated api. It is used for
+// internal generated message implentation only. Users should never use this
+// directly.
+template<typename Key, typename T,
+ FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value = 0>
+class MapField : public MapFieldBase {
+ // Handlers for key/value's proto field type.
+ typedef MapProtoTypeHandler<KeyProto> KeyProtoHandler;
+ typedef MapProtoTypeHandler<ValueProto> ValueProtoHandler;
+
+ // Define key/value's internal stored type.
+ typedef typename KeyProtoHandler::CppType KeyHandlerCpp;
+ typedef typename ValueProtoHandler::CppType ValHandlerCpp;
+ static const bool kIsKeyMessage = KeyProtoHandler::kIsMessage;
+ static const bool kIsValMessage = ValueProtoHandler::kIsMessage;
+ typedef typename MapIf<kIsKeyMessage, Key, KeyHandlerCpp>::type KeyCpp;
+ typedef typename MapIf<kIsValMessage, T , ValHandlerCpp>::type ValCpp;
+
+ // Handlers for key/value's internal stored type.
+ typedef MapCppTypeHandler<KeyCpp> KeyHandler;
+ typedef MapCppTypeHandler<ValCpp> ValHandler;
+
+ // Define message type for internal repeated field.
+ typedef MapEntry<Key, T, KeyProto, ValueProto, default_enum_value> EntryType;
+
+ // Enum needs to be handled differently from other types because it has
+ // different exposed type in google::protobuf::Map's api and repeated field's api. For
+ // details see the comment in the implementation of
+ // SyncMapWithRepeatedFieldNoLocki.
+ static const bool kIsValueEnum = ValueProtoHandler::kIsEnum;
+ typedef typename MapIf<kIsValueEnum, T, const T&>::type CastValueType;
+
+ public:
+ MapField();
+ // MapField doesn't own the default_entry, which means default_entry must
+ // outlive the lifetime of MapField.
+ MapField(const Message* default_entry);
+ ~MapField();
+
+ // Accessors
+ const Map<Key, T>& GetMap() const;
+ Map<Key, T>* MutableMap();
+
+ // Convenient methods for generated message implementation.
+ int size() const;
+ void Clear();
+ void MergeFrom(const MapField& other);
+ void Swap(MapField* other);
+
+ // Allocates metadata only if this MapField is part of a generated message.
+ void SetEntryDescriptor(const Descriptor** descriptor);
+ void SetAssignDescriptorCallback(void (*callback)());
+
+ // Set default enum value only for proto2 map field whose value is enum type.
+ void SetDefaultEnumValue();
+
+ // Used in the implementation of parsing. Caller should take the ownership.
+ EntryType* NewEntry() const;
+ // Used in the implementation of serializing enum value type. Caller should
+ // take the ownership.
+ EntryType* NewEnumEntryWrapper(const Key& key, const T t) const;
+ // Used in the implementation of serializing other value types. Caller should
+ // take the ownership.
+ EntryType* NewEntryWrapper(const Key& key, const T& t) const;
+
+ private:
+ // MapField needs MapEntry's default instance to create new MapEntry.
+ void InitDefaultEntryOnce() const;
+
+ // Convenient methods to get internal google::protobuf::Map
+ const Map<Key, T>& GetInternalMap() const;
+ Map<Key, T>* MutableInternalMap();
+
+ // Implements MapFieldBase
+ void SyncRepeatedFieldWithMapNoLock() const;
+ void SyncMapWithRepeatedFieldNoLock() const;
+ int SpaceUsedExcludingSelfNoLock() const;
+
+ mutable const EntryType* default_entry_;
+};
+
+// True if IsInitialized() is true for value field in all elements of t. T is
+// expected to be message. It's useful to have this helper here to keep the
+// protobuf compiler from ever having to emit loops in IsInitialized() methods.
+// We want the C++ compiler to inline this or not as it sees fit.
+template <typename Key, typename T>
+bool AllAreInitialized(const Map<Key, T>& t);
+
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_MAP_FIELD_H__
diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h
new file mode 100644
index 00000000..79302e48
--- /dev/null
+++ b/src/google/protobuf/map_field_inl.h
@@ -0,0 +1,278 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
+#define GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
+
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+
+#include <google/protobuf/map_field.h>
+#include <google/protobuf/map_type_handler.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::MapField()
+ : default_entry_(NULL) {
+ MapFieldBase::base_map_ = new Map<Key, T>;
+ SetDefaultEnumValue();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::MapField(
+ const Message* default_entry)
+ : default_entry_(down_cast<const EntryType*>(default_entry)) {
+ MapFieldBase::base_map_ = new Map<Key, T>;
+ SetDefaultEnumValue();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::~MapField() {
+ delete reinterpret_cast<Map<Key, T>*>(MapFieldBase::base_map_);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+int MapField<Key, T, KeyProto, ValueProto, default_enum_value>::size() const {
+ SyncMapWithRepeatedField();
+ return GetInternalMap().size();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto, default_enum_value>::Clear() {
+ SyncMapWithRepeatedField();
+ MutableInternalMap()->clear();
+ SetMapDirty();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+const Map<Key, T>&
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::GetMap() const {
+ SyncMapWithRepeatedField();
+ return GetInternalMap();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+Map<Key, T>*
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::MutableMap() {
+ SyncMapWithRepeatedField();
+ Map<Key, T>* result = MutableInternalMap();
+ SetMapDirty();
+ return result;
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto, default_enum_value>::MergeFrom(
+ const MapField& other) {
+ SyncMapWithRepeatedField();
+ other.SyncMapWithRepeatedField();
+
+ Map<Key, T>* map = MutableInternalMap();
+ const Map<Key, T>& other_map = other.GetInternalMap();
+ for (typename Map<Key, T>::const_iterator it = other_map.begin();
+ it != other_map.end(); ++it) {
+ (*map)[it->first] = it->second;
+ }
+ SetMapDirty();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto, default_enum_value>::Swap(
+ MapField* other) {
+ std::swap(repeated_field_, other->repeated_field_);
+ std::swap(base_map_, other->base_map_);
+ std::swap(state_, other->state_);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::SetEntryDescriptor(
+ const Descriptor** descriptor) {
+ entry_descriptor_ = descriptor;
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void
+MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::SetAssignDescriptorCallback(void (*callback)()) {
+ assign_descriptor_callback_ = callback;
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::SetDefaultEnumValue() {
+ MutableInternalMap()->SetDefaultEnumValue(default_enum_value);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapEntry<Key, T, KeyProto, ValueProto, default_enum_value>*
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::NewEntry() const {
+ // The MapEntry instance created here is only used in generated code for
+ // parsing. It doesn't have default instance, descriptor or reflection,
+ // because these are not needed in parsing and will prevent us from using it
+ // for parsing MessageLite.
+ return new EntryType();
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapEntry<Key, T, KeyProto, ValueProto, default_enum_value>*
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::NewEntryWrapper(
+ const Key& key, const T& t) const {
+ return EntryType::Wrap(key, t);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+MapEntry<Key, T, KeyProto, ValueProto, default_enum_value>*
+MapField<Key, T, KeyProto, ValueProto, default_enum_value>::NewEnumEntryWrapper(
+ const Key& key, const T t) const {
+ return EntryType::EnumWrap(key, t);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+const Map<Key, T>& MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::GetInternalMap() const {
+ return *reinterpret_cast<Map<Key, T>*>(MapFieldBase::base_map_);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+Map<Key, T>* MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::MutableInternalMap() {
+ return reinterpret_cast<Map<Key, T>*>(MapFieldBase::base_map_);
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::SyncRepeatedFieldWithMapNoLock() const {
+ if (repeated_field_ == NULL) {
+ repeated_field_ = new RepeatedPtrField<Message>();
+ }
+ const Map<Key, T>& map =
+ *static_cast<const Map<Key, T>*>(MapFieldBase::base_map_);
+ RepeatedPtrField<EntryType>* repeated_field =
+ reinterpret_cast<RepeatedPtrField<EntryType>*>(repeated_field_);
+
+ repeated_field->Clear();
+
+ for (typename Map<Key, T>::const_iterator it = map.begin();
+ it != map.end(); ++it) {
+ InitDefaultEntryOnce();
+ GOOGLE_CHECK(default_entry_ != NULL);
+ EntryType* new_entry = down_cast<EntryType*>(default_entry_->New());
+ repeated_field->AddAllocated(new_entry);
+ new_entry->set_key(it->first);
+ new_entry->set_value(it->second);
+ }
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::SyncMapWithRepeatedFieldNoLock() const {
+ Map<Key, T>* map = reinterpret_cast<Map<Key, T>*>(MapFieldBase::base_map_);
+ RepeatedPtrField<EntryType>* repeated_field =
+ reinterpret_cast<RepeatedPtrField<EntryType>*>(repeated_field_);
+ map->clear();
+ for (typename RepeatedPtrField<EntryType>::iterator 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
+ // stored type. We should not introduce value copy for them. We achieve this
+ // by casting to value for enum while casting to reference for other types.
+ (*map)[it->key()] = static_cast<CastValueType>(it->value());
+ }
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+int MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::SpaceUsedExcludingSelfNoLock() const {
+ int size = 0;
+ if (repeated_field_ != NULL) {
+ size += repeated_field_->SpaceUsedExcludingSelf();
+ }
+ Map<Key, T>* map = reinterpret_cast<Map<Key, T>*>(MapFieldBase::base_map_);
+ size += sizeof(*map);
+ for (typename Map<Key, T>::iterator it = map->begin();
+ it != map->end(); ++it) {
+ size += KeyHandler::SpaceUsedInMap(it->first);
+ size += ValHandler::SpaceUsedInMap(it->second);
+ }
+ return size;
+}
+
+template <typename Key, typename T, FieldDescriptor::Type KeyProto,
+ FieldDescriptor::Type ValueProto, int default_enum_value>
+void MapField<Key, T, KeyProto, ValueProto,
+ default_enum_value>::InitDefaultEntryOnce() const {
+ if (default_entry_ == NULL) {
+ InitMetadataOnce();
+ GOOGLE_CHECK(*entry_descriptor_ != NULL);
+ default_entry_ = down_cast<const EntryType*>(
+ MessageFactory::generated_factory()->GetPrototype(*entry_descriptor_));
+ }
+}
+
+template <typename Key, typename T>
+bool AllAreInitialized(const Map<Key, T>& t) {
+ for (typename Map<Key, T>::const_iterator it = t.begin(); it != t.end();
+ ++it) {
+ if (!it->second.IsInitialized()) return false;
+ }
+ return true;
+}
+
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_MAP_FIELD_INL_H__
diff --git a/src/google/protobuf/map_field_test.cc b/src/google/protobuf/map_field_test.cc
new file mode 100644
index 00000000..045f8f2c
--- /dev/null
+++ b/src/google/protobuf/map_field_test.cc
@@ -0,0 +1,435 @@
+// 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 <map>
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/map.h>
+#include <google/protobuf/map_unittest.pb.h>
+#include <google/protobuf/map_test_util.h>
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/map_field_inl.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/repeated_field.h>
+#include <gtest/gtest.h>
+
+namespace google {
+namespace protobuf {
+
+namespace internal {
+
+using unittest::TestAllTypes;
+
+class MapFieldBaseStub : public MapFieldBase {
+ public:
+ void SyncRepeatedFieldWithMap() const {
+ MapFieldBase::SyncRepeatedFieldWithMap();
+ }
+ void SyncMapWithRepeatedField() const {
+ MapFieldBase::SyncMapWithRepeatedField();
+ }
+ // Get underlined repeated field without synchronizing map.
+ RepeatedPtrField<Message>* InternalRepeatedField() {
+ return repeated_field_;
+ }
+ // Get underlined map without synchronizing repeated field.
+ template <typename MapType>
+ const MapType& GetMap() {
+ return *reinterpret_cast<MapType*>(base_map_);
+ }
+ // Get underlined map without synchronizing repeated field.
+ template <typename MapType>
+ MapType* MutableMap() {
+ return reinterpret_cast<MapType*>(base_map_);
+ }
+ bool IsMapClean() { return state_ != 0; }
+ bool IsRepeatedClean() { return state_ != 1; }
+ void SetMapDirty() { state_ = 0; }
+ void SetRepeatedDirty() { state_ = 1; }
+};
+
+class MapFieldBasePrimitiveTest : public ::testing::Test {
+ protected:
+ typedef MapField<int32, int32, FieldDescriptor::TYPE_INT32,
+ FieldDescriptor::TYPE_INT32> MapFieldType;
+
+ MapFieldBasePrimitiveTest() {
+ // Get descriptors
+ map_descriptor_ = unittest::TestMap::descriptor()
+ ->FindFieldByName("map_int32_int32")
+ ->message_type();
+ key_descriptor_ = map_descriptor_->FindFieldByName("key");
+ value_descriptor_ = map_descriptor_->FindFieldByName("value");
+
+ // Build map field
+ default_entry_ =
+ MessageFactory::generated_factory()->GetPrototype(map_descriptor_);
+ map_field_.reset(new MapFieldType(default_entry_));
+ map_field_base_ = map_field_.get();
+ map_ = map_field_->MutableMap();
+ initial_value_map_[0] = 100;
+ initial_value_map_[1] = 101;
+ map_->insert(initial_value_map_.begin(), initial_value_map_.end());
+ EXPECT_EQ(2, map_->size());
+ }
+
+ google::protobuf::scoped_ptr<MapFieldType> map_field_;
+ MapFieldBase* map_field_base_;
+ Map<int32, int32>* map_;
+ const Descriptor* map_descriptor_;
+ const FieldDescriptor* key_descriptor_;
+ const FieldDescriptor* value_descriptor_;
+ const Message* default_entry_;
+ std::map<int32, int32> initial_value_map_; // copy of initial values inserted
+};
+
+TEST_F(MapFieldBasePrimitiveTest, SpaceUsedExcludingSelf) {
+ EXPECT_LT(0, map_field_base_->SpaceUsedExcludingSelf());
+}
+
+TEST_F(MapFieldBasePrimitiveTest, GetRepeatedField) {
+ const RepeatedPtrField<Message>& repeated =
+ reinterpret_cast<const RepeatedPtrField<Message>&>(
+ map_field_base_->GetRepeatedField());
+ EXPECT_EQ(2, repeated.size());
+ for (int i = 0; i < repeated.size(); i++) {
+ const Message& message = repeated.Get(i);
+ int key = message.GetReflection()->GetInt32(message, key_descriptor_);
+ int value = message.GetReflection()->GetInt32(message, value_descriptor_);
+ EXPECT_EQ(value, initial_value_map_[key]);
+ }
+}
+
+TEST_F(MapFieldBasePrimitiveTest, MutableRepeatedField) {
+ RepeatedPtrField<Message>* repeated =
+ reinterpret_cast<RepeatedPtrField<Message>*>(
+ map_field_base_->MutableRepeatedField());
+ EXPECT_EQ(2, repeated->size());
+ for (int i = 0; i < repeated->size(); i++) {
+ const Message& message = repeated->Get(i);
+ int key = message.GetReflection()->GetInt32(message, key_descriptor_);
+ int value = message.GetReflection()->GetInt32(message, value_descriptor_);
+ EXPECT_EQ(value, initial_value_map_[key]);
+ }
+}
+
+namespace {
+enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY };
+} // anonymous namespace
+
+class MapFieldStateTest
+ : public testing::TestWithParam<State> {
+ public:
+ protected:
+ typedef MapField<int32, int32, FieldDescriptor::TYPE_INT32,
+ FieldDescriptor::TYPE_INT32> MapFieldType;
+ MapFieldStateTest() : state_(GetParam()) {
+ // Build map field
+ const Descriptor* map_descriptor =
+ unittest::TestMap::descriptor()
+ ->FindFieldByName("map_int32_int32")
+ ->message_type();
+ default_entry_ =
+ MessageFactory::generated_factory()->GetPrototype(map_descriptor);
+ map_field_.reset(new MapFieldType(default_entry_));
+ map_field_base_ = map_field_.get();
+
+ Expect(map_field_.get(), MAP_DIRTY, 0, 0, true);
+ switch (state_) {
+ case CLEAN:
+ AddOneStillClean(map_field_.get());
+ break;
+ case MAP_DIRTY:
+ MakeMapDirty(map_field_.get());
+ break;
+ case REPEATED_DIRTY:
+ MakeRepeatedDirty(map_field_.get());
+ break;
+ default:
+ break;
+ }
+ }
+
+ void AddOneStillClean(MapFieldType* map_field) {
+ MapFieldBase* map_field_base = map_field;
+ Map<int32, int32>* map = map_field->MutableMap();
+ (*map)[0] = 0;
+ map_field_base->GetRepeatedField();
+ Expect(map_field, CLEAN, 1, 1, false);
+ }
+
+ void MakeMapDirty(MapFieldType* map_field) {
+ Map<int32, int32>* map = map_field->MutableMap();
+ (*map)[0] = 0;
+ Expect(map_field, MAP_DIRTY, 1, 0, true);
+ }
+
+ void MakeRepeatedDirty(MapFieldType* map_field) {
+ MakeMapDirty(map_field);
+ MapFieldBase* map_field_base = map_field;
+ map_field_base->MutableRepeatedField();
+ MapFieldBaseStub* stub =
+ reinterpret_cast<MapFieldBaseStub*>(map_field_base);
+ Map<int32, int32>* map = stub->MutableMap<Map<int32, int32> >();
+ map->clear();
+
+ Expect(map_field, REPEATED_DIRTY, 0, 1, false);
+ }
+
+ void Expect(MapFieldType* map_field, State state, int map_size,
+ int repeated_size, bool is_repeated_null) {
+ MapFieldBase* map_field_base = map_field;
+ MapFieldBaseStub* stub =
+ reinterpret_cast<MapFieldBaseStub*>(map_field_base);
+
+ Map<int32, int32>* map = stub->MutableMap<Map<int32, int32> >();
+ RepeatedPtrField<Message>* repeated_field = stub->InternalRepeatedField();
+
+ switch (state) {
+ case MAP_DIRTY:
+ EXPECT_FALSE(stub->IsMapClean());
+ EXPECT_TRUE(stub->IsRepeatedClean());
+ break;
+ case REPEATED_DIRTY:
+ EXPECT_TRUE(stub->IsMapClean());
+ EXPECT_FALSE(stub->IsRepeatedClean());
+ break;
+ case CLEAN:
+ EXPECT_TRUE(stub->IsMapClean());
+ EXPECT_TRUE(stub->IsRepeatedClean());
+ break;
+ default:
+ FAIL();
+ }
+
+ EXPECT_EQ(map_size, map->size());
+ if (is_repeated_null) {
+ EXPECT_TRUE(repeated_field == NULL);
+ } else {
+ EXPECT_EQ(repeated_size, repeated_field->size());
+ }
+ }
+
+ google::protobuf::scoped_ptr<MapFieldType> map_field_;
+ MapFieldBase* map_field_base_;
+ State state_;
+ const Message* default_entry_;
+};
+
+INSTANTIATE_TEST_CASE_P(MapFieldStateTestInstance, MapFieldStateTest,
+ ::testing::Values(CLEAN, MAP_DIRTY, REPEATED_DIRTY));
+
+TEST_P(MapFieldStateTest, GetMap) {
+ map_field_->GetMap();
+ if (state_ != MAP_DIRTY) {
+ Expect(map_field_.get(), CLEAN, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ }
+}
+
+TEST_P(MapFieldStateTest, MutableMap) {
+ map_field_->MutableMap();
+ if (state_ != MAP_DIRTY) {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ }
+}
+
+TEST_P(MapFieldStateTest, MergeFromClean) {
+ MapFieldType other(default_entry_);
+ AddOneStillClean(&other);
+
+ map_field_->MergeFrom(other);
+
+ if (state_ != MAP_DIRTY) {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ }
+
+ Expect(&other, CLEAN, 1, 1, false);
+}
+
+TEST_P(MapFieldStateTest, MergeFromMapDirty) {
+ MapFieldType other(default_entry_);
+ MakeMapDirty(&other);
+
+ map_field_->MergeFrom(other);
+
+ if (state_ != MAP_DIRTY) {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ }
+
+ Expect(&other, MAP_DIRTY, 1, 0, true);
+}
+
+TEST_P(MapFieldStateTest, MergeFromRepeatedDirty) {
+ MapFieldType other(default_entry_);
+ MakeRepeatedDirty(&other);
+
+ map_field_->MergeFrom(other);
+
+ if (state_ != MAP_DIRTY) {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ }
+
+ Expect(&other, CLEAN, 1, 1, false);
+}
+
+TEST_P(MapFieldStateTest, SwapClean) {
+ MapFieldType other(default_entry_);
+ AddOneStillClean(&other);
+
+ map_field_->Swap(&other);
+
+ Expect(map_field_.get(), CLEAN, 1, 1, false);
+
+ switch (state_) {
+ case CLEAN:
+ Expect(&other, CLEAN, 1, 1, false);
+ break;
+ case MAP_DIRTY:
+ Expect(&other, MAP_DIRTY, 1, 0, true);
+ break;
+ case REPEATED_DIRTY:
+ Expect(&other, REPEATED_DIRTY, 0, 1, false);
+ break;
+ default:
+ break;
+ }
+}
+
+TEST_P(MapFieldStateTest, SwapMapDirty) {
+ MapFieldType other(default_entry_);
+ MakeMapDirty(&other);
+
+ map_field_->Swap(&other);
+
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+
+ switch (state_) {
+ case CLEAN:
+ Expect(&other, CLEAN, 1, 1, false);
+ break;
+ case MAP_DIRTY:
+ Expect(&other, MAP_DIRTY, 1, 0, true);
+ break;
+ case REPEATED_DIRTY:
+ Expect(&other, REPEATED_DIRTY, 0, 1, false);
+ break;
+ default:
+ break;
+ }
+}
+
+TEST_P(MapFieldStateTest, SwapRepeatedDirty) {
+ MapFieldType other(default_entry_);
+ MakeRepeatedDirty(&other);
+
+ map_field_->Swap(&other);
+
+ Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
+
+ switch (state_) {
+ case CLEAN:
+ Expect(&other, CLEAN, 1, 1, false);
+ break;
+ case MAP_DIRTY:
+ Expect(&other, MAP_DIRTY, 1, 0, true);
+ break;
+ case REPEATED_DIRTY:
+ Expect(&other, REPEATED_DIRTY, 0, 1, false);
+ break;
+ default:
+ break;
+ }
+}
+
+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);
+ }
+}
+
+TEST_P(MapFieldStateTest, SpaceUsedExcludingSelf) {
+ map_field_base_->SpaceUsedExcludingSelf();
+
+ switch (state_) {
+ case CLEAN:
+ Expect(map_field_.get(), CLEAN, 1, 1, false);
+ break;
+ case MAP_DIRTY:
+ Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
+ break;
+ case REPEATED_DIRTY:
+ Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
+ break;
+ default:
+ break;
+ }
+}
+
+TEST_P(MapFieldStateTest, GetMapField) {
+ map_field_base_->GetRepeatedField();
+
+ if (state_ != REPEATED_DIRTY) {
+ Expect(map_field_.get(), CLEAN, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
+ }
+}
+
+TEST_P(MapFieldStateTest, MutableMapField) {
+ map_field_base_->MutableRepeatedField();
+
+ if (state_ != REPEATED_DIRTY) {
+ Expect(map_field_.get(), REPEATED_DIRTY, 1, 1, false);
+ } else {
+ Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
+ }
+}
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/map_lite_unittest.proto b/src/google/protobuf/map_lite_unittest.proto
new file mode 100644
index 00000000..cc71555d
--- /dev/null
+++ b/src/google/protobuf/map_lite_unittest.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";
+
+option optimize_for = LITE_RUNTIME;
+
+package protobuf_unittest;
+
+message MapLite {
+ map<int32, int32> map_field = 1;
+}
diff --git a/src/google/protobuf/map_proto2_unittest.proto b/src/google/protobuf/map_proto2_unittest.proto
new file mode 100644
index 00000000..3d4af28e
--- /dev/null
+++ b/src/google/protobuf/map_proto2_unittest.proto
@@ -0,0 +1,68 @@
+// 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";
+
+
+// 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.
+// In map_test_util.h we do "using namespace unittest = protobuf_unittest".
+package protobuf_unittest;
+
+enum Proto2MapEnum {
+ PROTO2_MAP_ENUM_FOO = 0;
+ PROTO2_MAP_ENUM_BAR = 1;
+ PROTO2_MAP_ENUM_BAZ = 2;
+}
+
+enum Proto2MapEnumPlusExtra {
+ E_PROTO2_MAP_ENUM_FOO = 0;
+ E_PROTO2_MAP_ENUM_BAR = 1;
+ E_PROTO2_MAP_ENUM_BAZ = 2;
+ E_PROTO2_MAP_ENUM_EXTRA = 3;
+}
+
+enum Proto2MapEnumStartWithNonZero {
+ PROTO2_NON_ZERO_MAP_ENUM_FOO = 1;
+}
+
+message TestEnumMap {
+ map<int32, Proto2MapEnum> known_map_field = 101;
+ map<int32, Proto2MapEnum> unknown_map_field = 102;
+}
+
+message TestEnumMapPlusExtra {
+ map<int32, Proto2MapEnumPlusExtra> known_map_field = 101;
+ map<int32, Proto2MapEnumPlusExtra> unknown_map_field = 102;
+}
+
+message TestEnumStartWithNonZeroMap {
+ map<int32, Proto2MapEnumStartWithNonZero> map_field = 101;
+}
diff --git a/src/google/protobuf/map_test.cc b/src/google/protobuf/map_test.cc
new file mode 100644
index 00000000..9db67523
--- /dev/null
+++ b/src/google/protobuf/map_test.cc
@@ -0,0 +1,2252 @@
+// 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 <map>
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+#include <sstream>
+
+#include <google/protobuf/stubs/casts.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/stringprintf.h>
+#include <google/protobuf/testing/file.h>
+#include <google/protobuf/map_lite_unittest.pb.h>
+#include <google/protobuf/map_proto2_unittest.pb.h>
+#include <google/protobuf/map_unittest.pb.h>
+#include <google/protobuf/map_test_util.h>
+#include <google/protobuf/test_util.h>
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/descriptor_database.h>
+#include <google/protobuf/dynamic_message.h>
+#include <google/protobuf/map.h>
+#include <google/protobuf/map_field_inl.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/reflection.h>
+#include <google/protobuf/reflection_ops.h>
+#include <google/protobuf/text_format.h>
+#include <google/protobuf/wire_format.h>
+#include <google/protobuf/wire_format_lite_inl.h>
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/tokenizer.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/stubs/strutil.h>
+#include <google/protobuf/stubs/substitute.h>
+#include <google/protobuf/testing/googletest.h>
+#include <gtest/gtest.h>
+
+namespace google {
+
+using google::protobuf::unittest::ForeignMessage;
+using google::protobuf::unittest::TestAllTypes;
+using google::protobuf::unittest::TestMap;
+
+namespace protobuf {
+namespace internal {
+
+// Map API Test =====================================================
+
+class MapImplTest : public ::testing::Test {
+ protected:
+ MapImplTest()
+ : map_ptr_(new Map<int32, int32>),
+ map_(*map_ptr_),
+ const_map_(*map_ptr_) {
+ EXPECT_TRUE(map_.empty());
+ EXPECT_EQ(0, map_.size());
+ }
+ ~MapImplTest() {}
+
+ void ExpectSingleElement(int32 key, int32 value) {
+ EXPECT_FALSE(map_.empty());
+ EXPECT_EQ(1, map_.size());
+ ExpectElement(key, value);
+ }
+
+ void ExpectElements(const std::map<int32, int32>& map) {
+ EXPECT_FALSE(map_.empty());
+ EXPECT_EQ(map.size(), map_.size());
+ for (std::map<int32, int32>::const_iterator it = map.begin();
+ it != map.end(); ++it) {
+ ExpectElement(it->first, it->second);
+ }
+ }
+
+ void ExpectElement(int32 key, int32 value) {
+ // Test map size is correct.
+ EXPECT_EQ(value, map_[key]);
+ EXPECT_EQ(1, map_.count(key));
+
+ // Check mutable at and find work correctly.
+ EXPECT_EQ(value, map_.at(key));
+ Map<int32, int32>::iterator it = map_.find(key);
+
+ // interator dereferenceable
+ EXPECT_EQ(key, (*it).first);
+ EXPECT_EQ(value, (*it).second);
+ EXPECT_EQ(key, it->first);
+ EXPECT_EQ(value, it->second);
+
+ // iterator mutable
+ ((*it).second) = value + 1;
+ EXPECT_EQ(value + 1, map_[key]);
+ ((*it).second) = value;
+ EXPECT_EQ(value, map_[key]);
+
+ it->second = value + 1;
+ EXPECT_EQ(value + 1, map_[key]);
+ it->second = value;
+ EXPECT_EQ(value, map_[key]);
+
+ // copy constructor
+ Map<int32, int32>::iterator it_copy = it;
+ EXPECT_EQ(key, it_copy->first);
+ EXPECT_EQ(value, it_copy->second);
+
+ // Immutable API ================================================
+
+ // Check immutable at and find work correctly.
+ EXPECT_EQ(value, const_map_.at(key));
+ Map<int32, int32>::const_iterator const_it = const_map_.find(key);
+
+ // interator dereferenceable
+ EXPECT_EQ(key, (*const_it).first);
+ EXPECT_EQ(value, (*const_it).second);
+ EXPECT_EQ(key, const_it->first);
+ EXPECT_EQ(value, const_it->second);
+
+ // copy constructor
+ Map<int32, int32>::const_iterator const_it_copy = const_it;
+ EXPECT_EQ(key, const_it_copy->first);
+ EXPECT_EQ(value, const_it_copy->second);
+ }
+
+ google::protobuf::scoped_ptr<Map<int32, int32> > map_ptr_;
+ Map<int32, int32>& map_;
+ const Map<int32, int32>& const_map_;
+};
+
+TEST_F(MapImplTest, OperatorBracket) {
+ int32 key = 0;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ EXPECT_EQ(0, map_[key]);
+
+ map_[key] = value1;
+ ExpectSingleElement(key, value1);
+
+ map_[key] = value2;
+ ExpectSingleElement(key, value2);
+}
+
+TEST_F(MapImplTest, OperatorBracketNonExist) {
+ int32 key = 0;
+ int32 default_value = 0;
+
+ EXPECT_EQ(default_value, map_[key]);
+ ExpectSingleElement(key, default_value);
+}
+
+TEST_F(MapImplTest, MutableAt) {
+ int32 key = 0;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ map_[key] = value1;
+ ExpectSingleElement(key, value1);
+
+ map_.at(key) = value2;
+ ExpectSingleElement(key, value2);
+}
+
+#ifdef PROTOBUF_HAS_DEATH_TEST
+TEST_F(MapImplTest, MutableAtNonExistDeathTest) {
+ EXPECT_DEATH(map_.at(0), "");
+}
+
+TEST_F(MapImplTest, ImmutableAtNonExistDeathTest) {
+ EXPECT_DEATH(const_map_.at(0), "");
+}
+#endif // PROTOBUF_HAS_DEATH_TEST
+
+TEST_F(MapImplTest, CountNonExist) {
+ EXPECT_EQ(0, map_.count(0));
+}
+
+TEST_F(MapImplTest, MutableFindNonExist) {
+ EXPECT_TRUE(map_.end() == map_.find(0));
+}
+
+TEST_F(MapImplTest, ImmutableFindNonExist) {
+ EXPECT_TRUE(const_map_.end() == const_map_.find(0));
+}
+
+TEST_F(MapImplTest, ConstEnd) {
+ EXPECT_TRUE(const_map_.end() == const_map_.cend());
+}
+
+TEST_F(MapImplTest, GetReferenceFromIterator) {
+ for (int i = 0; i < 10; i++) {
+ map_[i] = i;
+ }
+
+ for (Map<int32, int32>::const_iterator it = map_.cbegin();
+ it != map_.cend();) {
+ Map<int32, int32>::const_reference entry = *it++;
+ EXPECT_EQ(entry.first, entry.second);
+ }
+
+ for (Map<int32, int32>::const_iterator it = const_map_.begin();
+ it != const_map_.end();) {
+ Map<int32, int32>::const_reference entry = *it++;
+ EXPECT_EQ(entry.first, entry.second);
+ }
+
+ for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
+ Map<int32, int32>::reference entry = *it++;
+ EXPECT_EQ(entry.first + 1, ++entry.second);
+ }
+}
+
+TEST_F(MapImplTest, IteratorBasic) {
+ map_[0] = 0;
+
+ // Default constructible (per forward iterator requirements).
+ Map<int, int>::const_iterator cit;
+ Map<int, int>::iterator it;
+
+ it = map_.begin();
+ cit = it; // Converts to const_iterator
+
+ // Can compare between them.
+ EXPECT_TRUE(it == cit);
+ EXPECT_FALSE(cit != it);
+
+ // Pre increment.
+ EXPECT_FALSE(it == ++cit);
+
+ // Post increment.
+ EXPECT_FALSE(it++ == cit);
+ EXPECT_TRUE(it == cit);
+}
+
+template <typename T>
+bool IsConstHelper(T& /*t*/) { // NOLINT. We want to catch non-const refs here.
+ return false;
+}
+template <typename T>
+bool IsConstHelper(const T& /*t*/) {
+ return true;
+}
+
+TEST_F(MapImplTest, IteratorConstness) {
+ map_[0] = 0;
+ EXPECT_TRUE(IsConstHelper(*map_.cbegin()));
+ EXPECT_TRUE(IsConstHelper(*const_map_.begin()));
+ EXPECT_FALSE(IsConstHelper(*map_.begin()));
+}
+
+bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; }
+template <typename T>
+bool IsForwardIteratorHelper(T /*t*/) {
+ return false;
+}
+
+TEST_F(MapImplTest, IteratorCategory) {
+ EXPECT_TRUE(IsForwardIteratorHelper(
+ std::iterator_traits<Map<int, int>::iterator>::iterator_category()));
+ EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits<
+ Map<int, int>::const_iterator>::iterator_category()));
+}
+
+TEST_F(MapImplTest, InsertSingle) {
+ int32 key = 0;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ // Insert a non-existed key.
+ std::pair<Map<int32, int32>::iterator, bool> result1 =
+ map_.insert(Map<int32, int32>::value_type(key, value1));
+ ExpectSingleElement(key, value1);
+
+ Map<int32, int32>::iterator it1 = result1.first;
+ EXPECT_EQ(key, it1->first);
+ EXPECT_EQ(value1, it1->second);
+ EXPECT_TRUE(result1.second);
+
+ // Insert an existed key.
+ std::pair<Map<int32, int32>::iterator, bool> result2 =
+ map_.insert(Map<int32, int32>::value_type(key, value2));
+ ExpectSingleElement(key, value1);
+
+ Map<int32, int32>::iterator it2 = result2.first;
+ EXPECT_TRUE(it1 == it2);
+ EXPECT_FALSE(result2.second);
+}
+
+TEST_F(MapImplTest, InsertByIterator) {
+ int32 key1 = 0;
+ int32 key2 = 1;
+ int32 value1a = 100;
+ int32 value1b = 101;
+ int32 value2a = 200;
+ int32 value2b = 201;
+
+ std::map<int32, int32> map1;
+ map1[key1] = value1a;
+ map1[key2] = value2a;
+
+ map_.insert(map1.begin(), map1.end());
+ ExpectElements(map1);
+
+ std::map<int32, int32> map2;
+ map2[key1] = value1b;
+ map2[key2] = value2b;
+
+ map_.insert(map2.begin(), map2.end());
+ ExpectElements(map1);
+}
+
+TEST_F(MapImplTest, EraseSingleByKey) {
+ int32 key = 0;
+ int32 value = 100;
+
+ map_[key] = value;
+ ExpectSingleElement(key, value);
+
+ // Erase an existing key.
+ EXPECT_EQ(1, map_.erase(key));
+ EXPECT_TRUE(map_.empty());
+ EXPECT_EQ(0, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(key));
+ EXPECT_TRUE(map_.begin() == map_.end());
+
+ // Erase a non-existing key.
+ EXPECT_EQ(0, map_.erase(key));
+}
+
+TEST_F(MapImplTest, EraseMutipleByKey) {
+ // erase in one specific order to trigger corner cases
+ for (int i = 0; i < 5; i++) {
+ map_[i] = i;
+ }
+
+ map_.erase(0);
+ EXPECT_EQ(4, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(0));
+
+ map_.erase(1);
+ EXPECT_EQ(3, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(1));
+
+ map_.erase(3);
+ EXPECT_EQ(2, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(3));
+
+ map_.erase(4);
+ EXPECT_EQ(1, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(4));
+
+ map_.erase(2);
+ EXPECT_EQ(0, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(2));
+}
+
+TEST_F(MapImplTest, EraseSingleByIterator) {
+ int32 key = 0;
+ int32 value = 100;
+
+ map_[key] = value;
+ ExpectSingleElement(key, value);
+
+ Map<int32, int32>::iterator it = map_.find(key);
+ map_.erase(it);
+ EXPECT_TRUE(map_.empty());
+ EXPECT_EQ(0, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(key));
+ EXPECT_TRUE(map_.begin() == map_.end());
+}
+
+TEST_F(MapImplTest, ValidIteratorAfterErase) {
+ for (int i = 0; i < 10; i++) {
+ map_[i] = i;
+ }
+
+ int count = 0;
+
+ for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
+ count++;
+ if (it->first % 2 == 1) {
+ map_.erase(it++);
+ } else {
+ ++it;
+ }
+ }
+
+ EXPECT_EQ(10, count);
+ EXPECT_EQ(5, map_.size());
+}
+
+TEST_F(MapImplTest, EraseByIterator) {
+ int32 key1 = 0;
+ int32 key2 = 1;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ std::map<int32, int32> map;
+ map[key1] = value1;
+ map[key2] = value2;
+
+ map_.insert(map.begin(), map.end());
+ ExpectElements(map);
+
+ map_.erase(map_.begin(), map_.end());
+ EXPECT_TRUE(map_.empty());
+ EXPECT_EQ(0, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(key1));
+ EXPECT_TRUE(map_.end() == map_.find(key2));
+ EXPECT_TRUE(map_.begin() == map_.end());
+}
+
+TEST_F(MapImplTest, Clear) {
+ int32 key = 0;
+ int32 value = 100;
+
+ map_[key] = value;
+ ExpectSingleElement(key, value);
+
+ map_.clear();
+
+ EXPECT_TRUE(map_.empty());
+ EXPECT_EQ(0, map_.size());
+ EXPECT_TRUE(map_.end() == map_.find(key));
+ EXPECT_TRUE(map_.begin() == map_.end());
+}
+
+TEST_F(MapImplTest, CopyConstructor) {
+ int32 key1 = 0;
+ int32 key2 = 1;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ std::map<int32, int32> map;
+ map[key1] = value1;
+ map[key2] = value2;
+
+ map_.insert(map.begin(), map.end());
+
+ Map<int32, int32> other(map_);
+
+ EXPECT_EQ(2, other.size());
+ EXPECT_EQ(value1, other.at(key1));
+ EXPECT_EQ(value2, other.at(key2));
+}
+
+TEST_F(MapImplTest, Assigner) {
+ int32 key1 = 0;
+ int32 key2 = 1;
+ int32 value1 = 100;
+ int32 value2 = 101;
+
+ std::map<int32, int32> map;
+ map[key1] = value1;
+ map[key2] = value2;
+
+ map_.insert(map.begin(), map.end());
+
+ Map<int32, int32> other;
+ int32 key_other = 123;
+ int32 value_other = 321;
+ other[key_other] = value_other;
+ EXPECT_EQ(1, other.size());
+
+ other = map_;
+
+ EXPECT_EQ(2, other.size());
+ EXPECT_EQ(value1, other.at(key1));
+ EXPECT_EQ(value2, other.at(key2));
+ EXPECT_TRUE(other.find(key_other) == other.end());
+
+ // Self assign
+ other = other;
+ EXPECT_EQ(2, other.size());
+ EXPECT_EQ(value1, other.at(key1));
+ EXPECT_EQ(value2, other.at(key2));
+}
+
+TEST_F(MapImplTest, Rehash) {
+ const int test_size = 50;
+ std::map<int32, int32> reference_map;
+ for (int i = 0; i < test_size; i++) {
+ reference_map[i] = i;
+ }
+ for (int i = 0; i < test_size; i++) {
+ map_[i] = reference_map[i];
+ EXPECT_EQ(reference_map[i], map_[i]);
+ }
+ for (int i = 0; i < test_size; i++) {
+ map_.erase(i);
+ EXPECT_TRUE(map_.end() == map_.find(i));
+ }
+ EXPECT_TRUE(map_.empty());
+}
+
+TEST_F(MapImplTest, EqualRange) {
+ int key = 100, key_missing = 101;
+ map_[key] = 100;
+
+ std::pair<google::protobuf::Map<int32, int32>::iterator,
+ google::protobuf::Map<int32, int32>::iterator> range = map_.equal_range(key);
+ EXPECT_TRUE(map_.find(key) == range.first);
+ EXPECT_TRUE(++map_.find(key) == range.second);
+
+ range = map_.equal_range(key_missing);
+ EXPECT_TRUE(map_.end() == range.first);
+ EXPECT_TRUE(map_.end() == range.second);
+
+ std::pair<google::protobuf::Map<int32, int32>::const_iterator,
+ google::protobuf::Map<int32, int32>::const_iterator> const_range =
+ const_map_.equal_range(key);
+ EXPECT_TRUE(const_map_.find(key) == const_range.first);
+ EXPECT_TRUE(++const_map_.find(key) == const_range.second);
+
+ const_range = const_map_.equal_range(key_missing);
+ EXPECT_TRUE(const_map_.end() == const_range.first);
+ EXPECT_TRUE(const_map_.end() == const_range.second);
+}
+
+TEST_F(MapImplTest, ConvertToStdMap) {
+ map_[100] = 101;
+ std::map<int32, int32> std_map(map_.begin(), map_.end());
+ EXPECT_EQ(1, std_map.size());
+ EXPECT_EQ(101, std_map[100]);
+}
+
+// Map Field Reflection Test ========================================
+
+static int Func(int i, int j) {
+ return i * j;
+}
+
+static string StrFunc(int i, int j) {
+ string str;
+ SStringPrintf(&str, "%d", Func(i, j));
+ return str;
+}
+
+static int Int(const string& value) {
+ int result = 0;
+ std::istringstream(value) >> result;
+ return result;
+}
+
+class MapFieldReflectionTest : public testing::Test {
+ protected:
+ typedef FieldDescriptor FD;
+};
+
+TEST_F(MapFieldReflectionTest, RegularFields) {
+ TestMap message;
+ const Reflection* refl = message.GetReflection();
+ const Descriptor* desc = message.GetDescriptor();
+
+ Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
+ Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
+ Map<string, string>* map_string_string = message.mutable_map_string_string();
+ Map<int32, ForeignMessage>* map_int32_foreign_message =
+ message.mutable_map_int32_foreign_message();
+
+ for (int i = 0; i < 10; ++i) {
+ (*map_int32_int32)[i] = Func(i, 1);
+ (*map_int32_double)[i] = Func(i, 2);
+ (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
+ (*map_int32_foreign_message)[i].set_c(Func(i, 6));
+ }
+
+ // Get FieldDescriptors for all the fields of interest.
+ const FieldDescriptor* fd_map_int32_int32 =
+ desc->FindFieldByName("map_int32_int32");
+ const FieldDescriptor* fd_map_int32_double =
+ desc->FindFieldByName("map_int32_double");
+ const FieldDescriptor* fd_map_string_string =
+ desc->FindFieldByName("map_string_string");
+ const FieldDescriptor* fd_map_int32_foreign_message =
+ desc->FindFieldByName("map_int32_foreign_message");
+
+ const FieldDescriptor* fd_map_int32_in32_key =
+ fd_map_int32_int32->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_in32_value =
+ fd_map_int32_int32->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_int32_double_key =
+ fd_map_int32_double->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_double_value =
+ fd_map_int32_double->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_string_string_key =
+ fd_map_string_string->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_string_string_value =
+ fd_map_string_string->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_int32_foreign_message_key =
+ fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_foreign_message_value =
+ fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
+
+ // Get RepeatedPtrField objects for all fields of interest.
+ const RepeatedPtrField<Message>& mf_int32_int32 =
+ refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32);
+ const RepeatedPtrField<Message>& mf_int32_double =
+ refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double);
+ const RepeatedPtrField<Message>& mf_string_string =
+ refl->GetRepeatedPtrField<Message>(message, fd_map_string_string);
+ const RepeatedPtrField<Message>&
+ mf_int32_foreign_message =
+ refl->GetRepeatedPtrField<Message>(
+ message, fd_map_int32_foreign_message);
+
+ // Get mutable RepeatedPtrField objects for all fields of interest.
+ RepeatedPtrField<Message>* mmf_int32_int32 =
+ refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32);
+ RepeatedPtrField<Message>* mmf_int32_double =
+ refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double);
+ RepeatedPtrField<Message>* mmf_string_string =
+ refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string);
+ RepeatedPtrField<Message>* mmf_int32_foreign_message =
+ refl->MutableRepeatedPtrField<Message>(
+ &message, fd_map_int32_foreign_message);
+
+ // Make sure we can do gets through the RepeatedPtrField objects.
+ for (int i = 0; i < 10; ++i) {
+ {
+ // Check gets through const objects.
+ const Message& message_int32_int32 = mf_int32_int32.Get(i);
+ int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_key);
+ int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_value);
+ EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
+
+ const Message& message_int32_double = mf_int32_double.Get(i);
+ int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
+ message_int32_double, fd_map_int32_double_key);
+ double value_int32_double =
+ message_int32_double.GetReflection()->GetDouble(
+ message_int32_double, fd_map_int32_double_value);
+ EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
+
+ const Message& message_string_string = mf_string_string.Get(i);
+ string key_string_string =
+ message_string_string.GetReflection()->GetString(
+ message_string_string, fd_map_string_string_key);
+ 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));
+
+ const Message& message_int32_message = mf_int32_foreign_message.Get(i);
+ int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
+ message_int32_message, fd_map_int32_foreign_message_key);
+ const ForeignMessage& value_int32_message =
+ down_cast<const ForeignMessage&>(
+ 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));
+ }
+
+ {
+ // Check gets through mutable objects.
+ const Message& message_int32_int32 = mmf_int32_int32->Get(i);
+ int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_key);
+ int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_value);
+ EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
+
+ const Message& message_int32_double = mmf_int32_double->Get(i);
+ int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
+ message_int32_double, fd_map_int32_double_key);
+ double value_int32_double =
+ message_int32_double.GetReflection()->GetDouble(
+ message_int32_double, fd_map_int32_double_value);
+ EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
+
+ const Message& message_string_string = mmf_string_string->Get(i);
+ string key_string_string =
+ message_string_string.GetReflection()->GetString(
+ message_string_string, fd_map_string_string_key);
+ 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));
+
+ const Message& message_int32_message = mmf_int32_foreign_message->Get(i);
+ int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
+ message_int32_message, fd_map_int32_foreign_message_key);
+ const ForeignMessage& value_int32_message =
+ down_cast<const ForeignMessage&>(
+ 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));
+ }
+ }
+
+ // Do sets through the RepeatedPtrField objects.
+ for (int i = 0; i < 10; i++) {
+ {
+ Message* message_int32_int32 = mmf_int32_int32->Mutable(i);
+ int32 key_int32_int32 = message_int32_int32->GetReflection()->GetInt32(
+ *message_int32_int32, fd_map_int32_in32_key);
+ message_int32_int32->GetReflection()->SetInt32(message_int32_int32,
+ fd_map_int32_in32_value,
+ Func(key_int32_int32, -1));
+
+ Message* message_int32_double = mmf_int32_double->Mutable(i);
+ int32 key_int32_double = message_int32_double->GetReflection()->GetInt32(
+ *message_int32_double, fd_map_int32_double_key);
+ message_int32_double->GetReflection()->SetDouble(
+ message_int32_double, fd_map_int32_double_value,
+ Func(key_int32_double, -2));
+
+ Message* message_string_string = mmf_string_string->Mutable(i);
+ string key_string_string =
+ message_string_string->GetReflection()->GetString(
+ *message_string_string, fd_map_string_string_key);
+ message_string_string->GetReflection()->SetString(
+ message_string_string, fd_map_string_string_value,
+ StrFunc(Int(key_string_string), -5));
+
+ Message* message_int32_message = mmf_int32_foreign_message->Mutable(i);
+ int32 key_int32_message =
+ message_int32_message->GetReflection()->GetInt32(
+ *message_int32_message, fd_map_int32_foreign_message_key);
+ ForeignMessage* value_int32_message = down_cast<ForeignMessage*>(
+ message_int32_message->GetReflection()
+ ->MutableMessage(message_int32_message,
+ fd_map_int32_foreign_message_value));
+ value_int32_message->set_c(Func(key_int32_message, -6));
+ }
+ }
+
+ // Check gets through mutable objects.
+ for (int i = 0; i < 10; i++) {
+ EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
+ EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
+ EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
+ EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
+ }
+}
+
+TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) {
+ TestMap message;
+ const Reflection* refl = message.GetReflection();
+ const Descriptor* desc = message.GetDescriptor();
+
+ Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
+ Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
+ Map<string, string>* map_string_string = message.mutable_map_string_string();
+ Map<int32, ForeignMessage>* map_int32_foreign_message =
+ message.mutable_map_int32_foreign_message();
+
+ for (int i = 0; i < 10; ++i) {
+ (*map_int32_int32)[i] = Func(i, 1);
+ (*map_int32_double)[i] = Func(i, 2);
+ (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
+ (*map_int32_foreign_message)[i].set_c(Func(i, 6));
+ }
+
+ // Get FieldDescriptors for all the fields of interest.
+ const FieldDescriptor* fd_map_int32_int32 =
+ desc->FindFieldByName("map_int32_int32");
+ const FieldDescriptor* fd_map_int32_double =
+ desc->FindFieldByName("map_int32_double");
+ const FieldDescriptor* fd_map_string_string =
+ desc->FindFieldByName("map_string_string");
+ const FieldDescriptor* fd_map_int32_foreign_message =
+ desc->FindFieldByName("map_int32_foreign_message");
+
+ const FieldDescriptor* fd_map_int32_in32_key =
+ fd_map_int32_int32->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_in32_value =
+ fd_map_int32_int32->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_int32_double_key =
+ fd_map_int32_double->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_double_value =
+ fd_map_int32_double->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_string_string_key =
+ fd_map_string_string->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_string_string_value =
+ fd_map_string_string->message_type()->FindFieldByName("value");
+ const FieldDescriptor* fd_map_int32_foreign_message_key =
+ fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
+ const FieldDescriptor* fd_map_int32_foreign_message_value =
+ fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
+
+ // Get RepeatedFieldRef objects for all fields of interest.
+ const RepeatedFieldRef<Message> mf_int32_int32 =
+ refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32);
+ const RepeatedFieldRef<Message> mf_int32_double =
+ refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double);
+ const RepeatedFieldRef<Message> mf_string_string =
+ refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string);
+ const RepeatedFieldRef<Message> mf_int32_foreign_message =
+ refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message);
+
+ // Get mutable RepeatedFieldRef objects for all fields of interest.
+ const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
+ refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32);
+ const MutableRepeatedFieldRef<Message> mmf_int32_double =
+ refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double);
+ const MutableRepeatedFieldRef<Message> mmf_string_string =
+ refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string);
+ const MutableRepeatedFieldRef<Message>
+ mmf_int32_foreign_message =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &message, fd_map_int32_foreign_message);
+
+ // Get entry default instances
+ google::protobuf::scoped_ptr<Message> entry_int32_int32(
+ MessageFactory::generated_factory()
+ ->GetPrototype(fd_map_int32_int32->message_type())
+ ->New());
+ google::protobuf::scoped_ptr<Message> entry_int32_double(
+ MessageFactory::generated_factory()
+ ->GetPrototype(fd_map_int32_double->message_type())
+ ->New());
+ google::protobuf::scoped_ptr<Message> entry_string_string(
+ MessageFactory::generated_factory()
+ ->GetPrototype(fd_map_string_string->message_type())
+ ->New());
+ google::protobuf::scoped_ptr<Message> entry_int32_foreign_message(
+ MessageFactory::generated_factory()
+ ->GetPrototype(fd_map_int32_foreign_message->message_type())
+ ->New());
+
+ EXPECT_EQ(10, mf_int32_int32.size());
+ EXPECT_EQ(10, mmf_int32_int32.size());
+ EXPECT_EQ(10, mf_int32_double.size());
+ EXPECT_EQ(10, mmf_int32_double.size());
+ EXPECT_EQ(10, mf_string_string.size());
+ EXPECT_EQ(10, mmf_string_string.size());
+ EXPECT_EQ(10, mf_int32_foreign_message.size());
+ EXPECT_EQ(10, mmf_int32_foreign_message.size());
+
+ EXPECT_FALSE(mf_int32_int32.empty());
+ EXPECT_FALSE(mmf_int32_int32.empty());
+ EXPECT_FALSE(mf_int32_double.empty());
+ EXPECT_FALSE(mmf_int32_double.empty());
+ EXPECT_FALSE(mf_string_string.empty());
+ EXPECT_FALSE(mmf_string_string.empty());
+ EXPECT_FALSE(mf_int32_foreign_message.empty());
+ EXPECT_FALSE(mmf_int32_foreign_message.empty());
+
+ // Make sure we can do gets through the RepeatedFieldRef objects.
+ for (int i = 0; i < 10; ++i) {
+ {
+ // Check gets through const objects.
+ const Message& message_int32_int32 =
+ mf_int32_int32.Get(i, entry_int32_int32.get());
+ int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_key);
+ int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_value);
+ EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
+
+ const Message& message_int32_double =
+ mf_int32_double.Get(i, entry_int32_double.get());
+ int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
+ message_int32_double, fd_map_int32_double_key);
+ double value_int32_double =
+ message_int32_double.GetReflection()->GetDouble(
+ message_int32_double, fd_map_int32_double_value);
+ EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
+
+ const Message& message_string_string =
+ mf_string_string.Get(i, entry_string_string.get());
+ string key_string_string =
+ message_string_string.GetReflection()->GetString(
+ message_string_string, fd_map_string_string_key);
+ 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));
+
+ const Message& message_int32_message =
+ mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
+ int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
+ message_int32_message, fd_map_int32_foreign_message_key);
+ const ForeignMessage& value_int32_message =
+ down_cast<const ForeignMessage&>(
+ 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));
+ }
+
+ {
+ // Check gets through mutable objects.
+ const Message& message_int32_int32 =
+ mmf_int32_int32.Get(i, entry_int32_int32.get());
+ int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_key);
+ int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_value);
+ EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
+
+ const Message& message_int32_double =
+ mmf_int32_double.Get(i, entry_int32_double.get());
+ int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
+ message_int32_double, fd_map_int32_double_key);
+ double value_int32_double =
+ message_int32_double.GetReflection()->GetDouble(
+ message_int32_double, fd_map_int32_double_value);
+ EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
+
+ const Message& message_string_string =
+ mmf_string_string.Get(i, entry_string_string.get());
+ string key_string_string =
+ message_string_string.GetReflection()->GetString(
+ message_string_string, fd_map_string_string_key);
+ 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));
+
+ const Message& message_int32_message =
+ mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
+ int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
+ message_int32_message, fd_map_int32_foreign_message_key);
+ const ForeignMessage& value_int32_message =
+ down_cast<const ForeignMessage&>(
+ 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));
+ }
+ }
+
+ // Make sure we can do sets through the RepeatedFieldRef objects.
+ for (int i = 0; i < 10; i++) {
+ const Message& message_int32_int32 =
+ mmf_int32_int32.Get(i, entry_int32_int32.get());
+ int key = message_int32_int32.GetReflection()->GetInt32(
+ message_int32_int32, fd_map_int32_in32_key);
+
+ entry_int32_int32->GetReflection()->SetInt32(
+ entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
+ key);
+ entry_int32_int32->GetReflection()->SetInt32(
+ entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
+ Func(key, -1));
+ entry_int32_double->GetReflection()->SetInt32(
+ entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
+ key);
+ entry_int32_double->GetReflection()->SetDouble(
+ entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
+ Func(key, -2));
+ entry_string_string->GetReflection()->SetString(
+ entry_string_string.get(),
+ fd_map_string_string->message_type()->field(0), StrFunc(key, 1));
+ entry_string_string->GetReflection()->SetString(
+ entry_string_string.get(),
+ fd_map_string_string->message_type()->field(1), StrFunc(key, -5));
+ entry_int32_foreign_message->GetReflection()->SetInt32(
+ entry_int32_foreign_message.get(),
+ fd_map_int32_foreign_message->message_type()->field(0), key);
+ Message* value_message =
+ entry_int32_foreign_message->GetReflection()->MutableMessage(
+ entry_int32_foreign_message.get(),
+ fd_map_int32_foreign_message->message_type()->field(1));
+ value_message->GetReflection()->SetInt32(
+ value_message, value_message->GetDescriptor()->FindFieldByName("c"),
+ Func(key, -6));
+
+ mmf_int32_int32.Set(i, *entry_int32_int32);
+ mmf_int32_double.Set(i, *entry_int32_double);
+ mmf_string_string.Set(i, *entry_string_string);
+ mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message);
+ }
+
+ for (int i = 0; i < 10; i++) {
+ EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
+ EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
+ EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
+ EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
+ }
+
+ // Test iterators.
+ {
+ int index = 0;
+ hash_map<int32, int32> result;
+ for (RepeatedFieldRef<Message>::iterator it = mf_int32_int32.begin();
+ it != mf_int32_int32.end(); ++it) {
+ const Message& message = *it;
+ int32 key =
+ message.GetReflection()->GetInt32(message, fd_map_int32_in32_key);
+ int32 value =
+ message.GetReflection()->GetInt32(message, fd_map_int32_in32_value);
+ result[key] = value;
+ ++index;
+ }
+ EXPECT_EQ(10, index);
+ for (hash_map<int32, int32>::const_iterator it = result.begin();
+ it != result.end(); ++it) {
+ EXPECT_EQ(message.map_int32_int32().at(it->first), it->second);
+ }
+ }
+
+ {
+ int index = 0;
+ hash_map<int32, double> result;
+ for (RepeatedFieldRef<Message>::iterator it = mf_int32_double.begin();
+ it != mf_int32_double.end(); ++it) {
+ const Message& message = *it;
+ int32 key =
+ message.GetReflection()->GetInt32(message, fd_map_int32_double_key);
+ double value = message.GetReflection()->GetDouble(
+ message, fd_map_int32_double_value);
+ result[key] = value;
+ ++index;
+ }
+ EXPECT_EQ(10, index);
+ for (hash_map<int32, double>::const_iterator it = result.begin();
+ it != result.end(); ++it) {
+ EXPECT_EQ(message.map_int32_double().at(it->first), it->second);
+ }
+ }
+
+ {
+ int index = 0;
+ hash_map<string, string> result;
+ for (RepeatedFieldRef<Message>::iterator it = mf_string_string.begin();
+ it != mf_string_string.end(); ++it) {
+ const Message& message = *it;
+ string key =
+ message.GetReflection()->GetString(message, fd_map_string_string_key);
+ string value = message.GetReflection()->GetString(
+ message, fd_map_string_string_value);
+ result[key] = value;
+ ++index;
+ }
+ EXPECT_EQ(10, index);
+ for (hash_map<string, string>::const_iterator it = result.begin();
+ it != result.end(); ++it) {
+ EXPECT_EQ(message.map_string_string().at(it->first), it->second);
+ }
+ }
+
+ {
+ int index = 0;
+ std::map<int32, ForeignMessage> result;
+ for (RepeatedFieldRef<Message>::iterator it =
+ mf_int32_foreign_message.begin();
+ it != mf_int32_foreign_message.end(); ++it) {
+ const Message& message = *it;
+ int32 key = message.GetReflection()->GetInt32(
+ message, fd_map_int32_foreign_message_key);
+ const ForeignMessage& sub_message = down_cast<const ForeignMessage&>(
+ message.GetReflection()
+ ->GetMessage(message, fd_map_int32_foreign_message_value));
+ result[key].MergeFrom(sub_message);
+ ++index;
+ }
+ EXPECT_EQ(10, index);
+ for (std::map<int32, ForeignMessage>::const_iterator it = result.begin();
+ it != result.end(); ++it) {
+ EXPECT_EQ(message.map_int32_foreign_message().at(it->first).c(),
+ it->second.c());
+ }
+ }
+
+ // Test MutableRepeatedFieldRef::Add()
+ entry_int32_int32->GetReflection()->SetInt32(
+ entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
+ 4321);
+ entry_int32_int32->GetReflection()->SetInt32(
+ entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
+ 1234);
+ mmf_int32_int32.Add(*entry_int32_int32);
+ EXPECT_EQ(1234, message.map_int32_int32().at(4321));
+
+ entry_int32_double->GetReflection()->SetInt32(
+ entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
+ 4321);
+ entry_int32_double->GetReflection()->SetDouble(
+ entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
+ 1234.0);
+ mmf_int32_double.Add(*entry_int32_double);
+ 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->GetReflection()->SetString(
+ entry_string_string.get(), fd_map_string_string->message_type()->field(1),
+ "1234");
+ mmf_string_string.Add(*entry_string_string);
+ EXPECT_EQ("1234", message.map_string_string().at("4321"));
+
+ entry_int32_foreign_message->GetReflection()->SetInt32(
+ entry_int32_foreign_message.get(),
+ fd_map_int32_foreign_message->message_type()->field(0), 4321);
+ Message* value_message =
+ entry_int32_foreign_message->GetReflection()->MutableMessage(
+ entry_int32_foreign_message.get(),
+ fd_map_int32_foreign_message->message_type()->field(1));
+ ForeignMessage foreign_message;
+ foreign_message.set_c(1234);
+ value_message->CopyFrom(foreign_message);
+
+ mmf_int32_foreign_message.Add(*entry_int32_foreign_message);
+ EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c());
+
+ // Test MutableRepeatedFieldRef::RemoveLast()
+ mmf_int32_int32.RemoveLast();
+ mmf_int32_double.RemoveLast();
+ mmf_string_string.RemoveLast();
+ mmf_int32_foreign_message.RemoveLast();
+ EXPECT_EQ(10, message.map_int32_int32().size());
+ EXPECT_EQ(10, message.map_int32_double().size());
+ EXPECT_EQ(10, message.map_string_string().size());
+ EXPECT_EQ(10, message.map_int32_foreign_message().size());
+
+ // Test MutableRepeatedFieldRef::SwapElements()
+ {
+ const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get());
+ int32 int32_value0a =
+ message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value);
+ const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get());
+ int32 int32_value9a =
+ message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value);
+
+ mmf_int32_int32.SwapElements(0, 9);
+
+ const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get());
+ int32 int32_value0b =
+ message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value);
+ const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get());
+ int32 int32_value9b =
+ message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value);
+
+ EXPECT_EQ(int32_value9a, int32_value0b);
+ EXPECT_EQ(int32_value0a, int32_value9b);
+ }
+
+ {
+ const Message& message0a =
+ mmf_int32_double.Get(0, entry_int32_double.get());
+ double double_value0a = message0a.GetReflection()->GetDouble(
+ message0a, fd_map_int32_double_value);
+ const Message& message9a =
+ mmf_int32_double.Get(9, entry_int32_double.get());
+ double double_value9a = message9a.GetReflection()->GetDouble(
+ message9a, fd_map_int32_double_value);
+
+ mmf_int32_double.SwapElements(0, 9);
+
+ const Message& message0b =
+ mmf_int32_double.Get(0, entry_int32_double.get());
+ double double_value0b = message0b.GetReflection()->GetDouble(
+ message0b, fd_map_int32_double_value);
+ const Message& message9b =
+ mmf_int32_double.Get(9, entry_int32_double.get());
+ double double_value9b = message9b.GetReflection()->GetDouble(
+ message9b, fd_map_int32_double_value);
+
+ EXPECT_EQ(double_value9a, double_value0b);
+ EXPECT_EQ(double_value0a, double_value9b);
+ }
+
+ {
+ const Message& message0a =
+ mmf_string_string.Get(0, entry_string_string.get());
+ 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(
+ 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(
+ message0b, fd_map_string_string_value);
+ const Message& message9b =
+ mmf_string_string.Get(9, entry_string_string.get());
+ string string_value9b = message9b.GetReflection()->GetString(
+ message9b, fd_map_string_string_value);
+
+ EXPECT_EQ(string_value9a, string_value0b);
+ EXPECT_EQ(string_value0a, string_value9b);
+ }
+
+ {
+ const Message& message0a =
+ mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
+ const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>(
+ 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<const ForeignMessage&>(
+ 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<const ForeignMessage&>(
+ 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<const ForeignMessage&>(
+ message9b.GetReflection()
+ ->GetMessage(message9b, fd_map_int32_foreign_message_value));
+ int32 int32_value9b = sub_message9b.c();
+
+ EXPECT_EQ(int32_value9a, int32_value0b);
+ EXPECT_EQ(int32_value0a, int32_value9b);
+ }
+}
+
+TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
+ // Set-up message content.
+ TestMap m0, m1, m2;
+ for (int i = 0; i < 10; ++i) {
+ (*m0.mutable_map_int32_int32())[i] = Func(i, 1);
+ (*m0.mutable_map_int32_double())[i] = Func(i, 2);
+ (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5);
+ (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6));
+ (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11);
+ (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12);
+ (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15);
+ (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16));
+ (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21);
+ (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22);
+ (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25);
+ (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26));
+ }
+
+ const Reflection* refl = m0.GetReflection();
+ const Descriptor* desc = m0.GetDescriptor();
+
+ // Get FieldDescriptors for all the fields of interest.
+ const FieldDescriptor* fd_map_int32_int32 =
+ desc->FindFieldByName("map_int32_int32");
+ const FieldDescriptor* fd_map_int32_double =
+ desc->FindFieldByName("map_int32_double");
+ const FieldDescriptor* fd_map_string_string =
+ desc->FindFieldByName("map_string_string");
+ const FieldDescriptor* fd_map_int32_foreign_message =
+ desc->FindFieldByName("map_int32_foreign_message");
+
+ // Get MutableRepeatedFieldRef objects for all fields of interest.
+ const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m0, fd_map_int32_int32);
+ const MutableRepeatedFieldRef<Message> mmf_int32_double =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m0, fd_map_int32_double);
+ const MutableRepeatedFieldRef<Message> mmf_string_string =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m0, fd_map_string_string);
+ const MutableRepeatedFieldRef<Message>
+ mmf_int32_foreign_message =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m0, fd_map_int32_foreign_message);
+
+ // Test MutableRepeatedRef::CopyFrom
+ mmf_int32_int32.CopyFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m1, fd_map_int32_int32));
+ mmf_int32_double.CopyFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m1, fd_map_int32_double));
+ mmf_string_string.CopyFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m1, fd_map_string_string));
+ mmf_int32_foreign_message.CopyFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ 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));
+ EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10));
+ EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1)));
+ EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c());
+ }
+
+ // Test MutableRepeatedRef::MergeFrom
+ mmf_int32_int32.MergeFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m2, fd_map_int32_int32));
+ mmf_int32_double.MergeFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m2, fd_map_int32_double));
+ mmf_string_string.MergeFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ m2, fd_map_string_string));
+ mmf_int32_foreign_message.MergeFrom(
+ refl->GetRepeatedFieldRef<Message>(
+ 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));
+ EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
+ EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
+ }
+
+ // Test MutableRepeatedRef::Swap
+ // Swap between m0 and m2.
+ mmf_int32_int32.Swap(
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m2, fd_map_int32_int32));
+ mmf_int32_double.Swap(
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m2, fd_map_int32_double));
+ mmf_string_string.Swap(
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &m2, fd_map_string_string));
+ mmf_int32_foreign_message.Swap(
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &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));
+ EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
+ EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
+ EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
+
+ // Check the content of m2.
+ EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10));
+ EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10));
+ EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1)));
+ EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c());
+ EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20));
+ EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20));
+ EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1)));
+ EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c());
+ }
+
+ // TODO(teboring): add test for duplicated key
+}
+
+// Generated Message Test ===========================================
+
+TEST(GeneratedMapFieldTest, Accessors) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFields(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+
+ MapTestUtil::ModifyMapFields(&message);
+ MapTestUtil::ExpectMapFieldsModified(message);
+}
+
+TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFieldsInitialized(&message);
+ MapTestUtil::ExpectMapFieldsSetInitialized(message);
+}
+
+TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) {
+ unittest::TestEnumStartWithNonZeroMap message;
+ EXPECT_EQ(unittest::PROTO2_NON_ZERO_MAP_ENUM_FOO,
+ (*message.mutable_map_field())[0]);
+}
+
+TEST(GeneratedMapFieldTest, Clear) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFields(&message);
+ message.Clear();
+ MapTestUtil::ExpectClear(message);
+}
+
+TEST(GeneratedMapFieldTest, ClearMessageMap) {
+ unittest::TestMessageMap message;
+
+ // Creates a TestAllTypes with default value
+ TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]);
+}
+
+TEST(GeneratedMapFieldTest, CopyFrom) {
+ unittest::TestMap message1, message2;
+
+ MapTestUtil::SetMapFields(&message1);
+ message2.CopyFrom(message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+
+ // Copying from self should be a no-op.
+ message2.CopyFrom(message2);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldTest, CopyFromMessageMap) {
+ unittest::TestMessageMap message1, message2;
+
+ (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
+ (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
+
+ message1.CopyFrom(message2);
+
+ // Checks repeated field is overwritten.
+ EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
+ EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
+}
+
+TEST(GeneratedMapFieldTest, SwapWithEmpty) {
+ unittest::TestMap message1, message2;
+
+ MapTestUtil::SetMapFields(&message1);
+ MapTestUtil::ExpectMapFieldsSet(message1);
+ MapTestUtil::ExpectClear(message2);
+
+ message1.Swap(&message2);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+ MapTestUtil::ExpectClear(message1);
+}
+
+TEST(GeneratedMapFieldTest, SwapWithSelf) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFields(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+
+ message.Swap(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+}
+
+TEST(GeneratedMapFieldTest, SwapWithOther) {
+ unittest::TestMap message1, message2;
+
+ MapTestUtil::SetMapFields(&message1);
+ MapTestUtil::SetMapFields(&message2);
+ MapTestUtil::ModifyMapFields(&message2);
+
+ message1.Swap(&message2);
+ MapTestUtil::ExpectMapFieldsModified(message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldTest, CopyConstructor) {
+ unittest::TestMap message1;
+ MapTestUtil::SetMapFields(&message1);
+
+ unittest::TestMap message2(message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldTest, CopyAssignmentOperator) {
+ unittest::TestMap message1;
+ MapTestUtil::SetMapFields(&message1);
+
+ unittest::TestMap message2;
+ message2 = message1;
+ MapTestUtil::ExpectMapFieldsSet(message2);
+
+ // Make sure that self-assignment does something sane.
+ message2.operator=(message2);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \
+ !defined(GOOGLE_PROTOBUF_NO_RTTI)
+TEST(GeneratedMapFieldTest, UpcastCopyFrom) {
+ // Test the CopyFrom method that takes in the generic const Message&
+ // parameter.
+ unittest::TestMap message1, message2;
+
+ MapTestUtil::SetMapFields(&message1);
+
+ const Message* source = implicit_cast<const Message*>(&message1);
+ message2.CopyFrom(*source);
+
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+#endif
+
+#ifndef PROTOBUF_TEST_NO_DESCRIPTORS
+
+TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) {
+ // Test copying from a DynamicMessage, which must fall back to using
+ // reflection.
+ unittest::TestMap message2;
+
+ // Construct a new version of the dynamic message via the factory.
+ DynamicMessageFactory factory;
+ google::protobuf::scoped_ptr<Message> message1;
+ message1.reset(
+ factory.GetPrototype(unittest::TestMap::descriptor())->New());
+
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.SetMapFieldsViaReflection(message1.get());
+ reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
+
+ message2.CopyFrom(*message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) {
+ // Test copying to a DynamicMessage, which must fall back to using reflection.
+ unittest::TestMap message2;
+ MapTestUtil::SetMapFields(&message2);
+
+ // Construct a new version of the dynamic message via the factory.
+ DynamicMessageFactory factory;
+ google::protobuf::scoped_ptr<Message> message1;
+ message1.reset(
+ factory.GetPrototype(unittest::TestMap::descriptor())->New());
+
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ message1->MergeFrom(message2);
+ reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
+}
+
+#endif // !PROTOBUF_TEST_NO_DESCRIPTORS
+
+TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) {
+ unittest::TestMap message1, message2;
+
+ MapTestUtil::SetMapFields(&message1);
+
+ // This field will test merging into an empty spot.
+ (*message2.mutable_map_int32_int32())[1] = 1;
+ message1.mutable_map_int32_int32()->erase(1);
+
+ // This tests overwriting.
+ (*message2.mutable_map_int32_double())[1] = 1;
+ (*message1.mutable_map_int32_double())[1] = 2;
+
+ message1.MergeFrom(message2);
+ MapTestUtil::ExpectMapFieldsSet(message1);
+}
+
+TEST(GeneratedMapFieldTest, MergeFromMessageMap) {
+ unittest::TestMessageMap message1, message2;
+
+ (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
+ (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
+
+ message1.MergeFrom(message2);
+
+ // Checks repeated field is overwritten.
+ EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
+ EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
+}
+
+// Test the generated SerializeWithCachedSizesToArray()
+TEST(GeneratedMapFieldTest, SerializationToArray) {
+ unittest::TestMap message1, message2;
+ string data;
+ MapTestUtil::SetMapFields(&message1);
+ int size = message1.ByteSize();
+ data.resize(size);
+ uint8* start = reinterpret_cast<uint8*>(string_as_array(&data));
+ uint8* end = message1.SerializeWithCachedSizesToArray(start);
+ EXPECT_EQ(size, end - start);
+ EXPECT_TRUE(message2.ParseFromString(data));
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+// Test the generated SerializeWithCachedSizes()
+TEST(GeneratedMapFieldTest, SerializationToStream) {
+ unittest::TestMap message1, message2;
+ MapTestUtil::SetMapFields(&message1);
+ int size = message1.ByteSize();
+ string data;
+ data.resize(size);
+ {
+ // Allow the output stream to buffer only one byte at a time.
+ io::ArrayOutputStream array_stream(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));
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+
+TEST(GeneratedMapFieldTest, SameTypeMaps) {
+ const Descriptor* map1 = unittest::TestSameTypeMap::descriptor()
+ ->FindFieldByName("map1")
+ ->message_type();
+ const Descriptor* map2 = unittest::TestSameTypeMap::descriptor()
+ ->FindFieldByName("map2")
+ ->message_type();
+
+ const Message* map1_entry =
+ MessageFactory::generated_factory()->GetPrototype(map1);
+ const Message* map2_entry =
+ MessageFactory::generated_factory()->GetPrototype(map2);
+
+ EXPECT_EQ(map1, map1_entry->GetDescriptor());
+ EXPECT_EQ(map2, map2_entry->GetDescriptor());
+}
+
+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;
+ from.SerializeToString(&data);
+
+ unittest::TestEnumMap to;
+ EXPECT_TRUE(to.ParseFromString(data));
+ EXPECT_EQ(0, to.unknown_map_field().size());
+ const UnknownFieldSet& unknown_field_set =
+ to.GetReflection()->GetUnknownFields(to);
+ EXPECT_EQ(1, unknown_field_set.field_count());
+ EXPECT_EQ(1, to.known_map_field().size());
+ EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
+
+ data.clear();
+ from.Clear();
+ to.SerializeToString(&data);
+ EXPECT_TRUE(from.ParseFromString(data));
+ EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count());
+ EXPECT_EQ(1, from.known_map_field().size());
+ EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0));
+ EXPECT_EQ(1, from.unknown_map_field().size());
+ EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0));
+}
+
+TEST(GeneratedMapFieldTest, StandardWireFormat) {
+ unittest::TestMap message;
+ string data = "\x0A\x04\x08\x01\x10\x01";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(1, message.map_int32_int32().at(1));
+}
+
+TEST(GeneratedMapFieldTest, UnorderedWireFormat) {
+ unittest::TestMap message;
+
+ // put value before key in wire format
+ string data = "\x0A\x04\x10\x01\x08\x02";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(1, message.map_int32_int32().at(2));
+}
+
+TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) {
+ unittest::TestMap message;
+
+ // Two key fields in wire format
+ string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(1, message.map_int32_int32().at(2));
+}
+
+TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) {
+ unittest::TestMap message;
+
+ // Two value fields in wire format
+ string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(2, message.map_int32_int32().at(1));
+}
+
+TEST(GeneratedMapFieldTest, MissedKeyWireFormat) {
+ unittest::TestMap message;
+
+ // No key field in wire format
+ string data = "\x0A\x02\x10\x01";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(1, message.map_int32_int32().at(0));
+}
+
+TEST(GeneratedMapFieldTest, MissedValueWireFormat) {
+ unittest::TestMap message;
+
+ // No value field in wire format
+ string data = "\x0A\x02\x08\x01";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(0, message.map_int32_int32().at(1));
+}
+
+TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) {
+ unittest::TestMap message;
+
+ // Unknown field in wire format
+ string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
+
+ EXPECT_TRUE(message.ParseFromString(data));
+ EXPECT_EQ(1, message.map_int32_int32().size());
+ EXPECT_EQ(3, message.map_int32_int32().at(2));
+}
+
+TEST(GeneratedMapFieldTest, CorruptedWireFormat) {
+ unittest::TestMap message;
+
+ // corrupted data in wire format
+ string data = "\x0A\x06\x08\x02\x11\x03";
+
+ EXPECT_FALSE(message.ParseFromString(data));
+}
+
+TEST(GeneratedMapFieldTest, MessageLiteMap) {
+ unittest::MapLite from, to;
+ (*from.mutable_map_field())[1] = 1;
+
+ string data;
+ from.SerializeToString(&data);
+ to.ParseFromString(data);
+
+ EXPECT_EQ(1, to.map_field().size());
+ EXPECT_EQ(1, to.map_field().at(1));
+}
+
+TEST(GeneratedMapFieldTest, IsInitialized) {
+ unittest::TestRequiredMessageMap map_message;
+
+ // Add an uninitialized message.
+ (*map_message.mutable_map_field())[0];
+ EXPECT_FALSE(map_message.IsInitialized());
+
+ // Initialize uninitialized message
+ (*map_message.mutable_map_field())[0].set_a(0);
+ (*map_message.mutable_map_field())[0].set_b(0);
+ (*map_message.mutable_map_field())[0].set_c(0);
+ EXPECT_TRUE(map_message.IsInitialized());
+}
+
+// Generated Message Reflection Test ================================
+
+TEST(GeneratedMapFieldReflectionTest, SpaceUsed) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.SetMapFieldsViaReflection(&message);
+
+ EXPECT_LT(0, message.GetReflection()->SpaceUsed(message));
+}
+
+TEST(GeneratedMapFieldReflectionTest, Accessors) {
+ // Set every field to a unique value then go back and check all those
+ // values.
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.SetMapFieldsViaReflection(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+ reflection_tester.ExpectMapFieldsSetViaReflection(message);
+
+ reflection_tester.ModifyMapFieldsViaReflection(&message);
+ MapTestUtil::ExpectMapFieldsModified(message);
+}
+
+TEST(GeneratedMapFieldReflectionTest, Swap) {
+ unittest::TestMap message1;
+ unittest::TestMap message2;
+
+ MapTestUtil::SetMapFields(&message1);
+
+ const Reflection* reflection = message1.GetReflection();
+ reflection->Swap(&message1, &message2);
+
+ MapTestUtil::ExpectClear(message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) {
+ unittest::TestMap message1;
+ unittest::TestMap message2;
+
+ MapTestUtil::SetMapFields(&message1);
+ MapTestUtil::SetMapFields(&message2);
+ MapTestUtil::ModifyMapFields(&message2);
+
+ const Reflection* reflection = message1.GetReflection();
+ reflection->Swap(&message1, &message2);
+
+ MapTestUtil::ExpectMapFieldsModified(message1);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(GeneratedMapFieldReflectionTest, SwapFields) {
+ unittest::TestMap message1;
+ unittest::TestMap message2;
+
+ MapTestUtil::SetMapFields(&message2);
+
+ vector<const FieldDescriptor*> fields;
+ const Reflection* reflection = message1.GetReflection();
+ reflection->ListFields(message2, &fields);
+ reflection->SwapFields(&message1, &message2, fields);
+
+ MapTestUtil::ExpectMapFieldsSet(message1);
+ MapTestUtil::ExpectClear(message2);
+}
+
+TEST(GeneratedMapFieldReflectionTest, ClearField) {
+ unittest::TestMap message;
+ MapTestUtil::SetMapFields(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.ClearMapFieldsViaReflection(&message);
+ MapTestUtil::ExpectClear(message);
+}
+
+TEST(GeneratedMapFieldReflectionTest, RemoveLast) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+
+ MapTestUtil::SetMapFields(&message);
+ MapTestUtil::ExpectMapsSize(message, 2);
+ std::vector<const Message*> expected_entries =
+ MapTestUtil::GetMapEntries(message, 0);
+
+ reflection_tester.RemoveLastMapsViaReflection(&message);
+
+ MapTestUtil::ExpectMapsSize(message, 1);
+ std::vector<const Message*> remained_entries =
+ MapTestUtil::GetMapEntries(message, 0);
+ EXPECT_TRUE(expected_entries == remained_entries);
+}
+
+TEST(GeneratedMapFieldReflectionTest, ReleaseLast) {
+ unittest::TestMap message;
+ const Descriptor* descriptor = message.GetDescriptor();
+ MapTestUtil::MapReflectionTester reflection_tester(descriptor);
+
+ MapTestUtil::SetMapFields(&message);
+
+ MapTestUtil::ExpectMapsSize(message, 2);
+
+ reflection_tester.ReleaseLastMapsViaReflection(&message);
+
+ MapTestUtil::ExpectMapsSize(message, 1);
+
+ // Now test that we actually release the right message.
+ message.Clear();
+ MapTestUtil::SetMapFields(&message);
+
+ MapTestUtil::ExpectMapsSize(message, 2);
+ std::vector<const Message*> expect_last =
+ MapTestUtil::GetMapEntries(message, 1);
+ std::vector<const Message*> release_last =
+ MapTestUtil::GetMapEntriesFromRelease(&message);
+ MapTestUtil::ExpectMapsSize(message, 1);
+ EXPECT_TRUE(expect_last == release_last);
+ for (std::vector<const Message*>::iterator it = release_last.begin();
+ it != release_last.end(); ++it) {
+ delete *it;
+ }
+}
+
+TEST(GeneratedMapFieldReflectionTest, SwapElements) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+
+ MapTestUtil::SetMapFields(&message);
+
+ // Get pointers of map entries at their original position
+ std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0);
+ std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1);
+
+ // Swap the first time.
+ reflection_tester.SwapMapsViaReflection(&message);
+
+ // Get pointer of map entry after swap once.
+ std::vector<const Message*> entries0_once =
+ MapTestUtil::GetMapEntries(message, 0);
+ std::vector<const Message*> entries1_once =
+ MapTestUtil::GetMapEntries(message, 1);
+
+ // Test map entries are swapped.
+ MapTestUtil::ExpectMapsSize(message, 2);
+ EXPECT_TRUE(entries0 == entries1_once);
+ EXPECT_TRUE(entries1 == entries0_once);
+
+ // Swap the second time.
+ reflection_tester.SwapMapsViaReflection(&message);
+
+ // Get pointer of map entry after swap once.
+ std::vector<const Message*> entries0_twice =
+ MapTestUtil::GetMapEntries(message, 0);
+ std::vector<const Message*> entries1_twice =
+ MapTestUtil::GetMapEntries(message, 1);
+
+ // Test map entries are swapped back.
+ MapTestUtil::ExpectMapsSize(message, 2);
+ EXPECT_TRUE(entries0 == entries0_twice);
+ EXPECT_TRUE(entries1 == entries1_twice);
+}
+
+TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
+}
+
+TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) {
+ unittest::TestMessageMap message;
+
+ const FieldDescriptor* map_field =
+ unittest::TestMessageMap::descriptor()->FindFieldByName(
+ "map_int32_message");
+ const FieldDescriptor* value =
+ map_field->message_type()->FindFieldByName("value");
+
+ Message* entry_message =
+ message.GetReflection()->AddMessage(&message, map_field);
+ EXPECT_EQ(
+ &entry_message->GetReflection()->GetMessage(*entry_message, value),
+ reinterpret_cast<const Message*>(&TestAllTypes::default_instance()));
+
+ Message* proto2_message =
+ entry_message->GetReflection()->MutableMessage(entry_message, value);
+ EXPECT_EQ(unittest::TestAllTypes::descriptor(),
+ proto2_message->GetDescriptor());
+ ASSERT_EQ(1, message.map_int32_message().size());
+}
+
+TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) {
+ unittest::TestMap message;
+ const FieldDescriptor* map_field =
+ unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32");
+ const FieldDescriptor* key =
+ map_field->message_type()->FindFieldByName("key");
+ const FieldDescriptor* value =
+ map_field->message_type()->FindFieldByName("value");
+
+ Message* entry_message1 =
+ message.GetReflection()->AddMessage(&message, map_field);
+ EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
+ EXPECT_FALSE(
+ entry_message1->GetReflection()->HasField(*entry_message1, value));
+
+ Message* entry_message2 =
+ message.GetReflection()->AddMessage(&message, map_field);
+ EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key));
+ EXPECT_FALSE(
+ entry_message2->GetReflection()->HasField(*entry_message2, value));
+
+ entry_message1->MergeFrom(*entry_message2);
+ EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
+ EXPECT_FALSE(
+ entry_message1->GetReflection()->HasField(*entry_message1, value));
+}
+
+TEST(GeneratedMapFieldReflectionTest, MapEntryClear) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester reflection_tester(
+ unittest::TestMap::descriptor());
+ reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
+}
+
+TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) {
+ unittest::TestEnumStartWithNonZeroMap message;
+ const Descriptor* descriptor = message.GetDescriptor();
+ const FieldDescriptor* field_descriptor =
+ descriptor->FindFieldByName("map_field");
+ const FieldDescriptor* value_descriptor =
+ field_descriptor->message_type()->FindFieldByName("value");
+ Message* sub_message =
+ message.GetReflection()->AddMessage(&message, field_descriptor);
+ EXPECT_EQ(1, sub_message->GetReflection()->GetEnumValue(*sub_message,
+ value_descriptor));
+}
+
+// Dynamic Message Test =============================================
+
+class MapFieldInDynamicMessageTest : public testing::Test {
+ protected:
+ const DescriptorPool* pool_;
+ DynamicMessageFactory factory_;
+ const Descriptor* map_descriptor_;
+ const Message* map_prototype_;
+
+ MapFieldInDynamicMessageTest()
+ : pool_(DescriptorPool::generated_pool()), factory_(pool_) {}
+
+ virtual void SetUp() {
+ map_descriptor_ =
+ pool_->FindMessageTypeByName("protobuf_unittest.TestMap");
+ ASSERT_TRUE(map_descriptor_ != NULL);
+ map_prototype_ = factory_.GetPrototype(map_descriptor_);
+ }
+};
+
+TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) {
+ // Check that all fields have independent offsets by setting each
+ // one to a unique value then checking that they all still have those
+ // unique values (i.e. they don't stomp each other).
+ scoped_ptr<Message> message(map_prototype_->New());
+ MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_);
+
+ reflection_tester.SetMapFieldsViaReflection(message.get());
+ reflection_tester.ExpectMapFieldsSetViaReflection(*message);
+}
+
+TEST_F(MapFieldInDynamicMessageTest, Map) {
+ // Check that map fields work properly.
+ scoped_ptr<Message> message(map_prototype_->New());
+
+ // Check set functions.
+ MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_);
+ reflection_tester.SetMapFieldsViaReflection(message.get());
+ reflection_tester.ExpectMapFieldsSetViaReflection(*message);
+}
+
+TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) {
+ // Test that SpaceUsed() works properly
+
+ // Since we share the implementation with generated messages, we don't need
+ // to test very much here. Just make sure it appears to be working.
+
+ scoped_ptr<Message> message(map_prototype_->New());
+ MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_);
+
+ int initial_space_used = message->SpaceUsed();
+
+ reflection_tester.SetMapFieldsViaReflection(message.get());
+ EXPECT_LT(initial_space_used, message->SpaceUsed());
+}
+
+// ReflectionOps Test ===============================================
+
+TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFields(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+}
+
+TEST(ReflectionOpsForMapFieldTest, MapCopy) {
+ unittest::TestMap message, message2;
+
+ MapTestUtil::SetMapFields(&message);
+
+ ReflectionOps::Copy(message, &message2);
+
+ MapTestUtil::ExpectMapFieldsSet(message2);
+
+ // Copying from self should be a no-op.
+ ReflectionOps::Copy(message2, &message2);
+ MapTestUtil::ExpectMapFieldsSet(message2);
+}
+
+TEST(ReflectionOpsForMapFieldTest, MergeMap) {
+ // Note: Copy is implemented in terms of Merge() so technically the Copy
+ // test already tested most of this.
+
+ unittest::TestMap message, message2;
+
+ MapTestUtil::SetMapFields(&message);
+
+ ReflectionOps::Merge(message2, &message);
+
+ MapTestUtil::ExpectMapFieldsSet(message);
+}
+
+TEST(ReflectionOpsForMapFieldTest, ClearMap) {
+ unittest::TestMap message;
+
+ MapTestUtil::SetMapFields(&message);
+
+ ReflectionOps::Clear(&message);
+
+ MapTestUtil::ExpectClear(message);
+}
+
+TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) {
+ unittest::TestMap message;
+ MapTestUtil::SetMapFields(&message);
+
+ // Set some unknown fields in message.
+ message.GetReflection()->MutableUnknownFields(&message)->
+ AddVarint(123456, 654321);
+
+ // Discard them.
+ ReflectionOps::DiscardUnknownFields(&message);
+ MapTestUtil::ExpectMapFieldsSet(message);
+
+ EXPECT_EQ(0, message.GetReflection()->
+ GetUnknownFields(message).field_count());
+}
+
+// Wire Format Test =================================================
+
+TEST(WireFormatForMapFieldTest, ParseMap) {
+ unittest::TestMap source, dest;
+ string data;
+
+ // Serialize using the generated code.
+ MapTestUtil::SetMapFields(&source);
+ source.SerializeToString(&data);
+
+ // Parse using WireFormat.
+ io::ArrayInputStream raw_input(data.data(), data.size());
+ io::CodedInputStream input(&raw_input);
+ WireFormat::ParseAndMergePartial(&input, &dest);
+
+ // Check.
+ MapTestUtil::ExpectMapFieldsSet(dest);
+}
+
+TEST(WireFormatForMapFieldTest, MapByteSize) {
+ unittest::TestMap message;
+ MapTestUtil::SetMapFields(&message);
+
+ EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
+ message.Clear();
+ EXPECT_EQ(0, message.ByteSize());
+ EXPECT_EQ(0, WireFormat::ByteSize(message));
+}
+
+TEST(WireFormatForMapFieldTest, SerializeMap) {
+ unittest::TestMap message;
+ string generated_data;
+ string dynamic_data;
+
+ MapTestUtil::SetMapFields(&message);
+
+ // Serialize using the generated code.
+ {
+ message.ByteSize();
+ io::StringOutputStream raw_output(&generated_data);
+ io::CodedOutputStream output(&raw_output);
+ message.SerializeWithCachedSizes(&output);
+ ASSERT_FALSE(output.HadError());
+ }
+
+ // Serialize using WireFormat.
+ {
+ io::StringOutputStream raw_output(&dynamic_data);
+ io::CodedOutputStream output(&raw_output);
+ int size = WireFormat::ByteSize(message);
+ WireFormat::SerializeWithCachedSizes(message, size, &output);
+ ASSERT_FALSE(output.HadError());
+ }
+
+ // Should be the same.
+ // Don't use EXPECT_EQ here because we're comparing raw binary data and
+ // we really don't want it dumped to stdout on failure.
+ EXPECT_TRUE(dynamic_data == generated_data);
+}
+
+TEST(WireFormatForMapFieldTest, MapParseHelpers) {
+ string data;
+
+ {
+ // Set up.
+ protobuf_unittest::TestMap message;
+ MapTestUtil::SetMapFields(&message);
+ message.SerializeToString(&data);
+ }
+
+ {
+ // Test ParseFromString.
+ protobuf_unittest::TestMap message;
+ EXPECT_TRUE(message.ParseFromString(data));
+ MapTestUtil::ExpectMapFieldsSet(message);
+ }
+
+ {
+ // Test ParseFromIstream.
+ protobuf_unittest::TestMap message;
+ stringstream stream(data);
+ EXPECT_TRUE(message.ParseFromIstream(&stream));
+ EXPECT_TRUE(stream.eof());
+ MapTestUtil::ExpectMapFieldsSet(message);
+ }
+
+ {
+ // Test ParseFromBoundedZeroCopyStream.
+ 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;
+ EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
+ MapTestUtil::ExpectMapFieldsSet(message);
+ }
+
+ {
+ // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
+ // EOF is reached before the expected number of bytes.
+ io::ArrayInputStream stream(data.data(), data.size());
+ protobuf_unittest::TestAllTypes message;
+ EXPECT_FALSE(
+ message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
+ }
+}
+
+// Text Format Test =================================================
+
+TEST(TextFormatMapTest, SerializeAndParse) {
+ unittest::TestMap source;
+ unittest::TestMap dest;
+ MapTestUtil::SetMapFields(&source);
+ string output;
+
+ // Test compact ASCII
+ TextFormat::Printer printer;
+ printer.PrintToString(source, &output);
+ TextFormat::Parser parser;
+ EXPECT_TRUE(parser.ParseFromString(output, &dest));
+ MapTestUtil::ExpectMapFieldsSet(dest);
+}
+
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/map_test_util.cc b/src/google/protobuf/map_test_util.cc
new file mode 100644
index 00000000..eb7ea511
--- /dev/null
+++ b/src/google/protobuf/map_test_util.cc
@@ -0,0 +1,1479 @@
+// 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 <google/protobuf/map_test_util.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/message.h>
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/testing/googletest.h>
+#include <gtest/gtest.h>
+
+namespace google {
+namespace protobuf {
+
+void MapTestUtil::SetMapFields(unittest::TestMap* message) {
+ // Add first element.
+ (*message->mutable_map_int32_int32())[0] = 0;
+ (*message->mutable_map_int64_int64())[0] = 0;
+ (*message->mutable_map_uint32_uint32())[0] = 0;
+ (*message->mutable_map_uint64_uint64())[0] = 0;
+ (*message->mutable_map_sint32_sint32())[0] = 0;
+ (*message->mutable_map_sint64_sint64())[0] = 0;
+ (*message->mutable_map_fixed32_fixed32())[0] = 0;
+ (*message->mutable_map_fixed64_fixed64())[0] = 0;
+ (*message->mutable_map_sfixed32_sfixed32())[0] = 0;
+ (*message->mutable_map_sfixed64_sfixed64())[0] = 0;
+ (*message->mutable_map_int32_float())[0] = 0.0;
+ (*message->mutable_map_int32_double())[0] = 0.0;
+ (*message->mutable_map_bool_bool())[0] = false;
+ (*message->mutable_map_string_string())["0"] = "0";
+ (*message->mutable_map_int32_bytes())[0] = "0";
+ (*message->mutable_map_int32_enum())[0] =
+ unittest::MAP_ENUM_BAR;
+ (*message->mutable_map_int32_foreign_message())[0].set_c(0);
+
+ // Add second element
+ (*message->mutable_map_int32_int32())[1] = 1;
+ (*message->mutable_map_int64_int64())[1] = 1;
+ (*message->mutable_map_uint32_uint32())[1] = 1;
+ (*message->mutable_map_uint64_uint64())[1] = 1;
+ (*message->mutable_map_sint32_sint32())[1] = 1;
+ (*message->mutable_map_sint64_sint64())[1] = 1;
+ (*message->mutable_map_fixed32_fixed32())[1] = 1;
+ (*message->mutable_map_fixed64_fixed64())[1] = 1;
+ (*message->mutable_map_sfixed32_sfixed32())[1] = 1;
+ (*message->mutable_map_sfixed64_sfixed64())[1] = 1;
+ (*message->mutable_map_int32_float())[1] = 1.0;
+ (*message->mutable_map_int32_double())[1] = 1.0;
+ (*message->mutable_map_bool_bool())[1] = true;
+ (*message->mutable_map_string_string())["1"] = "1";
+ (*message->mutable_map_int32_bytes())[1] = "1";
+ (*message->mutable_map_int32_enum())[1] =
+ unittest::MAP_ENUM_BAZ;
+ (*message->mutable_map_int32_foreign_message())[1].set_c(1);
+}
+
+void MapTestUtil::SetMapFieldsInitialized(unittest::TestMap* message) {
+ // Add first element using bracket operator, which should assign default
+ // value automatically.
+ (*message->mutable_map_int32_int32())[0];
+ (*message->mutable_map_int64_int64())[0];
+ (*message->mutable_map_uint32_uint32())[0];
+ (*message->mutable_map_uint64_uint64())[0];
+ (*message->mutable_map_sint32_sint32())[0];
+ (*message->mutable_map_sint64_sint64())[0];
+ (*message->mutable_map_fixed32_fixed32())[0];
+ (*message->mutable_map_fixed64_fixed64())[0];
+ (*message->mutable_map_sfixed32_sfixed32())[0];
+ (*message->mutable_map_sfixed64_sfixed64())[0];
+ (*message->mutable_map_int32_float())[0];
+ (*message->mutable_map_int32_double())[0];
+ (*message->mutable_map_bool_bool())[0];
+ (*message->mutable_map_string_string())["0"];
+ (*message->mutable_map_int32_bytes())[0];
+ (*message->mutable_map_int32_enum())[0];
+ (*message->mutable_map_int32_foreign_message())[0];
+}
+
+void MapTestUtil::ModifyMapFields(unittest::TestMap* message) {
+ (*message->mutable_map_int32_int32())[1] = 2;
+ (*message->mutable_map_int64_int64())[1] = 2;
+ (*message->mutable_map_uint32_uint32())[1] = 2;
+ (*message->mutable_map_uint64_uint64())[1] = 2;
+ (*message->mutable_map_sint32_sint32())[1] = 2;
+ (*message->mutable_map_sint64_sint64())[1] = 2;
+ (*message->mutable_map_fixed32_fixed32())[1] = 2;
+ (*message->mutable_map_fixed64_fixed64())[1] = 2;
+ (*message->mutable_map_sfixed32_sfixed32())[1] = 2;
+ (*message->mutable_map_sfixed64_sfixed64())[1] = 2;
+ (*message->mutable_map_int32_float())[1] = 2.0;
+ (*message->mutable_map_int32_double())[1] = 2.0;
+ (*message->mutable_map_bool_bool())[1] = false;
+ (*message->mutable_map_string_string())["1"] = "2";
+ (*message->mutable_map_int32_bytes())[1] = "2";
+ (*message->mutable_map_int32_enum())[1] =
+ unittest::MAP_ENUM_FOO;
+ (*message->mutable_map_int32_foreign_message())[1].set_c(2);
+}
+
+void MapTestUtil::ExpectClear(const unittest::TestMap& message) {
+ EXPECT_EQ(0, message.map_int32_int32().size());
+ EXPECT_EQ(0, message.map_int64_int64().size());
+ EXPECT_EQ(0, message.map_uint32_uint32().size());
+ EXPECT_EQ(0, message.map_uint64_uint64().size());
+ EXPECT_EQ(0, message.map_sint32_sint32().size());
+ EXPECT_EQ(0, message.map_sint64_sint64().size());
+ EXPECT_EQ(0, message.map_fixed32_fixed32().size());
+ EXPECT_EQ(0, message.map_fixed64_fixed64().size());
+ EXPECT_EQ(0, message.map_sfixed32_sfixed32().size());
+ EXPECT_EQ(0, message.map_sfixed64_sfixed64().size());
+ EXPECT_EQ(0, message.map_int32_float().size());
+ EXPECT_EQ(0, message.map_int32_double().size());
+ EXPECT_EQ(0, message.map_bool_bool().size());
+ EXPECT_EQ(0, message.map_string_string().size());
+ EXPECT_EQ(0, message.map_int32_bytes().size());
+ EXPECT_EQ(0, message.map_int32_enum().size());
+ EXPECT_EQ(0, message.map_int32_foreign_message().size());
+}
+
+void MapTestUtil::ExpectMapFieldsSet(const unittest::TestMap& message) {
+ EXPECT_EQ(2, message.map_int32_int32().size());
+ EXPECT_EQ(2, message.map_int64_int64().size());
+ EXPECT_EQ(2, message.map_uint32_uint32().size());
+ EXPECT_EQ(2, message.map_uint64_uint64().size());
+ EXPECT_EQ(2, message.map_sint32_sint32().size());
+ EXPECT_EQ(2, message.map_sint64_sint64().size());
+ EXPECT_EQ(2, message.map_fixed32_fixed32().size());
+ EXPECT_EQ(2, message.map_fixed64_fixed64().size());
+ EXPECT_EQ(2, message.map_sfixed32_sfixed32().size());
+ EXPECT_EQ(2, message.map_sfixed64_sfixed64().size());
+ EXPECT_EQ(2, message.map_int32_float().size());
+ EXPECT_EQ(2, message.map_int32_double().size());
+ EXPECT_EQ(2, message.map_bool_bool().size());
+ EXPECT_EQ(2, message.map_string_string().size());
+ EXPECT_EQ(2, message.map_int32_bytes().size());
+ EXPECT_EQ(2, message.map_int32_enum().size());
+ EXPECT_EQ(2, message.map_int32_foreign_message().size());
+
+ EXPECT_EQ(0, message.map_int32_int32().at(0));
+ EXPECT_EQ(0, message.map_int64_int64().at(0));
+ EXPECT_EQ(0, message.map_uint32_uint32().at(0));
+ EXPECT_EQ(0, message.map_uint64_uint64().at(0));
+ EXPECT_EQ(0, message.map_sint32_sint32().at(0));
+ EXPECT_EQ(0, message.map_sint64_sint64().at(0));
+ EXPECT_EQ(0, message.map_fixed32_fixed32().at(0));
+ EXPECT_EQ(0, message.map_fixed64_fixed64().at(0));
+ EXPECT_EQ(0, message.map_sfixed32_sfixed32().at(0));
+ EXPECT_EQ(0, message.map_sfixed64_sfixed64().at(0));
+ EXPECT_EQ(0, message.map_int32_float().at(0));
+ EXPECT_EQ(0, message.map_int32_double().at(0));
+ EXPECT_EQ(false, message.map_bool_bool().at(0));
+ EXPECT_EQ("0", message.map_string_string().at("0"));
+ EXPECT_EQ("0", message.map_int32_bytes().at(0));
+ EXPECT_EQ(unittest::MAP_ENUM_BAR, message.map_int32_enum().at(0));
+ EXPECT_EQ(0, message.map_int32_foreign_message().at(0).c());
+
+ EXPECT_EQ(1, message.map_int32_int32().at(1));
+ EXPECT_EQ(1, message.map_int64_int64().at(1));
+ EXPECT_EQ(1, message.map_uint32_uint32().at(1));
+ EXPECT_EQ(1, message.map_uint64_uint64().at(1));
+ EXPECT_EQ(1, message.map_sint32_sint32().at(1));
+ EXPECT_EQ(1, message.map_sint64_sint64().at(1));
+ EXPECT_EQ(1, message.map_fixed32_fixed32().at(1));
+ EXPECT_EQ(1, message.map_fixed64_fixed64().at(1));
+ EXPECT_EQ(1, message.map_sfixed32_sfixed32().at(1));
+ EXPECT_EQ(1, message.map_sfixed64_sfixed64().at(1));
+ EXPECT_EQ(1, message.map_int32_float().at(1));
+ EXPECT_EQ(1, message.map_int32_double().at(1));
+ EXPECT_EQ(true, message.map_bool_bool().at(1));
+ EXPECT_EQ("1", message.map_string_string().at("1"));
+ EXPECT_EQ("1", message.map_int32_bytes().at(1));
+ EXPECT_EQ(unittest::MAP_ENUM_BAZ, message.map_int32_enum().at(1));
+ EXPECT_EQ(1, message.map_int32_foreign_message().at(1).c());
+}
+
+void MapTestUtil::ExpectMapFieldsSetInitialized(
+ const unittest::TestMap& 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());
+ EXPECT_EQ(1, message.map_uint64_uint64().size());
+ EXPECT_EQ(1, message.map_sint32_sint32().size());
+ EXPECT_EQ(1, message.map_sint64_sint64().size());
+ EXPECT_EQ(1, message.map_fixed32_fixed32().size());
+ EXPECT_EQ(1, message.map_fixed64_fixed64().size());
+ EXPECT_EQ(1, message.map_sfixed32_sfixed32().size());
+ EXPECT_EQ(1, message.map_sfixed64_sfixed64().size());
+ EXPECT_EQ(1, message.map_int32_float().size());
+ EXPECT_EQ(1, message.map_int32_double().size());
+ EXPECT_EQ(1, message.map_bool_bool().size());
+ EXPECT_EQ(1, message.map_string_string().size());
+ EXPECT_EQ(1, message.map_int32_bytes().size());
+ EXPECT_EQ(1, message.map_int32_enum().size());
+ EXPECT_EQ(1, message.map_int32_foreign_message().size());
+
+ EXPECT_EQ(0, message.map_int32_int32().at(0));
+ EXPECT_EQ(0, message.map_int64_int64().at(0));
+ EXPECT_EQ(0, message.map_uint32_uint32().at(0));
+ EXPECT_EQ(0, message.map_uint64_uint64().at(0));
+ EXPECT_EQ(0, message.map_sint32_sint32().at(0));
+ EXPECT_EQ(0, message.map_sint64_sint64().at(0));
+ EXPECT_EQ(0, message.map_fixed32_fixed32().at(0));
+ EXPECT_EQ(0, message.map_fixed64_fixed64().at(0));
+ EXPECT_EQ(0, message.map_sfixed32_sfixed32().at(0));
+ EXPECT_EQ(0, message.map_sfixed64_sfixed64().at(0));
+ EXPECT_EQ(0, message.map_int32_float().at(0));
+ EXPECT_EQ(0, message.map_int32_double().at(0));
+ EXPECT_EQ(false, message.map_bool_bool().at(0));
+ EXPECT_EQ("", message.map_string_string().at("0"));
+ EXPECT_EQ("", message.map_int32_bytes().at(0));
+ EXPECT_EQ(unittest::MAP_ENUM_FOO, message.map_int32_enum().at(0));
+ EXPECT_EQ(0, message.map_int32_foreign_message().at(0).ByteSize());
+}
+
+void MapTestUtil::ExpectMapFieldsModified(
+ const unittest::TestMap& 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.
+ EXPECT_EQ(2, message.map_int32_int32().size());
+ EXPECT_EQ(2, message.map_int64_int64().size());
+ EXPECT_EQ(2, message.map_uint32_uint32().size());
+ EXPECT_EQ(2, message.map_uint64_uint64().size());
+ EXPECT_EQ(2, message.map_sint32_sint32().size());
+ EXPECT_EQ(2, message.map_sint64_sint64().size());
+ EXPECT_EQ(2, message.map_fixed32_fixed32().size());
+ EXPECT_EQ(2, message.map_fixed64_fixed64().size());
+ EXPECT_EQ(2, message.map_sfixed32_sfixed32().size());
+ EXPECT_EQ(2, message.map_sfixed64_sfixed64().size());
+ EXPECT_EQ(2, message.map_int32_float().size());
+ EXPECT_EQ(2, message.map_int32_double().size());
+ EXPECT_EQ(2, message.map_bool_bool().size());
+ EXPECT_EQ(2, message.map_string_string().size());
+ EXPECT_EQ(2, message.map_int32_bytes().size());
+ EXPECT_EQ(2, message.map_int32_enum().size());
+ EXPECT_EQ(2, message.map_int32_foreign_message().size());
+
+ EXPECT_EQ(0, message.map_int32_int32().at(0));
+ EXPECT_EQ(0, message.map_int64_int64().at(0));
+ EXPECT_EQ(0, message.map_uint32_uint32().at(0));
+ EXPECT_EQ(0, message.map_uint64_uint64().at(0));
+ EXPECT_EQ(0, message.map_sint32_sint32().at(0));
+ EXPECT_EQ(0, message.map_sint64_sint64().at(0));
+ EXPECT_EQ(0, message.map_fixed32_fixed32().at(0));
+ EXPECT_EQ(0, message.map_fixed64_fixed64().at(0));
+ EXPECT_EQ(0, message.map_sfixed32_sfixed32().at(0));
+ EXPECT_EQ(0, message.map_sfixed64_sfixed64().at(0));
+ EXPECT_EQ(0, message.map_int32_float().at(0));
+ EXPECT_EQ(0, message.map_int32_double().at(0));
+ EXPECT_EQ(false, message.map_bool_bool().at(0));
+ EXPECT_EQ("0", message.map_string_string().at("0"));
+ EXPECT_EQ("0", message.map_int32_bytes().at(0));
+ EXPECT_EQ(unittest::MAP_ENUM_BAR, message.map_int32_enum().at(0));
+ EXPECT_EQ(0, message.map_int32_foreign_message().at(0).c());
+
+ // Actually verify the second (modified) elements now.
+ EXPECT_EQ(2, message.map_int32_int32().at(1));
+ EXPECT_EQ(2, message.map_int64_int64().at(1));
+ EXPECT_EQ(2, message.map_uint32_uint32().at(1));
+ EXPECT_EQ(2, message.map_uint64_uint64().at(1));
+ EXPECT_EQ(2, message.map_sint32_sint32().at(1));
+ EXPECT_EQ(2, message.map_sint64_sint64().at(1));
+ EXPECT_EQ(2, message.map_fixed32_fixed32().at(1));
+ EXPECT_EQ(2, message.map_fixed64_fixed64().at(1));
+ EXPECT_EQ(2, message.map_sfixed32_sfixed32().at(1));
+ EXPECT_EQ(2, message.map_sfixed64_sfixed64().at(1));
+ EXPECT_EQ(2, message.map_int32_float().at(1));
+ EXPECT_EQ(2, message.map_int32_double().at(1));
+ EXPECT_EQ(false, message.map_bool_bool().at(1));
+ EXPECT_EQ("2", message.map_string_string().at("1"));
+ EXPECT_EQ("2", message.map_int32_bytes().at(1));
+ EXPECT_EQ(unittest::MAP_ENUM_FOO, message.map_int32_enum().at(1));
+ EXPECT_EQ(2, message.map_int32_foreign_message().at(1).c());
+}
+
+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<const Message*> MapTestUtil::GetMapEntries(
+ const unittest::TestMap& message, int index) {
+ const Descriptor* descriptor = message.GetDescriptor();
+ std::vector<const Message*> 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<const Message*> MapTestUtil::GetMapEntriesFromRelease(
+ unittest::TestMap* message) {
+ const Descriptor* descriptor = message->GetDescriptor();
+ std::vector<const Message*> 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;
+}
+
+MapTestUtil::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* MapTestUtil::MapReflectionTester::F(const string& name) {
+ const FieldDescriptor* result = NULL;
+ result = base_descriptor_->FindFieldByName(name);
+ GOOGLE_CHECK(result != NULL);
+ return result;
+}
+
+void MapTestUtil::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 MapTestUtil::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 MapTestUtil::MapReflectionTester::ModifyMapFieldsViaReflection(
+ Message* message) {
+ const Reflection* reflection = message->GetReflection();
+ Message* sub_message;
+ Message* sub_foreign_message;
+
+ // Find out which one's key is 0.
+ int size = reflection->FieldSize(*message, F("map_int32_int32"));
+ int target = 0;
+ for (int i = 0; i < size; i++) {
+ const Message& temp_message = reflection
+ ->GetRepeatedMessage(*message, F("map_int32_int32"), i);
+ if (temp_message.GetReflection()
+ ->GetInt32(temp_message, map_int32_int32_key_) == 1) {
+ target = i;
+ }
+ }
+
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_int32"), target);
+ sub_message->GetReflection()
+ ->SetInt32(sub_message, map_int32_int32_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int64_int64"), target);
+ sub_message->GetReflection()
+ ->SetInt64(sub_message, map_int64_int64_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_uint32_uint32"), target);
+ sub_message->GetReflection()
+ ->SetUInt32(sub_message, map_uint32_uint32_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_uint64_uint64"), target);
+ sub_message->GetReflection()
+ ->SetUInt64(sub_message, map_uint64_uint64_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_sint32_sint32"), target);
+ sub_message->GetReflection()
+ ->SetInt32(sub_message, map_sint32_sint32_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_sint64_sint64"), target);
+ sub_message->GetReflection()
+ ->SetInt64(sub_message, map_sint64_sint64_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_fixed32_fixed32"), target);
+ sub_message->GetReflection()
+ ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_fixed64_fixed64"), target);
+ sub_message->GetReflection()
+ ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_sfixed32_sfixed32"), target);
+ sub_message->GetReflection()
+ ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_sfixed64_sfixed64"), target);
+ sub_message->GetReflection()
+ ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 2);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_float"), target);
+ sub_message->GetReflection()
+ ->SetFloat(sub_message, map_int32_float_val_, 2.0);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_double"), target);
+ sub_message->GetReflection()
+ ->SetDouble(sub_message, map_int32_double_val_, 2.0);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_bool_bool"), target);
+ sub_message->GetReflection()
+ ->SetBool(sub_message, map_bool_bool_val_, false);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_string_string"), target);
+ sub_message->GetReflection()
+ ->SetString(sub_message, map_string_string_val_, "2");
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_bytes"), target);
+ sub_message->GetReflection()
+ ->SetString(sub_message, map_int32_bytes_val_, "2");
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_enum"), target);
+ sub_message->GetReflection()
+ ->SetEnum(sub_message, map_int32_enum_val_, map_enum_foo_);
+ sub_message = reflection
+ ->MutableRepeatedMessage(message, F("map_int32_foreign_message"), target);
+ 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_, 2);
+}
+
+void MapTestUtil::MapReflectionTester::RemoveLastMapsViaReflection(
+ Message* message) {
+ const Reflection* reflection = message->GetReflection();
+
+ vector<const FieldDescriptor*> 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 MapTestUtil::MapReflectionTester::ReleaseLastMapsViaReflection(
+ Message* message) {
+ const Reflection* reflection = message->GetReflection();
+
+ vector<const FieldDescriptor*> 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 MapTestUtil::MapReflectionTester::SwapMapsViaReflection(Message* message) {
+ const Reflection* reflection = message->GetReflection();
+ vector<const FieldDescriptor*> 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 MapTestUtil::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 MapTestUtil::MapReflectionTester::ExpectMapFieldsSetViaReflection(
+ const Message& message) {
+ string scratch;
+ const Reflection* reflection = message.GetReflection();
+ const Message* sub_message;
+
+ // -----------------------------------------------------------------
+
+ 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<int32, int32> map;
+ map[0] = 0;
+ map[1] = 1;
+ for (int i = 0; i < 2; i++) {
+ 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);
+ }
+ }
+ {
+ std::map<int64, int64> map;
+ map[0] = 0;
+ map[1] = 1;
+ for (int i = 0; i < 2; i++) {
+ 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);
+ }
+ }
+ {
+ std::map<uint32, uint32> map;
+ map[0] = 0;
+ map[1] = 1;
+ for (int i = 0; i < 2; i++) {
+ 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);
+ }
+ }
+ {
+ std::map<uint64, uint64> 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);
+ }
+ }
+ {
+ std::map<int32, int32> 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);
+ }
+ }
+ {
+ std::map<int64, int64> 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);
+ }
+ }
+ {
+ std::map<uint32, uint32> 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);
+ }
+ }
+ {
+ std::map<uint64, uint64> 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);
+ }
+ }
+ {
+ std::map<int32, int32> 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);
+ }
+ }
+ {
+ std::map<int64, int64> 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);
+ }
+ }
+ {
+ std::map<int32, float> 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);
+ }
+ }
+ {
+ std::map<int32, double> 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);
+ }
+ }
+ {
+ std::map<bool, bool> 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);
+ }
+ }
+ {
+ std::map<string, string> 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);
+ }
+ }
+ {
+ std::map<int32, string> 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);
+ }
+ }
+ {
+ std::map<int32, const EnumValueDescriptor*> 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);
+ }
+ }
+ {
+ std::map<int32, int32> 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);
+ }
+ }
+}
+
+void MapTestUtil::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 MapTestUtil::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));
+ }
+ // 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
new file mode 100644
index 00000000..653cf107
--- /dev/null
+++ b/src/google/protobuf/map_test_util.h
@@ -0,0 +1,149 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_MAP_TEST_UTIL_H__
+#define GOOGLE_PROTOBUF_MAP_TEST_UTIL_H__
+
+#include <google/protobuf/map_unittest.pb.h>
+
+namespace google {
+namespace protobuf {
+
+namespace unittest = ::protobuf_unittest;
+
+class MapTestUtil {
+ public:
+ // Set every field in the message to a unique value.
+ static void SetMapFields(unittest::TestMap* message);
+
+ // Set every field in the message to a default value.
+ static void SetMapFieldsInitialized(unittest::TestMap* message);
+
+ // Modify all the map fields of the messsage (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
+ // 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<const Message*> GetMapEntries(
+ const unittest::TestMap& message, int index);
+
+ // Get pointers of map entries from release.
+ static std::vector<const Message*> GetMapEntriesFromRelease(
+ unittest::TestMap* message);
+
+ // Like above, but use the reflection interface.
+ class MapReflectionTester {
+ public:
+ // base_descriptor must be a descriptor for TestMap, which is used for
+ // MapReflectionTester to fetch the FieldDescriptors needed to use the
+ // reflection interface.
+ explicit MapReflectionTester(const Descriptor* base_descriptor);
+
+ void SetMapFieldsViaReflection(Message* message);
+ void ClearMapFieldsViaReflection(Message* message);
+ void ModifyMapFieldsViaReflection(Message* message);
+ void RemoveLastMapsViaReflection(Message* message);
+ void ReleaseLastMapsViaReflection(Message* message);
+ void SwapMapsViaReflection(Message* message);
+ void MutableUnknownFieldsOfMapFieldsViaReflection(Message* message);
+ void ExpectMapFieldsSetViaReflection(const Message& message);
+ void ExpectClearViaReflection(const Message& message);
+ void ExpectMapEntryClearViaReflection(Message* message);
+
+ private:
+ const FieldDescriptor* F(const string& name);
+
+ const Descriptor* base_descriptor_;
+
+ const EnumValueDescriptor* map_enum_bar_;
+ const EnumValueDescriptor* map_enum_baz_;
+ const EnumValueDescriptor* map_enum_foo_;
+
+ const FieldDescriptor* foreign_c_;
+ const FieldDescriptor* map_int32_int32_key_;
+ const FieldDescriptor* map_int32_int32_val_;
+ const FieldDescriptor* map_int64_int64_key_;
+ const FieldDescriptor* map_int64_int64_val_;
+ const FieldDescriptor* map_uint32_uint32_key_;
+ const FieldDescriptor* map_uint32_uint32_val_;
+ const FieldDescriptor* map_uint64_uint64_key_;
+ const FieldDescriptor* map_uint64_uint64_val_;
+ const FieldDescriptor* map_sint32_sint32_key_;
+ const FieldDescriptor* map_sint32_sint32_val_;
+ const FieldDescriptor* map_sint64_sint64_key_;
+ const FieldDescriptor* map_sint64_sint64_val_;
+ const FieldDescriptor* map_fixed32_fixed32_key_;
+ const FieldDescriptor* map_fixed32_fixed32_val_;
+ const FieldDescriptor* map_fixed64_fixed64_key_;
+ const FieldDescriptor* map_fixed64_fixed64_val_;
+ const FieldDescriptor* map_sfixed32_sfixed32_key_;
+ const FieldDescriptor* map_sfixed32_sfixed32_val_;
+ const FieldDescriptor* map_sfixed64_sfixed64_key_;
+ const FieldDescriptor* map_sfixed64_sfixed64_val_;
+ const FieldDescriptor* map_int32_float_key_;
+ const FieldDescriptor* map_int32_float_val_;
+ const FieldDescriptor* map_int32_double_key_;
+ const FieldDescriptor* map_int32_double_val_;
+ const FieldDescriptor* map_bool_bool_key_;
+ const FieldDescriptor* map_bool_bool_val_;
+ const FieldDescriptor* map_string_string_key_;
+ const FieldDescriptor* map_string_string_val_;
+ const FieldDescriptor* map_int32_bytes_key_;
+ const FieldDescriptor* map_int32_bytes_val_;
+ const FieldDescriptor* map_int32_enum_key_;
+ const FieldDescriptor* map_int32_enum_val_;
+ const FieldDescriptor* map_int32_foreign_message_key_;
+ const FieldDescriptor* map_int32_foreign_message_val_;
+ };
+};
+
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_MAP_TEST_UTIL_H__
diff --git a/src/google/protobuf/map_type_handler.h b/src/google/protobuf/map_type_handler.h
new file mode 100644
index 00000000..88a6d7b8
--- /dev/null
+++ b/src/google/protobuf/map_type_handler.h
@@ -0,0 +1,486 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_TYPE_HANDLER_H__
+#define GOOGLE_PROTOBUF_TYPE_HANDLER_H__
+
+#include <google/protobuf/generated_message_util.h>
+#include <google/protobuf/message.h>
+#include <google/protobuf/wire_format_lite_inl.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+// Used for compile time type selection. MapIf::type will be TrueType if Flag is
+// true and FalseType otherwise.
+template<bool Flag, typename TrueType, typename FalseType>
+struct MapIf;
+
+template<typename TrueType, typename FalseType>
+struct MapIf<true, TrueType, FalseType> {
+ typedef TrueType type;
+};
+
+template<typename TrueType, typename FalseType>
+struct MapIf<false, TrueType, FalseType> {
+ typedef FalseType type;
+};
+
+// In MapField, string and message are stored as pointer while others are stored
+// as object. However, google::protobuf::Map has unified api. Functions in this class
+// convert key/value to type wanted in api regardless how it's stored
+// internally.
+template <typename Type>
+class MapCommonTypeHandler {
+ public:
+ static inline Type& Reference(Type* x) { return *x; }
+ static inline Type& Reference(Type& x) { return x; }
+ static inline const Type& Reference(const Type& x) { return x; }
+ static inline Type* Pointer(Type* x) { return x; }
+ static inline Type* Pointer(Type& x) { return &x; }
+ static inline const Type* Pointer(const Type* x) { return x; }
+ static inline const Type* Pointer(const Type& x) { return &x; }
+};
+
+// In proto2 Map, enum needs to be initialized to given default value, while
+// other types' default value can be inferred from the type.
+template <bool IsEnum, typename Type>
+class MapValueInitializer {
+ public:
+ static inline void Initialize(Type& type, int default_enum_value);
+};
+
+template <typename Type>
+class MapValueInitializer<true, Type> {
+ public:
+ static inline void Initialize(Type& value, int default_enum_value) {
+ value = static_cast<Type>(default_enum_value);
+ }
+};
+
+template <typename Type>
+class MapValueInitializer<false, Type> {
+ public:
+ static inline void Initialize(Type& value, int default_enum_value) {}
+};
+
+// Handlers for key/value stored type in MapField. ==================
+
+// Handler for message
+template <typename Type>
+class MapCppTypeHandler : public MapCommonTypeHandler<Type> {
+ public:
+ static const bool kIsStringOrMessage = true;
+ // SpaceUsedInMapEntry: Return bytes used by value in MapEntry, excluding
+ // those already calculate in sizeof(MapField).
+ static int SpaceUsedInMapEntry(const Type* value) {
+ return value->SpaceUsed();
+ }
+ // Return bytes used by value in Map.
+ static int SpaceUsedInMap(const Type& value) { return value.SpaceUsed(); }
+ static inline void Clear(Type** value) {
+ if (*value != NULL) (*value)->Type::Clear();
+ }
+ static inline void ClearMaybeByDefaultEnum(Type** value,
+ int default_enum_value) {
+ if (*value != NULL) (*value)->Type::Clear();
+ }
+ static inline void Merge(const Type& from, Type** to) {
+ (*to)->MergeFrom(from);
+ }
+
+ static void Delete(const Type* ptr) { delete ptr; }
+
+ // Assign default value to given instance.
+ static inline void AssignDefaultValue(Type** value) {
+ *value = const_cast<Type*>(&Type::default_instance());
+ }
+ // Initialize value when constructing MapEntry
+ static inline void Initialize(Type** x) { *x = NULL; }
+ // Same as above, but use default_enum_value to initialize enum type value.
+ static inline void InitializeMaybeByDefaultEnum(
+ Type** x, int default_enum_value) {
+ *x = NULL;
+ }
+ // Initialize value for the first time mutable accessor is called.
+ static inline void EnsureMutable(Type** value) {
+ if (*value == NULL) *value = new Type;
+ }
+ // Return default instance if value is not initialized when calling const
+ // reference accessor.
+ static inline const Type& DefaultIfNotInitialized(Type* value,
+ Type* default_value) {
+ return value != NULL ? *value : *default_value;
+ }
+ // Check if all required fields have values set.
+ static inline bool IsInitialized(Type* value) {
+ return value->IsInitialized();
+ }
+};
+
+// Handler for string.
+template <>
+class MapCppTypeHandler<string> : public MapCommonTypeHandler<string> {
+ public:
+ static const bool kIsStringOrMessage = true;
+ static inline void Merge(const string& from, string** to) { **to = from; }
+ static inline void Clear(string** value) { (*value)->clear(); }
+ static inline void ClearMaybeByDefaultEnum(string** value, int default_enum) {
+ (*value)->clear();
+ }
+ static inline int SpaceUsedInMapEntry(const string* value) {
+ return sizeof(*value) + StringSpaceUsedExcludingSelf(*value);
+ }
+ static inline int SpaceUsedInMap(const string& value) {
+ return sizeof(value) + StringSpaceUsedExcludingSelf(value);
+ }
+ static void Delete(const string* ptr) {
+ if (ptr != &::google::protobuf::internal::GetEmptyString()) delete ptr;
+ }
+ static inline void AssignDefaultValue(string** value) {}
+ static inline void Initialize(string** value) {
+ *value = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
+ }
+ static inline void InitializeMaybeByDefaultEnum(
+ string** value, int default_enum_value) {
+ *value = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyString());
+ }
+ static inline void EnsureMutable(string** value) {
+ if (*value == &::google::protobuf::internal::GetEmptyString()) *value = new string;
+ }
+ static inline const string& DefaultIfNotInitialized(string* value,
+ string* default_value) {
+ return value != default_value ? *value : *default_value;
+ }
+ static inline bool IsInitialized(string* value) { return true; }
+};
+
+// Base class for primitive type handlers.
+template <typename Type>
+class MapPrimitiveTypeHandler : public MapCommonTypeHandler<Type> {
+ public:
+ static const bool kIsStringOrMessage = false;
+ static inline void Delete(const Type& x) {}
+ static inline void Merge(const Type& from, Type* to) { *to = from; }
+ static inline int SpaceUsedInMapEntry(const Type& value) { return 0; }
+ static inline int SpaceUsedInMap(const Type& value) { return sizeof(Type); }
+ static inline void AssignDefaultValue(Type* value) {}
+ static inline const Type& DefaultIfNotInitialized(
+ const Type& value, const Type& default_value) {
+ return value;
+ }
+ static inline bool IsInitialized(const Type& value) { return true; }
+};
+
+// Handlers for primitive types.
+#define PRIMITIVE_HANDLER(CType) \
+ template <> \
+ class MapCppTypeHandler<CType> : public MapPrimitiveTypeHandler<CType> { \
+ public: \
+ static inline void Clear(CType* value) { *value = 0; } \
+ static inline void ClearMaybeByDefaultEnum(CType* value, \
+ int default_enum_value) { \
+ *value = static_cast<CType>(default_enum_value); \
+ } \
+ static inline void Initialize(CType* value) { *value = 0; } \
+ static inline void InitializeMaybeByDefaultEnum(CType* value, \
+ int default_enum_value) { \
+ *value = static_cast<CType>(default_enum_value); \
+ } \
+ static inline void EnsureMutable(CType* value) {} \
+ };
+
+PRIMITIVE_HANDLER(int32 )
+PRIMITIVE_HANDLER(int64 )
+PRIMITIVE_HANDLER(uint32)
+PRIMITIVE_HANDLER(uint64)
+PRIMITIVE_HANDLER(double)
+PRIMITIVE_HANDLER(float )
+PRIMITIVE_HANDLER(bool )
+
+#undef PRIMITIVE_HANDLER
+
+// Define constants for given proto field type
+template <FieldDescriptor::Type Type>
+class MapFieldTypeTraits {};
+
+#define TYPE_TRAITS(FieldType, CType, WireFormatType, IsMessage, IsEnum) \
+ template <> \
+ class MapFieldTypeTraits<FieldDescriptor::TYPE_##FieldType> { \
+ public: \
+ typedef CType CppType; \
+ static const bool kIsMessage = IsMessage; \
+ static const bool kIsEnum = IsEnum; \
+ static const WireFormatLite::WireType kWireType = \
+ WireFormatLite::WIRETYPE_##WireFormatType; \
+ };
+
+TYPE_TRAITS(MESSAGE , Message, LENGTH_DELIMITED, true, false)
+TYPE_TRAITS(STRING , string , LENGTH_DELIMITED, false, false)
+TYPE_TRAITS(BYTES , string , 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
+
+// Handler for proto field type. Define types and constants used in compile
+// time. Also define functions used in parsing and serializing.
+template <FieldDescriptor::Type Type>
+class MapProtoTypeHandler {
+ public:
+ // Internal stored type in MapEntry for given proto field type.
+ typedef typename MapFieldTypeTraits<Type>::CppType CppType;
+
+ // Whether given type is a message.
+ static const bool kIsMessage = MapFieldTypeTraits<Type>::kIsMessage;
+
+ // Whether given type is an enum.
+ static const bool kIsEnum = MapFieldTypeTraits<Type>::kIsEnum;
+
+ // The wire type of given proto field type.
+ static const WireFormatLite::WireType kWireType =
+ MapFieldTypeTraits<Type>::kWireType;
+
+ // Functions used in parsing and serialization. ===================
+
+ template <typename ValueType>
+ static inline int ByteSize(const ValueType& value);
+ template <typename ValueType>
+ static inline int GetCachedSize(const ValueType& value);
+ static inline void Write(int field, const CppType& value,
+ io::CodedOutputStream* output);
+ static inline uint8* WriteToArray(int field, const CppType& value,
+ uint8* output);
+ template <typename ValueType>
+ static inline bool Read(io::CodedInputStream* input, ValueType* value);
+};
+
+template <>
+template <typename ValueType>
+inline int MapProtoTypeHandler<FieldDescriptor::TYPE_MESSAGE>::ByteSize(
+ const ValueType& value) {
+ return WireFormatLite::MessageSizeNoVirtual(value);
+}
+
+#define BYTE_SIZE(FieldType, DeclaredType) \
+ template <> \
+ template <typename ValueType> \
+ inline int MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::ByteSize( \
+ const ValueType& value) { \
+ return WireFormatLite::DeclaredType##Size(value); \
+ }
+
+BYTE_SIZE(STRING, String)
+BYTE_SIZE(BYTES , Bytes)
+BYTE_SIZE(INT64 , Int64)
+BYTE_SIZE(UINT64, UInt64)
+BYTE_SIZE(INT32 , Int32)
+BYTE_SIZE(UINT32, UInt32)
+BYTE_SIZE(SINT64, SInt64)
+BYTE_SIZE(SINT32, SInt32)
+BYTE_SIZE(ENUM , Enum)
+
+#undef BYTE_SIZE
+
+#define FIXED_BYTE_SIZE(FieldType, DeclaredType) \
+ template <> \
+ template <typename ValueType> \
+ inline int MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::ByteSize( \
+ const ValueType& value) { \
+ return WireFormatLite::k##DeclaredType##Size; \
+ }
+
+FIXED_BYTE_SIZE(DOUBLE , Double)
+FIXED_BYTE_SIZE(FLOAT , Float)
+FIXED_BYTE_SIZE(FIXED64 , Fixed64)
+FIXED_BYTE_SIZE(FIXED32 , Fixed32)
+FIXED_BYTE_SIZE(SFIXED64, SFixed64)
+FIXED_BYTE_SIZE(SFIXED32, SFixed32)
+FIXED_BYTE_SIZE(BOOL , Bool)
+
+#undef FIXED_BYTE_SIZE
+
+template <>
+template <typename ValueType>
+inline int MapProtoTypeHandler<FieldDescriptor::TYPE_MESSAGE>::GetCachedSize(
+ const ValueType& value) {
+ return WireFormatLite::LengthDelimitedSize(value.GetCachedSize());
+}
+
+#define GET_CACHED_SIZE(FieldType, DeclaredType) \
+ template <> \
+ template <typename ValueType> \
+ inline int \
+ MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::GetCachedSize( \
+ const ValueType& value) { \
+ return WireFormatLite::DeclaredType##Size(value); \
+ }
+
+GET_CACHED_SIZE(STRING, String)
+GET_CACHED_SIZE(BYTES , Bytes)
+GET_CACHED_SIZE(INT64 , Int64)
+GET_CACHED_SIZE(UINT64, UInt64)
+GET_CACHED_SIZE(INT32 , Int32)
+GET_CACHED_SIZE(UINT32, UInt32)
+GET_CACHED_SIZE(SINT64, SInt64)
+GET_CACHED_SIZE(SINT32, SInt32)
+GET_CACHED_SIZE(ENUM , Enum)
+
+#undef GET_CACHED_SIZE
+
+#define GET_FIXED_CACHED_SIZE(FieldType, DeclaredType) \
+ template <> \
+ template <typename ValueType> \
+ inline int \
+ MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::GetCachedSize( \
+ const ValueType& value) { \
+ return WireFormatLite::k##DeclaredType##Size; \
+ }
+
+GET_FIXED_CACHED_SIZE(DOUBLE , Double)
+GET_FIXED_CACHED_SIZE(FLOAT , Float)
+GET_FIXED_CACHED_SIZE(FIXED64 , Fixed64)
+GET_FIXED_CACHED_SIZE(FIXED32 , Fixed32)
+GET_FIXED_CACHED_SIZE(SFIXED64, SFixed64)
+GET_FIXED_CACHED_SIZE(SFIXED32, SFixed32)
+GET_FIXED_CACHED_SIZE(BOOL , Bool)
+
+#undef GET_FIXED_CACHED_SIZE
+
+template <>
+inline void MapProtoTypeHandler<FieldDescriptor::TYPE_MESSAGE>::Write(
+ int field, const Message& value, io::CodedOutputStream* output) {
+ WireFormatLite::WriteMessageMaybeToArray(field, value, output);
+}
+
+template <>
+inline uint8* MapProtoTypeHandler<FieldDescriptor::TYPE_MESSAGE>::WriteToArray(
+ int field, const Message& value, uint8* output) {
+ return WireFormatLite::WriteMessageToArray(field, value, output);
+}
+
+#define WRITE_METHOD(FieldType, DeclaredType) \
+ template <> \
+ inline void MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::Write( \
+ int field, const CppType& value, io::CodedOutputStream* output) { \
+ return WireFormatLite::Write##DeclaredType(field, value, output); \
+ } \
+ template <> \
+ inline uint8* \
+ MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::WriteToArray( \
+ int field, const CppType& value, uint8* output) { \
+ return WireFormatLite::Write##DeclaredType##ToArray(field, value, output); \
+ }
+
+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(SFIXED64, SFixed64)
+WRITE_METHOD(SFIXED32, SFixed32)
+WRITE_METHOD(BOOL , Bool)
+
+#undef WRITE_METHOD
+
+template <>
+template <typename ValueType>
+inline bool MapProtoTypeHandler<FieldDescriptor::TYPE_MESSAGE>::Read(
+ io::CodedInputStream* input, ValueType* value) {
+ return WireFormatLite::ReadMessageNoVirtual(input, value);
+}
+
+template <>
+template <typename ValueType>
+inline bool MapProtoTypeHandler<FieldDescriptor::TYPE_STRING>::Read(
+ io::CodedInputStream* input, ValueType* value) {
+ return WireFormatLite::ReadString(input, value);
+}
+
+template <>
+template <typename ValueType>
+inline bool MapProtoTypeHandler<FieldDescriptor::TYPE_BYTES>::Read(
+ io::CodedInputStream* input, ValueType* value) {
+ return WireFormatLite::ReadBytes(input, value);
+}
+
+#define READ_METHOD(FieldType) \
+ template <> \
+ template <typename ValueType> \
+ inline bool MapProtoTypeHandler<FieldDescriptor::TYPE_##FieldType>::Read( \
+ io::CodedInputStream* input, ValueType* value) { \
+ return WireFormatLite::ReadPrimitive<CppType, \
+ WireFormatLite::TYPE_##FieldType>( \
+ input, value); \
+ }
+
+READ_METHOD(INT64)
+READ_METHOD(UINT64)
+READ_METHOD(INT32)
+READ_METHOD(UINT32)
+READ_METHOD(SINT64)
+READ_METHOD(SINT32)
+READ_METHOD(ENUM)
+READ_METHOD(DOUBLE)
+READ_METHOD(FLOAT)
+READ_METHOD(FIXED64)
+READ_METHOD(FIXED32)
+READ_METHOD(SFIXED64)
+READ_METHOD(SFIXED32)
+READ_METHOD(BOOL)
+
+#undef READ_METHOD
+
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_TYPE_HANDLER_H__
diff --git a/src/google/protobuf/map_unittest.proto b/src/google/protobuf/map_unittest.proto
new file mode 100644
index 00000000..9232d58f
--- /dev/null
+++ b/src/google/protobuf/map_unittest.proto
@@ -0,0 +1,82 @@
+// 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";
+
+
+import "google/protobuf/unittest.proto";
+
+// 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.
+// In map_test_util.h we do "using namespace unittest = protobuf_unittest".
+package protobuf_unittest;
+
+// Tests maps.
+message TestMap {
+ map<int32 , int32 > map_int32_int32 = 1;
+ map<int64 , int64 > map_int64_int64 = 2;
+ map<uint32 , uint32 > map_uint32_uint32 = 3;
+ map<uint64 , uint64 > map_uint64_uint64 = 4;
+ map<sint32 , sint32 > map_sint32_sint32 = 5;
+ map<sint64 , sint64 > map_sint64_sint64 = 6;
+ map<fixed32 , fixed32 > map_fixed32_fixed32 = 7;
+ map<fixed64 , fixed64 > map_fixed64_fixed64 = 8;
+ map<sfixed32, sfixed32> map_sfixed32_sfixed32 = 9;
+ map<sfixed64, sfixed64> map_sfixed64_sfixed64 = 10;
+ map<int32 , float > map_int32_float = 11;
+ map<int32 , double > map_int32_double = 12;
+ map<bool , bool > map_bool_bool = 13;
+ map<string , string > map_string_string = 14;
+ map<int32 , bytes > map_int32_bytes = 15;
+ map<int32 , MapEnum > map_int32_enum = 16;
+ map<int32 , ForeignMessage> map_int32_foreign_message = 17;
+}
+
+message TestMessageMap {
+ map<int32, TestAllTypes> map_int32_message = 1;
+}
+
+// Two map fields share the same entry default instance.
+message TestSameTypeMap {
+ map<int32, int32> map1 = 1;
+ map<int32, int32> map2 = 2;
+}
+
+
+enum MapEnum {
+ MAP_ENUM_FOO = 0;
+ MAP_ENUM_BAR = 1;
+ MAP_ENUM_BAZ = 2;
+}
+
+// Test embeded message with required fields
+message TestRequiredMessageMap {
+ map<int32, TestRequired> map_field = 1;
+}
diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc
index 1324ed9b..afe95461 100644
--- a/src/google/protobuf/message.cc
+++ b/src/google/protobuf/message.cc
@@ -40,6 +40,7 @@
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/once.h>
+#include <google/protobuf/reflection_internal.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/descriptor.pb.h>
@@ -49,6 +50,7 @@
#include <google/protobuf/wire_format.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/map_util.h>
+#include <google/protobuf/stubs/singleton.h>
#include <google/protobuf/stubs/stl_util.h>
namespace google {
@@ -222,6 +224,38 @@ void* Reflection::MutableRawRepeatedString(
}
+// Default EnumValue API implementations. Real reflection implementations should
+// override these. However, there are several legacy implementations that do
+// not, and cannot easily be changed at the same time as the Reflection API, so
+// we provide these for now.
+// TODO: Remove these once all Reflection implementations are updated.
+int Reflection::GetEnumValue(const Message& message,
+ const FieldDescriptor* field) const {
+ GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
+ return 0;
+}
+void Reflection::SetEnumValue(Message* message,
+ const FieldDescriptor* field,
+ int value) const {
+ GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
+}
+int Reflection::GetRepeatedEnumValue(
+ const Message& message,
+ const FieldDescriptor* field, int index) const {
+ GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
+ return 0;
+}
+void Reflection::SetRepeatedEnumValue(Message* message,
+ const FieldDescriptor* field, int index,
+ int value) const {
+ GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
+}
+void Reflection::AddEnumValue(Message* message,
+ const FieldDescriptor* field,
+ int value) const {
+ GOOGLE_LOG(FATAL) << "Unimplemented EnumValue API.";
+}
+
// =============================================================================
// MessageFactory
@@ -354,5 +388,64 @@ 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 internal {
+RepeatedFieldAccessor::~RepeatedFieldAccessor() {
+}
+} // namespace internal
+
+const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
+ const FieldDescriptor* field) const {
+ GOOGLE_CHECK(field->is_repeated());
+ switch (field->cpp_type()) {
+#define HANDLE_PRIMITIVE_TYPE(TYPE, type) \
+ case FieldDescriptor::CPPTYPE_ ## TYPE: \
+ return internal::Singleton<internal::RepeatedFieldPrimitiveAccessor<type> >::get();
+ HANDLE_PRIMITIVE_TYPE(INT32, int32)
+ HANDLE_PRIMITIVE_TYPE(UINT32, uint32)
+ HANDLE_PRIMITIVE_TYPE(INT64, int64)
+ HANDLE_PRIMITIVE_TYPE(UINT64, uint64)
+ HANDLE_PRIMITIVE_TYPE(FLOAT, float)
+ HANDLE_PRIMITIVE_TYPE(DOUBLE, double)
+ HANDLE_PRIMITIVE_TYPE(BOOL, bool)
+ HANDLE_PRIMITIVE_TYPE(ENUM, int32)
+#undef HANDLE_PRIMITIVE_TYPE
+ case FieldDescriptor::CPPTYPE_STRING:
+ switch (field->options().ctype()) {
+ default:
+ case FieldOptions::STRING:
+ return internal::Singleton<internal::RepeatedPtrFieldStringAccessor>::get();
+ }
+ break;
+ case FieldDescriptor::CPPTYPE_MESSAGE:
+ if (field->is_map()) {
+ return internal::Singleton<internal::MapFieldAccessor>::get();
+ } else {
+ return internal::Singleton<internal::RepeatedPtrFieldMessageAccessor>::get();
+ }
+ }
+ GOOGLE_LOG(FATAL) << "Should not reach here.";
+ return NULL;
+}
+
+namespace internal {
+// Macro defined in repeated_field.h. We can only define the Message-specific
+// GenericTypeHandler specializations here because we depend on Message, which
+// is not part of proto2-lite hence is not available in repeated_field.h.
+DEFINE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES_NOINLINE(Message);
+} // namespace internal
+
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/message.h b/src/google/protobuf/message.h
index 95935605..a200bc92 100644
--- a/src/google/protobuf/message.h
+++ b/src/google/protobuf/message.h
@@ -112,8 +112,10 @@
#include <iosfwd>
#include <string>
+#include <google/protobuf/stubs/type_traits.h>
#include <vector>
+#include <google/protobuf/arena.h>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/stubs/common.h>
@@ -121,6 +123,7 @@
#define GOOGLE_PROTOBUF_HAS_ONEOF
+#define GOOGLE_PROTOBUF_HAS_ARENAS
namespace google {
namespace protobuf {
@@ -174,6 +177,17 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
// for return-type covariance.)
virtual Message* New() const = 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
+ // during the Arena transition.
+ virtual Message* New(::google::protobuf::Arena* arena) const {
+ Message* message = New();
+ if (arena != NULL) {
+ arena->Own(message);
+ }
+ return message;
+ }
+
// Make this message into a copy of the given message. The given message
// must have the same descriptor, but need not necessarily be the same class.
// By default this is just implemented as "Clear(); MergeFrom(from);".
@@ -313,6 +327,20 @@ class LIBPROTOBUF_EXPORT Message : public MessageLite {
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
};
+namespace internal {
+// Forward-declare interfaces used to implement RepeatedFieldRef.
+// These are protobuf internals that users shouldn't care about.
+class RepeatedFieldAccessor;
+} // namespace internal
+
+// Forward-declare RepeatedFieldRef templates. The second type parameter is
+// used for SFINAE tricks. Users should ignore it.
+template<typename T, typename Enable = void>
+class RepeatedFieldRef;
+
+template<typename T, typename Enable = void>
+class MutableRepeatedFieldRef;
+
// This interface contains methods that can be used to dynamically access
// and modify the fields of a protocol message. Their semantics are
// similar to the accessors the protocol compiler generates.
@@ -361,12 +389,15 @@ class LIBPROTOBUF_EXPORT 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.
+ // 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;
// 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.
+ // 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;
// Estimate the amount of memory used by the message object.
@@ -385,23 +416,23 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual void ClearField(Message* message,
const FieldDescriptor* field) const = 0;
- // Check if the oneof is set. Returns ture if any field in oneof
+ // 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 {
+ virtual bool HasOneof(const Message& /*message*/,
+ const OneofDescriptor* /*oneof_descriptor*/) const {
return false;
}
- virtual void ClearOneof(Message* message,
- const OneofDescriptor* oneof_descriptor) const {}
+ virtual 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 {
+ const Message& /*message*/,
+ const OneofDescriptor* /*oneof_descriptor*/) const {
return NULL;
}
@@ -464,6 +495,15 @@ class LIBPROTOBUF_EXPORT Reflection {
const FieldDescriptor* field) const = 0;
virtual const EnumValueDescriptor* GetEnum(
const Message& message, const FieldDescriptor* field) const = 0;
+
+ // 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;
+
// See MutableMessage() for the meaning of the "factory" parameter.
virtual const Message& GetMessage(const Message& message,
const FieldDescriptor* field,
@@ -512,6 +552,14 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual void SetEnum (Message* message,
const FieldDescriptor* field,
const EnumValueDescriptor* value) const = 0;
+ // Set an enum field's value with an integer rather than EnumValueDescriptor.
+ // If the value does not correspond to a known enum value, either behavior is
+ // undefined (for proto2 messages), or the value is accepted silently for
+ // messages with new unknown-enum-value semantics.
+ virtual 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;
// otherwise, the default factory is used. If the field is an extension that
@@ -574,6 +622,14 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual const EnumValueDescriptor* GetRepeatedEnum(
const Message& message,
const FieldDescriptor* field, int index) const = 0;
+ // 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;
virtual const Message& GetRepeatedMessage(
const Message& message,
const FieldDescriptor* field, int index) const = 0;
@@ -614,6 +670,13 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual void SetRepeatedEnum(Message* message,
const FieldDescriptor* field, int index,
const EnumValueDescriptor* value) const = 0;
+ // Set an enum field's value with an integer rather than EnumValueDescriptor.
+ // If the value does not correspond to a known enum value, either behavior is
+ // undefined (for proto2 messages), or the value is accepted silently for
+ // messages with new unknown-enum-value semantics.
+ virtual 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(
@@ -643,12 +706,57 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual void AddEnum (Message* message,
const FieldDescriptor* field,
const EnumValueDescriptor* value) const = 0;
+ // Set an enum field's value with an integer rather than EnumValueDescriptor.
+ // If the value does not correspond to a known enum value, either behavior is
+ // undefined (for proto2 messages), or the value is accepted silently for
+ // messages with new unknown-enum-value semantics.
+ virtual 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;
+ // Get a RepeatedFieldRef object that can be used to read the underlying
+ // repeated field. The type parameter T must be set according to the
+ // field's cpp type. The following table shows the mapping from cpp type
+ // to acceptable T.
+ //
+ // field->cpp_type() T
+ // CPPTYPE_INT32 int32
+ // CPPTYPE_UINT32 uint32
+ // CPPTYPE_INT64 int64
+ // CPPTYPE_UINT64 uint64
+ // CPPTYPE_DOUBLE double
+ // CPPTYPE_FLOAT float
+ // CPPTYPE_BOOL bool
+ // CPPTYPE_ENUM generated enum type or int32
+ // CPPTYPE_STRING string
+ // CPPTYPE_MESSAGE generated message type or google::protobuf::Message
+ //
+ // A RepeatedFieldRef object can be copied and the resulted object will point
+ // to the same repeated field in the same message. The object can be used as
+ // long as the message is not destroyed.
+ //
+ // Note that to use this method users need to include the header file
+ // "google/protobuf/reflection.h" (which defines the RepeatedFieldRef
+ // class templates).
+ template<typename T>
+ RepeatedFieldRef<T> 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<typename T>
+ MutableRepeatedFieldRef<T> GetMutableRepeatedFieldRef(
+ Message* message, const FieldDescriptor* field) const;
+
+ // DEPRECATED. Please use Get(Mutable)RepeatedFieldRef() for repeated field
+ // access. The following repeated field accesors will be removed in the
+ // future.
+ //
// Repeated field accessors -------------------------------------------------
// The methods above, e.g. GetRepeatedInt32(msg, fd, index), provide singular
// access to the data in a RepeatedField. The methods below provide aggregate
@@ -659,22 +767,30 @@ class LIBPROTOBUF_EXPORT Reflection {
//
// Usage example: my_doubs = refl->GetRepeatedField<double>(msg, fd);
+ // DEPRECATED. Please use GetRepeatedFieldRef().
+ //
// for T = Cord and all protobuf scalar types except enums.
template<typename T>
const RepeatedField<T>& GetRepeatedField(
const Message&, const FieldDescriptor*) const;
+ // DEPRECATED. Please use GetMutableRepeatedFieldRef().
+ //
// for T = Cord and all protobuf scalar types except enums.
template<typename T>
RepeatedField<T>* MutableRepeatedField(
Message*, const FieldDescriptor*) const;
+ // DEPRECATED. Please use GetRepeatedFieldRef().
+ //
// for T = string, google::protobuf::internal::StringPieceField
// google::protobuf::Message & descendants.
template<typename T>
const RepeatedPtrField<T>& GetRepeatedPtrField(
const Message&, const FieldDescriptor*) const;
+ // DEPRECATED. Please use GetMutableRepeatedFieldRef().
+ //
// for T = string, google::protobuf::internal::StringPieceField
// google::protobuf::Message & descendants.
template<typename T>
@@ -693,6 +809,39 @@ class LIBPROTOBUF_EXPORT Reflection {
virtual const FieldDescriptor* FindKnownExtensionByNumber(
int number) const = 0;
+ // Feature Flags -------------------------------------------------------------
+
+ // Does this message support storing arbitrary integer values in enum fields?
+ // If |true|, GetEnumValue/SetEnumValue and associated repeated-field versions
+ // take arbitrary integer values, and the legacy GetEnum() getter will
+ // dynamically create an EnumValueDescriptor for any integer value without
+ // one. If |false|, setting an unknown enum value via the integer-based
+ // setters results in undefined behavior (in practice, GOOGLE_DCHECK-fails).
+ //
+ // Generic code that uses reflection to handle messages with enum fields
+ // should check this flag before using the integer-based setter, and either
+ // downgrade to a compatible value or use the UnknownFieldSet if not. For
+ // example:
+ //
+ // int new_value = GetValueFromApplicationLogic();
+ // if (reflection->SupportsUnknownEnumValues()) {
+ // reflection->SetEnumValue(message, field, new_value);
+ // } else {
+ // if (field_descriptor->enum_type()->
+ // FindValueByNumver(new_value) != NULL) {
+ // reflection->SetEnumValue(message, field, new_value);
+ // } else if (emit_unknown_enum_values) {
+ // reflection->MutableUnknownFields(message)->AddVarint(
+ // field->number(),
+ // new_value);
+ // } else {
+ // // convert value to a compatible/default value.
+ // new_value = CompatibleDowngrade(new_value);
+ // reflection->SetEnumValue(message, field, new_value);
+ // }
+ // }
+ virtual bool SupportsUnknownEnumValues() const { return false; }
+
// ---------------------------------------------------------------------------
protected:
@@ -705,7 +854,38 @@ class LIBPROTOBUF_EXPORT Reflection {
Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
int ctype, const Descriptor* message_type) const = 0;
+ virtual MessageFactory* GetMessageFactory() 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 decuded 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.
+ virtual 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 FieldDescriptor* field) const;
+
private:
+ template<typename T, typename Enable>
+ friend class RepeatedFieldRef;
+ template<typename T, typename Enable>
+ friend class MutableRepeatedFieldRef;
+
// 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
@@ -787,6 +967,7 @@ const RepeatedField<TYPE>& Reflection::GetRepeatedField<TYPE>( \
const Message& message, const FieldDescriptor* field) const; \
\
template<> \
+LIBPROTOBUF_EXPORT \
RepeatedField<TYPE>* Reflection::MutableRepeatedField<TYPE>( \
Message* message, const FieldDescriptor* field) const;
@@ -859,7 +1040,6 @@ inline RepeatedPtrField<PB>* Reflection::MutableRepeatedPtrField(
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 14cdc91f..63be0e9b 100644
--- a/src/google/protobuf/message_lite.cc
+++ b/src/google/protobuf/message_lite.cc
@@ -34,6 +34,7 @@
// Sanjay Ghemawat, Jeff Dean, and others.
#include <google/protobuf/message_lite.h>
+#include <google/protobuf/arena.h>
#include <string>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/coded_stream.h>
@@ -153,6 +154,15 @@ bool InlineParsePartialFromArray(const void* data, int size,
} // namespace
+
+MessageLite* MessageLite::New(::google::protobuf::Arena* arena) const {
+ MessageLite* message = New();
+ if (arena != NULL) {
+ arena->Own(message);
+ }
+ return message;
+}
+
bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) {
return InlineMergeFromCodedStream(input, this);
}
@@ -233,6 +243,12 @@ bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
bool MessageLite::SerializePartialToCodedStream(
io::CodedOutputStream* output) const {
const int size = ByteSize(); // Force size to be cached.
+ if (size < 0) {
+ // Messages >2G cannot be serialized due to overflow computing ByteSize.
+ GOOGLE_LOG(ERROR) << "Error computing ByteSize (possible overflow?).";
+ return false;
+ }
+
uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
if (buffer != NULL) {
uint8* end = SerializeWithCachedSizesToArray(buffer);
@@ -277,6 +293,12 @@ bool MessageLite::AppendToString(string* output) const {
bool MessageLite::AppendPartialToString(string* output) const {
int old_size = output->size();
int byte_size = ByteSize();
+ if (byte_size < 0) {
+ // Messages >2G cannot be serialized due to overflow computing ByteSize.
+ GOOGLE_LOG(ERROR) << "Error computing ByteSize (possible overflow?).";
+ return false;
+ }
+
STLStringResizeUninitialized(output, old_size + byte_size);
uint8* start =
reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size);
diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h
index 027cabf9..106982cc 100644
--- a/src/google/protobuf/message_lite.h
+++ b/src/google/protobuf/message_lite.h
@@ -43,7 +43,7 @@
namespace google {
namespace protobuf {
-
+ class Arena;
namespace io {
class CodedInputStream;
class CodedOutputStream;
@@ -88,6 +88,27 @@ class LIBPROTOBUF_EXPORT MessageLite {
// caller.
virtual MessageLite* New() const = 0;
+ // Construct a new instance on the arena. Ownership is passed to the caller
+ // if arena is a NULL. Default implementation for backwards compatibility.
+ virtual MessageLite* New(::google::protobuf::Arena* arena) const;
+
+ // Get the arena, if any, associated with this message. Virtual method
+ // required for generic operations but most arena-related operations should
+ // use the GetArenaNoVirtual() generated-code method. Default implementation
+ // to reduce code size by avoiding the need for per-type implementations when
+ // types do not implement arena support.
+ virtual ::google::protobuf::Arena* GetArena() const { return NULL; }
+
+ // Get a pointer that may be equal to this message's arena, or may not be. If
+ // the value returned by this method is equal to some arena pointer, then this
+ // message is on that arena; however, if this message is on some arena, this
+ // method may or may not return that arena's pointer. As a tradeoff, this
+ // method may be more efficient than GetArena(). The intent is to allow
+ // underlying representations that use e.g. tagged pointers to sometimes store
+ // the arena pointer directly, and sometimes in a more indirect way, and allow
+ // a fastpath comparison against the arena pointer when it's easy to obtain.
+ virtual void* GetMaybeArenaPointer() const { return GetArena(); }
+
// Clear all fields of the message and set them to their default values.
// Clear() avoids freeing memory, assuming that any memory allocated
// to hold parts of the message will be needed again to hold the next
diff --git a/src/google/protobuf/message_unittest.cc b/src/google/protobuf/message_unittest.cc
index a5f339f1..ebfb4321 100644
--- a/src/google/protobuf/message_unittest.cc
+++ b/src/google/protobuf/message_unittest.cc
@@ -242,6 +242,25 @@ TEST(MessageTest, CheckOverflow) {
#endif // PROTOBUF_HAS_DEATH_TEST
+namespace {
+
+class NegativeByteSize : public unittest::TestRequired {
+ public:
+ virtual int ByteSize() const { return -1; }
+};
+
+} // namespace
+
+TEST(MessageTest, SerializationFailsOnNegativeByteSize) {
+ NegativeByteSize message;
+ string string_output;
+ EXPECT_FALSE(message.AppendPartialToString(&string_output));
+
+ io::ArrayOutputStream coded_raw_output(NULL, 100);
+ io::CodedOutputStream coded_output(&coded_raw_output);
+ EXPECT_FALSE(message.SerializePartialToCodedStream(&coded_output));
+}
+
TEST(MessageTest, BypassInitializationCheckOnSerialize) {
unittest::TestRequired message;
io::ArrayOutputStream raw_output(NULL, 0);
diff --git a/src/google/protobuf/metadata.h b/src/google/protobuf/metadata.h
new file mode 100644
index 00000000..30b2a6ee
--- /dev/null
+++ b/src/google/protobuf/metadata.h
@@ -0,0 +1,164 @@
+// 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 header file defines an internal class that encapsulates internal message
+// metadata (Unknown-field set, Arena pointer, ...) and allows its
+// representation to be made more space-efficient via various optimizations.
+//
+// Note that this is distinct from google::protobuf::Metadata, which encapsulates
+// Descriptor and Reflection pointers.
+
+#ifndef GOOGLE_PROTOBUF_METADATA_H__
+#define GOOGLE_PROTOBUF_METADATA_H__
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/arena.h>
+#include <google/protobuf/unknown_field_set.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+// This is the representation for messages that support arena allocation. It
+// uses a tagged pointer to either store the Arena pointer, if there are no
+// unknown fields, or a pointer to a block of memory with both the Arena pointer
+// and the UnknownFieldSet, if there are unknown fields. This optimization
+// allows for "zero-overhead" storage of the Arena pointer, relative to the
+// above baseline implementation.
+//
+// The tagged pointer uses the LSB to disambiguate cases, and uses bit 0 == 0 to
+// indicate an arena pointer and bit 0 == 1 to indicate a UFS+Arena-container
+// pointer.
+class LIBPROTOBUF_EXPORT InternalMetadataWithArena {
+ public:
+ InternalMetadataWithArena() : ptr_(NULL) {}
+ explicit InternalMetadataWithArena(Arena* arena)
+ : ptr_ (arena) {}
+
+ ~InternalMetadataWithArena() {
+ if (have_unknown_fields() && arena() == NULL) {
+ delete PtrValue<Container>();
+ }
+ ptr_ = NULL;
+ }
+
+ inline const UnknownFieldSet& unknown_fields() const
+ GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
+ return PtrValue<Container>()->unknown_fields_;
+ } else {
+ return *UnknownFieldSet::default_instance();
+ }
+ }
+
+ inline UnknownFieldSet* mutable_unknown_fields() GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ if (GOOGLE_PREDICT_TRUE(have_unknown_fields())) {
+ return &PtrValue<Container>()->unknown_fields_;
+ } else {
+ return mutable_unknown_fields_slow();
+ }
+ }
+
+ inline Arena* arena() const GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ if (GOOGLE_PREDICT_FALSE(have_unknown_fields())) {
+ return PtrValue<Container>()->arena_;
+ } else {
+ return PtrValue<Arena>();
+ }
+ }
+
+ inline bool have_unknown_fields() const GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ return PtrTag() == kTagContainer;
+ }
+
+ inline void Swap(InternalMetadataWithArena* other) GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ // Semantics here are that we swap only the unknown fields, not the arena
+ // pointer. We cannot simply swap ptr_ with other->ptr_ because we need to
+ // maintain our own arena ptr. Also, our ptr_ and other's ptr_ may be in
+ // different states (direct arena pointer vs. container with UFS) so we
+ // cannot simply swap ptr_ and then restore the arena pointers. We reuse
+ // UFS's swap implementation instead.
+ if (have_unknown_fields() || other->have_unknown_fields()) {
+ mutable_unknown_fields()->Swap(other->mutable_unknown_fields());
+ }
+ }
+
+ inline void* raw_arena_ptr() const GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ return ptr_;
+ }
+
+ private:
+ void* ptr_;
+
+ // Tagged pointer implementation.
+ enum {
+ // ptr_ is an Arena*.
+ kTagArena = 0,
+ // ptr_ is a Container*.
+ kTagContainer = 1,
+ };
+ static const intptr_t kPtrTagMask = 1;
+ static const intptr_t kPtrValueMask = ~kPtrTagMask;
+
+ // Accessors for pointer tag and pointer value.
+ inline int PtrTag() const GOOGLE_ATTRIBUTE_ALWAYS_INLINE {
+ return reinterpret_cast<intptr_t>(ptr_) & kPtrTagMask;
+ }
+
+ template<typename T> T* PtrValue() const {
+ return reinterpret_cast<T*>(
+ reinterpret_cast<intptr_t>(ptr_) & kPtrValueMask);
+ }
+
+ // If ptr_'s tag is kTagContainer, it points to an instance of this struct.
+ struct Container {
+ UnknownFieldSet unknown_fields_;
+ Arena* arena_;
+ };
+
+ UnknownFieldSet* mutable_unknown_fields_slow() GOOGLE_ATTRIBUTE_NOINLINE {
+ Arena* my_arena = arena();
+ Container* container = Arena::Create<Container>(my_arena);
+ ptr_ = reinterpret_cast<void*>(
+ reinterpret_cast<intptr_t>(container) | kTagContainer);
+ container->arena_ = my_arena;
+ return &(container->unknown_fields_);
+ }
+};
+
+// Temporary compatibility typedef. Remove once this is released in components
+// and upb CL is submitted.
+typedef InternalMetadataWithArena InternalMetadata;
+
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_METADATA_H__
diff --git a/src/google/protobuf/new_delete_capture.cc b/src/google/protobuf/new_delete_capture.cc
new file mode 100644
index 00000000..baf42ffe
--- /dev/null
+++ b/src/google/protobuf/new_delete_capture.cc
@@ -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.
+
+// This file exists for testing allocation behavior when using arenas by hooking
+// new/delete. It is a copy of //experimental/mvels/util/new_delete_capture.cc.
+
+#include <google/protobuf/new_delete_capture.h>
+
+#include <pthread.h>
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/malloc_hook.h>
+#include <google/protobuf/stubs/spinlock.h>
+
+namespace google {
+namespace {
+
+pthread_t gthread;
+protobuf_unittest::NewDeleteCapture *ghooked_instance = NULL;
+SpinLock gspinlock(base::LINKER_INITIALIZED);
+
+} // namespace
+
+namespace protobuf_unittest {
+
+NewDeleteCapture::NewDeleteCapture()
+ : alloc_count_(0),
+ alloc_size_(0),
+ alloc_ptr_(NULL),
+ free_count_(0),
+ free_ptr_(NULL) {}
+
+NewDeleteCapture::~NewDeleteCapture() { Unhook(); }
+
+void NewDeleteCapture::Reset() {
+ alloc_count_ = 0;
+ alloc_size_ = 0;
+ free_count_ = 0;
+ alloc_ptr_ = NULL;
+ free_ptr_ = NULL;
+}
+
+bool NewDeleteCapture::Hook(bool reset) {
+ SpinLockHolder spinlock(&gspinlock);
+ if (ghooked_instance != this) {
+ GOOGLE_CHECK(ghooked_instance == NULL)
+ << " NewDeleteCapture can have only 1 active instance";
+ GOOGLE_CHECK(MallocHook::AddNewHook(NewHook));
+ GOOGLE_CHECK(MallocHook::AddDeleteHook(DeleteHook));
+ gthread = pthread_self();
+ ghooked_instance = this;
+ if (reset) {
+ Reset();
+ }
+ return true;
+ }
+ return false;
+}
+
+bool NewDeleteCapture::Unhook() {
+ SpinLockHolder spinlock(&gspinlock);
+ if (ghooked_instance == this) {
+ gthread = pthread_t();
+ ghooked_instance = NULL;
+ GOOGLE_CHECK(MallocHook::RemoveDeleteHook(DeleteHook));
+ GOOGLE_CHECK(MallocHook::RemoveNewHook(NewHook));
+ return true;
+ }
+ return false;
+}
+
+void NewDeleteCapture::NewHook(const void *ptr, size_t size) {
+ SpinLockHolder spinlock(&gspinlock);
+ if (gthread == pthread_self()) {
+ auto &rthis = *ghooked_instance;
+ if (++rthis.alloc_count_ == 1) {
+ rthis.alloc_size_ = size;
+ rthis.alloc_ptr_ = ptr;
+ }
+ }
+}
+
+void NewDeleteCapture::DeleteHook(const void *ptr) {
+ SpinLockHolder spinlock(&gspinlock);
+ if (gthread == pthread_self()) {
+ auto &rthis = *ghooked_instance;
+ if (++rthis.free_count_ == 1) {
+ rthis.free_ptr_ = ptr;
+ }
+ }
+}
+
+} // namespace protobuf_unittest
+} // namespace google
diff --git a/src/google/protobuf/new_delete_capture.h b/src/google/protobuf/new_delete_capture.h
new file mode 100644
index 00000000..4ab550cd
--- /dev/null
+++ b/src/google/protobuf/new_delete_capture.h
@@ -0,0 +1,175 @@
+// 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 exists for testing allocation behavior when using arenas by hooking
+// new/delete. It is a copy of //experimental/mvels/util/new_delete_capture.h.
+//
+// Copyright 2014 Google Inc.
+//
+// Author: Martijn Vels
+//
+// A simple class that captures memory allocations and deletes.
+//
+// This class is private to //strings and only intended to be used inside
+// unit tests. It uses the MallocHook functionality to capture memory
+// allocation and delete operations performed by the thread that activated
+// a hook on a specific instance.
+//
+// The class captures the following information:
+// - Total allocation count (new, malloc(), etc).
+// - Total delete count (delete, free(), etc).
+// - The size and returned pointer for the first memory allocation.
+// - The pointer for the first delete operation.
+//
+// The latter 2 infos (size and pointer of first new/delete) are usefull in
+// cases where you can closely scope a Hook() / Unhook sequence around a
+// specific piece of code where you expect no more than 1 pair of new / delete
+// operations.
+//
+// Sample usage where we expect a single unique alloc / free:
+//
+// NewDeleteCapture capture_alloc;
+// const void *ptr;
+// {
+// capture_alloc.Hook();
+// MyAllocationClass my_instance(size);
+// capture_alloc.Unhook();
+//
+// ptr = my_instance.ptr();
+// GOOGLE_CHECK_EQ(1, capture_alloc.alloc_count());
+// GOOGLE_CHECK_EQ(0, capture_alloc.free_count());
+// GOOGLE_CHECK_EQ(size, capture_alloc.alloc_size());
+// GOOGLE_CHECK_EQ(ptr, capture_alloc.alloc_ptr());
+//
+// capture_alloc.Hook();
+// }
+// capture_alloc.Unhook();
+// GOOGLE_CHECK_EQ(1, capture_alloc.alloc_count());
+// GOOGLE_CHECK_EQ(1, capture_alloc.free_count());
+// GOOGLE_CHECK_EQ(ptr, capture_alloc.free_ptr());
+//
+// You can only have one NewDeleteCapture instance active at the time. It is
+// total valid to have many instances in different threads, but only one
+// instance can have a hook active.
+//
+// Legal:
+//
+// NewDeleteCapture capture_alloc1;
+// NewDeleteCapture capture_alloc2;
+// const void *ptr;
+// {
+// capture_alloc1.Hook();
+// MyAllocationClass my_instance(size);
+// capture_alloc1.Unhook();
+//
+// capture_alloc2.Hook();
+// my_instance.reset(size);
+// capture_alloc2.Unhook();
+// }
+//
+// Illegal:
+//
+// NewDeleteCapture capture_alloc1;
+// NewDeleteCapture capture_alloc2;
+// const void *ptr;
+// {
+// capture_alloc1.Hook();
+// MyAllocationClass my_instance(size);
+//
+// capture_alloc2.Hook();
+// my_instance.reset(size);
+//
+// capture_alloc1.Unhook();
+// capture_alloc2.Unhook();
+// }
+//
+#ifndef GOOGLE_PROTOBUF_NEW_DELETE_CAPTURE_H__
+#define GOOGLE_PROTOBUF_NEW_DELETE_CAPTURE_H__
+
+#include <stddef.h>
+
+namespace google {
+namespace protobuf_unittest {
+
+class NewDeleteCapture {
+ public:
+ // Creates a new inactive capture instance
+ NewDeleteCapture();
+
+ // Destroys this capture instance. Active hooks are automatically removed.
+ ~NewDeleteCapture();
+
+ // Activates a hook on this instance. If reset is true (the default), all
+ // internal counters will be reset to 0.
+ // Returns true if the hook was activated, false if this instance already
+ // owned the hook.
+ // Requires no other instance owning the hook (check fails)
+ bool Hook(bool reset = true);
+
+ // De-activate the hook on this instance.
+ // Returns true if the hook was removed, false if this instance did not own
+ // the hook.
+ bool Unhook();
+
+ // Resets all counters to 0
+ void Reset();
+
+ // Returns the total number of allocations (new, malloc(), etc)
+ size_t alloc_count() const { return alloc_count_; }
+
+ // Returns the total number of deletes (delete, free(), etc)
+ size_t free_count() const { return free_count_; }
+
+ // Returns the size of the first observed allocation
+ size_t alloc_size() const { return alloc_size_; }
+
+ // Returns the allocated ptr of the first observed allocation
+ const void *alloc_ptr() const { return alloc_ptr_; }
+
+ // Returns the ptr of the first observed delete
+ const void* free_ptr() const { return free_ptr_; }
+
+ private:
+ static void NewHook(const void *ptr, size_t size);
+ static void DeleteHook(const void *ptr);
+
+ private:
+ size_t alloc_count_;
+ size_t alloc_size_;
+ const void *alloc_ptr_;
+
+ size_t free_count_;
+ const void *free_ptr_;
+};
+
+} // namespace protobuf_unittest
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_NEW_DELETE_CAPTURE_H__
diff --git a/src/google/protobuf/no_field_presence_test.cc b/src/google/protobuf/no_field_presence_test.cc
new file mode 100644
index 00000000..4b7b31d9
--- /dev/null
+++ b/src/google/protobuf/no_field_presence_test.cc
@@ -0,0 +1,537 @@
+// 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 <string>
+
+#include <google/protobuf/unittest_no_field_presence.pb.h>
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/descriptor.pb.h>
+#include <google/protobuf/descriptor.h>
+#include <gtest/gtest.h>
+
+namespace google {
+namespace protobuf {
+namespace {
+
+// Helper: checks that all fields have default (zero/empty) values.
+void CheckDefaultValues(
+ const proto2_nofieldpresence_unittest::TestAllTypes& m) {
+ EXPECT_EQ(0, m.optional_int32());
+ EXPECT_EQ(0, m.optional_int64());
+ EXPECT_EQ(0, m.optional_uint32());
+ EXPECT_EQ(0, m.optional_uint64());
+ EXPECT_EQ(0, m.optional_sint32());
+ EXPECT_EQ(0, m.optional_sint64());
+ EXPECT_EQ(0, m.optional_fixed32());
+ EXPECT_EQ(0, m.optional_fixed64());
+ EXPECT_EQ(0, m.optional_sfixed32());
+ EXPECT_EQ(0, m.optional_sfixed64());
+ EXPECT_EQ(0, m.optional_float());
+ EXPECT_EQ(0, m.optional_double());
+ EXPECT_EQ(false, m.optional_bool());
+ EXPECT_EQ(0, m.optional_string().size());
+ EXPECT_EQ(0, m.optional_bytes().size());
+
+ EXPECT_EQ(false, m.has_optional_nested_message());
+ // accessor for message fields returns default instance when not present
+ EXPECT_EQ(0, m.optional_nested_message().bb());
+ EXPECT_EQ(false, m.has_optional_proto2_message());
+ // Embedded proto2 messages still have proto2 semantics, e.g. non-zero default
+ // values. Here the submessage is not present but its accessor returns the
+ // default instance.
+ EXPECT_EQ(41, m.optional_proto2_message().default_int32());
+ EXPECT_EQ(false, m.has_optional_foreign_message());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO,
+ m.optional_nested_enum());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_FOO,
+ m.optional_foreign_enum());
+
+
+ EXPECT_EQ(0, m.repeated_int32_size());
+ EXPECT_EQ(0, m.repeated_int64_size());
+ EXPECT_EQ(0, m.repeated_uint32_size());
+ EXPECT_EQ(0, m.repeated_uint64_size());
+ EXPECT_EQ(0, m.repeated_sint32_size());
+ EXPECT_EQ(0, m.repeated_sint64_size());
+ EXPECT_EQ(0, m.repeated_fixed32_size());
+ EXPECT_EQ(0, m.repeated_fixed64_size());
+ EXPECT_EQ(0, m.repeated_sfixed32_size());
+ EXPECT_EQ(0, m.repeated_sfixed64_size());
+ EXPECT_EQ(0, m.repeated_float_size());
+ EXPECT_EQ(0, m.repeated_double_size());
+ EXPECT_EQ(0, m.repeated_bool_size());
+ EXPECT_EQ(0, m.repeated_string_size());
+ EXPECT_EQ(0, m.repeated_bytes_size());
+ EXPECT_EQ(0, m.repeated_nested_message_size());
+ EXPECT_EQ(0, m.repeated_foreign_message_size());
+ EXPECT_EQ(0, m.repeated_proto2_message_size());
+ EXPECT_EQ(0, m.repeated_nested_enum_size());
+ EXPECT_EQ(0, m.repeated_foreign_enum_size());
+ EXPECT_EQ(0, m.repeated_lazy_message_size());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::ONEOF_FIELD_NOT_SET,
+ m.oneof_field_case());
+}
+
+void FillValues(proto2_nofieldpresence_unittest::TestAllTypes* m) {
+ m->set_optional_int32(100);
+ m->set_optional_int64(101);
+ m->set_optional_uint32(102);
+ m->set_optional_uint64(103);
+ m->set_optional_sint32(104);
+ m->set_optional_sint64(105);
+ m->set_optional_fixed32(106);
+ m->set_optional_fixed64(107);
+ m->set_optional_sfixed32(108);
+ m->set_optional_sfixed64(109);
+ m->set_optional_float(110.0);
+ m->set_optional_double(111.0);
+ m->set_optional_bool(true);
+ m->set_optional_string("asdf");
+ m->set_optional_bytes("jkl;");
+ m->mutable_optional_nested_message()->set_bb(42);
+ m->mutable_optional_foreign_message()->set_c(43);
+ 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->mutable_optional_lazy_message()->set_bb(45);
+ m->add_repeated_int32(100);
+ m->add_repeated_int64(101);
+ m->add_repeated_uint32(102);
+ m->add_repeated_uint64(103);
+ m->add_repeated_sint32(104);
+ m->add_repeated_sint64(105);
+ m->add_repeated_fixed32(106);
+ m->add_repeated_fixed64(107);
+ m->add_repeated_sfixed32(108);
+ m->add_repeated_sfixed64(109);
+ m->add_repeated_float(110.0);
+ m->add_repeated_double(111.0);
+ m->add_repeated_bool(true);
+ m->add_repeated_string("asdf");
+ m->add_repeated_bytes("jkl;");
+ m->add_repeated_nested_message()->set_bb(46);
+ m->add_repeated_foreign_message()->set_c(47);
+ 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_lazy_message()->set_bb(49);
+
+ m->set_oneof_uint32(1);
+ m->mutable_oneof_nested_message()->set_bb(50);
+ m->set_oneof_string("test"); // only this one remains set
+}
+
+void CheckNonDefaultValues(
+const proto2_nofieldpresence_unittest::TestAllTypes& m) {
+ EXPECT_EQ(100, m.optional_int32());
+ EXPECT_EQ(101, m.optional_int64());
+ EXPECT_EQ(102, m.optional_uint32());
+ EXPECT_EQ(103, m.optional_uint64());
+ EXPECT_EQ(104, m.optional_sint32());
+ EXPECT_EQ(105, m.optional_sint64());
+ EXPECT_EQ(106, m.optional_fixed32());
+ EXPECT_EQ(107, m.optional_fixed64());
+ EXPECT_EQ(108, m.optional_sfixed32());
+ EXPECT_EQ(109, m.optional_sfixed64());
+ EXPECT_EQ(110.0, m.optional_float());
+ EXPECT_EQ(111.0, m.optional_double());
+ EXPECT_EQ(true, m.optional_bool());
+ EXPECT_EQ("asdf", m.optional_string());
+ EXPECT_EQ("jkl;", m.optional_bytes());
+ EXPECT_EQ(true, m.has_optional_nested_message());
+ EXPECT_EQ(42, m.optional_nested_message().bb());
+ EXPECT_EQ(true, m.has_optional_foreign_message());
+ EXPECT_EQ(43, m.optional_foreign_message().c());
+ EXPECT_EQ(true, m.has_optional_proto2_message());
+ EXPECT_EQ(44, m.optional_proto2_message().optional_int32());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
+ m.optional_nested_enum());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::FOREIGN_BAZ,
+ m.optional_foreign_enum());
+ EXPECT_EQ(true, m.has_optional_lazy_message());
+ EXPECT_EQ(45, m.optional_lazy_message().bb());
+
+ EXPECT_EQ(1, m.repeated_int32_size());
+ EXPECT_EQ(100, m.repeated_int32(0));
+ EXPECT_EQ(1, m.repeated_int64_size());
+ EXPECT_EQ(101, m.repeated_int64(0));
+ EXPECT_EQ(1, m.repeated_uint32_size());
+ EXPECT_EQ(102, m.repeated_uint32(0));
+ EXPECT_EQ(1, m.repeated_uint64_size());
+ EXPECT_EQ(103, m.repeated_uint64(0));
+ EXPECT_EQ(1, m.repeated_sint32_size());
+ EXPECT_EQ(104, m.repeated_sint32(0));
+ EXPECT_EQ(1, m.repeated_sint64_size());
+ EXPECT_EQ(105, m.repeated_sint64(0));
+ EXPECT_EQ(1, m.repeated_fixed32_size());
+ EXPECT_EQ(106, m.repeated_fixed32(0));
+ EXPECT_EQ(1, m.repeated_fixed64_size());
+ EXPECT_EQ(107, m.repeated_fixed64(0));
+ EXPECT_EQ(1, m.repeated_sfixed32_size());
+ EXPECT_EQ(108, m.repeated_sfixed32(0));
+ EXPECT_EQ(1, m.repeated_sfixed64_size());
+ EXPECT_EQ(109, m.repeated_sfixed64(0));
+ EXPECT_EQ(1, m.repeated_float_size());
+ EXPECT_EQ(110.0, m.repeated_float(0));
+ EXPECT_EQ(1, m.repeated_double_size());
+ EXPECT_EQ(111.0, m.repeated_double(0));
+ EXPECT_EQ(1, m.repeated_bool_size());
+ EXPECT_EQ(true, m.repeated_bool(0));
+ EXPECT_EQ(1, m.repeated_string_size());
+ EXPECT_EQ("asdf", m.repeated_string(0));
+ EXPECT_EQ(1, m.repeated_bytes_size());
+ EXPECT_EQ("jkl;", m.repeated_bytes(0));
+ EXPECT_EQ(1, m.repeated_nested_message_size());
+ EXPECT_EQ(46, m.repeated_nested_message(0).bb());
+ EXPECT_EQ(1, m.repeated_foreign_message_size());
+ EXPECT_EQ(47, m.repeated_foreign_message(0).c());
+ EXPECT_EQ(1, m.repeated_proto2_message_size());
+ EXPECT_EQ(48, m.repeated_proto2_message(0).optional_int32());
+ EXPECT_EQ(1, m.repeated_nested_enum_size());
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_BAZ,
+ m.repeated_nested_enum(0));
+ EXPECT_EQ(1, m.repeated_foreign_enum_size());
+ EXPECT_EQ(proto2_nofieldpresence_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());
+
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
+ m.oneof_field_case());
+ EXPECT_EQ("test", m.oneof_string());
+}
+
+TEST(NoFieldPresenceTest, BasicMessageTest) {
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+ // Check default values, fill all fields, check values. We just want to
+ // exercise the basic getters/setter paths here to make sure no
+ // field-presence-related changes broke these.
+ CheckDefaultValues(message);
+ FillValues(&message);
+ CheckNonDefaultValues(message);
+
+ // Clear() should be equivalent to getting a freshly-constructed message.
+ message.Clear();
+ CheckDefaultValues(message);
+}
+
+TEST(NoFieldPresenceTest, MessageFieldPresenceTest) {
+ // check that presence still works properly for message fields.
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+ EXPECT_EQ(false, message.has_optional_nested_message());
+ // Getter should fetch default instance, and not cause the field to become
+ // present.
+ EXPECT_EQ(0, message.optional_nested_message().bb());
+ EXPECT_EQ(false, message.has_optional_nested_message());
+ message.mutable_optional_nested_message()->set_bb(42);
+ EXPECT_EQ(true, message.has_optional_nested_message());
+ message.clear_optional_nested_message();
+ EXPECT_EQ(false, message.has_optional_nested_message());
+
+ // Likewise for a lazy message field.
+ EXPECT_EQ(false, message.has_optional_lazy_message());
+ // Getter should fetch default instance, and not cause the field to become
+ // present.
+ EXPECT_EQ(0, message.optional_lazy_message().bb());
+ EXPECT_EQ(false, message.has_optional_lazy_message());
+ message.mutable_optional_lazy_message()->set_bb(42);
+ EXPECT_EQ(true, message.has_optional_lazy_message());
+ message.clear_optional_lazy_message();
+ 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());
+}
+
+TEST(NoFieldPresenceTest, ReflectionHasFieldTest) {
+ // check that HasField reports true on all scalar fields. Check that it
+ // behaves properly for message fields.
+
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* desc = message.GetDescriptor();
+
+ // Check initial state: scalars not present (due to need to be consistent with
+ // MergeFrom()), message fields not present, oneofs not present.
+ for (int i = 0; i < desc->field_count(); i++) {
+ const google::protobuf::FieldDescriptor* field = desc->field(i);
+ if (field->is_repeated()) continue;
+ EXPECT_EQ(false, r->HasField(message, field));
+ }
+
+ // Test field presence of a message field on the default instance.
+ const google::protobuf::FieldDescriptor* msg_field =
+ desc->FindFieldByName("optional_nested_message");
+ 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);
+ for (int i = 0; i < desc->field_count(); i++) {
+ const google::protobuf::FieldDescriptor* field = desc->field(i);
+ if (field->is_repeated() || field->containing_oneof()) {
+ continue;
+ }
+ if (field->options().ctype() != google::protobuf::FieldOptions::STRING) {
+ continue;
+ }
+ EXPECT_EQ(true, r->HasField(message, field));
+ }
+
+ message.Clear();
+
+ // Check zero/empty-means-not-present semantics.
+ const google::protobuf::FieldDescriptor* field_int32 = desc->FindFieldByName(
+ "optional_int32");
+ const google::protobuf::FieldDescriptor* field_double = desc->FindFieldByName(
+ "optional_double");
+ const google::protobuf::FieldDescriptor* field_string = desc->FindFieldByName(
+ "optional_string");
+
+ EXPECT_EQ(false, r->HasField(message, field_int32));
+ EXPECT_EQ(false, r->HasField(message, field_double));
+ EXPECT_EQ(false, r->HasField(message, field_string));
+
+ message.set_optional_int32(42);
+ EXPECT_EQ(true, r->HasField(message, field_int32));
+ message.set_optional_int32(0);
+ EXPECT_EQ(false, r->HasField(message, field_int32));
+
+ message.set_optional_double(42.0);
+ EXPECT_EQ(true, r->HasField(message, field_double));
+ message.set_optional_double(0.0);
+ EXPECT_EQ(false, r->HasField(message, field_double));
+
+ message.set_optional_string("test");
+ EXPECT_EQ(true, r->HasField(message, field_string));
+ message.set_optional_string("");
+ EXPECT_EQ(false, r->HasField(message, field_string));
+}
+
+TEST(NoFieldPresenceTest, HasFieldOneofsTest) {
+ // check that HasField behaves properly for oneofs.
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* desc = message.GetDescriptor();
+ const google::protobuf::FieldDescriptor* desc_oneof_uint32 =
+ desc->FindFieldByName("oneof_uint32");
+ const google::protobuf::FieldDescriptor* desc_oneof_nested_message =
+ desc->FindFieldByName("oneof_nested_message");
+ const google::protobuf::FieldDescriptor* desc_oneof_string =
+ desc->FindFieldByName("oneof_string");
+ GOOGLE_CHECK_NOTNULL(desc_oneof_uint32);
+ GOOGLE_CHECK_NOTNULL(desc_oneof_nested_message);
+ GOOGLE_CHECK_NOTNULL(desc_oneof_string);
+
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
+
+ message.set_oneof_string("test");
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
+ EXPECT_EQ(true, r->HasField(message, desc_oneof_string));
+ message.mutable_oneof_nested_message()->set_bb(42);
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
+ EXPECT_EQ(true, r->HasField(message, desc_oneof_nested_message));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
+
+ message.Clear();
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_uint32));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_nested_message));
+ EXPECT_EQ(false, r->HasField(message, desc_oneof_string));
+}
+
+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;
+
+ // All default values -> no output.
+ message.SerializeToString(&output);
+ EXPECT_EQ(0, output.size());
+
+ // Zero values -> still no output.
+ message.set_optional_int32(0);
+ message.set_optional_int64(0);
+ message.set_optional_uint32(0);
+ message.set_optional_uint64(0);
+ message.set_optional_sint32(0);
+ message.set_optional_sint64(0);
+ message.set_optional_fixed32(0);
+ message.set_optional_fixed64(0);
+ message.set_optional_sfixed32(0);
+ message.set_optional_sfixed64(0);
+ message.set_optional_float(0);
+ message.set_optional_double(0);
+ message.set_optional_bool(0);
+ message.set_optional_string("");
+ message.set_optional_bytes("");
+ message.set_optional_nested_enum(
+ proto2_nofieldpresence_unittest::TestAllTypes_NestedEnum_FOO); // first enum entry
+ message.set_optional_foreign_enum(
+ proto2_nofieldpresence_unittest::FOREIGN_FOO); // first enum entry
+
+ message.SerializeToString(&output);
+ EXPECT_EQ(0, output.size());
+
+ message.set_optional_int32(1);
+ message.SerializeToString(&output);
+ EXPECT_EQ(2, output.size());
+ EXPECT_EQ("\x08\x01", output);
+
+ message.set_optional_int32(0);
+ message.SerializeToString(&output);
+ EXPECT_EQ(0, output.size());
+}
+
+TEST(NoFieldPresenceTest, MergeFromIfNonzeroTest) {
+ // check that MergeFrom copies if nonzero/nondefault only.
+ proto2_nofieldpresence_unittest::TestAllTypes source;
+ proto2_nofieldpresence_unittest::TestAllTypes dest;
+
+ dest.set_optional_int32(42);
+ dest.set_optional_string("test");
+ source.set_optional_int32(0);
+ source.set_optional_string("");
+ // MergeFrom() copies only if present in serialization, i.e., non-zero.
+ dest.MergeFrom(source);
+ EXPECT_EQ(42, dest.optional_int32());
+ EXPECT_EQ("test", dest.optional_string());
+
+ source.set_optional_int32(84);
+ source.set_optional_string("test2");
+ dest.MergeFrom(source);
+ EXPECT_EQ(84, dest.optional_int32());
+ EXPECT_EQ("test2", dest.optional_string());
+}
+
+TEST(NoFieldPresenceTest, IsInitializedTest) {
+ // Check that IsInitialized works properly.
+ proto2_nofieldpresence_unittest::TestProto2Required message;
+
+ EXPECT_EQ(true, message.IsInitialized());
+ message.mutable_proto2()->set_a(1);
+ EXPECT_EQ(false, message.IsInitialized());
+ message.mutable_proto2()->set_b(1);
+ EXPECT_EQ(false, message.IsInitialized());
+ message.mutable_proto2()->set_c(1);
+ EXPECT_EQ(true, message.IsInitialized());
+}
+
+TEST(NoFieldPresenceTest, LazyMessageFieldHasBit) {
+ // Check that has-bit interaction with lazy message works (has-bit before and
+ // after lazy decode).
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* desc = message.GetDescriptor();
+ const google::protobuf::FieldDescriptor* field = desc->FindFieldByName(
+ "optional_lazy_message");
+ GOOGLE_CHECK_NOTNULL(field);
+
+ EXPECT_EQ(false, message.has_optional_lazy_message());
+ EXPECT_EQ(false, r->HasField(message, field));
+
+ message.mutable_optional_lazy_message()->set_bb(42);
+ EXPECT_EQ(true, message.has_optional_lazy_message());
+ EXPECT_EQ(true, r->HasField(message, field));
+
+ // Serialize and parse with a new message object so that lazy field on new
+ // object is in unparsed state.
+ string output;
+ message.SerializeToString(&output);
+ proto2_nofieldpresence_unittest::TestAllTypes message2;
+ message2.ParseFromString(output);
+
+ EXPECT_EQ(true, message2.has_optional_lazy_message());
+ EXPECT_EQ(true, r->HasField(message2, field));
+
+ // Access field to force lazy parse.
+ EXPECT_EQ(42, message.optional_lazy_message().bb());
+ EXPECT_EQ(true, message2.has_optional_lazy_message());
+ EXPECT_EQ(true, r->HasField(message2, field));
+}
+
+TEST(NoFieldPresenceTest, OneofPresence) {
+ proto2_nofieldpresence_unittest::TestAllTypes message;
+ // 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;
+ message.SerializeToString(&serialized);
+ // Tag: 113 --> tag is (113 << 3) | 2 (length delimited) = 906
+ // varint: 0x8a 0x07
+ // Length: 0x00
+ EXPECT_EQ(3, serialized.size());
+ EXPECT_EQ(static_cast<char>(0x8a), serialized.at(0));
+ EXPECT_EQ(static_cast<char>(0x07), serialized.at(1));
+ EXPECT_EQ(static_cast<char>(0x00), serialized.at(2));
+
+ message.Clear();
+ EXPECT_TRUE(message.ParseFromString(serialized));
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofString,
+ message.oneof_field_case());
+
+ // Also test int32 and enum fields.
+ message.Clear();
+ message.set_oneof_uint32(0); // would not go on wire if ordinary field.
+ message.SerializeToString(&serialized);
+ EXPECT_EQ(3, serialized.size());
+ EXPECT_TRUE(message.ParseFromString(serialized));
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofUint32,
+ message.oneof_field_case());
+
+ message.Clear();
+ 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));
+ EXPECT_EQ(proto2_nofieldpresence_unittest::TestAllTypes::kOneofEnum,
+ message.oneof_field_case());
+
+ message.Clear();
+ message.set_oneof_string("test");
+ message.clear_oneof_string();
+ EXPECT_EQ(0, message.ByteSize());
+}
+
+} // namespace
+} // namespace protobuf
+
+} // namespace google
diff --git a/src/google/protobuf/preserve_unknown_enum_test.cc b/src/google/protobuf/preserve_unknown_enum_test.cc
new file mode 100644
index 00000000..816e52ca
--- /dev/null
+++ b/src/google/protobuf/preserve_unknown_enum_test.cc
@@ -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.
+
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/unittest_preserve_unknown_enum.pb.h>
+#include <google/protobuf/dynamic_message.h>
+#include <google/protobuf/descriptor.h>
+#include <gtest/gtest.h>
+
+namespace google {
+namespace protobuf {
+namespace {
+
+void FillMessage(
+ proto2_preserve_unknown_enum_unittest::MyMessagePlusExtra* message) {
+ message->set_e(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA);
+ message->add_repeated_e(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA);
+ message->add_repeated_packed_e(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA);
+ message->set_oneof_e_1(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA);
+}
+
+void CheckMessage(
+ const proto2_preserve_unknown_enum_unittest::MyMessagePlusExtra& message) {
+ EXPECT_EQ(proto2_preserve_unknown_enum_unittest::E_EXTRA,
+ message.e());
+ EXPECT_EQ(1, message.repeated_e_size());
+ EXPECT_EQ(proto2_preserve_unknown_enum_unittest::E_EXTRA,
+ message.repeated_e(0));
+ EXPECT_EQ(1, message.repeated_packed_e_size());
+ EXPECT_EQ(proto2_preserve_unknown_enum_unittest::E_EXTRA,
+ message.repeated_packed_e(0));
+ EXPECT_EQ(proto2_preserve_unknown_enum_unittest::E_EXTRA,
+ message.oneof_e_1());
+}
+
+void CheckMessage(
+ const proto2_preserve_unknown_enum_unittest::MyMessage& message) {
+ EXPECT_EQ(static_cast<int>(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA),
+ static_cast<int>(message.e()));
+ EXPECT_EQ(1, message.repeated_e_size());
+ EXPECT_EQ(static_cast<int>(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA),
+ static_cast<int>(message.repeated_e(0)));
+ EXPECT_EQ(1, message.repeated_packed_e_size());
+ EXPECT_EQ(static_cast<int>(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA),
+ static_cast<int>(message.repeated_packed_e(0)));
+ EXPECT_EQ(static_cast<int>(
+ proto2_preserve_unknown_enum_unittest::E_EXTRA),
+ static_cast<int>(message.oneof_e_1()));
+}
+
+} // anonymous namespace
+
+// Test that parsing preserves an unknown value in the enum field and does not
+// punt it to the UnknownFieldSet.
+TEST(PreserveUnknownEnumTest, PreserveParseAndSerialize) {
+ proto2_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message;
+ FillMessage(&orig_message);
+ string serialized;
+ orig_message.SerializeToString(&serialized);
+
+ proto2_preserve_unknown_enum_unittest::MyMessage message;
+ EXPECT_EQ(true, message.ParseFromString(serialized));
+ CheckMessage(message);
+
+ serialized.clear();
+ message.SerializeToString(&serialized);
+ EXPECT_EQ(true, orig_message.ParseFromString(serialized));
+ CheckMessage(orig_message);
+}
+
+// Test that reflection based implementation also keeps unknown enum values and
+// doesn't put them into UnknownFieldSet.
+TEST(PreserveUnknownEnumTest, PreserveParseAndSerializeDynamicMessage) {
+ proto2_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message;
+ FillMessage(&orig_message);
+ string serialized = orig_message.SerializeAsString();
+
+ google::protobuf::DynamicMessageFactory factory;
+ google::protobuf::scoped_ptr<google::protobuf::Message> message(factory.GetPrototype(
+ proto2_preserve_unknown_enum_unittest::MyMessage::descriptor())->New());
+ EXPECT_EQ(true, message->ParseFromString(serialized));
+ message->DiscardUnknownFields();
+
+ serialized = message->SerializeAsString();
+ EXPECT_EQ(true, orig_message.ParseFromString(serialized));
+ CheckMessage(orig_message);
+}
+
+// Test that reflection provides EnumValueDescriptors for unknown values.
+TEST(PreserveUnknownEnumTest, DynamicEnumValueDescriptors) {
+ proto2_preserve_unknown_enum_unittest::MyMessagePlusExtra orig_message;
+ FillMessage(&orig_message);
+ string serialized;
+ orig_message.SerializeToString(&serialized);
+
+ proto2_preserve_unknown_enum_unittest::MyMessage message;
+ EXPECT_EQ(true, message.ParseFromString(serialized));
+ CheckMessage(message);
+
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* d = message.GetDescriptor();
+ const google::protobuf::FieldDescriptor* field = d->FindFieldByName("e");
+
+ // This should dynamically create an EnumValueDescriptor.
+ const google::protobuf::EnumValueDescriptor* enum_value = r->GetEnum(message, field);
+ EXPECT_EQ(enum_value->number(),
+ static_cast<int>(proto2_preserve_unknown_enum_unittest::E_EXTRA));
+
+ // Fetching value for a second time should return the same pointer.
+ const google::protobuf::EnumValueDescriptor* enum_value_second =
+ r->GetEnum(message, field);
+ EXPECT_EQ(enum_value, enum_value_second);
+
+ // Check the repeated case too.
+ const google::protobuf::FieldDescriptor* repeated_field =
+ d->FindFieldByName("repeated_e");
+ enum_value = r->GetRepeatedEnum(message, repeated_field, 0);
+ EXPECT_EQ(enum_value->number(),
+ static_cast<int>(proto2_preserve_unknown_enum_unittest::E_EXTRA));
+ // Should reuse the same EnumValueDescriptor, even for a different field.
+ EXPECT_EQ(enum_value, enum_value_second);
+
+ // We should be able to use the returned value descriptor to set a value on
+ // another message.
+ google::protobuf::Message* m = message.New();
+ r->SetEnum(m, field, enum_value);
+ EXPECT_EQ(enum_value, r->GetEnum(*m, field));
+ delete m;
+}
+
+// Test that the new integer-based enum reflection API works.
+TEST(PreserveUnknownEnumTest, IntegerEnumReflectionAPI) {
+ proto2_preserve_unknown_enum_unittest::MyMessage message;
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* d = message.GetDescriptor();
+
+ const google::protobuf::FieldDescriptor* singular_field = d->FindFieldByName("e");
+ const google::protobuf::FieldDescriptor* repeated_field =
+ d->FindFieldByName("repeated_e");
+
+ r->SetEnumValue(&message, singular_field, 42);
+ EXPECT_EQ(42, r->GetEnumValue(message, singular_field));
+ r->AddEnumValue(&message, repeated_field, 42);
+ r->AddEnumValue(&message, repeated_field, 42);
+ EXPECT_EQ(42, r->GetRepeatedEnumValue(message, repeated_field, 0));
+ r->SetRepeatedEnumValue(&message, repeated_field, 1, 84);
+ EXPECT_EQ(84, r->GetRepeatedEnumValue(message, repeated_field, 1));
+ const google::protobuf::EnumValueDescriptor* enum_value = r->GetEnum(message,
+ singular_field);
+ EXPECT_EQ(42, enum_value->number());
+}
+
+// Test that the EnumValue API works properly for proto2 messages as well.
+TEST(PreserveUnknownEnumTest, Proto2CatchesUnknownValues) {
+ protobuf_unittest::TestAllTypes message; // proto2 message
+ const google::protobuf::Reflection* r = message.GetReflection();
+ const google::protobuf::Descriptor* d = message.GetDescriptor();
+ const google::protobuf::FieldDescriptor* singular_field =
+ d->FindFieldByName("optional_nested_enum");
+ const google::protobuf::FieldDescriptor* repeated_field =
+ d->FindFieldByName("repeated_nested_enum");
+ // Add one element to the repeated field so that we can test
+ // SetRepeatedEnumValue.
+ const google::protobuf::EnumValueDescriptor* enum_value =
+ repeated_field->enum_type()->FindValueByName("BAR");
+ EXPECT_TRUE(enum_value != NULL);
+ r->AddEnum(&message, repeated_field, enum_value);
+
+#ifdef PROTOBUF_HAS_DEATH_TEST
+ // Enum-field integer-based setters GOOGLE_DCHECK-fail on invalid values, in order to
+ // remain consistent with proto2 generated code.
+ EXPECT_DEBUG_DEATH({
+ r->SetEnumValue(&message, singular_field, 4242);
+ r->GetEnum(message, singular_field)->number();
+ }, "SetEnumValue accepts only valid integer values");
+ EXPECT_DEBUG_DEATH({
+ r->SetRepeatedEnumValue(&message, repeated_field, 0, 4242);
+ r->GetRepeatedEnum(message, repeated_field, 0);
+ }, "SetRepeatedEnumValue accepts only valid integer values");
+ EXPECT_DEBUG_DEATH({
+ r->AddEnumValue(&message, repeated_field, 4242);
+ r->GetRepeatedEnum(message, repeated_field, 1);
+ }, "AddEnumValue accepts only valid integer values");
+#endif // PROTOBUF_HAS_DEATH_TEST
+}
+
+TEST(PreserveUnknownEnumTest, SupportsUnknownEnumValuesAPI) {
+ protobuf_unittest::TestAllTypes proto2_message;
+ proto2_preserve_unknown_enum_unittest::MyMessage new_message;
+
+ const google::protobuf::Reflection* proto2_reflection = proto2_message.GetReflection();
+ const google::protobuf::Reflection* new_reflection = new_message.GetReflection();
+
+ EXPECT_FALSE(proto2_reflection->SupportsUnknownEnumValues());
+ EXPECT_TRUE(new_reflection->SupportsUnknownEnumValues());
+}
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/proto3_arena_unittest.cc b/src/google/protobuf/proto3_arena_unittest.cc
new file mode 100644
index 00000000..c3b5996f
--- /dev/null
+++ b/src/google/protobuf/proto3_arena_unittest.cc
@@ -0,0 +1,185 @@
+// 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 <string>
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+#include <vector>
+
+#include <google/protobuf/test_util.h>
+#include <google/protobuf/unittest.pb.h>
+#include <google/protobuf/unittest_proto3_arena.pb.h>
+#include <google/protobuf/arena.h>
+#include <google/protobuf/testing/googletest.h>
+#include <gtest/gtest.h>
+
+namespace google {
+using proto3_arena_unittest::TestAllTypes;
+
+namespace protobuf {
+namespace {
+// We selectively set/check a few representative fields rather than all fields
+// as this test is only expected to cover the basics of arena support.
+void SetAllFields(TestAllTypes* m) {
+ m->set_optional_int32(100);
+ m->set_optional_string("asdf");
+ m->set_optional_bytes("jkl;");
+ m->mutable_optional_nested_message()->set_bb(42);
+ 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->mutable_optional_lazy_message()->set_bb(45);
+ m->add_repeated_int32(100);
+ m->add_repeated_string("asdf");
+ m->add_repeated_bytes("jkl;");
+ m->add_repeated_nested_message()->set_bb(46);
+ 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_lazy_message()->set_bb(49);
+
+ m->set_oneof_uint32(1);
+ m->mutable_oneof_nested_message()->set_bb(50);
+ m->set_oneof_string("test"); // only this one remains set
+}
+
+void ExpectAllFieldsSet(const TestAllTypes& m) {
+ EXPECT_EQ(100, m.optional_int32());
+ EXPECT_EQ("asdf", m.optional_string());
+ EXPECT_EQ("jkl;", m.optional_bytes());
+ EXPECT_EQ(true, m.has_optional_nested_message());
+ EXPECT_EQ(42, m.optional_nested_message().bb());
+ EXPECT_EQ(true, m.has_optional_foreign_message());
+ 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(true, m.has_optional_lazy_message());
+ EXPECT_EQ(45, m.optional_lazy_message().bb());
+
+ EXPECT_EQ(1, m.repeated_int32_size());
+ EXPECT_EQ(100, m.repeated_int32(0));
+ EXPECT_EQ(1, m.repeated_string_size());
+ EXPECT_EQ("asdf", m.repeated_string(0));
+ EXPECT_EQ(1, m.repeated_bytes_size());
+ EXPECT_EQ("jkl;", m.repeated_bytes(0));
+ EXPECT_EQ(1, m.repeated_nested_message_size());
+ EXPECT_EQ(46, m.repeated_nested_message(0).bb());
+ EXPECT_EQ(1, m.repeated_foreign_message_size());
+ EXPECT_EQ(47, m.repeated_foreign_message(0).c());
+ EXPECT_EQ(1, m.repeated_nested_enum_size());
+ 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(1, m.repeated_lazy_message_size());
+ EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
+
+ EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
+ m.oneof_field_case());
+ EXPECT_EQ("test", m.oneof_string());
+}
+
+// In this file we only test some basic functionalities of arena support in
+// proto3 and expect the arena support to be fully tested in proto2 unittests
+// because proto3 shares most code with proto2.
+
+TEST(ArenaTest, Parsing) {
+ TestAllTypes original;
+ SetAllFields(&original);
+
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->ParseFromString(original.SerializeAsString());
+ ExpectAllFieldsSet(*arena_message);
+}
+
+TEST(ArenaTest, UnknownFields) {
+ TestAllTypes original;
+ SetAllFields(&original);
+
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->ParseFromString(original.SerializeAsString());
+ ExpectAllFieldsSet(*arena_message);
+
+ // In proto3 we can still get a pointer to the UnknownFieldSet through
+ // reflection API.
+ UnknownFieldSet* unknown_fields =
+ arena_message->GetReflection()->MutableUnknownFields(arena_message);
+ // We can modify this UnknownFieldSet.
+ unknown_fields->AddVarint(1, 2);
+ // But the change will never will serialized back.
+ ASSERT_EQ(original.ByteSize(), arena_message->ByteSize());
+ ASSERT_TRUE(
+ arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
+}
+
+TEST(ArenaTest, Swap) {
+ Arena arena1;
+ Arena arena2;
+
+ // Test Swap().
+ TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
+ TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
+ arena1_message->Swap(arena2_message);
+ EXPECT_EQ(&arena1, arena1_message->GetArena());
+ EXPECT_EQ(&arena2, arena2_message->GetArena());
+}
+
+TEST(ArenaTest, SetAllocatedMessage) {
+ Arena arena;
+ TestAllTypes *arena_message = Arena::CreateMessage<TestAllTypes>(&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());
+}
+
+TEST(ArenaTest, ReleaseMessage) {
+ Arena arena;
+ TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
+ arena_message->mutable_optional_nested_message()->set_bb(118);
+ google::protobuf::scoped_ptr<TestAllTypes::NestedMessage> nested(
+ arena_message->release_optional_nested_message());
+ EXPECT_EQ(118, nested->bb());
+}
+
+} // namespace
+} // namespace protobuf
+} // namespace google
diff --git a/src/google/protobuf/proto_cast.h b/src/google/protobuf/proto_cast.h
new file mode 100644
index 00000000..e25c219f
--- /dev/null
+++ b/src/google/protobuf/proto_cast.h
@@ -0,0 +1,58 @@
+// 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_UTIL_PROTO_CAST_H__
+#define GOOGLE_PROTOBUF_UTIL_PROTO_CAST_H__
+
+#include <string>
+
+#include <google/protobuf/stubs/common.h>
+
+// proto_cast<> is used to simulate over-the-wire conversion of one
+// proto message into another. This is primarily useful for unit tests
+// which validate the version-compatibility semantics of protobufs.
+// Usage is similar to C++-style typecasts:
+//
+// OldMessage old_message = /*...*/;
+// NewMessage new_message = proto_cast<NewMessage>(old_message);
+namespace google {
+template<typename NewProto,
+ typename OldProto>
+NewProto proto_cast(const OldProto& old_proto) {
+ string wire_format;
+ GOOGLE_CHECK(old_proto.SerializeToString(&wire_format));
+
+ NewProto new_proto;
+ GOOGLE_CHECK(new_proto.ParseFromString(wire_format));
+ return new_proto;
+}
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_UTIL_PROTO_CAST_H__
diff --git a/src/google/protobuf/proto_cast_test.cc b/src/google/protobuf/proto_cast_test.cc
new file mode 100644
index 00000000..eb101eb6
--- /dev/null
+++ b/src/google/protobuf/proto_cast_test.cc
@@ -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.
+
+#include <google/protobuf/util/proto_cast.h>
+
+#include <google/protobuf/util/unknown_enum_test.pb.h>
+#include <gtest/gtest.h>
+#include <google/protobuf/testing/gmock.h>
+
+namespace google {
+using google::protobuf::util::UpRevision;
+using google::protobuf::util::DownRevision;
+
+namespace {
+
+TEST(ProtoCastTest, V2KnownValue) {
+ UpRevision sender;
+ sender.set_value(UpRevision::NONDEFAULT_VALUE);
+
+ DownRevision receiver = proto_cast<DownRevision>(sender);
+ ASSERT_EQ(DownRevision::NONDEFAULT_VALUE, receiver.value());
+}
+
+TEST(ProtoCastTest, V2UnknownValue) {
+ UpRevision sender;
+ sender.set_value(UpRevision::NEW_VALUE);
+
+ DownRevision receiver = proto_cast<DownRevision>(sender);
+ ASSERT_EQ(DownRevision::DEFAULT_VALUE, receiver.value());
+}
+
+} // namespace
+} // namespace google
diff --git a/src/google/protobuf/reflection.h b/src/google/protobuf/reflection.h
new file mode 100644
index 00000000..17d89a18
--- /dev/null
+++ b/src/google/protobuf/reflection.h
@@ -0,0 +1,306 @@
+// 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 header defines the RepeatedFieldRef class template used to access
+// repeated fields with protobuf reflection API.
+#ifndef GOOGLE_PROTOBUF_REFLECTION_H__
+#define GOOGLE_PROTOBUF_REFLECTION_H__
+
+#include <google/protobuf/message.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+template<typename T, typename Enable = void>
+struct RefTypeTraits;
+} // namespace internal
+
+template<typename T>
+RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef(
+ const Message& message, const FieldDescriptor* field) const {
+ return RepeatedFieldRef<T>(message, field);
+}
+
+template<typename T>
+MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef(
+ Message* message, const FieldDescriptor* field) const {
+ return MutableRepeatedFieldRef<T>(message, field);
+}
+
+// RepeatedFieldRef definition for non-message types.
+template<typename T>
+class RepeatedFieldRef<
+ T, typename internal::enable_if<!internal::is_base_of<Message, T>::value>::type> {
+ typedef typename internal::RefTypeTraits<T>::iterator IteratorType;
+ typedef typename internal::RefTypeTraits<T>::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<T>(data_, index);
+ }
+
+ typedef IteratorType iterator;
+ typedef IteratorType const_iterator;
+ 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) {
+ const Reflection* reflection = message.GetReflection();
+ data_ = reflection->RepeatedFieldData(
+ const_cast<Message*>(&message), field,
+ internal::RefTypeTraits<T>::cpp_type, NULL);
+ accessor_ = reflection->RepeatedFieldAccessor(field);
+ }
+
+ const void* data_;
+ const AccessorType* accessor_;
+};
+
+// MutableRepeatedFieldRef definition for non-message types.
+template<typename T>
+class MutableRepeatedFieldRef<
+ T, typename internal::enable_if<!internal::is_base_of<Message, T>::value>::type> {
+ typedef typename internal::RefTypeTraits<T>::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<T>(data_, index);
+ }
+
+ void Set(int index, const T& value) const {
+ accessor_->template Set<T>(data_, index, value);
+ }
+ void Add(const T& value) const {
+ accessor_->template Add<T>(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 Swap(const MutableRepeatedFieldRef& other) const {
+ accessor_->Swap(data_, other.accessor_, other.data_);
+ }
+
+ template<typename Container>
+ void MergeFrom(const Container& container) const {
+ typedef typename Container::const_iterator Iterator;
+ for (Iterator it = container.begin(); it != container.end(); ++it) {
+ Add(*it);
+ }
+ }
+ template<typename Container>
+ void CopyFrom(const Container& container) const {
+ Clear();
+ MergeFrom(container);
+ }
+
+ private:
+ friend class Reflection;
+ MutableRepeatedFieldRef(
+ Message* message,
+ const FieldDescriptor* field) {
+ const Reflection* reflection = message->GetReflection();
+ data_ = reflection->RepeatedFieldData(
+ message, field, internal::RefTypeTraits<T>::cpp_type, NULL);
+ accessor_ = reflection->RepeatedFieldAccessor(field);
+ }
+
+ void* data_;
+ const AccessorType* accessor_;
+};
+
+// RepeatedFieldRef definition for message types.
+template<typename T>
+class RepeatedFieldRef<
+ T, typename internal::enable_if<internal::is_base_of<Message, T>::value>::type> {
+ typedef typename internal::RefTypeTraits<T>::iterator IteratorType;
+ typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
+
+ public:
+ 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
+ // will be returned.
+ //
+ // Example:
+ // RepeatedFieldRef<Message> h = ...
+ // unique_ptr<Message> scratch_space(h.NewMessage());
+ // const Message& item = h.Get(index, scratch_space.get());
+ const T& Get(int index, T* scratch_space) const {
+ return *static_cast<const T*>(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<T*>(default_instance_->New());
+ }
+
+ typedef IteratorType iterator;
+ typedef IteratorType const_iterator;
+ iterator begin() const {
+ return iterator(data_, accessor_, true, NewMessage());
+ }
+ iterator end() const {
+ return iterator(data_, accessor_, false, NewMessage());
+ }
+
+ private:
+ friend class Reflection;
+ RepeatedFieldRef(
+ const Message& message,
+ const FieldDescriptor* field) {
+ const Reflection* reflection = message.GetReflection();
+ data_ = reflection->RepeatedFieldData(
+ const_cast<Message*>(&message), field,
+ internal::RefTypeTraits<T>::cpp_type,
+ internal::RefTypeTraits<T>::GetMessageFieldDescriptor());
+ accessor_ = reflection->RepeatedFieldAccessor(field);
+ default_instance_ =
+ reflection->GetMessageFactory()->GetPrototype(field->message_type());
+ }
+
+ const void* data_;
+ const AccessorType* accessor_;
+ const Message* default_instance_;
+};
+
+// MutableRepeatedFieldRef definition for non-message types.
+template<typename T>
+class MutableRepeatedFieldRef<
+ T, typename internal::enable_if<internal::is_base_of<Message, T>::value>::type> {
+ typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
+
+ public:
+ bool empty() const {
+ return accessor_->IsEmpty(data_);
+ }
+ int size() const {
+ return accessor_->Size(data_);
+ }
+ // See comments for RepeatedFieldRef<Message>::Get()
+ const T& Get(int index, T* scratch_space) const {
+ return *static_cast<const T*>(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<T*>(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 SwapElements(int index1, int index2) const {
+ accessor_->SwapElements(data_, index1, index2);
+ }
+ void Clear() const {
+ accessor_->Clear(data_);
+ }
+
+ void Swap(const MutableRepeatedFieldRef& other) const {
+ accessor_->Swap(data_, other.accessor_, other.data_);
+ }
+
+ template<typename Container>
+ void MergeFrom(const Container& container) const {
+ typedef typename Container::const_iterator Iterator;
+ for (Iterator it = container.begin(); it != container.end(); ++it) {
+ Add(*it);
+ }
+ }
+ template<typename Container>
+ void CopyFrom(const Container& container) const {
+ Clear();
+ MergeFrom(container);
+ }
+
+ private:
+ friend class Reflection;
+ MutableRepeatedFieldRef(
+ Message* message,
+ const FieldDescriptor* field) {
+ const Reflection* reflection = message->GetReflection();
+ data_ = reflection->RepeatedFieldData(
+ message, field, internal::RefTypeTraits<T>::cpp_type,
+ internal::RefTypeTraits<T>::GetMessageFieldDescriptor());
+ accessor_ = reflection->RepeatedFieldAccessor(field);
+ default_instance_ =
+ reflection->GetMessageFactory()->GetPrototype(field->message_type());
+ }
+
+ void* data_;
+ const AccessorType* accessor_;
+ const Message* default_instance_;
+};
+} // namespace protobuf
+} // namespace google
+
+// Implementation details for (Mutable)RepeatedFieldRef.
+#include <google/protobuf/repeated_field_reflection.h>
+
+#endif // GOOGLE_PROTOBUF_REFLECTION_H__
diff --git a/src/google/protobuf/reflection_internal.h b/src/google/protobuf/reflection_internal.h
new file mode 100644
index 00000000..fcb42471
--- /dev/null
+++ b/src/google/protobuf/reflection_internal.h
@@ -0,0 +1,378 @@
+// 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_REFLECTION_INTERNAL_H__
+#define GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
+
+#include <google/protobuf/map_field.h>
+#include <google/protobuf/reflection.h>
+#include <google/protobuf/repeated_field.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+// A base class for RepeatedFieldAccessor implementations that can support
+// random-access efficiently. All iterator methods delegates the work to
+// corresponding random-access methods.
+class RandomAccessRepeatedFieldAccessor : public RepeatedFieldAccessor {
+ public:
+ virtual ~RandomAccessRepeatedFieldAccessor() {}
+
+ virtual Iterator* BeginIterator(const Field* data) const {
+ return PositionToIterator(0);
+ }
+ virtual Iterator* EndIterator(const Field* data) const {
+ return PositionToIterator(this->Size(data));
+ }
+ virtual Iterator* CopyIterator(const Field* data,
+ const Iterator* iterator) const {
+ return const_cast<Iterator*>(iterator);
+ }
+ virtual Iterator* AdvanceIterator(const Field* data,
+ Iterator* iterator) const {
+ return PositionToIterator(IteratorToPosition(iterator) + 1);
+ }
+ virtual bool EqualsIterator(const Field* data,
+ const Iterator* a,
+ const Iterator* b) const {
+ return a == b;
+ }
+ virtual void DeleteIterator(const Field* data, Iterator* iterator) const {
+ }
+ virtual const Value* GetIteratorValue(const Field* data,
+ const Iterator* iterator,
+ Value* scratch_space) const {
+ return Get(data, static_cast<int>(IteratorToPosition(iterator)),
+ scratch_space);
+ }
+
+ private:
+ static intptr_t IteratorToPosition(const Iterator* iterator) {
+ return reinterpret_cast<intptr_t>(iterator);
+ }
+ static Iterator* PositionToIterator(intptr_t position) {
+ return reinterpret_cast<Iterator*>(position);
+ }
+};
+
+// Base class for RepeatedFieldAccessor implementations that manipulates
+// RepeatedField<T>.
+template<typename T>
+class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor {
+ public:
+ RepeatedFieldWrapper() {}
+ virtual ~RepeatedFieldWrapper() {}
+ virtual bool IsEmpty(const Field* data) const {
+ return GetRepeatedField(data)->empty();
+ }
+ virtual int Size(const Field* data) const {
+ return GetRepeatedField(data)->size();
+ }
+ virtual const Value* Get(const Field* data, int index,
+ Value* scratch_space) const {
+ return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
+ }
+ virtual void Clear(Field* data) const {
+ MutableRepeatedField(data)->Clear();
+ }
+ virtual void Set(Field* data, int index, const Value* value) const {
+ MutableRepeatedField(data)->Set(index, ConvertToT(value));
+ }
+ virtual void Add(Field* data, const Value* value) const {
+ MutableRepeatedField(data)->Add(ConvertToT(value));
+ }
+ virtual void RemoveLast(Field* data) const {
+ MutableRepeatedField(data)->RemoveLast();
+ }
+ virtual void SwapElements(Field* data, int index1, int index2) const {
+ MutableRepeatedField(data)->SwapElements(index1, index2);
+ }
+
+ protected:
+ typedef RepeatedField<T> RepeatedFieldType;
+ static const RepeatedFieldType* GetRepeatedField(const Field* data) {
+ return reinterpret_cast<const RepeatedFieldType*>(data);
+ }
+ static RepeatedFieldType* MutableRepeatedField(Field* data) {
+ return reinterpret_cast<RepeatedFieldType*>(data);
+ }
+
+ // Convert an object recevied by this accessor to an object to be stored in
+ // the underlying RepeatedField.
+ 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
+ // scratch_space and scratch_space should be returned.
+ virtual const Value* ConvertFromT(const T& value,
+ Value* scratch_space) const = 0;
+};
+
+// Base class for RepeatedFieldAccessor implementations that manipulates
+// RepeatedPtrField<T>.
+template<typename T>
+class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor {
+ public:
+ RepeatedPtrFieldWrapper() {}
+ virtual ~RepeatedPtrFieldWrapper() {}
+ virtual bool IsEmpty(const Field* data) const {
+ return GetRepeatedField(data)->empty();
+ }
+ virtual int Size(const Field* data) const {
+ return GetRepeatedField(data)->size();
+ }
+ virtual const Value* Get(const Field* data, int index,
+ Value* scratch_space) const {
+ return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
+ }
+ virtual void Clear(Field* data) const {
+ MutableRepeatedField(data)->Clear();
+ }
+ virtual void Set(Field* data, int index, const Value* value) const {
+ ConvertToT(value, MutableRepeatedField(data)->Mutable(index));
+ }
+ virtual void Add(Field* data, const Value* value) const {
+ T* allocated = New(value);
+ ConvertToT(value, allocated);
+ MutableRepeatedField(data)->AddAllocated(allocated);
+ }
+ virtual void RemoveLast(Field* data) const {
+ MutableRepeatedField(data)->RemoveLast();
+ }
+ virtual void SwapElements(Field* data, int index1, int index2) const {
+ MutableRepeatedField(data)->SwapElements(index1, index2);
+ }
+
+ protected:
+ typedef RepeatedPtrField<T> RepeatedFieldType;
+ static const RepeatedFieldType* GetRepeatedField(const Field* data) {
+ return reinterpret_cast<const RepeatedFieldType*>(data);
+ }
+ static RepeatedFieldType* MutableRepeatedField(Field* data) {
+ return reinterpret_cast<RepeatedFieldType*>(data);
+ }
+
+ // Create a new T instance. For repeated message fields, T can be specified
+ // as google::protobuf::Message so we can't use "new T()" directly. In that case, value
+ // should be a message of the same type (it's ensured by the caller) and a
+ // new message object will be created using it.
+ virtual T* New(const Value* value) const = 0;
+
+ // Convert an object received by this accessor to an object that will be
+ // stored in the underlying RepeatedPtrField.
+ 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
+ // scratch_space and scratch_space should be returned.
+ virtual const Value* ConvertFromT(const T& value,
+ Value* scratch_space) const = 0;
+};
+
+// An implementation of RandomAccessRepeatedFieldAccessor that manipulates
+// MapFieldBase.
+class MapFieldAccessor : public RandomAccessRepeatedFieldAccessor {
+ public:
+ MapFieldAccessor() {}
+ virtual ~MapFieldAccessor() {}
+ virtual bool IsEmpty(const Field* data) const {
+ return GetRepeatedField(data)->empty();
+ }
+ virtual int Size(const Field* data) const {
+ return GetRepeatedField(data)->size();
+ }
+ virtual const Value* Get(const Field* data, int index,
+ Value* scratch_space) const {
+ return ConvertFromEntry(GetRepeatedField(data)->Get(index), scratch_space);
+ }
+ virtual void Clear(Field* data) const {
+ MutableRepeatedField(data)->Clear();
+ }
+ virtual void Set(Field* data, int index, const Value* value) const {
+ ConvertToEntry(value, MutableRepeatedField(data)->Mutable(index));
+ }
+ virtual void Add(Field* data, const Value* value) const {
+ Message* allocated = New(value);
+ ConvertToEntry(value, allocated);
+ MutableRepeatedField(data)->AddAllocated(allocated);
+ }
+ virtual void RemoveLast(Field* data) const {
+ MutableRepeatedField(data)->RemoveLast();
+ }
+ virtual void SwapElements(Field* data, int index1, int index2) const {
+ MutableRepeatedField(data)->SwapElements(index1, index2);
+ }
+ virtual void Swap(
+ Field* data,
+ const internal::RepeatedFieldAccessor* other_mutator,
+ Field* other_data) const {
+ GOOGLE_CHECK(this == other_mutator);
+ MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
+ }
+
+ protected:
+ typedef RepeatedPtrField<Message> RepeatedFieldType;
+ static const RepeatedFieldType* GetRepeatedField(const Field* data) {
+ return reinterpret_cast<const RepeatedFieldType*>(
+ (&reinterpret_cast<const MapFieldBase*>(data)->GetRepeatedField()));
+ }
+ static RepeatedFieldType* MutableRepeatedField(Field* data) {
+ return reinterpret_cast<RepeatedFieldType*>(
+ reinterpret_cast<MapFieldBase*>(data)->MutableRepeatedField());
+ }
+ virtual Message* New(const Value* value) const {
+ return static_cast<const Message*>(value)->New();
+ }
+ // Convert an object received by this accessor to an MapEntry message to be
+ // stored in the underlying MapFieldBase.
+ virtual void ConvertToEntry(const Value* value, Message* result) const {
+ result->CopyFrom(*static_cast<const Message*>(value));
+ }
+ // Convert a MapEntry message stored in the underlying MapFieldBase to an
+ // object that will be returned by this accessor.
+ virtual const Value* ConvertFromEntry(const Message& value,
+ Value* scratch_space) const {
+ return static_cast<const Value*>(&value);
+ }
+};
+
+// Default implementations of RepeatedFieldAccessor for primitive types.
+template<typename T>
+class RepeatedFieldPrimitiveAccessor : public RepeatedFieldWrapper<T> {
+ typedef void Field;
+ typedef void Value;
+ using RepeatedFieldWrapper<T>::MutableRepeatedField;
+
+ public:
+ RepeatedFieldPrimitiveAccessor() {}
+ virtual ~RepeatedFieldPrimitiveAccessor() {}
+ virtual void Swap(
+ Field* data,
+ const internal::RepeatedFieldAccessor* other_mutator,
+ Field* other_data) const {
+ // Currently RepeatedFieldPrimitiveAccessor is the only implementation of
+ // RepeatedFieldAccessor for primitive types. As we are using singletons
+ // for these accessors, here "other_mutator" must be "this".
+ GOOGLE_CHECK(this == other_mutator);
+ MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
+ }
+
+ protected:
+ virtual T ConvertToT(const Value* value) const {
+ return *static_cast<const T*>(value);
+ }
+ virtual const Value* ConvertFromT(const T& value,
+ Value* scratch_space) const {
+ return static_cast<const Value*>(&value);
+ }
+};
+
+// Default implementation of RepeatedFieldAccessor for string fields with
+// ctype=STRING.
+class RepeatedPtrFieldStringAccessor : public RepeatedPtrFieldWrapper<string> {
+ typedef void Field;
+ typedef void Value;
+ using RepeatedFieldAccessor::Add;
+
+ public:
+ RepeatedPtrFieldStringAccessor() {}
+ virtual ~RepeatedPtrFieldStringAccessor() {}
+ virtual void Swap(
+ Field* data,
+ const internal::RepeatedFieldAccessor* other_mutator,
+ Field* other_data) const {
+ if (this == other_mutator) {
+ MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
+ } else {
+ RepeatedPtrField<string> tmp;
+ tmp.Swap(MutableRepeatedField(data));
+ int other_size = other_mutator->Size(other_data);
+ for (int i = 0; i < other_size; ++i) {
+ Add<string>(data, other_mutator->Get<string>(other_data, i));
+ }
+ int size = Size(data);
+ other_mutator->Clear(other_data);
+ for (int i = 0; i < size; ++i) {
+ other_mutator->Add<string>(other_data, tmp.Get(i));
+ }
+ }
+ }
+
+ protected:
+ virtual string* New(const Value*) const {
+ return new string();
+ }
+ virtual void ConvertToT(const Value* value, string* result) const {
+ *result = *static_cast<const string*>(value);
+ }
+ virtual const Value* ConvertFromT(const string& value,
+ Value* scratch_space) const {
+ return static_cast<const Value*>(&value);
+ }
+};
+
+
+class RepeatedPtrFieldMessageAccessor
+ : public RepeatedPtrFieldWrapper<Message> {
+ typedef void Field;
+ typedef void Value;
+
+ public:
+ RepeatedPtrFieldMessageAccessor() {}
+ virtual ~RepeatedPtrFieldMessageAccessor() {}
+ virtual void Swap(
+ Field* data,
+ const internal::RepeatedFieldAccessor* other_mutator,
+ Field* other_data) const {
+ GOOGLE_CHECK(this == other_mutator);
+ MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
+ }
+
+ protected:
+ virtual Message* New(const Value* value) const {
+ return static_cast<const Message*>(value)->New();
+ }
+ virtual void ConvertToT(const Value* value, Message* result) const {
+ result->CopyFrom(*static_cast<const Message*>(value));
+ }
+ virtual const Value* ConvertFromT(const Message& value,
+ Value* scratch_space) const {
+ return static_cast<const Value*>(&value);
+ }
+};
+} // namespace internal
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
diff --git a/src/google/protobuf/repeated_field.cc b/src/google/protobuf/repeated_field.cc
index b400732f..e5aedadc 100644
--- a/src/google/protobuf/repeated_field.cc
+++ b/src/google/protobuf/repeated_field.cc
@@ -42,42 +42,52 @@ namespace protobuf {
namespace internal {
-void RepeatedPtrFieldBase::Reserve(int new_size) {
- if (total_size_ >= new_size) return;
-
- void** old_elements = elements_;
- total_size_ = max(kMinRepeatedFieldAllocationSize,
- max(total_size_ * 2, new_size));
- elements_ = new void*[total_size_];
- if (old_elements != NULL) {
- memcpy(elements_, old_elements, allocated_size_ * sizeof(elements_[0]));
- delete [] old_elements;
+void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
+ int new_size = current_size_ + extend_amount;
+ if (total_size_ >= new_size) {
+ // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence
+ // total_size must be non-zero since it is lower-bounded by new_size.
+ return &rep_->elements[current_size_];
+ }
+ Rep* old_rep = rep_;
+ Arena* arena = GetArenaNoVirtual();
+ new_size = max(kMinRepeatedFieldAllocationSize,
+ max(total_size_ * 2, new_size));
+ if (arena == NULL) {
+ rep_ = reinterpret_cast<Rep*>(
+ new char[kRepHeaderSize + sizeof(old_rep->elements[0])*new_size]);
+ } else {
+ rep_ = reinterpret_cast<Rep*>(
+ ::google::protobuf::Arena::CreateArray<char>(arena,
+ kRepHeaderSize + sizeof(old_rep->elements[0])*new_size));
+ }
+ total_size_ = new_size;
+ if (old_rep && old_rep->allocated_size > 0) {
+ memcpy(rep_->elements, old_rep->elements,
+ old_rep->allocated_size * sizeof(rep_->elements[0]));
+ rep_->allocated_size = old_rep->allocated_size;
+ } else {
+ rep_->allocated_size = 0;
+ }
+ if (arena == NULL) {
+ delete [] reinterpret_cast<char*>(old_rep);
}
+ return &rep_->elements[current_size_];
}
-void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) {
- if (this == other) return;
- void** swap_elements = elements_;
- int swap_current_size = current_size_;
- int swap_allocated_size = allocated_size_;
- int swap_total_size = total_size_;
-
- elements_ = other->elements_;
- current_size_ = other->current_size_;
- allocated_size_ = other->allocated_size_;
- total_size_ = other->total_size_;
-
- other->elements_ = swap_elements;
- other->current_size_ = swap_current_size;
- other->allocated_size_ = swap_allocated_size;
- other->total_size_ = swap_total_size;
+void RepeatedPtrFieldBase::Reserve(int new_size) {
+ if (new_size > current_size_) {
+ InternalExtend(new_size - current_size_);
+ }
}
-string* StringTypeHandlerBase::New() {
- return new string;
-}
-void StringTypeHandlerBase::Delete(string* value) {
- delete value;
+void RepeatedPtrFieldBase::CloseGap(int start, int num) {
+ if (rep_ == NULL) return;
+ // Close up a gap of "num" elements starting at offset "start".
+ for (int i = start + num; i < rep_->allocated_size; ++i)
+ rep_->elements[i - num] = rep_->elements[i];
+ current_size_ -= num;
+ rep_->allocated_size -= num;
}
} // namespace internal
diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h
index 50051831..4798eeda 100644
--- a/src/google/protobuf/repeated_field.h
+++ b/src/google/protobuf/repeated_field.h
@@ -53,8 +53,10 @@
#include <string>
#include <iterator>
+#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/type_traits.h>
+#include <google/protobuf/arena.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/message_lite.h>
@@ -83,7 +85,8 @@ inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) {
}
template <typename Iter>
-inline int CalculateReserve(Iter begin, Iter end, std::input_iterator_tag) {
+inline int CalculateReserve(Iter /*begin*/, Iter /*end*/,
+ std::input_iterator_tag /*unused*/) {
return -1;
}
@@ -103,6 +106,7 @@ template <typename Element>
class RepeatedField {
public:
RepeatedField();
+ explicit RepeatedField(Arena* arena);
RepeatedField(const RepeatedField& other);
template <typename Iter>
RepeatedField(Iter begin, const Iter& end);
@@ -152,9 +156,16 @@ class RepeatedField {
Element* mutable_data();
const Element* data() const;
- // Swap entire contents with "other".
+ // Swap entire contents with "other". If they are separate arenas then, copies
+ // data between each other.
void Swap(RepeatedField* other);
+ // Swap entire contents with "other". Should be called only if the caller can
+ // guarantee that both repeated fields are on the same arena or are on the
+ // heap. Swapping between different arenas is disallowed and caught by a
+ // GOOGLE_DCHECK (see API docs for details).
+ void UnsafeArenaSwap(RepeatedField* other);
+
// Swap two elements.
void SwapElements(int index1, int index2);
@@ -171,8 +182,10 @@ class RepeatedField {
iterator begin();
const_iterator begin() const;
+ const_iterator cbegin() const;
iterator end();
const_iterator end() const;
+ const_iterator cend() const;
// Reverse iterator support
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
@@ -194,22 +207,67 @@ class RepeatedField {
// sizeof(*this)
int SpaceUsedExcludingSelf() const;
+ // Remove the element referenced by position.
+ iterator erase(const_iterator position);
+
+ // Remove the elements in the range [first, last).
+ iterator erase(const_iterator first, const_iterator last);
+
+ // Get the Arena on which this RepeatedField stores its elements.
+ ::google::protobuf::Arena* GetArena() const {
+ return GetArenaNoVirtual();
+ }
+
private:
static const int kInitialSize = 0;
-
- Element* elements_;
- int current_size_;
- int total_size_;
+ // A note on the representation here (see also comment below for
+ // RepeatedPtrFieldBase's struct Rep):
+ //
+ // We maintain the same sizeof(RepeatedField) as before we added arena support
+ // so that we do not degrade performance by bloating memory usage. Directly
+ // adding an arena_ element to RepeatedField is quite costly. By using
+ // indirection in this way, we keep the same size when the RepeatedField is
+ // empty (common case), and add only an 8-byte header to the elements array
+ // when non-empty. We make sure to place the size fields directly in the
+ // RepeatedField class to avoid costly cache misses due to the indirection.
+ int current_size_;
+ int total_size_;
+ struct Rep {
+ Arena* arena;
+ Element elements[1];
+ };
+ // We can not use sizeof(Rep) - sizeof(Element) due to the trailing padding on
+ // the struct. We can not use sizeof(Arena*) as well because there might be
+ // a "gap" after the field arena and before the field elements (e.g., when
+ // Element is double and pointer is 32bit).
+ static const size_t kRepHeaderSize;
+ // Contains arena ptr and the elements array. We also keep the invariant that
+ // if rep_ is NULL, then arena is NULL.
+ Rep* rep_;
+
+ friend class Arena;
+ typedef void InternalArenaConstructable_;
// Move the contents of |from| into |to|, possibly clobbering |from| in the
// process. For primitive types this is just a memcpy(), but it could be
// specialized for non-primitive types to, say, swap each element instead.
- void MoveArray(Element to[], Element from[], int size);
+ void MoveArray(Element* to, Element* from, int size);
// Copy the elements of |from| into |to|.
- void CopyArray(Element to[], const Element from[], int size);
+ void CopyArray(Element* to, const Element* from, int size);
+
+ inline void InternalSwap(RepeatedField* other);
+
+ // Internal helper expected by Arena methods.
+ inline Arena* GetArenaNoVirtual() const {
+ return (rep_ == NULL) ? NULL : rep_->arena;
+ }
};
+template<typename Element>
+const size_t RepeatedField<Element>::kRepHeaderSize =
+ reinterpret_cast<size_t>(&reinterpret_cast<Rep*>(16)->elements[0]) - 16;
+
namespace internal {
template <typename It> class RepeatedPtrIterator;
template <typename It, typename VoidPtr> class RepeatedPtrOverPtrsIterator;
@@ -224,13 +282,48 @@ namespace internal {
template <typename Element,
bool HasTrivialCopy = has_trivial_copy<Element>::value>
struct ElementCopier {
- void operator()(Element to[], const Element from[], int array_size);
+ void operator()(Element* to, const Element* from, int array_size);
};
} // namespace internal
namespace internal {
+// type-traits helper for RepeatedPtrFieldBase: we only want to invoke
+// arena-related "copy if on different arena" behavior if the necessary methods
+// exist on the contained type. In particular, we rely on MergeFrom() existing
+// as a general proxy for the fact that a copy will work, and we also provide a
+// specific override for string*.
+template<typename T>
+struct TypeImplementsMergeBehavior {
+ typedef char HasMerge;
+ typedef long HasNoMerge;
+
+ // We accept either of:
+ // - void MergeFrom(const T& other)
+ // - bool MergeFrom(const T& other)
+ //
+ // We mangle these names a bit to avoid compatibility issues in 'unclean'
+ // include environments that may have, e.g., "#define test ..." (yes, this
+ // exists).
+ template<typename U, typename RetType, RetType (U::*)(const U& arg)>
+ struct CheckType;
+ template<typename U> static HasMerge Check(
+ CheckType<U, void, &U::MergeFrom>*);
+ template<typename U> static HasMerge Check(
+ CheckType<U, bool, &U::MergeFrom>*);
+ template<typename U> static HasNoMerge Check(...);
+
+ // Resovles to either google::protobuf::internal::true_type or google::protobuf::internal::false_type.
+ typedef google::protobuf::internal::integral_constant<bool,
+ (sizeof(Check<T>(0)) == sizeof(HasMerge))> type;
+};
+
+template<>
+struct TypeImplementsMergeBehavior< ::std::string > {
+ typedef google::protobuf::internal::true_type type;
+};
+
// This is the common base class for RepeatedPtrFields. It deals only in void*
// pointers. Users should not use this interface directly.
//
@@ -261,11 +354,18 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
// use of AddFromCleared(), which is not part of the public interface.
friend class ExtensionSet;
+ // The MapFieldBase implementation needs to call protected methods directly,
+ // reinterpreting pointers as being to Message instead of a specific Message
+ // subclass.
+ friend class MapFieldBase;
+
// To parse directly into a proto2 generated class, the upb class GMR_Handlers
// needs to be able to modify a RepeatedPtrFieldBase directly.
friend class LIBPROTOBUF_EXPORT upb::google_opensource::GMR_Handlers;
RepeatedPtrFieldBase();
+ explicit RepeatedPtrFieldBase(::google::protobuf::Arena* arena);
+ ~RepeatedPtrFieldBase() {}
// Must be called from destructor.
template <typename TypeHandler>
@@ -279,7 +379,10 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
typename TypeHandler::Type* Mutable(int index);
template <typename TypeHandler>
- typename TypeHandler::Type* Add();
+ void Delete(int index);
+ template <typename TypeHandler>
+ typename TypeHandler::Type* Add(typename TypeHandler::Type* prototype = NULL);
+
template <typename TypeHandler>
void RemoveLast();
template <typename TypeHandler>
@@ -289,13 +392,7 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
void CopyFrom(const RepeatedPtrFieldBase& other);
- void CloseGap(int start, int num) {
- // Close up a gap of "num" elements starting at offset "start".
- for (int i = start + num; i < allocated_size_; ++i)
- elements_[i - num] = elements_[i];
- current_size_ -= num;
- allocated_size_ -= num;
- }
+ void CloseGap(int start, int num);
void Reserve(int new_size);
@@ -310,7 +407,8 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
const typename TypeHandler::Type* const* data() const;
- void Swap(RepeatedPtrFieldBase* other);
+ template <typename TypeHandler>
+ inline void Swap(RepeatedPtrFieldBase* other) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
void SwapElements(int index1, int index2);
@@ -324,10 +422,25 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
typename TypeHandler::Type* AddFromCleared();
+ template<typename TypeHandler>
+ void AddAllocated(typename TypeHandler::Type* value) {
+ typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
+ AddAllocatedInternal<TypeHandler>(value, t);
+ }
+
template <typename TypeHandler>
- void AddAllocated(typename TypeHandler::Type* value);
+ void UnsafeArenaAddAllocated(typename TypeHandler::Type* value);
+
template <typename TypeHandler>
- typename TypeHandler::Type* ReleaseLast();
+ typename TypeHandler::Type* ReleaseLast() {
+ typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
+ return ReleaseLastInternal<TypeHandler>(t);
+ }
+
+ // Releases last element and returns it, but does not do out-of-arena copy.
+ // And just returns the raw pointer to the contained element in the arena.
+ template <typename TypeHandler>
+ typename TypeHandler::Type* UnsafeArenaReleaseLast();
int ClearedCount() const;
template <typename TypeHandler>
@@ -335,13 +448,61 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
template <typename TypeHandler>
typename TypeHandler::Type* ReleaseCleared();
+ protected:
+ inline void InternalSwap(RepeatedPtrFieldBase* other);
+
+ template <typename TypeHandler>
+ void AddAllocatedInternal(typename TypeHandler::Type* value,
+ google::protobuf::internal::true_type);
+ template <typename TypeHandler>
+ void AddAllocatedInternal(typename TypeHandler::Type* value,
+ google::protobuf::internal::false_type);
+
+ template <typename TypeHandler>
+ void AddAllocatedSlowWithCopy(typename TypeHandler::Type* value,
+ Arena* value_arena,
+ Arena* my_arena)
+ GOOGLE_ATTRIBUTE_NOINLINE;
+ template <typename TypeHandler>
+ void AddAllocatedSlowWithoutCopy(typename TypeHandler::Type* value)
+ GOOGLE_ATTRIBUTE_NOINLINE;
+
+ template <typename TypeHandler>
+ typename TypeHandler::Type* ReleaseLastInternal(google::protobuf::internal::true_type);
+ template <typename TypeHandler>
+ typename TypeHandler::Type* ReleaseLastInternal(google::protobuf::internal::false_type);
+
+ template<typename TypeHandler>
+ inline void SwapFallback(RepeatedPtrFieldBase* other) GOOGLE_ATTRIBUTE_NOINLINE;
+
+ inline Arena* GetArenaNoVirtual() const {
+ return arena_;
+ }
+
private:
static const int kInitialSize = 0;
-
- void** elements_;
+ // A few notes on internal representation:
+ //
+ // We use an indirected approach, with struct Rep, to keep
+ // sizeof(RepeatedPtrFieldBase) equivalent to what it was before arena support
+ // was added, namely, 3 8-byte machine words on x86-64. An instance of Rep is
+ // allocated only when the repeated field is non-empty, and it is a
+ // dynamically-sized struct (the header is directly followed by elements[]).
+ // We place arena_ and current_size_ directly in the object to avoid cache
+ // misses due to the indirection, because these fields are checked frequently.
+ // Placing all fields directly in the RepeatedPtrFieldBase instance costs
+ // significant performance for memory-sensitive workloads.
+ Arena* arena_;
int current_size_;
- int allocated_size_;
int total_size_;
+ struct Rep {
+ 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
+ // if rep_ is NULL, then arena is NULL.
+ Rep* rep_;
template <typename TypeHandler>
static inline typename TypeHandler::Type* cast(void* element) {
@@ -352,6 +513,22 @@ class LIBPROTOBUF_EXPORT RepeatedPtrFieldBase {
return reinterpret_cast<const typename TypeHandler::Type*>(element);
}
+ // Non-templated inner function to avoid code duplication. Takes a function
+ // pointer to the type-specific (templated) inner allocate/merge loop.
+ void MergeFromInternal(
+ const RepeatedPtrFieldBase& other,
+ void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int));
+
+ template<typename TypeHandler>
+ void MergeFromInnerLoop(
+ void** our_elems, void** other_elems, int length, int already_allocated);
+
+ // Internal helper: extend array space if necessary to contain |extend_amount|
+ // more elements, and return a pointer to the element immediately following
+ // the old list of elements. This interface factors out common behavior from
+ // Reserve() and MergeFrom() to reduce code size. |extend_amount| must be > 0.
+ void** InternalExtend(int extend_amount);
+
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase);
};
@@ -359,22 +536,98 @@ template <typename GenericType>
class GenericTypeHandler {
public:
typedef GenericType Type;
- static GenericType* New() { return new GenericType; }
- static void Delete(GenericType* value) { delete value; }
- static void Clear(GenericType* value) { value->Clear(); }
- static void Merge(const GenericType& from, GenericType* to) {
+ static inline GenericType* New(Arena* arena) {
+ return ::google::protobuf::Arena::CreateMaybeMessage<Type>(
+ arena, static_cast<GenericType*>(0));
+ }
+ // We force NewFromPrototype() and Delete() to be non-inline to reduce code
+ // size: else, several other methods get inlined copies of message types'
+ // constructors and destructors. Note that the GOOGLE_ATTRIBUTE_NOINLINE macro
+ // requires the 'inline' storage class here, which is somewhat confusing, but
+ // the compiler does the right thing.
+ static inline GenericType* NewFromPrototype(const GenericType* prototype,
+ ::google::protobuf::Arena* arena = NULL)
+ GOOGLE_ATTRIBUTE_NOINLINE {
+ return New(arena);
+ }
+ static inline void Delete(GenericType* value, Arena* arena)
+ GOOGLE_ATTRIBUTE_NOINLINE {
+ if (arena == NULL) {
+ delete value;
+ }
+ }
+ static inline ::google::protobuf::Arena* GetArena(GenericType* value) {
+ return ::google::protobuf::Arena::GetArena<Type>(value);
+ }
+ static inline void* GetMaybeArenaPointer(GenericType* value) {
+ return ::google::protobuf::Arena::GetArena<Type>(value);
+ }
+
+ static inline void Clear(GenericType* value) { value->Clear(); }
+ static inline void Merge(const GenericType& from, GenericType* to)
+ GOOGLE_ATTRIBUTE_NOINLINE {
to->MergeFrom(from);
}
- static int SpaceUsed(const GenericType& value) { return value.SpaceUsed(); }
- static const Type& default_instance() { return Type::default_instance(); }
+ static inline int SpaceUsed(const GenericType& value) {
+ return value.SpaceUsed();
+ }
+ static inline const Type& default_instance() {
+ return Type::default_instance();
+ }
};
+// Macros for specializing GenericTypeHandler for base proto types, these are
+// are defined here, to allow inlining them at their callsites.
+#define DEFINE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(Inline, TypeName) \
+ template<> \
+ Inline TypeName* GenericTypeHandler<TypeName>::NewFromPrototype( \
+ const TypeName* prototype, google::protobuf::Arena* arena) { \
+ return prototype->New(arena); \
+ } \
+ template<> \
+ Inline google::protobuf::Arena* GenericTypeHandler<TypeName>::GetArena( \
+ TypeName* value) { \
+ return value->GetArena(); \
+ } \
+ template<> \
+ Inline void* GenericTypeHandler<TypeName>::GetMaybeArenaPointer( \
+ TypeName* value) { \
+ return value->GetMaybeArenaPointer(); \
+ }
+#define DEFINE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES_NOINLINE(TypeName) \
+ DEFINE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(, TypeName)
+
+// Implements GenericTypeHandler specialization required by RepeatedPtrFields
+// to work with MessageLite type.
template <>
inline void GenericTypeHandler<MessageLite>::Merge(
const MessageLite& from, MessageLite* to) {
to->CheckTypeAndMergeFrom(from);
}
+DEFINE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(inline, MessageLite);
+
+// 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<> \
+ TypeName* GenericTypeHandler<TypeName>::NewFromPrototype( \
+ const TypeName* prototype, google::protobuf::Arena* arena); \
+ template<> \
+ google::protobuf::Arena* GenericTypeHandler<TypeName>::GetArena( \
+ TypeName* value); \
+ template<> \
+ void* GenericTypeHandler<TypeName>::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
+// Message.
+DECLARE_SPECIALIZATIONS_FOR_BASE_PROTO_TYPES(Message);
+
+
+#undef DECLARE_SPECIALIZATIONS_FOR_BASE_CLASSES
+
template <>
inline const MessageLite& GenericTypeHandler<MessageLite>::default_instance() {
// Yes, the behavior of the code is undefined, but this function is only
@@ -405,11 +658,28 @@ inline const Message& GenericTypeHandler<Message>::default_instance() {
class LIBPROTOBUF_EXPORT StringTypeHandlerBase {
public:
typedef string Type;
- static string* New();
- static void Delete(string* value);
- static void Clear(string* value) { value->clear(); }
- static void Merge(const string& from, string* to) { *to = from; }
- static const Type& default_instance() {
+
+ static inline string* New(Arena* arena) {
+ return Arena::Create<string>(arena);
+ }
+ static inline string* NewFromPrototype(const string*,
+ ::google::protobuf::Arena* arena) {
+ return New(arena);
+ }
+ static inline ::google::protobuf::Arena* GetArena(string*) {
+ return NULL;
+ }
+ static inline void* GetMaybeArenaPointer(string* value) {
+ return NULL;
+ }
+ static inline void Delete(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 inline const Type& default_instance() {
return ::google::protobuf::internal::GetEmptyString();
}
};
@@ -430,6 +700,8 @@ template <typename Element>
class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
public:
RepeatedPtrField();
+ explicit RepeatedPtrField(::google::protobuf::Arena* arena);
+
RepeatedPtrField(const RepeatedPtrField& other);
template <typename Iter>
RepeatedPtrField(Iter begin, const Iter& end);
@@ -469,9 +741,16 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
Element** mutable_data();
const Element* const* data() const;
- // Swap entire contents with "other".
+ // Swap entire contents with "other". If they are on separate arenas, then
+ // copies data.
void Swap(RepeatedPtrField* other);
+ // Swap entire contents with "other". Caller should guarantee that either both
+ // fields are on the same arena or both are on the heap. Swapping between
+ // different arenas with this function is disallowed and is caught via
+ // GOOGLE_DCHECK.
+ void UnsafeArenaSwap(RepeatedPtrField* other);
+
// Swap two elements.
void SwapElements(int index1, int index2);
@@ -488,8 +767,10 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
iterator begin();
const_iterator begin() const;
+ const_iterator cbegin() const;
iterator end();
const_iterator end() const;
+ const_iterator cend() const;
// Reverse iterator support
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
@@ -528,11 +809,36 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
// Add an already-allocated object, passing ownership to the
// RepeatedPtrField.
+ //
+ // Note that some special behavior occurs with respect to arenas:
+ //
+ // (i) if this field holds submessages, the new submessage will be copied if
+ // the original is in an arena and this RepeatedPtrField is either in a
+ // different arena, or on the heap.
+ // (ii) if this field holds strings, the passed-in string *must* be
+ // heap-allocated, not arena-allocated. There is no way to dynamically check
+ // this at runtime, so User Beware.
void AddAllocated(Element* value);
+
// Remove the last element and return it, passing ownership to the caller.
// Requires: size() > 0
+ //
+ // If this RepeatedPtrField is on an arena, an object copy is required to pass
+ // ownership back to the user (for compatible semantics). Use
+ // UnsafeArenaReleaseLast() if this behavior is undesired.
Element* ReleaseLast();
+ // Add an already-allocated object, skipping arena-ownership checks. The user
+ // must guarantee that the given object is in the same arena as this
+ // RepeatedPtrField.
+ void UnsafeArenaAddAllocated(Element* value);
+
+ // Remove the last element and return it. Works only when operating on an
+ // arena. The returned pointer is to the original object in the arena, hence
+ // has the arena's lifetime.
+ // Requires: current_size_ > 0
+ Element* UnsafeArenaReleaseLast();
+
// Extract elements with indices in the range "[start .. start+num-1]".
// The caller assumes ownership of the extracted elements and is responsible
// for deleting them when they are no longer needed.
@@ -542,8 +848,20 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
// to perform any further operations (like deletion) on these elements.
// Caution: implementation also moves elements with indices [start+num ..].
// Calling this routine inside a loop can cause quadratic behavior.
+ //
+ // Memory copying behavior is identical to ReleaseLast(), described above: if
+ // this RepeatedPtrField is on an arena, an object copy is performed for each
+ // returned element, so that all returned element pointers are to
+ // heap-allocated copies. If this copy is not desired, the user should call
+ // UnsafeArenaExtractSubrange().
void ExtractSubrange(int start, int num, Element** elements);
+ // Identical to ExtractSubrange() described above, except that when this
+ // repeated field is on an arena, no object copies are performed. Instead, the
+ // raw object pointers are returned. Thus, if on an arena, the returned
+ // objects must not be freed, because they will not be heap-allocated objects.
+ void UnsafeArenaExtractSubrange(int start, int num, Element** elements);
+
// When elements are removed by calls to RemoveLast() or Clear(), they
// are not actually freed. Instead, they are cleared and kept so that
// they can be reused later. This can save lots of CPU time when
@@ -558,12 +876,30 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
// Add an element to the pool of cleared objects, passing ownership to
// the RepeatedPtrField. The element must be cleared prior to calling
// this method.
+ //
+ // This method cannot be called when the repeated field is on an arena or when
+ // |value| is; both cases will trigger a GOOGLE_DCHECK-failure.
void AddCleared(Element* value);
// Remove a single element from the cleared pool and return it, passing
// ownership to the caller. The element is guaranteed to be cleared.
// Requires: ClearedCount() > 0
+ //
+ //
+ // This method cannot be called when the repeated field is on an arena; doing
+ // so will trigger a GOOGLE_DCHECK-failure.
Element* ReleaseCleared();
+ // Remove the element referenced by position.
+ iterator erase(const_iterator position);
+
+ // Removes the elements in the range [first, last).
+ iterator erase(const_iterator first, const_iterator last);
+
+ // Gets the arena on which this RepeatedPtrField stores its elements.
+ ::google::protobuf::Arena* GetArena() const {
+ return GetArenaNoVirtual();
+ }
+
protected:
// Note: RepeatedPtrField SHOULD NOT be subclassed by users. We only
// subclass it in one place as a hack for compatibility with proto1. The
@@ -571,31 +907,61 @@ class RepeatedPtrField : public internal::RepeatedPtrFieldBase {
// methods on RepeatedPtrFieldBase.
class TypeHandler;
+ // Internal arena accessor expected by helpers in Arena.
+ inline Arena* GetArenaNoVirtual() const;
+
+ private:
+ // Implementations for ExtractSubrange(). The copying behavior must be
+ // included only if the type supports the necessary operations (e.g.,
+ // MergeFrom()), so we must resolve this at compile time. ExtractSubrange()
+ // uses SFINAE to choose one of the below implementations.
+ void ExtractSubrangeInternal(int start, int num, Element** elements,
+ google::protobuf::internal::true_type);
+ void ExtractSubrangeInternal(int start, int num, Element** elements,
+ google::protobuf::internal::false_type);
+
+ friend class Arena;
+ typedef void InternalArenaConstructable_;
+
};
// implementation ====================================================
template <typename Element>
inline RepeatedField<Element>::RepeatedField()
- : elements_(NULL),
- current_size_(0),
- total_size_(kInitialSize) {
+ : current_size_(0),
+ total_size_(0),
+ rep_(NULL) {
+}
+
+template <typename Element>
+inline RepeatedField<Element>::RepeatedField(Arena* arena)
+ : current_size_(0),
+ total_size_(0),
+ rep_(NULL) {
+ // In case arena is NULL, then we do not create rep_, as code has an invariant
+ // `rep_ == NULL then arena == NULL`.
+ if (arena != NULL) {
+ rep_ = reinterpret_cast<Rep*>(
+ ::google::protobuf::Arena::CreateArray<char>(arena, kRepHeaderSize));
+ rep_->arena = arena;
+ }
}
template <typename Element>
inline RepeatedField<Element>::RepeatedField(const RepeatedField& other)
- : elements_(NULL),
- current_size_(0),
- total_size_(kInitialSize) {
+ : current_size_(0),
+ total_size_(0),
+ rep_(NULL) {
CopyFrom(other);
}
template <typename Element>
template <typename Iter>
-inline RepeatedField<Element>::RepeatedField(Iter begin, const Iter& end)
- : elements_(NULL),
- current_size_(0),
- total_size_(kInitialSize) {
+RepeatedField<Element>::RepeatedField(Iter begin, const Iter& end)
+ : current_size_(0),
+ total_size_(0),
+ rep_(NULL) {
int reserve = internal::CalculateReserve(begin, end);
if (reserve != -1) {
Reserve(reserve);
@@ -611,7 +977,20 @@ inline RepeatedField<Element>::RepeatedField(Iter begin, const Iter& end)
template <typename Element>
RepeatedField<Element>::~RepeatedField() {
- delete [] elements_;
+ // See explanation in Reserve(): we need to invoke destructors here for the
+ // case that Element has a non-trivial destructor. If Element has a trivial
+ // destructor (for example, if it's a primitive type, like int32), this entire
+ // loop will be removed by the optimizer.
+ if (rep_ != NULL) {
+ Element* e = &rep_->elements[0];
+ Element* limit = &rep_->elements[total_size_];
+ for (; e < limit; e++) {
+ e->Element::~Element();
+ }
+ if (rep_->arena == NULL) {
+ delete[] reinterpret_cast<char*>(rep_);
+ }
+ }
}
template <typename Element>
@@ -639,22 +1018,23 @@ inline int RepeatedField<Element>::Capacity() const {
template<typename Element>
inline void RepeatedField<Element>::AddAlreadyReserved(const Element& value) {
- GOOGLE_DCHECK_LT(size(), Capacity());
- elements_[current_size_++] = value;
+ GOOGLE_DCHECK_LT(current_size_, total_size_);
+ rep_->elements[current_size_++] = value;
}
template<typename Element>
inline Element* RepeatedField<Element>::AddAlreadyReserved() {
- GOOGLE_DCHECK_LT(size(), Capacity());
- return &elements_[current_size_++];
+ GOOGLE_DCHECK_LT(current_size_, total_size_);
+ return &rep_->elements[current_size_++];
}
template<typename Element>
inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
GOOGLE_DCHECK_GE(new_size, 0);
- if (new_size > size()) {
+ if (new_size > current_size_) {
Reserve(new_size);
- std::fill(&elements_[current_size_], &elements_[new_size], value);
+ std::fill(&rep_->elements[current_size_],
+ &rep_->elements[new_size], value);
}
current_size_ = new_size;
}
@@ -662,40 +1042,40 @@ inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
template <typename Element>
inline const Element& RepeatedField<Element>::Get(int index) const {
GOOGLE_DCHECK_GE(index, 0);
- GOOGLE_DCHECK_LT(index, size());
- return elements_[index];
+ GOOGLE_DCHECK_LT(index, current_size_);
+ return rep_->elements[index];
}
template <typename Element>
inline Element* RepeatedField<Element>::Mutable(int index) {
GOOGLE_DCHECK_GE(index, 0);
- GOOGLE_DCHECK_LT(index, size());
- return elements_ + index;
+ GOOGLE_DCHECK_LT(index, current_size_);
+ return &rep_->elements[index];
}
template <typename Element>
inline void RepeatedField<Element>::Set(int index, const Element& value) {
GOOGLE_DCHECK_GE(index, 0);
- GOOGLE_DCHECK_LT(index, size());
- elements_[index] = value;
+ GOOGLE_DCHECK_LT(index, current_size_);
+ rep_->elements[index] = value;
}
template <typename Element>
inline void RepeatedField<Element>::Add(const Element& value) {
if (current_size_ == total_size_) Reserve(total_size_ + 1);
- elements_[current_size_++] = value;
+ rep_->elements[current_size_++] = value;
}
template <typename Element>
inline Element* RepeatedField<Element>::Add() {
if (current_size_ == total_size_) Reserve(total_size_ + 1);
- return &elements_[current_size_++];
+ return &rep_->elements[current_size_++];
}
template <typename Element>
inline void RepeatedField<Element>::RemoveLast() {
GOOGLE_DCHECK_GT(current_size_, 0);
- --current_size_;
+ current_size_--;
}
template <typename Element>
@@ -703,7 +1083,7 @@ void RepeatedField<Element>::ExtractSubrange(
int start, int num, Element* elements) {
GOOGLE_DCHECK_GE(start, 0);
GOOGLE_DCHECK_GE(num, 0);
- GOOGLE_DCHECK_LE(start + num, this->size());
+ GOOGLE_DCHECK_LE(start + num, this->current_size_);
// Save the values of the removed elements if requested.
if (elements != NULL) {
@@ -713,9 +1093,9 @@ void RepeatedField<Element>::ExtractSubrange(
// Slide remaining elements down to fill the gap.
if (num > 0) {
- for (int i = start + num; i < this->size(); ++i)
+ for (int i = start + num; i < this->current_size_; ++i)
this->Set(i - num, this->Get(i));
- this->Truncate(this->size() - num);
+ this->Truncate(this->current_size_ - num);
}
}
@@ -729,7 +1109,8 @@ inline void RepeatedField<Element>::MergeFrom(const RepeatedField& other) {
GOOGLE_CHECK_NE(&other, this);
if (other.current_size_ != 0) {
Reserve(current_size_ + other.current_size_);
- CopyArray(elements_ + current_size_, other.elements_, other.current_size_);
+ CopyArray(rep_->elements + current_size_,
+ other.rep_->elements, other.current_size_);
current_size_ += other.current_size_;
}
}
@@ -742,62 +1123,98 @@ inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
}
template <typename Element>
+inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
+ const_iterator position) {
+ return erase(position, position + 1);
+}
+
+template <typename Element>
+inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
+ const_iterator first, const_iterator last) {
+ size_type first_offset = first - cbegin();
+ Truncate(std::copy(last, cend(), begin() + first_offset) - cbegin());
+ return begin() + first_offset;
+}
+
+template <typename Element>
inline Element* RepeatedField<Element>::mutable_data() {
- return elements_;
+ return rep_ ? rep_->elements : NULL;
}
template <typename Element>
inline const Element* RepeatedField<Element>::data() const {
- return elements_;
+ return rep_ ? rep_->elements : NULL;
}
template <typename Element>
+inline void RepeatedField<Element>::InternalSwap(RepeatedField* other) {
+ std::swap(rep_, other->rep_);
+ std::swap(current_size_, other->current_size_);
+ std::swap(total_size_, other->total_size_);
+}
+
+template <typename Element>
void RepeatedField<Element>::Swap(RepeatedField* other) {
if (this == other) return;
- Element* swap_elements = elements_;
- int swap_current_size = current_size_;
- int swap_total_size = total_size_;
-
- elements_ = other->elements_;
- current_size_ = other->current_size_;
- total_size_ = other->total_size_;
+ if (GetArenaNoVirtual() == other->GetArenaNoVirtual()) {
+ InternalSwap(other);
+ } else {
+ RepeatedField<Element> temp(other->GetArenaNoVirtual());
+ temp.MergeFrom(*this);
+ CopyFrom(*other);
+ other->UnsafeArenaSwap(&temp);
+ }
+}
- other->elements_ = swap_elements;
- other->current_size_ = swap_current_size;
- other->total_size_ = swap_total_size;
+template <typename Element>
+void RepeatedField<Element>::UnsafeArenaSwap(RepeatedField* other) {
+ if (this == other) return;
+ GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual());
+ InternalSwap(other);
}
template <typename Element>
void RepeatedField<Element>::SwapElements(int index1, int index2) {
using std::swap; // enable ADL with fallback
- swap(elements_[index1], elements_[index2]);
+ swap(rep_->elements[index1], rep_->elements[index2]);
}
template <typename Element>
inline typename RepeatedField<Element>::iterator
RepeatedField<Element>::begin() {
- return elements_;
+ return rep_ ? rep_->elements : NULL;
}
template <typename Element>
inline typename RepeatedField<Element>::const_iterator
RepeatedField<Element>::begin() const {
- return elements_;
+ return rep_ ? rep_->elements : NULL;
+}
+template <typename Element>
+inline typename RepeatedField<Element>::const_iterator
+RepeatedField<Element>::cbegin() const {
+ return rep_ ? rep_->elements : NULL;
}
template <typename Element>
inline typename RepeatedField<Element>::iterator
RepeatedField<Element>::end() {
- return elements_ + current_size_;
+ return rep_ ? rep_->elements + current_size_ : NULL;
}
template <typename Element>
inline typename RepeatedField<Element>::const_iterator
RepeatedField<Element>::end() const {
- return elements_ + current_size_;
+ return rep_ ? rep_->elements + current_size_ : NULL;
+}
+template <typename Element>
+inline typename RepeatedField<Element>::const_iterator
+RepeatedField<Element>::cend() const {
+ return rep_ ? rep_->elements + current_size_ : NULL;
}
template <typename Element>
inline int RepeatedField<Element>::SpaceUsedExcludingSelf() const {
- return (elements_ != NULL) ? total_size_ * sizeof(elements_[0]) : 0;
+ return rep_ ?
+ (total_size_ * sizeof(Element) + kRepHeaderSize) : 0;
}
// Avoid inlining of Reserve(): new, copy, and delete[] lead to a significant
@@ -805,32 +1222,66 @@ inline int RepeatedField<Element>::SpaceUsedExcludingSelf() const {
template <typename Element>
void RepeatedField<Element>::Reserve(int new_size) {
if (total_size_ >= new_size) return;
-
- Element* old_elements = elements_;
- total_size_ = max(google::protobuf::internal::kMinRepeatedFieldAllocationSize,
- max(total_size_ * 2, new_size));
- elements_ = new Element[total_size_];
- if (old_elements != NULL) {
- MoveArray(elements_, old_elements, current_size_);
- delete [] old_elements;
+ Rep* old_rep = rep_;
+ Arena* arena = GetArenaNoVirtual();
+ new_size = max(google::protobuf::internal::kMinRepeatedFieldAllocationSize,
+ max(total_size_ * 2, new_size));
+ if (arena == NULL) {
+ rep_ = reinterpret_cast<Rep*>(
+ new char[kRepHeaderSize + sizeof(Element)*new_size]);
+ } else {
+ rep_ = reinterpret_cast<Rep*>(
+ ::google::protobuf::Arena::CreateArray<char>(arena,
+ kRepHeaderSize + sizeof(Element)*new_size));
+ }
+ rep_->arena = arena;
+ total_size_ = new_size;
+ // Invoke placement-new on newly allocated elements. We shouldn't have to do
+ // this, since Element is supposed to be POD, but a previous version of this
+ // code allocated storage with "new Element[size]" and some code uses
+ // RepeatedField with non-POD types, relying on constructor invocation. If
+ // Element has a trivial constructor (e.g., int32), gcc (tested with -O2)
+ // completely removes this loop because the loop body is empty, so this has no
+ // effect unless its side-effects are required for correctness.
+ // Note that we do this before MoveArray() below because Element's copy
+ // assignment implementation will want an initialized instance first.
+ Element* e = &rep_->elements[0];
+ Element* limit = &rep_->elements[total_size_];
+ for (; e < limit; e++) {
+ new (e) Element();
+ }
+ if (current_size_ > 0) {
+ MoveArray(rep_->elements, old_rep->elements, current_size_);
+ }
+ // Likewise, we need to invoke destructors on the old array. If Element has no
+ // destructor, this loop will disappear.
+ e = &old_rep->elements[0];
+ limit = &old_rep->elements[current_size_];
+ for (; e < limit; e++) {
+ e->Element::~Element();
+ }
+ if (arena == NULL) {
+ delete[] reinterpret_cast<char*>(old_rep);
}
}
template <typename Element>
inline void RepeatedField<Element>::Truncate(int new_size) {
GOOGLE_DCHECK_LE(new_size, current_size_);
- current_size_ = new_size;
+ if (current_size_ > 0) {
+ current_size_ = new_size;
+ }
}
template <typename Element>
inline void RepeatedField<Element>::MoveArray(
- Element to[], Element from[], int array_size) {
+ Element* to, Element* from, int array_size) {
CopyArray(to, from, array_size);
}
template <typename Element>
inline void RepeatedField<Element>::CopyArray(
- Element to[], const Element from[], int array_size) {
+ Element* to, const Element* from, int array_size) {
internal::ElementCopier<Element>()(to, from, array_size);
}
@@ -838,13 +1289,13 @@ namespace internal {
template <typename Element, bool HasTrivialCopy>
void ElementCopier<Element, HasTrivialCopy>::operator()(
- Element to[], const Element from[], int array_size) {
+ Element* to, const Element* from, int array_size) {
std::copy(from, from + array_size, to);
}
template <typename Element>
struct ElementCopier<Element, true> {
- void operator()(Element to[], const Element from[], int array_size) {
+ void operator()(Element* to, const Element* from, int array_size) {
memcpy(to, from, array_size * sizeof(Element));
}
};
@@ -857,18 +1308,55 @@ struct ElementCopier<Element, true> {
namespace internal {
inline RepeatedPtrFieldBase::RepeatedPtrFieldBase()
- : elements_(NULL),
+ : arena_(NULL),
+ current_size_(0),
+ total_size_(0),
+ rep_(NULL) {
+}
+
+inline RepeatedPtrFieldBase::RepeatedPtrFieldBase(::google::protobuf::Arena* arena)
+ : arena_(arena),
current_size_(0),
- allocated_size_(0),
- total_size_(kInitialSize) {
+ total_size_(0),
+ rep_(NULL) {
}
template <typename TypeHandler>
void RepeatedPtrFieldBase::Destroy() {
- for (int i = 0; i < allocated_size_; i++) {
- TypeHandler::Delete(cast<TypeHandler>(elements_[i]));
+ if (rep_ != NULL) {
+ for (int i = 0; i < rep_->allocated_size; i++) {
+ TypeHandler::Delete(cast<TypeHandler>(rep_->elements[i]), arena_);
+ }
+ if (arena_ == NULL) {
+ delete [] reinterpret_cast<char*>(rep_);
+ }
}
- delete [] elements_;
+ rep_ = NULL;
+}
+
+template <typename TypeHandler>
+inline void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) {
+ if (other->GetArenaNoVirtual() == GetArenaNoVirtual()) {
+ InternalSwap(other);
+ } else {
+ SwapFallback<TypeHandler>(other);
+ }
+}
+
+template <typename TypeHandler>
+inline void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) {
+ GOOGLE_DCHECK(other->GetArenaNoVirtual() != GetArenaNoVirtual());
+
+ // Copy semantics in this case. We try to improve efficiency by placing the
+ // temporary on |other|'s arena so that messages are copied cross-arena only
+ // once, not twice.
+ RepeatedPtrFieldBase temp(other->GetArenaNoVirtual());
+ temp.MergeFrom<TypeHandler>(*this);
+ this->Clear<TypeHandler>();
+ this->MergeFrom<TypeHandler>(*other);
+ other->Clear<TypeHandler>();
+ other->InternalSwap(&temp);
+ temp.Destroy<TypeHandler>(); // Frees rep_ if `other` had no arena.
}
inline bool RepeatedPtrFieldBase::empty() const {
@@ -883,8 +1371,8 @@ template <typename TypeHandler>
inline const typename TypeHandler::Type&
RepeatedPtrFieldBase::Get(int index) const {
GOOGLE_DCHECK_GE(index, 0);
- GOOGLE_DCHECK_LT(index, size());
- return *cast<TypeHandler>(elements_[index]);
+ GOOGLE_DCHECK_LT(index, current_size_);
+ return *cast<TypeHandler>(rep_->elements[index]);
}
@@ -892,42 +1380,106 @@ template <typename TypeHandler>
inline typename TypeHandler::Type*
RepeatedPtrFieldBase::Mutable(int index) {
GOOGLE_DCHECK_GE(index, 0);
- GOOGLE_DCHECK_LT(index, size());
- return cast<TypeHandler>(elements_[index]);
+ GOOGLE_DCHECK_LT(index, current_size_);
+ return cast<TypeHandler>(rep_->elements[index]);
}
template <typename TypeHandler>
-inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add() {
- if (current_size_ < allocated_size_) {
- return cast<TypeHandler>(elements_[current_size_++]);
- }
- if (allocated_size_ == total_size_) Reserve(total_size_ + 1);
- typename TypeHandler::Type* result = TypeHandler::New();
- ++allocated_size_;
- elements_[current_size_++] = result;
+inline void RepeatedPtrFieldBase::Delete(int index) {
+ GOOGLE_DCHECK_GE(index, 0);
+ GOOGLE_DCHECK_LT(index, current_size_);
+ TypeHandler::Delete(cast<TypeHandler>(rep_->elements[index]), arena_);
+}
+
+template <typename TypeHandler>
+inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add(
+ typename TypeHandler::Type* prototype) {
+ if (rep_ != NULL && current_size_ < rep_->allocated_size) {
+ return cast<TypeHandler>(rep_->elements[current_size_++]);
+ }
+ if (!rep_ || rep_->allocated_size == total_size_) {
+ Reserve(total_size_ + 1);
+ }
+ ++rep_->allocated_size;
+ typename TypeHandler::Type* result =
+ TypeHandler::NewFromPrototype(prototype, arena_);
+ rep_->elements[current_size_++] = result;
return result;
}
template <typename TypeHandler>
inline void RepeatedPtrFieldBase::RemoveLast() {
GOOGLE_DCHECK_GT(current_size_, 0);
- TypeHandler::Clear(cast<TypeHandler>(elements_[--current_size_]));
+ TypeHandler::Clear(cast<TypeHandler>(rep_->elements[--current_size_]));
}
template <typename TypeHandler>
void RepeatedPtrFieldBase::Clear() {
- for (int i = 0; i < current_size_; i++) {
- TypeHandler::Clear(cast<TypeHandler>(elements_[i]));
+ const int n = current_size_;
+ GOOGLE_DCHECK_GE(n, 0);
+ if (n > 0) {
+ void* const* elements = raw_data();
+ int i = 0;
+ do {
+ TypeHandler::Clear(cast<TypeHandler>(elements[i++]));
+ } while (i < n);
+ current_size_ = 0;
}
- current_size_ = 0;
}
+// To avoid unnecessary code duplication and reduce binary size, we use a
+// layered approach to implementing MergeFrom(). The toplevel method is
+// templated, so we get a small thunk per concrete message type in the binary.
+// This calls a shared implementation with most of the logic, passing a function
+// pointer to another type-specific piece of code that calls the object-allocate
+// and merge handlers.
template <typename TypeHandler>
inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) {
- GOOGLE_CHECK_NE(&other, this);
- Reserve(current_size_ + other.current_size_);
- for (int i = 0; i < other.current_size_; i++) {
- TypeHandler::Merge(other.template Get<TypeHandler>(i), Add<TypeHandler>());
+ GOOGLE_DCHECK_NE(&other, this);
+ if (other.current_size_ == 0) return;
+ MergeFromInternal(
+ other, &RepeatedPtrFieldBase::MergeFromInnerLoop<TypeHandler>);
+}
+
+inline void RepeatedPtrFieldBase::MergeFromInternal(
+ const RepeatedPtrFieldBase& other,
+ void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int)) {
+ // Note: wrapper has already guaranteed that other.rep_ != NULL here.
+ int other_size = other.current_size_;
+ void** other_elements = other.rep_->elements;
+ void** new_elements = InternalExtend(other_size);
+ int allocated_elems = rep_->allocated_size - current_size_;
+ (this->*inner_loop)(new_elements, other_elements,
+ other_size, allocated_elems);
+ current_size_ += other_size;
+ if (rep_->allocated_size < current_size_) {
+ rep_->allocated_size = current_size_;
+ }
+}
+
+// Merges other_elems to our_elems.
+template<typename TypeHandler>
+void RepeatedPtrFieldBase::MergeFromInnerLoop(
+ void** our_elems, void** other_elems, int length, int already_allocated) {
+ // Split into two loops, over ranges [0, allocated) and [allocated, length),
+ // to avoid a branch within the loop.
+ for (int i = 0; i < already_allocated && i < length; i++) {
+ // Already allocated: use existing element.
+ typename TypeHandler::Type* other_elem =
+ reinterpret_cast<typename TypeHandler::Type*>(other_elems[i]);
+ typename TypeHandler::Type* new_elem =
+ reinterpret_cast<typename TypeHandler::Type*>(our_elems[i]);
+ TypeHandler::Merge(*other_elem, new_elem);
+ }
+ Arena* arena = GetArenaNoVirtual();
+ for (int i = already_allocated; i < length; i++) {
+ // Not allocated: alloc a new element first, then merge it.
+ typename TypeHandler::Type* other_elem =
+ reinterpret_cast<typename TypeHandler::Type*>(other_elems[i]);
+ typename TypeHandler::Type* new_elem =
+ TypeHandler::NewFromPrototype(other_elem, arena);
+ TypeHandler::Merge(*other_elem, new_elem);
+ our_elems[i] = new_elem;
}
}
@@ -943,18 +1495,18 @@ inline int RepeatedPtrFieldBase::Capacity() const {
}
inline void* const* RepeatedPtrFieldBase::raw_data() const {
- return elements_;
+ return rep_ ? rep_->elements : NULL;
}
inline void** RepeatedPtrFieldBase::raw_mutable_data() const {
- return elements_;
+ return rep_ ? const_cast<void**>(rep_->elements) : NULL;
}
template <typename TypeHandler>
inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() {
// TODO(kenton): Breaks C++ aliasing rules. We should probably remove this
// method entirely.
- return reinterpret_cast<typename TypeHandler::Type**>(elements_);
+ return reinterpret_cast<typename TypeHandler::Type**>(raw_mutable_data());
}
template <typename TypeHandler>
@@ -962,89 +1514,212 @@ 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<const typename TypeHandler::Type* const*>(elements_);
+ return reinterpret_cast<const typename TypeHandler::Type* const*>(raw_data());
}
inline void RepeatedPtrFieldBase::SwapElements(int index1, int index2) {
using std::swap; // enable ADL with fallback
- swap(elements_[index1], elements_[index2]);
+ swap(rep_->elements[index1], rep_->elements[index2]);
}
template <typename TypeHandler>
inline int RepeatedPtrFieldBase::SpaceUsedExcludingSelf() const {
- int allocated_bytes =
- (elements_ != NULL) ? total_size_ * sizeof(elements_[0]) : 0;
- for (int i = 0; i < allocated_size_; ++i) {
- allocated_bytes += TypeHandler::SpaceUsed(*cast<TypeHandler>(elements_[i]));
+ int allocated_bytes = total_size_ * sizeof(void*);
+ if (rep_ != NULL) {
+ for (int i = 0; i < rep_->allocated_size; ++i) {
+ allocated_bytes += TypeHandler::SpaceUsed(
+ *cast<TypeHandler>(rep_->elements[i]));
+ }
+ allocated_bytes += kRepHeaderSize;
}
return allocated_bytes;
}
template <typename TypeHandler>
inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() {
- if (current_size_ < allocated_size_) {
- return cast<TypeHandler>(elements_[current_size_++]);
+ if (rep_ != NULL && current_size_ < rep_->allocated_size) {
+ return cast<TypeHandler>(rep_->elements[current_size_++]);
} else {
return NULL;
}
}
+// AddAllocated version that implements arena-safe copying behavior.
+template <typename TypeHandler>
+void RepeatedPtrFieldBase::AddAllocatedInternal(
+ typename TypeHandler::Type* value,
+ google::protobuf::internal::true_type) {
+ Arena* element_arena = reinterpret_cast<Arena*>(
+ TypeHandler::GetMaybeArenaPointer(value));
+ Arena* arena = GetArenaNoVirtual();
+ if (arena == element_arena && rep_ &&
+ rep_->allocated_size < total_size_) {
+ // Fast path: underlying arena representation (tagged pointer) is equal to
+ // our arena pointer, and we can add to array without resizing it (at least
+ // one slot that is not allocated).
+ void** elems = rep_->elements;
+ if (current_size_ < rep_->allocated_size) {
+ // Make space at [current] by moving first allocated element to end of
+ // allocated list.
+ elems[rep_->allocated_size] = elems[current_size_];
+ }
+ elems[current_size_] = value;
+ current_size_ = current_size_ + 1;
+ rep_->allocated_size = rep_->allocated_size + 1;
+ return;
+ } else {
+ AddAllocatedSlowWithCopy<TypeHandler>(
+ value, TypeHandler::GetArena(value), arena);
+ }
+}
+
+// Slowpath handles all cases, copying if necessary.
+template<typename TypeHandler>
+void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy(
+ // Pass value_arena and my_arena to avoid duplicate virtual call (value) or
+ // load (mine).
+ typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena) {
+ // Ensure that either the value is in the same arena, or if not, we do the
+ // appropriate thing: Own() it (if it's on heap and we're in an arena) or copy
+ // it to our arena/heap (otherwise).
+ if (my_arena != NULL && value_arena == NULL) {
+ my_arena->Own(value);
+ } else if (my_arena != value_arena) {
+ typename TypeHandler::Type* new_value =
+ TypeHandler::NewFromPrototype(value, my_arena);
+ TypeHandler::Merge(*value, new_value);
+ TypeHandler::Delete(value, value_arena);
+ value = new_value;
+ }
+
+ UnsafeArenaAddAllocated<TypeHandler>(value);
+}
+
+// AddAllocated version that does not implement arena-safe copying behavior.
template <typename TypeHandler>
-void RepeatedPtrFieldBase::AddAllocated(
+void RepeatedPtrFieldBase::AddAllocatedInternal(
+ typename TypeHandler::Type* value,
+ google::protobuf::internal::false_type) {
+ if (rep_ && rep_->allocated_size < total_size_) {
+ // Fast path: underlying arena representation (tagged pointer) is equal to
+ // our arena pointer, and we can add to array without resizing it (at least
+ // one slot that is not allocated).
+ void** elems = rep_->elements;
+ if (current_size_ < rep_->allocated_size) {
+ // Make space at [current] by moving first allocated element to end of
+ // allocated list.
+ elems[rep_->allocated_size] = elems[current_size_];
+ }
+ elems[current_size_] = value;
+ current_size_ = current_size_ + 1;
+ ++rep_->allocated_size;
+ return;
+ } else {
+ UnsafeArenaAddAllocated<TypeHandler>(value);
+ }
+}
+
+template <typename TypeHandler>
+void RepeatedPtrFieldBase::UnsafeArenaAddAllocated(
typename TypeHandler::Type* value) {
// Make room for the new pointer.
- if (current_size_ == total_size_) {
+ if (!rep_ || current_size_ == total_size_) {
// The array is completely full with no cleared objects, so grow it.
Reserve(total_size_ + 1);
- ++allocated_size_;
- } else if (allocated_size_ == total_size_) {
+ ++rep_->allocated_size;
+ } else if (rep_->allocated_size == total_size_) {
// There is no more space in the pointer array because it contains some
// cleared objects awaiting reuse. We don't want to grow the array in this
// case because otherwise a loop calling AddAllocated() followed by Clear()
// would leak memory.
- TypeHandler::Delete(cast<TypeHandler>(elements_[current_size_]));
- } else if (current_size_ < allocated_size_) {
+ TypeHandler::Delete(
+ cast<TypeHandler>(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.
- elements_[allocated_size_] = elements_[current_size_];
- ++allocated_size_;
+ rep_->elements[rep_->allocated_size] = rep_->elements[current_size_];
+ ++rep_->allocated_size;
} else {
// There are no cleared objects.
- ++allocated_size_;
+ ++rep_->allocated_size;
}
- elements_[current_size_++] = value;
+ rep_->elements[current_size_++] = value;
}
+// ReleaseLast() for types that implement merge/copy behavior.
template <typename TypeHandler>
-inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLast() {
+inline typename TypeHandler::Type*
+RepeatedPtrFieldBase::ReleaseLastInternal(google::protobuf::internal::true_type) {
+ // First, release an element.
+ typename TypeHandler::Type* result = UnsafeArenaReleaseLast<TypeHandler>();
+ // Now perform a copy if we're on an arena.
+ Arena* arena = GetArenaNoVirtual();
+ if (arena == NULL) {
+ return result;
+ } else {
+ typename TypeHandler::Type* new_result =
+ TypeHandler::NewFromPrototype(result, NULL);
+ TypeHandler::Merge(*result, new_result);
+ return new_result;
+ }
+}
+
+// ReleaseLast() for types that *do not* implement merge/copy behavior -- this
+// is the same as UnsafeArenaReleaseLast(). Note that we GOOGLE_DCHECK-fail if we're on
+// an arena, since the user really should implement the copy operation in this
+// case.
+template <typename TypeHandler>
+inline typename TypeHandler::Type*
+RepeatedPtrFieldBase::ReleaseLastInternal(google::protobuf::internal::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; "
+ << "please implement MergeFrom for your type.";
+ return UnsafeArenaReleaseLast<TypeHandler>();
+}
+
+template <typename TypeHandler>
+inline typename TypeHandler::Type*
+ RepeatedPtrFieldBase::UnsafeArenaReleaseLast() {
GOOGLE_DCHECK_GT(current_size_, 0);
typename TypeHandler::Type* result =
- cast<TypeHandler>(elements_[--current_size_]);
- --allocated_size_;
- if (current_size_ < allocated_size_) {
+ cast<TypeHandler>(rep_->elements[--current_size_]);
+ --rep_->allocated_size;
+ if (current_size_ < rep_->allocated_size) {
// There are cleared elements on the end; replace the removed element
// with the last allocated element.
- elements_[current_size_] = elements_[allocated_size_];
+ rep_->elements[current_size_] = rep_->elements[rep_->allocated_size];
}
return result;
}
inline int RepeatedPtrFieldBase::ClearedCount() const {
- return allocated_size_ - current_size_;
+ return rep_ ? (rep_->allocated_size - current_size_) : 0;
}
template <typename TypeHandler>
inline void RepeatedPtrFieldBase::AddCleared(
typename TypeHandler::Type* value) {
- if (allocated_size_ == total_size_) Reserve(total_size_ + 1);
- elements_[allocated_size_++] = value;
+ GOOGLE_DCHECK(GetArenaNoVirtual() == NULL)
+ << "AddCleared() can only be used on a RepeatedPtrField not on an arena.";
+ GOOGLE_DCHECK(TypeHandler::GetArena(value) == NULL)
+ << "AddCleared() can only accept values not on an arena.";
+ if (!rep_ || rep_->allocated_size == total_size_) {
+ Reserve(total_size_ + 1);
+ }
+ rep_->elements[rep_->allocated_size++] = value;
}
template <typename TypeHandler>
inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() {
- GOOGLE_DCHECK_GT(allocated_size_, current_size_);
- return cast<TypeHandler>(elements_[--allocated_size_]);
+ GOOGLE_DCHECK(GetArenaNoVirtual() == NULL)
+ << "ReleaseCleared() can only be used on a RepeatedPtrField not on "
+ << "an arena.";
+ GOOGLE_DCHECK(GetArenaNoVirtual() == NULL);
+ GOOGLE_DCHECK(rep_ != NULL);
+ GOOGLE_DCHECK_GT(rep_->allocated_size, current_size_);
+ return cast<TypeHandler>(rep_->elements[--rep_->allocated_size]);
}
} // namespace internal
@@ -1063,12 +1738,17 @@ class RepeatedPtrField<string>::TypeHandler
template <typename Element>
-inline RepeatedPtrField<Element>::RepeatedPtrField() {}
+inline RepeatedPtrField<Element>::RepeatedPtrField()
+ : RepeatedPtrFieldBase() {}
+
+template <typename Element>
+inline RepeatedPtrField<Element>::RepeatedPtrField(::google::protobuf::Arena* arena) :
+ RepeatedPtrFieldBase(arena) {}
template <typename Element>
inline RepeatedPtrField<Element>::RepeatedPtrField(
const RepeatedPtrField& other)
- : RepeatedPtrFieldBase() {
+ : RepeatedPtrFieldBase() {
CopyFrom(other);
}
@@ -1134,14 +1814,71 @@ inline void RepeatedPtrField<Element>::DeleteSubrange(int start, int num) {
GOOGLE_DCHECK_GE(start, 0);
GOOGLE_DCHECK_GE(num, 0);
GOOGLE_DCHECK_LE(start + num, size());
- for (int i = 0; i < num; ++i)
- delete RepeatedPtrFieldBase::Mutable<TypeHandler>(start + i);
+ for (int i = 0; i < num; ++i) {
+ RepeatedPtrFieldBase::Delete<TypeHandler>(start + i);
+ }
ExtractSubrange(start, num, NULL);
}
template <typename Element>
inline void RepeatedPtrField<Element>::ExtractSubrange(
int start, int num, Element** elements) {
+ typename internal::TypeImplementsMergeBehavior<
+ typename TypeHandler::Type>::type t;
+ ExtractSubrangeInternal(start, num, elements, t);
+}
+
+// ExtractSubrange() implementation for types that implement merge/copy
+// behavior.
+template <typename Element>
+inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
+ int start, int num, Element** elements, google::protobuf::internal::true_type) {
+ GOOGLE_DCHECK_GE(start, 0);
+ GOOGLE_DCHECK_GE(num, 0);
+ GOOGLE_DCHECK_LE(start + num, size());
+
+ if (num > 0) {
+ // Save the values of the removed elements if requested.
+ if (elements != NULL) {
+ if (GetArenaNoVirtual() != NULL) {
+ // If we're on an arena, we perform a copy for each element so that the
+ // returned elements are heap-allocated.
+ for (int i = 0; i < num; ++i) {
+ Element* element = RepeatedPtrFieldBase::
+ Mutable<TypeHandler>(i + start);
+ typename TypeHandler::Type* new_value =
+ TypeHandler::NewFromPrototype(element, NULL);
+ TypeHandler::Merge(*element, new_value);
+ elements[i] = new_value;
+ }
+ } else {
+ for (int i = 0; i < num; ++i) {
+ elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
+ }
+ }
+ }
+ CloseGap(start, num);
+ }
+}
+
+// ExtractSubrange() implementation for types that do not implement merge/copy
+// behavior.
+template<typename Element>
+inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
+ int start, int num, Element** elements, google::protobuf::internal::false_type) {
+ // This case is identical to UnsafeArenaExtractSubrange(). However, since
+ // ExtractSubrange() must return heap-allocated objects by contract, and we
+ // cannot fulfill this contract if we are an on arena, we must GOOGLE_DCHECK() that
+ // we are not on an arena.
+ GOOGLE_DCHECK(GetArenaNoVirtual() == NULL)
+ << "ExtractSubrange() when arena is non-NULL is only supported when "
+ << "the Element type supplies a MergeFrom() operation to make copies.";
+ UnsafeArenaExtractSubrange(start, num, elements);
+}
+
+template <typename Element>
+inline void RepeatedPtrField<Element>::UnsafeArenaExtractSubrange(
+ int start, int num, Element** elements) {
GOOGLE_DCHECK_GE(start, 0);
GOOGLE_DCHECK_GE(num, 0);
GOOGLE_DCHECK_LE(start + num, size());
@@ -1149,8 +1886,9 @@ inline void RepeatedPtrField<Element>::ExtractSubrange(
if (num > 0) {
// Save the values of the removed elements if requested.
if (elements != NULL) {
- for (int i = 0; i < num; ++i)
+ for (int i = 0; i < num; ++i) {
elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
+ }
}
CloseGap(start, num);
}
@@ -1174,6 +1912,21 @@ inline void RepeatedPtrField<Element>::CopyFrom(
}
template <typename Element>
+inline typename RepeatedPtrField<Element>::iterator
+RepeatedPtrField<Element>::erase(const_iterator position) {
+ return erase(position, position + 1);
+}
+
+template <typename Element>
+inline typename RepeatedPtrField<Element>::iterator
+RepeatedPtrField<Element>::erase(const_iterator first, const_iterator last) {
+ size_type pos_offset = std::distance(cbegin(), first);
+ size_type last_offset = std::distance(cbegin(), last);
+ DeleteSubrange(pos_offset, last_offset - pos_offset);
+ return begin() + pos_offset;
+}
+
+template <typename Element>
inline Element** RepeatedPtrField<Element>::mutable_data() {
return RepeatedPtrFieldBase::mutable_data<TypeHandler>();
}
@@ -1184,16 +1937,32 @@ inline const Element* const* RepeatedPtrField<Element>::data() const {
}
template <typename Element>
-void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) {
- RepeatedPtrFieldBase::Swap(other);
+inline void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) {
+ if (this == other)
+ return;
+ RepeatedPtrFieldBase::Swap<TypeHandler>(other);
+}
+
+template <typename Element>
+inline void RepeatedPtrField<Element>::UnsafeArenaSwap(
+ RepeatedPtrField* other) {
+ GOOGLE_DCHECK(GetArenaNoVirtual() == other->GetArenaNoVirtual());
+ if (this == other)
+ return;
+ RepeatedPtrFieldBase::InternalSwap(other);
}
template <typename Element>
-void RepeatedPtrField<Element>::SwapElements(int index1, int index2) {
+inline void RepeatedPtrField<Element>::SwapElements(int index1, int index2) {
RepeatedPtrFieldBase::SwapElements(index1, index2);
}
template <typename Element>
+inline Arena* RepeatedPtrField<Element>::GetArenaNoVirtual() const {
+ return RepeatedPtrFieldBase::GetArenaNoVirtual();
+}
+
+template <typename Element>
inline int RepeatedPtrField<Element>::SpaceUsedExcludingSelf() const {
return RepeatedPtrFieldBase::SpaceUsedExcludingSelf<TypeHandler>();
}
@@ -1204,10 +1973,19 @@ inline void RepeatedPtrField<Element>::AddAllocated(Element* value) {
}
template <typename Element>
+inline void RepeatedPtrField<Element>::UnsafeArenaAddAllocated(Element* value) {
+ RepeatedPtrFieldBase::UnsafeArenaAddAllocated<TypeHandler>(value);
+}
+
+template <typename Element>
inline Element* RepeatedPtrField<Element>::ReleaseLast() {
return RepeatedPtrFieldBase::ReleaseLast<TypeHandler>();
}
+template <typename Element>
+inline Element* RepeatedPtrField<Element>::UnsafeArenaReleaseLast() {
+ return RepeatedPtrFieldBase::UnsafeArenaReleaseLast<TypeHandler>();
+}
template <typename Element>
inline int RepeatedPtrField<Element>::ClearedCount() const {
@@ -1307,11 +2085,11 @@ class RepeatedPtrIterator
it_ += d;
return *this;
}
- friend iterator operator+(iterator it, difference_type d) {
+ friend iterator operator+(iterator it, const difference_type d) {
it += d;
return it;
}
- friend iterator operator+(difference_type d, iterator it) {
+ friend iterator operator+(const difference_type d, iterator it) {
it += d;
return it;
}
@@ -1422,6 +2200,12 @@ class RepeatedPtrOverPtrsIterator
VoidPtr* it_;
};
+void RepeatedPtrFieldBase::InternalSwap(RepeatedPtrFieldBase* other) {
+ std::swap(rep_, other->rep_);
+ std::swap(current_size_, other->current_size_);
+ std::swap(total_size_, other->total_size_);
+}
+
} // namespace internal
template <typename Element>
@@ -1435,6 +2219,11 @@ RepeatedPtrField<Element>::begin() const {
return iterator(raw_data());
}
template <typename Element>
+inline typename RepeatedPtrField<Element>::const_iterator
+RepeatedPtrField<Element>::cbegin() const {
+ return begin();
+}
+template <typename Element>
inline typename RepeatedPtrField<Element>::iterator
RepeatedPtrField<Element>::end() {
return iterator(raw_data() + size());
@@ -1444,6 +2233,11 @@ inline typename RepeatedPtrField<Element>::const_iterator
RepeatedPtrField<Element>::end() const {
return iterator(raw_data() + size());
}
+template <typename Element>
+inline typename RepeatedPtrField<Element>::const_iterator
+RepeatedPtrField<Element>::cend() const {
+ return end();
+}
template <typename Element>
inline typename RepeatedPtrField<Element>::pointer_iterator
diff --git a/src/google/protobuf/repeated_field_reflection.h b/src/google/protobuf/repeated_field_reflection.h
new file mode 100644
index 00000000..44d14d5b
--- /dev/null
+++ b/src/google/protobuf/repeated_field_reflection.h
@@ -0,0 +1,337 @@
+// 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 header file is protobuf internal. Users should not include this
+// file directly.
+#ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__
+#define GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__
+
+#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
+
+#include <google/protobuf/generated_enum_reflection.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+// Interfaces used to implement reflection RepeatedFieldRef API.
+// Reflection::GetRepeatedAccessor() should return a pointer to an singleton
+// object that implements the below interface.
+//
+// This interface passes/returns values using void pointers. The actual type
+// of the value depends on the field's cpp_type. Following is a mapping from
+// cpp_type to the type that should be used in this interface:
+//
+// field->cpp_type() T Actual type of void*
+// CPPTYPE_INT32 int32 int32
+// CPPTYPE_UINT32 uint32 uint32
+// CPPTYPE_INT64 int64 int64
+// CPPTYPE_UINT64 uint64 uint64
+// CPPTYPE_DOUBLE double double
+// CPPTYPE_FLOAT float float
+// CPPTYPE_BOOL bool bool
+// CPPTYPE_ENUM generated enum type int32
+// CPPTYPE_STRING string string
+// CPPTYPE_MESSAGE generated message type google::protobuf::Message
+// or google::protobuf::Message
+//
+// Note that for enums we use int32 in the interface.
+//
+// You can map from T to the actual type using RefTypeTraits:
+// typedef RefTypeTraits<T>::AccessorValueType ActualType;
+class LIBPROTOBUF_EXPORT RepeatedFieldAccessor {
+ public:
+ // Typedefs for clarity.
+ typedef void Field;
+ typedef void Value;
+ typedef void Iterator;
+
+ virtual ~RepeatedFieldAccessor();
+ virtual bool IsEmpty(const Field* data) const = 0;
+ virtual int Size(const Field* data) const = 0;
+ // Depends on the underlying representation of the repeated field, this
+ // method can return a pointer to the underlying object if such an object
+ // 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 void Clear(Field* data) const = 0;
+ virtual void Set(Field* data, int index, const Value* value) const = 0;
+ virtual void Add(Field* data, const Value* value) const = 0;
+ virtual void RemoveLast(Field* data) const = 0;
+ virtual void SwapElements(Field* data, int index1, int index2) const = 0;
+ virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator,
+ Field* other_data) const = 0;
+
+ // Create an iterator that points at the begining of the repeated field.
+ virtual Iterator* BeginIterator(const Field* data) const = 0;
+ // Create an iterator that points at the end of the repeated field.
+ virtual Iterator* EndIterator(const Field* data) const = 0;
+ // Make a copy of an iterator and return the new copy.
+ virtual Iterator* CopyIterator(const Field* data,
+ const Iterator* iterator) const = 0;
+ // Move an iterator to point to the next element.
+ virtual Iterator* AdvanceIterator(const Field* data,
+ Iterator* iterator) const = 0;
+ // Compare whether two iterators point to the same element.
+ virtual bool EqualsIterator(const Field* data, const Iterator* a,
+ const Iterator* b) const = 0;
+ // Delete an iterator created by BeginIterator(), EndIterator() and
+ // CopyIterator().
+ virtual void DeleteIterator(const Field* data, Iterator* iterator) const = 0;
+ // Like Get() but for iterators.
+ virtual const Value* GetIteratorValue(const Field* data,
+ const Iterator* iterator,
+ Value* scratch_space) const = 0;
+
+ // Templated methods that make using this interface easier for non-message
+ // types.
+ template<typename T>
+ T Get(const Field* data, int index) const {
+ typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
+ ActualType scratch_space;
+ return static_cast<T>(
+ *reinterpret_cast<const ActualType*>(
+ Get(data, index, static_cast<Value*>(&scratch_space))));
+ }
+
+ template<typename T, typename ValueType>
+ void Set(Field* data, int index, const ValueType& value) const {
+ typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
+ // In this RepeatedFieldAccessor interface we pass/return data using
+ // raw pointers. Type of the data these raw pointers point to should
+ // be ActualType. Here we have a ValueType object and want a ActualType
+ // pointer. We can't cast a ValueType pointer to an ActualType pointer
+ // directly because their type might be different (for enums ValueType
+ // may be a generated enum type while ActualType is int32). To be safe
+ // we make a copy to get a temporary ActualType object and use it.
+ ActualType tmp = static_cast<ActualType>(value);
+ Set(data, index, static_cast<const Value*>(&tmp));
+ }
+
+ template<typename T, typename ValueType>
+ void Add(Field* data, const ValueType& value) const {
+ typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
+ // In this RepeatedFieldAccessor interface we pass/return data using
+ // raw pointers. Type of the data these raw pointers point to should
+ // be ActualType. Here we have a ValueType object and want a ActualType
+ // pointer. We can't cast a ValueType pointer to an ActualType pointer
+ // directly because their type might be different (for enums ValueType
+ // may be a generated enum type while ActualType is int32). To be safe
+ // we make a copy to get a temporary ActualType object and use it.
+ ActualType tmp = static_cast<ActualType>(value);
+ Add(data, static_cast<const Value*>(&tmp));
+ }
+};
+
+// Implement (Mutable)RepeatedFieldRef::iterator
+template<typename T>
+class RepeatedFieldRefIterator
+ : public std::iterator<std::forward_iterator_tag, T> {
+ typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType;
+ typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType;
+ typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType;
+
+ public:
+ // Constructor for non-message fields.
+ RepeatedFieldRefIterator(const void* data,
+ const RepeatedFieldAccessor* accessor,
+ bool begin)
+ : data_(data), accessor_(accessor),
+ iterator_(begin ? accessor->BeginIterator(data) :
+ accessor->EndIterator(data)),
+ scratch_space_(new AccessorValueType) {
+ }
+ // Constructor for message fields.
+ RepeatedFieldRefIterator(const void* data,
+ 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_);
+ }
+ RepeatedFieldRefIterator operator++(int) {
+ RepeatedFieldRefIterator tmp(*this);
+ iterator_ = accessor_->AdvanceIterator(data_, iterator_);
+ return tmp;
+ }
+ RepeatedFieldRefIterator& operator++() {
+ iterator_ = accessor_->AdvanceIterator(data_, iterator_);
+ return *this;
+ }
+ IteratorValueType operator*() const {
+ return static_cast<IteratorValueType>(
+ *static_cast<const AccessorValueType*>(
+ accessor_->GetIteratorValue(
+ data_, iterator_, scratch_space_.get())));
+ }
+ IteratorPointerType operator->() const {
+ return static_cast<IteratorPointerType>(
+ accessor_->GetIteratorValue(
+ data_, iterator_, scratch_space_.get()));
+ }
+ bool operator!=(const RepeatedFieldRefIterator& other) const {
+ assert(data_ == other.data_);
+ assert(accessor_ == other.accessor_);
+ return !accessor_->EqualsIterator(data_, iterator_, other.iterator_);
+ }
+ bool operator==(const RepeatedFieldRefIterator& other) const {
+ return !this->operator!=(other);
+ }
+
+ RepeatedFieldRefIterator(const RepeatedFieldRefIterator& other)
+ : data_(other.data_), accessor_(other.accessor_),
+ iterator_(accessor_->CopyIterator(data_, other.iterator_)) {
+ }
+ RepeatedFieldRefIterator& operator=(const RepeatedFieldRefIterator& other) {
+ if (this != &other) {
+ accessor_->DeleteIterator(data_, iterator_);
+ data_ = other.data_;
+ accessor_ = other.accessor_;
+ iterator_ = accessor_->CopyIterator(data_, other.iterator_);
+ }
+ return *this;
+ }
+
+ protected:
+ const void* data_;
+ const RepeatedFieldAccessor* accessor_;
+ void* iterator_;
+ google::protobuf::scoped_ptr<AccessorValueType> scratch_space_;
+};
+
+// TypeTraits that maps the type parameter T of RepeatedFieldRef or
+// MutableRepeatedFieldRef to corresponding iterator type,
+// RepeatedFieldAccessor type, etc.
+template<typename T>
+struct PrimitiveTraits {
+ static const bool is_primitive = false;
+};
+#define DEFINE_PRIMITIVE(TYPE, type) \
+ template<> struct PrimitiveTraits<type> { \
+ 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)
+DEFINE_PRIMITIVE(UINT64, uint64)
+DEFINE_PRIMITIVE(FLOAT, float)
+DEFINE_PRIMITIVE(DOUBLE, double)
+DEFINE_PRIMITIVE(BOOL, bool)
+#undef DEFINE_PRIMITIVE
+
+template<typename T>
+struct RefTypeTraits<
+ T, typename internal::enable_if<PrimitiveTraits<T>::is_primitive>::type> {
+ typedef RepeatedFieldRefIterator<T> iterator;
+ typedef RepeatedFieldAccessor AccessorType;
+ typedef T AccessorValueType;
+ typedef T IteratorValueType;
+ typedef T* IteratorPointerType;
+ static const FieldDescriptor::CppType cpp_type =
+ PrimitiveTraits<T>::cpp_type;
+ static const Descriptor* GetMessageFieldDescriptor() {
+ return NULL;
+ }
+};
+
+template<typename T>
+struct RefTypeTraits<
+ T, typename internal::enable_if<is_proto_enum<T>::value>::type> {
+ typedef RepeatedFieldRefIterator<T> iterator;
+ typedef RepeatedFieldAccessor AccessorType;
+ // We use int32 for repeated enums in RepeatedFieldAccessor.
+ typedef int32 AccessorValueType;
+ typedef T IteratorValueType;
+ typedef int32* IteratorPointerType;
+ static const FieldDescriptor::CppType cpp_type =
+ FieldDescriptor::CPPTYPE_ENUM;
+ static const Descriptor* GetMessageFieldDescriptor() {
+ return NULL;
+ }
+};
+
+template<typename T>
+struct RefTypeTraits<
+ T, typename internal::enable_if<internal::is_same<string, T>::value>::type> {
+ typedef RepeatedFieldRefIterator<T> iterator;
+ typedef RepeatedFieldAccessor AccessorType;
+ typedef string AccessorValueType;
+ typedef string IteratorValueType;
+ typedef string* IteratorPointerType;
+ static const FieldDescriptor::CppType cpp_type =
+ FieldDescriptor::CPPTYPE_STRING;
+ static const Descriptor* GetMessageFieldDescriptor() {
+ return NULL;
+ }
+};
+
+template<typename T>
+struct MessageDescriptorGetter {
+ static const Descriptor* get() {
+ return T::default_instance().GetDescriptor();
+ }
+};
+template<>
+struct MessageDescriptorGetter<Message> {
+ static const Descriptor* get() {
+ return NULL;
+ }
+};
+
+template<typename T>
+struct RefTypeTraits<
+ T, typename internal::enable_if<internal::is_base_of<Message, T>::value>::type> {
+ typedef RepeatedFieldRefIterator<T> iterator;
+ typedef RepeatedFieldAccessor AccessorType;
+ typedef Message AccessorValueType;
+ typedef const T& IteratorValueType;
+ typedef const T* IteratorPointerType;
+ static const FieldDescriptor::CppType cpp_type =
+ FieldDescriptor::CPPTYPE_MESSAGE;
+ static const Descriptor* GetMessageFieldDescriptor() {
+ return MessageDescriptorGetter<T>::get();
+ }
+};
+} // namespace internal
+} // namespace protobuf
+} // namespace google
+#endif // GOOGLE_PROTOBUF_REPEATED_FIELD_REFLECTION_H__
diff --git a/src/google/protobuf/repeated_field_reflection_unittest.cc b/src/google/protobuf/repeated_field_reflection_unittest.cc
index 62833aab..8b821806 100644
--- a/src/google/protobuf/repeated_field_reflection_unittest.cc
+++ b/src/google/protobuf/repeated_field_reflection_unittest.cc
@@ -33,10 +33,12 @@
// Test reflection methods for aggregate access to Repeated[Ptr]Fields.
// This test proto2 methods on a proto2 layout.
-#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/stringprintf.h>
+#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/unittest.pb.h>
#include <google/protobuf/test_util.h>
+#include <google/protobuf/reflection.h>
#include <gtest/gtest.h>
namespace google {
@@ -58,7 +60,6 @@ static string StrFunc(int i, int j) {
return str;
}
-
TEST(RepeatedFieldReflectionTest, RegularFields) {
TestAllTypes message;
const Reflection* refl = message.GetReflection();
@@ -113,7 +114,7 @@ TEST(RepeatedFieldReflectionTest, RegularFields) {
refl->MutableRepeatedPtrField<Message>(
&message, fd_repeated_foreign_message);
- // Make sure we can do get and sets through the Repeated[Ptr]Field objects.
+ // Make sure we can do gets and sets through the Repeated[Ptr]Field objects.
for (int i = 0; i < 10; ++i) {
// Check gets through const objects.
EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
@@ -158,8 +159,6 @@ TEST(RepeatedFieldReflectionTest, RegularFields) {
}
-
-
TEST(RepeatedFieldReflectionTest, ExtensionFields) {
TestAllExtensions extended_message;
const Reflection* refl = extended_message.GetReflection();
@@ -190,6 +189,516 @@ TEST(RepeatedFieldReflectionTest, ExtensionFields) {
}
}
+template<typename Ref, typename MessageType, typename ValueType>
+void TestRepeatedFieldRefIterator(
+ 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) {
+ EXPECT_EQ((message.*GetFunc)(index), *it);
+ ++index;
+ }
+ EXPECT_EQ(handle.size(), index);
+}
+
+TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) {
+ TestAllTypes message;
+ const Reflection* refl = message.GetReflection();
+ const Descriptor* desc = message.GetDescriptor();
+
+ for (int i = 0; i < 10; ++i) {
+ message.add_repeated_int32(Func(i, 1));
+ message.add_repeated_double(Func(i, 2));
+ message.add_repeated_string(StrFunc(i, 5));
+ message.add_repeated_foreign_message()->set_c(Func(i, 6));
+ }
+
+ // Get FieldDescriptors for all the fields of interest.
+ const FieldDescriptor* fd_repeated_int32 =
+ desc->FindFieldByName("repeated_int32");
+ const FieldDescriptor* fd_repeated_double =
+ desc->FindFieldByName("repeated_double");
+ const FieldDescriptor* fd_repeated_string =
+ desc->FindFieldByName("repeated_string");
+ const FieldDescriptor* fd_repeated_foreign_message =
+ desc->FindFieldByName("repeated_foreign_message");
+
+ // Get RepeatedFieldRef objects for all fields of interest.
+ const RepeatedFieldRef<int32> rf_int32 =
+ refl->GetRepeatedFieldRef<int32>(message, fd_repeated_int32);
+ const RepeatedFieldRef<double> rf_double =
+ refl->GetRepeatedFieldRef<double>(message, fd_repeated_double);
+ const RepeatedFieldRef<string> rf_string =
+ refl->GetRepeatedFieldRef<string>(message, fd_repeated_string);
+ const RepeatedFieldRef<ForeignMessage> rf_foreign_message =
+ refl->GetRepeatedFieldRef<ForeignMessage>(
+ message, fd_repeated_foreign_message);
+ const RepeatedFieldRef<Message> rf_message =
+ refl->GetRepeatedFieldRef<Message>(
+ message, fd_repeated_foreign_message);
+
+ // Get MutableRepeatedFieldRef objects for all fields of interest.
+ const MutableRepeatedFieldRef<int32> mrf_int32 =
+ refl->GetMutableRepeatedFieldRef<int32>(&message, fd_repeated_int32);
+ const MutableRepeatedFieldRef<double> mrf_double =
+ refl->GetMutableRepeatedFieldRef<double>(&message, fd_repeated_double);
+ const MutableRepeatedFieldRef<string> mrf_string =
+ refl->GetMutableRepeatedFieldRef<string>(&message, fd_repeated_string);
+ const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
+ refl->GetMutableRepeatedFieldRef<ForeignMessage>(
+ &message, fd_repeated_foreign_message);
+ const MutableRepeatedFieldRef<Message> mrf_message =
+ refl->GetMutableRepeatedFieldRef<Message>(
+ &message, fd_repeated_foreign_message);
+
+ EXPECT_EQ(message.repeated_int32_size(), rf_int32.size());
+ EXPECT_EQ(message.repeated_int32_size(), mrf_int32.size());
+ EXPECT_EQ(message.repeated_double_size(), rf_double.size());
+ 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(),
+ mrf_foreign_message.size());
+ EXPECT_EQ(message.repeated_foreign_message_size(), rf_message.size());
+ EXPECT_EQ(message.repeated_foreign_message_size(), mrf_message.size());
+
+ EXPECT_FALSE(rf_int32.empty());
+ EXPECT_FALSE(mrf_int32.empty());
+ EXPECT_FALSE(rf_double.empty());
+ EXPECT_FALSE(mrf_double.empty());
+ EXPECT_FALSE(rf_string.empty());
+ EXPECT_FALSE(mrf_string.empty());
+ EXPECT_FALSE(rf_foreign_message.empty());
+ EXPECT_FALSE(mrf_foreign_message.empty());
+ EXPECT_FALSE(rf_message.empty());
+ EXPECT_FALSE(mrf_message.empty());
+
+ // Make sure we can do gets and sets through the RepeatedFieldRef objects.
+ for (int i = 0; i < 10; ++i) {
+ // Check gets through const objects.
+ EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
+ EXPECT_EQ(rf_double.Get(i), Func(i, 2));
+ 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<const ForeignMessage&>(
+ 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<const ForeignMessage&>(
+ mrf_message.Get(i, &scratch_space)).c(), Func(i, 6));
+
+ // Check sets through mutable objects.
+ mrf_int32.Set(i, Func(i, -1));
+ mrf_double.Set(i, Func(i, -2));
+ mrf_string.Set(i, StrFunc(i, -5));
+ ForeignMessage foreign_message;
+ foreign_message.set_c(Func(i, -6));
+ mrf_foreign_message.Set(i, foreign_message);
+ EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
+ EXPECT_EQ(message.repeated_double(i), Func(i, -2));
+ EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
+ EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
+ foreign_message.set_c(Func(i, 7));
+ mrf_message.Set(i, foreign_message);
+ EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
+ }
+
+ // Test iterators.
+ TestRepeatedFieldRefIterator(rf_int32, message,
+ &TestAllTypes::repeated_int32);
+ TestRepeatedFieldRefIterator(rf_double, message,
+ &TestAllTypes::repeated_double);
+ TestRepeatedFieldRefIterator(rf_string, message,
+ &TestAllTypes::repeated_string);
+
+ // Test iterators for message fields.
+ typedef RepeatedFieldRef<ForeignMessage>::iterator MessageIterator;
+ int index = 0;
+ for (MessageIterator it = rf_foreign_message.begin();
+ it != rf_foreign_message.end(); ++it) {
+ EXPECT_EQ(message.repeated_foreign_message(index).c(), it->c());
+ ++index;
+ }
+ EXPECT_EQ(10, index);
+
+ // Test iterator operators that are not ususally used in regular for-loops.
+ // Including: post increment, assign, ==.
+ MessageIterator old_it = rf_foreign_message.begin();
+ MessageIterator new_it = old_it++;
+ EXPECT_FALSE(old_it == new_it);
+ // Check that old_it++ increments old_it once.
+ for (index = 1; old_it != rf_foreign_message.end(); ++old_it, ++index) {
+ EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
+ }
+ EXPECT_EQ(10, index);
+ // Test assign operator.
+ old_it = new_it;
+ for (index = 0; old_it != rf_foreign_message.end(); ++old_it, ++index) {
+ EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
+ }
+ EXPECT_EQ(10, index);
+ // Check that the returned value of old_it++ is the one before increment.
+ for (index = 0; new_it != rf_foreign_message.end(); ++new_it, ++index) {
+ EXPECT_EQ(message.repeated_foreign_message(index).c(), new_it->c());
+ }
+ EXPECT_EQ(10, index);
+
+ // Test MutableRepeatedFieldRef::Add()
+ mrf_int32.Add(1234);
+ mrf_double.Add(1234.0);
+ mrf_string.Add("1234");
+ ForeignMessage foreign_message;
+ foreign_message.set_c(1234);
+ mrf_foreign_message.Add(foreign_message);
+ EXPECT_EQ(1234, message.repeated_int32(10));
+ EXPECT_EQ(1234.0, message.repeated_double(10));
+ EXPECT_EQ("1234", message.repeated_string(10));
+ EXPECT_EQ(1234, message.repeated_foreign_message(10).c());
+
+ // Test MutableRepeatedFieldRef::RemoveLast()
+ mrf_int32.RemoveLast();
+ mrf_double.RemoveLast();
+ mrf_string.RemoveLast();
+ mrf_foreign_message.RemoveLast();
+ EXPECT_EQ(10, message.repeated_int32_size());
+ EXPECT_EQ(10, message.repeated_double_size());
+ EXPECT_EQ(10, message.repeated_string_size());
+ EXPECT_EQ(10, message.repeated_foreign_message_size());
+
+ // Test MutableRepeatedFieldRef::SwapElements()
+ mrf_int32.SwapElements(0, 9);
+ mrf_double.SwapElements(0, 9);
+ mrf_string.SwapElements(0, 9);
+ mrf_foreign_message.SwapElements(0, 9);
+ EXPECT_EQ(Func(9, -1), message.repeated_int32(0));
+ EXPECT_EQ(Func(0, -1), message.repeated_int32(9));
+ EXPECT_EQ(Func(9, -2), message.repeated_double(0));
+ EXPECT_EQ(Func(0, -2), message.repeated_double(9));
+ EXPECT_EQ(StrFunc(9, -5), message.repeated_string(0));
+ EXPECT_EQ(StrFunc(0, -5), message.repeated_string(9));
+ EXPECT_EQ(Func(9, 7), message.repeated_foreign_message(0).c());
+ EXPECT_EQ(Func(0, 7), message.repeated_foreign_message(9).c());
+
+ // Test MutableRepeatedFieldRef::Clear()
+ mrf_int32.Clear();
+ mrf_double.Clear();
+ mrf_string.Clear();
+ mrf_foreign_message.Clear();
+ EXPECT_EQ(0, message.repeated_int32_size());
+ EXPECT_EQ(0, message.repeated_double_size());
+ EXPECT_EQ(0, message.repeated_string_size());
+ EXPECT_EQ(0, message.repeated_foreign_message_size());
+
+ // Test (Mutable)RepeatedFieldRef::empty()
+ EXPECT_TRUE(rf_int32.empty());
+ EXPECT_TRUE(mrf_int32.empty());
+ EXPECT_TRUE(rf_double.empty());
+ EXPECT_TRUE(mrf_double.empty());
+ EXPECT_TRUE(rf_string.empty());
+ EXPECT_TRUE(mrf_string.empty());
+ EXPECT_TRUE(rf_foreign_message.empty());
+ EXPECT_TRUE(mrf_foreign_message.empty());
+ EXPECT_TRUE(rf_message.empty());
+ EXPECT_TRUE(mrf_message.empty());
+
+#ifdef PROTOBUF_HAS_DEATH_TEST
+ // Make sure types are checked correctly at runtime.
+ const FieldDescriptor* fd_optional_int32 =
+ desc->FindFieldByName("optional_int32");
+ EXPECT_DEATH(refl->GetRepeatedFieldRef<int32>(
+ message, fd_optional_int32), "");
+ EXPECT_DEATH(refl->GetRepeatedFieldRef<double>(
+ message, fd_repeated_int32), "");
+ EXPECT_DEATH(refl->GetRepeatedFieldRef<TestAllTypes>(
+ message, fd_repeated_foreign_message), "");
+#endif // PROTOBUF_HAS_DEATH_TEST
+}
+
+TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForEnums) {
+ TestAllTypes message;
+ const Reflection* refl = message.GetReflection();
+ const Descriptor* desc = message.GetDescriptor();
+
+ for (int i = 0; i < 10; ++i) {
+ message.add_repeated_nested_enum(TestAllTypes::BAR);
+ }
+
+ const FieldDescriptor* fd_repeated_nested_enum =
+ desc->FindFieldByName("repeated_nested_enum");
+ const RepeatedFieldRef<TestAllTypes::NestedEnum> enum_ref =
+ refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ message, fd_repeated_nested_enum);
+ const MutableRepeatedFieldRef<TestAllTypes::NestedEnum>
+ mutable_enum_ref =
+ refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ &message, fd_repeated_nested_enum);
+ const RepeatedFieldRef<int32> int32_ref =
+ refl->GetRepeatedFieldRef<int32>(
+ message, fd_repeated_nested_enum);
+ const MutableRepeatedFieldRef<int32> mutable_int32_ref =
+ refl->GetMutableRepeatedFieldRef<int32>(
+ &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());
+ EXPECT_EQ(message.repeated_nested_enum_size(), int32_ref.size());
+ EXPECT_EQ(message.repeated_nested_enum_size(), mutable_int32_ref.size());
+
+ EXPECT_FALSE(enum_ref.empty());
+ EXPECT_FALSE(mutable_enum_ref.empty());
+ EXPECT_FALSE(int32_ref.empty());
+ EXPECT_FALSE(mutable_int32_ref.empty());
+
+ for (int i = 0; i < 10; ++i) {
+ EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(i));
+ EXPECT_EQ(TestAllTypes::BAR, mutable_enum_ref.Get(i));
+ mutable_enum_ref.Set(i, TestAllTypes::BAZ);
+ EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(i));
+ EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
+
+ message.set_repeated_nested_enum(i, TestAllTypes::BAR);
+ EXPECT_EQ(TestAllTypes::BAR, int32_ref.Get(i));
+ EXPECT_EQ(TestAllTypes::BAR, mutable_int32_ref.Get(i));
+ mutable_int32_ref.Set(i, TestAllTypes::BAZ);
+ EXPECT_EQ(TestAllTypes::BAZ, int32_ref.Get(i));
+ EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
+ }
+
+ TestRepeatedFieldRefIterator(enum_ref, message,
+ &TestAllTypes::repeated_nested_enum);
+ TestRepeatedFieldRefIterator(int32_ref, message,
+ &TestAllTypes::repeated_nested_enum);
+
+ // Test Add()
+ mutable_enum_ref.Add(TestAllTypes::FOO);
+ EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(10));
+ mutable_int32_ref.Add(TestAllTypes::BAR);
+ EXPECT_EQ(TestAllTypes::BAR, message.repeated_nested_enum(11));
+
+ // Test RemoveLast()
+ mutable_enum_ref.RemoveLast();
+ EXPECT_EQ(11, message.repeated_nested_enum_size());
+ mutable_int32_ref.RemoveLast();
+ EXPECT_EQ(10, message.repeated_nested_enum_size());
+
+ // Test SwapElements()
+ mutable_enum_ref.Set(0, TestAllTypes::BAR);
+ mutable_enum_ref.Set(9, TestAllTypes::BAZ);
+ mutable_enum_ref.SwapElements(0, 9);
+ EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(0));
+ EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(9));
+ mutable_int32_ref.SwapElements(0, 9);
+ EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(0));
+ EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(9));
+
+ // Test Clear()
+ mutable_enum_ref.Clear();
+ EXPECT_EQ(0, message.repeated_nested_enum_size());
+ mutable_enum_ref.Add(TestAllTypes::FOO);
+ EXPECT_EQ(1, message.repeated_nested_enum_size());
+ mutable_int32_ref.Clear();
+ EXPECT_EQ(0, message.repeated_nested_enum_size());
+
+ // Test empty()
+ EXPECT_TRUE(enum_ref.empty());
+ EXPECT_TRUE(mutable_enum_ref.empty());
+ EXPECT_TRUE(int32_ref.empty());
+ EXPECT_TRUE(mutable_int32_ref.empty());
+}
+
+TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) {
+ TestAllExtensions extended_message;
+ const Reflection* refl = extended_message.GetReflection();
+ const Descriptor* desc = extended_message.GetDescriptor();
+
+ for (int i = 0; i < 10; ++i) {
+ extended_message.AddExtension(
+ unittest::repeated_int64_extension, Func(i, 1));
+ }
+
+ const FieldDescriptor* fd_repeated_int64_extension =
+ desc->file()->FindExtensionByName("repeated_int64_extension");
+ GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
+
+ const RepeatedFieldRef<int64> rf_int64_extension =
+ refl->GetRepeatedFieldRef<int64>(extended_message,
+ fd_repeated_int64_extension);
+
+ const MutableRepeatedFieldRef<int64> mrf_int64_extension =
+ refl->GetMutableRepeatedFieldRef<int64>(&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));
+ }
+}
+
+
+TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
+ // Set-up message content.
+ TestAllTypes m0, m1, m2;
+ for (int i = 0; i < 10; ++i) {
+ m0.add_repeated_int32(Func(i, 1));
+ m0.add_repeated_double(Func(i, 2));
+ m0.add_repeated_string(StrFunc(i, 5));
+ m0.add_repeated_foreign_message()->set_c(Func(i, 6));
+ m0.add_repeated_nested_enum(TestAllTypes::FOO);
+ m1.add_repeated_int32(Func(i, 11));
+ m1.add_repeated_double(Func(i, 12));
+ m1.add_repeated_string(StrFunc(i, 15));
+ m1.add_repeated_foreign_message()->set_c(Func(i, 16));
+ m1.add_repeated_nested_enum(TestAllTypes::BAR);
+ m2.add_repeated_int32(Func(i, 21));
+ m2.add_repeated_double(Func(i, 22));
+ m2.add_repeated_string(StrFunc(i, 25));
+ m2.add_repeated_foreign_message()->set_c(Func(i, 26));
+ m2.add_repeated_nested_enum(TestAllTypes::BAZ);
+ }
+
+ const Reflection* refl = m0.GetReflection();
+ const Descriptor* desc = m0.GetDescriptor();
+
+ // Get FieldDescriptors for all the fields of interest.
+ const FieldDescriptor* fd_repeated_int32 =
+ desc->FindFieldByName("repeated_int32");
+ const FieldDescriptor* fd_repeated_double =
+ desc->FindFieldByName("repeated_double");
+ const FieldDescriptor* fd_repeated_string =
+ desc->FindFieldByName("repeated_string");
+ const FieldDescriptor* fd_repeated_foreign_message =
+ desc->FindFieldByName("repeated_foreign_message");
+ const FieldDescriptor* fd_repeated_nested_enum =
+ desc->FindFieldByName("repeated_nested_enum");
+
+ // Get MutableRepeatedFieldRef objects for all fields of interest.
+ const MutableRepeatedFieldRef<int32> mrf_int32 =
+ refl->GetMutableRepeatedFieldRef<int32>(&m0, fd_repeated_int32);
+ const MutableRepeatedFieldRef<double> mrf_double =
+ refl->GetMutableRepeatedFieldRef<double>(&m0, fd_repeated_double);
+ const MutableRepeatedFieldRef<string> mrf_string =
+ refl->GetMutableRepeatedFieldRef<string>(&m0, fd_repeated_string);
+ const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
+ refl->GetMutableRepeatedFieldRef<ForeignMessage>(
+ &m0, fd_repeated_foreign_message);
+ const MutableRepeatedFieldRef<TestAllTypes::NestedEnum>
+ mrf_nested_enum =
+ refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ &m0, fd_repeated_nested_enum);
+
+ // Test MutableRepeatedRef::CopyFrom
+ mrf_int32.CopyFrom(
+ refl->GetRepeatedFieldRef<int32>(m1, fd_repeated_int32));
+ mrf_double.CopyFrom(
+ refl->GetRepeatedFieldRef<double>(m1, fd_repeated_double));
+ mrf_string.CopyFrom(
+ refl->GetRepeatedFieldRef<string>(m1, fd_repeated_string));
+ mrf_foreign_message.CopyFrom(
+ refl->GetRepeatedFieldRef<ForeignMessage>(
+ m1, fd_repeated_foreign_message));
+ mrf_nested_enum.CopyFrom(
+ refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ 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));
+ EXPECT_EQ(StrFunc(i, 15), m0.repeated_string(i));
+ EXPECT_EQ(Func(i, 16), m0.repeated_foreign_message(i).c());
+ EXPECT_EQ(TestAllTypes::BAR, m0.repeated_nested_enum(i));
+ }
+
+ // Test MutableRepeatedRef::MergeFrom
+ mrf_int32.MergeFrom(
+ refl->GetRepeatedFieldRef<int32>(m2, fd_repeated_int32));
+ mrf_double.MergeFrom(
+ refl->GetRepeatedFieldRef<double>(m2, fd_repeated_double));
+ mrf_string.MergeFrom(
+ refl->GetRepeatedFieldRef<string>(m2, fd_repeated_string));
+ mrf_foreign_message.MergeFrom(
+ refl->GetRepeatedFieldRef<ForeignMessage>(
+ m2, fd_repeated_foreign_message));
+ mrf_nested_enum.MergeFrom(
+ refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ 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));
+ EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i + 10));
+ EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i + 10).c());
+ EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i + 10));
+ }
+
+ // Test MutableRepeatedRef::Swap
+ // Swap between m0 and m2.
+ mrf_int32.Swap(
+ refl->GetMutableRepeatedFieldRef<int32>(&m2, fd_repeated_int32));
+ mrf_double.Swap(
+ refl->GetMutableRepeatedFieldRef<double>(&m2, fd_repeated_double));
+ mrf_string.Swap(
+ refl->GetMutableRepeatedFieldRef<string>(&m2, fd_repeated_string));
+ mrf_foreign_message.Swap(
+ refl->GetMutableRepeatedFieldRef<ForeignMessage>(
+ &m2, fd_repeated_foreign_message));
+ mrf_nested_enum.Swap(
+ refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
+ &m2, fd_repeated_nested_enum));
+ for (int i = 0; i < 10; ++i) {
+ // Check the content of m0.
+ EXPECT_EQ(Func(i, 21), m0.repeated_int32(i));
+ EXPECT_EQ(Func(i, 22), m0.repeated_double(i));
+ EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i));
+ EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i).c());
+ EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i));
+
+ // Check the content of m2.
+ EXPECT_EQ(Func(i, 11), m2.repeated_int32(i));
+ EXPECT_EQ(Func(i, 12), m2.repeated_double(i));
+ EXPECT_EQ(StrFunc(i, 15), m2.repeated_string(i));
+ EXPECT_EQ(Func(i, 16), m2.repeated_foreign_message(i).c());
+ EXPECT_EQ(TestAllTypes::BAR, m2.repeated_nested_enum(i));
+ EXPECT_EQ(Func(i, 21), m2.repeated_int32(i + 10));
+ EXPECT_EQ(Func(i, 22), m2.repeated_double(i + 10));
+ EXPECT_EQ(StrFunc(i, 25), m2.repeated_string(i + 10));
+ EXPECT_EQ(Func(i, 26), m2.repeated_foreign_message(i + 10).c());
+ EXPECT_EQ(TestAllTypes::BAZ, m2.repeated_nested_enum(i + 10));
+ }
+}
+
+// Test that GetRepeatedFieldRef/MutableRepeatedFieldRef works with
+// DynamicMessage.
+TEST(RepeatedFieldReflectionTest, RepeatedFieldRefDynamicMessage) {
+ // DynamicMessage shares the same memory layout as generated message
+ // and use the same GeneratedMessageReflection code for reflection.
+ // All code paths should already be covered by the other tests for
+ // generated messages. Here we just test one field.
+
+ const Descriptor* desc = TestAllTypes::descriptor();
+ const FieldDescriptor* fd_repeated_int32 =
+ desc->FindFieldByName("repeated_int32");
+
+ DynamicMessageFactory factory;
+ google::protobuf::scoped_ptr<Message> dynamic_message(factory.GetPrototype(desc)->New());
+ const Reflection* refl = dynamic_message->GetReflection();
+
+ MutableRepeatedFieldRef<int32> rf_int32 =
+ refl->GetMutableRepeatedFieldRef<int32>(
+ 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));
+}
+
} // namespace
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc
index f4d48f59..15c0c93e 100644
--- a/src/google/protobuf/repeated_field_unittest.cc
+++ b/src/google/protobuf/repeated_field_unittest.cc
@@ -92,7 +92,8 @@ TEST(RepeatedField, Small) {
EXPECT_TRUE(field.empty());
EXPECT_EQ(field.size(), 0);
- int expected_usage = 4 * sizeof(int);
+ // Additional bytes are for 'struct Rep' header.
+ int expected_usage = 4 * sizeof(int) + sizeof(Arena*);
EXPECT_EQ(field.SpaceUsedExcludingSelf(), expected_usage);
}
@@ -295,6 +296,39 @@ TEST(RepeatedField, CopyFromSelf) {
EXPECT_EQ(3, me.Get(0));
}
+TEST(RepeatedField, Erase) {
+ RepeatedField<int> me;
+ RepeatedField<int>::iterator it = me.erase(me.begin(), me.end());
+ EXPECT_TRUE(me.begin() == it);
+ EXPECT_EQ(0, me.size());
+
+ me.Add(1);
+ me.Add(2);
+ me.Add(3);
+ it = me.erase(me.begin(), me.end());
+ EXPECT_TRUE(me.begin() == it);
+ EXPECT_EQ(0, me.size());
+
+ me.Add(4);
+ me.Add(5);
+ me.Add(6);
+ it = me.erase(me.begin() + 2, me.end());
+ EXPECT_TRUE(me.begin() + 2 == it);
+ EXPECT_EQ(2, me.size());
+ EXPECT_EQ(4, me.Get(0));
+ EXPECT_EQ(5, me.Get(1));
+
+ me.Add(6);
+ me.Add(7);
+ me.Add(8);
+ it = me.erase(me.begin() + 1, me.begin() + 3);
+ EXPECT_TRUE(me.begin() + 1 == it);
+ EXPECT_EQ(3, me.size());
+ EXPECT_EQ(4, me.Get(0));
+ EXPECT_EQ(7, me.Get(1));
+ EXPECT_EQ(8, me.Get(2));
+}
+
TEST(RepeatedField, CopyConstruct) {
RepeatedField<int> source;
source.Add(1);
@@ -741,6 +775,39 @@ TEST(RepeatedPtrField, CopyFromSelf) {
EXPECT_EQ("1", me.Get(0));
}
+TEST(RepeatedPtrField, Erase) {
+ RepeatedPtrField<string> me;
+ RepeatedPtrField<string>::iterator it = me.erase(me.begin(), me.end());
+ EXPECT_TRUE(me.begin() == it);
+ EXPECT_EQ(0, me.size());
+
+ *me.Add() = "1";
+ *me.Add() = "2";
+ *me.Add() = "3";
+ it = me.erase(me.begin(), me.end());
+ EXPECT_TRUE(me.begin() == it);
+ EXPECT_EQ(0, me.size());
+
+ *me.Add() = "4";
+ *me.Add() = "5";
+ *me.Add() = "6";
+ it = me.erase(me.begin() + 2, me.end());
+ EXPECT_TRUE(me.begin() + 2 == it);
+ EXPECT_EQ(2, me.size());
+ EXPECT_EQ("4", me.Get(0));
+ EXPECT_EQ("5", me.Get(1));
+
+ *me.Add() = "6";
+ *me.Add() = "7";
+ *me.Add() = "8";
+ it = me.erase(me.begin() + 1, me.begin() + 3);
+ EXPECT_TRUE(me.begin() + 1 == it);
+ EXPECT_EQ(3, me.size());
+ EXPECT_EQ("4", me.Get(0));
+ EXPECT_EQ("7", me.Get(1));
+ EXPECT_EQ("8", me.Get(2));
+}
+
TEST(RepeatedPtrField, CopyConstruct) {
RepeatedPtrField<string> source;
source.Add()->assign("1");
diff --git a/src/google/protobuf/stubs/atomic_sequence_num.h b/src/google/protobuf/stubs/atomic_sequence_num.h
new file mode 100644
index 00000000..bb20942f
--- /dev/null
+++ b/src/google/protobuf/stubs/atomic_sequence_num.h
@@ -0,0 +1,54 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#ifndef GOOGLE_PROTOBUF_ATOMIC_SEQUENCE_NUM_H_
+#define GOOGLE_PROTOBUF_ATOMIC_SEQUENCE_NUM_H_
+
+#include <google/protobuf/stubs/atomicops.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+class SequenceNumber {
+ public:
+ SequenceNumber() : word_(0) {}
+
+ AtomicWord GetNext() {
+ return NoBarrier_AtomicIncrement(&word_, 1) - 1;
+ }
+ private:
+ AtomicWord word_;
+};
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_ATOMIC_SEQUENCE_NUM_H_
diff --git a/src/google/protobuf/stubs/atomicops.h b/src/google/protobuf/stubs/atomicops.h
index b1336e36..bbe2d845 100644
--- a/src/google/protobuf/stubs/atomicops.h
+++ b/src/google/protobuf/stubs/atomicops.h
@@ -62,6 +62,15 @@ namespace google {
namespace protobuf {
namespace internal {
+#if defined(GOOGLE_PROTOBUF_ARCH_POWER)
+#if defined(_LP64) || defined(__LP64__)
+typedef int32 Atomic32;
+typedef intptr_t Atomic64;
+#else
+typedef intptr_t Atomic32;
+typedef int64 Atomic64;
+#endif
+#else
typedef int32 Atomic32;
#ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
// We need to be able to go between Atomic64 and AtomicWord implicitly. This
@@ -75,6 +84,7 @@ typedef int64 Atomic64;
typedef intptr_t Atomic64;
#endif
#endif
+#endif
// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
// Atomic64 routines below, depending on your architecture.
@@ -179,6 +189,10 @@ GOOGLE_PROTOBUF_ATOMICOPS_ERROR
#elif defined(GOOGLE_PROTOBUF_OS_SOLARIS)
#include <google/protobuf/stubs/atomicops_internals_solaris.h>
+// AIX
+#elif defined(GOOGLE_PROTOBUF_OS_AIX)
+#include <google/protobuf/stubs/atomicops_internals_aix.h>
+
// Apple.
#elif defined(GOOGLE_PROTOBUF_OS_APPLE)
#include <google/protobuf/stubs/atomicops_internals_macosx.h>
diff --git a/src/google/protobuf/stubs/atomicops_internals_aix.h b/src/google/protobuf/stubs/atomicops_internals_aix.h
new file mode 100644
index 00000000..b8a42f21
--- /dev/null
+++ b/src/google/protobuf/stubs/atomicops_internals_aix.h
@@ -0,0 +1,440 @@
+// Copyright 2014 Bloomberg Finance LP. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Bloomberg Finance LP. 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 is an internal atomic implementation, use atomicops.h instead.
+
+#ifndef GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_AIX_H_
+#define GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_AIX_H_
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
+ Atomic32 old_value,
+ Atomic32 new_value) {
+ Atomic32 result;
+
+ asm volatile (
+ "1: lwarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpw %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+ " stwcx. %[val], %[zero], %[obj] \n\t" // store new value
+ " bne- 1b \n\t"
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr,
+ Atomic32 new_value) {
+ Atomic32 result;
+
+ asm volatile (
+ "1: lwarx %[res], %[zero], %[obj] \n\t"
+ " stwcx. %[val], %[zero], %[obj] \n\t"
+ " bne- 1b \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr,
+ Atomic32 increment) {
+ Atomic32 result;
+
+ asm volatile (
+ "1: lwarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " add %[res], %[val], %[res] \n\t" // add the operand
+ " stwcx. %[res], %[zero], %[obj] \n\t" // store old value
+ // if still reserved
+ " bne- 1b \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (increment),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline void MemoryBarrier(void) {
+ asm volatile (
+ " lwsync \n\t"
+ " isync \n\t"
+ :
+ :
+ : "memory");
+}
+
+inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
+ Atomic32 increment) {
+ Atomic32 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: lwarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " add %[res], %[val], %[res] \n\t" // add the operand
+ " stwcx. %[res], %[zero], %[obj] \n\t" // store old value
+ // if still reserved
+ " bne- 1b \n\t"
+ " isync \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (increment),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
+ Atomic32 old_value,
+ Atomic32 new_value) {
+ Atomic32 result;
+
+ asm volatile (
+ "1: lwarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpw %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+ " stwcx. %[val], %[zero], %[obj] \n\t" // store new value
+ " bne- 1b \n\t"
+
+ " isync \n\t"
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
+ Atomic32 old_value,
+ Atomic32 new_value) {
+ Atomic32 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: lwarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpw %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+ " stwcx. %[val], %[zero], %[obj] \n\t" // store new value
+ " bne- 1b \n\t"
+
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) {
+ *ptr = value;
+}
+
+inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) {
+ asm volatile (
+ " stw %[val], %[obj] \n\t"
+ " isync \n\t"
+ : [obj] "=m" (*ptr)
+ : [val] "b" (value));
+}
+
+inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) {
+ asm volatile (
+ " lwsync \n\t"
+ " stw %[val], %[obj] \n\t"
+ : [obj] "=m" (*ptr)
+ : [val] "b" (value));
+}
+
+inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) {
+ return *ptr;
+}
+
+inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) {
+ Atomic32 result;
+
+ asm volatile (
+ "1: lwz %[res], %[obj] \n\t"
+ " cmpw %[res], %[res] \n\t" // create data
+ // dependency for
+ // load/load ordering
+ " bne- 1b \n\t" // never taken
+
+ " isync \n\t"
+ : [res] "=b" (result)
+ : [obj] "m" (*ptr),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic32 Release_Load(volatile const Atomic32* ptr) {
+ Atomic32 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: lwz %[res], %[obj] \n\t"
+ " cmpw %[res], %[res] \n\t" // create data
+ // dependency for
+ // load/load ordering
+ " bne- 1b \n\t" // never taken
+ : [res] "=b" (result)
+ : [obj] "m" (*ptr),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+#ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
+inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
+ Atomic64 old_value,
+ Atomic64 new_value) {
+ Atomic64 result;
+
+ asm volatile (
+ "1: ldarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpd %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+
+ " stdcx. %[val], %[zero], %[obj] \n\t" // store the new value
+ " bne- 1b \n\t"
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr,
+ Atomic64 new_value) {
+ Atomic64 result;
+
+ asm volatile (
+ "1: ldarx %[res], %[zero], %[obj] \n\t"
+ " stdcx. %[val], %[zero], %[obj] \n\t"
+ " bne- 1b \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr,
+ Atomic64 increment) {
+ Atomic64 result;
+
+ asm volatile (
+ "1: ldarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " add %[res], %[res], %[val] \n\t" // add the operand
+ " stdcx. %[res], %[zero], %[obj] \n\t" // store old value if
+ // still reserved
+
+ " bne- 1b \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (increment),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr,
+ Atomic64 increment) {
+
+ Atomic64 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: ldarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " add %[res], %[res], %[val] \n\t" // add the operand
+ " stdcx. %[res], %[zero], %[obj] \n\t" // store old value if
+ // still reserved
+
+ " bne- 1b \n\t"
+
+ " isync \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [val] "b" (increment),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
+ Atomic64 old_value,
+ Atomic64 new_value) {
+ Atomic64 result;
+
+ asm volatile (
+ "1: ldarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpd %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+
+ " stdcx. %[val], %[zero], %[obj] \n\t" // store the new value
+ " bne- 1b \n\t"
+ " isync \n\t"
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
+ Atomic64 old_value,
+ Atomic64 new_value) {
+ Atomic64 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: ldarx %[res], %[zero], %[obj] \n\t" // load and reserve
+ " cmpd %[cmp], %[res] \n\t" // compare values
+ " bne- 2f \n\t"
+
+ " stdcx. %[val], %[zero], %[obj] \n\t" // store the new value
+ " bne- 1b \n\t"
+ "2: \n\t"
+ : [res] "=&b" (result)
+ : [obj] "b" (ptr),
+ [cmp] "b" (old_value),
+ [val] "b" (new_value),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) {
+ *ptr = value;
+}
+
+inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) {
+ asm volatile (
+ " std %[val], %[obj] \n\t"
+ " isync \n\t"
+ : [obj] "=m" (*ptr)
+ : [val] "b" (value));
+}
+
+inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) {
+ asm volatile (
+ " lwsync \n\t"
+ " std %[val], %[obj] \n\t"
+ : [obj] "=m" (*ptr)
+ : [val] "b" (value));
+}
+
+inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) {
+ return *ptr;
+}
+
+inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) {
+ Atomic64 result;
+
+ asm volatile (
+ "1: ld %[res], %[obj] \n\t"
+ " cmpd %[res], %[res] \n\t" // create data
+ // dependency for
+ // load/load ordering
+ " bne- 1b \n\t" // never taken
+
+ " isync \n\t"
+ : [res] "=b" (result)
+ : [obj] "m" (*ptr),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+
+inline Atomic64 Release_Load(volatile const Atomic64* ptr) {
+ Atomic64 result;
+
+ asm volatile (
+ " lwsync \n\t"
+
+ "1: ld %[res], %[obj] \n\t"
+ " cmpd %[res], %[res] \n\t" // create data
+ // dependency for
+ // load/load ordering
+ " bne- 1b \n\t" // never taken
+ : [res] "=b" (result)
+ : [obj] "m" (*ptr),
+ [zero] "i" (0)
+ : "cr0", "ctr");
+
+ return result;
+}
+#endif
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_ATOMICOPS_INTERNALS_SPARC_GCC_H_
diff --git a/src/google/protobuf/stubs/casts.h b/src/google/protobuf/stubs/casts.h
new file mode 100644
index 00000000..cccf65a1
--- /dev/null
+++ b/src/google/protobuf/stubs/casts.h
@@ -0,0 +1,123 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef GOOGLE_PROTOBUF_CASTS_H__
+#define GOOGLE_PROTOBUF_CASTS_H__
+
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/type_traits.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+// Use implicit_cast as a safe version of static_cast or const_cast
+// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
+// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
+// a const pointer to Foo).
+// When you use implicit_cast, the compiler checks that the cast is safe.
+// Such explicit implicit_casts are necessary in surprisingly many
+// situations where C++ demands an exact type match instead of an
+// argument type convertable to a target type.
+//
+// The From type can be inferred, so the preferred syntax for using
+// implicit_cast is the same as for static_cast etc.:
+//
+// implicit_cast<ToType>(expr)
+//
+// implicit_cast would have been part of the C++ standard library,
+// but the proposal was submitted too late. It will probably make
+// its way into the language in the future.
+template<typename To, typename From>
+inline To implicit_cast(From const &f) {
+ return f;
+}
+
+// When you upcast (that is, cast a pointer from type Foo to type
+// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
+// always succeed. When you downcast (that is, cast a pointer from
+// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
+// how do you know the pointer is really of type SubclassOfFoo? It
+// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
+// when you downcast, you should use this macro. In debug mode, we
+// use dynamic_cast<> to double-check the downcast is legal (we die
+// if it's not). In normal mode, we do the efficient static_cast<>
+// instead. Thus, it's important to test in debug mode to make sure
+// the cast is legal!
+// This is the only place in the code we should use dynamic_cast<>.
+// In particular, you SHOULDN'T be using dynamic_cast<> in order to
+// do RTTI (eg code like this:
+// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
+// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
+// You should design the code some other way not to need this.
+
+template<typename To, typename From> // use like this: down_cast<T*>(foo);
+inline To down_cast(From* f) { // so we only accept pointers
+ // Ensures that To is a sub-type of From *. This test is here only
+ // for compile-time type checking, and has no overhead in an
+ // optimized build at run-time, as it will be optimized away
+ // completely.
+ if (false) {
+ implicit_cast<From*, To>(0);
+ }
+
+#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
+ assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
+#endif
+ return static_cast<To>(f);
+}
+
+template<typename To, typename From> // use like this: down_cast<T&>(foo);
+inline To down_cast(From& f) {
+ typedef typename remove_reference<To>::type* ToAsPointer;
+ // Ensures that To is a sub-type of From *. This test is here only
+ // for compile-time type checking, and has no overhead in an
+ // optimized build at run-time, as it will be optimized away
+ // completely.
+ if (false) {
+ implicit_cast<From*, ToAsPointer>(0);
+ }
+
+#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
+ // RTTI: debug mode only!
+ assert(dynamic_cast<ToAsPointer>(&f) != NULL);
+#endif
+ return *static_cast<ToAsPointer>(&f);
+}
+
+} // namespace internal
+
+// We made these internal so that they would show up as such in the docs,
+// but we don't want to stick "internal::" in front of them everywhere.
+using internal::implicit_cast;
+using internal::down_cast;
+
+} // namespace protobuf
+} // namespace google
+#endif // GOOGLE_PROTOBUF_CASTS_H__
diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc
index 1e02b228..54e00ccb 100644
--- a/src/google/protobuf/stubs/common.cc
+++ b/src/google/protobuf/stubs/common.cc
@@ -171,6 +171,7 @@ DECLARE_STREAM_OPERATOR(uint , "%u" )
DECLARE_STREAM_OPERATOR(long , "%ld")
DECLARE_STREAM_OPERATOR(unsigned long, "%lu")
DECLARE_STREAM_OPERATOR(double , "%g" )
+DECLARE_STREAM_OPERATOR(void* , "%p" )
#undef DECLARE_STREAM_OPERATOR
LogMessage::LogMessage(LogLevel level, const char* filename, int line)
diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h
index 79ced81f..c3f735a2 100644
--- a/src/google/protobuf/stubs/common.h
+++ b/src/google/protobuf/stubs/common.h
@@ -113,24 +113,24 @@ 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 2006000
+#define GOOGLE_PROTOBUF_VERSION 3000000
// The minimum library version which works with the current version of the
// headers.
-#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2006000
+#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3000000
// 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 = 2006000;
+static const int kMinHeaderVersionForLibrary = 3000000;
// The minimum protoc version which works with the current version of the
// headers.
-#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2006000
+#define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 3000000
// The minimum header version which works with the current version of
// protoc. This constant should only be used in VerifyVersion().
-static const int kMinHeaderVersionForProtoc = 2006000;
+static const int kMinHeaderVersionForProtoc = 3000000;
// Verifies that the headers and libraries are compatible. Use the macro
// below to call this.
@@ -218,6 +218,17 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
#endif
#endif
+#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))
+#else
+// Other compilers will have to figure it out for themselves.
+#define GOOGLE_ATTRIBUTE_NOINLINE
+#endif
+#endif
+
#ifndef GOOGLE_ATTRIBUTE_DEPRECATED
#ifdef __GNUC__
// If the method/variable/type is used anywhere, produce a warning.
@@ -236,6 +247,15 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
#endif
#endif
+#ifndef GOOGLE_PREDICT_FALSE
+#ifdef __GNUC__
+// Provided at least since GCC 3.0.
+#define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(!!(x), 1))
+#else
+#define GOOGLE_PREDICT_FALSE
+#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).
@@ -246,6 +266,60 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
#define GOOGLE_SAFE_CONCURRENT_WRITES_END()
#endif
+#define GOOGLE_GUARDED_BY(x)
+#define GOOGLE_FALLTHROUGH_INTENDED
+#define GOOGLE_ATTRIBUTE_COLD
+
+// x86 and x86-64 can perform unaligned loads/stores directly.
+#if defined(_M_X64) || defined(__x86_64__) || \
+ defined(_M_IX86) || defined(__i386__)
+
+#define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
+#define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
+#define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
+
+#define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
+#define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
+#define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
+
+#else
+inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
+ uint16 t;
+ memcpy(&t, p, sizeof t);
+ return t;
+}
+
+inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
+ uint32 t;
+ memcpy(&t, p, sizeof t);
+ return t;
+}
+
+inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
+ uint64 t;
+ memcpy(&t, p, sizeof t);
+ return t;
+}
+
+inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
+ memcpy(p, &v, sizeof v);
+}
+
+inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
+ memcpy(p, &v, sizeof v);
+}
+
+inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
+ memcpy(p, &v, sizeof v);
+}
+#endif
+
+#if defined(_MSC_VER)
+#define GOOGLE_THREAD_LOCAL __declspec(thread)
+#else
+#define GOOGLE_THREAD_LOCAL __thread
+#endif
+
// ===================================================================
// from google3/base/basictypes.h
@@ -288,71 +362,6 @@ static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
-namespace internal {
-
-// Use implicit_cast as a safe version of static_cast or const_cast
-// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
-// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
-// a const pointer to Foo).
-// When you use implicit_cast, the compiler checks that the cast is safe.
-// Such explicit implicit_casts are necessary in surprisingly many
-// situations where C++ demands an exact type match instead of an
-// argument type convertable to a target type.
-//
-// The From type can be inferred, so the preferred syntax for using
-// implicit_cast is the same as for static_cast etc.:
-//
-// implicit_cast<ToType>(expr)
-//
-// implicit_cast would have been part of the C++ standard library,
-// but the proposal was submitted too late. It will probably make
-// its way into the language in the future.
-template<typename To, typename From>
-inline To implicit_cast(From const &f) {
- return f;
-}
-
-// When you upcast (that is, cast a pointer from type Foo to type
-// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
-// always succeed. When you downcast (that is, cast a pointer from
-// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
-// how do you know the pointer is really of type SubclassOfFoo? It
-// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
-// when you downcast, you should use this macro. In debug mode, we
-// use dynamic_cast<> to double-check the downcast is legal (we die
-// if it's not). In normal mode, we do the efficient static_cast<>
-// instead. Thus, it's important to test in debug mode to make sure
-// the cast is legal!
-// This is the only place in the code we should use dynamic_cast<>.
-// In particular, you SHOULDN'T be using dynamic_cast<> in order to
-// do RTTI (eg code like this:
-// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
-// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
-// You should design the code some other way not to need this.
-
-template<typename To, typename From> // use like this: down_cast<T*>(foo);
-inline To down_cast(From* f) { // so we only accept pointers
- // Ensures that To is a sub-type of From *. This test is here only
- // for compile-time type checking, and has no overhead in an
- // optimized build at run-time, as it will be optimized away
- // completely.
- if (false) {
- implicit_cast<From*, To>(0);
- }
-
-#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
- assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
-#endif
- return static_cast<To>(f);
-}
-
-} // namespace internal
-
-// We made these internal so that they would show up as such in the docs,
-// but we don't want to stick "internal::" in front of them everywhere.
-using internal::implicit_cast;
-using internal::down_cast;
-
// The COMPILE_ASSERT macro can be used to verify that a compile time
// expression is true. For example, you could use it to verify the
// size of a static array:
@@ -378,8 +387,9 @@ struct CompileAssert {
#undef GOOGLE_COMPILE_ASSERT
#define GOOGLE_COMPILE_ASSERT(expr, msg) \
- typedef ::google::protobuf::internal::CompileAssert<(bool(expr))> \
- msg[bool(expr) ? 1 : -1]
+ ::google::protobuf::internal::CompileAssert<(bool(expr))> \
+ msg[bool(expr) ? 1 : -1]; \
+ (void)msg
// Implementation details of COMPILE_ASSERT:
@@ -449,7 +459,7 @@ class scoped_ptr {
// The element type
typedef C element_type;
- // Constructor. Defaults to intializing with NULL.
+ // Constructor. Defaults to initializing with NULL.
// There is no way to create an uninitialized scoped_ptr.
// The input parameter must be allocated with new.
explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
@@ -536,7 +546,7 @@ class scoped_array {
// The element type
typedef C element_type;
- // Constructor. Defaults to intializing with NULL.
+ // Constructor. Defaults to initializing with NULL.
// There is no way to create an uninitialized scoped_array.
// The input parameter must be allocated with new [].
explicit scoped_array(C* p = NULL) : array_(p) { }
@@ -657,6 +667,7 @@ class LIBPROTOBUF_EXPORT LogMessage {
LogMessage& operator<<(long value);
LogMessage& operator<<(unsigned long value);
LogMessage& operator<<(double value);
+ LogMessage& operator<<(void* value);
private:
friend class LogFinisher;
@@ -731,7 +742,8 @@ T* CheckNotNull(const char* /* file */, int /* line */,
}
} // namespace internal
#define GOOGLE_CHECK_NOTNULL(A) \
- internal::CheckNotNull(__FILE__, __LINE__, "'" #A "' must not be NULL", (A))
+ ::google::protobuf::internal::CheckNotNull(\
+ __FILE__, __LINE__, "'" #A "' must not be NULL", (A))
#ifdef NDEBUG
diff --git a/src/google/protobuf/stubs/common_unittest.cc b/src/google/protobuf/stubs/common_unittest.cc
index cb892072..5f458e98 100644
--- a/src/google/protobuf/stubs/common_unittest.cc
+++ b/src/google/protobuf/stubs/common_unittest.cc
@@ -31,6 +31,7 @@
// Author: kenton@google.com (Kenton Varda)
#include <vector>
+#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
@@ -94,9 +95,9 @@ TEST(LoggingTest, DefaultLogging) {
string text = GetCapturedTestStderr();
EXPECT_EQ(
- "[libprotobuf INFO "__FILE__":" + SimpleItoa(line + 1) + "] A message.\n"
- "[libprotobuf WARNING "__FILE__":" + SimpleItoa(line + 2) + "] A warning.\n"
- "[libprotobuf ERROR "__FILE__":" + SimpleItoa(line + 3) + "] An error.\n",
+ "[libprotobuf INFO " __FILE__ ":" + SimpleItoa(line + 1) + "] A message.\n"
+ "[libprotobuf WARNING " __FILE__ ":" + SimpleItoa(line + 2) + "] A warning.\n"
+ "[libprotobuf ERROR " __FILE__ ":" + SimpleItoa(line + 3) + "] An error.\n",
text);
}
@@ -127,10 +128,10 @@ TEST(LoggingTest, CaptureLogging) {
ASSERT_EQ(2, captured_messages_.size());
EXPECT_EQ(
- "2 "__FILE__":" + SimpleItoa(start_line + 1) + ": An error.",
+ "2 " __FILE__ ":" + SimpleItoa(start_line + 1) + ": An error.",
captured_messages_[0]);
EXPECT_EQ(
- "1 "__FILE__":" + SimpleItoa(start_line + 2) + ": A warning.",
+ "1 " __FILE__ ":" + SimpleItoa(start_line + 2) + ": A warning.",
captured_messages_[1]);
}
@@ -153,10 +154,10 @@ TEST(LoggingTest, SilenceLogging) {
ASSERT_EQ(2, captured_messages_.size());
EXPECT_EQ(
- "0 "__FILE__":" + SimpleItoa(line1) + ": Visible1",
+ "0 " __FILE__ ":" + SimpleItoa(line1) + ": Visible1",
captured_messages_[0]);
EXPECT_EQ(
- "0 "__FILE__":" + SimpleItoa(line2) + ": Visible2",
+ "0 " __FILE__ ":" + SimpleItoa(line2) + ": Visible2",
captured_messages_[1]);
}
diff --git a/src/google/protobuf/stubs/fastmem.h b/src/google/protobuf/stubs/fastmem.h
new file mode 100644
index 00000000..763a6e60
--- /dev/null
+++ b/src/google/protobuf/stubs/fastmem.h
@@ -0,0 +1,152 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Fast memory copying and comparison routines.
+// strings::fastmemcmp_inlined() replaces memcmp()
+// strings::memcpy_inlined() replaces memcpy()
+// strings::memeq(a, b, n) replaces memcmp(a, b, n) == 0
+//
+// strings::*_inlined() routines are inline versions of the
+// routines exported by this module. Sometimes using the inlined
+// versions is faster. Measure before using the inlined versions.
+//
+// Performance measurement:
+// strings::fastmemcmp_inlined
+// Analysis: memcmp, fastmemcmp_inlined, fastmemcmp
+// 2012-01-30
+
+#ifndef GOOGLE_PROTOBUF_STUBS_FASTMEM_H_
+#define GOOGLE_PROTOBUF_STUBS_FASTMEM_H_
+
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <google/protobuf/stubs/common.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+
+// Return true if the n bytes at a equal the n bytes at b.
+// The regions are allowed to overlap.
+//
+// The performance is similar to the performance memcmp(), but faster for
+// moderately-sized inputs, or inputs that share a common prefix and differ
+// somewhere in their last 8 bytes. Further optimizations can be added later
+// 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<size_t>(7);
+ if (GOOGLE_PREDICT_FALSE(n_rounded_down == 0)) { // n <= 7
+ return memcmp(a, b, n) == 0;
+ }
+ // n >= 8
+ uint64 u = GOOGLE_UNALIGNED_LOAD64(a) ^ GOOGLE_UNALIGNED_LOAD64(b);
+ uint64 v = GOOGLE_UNALIGNED_LOAD64(a + n - 8) ^ GOOGLE_UNALIGNED_LOAD64(b + n - 8);
+ if ((u | v) != 0) { // The first or last 8 bytes differ.
+ return false;
+ }
+ a += 8;
+ b += 8;
+ n = n_rounded_down - 8;
+ if (n > 128) {
+ // As of 2012, memcmp on x86-64 uses a big unrolled loop with SSE2
+ // instructions, and while we could try to do something faster, it
+ // doesn't seem worth pursuing.
+ return memcmp(a, b, n) == 0;
+ }
+ for (; n >= 16; n -= 16) {
+ uint64 x = GOOGLE_UNALIGNED_LOAD64(a) ^ GOOGLE_UNALIGNED_LOAD64(b);
+ uint64 y = GOOGLE_UNALIGNED_LOAD64(a + 8) ^ GOOGLE_UNALIGNED_LOAD64(b + 8);
+ if ((x | y) != 0) {
+ return false;
+ }
+ a += 16;
+ b += 16;
+ }
+ // n must be 0 or 8 now because it was a multiple of 8 at the top of the loop.
+ return n == 0 || GOOGLE_UNALIGNED_LOAD64(a) == GOOGLE_UNALIGNED_LOAD64(b);
+}
+
+inline int fastmemcmp_inlined(const char *a, const char *b, size_t n) {
+ if (n >= 64) {
+ return memcmp(a, b, n);
+ }
+ const char* a_limit = a + n;
+ while (a + sizeof(uint64) <= a_limit &&
+ GOOGLE_UNALIGNED_LOAD64(a) == GOOGLE_UNALIGNED_LOAD64(b)) {
+ a += sizeof(uint64);
+ b += sizeof(uint64);
+ }
+ if (a + sizeof(uint32) <= a_limit &&
+ GOOGLE_UNALIGNED_LOAD32(a) == GOOGLE_UNALIGNED_LOAD32(b)) {
+ a += sizeof(uint32);
+ b += sizeof(uint32);
+ }
+ while (a < a_limit) {
+ int d = static_cast<uint32>(*a++) - static_cast<uint32>(*b++);
+ if (d) return d;
+ }
+ return 0;
+}
+
+// The standard memcpy operation is slow for variable small sizes.
+// This implementation inlines the optimal realization for sizes 1 to 16.
+// To avoid code bloat don't use it in case of not performance-critical spots,
+// nor when you don't expect very frequent values of size <= 16.
+inline void memcpy_inlined(char *dst, const char *src, size_t size) {
+ // Compiler inlines code with minimal amount of data movement when third
+ // parameter of memcpy is a constant.
+ switch (size) {
+ case 1: memcpy(dst, src, 1); break;
+ case 2: memcpy(dst, src, 2); break;
+ case 3: memcpy(dst, src, 3); break;
+ case 4: memcpy(dst, src, 4); break;
+ case 5: memcpy(dst, src, 5); break;
+ case 6: memcpy(dst, src, 6); break;
+ case 7: memcpy(dst, src, 7); break;
+ case 8: memcpy(dst, src, 8); break;
+ case 9: memcpy(dst, src, 9); break;
+ case 10: memcpy(dst, src, 10); break;
+ case 11: memcpy(dst, src, 11); break;
+ case 12: memcpy(dst, src, 12); break;
+ case 13: memcpy(dst, src, 13); break;
+ case 14: memcpy(dst, src, 14); break;
+ case 15: memcpy(dst, src, 15); break;
+ case 16: memcpy(dst, src, 16); break;
+ default: memcpy(dst, src, size); break;
+ }
+}
+
+} // namespace internal
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_STUBS_FASTMEM_H_
diff --git a/src/google/protobuf/stubs/map_util.h b/src/google/protobuf/stubs/map_util.h
index 7495cb6a..4cccbbed 100644
--- a/src/google/protobuf/stubs/map_util.h
+++ b/src/google/protobuf/stubs/map_util.h
@@ -353,7 +353,6 @@ template <class Collection>
void InsertOrDie(Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& data) {
- typedef typename Collection::value_type value_type;
GOOGLE_CHECK(InsertIfNotPresent(collection, key, data))
<< "duplicate key: " << key;
}
@@ -364,7 +363,6 @@ void InsertOrDieNoPrint(
Collection* const collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& data) {
- typedef typename Collection::value_type value_type;
GOOGLE_CHECK(InsertIfNotPresent(collection, key, data)) << "duplicate key.";
}
diff --git a/src/google/protobuf/stubs/platform_macros.h b/src/google/protobuf/stubs/platform_macros.h
index 7956d076..1ff09b83 100644
--- a/src/google/protobuf/stubs/platform_macros.h
+++ b/src/google/protobuf/stubs/platform_macros.h
@@ -72,6 +72,9 @@
#else
#define GOOGLE_PROTOBUF_ARCH_32_BIT 1
#endif
+#elif defined(_POWER)
+#define GOOGLE_PROTOBUF_ARCH_POWER 1
+#define GOOGLE_PROTOBUF_ARCH_64_BIT 1
#elif defined(__GNUC__)
# if (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4))
// We fallback to the generic Clang/GCC >= 4.7 implementation in atomicops.h
@@ -96,6 +99,8 @@ GOOGLE_PROTOBUF_PLATFORM_ERROR
#define GOOGLE_PROTOBUF_OS_NACL
#elif defined(sun)
#define GOOGLE_PROTOBUF_OS_SOLARIS
+#elif defined(_AIX)
+#define GOOGLE_PROTOBUF_OS_AIX
#endif
#undef GOOGLE_PROTOBUF_PLATFORM_ERROR
diff --git a/src/google/protobuf/stubs/singleton.h b/src/google/protobuf/stubs/singleton.h
new file mode 100644
index 00000000..e123e4fe
--- /dev/null
+++ b/src/google/protobuf/stubs/singleton.h
@@ -0,0 +1,64 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2014 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#ifndef GOOGLE_PROTOBUF_STUBS_SINGLETON_H__
+#define GOOGLE_PROTOBUF_STUBS_SINGLETON_H__
+
+#include <google/protobuf/stubs/atomicops.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/once.h>
+
+namespace google {
+namespace protobuf {
+namespace internal {
+template<typename T>
+class Singleton {
+ public:
+ static T* get() {
+ GoogleOnceInit(&once_, &Singleton<T>::Init);
+ return instance_;
+ }
+ private:
+ static void Init() {
+ instance_ = new T();
+ }
+ static ProtobufOnceType once_;
+ static T* instance_;
+};
+
+template<typename T>
+ProtobufOnceType Singleton<T>::once_;
+
+template<typename T>
+T* Singleton<T>::instance_;
+} // namespace internal
+} // namespace protobuf
+} // namespace google
+
+#endif // GOOGLE_PROTOBUF_STUBS_SINGLETON_H__
diff --git a/src/google/protobuf/stubs/strutil.cc b/src/google/protobuf/stubs/strutil.cc
index a8982593..86d45685 100644
--- a/src/google/protobuf/stubs/strutil.cc
+++ b/src/google/protobuf/stubs/strutil.cc
@@ -94,6 +94,34 @@ void StripString(string* s, const char* remove, char replacewith) {
}
}
+void StripWhitespace(string* str) {
+ int str_length = str->length();
+
+ // Strip off leading whitespace.
+ int first = 0;
+ while (first < str_length && ascii_isspace(str->at(first))) {
+ ++first;
+ }
+ // If entire string is white space.
+ if (first == str_length) {
+ str->clear();
+ return;
+ }
+ if (first > 0) {
+ str->erase(0, first);
+ str_length -= first;
+ }
+
+ // Strip off trailing whitespace.
+ int last = str_length - 1;
+ while (last >= 0 && ascii_isspace(str->at(last))) {
+ --last;
+ }
+ if (last != (str_length - 1) && last >= 0) {
+ str->erase(last + 1, string::npos);
+ }
+}
+
// ----------------------------------------------------------------------
// StringReplace()
// Replace the "old" pattern with the "new" pattern in a string,
@@ -1275,5 +1303,33 @@ string ToHex(uint64 num) {
return string(bufptr, buf + 16 - bufptr);
}
+int GlobalReplaceSubstring(const string& substring,
+ const string& replacement,
+ string* s) {
+ GOOGLE_CHECK(s != NULL);
+ if (s->empty() || substring.empty())
+ return 0;
+ string tmp;
+ int num_replacements = 0;
+ int pos = 0;
+ for (int match_pos = s->find(substring.data(), pos, substring.length());
+ match_pos != string::npos;
+ pos = match_pos + substring.length(),
+ match_pos = s->find(substring.data(), pos, substring.length())) {
+ ++num_replacements;
+ // Append the original content before the match.
+ tmp.append(*s, pos, match_pos - pos);
+ // Append the replacement for the match.
+ tmp.append(replacement.begin(), replacement.end());
+ }
+ // Append the content after the last match. If no replacements were made, the
+ // original string is left untouched.
+ if (num_replacements > 0) {
+ tmp.append(*s, pos, s->length() - pos);
+ s->swap(tmp);
+ }
+ return num_replacements;
+}
+
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/stubs/strutil.h b/src/google/protobuf/stubs/strutil.h
index 4178ac9f..f2e1a944 100644
--- a/src/google/protobuf/stubs/strutil.h
+++ b/src/google/protobuf/stubs/strutil.h
@@ -57,6 +57,8 @@ namespace protobuf {
// strings, so locale should not be taken into account.
// ascii_isdigit()
// Like above, but only accepts digits.
+// ascii_isspace()
+// Check if the character is a space character.
// ----------------------------------------------------------------------
inline bool ascii_isalnum(char c) {
@@ -69,6 +71,10 @@ inline bool ascii_isdigit(char c) {
return ('0' <= c && c <= '9');
}
+inline bool ascii_isspace(char c) {
+ return c == ' ';
+}
+
// ----------------------------------------------------------------------
// HasPrefixString()
// Check if a string begins with a given prefix.
@@ -119,10 +125,15 @@ inline string StripSuffixString(const string& str, const string& suffix) {
// in 'remove') with the character 'replacewith'.
// Good for keeping html characters or protocol characters (\t) out
// of places where they might cause a problem.
+// StripWhitespace
+// Removes whitespaces from both ends of the given string.
// ----------------------------------------------------------------------
LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
char replacewith);
+LIBPROTOBUF_EXPORT void StripWhitespace(string* s);
+
+
// ----------------------------------------------------------------------
// LowerString()
// UpperString()
@@ -501,6 +512,16 @@ inline string ToString(string a) {
// StrCat()
// These methods join some strings together.
// ----------------------------------------------------------------------
+template <typename T1, typename T2, typename T3, typename T4, typename T5,
+ typename T6, typename T7>
+string StrCat(
+ const T1& a, const T2& b, const T3& c, const T4& d, const T5& e,
+ const T6& f, const T7& g) {
+ return internal::ToString(a) + internal::ToString(b) +
+ internal::ToString(c) + internal::ToString(d) + internal::ToString(e) +
+ internal::ToString(f) + internal::ToString(g);
+}
+
template <typename T1, typename T2, typename T3, typename T4, typename T5>
string StrCat(
const T1& a, const T2& b, const T3& c, const T4& d, const T5& e) {
@@ -556,6 +577,17 @@ string Join(const Range& components,
// ----------------------------------------------------------------------
LIBPROTOBUF_EXPORT string ToHex(uint64 num);
+// ----------------------------------------------------------------------
+// GlobalReplaceSubstring()
+// Replaces all instances of a substring in a string. Does nothing
+// if 'substring' is empty. Returns the number of replacements.
+//
+// NOTE: The string pieces must not overlap s.
+// ----------------------------------------------------------------------
+LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring,
+ const string& replacement,
+ string* s);
+
} // namespace protobuf
} // namespace google
diff --git a/src/google/protobuf/stubs/template_util.h b/src/google/protobuf/stubs/template_util.h
index 4f30ffa3..feef904b 100644
--- a/src/google/protobuf/stubs/template_util.h
+++ b/src/google/protobuf/stubs/template_util.h
@@ -37,7 +37,7 @@
// any changes here, make sure that you're not breaking any platforms.
//
//
-// The names choosen here reflect those used in tr1 and the boost::mpl
+// The names chosen here reflect those used in tr1 and the boost::mpl
// library, there are similar operations used in the Loki library as
// well. I prefer the boost names for 2 reasons:
// 1. I think that portions of the Boost libraries are more likely to
diff --git a/src/google/protobuf/stubs/type_traits.h b/src/google/protobuf/stubs/type_traits.h
index e41f5e6f..b58cae3f 100644
--- a/src/google/protobuf/stubs/type_traits.h
+++ b/src/google/protobuf/stubs/type_traits.h
@@ -35,6 +35,7 @@
// any changes here, make sure that you're not breaking any platforms.
//
// Define a small subset of tr1 type traits. The traits we define are:
+// enable_if
// is_integral
// is_floating_point
// is_pointer
@@ -60,12 +61,27 @@
#include <utility> // For pair
+#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/template_util.h> // For true_type and false_type
namespace google {
namespace protobuf {
namespace internal {
+template<typename B, typename D>
+struct is_base_of {
+ typedef char (&yes)[1];
+ typedef char (&no)[2];
+
+ static yes check(const B*);
+ static no check(const void*);
+
+ enum {
+ value = sizeof(check(static_cast<const D*>(NULL))) == sizeof(yes),
+ };
+};
+
+template <bool cond, class T = void> struct enable_if;
template <class T> struct is_integral;
template <class T> struct is_floating_point;
template <class T> struct is_pointer;
@@ -87,10 +103,17 @@ template <class T> struct remove_reference;
template <class T> struct add_reference;
template <class T> struct remove_pointer;
template <class T, class U> struct is_same;
-#if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
+#if !(defined(__GNUC__) && __GNUC__ <= 3)
template <class From, class To> struct is_convertible;
#endif
+// enable_if, equivalent semantics to c++11 std::enable_if, specifically:
+// "If B is true, the member typedef type shall equal T; otherwise, there
+// shall be no member typedef type."
+// Specified by 20.9.7.6 [Other transformations]
+
+template<bool cond, class T> struct enable_if { typedef T type; };
+template<class T> struct enable_if<false, T> {};
// is_integral is false except for the built-in integer types. A
// cv-qualified type is integral if and only if the underlying type is.
template <class T> struct is_integral : false_type { };
@@ -144,7 +167,7 @@ template <class T> struct is_pointer<const volatile T> : is_pointer<T> { };
#if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
-namespace internal {
+namespace type_traits_internal {
template <class T> struct is_class_or_union {
template <class U> static small_ tester(void (U::*)());
@@ -159,7 +182,7 @@ template <bool NotUnum, class T> struct is_enum_impl
template <class T> struct is_enum_impl<true, T> : false_type { };
-} // namespace internal
+} // namespace type_traits_internal
// Specified by TR1 [4.5.1] primary type categories.
@@ -177,12 +200,12 @@ template <class T> struct is_enum_impl<true, T> : false_type { };
// because it can't be used with some types (e.g. void or classes with
// inaccessible conversion operators).
template <class T> struct is_enum
- : internal::is_enum_impl<
+ : type_traits_internal::is_enum_impl<
is_same<T, void>::value ||
is_integral<T>::value ||
is_floating_point<T>::value ||
is_reference<T>::value ||
- internal::is_class_or_union<T>::value,
+ type_traits_internal::is_class_or_union<T>::value,
T> { };
template <class T> struct is_enum<const T> : is_enum<T> { };
@@ -299,8 +322,8 @@ template<typename T, typename U> struct is_same : public false_type { };
template<typename T> struct is_same<T, T> : public true_type { };
// Specified by TR1 [4.6] Relationships between types
-#if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
-namespace internal {
+#if !(defined(__GNUC__) && __GNUC__ <= 3)
+namespace type_traits_internal {
// This class is an implementation detail for is_convertible, and you
// don't need to know how it works to use is_convertible. For those
@@ -316,16 +339,17 @@ struct ConvertHelper {
static small_ Test(To);
static big_ Test(...);
static From Create();
+ enum {
+ value = sizeof(Test(Create())) == sizeof(small_)
+ };
};
-} // namespace internal
+} // namespace type_traits_internal
// Inherits from true_type if From is convertible to To, false_type otherwise.
template <typename From, typename To>
struct is_convertible
: integral_constant<bool,
- sizeof(internal::ConvertHelper<From, To>::Test(
- internal::ConvertHelper<From, To>::Create()))
- == sizeof(small_)> {
+ type_traits_internal::ConvertHelper<From, To>::value> {
};
#endif
diff --git a/src/google/protobuf/stubs/type_traits_unittest.cc b/src/google/protobuf/stubs/type_traits_unittest.cc
index 7a8cbfb8..49c10ace 100644
--- a/src/google/protobuf/stubs/type_traits_unittest.cc
+++ b/src/google/protobuf/stubs/type_traits_unittest.cc
@@ -41,7 +41,10 @@
#include <gtest/gtest.h>
typedef int int32;
+// IBM AIX typedefs `int64` in `sys/inttypes.h`, included transitively above.
+#ifndef _AIX
typedef long int64;
+#endif
using std::string;
using std::vector;
@@ -607,7 +610,7 @@ TEST(TypeTraitsTest, TestIsSame) {
}
TEST(TypeTraitsTest, TestConvertible) {
-#if !defined(_MSC_VER) && !(defined(__GNUC__) && __GNUC__ <= 3)
+#if !(defined(__GNUC__) && __GNUC__ <= 3)
EXPECT_TRUE((is_convertible<int, int>::value));
EXPECT_TRUE((is_convertible<int, long>::value));
EXPECT_TRUE((is_convertible<long, int>::value));
diff --git a/src/google/protobuf/testing/file.cc b/src/google/protobuf/testing/file.cc
index 20e01a15..5344ec15 100644
--- a/src/google/protobuf/testing/file.cc
+++ b/src/google/protobuf/testing/file.cc
@@ -133,6 +133,8 @@ bool File::RecursivelyCreateDir(const string& path, int mode) {
void File::DeleteRecursively(const string& name,
void* dummy1, void* dummy2) {
+ if (name.empty()) return;
+
// We don't care too much about error checking here since this is only used
// in tests to delete temporary directories that are under /tmp anyway.
diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc
index 84cdbb57..8e867c09 100644
--- a/src/google/protobuf/text_format.cc
+++ b/src/google/protobuf/text_format.cc
@@ -50,6 +50,7 @@
#include <google/protobuf/unknown_field_set.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/tokenizer.h>
+#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/stl_util.h>
@@ -319,16 +320,16 @@ class TextFormat::Parser::ParserImpl {
message);
}
- // Consumes the specified message with the given starting delimeter.
- // This method checks to see that the end delimeter at the conclusion of
- // the consumption matches the starting delimeter passed in here.
- bool ConsumeMessage(Message* message, const string delimeter) {
+ // 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("}")) {
DO(ConsumeField(message));
}
- // Confirm that we have a valid ending delimeter.
- DO(Consume(delimeter));
+ // Confirm that we have a valid ending delimiter.
+ DO(Consume(delimiter));
return true;
}
@@ -428,7 +429,7 @@ class TextFormat::Parser::ParserImpl {
// Try to guess the type of this field.
// If this field is not a message, there should be a ":" between the
// field name and the field value and also the field value should not
- // start with "{" or "<" which indicates the begining of a message body.
+ // 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 (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
@@ -526,7 +527,7 @@ class TextFormat::Parser::ParserImpl {
// Try to guess the type of this field.
// If this field is not a message, there should be a ":" between the
// field name and the field value and also the field value should not
- // start with "{" or "<" which indicates the begining of a message body.
+ // 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 (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
@@ -551,19 +552,19 @@ class TextFormat::Parser::ParserImpl {
parse_info_tree_ = CreateNested(parent, field);
}
- string delimeter;
+ string delimiter;
if (TryConsume("<")) {
- delimeter = ">";
+ delimiter = ">";
} else {
DO(Consume("{"));
- delimeter = "}";
+ delimiter = "}";
}
if (field->is_repeated()) {
- DO(ConsumeMessage(reflection->AddMessage(message, field), delimeter));
+ DO(ConsumeMessage(reflection->AddMessage(message, field), delimiter));
} else {
DO(ConsumeMessage(reflection->MutableMessage(message, field),
- delimeter));
+ delimiter));
}
// Reset the parse information tree.
@@ -571,20 +572,20 @@ class TextFormat::Parser::ParserImpl {
return true;
}
- // Skips the whole body of a message including the begining delimeter and
- // the ending delimeter.
+ // Skips the whole body of a message including the beginning delimiter and
+ // the ending delimiter.
bool SkipFieldMessage() {
- string delimeter;
+ string delimiter;
if (TryConsume("<")) {
- delimeter = ">";
+ delimiter = ">";
} else {
DO(Consume("{"));
- delimeter = "}";
+ delimiter = "}";
}
while (!LookingAt(">") && !LookingAt("}")) {
DO(SkipField());
}
- DO(Consume(delimeter));
+ DO(Consume(delimiter));
return true;
}
@@ -1583,10 +1584,23 @@ void TextFormat::Printer::PrintFieldValue(
}
case FieldDescriptor::CPPTYPE_ENUM: {
- const EnumValueDescriptor *enum_val = field->is_repeated()
- ? reflection->GetRepeatedEnum(message, field, index)
- : reflection->GetEnum(message, field);
- generator.Print(printer->PrintEnum(enum_val->number(), enum_val->name()));
+ 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) {
+ generator.Print(printer->PrintEnum(enum_value, enum_desc->name()));
+ } else {
+ // Ordinarily, enum_desc should not be null, because proto2 has the
+ // invariant that set enum field values must be in-range, but with the
+ // new integer-based API for enums (or the RepeatedField<int> loophole),
+ // it is possible for the user to force an unknown integer value. So we
+ // simply use the integer value itself as the enum value name in this
+ // case.
+ generator.Print(printer->PrintEnum(enum_value,
+ StringPrintf("%d", enum_value)));
+ }
break;
}
diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h
index 29549410..44d68cab 100644
--- a/src/google/protobuf/text_format.h
+++ b/src/google/protobuf/text_format.h
@@ -40,6 +40,9 @@
#include <map>
#include <memory>
+#ifndef _SHARED_PTR_H
+#include <google/protobuf/stubs/shared_ptr.h>
+#endif
#include <string>
#include <vector>
@@ -268,7 +271,7 @@ class LIBPROTOBUF_EXPORT TextFormat {
bool print_message_fields_in_index_order_;
- scoped_ptr<const FieldValuePrinter> default_field_value_printer_;
+ google::protobuf::scoped_ptr<const FieldValuePrinter> default_field_value_printer_;
typedef map<const FieldDescriptor*,
const FieldValuePrinter*> CustomPrinterMap;
CustomPrinterMap custom_printers_;
diff --git a/src/google/protobuf/text_format_unittest.cc b/src/google/protobuf/text_format_unittest.cc
index 55732688..477fdcbd 100644
--- a/src/google/protobuf/text_format_unittest.cc
+++ b/src/google/protobuf/text_format_unittest.cc
@@ -263,7 +263,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) {
}
TEST_F(TextFormatTest, PrintUnknownFieldsHidden) {
- // Test printing of unknown fields in a message when supressed.
+ // Test printing of unknown fields in a message when suppressed.
unittest::OneString message;
message.set_data("data");
@@ -586,7 +586,7 @@ TEST_F(TextFormatTest, ParseConcatenatedString) {
// Compare.
EXPECT_EQ("foobar", proto_.optional_string());
- // Create a parse string with multiple parts on seperate lines.
+ // Create a parse string with multiple parts on separate lines.
parse_string = "optional_string: \"foo\"\n"
"\"bar\"\n";
@@ -1324,12 +1324,12 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) {
"Expected \":\", found \"{\".", 1, 22);
}
-TEST_F(TextFormatParserTest, MessageDelimeters) {
- // Non-matching delimeters.
+TEST_F(TextFormatParserTest, MessageDelimiters) {
+ // Non-matching delimiters.
ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".",
3, 1);
- // Invalid delimeters.
+ // Invalid delimiters.
ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".",
1, 15);
@@ -1340,7 +1340,7 @@ TEST_F(TextFormatParserTest, MessageDelimeters) {
}
TEST_F(TextFormatParserTest, UnknownExtension) {
- // Non-matching delimeters.
+ // Non-matching delimiters.
ExpectFailure("[blahblah]: 123",
"Extension \"blahblah\" is not defined or is not an "
"extension of \"protobuf_unittest.TestAllTypes\".",
diff --git a/src/google/protobuf/unittest.proto b/src/google/protobuf/unittest.proto
index c48cc923..745f77c5 100644
--- a/src/google/protobuf/unittest.proto
+++ b/src/google/protobuf/unittest.proto
@@ -34,12 +34,14 @@
//
// A proto file we will use for unit testing.
+syntax = "proto2";
// Some generic_services option(s) added automatically.
// See: http://go/proto2-generic-services-default
option cc_generic_services = true; // auto-added
option java_generic_services = true; // auto-added
option py_generic_services = true; // auto-added
+option cc_enable_arenas = true;
import "google/protobuf/unittest_import.proto";
@@ -429,7 +431,7 @@ message TestMutualRecursionB {
}
// Test that groups have disjoint field numbers from their siblings and
-// parents. This is NOT possible in proto1; only proto2. When attempting
+// parents. This is NOT possible in proto1; only google.protobuf. When attempting
// to compile with proto1, this will emit an error; so we only include it
// in protobuf_unittest_proto.
message TestDupFieldNumber { // NO_PROTO1
@@ -505,6 +507,15 @@ message TestFieldOrderings {
optional int64 my_int = 1;
extensions 12 to 100;
optional float my_float = 101;
+ message NestedMessage {
+ optional int64 oo = 2;
+ // The field name "b" fails to compile in proto1 because it conflicts with
+ // a local variable named "b" in one of the generated methods. Doh.
+ // This file needs to compile in proto1 to test backwards-compatibility.
+ optional int32 bb = 1;
+ }
+
+ optional NestedMessage optional_nested_message = 200;
}
diff --git a/src/google/protobuf/unittest_arena.proto b/src/google/protobuf/unittest_arena.proto
new file mode 100644
index 00000000..cd7e437e
--- /dev/null
+++ b/src/google/protobuf/unittest_arena.proto
@@ -0,0 +1,46 @@
+// 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";
+
+import "google/protobuf/unittest_no_arena_import.proto";
+
+package proto2_arena_unittest;
+
+option cc_enable_arenas = true;
+
+message NestedMessage {
+ optional int32 d = 1;
+}
+
+message ArenaMessage {
+ repeated NestedMessage repeated_nested_message = 1;
+ repeated ImportNoArenaNestedMessage repeated_import_no_arena_message = 2;
+};
diff --git a/src/google/protobuf/unittest_custom_options.proto b/src/google/protobuf/unittest_custom_options.proto
index 0bb12e17..d4d6e869 100644
--- a/src/google/protobuf/unittest_custom_options.proto
+++ b/src/google/protobuf/unittest_custom_options.proto
@@ -32,8 +32,9 @@
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
-// A proto file used to test the "custom options" feature of proto2.
+// A proto file used to test the "custom options" feature of google.protobuf.
+syntax = "proto2";
// Some generic_services option(s) added automatically.
// See: http://go/proto2-generic-services-default
diff --git a/src/google/protobuf/unittest_drop_unknown_fields.proto b/src/google/protobuf/unittest_drop_unknown_fields.proto
new file mode 100644
index 00000000..66b31acf
--- /dev/null
+++ b/src/google/protobuf/unittest_drop_unknown_fields.proto
@@ -0,0 +1,55 @@
+// 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 unittest_drop_unknown_fields;
+
+message Foo {
+ enum NestedEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ }
+ optional int32 int32_value = 1;
+ optional NestedEnum enum_value = 2;
+}
+
+message FooWithExtraFields {
+ enum NestedEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ QUX = 3;
+ }
+ optional int32 int32_value = 1;
+ optional NestedEnum enum_value = 2;
+ optional int32 extra_int32_value = 3;
+}
diff --git a/src/google/protobuf/unittest_embed_optimize_for.proto b/src/google/protobuf/unittest_embed_optimize_for.proto
index ad515131..d8b0f9b9 100644
--- a/src/google/protobuf/unittest_embed_optimize_for.proto
+++ b/src/google/protobuf/unittest_embed_optimize_for.proto
@@ -34,6 +34,7 @@
//
// A proto file which imports a proto file that uses optimize_for = CODE_SIZE.
+syntax = "proto2";
import "google/protobuf/unittest_optimize_for.proto";
package protobuf_unittest;
diff --git a/src/google/protobuf/unittest_empty.proto b/src/google/protobuf/unittest_empty.proto
index b7337ef3..36443e7e 100644
--- a/src/google/protobuf/unittest_empty.proto
+++ b/src/google/protobuf/unittest_empty.proto
@@ -35,3 +35,4 @@
// This file intentionally left blank. (At one point this wouldn't compile
// correctly.)
+syntax = "proto2";
diff --git a/src/google/protobuf/unittest_enormous_descriptor.proto b/src/google/protobuf/unittest_enormous_descriptor.proto
index 3ba8b419..2250261d 100644
--- a/src/google/protobuf/unittest_enormous_descriptor.proto
+++ b/src/google/protobuf/unittest_enormous_descriptor.proto
@@ -35,6 +35,7 @@
// A proto file that has an extremely large descriptor. Used to test that
// descriptors over 64k don't break the string literal length limit in Java.
+syntax = "proto2";
package google.protobuf;
option java_package = "com.google.protobuf";
diff --git a/src/google/protobuf/unittest_import.proto b/src/google/protobuf/unittest_import.proto
index 38275dc9..7e165220 100644
--- a/src/google/protobuf/unittest_import.proto
+++ b/src/google/protobuf/unittest_import.proto
@@ -34,6 +34,7 @@
//
// A proto file which is imported by unittest.proto to test importing.
+syntax = "proto2";
// 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.
@@ -42,8 +43,9 @@
package protobuf_unittest_import;
option optimize_for = SPEED;
+option cc_enable_arenas = true;
-// Excercise the java_package option.
+// Exercise the java_package option.
option java_package = "com.google.protobuf.test";
// Do not set a java_outer_classname here to verify that Proto2 works without
diff --git a/src/google/protobuf/unittest_import_lite.proto b/src/google/protobuf/unittest_import_lite.proto
index 7cee113a..a7afa452 100644
--- a/src/google/protobuf/unittest_import_lite.proto
+++ b/src/google/protobuf/unittest_import_lite.proto
@@ -32,6 +32,7 @@
//
// This is like unittest_import.proto but with optimize_for = LITE_RUNTIME.
+syntax = "proto2";
package protobuf_unittest_import;
option optimize_for = LITE_RUNTIME;
diff --git a/src/google/protobuf/unittest_import_public.proto b/src/google/protobuf/unittest_import_public.proto
index e5929cb5..ffaf7736 100644
--- a/src/google/protobuf/unittest_import_public.proto
+++ b/src/google/protobuf/unittest_import_public.proto
@@ -30,6 +30,7 @@
// Author: liujisi@google.com (Pherl Liu)
+syntax = "proto2";
package protobuf_unittest_import;
diff --git a/src/google/protobuf/unittest_import_public_lite.proto b/src/google/protobuf/unittest_import_public_lite.proto
index e39bdb49..33549c22 100644
--- a/src/google/protobuf/unittest_import_public_lite.proto
+++ b/src/google/protobuf/unittest_import_public_lite.proto
@@ -30,6 +30,7 @@
// Author: liujisi@google.com (Pherl Liu)
+syntax = "proto2";
package protobuf_unittest_import;
diff --git a/src/google/protobuf/unittest_lite.proto b/src/google/protobuf/unittest_lite.proto
index 30dd67fb..662c0e46 100644
--- a/src/google/protobuf/unittest_lite.proto
+++ b/src/google/protobuf/unittest_lite.proto
@@ -32,6 +32,7 @@
//
// This is like unittest.proto but with optimize_for = LITE_RUNTIME.
+syntax = "proto2";
package protobuf_unittest;
import "google/protobuf/unittest_import_lite.proto";
diff --git a/src/google/protobuf/unittest_lite_imports_nonlite.proto b/src/google/protobuf/unittest_lite_imports_nonlite.proto
index 5ef8a06d..132d6a82 100644
--- a/src/google/protobuf/unittest_lite_imports_nonlite.proto
+++ b/src/google/protobuf/unittest_lite_imports_nonlite.proto
@@ -32,6 +32,7 @@
//
// Tests that a "lite" message can import a regular message.
+syntax = "proto2";
package protobuf_unittest;
import "google/protobuf/unittest.proto";
diff --git a/src/google/protobuf/unittest_mset.proto b/src/google/protobuf/unittest_mset.proto
index 8fbe7abb..3aa31fa9 100644
--- a/src/google/protobuf/unittest_mset.proto
+++ b/src/google/protobuf/unittest_mset.proto
@@ -34,8 +34,10 @@
//
// This file contains messages for testing message_set_wire_format.
+syntax = "proto2";
package protobuf_unittest;
+option cc_enable_arenas = true;
option optimize_for = SPEED;
// A message with message_set_wire_format.
diff --git a/src/google/protobuf/unittest_no_arena.proto b/src/google/protobuf/unittest_no_arena.proto
new file mode 100644
index 00000000..1a420838
--- /dev/null
+++ b/src/google/protobuf/unittest_no_arena.proto
@@ -0,0 +1,200 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Author: kenton@google.com (Kenton Varda)
+// Based on original Protocol Buffers design by
+// Sanjay Ghemawat, Jeff Dean, and others.
+//
+// This proto file contains copies of TestAllTypes and friends, but with arena
+// support disabled in code generation. It allows us to test the performance
+// impact against baseline (non-arena) google.protobuf.
+
+syntax = "proto2";
+
+// Some generic_services option(s) added automatically.
+// See: http://go/proto2-generic-services-default
+option cc_generic_services = true; // auto-added
+option java_generic_services = true; // auto-added
+option py_generic_services = true; // auto-added
+option cc_enable_arenas = false;
+
+import "google/protobuf/unittest_import.proto";
+import "google/protobuf/unittest_arena.proto";
+
+// 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.
+// In test_util.h we do "using namespace unittest = protobuf_unittest".
+package protobuf_unittest_no_arena;
+
+// Protos optimized for SPEED use a strict superset of the generated code
+// of equivalent ones optimized for CODE_SIZE, so we should optimize all our
+// tests for speed unless explicitly testing code size optimization.
+option optimize_for = SPEED;
+
+option java_outer_classname = "UnittestProto";
+
+// This proto includes every type of field in both singular and repeated
+// forms.
+message TestAllTypes {
+ message NestedMessage {
+ // The field name "b" fails to compile in proto1 because it conflicts with
+ // a local variable named "b" in one of the generated methods. Doh.
+ // This file needs to compile in proto1 to test backwards-compatibility.
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 1;
+ BAR = 2;
+ BAZ = 3;
+ NEG = -1; // Intentionally negative.
+ }
+
+ // Singular
+ 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 group OptionalGroup = 16 {
+ optional int32 a = 17;
+ }
+
+ optional NestedMessage optional_nested_message = 18;
+ optional ForeignMessage optional_foreign_message = 19;
+ optional protobuf_unittest_import.ImportMessage optional_import_message = 20;
+
+ optional NestedEnum optional_nested_enum = 21;
+ optional ForeignEnum optional_foreign_enum = 22;
+ optional protobuf_unittest_import.ImportEnum optional_import_enum = 23;
+
+ optional string optional_string_piece = 24 [ctype=STRING_PIECE];
+ optional string optional_cord = 25 [ctype=CORD];
+
+ // Defined in unittest_import_public.proto
+ optional protobuf_unittest_import.PublicImportMessage
+ optional_public_import_message = 26;
+
+ optional NestedMessage optional_lazy_message = 27 [lazy=true];
+
+ // 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 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 group RepeatedGroup = 46 {
+ optional int32 a = 47;
+ }
+
+ repeated NestedMessage repeated_nested_message = 48;
+ repeated ForeignMessage repeated_foreign_message = 49;
+ repeated protobuf_unittest_import.ImportMessage repeated_import_message = 50;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+ repeated ForeignEnum repeated_foreign_enum = 52;
+ repeated protobuf_unittest_import.ImportEnum repeated_import_enum = 53;
+
+ repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
+ repeated string repeated_cord = 55 [ctype=CORD];
+
+ repeated NestedMessage repeated_lazy_message = 57 [lazy=true];
+
+ // Singular with defaults
+ optional int32 default_int32 = 61 [default = 41 ];
+ optional int64 default_int64 = 62 [default = 42 ];
+ optional uint32 default_uint32 = 63 [default = 43 ];
+ optional uint64 default_uint64 = 64 [default = 44 ];
+ optional sint32 default_sint32 = 65 [default = -45 ];
+ optional sint64 default_sint64 = 66 [default = 46 ];
+ optional fixed32 default_fixed32 = 67 [default = 47 ];
+ optional fixed64 default_fixed64 = 68 [default = 48 ];
+ optional sfixed32 default_sfixed32 = 69 [default = 49 ];
+ optional sfixed64 default_sfixed64 = 70 [default = -50 ];
+ optional float default_float = 71 [default = 51.5 ];
+ optional double default_double = 72 [default = 52e3 ];
+ optional bool default_bool = 73 [default = true ];
+ optional string default_string = 74 [default = "hello"];
+ optional bytes default_bytes = 75 [default = "world"];
+
+ optional NestedEnum default_nested_enum = 81 [default = BAR ];
+ optional ForeignEnum default_foreign_enum = 82 [default = FOREIGN_BAR];
+ optional protobuf_unittest_import.ImportEnum
+ default_import_enum = 83 [default = IMPORT_BAR];
+
+ optional string default_string_piece = 84 [ctype=STRING_PIECE,default="abc"];
+ optional string default_cord = 85 [ctype=CORD,default="123"];
+
+ // For oneof test
+ oneof oneof_field {
+ uint32 oneof_uint32 = 111;
+ NestedMessage oneof_nested_message = 112;
+ string oneof_string = 113;
+ bytes oneof_bytes = 114;
+ }
+}
+
+// Define these after TestAllTypes to make sure the compiler can handle
+// that.
+message ForeignMessage {
+ optional int32 c = 1;
+}
+
+enum ForeignEnum {
+ FOREIGN_FOO = 4;
+ FOREIGN_BAR = 5;
+ FOREIGN_BAZ = 6;
+}
+
+message TestNoArenaMessage {
+ optional proto2_arena_unittest.ArenaMessage arena_message = 1;
+};
diff --git a/src/google/protobuf/unittest_no_arena_import.proto b/src/google/protobuf/unittest_no_arena_import.proto
new file mode 100644
index 00000000..072af49e
--- /dev/null
+++ b/src/google/protobuf/unittest_no_arena_import.proto
@@ -0,0 +1,37 @@
+// 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_arena_unittest;
+
+message ImportNoArenaNestedMessage {
+ optional int32 d = 1;
+};
diff --git a/src/google/protobuf/unittest_no_field_presence.proto b/src/google/protobuf/unittest_no_field_presence.proto
new file mode 100644
index 00000000..e04da0ac
--- /dev/null
+++ b/src/google/protobuf/unittest_no_field_presence.proto
@@ -0,0 +1,138 @@
+// 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 proto file used to test a message type with no explicit field presence.
+
+syntax = "proto3";
+
+// We want to test embedded proto2 messages, so include some proto2 types.
+import "google/protobuf/unittest.proto";
+
+package proto2_nofieldpresence_unittest;
+
+// This proto includes every type of field in both singular and repeated
+// forms.
+message TestAllTypes {
+ message NestedMessage {
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ }
+
+ // Singular
+ // TODO: remove 'optional' labels as soon as CL 69188077 is LGTM'd to make
+ // 'optional' optional.
+ 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 NestedMessage optional_nested_message = 18;
+ optional ForeignMessage optional_foreign_message = 19;
+ optional protobuf_unittest.TestAllTypes optional_proto2_message = 20;
+
+ optional NestedEnum optional_nested_enum = 21;
+ optional ForeignEnum optional_foreign_enum = 22;
+ // N.B.: proto2-enum-type fields not allowed, because their default values
+ // might not be zero.
+ //optional protobuf_unittest.ForeignEnum optional_proto2_enum = 23;
+
+ optional string optional_string_piece = 24 [ctype=STRING_PIECE];
+ optional string optional_cord = 25 [ctype=CORD];
+
+ optional NestedMessage optional_lazy_message = 30 [lazy=true];
+
+ // 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 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 NestedMessage repeated_nested_message = 48;
+ repeated ForeignMessage repeated_foreign_message = 49;
+ repeated protobuf_unittest.TestAllTypes repeated_proto2_message = 50;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+ repeated ForeignEnum repeated_foreign_enum = 52;
+
+ repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
+ repeated string repeated_cord = 55 [ctype=CORD];
+
+ repeated NestedMessage repeated_lazy_message = 57 [lazy=true];
+
+ oneof oneof_field {
+ uint32 oneof_uint32 = 111;
+ NestedMessage oneof_nested_message = 112;
+ string oneof_string = 113;
+ NestedEnum oneof_enum = 114;
+ }
+}
+
+message TestProto2Required {
+ optional protobuf_unittest.TestRequired proto2 = 1;
+}
+
+// Define these after TestAllTypes to make sure the compiler can handle
+// that.
+message ForeignMessage {
+ optional int32 c = 1;
+}
+
+enum ForeignEnum {
+ FOREIGN_FOO = 0;
+ FOREIGN_BAR = 1;
+ FOREIGN_BAZ = 2;
+}
diff --git a/src/google/protobuf/unittest_no_generic_services.proto b/src/google/protobuf/unittest_no_generic_services.proto
index cf6d16c2..8fc7713c 100644
--- a/src/google/protobuf/unittest_no_generic_services.proto
+++ b/src/google/protobuf/unittest_no_generic_services.proto
@@ -30,6 +30,7 @@
// Author: kenton@google.com (Kenton Varda)
+syntax = "proto2";
package google.protobuf.no_generic_services_test;
diff --git a/src/google/protobuf/unittest_optimize_for.proto b/src/google/protobuf/unittest_optimize_for.proto
index 9f7b4eea..ee9cc7bd 100644
--- a/src/google/protobuf/unittest_optimize_for.proto
+++ b/src/google/protobuf/unittest_optimize_for.proto
@@ -34,6 +34,7 @@
//
// A proto file which uses optimize_for = CODE_SIZE.
+syntax = "proto2";
import "google/protobuf/unittest.proto";
package protobuf_unittest;
diff --git a/src/google/protobuf/unittest_preserve_unknown_enum.proto b/src/google/protobuf/unittest_preserve_unknown_enum.proto
new file mode 100644
index 00000000..ba050d70
--- /dev/null
+++ b/src/google/protobuf/unittest_preserve_unknown_enum.proto
@@ -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.
+
+syntax = "proto3";
+
+package proto2_preserve_unknown_enum_unittest;
+
+enum MyEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+}
+
+enum MyEnumPlusExtra {
+ E_FOO = 0;
+ E_BAR = 1;
+ E_BAZ = 2;
+ E_EXTRA = 3;
+}
+
+message MyMessage {
+ optional MyEnum e = 1;
+ repeated MyEnum repeated_e = 2;
+ repeated MyEnum repeated_packed_e = 3 [packed=true];
+ oneof o {
+ MyEnum oneof_e_1 = 4;
+ MyEnum oneof_e_2 = 5;
+ }
+}
+
+message MyMessagePlusExtra {
+ optional MyEnumPlusExtra e = 1;
+ repeated MyEnumPlusExtra repeated_e = 2;
+ repeated MyEnumPlusExtra repeated_packed_e = 3 [packed=true];
+ oneof o {
+ MyEnumPlusExtra oneof_e_1 = 4;
+ MyEnumPlusExtra oneof_e_2 = 5;
+ }
+}
diff --git a/src/google/protobuf/unittest_proto3_arena.proto b/src/google/protobuf/unittest_proto3_arena.proto
new file mode 100644
index 00000000..53f23d87
--- /dev/null
+++ b/src/google/protobuf/unittest_proto3_arena.proto
@@ -0,0 +1,144 @@
+// 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";
+
+option cc_enable_arenas = true;
+
+import "google/protobuf/unittest_import.proto";
+
+package proto3_arena_unittest;
+
+// This proto includes every type of field in both singular and repeated
+// forms.
+message TestAllTypes {
+ message NestedMessage {
+ // The field name "b" fails to compile in proto1 because it conflicts with
+ // a local variable named "b" in one of the generated methods. Doh.
+ // This file needs to compile in proto1 to test backwards-compatibility.
+ optional int32 bb = 1;
+ }
+
+ enum NestedEnum {
+ FOO = 0;
+ BAR = 1;
+ BAZ = 2;
+ NEG = -1; // Intentionally negative.
+ }
+
+ // Singular
+ 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 group OptionalGroup = 16 {
+ optional int32 a = 17;
+ }
+
+ optional NestedMessage optional_nested_message = 18;
+ optional ForeignMessage optional_foreign_message = 19;
+ optional protobuf_unittest_import.ImportMessage optional_import_message = 20;
+
+ optional NestedEnum optional_nested_enum = 21;
+ optional ForeignEnum optional_foreign_enum = 22;
+
+ optional string optional_string_piece = 24 [ctype=STRING_PIECE];
+ optional string optional_cord = 25 [ctype=CORD];
+
+ // Defined in unittest_import_public.proto
+ optional protobuf_unittest_import.PublicImportMessage
+ optional_public_import_message = 26;
+
+ optional NestedMessage optional_lazy_message = 27 [lazy=true];
+
+ // 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 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 group RepeatedGroup = 46 {
+ optional int32 a = 47;
+ }
+
+ repeated NestedMessage repeated_nested_message = 48;
+ repeated ForeignMessage repeated_foreign_message = 49;
+ repeated protobuf_unittest_import.ImportMessage repeated_import_message = 50;
+
+ repeated NestedEnum repeated_nested_enum = 51;
+ repeated ForeignEnum repeated_foreign_enum = 52;
+
+ repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
+ repeated string repeated_cord = 55 [ctype=CORD];
+
+ repeated NestedMessage repeated_lazy_message = 57 [lazy=true];
+
+ oneof oneof_field {
+ uint32 oneof_uint32 = 111;
+ NestedMessage oneof_nested_message = 112;
+ string oneof_string = 113;
+ bytes oneof_bytes = 114;
+ }
+}
+
+// Define these after TestAllTypes to make sure the compiler can handle
+// that.
+message ForeignMessage {
+ optional int32 c = 1;
+}
+
+enum ForeignEnum {
+ FOREIGN_FOO = 0;
+ FOREIGN_BAR = 1;
+ FOREIGN_BAZ = 2;
+}
diff --git a/src/google/protobuf/unknown_enum_impl.h b/src/google/protobuf/unknown_enum_impl.h
new file mode 100644
index 00000000..fb5380aa
--- /dev/null
+++ b/src/google/protobuf/unknown_enum_impl.h
@@ -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.
+
+#ifndef GOOGLE_PROTOBUF_UTIL_UNKNOWN_ENUM_IMPL_H__
+#define GOOGLE_PROTOBUF_UTIL_UNKNOWN_ENUM_IMPL_H__
+
+#include <stdlib.h>
+
+#include <google/protobuf/stubs/common.h>
+#include "net/proto/tagmapper.h"
+#include <google/protobuf/bridge/compatibility_mode_support.h>
+
+namespace google {
+namespace protobuf {
+
+// google/protobuf/message.h
+class Message;
+
+namespace util {
+
+// NOTE: You should not call these functions directly. Instead use either
+// HAS_UNKNOWN_ENUM() or GET_UNKNOWN_ENUM(), defined in the public header.
+// The macro-versions operate in a type-safe manner and behave appropriately
+// for the proto version of the message, whereas these versions assume a
+// specific proto version and allow the caller to pass in any arbitrary integer
+// value as a field number.
+//
+// Returns whether the message has unrecognized the enum value for a given
+// field. It also stores the value into the unknown_value parameter if the
+// function returns true and the pointer is not NULL.
+//
+// In proto2, invalid enum values will be treated as unknown fields. This
+// function checks that case.
+bool HasUnknownEnum(const Message& message, int32 field_number,
+ int32* unknown_value = nullptr);
+// Same as above, but returns all unknown enums.
+bool GetRepeatedEnumUnknowns(const Message& message, int32 field_number,
+ vector<int32>* unknown_values = nullptr);
+// In proto1, invalue enum values are stored in the same way as valid enum
+// values.
+// TODO(karner): Delete this once the migration to proto2 is complete.
+bool HasUnknownEnumProto1(const Message& message, int32 field_number,
+ int32* unknown_value);
+// Same as above, but returns all unknown enums.
+bool GetRepeatedEnumUnknownsProto1(const Message& message, int32 field_number,
+ vector<int32>* unknown_values);
+// Invokes the appropriate version based on whether the message is proto1
+// or proto2.
+template <typename T>
+bool HasUnknownEnum_Template(const T& message, int32 field_number,
+ int32* unknown_value = nullptr) {
+ if (internal::is_base_of<bridge::internal::Proto1CompatibleMessage, T>::value ||
+ !internal::is_base_of<ProtocolMessage, T>::value) {
+ return HasUnknownEnum(message, field_number, unknown_value);
+ } else {
+ return HasUnknownEnumProto1(message, field_number, unknown_value);
+ }
+}
+// Invokes the appropriate version based on whether the message is proto1
+// or proto2.
+template <typename T>
+bool GetRepeatedEnumUnknowns_Template(
+ const T& message, int32 field_number,
+ vector<int32>* unknown_values = nullptr) {
+ if (internal::is_base_of<bridge::internal::Proto1CompatibleMessage, T>::value ||
+ !internal::is_base_of<ProtocolMessage, T>::value) {
+ return GetRepeatedEnumUnknowns(message, field_number, unknown_values);
+ } else {
+ return GetRepeatedEnumUnknownsProto1(message, field_number,
+ unknown_values);
+ }
+}
+
+// NOTE: You should not call these functions directly. Instead use
+// SET_UNKNOWN_ENUM(), defined in the public header. The macro-versions
+// operate in a type-safe manner and behave appropriately for the proto
+// version of the message, whereas these versions assume a specific proto
+// version and allow the caller to pass in any arbitrary integer value as a
+// field number.
+//
+// Sets the given value in the unknown fields of the message.
+void SetUnknownEnum(Message* message, int32 field_number, int32 unknown_value);
+// In proto1, invalue enum values are stored in the same way as valid enum
+// values.
+// TODO(karner): Delete this once the migration to proto2 is complete.
+void SetUnknownEnumProto1(Message* message, int32 field_number,
+ int32 unknown_value);
+// Invokes the appropriate version based on whether the message is proto1
+// or proto2.
+template <typename T>
+void SetUnknownEnum_Template(T* message, int32 field_number,
+ int32 unknown_value) {
+ if (internal::is_base_of<bridge::internal::Proto1CompatibleMessage, T>::value ||
+ !internal::is_base_of<ProtocolMessage, T>::value) {
+ SetUnknownEnum(message, field_number, unknown_value);
+ } else {
+ SetUnknownEnumProto1(message, field_number, unknown_value);
+ }
+}
+
+} // namespace util
+} // namespace protobuf
+
+} // namespace google
+#endif // GOOGLE_PROTOBUF_UTIL_UNKNOWN_ENUM_IMPL_H__
diff --git a/src/google/protobuf/unknown_enum_test.proto b/src/google/protobuf/unknown_enum_test.proto
new file mode 100644
index 00000000..caafadcd
--- /dev/null
+++ b/src/google/protobuf/unknown_enum_test.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.
+
+// Definitions of protos for testing cross-version compatibility. The
+// UpRevision message acts as if it were a newer version of the DownRevision
+// message. That is, UpRevision shares all the same fields as DownRevision,
+// but UpRevision can add fields and add enum values.
+syntax = "proto2";
+
+package google.protobuf.util;
+
+
+message DownRevision {
+ enum Enum {
+ DEFAULT_VALUE = 2;
+ NONDEFAULT_VALUE = 3;
+ }
+
+ optional Enum value = 1 [default = DEFAULT_VALUE];
+ repeated Enum values = 2;
+}
+
+message UpRevision {
+ enum Enum {
+ DEFAULT_VALUE = 2;
+ NONDEFAULT_VALUE = 3;
+ NEW_VALUE = 4;
+ NEW_VALUE_2 = 5;
+ NEW_VALUE_3 = 6;
+ }
+
+ optional Enum value = 1 [default = DEFAULT_VALUE];
+ repeated Enum values = 2;
+}
diff --git a/src/google/protobuf/unknown_field_set.cc b/src/google/protobuf/unknown_field_set.cc
index 020a323b..e15280c8 100644
--- a/src/google/protobuf/unknown_field_set.cc
+++ b/src/google/protobuf/unknown_field_set.cc
@@ -44,6 +44,25 @@
namespace google {
namespace protobuf {
+namespace {
+// This global instance is returned by unknown_fields() on any message class
+// when the object has no unknown fields. This is necessary because we now
+// instantiate the UnknownFieldSet dynamically only when required.
+UnknownFieldSet* default_unknown_field_set_instance_ = NULL;
+
+void InitDefaultUnknownFieldSet() {
+ default_unknown_field_set_instance_ = new UnknownFieldSet();
+}
+
+GOOGLE_PROTOBUF_DECLARE_ONCE(default_unknown_field_set_once_init_);
+}
+
+const UnknownFieldSet* UnknownFieldSet::default_instance() {
+ ::google::protobuf::GoogleOnceInit(&default_unknown_field_set_once_init_,
+ &InitDefaultUnknownFieldSet);
+ return default_unknown_field_set_instance_;
+}
+
UnknownFieldSet::UnknownFieldSet()
: fields_(NULL) {}
@@ -53,31 +72,63 @@ UnknownFieldSet::~UnknownFieldSet() {
}
void UnknownFieldSet::ClearFallback() {
- GOOGLE_DCHECK(fields_ != NULL);
- for (int i = 0; i < fields_->size(); i++) {
- (*fields_)[i].Delete();
+ if (fields_ != NULL) {
+ for (int i = 0; i < fields_->size(); i++) {
+ (*fields_)[i].Delete();
+ }
+ delete fields_;
+ fields_ = NULL;
}
- fields_->clear();
}
void UnknownFieldSet::ClearAndFreeMemory() {
if (fields_ != NULL) {
Clear();
- delete fields_;
- fields_ = NULL;
+ }
+}
+
+void UnknownFieldSet::InternalMergeFrom(const UnknownFieldSet& other) {
+ int other_field_count = other.field_count();
+ if (other_field_count > 0) {
+ fields_ = new vector<UnknownField>();
+ for (int i = 0; i < other_field_count; i++) {
+ fields_->push_back((*other.fields_)[i]);
+ fields_->back().DeepCopy();
+ }
}
}
void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
- for (int i = 0; i < other.field_count(); i++) {
- AddField(other.field(i));
+ int other_field_count = other.field_count();
+ if (other_field_count > 0) {
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
+ for (int i = 0; i < other_field_count; i++) {
+ fields_->push_back((*other.fields_)[i]);
+ fields_->back().DeepCopy();
+ }
}
}
+// A specialized MergeFrom for performance when we are merging from an UFS that
+// is temporary and can be destroyed in the process.
+void UnknownFieldSet::MergeFromAndDestroy(UnknownFieldSet* other) {
+ int other_field_count = other->field_count();
+ if (other_field_count > 0) {
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
+ for (int i = 0; i < other_field_count; i++) {
+ fields_->push_back((*other->fields_)[i]);
+ (*other->fields_)[i].Reset();
+ }
+ }
+ delete other->fields_;
+ other->fields_ = NULL;
+}
+
int UnknownFieldSet::SpaceUsedExcludingSelf() const {
if (fields_ == NULL) return 0;
int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size();
+
for (int i = 0; i < fields_->size(); i++) {
const UnknownField& field = (*fields_)[i];
switch (field.type()) {
@@ -101,61 +152,60 @@ int UnknownFieldSet::SpaceUsed() const {
}
void UnknownFieldSet::AddVarint(int number, uint64 value) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
UnknownField field;
field.number_ = number;
field.SetType(UnknownField::TYPE_VARINT);
field.varint_ = value;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
}
void UnknownFieldSet::AddFixed32(int number, uint32 value) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
UnknownField field;
field.number_ = number;
field.SetType(UnknownField::TYPE_FIXED32);
field.fixed32_ = value;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
}
void UnknownFieldSet::AddFixed64(int number, uint64 value) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
UnknownField field;
field.number_ = number;
field.SetType(UnknownField::TYPE_FIXED64);
field.fixed64_ = value;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
}
string* UnknownFieldSet::AddLengthDelimited(int number) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
UnknownField field;
field.number_ = number;
field.SetType(UnknownField::TYPE_LENGTH_DELIMITED);
field.length_delimited_.string_value_ = new string;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
return field.length_delimited_.string_value_;
}
UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
UnknownField field;
field.number_ = number;
field.SetType(UnknownField::TYPE_GROUP);
field.group_ = new UnknownFieldSet;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
return field.group_;
}
void UnknownFieldSet::AddField(const UnknownField& field) {
- if (fields_ == NULL) fields_ = new vector<UnknownField>;
+ if (fields_ == NULL) fields_ = new vector<UnknownField>();
fields_->push_back(field);
fields_->back().DeepCopy();
}
void UnknownFieldSet::DeleteSubrange(int start, int num) {
- GOOGLE_DCHECK(fields_ != NULL);
// Delete the specified fields.
for (int i = 0; i < num; ++i) {
(*fields_)[i + start].Delete();
@@ -168,6 +218,11 @@ void UnknownFieldSet::DeleteSubrange(int start, int num) {
for (int i = 0; i < num; ++i) {
fields_->pop_back();
}
+ if (fields_ && fields_->size() == 0) {
+ // maintain invariant: never hold fields_ if empty.
+ delete fields_;
+ fields_ = NULL;
+ }
}
void UnknownFieldSet::DeleteByNumber(int number) {
@@ -185,13 +240,18 @@ void UnknownFieldSet::DeleteByNumber(int number) {
}
}
fields_->resize(left);
+ if (left == 0) {
+ // maintain invariant: never hold fields_ if empty.
+ delete fields_;
+ fields_ = NULL;
+ }
}
bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
UnknownFieldSet other;
if (internal::WireFormat::SkipMessage(input, &other) &&
input->ConsumedEntireMessage()) {
- MergeFrom(other);
+ MergeFromAndDestroy(&other);
return true;
} else {
return false;
@@ -227,6 +287,22 @@ void UnknownField::Delete() {
}
}
+// Reset all owned ptrs, a special function for performance, to avoid double
+// owning the ptrs, when we merge from a temporary UnknownFieldSet objects.
+void UnknownField::Reset() {
+ switch (type()) {
+ case UnknownField::TYPE_LENGTH_DELIMITED:
+ length_delimited_.string_value_ = NULL;
+ break;
+ case UnknownField::TYPE_GROUP: {
+ group_ = NULL;
+ break;
+ }
+ default:
+ break;
+ }
+}
+
void UnknownField::DeepCopy() {
switch (type()) {
case UnknownField::TYPE_LENGTH_DELIMITED:
@@ -234,8 +310,8 @@ void UnknownField::DeepCopy() {
*length_delimited_.string_value_);
break;
case UnknownField::TYPE_GROUP: {
- UnknownFieldSet* group = new UnknownFieldSet;
- group->MergeFrom(*group_);
+ UnknownFieldSet* group = new UnknownFieldSet();
+ group->InternalMergeFrom(*group_);
group_ = group;
break;
}
diff --git a/src/google/protobuf/unknown_field_set.h b/src/google/protobuf/unknown_field_set.h
index ba202eb6..e8c0a13c 100644
--- a/src/google/protobuf/unknown_field_set.h
+++ b/src/google/protobuf/unknown_field_set.h
@@ -88,6 +88,9 @@ class LIBPROTOBUF_EXPORT UnknownFieldSet {
// Merge the contents of some other UnknownFieldSet with this one.
void MergeFrom(const UnknownFieldSet& other);
+ // Similar to above, but this function will destroy the contents of other.
+ void MergeFromAndDestroy(UnknownFieldSet* other);
+
// Swaps the contents of some other UnknownFieldSet with this one.
inline void Swap(UnknownFieldSet* x);
@@ -141,12 +144,22 @@ class LIBPROTOBUF_EXPORT UnknownFieldSet {
return ParseFromArray(data.data(), static_cast<int>(data.size()));
}
+ static const UnknownFieldSet* default_instance();
private:
-
+ // For InternalMergeFrom
+ friend class UnknownField;
+ // Merges from other UnknownFieldSet. This method assumes, that this object
+ // is newly created and has fields_ == NULL;
+ void InternalMergeFrom(const UnknownFieldSet& other);
void ClearFallback();
- vector<UnknownField>* fields_;
-
+ // fields_ is either NULL, or a pointer to a vector that is *non-empty*. We
+ // never hold the empty vector because we want the 'do we have any unknown
+ // fields' check to be fast, and avoid a cache miss: the UFS instance gets
+ // embedded in the message object, so 'fields_ != NULL' tests a member
+ // variable hot in the cache, without the need to go touch a vector somewhere
+ // else in memory.
+ std::vector<UnknownField>* fields_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UnknownFieldSet);
};
@@ -198,6 +211,10 @@ class LIBPROTOBUF_EXPORT UnknownField {
// If this UnknownField contains a pointer, delete it.
void Delete();
+ // Reset all the underlying pointers to NULL. A special function to be only
+ // used while merging from a temporary UFS.
+ void Reset();
+
// Make a deep copy of any pointers in this UnknownField.
void DeepCopy();
@@ -207,13 +224,16 @@ class LIBPROTOBUF_EXPORT UnknownField {
uint32 number_;
uint32 type_;
+
+ union LengthDelimited {
+ string* string_value_;
+ };
+
union {
uint64 varint_;
uint32 fixed32_;
uint64 fixed64_;
- mutable union {
- string* string_value_;
- } length_delimited_;
+ mutable union LengthDelimited length_delimited_;
UnknownFieldSet* group_;
};
};
@@ -222,13 +242,14 @@ class LIBPROTOBUF_EXPORT UnknownField {
// inline implementations
inline void UnknownFieldSet::Clear() {
- if (fields_ != NULL) {
+ if (fields_) {
ClearFallback();
}
}
inline bool UnknownFieldSet::empty() const {
- return fields_ == NULL || fields_->empty();
+ // Invariant: fields_ is never empty if present.
+ return !fields_;
}
inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
@@ -236,9 +257,10 @@ inline void UnknownFieldSet::Swap(UnknownFieldSet* x) {
}
inline int UnknownFieldSet::field_count() const {
- return (fields_ == NULL) ? 0 : static_cast<int>(fields_->size());
+ return fields_ ? static_cast<int>(fields_->size()) : 0;
}
inline const UnknownField& UnknownFieldSet::field(int index) const {
+ GOOGLE_DCHECK(fields_ != NULL);
return (*fields_)[index];
}
inline UnknownField* UnknownFieldSet::mutable_field(int index) {
diff --git a/src/google/protobuf/unknown_field_set_unittest.cc b/src/google/protobuf/unknown_field_set_unittest.cc
index aaaa2e89..9b02f0b0 100644
--- a/src/google/protobuf/unknown_field_set_unittest.cc
+++ b/src/google/protobuf/unknown_field_set_unittest.cc
@@ -482,6 +482,13 @@ TEST_F(UnknownFieldSetTest, UnknownEnumValue) {
}
}
+TEST_F(UnknownFieldSetTest, SpaceUsedExcludingSelf) {
+ UnknownFieldSet empty;
+ empty.AddVarint(1, 0);
+ EXPECT_EQ(sizeof(vector<UnknownField>) + sizeof(UnknownField),
+ empty.SpaceUsedExcludingSelf());
+}
+
TEST_F(UnknownFieldSetTest, SpaceUsed) {
unittest::TestEmptyMessage empty_message;
diff --git a/src/google/protobuf/wire_format.cc b/src/google/protobuf/wire_format.cc
index 6bdfcd62..f4f02157 100644
--- a/src/google/protobuf/wire_format.cc
+++ b/src/google/protobuf/wire_format.cc
@@ -512,10 +512,15 @@ bool WireFormat::ParseAndMergeField(
int value;
if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
input, &value)) return false;
- const EnumValueDescriptor* enum_value =
- field->enum_type()->FindValueByNumber(value);
- if (enum_value != NULL) {
- message_reflection->AddEnum(message, field, enum_value);
+ if (message->GetDescriptor()->file()->syntax() ==
+ FileDescriptor::SYNTAX_PROTO3) {
+ message_reflection->AddEnumValue(message, field, value);
+ } else {
+ const EnumValueDescriptor* enum_value =
+ field->enum_type()->FindValueByNumber(value);
+ if (enum_value != NULL) {
+ message_reflection->AddEnum(message, field, enum_value);
+ }
}
}
@@ -572,21 +577,31 @@ bool WireFormat::ParseAndMergeField(
int value;
if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
input, &value)) return false;
- const EnumValueDescriptor* enum_value =
- field->enum_type()->FindValueByNumber(value);
- if (enum_value != NULL) {
+ if (message->GetDescriptor()->file()->syntax() ==
+ FileDescriptor::SYNTAX_PROTO3) {
if (field->is_repeated()) {
- message_reflection->AddEnum(message, field, enum_value);
+ message_reflection->AddEnumValue(message, field, value);
} else {
- message_reflection->SetEnum(message, field, enum_value);
+ message_reflection->SetEnumValue(message, field, value);
}
} else {
- // The enum value is not one of the known values. Add it to the
- // UnknownFieldSet.
- int64 sign_extended_value = static_cast<int64>(value);
- message_reflection->MutableUnknownFields(message)
- ->AddVarint(WireFormatLite::GetTagFieldNumber(tag),
- sign_extended_value);
+ const EnumValueDescriptor* enum_value =
+ field->enum_type()->FindValueByNumber(value);
+ if (enum_value != NULL) {
+ if (field->is_repeated()) {
+ message_reflection->AddEnum(message, field, enum_value);
+ } else {
+ message_reflection->SetEnum(message, field, enum_value);
+ }
+ } else {
+ // The enum value is not one of the known values. Add it to the
+ // UnknownFieldSet.
+ int64 sign_extended_value = static_cast<int64>(value);
+ message_reflection->MutableUnknownFields(message)
+ ->AddVarint(
+ WireFormatLite::GetTagFieldNumber(tag),
+ sign_extended_value);
+ }
}
break;
}
diff --git a/src/google/protobuf/wire_format_lite.cc b/src/google/protobuf/wire_format_lite.cc
index 8de82784..50616551 100644
--- a/src/google/protobuf/wire_format_lite.cc
+++ b/src/google/protobuf/wire_format_lite.cc
@@ -55,11 +55,16 @@ const int WireFormatLite::kMessageSetMessageTag;
#endif
+// IBM xlC requires prefixing constants with WireFormatLite::
const int WireFormatLite::kMessageSetItemTagsSize =
- io::CodedOutputStream::StaticVarintSize32<kMessageSetItemStartTag>::value +
- io::CodedOutputStream::StaticVarintSize32<kMessageSetItemEndTag>::value +
- io::CodedOutputStream::StaticVarintSize32<kMessageSetTypeIdTag>::value +
- io::CodedOutputStream::StaticVarintSize32<kMessageSetMessageTag>::value;
+ io::CodedOutputStream::StaticVarintSize32<
+ WireFormatLite::kMessageSetItemStartTag>::value +
+ io::CodedOutputStream::StaticVarintSize32<
+ WireFormatLite::kMessageSetItemEndTag>::value +
+ io::CodedOutputStream::StaticVarintSize32<
+ WireFormatLite::kMessageSetTypeIdTag>::value +
+ io::CodedOutputStream::StaticVarintSize32<
+ WireFormatLite::kMessageSetMessageTag>::value;
const WireFormatLite::CppType
WireFormatLite::kFieldTypeToCppTypeMap[MAX_FIELD_TYPE + 1] = {
@@ -291,7 +296,7 @@ bool WireFormatLite::ReadPackedEnumNoInline(io::CodedInputStream* input,
int, WireFormatLite::TYPE_ENUM>(input, &value)) {
return false;
}
- if (is_valid(value)) {
+ if (is_valid == NULL || is_valid(value)) {
values->Add(value);
}
}
@@ -451,19 +456,24 @@ void WireFormatLite::WriteMessageMaybeToArray(int field_number,
}
}
-bool WireFormatLite::ReadString(io::CodedInputStream* input,
- string* value) {
- // String is for UTF-8 text only
+static inline bool ReadBytesToString(io::CodedInputStream* input,
+ string* value) GOOGLE_ATTRIBUTE_ALWAYS_INLINE;
+static inline bool ReadBytesToString(io::CodedInputStream* input,
+ string* value) {
uint32 length;
- if (!input->ReadVarint32(&length)) return false;
- if (!input->InternalReadStringInline(value, length)) return false;
- return true;
+ return input->ReadVarint32(&length) &&
+ input->InternalReadStringInline(value, length);
}
-bool WireFormatLite::ReadBytes(io::CodedInputStream* input,
- string* value) {
- uint32 length;
- if (!input->ReadVarint32(&length)) return false;
- return input->InternalReadStringInline(value, length);
+
+bool WireFormatLite::ReadBytes(io::CodedInputStream* input, string* value) {
+ return ReadBytesToString(input, value);
+}
+
+bool WireFormatLite::ReadBytes(io::CodedInputStream* input, string** p) {
+ if (*p == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
+ *p = new ::std::string();
+ }
+ return ReadBytesToString(input, *p);
}
} // namespace internal
diff --git a/src/google/protobuf/wire_format_lite.h b/src/google/protobuf/wire_format_lite.h
index 21aa4889..acf88ead 100644
--- a/src/google/protobuf/wire_format_lite.h
+++ b/src/google/protobuf/wire_format_lite.h
@@ -292,13 +292,22 @@ class LIBPROTOBUF_EXPORT WireFormatLite {
static bool ReadPackedPrimitiveNoInline(input, RepeatedField<CType>* value);
// Read a packed enum field. Values for which is_valid() returns false are
- // dropped.
+ // dropped. If is_valid == NULL, no values are dropped.
static bool ReadPackedEnumNoInline(input,
bool (*is_valid)(int),
RepeatedField<int>* value);
- static bool ReadString(input, string* value);
- static bool ReadBytes (input, string* value);
+ // Read a string. ReadString(..., string* value) requires an existing string.
+ static inline bool ReadString(input, string* value);
+ // ReadString(..., string** p) is internal-only, and should only be called
+ // from generated code. It starts by setting *p to "new string"
+ // if *p == &GetEmptyStringAlreadyInited(). It then invokes
+ // ReadString(input, *p). This is useful for reducing code size.
+ static inline bool ReadString(input, string** p);
+ // Analogous to ReadString().
+ static bool ReadBytes(input, string* value);
+ static bool ReadBytes(input, string** p);
+
static inline bool ReadGroup (field_number, input, MessageLite* value);
static inline bool ReadMessage(input, MessageLite* value);
@@ -654,6 +663,19 @@ inline int64 WireFormatLite::ZigZagDecode64(uint64 n) {
return (n >> 1) ^ -static_cast<int64>(n & 1);
}
+// String is for UTF-8 text only, but, even so, ReadString() can simply
+// call ReadBytes().
+
+inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
+ string* value) {
+ return ReadBytes(input, value);
+}
+
+inline bool WireFormatLite::ReadString(io::CodedInputStream* input,
+ string** p) {
+ return ReadBytes(input, p);
+}
+
} // namespace internal
} // namespace protobuf
diff --git a/src/google/protobuf/wire_format_lite_inl.h b/src/google/protobuf/wire_format_lite_inl.h
index 4e8ac9b6..129fc63f 100644
--- a/src/google/protobuf/wire_format_lite_inl.h
+++ b/src/google/protobuf/wire_format_lite_inl.h
@@ -47,6 +47,7 @@
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/arenastring.h>
namespace google {
@@ -380,7 +381,7 @@ inline bool WireFormatLite::ReadPackedFixedSizePrimitive(
#else
values->Reserve(old_entries + new_entries);
CType value;
- for (int i = 0; i < new_entries; ++i) {
+ for (uint32 i = 0; i < new_entries; ++i) {
if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
values->AddAlreadyReserved(value);
}
@@ -426,6 +427,7 @@ bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
}
+
inline bool WireFormatLite::ReadGroup(int field_number,
io::CodedInputStream* input,
MessageLite* value) {
@@ -442,15 +444,12 @@ inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
MessageLite* value) {
uint32 length;
if (!input->ReadVarint32(&length)) return false;
- if (!input->IncrementRecursionDepth()) return false;
- io::CodedInputStream::Limit limit = input->PushLimit(length);
- if (!value->MergePartialFromCodedStream(input)) return false;
+ std::pair<io::CodedInputStream::Limit, int> p =
+ input->IncrementRecursionDepthAndPushLimit(length);
+ if (p.second < 0 || !value->MergePartialFromCodedStream(input)) return false;
// Make sure that parsing stopped when the limit was hit, not at an endgroup
// tag.
- if (!input->ConsumedEntireMessage()) return false;
- input->PopLimit(limit);
- input->DecrementRecursionDepth();
- return true;
+ return input->DecrementRecursionDepthAndPopLimit(p.first);
}
// We name the template parameter something long and extremely unlikely to occur
@@ -483,17 +482,14 @@ inline bool WireFormatLite::ReadMessageNoVirtual(
io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) {
uint32 length;
if (!input->ReadVarint32(&length)) return false;
- if (!input->IncrementRecursionDepth()) return false;
- io::CodedInputStream::Limit limit = input->PushLimit(length);
- if (!value->
+ std::pair<io::CodedInputStream::Limit, int> p =
+ input->IncrementRecursionDepthAndPushLimit(length);
+ if (p.second < 0 || !value->
MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
return false;
// Make sure that parsing stopped when the limit was hit, not at an endgroup
// tag.
- if (!input->ConsumedEntireMessage()) return false;
- input->PopLimit(limit);
- input->DecrementRecursionDepth();
- return true;
+ return input->DecrementRecursionDepthAndPopLimit(p.first);
}
// ===================================================================
diff --git a/src/google/protobuf/wire_format_unittest.cc b/src/google/protobuf/wire_format_unittest.cc
index 81a024b3..a3062a6a 100644
--- a/src/google/protobuf/wire_format_unittest.cc
+++ b/src/google/protobuf/wire_format_unittest.cc
@@ -980,7 +980,8 @@ TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) {
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
ASSERT_EQ(1, errors.size());
EXPECT_TRUE(StartsWith(errors[0],
- "String field 'data' contains invalid UTF-8 data when "
+ "String field 'protobuf_unittest.OneString.data' "
+ "contains invalid UTF-8 data when "
"serializing a protocol buffer. Use the "
"'bytes' type if you intend to send raw bytes."));
#else
@@ -1003,7 +1004,8 @@ TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
ASSERT_EQ(1, errors.size());
EXPECT_TRUE(StartsWith(errors[0],
- "String field 'data' contains invalid UTF-8 data when "
+ "String field 'protobuf_unittest.OneString.data' "
+ "contains invalid UTF-8 data when "
"parsing a protocol buffer. Use the "
"'bytes' type if you intend to send raw bytes."));
diff --git a/vsprojects/extract_includes.bat b/vsprojects/extract_includes.bat
index beab8c4a..587c1a8a 100755
--- a/vsprojects/extract_includes.bat
+++ b/vsprojects/extract_includes.bat
@@ -7,44 +7,72 @@ md include\google\protobuf\compiler
md include\google\protobuf\compiler\cpp
md include\google\protobuf\compiler\java
md include\google\protobuf\compiler\python
-copy ..\src\google\protobuf\stubs\atomicops.h include\google\protobuf\stubs\atomicops.h
-copy ..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.h include\google\protobuf\stubs\atomicops_internals_x86_msvc.h
-copy ..\src\google\protobuf\stubs\common.h include\google\protobuf\stubs\common.h
-copy ..\src\google\protobuf\stubs\once.h include\google\protobuf\stubs\once.h
-copy ..\src\google\protobuf\stubs\platform_macros.h include\google\protobuf\stubs\platform_macros.h
-copy ..\src\google\protobuf\stubs\template_util.h include\google\protobuf\stubs\template_util.h
-copy ..\src\google\protobuf\stubs\type_traits.h include\google\protobuf\stubs\type_traits.h
+copy ..\src\google\protobuf\arena.h include\google\protobuf\arena.h
+copy ..\src\google\protobuf\arenastring.h include\google\protobuf\arenastring.h
+copy ..\src\google\protobuf\compiler\code_generator.h include\google\protobuf\compiler\code_generator.h
+copy ..\src\google\protobuf\compiler\command_line_interface.h include\google\protobuf\compiler\command_line_interface.h
+copy ..\src\google\protobuf\compiler\cpp\cpp_generator.h include\google\protobuf\compiler\cpp\cpp_generator.h
+copy ..\src\google\protobuf\compiler\importer.h include\google\protobuf\compiler\importer.h
+copy ..\src\google\protobuf\compiler\java\java_generator.h include\google\protobuf\compiler\java\java_generator.h
+copy ..\src\google\protobuf\compiler\parser.h include\google\protobuf\compiler\parser.h
+copy ..\src\google\protobuf\compiler\plugin.h include\google\protobuf\compiler\plugin.h
+copy ..\src\google\protobuf\compiler\plugin.pb.h include\google\protobuf\compiler\plugin.pb.h
+copy ..\src\google\protobuf\compiler\python\python_generator.h include\google\protobuf\compiler\python\python_generator.h
+copy ..\src\google\protobuf\descriptor_database.h include\google\protobuf\descriptor_database.h
copy ..\src\google\protobuf\descriptor.h include\google\protobuf\descriptor.h
copy ..\src\google\protobuf\descriptor.pb.h include\google\protobuf\descriptor.pb.h
-copy ..\src\google\protobuf\descriptor_database.h include\google\protobuf\descriptor_database.h
copy ..\src\google\protobuf\dynamic_message.h include\google\protobuf\dynamic_message.h
copy ..\src\google\protobuf\extension_set.h include\google\protobuf\extension_set.h
copy ..\src\google\protobuf\generated_enum_reflection.h include\google\protobuf\generated_enum_reflection.h
-copy ..\src\google\protobuf\generated_message_util.h include\google\protobuf\generated_message_util.h
copy ..\src\google\protobuf\generated_message_reflection.h include\google\protobuf\generated_message_reflection.h
+copy ..\src\google\protobuf\generated_message_util.h include\google\protobuf\generated_message_util.h
+copy ..\src\google\protobuf\io\coded_stream.h include\google\protobuf\io\coded_stream.h
+copy ..\src\google\protobuf\io\gzip_stream.h include\google\protobuf\io\gzip_stream.h
+copy ..\src\google\protobuf\io\printer.h include\google\protobuf\io\printer.h
+copy ..\src\google\protobuf\io\strtod.h include\google\protobuf\io\strtod.h
+copy ..\src\google\protobuf\io\tokenizer.h include\google\protobuf\io\tokenizer.h
+copy ..\src\google\protobuf\io\zero_copy_stream.h include\google\protobuf\io\zero_copy_stream.h
+copy ..\src\google\protobuf\io\zero_copy_stream_impl.h include\google\protobuf\io\zero_copy_stream_impl.h
+copy ..\src\google\protobuf\io\zero_copy_stream_impl_lite.h include\google\protobuf\io\zero_copy_stream_impl_lite.h
+copy ..\src\google\protobuf\map_entry.h include\google\protobuf\map_entry.h
+copy ..\src\google\protobuf\map_field.h include\google\protobuf\map_field.h
+copy ..\src\google\protobuf\map_field_inl.h include\google\protobuf\map_field_inl.h
+copy ..\src\google\protobuf\map.h include\google\protobuf\map.h
+copy ..\src\google\protobuf\map_type_handler.h include\google\protobuf\map_type_handler.h
copy ..\src\google\protobuf\message.h include\google\protobuf\message.h
copy ..\src\google\protobuf\message_lite.h include\google\protobuf\message_lite.h
+copy ..\src\google\protobuf\metadata.h include\google\protobuf\metadata.h
+copy ..\src\google\protobuf\reflection.h include\google\protobuf\reflection.h
copy ..\src\google\protobuf\reflection_ops.h include\google\protobuf\reflection_ops.h
copy ..\src\google\protobuf\repeated_field.h include\google\protobuf\repeated_field.h
+copy ..\src\google\protobuf\repeated_field_reflection.h include\google\protobuf\repeated_field_reflection.h
copy ..\src\google\protobuf\service.h include\google\protobuf\service.h
+copy ..\src\google\protobuf\stubs\atomicops.h include\google\protobuf\stubs\atomicops.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_aix.h include\google\protobuf\stubs\atomicops_internals_aix.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_arm64_gcc.h include\google\protobuf\stubs\atomicops_internals_arm64_gcc.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_arm_gcc.h include\google\protobuf\stubs\atomicops_internals_arm_gcc.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_arm_qnx.h include\google\protobuf\stubs\atomicops_internals_arm_qnx.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_atomicword_compat.h include\google\protobuf\stubs\atomicops_internals_atomicword_compat.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_generic_gcc.h include\google\protobuf\stubs\atomicops_internals_generic_gcc.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_macosx.h include\google\protobuf\stubs\atomicops_internals_macosx.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_mips_gcc.h include\google\protobuf\stubs\atomicops_internals_mips_gcc.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_pnacl.h include\google\protobuf\stubs\atomicops_internals_pnacl.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_solaris.h include\google\protobuf\stubs\atomicops_internals_solaris.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_tsan.h include\google\protobuf\stubs\atomicops_internals_tsan.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_x86_gcc.h include\google\protobuf\stubs\atomicops_internals_x86_gcc.h
+copy ..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.h include\google\protobuf\stubs\atomicops_internals_x86_msvc.h
+copy ..\src\google\protobuf\stubs\atomic_sequence_num.h include\google\protobuf\stubs\atomic_sequence_num.h
+copy ..\src\google\protobuf\stubs\casts.h include\google\protobuf\stubs\casts.h
+copy ..\src\google\protobuf\stubs\common.h include\google\protobuf\stubs\common.h
+copy ..\src\google\protobuf\stubs\fastmem.h include\google\protobuf\stubs\fastmem.h
+copy ..\src\google\protobuf\stubs\once.h include\google\protobuf\stubs\once.h
+copy ..\src\google\protobuf\stubs\platform_macros.h include\google\protobuf\stubs\platform_macros.h
+copy ..\src\google\protobuf\stubs\singleton.h include\google\protobuf\stubs\singleton.h
+copy ..\src\google\protobuf\stubs\stl_util.h include\google\protobuf\stubs\stl_util.h
+copy ..\src\google\protobuf\stubs\template_util.h include\google\protobuf\stubs\template_util.h
+copy ..\src\google\protobuf\stubs\type_traits.h include\google\protobuf\stubs\type_traits.h
copy ..\src\google\protobuf\text_format.h include\google\protobuf\text_format.h
copy ..\src\google\protobuf\unknown_field_set.h include\google\protobuf\unknown_field_set.h
copy ..\src\google\protobuf\wire_format.h include\google\protobuf\wire_format.h
copy ..\src\google\protobuf\wire_format_lite.h include\google\protobuf\wire_format_lite.h
copy ..\src\google\protobuf\wire_format_lite_inl.h include\google\protobuf\wire_format_lite_inl.h
-copy ..\src\google\protobuf\io\coded_stream.h include\google\protobuf\io\coded_stream.h
-copy ..\src\google\protobuf\io\gzip_stream.h include\google\protobuf\io\gzip_stream.h
-copy ..\src\google\protobuf\io\printer.h include\google\protobuf\io\printer.h
-copy ..\src\google\protobuf\io\strtod.h include\google\protobuf\io\strtod.h
-copy ..\src\google\protobuf\io\tokenizer.h include\google\protobuf\io\tokenizer.h
-copy ..\src\google\protobuf\io\zero_copy_stream.h include\google\protobuf\io\zero_copy_stream.h
-copy ..\src\google\protobuf\io\zero_copy_stream_impl.h include\google\protobuf\io\zero_copy_stream_impl.h
-copy ..\src\google\protobuf\io\zero_copy_stream_impl_lite.h include\google\protobuf\io\zero_copy_stream_impl_lite.h
-copy ..\src\google\protobuf\compiler\code_generator.h include\google\protobuf\compiler\code_generator.h
-copy ..\src\google\protobuf\compiler\command_line_interface.h include\google\protobuf\compiler\command_line_interface.h
-copy ..\src\google\protobuf\compiler\importer.h include\google\protobuf\compiler\importer.h
-copy ..\src\google\protobuf\compiler\parser.h include\google\protobuf\compiler\parser.h
-copy ..\src\google\protobuf\compiler\cpp\cpp_generator.h include\google\protobuf\compiler\cpp\cpp_generator.h
-copy ..\src\google\protobuf\compiler\java\java_generator.h include\google\protobuf\compiler\java\java_generator.h
-copy ..\src\google\protobuf\compiler\python\python_generator.h include\google\protobuf\compiler\python\python_generator.h
-copy ..\src\google\protobuf\compiler\plugin.h include\google\protobuf\compiler\plugin.h
diff --git a/vsprojects/libprotobuf-lite.vcproj b/vsprojects/libprotobuf-lite.vcproj
index 06b15988..09eca4d8 100644
--- a/vsprojects/libprotobuf-lite.vcproj
+++ b/vsprojects/libprotobuf-lite.vcproj
@@ -18,7 +18,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -81,7 +81,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -248,7 +248,11 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
- RelativePath="..\src\google\protobuf\io\coded_stream.cc"
+ RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_gcc.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.cc"
>
</File>
<File
@@ -256,23 +260,31 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\extension_set.cc"
+ RelativePath="..\src\google\protobuf\stubs\once.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\generated_message_util.cc"
+ RelativePath="..\src\google\protobuf\stubs\stringprintf.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\message_lite.cc"
+ RelativePath="..\src\google\protobuf\arena.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\once.cc"
+ RelativePath="..\src\google\protobuf\arenastring.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.cc"
+ RelativePath="..\src\google\protobuf\extension_set.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\generated_message_util.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\message_lite.cc"
>
</File>
<File
@@ -284,15 +296,15 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\zero_copy_stream.cc"
+ RelativePath="..\src\google\protobuf\io\coded_stream.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc"
+ RelativePath="..\src\google\protobuf\io\zero_copy_stream.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\stringprintf.cc"
+ RelativePath="..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc"
>
</File>
</Filter>
diff --git a/vsprojects/libprotobuf.vcproj b/vsprojects/libprotobuf.vcproj
index 1a488f7d..095d3948 100644
--- a/vsprojects/libprotobuf.vcproj
+++ b/vsprojects/libprotobuf.vcproj
@@ -18,7 +18,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -81,7 +81,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -328,7 +328,11 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
- RelativePath="..\src\google\protobuf\io\coded_stream.cc"
+ RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_gcc.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.cc"
>
</File>
<File
@@ -336,19 +340,19 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\descriptor.cc"
+ RelativePath="..\src\google\protobuf\stubs\once.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\descriptor.pb.cc"
+ RelativePath="..\src\google\protobuf\stubs\stringprintf.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\descriptor_database.cc"
+ RelativePath="..\src\google\protobuf\arena.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\dynamic_message.cc"
+ RelativePath="..\src\google\protobuf\arenastring.cc"
>
</File>
<File
@@ -356,83 +360,99 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\extension_set_heavy.cc"
+ RelativePath="..\src\google\protobuf\generated_message_util.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\generated_message_reflection.cc"
+ RelativePath="..\src\google\protobuf\map_field.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\generated_message_util.cc"
+ RelativePath="..\src\google\protobuf\message_lite.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\gzip_stream.cc"
+ RelativePath="..\src\google\protobuf\repeated_field.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\strtod.cc"
+ RelativePath="..\src\google\protobuf\wire_format_lite.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\importer.cc"
+ RelativePath="..\src\google\protobuf\io\coded_stream.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\message.cc"
+ RelativePath="..\src\google\protobuf\io\zero_copy_stream.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\message_lite.cc"
+ RelativePath="..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\once.cc"
+ RelativePath="..\src\google\protobuf\stubs\strutil.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\atomicops_internals_x86_msvc.cc"
+ RelativePath="..\src\google\protobuf\stubs\strutil.h"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\parser.cc"
+ RelativePath="..\src\google\protobuf\stubs\substitute.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\printer.cc"
+ RelativePath="..\src\google\protobuf\stubs\substitute.h"
>
</File>
<File
- RelativePath="..\src\google\protobuf\reflection_ops.cc"
+ RelativePath="..\src\google\protobuf\stubs\structurally_valid.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\repeated_field.cc"
+ RelativePath="..\src\google\protobuf\descriptor.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\service.cc"
+ RelativePath="..\src\google\protobuf\descriptor_database.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\structurally_valid.cc"
+ RelativePath="..\src\google\protobuf\descriptor.pb.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\strutil.cc"
+ RelativePath="..\src\google\protobuf\dynamic_message.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\substitute.cc"
+ RelativePath="..\src\google\protobuf\extension_set_heavy.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\text_format.cc"
+ RelativePath="..\src\google\protobuf\generated_message_reflection.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\tokenizer.cc"
+ RelativePath="..\src\google\protobuf\message.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\reflection_internal.h"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\reflection_ops.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\service.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\text_format.cc"
>
</File>
<File
@@ -444,11 +464,19 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\wire_format_lite.cc"
+ RelativePath="..\src\google\protobuf\io\gzip_stream.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\zero_copy_stream.cc"
+ RelativePath="..\src\google\protobuf\io\printer.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\io\strtod.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\io\tokenizer.cc"
>
</File>
<File
@@ -456,11 +484,11 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\zero_copy_stream_impl_lite.cc"
+ RelativePath="..\src\google\protobuf\compiler\importer.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\stringprintf.cc"
+ RelativePath="..\src\google\protobuf\compiler\parser.cc"
>
</File>
</Filter>
diff --git a/vsprojects/libprotoc.vcproj b/vsprojects/libprotoc.vcproj
index ccbc0b36..45a5936f 100644
--- a/vsprojects/libprotoc.vcproj
+++ b/vsprojects/libprotoc.vcproj
@@ -18,7 +18,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -81,7 +81,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="4"
>
<Tool
@@ -324,22 +324,6 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\subprocess.cc"
- >
- </File>
- <File
- RelativePath="..\src\google\protobuf\compiler\zip_writer.cc"
- >
- </File>
- <File
- RelativePath="..\src\google\protobuf\compiler\plugin.cc"
- >
- </File>
- <File
- RelativePath="..\src\google\protobuf\compiler\plugin.pb.cc"
- >
- </File>
- <File
RelativePath="..\src\google\protobuf\compiler\cpp\cpp_enum.cc"
>
</File>
@@ -368,6 +352,10 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\compiler\cpp\cpp_map_field.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\compiler\cpp\cpp_message.cc"
>
</File>
@@ -392,6 +380,10 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\compiler\java\java_doc_comment.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\compiler\java\java_enum.cc"
>
</File>
@@ -428,6 +420,10 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\compiler\java\java_map_field.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\compiler\java\java_message.cc"
>
</File>
@@ -456,9 +452,65 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_enum.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_enum_field.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_extension.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_field.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_file.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_generator.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_helpers.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_message.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_message_field.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\javanano\javanano_primitive_field.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\plugin.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\plugin.pb.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\compiler\python\python_generator.cc"
>
</File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\subprocess.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\zip_writer.cc"
+ >
+ </File>
</Filter>
</Files>
<Globals>
diff --git a/vsprojects/lite-test.vcproj b/vsprojects/lite-test.vcproj
index 0314b36d..bb338092 100644
--- a/vsprojects/lite-test.vcproj
+++ b/vsprojects/lite-test.vcproj
@@ -19,7 +19,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
@@ -96,7 +96,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
diff --git a/vsprojects/protoc.vcproj b/vsprojects/protoc.vcproj
index 17e8474e..4359e238 100644
--- a/vsprojects/protoc.vcproj
+++ b/vsprojects/protoc.vcproj
@@ -18,7 +18,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
@@ -91,7 +91,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
diff --git a/vsprojects/readme.txt b/vsprojects/readme.txt
index 288345ad..ad342497 100644
--- a/vsprojects/readme.txt
+++ b/vsprojects/readme.txt
@@ -6,6 +6,13 @@ accessible to those who primarily work with MSVC.
Compiling and Installing
========================
+0) Check whether a gtest directory exists in the upper level directory. If
+ you checkout the code from github via "git clone", this gtest directory
+ won't exist and you won't be able to build the tests described below. To
+ avoid this problem consider downloading one of the release tar balls which
+ contains gtest already and copying the gest directory from there to your
+ protobuf directory:
+ https://github.com/google/protobuf/releases
1) Open protobuf.sln in Microsoft Visual Studio.
2) Choose "Debug" or "Release" configuration as desired.*
3) From the Build menu, choose "Build Solution". Wait for compiling to finish.
diff --git a/vsprojects/test_plugin.vcproj b/vsprojects/test_plugin.vcproj
index 2dcf2ad6..549f9503 100755
--- a/vsprojects/test_plugin.vcproj
+++ b/vsprojects/test_plugin.vcproj
@@ -19,7 +19,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
@@ -96,7 +96,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
diff --git a/vsprojects/tests.vcproj b/vsprojects/tests.vcproj
index fb815b5a..23c81f0e 100644
--- a/vsprojects/tests.vcproj
+++ b/vsprojects/tests.vcproj
@@ -19,7 +19,7 @@
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
- IntermediateDirectory="Debug"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
@@ -96,7 +96,7 @@
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
- IntermediateDirectory="Release"
+ IntermediateDirectory="$(OutDir)\$(ProjectName)"
ConfigurationType="1"
>
<Tool
@@ -247,23 +247,23 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
- RelativePath="..\src\google\protobuf\io\coded_stream_unittest.cc"
+ RelativePath="..\src\google\protobuf\arenastring_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\command_line_interface_unittest.cc"
+ RelativePath="..\src\google\protobuf\arena_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\mock_code_generator.cc"
+ RelativePath="..\src\google\protobuf\compiler\command_line_interface_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\common_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\cpp\cpp_bootstrap_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\cpp\cpp_bootstrap_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\cpp\cpp_plugin_unittest.cc"
>
</File>
<File
@@ -271,23 +271,23 @@
>
</File>
<File
- RelativePath=".\google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ RelativePath="..\src\google\protobuf\compiler\importer_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\cpp\cpp_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\java\java_doc_comment_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\cpp\cpp_plugin_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\java\java_plugin_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\java\java_plugin_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\mock_code_generator.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\java\java_doc_comment_unittest.cc"
+ RelativePath="..\src\google\protobuf\compiler\parser_unittest.cc"
>
</File>
<File
@@ -303,6 +303,10 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\drop_unknown_fields_test.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\dynamic_message_unittest.cc"
>
</File>
@@ -311,19 +315,35 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\testing\file.cc"
+ RelativePath="..\src\google\protobuf\generated_message_reflection_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\generated_message_reflection_unittest.cc"
+ RelativePath="..\src\google\protobuf\io\coded_stream_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\testing\googletest.cc"
+ RelativePath="..\src\google\protobuf\io\printer_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\importer_unittest.cc"
+ RelativePath="..\src\google\protobuf\io\tokenizer_unittest.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\io\zero_copy_stream_unittest.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\map_field_test.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\map_test.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\map_test_util.cc"
>
</File>
<File
@@ -331,15 +351,15 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\once_unittest.cc"
+ RelativePath="..\src\google\protobuf\no_field_presence_test.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\compiler\parser_unittest.cc"
+ RelativePath="..\src\google\protobuf\preserve_unknown_enum_test.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\printer_unittest.cc"
+ RelativePath="..\src\google\protobuf\proto3_arena_unittest.cc"
>
</File>
<File
@@ -347,15 +367,19 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\repeated_field_reflection_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\repeated_field_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\repeated_field_reflection_unittest.cc"
+ RelativePath="..\src\google\protobuf\stubs\common_unittest.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\structurally_valid_unittest.cc"
+ RelativePath="..\src\google\protobuf\stubs\once_unittest.cc"
>
</File>
<File
@@ -363,6 +387,14 @@
>
</File>
<File
+ RelativePath="..\src\google\protobuf\stubs\structurally_valid_unittest.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\stubs\strutil_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\src\google\protobuf\stubs\template_util_unittest.cc"
>
</File>
@@ -371,7 +403,11 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\stubs\strutil_unittest.cc"
+ RelativePath="..\src\google\protobuf\testing\file.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\testing\googletest.cc"
>
</File>
<File
@@ -383,11 +419,31 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\tokenizer_unittest.cc"
+ RelativePath="..\src\google\protobuf\unknown_field_set_unittest.cc"
>
</File>
<File
- RelativePath=".\google\protobuf\unittest.pb.cc"
+ RelativePath="..\src\google\protobuf\wire_format_unittest.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\map_lite_unittest.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\map_proto2_unittest.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\map_unittest.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\unittest_arena.pb.cc"
>
</File>
<File
@@ -395,14 +451,30 @@
>
</File>
<File
+ RelativePath=".\google\protobuf\unittest_drop_unknown_fields.pb.cc"
+ >
+ </File>
+ <File
RelativePath=".\google\protobuf\unittest_embed_optimize_for.pb.cc"
>
</File>
<File
+ RelativePath=".\google\protobuf\unittest_empty.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\unittest_import_lite.pb.cc"
+ >
+ </File>
+ <File
RelativePath=".\google\protobuf\unittest_import.pb.cc"
>
</File>
<File
+ RelativePath=".\google\protobuf\unittest_import_public_lite.pb.cc"
+ >
+ </File>
+ <File
RelativePath=".\google\protobuf\unittest_import_public.pb.cc"
>
</File>
@@ -411,11 +483,23 @@
>
</File>
<File
+ RelativePath=".\google\protobuf\unittest_lite.pb.cc"
+ >
+ </File>
+ <File
RelativePath=".\google\protobuf\unittest_mset.pb.cc"
>
</File>
<File
- RelativePath=".\google\protobuf\unittest_optimize_for.pb.cc"
+ RelativePath=".\google\protobuf\unittest_no_arena_import.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\unittest_no_arena.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\unittest_no_field_presence.pb.cc"
>
</File>
<File
@@ -423,29 +507,33 @@
>
</File>
<File
- RelativePath="..\src\google\protobuf\unknown_field_set_unittest.cc"
+ RelativePath=".\google\protobuf\unittest_optimize_for.pb.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\wire_format_unittest.cc"
+ RelativePath=".\google\protobuf\unittest.pb.cc"
>
</File>
<File
- RelativePath="..\src\google\protobuf\io\zero_copy_stream_unittest.cc"
+ RelativePath=".\google\protobuf\unittest_preserve_unknown_enum.pb.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\google\protobuf\unittest_proto3_arena.pb.cc"
>
</File>
</Filter>
<File
- RelativePath="..\src\google\protobuf\compiler\cpp\cpp_test_bad_identifiers.proto"
+ RelativePath="..\src\google\protobuf\map_lite_unittest.proto"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCustomBuildTool"
- Description="Generating cpp_test_bad_identifiers.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto&#x0D;&#x0A;"
- Outputs="google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.h;google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ Description="Generating map_lite_unittest.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/map_lite_unittest.proto"
+ Outputs="google\protobuf\map_lite_unittest.pb.h;google\protobuf\map_lite_unittest.pb.cc"
/>
</FileConfiguration>
<FileConfiguration
@@ -453,9 +541,57 @@
>
<Tool
Name="VCCustomBuildTool"
- Description="Generating cpp_test_bad_identifiers.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto&#x0D;&#x0A;"
- Outputs="google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.h;google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ Description="Generating map_lite_unittest.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/map_lite_unittest.proto"
+ Outputs="google\protobuf\map_lite_unittest.pb.h;google\protobuf\map_lite_unittest.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\map_proto2_unittest.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating map_proto2_unittest.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/map_proto2_unittest.proto"
+ Outputs="google\protobuf\map_proto2_unittest.pb.h;google\protobuf\map_proto2_unittest.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating map_proto2_unittest.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/map_proto2_unittest.proto"
+ Outputs="google\protobuf\map_proto2_unittest.pb.h;google\protobuf\map_proto2_unittest.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\map_unittest.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating map_unittest.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/map_unittest.proto"
+ Outputs="google\protobuf\map_unittest.pb.h;google\protobuf\map_unittest.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating map_unittest.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/map_unittest.proto"
+ Outputs="google\protobuf\map_unittest.pb.h;google\protobuf\map_unittest.pb.cc"
/>
</FileConfiguration>
</File>
@@ -468,7 +604,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest.proto"
Outputs="google\protobuf\unittest.pb.h;google\protobuf\unittest.pb.cc"
/>
</FileConfiguration>
@@ -478,12 +614,36 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest.proto"
Outputs="google\protobuf\unittest.pb.h;google\protobuf\unittest.pb.cc"
/>
</FileConfiguration>
</File>
<File
+ RelativePath="..\src\google\protobuf\unittest_arena.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_arena.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_arena.proto"
+ Outputs="google\protobuf\unittest_arena.pb.h;google\protobuf\unittest_arena.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_arena.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_arena.proto"
+ Outputs="google\protobuf\unittest_arena.pb.h;google\protobuf\unittest_arena.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\src\google\protobuf\unittest_custom_options.proto"
>
<FileConfiguration
@@ -492,7 +652,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_custom_options.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_custom_options.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_custom_options.proto"
Outputs="google\protobuf\unittest_custom_options.pb.h;google\protobuf\unittest_custom_options.pb.cc"
/>
</FileConfiguration>
@@ -502,12 +662,36 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_custom_options.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_custom_options.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_custom_options.proto"
Outputs="google\protobuf\unittest_custom_options.pb.h;google\protobuf\unittest_custom_options.pb.cc"
/>
</FileConfiguration>
</File>
<File
+ RelativePath="..\src\google\protobuf\unittest_drop_unknown_fields.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_drop_unknown_fields.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_drop_unknown_fields.proto"
+ Outputs="google\protobuf\unittest_drop_unknown_fields.pb.h;google\protobuf\unittest_drop_unknown_fields.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_drop_unknown_fields.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_drop_unknown_fields.proto"
+ Outputs="google\protobuf\unittest_drop_unknown_fields.pb.h;google\protobuf\unittest_drop_unknown_fields.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\src\google\protobuf\unittest_embed_optimize_for.proto"
>
<FileConfiguration
@@ -516,7 +700,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_embed_optimize_for.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_embed_optimize_for.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_embed_optimize_for.proto"
Outputs="google\protobuf\unittest_embed_optimize_for.pb.h;google\protobuf\unittest_embed_optimize_for.pb.cc"
/>
</FileConfiguration>
@@ -526,12 +710,60 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_embed_optimize_for.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_embed_optimize_for.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_embed_optimize_for.proto"
Outputs="google\protobuf\unittest_embed_optimize_for.pb.h;google\protobuf\unittest_embed_optimize_for.pb.cc"
/>
</FileConfiguration>
</File>
<File
+ RelativePath="..\src\google\protobuf\unittest_empty.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_empty.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_empty.proto"
+ Outputs="google\protobuf\unittest_empty.pb.h;google\protobuf\unittest_empty.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_empty.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_empty.proto"
+ Outputs="google\protobuf\unittest_empty.pb.h;google\protobuf\unittest_empty.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_import_lite.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_import_lite.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_lite.proto"
+ Outputs="google\protobuf\unittest_import_lite.pb.h;google\protobuf\unittest_import_lite.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_import_lite.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_lite.proto"
+ Outputs="google\protobuf\unittest_import_lite.pb.h;google\protobuf\unittest_import_lite.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\src\google\protobuf\unittest_import.proto"
>
<FileConfiguration
@@ -540,7 +772,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_import.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import.proto"
Outputs="google\protobuf\unittest_import.pb.h;google\protobuf\unittest_import.pb.cc"
/>
</FileConfiguration>
@@ -550,12 +782,36 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_import.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import.proto"
Outputs="google\protobuf\unittest_import.pb.h;google\protobuf\unittest_import.pb.cc"
/>
</FileConfiguration>
</File>
<File
+ RelativePath="..\src\google\protobuf\unittest_import_public_lite.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_import_public_lite.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public_lite.proto"
+ Outputs="google\protobuf\unittest_import_public_lite.pb.h;google\protobuf\unittest_import_public_lite.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_import_public_lite.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public_lite.proto"
+ Outputs="google\protobuf\unittest_import_public_lite.pb.h;google\protobuf\unittest_import_public_lite.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\src\google\protobuf\unittest_import_public.proto"
>
<FileConfiguration
@@ -564,7 +820,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_import_public.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public.proto"
Outputs="google\protobuf\unittest_import_public.pb.h;google\protobuf\unittest_import_public.pb.cc"
/>
</FileConfiguration>
@@ -574,7 +830,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_import_public.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_import_public.proto"
Outputs="google\protobuf\unittest_import_public.pb.h;google\protobuf\unittest_import_public.pb.cc"
/>
</FileConfiguration>
@@ -588,7 +844,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_lite_imports_nonlite.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite_imports_nonlite.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite_imports_nonlite.proto"
Outputs="google\protobuf\unittest_lite_imports_nonlite.pb.h;google\protobuf\unittest_lite_imports_nonlite.pb.cc"
/>
</FileConfiguration>
@@ -598,12 +854,36 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_lite_imports_nonlite.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite_imports_nonlite.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite_imports_nonlite.proto"
Outputs="google\protobuf\unittest_lite_imports_nonlite.pb.h;google\protobuf\unittest_lite_imports_nonlite.pb.cc"
/>
</FileConfiguration>
</File>
<File
+ RelativePath="..\src\google\protobuf\unittest_lite.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_lite.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite.proto"
+ Outputs="google\protobuf\unittest_lite.pb.h;google\protobuf\unittest_lite.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_lite.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_lite.proto"
+ Outputs="google\protobuf\unittest_lite.pb.h;google\protobuf\unittest_lite.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
RelativePath="..\src\google\protobuf\unittest_mset.proto"
>
<FileConfiguration
@@ -612,7 +892,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_mset.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_mset.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_mset.proto"
Outputs="google\protobuf\unittest_mset.pb.h;google\protobuf\unittest_mset.pb.cc"
/>
</FileConfiguration>
@@ -622,22 +902,22 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_mset.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_mset.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_mset.proto"
Outputs="google\protobuf\unittest_mset.pb.h;google\protobuf\unittest_mset.pb.cc"
/>
</FileConfiguration>
</File>
<File
- RelativePath="..\src\google\protobuf\unittest_optimize_for.proto"
+ RelativePath="..\src\google\protobuf\unittest_no_arena_import.proto"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCustomBuildTool"
- Description="Generating unittest_optimize_for.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_optimize_for.proto&#x0D;&#x0A;"
- Outputs="google\protobuf\unittest_optimize_for.pb.h;google\protobuf\unittest_optimize_for.pb.cc"
+ Description="Generating unittest_no_arena_import.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_arena_import.proto"
+ Outputs="google\protobuf\unittest_no_arena_import.pb.h;google\protobuf\unittest_no_arena_import.pb.cc"
/>
</FileConfiguration>
<FileConfiguration
@@ -645,9 +925,57 @@
>
<Tool
Name="VCCustomBuildTool"
- Description="Generating unittest_optimize_for.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_optimize_for.proto&#x0D;&#x0A;"
- Outputs="google\protobuf\unittest_optimize_for.pb.h;google\protobuf\unittest_optimize_for.pb.cc"
+ Description="Generating unittest_no_arena_import.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_arena_import.proto"
+ Outputs="google\protobuf\unittest_no_arena_import.pb.h;google\protobuf\unittest_no_arena_import.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_no_arena.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_no_arena.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_arena.proto"
+ Outputs="google\protobuf\unittest_no_arena.pb.h;google\protobuf\unittest_no_arena.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_no_arena.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_arena.proto"
+ Outputs="google\protobuf\unittest_no_arena.pb.h;google\protobuf\unittest_no_arena.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_no_field_presence.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_no_field_presence.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_field_presence.proto"
+ Outputs="google\protobuf\unittest_no_field_presence.pb.h;google\protobuf\unittest_no_field_presence.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_no_field_presence.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_field_presence.proto"
+ Outputs="google\protobuf\unittest_no_field_presence.pb.h;google\protobuf\unittest_no_field_presence.pb.cc"
/>
</FileConfiguration>
</File>
@@ -660,7 +988,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_no_generic_services.pb.{h,cc}..."
- CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_generic_services.proto&#x0D;&#x0A;"
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_generic_services.proto"
Outputs="google\protobuf\unittest_no_generic_services.pb.h;google\protobuf\unittest_no_generic_services.pb.cc"
/>
</FileConfiguration>
@@ -670,11 +998,107 @@
<Tool
Name="VCCustomBuildTool"
Description="Generating unittest_no_generic_services.pb.{h,cc}..."
- CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_generic_services.proto&#x0D;&#x0A;"
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_no_generic_services.proto"
Outputs="google\protobuf\unittest_no_generic_services.pb.h;google\protobuf\unittest_no_generic_services.pb.cc"
/>
</FileConfiguration>
</File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_optimize_for.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_optimize_for.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_optimize_for.proto"
+ Outputs="google\protobuf\unittest_optimize_for.pb.h;google\protobuf\unittest_optimize_for.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_optimize_for.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_optimize_for.proto"
+ Outputs="google\protobuf\unittest_optimize_for.pb.h;google\protobuf\unittest_optimize_for.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_preserve_unknown_enum.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_preserve_unknown_enum.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_preserve_unknown_enum.proto"
+ Outputs="google\protobuf\unittest_preserve_unknown_enum.pb.h;google\protobuf\unittest_preserve_unknown_enum.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_preserve_unknown_enum.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_preserve_unknown_enum.proto"
+ Outputs="google\protobuf\unittest_preserve_unknown_enum.pb.h;google\protobuf\unittest_preserve_unknown_enum.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\unittest_proto3_arena.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_proto3_arena.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_proto3_arena.proto"
+ Outputs="google\protobuf\unittest_proto3_arena.pb.h;google\protobuf\unittest_proto3_arena.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating unittest_proto3_arena.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/unittest_proto3_arena.proto"
+ Outputs="google\protobuf\unittest_proto3_arena.pb.h;google\protobuf\unittest_proto3_arena.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\src\google\protobuf\compiler\cpp\cpp_test_bad_identifiers.proto"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating cpp_test_bad_identifiers.pb.{h,cc}..."
+ CommandLine="Debug\protoc -I../src --cpp_out=. ../src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto"
+ Outputs="google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.h;google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCustomBuildTool"
+ Description="Generating cpp_test_bad_identifiers.pb.{h,cc}..."
+ CommandLine="Release\protoc -I../src --cpp_out=. ../src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto"
+ Outputs="google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.h;google\protobuf\compiler\cpp\cpp_test_bad_identifiers.pb.cc"
+ />
+ </FileConfiguration>
+ </File>
</Files>
<Globals>
</Globals>