JS IR: initial lowerings reuse

This commit is contained in:
Anton Bannykh
2018-03-20 20:19:21 +03:00
committed by Anton Bannykh
parent 6adf7eaf04
commit a514c0f515
12 changed files with 222 additions and 53 deletions
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.ir.backend.js
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
object JsLoweredDeclarationOrigin : IrDeclarationOrigin {
object CLASS_STATIC_INITIALIZER : IrDeclarationOriginImpl("CLASS_STATIC_INITIALIZER")
}
@@ -0,0 +1,113 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.ir.backend.js
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.ReflectionTypes
import org.jetbrains.kotlin.backend.common.ir.Ir
import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
class JsIrBackendContext(
val module: ModuleDescriptor,
override val irBuiltIns: IrBuiltIns,
irModuleFragment: IrModuleFragment,
symbolTable: SymbolTable
) : CommonBackendContext {
override val builtIns = module.builtIns
override val sharedVariablesManager = JsSharedVariablesManager(builtIns)
override val reflectionTypes: ReflectionTypes by lazy(LazyThreadSafetyMode.PUBLICATION) {
// TODO
ReflectionTypes(module, FqName("kotlin.reflect"))
}
override val ir: Ir<CommonBackendContext> = object : Ir<CommonBackendContext>(this, irModuleFragment) {
override val symbols: Symbols<CommonBackendContext> = object : Symbols<CommonBackendContext>(this@JsIrBackendContext, symbolTable) {
override fun calc(initializer: () -> IrClassSymbol): IrClassSymbol {
return object : IrClassSymbol {
override val owner: IrClass get() = TODO("not implemented")
override val isBound: Boolean get() = TODO("not implemented")
override fun bind(owner: IrClass) = TODO("not implemented")
override val descriptor: ClassDescriptor get() = TODO("not implemented")
}
}
override val areEqual
get () = TODO("not implemented")
override val ThrowNullPointerException
get () = TODO("not implemented")
override val ThrowNoWhenBranchMatchedException
get () = TODO("not implemented")
override val ThrowTypeCastException
get () = TODO("not implemented")
override val ThrowUninitializedPropertyAccessException = symbolTable.referenceSimpleFunction(
irBuiltIns.defineOperator(
"throwUninitializedPropertyAccessException",
builtIns.nothingType,
listOf(builtIns.stringType)
).descriptor
)
override val stringBuilder
get() = TODO("not implemented")
override val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
override val coroutineImpl: IrClassSymbol
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
override val coroutineSuspendedGetter: IrSimpleFunctionSymbol
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
}
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
}
private fun find(memberScope: MemberScope, className: String): ClassDescriptor {
return find(memberScope, Name.identifier(className))
}
private fun find(memberScope: MemberScope, name: Name): ClassDescriptor {
return memberScope.getContributedClassifier(name, NoLookupLocation.FROM_BACKEND) as ClassDescriptor
}
override fun getInternalClass(name: String): ClassDescriptor {
return find(module.getPackage(FqName("kotlin.js")).memberScope, name)
}
override fun getClass(fqName: FqName): ClassDescriptor {
return find(module.getPackage(fqName.parent()).memberScope, fqName.shortName())
}
override fun getInternalFunctions(name: String): List<FunctionDescriptor> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun log(message: () -> String) {
/*TODO*/
print(message())
}
override fun report(element: IrElement?, irFile: IrFile?, message: String, isError: Boolean) {
/*TODO*/
print(message)
}
}
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.ir.backend.js
import org.jetbrains.kotlin.backend.common.descriptors.SharedVariablesManager
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
class JsSharedVariablesManager(builtIns: KotlinBuiltIns): SharedVariablesManager {
override fun createSharedVariableDescriptor(variableDescriptor: VariableDescriptor): VariableDescriptor {
TODO("not implemented")
}
override fun defineSharedValue(sharedVariableDescriptor: VariableDescriptor, originalDeclaration: IrVariable): IrStatement {
TODO("not implemented")
}
override fun getSharedValue(sharedVariableDescriptor: VariableDescriptor, originalGet: IrGetValue): IrExpression {
TODO("not implemented")
}
override fun setSharedValue(sharedVariableDescriptor: VariableDescriptor, originalSet: IrSetVariable): IrExpression {
TODO("not implemented")
}
}
@@ -6,8 +6,15 @@
package org.jetbrains.kotlin.ir.backend.js
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.common.runOnFilePostfix
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.IrModuleToJsTransformer
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
@@ -25,9 +32,26 @@ fun compile(
TopDownAnalyzerFacadeForJS.checkForErrors(files, analysisResult.bindingContext)
val moduleFragment = Psi2IrTranslator().generateModule(analysisResult.moduleDescriptor, files, analysisResult.bindingContext)
val psi2IrTranslator = Psi2IrTranslator()
val psi2IrContext = psi2IrTranslator.createGeneratorContext(analysisResult.moduleDescriptor, analysisResult.bindingContext)
val moduleFragment = psi2IrTranslator.generateModuleFragment(psi2IrContext, files)
val context = JsIrBackendContext(analysisResult.moduleDescriptor, psi2IrContext.irBuiltIns, moduleFragment, psi2IrContext.symbolTable)
ExternalDependenciesGenerator(psi2IrContext.symbolTable, psi2IrContext.irBuiltIns).generateUnboundSymbolsAsDependencies(moduleFragment)
moduleFragment.files.forEach { context.lower(it) }
val program = moduleFragment.accept(IrModuleToJsTransformer(), null)
return program.toString()
}
fun JsIrBackendContext.lower(file: IrFile) {
LateinitLowering(this, true).lower(file)
PropertiesLowering().lower(file)
LocalFunctionsLowering(this).lower(file)
DefaultArgumentStubGenerator(this).runOnFilePostfix(file)
InitializersLowering(this, JsLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER).runOnFilePostfix(file)
}