IDL2K avoid unnecessary super() calls in secondary constructors
This commit is contained in:
@@ -567,10 +567,10 @@ native public open class ArrayBuffer(length: Int) : Transferable {
|
||||
}
|
||||
|
||||
native public open class Int8Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Int8Array) : super()
|
||||
constructor(array: Array<Byte>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Int8Array)
|
||||
constructor(array: Array<Byte>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -595,10 +595,10 @@ native public open class Int8Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Uint8Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Uint8Array) : super()
|
||||
constructor(array: Array<Byte>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Uint8Array)
|
||||
constructor(array: Array<Byte>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -623,10 +623,10 @@ native public open class Uint8Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Uint8ClampedArray : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Uint8ClampedArray) : super()
|
||||
constructor(array: Array<Byte>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Uint8ClampedArray)
|
||||
constructor(array: Array<Byte>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -651,10 +651,10 @@ native public open class Uint8ClampedArray : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Int16Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Int16Array) : super()
|
||||
constructor(array: Array<Short>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Int16Array)
|
||||
constructor(array: Array<Short>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -679,10 +679,10 @@ native public open class Int16Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Uint16Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Uint16Array) : super()
|
||||
constructor(array: Array<Short>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Uint16Array)
|
||||
constructor(array: Array<Short>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -707,10 +707,10 @@ native public open class Uint16Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Int32Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Int32Array) : super()
|
||||
constructor(array: Array<Int>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Int32Array)
|
||||
constructor(array: Array<Int>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -735,10 +735,10 @@ native public open class Int32Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Uint32Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Uint32Array) : super()
|
||||
constructor(array: Array<Int>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Uint32Array)
|
||||
constructor(array: Array<Int>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -763,10 +763,10 @@ native public open class Uint32Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Float32Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Float32Array) : super()
|
||||
constructor(array: Array<Float>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Float32Array)
|
||||
constructor(array: Array<Float>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -791,10 +791,10 @@ native public open class Float32Array : ArrayBufferView {
|
||||
}
|
||||
|
||||
native public open class Float64Array : ArrayBufferView {
|
||||
constructor(length: Int) : super()
|
||||
constructor(array: Float64Array) : super()
|
||||
constructor(array: Array<Double>) : super()
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
||||
constructor(length: Int)
|
||||
constructor(array: Float64Array)
|
||||
constructor(array: Array<Double>)
|
||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
|
||||
var length: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
|
||||
@@ -2996,8 +2996,8 @@ native public open class HitRegionOptions {
|
||||
}
|
||||
|
||||
native public open class ImageData : ImageBitmapSource {
|
||||
constructor(sw: Int, sh: Int) : super()
|
||||
constructor(data: Uint8ClampedArray, sw: Int, sh: Int = noImpl) : super()
|
||||
constructor(sw: Int, sh: Int)
|
||||
constructor(data: Uint8ClampedArray, sw: Int, sh: Int = noImpl)
|
||||
var width: Int
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
@@ -5086,8 +5086,8 @@ native public open class DOMRectInit {
|
||||
}
|
||||
|
||||
native public open class DOMQuad {
|
||||
constructor(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl) : super()
|
||||
constructor(rect: DOMRectInit = noImpl) : super()
|
||||
constructor(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl)
|
||||
constructor(rect: DOMRectInit = noImpl)
|
||||
var p1: DOMPoint
|
||||
get() = noImpl
|
||||
set(value) = noImpl
|
||||
|
||||
@@ -83,7 +83,7 @@ private fun resolveDefinitionKind(repository: Repository, iface: InterfaceDefini
|
||||
private fun InterfaceDefinition.mapAttributes(repository: Repository) = attributes.map { generateAttribute(!dictionary, repository, it) }
|
||||
private fun InterfaceDefinition.mapOperations(repository: Repository) = operations.flatMap { generateFunctions(repository, it) }
|
||||
private fun Constant.mapConstant(repository : Repository) = GenerateAttribute(name, mapType(repository, type), value, false, true, false, false, true)
|
||||
private fun emptyConstructor() = ExtendedAttribute(null, "Constructor", emptyList())
|
||||
private val EMPTY_CONSTRUCTOR = ExtendedAttribute(null, "Constructor", emptyList())
|
||||
|
||||
fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
|
||||
val superClasses = iface.superTypes
|
||||
@@ -93,7 +93,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
|
||||
|
||||
assert(superClasses.size() <= 1) { "Type ${iface.name} should have one or zero super classes but found ${superClasses.map { it.name }}" }
|
||||
val superClass = superClasses.singleOrNull()
|
||||
val superConstructor = superClass?.findConstructors()?.firstOrNull()
|
||||
val superConstructor = superClass?.findConstructors()?.firstOrNull() ?: EMPTY_CONSTRUCTOR
|
||||
|
||||
val declaredConstructors = iface.findConstructors()
|
||||
val entityKind = resolveDefinitionKind(repository, iface, declaredConstructors)
|
||||
@@ -101,26 +101,27 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
|
||||
|
||||
val primaryConstructor = when {
|
||||
declaredConstructors.size() == 1 -> declaredConstructors.single()
|
||||
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> emptyConstructor()
|
||||
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> EMPTY_CONSTRUCTOR
|
||||
else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() }
|
||||
}
|
||||
val secondaryConstructors = declaredConstructors.filter { it != primaryConstructor }
|
||||
|
||||
val primaryConstructorWithCall = if (primaryConstructor != null) {
|
||||
val constructorAsFunction = generateConstructorAsFunction(repository, primaryConstructor)
|
||||
val primaryConstructorWithCall = primaryConstructor?.let { constructor ->
|
||||
val constructorAsFunction = generateConstructorAsFunction(repository, constructor)
|
||||
val superCall = when {
|
||||
superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, superClass.name, superConstructor ?: emptyConstructor())
|
||||
superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, superClass.name, superConstructor)
|
||||
else -> null
|
||||
}
|
||||
|
||||
ConstructorWithSuperTypeCall(constructorAsFunction, primaryConstructor, superCall)
|
||||
} else null
|
||||
ConstructorWithSuperTypeCall(constructorAsFunction, constructor, superCall)
|
||||
}
|
||||
|
||||
val secondaryConstructorsWithCall = secondaryConstructors.map { secondaryConstructor ->
|
||||
val constructorAsFunction = generateConstructorAsFunction(repository, secondaryConstructor)
|
||||
val initCall = when {
|
||||
primaryConstructorWithCall != null -> superOrPrimaryConstructorCall(constructorAsFunction, "this", primaryConstructorWithCall.constructorAttribute)
|
||||
else -> superOrPrimaryConstructorCall(constructorAsFunction, "super", superConstructor ?: emptyConstructor())
|
||||
superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, "super", superConstructor)
|
||||
else -> null
|
||||
}
|
||||
|
||||
ConstructorWithSuperTypeCall(constructorAsFunction, secondaryConstructor, initCall)
|
||||
|
||||
@@ -60,7 +60,7 @@ private fun Appendable.renderArgumentsDeclaration(args: List<GenerateAttribute>,
|
||||
append(it.initializer.replaceWrongConstants(it.type))
|
||||
}
|
||||
}
|
||||
}.joinTo(this, ", ")
|
||||
}.joinTo(this, ", ", "(", ")")
|
||||
|
||||
private fun renderCall(call: GenerateFunctionCall) = "${call.name.replaceKeywords()}(${call.arguments.map { it.replaceKeywords() }.join(", ")})"
|
||||
|
||||
@@ -80,9 +80,9 @@ private fun Appendable.renderFunctionDeclaration(f: GenerateFunction, override:
|
||||
if (f.name in keywords) {
|
||||
append("native(\"${f.name}\") ")
|
||||
}
|
||||
append("fun ${f.name.replaceKeywords()}(")
|
||||
append("fun ${f.name.replaceKeywords()}")
|
||||
renderArgumentsDeclaration(f.arguments, override)
|
||||
appendln("): ${f.returnType.render()} = noImpl")
|
||||
appendln(": ${f.returnType.render()} = noImpl")
|
||||
}
|
||||
|
||||
fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUnions: Map<String, List<String>>, iface: GenerateTraitOrClass, markerAnnotation: Boolean = false) {
|
||||
@@ -100,17 +100,13 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
|
||||
|
||||
append(iface.name)
|
||||
val primary = iface.primaryConstructor
|
||||
if (primary != null) {
|
||||
if (primary.constructor.arguments.isNotEmpty() || iface.secondaryConstructors.isNotEmpty()) {
|
||||
append("(")
|
||||
renderArgumentsDeclaration(primary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
|
||||
append(")")
|
||||
}
|
||||
if (primary != null && (primary.constructor.arguments.isNotEmpty() || iface.secondaryConstructors.isNotEmpty())) {
|
||||
renderArgumentsDeclaration(primary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
|
||||
}
|
||||
|
||||
val superCallName = primary?.initTypeCall?.name
|
||||
val superTypesWithCalls =
|
||||
listOf(primary?.initTypeCall).filterNotNull().map { renderCall(it) } +
|
||||
(primary?.initTypeCall?.let { listOf(renderCall(it)) } ?: emptyList()) +
|
||||
iface.superTypes.filter { it != superCallName && it in allSuperTypesNames } +
|
||||
(typeNamesToUnions[iface.name] ?: emptyList())
|
||||
|
||||
@@ -122,9 +118,8 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
|
||||
|
||||
iface.secondaryConstructors.forEach { secondary ->
|
||||
indent(1)
|
||||
append("constructor(")
|
||||
append("constructor")
|
||||
renderArgumentsDeclaration(secondary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
|
||||
append(")")
|
||||
|
||||
if (secondary.initTypeCall != null) {
|
||||
append(" : ")
|
||||
|
||||
Reference in New Issue
Block a user