/* * Copyright 2010-2020 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.generators.evaluate import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.Printer import java.io.File val FIR_DEST_FILE: File = File("compiler/fir/evaluate/src/org/jetbrains/kotlin/fir/evaluate/FirOperationsMapGenerated.kt") private val EXCLUDED_FUNCTIONS: List = listOf( "not", "toChar", "toString", "and", "or", "xor", "shl", "shr", "ushr", "compareTo", "equals", "rangeTo", "subSequence" ) private val EXCLUDED_TYPES: List = listOf("Boolean", "Char", "String") fun main() { GeneratorsFileUtil.writeFileIfContentChanged(FIR_DEST_FILE, generateFirMap()) } fun generateFirMap(): String { val sb = StringBuilder() val p = Printer(sb) p.println(File("license/COPYRIGHT_HEADER.txt").readText()) p.println("@file:Suppress(\"DEPRECATION\", \"DEPRECATION_ERROR\")") p.println( """ |package org.jetbrains.kotlin.fir.evaluate | |import org.jetbrains.kotlin.types.ConstantValueKind | |/** This file is generated by org.jetbrains.kotlin.generators.evaluate:generateFirMap(). DO NOT MODIFY MANUALLY */ | |internal data class UnaryOperationKey(val opr: ConstantValueKind, val opName: String) |internal data class BinaryOperationKey(val opr1: ConstantValueKind, val opr2: ConstantValueKind, val opName: String) | |@Suppress("UNCHECKED_CAST") |private fun unaryOperation( | t: ConstantValueKind, | opName: String, | operation: Function1 |) = UnaryOperationKey(t, opName) to operation as Function1 | |@Suppress("UNCHECKED_CAST") |private fun binaryOperation( | t: ConstantValueKind, | u: ConstantValueKind, | opName: String, | operation: Function2 |) = BinaryOperationKey(t, u, opName) to operation as Function2 """.trimMargin() ) p.println() val unaryOperationsMap = arrayListOf, Boolean>>() val binaryOperationsMap = arrayListOf>>() populateOperationsMap(unaryOperationsMap, binaryOperationsMap, EXCLUDED_FUNCTIONS) p.println("internal val unaryOperations: HashMap, Function1> = hashMapOf(") p.pushIndent() p.renderUnaryOperations(unaryOperationsMap) p.popIndent() p.println(")") p.println() p.println("internal val binaryOperations: HashMap, Function2> = hashMapOf(") p.pushIndent() p.renderBinaryOperations(binaryOperationsMap) p.popIndent() p.println(")") p.println() p.println( """ |internal val unaryOperatorNames = unaryOperations.map { it.key.opName }.toHashSet() |internal val binaryOperatorNames = binaryOperations.map { it.key.opName }.toHashSet() """.trimMargin() ) return sb.toString() } private fun Printer.renderUnaryOperations(unaryOperationsMap: List, Boolean>>) { val unaryOperationsMapIterator = unaryOperationsMap.iterator() while (unaryOperationsMapIterator.hasNext()) { val (funcName, parameters, isFunction) = unaryOperationsMapIterator.next() if (parameters.first().constructor.declarationDescriptor!!.name.asString() in EXCLUDED_TYPES) { continue } val parenthesesOrBlank = if (isFunction) "()" else "" this.println( "unaryOperation(", parameters.joinToString(", ") { it.asString() }, ", ", "\"$funcName\"", ", { a -> a.$funcName$parenthesesOrBlank })", if (unaryOperationsMapIterator.hasNext()) "," else "" ) } } private fun Printer.renderBinaryOperations(binaryOperationsMap: List>>) { val binaryOperationsMapIterator = binaryOperationsMap.iterator() while (binaryOperationsMapIterator.hasNext()) { val (funcName, parameters) = binaryOperationsMapIterator.next() if (parameters.first().constructor.declarationDescriptor!!.name.asString() in EXCLUDED_TYPES) { continue } this.println( "binaryOperation(", parameters.joinToString(", ") { it.asString() }, ", ", "\"$funcName\"", ", { a, b -> a.$funcName(b) })", if (binaryOperationsMapIterator.hasNext()) "," else "" ) } } private fun KotlinType.asString(): String = "ConstantValueKind." + constructor.declarationDescriptor!!.name.asString()