Kotlin: Simplify kotlin_name method
This also prepares for it being a central way to control name generation
for other Kotlin API entities like methods.
Bug: b/450252312
Test: ./gradlew connectedAndroidTest
Change-Id: I431d5f968f1d3036efd0ce44d2cc3fdbdfd025d4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/270574
Reviewed-by: Mridul Goyal <mridulgoyal@google.com>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Jim Blackler <jimblackler@google.com>
diff --git a/generator/dawn_json_generator.py b/generator/dawn_json_generator.py
index c761ca0..ef94071 100644
--- a/generator/dawn_json_generator.py
+++ b/generator/dawn_json_generator.py
@@ -929,8 +929,8 @@
)
return None
- def kotlin_name(name, category=None):
- return ('GPU' if category == 'object' else '') + name
+ def kotlin_name(type):
+ return f"{'GPU' if type.category == 'object' else ''}{type.name.CamelCase()}"
def kotlin_return(method):
for argument in method.arguments:
@@ -990,7 +990,7 @@
if type.category == 'kotlin type':
# Standard library Kotlin class (with namespace) just needs converting.
return type.name.get().replace('.', '/')
- return f"{kt_file_path}/{kotlin_name(type.name.CamelCase(), category)}"
+ return f"{kt_file_path}/{kotlin_name(type)}"
# A structure may need to know which other structures listed it as a chain root, e.g.
# to know whether to mark the generated class 'open'.
diff --git a/generator/templates/art/api_kotlin_async_helpers.kt b/generator/templates/art/api_kotlin_async_helpers.kt
index a09a691..4d8f857 100644
--- a/generator/templates/art/api_kotlin_async_helpers.kt
+++ b/generator/templates/art/api_kotlin_async_helpers.kt
@@ -70,7 +70,7 @@
{%- endif %}
//* The wrapped method has executor and callback function stripped out (the wrapper supplies
//* those so the client doesn't have to).
- public suspend fun {{ kotlin_name(obj.name.CamelCase(), 'object') }}.{{ method.name.camelCase() }}(
+ public suspend fun {{ kotlin_name(obj) }}.{{ method.name.camelCase() }}(
{%- for arg in kotlin_record_members(method.arguments) if not (
arg.type.category == 'callback function' or
(arg.type.category == 'kotlin type' and arg.type.name.get() == 'java.util.concurrent.Executor')
diff --git a/generator/templates/art/api_kotlin_object.kt b/generator/templates/art/api_kotlin_object.kt
index d6a965e..6115a09 100644
--- a/generator/templates/art/api_kotlin_object.kt
+++ b/generator/templates/art/api_kotlin_object.kt
@@ -37,7 +37,7 @@
{% if doc_str | trim %}
{{ generate_simple_kdoc(doc_str) }}
{% endif %}
-public class {{ kotlin_name(obj.name.CamelCase(), 'object') }}(public val handle: Long): AutoCloseable {
+public class {{ kotlin_name(obj) }}(public val handle: Long): AutoCloseable {
{% set all_method_info = object_info.methods if object_info else {} %}
{% for method in obj.methods if include_method(obj, method) %}
//* Generating KDocs
@@ -79,6 +79,6 @@
//* A structural comparison of the wrapper object is equivalent to a referential comparison of
//* the wrapped object.
override fun equals(other: Any?): Boolean =
- other is {{ kotlin_name(obj.name.CamelCase(), 'object') }} && other.handle == handle
+ other is {{ kotlin_name(obj) }} && other.handle == handle
override fun hashCode(): Int = handle.hashCode()
}
diff --git a/generator/templates/art/api_kotlin_types.kt b/generator/templates/art/api_kotlin_types.kt
index 015dd0f..11f2ef5 100644
--- a/generator/templates/art/api_kotlin_types.kt
+++ b/generator/templates/art/api_kotlin_types.kt
@@ -41,14 +41,14 @@
{%- elif arg.length and arg.length != 'constant' %}
{# * annotation can mean an array, e.g. an output argument #}
{%- if type.category in ['callback function', 'callback info', 'function pointer', 'object', 'structure'] -%}
- Array<{{ kotlin_name(type.name.CamelCase(), type.category) }}>
+ Array<{{ kotlin_name(type) }}>
{%- elif type.category in ['bitmask', 'enum'] or type.name.get() in ['int', 'int32_t', 'uint32_t'] -%}
IntArray
{%- else -%}
{{ unreachable_code() }}
{% endif %}
{%- elif type.category in ['callback function', 'function pointer', 'object'] %}
- {{- kotlin_name(type.name.CamelCase(), type.category) }}
+ {{- kotlin_name(type) }}
{%- if optional or default_value %}?{% endif %}
{%- elif type.category == 'structure' or type.category == 'callback info' %}
{{- type.name.CamelCase() }}{{ '?' if optional }}
diff --git a/generator/templates/art/methods.cpp b/generator/templates/art/methods.cpp
index 6d7563c..22ff6e0 100644
--- a/generator/templates/art/methods.cpp
+++ b/generator/templates/art/methods.cpp
@@ -65,7 +65,7 @@
}
{% macro render_method(method, object) %}
- {% set ObjectName = kotlin_name(object.name.CamelCase(), 'object') if object else "FunctionsKt" %}
+ {% set ObjectName = kotlin_name(object) if object else "FunctionsKt" %}
{% set FunctionSuffix = ObjectName + "_" + method.name.camelCase() %}
{% set KotlinRecord = FunctionSuffix + "KotlinRecord" %}
{% set ArgsStruct = FunctionSuffix + "ArgsStruct" %}
@@ -192,7 +192,7 @@
//* Every object gets a Release method, to supply a Kotlin AutoCloseable.
extern "C"
JNIEXPORT void JNICALL
- Java_{{ kotlin_package.replace('.', '_') }}_{{ kotlin_name(obj.name.CamelCase(), 'object') }}_close(
+ Java_{{ kotlin_package.replace('.', '_') }}_{{ kotlin_name(obj) }}_close(
JNIEnv *env, jobject obj) {
JNIClasses* classes = JNIClasses::getInstance(env);
jclass clz = classes->{{ obj.name.camelCase() }};