Skip to content

Commit

Permalink
Moving Kotlin DSL message generators to the kotlin/ directory.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 675162587
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 19, 2024
1 parent 7a7a087 commit 9c14d53
Show file tree
Hide file tree
Showing 18 changed files with 539 additions and 725 deletions.
5 changes: 4 additions & 1 deletion src/google/protobuf/compiler/java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ cc_library(
"generator_factory.h",
],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"],
visibility = [
"//src/google/protobuf/compiler/java:__subpackages__",
"//src/google/protobuf/compiler/kotlin:__subpackages__",
],
deps = [
":helpers",
"//src/google/protobuf",
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/field_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +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);
(*variables)["jvm_synthetic"] = JvmSynthetic(info->options.jvm_dsl);
if (!descriptor->is_repeated()) {
(*variables)["annotation_field_type"] =
std::string(FieldTypeName(descriptor->type()));
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/java/full/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cc_library(
name = "fg",
hdrs = ["field_generator.h"],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/kotlin:__subpackages__"],
deps = [
"//src/google/protobuf/compiler/java:generator_common",
"//src/google/protobuf/io:printer",
Expand Down Expand Up @@ -49,6 +50,7 @@ cc_library(
srcs = ["make_field_gens.cc"],
hdrs = ["make_field_gens.h"],
strip_include_prefix = "/src",
visibility = ["//src/google/protobuf/compiler/kotlin:__subpackages__"],
deps = [
":fg",
":fgs",
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/full/field_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ImmutableFieldGenerator : public FieldGenerator {
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
virtual void GenerateFieldBuilderInitializationCode(
io::Printer* printer) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
// virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;

virtual void GenerateBuilderParsingCodeFromPacked(
io::Printer* printer) const {
Expand Down
300 changes: 0 additions & 300 deletions src/google/protobuf/compiler/java/full/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1208,306 +1208,6 @@ void ImmutableMessageGenerator::GenerateMutableCopy(io::Printer* printer) {
"mutable_name", name_resolver_->GetJavaMutableClassName(descriptor_));
}

void ImmutableMessageGenerator::GenerateKotlinDsl(io::Printer* printer) const {
printer->Print(
"@kotlin.OptIn"
"(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
"@com.google.protobuf.kotlin.ProtoDslMarker\n");
printer->Print(
"public class Dsl private constructor(\n"
" private val _builder: $message$.Builder\n"
") {\n"
" public companion object {\n"
" @kotlin.jvm.JvmSynthetic\n"
" @kotlin.PublishedApi\n"
" internal fun _create(builder: $message$.Builder): Dsl = "
"Dsl(builder)\n"
" }\n"
"\n"
" @kotlin.jvm.JvmSynthetic\n"
" @kotlin.PublishedApi\n"
" internal fun _build(): $message$ = _builder.build()\n",
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));

printer->Indent();

for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\n");
field_generators_.get(descriptor_->field(i))
.GenerateKotlinDslMembers(printer);
}

for (auto& kv : oneofs_) {
const OneofDescriptor* oneof = kv.second;
printer->Print(
"public val $oneof_name$Case: $message$.$oneof_capitalized_name$Case\n"
" @JvmName(\"get$oneof_capitalized_name$Case\")\n"
" get() = _builder.get$oneof_capitalized_name$Case()\n\n"
"public fun clear$oneof_capitalized_name$() {\n"
" _builder.clear$oneof_capitalized_name$()\n"
"}\n",
"oneof_name", context_->GetOneofGeneratorInfo(oneof)->name,
"oneof_capitalized_name",
context_->GetOneofGeneratorInfo(oneof)->capitalized_name, "message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));
}

if (descriptor_->extension_range_count() > 0) {
GenerateKotlinExtensions(printer);
}

printer->Outdent();
printer->Print("}\n");
}

void ImmutableMessageGenerator::GenerateKotlinMembers(
io::Printer* printer) const {
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.() -> "
"kotlin.Unit): "
"$message$ "
"=\n"
" $message_kt$.Dsl._create($message$.newBuilder()).apply { block() "
"}._build()\n",
"camelcase_name", name_resolver_->GetKotlinFactoryName(descriptor_),
"message_kt",
EscapeKotlinKeywords(
name_resolver_->GetKotlinExtensionsClassName(descriptor_)),
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)));

WriteMessageDocComment(printer, descriptor_, context_->options(),
/* kdoc */ true);
printer->Emit(
{
io::Printer::Sub{"name_kt", absl::StrCat(descriptor_->name(), "Kt")}
.AnnotatedAs(descriptor_),
{"body",
[&]() {
GenerateKotlinDsl(printer);
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
ImmutableMessageGenerator(descriptor_->nested_type(i), context_)
.GenerateKotlinMembers(printer);
}
}},
},
R"kt(
public object $name_kt$ {
$body$;
}
)kt");
}

void ImmutableMessageGenerator::GenerateTopLevelKotlinMembers(
io::Printer* printer) const {
printer->Print("@kotlin.jvm.JvmSynthetic\n");


printer->Print(
"public inline fun $message$.copy(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): $message$ =\n"
" $message_kt$.Dsl._create(this.toBuilder()).apply { block() "
"}._build()\n\n",
"message",
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true)),
"message_kt",
name_resolver_->GetKotlinExtensionsClassNameEscaped(descriptor_));

for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
ImmutableMessageGenerator(descriptor_->nested_type(i), context_)
.GenerateTopLevelKotlinMembers(printer);
}

GenerateKotlinOrNull(printer);
}

void ImmutableMessageGenerator::GenerateKotlinOrNull(
io::Printer* printer) const {
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (field->has_presence() && GetJavaType(field) == JAVATYPE_MESSAGE) {
if (field->options().deprecated()) {
printer->Print(
"@kotlin.Deprecated(message = \"Field $name$ is deprecated\")\n",
"name", context_->GetFieldGeneratorInfo(field)->name);
}
printer->Print(
"public val $full_classname$OrBuilder.$camelcase_name$OrNull: "
"$full_name$?\n"
" get() = if (has$name$()) get$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);
}
}
}

void ImmutableMessageGenerator::GenerateKotlinExtensions(
io::Printer* printer) const {
std::string message_name =
EscapeKotlinKeywords(name_resolver_->GetClassName(descriptor_, true));

printer->Print(
"@Suppress(\"UNCHECKED_CAST\")\n"
"@kotlin.jvm.JvmSynthetic\n"
"public operator fun <T : kotlin.Any> get(extension: "
"com.google.protobuf.ExtensionLite<$message$, T>): T {\n"
" return if (extension.isRepeated) {\n"
" get(extension as com.google.protobuf.ExtensionLite<$message$, "
"kotlin.collections.List<*>>) as T\n"
" } else {\n"
" _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);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public operator fun contains(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>): "
"Boolean {\n"
" return _builder.hasExtension(extension)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public fun clear(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>) "
"{\n"
" _builder.clearExtension(extension)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"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);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : Comparable<T>> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
" value: T\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, "
"com.google.protobuf.ByteString>,\n"
" value: com.google.protobuf.ByteString\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : com.google.protobuf.MessageLite> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
" value: T\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"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);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E : kotlin.Any> "
"com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.plusAssign"
"(value: E) {\n"
" add(value)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"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);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E : kotlin.Any> "
"com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.plusAssign(values: "
"Iterable<E>) {\n"
" addAll(values)\n"
"}\n\n",
"message", message_name);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"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);

printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline fun com.google.protobuf.kotlin.ExtensionList<*, "
"$message$>.clear() {\n"
" clear(extension)\n"
"}\n\n",
"message", message_name);
}

void ImmutableMessageGenerator::GenerateAnyMethods(io::Printer* printer) {
printer->Print(
"private static String getTypeUrl(\n"
Expand Down
5 changes: 0 additions & 5 deletions src/google/protobuf/compiler/java/full/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ class ImmutableMessageGenerator : public MessageGenerator {

// Returns an estimate of the number of bytes the printed code will compile to
int GenerateStaticVariableInitializers(io::Printer* printer) override;
void GenerateKotlinDsl(io::Printer* printer) const override;
void GenerateKotlinMembers(io::Printer* printer) const override;
void GenerateTopLevelKotlinMembers(io::Printer* printer) const override;

private:
void GenerateFieldAccessorTable(io::Printer* printer, int* bytecode_estimate);
Expand All @@ -76,8 +73,6 @@ class ImmutableMessageGenerator : public MessageGenerator {
void GenerateParser(io::Printer* printer);
void GenerateParsingConstructor(io::Printer* printer);
void GenerateMutableCopy(io::Printer* printer);
void GenerateKotlinExtensions(io::Printer* printer) const;
void GenerateKotlinOrNull(io::Printer* printer) const;
void GenerateAnyMethods(io::Printer* printer);

Context* context_;
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/java/generator_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FieldGenerator {
public:
virtual ~FieldGenerator() = default;
virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
};

// Convenience class which constructs FieldGenerators for a Descriptor.
Expand Down
3 changes: 0 additions & 3 deletions src/google/protobuf/compiler/java/generator_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class MessageGenerator {
// Generate code to register all contained extensions with an
// ExtensionRegistry.
virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
virtual void GenerateKotlinDsl(io::Printer* printer) const = 0;
virtual void GenerateKotlinMembers(io::Printer* printer) const = 0;
virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0;

protected:
const Descriptor* descriptor_;
Expand Down
Loading

0 comments on commit 9c14d53

Please sign in to comment.