Skip to content

Commit

Permalink
Add header verification for functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed May 14, 2024
1 parent 5cbdf7e commit a791b8b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.touchlab.skie.sir.element

import co.touchlab.skie.sir.signature.Signature
import co.touchlab.skie.util.collisionFreeIdentifier
import co.touchlab.skie.util.swift.escapeSwiftIdentifier
import io.outfoxx.swiftpoet.CodeBlock
import io.outfoxx.swiftpoet.FunctionSpec
Expand Down Expand Up @@ -76,4 +77,31 @@ fun SirFunction.copyValueParametersFrom(valueParameters: List<SirValueParameter>
inout = it.inout,
)
}

fixValueParameterCollisions()
}

fun SirFunction.fixValueParameterCollisions() {
fixValueParameterNameCollisions()
fixValueParameterLabelCollisions()
}

private fun SirFunction.fixValueParameterNameCollisions() {
val usedNames = mutableSetOf<String>()

valueParameters.forEach { parameter ->
parameter.name = parameter.name.collisionFreeIdentifier(usedNames)

usedNames.add(parameter.name)
}
}

private fun SirFunction.fixValueParameterLabelCollisions() {
val usedNames = mutableSetOf<String>()

valueParameters.forEach { parameter ->
parameter.label = parameter.labelOrName.collisionFreeIdentifier(usedNames)

usedNames.add(parameter.labelOrName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,28 @@ sealed interface SirTypeParameterParent {
}
}

fun SirTypeParameterParent.copyTypeParametersFrom(other: SirClass) {
val copiesWithOriginal = other.typeParameters.map {
fun SirTypeParameterParent.copyTypeParametersFrom(other: SirTypeParameterParent) {
copyTypeParametersFrom(other.typeParameters)
}

fun SirTypeParameterParent.copyTypeParametersFrom(
copiedTypeParameters: List<SirTypeParameter>,
allTypeParameters: List<SirTypeParameter> = copiedTypeParameters,
) {
val copiesWithOriginal = copiedTypeParameters.map {
it to SirTypeParameter(it.name)
}

val substitutions = copiesWithOriginal.toMap()
val nonCopiedTypeParameters = allTypeParameters - copiedTypeParameters.toSet()

val nonCopiedSubstitutions = nonCopiedTypeParameters.map { nonCopiedTypeParameter ->
val existingTypeParameter = (typeParameters.firstOrNull { it.name == nonCopiedTypeParameter.name }
?: error("Type parameter ${nonCopiedTypeParameter.name} not found in parent scope"))

nonCopiedTypeParameter to existingTypeParameter
}

val substitutions = (copiesWithOriginal + nonCopiedSubstitutions).toMap()

copiesWithOriginal.forEach { (original, copy) ->
// TODO This is not entirely correct, because we don't substitute type parameters from parent scope as nested scopes are not implemented yet.
Expand Down

0 comments on commit a791b8b

Please sign in to comment.