IDL2K avoid unnecessary super() calls in secondary constructors

This commit is contained in:
Sergey Mashkov
2015-06-16 17:29:36 +03:00
parent 28f228ccc0
commit c0c0f6152a
4 changed files with 57 additions and 61 deletions
@@ -567,10 +567,10 @@ native public open class ArrayBuffer(length: Int) : Transferable {
} }
native public open class Int8Array : ArrayBufferView { native public open class Int8Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Int8Array) : super() constructor(array: Int8Array)
constructor(array: Array<Byte>) : super() constructor(array: Array<Byte>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -595,10 +595,10 @@ native public open class Int8Array : ArrayBufferView {
} }
native public open class Uint8Array : ArrayBufferView { native public open class Uint8Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Uint8Array) : super() constructor(array: Uint8Array)
constructor(array: Array<Byte>) : super() constructor(array: Array<Byte>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -623,10 +623,10 @@ native public open class Uint8Array : ArrayBufferView {
} }
native public open class Uint8ClampedArray : ArrayBufferView { native public open class Uint8ClampedArray : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Uint8ClampedArray) : super() constructor(array: Uint8ClampedArray)
constructor(array: Array<Byte>) : super() constructor(array: Array<Byte>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -651,10 +651,10 @@ native public open class Uint8ClampedArray : ArrayBufferView {
} }
native public open class Int16Array : ArrayBufferView { native public open class Int16Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Int16Array) : super() constructor(array: Int16Array)
constructor(array: Array<Short>) : super() constructor(array: Array<Short>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -679,10 +679,10 @@ native public open class Int16Array : ArrayBufferView {
} }
native public open class Uint16Array : ArrayBufferView { native public open class Uint16Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Uint16Array) : super() constructor(array: Uint16Array)
constructor(array: Array<Short>) : super() constructor(array: Array<Short>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -707,10 +707,10 @@ native public open class Uint16Array : ArrayBufferView {
} }
native public open class Int32Array : ArrayBufferView { native public open class Int32Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Int32Array) : super() constructor(array: Int32Array)
constructor(array: Array<Int>) : super() constructor(array: Array<Int>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -735,10 +735,10 @@ native public open class Int32Array : ArrayBufferView {
} }
native public open class Uint32Array : ArrayBufferView { native public open class Uint32Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Uint32Array) : super() constructor(array: Uint32Array)
constructor(array: Array<Int>) : super() constructor(array: Array<Int>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -763,10 +763,10 @@ native public open class Uint32Array : ArrayBufferView {
} }
native public open class Float32Array : ArrayBufferView { native public open class Float32Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Float32Array) : super() constructor(array: Float32Array)
constructor(array: Array<Float>) : super() constructor(array: Array<Float>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -791,10 +791,10 @@ native public open class Float32Array : ArrayBufferView {
} }
native public open class Float64Array : ArrayBufferView { native public open class Float64Array : ArrayBufferView {
constructor(length: Int) : super() constructor(length: Int)
constructor(array: Float64Array) : super() constructor(array: Float64Array)
constructor(array: Array<Double>) : super() constructor(array: Array<Double>)
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super() constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl)
var length: Int var length: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
+4 -4
View File
@@ -2996,8 +2996,8 @@ native public open class HitRegionOptions {
} }
native public open class ImageData : ImageBitmapSource { native public open class ImageData : ImageBitmapSource {
constructor(sw: Int, sh: Int) : super() constructor(sw: Int, sh: Int)
constructor(data: Uint8ClampedArray, sw: Int, sh: Int = noImpl) : super() constructor(data: Uint8ClampedArray, sw: Int, sh: Int = noImpl)
var width: Int var width: Int
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
@@ -5086,8 +5086,8 @@ native public open class DOMRectInit {
} }
native public open class DOMQuad { native public open class DOMQuad {
constructor(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl) : super() constructor(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl)
constructor(rect: DOMRectInit = noImpl) : super() constructor(rect: DOMRectInit = noImpl)
var p1: DOMPoint var p1: DOMPoint
get() = noImpl get() = noImpl
set(value) = noImpl set(value) = noImpl
+10 -9
View File
@@ -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.mapAttributes(repository: Repository) = attributes.map { generateAttribute(!dictionary, repository, it) }
private fun InterfaceDefinition.mapOperations(repository: Repository) = operations.flatMap { generateFunctions(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 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 { fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
val superClasses = iface.superTypes 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 }}" } 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 superClass = superClasses.singleOrNull()
val superConstructor = superClass?.findConstructors()?.firstOrNull() val superConstructor = superClass?.findConstructors()?.firstOrNull() ?: EMPTY_CONSTRUCTOR
val declaredConstructors = iface.findConstructors() val declaredConstructors = iface.findConstructors()
val entityKind = resolveDefinitionKind(repository, iface, declaredConstructors) val entityKind = resolveDefinitionKind(repository, iface, declaredConstructors)
@@ -101,26 +101,27 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
val primaryConstructor = when { val primaryConstructor = when {
declaredConstructors.size() == 1 -> declaredConstructors.single() declaredConstructors.size() == 1 -> declaredConstructors.single()
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> emptyConstructor() declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> EMPTY_CONSTRUCTOR
else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() } else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() }
} }
val secondaryConstructors = declaredConstructors.filter { it != primaryConstructor } val secondaryConstructors = declaredConstructors.filter { it != primaryConstructor }
val primaryConstructorWithCall = if (primaryConstructor != null) { val primaryConstructorWithCall = primaryConstructor?.let { constructor ->
val constructorAsFunction = generateConstructorAsFunction(repository, primaryConstructor) val constructorAsFunction = generateConstructorAsFunction(repository, constructor)
val superCall = when { val superCall = when {
superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, superClass.name, superConstructor ?: emptyConstructor()) superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, superClass.name, superConstructor)
else -> null else -> null
} }
ConstructorWithSuperTypeCall(constructorAsFunction, primaryConstructor, superCall) ConstructorWithSuperTypeCall(constructorAsFunction, constructor, superCall)
} else null }
val secondaryConstructorsWithCall = secondaryConstructors.map { secondaryConstructor -> val secondaryConstructorsWithCall = secondaryConstructors.map { secondaryConstructor ->
val constructorAsFunction = generateConstructorAsFunction(repository, secondaryConstructor) val constructorAsFunction = generateConstructorAsFunction(repository, secondaryConstructor)
val initCall = when { val initCall = when {
primaryConstructorWithCall != null -> superOrPrimaryConstructorCall(constructorAsFunction, "this", primaryConstructorWithCall.constructorAttribute) 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) ConstructorWithSuperTypeCall(constructorAsFunction, secondaryConstructor, initCall)
@@ -60,7 +60,7 @@ private fun Appendable.renderArgumentsDeclaration(args: List<GenerateAttribute>,
append(it.initializer.replaceWrongConstants(it.type)) append(it.initializer.replaceWrongConstants(it.type))
} }
} }
}.joinTo(this, ", ") }.joinTo(this, ", ", "(", ")")
private fun renderCall(call: GenerateFunctionCall) = "${call.name.replaceKeywords()}(${call.arguments.map { it.replaceKeywords() }.join(", ")})" 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) { if (f.name in keywords) {
append("native(\"${f.name}\") ") append("native(\"${f.name}\") ")
} }
append("fun ${f.name.replaceKeywords()}(") append("fun ${f.name.replaceKeywords()}")
renderArgumentsDeclaration(f.arguments, override) 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) { 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) append(iface.name)
val primary = iface.primaryConstructor val primary = iface.primaryConstructor
if (primary != null) { if (primary != null && (primary.constructor.arguments.isNotEmpty() || iface.secondaryConstructors.isNotEmpty())) {
if (primary.constructor.arguments.isNotEmpty() || iface.secondaryConstructors.isNotEmpty()) { renderArgumentsDeclaration(primary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
append("(")
renderArgumentsDeclaration(primary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
append(")")
}
} }
val superCallName = primary?.initTypeCall?.name val superCallName = primary?.initTypeCall?.name
val superTypesWithCalls = val superTypesWithCalls =
listOf(primary?.initTypeCall).filterNotNull().map { renderCall(it) } + (primary?.initTypeCall?.let { listOf(renderCall(it)) } ?: emptyList()) +
iface.superTypes.filter { it != superCallName && it in allSuperTypesNames } + iface.superTypes.filter { it != superCallName && it in allSuperTypesNames } +
(typeNamesToUnions[iface.name] ?: emptyList()) (typeNamesToUnions[iface.name] ?: emptyList())
@@ -122,9 +118,8 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
iface.secondaryConstructors.forEach { secondary -> iface.secondaryConstructors.forEach { secondary ->
indent(1) indent(1)
append("constructor(") append("constructor")
renderArgumentsDeclaration(secondary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false) renderArgumentsDeclaration(secondary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
append(")")
if (secondary.initTypeCall != null) { if (secondary.initTypeCall != null) {
append(" : ") append(" : ")