Revert IDL2K Multiple constructors (via secondary constructors when needed)
This commit is contained in:
@@ -562,11 +562,7 @@ native public open class ArrayBuffer(length: Int) : Transferable {
|
|||||||
fun slice(begin: Int, end: Int = noImpl): ArrayBuffer = noImpl
|
fun slice(begin: Int, end: Int = noImpl): ArrayBuffer = noImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Int8Array : ArrayBufferView {
|
native public open class Int8Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Int8Array) : super()
|
|
||||||
constructor(array: Array<Byte>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -590,11 +586,7 @@ native public open class Int8Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Uint8Array : ArrayBufferView {
|
native public open class Uint8Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Uint8Array) : super()
|
|
||||||
constructor(array: Array<Byte>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -618,11 +610,7 @@ native public open class Uint8Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Uint8ClampedArray : ArrayBufferView {
|
native public open class Uint8ClampedArray(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Uint8ClampedArray) : super()
|
|
||||||
constructor(array: Array<Byte>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -646,11 +634,7 @@ native public open class Uint8ClampedArray : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Int16Array : ArrayBufferView {
|
native public open class Int16Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Int16Array) : super()
|
|
||||||
constructor(array: Array<Short>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -674,11 +658,7 @@ native public open class Int16Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Uint16Array : ArrayBufferView {
|
native public open class Uint16Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Uint16Array) : super()
|
|
||||||
constructor(array: Array<Short>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -702,11 +682,7 @@ native public open class Uint16Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Int32Array : ArrayBufferView {
|
native public open class Int32Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Int32Array) : super()
|
|
||||||
constructor(array: Array<Int>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -730,11 +706,7 @@ native public open class Int32Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Uint32Array : ArrayBufferView {
|
native public open class Uint32Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Uint32Array) : super()
|
|
||||||
constructor(array: Array<Int>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -758,11 +730,7 @@ native public open class Uint32Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Float32Array : ArrayBufferView {
|
native public open class Float32Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Float32Array) : super()
|
|
||||||
constructor(array: Array<Float>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -786,11 +754,7 @@ native public open class Float32Array : ArrayBufferView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Float64Array : ArrayBufferView {
|
native public open class Float64Array(length: Int) : ArrayBufferView {
|
||||||
constructor(length: Int) : super()
|
|
||||||
constructor(array: Float64Array) : super()
|
|
||||||
constructor(array: Array<Double>) : super()
|
|
||||||
constructor(buffer: ArrayBuffer, byteOffset: Int = noImpl, length: Int = noImpl) : super()
|
|
||||||
var length: Int
|
var length: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
|
|||||||
@@ -2810,8 +2810,7 @@ native public open class CanvasRenderingContext2DSettings {
|
|||||||
var alpha: Boolean = true
|
var alpha: Boolean = true
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class CanvasRenderingContext2D() : RenderingContext, CanvasImageSource, ImageBitmapSource {
|
native public open class CanvasRenderingContext2D : RenderingContext, CanvasImageSource, ImageBitmapSource {
|
||||||
constructor(width: Int, height: Int) : this()
|
|
||||||
var canvas: HTMLCanvasElement
|
var canvas: HTMLCanvasElement
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -2995,9 +2994,7 @@ native public open class HitRegionOptions {
|
|||||||
var role: String? = null
|
var role: String? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class ImageData : ImageBitmapSource {
|
native public open class ImageData(sw: Int, sh: Int) : ImageBitmapSource {
|
||||||
constructor(sw: Int, sh: Int) : super()
|
|
||||||
constructor(data: Uint8ClampedArray, sw: Int, sh: Int = noImpl) : super()
|
|
||||||
var width: Int
|
var width: Int
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -3041,10 +3038,7 @@ native public open class DrawingStyle(scope: Element = noImpl) {
|
|||||||
fun getLineDash(): Array<Double> = noImpl
|
fun getLineDash(): Array<Double> = noImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class Path2D() {
|
native public open class Path2D {
|
||||||
constructor(path: Path2D) : this()
|
|
||||||
constructor(paths: Array<Path2D>, fillRule: String = "nonzero") : this()
|
|
||||||
constructor(d: String) : this()
|
|
||||||
fun addPath(path: Path2D, transformation: SVGMatrix? = null): Unit = noImpl
|
fun addPath(path: Path2D, transformation: SVGMatrix? = null): Unit = noImpl
|
||||||
fun addPathByStrokingPath(path: Path2D, styles: dynamic, transformation: SVGMatrix? = null): Unit = noImpl
|
fun addPathByStrokingPath(path: Path2D, styles: dynamic, transformation: SVGMatrix? = null): Unit = noImpl
|
||||||
fun addText(text: String, styles: dynamic, transformation: SVGMatrix?, x: Double, y: Double, maxWidth: Double = noImpl): Unit = noImpl
|
fun addText(text: String, styles: dynamic, transformation: SVGMatrix?, x: Double, y: Double, maxWidth: Double = noImpl): Unit = noImpl
|
||||||
@@ -5036,9 +5030,7 @@ native public open class DOMPointReadOnly(x: Double, y: Double, z: Double, w: Do
|
|||||||
fun matrixTransform(matrix: DOMMatrixReadOnly): DOMPoint = noImpl
|
fun matrixTransform(matrix: DOMMatrixReadOnly): DOMPoint = noImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class DOMPoint : DOMPointReadOnly {
|
native public open class DOMPoint(point: DOMPointInit = noImpl) : DOMPointReadOnly(noImpl, noImpl, noImpl, noImpl) {
|
||||||
constructor(point: DOMPointInit = noImpl) : super(noImpl, noImpl, noImpl, noImpl)
|
|
||||||
constructor(x: Double = 0.0, y: Double = 0.0, z: Double = 0.0, w: Double = 1.0) : super(x, y, z, w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class DOMPointInit {
|
native public open class DOMPointInit {
|
||||||
@@ -5085,9 +5077,7 @@ native public open class DOMRectInit {
|
|||||||
var height: Double = 0.0
|
var height: Double = 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class DOMQuad {
|
native public open class DOMQuad(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl) {
|
||||||
constructor(p1: DOMPointInit = noImpl, p2: DOMPointInit = noImpl, p3: DOMPointInit = noImpl, p4: DOMPointInit = noImpl) : super()
|
|
||||||
constructor(rect: DOMRectInit = noImpl) : super()
|
|
||||||
var p1: DOMPoint
|
var p1: DOMPoint
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
@@ -5196,12 +5186,7 @@ native public open class DOMMatrixReadOnly(numberSequence: Array<Double>) {
|
|||||||
fun toFloat64Array(): Float64Array = noImpl
|
fun toFloat64Array(): Float64Array = noImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
native public open class DOMMatrix() : DOMMatrixReadOnly(noImpl) {
|
native public open class DOMMatrix : DOMMatrixReadOnly(noImpl) {
|
||||||
constructor(transformList: String) : this()
|
|
||||||
constructor(other: DOMMatrixReadOnly) : this()
|
|
||||||
constructor(array32: Float32Array) : this()
|
|
||||||
constructor(array64: Float64Array) : this()
|
|
||||||
constructor(numberSequence: Array<Double>) : this()
|
|
||||||
fun multiplySelf(other: DOMMatrix): DOMMatrix = noImpl
|
fun multiplySelf(other: DOMMatrix): DOMMatrix = noImpl
|
||||||
fun preMultiplySelf(other: DOMMatrix): DOMMatrix = noImpl
|
fun preMultiplySelf(other: DOMMatrix): DOMMatrix = noImpl
|
||||||
fun translateSelf(tx: Double, ty: Double, tz: Double = 0.0): DOMMatrix = noImpl
|
fun translateSelf(tx: Double, ty: Double, tz: Double = 0.0): DOMMatrix = noImpl
|
||||||
|
|||||||
@@ -20,8 +20,7 @@ import org.w3c.performance.*
|
|||||||
import org.w3c.workers.*
|
import org.w3c.workers.*
|
||||||
import org.w3c.xhr.*
|
import org.w3c.xhr.*
|
||||||
|
|
||||||
native public open class Blob() : ImageBitmapSource {
|
native public open class Blob : ImageBitmapSource {
|
||||||
constructor(blobParts: Array<dynamic>, options: BlobPropertyBag = noImpl) : this()
|
|
||||||
var size: Long
|
var size: Long
|
||||||
get() = noImpl
|
get() = noImpl
|
||||||
set(value) = noImpl
|
set(value) = noImpl
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ fun String.dropNullable() = when {
|
|||||||
else -> this
|
else -> this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun String.copyNullabilityFrom(type: String) = when {
|
fun String.copyNullabilityFrom(type : String) = when {
|
||||||
type.isNullable() -> ensureNullable()
|
type.isNullable() -> ensureNullable()
|
||||||
else -> this
|
else -> this
|
||||||
}
|
}
|
||||||
@@ -94,85 +94,54 @@ fun generateAttribute(putNoImpl: Boolean, repository: Repository, attribute: Att
|
|||||||
)
|
)
|
||||||
|
|
||||||
private fun InterfaceDefinition.superTypes(repository: Repository) = superTypes.map { repository.interfaces[it] }.filterNotNull()
|
private fun InterfaceDefinition.superTypes(repository: Repository) = superTypes.map { repository.interfaces[it] }.filterNotNull()
|
||||||
private fun resolveDefinitionKind(repository: Repository, iface: InterfaceDefinition, constructors: List<ExtendedAttribute> = iface.findConstructors()): GenerateDefinitionKind =
|
private fun resolveDefinitionKind(repository: Repository, iface: InterfaceDefinition, constructor: ExtendedAttribute? = iface.findConstructor()): GenerateDefinitionKind =
|
||||||
if (iface.dictionary || constructors.isNotEmpty() || iface.superTypes(repository).any { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }) {
|
if (iface.dictionary || constructor != null || iface.superTypes(repository).any { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }) {
|
||||||
GenerateDefinitionKind.CLASS
|
GenerateDefinitionKind.CLASS
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
GenerateDefinitionKind.TRAIT
|
GenerateDefinitionKind.TRAIT
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
private fun Constant.mapConstant(repository : Repository) = GenerateAttribute(name, mapType(repository, type), value, false, true, false, false)
|
||||||
private fun emptyConstructor() = ExtendedAttribute(null, "Constructor", emptyList())
|
|
||||||
|
|
||||||
fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
|
fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
|
||||||
val superClasses = iface.superTypes
|
val constructor = iface.findConstructor()
|
||||||
|
val constructorFunction = generateFunction(repository, Operation("", "Unit", constructor?.arguments ?: emptyList(), emptyList()), functionName = "", nativeGetterOrSetter = NativeGetterOrSetter.NONE)
|
||||||
|
val constructorArgumentNames = constructorFunction.arguments.map { it.name }.toSet()
|
||||||
|
|
||||||
|
val constructorSuperCalls = iface.superTypes
|
||||||
.map { repository.interfaces[it] }
|
.map { repository.interfaces[it] }
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.filter { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }
|
.filter { resolveDefinitionKind(repository, it) == GenerateDefinitionKind.CLASS }
|
||||||
|
.map {
|
||||||
|
val superConstructor = it.findConstructor()
|
||||||
|
GenerateFunctionCall(
|
||||||
|
name = it.name,
|
||||||
|
arguments = if (superConstructor == null) {
|
||||||
|
emptyList()
|
||||||
|
} else {
|
||||||
|
superConstructor.arguments.map { arg ->
|
||||||
|
if (arg.name in constructorArgumentNames) arg.name else "noImpl"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
assert(superClasses.size() <= 1) { "Type ${iface.name} should have one or zero super classes but found ${superClasses.map { it.name }}" }
|
val entityKind = resolveDefinitionKind(repository, iface, constructor)
|
||||||
val superClass = superClasses.singleOrNull()
|
|
||||||
val superConstructor = superClass?.findConstructors()?.firstOrNull()
|
|
||||||
|
|
||||||
val declaredConstructors = iface.findConstructors()
|
|
||||||
val entityKind = resolveDefinitionKind(repository, iface, declaredConstructors)
|
|
||||||
val extensions = repository.externals[iface.name]?.map { repository.interfaces[it] }?.filterNotNull() ?: emptyList()
|
val extensions = repository.externals[iface.name]?.map { repository.interfaces[it] }?.filterNotNull() ?: emptyList()
|
||||||
|
|
||||||
val primaryConstructor = when {
|
|
||||||
declaredConstructors.size() == 1 -> declaredConstructors.single()
|
|
||||||
declaredConstructors.isEmpty() && entityKind == GenerateDefinitionKind.CLASS -> emptyConstructor()
|
|
||||||
else -> declaredConstructors.firstOrNull { it.arguments.isEmpty() }
|
|
||||||
}
|
|
||||||
val secondaryConstructors = declaredConstructors.filter { it != primaryConstructor }
|
|
||||||
|
|
||||||
val primaryConstructorWithCall = if (primaryConstructor != null) {
|
|
||||||
val constructorAsFunction = generateConstructorAsFunction(repository, primaryConstructor)
|
|
||||||
val superCall = when {
|
|
||||||
superClass != null -> superOrPrimaryConstructorCall(constructorAsFunction, superClass.name, superConstructor ?: emptyConstructor())
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
ConstructorWithSuperTypeCall(constructorAsFunction, primaryConstructor, superCall)
|
|
||||||
} else null
|
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
ConstructorWithSuperTypeCall(constructorAsFunction, secondaryConstructor, initCall)
|
|
||||||
}
|
|
||||||
|
|
||||||
return GenerateTraitOrClass(iface.name, iface.namespace, entityKind, iface.superTypes,
|
return GenerateTraitOrClass(iface.name, iface.namespace, entityKind, iface.superTypes,
|
||||||
memberAttributes = (iface.mapAttributes(repository) + extensions.flatMap { it.mapAttributes(repository) }).distinct().toList(),
|
memberAttributes = (iface.mapAttributes(repository) + extensions.flatMap { it.mapAttributes(repository) }).distinct().toList(),
|
||||||
memberFunctions = (iface.mapOperations(repository) + extensions.flatMap { it.mapOperations(repository) }).distinct().toList(),
|
memberFunctions = (iface.mapOperations(repository) + extensions.flatMap { it.mapOperations(repository) }).distinct().toList(),
|
||||||
constants = (iface.constants.map { it.mapConstant(repository) } + extensions.flatMap { it.constants.map { it.mapConstant(repository) } }.distinct().toList()),
|
constants = (iface.constants.map {it.mapConstant(repository)} + extensions.flatMap { it.constants.map {it.mapConstant(repository)} }.distinct().toList()),
|
||||||
primaryConstructor = primaryConstructorWithCall,
|
constructor = constructorFunction,
|
||||||
secondaryConstructors = secondaryConstructorsWithCall
|
superConstructorCalls = constructorSuperCalls
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateConstructorAsFunction(repository: Repository, constructor: ExtendedAttribute) = generateFunction(
|
fun mapUnionType(it : UnionType) = GenerateTraitOrClass(
|
||||||
repository,
|
|
||||||
Operation("", "Unit", constructor.arguments, emptyList()),
|
|
||||||
functionName = "",
|
|
||||||
nativeGetterOrSetter = NativeGetterOrSetter.NONE)
|
|
||||||
|
|
||||||
fun superOrPrimaryConstructorCall(constructorAsFunction: GenerateFunction, superClassName: String, superOrPrimaryConstructor: ExtendedAttribute): GenerateFunctionCall {
|
|
||||||
val constructorArgumentNames = constructorAsFunction.arguments.map { it.name }.toSet()
|
|
||||||
return GenerateFunctionCall(
|
|
||||||
name = superClassName,
|
|
||||||
arguments = superOrPrimaryConstructor.arguments.map { arg ->
|
|
||||||
if (arg.name in constructorArgumentNames) arg.name else "noImpl"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun mapUnionType(it: UnionType) = GenerateTraitOrClass(
|
|
||||||
name = it.name,
|
name = it.name,
|
||||||
namespace = it.namespace,
|
namespace = it.namespace,
|
||||||
kind = GenerateDefinitionKind.TRAIT,
|
kind = GenerateDefinitionKind.TRAIT,
|
||||||
@@ -180,16 +149,16 @@ fun mapUnionType(it: UnionType) = GenerateTraitOrClass(
|
|||||||
memberAttributes = emptyList(),
|
memberAttributes = emptyList(),
|
||||||
memberFunctions = emptyList(),
|
memberFunctions = emptyList(),
|
||||||
constants = emptyList(),
|
constants = emptyList(),
|
||||||
primaryConstructor = null,
|
constructor = null,
|
||||||
secondaryConstructors = emptyList()
|
superConstructorCalls = emptyList()
|
||||||
)
|
)
|
||||||
|
|
||||||
fun generateUnionTypeTraits(allUnionTypes: Iterable<UnionType>): List<GenerateTraitOrClass> = allUnionTypes.map(::mapUnionType)
|
fun generateUnionTypeTraits(allUnionTypes : Iterable<UnionType>): List<GenerateTraitOrClass> = allUnionTypes.map(::mapUnionType)
|
||||||
|
|
||||||
fun mapDefinitions(repository: Repository, definitions: Iterable<InterfaceDefinition>) =
|
fun mapDefinitions(repository: Repository, definitions: Iterable<InterfaceDefinition>) =
|
||||||
definitions.filter { "NoInterfaceObject" !in it.extendedAttributes.map { it.call } }.map { generateTrait(repository, it) }
|
definitions.filter { "NoInterfaceObject" !in it.extendedAttributes.map { it.call } }.map { generateTrait(repository, it) }
|
||||||
|
|
||||||
fun generateUnions(ifaces: List<GenerateTraitOrClass>, typedefs: Iterable<TypedefDefinition>): GenerateUnionTypes {
|
fun generateUnions(ifaces: List<GenerateTraitOrClass>, typedefs: Iterable<TypedefDefinition>) : GenerateUnionTypes {
|
||||||
val declaredTypes = ifaces.toMap { it.name }
|
val declaredTypes = ifaces.toMap { it.name }
|
||||||
|
|
||||||
val anonymousUnionTypes = collectUnionTypes(declaredTypes)
|
val anonymousUnionTypes = collectUnionTypes(declaredTypes)
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ val GenerateAttribute.signature: String
|
|||||||
get() = "$name:${type.typeSignature}"
|
get() = "$name:${type.typeSignature}"
|
||||||
|
|
||||||
fun GenerateAttribute.dynamicIfUnknownType(allTypes : Set<String>, standardTypes : Set<String> = standardTypes()) = copy(type = type.dynamicIfUnknownType(allTypes, standardTypes))
|
fun GenerateAttribute.dynamicIfUnknownType(allTypes : Set<String>, standardTypes : Set<String> = standardTypes()) = copy(type = type.dynamicIfUnknownType(allTypes, standardTypes))
|
||||||
fun List<GenerateAttribute>.dynamicIfUnknownType(allTypes : Set<String>, standardTypes : Set<String> = standardTypes()) = map { it.dynamicIfUnknownType(allTypes, standardTypes) }
|
|
||||||
|
|
||||||
enum class NativeGetterOrSetter {
|
enum class NativeGetterOrSetter {
|
||||||
NONE,
|
NONE,
|
||||||
@@ -73,7 +72,6 @@ data class GenerateFunction(
|
|||||||
val nativeGetterOrSetter: NativeGetterOrSetter
|
val nativeGetterOrSetter: NativeGetterOrSetter
|
||||||
)
|
)
|
||||||
|
|
||||||
data class ConstructorWithSuperTypeCall(val constructor: GenerateFunction, val constructorAttribute: ExtendedAttribute, val initTypeCall: GenerateFunctionCall?)
|
|
||||||
data class GenerateTraitOrClass(
|
data class GenerateTraitOrClass(
|
||||||
val name: String,
|
val name: String,
|
||||||
val namespace: String,
|
val namespace: String,
|
||||||
@@ -82,9 +80,14 @@ data class GenerateTraitOrClass(
|
|||||||
val memberAttributes: List<GenerateAttribute>,
|
val memberAttributes: List<GenerateAttribute>,
|
||||||
val memberFunctions: List<GenerateFunction>,
|
val memberFunctions: List<GenerateFunction>,
|
||||||
val constants: List<GenerateAttribute>,
|
val constants: List<GenerateAttribute>,
|
||||||
val primaryConstructor: ConstructorWithSuperTypeCall?,
|
val constructor: GenerateFunction?,
|
||||||
val secondaryConstructors: List<ConstructorWithSuperTypeCall>
|
val superConstructorCalls: List<GenerateFunctionCall>
|
||||||
)
|
) {
|
||||||
|
init {
|
||||||
|
assert(superConstructorCalls.size() <= 1, "It shoould be zero or one super constructors")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
val GenerateFunction.signature: String
|
val GenerateFunction.signature: String
|
||||||
get() = arguments.map { it.type.typeSignature }.joinToString(", ", "$name(", ")")
|
get() = arguments.map { it.type.typeSignature }.joinToString(", ", "$name(", ")")
|
||||||
@@ -93,9 +96,9 @@ fun GenerateFunction.dynamicIfUnknownType(allTypes : Set<String>) = standardType
|
|||||||
copy(returnType = returnType.dynamicIfUnknownType(allTypes, standardTypes), arguments = arguments.map { it.dynamicIfUnknownType(allTypes, standardTypes) })
|
copy(returnType = returnType.dynamicIfUnknownType(allTypes, standardTypes), arguments = arguments.map { it.dynamicIfUnknownType(allTypes, standardTypes) })
|
||||||
}
|
}
|
||||||
|
|
||||||
fun InterfaceDefinition.findExtendedAttributes(name: String) = extendedAttributes.filter { it.call == name }
|
fun InterfaceDefinition.findExtendedAttribute(name: String) = extendedAttributes.firstOrNull { it.call == name }
|
||||||
fun InterfaceDefinition?.hasExtendedAttribute(name: String) = this?.findExtendedAttributes(name)?.isNotEmpty() ?: false
|
fun InterfaceDefinition?.hasExtendedAttribute(name: String) = this?.findExtendedAttribute(name) ?: null != null
|
||||||
fun InterfaceDefinition.findConstructors() = findExtendedAttributes("Constructor")
|
fun InterfaceDefinition.findConstructor() = findExtendedAttribute("Constructor")
|
||||||
|
|
||||||
data class GenerateUnionTypes(
|
data class GenerateUnionTypes(
|
||||||
val typeNamesToUnionsMap: Map<String, List<String>>,
|
val typeNamesToUnionsMap: Map<String, List<String>>,
|
||||||
|
|||||||
@@ -94,23 +94,20 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
|
|||||||
GenerateDefinitionKind.TRAIT -> append("interface ")
|
GenerateDefinitionKind.TRAIT -> append("interface ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
append(iface.name)
|
||||||
|
if (iface.constructor != null && iface.constructor.arguments.isNotEmpty()) {
|
||||||
|
append("(")
|
||||||
|
renderArgumentsDeclaration(iface.constructor.arguments.map { it.dynamicIfUnknownType(allTypes.keySet()) }, false)
|
||||||
|
append(")")
|
||||||
|
}
|
||||||
|
|
||||||
val allSuperTypes = iface.allSuperTypes(allTypes)
|
val allSuperTypes = iface.allSuperTypes(allTypes)
|
||||||
val allSuperTypesNames = allSuperTypes.map { it.name }.toSet()
|
val allSuperTypesNames = allSuperTypes.map { it.name }.toSet()
|
||||||
|
|
||||||
append(iface.name)
|
val superCalls = iface.superConstructorCalls.map { it.name }.toSet()
|
||||||
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(")")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val superCallName = primary?.initTypeCall?.name
|
|
||||||
val superTypesWithCalls =
|
val superTypesWithCalls =
|
||||||
listOf(primary?.initTypeCall).filterNotNull().map { renderCall(it) } +
|
iface.superConstructorCalls.map { renderCall(it) } +
|
||||||
iface.superTypes.filter { it != superCallName && it in allSuperTypesNames } +
|
iface.superTypes.filter { it !in superCalls && it in allSuperTypesNames } +
|
||||||
(typeNamesToUnions[iface.name] ?: emptyList())
|
(typeNamesToUnions[iface.name] ?: emptyList())
|
||||||
|
|
||||||
if (superTypesWithCalls.isNotEmpty()) {
|
if (superTypesWithCalls.isNotEmpty()) {
|
||||||
@@ -119,20 +116,6 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
|
|||||||
|
|
||||||
appendln (" {")
|
appendln (" {")
|
||||||
|
|
||||||
iface.secondaryConstructors.forEach { secondary ->
|
|
||||||
indent(1)
|
|
||||||
append("constructor(")
|
|
||||||
renderArgumentsDeclaration(secondary.constructor.arguments.dynamicIfUnknownType(allTypes.keySet()), false)
|
|
||||||
append(")")
|
|
||||||
|
|
||||||
if (secondary.initTypeCall != null) {
|
|
||||||
append(" : ")
|
|
||||||
append(renderCall(secondary.initTypeCall))
|
|
||||||
}
|
|
||||||
|
|
||||||
appendln()
|
|
||||||
}
|
|
||||||
|
|
||||||
val superAttributes = allSuperTypes.flatMap { it.memberAttributes }.distinct()
|
val superAttributes = allSuperTypes.flatMap { it.memberAttributes }.distinct()
|
||||||
val superFunctions = allSuperTypes.flatMap { it.memberFunctions }.distinct()
|
val superFunctions = allSuperTypes.flatMap { it.memberFunctions }.distinct()
|
||||||
val superSignatures = superAttributes.map { it.signature } merge superFunctions.map { it.signature }
|
val superSignatures = superAttributes.map { it.signature } merge superFunctions.map { it.signature }
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ data class FunctionType(val parameterTypes : List<Attribute>, val returnType : S
|
|||||||
val FunctionType.arity : Int
|
val FunctionType.arity : Int
|
||||||
get() = parameterTypes.size()
|
get() = parameterTypes.size()
|
||||||
val FunctionType.text : String
|
val FunctionType.text : String
|
||||||
get() = "(${parameterTypes.map { it.formatFunctionTypePart() }.join(", ")}) -> $returnType"
|
get() = "(${parameterTypes.map { it.formatFunctionTypePart() }.join(", ")}) -> ${returnType}"
|
||||||
|
|
||||||
fun FunctionType(text : String) : FunctionType {
|
fun FunctionType(text : String) : FunctionType {
|
||||||
val (parameters, returnType) = text.split("->".toRegex()).map { it.trim() }.filter { it != "" }
|
val (parameters, returnType) = text.split("->".toRegex()).map { it.trim() }.filter { it != "" }
|
||||||
@@ -135,13 +135,12 @@ private fun mapTypedef(repository: Repository, type: String): String {
|
|||||||
private fun splitUnionType(unionType: String) =
|
private fun splitUnionType(unionType: String) =
|
||||||
unionType.replace("Union<".toRegex(), "").replace("[>]+".toRegex(), "").split("\\s*,\\s*".toRegex()).distinct().map {it.replace("\\?$".toRegex(), "")}
|
unionType.replace("Union<".toRegex(), "").replace("[>]+".toRegex(), "").split("\\s*,\\s*".toRegex()).distinct().map {it.replace("\\?$".toRegex(), "")}
|
||||||
|
|
||||||
private fun GenerateFunction.allTypes() = sequenceOf(returnType) + arguments.asSequence().map { it.type }
|
private fun GenerateFunction?.allTypes() = if (this != null) sequenceOf(returnType) + arguments.asSequence().map { it.type } else emptySequence()
|
||||||
|
|
||||||
private fun collectUnionTypes(allTypes: Map<String, GenerateTraitOrClass>) =
|
private fun collectUnionTypes(allTypes: Map<String, GenerateTraitOrClass>) =
|
||||||
allTypes.values().asSequence()
|
allTypes.values().asSequence()
|
||||||
.flatMap {
|
.flatMap {
|
||||||
it.secondaryConstructors.asSequence().flatMap { it.constructor.allTypes() } +
|
it.constructor.allTypes() +
|
||||||
sequenceOf(it.primaryConstructor).filterNotNull().flatMap { it.constructor.allTypes() } +
|
|
||||||
it.memberAttributes.asSequence().map { it.type } +
|
it.memberAttributes.asSequence().map { it.type } +
|
||||||
it.memberFunctions.asSequence().flatMap { it.allTypes() }
|
it.memberFunctions.asSequence().flatMap { it.allTypes() }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user