All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.sir.util.Comparators.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.sir.util

import org.jetbrains.kotlin.sir.*

/**
 * A set of [Comparator] objects that could be used to sort collections of SIR nodes.
 */
object Comparators {
    val stableExtensionComparator: Comparator = compareBy { it.extendedType.swift }
    val stableNamedComparator: Comparator = compareBy { it.name }
    val stableVariableComparator: Comparator = compareBy { it.name }
    val stableInitComparator: Comparator = compareBy(
        { it.parameters.size },
        { mangleParameters(it.parameters) },
    )
    val stableFunctionComparator: Comparator = compareBy(
        { it.name },
        { it.parameters.size },
        { mangleParameters(it.parameters) },
    )

    val stableCallableComparator: Comparator = compareBy {
        when (it) {
            is SirInit -> 0
            is SirFunction -> 1
            is SirAccessor -> 2
        }
    }.thenComparing(stableInitComparator)
        .thenComparing(stableFunctionComparator)

    private fun mangleParameters(params: List): String =
        params.joinToString { "${it.parameterName}-${it.argumentName}:${it.type.swift}" }

    private val SirType.swift
        get(): String = when (this) {
            is SirExistentialType -> error("Existential types are not supported yet")
            is SirNominalType -> type.name
            is SirErrorType -> "SirErrorType"
            is SirUnsupportedType -> "SirUnsupportedType"
        }

    private inline fun  Comparator.thenComparing(comparator: Comparator): Comparator {
        return thenComparing { a, b ->
            if (a is R && b is R) comparator.compare(a, b) else 0
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy