Skip to content

Commit 9518b1c

Browse files
authored
feat: ArgVariant annotation and plot methods with count parameter (#264)
* feat(generator): introduce ArgVariant annotation New annotation provides an ability to generate methods definitions for different types and names. * feat(api): expose count param for plot* methods
1 parent cae0581 commit 9518b1c

File tree

8 files changed

+11032
-5212
lines changed

8 files changed

+11032
-5212
lines changed

buildSrc/src/main/kotlin/tool/generator/api/BindingSourceProcessor.kt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import spoon.reflect.declaration.CtElement
55
import spoon.reflect.declaration.CtField
66
import spoon.reflect.declaration.CtMethod
77
import spoon.reflect.declaration.CtType
8+
import kotlin.math.max
89

910
class BindingSourceProcessor(
1011
private val type: CtType<*>
@@ -101,7 +102,53 @@ class BindingSourceProcessor(
101102
contentToReplace[ReplacePos(it.position.sourceStart, it.position.sourceEnd + 6)] = ""
102103
}
103104

104-
val content = jvmMethodContent(method) + jniMethodContent(method)
105+
val content = mutableListOf<String>()
106+
107+
// Handle argument variants
108+
if (method.parameters.find { it.hasAnnotation(A_NAME_ARG_VARIANT) } != null) {
109+
data class ArgVar(val idx: Int, val type: String, val name: String)
110+
111+
val variants = mutableListOf<ArgVar>()
112+
113+
method.parameters.forEachIndexed { idx, p ->
114+
if (p.hasAnnotation(A_NAME_ARG_VARIANT)) {
115+
val a = p.getAnnotation(A_NAME_ARG_VARIANT)!!
116+
val types = (a.getValueAsObject(A_VALUE_TYPE) as Array<*>).map { it.toString() }
117+
val names = (a.getValueAsObject(A_VALUE_NAME) as Array<*>).map { it.toString() }
118+
119+
if (types.isNotEmpty() && names.isNotEmpty() && types.size != names.size) {
120+
error("Types size should be the as names! Current: types=${types.size}, names=${names.size}")
121+
}
122+
123+
for (i in 0 until max(types.size, names.size)) {
124+
variants += ArgVar(
125+
idx,
126+
types.getOrNull(i) ?: p.type.simpleName,
127+
names.getOrNull(i) ?: p.simpleName,
128+
)
129+
}
130+
}
131+
}
132+
133+
val variantsMap = variants.groupBy { it.idx }
134+
val variantsCount = variantsMap.values.first().size
135+
136+
for (i in 0 until variantsCount) {
137+
val m = method.clone()
138+
m.setParent<Nothing>(method.parent)
139+
variantsMap.values.forEach { vList ->
140+
val v = vList[i]
141+
val p = m.parameters[v.idx]
142+
p.setAnnotations<Nothing>(p.annotations.filterNot { it.name == A_NAME_ARG_VARIANT })
143+
p.setType<Nothing>(m.factory.createTypeParameterReference(v.type))
144+
p.setSimpleName<Nothing>(v.name)
145+
}
146+
content += jvmMethodContent(m) + jniMethodContent(m)
147+
}
148+
} else {
149+
content += jvmMethodContent(method) + jniMethodContent(method)
150+
}
151+
105152
contentToReplace[method.findReplacePos()] = content.joinWithIndention()
106153
return contentToReplace
107154
}

buildSrc/src/main/kotlin/tool/generator/api/jni_content.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private fun convertParams2jni(f: Factory, params: List<CtParameter<*>>, defaults
110110
result += f.createParameter<Nothing>().apply {
111111
val type = if (p.type.isPtrClass()) {
112112
f.createTypeParam("long")
113-
} else if (p.type.isClass) {
113+
} else {
114114
when (p.type.simpleName) {
115115
"ImBoolean" -> f.createTypeParam("boolean[]")
116116
"ImShort" -> f.createTypeParam("short[]")
@@ -120,8 +120,6 @@ private fun convertParams2jni(f: Factory, params: List<CtParameter<*>>, defaults
120120
"ImDouble" -> f.createTypeParam("double[]")
121121
else -> p.type
122122
}
123-
} else {
124-
p.type
125123
}
126124

127125
setType<Nothing>(type)
@@ -138,7 +136,7 @@ private fun joinInBodyParams(params: List<CtParameter<*>>, defaults: IntArray):
138136
"reinterpret_cast<${p.type.simpleName}*>(${p.simpleName})"
139137
} else if (p.isPrimitivePtrType()) {
140138
"&${p.simpleName}[0]"
141-
} else if (p.type.isClass) {
139+
} else {
142140
when (p.type.simpleName) {
143141
"ImBoolean", "ImShort", "ImInt", "ImFloat", "ImLong", "ImDouble" -> {
144142
"(${p.simpleName} != NULL ? &${p.simpleName}[0] : NULL)"
@@ -166,8 +164,6 @@ private fun joinInBodyParams(params: List<CtParameter<*>>, defaults: IntArray):
166164

167165
else -> p.simpleName
168166
}
169-
} else {
170-
p.simpleName
171167
}
172168
}
173169

buildSrc/src/main/kotlin/tool/generator/api/jvm_content.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private fun joinInBodyParams(params: List<CtParameter<*>>, defaults: IntArray):
2020
fun param2str(p: CtParameter<*>): String {
2121
return if (p.type.isPtrClass()) {
2222
"${p.simpleName}.$PTR_JVM_FIELD"
23-
} else if (p.type.isClass) {
23+
} else {
2424
when (p.type.simpleName) {
2525
"ImBoolean", "ImShort", "ImInt", "ImFloat", "ImLong", "ImDouble" -> {
2626
"${p.simpleName} != null ? ${p.simpleName}.getData() : null"
@@ -42,8 +42,6 @@ private fun joinInBodyParams(params: List<CtParameter<*>>, defaults: IntArray):
4242

4343
else -> p.simpleName
4444
}
45-
} else {
46-
p.simpleName
4745
}
4846
}
4947

@@ -147,7 +145,7 @@ private fun createMethod(origM: CtMethod<*>, params: List<CtParameter<*>>, defau
147145
append(createBodyStaticStructReturn(origM, params, defaults))
148146
} else if (DST_RETURN_TYPE_SET.contains(origM.type.simpleName)) {
149147
append(createBodyDstReturn(origM, params, defaults))
150-
} else if (origM.type.isClass && !origM.type.isPrimitive && !origM.isType("void") && !origM.isType("String") && !origM.isPrimitivePtrType()) {
148+
} else if (origM.type.isPtrClass()) {
151149
append(createBodyStructReturn(origM, params, defaults))
152150
} else {
153151
if (!origM.isType("void")) {

buildSrc/src/main/kotlin/tool/generator/api/util.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const val A_NAME_OPT_ARG = "OptArg"
1919
const val A_NAME_EXCLUDED_SOURCE = "ExcludedSource"
2020
const val A_NAME_RETURN_VALUE = "ReturnValue"
2121
const val A_NAME_ARG_VALUE = "ArgValue"
22+
const val A_NAME_ARG_VARIANT = "ArgVariant"
2223
const val A_NAME_TYPE_ARRAY = "TypeArray"
2324
const val A_NAME_TYPE_STD_STRING = "TypeStdString"
2425

@@ -56,6 +57,7 @@ val CLEANUP_ANNOTATIONS_LIST = listOf(
5657
A_NAME_RETURN_VALUE,
5758
A_NAME_OPT_ARG,
5859
A_NAME_ARG_VALUE,
60+
A_NAME_ARG_VARIANT,
5961
A_NAME_TYPE_ARRAY,
6062
A_NAME_TYPE_STD_STRING,
6163
A_NAME_BINDING_AST_ENUM,

0 commit comments

Comments
 (0)