Parameterized the Kotlin DSL code generator so that it can emit output that works on non-JVM platforms.

Note however that we do not currently have open-source support non-JVM platforms for the main (non-DSL) Protobuf library.

PiperOrigin-RevId: 653751253
pull/17446/head
Joshua Haberman 7 months ago committed by Copybara-Service
parent b764fd9a19
commit 8995b485b9
  1. 1
      src/google/protobuf/compiler/java/BUILD.bazel
  2. 1
      src/google/protobuf/compiler/java/context.cc
  3. 1
      src/google/protobuf/compiler/java/field_common.cc
  4. 2
      src/google/protobuf/compiler/java/field_common.h
  5. 17
      src/google/protobuf/compiler/java/helpers.h
  6. 7
      src/google/protobuf/compiler/java/kotlin_generator.cc
  7. 167
      src/google/protobuf/compiler/java/lite/enum_field.cc
  8. 65
      src/google/protobuf/compiler/java/lite/map_field.cc
  9. 168
      src/google/protobuf/compiler/java/lite/message.cc
  10. 163
      src/google/protobuf/compiler/java/lite/message_field.cc
  11. 167
      src/google/protobuf/compiler/java/lite/primitive_field.cc
  12. 129
      src/google/protobuf/compiler/java/lite/string_field.cc
  13. 8
      src/google/protobuf/compiler/java/options.h

@ -197,6 +197,7 @@ cc_library(
"//src/google/protobuf",
"//src/google/protobuf:port",
"//src/google/protobuf/compiler:code_generator",
"@com_google_absl//absl/strings",
],
)

@ -153,6 +153,7 @@ void Context::InitializeFieldGeneratorInfoForFields(
absl::StrAppend(&info.capitalized_name, field->number());
info.disambiguated_reason = conflict_reason[i];
}
info.options = options_;
field_generator_info_map_[field] = info;
}
}

@ -41,6 +41,7 @@ void SetCommonFieldVariables(
(*variables)["kt_capitalized_name"] =
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_")
: info->capitalized_name;
(*variables)["jvm_synthetic"] = JvmSynthetic(info->options);
if (!descriptor->is_repeated()) {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type()));

@ -3,6 +3,7 @@
#include <string>
#include "google/protobuf/compiler/java/options.h"
#include "google/protobuf/descriptor.h"
namespace google {
@ -15,6 +16,7 @@ struct FieldGeneratorInfo {
std::string name;
std::string capitalized_name;
std::string disambiguated_reason;
Options options;
};
// Oneof information used in OneofFieldGenerators.

@ -380,6 +380,23 @@ const FieldDescriptor* MapKeyField(const FieldDescriptor* descriptor);
const FieldDescriptor* MapValueField(const FieldDescriptor* descriptor);
inline std::string JvmSynthetic(const Options& options) {
return options.jvm_dsl ? "@kotlin.jvm.JvmSynthetic\n" : "";
}
struct JvmNameContext {
const Options& options;
io::Printer* printer;
};
inline void JvmName(absl::string_view name, const JvmNameContext& context) {
if (!context.options.jvm_dsl) return;
context.printer->Emit("@kotlin.jvm.JvmName(\"");
// Note: `name` will likely have vars in it that we do want to interpolate.
context.printer->Emit(name);
context.printer->Emit("\")\n");
}
} // namespace java
} // namespace compiler
} // namespace protobuf

@ -7,6 +7,7 @@
#include "google/protobuf/compiler/java/kotlin_generator.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/java/file.h"
#include "google/protobuf/compiler/java/generator.h"
@ -57,6 +58,8 @@ bool KotlinGenerator::Generate(const FileDescriptor* file,
file_options.annotation_list_file = option.second;
} else if (option.first == "experimental_strip_nonfunctional_codegen") {
file_options.strip_nonfunctional_codegen = true;
} else if (option.first == "no_jvm_dsl") {
file_options.jvm_dsl = false;
} else {
*error = absl::StrCat("Unknown generator option: ", option.first);
return false;
@ -81,8 +84,8 @@ bool KotlinGenerator::Generate(const FileDescriptor* file,
return std::unique_ptr<io::ZeroCopyOutputStream>(context->Open(filename));
};
std::string package_dir = JavaPackageToDir(file_generator->java_package());
std::string kotlin_filename =
absl::StrCat(package_dir, file_generator->GetKotlinClassname(), ".kt");
std::string kotlin_filename = absl::StrCat(
package_dir, file_generator->GetKotlinClassname(), ".proto.kt");
all_files.push_back(kotlin_filename);
std::string info_full_path = absl::StrCat(kotlin_filename, ".pb.meta");
if (file_options.annotate_code) {

@ -303,24 +303,37 @@ void ImmutableEnumFieldLiteGenerator::GenerateBuilderMembers(
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->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");
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->Print(
variables_,
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"
" @JvmName(\"${$get$kt_capitalized_name$Value$}$\")\n"
" $jvm_name_get$"
" get() = $kt_dsl_builder$.${$$kt_property_name$Value$}$\n"
" @JvmName(\"${$set$kt_capitalized_name$Value$}$\")\n"
" $jvm_name_set$"
" set(value) {\n"
" $kt_dsl_builder$.${$$kt_property_name$Value$}$ = value\n"
" }\n");
@ -329,17 +342,16 @@ void ImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
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->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(
variables_,
"public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
" return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
"}\n");
@ -894,8 +906,9 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateInitializationCode(
void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
JvmNameContext name_ctx = {context_->options(), printer};
printer->Print(
variables_,
"/**\n"
" * An uninstantiable, behaviorless type to represent the field in\n"
" * generics.\n"
@ -907,59 +920,73 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
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");
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->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"
"}");
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->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"
"}");
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->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"
"}");
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->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
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$}$>."
@ -970,10 +997,12 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
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"
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"
@ -983,14 +1012,18 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateKotlinDslMembers(
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"
"}");
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 {

@ -837,8 +837,9 @@ void ImmutableMapFieldLiteGenerator::GenerateBuilderMembers(
void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
JvmNameContext name_ctx = {context_->options(), printer};
printer->Print(
variables_,
"/**\n"
" * An uninstantiable, behaviorless type to represent the field in\n"
" * generics.\n"
@ -850,22 +851,27 @@ void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
WriteFieldDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print(
variables_,
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"
" @kotlin.jvm.JvmSynthetic\n"
" @JvmName(\"get$kt_capitalized_name$Map\")\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->Print(
variables_,
"@JvmName(\"put$kt_capitalized_name$\")\n"
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"
@ -874,10 +880,12 @@ void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
WriteFieldDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@JvmName(\"set$kt_capitalized_name$\")\n"
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"
@ -887,10 +895,13 @@ void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
WriteFieldDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@JvmName(\"remove$kt_capitalized_name$\")\n"
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"
@ -899,10 +910,13 @@ void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
WriteFieldDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@JvmName(\"putAll$kt_capitalized_name$\")\n"
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$>) "
@ -912,10 +926,13 @@ void ImmutableMapFieldLiteGenerator::GenerateKotlinDslMembers(
WriteFieldDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@JvmName(\"clear$kt_capitalized_name$\")\n"
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"

@ -751,16 +751,16 @@ void ImmutableMessageLiteGenerator::GenerateKotlinDsl(
" private val _builder: $message$.Builder\n"
") {\n"
" public companion object {\n"
" @kotlin.jvm.JvmSynthetic\n"
"$ jvm_synthetic$"
" @kotlin.PublishedApi\n"
" internal fun _create(builder: $message$.Builder): Dsl = "
"Dsl(builder)\n"
" }\n"
"\n"
" @kotlin.jvm.JvmSynthetic\n"
"$ jvm_synthetic$"
" @kotlin.PublishedApi\n"
" internal fun _build(): $message$ = _builder.build()\n",
"message",
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));
printer->Indent();
@ -772,19 +772,26 @@ void ImmutableMessageLiteGenerator::GenerateKotlinDsl(
}
for (auto& kv : oneofs_) {
JvmNameContext name_ctx = {context_->options(), printer};
const OneofDescriptor* oneof = kv.second;
auto oneof_name = context_->GetOneofGeneratorInfo(oneof)->name;
printer->Print(
printer->Emit(
{
{"jvm_name",
[&] { JvmName("get$oneof_capitalized_name$Case", name_ctx); }},
{"oneof_name", oneof_name},
{"oneof_capitalized_name",
context_->GetOneofGeneratorInfo(oneof)->capitalized_name},
{"oneof_property_name", GetKotlinPropertyName(oneof_name)},
{"message", EscapeKotlinKeywords(
name_resolver_->GetClassName(descriptor_, true))},
},
"public val $oneof_name$Case: $message$.$oneof_capitalized_name$Case\n"
" @JvmName(\"get$oneof_capitalized_name$Case\")\n"
"$jvm_name$"
" get() = _builder.$oneof_property_name$Case\n\n"
"public fun clear$oneof_capitalized_name$() {\n"
" _builder.clear$oneof_capitalized_name$()\n"
"}\n",
"oneof_name", oneof_name, "oneof_capitalized_name",
context_->GetOneofGeneratorInfo(oneof)->capitalized_name,
"oneof_property_name", GetKotlinPropertyName(oneof_name), "message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));
"}\n");
}
if (descriptor_->extension_range_count() > 0) {
@ -797,9 +804,11 @@ void ImmutableMessageLiteGenerator::GenerateKotlinDsl(
void ImmutableMessageLiteGenerator::GenerateKotlinMembers(
io::Printer* printer) const {
printer->Print("@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n",
"camelcase_name",
name_resolver_->GetKotlinFactoryName(descriptor_));
if (context_->options().jvm_dsl) {
printer->Print("@kotlin.jvm.JvmName(\"-initialize$camelcase_name$\")\n",
"camelcase_name",
name_resolver_->GetKotlinFactoryName(descriptor_));
}
printer->Print(
"public inline fun $camelcase_name$(block: $message_kt$.Dsl.() -> "
@ -850,7 +859,9 @@ void ImmutableMessageLiteGenerator::GenerateTopLevelKotlinMembers(
void ImmutableMessageLiteGenerator::GenerateKotlinOrNull(
io::Printer* printer) const {
// Generate getFieldOrNull getters for all optional message fields.
// Generate getFieldOrNull getters for all optional message fields in both
// `Foo` and `Foo.Builder`. We don't simply generate them in `FooOrBuilder`
// because some Kotlin proto implementations don't have `FooOrBuilder`.
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (field->has_presence() && GetJavaType(field) == JAVATYPE_MESSAGE) {
@ -860,28 +871,46 @@ void ImmutableMessageLiteGenerator::GenerateKotlinOrNull(
"name", context_->GetFieldGeneratorInfo(field)->name);
}
printer->Print(
"public val $full_classname$OrBuilder.$camelcase_name$OrNull: "
"public val $full_classname$.$camelcase_name$OrNull: "
"$full_name$?\n"
" get() = if (has$name$()) get$name$() else null\n\n",
" get() = if (has$capitalized_name$()) this.$name$ else null\n\n",
"full_classname",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)),
"camelcase_name", context_->GetFieldGeneratorInfo(field)->name,
"full_name",
EscapeKotlinKeywords(
name_resolver_->GetImmutableClassName(field->message_type())),
"name", context_->GetFieldGeneratorInfo(field)->capitalized_name);
"capitalized_name",
context_->GetFieldGeneratorInfo(field)->capitalized_name, "name",
EscapeKotlinKeywords(GetKotlinPropertyName(
context_->GetFieldGeneratorInfo(field)->capitalized_name)));
printer->Print(
"public val $full_classname$.Builder.$camelcase_name$OrNull: "
"$full_name$?\n"
" get() = if (has$capitalized_name$()) this.$name$ else null\n\n",
"full_classname",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)),
"camelcase_name", context_->GetFieldGeneratorInfo(field)->name,
"full_name",
EscapeKotlinKeywords(
name_resolver_->GetImmutableClassName(field->message_type())),
"capitalized_name",
context_->GetFieldGeneratorInfo(field)->capitalized_name, "name",
EscapeKotlinKeywords(GetKotlinPropertyName(
context_->GetFieldGeneratorInfo(field)->capitalized_name)));
}
}
}
void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
io::Printer* printer) const {
JvmNameContext name_ctx = {context_->options(), printer};
std::string message_name =
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true));
printer->Print(
"@Suppress(\"UNCHECKED_CAST\")\n"
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public operator fun <T : kotlin.Any> get(extension: "
"com.google.protobuf.ExtensionLite<$message$, T>): T {\n"
" return if (extension.isRepeated) {\n"
@ -891,51 +920,63 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
" _builder.getExtension(extension)\n"
" }\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.OptIn"
"(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
"@kotlin.jvm.JvmName(\"-getRepeatedExtension\")\n"
"public operator fun <E : kotlin.Any> get(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, "
"kotlin.collections.List<E>>\n"
"): com.google.protobuf.kotlin.ExtensionList<E, $message$> {\n"
" return com.google.protobuf.kotlin.ExtensionList(extension, "
"_builder.getExtension(extension))\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
if (context_->options().jvm_dsl) {
// TODO: generate this on Kotlin Native once the [Mutable]List
// issue is resolved.
printer->Emit(
{
{"jvm_name", [&] { JvmName("-getRepeatedExtension", name_ctx); }},
{"jvm_synthetic", JvmSynthetic(context_->options())},
{"message", message_name},
},
"$jvm_synthetic$"
"@kotlin.OptIn"
"(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
"$jvm_name$"
"public operator fun <E : kotlin.Any> get(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, "
"kotlin.collections.List<E>>\n"
"): com.google.protobuf.kotlin.ExtensionList<E, $message$> {\n"
" return com.google.protobuf.kotlin.ExtensionList(extension, "
"_builder.getExtension(extension))\n"
"}\n\n");
}
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public operator fun contains(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>): "
"Boolean {\n"
" return _builder.hasExtension(extension)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public fun clear(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>) "
"{\n"
" _builder.clearExtension(extension)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public fun <T : kotlin.Any> setExtension(extension: "
"com.google.protobuf.ExtensionLite<$message$, T>, "
"value: T) {\n"
" _builder.setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : Comparable<T>> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
@ -943,10 +984,11 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, "
@ -955,10 +997,11 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : com.google.protobuf.MessageLite> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
@ -966,18 +1009,24 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
// TODO: generate these methods on Kotlin Native once the
// [Mutable]List issue is resolved.
if (!context_->options().jvm_dsl) return;
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public fun<E : kotlin.Any> com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.add(value: E) {\n"
" _builder.addExtension(this.extension, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E : kotlin.Any> "
"com.google.protobuf.kotlin.ExtensionList<E, "
@ -985,20 +1034,22 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
"(value: E) {\n"
" add(value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public fun<E : kotlin.Any> com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.addAll(values: Iterable<E>) {\n"
" for (value in values) {\n"
" add(value)\n"
" }\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E : kotlin.Any> "
"com.google.protobuf.kotlin.ExtensionList<E, "
@ -1006,26 +1057,29 @@ void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
"Iterable<E>) {\n"
" addAll(values)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"public operator fun <E : kotlin.Any> "
"com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.set(index: Int, value: "
"E) {\n"
" _builder.setExtension(this.extension, index, value)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"$jvm_synthetic$"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline fun com.google.protobuf.kotlin.ExtensionList<*, "
"$message$>.clear() {\n"
" clear(extension)\n"
"}\n\n",
"message", message_name);
"jvm_synthetic", JvmSynthetic(context_->options()), "message",
message_name);
}
} // namespace java

@ -279,30 +279,37 @@ void ImmutableMessageFieldLiteGenerator::GenerateBuilderMembers(
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->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");
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(variables_,
"public fun ${$clear$kt_capitalized_name$$}$() {\n"
" $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
"}\n");
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(
variables_,
"public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
" return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
"}\n");
@ -313,10 +320,10 @@ void ImmutableMessageFieldLiteGenerator::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");
printer->Print(
"$kt_deprecation$\n"
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");
}
}
@ -802,8 +809,9 @@ std::string RepeatedImmutableMessageFieldLiteGenerator::GetBoxedType() const {
void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
JvmNameContext name_ctx = {context_->options(), printer};
printer->Print(
variables_,
"/**\n"
" * An uninstantiable, behaviorless type to represent the field in\n"
" * generics.\n"
@ -815,59 +823,73 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
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");
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->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");
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->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");
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->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");
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->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
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$}$>."
@ -878,10 +900,12 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
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"
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"
@ -891,14 +915,19 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
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");
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

@ -328,43 +328,55 @@ void ImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
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->Print(
variables_,
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"
" @JvmName(\"${$get$kt_capitalized_name$$}$\")\n"
" $jvm_name_get$"
" get() = $kt_dsl_builder$.get${$$kt_capitalized_name$$}$()\n"
" @JvmName(\"${$set$kt_capitalized_name$$}$\")\n"
" $jvm_name_set$"
" 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");
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(variables_,
"public fun ${$clear$kt_capitalized_name$$}$() {\n"
" $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
"}\n");
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(
variables_,
"public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
" return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
"}\n");
@ -713,8 +725,9 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateBuilderMembers(
void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
JvmNameContext name_ctx = {context_->options(), printer};
printer->Print(
variables_,
"/**\n"
" * An uninstantiable, behaviorless type to represent the field in\n"
" * generics.\n"
@ -725,59 +738,73 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers(
" : com.google.protobuf.kotlin.DslProxy()\n");
WriteFieldDocComment(printer, descriptor_, context_->options());
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");
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->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"
"}");
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->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"
"}");
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->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"
"}");
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->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.jvm.JvmName(\"plusAssignAll$kt_capitalized_name$\")\n"
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$}$>."
@ -788,10 +815,12 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers(
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"
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"
@ -801,14 +830,18 @@ void RepeatedImmutablePrimitiveFieldLiteGenerator::GenerateKotlinDslMembers(
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"
"}");
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(

@ -332,31 +332,38 @@ void ImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
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->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");
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(variables_,
"public fun ${$clear$kt_capitalized_name$$}$() {\n"
" $kt_dsl_builder$.${$clear$capitalized_name$$}$()\n"
"}\n");
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(
variables_,
"public fun ${$has$kt_capitalized_name$$}$(): kotlin.Boolean {\n"
" return $kt_dsl_builder$.${$has$capitalized_name$$}$()\n"
"}\n");
@ -803,8 +810,9 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateBuilderMembers(
void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
JvmNameContext name_ctx = {context_->options(), printer};
printer->Print(
variables_,
"/**\n"
" * An uninstantiable, behaviorless type to represent the field in\n"
" * generics.\n"
@ -819,7 +827,6 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
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"
@ -833,37 +840,47 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
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");
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->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");
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->Print(
variables_,
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.jvm.JvmName(\"addAll$kt_capitalized_name$\")\n"
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"
@ -874,10 +891,13 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
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"
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$}$>."
@ -889,10 +909,12 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
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"
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"
@ -902,14 +924,17 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateKotlinDslMembers(
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"
"}");
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(

@ -25,8 +25,8 @@ struct Options {
generate_shared_code(false),
enforce_lite(false),
annotate_code(false),
strip_nonfunctional_codegen(false) {
}
strip_nonfunctional_codegen(false),
jvm_dsl(true) {}
bool generate_immutable_code;
bool generate_mutable_code;
@ -46,6 +46,10 @@ struct Options {
std::string output_list_file;
// If true, strip out nonfunctional codegen.
bool strip_nonfunctional_codegen;
// If true, generate JVM-specific DSL code. This defaults to true for
// compatibility with the old behavior.
bool jvm_dsl;
};
} // namespace java

Loading…
Cancel
Save