From d3cbd2154ee96cd1ff42fc6c48e871a9c00ad712 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Thu, 3 Oct 2024 15:22:52 -0700 Subject: [PATCH] Merge kotlin dsl field generators and move them into kotlin/ sub folder PiperOrigin-RevId: 682052240 --- src/google/protobuf/compiler/java/BUILD.bazel | 5 +- .../protobuf/compiler/java/full/enum_field.cc | 153 --- .../protobuf/compiler/java/full/enum_field.h | 2 - .../protobuf/compiler/java/full/map_field.cc | 108 --- .../protobuf/compiler/java/full/map_field.h | 1 - .../compiler/java/full/message_field.cc | 152 --- .../compiler/java/full/message_field.h | 5 - .../compiler/java/full/primitive_field.cc | 153 --- .../compiler/java/full/primitive_field.h | 2 - .../compiler/java/full/string_field.cc | 148 --- .../compiler/java/full/string_field.h | 2 - .../protobuf/compiler/java/generator_common.h | 1 - .../protobuf/compiler/java/lite/enum_field.cc | 186 ---- .../protobuf/compiler/java/lite/enum_field.h | 2 - .../protobuf/compiler/java/lite/map_field.cc | 124 --- .../protobuf/compiler/java/lite/map_field.h | 1 - .../compiler/java/lite/message_field.cc | 181 ---- .../compiler/java/lite/message_field.h | 3 - .../compiler/java/lite/primitive_field.cc | 184 ---- .../compiler/java/lite/primitive_field.h | 2 - .../compiler/java/lite/string_field.cc | 175 ---- .../compiler/java/lite/string_field.h | 2 - .../protobuf/compiler/kotlin/BUILD.bazel | 4 + src/google/protobuf/compiler/kotlin/field.cc | 868 ++++++++++++++++++ src/google/protobuf/compiler/kotlin/field.h | 50 + .../protobuf/compiler/kotlin/message.cc | 39 +- src/google/protobuf/compiler/kotlin/message.h | 9 +- 27 files changed, 939 insertions(+), 1623 deletions(-) create mode 100644 src/google/protobuf/compiler/kotlin/field.cc create mode 100644 src/google/protobuf/compiler/kotlin/field.h diff --git a/src/google/protobuf/compiler/java/BUILD.bazel b/src/google/protobuf/compiler/java/BUILD.bazel index 7bb380d4e8..d9940cd70f 100644 --- a/src/google/protobuf/compiler/java/BUILD.bazel +++ b/src/google/protobuf/compiler/java/BUILD.bazel @@ -71,7 +71,10 @@ cc_library( "internal_helpers.h", ], strip_include_prefix = "/src", - visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], + visibility = [ + "//src/google/protobuf/compiler/java:__subpackages__", + "//src/google/protobuf/compiler/kotlin:__subpackages__", + ], deps = [ ":helpers", ":java_features_bootstrap", diff --git a/src/google/protobuf/compiler/java/full/enum_field.cc b/src/google/protobuf/compiler/java/full/enum_field.cc index e0ea6eea65..5150a21761 100644 --- a/src/google/protobuf/compiler/java/full/enum_field.cc +++ b/src/google/protobuf/compiler/java/full/enum_field.cc @@ -47,7 +47,6 @@ void SetEnumVariables( (*variables)["type"] = name_resolver->GetImmutableClassName(descriptor->enum_type()); - variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])}); (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->enum_type()); (*variables)["default"] = @@ -62,12 +61,6 @@ void SetEnumVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); if (HasHasbit(descriptor)) { // For singular messages and builders, one bit is used for the hasField bit. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); @@ -267,51 +260,6 @@ void ImmutableEnumFieldGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void ImmutableEnumFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - if (SupportUnknownEnumValue(descriptor_)) { - printer->Print( - variables_, - "$kt_deprecation$public var $kt_name$Value: kotlin.Int\n" - " @JvmName(\"${$get$kt_capitalized_name$Value$}$\")\n" - " get() = $kt_dsl_builder$.${$$kt_property_name$Value$}$\n" - " @JvmName(\"${$set$kt_capitalized_name$Value$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_property_name$Value$}$ = value\n" - " }\n"); - } - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutableEnumFieldGenerator::GenerateFieldBuilderInitializationCode( io::Printer* printer) const { // noop for enums @@ -1077,107 +1025,6 @@ void RepeatedImmutableEnumFieldGenerator::GenerateHashCode( "}\n"); } -void RepeatedImmutableEnumFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - printer->Print( - variables_, - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " @kotlin.jvm.JvmSynthetic\n" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - std::string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } diff --git a/src/google/protobuf/compiler/java/full/enum_field.h b/src/google/protobuf/compiler/java/full/enum_field.h index c238fad42c..bfd6184ab5 100644 --- a/src/google/protobuf/compiler/java/full/enum_field.h +++ b/src/google/protobuf/compiler/java/full/enum_field.h @@ -65,7 +65,6 @@ class ImmutableEnumFieldGenerator : public ImmutableFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -131,7 +130,6 @@ class RepeatedImmutableEnumFieldGenerator : public ImmutableEnumFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; }; diff --git a/src/google/protobuf/compiler/java/full/map_field.cc b/src/google/protobuf/compiler/java/full/map_field.cc index be36f7dc16..e081504346 100644 --- a/src/google/protobuf/compiler/java/full/map_field.cc +++ b/src/google/protobuf/compiler/java/full/map_field.cc @@ -42,17 +42,6 @@ std::string TypeName(const FieldDescriptor* field, } } -std::string KotlinTypeName(const FieldDescriptor* field, - ClassNameResolver* name_resolver) { - if (GetJavaType(field) == JAVATYPE_MESSAGE) { - return name_resolver->GetImmutableClassName(field->message_type()); - } else if (GetJavaType(field) == JAVATYPE_ENUM) { - return name_resolver->GetImmutableClassName(field->enum_type()); - } else { - return std::string(KotlinTypeName(GetJavaType(field))); - } -} - std::string WireType(const FieldDescriptor* field) { return absl::StrCat("com.google.protobuf.WireFormat.FieldType.", FieldTypeName(field->type())); @@ -93,8 +82,6 @@ void ImmutableMapFieldGenerator::SetMessageVariables( variables_["key_type"] = TypeName(key, name_resolver, false); std::string boxed_key_type = TypeName(key, name_resolver, true); variables_["boxed_key_type"] = boxed_key_type; - variables_["kt_key_type"] = KotlinTypeName(key, name_resolver); - variables_["kt_value_type"] = KotlinTypeName(value, name_resolver); // Used for calling the serialization function. variables_["short_key_type"] = boxed_key_type.substr(boxed_key_type.rfind('.') + 1); @@ -170,12 +157,6 @@ void ImmutableMapFieldGenerator::SetMessageVariables( // by the proto compiler variables_["deprecation"] = descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables_.insert( - {"kt_deprecation", - descriptor_->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - variables_["name"], " is deprecated\") ") - : ""}); variables_["on_changed"] = "onChanged();"; variables_.insert( @@ -959,95 +940,6 @@ void ImmutableMapFieldGenerator::GenerateMessageMapGetters( printer->Annotate("{", "}", descriptor_); } -void ImmutableMapFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - printer->Print( - variables_, - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " @kotlin.jvm.JvmSynthetic\n" - " @JvmName(\"get$kt_capitalized_name$Map\")\n" - " get() = com.google.protobuf.kotlin.DslMap(\n" - " $kt_dsl_builder$.${$$kt_property_name$Map$}$\n" - " )\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "@JvmName(\"put$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .put(key: $kt_key_type$, value: $kt_value_type$) {\n" - " $kt_dsl_builder$.${$put$capitalized_name$$}$(key, value)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@JvmName(\"set$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .set(key: $kt_key_type$, value: $kt_value_type$) {\n" - " put(key, value)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@JvmName(\"remove$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .remove(key: $kt_key_type$) {\n" - " $kt_dsl_builder$.${$remove$capitalized_name$$}$(key)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@JvmName(\"putAll$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .putAll(map: kotlin.collections.Map<$kt_key_type$, " - "$kt_value_type$>) " - "{\n" - " $kt_dsl_builder$.${$putAll$capitalized_name$$}$(map)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@JvmName(\"clear$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - " }\n"); -} - void ImmutableMapFieldGenerator::GenerateFieldBuilderInitializationCode( io::Printer* printer) const { // Nothing to initialize. diff --git a/src/google/protobuf/compiler/java/full/map_field.h b/src/google/protobuf/compiler/java/full/map_field.h index c41205c4cb..563532491d 100644 --- a/src/google/protobuf/compiler/java/full/map_field.h +++ b/src/google/protobuf/compiler/java/full/map_field.h @@ -44,7 +44,6 @@ class ImmutableMapFieldGenerator : public ImmutableFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/full/message_field.cc b/src/google/protobuf/compiler/java/full/message_field.cc index f26c57b072..80ec9ade69 100644 --- a/src/google/protobuf/compiler/java/full/message_field.cc +++ b/src/google/protobuf/compiler/java/full/message_field.cc @@ -43,7 +43,6 @@ void SetMessageVariables( (*variables)["type"] = name_resolver->GetImmutableClassName(descriptor->message_type()); - variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])}); (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = @@ -53,12 +52,6 @@ void SetMessageVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); (*variables)["on_changed"] = "onChanged();"; (*variables)["get_parser"] = "parser()"; @@ -371,50 +364,6 @@ void ImmutableMessageFieldGenerator::GenerateBuilderMembers( "}\n"); } -void ImmutableMessageFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.clear$capitalized_name$()\n" - "}\n"); - printer->Annotate("{", "}", descriptor_, Semantic::kSet); - - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - - GenerateKotlinOrNull(printer); -} - -void ImmutableMessageFieldGenerator::GenerateKotlinOrNull(io::Printer* printer) const { - if (descriptor_->has_presence() && - descriptor_->real_containing_oneof() == nullptr) { - printer->Print(variables_, - "$kt_deprecation$\n" - "public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n" - " get() = $kt_dsl_builder$.$name$OrNull\n"); - } -} - void ImmutableMessageFieldGenerator::GenerateFieldBuilderInitializationCode( io::Printer* printer) const { printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); @@ -1356,107 +1305,6 @@ std::string RepeatedImmutableMessageFieldGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } -void RepeatedImmutableMessageFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - printer->Print( - variables_, - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " @kotlin.jvm.JvmSynthetic\n" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n\n"); -} - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/full/message_field.h b/src/google/protobuf/compiler/java/full/message_field.h index 709ea4309d..ee9c407d8f 100644 --- a/src/google/protobuf/compiler/java/full/message_field.h +++ b/src/google/protobuf/compiler/java/full/message_field.h @@ -66,7 +66,6 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -87,9 +86,6 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator { const char* trailing_code, absl::optional semantic = absl::nullopt) const; - - private: - void GenerateKotlinOrNull(io::Printer* printer) const; }; class ImmutableMessageOneofFieldGenerator @@ -143,7 +139,6 @@ class RepeatedImmutableMessageFieldGenerator io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/full/primitive_field.cc b/src/google/protobuf/compiler/java/full/primitive_field.cc index b9fe799f4d..0aa274c018 100644 --- a/src/google/protobuf/compiler/java/full/primitive_field.cc +++ b/src/google/protobuf/compiler/java/full/primitive_field.cc @@ -46,7 +46,6 @@ void SetPrimitiveVariables( (*variables)["type"] = std::string(PrimitiveTypeName(javaType)); (*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType)); - (*variables)["kt_type"] = std::string(KotlinTypeName(javaType)); variables->insert({"field_type", (*variables)["type"]}); std::string name = (*variables)["name"]; @@ -100,11 +99,6 @@ void SetPrimitiveVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - (*variables)["kt_deprecation"] = - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", name, - " is deprecated\") ") - : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { (*variables)["fixed_size"] = absl::StrCat(fixed_size); @@ -287,52 +281,6 @@ void ImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( "}\n"); } -void ImmutablePrimitiveFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - if (descriptor_->name() == "is_initialized") { - printer->Print( - variables_, - "// TODO: remove this hack; we should access properties\n" - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n" - " get() = $kt_dsl_builder$.${$get$kt_capitalized_name$$}$()\n" - " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$set$kt_capitalized_name$$}$(value)\n" - " }\n"); - } else { - printer->Print(variables_, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - } - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutablePrimitiveFieldGenerator::GenerateFieldBuilderInitializationCode( io::Printer* printer) const { // noop for primitives @@ -840,107 +788,6 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void RepeatedImmutablePrimitiveFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - printer->Print( - variables_, - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " @kotlin.jvm.JvmSynthetic\n" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - void RepeatedImmutablePrimitiveFieldGenerator:: GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives diff --git a/src/google/protobuf/compiler/java/full/primitive_field.h b/src/google/protobuf/compiler/java/full/primitive_field.h index 6533228ce3..948800bf40 100644 --- a/src/google/protobuf/compiler/java/full/primitive_field.h +++ b/src/google/protobuf/compiler/java/full/primitive_field.h @@ -65,7 +65,6 @@ class ImmutablePrimitiveFieldGenerator : public ImmutableFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -131,7 +130,6 @@ class RepeatedImmutablePrimitiveFieldGenerator io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; }; diff --git a/src/google/protobuf/compiler/java/full/string_field.cc b/src/google/protobuf/compiler/java/full/string_field.cc index d5b5d582bc..3b04552826 100644 --- a/src/google/protobuf/compiler/java/full/string_field.cc +++ b/src/google/protobuf/compiler/java/full/string_field.cc @@ -71,12 +71,6 @@ void SetPrimitiveVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); (*variables)["on_changed"] = "onChanged();"; if (HasHasbit(descriptor)) { @@ -366,39 +360,6 @@ void ImmutableStringFieldGenerator::GenerateBuilderMembers( "}\n"); } -void ImmutableStringFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$public var $kt_name$: kotlin.String\n" - " @JvmName(\"${$get$kt_capitalized_name$$}$\")\n" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " @JvmName(\"${$set$kt_capitalized_name$$}$\")\n" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutableStringFieldGenerator::GenerateFieldBuilderInitializationCode( io::Printer* printer) const { // noop for primitives @@ -970,115 +931,6 @@ void RepeatedImmutableStringFieldGenerator::GenerateBuilderMembers( "}\n"); } -void RepeatedImmutableStringFieldGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - printer->Print( - variables_, - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - // property for List - WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "$kt_deprecation$public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "\n" - " @kotlin.jvm.JvmSynthetic\n" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - // List.add(String) - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"add$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "." - "add(value: kotlin.String) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}\n"); - - // List += String - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssign$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "." - "plusAssign(value: kotlin.String) {\n" - " add(value)\n" - "}\n"); - - // List.addAll(Iterable) - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "." - "addAll(values: kotlin.collections.Iterable) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}\n"); - - // List += Iterable - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "." - "plusAssign(values: kotlin.collections.Iterable) {\n" - " addAll(values)\n" - "}\n"); - - // List[Int] = String - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"set$kt_capitalized_name$\")\n" - "public operator fun com.google.protobuf.kotlin.DslList" - "." - "set(index: kotlin.Int, value: kotlin.String) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print(variables_, - "@kotlin.jvm.JvmSynthetic\n" - "@kotlin.jvm.JvmName(\"clear$kt_capitalized_name$\")\n" - "public fun com.google.protobuf.kotlin.DslList" - "." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - void RepeatedImmutableStringFieldGenerator:: GenerateFieldBuilderInitializationCode(io::Printer* printer) const { // noop for primitives diff --git a/src/google/protobuf/compiler/java/full/string_field.h b/src/google/protobuf/compiler/java/full/string_field.h index 1d2d1b76b5..d9373967f7 100644 --- a/src/google/protobuf/compiler/java/full/string_field.h +++ b/src/google/protobuf/compiler/java/full/string_field.h @@ -64,7 +64,6 @@ class ImmutableStringFieldGenerator : public ImmutableFieldGenerator { io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -129,7 +128,6 @@ class RepeatedImmutableStringFieldGenerator io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; }; diff --git a/src/google/protobuf/compiler/java/generator_common.h b/src/google/protobuf/compiler/java/generator_common.h index 33843a0597..c2c8859d0f 100644 --- a/src/google/protobuf/compiler/java/generator_common.h +++ b/src/google/protobuf/compiler/java/generator_common.h @@ -23,7 +23,6 @@ class FieldGenerator { public: virtual ~FieldGenerator() = default; virtual void GenerateSerializationCode(io::Printer* printer) const = 0; - virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; }; // Convenience class which constructs FieldGenerators for a Descriptor. diff --git a/src/google/protobuf/compiler/java/lite/enum_field.cc b/src/google/protobuf/compiler/java/lite/enum_field.cc index 4d488767f9..a6eda659ed 100644 --- a/src/google/protobuf/compiler/java/lite/enum_field.cc +++ b/src/google/protobuf/compiler/java/lite/enum_field.cc @@ -53,7 +53,6 @@ void SetEnumVariables( (*variables)["type"] = name_resolver->GetImmutableClassName(descriptor->enum_type()); - variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])}); (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->enum_type()); (*variables)["default"] = @@ -68,12 +67,6 @@ void SetEnumVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (HasHasbit(descriptor)) { @@ -298,63 +291,6 @@ void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void ImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, - }, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - if (SupportUnknownEnumValue(descriptor_)) { - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$Value$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$Value$}$", name_ctx); }}, - }, - "$kt_deprecation$public var $kt_name$Value: kotlin.Int\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.${$$kt_property_name$Value$}$\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_property_name$Value$}$ = value\n" - " }\n"); - } - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode( io::Printer* printer) const { if (!IsDefaultValueJavaDefault(descriptor_)) { @@ -903,128 +839,6 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode( printer->Print(variables_, "$name$_ = emptyIntList();\n"); } -void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - printer->Print( - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - "$ jvm_synthetic$" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - std::string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); } diff --git a/src/google/protobuf/compiler/java/lite/enum_field.h b/src/google/protobuf/compiler/java/lite/enum_field.h index 5bcc00dba5..44090d3f6b 100644 --- a/src/google/protobuf/compiler/java/lite/enum_field.h +++ b/src/google/protobuf/compiler/java/lite/enum_field.h @@ -55,7 +55,6 @@ class ImmutableEnumFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -103,7 +102,6 @@ class RepeatedImmutableEnumFieldLiteGenerator void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/lite/map_field.cc b/src/google/protobuf/compiler/java/lite/map_field.cc index 29d285c2e3..433af226ad 100644 --- a/src/google/protobuf/compiler/java/lite/map_field.cc +++ b/src/google/protobuf/compiler/java/lite/map_field.cc @@ -41,17 +41,6 @@ std::string TypeName(const FieldDescriptor* field, } } -std::string KotlinTypeName(const FieldDescriptor* field, - ClassNameResolver* name_resolver) { - if (GetJavaType(field) == JAVATYPE_MESSAGE) { - return name_resolver->GetImmutableClassName(field->message_type()); - } else if (GetJavaType(field) == JAVATYPE_ENUM) { - return name_resolver->GetImmutableClassName(field->enum_type()); - } else { - return std::string(KotlinTypeName(GetJavaType(field))); - } -} - std::string WireType(const FieldDescriptor* field) { return absl::StrCat("com.google.protobuf.WireFormat.FieldType.", FieldTypeName(field->type())); @@ -78,8 +67,6 @@ void SetMessageVariables( (*variables)["key_type"] = TypeName(key, name_resolver, false); (*variables)["boxed_key_type"] = TypeName(key, name_resolver, true); - (*variables)["kt_key_type"] = KotlinTypeName(key, name_resolver); - (*variables)["kt_value_type"] = KotlinTypeName(value, name_resolver); (*variables)["key_wire_type"] = WireType(key); (*variables)["key_default_value"] = DefaultValue(key, true, name_resolver, context->options()); @@ -141,12 +128,6 @@ void SetMessageVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); variables->insert( {"default_entry", absl::StrCat((*variables)["capitalized_name"], @@ -835,111 +816,6 @@ void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers( } } -void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - printer->Print( - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("get$kt_capitalized_name$Map", name_ctx); }}, - }, - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - "$ jvm_synthetic$" - "$jvm_name$" - " get() = com.google.protobuf.kotlin.DslMap(\n" - " $kt_dsl_builder$.${$$kt_property_name$Map$}$\n" - " )\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("put$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .put(key: $kt_key_type$, value: $kt_value_type$) {\n" - " $kt_dsl_builder$.${$put$capitalized_name$$}$(key, value)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .set(key: $kt_key_type$, value: $kt_value_type$) {\n" - " put(key, value)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("remove$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .remove(key: $kt_key_type$) {\n" - " $kt_dsl_builder$.${$remove$capitalized_name$$}$(key)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("putAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .putAll(map: kotlin.collections.Map<$kt_key_type$, $kt_value_type$>) " - "{\n" - " $kt_dsl_builder$.${$putAll$capitalized_name$$}$(map)\n" - " }\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslMap" - "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - " .clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - " }\n"); -} - void ImmutableMapFieldLiteGenerator::GenerateInitializationCode( io::Printer* printer) const { // Nothing to initialize. diff --git a/src/google/protobuf/compiler/java/lite/map_field.h b/src/google/protobuf/compiler/java/lite/map_field.h index eb72891ac5..846ccc28bd 100644 --- a/src/google/protobuf/compiler/java/lite/map_field.h +++ b/src/google/protobuf/compiler/java/lite/map_field.h @@ -38,7 +38,6 @@ class ImmutableMapFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/lite/message_field.cc b/src/google/protobuf/compiler/java/lite/message_field.cc index 2346747205..79fcf959e8 100644 --- a/src/google/protobuf/compiler/java/lite/message_field.cc +++ b/src/google/protobuf/compiler/java/lite/message_field.cc @@ -44,7 +44,6 @@ void SetMessageVariables( (*variables)["type"] = name_resolver->GetImmutableClassName(descriptor->message_type()); - variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])}); (*variables)["mutable_type"] = name_resolver->GetMutableClassName(descriptor->message_type()); (*variables)["group_or_message"] = @@ -54,12 +53,6 @@ void SetMessageVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (HasHasbit(descriptor)) { @@ -276,56 +269,6 @@ void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void ImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, - }, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - GenerateKotlinOrNull(printer); -} - -void ImmutableMessageFieldLiteGenerator::GenerateKotlinOrNull( - io::Printer* printer) const { - if (descriptor_->has_presence() && - descriptor_->real_containing_oneof() == nullptr) { - printer->Print( - "$kt_deprecation$\n" - "public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n" - " get() = $kt_dsl_builder$.$name$OrNull\n"); - } -} - void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo( io::Printer* printer, std::vector* output) const { WriteIntToUtf16CharSequence(descriptor_->number(), output); @@ -810,130 +753,6 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateInitializationCode( std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const { return name_resolver_->GetImmutableClassName(descriptor_->message_type()); } - -void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - printer->Print( - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Print( - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - "$ jvm_synthetic$" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, - }, - - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); -} - } // namespace java } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/java/lite/message_field.h b/src/google/protobuf/compiler/java/lite/message_field.h index 495b3933b9..8424bf3da4 100644 --- a/src/google/protobuf/compiler/java/lite/message_field.h +++ b/src/google/protobuf/compiler/java/lite/message_field.h @@ -57,7 +57,6 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -69,7 +68,6 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator { Context* context_; private: - void GenerateKotlinOrNull(io::Printer* printer) const; }; class ImmutableMessageOneofFieldLiteGenerator @@ -110,7 +108,6 @@ class RepeatedImmutableMessageFieldLiteGenerator void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/lite/primitive_field.cc b/src/google/protobuf/compiler/java/lite/primitive_field.cc index 3e0d174671..2cbcd3e22e 100644 --- a/src/google/protobuf/compiler/java/lite/primitive_field.cc +++ b/src/google/protobuf/compiler/java/lite/primitive_field.cc @@ -52,7 +52,6 @@ void SetPrimitiveVariables( JavaType javaType = GetJavaType(descriptor); (*variables)["type"] = std::string(PrimitiveTypeName(javaType)); (*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType)); - (*variables)["kt_type"] = std::string(KotlinTypeName(javaType)); variables->insert({"field_type", (*variables)["type"]}); (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver, context->options()); @@ -121,11 +120,6 @@ void SetPrimitiveVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - (*variables)["kt_deprecation"] = - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", name, - " is deprecated\") ") - : ""; int fixed_size = FixedSize(GetType(descriptor)); if (fixed_size != -1) { (*variables)["fixed_size"] = absl::StrCat(fixed_size); @@ -326,63 +320,6 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void ImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - WriteFieldDocComment(printer, descriptor_, context_->options()); - if (descriptor_->name() == "is_initialized") { - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, - }, - "// TODO: b/336400327 - remove this hack; we should access properties\n" - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.get${$$kt_capitalized_name$$}$()\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$set$kt_capitalized_name$$}$(value)\n" - " }\n"); - } else { - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, - }, - "$kt_deprecation$public var $kt_name$: $kt_type$\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - } - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( io::Printer* printer, std::vector* output) const { WriteIntToUtf16CharSequence(descriptor_->number(), output); @@ -723,127 +660,6 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - printer->Print( - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - WriteFieldDocComment(printer, descriptor_, context_->options()); - printer->Print( - "$kt_deprecation$ public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" - "$ jvm_synthetic$" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "add(value: $kt_type$) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(value: $kt_type$) {\n" - " add(value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" - " addAll(values)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public operator fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "set(index: kotlin.Int, value: $kt_type$) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo( io::Printer* printer, std::vector* output) const { WriteIntToUtf16CharSequence(descriptor_->number(), output); diff --git a/src/google/protobuf/compiler/java/lite/primitive_field.h b/src/google/protobuf/compiler/java/lite/primitive_field.h index 56b6fc7b77..eb6d5f5590 100644 --- a/src/google/protobuf/compiler/java/lite/primitive_field.h +++ b/src/google/protobuf/compiler/java/lite/primitive_field.h @@ -57,7 +57,6 @@ class ImmutablePrimitiveFieldLiteGenerator void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -107,7 +106,6 @@ class RepeatedImmutablePrimitiveFieldLiteGenerator void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/java/lite/string_field.cc b/src/google/protobuf/compiler/java/lite/string_field.cc index 05237fc1fd..6ce00aa3f5 100644 --- a/src/google/protobuf/compiler/java/lite/string_field.cc +++ b/src/google/protobuf/compiler/java/lite/string_field.cc @@ -62,12 +62,6 @@ void SetPrimitiveVariables( // by the proto compiler (*variables)["deprecation"] = descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; - variables->insert( - {"kt_deprecation", - descriptor->options().deprecated() - ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", - (*variables)["name"], " is deprecated\") ") - : ""}); (*variables)["required"] = descriptor->is_required() ? "true" : "false"; if (!context->options().opensource_runtime) { (*variables)["enforce_utf8"] = CheckUtf8(descriptor) ? "true" : "false"; @@ -327,46 +321,6 @@ void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void ImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - WriteFieldDocComment(printer, descriptor_, context_->options(), - /* kdoc */ true); - printer->Emit( - { - {"jvm_name_get", - [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, - {"jvm_name_set", - [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, - }, - "$kt_deprecation$public var $kt_name$: kotlin.String\n" - " $jvm_name_get$" - " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" - " $jvm_name_set$" - " set(value) {\n" - " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" - " }\n"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$clear$kt_capitalized_name$$}$() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}\n"); - - if (descriptor_->has_presence()) { - WriteFieldAccessorDocComment(printer, descriptor_, HAZZER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" - " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" - "}\n"); - } -} - void ImmutableStringFieldLiteGenerator::GenerateFieldInfo( io::Printer* printer, std::vector* output) const { WriteIntToUtf16CharSequence(descriptor_->number(), output); @@ -808,135 +762,6 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers( printer->Annotate("{", "}", descriptor_, Semantic::kSet); } -void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers( - io::Printer* printer) const { - auto vars = printer->WithVars(variables_); - JvmNameContext name_ctx = {context_->options(), printer}; - printer->Print( - "/**\n" - " * An uninstantiable, behaviorless type to represent the field in\n" - " * generics.\n" - " */\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" - " : com.google.protobuf.kotlin.DslProxy()\n"); - - // property for List - WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Print( - "$kt_deprecation$public val $kt_name$: " - "com.google.protobuf.kotlin.DslList" - "\n" - "@kotlin.OptIn" - "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" - " get() = com.google.protobuf.kotlin.DslList(\n" - " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" - " )\n"); - - // List.add(String) - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "." - "add(value: kotlin.String) {\n" - " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" - "}\n"); - - // List += String - WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "." - "plusAssign(value: kotlin.String) {\n" - " add(value)\n" - "}\n"); - - // List.addAll(Iterable) - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "." - "addAll(values: kotlin.collections.Iterable) {\n" - " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" - "}\n"); - - // List += Iterable - WriteFieldAccessorDocComment(printer, descriptor_, LIST_MULTI_ADDER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", - [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "@Suppress(\"NOTHING_TO_INLINE\")\n" - "public inline operator fun com.google.protobuf.kotlin.DslList" - "." - "plusAssign(values: kotlin.collections.Iterable) {\n" - " addAll(values)\n" - "}\n"); - - // List[Int] = String - WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public operator fun com.google.protobuf.kotlin.DslList" - "." - "set(index: kotlin.Int, value: kotlin.String) {\n" - " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" - "}"); - - WriteFieldAccessorDocComment(printer, descriptor_, CLEARER, - context_->options(), - /* builder */ false, /* kdoc */ true); - printer->Emit( - { - {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, - }, - "$jvm_synthetic$" - "$jvm_name$" - "public fun com.google.protobuf.kotlin.DslList" - "." - "clear() {\n" - " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" - "}"); -} - void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo( io::Printer* printer, std::vector* output) const { WriteIntToUtf16CharSequence(descriptor_->number(), output); diff --git a/src/google/protobuf/compiler/java/lite/string_field.h b/src/google/protobuf/compiler/java/lite/string_field.h index 59291869c0..2a83e88f5e 100644 --- a/src/google/protobuf/compiler/java/lite/string_field.h +++ b/src/google/protobuf/compiler/java/lite/string_field.h @@ -58,7 +58,6 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator { void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; @@ -107,7 +106,6 @@ class RepeatedImmutableStringFieldLiteGenerator void GenerateInitializationCode(io::Printer* printer) const override; void GenerateFieldInfo(io::Printer* printer, std::vector* output) const override; - void GenerateKotlinDslMembers(io::Printer* printer) const override; std::string GetBoxedType() const override; diff --git a/src/google/protobuf/compiler/kotlin/BUILD.bazel b/src/google/protobuf/compiler/kotlin/BUILD.bazel index 5555adef0f..9ecbe77986 100644 --- a/src/google/protobuf/compiler/kotlin/BUILD.bazel +++ b/src/google/protobuf/compiler/kotlin/BUILD.bazel @@ -22,10 +22,12 @@ cc_library( cc_library( name = "kotlin_internal", srcs = [ + "field.cc", "file.cc", "message.cc", ], hdrs = [ + "field.h", "file.h", "message.h", ], @@ -42,12 +44,14 @@ cc_library( "//src/google/protobuf/compiler/java", "//src/google/protobuf/compiler/java:generator_common", "//src/google/protobuf/compiler/java:helpers", + "//src/google/protobuf/compiler/java:internal_helpers", "//src/google/protobuf/compiler/java/full:fg", "//src/google/protobuf/compiler/java/full:mfg", "//src/google/protobuf/compiler/java/lite:field_generators", "//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/kotlin/field.cc b/src/google/protobuf/compiler/kotlin/field.cc new file mode 100644 index 0000000000..1f085d1219 --- /dev/null +++ b/src/google/protobuf/compiler/kotlin/field.cc @@ -0,0 +1,868 @@ +#include "google/protobuf/compiler/kotlin/field.h" + +#include + +#include "absl/container/flat_hash_map.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/field_common.h" +#include "google/protobuf/compiler/java/helpers.h" +#include "google/protobuf/compiler/java/internal_helpers.h" +#include "google/protobuf/compiler/java/name_resolver.h" +#include "google/protobuf/descriptor.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace kotlin { + +FieldGenerator::FieldGenerator(const FieldDescriptor* descriptor, + java::Context* context, bool lite) + : descriptor_(descriptor), context_(context), lite_(lite) { + java::SetCommonFieldVariables( + descriptor, context->GetFieldGeneratorInfo(descriptor), &variables_); + variables_.insert( + {"kt_deprecation", + descriptor->options().deprecated() + ? absl::StrCat("@kotlin.Deprecated(message = \"Field ", + variables_["name"], " is deprecated\") ") + : ""}); +} + +void FieldGenerator::Generate(io::Printer* printer) const { + auto cleanup = printer->WithVars(variables_); + switch (java::GetJavaType(descriptor_)) { + case java::JAVATYPE_MESSAGE: + if (descriptor_->is_repeated() && + java::IsMapEntry(descriptor_->message_type())) { + GenerateMapField(printer); + } else { + GenerateMessageField(printer); + } + break; + case java::JAVATYPE_STRING: + GenerateStringField(printer); + break; + case java::JAVATYPE_ENUM: + GenerateEnumField(printer); + break; + default: + GeneratePritimiveField(printer); + break; + } +} + +void FieldGenerator::GeneratePritimiveField(io::Printer* printer) const { + java::JavaType javaType = java::GetJavaType(descriptor_); + auto cleanup = printer->WithVars( + {{"kt_type", std::string(java::KotlinTypeName(javaType))}}); + + if (descriptor_->is_repeated()) { + GenerateRepeatedPritimiveField(printer); + return; + } + + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + java::WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + if (descriptor_->name() == "is_initialized") { + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, + }, + "// TODO: b/336400327 - remove this hack; we should access properties\n" + "$kt_deprecation$public var $kt_name$: $kt_type$\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.get${$$kt_capitalized_name$$}$()\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$set$kt_capitalized_name$$}$(value)\n" + " }\n"); + } else { + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, + }, + "$kt_deprecation$public var $kt_name$: $kt_type$\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" + " }\n"); + } + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$clear$kt_capitalized_name$$}$() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}\n"); + + if (descriptor_->has_presence()) { + WriteFieldAccessorDocComment(printer, descriptor_, java::HAZZER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" + " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" + "}\n"); + } +} + +void FieldGenerator::GenerateRepeatedPritimiveField( + io::Printer* printer) const { + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + printer->Print( + "/**\n" + " * An uninstantiable, behaviorless type to represent the field in\n" + " * generics.\n" + " */\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" + " : com.google.protobuf.kotlin.DslProxy()\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Print( + "$kt_deprecation$ public val $kt_name$: " + "com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + "$ jvm_synthetic$" + " get() = com.google.protobuf.kotlin.DslList(\n" + " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" + " )\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "add(value: $kt_type$) {\n" + " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(value: $kt_type$) {\n" + " add(value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " addAll(values)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_INDEXED_SETTER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "set(index: kotlin.Int, value: $kt_type$) {\n" + " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "clear() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}"); +} + +void FieldGenerator::GenerateMessageField(io::Printer* printer) const { + auto name_resolver = context_->GetNameResolver(); + auto cleanup = printer->WithVars( + {{"kt_type", + java::EscapeKotlinKeywords(name_resolver->GetImmutableClassName( + descriptor_->message_type()))}}); + + if (descriptor_->is_repeated()) { + GenerateRepeatedMessageField(printer); + return; + } + + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, + }, + "$kt_deprecation$public var $kt_name$: $kt_type$\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" + " }\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$clear$kt_capitalized_name$$}$() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::HAZZER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" + " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" + "}\n"); + if (descriptor_->has_presence() && + descriptor_->real_containing_oneof() == nullptr) { + printer->Print( + "$kt_deprecation$\n" + "public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n" + " get() = $kt_dsl_builder$.$name$OrNull\n"); + } +} + +void FieldGenerator::GenerateRepeatedMessageField(io::Printer* printer) const { + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + printer->Print( + "/**\n" + " * An uninstantiable, behaviorless type to represent the field in\n" + " * generics.\n" + " */\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" + " : com.google.protobuf.kotlin.DslProxy()\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Print( + "$kt_deprecation$ public val $kt_name$: " + "com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + "$ jvm_synthetic$" + " get() = com.google.protobuf.kotlin.DslList(\n" + " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" + " )\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "add(value: $kt_type$) {\n" + " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(value: $kt_type$) {\n" + " add(value)\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " addAll(values)\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_INDEXED_SETTER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "set(index: kotlin.Int, value: $kt_type$) {\n" + " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" + "}\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, + }, + + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "clear() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}\n"); +} + +void FieldGenerator::GenerateStringField(io::Printer* printer) const { + if (descriptor_->is_repeated()) { + GenerateRepeatedStringField(printer); + return; + } + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, + }, + "$kt_deprecation$public var $kt_name$: kotlin.String\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" + " }\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$clear$kt_capitalized_name$$}$() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}\n"); + + if (descriptor_->has_presence()) { + WriteFieldAccessorDocComment(printer, descriptor_, java::HAZZER, + context_->options(), /* kdoc */ true); + printer->Print( + "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" + " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" + "}\n"); + } +} + +void FieldGenerator::GenerateRepeatedStringField(io::Printer* printer) const { + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + printer->Print( + "/**\n" + " * An uninstantiable, behaviorless type to represent the field in\n" + " * generics.\n" + " */\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" + " : com.google.protobuf.kotlin.DslProxy()\n"); + + // property for List + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_GETTER, + context_->options(), /* kdoc */ true); + printer->Print( + "$kt_deprecation$public val $kt_name$: " + "com.google.protobuf.kotlin.DslList" + "\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + " get() = com.google.protobuf.kotlin.DslList(\n" + " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" + " )\n"); + + // List.add(String) + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "." + "add(value: kotlin.String) {\n" + " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" + "}\n"); + + // List += String + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "." + "plusAssign(value: kotlin.String) {\n" + " add(value)\n" + "}\n"); + + // List.addAll(Iterable) + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "." + "addAll(values: kotlin.collections.Iterable) {\n" + " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" + "}\n"); + + // List += Iterable + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "." + "plusAssign(values: kotlin.collections.Iterable) {\n" + " addAll(values)\n" + "}\n"); + + // List[Int] = String + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_INDEXED_SETTER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public operator fun com.google.protobuf.kotlin.DslList" + "." + "set(index: kotlin.Int, value: kotlin.String) {\n" + " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "." + "clear() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}"); +} + +void FieldGenerator::GenerateEnumField(io::Printer* printer) const { + auto name_resolver = context_->GetNameResolver(); + auto cleanup = printer->WithVars( + {{"kt_type", + java::EscapeKotlinKeywords( + name_resolver->GetImmutableClassName(descriptor_->enum_type()))}}); + + if (descriptor_->is_repeated()) { + GenerateRepeatedEnumField(printer); + return; + } + + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$$}$", name_ctx); }}, + }, + "$kt_deprecation$public var $kt_name$: $kt_type$\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.${$$kt_safe_name$$}$\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$$kt_safe_name$$}$ = value\n" + " }\n"); + + if (java::SupportUnknownEnumValue(descriptor_)) { + printer->Emit( + { + {"jvm_name_get", + [&] { JvmName("${$get$kt_capitalized_name$Value$}$", name_ctx); }}, + {"jvm_name_set", + [&] { JvmName("${$set$kt_capitalized_name$Value$}$", name_ctx); }}, + }, + "$kt_deprecation$public var $kt_name$Value: kotlin.Int\n" + " $jvm_name_get$" + " get() = $kt_dsl_builder$.${$$kt_property_name$Value$}$\n" + " $jvm_name_set$" + " set(value) {\n" + " $kt_dsl_builder$.${$$kt_property_name$Value$}$ = value\n" + " }\n"); + } + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), + /* kdoc */ true); + printer->Print( + "public fun ${$clear$kt_capitalized_name$$}$() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}\n"); + + if (descriptor_->has_presence()) { + WriteFieldAccessorDocComment(printer, descriptor_, java::HAZZER, + context_->options(), + /* kdoc */ true); + printer->Print( + "public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n" + " return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n" + "}\n"); + } +} + +void FieldGenerator::GenerateRepeatedEnumField(io::Printer* printer) const { + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + printer->Print( + "/**\n" + " * An uninstantiable, behaviorless type to represent the field in\n" + " * generics.\n" + " */\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" + " : com.google.protobuf.kotlin.DslProxy()\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Print( + "$kt_deprecation$ public val $kt_name$: " + "com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + "$ jvm_synthetic$" + " get() = com.google.protobuf.kotlin.DslList(\n" + " $kt_dsl_builder$.${$$kt_property_name$List$}$\n" + " )\n"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("add$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "add(value: $kt_type$) {\n" + " $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssign$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(value: $kt_type$) {\n" + " add(value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("addAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "addAll(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_MULTI_ADDER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("plusAssignAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "plusAssign(values: kotlin.collections.Iterable<$kt_type$>) {\n" + " addAll(values)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_INDEXED_SETTER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public operator fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "set(index: kotlin.Int, value: $kt_type$) {\n" + " $kt_dsl_builder$.${$set$capitalized_name$$}$(index, value)\n" + "}"); + + WriteFieldAccessorDocComment(printer, descriptor_, java::CLEARER, + context_->options(), /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslList" + "<$kt_type$, ${$$kt_capitalized_name$Proxy$}$>." + "clear() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + "}"); +} + +namespace { +std::string KotlinTypeName(const FieldDescriptor* field, + java::ClassNameResolver* name_resolver) { + if (java::GetJavaType(field) == java::JAVATYPE_MESSAGE) { + return name_resolver->GetImmutableClassName(field->message_type()); + } else if (java::GetJavaType(field) == java::JAVATYPE_ENUM) { + return name_resolver->GetImmutableClassName(field->enum_type()); + } else { + return std::string(java::KotlinTypeName(java::GetJavaType(field))); + } +} +} // namespace + +void FieldGenerator::GenerateMapField(io::Printer* printer) const { + auto name_resolver = context_->GetNameResolver(); + const FieldDescriptor* key = java::MapKeyField(descriptor_); + const FieldDescriptor* value = java::MapValueField(descriptor_); + auto cleanup = printer->WithVars( + {{"kt_key_type", KotlinTypeName(key, name_resolver)}, + {"kt_value_type", KotlinTypeName(value, name_resolver)}}); + + java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; + printer->Print( + "/**\n" + " * An uninstantiable, behaviorless type to represent the field in\n" + " * generics.\n" + " */\n" + "@kotlin.OptIn" + "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n" + "public class ${$$kt_capitalized_name$Proxy$}$ private constructor()" + " : com.google.protobuf.kotlin.DslProxy()\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("get$kt_capitalized_name$Map", name_ctx); }}, + }, + "$kt_deprecation$ public val $kt_name$: " + "com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + "$ jvm_synthetic$" + "$jvm_name$" + " get() = com.google.protobuf.kotlin.DslMap(\n" + " $kt_dsl_builder$.${$$kt_property_name$Map$}$\n" + " )\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("put$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + " .put(key: $kt_key_type$, value: $kt_value_type$) {\n" + " $kt_dsl_builder$.${$put$capitalized_name$$}$(key, value)\n" + " }\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", [&] { JvmName("set$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "@Suppress(\"NOTHING_TO_INLINE\")\n" + "public inline operator fun com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + " .set(key: $kt_key_type$, value: $kt_value_type$) {\n" + " put(key, value)\n" + " }\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("remove$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + " .remove(key: $kt_key_type$) {\n" + " $kt_dsl_builder$.${$remove$capitalized_name$$}$(key)\n" + " }\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("putAll$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + " .putAll(map: kotlin.collections.Map<$kt_key_type$, $kt_value_type$>) " + "{\n" + " $kt_dsl_builder$.${$putAll$capitalized_name$$}$(map)\n" + " }\n"); + + WriteFieldDocComment(printer, descriptor_, context_->options(), + /* kdoc */ true); + printer->Emit( + { + {"jvm_name", + [&] { JvmName("clear$kt_capitalized_name$", name_ctx); }}, + }, + "$jvm_synthetic$" + "$jvm_name$" + "public fun com.google.protobuf.kotlin.DslMap" + "<$kt_key_type$, $kt_value_type$, ${$$kt_capitalized_name$Proxy$}$>\n" + " .clear() {\n" + " $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n" + " }\n"); +} + +} // namespace kotlin +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/kotlin/field.h b/src/google/protobuf/compiler/kotlin/field.h new file mode 100644 index 0000000000..f9ce4cdfa3 --- /dev/null +++ b/src/google/protobuf/compiler/kotlin/field.h @@ -0,0 +1,50 @@ +#ifndef GOOGLE_PROTOBUF_COMPILER_KOTLIN_FIELD_H__ +#define GOOGLE_PROTOBUF_COMPILER_KOTLIN_FIELD_H__ + +#include + +#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" +#include "google/protobuf/io/printer.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace kotlin { + +class FieldGenerator { + public: + FieldGenerator(const FieldDescriptor* descriptor, java::Context* context, + bool lite); + FieldGenerator(const FieldGenerator&) = delete; + FieldGenerator& operator=(const FieldGenerator&) = delete; + ~FieldGenerator() = default; + + void Generate(io::Printer* printer) const; + + private: + const FieldDescriptor* descriptor_; + absl::flat_hash_map variables_; + java::Context* context_; + bool lite_; + + void GeneratePritimiveField(io::Printer* printer) const; + void GenerateMessageField(io::Printer* printer) const; + void GenerateMapField(io::Printer* printer) const; + void GenerateStringField(io::Printer* printer) const; + void GenerateEnumField(io::Printer* printer) const; + + void GenerateRepeatedPritimiveField(io::Printer* printer) const; + void GenerateRepeatedMessageField(io::Printer* printer) const; + void GenerateRepeatedStringField(io::Printer* printer) const; + void GenerateRepeatedEnumField(io::Printer* printer) const; +}; + +} // namespace kotlin +} // namespace compiler +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_COMPILER_KOTLIN_FIELD_H__ diff --git a/src/google/protobuf/compiler/kotlin/message.cc b/src/google/protobuf/compiler/kotlin/message.cc index e64a0987e6..9026dfaee1 100644 --- a/src/google/protobuf/compiler/kotlin/message.cc +++ b/src/google/protobuf/compiler/kotlin/message.cc @@ -7,7 +7,9 @@ #include "google/protobuf/compiler/kotlin/message.h" +#include #include +#include #include "absl/log/absl_check.h" #include "absl/strings/str_cat.h" @@ -16,11 +18,8 @@ #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/full/field_generator.h" -#include "google/protobuf/compiler/java/full/make_field_gens.h" -#include "google/protobuf/compiler/java/lite/field_generator.h" -#include "google/protobuf/compiler/java/lite/make_field_gens.h" #include "google/protobuf/compiler/java/name_resolver.h" +#include "google/protobuf/compiler/kotlin/field.h" #include "google/protobuf/io/printer.h" // Must be last. @@ -39,36 +38,17 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor, lite_(!java::HasDescriptorMethods(descriptor_->file(), context->EnforceLite())), jvm_dsl_(!lite_ || context->options().jvm_dsl), - lite_field_generators_( - java::FieldGeneratorMap( - descriptor_)), - field_generators_( - java::FieldGeneratorMap(descriptor_)) { + field_generators_(java::FieldGeneratorMap(descriptor_)) { for (int i = 0; i < descriptor_->field_count(); i++) { if (java::IsRealOneof(descriptor_->field(i))) { const OneofDescriptor* oneof = descriptor_->field(i)->containing_oneof(); ABSL_CHECK(oneofs_.emplace(oneof->index(), oneof).first->second == oneof); } } - if (lite_) { - lite_field_generators_ = - java::MakeImmutableFieldLiteGenerators(descriptor_, context_); - } else { - field_generators_ = - java::MakeImmutableFieldGenerators(descriptor_, context_); - } -} - -void MessageGenerator::GenerateFieldMembers(io::Printer* printer) const { for (int i = 0; i < descriptor_->field_count(); i++) { - printer->Print("\n"); - if (lite_) { - lite_field_generators_.get(descriptor_->field(i)) - .GenerateKotlinDslMembers(printer); - } else { - field_generators_.get(descriptor_->field(i)) - .GenerateKotlinDslMembers(printer); - } + const FieldDescriptor* field = descriptor->field(i); + auto generator = std::make_unique(field, context_, lite_); + field_generators_.Add(field, std::move(generator)); } } @@ -97,7 +77,10 @@ void MessageGenerator::Generate(io::Printer* printer) const { printer->Indent(); - GenerateFieldMembers(printer); + for (int i = 0; i < descriptor_->field_count(); i++) { + printer->Print("\n"); + field_generators_.get(descriptor_->field(i)).Generate(printer); + } for (auto& kv : oneofs_) { java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; diff --git a/src/google/protobuf/compiler/kotlin/message.h b/src/google/protobuf/compiler/kotlin/message.h index 28bb428b72..9c10476618 100644 --- a/src/google/protobuf/compiler/kotlin/message.h +++ b/src/google/protobuf/compiler/kotlin/message.h @@ -11,9 +11,8 @@ #include "absl/container/btree_map.h" #include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/generator_common.h" -#include "google/protobuf/compiler/java/full/field_generator.h" -#include "google/protobuf/compiler/java/lite/field_generator.h" #include "google/protobuf/compiler/java/name_resolver.h" +#include "google/protobuf/compiler/kotlin/field.h" #include "google/protobuf/descriptor.h" namespace google { @@ -40,11 +39,7 @@ class MessageGenerator { bool lite_; bool jvm_dsl_; - // TODO: b/366047913 - These can be simplified once lite and full field - // generators are unified. - java::FieldGeneratorMap - lite_field_generators_; - java::FieldGeneratorMap field_generators_; + java::FieldGeneratorMap field_generators_; void GenerateExtensions(io::Printer* printer) const; void GenerateOrNull(io::Printer* printer) const;