Merge kotlin dsl field generators and move them into kotlin/ sub folder

PiperOrigin-RevId: 682052240
pull/18564/head
Protobuf Team Bot 2 months ago committed by Copybara-Service
parent 671e6c5ef6
commit d3cbd2154e
  1. 5
      src/google/protobuf/compiler/java/BUILD.bazel
  2. 153
      src/google/protobuf/compiler/java/full/enum_field.cc
  3. 2
      src/google/protobuf/compiler/java/full/enum_field.h
  4. 108
      src/google/protobuf/compiler/java/full/map_field.cc
  5. 1
      src/google/protobuf/compiler/java/full/map_field.h
  6. 152
      src/google/protobuf/compiler/java/full/message_field.cc
  7. 5
      src/google/protobuf/compiler/java/full/message_field.h
  8. 153
      src/google/protobuf/compiler/java/full/primitive_field.cc
  9. 2
      src/google/protobuf/compiler/java/full/primitive_field.h
  10. 148
      src/google/protobuf/compiler/java/full/string_field.cc
  11. 2
      src/google/protobuf/compiler/java/full/string_field.h
  12. 1
      src/google/protobuf/compiler/java/generator_common.h
  13. 186
      src/google/protobuf/compiler/java/lite/enum_field.cc
  14. 2
      src/google/protobuf/compiler/java/lite/enum_field.h
  15. 124
      src/google/protobuf/compiler/java/lite/map_field.cc
  16. 1
      src/google/protobuf/compiler/java/lite/map_field.h
  17. 181
      src/google/protobuf/compiler/java/lite/message_field.cc
  18. 3
      src/google/protobuf/compiler/java/lite/message_field.h
  19. 184
      src/google/protobuf/compiler/java/lite/primitive_field.cc
  20. 2
      src/google/protobuf/compiler/java/lite/primitive_field.h
  21. 175
      src/google/protobuf/compiler/java/lite/string_field.cc
  22. 2
      src/google/protobuf/compiler/java/lite/string_field.h
  23. 4
      src/google/protobuf/compiler/kotlin/BUILD.bazel
  24. 868
      src/google/protobuf/compiler/kotlin/field.cc
  25. 50
      src/google/protobuf/compiler/kotlin/field.h
  26. 39
      src/google/protobuf/compiler/kotlin/message.cc
  27. 9
      src/google/protobuf/compiler/kotlin/message.h

@ -71,7 +71,10 @@ cc_library(
"internal_helpers.h", "internal_helpers.h",
], ],
strip_include_prefix = "/src", 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 = [ deps = [
":helpers", ":helpers",
":java_features_bootstrap", ":java_features_bootstrap",

@ -47,7 +47,6 @@ void SetEnumVariables(
(*variables)["type"] = (*variables)["type"] =
name_resolver->GetImmutableClassName(descriptor->enum_type()); name_resolver->GetImmutableClassName(descriptor->enum_type());
variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])});
(*variables)["mutable_type"] = (*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->enum_type()); name_resolver->GetMutableClassName(descriptor->enum_type());
(*variables)["default"] = (*variables)["default"] =
@ -62,12 +61,6 @@ void SetEnumVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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)) { if (HasHasbit(descriptor)) {
// For singular messages and builders, one bit is used for the hasField bit. // For singular messages and builders, one bit is used for the hasField bit.
(*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex); (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
@ -267,51 +260,6 @@ void ImmutableEnumFieldGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void ImmutableEnumFieldGenerator::GenerateFieldBuilderInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
// noop for enums // noop for enums
@ -1077,107 +1025,6 @@ void RepeatedImmutableEnumFieldGenerator::GenerateHashCode(
"}\n"); "}\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 { std::string RepeatedImmutableEnumFieldGenerator::GetBoxedType() const {
return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); return name_resolver_->GetImmutableClassName(descriptor_->enum_type());
} }

@ -65,7 +65,6 @@ class ImmutableEnumFieldGenerator : public ImmutableFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -131,7 +130,6 @@ class RepeatedImmutableEnumFieldGenerator : public ImmutableEnumFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
}; };

@ -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) { std::string WireType(const FieldDescriptor* field) {
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.", return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type())); FieldTypeName(field->type()));
@ -93,8 +82,6 @@ void ImmutableMapFieldGenerator::SetMessageVariables(
variables_["key_type"] = TypeName(key, name_resolver, false); variables_["key_type"] = TypeName(key, name_resolver, false);
std::string boxed_key_type = TypeName(key, name_resolver, true); std::string boxed_key_type = TypeName(key, name_resolver, true);
variables_["boxed_key_type"] = boxed_key_type; 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. // Used for calling the serialization function.
variables_["short_key_type"] = variables_["short_key_type"] =
boxed_key_type.substr(boxed_key_type.rfind('.') + 1); boxed_key_type.substr(boxed_key_type.rfind('.') + 1);
@ -170,12 +157,6 @@ void ImmutableMapFieldGenerator::SetMessageVariables(
// by the proto compiler // by the proto compiler
variables_["deprecation"] = variables_["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : ""; 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_["on_changed"] = "onChanged();";
variables_.insert( variables_.insert(
@ -959,95 +940,6 @@ void ImmutableMapFieldGenerator::GenerateMessageMapGetters(
printer->Annotate("{", "}", descriptor_); 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( void ImmutableMapFieldGenerator::GenerateFieldBuilderInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
// Nothing to initialize. // Nothing to initialize.

@ -44,7 +44,6 @@ class ImmutableMapFieldGenerator : public ImmutableFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -43,7 +43,6 @@ void SetMessageVariables(
(*variables)["type"] = (*variables)["type"] =
name_resolver->GetImmutableClassName(descriptor->message_type()); name_resolver->GetImmutableClassName(descriptor->message_type());
variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])});
(*variables)["mutable_type"] = (*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->message_type()); name_resolver->GetMutableClassName(descriptor->message_type());
(*variables)["group_or_message"] = (*variables)["group_or_message"] =
@ -53,12 +52,6 @@ void SetMessageVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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)["on_changed"] = "onChanged();";
(*variables)["get_parser"] = "parser()"; (*variables)["get_parser"] = "parser()";
@ -371,50 +364,6 @@ void ImmutableMessageFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\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( void ImmutableMessageFieldGenerator::GenerateFieldBuilderInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n"); printer->Print(variables_, "get$capitalized_name$FieldBuilder();\n");
@ -1356,107 +1305,6 @@ std::string RepeatedImmutableMessageFieldGenerator::GetBoxedType() const {
return name_resolver_->GetImmutableClassName(descriptor_->message_type()); 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 java
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf

@ -66,7 +66,6 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -87,9 +86,6 @@ class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator {
const char* trailing_code, const char* trailing_code,
absl::optional<io::AnnotationCollector::Semantic> semantic = absl::optional<io::AnnotationCollector::Semantic> semantic =
absl::nullopt) const; absl::nullopt) const;
private:
void GenerateKotlinOrNull(io::Printer* printer) const;
}; };
class ImmutableMessageOneofFieldGenerator class ImmutableMessageOneofFieldGenerator
@ -143,7 +139,6 @@ class RepeatedImmutableMessageFieldGenerator
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -46,7 +46,6 @@ void SetPrimitiveVariables(
(*variables)["type"] = std::string(PrimitiveTypeName(javaType)); (*variables)["type"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType)); (*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
variables->insert({"field_type", (*variables)["type"]}); variables->insert({"field_type", (*variables)["type"]});
std::string name = (*variables)["name"]; std::string name = (*variables)["name"];
@ -100,11 +99,6 @@ void SetPrimitiveVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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)); int fixed_size = FixedSize(GetType(descriptor));
if (fixed_size != -1) { if (fixed_size != -1) {
(*variables)["fixed_size"] = absl::StrCat(fixed_size); (*variables)["fixed_size"] = absl::StrCat(fixed_size);
@ -287,52 +281,6 @@ void ImmutablePrimitiveFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\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( void ImmutablePrimitiveFieldGenerator::GenerateFieldBuilderInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
// noop for primitives // noop for primitives
@ -840,107 +788,6 @@ void RepeatedImmutablePrimitiveFieldGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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:: void RepeatedImmutablePrimitiveFieldGenerator::
GenerateFieldBuilderInitializationCode(io::Printer* printer) const { GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
// noop for primitives // noop for primitives

@ -65,7 +65,6 @@ class ImmutablePrimitiveFieldGenerator : public ImmutableFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -131,7 +130,6 @@ class RepeatedImmutablePrimitiveFieldGenerator
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
}; };

@ -71,12 +71,6 @@ void SetPrimitiveVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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)["on_changed"] = "onChanged();";
if (HasHasbit(descriptor)) { if (HasHasbit(descriptor)) {
@ -366,39 +360,6 @@ void ImmutableStringFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\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( void ImmutableStringFieldGenerator::GenerateFieldBuilderInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
// noop for primitives // noop for primitives
@ -970,115 +931,6 @@ void RepeatedImmutableStringFieldGenerator::GenerateBuilderMembers(
"}\n"); "}\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<String>
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"
"<kotlin.String, ${$$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");
// List<String>.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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"add(value: kotlin.String) {\n"
" $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
"}\n");
// List<String> += 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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(value: kotlin.String) {\n"
" add(value)\n"
"}\n");
// List<String>.addAll(Iterable<String>)
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"addAll(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
"}\n");
// List<String> += Iterable<String>
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" addAll(values)\n"
"}\n");
// List<String>[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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"clear() {\n"
" $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
"}");
}
void RepeatedImmutableStringFieldGenerator:: void RepeatedImmutableStringFieldGenerator::
GenerateFieldBuilderInitializationCode(io::Printer* printer) const { GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
// noop for primitives // noop for primitives

@ -64,7 +64,6 @@ class ImmutableStringFieldGenerator : public ImmutableFieldGenerator {
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -129,7 +128,6 @@ class RepeatedImmutableStringFieldGenerator
io::Printer* printer) const override; io::Printer* printer) const override;
void GenerateEqualsCode(io::Printer* printer) const override; void GenerateEqualsCode(io::Printer* printer) const override;
void GenerateHashCode(io::Printer* printer) const override; void GenerateHashCode(io::Printer* printer) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
}; };

@ -23,7 +23,6 @@ class FieldGenerator {
public: public:
virtual ~FieldGenerator() = default; virtual ~FieldGenerator() = default;
virtual void GenerateSerializationCode(io::Printer* printer) const = 0; virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
}; };
// Convenience class which constructs FieldGenerators for a Descriptor. // Convenience class which constructs FieldGenerators for a Descriptor.

@ -53,7 +53,6 @@ void SetEnumVariables(
(*variables)["type"] = (*variables)["type"] =
name_resolver->GetImmutableClassName(descriptor->enum_type()); name_resolver->GetImmutableClassName(descriptor->enum_type());
variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])});
(*variables)["mutable_type"] = (*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->enum_type()); name_resolver->GetMutableClassName(descriptor->enum_type());
(*variables)["default"] = (*variables)["default"] =
@ -68,12 +67,6 @@ void SetEnumVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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"; (*variables)["required"] = descriptor->is_required() ? "true" : "false";
if (HasHasbit(descriptor)) { if (HasHasbit(descriptor)) {
@ -298,63 +291,6 @@ void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void ImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
if (!IsDefaultValueJavaDefault(descriptor_)) { if (!IsDefaultValueJavaDefault(descriptor_)) {
@ -903,128 +839,6 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
printer->Print(variables_, "$name$_ = emptyIntList();\n"); 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 { std::string RepeatedImmutableEnumFieldLiteGenerator::GetBoxedType() const {
return name_resolver_->GetImmutableClassName(descriptor_->enum_type()); return name_resolver_->GetImmutableClassName(descriptor_->enum_type());
} }

@ -55,7 +55,6 @@ class ImmutableEnumFieldLiteGenerator : public ImmutableFieldLiteGenerator {
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -103,7 +102,6 @@ class RepeatedImmutableEnumFieldLiteGenerator
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -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) { std::string WireType(const FieldDescriptor* field) {
return absl::StrCat("com.google.protobuf.WireFormat.FieldType.", return absl::StrCat("com.google.protobuf.WireFormat.FieldType.",
FieldTypeName(field->type())); FieldTypeName(field->type()));
@ -78,8 +67,6 @@ void SetMessageVariables(
(*variables)["key_type"] = TypeName(key, name_resolver, false); (*variables)["key_type"] = TypeName(key, name_resolver, false);
(*variables)["boxed_key_type"] = TypeName(key, name_resolver, true); (*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_wire_type"] = WireType(key);
(*variables)["key_default_value"] = (*variables)["key_default_value"] =
DefaultValue(key, true, name_resolver, context->options()); DefaultValue(key, true, name_resolver, context->options());
@ -141,12 +128,6 @@ void SetMessageVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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( variables->insert(
{"default_entry", absl::StrCat((*variables)["capitalized_name"], {"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( void ImmutableMapFieldLiteGenerator::GenerateInitializationCode(
io::Printer* printer) const { io::Printer* printer) const {
// Nothing to initialize. // Nothing to initialize.

@ -38,7 +38,6 @@ class ImmutableMapFieldLiteGenerator : public ImmutableFieldLiteGenerator {
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -44,7 +44,6 @@ void SetMessageVariables(
(*variables)["type"] = (*variables)["type"] =
name_resolver->GetImmutableClassName(descriptor->message_type()); name_resolver->GetImmutableClassName(descriptor->message_type());
variables->insert({"kt_type", EscapeKotlinKeywords((*variables)["type"])});
(*variables)["mutable_type"] = (*variables)["mutable_type"] =
name_resolver->GetMutableClassName(descriptor->message_type()); name_resolver->GetMutableClassName(descriptor->message_type());
(*variables)["group_or_message"] = (*variables)["group_or_message"] =
@ -54,12 +53,6 @@ void SetMessageVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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"; (*variables)["required"] = descriptor->is_required() ? "true" : "false";
if (HasHasbit(descriptor)) { if (HasHasbit(descriptor)) {
@ -276,56 +269,6 @@ void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void ImmutableMessageFieldLiteGenerator::GenerateFieldInfo(
io::Printer* printer, std::vector<uint16_t>* output) const { io::Printer* printer, std::vector<uint16_t>* output) const {
WriteIntToUtf16CharSequence(descriptor_->number(), output); WriteIntToUtf16CharSequence(descriptor_->number(), output);
@ -810,130 +753,6 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateInitializationCode(
std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const { std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const {
return name_resolver_->GetImmutableClassName(descriptor_->message_type()); 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 java
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf

@ -57,7 +57,6 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator {
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -69,7 +68,6 @@ class ImmutableMessageFieldLiteGenerator : public ImmutableFieldLiteGenerator {
Context* context_; Context* context_;
private: private:
void GenerateKotlinOrNull(io::Printer* printer) const;
}; };
class ImmutableMessageOneofFieldLiteGenerator class ImmutableMessageOneofFieldLiteGenerator
@ -110,7 +108,6 @@ class RepeatedImmutableMessageFieldLiteGenerator
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -52,7 +52,6 @@ void SetPrimitiveVariables(
JavaType javaType = GetJavaType(descriptor); JavaType javaType = GetJavaType(descriptor);
(*variables)["type"] = std::string(PrimitiveTypeName(javaType)); (*variables)["type"] = std::string(PrimitiveTypeName(javaType));
(*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType)); (*variables)["boxed_type"] = std::string(BoxedPrimitiveTypeName(javaType));
(*variables)["kt_type"] = std::string(KotlinTypeName(javaType));
variables->insert({"field_type", (*variables)["type"]}); variables->insert({"field_type", (*variables)["type"]});
(*variables)["default"] = (*variables)["default"] =
ImmutableDefaultValue(descriptor, name_resolver, context->options()); ImmutableDefaultValue(descriptor, name_resolver, context->options());
@ -121,11 +120,6 @@ void SetPrimitiveVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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)); int fixed_size = FixedSize(GetType(descriptor));
if (fixed_size != -1) { if (fixed_size != -1) {
(*variables)["fixed_size"] = absl::StrCat(fixed_size); (*variables)["fixed_size"] = absl::StrCat(fixed_size);
@ -326,63 +320,6 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void ImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
io::Printer* printer, std::vector<uint16_t>* output) const { io::Printer* printer, std::vector<uint16_t>* output) const {
WriteIntToUtf16CharSequence(descriptor_->number(), output); WriteIntToUtf16CharSequence(descriptor_->number(), output);
@ -723,127 +660,6 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateFieldInfo(
io::Printer* printer, std::vector<uint16_t>* output) const { io::Printer* printer, std::vector<uint16_t>* output) const {
WriteIntToUtf16CharSequence(descriptor_->number(), output); WriteIntToUtf16CharSequence(descriptor_->number(), output);

@ -57,7 +57,6 @@ class ImmutablePrimitiveFieldLiteGenerator
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -107,7 +106,6 @@ class RepeatedImmutablePrimitiveFieldLiteGenerator
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -62,12 +62,6 @@ void SetPrimitiveVariables(
// by the proto compiler // by the proto compiler
(*variables)["deprecation"] = (*variables)["deprecation"] =
descriptor->options().deprecated() ? "@java.lang.Deprecated " : ""; 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"; (*variables)["required"] = descriptor->is_required() ? "true" : "false";
if (!context->options().opensource_runtime) { if (!context->options().opensource_runtime) {
(*variables)["enforce_utf8"] = CheckUtf8(descriptor) ? "true" : "false"; (*variables)["enforce_utf8"] = CheckUtf8(descriptor) ? "true" : "false";
@ -327,46 +321,6 @@ void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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( void ImmutableStringFieldLiteGenerator::GenerateFieldInfo(
io::Printer* printer, std::vector<uint16_t>* output) const { io::Printer* printer, std::vector<uint16_t>* output) const {
WriteIntToUtf16CharSequence(descriptor_->number(), output); WriteIntToUtf16CharSequence(descriptor_->number(), output);
@ -808,135 +762,6 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
printer->Annotate("{", "}", descriptor_, Semantic::kSet); 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<String>
WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER,
context_->options(),
/* builder */ false, /* kdoc */ true);
printer->Print(
"$kt_deprecation$public val $kt_name$: "
"com.google.protobuf.kotlin.DslList"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>\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<String>.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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"add(value: kotlin.String) {\n"
" $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
"}\n");
// List<String> += 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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(value: kotlin.String) {\n"
" add(value)\n"
"}\n");
// List<String>.addAll(Iterable<String>)
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"addAll(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
"}\n");
// List<String> += Iterable<String>
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" addAll(values)\n"
"}\n");
// List<String>[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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"clear() {\n"
" $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
"}");
}
void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo( void RepeatedImmutableStringFieldLiteGenerator::GenerateFieldInfo(
io::Printer* printer, std::vector<uint16_t>* output) const { io::Printer* printer, std::vector<uint16_t>* output) const {
WriteIntToUtf16CharSequence(descriptor_->number(), output); WriteIntToUtf16CharSequence(descriptor_->number(), output);

@ -58,7 +58,6 @@ class ImmutableStringFieldLiteGenerator : public ImmutableFieldLiteGenerator {
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;
@ -107,7 +106,6 @@ class RepeatedImmutableStringFieldLiteGenerator
void GenerateInitializationCode(io::Printer* printer) const override; void GenerateInitializationCode(io::Printer* printer) const override;
void GenerateFieldInfo(io::Printer* printer, void GenerateFieldInfo(io::Printer* printer,
std::vector<uint16_t>* output) const override; std::vector<uint16_t>* output) const override;
void GenerateKotlinDslMembers(io::Printer* printer) const override;
std::string GetBoxedType() const override; std::string GetBoxedType() const override;

@ -22,10 +22,12 @@ cc_library(
cc_library( cc_library(
name = "kotlin_internal", name = "kotlin_internal",
srcs = [ srcs = [
"field.cc",
"file.cc", "file.cc",
"message.cc", "message.cc",
], ],
hdrs = [ hdrs = [
"field.h",
"file.h", "file.h",
"message.h", "message.h",
], ],
@ -42,12 +44,14 @@ cc_library(
"//src/google/protobuf/compiler/java", "//src/google/protobuf/compiler/java",
"//src/google/protobuf/compiler/java:generator_common", "//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/compiler/java:helpers", "//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:fg",
"//src/google/protobuf/compiler/java/full:mfg", "//src/google/protobuf/compiler/java/full:mfg",
"//src/google/protobuf/compiler/java/lite:field_generators", "//src/google/protobuf/compiler/java/lite:field_generators",
"//src/google/protobuf/io", "//src/google/protobuf/io",
"//src/google/protobuf/io:printer", "//src/google/protobuf/io:printer",
"@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
], ],

@ -0,0 +1,868 @@
#include "google/protobuf/compiler/kotlin/field.h"
#include <string>
#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<String>
WriteFieldAccessorDocComment(printer, descriptor_, java::LIST_GETTER,
context_->options(), /* kdoc */ true);
printer->Print(
"$kt_deprecation$public val $kt_name$: "
"com.google.protobuf.kotlin.DslList"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>\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<String>.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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"add(value: kotlin.String) {\n"
" $kt_dsl_builder$.${$add$capitalized_name$$}$(value)\n"
"}\n");
// List<String> += 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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(value: kotlin.String) {\n"
" add(value)\n"
"}\n");
// List<String>.addAll(Iterable<String>)
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"addAll(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" $kt_dsl_builder$.${$addAll$capitalized_name$$}$(values)\n"
"}\n");
// List<String> += Iterable<String>
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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"plusAssign(values: kotlin.collections.Iterable<kotlin.String>) {\n"
" addAll(values)\n"
"}\n");
// List<String>[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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"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"
"<kotlin.String, ${$$kt_capitalized_name$Proxy$}$>."
"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

@ -0,0 +1,50 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_KOTLIN_FIELD_H__
#define GOOGLE_PROTOBUF_COMPILER_KOTLIN_FIELD_H__
#include <string>
#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<absl::string_view, std::string> 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__

@ -7,7 +7,9 @@
#include "google/protobuf/compiler/kotlin/message.h" #include "google/protobuf/compiler/kotlin/message.h"
#include <memory>
#include <string> #include <string>
#include <utility>
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@ -16,11 +18,8 @@
#include "google/protobuf/compiler/java/field_common.h" #include "google/protobuf/compiler/java/field_common.h"
#include "google/protobuf/compiler/java/generator_common.h" #include "google/protobuf/compiler/java/generator_common.h"
#include "google/protobuf/compiler/java/helpers.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/java/name_resolver.h"
#include "google/protobuf/compiler/kotlin/field.h"
#include "google/protobuf/io/printer.h" #include "google/protobuf/io/printer.h"
// Must be last. // Must be last.
@ -39,36 +38,17 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
lite_(!java::HasDescriptorMethods(descriptor_->file(), lite_(!java::HasDescriptorMethods(descriptor_->file(),
context->EnforceLite())), context->EnforceLite())),
jvm_dsl_(!lite_ || context->options().jvm_dsl), jvm_dsl_(!lite_ || context->options().jvm_dsl),
lite_field_generators_( field_generators_(java::FieldGeneratorMap<FieldGenerator>(descriptor_)) {
java::FieldGeneratorMap<java::ImmutableFieldLiteGenerator>(
descriptor_)),
field_generators_(
java::FieldGeneratorMap<java::ImmutableFieldGenerator>(descriptor_)) {
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
if (java::IsRealOneof(descriptor_->field(i))) { if (java::IsRealOneof(descriptor_->field(i))) {
const OneofDescriptor* oneof = descriptor_->field(i)->containing_oneof(); const OneofDescriptor* oneof = descriptor_->field(i)->containing_oneof();
ABSL_CHECK(oneofs_.emplace(oneof->index(), oneof).first->second == 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++) { for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\n"); const FieldDescriptor* field = descriptor->field(i);
if (lite_) { auto generator = std::make_unique<FieldGenerator>(field, context_, lite_);
lite_field_generators_.get(descriptor_->field(i)) field_generators_.Add(field, std::move(generator));
.GenerateKotlinDslMembers(printer);
} else {
field_generators_.get(descriptor_->field(i))
.GenerateKotlinDslMembers(printer);
}
} }
} }
@ -97,7 +77,10 @@ void MessageGenerator::Generate(io::Printer* printer) const {
printer->Indent(); 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_) { for (auto& kv : oneofs_) {
java::JvmNameContext name_ctx = {context_->options(), printer, lite_}; java::JvmNameContext name_ctx = {context_->options(), printer, lite_};

@ -11,9 +11,8 @@
#include "absl/container/btree_map.h" #include "absl/container/btree_map.h"
#include "google/protobuf/compiler/java/context.h" #include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/generator_common.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/java/name_resolver.h"
#include "google/protobuf/compiler/kotlin/field.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
namespace google { namespace google {
@ -40,11 +39,7 @@ class MessageGenerator {
bool lite_; bool lite_;
bool jvm_dsl_; bool jvm_dsl_;
// TODO: b/366047913 - These can be simplified once lite and full field java::FieldGeneratorMap<FieldGenerator> field_generators_;
// generators are unified.
java::FieldGeneratorMap<java::ImmutableFieldLiteGenerator>
lite_field_generators_;
java::FieldGeneratorMap<java::ImmutableFieldGenerator> field_generators_;
void GenerateExtensions(io::Printer* printer) const; void GenerateExtensions(io::Printer* printer) const;
void GenerateOrNull(io::Printer* printer) const; void GenerateOrNull(io::Printer* printer) const;

Loading…
Cancel
Save