Skip to content

Commit

Permalink
Switch to auto generated IR tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpiroman committed Oct 29, 2021
1 parent 47aba01 commit bbe2389
Show file tree
Hide file tree
Showing 226 changed files with 4,518 additions and 2,859 deletions.
Expand Up @@ -24,10 +24,7 @@ import org.jetbrains.kotlin.fir.resolve.calls.ResolvedCallArgument
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.ir.builders.declarations.UNDEFINED_PARAMETER_INDEX
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBlock
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
Expand Down
Expand Up @@ -26,8 +26,6 @@ import org.jetbrains.kotlin.fir.resolve.FirSamResolverImpl
import org.jetbrains.kotlin.fir.resolve.calls.getExpectedType
import org.jetbrains.kotlin.fir.resolve.calls.isFunctional
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
import org.jetbrains.kotlin.fir.types.isBuiltinFunctionalType
import org.jetbrains.kotlin.fir.types.isKMutableProperty
import org.jetbrains.kotlin.fir.resolve.substitution.AbstractConeSubstitutor
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap
Expand Down Expand Up @@ -672,7 +670,7 @@ class CallAndReferenceGenerator(
}
val argumentMapping = this.elements.zip(argument.arguments).toMap()
// [IrElementTransformer] is not preferred, since it's hard to visit vararg elements only.
val irVarargElements = elements as MutableList<IrVarargElement>
val irVarargElements = elements
irVarargElements.replaceAll { irVarargElement ->
if (irVarargElement is IrExpression) {
val firVarargArgument =
Expand Down
Expand Up @@ -75,7 +75,7 @@ class CheckIrElementVisitor(
}
}

override fun <T> visitConst(expression: IrConst<T>) {
override fun visitConst(expression: IrConst<*>) {
super.visitConst(expression)

@Suppress("UNUSED_VARIABLE")
Expand Down
Expand Up @@ -41,7 +41,7 @@ class ScopeValidator(
}
}

fun visitVariableAccess(element: IrElement, variable: IrValueDeclaration) {
fun visitValueAccess(element: IrElement, variable: IrValueDeclaration) {
if (variable !in this.values) {
reportError(element, "Value ${variable.render()} not accessible")
}
Expand Down Expand Up @@ -130,7 +130,7 @@ class ScopeValidator(
}

override fun visitValueAccess(expression: IrValueAccessExpression, data: Visibles) {
data.visitVariableAccess(expression, expression.symbol.owner)
data.visitValueAccess(expression, expression.symbol.owner)
super.visitValueAccess(expression, data)
}

Expand Down
Expand Up @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.addArgument
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isInt
Expand Down
Expand Up @@ -374,8 +374,8 @@ fun usefulDeclarations(
expression.symbol.owner.enqueue("raw function access")
}

override fun visitVariableAccess(expression: IrValueAccessExpression) {
super.visitVariableAccess(expression)
override fun visitValueAccess(expression: IrValueAccessExpression) {
super.visitValueAccess(expression)

expression.symbol.owner.enqueue("variable access")
}
Expand Down
Expand Up @@ -43,7 +43,7 @@ class ConstTransformer(private val context: JsIrBackendContext) : IrElementTrans
private fun createLong(v: Long): IrExpression =
lowerConst(context.intrinsics.longClassSymbol, IrConstImpl.Companion::int, v.toInt(), (v shr 32).toInt())

override fun <T> visitConst(expression: IrConst<T>): IrExpression {
override fun visitConst(expression: IrConst<*>): IrExpression {
with(context.intrinsics) {
if (expression.type.isUnsigned() && expression.kind != IrConstKind.Null) {
return when (expression.type.classifierOrNull) {
Expand Down
Expand Up @@ -48,7 +48,7 @@ class IrElementToJsExpressionTransformer : BaseIrElementToJsNodeTransformer<JsEx
return irFunction.accept(IrFunctionToJsTransformer(), context).apply { name = null }
}

override fun <T> visitConst(expression: IrConst<T>, context: JsGenerationContext): JsExpression {
override fun visitConst(expression: IrConst<*>, context: JsGenerationContext): JsExpression {
val kind = expression.kind
return when (kind) {
is IrConstKind.String -> JsStringLiteral(kind.valueOf(expression))
Expand Down
Expand Up @@ -89,7 +89,7 @@ fun foldString(expression: IrExpression, context: JsIrBackendContext): String? {
expression.acceptChildrenVoid(this)
}

override fun <T> visitConst(expression: IrConst<T>) {
override fun visitConst(expression: IrConst<*>) {
builder.append(expression.kind.valueOf(expression))
}

Expand Down
Expand Up @@ -9,15 +9,16 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.ir.constantValue
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.incremental.components.InlineConstTracker
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.expressions.impl.copyWithOffsets
import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid

internal val constPhase1 = makeIrFilePhase(
Expand Down
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.copyWithOffsets
import org.jetbrains.kotlin.ir.symbols.impl.IrPublicSymbolBase
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
import org.jetbrains.kotlin.ir.types.*
Expand Down
Expand Up @@ -844,7 +844,7 @@ class ExpressionCodegen(
return unitValue
}

override fun <T> visitConst(expression: IrConst<T>, data: BlockInfo): PromisedValue {
override fun visitConst(expression: IrConst<*>, data: BlockInfo): PromisedValue {
expression.markLineNumber(startOffset = true)
when (val value = expression.value) {
is Boolean -> {
Expand Down
Expand Up @@ -21,7 +21,10 @@ import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.getInlineClassBackingField
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.wasm.ir.*
Expand Down Expand Up @@ -99,7 +102,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
}
}

override fun <T> visitConst(expression: IrConst<T>) {
override fun visitConst(expression: IrConst<*>) {
when (val kind = expression.kind) {
is IrConstKind.Null -> generateDefaultInitializerForType(context.transformType(expression.type), body)
is IrConstKind.Boolean -> body.buildConstI32(if (kind.valueOf(expression)) 1 else 0)
Expand Down
Expand Up @@ -13,7 +13,8 @@ import org.jetbrains.kotlin.ir.interpreter.accessesTopLevelOrObjectField
import org.jetbrains.kotlin.ir.interpreter.fqName
import org.jetbrains.kotlin.ir.interpreter.isAccessToObject
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.statements
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor

class IrCompileTimeChecker(
Expand Down Expand Up @@ -84,7 +85,7 @@ class IrCompileTimeChecker(
return body.kind == IrSyntheticBodyKind.ENUM_VALUES || body.kind == IrSyntheticBodyKind.ENUM_VALUEOF
}

override fun <T> visitConst(expression: IrConst<T>, data: Nothing?): Boolean {
override fun visitConst(expression: IrConst<*>, data: Nothing?): Boolean {
if (expression.type.getUnsignedType() != null) {
val constructor = expression.type.classOrNull?.owner?.constructors?.singleOrNull() ?: return false
return mode.canEvaluateFunction(constructor)
Expand Down
Expand Up @@ -23,7 +23,10 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.toArrayOrPrimitiveArrayType
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.indexOrMinusOne
import org.jetbrains.kotlin.ir.util.isCrossinline
import org.jetbrains.kotlin.ir.util.isNoinline
import org.jetbrains.kotlin.ir.util.varargElementType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
Expand Down Expand Up @@ -112,33 +115,35 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
makeParameter(it.thisAsReceiverParameter, IrDeclarationOrigin.SCRIPT_IMPLICIT_RECEIVER, parametersIndex++)
}

irScript.providedProperties = descriptor.scriptProvidedProperties.zip(descriptor.scriptProvidedPropertiesParameters)
.map { (providedProperty, parameter) ->
// TODO: initializer
// TODO: do not keep direct links
val type = providedProperty.type.toIrType()
val valueParameter = context.symbolTable.declareValueParameter(
descriptor.scriptProvidedProperties.zip(descriptor.scriptProvidedPropertiesParameters) { providedProperty, parameter ->
// TODO: initializer
// TODO: do not keep direct links
val type = providedProperty.type.toIrType()
val valueParameter = context.symbolTable.declareValueParameter(
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, parameter, type
) { symbol ->
context.irFactory.createValueParameter(
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, parameter, type
) { symbol ->
context.irFactory.createValueParameter(
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, symbol, descriptor.name,
parametersIndex, type, null, isCrossinline = false, isNoinline = false, isHidden = false, isAssignable = false
).also { it.parent = irScript }
}
parametersIndex++
val irProperty =
PropertyGenerator(declarationGenerator).generateSyntheticProperty(
ktScript,
providedProperty,
valueParameter,
generateSyntheticAccessors = true
)
irProperty.origin = IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY
irScript.statements += irProperty
valueParameter to irProperty.symbol
IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY, symbol, descriptor.name,
parametersIndex, type, null, isCrossinline = false, isNoinline = false, isHidden = false, isAssignable = false
).also { it.parent = irScript }
}
parametersIndex++
val irProperty =
PropertyGenerator(declarationGenerator).generateSyntheticProperty(
ktScript,
providedProperty,
valueParameter,
generateSyntheticAccessors = true
)
irProperty.origin = IrDeclarationOrigin.SCRIPT_PROVIDED_PROPERTY
irScript.statements += irProperty
valueParameter to irProperty.symbol
}.unzip().let { (params, props) ->
irScript.providedProperties = props
irScript.providedPropertiesParameters = params
}

irScript.constructor = with(IrFunctionBuilder().apply {
isPrimary = true
Expand All @@ -157,7 +162,7 @@ class ScriptGenerator(declarationGenerator: DeclarationGenerator) : DeclarationG
addIfNotNull(irScript.earlierScriptsParameter)
addAll(irScript.explicitCallParameters)
addAll(irScript.implicitReceiversParameters)
irScript.providedProperties.forEach { add(it.first) }
addAll(irScript.providedPropertiesParameters)
}
irConstructor.parent = irScript
}
Expand Down
50 changes: 49 additions & 1 deletion compiler/ir/ir.tree/build.gradle.kts
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.ideaExt.idea

plugins {
kotlin("jvm")
id("jps-compatible")
Expand All @@ -15,6 +17,52 @@ dependencies {
}

sourceSets {
"main" { projectDefault() }
"main" {
projectDefault()
this.java.srcDir("gen")
}
"test" {}
}

val generatorClasspath by configurations.creating

dependencies {
generatorClasspath(project("tree-generator"))
}

val generationRoot = projectDir.resolve("gen")

val generateTree by tasks.registering(NoDebugJavaExec::class) {

val generatorRoot = "$projectDir/tree-generator/src/"

val generatorConfigurationFiles = fileTree(generatorRoot) {
include("**/*.kt")
}

inputs.files(generatorConfigurationFiles)
outputs.dirs(generationRoot)

args(generationRoot)
workingDir = rootDir
classpath = generatorClasspath
main = "org.jetbrains.kotlin.ir.generator.MainKt"
systemProperties["line.separator"] = "\n"
}

val compileKotlin by tasks

compileKotlin.dependsOn(generateTree)

tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>> {
kotlinOptions {
freeCompilerArgs += "-Xinline-classes"
}
}

if (kotlinBuildProperties.isInJpsBuildIdeaSync) {
apply(plugin = "idea")
idea {
this.module.generatedSourceDirs.add(generationRoot)
}
}
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2021 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.
*/

//This file was generated automatically
//DO NOT MODIFY IT MANUALLY

package org.jetbrains.kotlin.ir

/**
* A non-leaf IR tree element.
* @sample org.jetbrains.kotlin.ir.generator.IrTree.abstractElement
*/
abstract class IrAbstractElement : IrElement
22 changes: 22 additions & 0 deletions compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/IrElement.kt
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2021 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.
*/

//This file was generated automatically
//DO NOT MODIFY IT MANUALLY

package org.jetbrains.kotlin.ir

import kotlin.Int
import org.jetbrains.kotlin.ir.IrElementBase

/**
* A non-leaf IR tree element.
* @sample org.jetbrains.kotlin.ir.generator.IrTree.baseElement
*/
interface IrElement : IrElementBase {
val startOffset: Int

val endOffset: Int
}
15 changes: 15 additions & 0 deletions compiler/ir/ir.tree/gen/org/jetbrains/kotlin/ir/IrStatement.kt
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2021 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.
*/

//This file was generated automatically
//DO NOT MODIFY IT MANUALLY

package org.jetbrains.kotlin.ir

/**
* A non-leaf IR tree element.
* @sample org.jetbrains.kotlin.ir.generator.IrTree.statement
*/
interface IrStatement : IrElement

0 comments on commit bbe2389

Please sign in to comment.