[FIR/IR generator] Move usedTypes to AbstractElement, rename it

This commit is contained in:
Sergej Jaskiewicz
2023-12-05 14:52:36 +01:00
committed by Space Team
parent e4078d163b
commit 19ff050eae
9 changed files with 23 additions and 9 deletions
@@ -22,7 +22,6 @@ fun Implementation.generateCode(generationPath: File): GeneratedFile =
this.typeName, this.typeName,
fileSuppressions = listOf("DuplicatedCode", "unused"), fileSuppressions = listOf("DuplicatedCode", "unused"),
) { ) {
addAllImports(usedTypes)
ImplementationPrinter(this).printImplementation(this@generateCode) ImplementationPrinter(this).printImplementation(this@generateCode)
} }
@@ -208,7 +208,7 @@ object IrTree : AbstractTreeBuilder() {
+field("isCrossinline", boolean) +field("isCrossinline", boolean)
+field("isNoinline", boolean) +field("isNoinline", boolean)
+field("isHidden", boolean) { +field("isHidden", boolean) {
usedTypes.add(idSignatureType) additionalImports.add(idSignatureType)
kDoc = """ kDoc = """
If `true`, the value parameter does not participate in [IdSignature] computation. If `true`, the value parameter does not participate in [IdSignature] computation.
@@ -487,7 +487,7 @@ object IrTree : AbstractTreeBuilder() {
+field("name", type<Name>(), mutable = false) +field("name", type<Name>(), mutable = false)
+field("irBuiltins", type(Packages.tree, "IrBuiltIns"), mutable = false) +field("irBuiltins", type(Packages.tree, "IrBuiltIns"), mutable = false)
+listField("files", file, mutability = MutableList) +listField("files", file, mutability = MutableList)
usedTypes += ArbitraryImportable(Packages.tree, "UNDEFINED_OFFSET") additionalImports += ArbitraryImportable(Packages.tree, "UNDEFINED_OFFSET")
+field("startOffset", int, mutable = false) { +field("startOffset", int, mutable = false) {
baseGetter = "UNDEFINED_OFFSET" baseGetter = "UNDEFINED_OFFSET"
} }
@@ -722,8 +722,8 @@ object IrTree : AbstractTreeBuilder() {
visibility = Visibility.PROTECTED visibility = Visibility.PROTECTED
} }
usedTypes += ArbitraryImportable(Packages.exprs, "checkArgumentSlotAccess")
generationCallback = { generationCallback = {
addImport(ArbitraryImportable(Packages.exprs, "checkArgumentSlotAccess"))
val indexParam = FunctionParameter("index", StandardTypes.int) val indexParam = FunctionParameter("index", StandardTypes.int)
val valueArgumentParam = FunctionParameter("valueArgument", expression.copy(nullable = true)) val valueArgumentParam = FunctionParameter("valueArgument", expression.copy(nullable = true))
val typeArgumentParam = FunctionParameter("type", irTypeType.copy(nullable = true)) val typeArgumentParam = FunctionParameter("type", irTypeType.copy(nullable = true))
@@ -124,8 +124,6 @@ class Element(
override var kDoc: String? = null override var kDoc: String? = null
val usedTypes = mutableListOf<Importable>()
override fun toString() = name override fun toString() = name
operator fun TypeVariable.unaryPlus() = apply { operator fun TypeVariable.unaryPlus() = apply {
@@ -137,7 +137,6 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
fun printElements(generationPath: File, model: Model) = model.elements.map { element -> fun printElements(generationPath: File, model: Model) = model.elements.map { element ->
printGeneratedType(generationPath, TREE_GENERATOR_README, element.packageName, element.typeName) { printGeneratedType(generationPath, TREE_GENERATOR_README, element.packageName, element.typeName) {
addAllImports(element.usedTypes)
ElementPrinter(this).printElement(element) ElementPrinter(this).printElement(element)
} }
} }
@@ -166,6 +166,15 @@ abstract class AbstractElement<Element, Field, Implementation>(
} }
} }
/**
* Types/functions that you want to additionally import in the file with the element class.
*
* This is useful if, for example, default values of fields reference classes or functions from other packages.
*
* Note that classes referenced in field types will be imported automatically.
*/
val additionalImports = mutableListOf<Importable>()
final override fun get(fieldName: String): Field? { final override fun get(fieldName: String): Field? {
return allFields.firstOrNull { it.name == fieldName } return allFields.firstOrNull { it.name == fieldName }
} }
@@ -31,6 +31,7 @@ abstract class AbstractElementPrinter<Element : AbstractElement<Element, Field,
context(ImportCollector) context(ImportCollector)
fun printElement(element: Element) { fun printElement(element: Element) {
addAllImports(element.additionalImports)
printer.run { printer.run {
val kind = element.kind ?: error("Expected non-null element kind") val kind = element.kind ?: error("Expected non-null element kind")
@@ -42,7 +42,14 @@ abstract class AbstractImplementation<Implementation, Element, Field>(
override val packageName = element.packageName + ".impl" override val packageName = element.packageName + ".impl"
val usedTypes = mutableListOf<Importable>() /**
* Types/functions that you want to additionally import in the file with the implementation class.
*
* This is useful if, for example, default values of fields reference classes or functions from other packages.
*
* Note that classes referenced in field types will be imported automatically.
*/
val additionalImports = mutableListOf<Importable>()
init { init {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@@ -32,6 +32,7 @@ abstract class AbstractImplementationPrinter<Implementation, Element, Implementa
context(ImportCollector) context(ImportCollector)
fun printImplementation(implementation: Implementation) { fun printImplementation(implementation: Implementation) {
addAllImports(implementation.additionalImports)
printer.run { printer.run {
buildSet { buildSet {
if (implementation.requiresOptIn) { if (implementation.requiresOptIn) {
@@ -156,7 +156,7 @@ abstract class AbstractImplementationConfigurator<Implementation, Element, Imple
* Note that classes referenced in field types will be imported automatically. * Note that classes referenced in field types will be imported automatically.
*/ */
fun additionalImports(vararg importables: Importable) { fun additionalImports(vararg importables: Importable) {
importables.forEach { implementation.usedTypes += it } implementation.additionalImports.addAll(importables)
} }
/** /**