From 8e3e09937e1514323cad74b000450c6bd65c04e7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 18 Apr 2024 09:22:50 -0700 Subject: [PATCH] Refactored the Java generator into separate packages and targets. PiperOrigin-RevId: 626057584 --- pkg/BUILD.bazel | 1 + src/google/protobuf/compiler/BUILD.bazel | 1 + src/google/protobuf/compiler/java/BUILD.bazel | 131 ++++---- src/google/protobuf/compiler/java/context.cc | 2 +- src/google/protobuf/compiler/java/field.cc | 294 ------------------ src/google/protobuf/compiler/java/field.h | 169 ---------- .../protobuf/compiler/java/field_common.cc | 85 +++++ .../protobuf/compiler/java/field_common.h | 44 +++ src/google/protobuf/compiler/java/file.cc | 65 ++-- src/google/protobuf/compiler/java/file.h | 2 +- .../protobuf/compiler/java/generator_common.h | 89 ++++++ .../compiler/java/generator_factory.cc | 63 ---- .../compiler/java/generator_factory.h | 147 ++++++--- .../compiler/java/immutable/BUILD.bazel | 131 ++++++++ .../compiler/java/{ => immutable}/enum.cc | 12 +- .../compiler/java/{ => immutable}/enum.h | 17 +- .../java/{ => immutable}/enum_field.cc | 3 +- .../java/{ => immutable}/enum_field.h | 4 +- .../java/{ => immutable}/extension.cc | 5 +- .../compiler/java/{ => immutable}/extension.h | 29 +- .../compiler/java/immutable/field_generator.h | 54 ++++ .../java/immutable/generator_factory.cc | 67 ++++ .../java/immutable/generator_factory.h | 19 ++ .../java/immutable/make_field_generators.cc | 114 +++++++ .../java/immutable/make_field_generators.h | 29 ++ .../java/{ => immutable}/map_field.cc | 4 +- .../compiler/java/{ => immutable}/map_field.h | 5 +- .../compiler/java/{ => immutable}/message.cc | 27 +- .../compiler/java/{ => immutable}/message.h | 45 +-- .../java/{ => immutable}/message_builder.cc | 15 +- .../java/{ => immutable}/message_builder.h | 5 +- .../java/{ => immutable}/message_field.cc | 4 +- .../java/{ => immutable}/message_field.h | 3 +- .../java/{ => immutable}/primitive_field.cc | 3 +- .../java/{ => immutable}/primitive_field.h | 3 +- .../compiler/java/{ => immutable}/service.cc | 8 +- .../compiler/java/{ => immutable}/service.h | 17 +- .../java/{ => immutable}/string_field.cc | 3 +- .../java/{ => immutable}/string_field.h | 3 +- .../protobuf/compiler/java/lite/BUILD.bazel | 77 +++++ .../java/{enum_lite.cc => lite/enum.cc} | 2 +- .../java/{enum_lite.h => lite/enum.h} | 5 +- .../enum_field.cc} | 3 +- .../{enum_field_lite.h => lite/enum_field.h} | 3 +- .../{extension_lite.cc => lite/extension.cc} | 2 +- .../{extension_lite.h => lite/extension.h} | 3 +- .../compiler/java/lite/field_generator.h | 38 +++ .../compiler/java/lite/generator_factory.cc | 70 +++++ .../compiler/java/lite/generator_factory.h | 19 ++ .../java/lite/make_field_generators.cc | 110 +++++++ .../java/lite/make_field_generators.h | 32 ++ .../{map_field_lite.cc => lite/map_field.cc} | 3 +- .../{map_field_lite.h => lite/map_field.h} | 8 +- .../java/{message_lite.cc => lite/message.cc} | 17 +- .../java/{message_lite.h => lite/message.h} | 12 +- .../message_builder.cc} | 16 +- .../message_builder.h} | 3 +- .../message_field.cc} | 3 +- .../message_field.h} | 6 +- .../primitive_field.cc} | 3 +- .../primitive_field.h} | 6 +- .../string_field.cc} | 3 +- .../string_field.h} | 6 +- .../compiler/java/message_serialization.h | 14 +- 64 files changed, 1347 insertions(+), 839 deletions(-) delete mode 100644 src/google/protobuf/compiler/java/field.cc delete mode 100644 src/google/protobuf/compiler/java/field.h create mode 100644 src/google/protobuf/compiler/java/field_common.cc create mode 100644 src/google/protobuf/compiler/java/field_common.h create mode 100644 src/google/protobuf/compiler/java/generator_common.h delete mode 100644 src/google/protobuf/compiler/java/generator_factory.cc create mode 100644 src/google/protobuf/compiler/java/immutable/BUILD.bazel rename src/google/protobuf/compiler/java/{ => immutable}/enum.cc (97%) rename src/google/protobuf/compiler/java/{ => immutable}/enum.h (74%) rename src/google/protobuf/compiler/java/{ => immutable}/enum_field.cc (99%) rename src/google/protobuf/compiler/java/{ => immutable}/enum_field.h (97%) rename src/google/protobuf/compiler/java/{ => immutable}/extension.cc (96%) rename src/google/protobuf/compiler/java/{ => immutable}/extension.h (63%) create mode 100644 src/google/protobuf/compiler/java/immutable/field_generator.h create mode 100644 src/google/protobuf/compiler/java/immutable/generator_factory.cc create mode 100644 src/google/protobuf/compiler/java/immutable/generator_factory.h create mode 100644 src/google/protobuf/compiler/java/immutable/make_field_generators.cc create mode 100644 src/google/protobuf/compiler/java/immutable/make_field_generators.h rename src/google/protobuf/compiler/java/{ => immutable}/map_field.cc (99%) rename src/google/protobuf/compiler/java/{ => immutable}/map_field.h (93%) rename src/google/protobuf/compiler/java/{ => immutable}/message.cc (98%) rename src/google/protobuf/compiler/java/{ => immutable}/message.h (65%) rename src/google/protobuf/compiler/java/{ => immutable}/message_builder.cc (97%) rename src/google/protobuf/compiler/java/{ => immutable}/message_builder.h (94%) rename src/google/protobuf/compiler/java/{ => immutable}/message_field.cc (99%) rename src/google/protobuf/compiler/java/{ => immutable}/message_field.h (98%) rename src/google/protobuf/compiler/java/{ => immutable}/primitive_field.cc (99%) rename src/google/protobuf/compiler/java/{ => immutable}/primitive_field.h (98%) rename src/google/protobuf/compiler/java/{ => immutable}/service.cc (98%) rename src/google/protobuf/compiler/java/{ => immutable}/service.h (87%) rename src/google/protobuf/compiler/java/{ => immutable}/string_field.cc (99%) rename src/google/protobuf/compiler/java/{ => immutable}/string_field.h (98%) create mode 100644 src/google/protobuf/compiler/java/lite/BUILD.bazel rename src/google/protobuf/compiler/java/{enum_lite.cc => lite/enum.cc} (99%) rename src/google/protobuf/compiler/java/{enum_lite.h => lite/enum.h} (92%) rename src/google/protobuf/compiler/java/{enum_field_lite.cc => lite/enum_field.cc} (99%) rename src/google/protobuf/compiler/java/{enum_field_lite.h => lite/enum_field.h} (97%) rename src/google/protobuf/compiler/java/{extension_lite.cc => lite/extension.cc} (98%) rename src/google/protobuf/compiler/java/{extension_lite.h => lite/extension.h} (94%) create mode 100644 src/google/protobuf/compiler/java/lite/field_generator.h create mode 100644 src/google/protobuf/compiler/java/lite/generator_factory.cc create mode 100644 src/google/protobuf/compiler/java/lite/generator_factory.h create mode 100644 src/google/protobuf/compiler/java/lite/make_field_generators.cc create mode 100644 src/google/protobuf/compiler/java/lite/make_field_generators.h rename src/google/protobuf/compiler/java/{map_field_lite.cc => lite/map_field.cc} (99%) rename src/google/protobuf/compiler/java/{map_field_lite.h => lite/map_field.h} (86%) rename src/google/protobuf/compiler/java/{message_lite.cc => lite/message.cc} (98%) rename src/google/protobuf/compiler/java/{message_lite.h => lite/message.h} (85%) rename src/google/protobuf/compiler/java/{message_builder_lite.cc => lite/message_builder.cc} (91%) rename src/google/protobuf/compiler/java/{message_builder_lite.h => lite/message_builder.h} (93%) rename src/google/protobuf/compiler/java/{message_field_lite.cc => lite/message_field.cc} (99%) rename src/google/protobuf/compiler/java/{message_field_lite.h => lite/message_field.h} (95%) rename src/google/protobuf/compiler/java/{primitive_field_lite.cc => lite/primitive_field.cc} (99%) rename src/google/protobuf/compiler/java/{primitive_field_lite.h => lite/primitive_field.h} (95%) rename src/google/protobuf/compiler/java/{string_field_lite.cc => lite/string_field.cc} (99%) rename src/google/protobuf/compiler/java/{string_field_lite.h => lite/string_field.h} (95%) diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 3f2ec63653..34d65ceb41 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -198,6 +198,7 @@ cc_dist_library( "//src/google/protobuf/compiler/cpp", "//src/google/protobuf/compiler/csharp", "//src/google/protobuf/compiler/java", + "//src/google/protobuf/compiler/java:kotlin", "//src/google/protobuf/compiler/objectivec", "//src/google/protobuf/compiler/php", "//src/google/protobuf/compiler/python", diff --git a/src/google/protobuf/compiler/BUILD.bazel b/src/google/protobuf/compiler/BUILD.bazel index 8f9ffb3e7d..02729619fd 100644 --- a/src/google/protobuf/compiler/BUILD.bazel +++ b/src/google/protobuf/compiler/BUILD.bazel @@ -195,6 +195,7 @@ cc_library( "//src/google/protobuf/compiler/cpp", "//src/google/protobuf/compiler/csharp", "//src/google/protobuf/compiler/java", + "//src/google/protobuf/compiler/java:kotlin", "//src/google/protobuf/compiler/objectivec", "//src/google/protobuf/compiler/php", "//src/google/protobuf/compiler/python", diff --git a/src/google/protobuf/compiler/java/BUILD.bazel b/src/google/protobuf/compiler/java/BUILD.bazel index 1e0fa599a5..39f862f6cc 100644 --- a/src/google/protobuf/compiler/java/BUILD.bazel +++ b/src/google/protobuf/compiler/java/BUILD.bazel @@ -6,6 +6,12 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test") load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix") load("//build_defs:cpp_opts.bzl", "COPTS") +package( + default_visibility = [ + "//src/google/protobuf/compiler/java:__subpackages__", + ], +) + cc_library( name = "names", hdrs = ["names.h"], @@ -13,19 +19,22 @@ cc_library( strip_include_prefix = "/src", visibility = ["//visibility:public"], deps = [ - ":names_internal", + ":helpers", "//src/google/protobuf", ], ) cc_library( - name = "names_internal", + name = "helpers", srcs = [ + "doc_comment.cc", "helpers.cc", "name_resolver.cc", "names.cc", ], hdrs = [ + "context.h", + "doc_comment.h", "generator.h", "helpers.h", "name_resolver.h", @@ -34,7 +43,10 @@ cc_library( ], copts = COPTS, strip_include_prefix = "/src", - visibility = ["//pkg:__pkg__"], + visibility = [ + "//pkg:__pkg__", + "//src/google/protobuf/compiler/java:__subpackages__", + ], deps = [ ":java_features_bootstrap", "//src/google/protobuf", @@ -70,64 +82,16 @@ cc_library( name = "java", srcs = [ "context.cc", - "doc_comment.cc", - "enum.cc", - "enum_field.cc", - "enum_field_lite.cc", - "enum_lite.cc", - "extension.cc", - "extension_lite.cc", - "field.cc", "file.cc", "generator.cc", - "generator_factory.cc", - "kotlin_generator.cc", - "map_field.cc", - "map_field_lite.cc", - "message.cc", - "message_builder.cc", - "message_builder_lite.cc", - "message_field.cc", - "message_field_lite.cc", - "message_lite.cc", - "message_serialization.cc", - "primitive_field.cc", - "primitive_field_lite.cc", - "service.cc", "shared_code_generator.cc", - "string_field.cc", - "string_field_lite.cc", ], hdrs = [ - "context.h", - "doc_comment.h", - "enum.h", - "enum_field.h", - "enum_field_lite.h", - "enum_lite.h", - "extension.h", - "extension_lite.h", - "field.h", "file.h", "generator.h", "generator_factory.h", "java_features.pb.h", - "kotlin_generator.h", - "map_field.h", - "map_field_lite.h", - "message.h", - "message_builder.h", - "message_builder_lite.h", - "message_field.h", - "message_field_lite.h", - "message_lite.h", - "message_serialization.h", - "primitive_field.h", - "primitive_field_lite.h", - "service.h", "shared_code_generator.h", - "string_field.h", - "string_field_lite.h", ], copts = COPTS, strip_include_prefix = "/src", @@ -136,9 +100,10 @@ cc_library( "//src/google/protobuf/compiler:__pkg__", ], deps = [ + ":generator_common", + ":helpers", ":java_features_bootstrap", ":names", - ":names_internal", "//src/google/protobuf", "//src/google/protobuf:arena", "//src/google/protobuf:port", @@ -146,6 +111,8 @@ cc_library( "//src/google/protobuf/compiler:code_generator", "//src/google/protobuf/compiler:retention", "//src/google/protobuf/compiler:versions", + "//src/google/protobuf/compiler/java/immutable", + "//src/google/protobuf/compiler/java/lite", "//src/google/protobuf/io", "//src/google/protobuf/io:printer", "@com_google_absl//absl/container:btree", @@ -158,6 +125,62 @@ cc_library( ], ) +cc_library( + name = "generator_common", + srcs = ["field_common.cc"], + hdrs = [ + "field_common.h", + "generator_common.h", + "generator_factory.h", + ], + strip_include_prefix = "/src", + visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], + deps = [ + ":helpers", + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/strings:string_view", + ], +) + +cc_library( + name = "message_serialization", + srcs = ["message_serialization.cc"], + hdrs = ["message_serialization.h"], + strip_include_prefix = "/src", + deps = [ + ":generator_common", + ":helpers", + "//src/google/protobuf", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + ], +) + +cc_library( + name = "kotlin", + srcs = ["kotlin_generator.cc"], + hdrs = ["kotlin_generator.h"], + strip_include_prefix = "/src", + visibility = [ + "//pkg:__pkg__", + "//src/google/protobuf/compiler:__pkg__", + ], + deps = [ + ":helpers", + ":java", + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler:code_generator", + ], +) + cc_test( name = "doc_comment_unittest", srcs = ["doc_comment_unittest.cc"], @@ -167,7 +190,7 @@ cc_test( "//src/google/protobuf:descriptor_proto_srcs", ], deps = [ - ":java", + ":helpers", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -178,7 +201,7 @@ cc_test( srcs = ["plugin_unittest.cc"], deps = [ ":java", - "//:protobuf", + "//src/google/protobuf", "//src/google/protobuf/compiler:command_line_interface", "//src/google/protobuf/io", "//src/google/protobuf/io:printer", diff --git a/src/google/protobuf/compiler/java/context.cc b/src/google/protobuf/compiler/java/context.cc index 371c96ac69..e37afbcb18 100644 --- a/src/google/protobuf/compiler/java/context.cc +++ b/src/google/protobuf/compiler/java/context.cc @@ -13,7 +13,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/strings/strip.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.h" diff --git a/src/google/protobuf/compiler/java/field.cc b/src/google/protobuf/compiler/java/field.cc deleted file mode 100644 index da6210973d..0000000000 --- a/src/google/protobuf/compiler/java/field.cc +++ /dev/null @@ -1,294 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#include "google/protobuf/compiler/java/field.h" - -#include -#include - -#include "absl/container/flat_hash_map.h" -#include "absl/log/absl_log.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/substitute.h" -#include "google/protobuf/compiler/java/context.h" -#include "google/protobuf/compiler/java/enum_field.h" -#include "google/protobuf/compiler/java/enum_field_lite.h" -#include "google/protobuf/compiler/java/helpers.h" -#include "google/protobuf/compiler/java/map_field.h" -#include "google/protobuf/compiler/java/map_field_lite.h" -#include "google/protobuf/compiler/java/message_field.h" -#include "google/protobuf/compiler/java/message_field_lite.h" -#include "google/protobuf/compiler/java/primitive_field.h" -#include "google/protobuf/compiler/java/primitive_field_lite.h" -#include "google/protobuf/compiler/java/string_field.h" -#include "google/protobuf/compiler/java/string_field_lite.h" -#include "google/protobuf/io/printer.h" - - -namespace google { -namespace protobuf { -namespace compiler { -namespace java { - -namespace { - -ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field, - int messageBitIndex, - int builderBitIndex, - Context* context) { - if (field->is_repeated()) { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - if (IsMapEntry(field->message_type())) { - return new ImmutableMapFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - } else { - return new RepeatedImmutableMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } - case JAVATYPE_ENUM: - return new RepeatedImmutableEnumFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - case JAVATYPE_STRING: - return new RepeatedImmutableStringFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - default: - return new RepeatedImmutablePrimitiveFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } - } else { - if (IsRealOneof(field)) { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - return new ImmutableMessageOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - case JAVATYPE_ENUM: - return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - case JAVATYPE_STRING: - return new ImmutableStringOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - default: - return new ImmutablePrimitiveOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } - } else { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - return new ImmutableMessageFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - case JAVATYPE_ENUM: - return new ImmutableEnumFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - case JAVATYPE_STRING: - return new ImmutableStringFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - default: - return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex, - builderBitIndex, context); - } - } - } -} - -ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator( - const FieldDescriptor* field, int messageBitIndex, Context* context) { - if (field->is_repeated()) { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - if (IsMapEntry(field->message_type())) { - return new ImmutableMapFieldLiteGenerator(field, messageBitIndex, - context); - } else { - return new RepeatedImmutableMessageFieldLiteGenerator( - field, messageBitIndex, context); - } - case JAVATYPE_ENUM: - return new RepeatedImmutableEnumFieldLiteGenerator( - field, messageBitIndex, context); - case JAVATYPE_STRING: - return new RepeatedImmutableStringFieldLiteGenerator( - field, messageBitIndex, context); - default: - return new RepeatedImmutablePrimitiveFieldLiteGenerator( - field, messageBitIndex, context); - } - } else { - if (IsRealOneof(field)) { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - return new ImmutableMessageOneofFieldLiteGenerator( - field, messageBitIndex, context); - case JAVATYPE_ENUM: - return new ImmutableEnumOneofFieldLiteGenerator( - field, messageBitIndex, context); - case JAVATYPE_STRING: - return new ImmutableStringOneofFieldLiteGenerator( - field, messageBitIndex, context); - default: - return new ImmutablePrimitiveOneofFieldLiteGenerator( - field, messageBitIndex, context); - } - } else { - switch (GetJavaType(field)) { - case JAVATYPE_MESSAGE: - return new ImmutableMessageFieldLiteGenerator(field, messageBitIndex, - context); - case JAVATYPE_ENUM: - return new ImmutableEnumFieldLiteGenerator(field, messageBitIndex, - context); - case JAVATYPE_STRING: - return new ImmutableStringFieldLiteGenerator(field, messageBitIndex, - context); - default: - return new ImmutablePrimitiveFieldLiteGenerator( - field, messageBitIndex, context); - } - } - } -} - - -static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) { - // 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. - ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() " - << "called on field generator that does not support packing."; -} - -} // namespace - -ImmutableFieldGenerator::~ImmutableFieldGenerator() {} - -void ImmutableFieldGenerator::GenerateBuilderParsingCodeFromPacked( - io::Printer* printer) const { - ReportUnexpectedPackedFieldsCall(printer); -} - -ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {} - -// =================================================================== - -template <> -FieldGeneratorMap::FieldGeneratorMap( - const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), field_generators_(descriptor->field_count()) { - // Construct all the FieldGenerators and assign them bit indices for their - // bit fields. - int messageBitIndex = 0; - int builderBitIndex = 0; - for (int i = 0; i < descriptor->field_count(); i++) { - ImmutableFieldGenerator* generator = MakeImmutableGenerator( - descriptor->field(i), messageBitIndex, builderBitIndex, context); - field_generators_[i].reset(generator); - messageBitIndex += generator->GetNumBitsForMessage(); - builderBitIndex += generator->GetNumBitsForBuilder(); - } -} - -template <> -FieldGeneratorMap::~FieldGeneratorMap() {} - -template <> -FieldGeneratorMap::FieldGeneratorMap( - const Descriptor* descriptor, Context* context) - : descriptor_(descriptor), field_generators_(descriptor->field_count()) { - // Construct all the FieldGenerators and assign them bit indices for their - // bit fields. - int messageBitIndex = 0; - for (int i = 0; i < descriptor->field_count(); i++) { - ImmutableFieldLiteGenerator* generator = MakeImmutableLiteGenerator( - descriptor->field(i), messageBitIndex, context); - field_generators_[i].reset(generator); - messageBitIndex += generator->GetNumBitsForMessage(); - } -} - -template <> -FieldGeneratorMap::~FieldGeneratorMap() {} - - -void SetCommonFieldVariables( - const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, - absl::flat_hash_map* variables) { - (*variables)["field_name"] = descriptor->name(); - (*variables)["name"] = info->name; - (*variables)["classname"] = descriptor->containing_type()->name(); - (*variables)["capitalized_name"] = info->capitalized_name; - (*variables)["disambiguated_reason"] = info->disambiguated_reason; - (*variables)["constant_name"] = FieldConstantName(descriptor); - (*variables)["number"] = absl::StrCat(descriptor->number()); - (*variables)["kt_dsl_builder"] = "_builder"; - // These variables are placeholders to pick out the beginning and ends of - // identifiers for annotations (when doing so with existing variables would - // be ambiguous or impossible). They should never be set to anything but the - // empty string. - (*variables)["{"] = ""; - (*variables)["}"] = ""; - (*variables)["kt_name"] = IsForbiddenKotlin(info->name) - ? absl::StrCat(info->name, "_") - : info->name; - (*variables)["kt_capitalized_name"] = - IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_") - : info->capitalized_name; - if (!descriptor->is_repeated()) { - (*variables)["annotation_field_type"] = - std::string(FieldTypeName(descriptor->type())); - } else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE && - IsMapEntry(descriptor->message_type())) { - (*variables)["annotation_field_type"] = - absl::StrCat(FieldTypeName(descriptor->type()), "MAP"); - } else { - (*variables)["annotation_field_type"] = - absl::StrCat(FieldTypeName(descriptor->type()), "_LIST"); - if (descriptor->is_packed()) { - variables->insert( - {"annotation_field_type", - absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")}); - } - } -} - -void SetCommonOneofVariables( - const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, - absl::flat_hash_map* variables) { - (*variables)["oneof_name"] = info->name; - (*variables)["oneof_capitalized_name"] = info->capitalized_name; - (*variables)["oneof_index"] = - absl::StrCat(descriptor->containing_oneof()->index()); - (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); - (*variables)["set_oneof_case_message"] = - absl::StrCat(info->name, "Case_ = ", descriptor->number()); - (*variables)["clear_oneof_case_message"] = - absl::StrCat(info->name, "Case_ = 0"); - (*variables)["has_oneof_case_message"] = - absl::StrCat(info->name, "Case_ == ", descriptor->number()); -} - -void PrintExtraFieldInfo( - const absl::flat_hash_map& variables, - io::Printer* printer) { - auto it = variables.find("disambiguated_reason"); - if (it != variables.end() && !it->second.empty()) { - printer->Print( - variables, - "// An alternative name is used for field \"$field_name$\" because:\n" - "// $disambiguated_reason$\n"); - } -} - -} // namespace java -} // namespace compiler -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/compiler/java/field.h b/src/google/protobuf/compiler/java/field.h deleted file mode 100644 index 7bcaf247e4..0000000000 --- a/src/google/protobuf/compiler/java/field.h +++ /dev/null @@ -1,169 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. - -#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ -#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ - -#include -#include -#include - -#include "absl/container/flat_hash_map.h" -#include "absl/log/absl_check.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/port.h" - -namespace google { -namespace protobuf { -namespace compiler { -namespace java { -class Context; // context.h -class ClassNameResolver; // name_resolver.h -} // namespace java -} // namespace compiler -namespace io { -class Printer; // printer.h -} -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { -namespace compiler { -namespace java { - -class ImmutableFieldGenerator { - public: - ImmutableFieldGenerator() {} - ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete; - ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete; - virtual ~ImmutableFieldGenerator(); - - virtual int GetMessageBitIndex() const = 0; - virtual int GetBuilderBitIndex() const = 0; - virtual int GetNumBitsForMessage() const = 0; - virtual int GetNumBitsForBuilder() const = 0; - virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; - virtual void GenerateMembers(io::Printer* printer) const = 0; - virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; - virtual void GenerateInitializationCode(io::Printer* printer) const = 0; - virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0; - virtual void GenerateMergingCode(io::Printer* printer) const = 0; - virtual void GenerateBuildingCode(io::Printer* printer) const = 0; - virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0; - virtual void GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const; - virtual void GenerateSerializationCode(io::Printer* printer) const = 0; - virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; - virtual void GenerateFieldBuilderInitializationCode( - io::Printer* printer) const = 0; - virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; - - virtual void GenerateEqualsCode(io::Printer* printer) const = 0; - virtual void GenerateHashCode(io::Printer* printer) const = 0; - - virtual std::string GetBoxedType() const = 0; -}; - -class ImmutableFieldLiteGenerator { - public: - ImmutableFieldLiteGenerator() {} - ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete; - ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) = - delete; - virtual ~ImmutableFieldLiteGenerator(); - - virtual int GetNumBitsForMessage() const = 0; - virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; - virtual void GenerateMembers(io::Printer* printer) const = 0; - virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; - virtual void GenerateInitializationCode(io::Printer* printer) const = 0; - virtual void GenerateFieldInfo(io::Printer* printer, - std::vector* output) const = 0; - virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; - - virtual std::string GetBoxedType() const = 0; -}; - - -// Convenience class which constructs FieldGenerators for a Descriptor. -template -class FieldGeneratorMap { - public: - explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context); - FieldGeneratorMap(const FieldGeneratorMap&) = delete; - FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; - ~FieldGeneratorMap(); - - const FieldGeneratorType& get(const FieldDescriptor* field) const; - - private: - const Descriptor* descriptor_; - std::vector> field_generators_; -}; - -template -inline const FieldGeneratorType& FieldGeneratorMap::get( - const FieldDescriptor* field) const { - ABSL_CHECK_EQ(field->containing_type(), descriptor_); - return *field_generators_[field->index()]; -} - -// Instantiate template for mutable and immutable maps. -template <> -FieldGeneratorMap::FieldGeneratorMap( - const Descriptor* descriptor, Context* context); - -template <> -FieldGeneratorMap::~FieldGeneratorMap(); - - -template <> -FieldGeneratorMap::FieldGeneratorMap( - const Descriptor* descriptor, Context* context); - -template <> -FieldGeneratorMap::~FieldGeneratorMap(); - - -// Field information used in FieldGenerators. -struct FieldGeneratorInfo { - std::string name; - std::string capitalized_name; - std::string disambiguated_reason; -}; - -// Oneof information used in OneofFieldGenerators. -struct OneofGeneratorInfo { - std::string name; - std::string capitalized_name; -}; - -// Set some common variables used in variable FieldGenerators. -void SetCommonFieldVariables( - const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, - absl::flat_hash_map* variables); - -// Set some common oneof variables used in OneofFieldGenerators. -void SetCommonOneofVariables( - const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, - absl::flat_hash_map* variables); - -// Print useful comments before a field's accessors. -void PrintExtraFieldInfo( - const absl::flat_hash_map& variables, - io::Printer* printer); - -} // namespace java -} // namespace compiler -} // namespace protobuf -} // namespace google - -#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ diff --git a/src/google/protobuf/compiler/java/field_common.cc b/src/google/protobuf/compiler/java/field_common.cc new file mode 100644 index 0000000000..4acafabe36 --- /dev/null +++ b/src/google/protobuf/compiler/java/field_common.cc @@ -0,0 +1,85 @@ +#include "google/protobuf/compiler/java/field_common.h" + +#include + +#include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +void SetCommonFieldVariables( + const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, + absl::flat_hash_map* variables) { + (*variables)["field_name"] = descriptor->name(); + (*variables)["name"] = info->name; + (*variables)["classname"] = descriptor->containing_type()->name(); + (*variables)["capitalized_name"] = info->capitalized_name; + (*variables)["disambiguated_reason"] = info->disambiguated_reason; + (*variables)["constant_name"] = FieldConstantName(descriptor); + (*variables)["number"] = absl::StrCat(descriptor->number()); + (*variables)["kt_dsl_builder"] = "_builder"; + // These variables are placeholders to pick out the beginning and ends of + // identifiers for annotations (when doing so with existing variables would + // be ambiguous or impossible). They should never be set to anything but the + // empty string. + (*variables)["{"] = ""; + (*variables)["}"] = ""; + (*variables)["kt_name"] = IsForbiddenKotlin(info->name) + ? absl::StrCat(info->name, "_") + : info->name; + (*variables)["kt_capitalized_name"] = + IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_") + : info->capitalized_name; + if (!descriptor->is_repeated()) { + (*variables)["annotation_field_type"] = + std::string(FieldTypeName(descriptor->type())); + } else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE && + IsMapEntry(descriptor->message_type())) { + (*variables)["annotation_field_type"] = + absl::StrCat(FieldTypeName(descriptor->type()), "MAP"); + } else { + (*variables)["annotation_field_type"] = + absl::StrCat(FieldTypeName(descriptor->type()), "_LIST"); + if (descriptor->is_packed()) { + variables->insert( + {"annotation_field_type", + absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")}); + } + } +} + +void SetCommonOneofVariables( + const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, + absl::flat_hash_map* variables) { + (*variables)["oneof_name"] = info->name; + (*variables)["oneof_capitalized_name"] = info->capitalized_name; + (*variables)["oneof_index"] = + absl::StrCat(descriptor->containing_oneof()->index()); + (*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); + (*variables)["set_oneof_case_message"] = + absl::StrCat(info->name, "Case_ = ", descriptor->number()); + (*variables)["clear_oneof_case_message"] = + absl::StrCat(info->name, "Case_ = 0"); + (*variables)["has_oneof_case_message"] = + absl::StrCat(info->name, "Case_ == ", descriptor->number()); +} + +void PrintExtraFieldInfo( + const absl::flat_hash_map& variables, + io::Printer* printer) { + auto it = variables.find("disambiguated_reason"); + if (it != variables.end() && !it->second.empty()) { + printer->Print( + variables, + "// An alternative name is used for field \"$field_name$\" because:\n" + "// $disambiguated_reason$\n"); + } +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/java/field_common.h b/src/google/protobuf/compiler/java/field_common.h new file mode 100644 index 0000000000..54e58a0ecb --- /dev/null +++ b/src/google/protobuf/compiler/java/field_common.h @@ -0,0 +1,44 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ + +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +// Field information used in FieldGenerators. +struct FieldGeneratorInfo { + std::string name; + std::string capitalized_name; + std::string disambiguated_reason; +}; + +// Oneof information used in OneofFieldGenerators. +struct OneofGeneratorInfo { + std::string name; + std::string capitalized_name; +}; + +// Set some common variables used in variable FieldGenerators. +void SetCommonFieldVariables( + const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, + absl::flat_hash_map* variables); + +// Set some common oneof variables used in OneofFieldGenerators. +void SetCommonOneofVariables( + const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, + absl::flat_hash_map* variables); + +// Print useful comments before a field's accessors. +void PrintExtraFieldInfo( + const absl::flat_hash_map& variables, + io::Printer* printer); + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index ffee03871f..2ce6e8978e 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -12,22 +12,23 @@ #include "google/protobuf/compiler/java/file.h" #include +#include #include #include "absl/container/btree_set.h" +#include "absl/log/absl_check.h" #include "absl/log/absl_log.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/java/context.h" -#include "google/protobuf/compiler/java/enum.h" -#include "google/protobuf/compiler/java/enum_lite.h" -#include "google/protobuf/compiler/java/extension.h" +#include "google/protobuf/compiler/java/generator_common.h" #include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/compiler/java/helpers.h" -#include "google/protobuf/compiler/java/message.h" +#include "google/protobuf/compiler/java/immutable/generator_factory.h" +#include "google/protobuf/compiler/java/lite/generator_factory.h" #include "google/protobuf/compiler/java/name_resolver.h" -#include "google/protobuf/compiler/java/service.h" +#include "google/protobuf/compiler/java/options.h" #include "google/protobuf/compiler/java/shared_code_generator.h" #include "google/protobuf/compiler/retention.h" #include "google/protobuf/compiler/versions.h" @@ -36,6 +37,7 @@ #include "google/protobuf/io/printer.h" #include "google/protobuf/io/zero_copy_stream.h" + // Must be last. #include "google/protobuf/port_def.inc" @@ -165,6 +167,18 @@ void MaybeRestartJavaMethod(io::Printer* printer, int* bytecode_estimate, *bytecode_estimate = 0; } } + +std::unique_ptr CreateGeneratorFactory( + const FileDescriptor* file, const Options& options, Context* context, + bool immutable_api) { + ABSL_CHECK(immutable_api) + << "Open source release does not support the mutable API"; + if (HasDescriptorMethods(file, context->EnforceLite())) { + return MakeImmutableGeneratorFactory(context); + } else { + return MakeImmutableLiteGeneratorFactory(context); + } +} } // namespace FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, @@ -174,18 +188,19 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options, message_generators_(file->message_type_count()), extension_generators_(file->extension_count()), context_(new Context(file, options)), + generator_factory_( + CreateGeneratorFactory(file, options, context_.get(), immutable_api)), name_resolver_(context_->GetNameResolver()), options_(options), immutable_api_(immutable_api) { classname_ = name_resolver_->GetFileClassName(file, immutable_api); - generator_factory_.reset(new ImmutableGeneratorFactory(context_.get())); for (int i = 0; i < file_->message_type_count(); ++i) { - message_generators_[i].reset( - generator_factory_->NewMessageGenerator(file_->message_type(i))); + message_generators_[i] = + generator_factory_->NewMessageGenerator(file_->message_type(i)); } for (int i = 0; i < file_->extension_count(); ++i) { - extension_generators_[i].reset( - generator_factory_->NewExtensionGenerator(file_->extension(i))); + extension_generators_[i] = + generator_factory_->NewExtensionGenerator(file_->extension(i)); } } @@ -322,13 +337,8 @@ void FileGenerator::Generate(io::Printer* printer) { if (!MultipleJavaFiles(file_, immutable_api_)) { for (int i = 0; i < file_->enum_type_count(); i++) { - if (HasDescriptorMethods(file_, context_->EnforceLite())) { - EnumGenerator(file_->enum_type(i), immutable_api_, context_.get()) - .Generate(printer); - } else { - EnumLiteGenerator(file_->enum_type(i), immutable_api_, context_.get()) - .Generate(printer); - } + generator_factory_->NewEnumGenerator(file_->enum_type(i)) + ->Generate(printer); } for (int i = 0; i < file_->message_type_count(); i++) { message_generators_[i]->GenerateInterface(printer); @@ -556,21 +566,12 @@ void FileGenerator::GenerateSiblings( std::vector* annotation_list) { if (MultipleJavaFiles(file_, immutable_api_)) { for (int i = 0; i < file_->enum_type_count(); i++) { - if (HasDescriptorMethods(file_, context_->EnforceLite())) { - EnumGenerator generator(file_->enum_type(i), immutable_api_, - context_.get()); - GenerateSibling( - package_dir, java_package_, file_->enum_type(i), context, file_list, - options_.annotate_code, annotation_list, "", &generator, - options_.opensource_runtime, &EnumGenerator::Generate); - } else { - EnumLiteGenerator generator(file_->enum_type(i), immutable_api_, - context_.get()); - GenerateSibling( - package_dir, java_package_, file_->enum_type(i), context, file_list, - options_.annotate_code, annotation_list, "", &generator, - options_.opensource_runtime, &EnumLiteGenerator::Generate); - } + std::unique_ptr generator( + generator_factory_->NewEnumGenerator(file_->enum_type(i))); + GenerateSibling( + package_dir, java_package_, file_->enum_type(i), context, file_list, + options_.annotate_code, annotation_list, "", generator.get(), + options_.opensource_runtime, &EnumGenerator::Generate); } for (int i = 0; i < file_->message_type_count(); i++) { if (immutable_api_) { diff --git a/src/google/protobuf/compiler/java/file.h b/src/google/protobuf/compiler/java/file.h index ab13afcc2a..7d995ee33c 100644 --- a/src/google/protobuf/compiler/java/file.h +++ b/src/google/protobuf/compiler/java/file.h @@ -88,8 +88,8 @@ class FileGenerator { std::vector> message_generators_; std::vector> extension_generators_; - std::unique_ptr generator_factory_; std::unique_ptr context_; + std::unique_ptr generator_factory_; ClassNameResolver* name_resolver_; const Options options_; bool immutable_api_; diff --git a/src/google/protobuf/compiler/java/generator_common.h b/src/google/protobuf/compiler/java/generator_common.h new file mode 100644 index 0000000000..c2c8859d0f --- /dev/null +++ b/src/google/protobuf/compiler/java/generator_common.h @@ -0,0 +1,89 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ + +#include +#include +#include + +#include "absl/log/absl_check.h" +#include "absl/log/absl_log.h" +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/name_resolver.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/io/printer.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +static const int kMaxStaticSize = 1 << 15; // aka 32k + +class FieldGenerator { + public: + virtual ~FieldGenerator() = default; + virtual void GenerateSerializationCode(io::Printer* printer) const = 0; +}; + +// Convenience class which constructs FieldGenerators for a Descriptor. +template +class FieldGeneratorMap { + public: + explicit FieldGeneratorMap(const Descriptor* descriptor) + : descriptor_(descriptor) { + field_generators_.reserve(static_cast(descriptor->field_count())); + } + + ~FieldGeneratorMap() { + for (const auto* g : field_generators_) { + delete g; + } + } + + FieldGeneratorMap(FieldGeneratorMap&&) = default; + FieldGeneratorMap& operator=(FieldGeneratorMap&&) = default; + + FieldGeneratorMap(const FieldGeneratorMap&) = delete; + FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; + + void Add(const FieldDescriptor* field, + std::unique_ptr field_generator) { + ABSL_CHECK_EQ(field->containing_type(), descriptor_); + field_generators_.push_back(field_generator.release()); + } + + const FieldGeneratorType& get(const FieldDescriptor* field) const { + ABSL_CHECK_EQ(field->containing_type(), descriptor_); + return *field_generators_[static_cast(field->index())]; + } + + std::vector field_generators() const { + std::vector field_generators; + field_generators.reserve(field_generators_.size()); + for (const auto* g : field_generators_) { + field_generators.push_back(g); + } + return field_generators; + } + + private: + const Descriptor* descriptor_; + std::vector field_generators_; +}; + +inline void ReportUnexpectedPackedFieldsCall() { + // 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. + ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() " + << "called on field generator that does not support packing."; +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ diff --git a/src/google/protobuf/compiler/java/generator_factory.cc b/src/google/protobuf/compiler/java/generator_factory.cc deleted file mode 100644 index c60a891be1..0000000000 --- a/src/google/protobuf/compiler/java/generator_factory.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd - -// Author: liujisi@google.com (Pherl Liu) - -#include "google/protobuf/compiler/java/generator_factory.h" - -#include "google/protobuf/compiler/java/context.h" -#include "google/protobuf/compiler/java/enum_field.h" -#include "google/protobuf/compiler/java/extension.h" -#include "google/protobuf/compiler/java/extension_lite.h" -#include "google/protobuf/compiler/java/field.h" -#include "google/protobuf/compiler/java/helpers.h" -#include "google/protobuf/compiler/java/message.h" -#include "google/protobuf/compiler/java/message_lite.h" -#include "google/protobuf/compiler/java/service.h" - -namespace google { -namespace protobuf { -namespace compiler { -namespace java { - -GeneratorFactory::GeneratorFactory() {} -GeneratorFactory::~GeneratorFactory() {} - -// =================================================================== - -ImmutableGeneratorFactory::ImmutableGeneratorFactory(Context* context) - : context_(context) {} -ImmutableGeneratorFactory::~ImmutableGeneratorFactory() {} - -MessageGenerator* ImmutableGeneratorFactory::NewMessageGenerator( - const Descriptor* descriptor) const { - if (HasDescriptorMethods(descriptor, context_->EnforceLite())) { - return new ImmutableMessageGenerator(descriptor, context_); - } else { - return new ImmutableMessageLiteGenerator(descriptor, context_); - } -} - -ExtensionGenerator* ImmutableGeneratorFactory::NewExtensionGenerator( - const FieldDescriptor* descriptor) const { - if (HasDescriptorMethods(descriptor->file(), context_->EnforceLite())) { - return new ImmutableExtensionGenerator(descriptor, context_); - } else { - return new ImmutableExtensionLiteGenerator(descriptor, context_); - } -} - -ServiceGenerator* ImmutableGeneratorFactory::NewServiceGenerator( - const ServiceDescriptor* descriptor) const { - return new ImmutableServiceGenerator(descriptor, context_); -} - - -} // namespace java -} // namespace compiler -} // namespace protobuf -} // namespace google diff --git a/src/google/protobuf/compiler/java/generator_factory.h b/src/google/protobuf/compiler/java/generator_factory.h index 1c52c158a2..2dcfad9517 100644 --- a/src/google/protobuf/compiler/java/generator_factory.h +++ b/src/google/protobuf/compiler/java/generator_factory.h @@ -10,69 +10,124 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__ -#include "google/protobuf/port.h" +#include +#include -namespace google { -namespace protobuf { -class FieldDescriptor; // descriptor.h -class Descriptor; // descriptor.h -class ServiceDescriptor; // descriptor.h -namespace compiler { -namespace java { -class MessageGenerator; // message.h -class ExtensionGenerator; // extension.h -class ServiceGenerator; // service.h -class Context; // context.h -} // namespace java -} // namespace compiler -} // namespace protobuf -} // namespace google +#include "absl/container/btree_map.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { namespace compiler { namespace java { +class MessageGenerator { + public: + explicit MessageGenerator(const Descriptor* descriptor); + MessageGenerator(const MessageGenerator&) = delete; + MessageGenerator& operator=(const MessageGenerator&) = delete; + virtual ~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. + virtual void GenerateStaticVariables(io::Printer* printer, + int* bytecode_estimate) = 0; + + // Output code which initializes the static variables generated by + // GenerateStaticVariables(). Returns an estimate of bytecode size. + virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0; + + // Generate the class itself. + virtual void Generate(io::Printer* printer) = 0; + + // Generates the base interface that both the class and its builder + // implement + virtual void GenerateInterface(io::Printer* printer) = 0; + + // Generate code to register all contained extensions with an + // ExtensionRegistry. + virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0; + virtual void GenerateKotlinDsl(io::Printer* printer) const = 0; + virtual void GenerateKotlinMembers(io::Printer* printer) const = 0; + virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0; + + protected: + const Descriptor* descriptor_; + absl::btree_map oneofs_; +}; + +class EnumGenerator { + public: + virtual ~EnumGenerator() = default; + virtual void Generate(io::Printer* printer) = 0; +}; + +// Generates code for an extension, which may be within the scope of some +// message or may be at file scope. This is much simpler than FieldGenerator +// since extensions are just simple identifiers with interesting types. +class ExtensionGenerator { + public: + virtual ~ExtensionGenerator() = default; + + virtual void Generate(io::Printer* printer) = 0; + + // Returns an estimate of the number of bytes the printed code will compile + // to + virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0; + + // Returns an estimate of the number of bytes the printed code will compile + // to + virtual int GenerateRegistrationCode(io::Printer* printer) = 0; + + protected: + static void InitTemplateVars( + const FieldDescriptor* descriptor, const std::string& scope, + bool immutable, ClassNameResolver* name_resolver, + absl::flat_hash_map* vars_pointer, + Context* context); +}; + +class ServiceGenerator { + public: + explicit ServiceGenerator(const ServiceDescriptor* descriptor) + : descriptor_(descriptor) {} + virtual ~ServiceGenerator() = default; + + ServiceGenerator(const ServiceGenerator&) = delete; + ServiceGenerator& operator=(const ServiceGenerator&) = delete; + + virtual void Generate(io::Printer* printer) = 0; + + enum RequestOrResponse { REQUEST, RESPONSE }; + enum IsAbstract { IS_ABSTRACT, IS_CONCRETE }; + + protected: + const ServiceDescriptor* descriptor_; +}; + +// An interface to create generators for a given descriptor. This interface +// is implemented for every variant of the Java API (Mutable, Immutable, Lite). class GeneratorFactory { public: - GeneratorFactory(); - GeneratorFactory(const GeneratorFactory&) = delete; - GeneratorFactory& operator=(const GeneratorFactory&) = delete; - virtual ~GeneratorFactory(); + virtual ~GeneratorFactory() = default; - virtual MessageGenerator* NewMessageGenerator( + virtual std::unique_ptr NewMessageGenerator( const Descriptor* descriptor) const = 0; - virtual ExtensionGenerator* NewExtensionGenerator( + virtual std::unique_ptr NewEnumGenerator( + const EnumDescriptor* descriptor) const = 0; + + virtual std::unique_ptr NewExtensionGenerator( const FieldDescriptor* descriptor) const = 0; - virtual ServiceGenerator* NewServiceGenerator( + virtual std::unique_ptr NewServiceGenerator( const ServiceDescriptor* descriptor) const = 0; }; -// Factory that creates generators for immutable-default messages. -class ImmutableGeneratorFactory : public GeneratorFactory { - public: - ImmutableGeneratorFactory(Context* context); - ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete; - ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) = - delete; - ~ImmutableGeneratorFactory() override; - - MessageGenerator* NewMessageGenerator( - const Descriptor* descriptor) const override; - - ExtensionGenerator* NewExtensionGenerator( - const FieldDescriptor* descriptor) const override; - - ServiceGenerator* NewServiceGenerator( - const ServiceDescriptor* descriptor) const override; - - private: - Context* context_; -}; - - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/immutable/BUILD.bazel b/src/google/protobuf/compiler/java/immutable/BUILD.bazel new file mode 100644 index 0000000000..a65a03acbd --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/BUILD.bazel @@ -0,0 +1,131 @@ +# We use abbreviated target names in this directory to work around: +# https://github.com/bazelbuild/bazel/issues/18683 +cc_library( + name = "fg", + hdrs = ["field_generator.h"], + strip_include_prefix = "/src", + deps = [ + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/io:printer", + ], +) + +# We have to expose the field generators individually (instead of just through the factory) because +# the mutable generators delegate to them directly. +cc_library( + name = "fgs", + srcs = [ + "enum_field.cc", + "map_field.cc", + "message_field.cc", + "primitive_field.cc", + "string_field.cc", + ], + hdrs = [ + "enum_field.h", + "map_field.h", + "message_field.h", + "primitive_field.h", + "string_field.h", + ], + strip_include_prefix = "/src", + deps = [ + ":fg", + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "mfg", + srcs = ["make_field_generators.cc"], + hdrs = ["make_field_generators.h"], + strip_include_prefix = "/src", + deps = [ + ":fg", + ":fgs", + "//src/google/protobuf", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + ], +) + +cc_library( + name = "service", + srcs = ["service.cc"], + hdrs = ["service.h"], + strip_include_prefix = "/src", + visibility = ["//src/google/protobuf/compiler/java/lite:__pkg__"], + deps = [ + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "eg", + srcs = ["enum.cc"], + hdrs = ["enum.h"], + strip_include_prefix = "/src", + deps = [ + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "immutable", + srcs = [ + "extension.cc", + "generator_factory.cc", + "message.cc", + "message.h", + "message_builder.cc", + ], + hdrs = [ + "generator_factory.h", + # We don't actually want to put the remaining headers in `hdrs`. + # They are logically private, and should be in srcs=[], but + # unfortunately `strip_include_prefix` doesn't have any effect + # on headers listed in `srcs.` + "message.h", + "extension.h", + "message_builder.h", + ], + strip_include_prefix = "/src", + visibility = ["//src/google/protobuf/compiler/java:__pkg__"], + deps = [ + ":eg", + ":fg", + ":mfg", + ":service", + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/compiler/java:message_serialization", + "//src/google/protobuf/io", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/strings", + ], +) diff --git a/src/google/protobuf/compiler/java/enum.cc b/src/google/protobuf/compiler/java/immutable/enum.cc similarity index 97% rename from src/google/protobuf/compiler/java/enum.cc rename to src/google/protobuf/compiler/java/immutable/enum.cc index eb7803a57a..61b307914c 100644 --- a/src/google/protobuf/compiler/java/enum.cc +++ b/src/google/protobuf/compiler/java/immutable/enum.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/enum.h" +#include "google/protobuf/compiler/java/immutable/enum.h" #include @@ -30,8 +30,8 @@ namespace protobuf { namespace compiler { namespace java { -EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, - bool immutable_api, Context* context) +EnumNonLiteGenerator::EnumNonLiteGenerator(const EnumDescriptor* descriptor, + bool immutable_api, Context* context) : descriptor_(descriptor), immutable_api_(immutable_api), context_(context), @@ -52,9 +52,7 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, } } -EnumGenerator::~EnumGenerator() {} - -void EnumGenerator::Generate(io::Printer* printer) { +void EnumNonLiteGenerator::Generate(io::Printer* printer) { WriteEnumDocComment(printer, descriptor_, context_->options()); MaybePrintGeneratedAnnotation(context_, printer, descriptor_, immutable_api_); @@ -376,7 +374,7 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print("}\n\n"); } -bool EnumGenerator::CanUseEnumValues() { +bool EnumNonLiteGenerator::CanUseEnumValues() { if (canonical_values_.size() != descriptor_->value_count()) { return false; } diff --git a/src/google/protobuf/compiler/java/enum.h b/src/google/protobuf/compiler/java/immutable/enum.h similarity index 74% rename from src/google/protobuf/compiler/java/enum.h rename to src/google/protobuf/compiler/java/immutable/enum.h index 4c020b76a8..18ccc01e95 100644 --- a/src/google/protobuf/compiler/java/enum.h +++ b/src/google/protobuf/compiler/java/immutable/enum.h @@ -15,6 +15,7 @@ #include #include +#include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/descriptor.h" namespace google { @@ -36,15 +37,17 @@ namespace protobuf { namespace compiler { namespace java { -class EnumGenerator { +// This class is in the immutable/ directory, but since the mutable API for +// enums is nearly identical to the immutable one, we also use this for mutable +// enums. It is used for all enums except lite enums. +class EnumNonLiteGenerator : public EnumGenerator { public: - EnumGenerator(const EnumDescriptor* descriptor, bool immutable_api, - Context* context); - EnumGenerator(const EnumGenerator&) = delete; - EnumGenerator& operator=(const EnumGenerator&) = delete; - ~EnumGenerator(); + EnumNonLiteGenerator(const EnumDescriptor* descriptor, bool immutable_api, + Context* context); + EnumNonLiteGenerator(const EnumNonLiteGenerator&) = delete; + EnumNonLiteGenerator& operator=(const EnumNonLiteGenerator&) = delete; - void Generate(io::Printer* printer); + void Generate(io::Printer* printer) override; private: const EnumDescriptor* descriptor_; diff --git a/src/google/protobuf/compiler/java/enum_field.cc b/src/google/protobuf/compiler/java/immutable/enum_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/enum_field.cc rename to src/google/protobuf/compiler/java/immutable/enum_field.cc index b8e4d79371..2cb734f498 100644 --- a/src/google/protobuf/compiler/java/enum_field.cc +++ b/src/google/protobuf/compiler/java/immutable/enum_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/enum_field.h" +#include "google/protobuf/compiler/java/immutable/enum_field.h" #include #include @@ -20,6 +20,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/enum_field.h b/src/google/protobuf/compiler/java/immutable/enum_field.h similarity index 97% rename from src/google/protobuf/compiler/java/enum_field.h rename to src/google/protobuf/compiler/java/immutable/enum_field.h index 896c6e455b..625b7c21af 100644 --- a/src/google/protobuf/compiler/java/enum_field.h +++ b/src/google/protobuf/compiler/java/immutable/enum_field.h @@ -15,7 +15,9 @@ #include #include "absl/container/flat_hash_map.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/io/printer.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/extension.cc b/src/google/protobuf/compiler/java/immutable/extension.cc similarity index 96% rename from src/google/protobuf/compiler/java/extension.cc rename to src/google/protobuf/compiler/java/immutable/extension.cc index f7b738a5e3..d731d82400 100644 --- a/src/google/protobuf/compiler/java/extension.cc +++ b/src/google/protobuf/compiler/java/immutable/extension.cc @@ -9,13 +9,10 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/extension.h" +#include "google/protobuf/compiler/java/immutable/extension.h" -#include "absl/container/flat_hash_map.h" -#include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/extension.h b/src/google/protobuf/compiler/java/immutable/extension.h similarity index 63% rename from src/google/protobuf/compiler/java/extension.h rename to src/google/protobuf/compiler/java/immutable/extension.h index 36d30fd3f8..fbb8bea6b1 100644 --- a/src/google/protobuf/compiler/java/extension.h +++ b/src/google/protobuf/compiler/java/immutable/extension.h @@ -15,6 +15,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/port.h" namespace google { @@ -37,34 +38,6 @@ namespace protobuf { namespace compiler { namespace java { -// Generates code for an extension, which may be within the scope of some -// message or may be at file scope. This is much simpler than FieldGenerator -// since extensions are just simple identifiers with interesting types. -class ExtensionGenerator { - public: - explicit ExtensionGenerator() {} - ExtensionGenerator(const ExtensionGenerator&) = delete; - ExtensionGenerator& operator=(const ExtensionGenerator&) = delete; - virtual ~ExtensionGenerator() {} - - virtual void Generate(io::Printer* printer) = 0; - - // Returns an estimate of the number of bytes the printed code will compile - // to - virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0; - - // Returns an estimate of the number of bytes the printed code will compile - // to - virtual int GenerateRegistrationCode(io::Printer* printer) = 0; - - protected: - static void InitTemplateVars( - const FieldDescriptor* descriptor, const std::string& scope, - bool immutable, ClassNameResolver* name_resolver, - absl::flat_hash_map* vars_pointer, - Context* context); -}; - class ImmutableExtensionGenerator : public ExtensionGenerator { public: explicit ImmutableExtensionGenerator(const FieldDescriptor* descriptor, diff --git a/src/google/protobuf/compiler/java/immutable/field_generator.h b/src/google/protobuf/compiler/java/immutable/field_generator.h new file mode 100644 index 0000000000..eb7a897ae5 --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/field_generator.h @@ -0,0 +1,54 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ + +#include + +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/io/printer.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +class ImmutableFieldGenerator : public FieldGenerator { + public: + ImmutableFieldGenerator() = default; + ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete; + ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete; + ~ImmutableFieldGenerator() override = default; + + virtual int GetMessageBitIndex() const = 0; + virtual int GetBuilderBitIndex() const = 0; + virtual int GetNumBitsForMessage() const = 0; + virtual int GetNumBitsForBuilder() const = 0; + virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; + virtual void GenerateMembers(io::Printer* printer) const = 0; + virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; + virtual void GenerateInitializationCode(io::Printer* printer) const = 0; + virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0; + virtual void GenerateMergingCode(io::Printer* printer) const = 0; + virtual void GenerateBuildingCode(io::Printer* printer) const = 0; + virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0; + virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; + virtual void GenerateFieldBuilderInitializationCode( + io::Printer* printer) const = 0; + virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; + + virtual void GenerateBuilderParsingCodeFromPacked( + io::Printer* printer) const { + ReportUnexpectedPackedFieldsCall(); + } + + virtual void GenerateEqualsCode(io::Printer* printer) const = 0; + virtual void GenerateHashCode(io::Printer* printer) const = 0; + + virtual std::string GetBoxedType() const = 0; +}; + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/java/immutable/generator_factory.cc b/src/google/protobuf/compiler/java/immutable/generator_factory.cc new file mode 100644 index 0000000000..f1b9989449 --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/generator_factory.cc @@ -0,0 +1,67 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// Author: liujisi@google.com (Pherl Liu) + +#include "google/protobuf/compiler/java/generator_factory.h" + +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/immutable/enum.h" +#include "google/protobuf/compiler/java/immutable/extension.h" +#include "google/protobuf/compiler/java/immutable/message.h" +#include "google/protobuf/compiler/java/immutable/service.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +// Factory that creates generators for immutable-default messages. +class ImmutableGeneratorFactory : public GeneratorFactory { + public: + explicit ImmutableGeneratorFactory(Context* context) : context_(context) {} + ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete; + ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) = + delete; + ~ImmutableGeneratorFactory() override = default; + + std::unique_ptr NewMessageGenerator( + const Descriptor* descriptor) const override { + return std::make_unique(descriptor, context_); + } + + std::unique_ptr NewEnumGenerator( + const EnumDescriptor* descriptor) const override { + return std::make_unique(descriptor, true, context_); + } + + std::unique_ptr NewExtensionGenerator( + const FieldDescriptor* descriptor) const override { + return std::make_unique(descriptor, context_); + } + + std::unique_ptr NewServiceGenerator( + const ServiceDescriptor* descriptor) const override { + return std::make_unique(descriptor, context_); + } + + private: + Context* context_; +}; + +std::unique_ptr MakeImmutableGeneratorFactory( + Context* context) { + return std::make_unique(context); +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/java/immutable/generator_factory.h b/src/google/protobuf/compiler/java/immutable/generator_factory.h new file mode 100644 index 0000000000..b443e454fb --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/generator_factory.h @@ -0,0 +1,19 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__ + +#include "google/protobuf/compiler/java/generator_factory.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +std::unique_ptr MakeImmutableGeneratorFactory( + Context* context); + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__ diff --git a/src/google/protobuf/compiler/java/immutable/make_field_generators.cc b/src/google/protobuf/compiler/java/immutable/make_field_generators.cc new file mode 100644 index 0000000000..7636c493bb --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/make_field_generators.cc @@ -0,0 +1,114 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// Author: kenton@google.com (Kenton Varda) +// Based on original Protocol Buffers design by +// Sanjay Ghemawat, Jeff Dean, and others. + +#include +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/compiler/java/immutable/enum_field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/compiler/java/immutable/map_field.h" +#include "google/protobuf/compiler/java/immutable/message_field.h" +#include "google/protobuf/compiler/java/immutable/primitive_field.h" +#include "google/protobuf/compiler/java/immutable/string_field.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +namespace { + +std::unique_ptr MakeImmutableGenerator( + const FieldDescriptor* field, int messageBitIndex, int builderBitIndex, + Context* context) { + if (field->is_repeated()) { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + if (IsMapEntry(field->message_type())) { + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + } else { + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + } + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + } + } else { + if (IsRealOneof(field)) { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + } + } else { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, builderBitIndex, context); + } + } + } +} + +} // namespace + +FieldGeneratorMap MakeImmutableFieldGenerators( + const Descriptor* descriptor, Context* context) { + // Construct all the FieldGenerators and assign them bit indices for their + // bit fields. + int messageBitIndex = 0; + int builderBitIndex = 0; + FieldGeneratorMap ret(descriptor); + for (int i = 0; i < descriptor->field_count(); i++) { + const FieldDescriptor* field = descriptor->field(i); + auto generator = MakeImmutableGenerator(field, messageBitIndex, + builderBitIndex, context); + messageBitIndex += generator->GetNumBitsForMessage(); + builderBitIndex += generator->GetNumBitsForBuilder(); + ret.Add(field, std::move(generator)); + } + return ret; +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/java/immutable/make_field_generators.h b/src/google/protobuf/compiler/java/immutable/make_field_generators.h new file mode 100644 index 0000000000..a5dab5dbec --- /dev/null +++ b/src/google/protobuf/compiler/java/immutable/make_field_generators.h @@ -0,0 +1,29 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__ + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +FieldGeneratorMap MakeImmutableFieldGenerators( + const Descriptor* descriptor, Context* context); + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__ diff --git a/src/google/protobuf/compiler/java/map_field.cc b/src/google/protobuf/compiler/java/immutable/map_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/map_field.cc rename to src/google/protobuf/compiler/java/immutable/map_field.cc index 543aefb75d..02e85874c0 100644 --- a/src/google/protobuf/compiler/java/map_field.cc +++ b/src/google/protobuf/compiler/java/immutable/map_field.cc @@ -5,7 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include "google/protobuf/compiler/java/map_field.h" +#include "google/protobuf/compiler/java/immutable/map_field.h" #include @@ -13,7 +13,7 @@ #include "absl/strings/str_join.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/map_field.h b/src/google/protobuf/compiler/java/immutable/map_field.h similarity index 93% rename from src/google/protobuf/compiler/java/map_field.h rename to src/google/protobuf/compiler/java/immutable/map_field.h index d420a64091..72613f8659 100644 --- a/src/google/protobuf/compiler/java/map_field.h +++ b/src/google/protobuf/compiler/java/immutable/map_field.h @@ -8,7 +8,10 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_H__ -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/io/printer.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message.cc b/src/google/protobuf/compiler/java/immutable/message.cc similarity index 98% rename from src/google/protobuf/compiler/java/message.cc rename to src/google/protobuf/compiler/java/immutable/message.cc index 8f13cca6db..0d75c69efb 100644 --- a/src/google/protobuf/compiler/java/message.cc +++ b/src/google/protobuf/compiler/java/immutable/message.cc @@ -9,24 +9,29 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message.h" +#include "google/protobuf/compiler/java/immutable/message.h" #include #include #include +#include #include +#include "absl/container/flat_hash_map.h" +#include "absl/log/absl_check.h" #include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include "absl/strings/substitute.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/enum.h" -#include "google/protobuf/compiler/java/extension.h" -#include "google/protobuf/compiler/java/generator_factory.h" +#include "google/protobuf/compiler/java/field_common.h" +#include "google/protobuf/compiler/java/generator_common.h" #include "google/protobuf/compiler/java/helpers.h" -#include "google/protobuf/compiler/java/message_builder.h" -#include "google/protobuf/compiler/java/message_builder_lite.h" +#include "google/protobuf/compiler/java/immutable/enum.h" +#include "google/protobuf/compiler/java/immutable/extension.h" +#include "google/protobuf/compiler/java/immutable/make_field_generators.h" +#include "google/protobuf/compiler/java/immutable/message_builder.h" #include "google/protobuf/compiler/java/message_serialization.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.h" @@ -75,7 +80,7 @@ ImmutableMessageGenerator::ImmutableMessageGenerator( : MessageGenerator(descriptor), context_(context), name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + field_generators_(MakeImmutableFieldGenerators(descriptor, context_)) { ABSL_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; @@ -362,7 +367,8 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { // Nested types for (int i = 0; i < descriptor_->enum_type_count(); i++) { - EnumGenerator(descriptor_->enum_type(i), true, context_).Generate(printer); + EnumNonLiteGenerator(descriptor_->enum_type(i), true, context_) + .Generate(printer); } for (int i = 0; i < descriptor_->nested_type_count(); i++) { @@ -593,8 +599,9 @@ void ImmutableMessageGenerator::GenerateMessageSerializationMethods( } } - GenerateSerializeFieldsAndExtensions(printer, field_generators_, descriptor_, - sorted_fields.get()); + GenerateSerializeFieldsAndExtensions(printer, + field_generators_.field_generators(), + descriptor_, sorted_fields.get()); if (descriptor_->options().message_set_wire_format()) { printer->Print("getUnknownFields().writeAsMessageSetTo(output);\n"); diff --git a/src/google/protobuf/compiler/java/message.h b/src/google/protobuf/compiler/java/immutable/message.h similarity index 65% rename from src/google/protobuf/compiler/java/message.h rename to src/google/protobuf/compiler/java/immutable/message.h index c998b58019..268f191fcd 100644 --- a/src/google/protobuf/compiler/java/message.h +++ b/src/google/protobuf/compiler/java/immutable/message.h @@ -12,10 +12,13 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__ +#include #include +#include -#include "absl/container/btree_map.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/generator_factory.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { @@ -36,44 +39,6 @@ namespace protobuf { namespace compiler { namespace java { -static const int kMaxStaticSize = 1 << 15; // aka 32k - -class MessageGenerator { - public: - explicit MessageGenerator(const Descriptor* descriptor); - MessageGenerator(const MessageGenerator&) = delete; - MessageGenerator& operator=(const MessageGenerator&) = delete; - virtual ~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. - virtual void GenerateStaticVariables(io::Printer* printer, - int* bytecode_estimate) = 0; - - // Output code which initializes the static variables generated by - // GenerateStaticVariables(). Returns an estimate of bytecode size. - virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0; - - // Generate the class itself. - virtual void Generate(io::Printer* printer) = 0; - - // Generates the base interface that both the class and its builder - // implement - virtual void GenerateInterface(io::Printer* printer) = 0; - - // Generate code to register all contained extensions with an - // ExtensionRegistry. - virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0; - virtual void GenerateKotlinDsl(io::Printer* printer) const = 0; - virtual void GenerateKotlinMembers(io::Printer* printer) const = 0; - virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0; - - protected: - const Descriptor* descriptor_; - absl::btree_map oneofs_; -}; - class ImmutableMessageGenerator : public MessageGenerator { public: ImmutableMessageGenerator(const Descriptor* descriptor, Context* context); diff --git a/src/google/protobuf/compiler/java/message_builder.cc b/src/google/protobuf/compiler/java/immutable/message_builder.cc similarity index 97% rename from src/google/protobuf/compiler/java/message_builder.cc rename to src/google/protobuf/compiler/java/immutable/message_builder.cc index 59cf71a1f0..3bc0900c16 100644 --- a/src/google/protobuf/compiler/java/message_builder.cc +++ b/src/google/protobuf/compiler/java/immutable/message_builder.cc @@ -9,21 +9,28 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message_builder.h" +#include "google/protobuf/compiler/java/immutable/message_builder.h" +#include #include +#include #include #include "absl/container/btree_set.h" #include "absl/container/flat_hash_map.h" +#include "absl/log/absl_check.h" #include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/enum.h" -#include "google/protobuf/compiler/java/extension.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/compiler/java/immutable/enum.h" +#include "google/protobuf/compiler/java/immutable/extension.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/compiler/java/immutable/make_field_generators.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/io/printer.h" @@ -73,7 +80,7 @@ MessageBuilderGenerator::MessageBuilderGenerator(const Descriptor* descriptor, : descriptor_(descriptor), context_(context), name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + field_generators_(MakeImmutableFieldGenerators(descriptor, context_)) { ABSL_CHECK(HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A non-lite message generator is used to " "generate lite messages."; diff --git a/src/google/protobuf/compiler/java/message_builder.h b/src/google/protobuf/compiler/java/immutable/message_builder.h similarity index 94% rename from src/google/protobuf/compiler/java/message_builder.h rename to src/google/protobuf/compiler/java/immutable/message_builder.h index dd5e78391e..ad2b378721 100644 --- a/src/google/protobuf/compiler/java/message_builder.h +++ b/src/google/protobuf/compiler/java/immutable/message_builder.h @@ -12,10 +12,13 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_BUILDER_H__ +#include #include +#include #include "absl/container/btree_map.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message_field.cc b/src/google/protobuf/compiler/java/immutable/message_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/message_field.cc rename to src/google/protobuf/compiler/java/immutable/message_field.cc index 84638c9804..1b76fc2f63 100644 --- a/src/google/protobuf/compiler/java/message_field.cc +++ b/src/google/protobuf/compiler/java/immutable/message_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message_field.h" +#include "google/protobuf/compiler/java/immutable/message_field.h" #include @@ -17,10 +17,10 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" -#include "google/protobuf/wire_format.h" // Must be last. #include "google/protobuf/port_def.inc" diff --git a/src/google/protobuf/compiler/java/message_field.h b/src/google/protobuf/compiler/java/immutable/message_field.h similarity index 98% rename from src/google/protobuf/compiler/java/message_field.h rename to src/google/protobuf/compiler/java/immutable/message_field.h index bc9972f2c0..ee3c4e5289 100644 --- a/src/google/protobuf/compiler/java/message_field.h +++ b/src/google/protobuf/compiler/java/immutable/message_field.h @@ -14,7 +14,8 @@ #include -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" namespace google { diff --git a/src/google/protobuf/compiler/java/primitive_field.cc b/src/google/protobuf/compiler/java/immutable/primitive_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/primitive_field.cc rename to src/google/protobuf/compiler/java/immutable/primitive_field.cc index c9535a62f1..eb31a7b756 100644 --- a/src/google/protobuf/compiler/java/primitive_field.cc +++ b/src/google/protobuf/compiler/java/immutable/primitive_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/primitive_field.h" +#include "google/protobuf/compiler/java/immutable/primitive_field.h" #include #include @@ -19,6 +19,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.h" diff --git a/src/google/protobuf/compiler/java/primitive_field.h b/src/google/protobuf/compiler/java/immutable/primitive_field.h similarity index 98% rename from src/google/protobuf/compiler/java/primitive_field.h rename to src/google/protobuf/compiler/java/immutable/primitive_field.h index 93ba9d4721..9aa52bfb8e 100644 --- a/src/google/protobuf/compiler/java/primitive_field.h +++ b/src/google/protobuf/compiler/java/immutable/primitive_field.h @@ -14,7 +14,8 @@ #include -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/service.cc b/src/google/protobuf/compiler/java/immutable/service.cc similarity index 98% rename from src/google/protobuf/compiler/java/service.cc rename to src/google/protobuf/compiler/java/immutable/service.cc index 3769ff0855..f93f9136d7 100644 --- a/src/google/protobuf/compiler/java/service.cc +++ b/src/google/protobuf/compiler/java/immutable/service.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/service.h" +#include "google/protobuf/compiler/java/immutable/service.h" #include "absl/log/absl_log.h" #include "absl/strings/str_cat.h" @@ -27,12 +27,6 @@ namespace protobuf { namespace compiler { namespace java { -ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor) - : descriptor_(descriptor) {} - -ServiceGenerator::~ServiceGenerator() {} - -// =================================================================== ImmutableServiceGenerator::ImmutableServiceGenerator( const ServiceDescriptor* descriptor, Context* context) : ServiceGenerator(descriptor), diff --git a/src/google/protobuf/compiler/java/service.h b/src/google/protobuf/compiler/java/immutable/service.h similarity index 87% rename from src/google/protobuf/compiler/java/service.h rename to src/google/protobuf/compiler/java/immutable/service.h index f963144a2c..6a2479f849 100644 --- a/src/google/protobuf/compiler/java/service.h +++ b/src/google/protobuf/compiler/java/immutable/service.h @@ -12,6 +12,7 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_H__ +#include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/descriptor.h" namespace google { @@ -33,22 +34,6 @@ namespace protobuf { namespace compiler { namespace java { -class ServiceGenerator { - public: - explicit ServiceGenerator(const ServiceDescriptor* descriptor); - ServiceGenerator(const ServiceGenerator&) = delete; - ServiceGenerator& operator=(const ServiceGenerator&) = delete; - virtual ~ServiceGenerator(); - - virtual void Generate(io::Printer* printer) = 0; - - enum RequestOrResponse { REQUEST, RESPONSE }; - enum IsAbstract { IS_ABSTRACT, IS_CONCRETE }; - - protected: - const ServiceDescriptor* descriptor_; -}; - class ImmutableServiceGenerator : public ServiceGenerator { public: ImmutableServiceGenerator(const ServiceDescriptor* descriptor, diff --git a/src/google/protobuf/compiler/java/string_field.cc b/src/google/protobuf/compiler/java/immutable/string_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/string_field.cc rename to src/google/protobuf/compiler/java/immutable/string_field.cc index 11b06ff4fd..c9ac5f4e7c 100644 --- a/src/google/protobuf/compiler/java/string_field.cc +++ b/src/google/protobuf/compiler/java/immutable/string_field.cc @@ -10,7 +10,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/string_field.h" +#include "google/protobuf/compiler/java/immutable/string_field.h" #include #include @@ -20,6 +20,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/string_field.h b/src/google/protobuf/compiler/java/immutable/string_field.h similarity index 98% rename from src/google/protobuf/compiler/java/string_field.h rename to src/google/protobuf/compiler/java/immutable/string_field.h index 2c3c5b342a..cf95be0da8 100644 --- a/src/google/protobuf/compiler/java/string_field.h +++ b/src/google/protobuf/compiler/java/immutable/string_field.h @@ -15,7 +15,8 @@ #include -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/immutable/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/lite/BUILD.bazel b/src/google/protobuf/compiler/java/lite/BUILD.bazel new file mode 100644 index 0000000000..7904d4b42e --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/BUILD.bazel @@ -0,0 +1,77 @@ +cc_library( + name = "field_generators", + srcs = [ + "enum_field.cc", + "extension.cc", + "make_field_generators.cc", + "map_field.cc", + "message_field.cc", + "primitive_field.cc", + "string_field.cc", + ], + hdrs = [ + "field_generator.h", + "make_field_generators.h", + # We don't actually want to put the remaining headers in `hdrs`. + # They are logically private, and should be in srcs=[], but + # unfortunately `strip_include_prefix` doesn't have any effect + # on headers listed in `srcs.` + "map_field.h", + "enum_field.h", + "extension.h", + "message_field.h", + "primitive_field.h", + "string_field.h", + ], + strip_include_prefix = "/src", + visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], + deps = [ + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/log:absl_log", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "lite", + srcs = [ + "enum.cc", + "extension.cc", + "generator_factory.cc", + "message.cc", + "message_builder.cc", + ], + hdrs = [ + "generator_factory.h", + # We don't actually want to put the remaining headers in `hdrs`. + # They are logically private, and should be in srcs=[], but + # unfortunately `strip_include_prefix` doesn't have any effect + # on headers listed in `srcs.` + "enum.h", + "extension.h", + "message.h", + "message_builder.h", + ], + strip_include_prefix = "/src", + visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], + deps = [ + ":field_generators", + "//src/google/protobuf", + "//src/google/protobuf:port", + "//src/google/protobuf/compiler/java:generator_common", + "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/compiler/java/immutable:service", + "//src/google/protobuf/io", + "//src/google/protobuf/io:printer", + "@com_google_absl//absl/container:btree", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/strings", + ], +) diff --git a/src/google/protobuf/compiler/java/enum_lite.cc b/src/google/protobuf/compiler/java/lite/enum.cc similarity index 99% rename from src/google/protobuf/compiler/java/enum_lite.cc rename to src/google/protobuf/compiler/java/lite/enum.cc index 40fde77e8b..3b42a12d6e 100644 --- a/src/google/protobuf/compiler/java/enum_lite.cc +++ b/src/google/protobuf/compiler/java/lite/enum.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/enum_lite.h" +#include "google/protobuf/compiler/java/lite/enum.h" #include diff --git a/src/google/protobuf/compiler/java/enum_lite.h b/src/google/protobuf/compiler/java/lite/enum.h similarity index 92% rename from src/google/protobuf/compiler/java/enum_lite.h rename to src/google/protobuf/compiler/java/lite/enum.h index 9a9af55973..10a7f86cdd 100644 --- a/src/google/protobuf/compiler/java/enum_lite.h +++ b/src/google/protobuf/compiler/java/lite/enum.h @@ -15,6 +15,7 @@ #include #include +#include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/descriptor.h" namespace google { @@ -36,7 +37,7 @@ namespace protobuf { namespace compiler { namespace java { -class EnumLiteGenerator { +class EnumLiteGenerator : public EnumGenerator { public: EnumLiteGenerator(const EnumDescriptor* descriptor, bool immutable_api, Context* context); @@ -44,7 +45,7 @@ class EnumLiteGenerator { EnumLiteGenerator& operator=(const EnumLiteGenerator&) = delete; ~EnumLiteGenerator(); - void Generate(io::Printer* printer); + void Generate(io::Printer* printer) override; private: const EnumDescriptor* descriptor_; diff --git a/src/google/protobuf/compiler/java/enum_field_lite.cc b/src/google/protobuf/compiler/java/lite/enum_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/enum_field_lite.cc rename to src/google/protobuf/compiler/java/lite/enum_field.cc index b86101f5f5..f2fb340b1b 100644 --- a/src/google/protobuf/compiler/java/enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/lite/enum_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/enum_field_lite.h" +#include "google/protobuf/compiler/java/lite/enum_field.h" #include #include @@ -20,6 +20,7 @@ #include "absl/strings/str_join.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/enum_field_lite.h b/src/google/protobuf/compiler/java/lite/enum_field.h similarity index 97% rename from src/google/protobuf/compiler/java/enum_field_lite.h rename to src/google/protobuf/compiler/java/lite/enum_field.h index 95a55728b5..5bcc00dba5 100644 --- a/src/google/protobuf/compiler/java/enum_field_lite.h +++ b/src/google/protobuf/compiler/java/lite/enum_field.h @@ -16,7 +16,8 @@ #include #include "absl/container/flat_hash_map.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/extension_lite.cc b/src/google/protobuf/compiler/java/lite/extension.cc similarity index 98% rename from src/google/protobuf/compiler/java/extension_lite.cc rename to src/google/protobuf/compiler/java/lite/extension.cc index b32e5d9cd7..9252955b5d 100644 --- a/src/google/protobuf/compiler/java/extension_lite.cc +++ b/src/google/protobuf/compiler/java/lite/extension.cc @@ -5,7 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include "google/protobuf/compiler/java/extension_lite.h" +#include "google/protobuf/compiler/java/lite/extension.h" #include diff --git a/src/google/protobuf/compiler/java/extension_lite.h b/src/google/protobuf/compiler/java/lite/extension.h similarity index 94% rename from src/google/protobuf/compiler/java/extension_lite.h rename to src/google/protobuf/compiler/java/lite/extension.h index ba95956614..46f3b21782 100644 --- a/src/google/protobuf/compiler/java/extension_lite.h +++ b/src/google/protobuf/compiler/java/lite/extension.h @@ -10,7 +10,8 @@ #include -#include "google/protobuf/compiler/java/extension.h" +#include "google/protobuf/compiler/java/generator_factory.h" +#include "google/protobuf/descriptor.h" #include "google/protobuf/port.h" namespace google { diff --git a/src/google/protobuf/compiler/java/lite/field_generator.h b/src/google/protobuf/compiler/java/lite/field_generator.h new file mode 100644 index 0000000000..7e34f11323 --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/field_generator.h @@ -0,0 +1,38 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ + +#include + +#include "google/protobuf/io/printer.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +class ImmutableFieldLiteGenerator { + public: + ImmutableFieldLiteGenerator() = default; + ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete; + ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) = + delete; + virtual ~ImmutableFieldLiteGenerator() = default; + + virtual int GetNumBitsForMessage() const = 0; + virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; + virtual void GenerateMembers(io::Printer* printer) const = 0; + virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; + virtual void GenerateInitializationCode(io::Printer* printer) const = 0; + virtual void GenerateFieldInfo(io::Printer* printer, + std::vector* output) const = 0; + virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; + + virtual std::string GetBoxedType() const = 0; +}; + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ diff --git a/src/google/protobuf/compiler/java/lite/generator_factory.cc b/src/google/protobuf/compiler/java/lite/generator_factory.cc new file mode 100644 index 0000000000..c3f52fe92a --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/generator_factory.cc @@ -0,0 +1,70 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// Author: liujisi@google.com (Pherl Liu) + +#include "google/protobuf/compiler/java/generator_factory.h" + +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/immutable/service.h" +#include "google/protobuf/compiler/java/lite/enum.h" +#include "google/protobuf/compiler/java/lite/extension.h" +#include "google/protobuf/compiler/java/lite/message.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +// Factory that creates generators for immutable-default messages. +class ImmutableLiteGeneratorFactory : public GeneratorFactory { + public: + explicit ImmutableLiteGeneratorFactory(Context* context) + : context_(context) {} + ImmutableLiteGeneratorFactory(const ImmutableLiteGeneratorFactory&) = delete; + ImmutableLiteGeneratorFactory& operator=( + const ImmutableLiteGeneratorFactory&) = delete; + ~ImmutableLiteGeneratorFactory() override = default; + + std::unique_ptr NewMessageGenerator( + const Descriptor* descriptor) const override { + return std::make_unique(descriptor, + context_); + } + + std::unique_ptr NewEnumGenerator( + const EnumDescriptor* descriptor) const override { + return std::make_unique(descriptor, true, context_); + } + + std::unique_ptr NewExtensionGenerator( + const FieldDescriptor* descriptor) const override { + return std::make_unique(descriptor, + context_); + } + + std::unique_ptr NewServiceGenerator( + const ServiceDescriptor* descriptor) const override { + return std::make_unique(descriptor, context_); + } + + private: + Context* context_; +}; + +std::unique_ptr MakeImmutableLiteGeneratorFactory( + Context* context) { + return std::make_unique(context); +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/java/lite/generator_factory.h b/src/google/protobuf/compiler/java/lite/generator_factory.h new file mode 100644 index 0000000000..cc3309cc3a --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/generator_factory.h @@ -0,0 +1,19 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__ + +#include "google/protobuf/compiler/java/generator_factory.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +std::unique_ptr MakeImmutableLiteGeneratorFactory( + Context* context); + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__ diff --git a/src/google/protobuf/compiler/java/lite/make_field_generators.cc b/src/google/protobuf/compiler/java/lite/make_field_generators.cc new file mode 100644 index 0000000000..6071662c58 --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/make_field_generators.cc @@ -0,0 +1,110 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// Author: kenton@google.com (Kenton Varda) +// Based on original Protocol Buffers design by +// Sanjay Ghemawat, Jeff Dean, and others. + +#include "google/protobuf/compiler/java/lite/make_field_generators.h" + +#include +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/compiler/java/lite/enum_field.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/compiler/java/lite/map_field.h" +#include "google/protobuf/compiler/java/lite/message_field.h" +#include "google/protobuf/compiler/java/lite/primitive_field.h" +#include "google/protobuf/compiler/java/lite/string_field.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +namespace { + +std::unique_ptr CreateImmutableLiteGenerator( + const FieldDescriptor* field, int messageBitIndex, Context* context) { + if (field->is_repeated()) { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + if (IsMapEntry(field->message_type())) { + return std::make_unique( + field, messageBitIndex, context); + } else { + return std::make_unique( + field, messageBitIndex, context); + } + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, context); + } + } else { + if (IsRealOneof(field)) { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + return std::make_unique( + field, messageBitIndex, context); + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, context); + } + } else { + switch (GetJavaType(field)) { + case JAVATYPE_MESSAGE: + return std::make_unique( + field, messageBitIndex, context); + case JAVATYPE_ENUM: + return std::make_unique( + field, messageBitIndex, context); + case JAVATYPE_STRING: + return std::make_unique( + field, messageBitIndex, context); + default: + return std::make_unique( + field, messageBitIndex, context); + } + } + } +} + +} // namespace + +FieldGeneratorMap MakeImmutableFieldLiteGenerators( + const Descriptor* descriptor, Context* context) { + int messageBitIndex = 0; + FieldGeneratorMap field_generators(descriptor); + for (int i = 0; i < descriptor->field_count(); i++) { + const FieldDescriptor* field = descriptor->field(i); + auto generator = + CreateImmutableLiteGenerator(field, messageBitIndex, context); + messageBitIndex += generator->GetNumBitsForMessage(); + field_generators.Add(field, std::move(generator)); + } + return field_generators; +} + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/java/lite/make_field_generators.h b/src/google/protobuf/compiler/java/lite/make_field_generators.h new file mode 100644 index 0000000000..436c3f896e --- /dev/null +++ b/src/google/protobuf/compiler/java/lite/make_field_generators.h @@ -0,0 +1,32 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__ +#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__ + +#include +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { + +FieldGeneratorMap MakeImmutableFieldLiteGenerators( + const Descriptor* descriptor, Context* context); + +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__ diff --git a/src/google/protobuf/compiler/java/map_field_lite.cc b/src/google/protobuf/compiler/java/lite/map_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/map_field_lite.cc rename to src/google/protobuf/compiler/java/lite/map_field.cc index d854899576..74fc93f347 100644 --- a/src/google/protobuf/compiler/java/map_field_lite.cc +++ b/src/google/protobuf/compiler/java/lite/map_field.cc @@ -5,13 +5,14 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include "google/protobuf/compiler/java/map_field_lite.h" +#include "google/protobuf/compiler/java/lite/map_field.h" #include #include #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/map_field_lite.h b/src/google/protobuf/compiler/java/lite/map_field.h similarity index 86% rename from src/google/protobuf/compiler/java/map_field_lite.h rename to src/google/protobuf/compiler/java/lite/map_field.h index 8a2837bc8e..eb72891ac5 100644 --- a/src/google/protobuf/compiler/java/map_field_lite.h +++ b/src/google/protobuf/compiler/java/lite/map_field.h @@ -9,8 +9,14 @@ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MAP_FIELD_LITE_H__ #include +#include +#include -#include "google/protobuf/compiler/java/field.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message_lite.cc b/src/google/protobuf/compiler/java/lite/message.cc similarity index 98% rename from src/google/protobuf/compiler/java/message_lite.cc rename to src/google/protobuf/compiler/java/lite/message.cc index f90e6d830f..3363162dc1 100644 --- a/src/google/protobuf/compiler/java/message_lite.cc +++ b/src/google/protobuf/compiler/java/lite/message.cc @@ -9,27 +9,32 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message_lite.h" +#include "google/protobuf/compiler/java/lite/message.h" #include +#include #include #include #include +#include #include #include "absl/container/flat_hash_map.h" +#include "absl/log/absl_check.h" #include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" #include "absl/strings/substitute.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/enum_lite.h" -#include "google/protobuf/compiler/java/extension_lite.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/generator.h" #include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/compiler/java/helpers.h" -#include "google/protobuf/compiler/java/message_builder.h" -#include "google/protobuf/compiler/java/message_builder_lite.h" +#include "google/protobuf/compiler/java/lite/enum.h" +#include "google/protobuf/compiler/java/lite/extension.h" +#include "google/protobuf/compiler/java/lite/make_field_generators.h" +#include "google/protobuf/compiler/java/lite/message_builder.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/io/coded_stream.h" @@ -53,7 +58,7 @@ ImmutableMessageLiteGenerator::ImmutableMessageLiteGenerator( : MessageGenerator(descriptor), context_(context), name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + field_generators_(MakeImmutableFieldLiteGenerators(descriptor, context)) { ABSL_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; diff --git a/src/google/protobuf/compiler/java/message_lite.h b/src/google/protobuf/compiler/java/lite/message.h similarity index 85% rename from src/google/protobuf/compiler/java/message_lite.h rename to src/google/protobuf/compiler/java/lite/message.h index b7bebc0849..a20cb251ec 100644 --- a/src/google/protobuf/compiler/java/message_lite.h +++ b/src/google/protobuf/compiler/java/lite/message.h @@ -12,8 +12,16 @@ #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ #define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_LITE_H__ -#include "google/protobuf/compiler/java/field.h" -#include "google/protobuf/compiler/java/message.h" +#include +#include + +#include "google/protobuf/compiler/java/context.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/generator_factory.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/compiler/java/name_resolver.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/io/printer.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message_builder_lite.cc b/src/google/protobuf/compiler/java/lite/message_builder.cc similarity index 91% rename from src/google/protobuf/compiler/java/message_builder_lite.cc rename to src/google/protobuf/compiler/java/lite/message_builder.cc index bcc1cc50ee..9ae4081555 100644 --- a/src/google/protobuf/compiler/java/message_builder_lite.cc +++ b/src/google/protobuf/compiler/java/lite/message_builder.cc @@ -9,27 +9,21 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message_builder_lite.h" +#include "google/protobuf/compiler/java/lite/message_builder.h" -#include -#include #include -#include #include "absl/container/flat_hash_map.h" #include "absl/strings/str_cat.h" -#include "absl/strings/substitute.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" -#include "google/protobuf/compiler/java/enum.h" -#include "google/protobuf/compiler/java/extension.h" -#include "google/protobuf/compiler/java/generator_factory.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/compiler/java/lite/enum.h" +#include "google/protobuf/compiler/java/lite/make_field_generators.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/descriptor.pb.h" -#include "google/protobuf/io/coded_stream.h" #include "google/protobuf/io/printer.h" -#include "google/protobuf/wire_format.h" // Must be last. #include "google/protobuf/port_def.inc" @@ -44,7 +38,7 @@ MessageBuilderLiteGenerator::MessageBuilderLiteGenerator( : descriptor_(descriptor), context_(context), name_resolver_(context->GetNameResolver()), - field_generators_(descriptor, context_) { + field_generators_(MakeImmutableFieldLiteGenerators(descriptor, context)) { ABSL_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite())) << "Generator factory error: A lite message generator is used to " "generate non-lite messages."; diff --git a/src/google/protobuf/compiler/java/message_builder_lite.h b/src/google/protobuf/compiler/java/lite/message_builder.h similarity index 93% rename from src/google/protobuf/compiler/java/message_builder_lite.h rename to src/google/protobuf/compiler/java/lite/message_builder.h index 30dce624f6..f7d897c93d 100644 --- a/src/google/protobuf/compiler/java/message_builder_lite.h +++ b/src/google/protobuf/compiler/java/lite/message_builder.h @@ -15,7 +15,8 @@ #include #include "absl/container/btree_map.h" -#include "google/protobuf/compiler/java/field.h" +#include "google/protobuf/compiler/java/generator_common.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message_field_lite.cc b/src/google/protobuf/compiler/java/lite/message_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/message_field_lite.cc rename to src/google/protobuf/compiler/java/lite/message_field.cc index 45997ef324..7c5cc3cf77 100644 --- a/src/google/protobuf/compiler/java/message_field_lite.cc +++ b/src/google/protobuf/compiler/java/lite/message_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/message_field_lite.h" +#include "google/protobuf/compiler/java/lite/message_field.h" #include #include @@ -17,6 +17,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/io/printer.h" diff --git a/src/google/protobuf/compiler/java/message_field_lite.h b/src/google/protobuf/compiler/java/lite/message_field.h similarity index 95% rename from src/google/protobuf/compiler/java/message_field_lite.h rename to src/google/protobuf/compiler/java/lite/message_field.h index 27eb1fe8eb..495b3933b9 100644 --- a/src/google/protobuf/compiler/java/message_field_lite.h +++ b/src/google/protobuf/compiler/java/lite/message_field.h @@ -14,8 +14,12 @@ #include #include +#include -#include "google/protobuf/compiler/java/field.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/primitive_field_lite.cc b/src/google/protobuf/compiler/java/lite/primitive_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/primitive_field_lite.cc rename to src/google/protobuf/compiler/java/lite/primitive_field.cc index a794b2943a..69924c9c83 100644 --- a/src/google/protobuf/compiler/java/primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/lite/primitive_field.cc @@ -9,7 +9,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/primitive_field_lite.h" +#include "google/protobuf/compiler/java/lite/primitive_field.h" #include #include @@ -19,6 +19,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/wire_format.h" diff --git a/src/google/protobuf/compiler/java/primitive_field_lite.h b/src/google/protobuf/compiler/java/lite/primitive_field.h similarity index 95% rename from src/google/protobuf/compiler/java/primitive_field_lite.h rename to src/google/protobuf/compiler/java/lite/primitive_field.h index d979a65e1e..56b6fc7b77 100644 --- a/src/google/protobuf/compiler/java/primitive_field_lite.h +++ b/src/google/protobuf/compiler/java/lite/primitive_field.h @@ -14,8 +14,12 @@ #include #include +#include -#include "google/protobuf/compiler/java/field.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/string_field_lite.cc b/src/google/protobuf/compiler/java/lite/string_field.cc similarity index 99% rename from src/google/protobuf/compiler/java/string_field_lite.cc rename to src/google/protobuf/compiler/java/lite/string_field.cc index bbe99d8711..833f415585 100644 --- a/src/google/protobuf/compiler/java/string_field_lite.cc +++ b/src/google/protobuf/compiler/java/lite/string_field.cc @@ -10,7 +10,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. -#include "google/protobuf/compiler/java/string_field_lite.h" +#include "google/protobuf/compiler/java/lite/string_field.h" #include #include @@ -20,6 +20,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/doc_comment.h" +#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/wire_format.h" diff --git a/src/google/protobuf/compiler/java/string_field_lite.h b/src/google/protobuf/compiler/java/lite/string_field.h similarity index 95% rename from src/google/protobuf/compiler/java/string_field_lite.h rename to src/google/protobuf/compiler/java/lite/string_field.h index 93095b5fa3..59291869c0 100644 --- a/src/google/protobuf/compiler/java/string_field_lite.h +++ b/src/google/protobuf/compiler/java/lite/string_field.h @@ -15,8 +15,12 @@ #include #include +#include -#include "google/protobuf/compiler/java/field.h" +#include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" +#include "google/protobuf/compiler/java/lite/field_generator.h" +#include "google/protobuf/descriptor.h" namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/java/message_serialization.h b/src/google/protobuf/compiler/java/message_serialization.h index 733172a823..3ebb099ded 100644 --- a/src/google/protobuf/compiler/java/message_serialization.h +++ b/src/google/protobuf/compiler/java/message_serialization.h @@ -12,8 +12,8 @@ #include #include -#include "google/protobuf/compiler/java/field.h" -#include "google/protobuf/compiler/java/helpers.h" +#include "absl/types/span.h" +#include "google/protobuf/compiler/java/generator_common.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" @@ -29,12 +29,9 @@ void GenerateSerializeExtensionRange(io::Printer* printer, // Generates code to serialize all fields and extension ranges for the specified // message descriptor, sorting serialization calls in increasing order by field // number. -// -// Templatized to support different field generator implementations. -template -void GenerateSerializeFieldsAndExtensions( +inline void GenerateSerializeFieldsAndExtensions( io::Printer* printer, - const FieldGeneratorMap& field_generators, + const std::vector& field_generators, const Descriptor* descriptor, const FieldDescriptor** sorted_fields) { std::vector sorted_extensions; sorted_extensions.reserve(descriptor->extension_range_count()); @@ -62,7 +59,8 @@ void GenerateSerializeFieldsAndExtensions( if (range != nullptr) { GenerateSerializeExtensionRange(printer, range); } - field_generators.get(field).GenerateSerializationCode(printer); + size_t idx = static_cast(field->index()); + field_generators[idx]->GenerateSerializationCode(printer); } // After serializing all fields, serialize any remaining extensions via a