Switch JvmBackendContext to CommonBackendContext

(cherry picked from commit 5d4736c)
This commit is contained in:
Mikhael Bogdanov
2017-05-31 12:03:10 +02:00
parent f963b8bd42
commit e16bdd4287
3 changed files with 104 additions and 5 deletions
@@ -28,6 +28,8 @@ interface CommonBackendContext : BackendContext {
//TODO move to builtins
fun getInternalClass(name: String): ClassDescriptor
fun getClass(fqName: FqName): ClassDescriptor
//TODO move to builtins
fun getInternalFunctions(name: String): List<FunctionDescriptor>
@@ -16,19 +16,114 @@
package org.jetbrains.kotlin.backend.jvm
import org.jetbrains.kotlin.backend.common.BackendContext
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.backend.jvm.descriptors.JvmSharedVariablesManager
import org.jetbrains.kotlin.backend.jvm.descriptors.SpecialDescriptorsFactory
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
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.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.resolve.scopes.MemberScope
class JvmBackendContext(
val state: GenerationState,
val psiSourceManager: PsiSourceManager,
override val irBuiltIns: IrBuiltIns
) : BackendContext {
psiSourceManager: PsiSourceManager,
override val irBuiltIns: IrBuiltIns,
irModuleFragment: IrModuleFragment, symbolTable: SymbolTable
) : CommonBackendContext {
override val builtIns = state.module.builtIns
val specialDescriptorsFactory = SpecialDescriptorsFactory(psiSourceManager, builtIns)
override val sharedVariablesManager = JvmSharedVariablesManager(builtIns)
override val reflectionTypes: ReflectionTypes by lazy(LazyThreadSafetyMode.PUBLICATION) {
ReflectionTypes(state.module, FqName("kotlin.reflect.jvm.internal"))
}
override val ir: Ir<CommonBackendContext> = object : Ir<CommonBackendContext>(this, irModuleFragment) {
override val symbols: Symbols<CommonBackendContext> = object: Symbols<CommonBackendContext>(this@JvmBackendContext, symbolTable) {
override val areEqual
get () = symbolTable.referenceSimpleFunction(context.getInternalFunctions("areEqual").single())
override val ThrowNullPointerException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowNullPointerException").single())
override val ThrowNoWhenBranchMatchedException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowNoWhenBranchMatchedException").single())
override val ThrowTypeCastException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowTypeCastException").single())
override val ThrowUninitializedPropertyAccessException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowUninitializedPropertyAccessException").single()
)
override val stringBuilder
get() = symbolTable.referenceClass(
context.getClass(FqName("java.lang.StringBuilder"))
)
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(state.module.getPackage(FqName("kotlin.jvm.internal")).memberScope, name)
}
override fun getClass(fqName: FqName): ClassDescriptor {
return find(state.module.getPackage(fqName.parent()).memberScope, fqName.shortName())
}
override fun getInternalFunctions(name: String): List<FunctionDescriptor> {
return when (name) {
"ThrowUninitializedPropertyAccessException" ->
getInternalClass("Intrinsics").staticScope.
getContributedFunctions(Name.identifier("throwUninitializedPropertyAccessException"), NoLookupLocation.FROM_BACKEND).toList()
else -> TODO(name)
}
}
override fun log(message: () -> String) {
/*TODO*/
print(message())
}
override fun report(element: IrElement?, irFile: IrFile?, message: String, isError: Boolean) {
/*TODO*/
print(message)
}
}
@@ -37,7 +37,9 @@ object JvmBackendFacade {
val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext)
val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files)
val jvmBackendContext = JvmBackendContext(state, psi2irContext.sourceManager, psi2irContext.irBuiltIns)
val jvmBackendContext = JvmBackendContext(
state, psi2irContext.sourceManager, psi2irContext.irBuiltIns, irModuleFragment, psi2irContext.symbolTable
)
val jvmBackend = JvmBackend(jvmBackendContext)
for (irFile in irModuleFragment.files) {