Compare commits

...

5 Commits

Author SHA1 Message Date
Anton Bannykh
167c809337 review fix 2018-03-29 16:28:09 +03:00
Anton Bannykh
8485307fc3 review fix 2018-03-28 21:05:16 +03:00
Anton Bannykh
52f0d7b0cb review fix 2018-03-28 21:04:25 +03:00
Anton Bannykh
5c02110c58 review fix 2018-03-28 15:25:23 +03:00
Anton Bannykh
2dac8efa79 JS IR: initial lowerings reuse 2018-03-28 14:48:07 +03:00
12 changed files with 215 additions and 54 deletions

View File

@@ -34,7 +34,14 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
protected fun builtInsPackage(vararg packageNameSegments: String) =
context.builtIns.builtInsModule.getPackage(FqName.fromSegments(listOf(*packageNameSegments))).memberScope
val refClass = symbolTable.referenceClass(context.getInternalClass("Ref"))
// This hack allows to disable related symbol instantiation in descendants
// TODO move relevant symbols to non-common code
open fun calc(initializer: () -> IrClassSymbol): IrClassSymbol {
return initializer()
}
val refClass = calc { symbolTable.referenceClass(context.getInternalClass("Ref")) }
//abstract val areEqualByValue: List<IrFunctionSymbol>
@@ -44,7 +51,7 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
abstract val ThrowNoWhenBranchMatchedException: IrFunctionSymbol
abstract val ThrowTypeCastException: IrFunctionSymbol
abstract val ThrowUninitializedPropertyAccessException: IrFunctionSymbol
abstract val ThrowUninitializedPropertyAccessException: IrSimpleFunctionSymbol
abstract val stringBuilder: IrClassSymbol
@@ -74,7 +81,7 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
// val getProgressionLast = context.getInternalFunctions("getProgressionLast")
// .map { Pair(it.returnType, symbolTable.referenceSimpleFunction(it)) }.toMap()
val defaultConstructorMarker = symbolTable.referenceClass(context.getInternalClass("DefaultConstructorMarker"))
val defaultConstructorMarker = calc { symbolTable.referenceClass(context.getInternalClass("DefaultConstructorMarker")) }
val any = symbolTable.referenceClass(builtIns.any)
val unit = symbolTable.referenceClass(builtIns.unit)
@@ -137,13 +144,13 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
abstract val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
val intAnd = symbolTable.referenceFunction(
val intAnd = symbolTable.referenceSimpleFunction(
builtIns.intType.memberScope
.getContributedFunctions(OperatorNameConventions.AND, NoLookupLocation.FROM_BACKEND)
.single()
)
val intPlusInt = symbolTable.referenceFunction(
val intPlusInt = symbolTable.referenceSimpleFunction(
builtIns.intType.memberScope
.getContributedFunctions(OperatorNameConventions.PLUS, NoLookupLocation.FROM_BACKEND)
.single {
@@ -164,14 +171,14 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
abstract val coroutineSuspendedGetter: IrSimpleFunctionSymbol
val kFunctionImpl = symbolTable.referenceClass(context.reflectionTypes.kFunctionImpl)
val kFunctionImpl = calc { symbolTable.referenceClass(context.reflectionTypes.kFunctionImpl) }
val kProperty0Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty0Impl)
val kProperty1Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty1Impl)
val kProperty2Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty2Impl)
val kMutableProperty0Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty0Impl)
val kMutableProperty1Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty1Impl)
val kMutableProperty2Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty2Impl)
val kProperty0Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kProperty0Impl) }
val kProperty1Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kProperty1Impl) }
val kProperty2Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kProperty2Impl) }
val kMutableProperty0Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kMutableProperty0Impl) }
val kMutableProperty1Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kMutableProperty1Impl) }
val kMutableProperty2Impl = calc { symbolTable.referenceClass(context.reflectionTypes.kMutableProperty2Impl) }
// val kLocalDelegatedPropertyImpl = symbolTable.referenceClass(context.reflectionTypes.kLocalDelegatedPropertyImpl)
// val kLocalDelegatedMutablePropertyImpl = symbolTable.referenceClass(context.reflectionTypes.kLocalDelegatedMutablePropertyImpl)

View File

@@ -1,24 +1,12 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 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.backend.jvm.lower
package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.SourceElement
@@ -29,6 +17,7 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrAnonymousInitializer
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.expressions.IrExpression
@@ -48,7 +37,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import java.util.*
class InitializersLowering(val context: JvmBackendContext) : ClassLoweringPass {
class InitializersLowering(val context: CommonBackendContext, val declarationOrigin: IrDeclarationOrigin) : ClassLoweringPass {
override fun lower(irClass: IrClass) {
val classInitializersBuilder = ClassInitializersBuilder(irClass)
irClass.acceptChildrenVoid(classInitializersBuilder)
@@ -106,9 +95,9 @@ class InitializersLowering(val context: JvmBackendContext) : ClassLoweringPass {
fun createStaticInitializationMethod(irClass: IrClass) {
val staticInitializerDescriptor = SimpleFunctionDescriptorImpl.create(
irClass.descriptor, Annotations.EMPTY, clinitName,
CallableMemberDescriptor.Kind.SYNTHESIZED,
SourceElement.NO_SOURCE
irClass.descriptor, Annotations.EMPTY, clinitName,
CallableMemberDescriptor.Kind.SYNTHESIZED,
SourceElement.NO_SOURCE
)
staticInitializerDescriptor.initialize(
null, null, emptyList(), emptyList(),
@@ -116,7 +105,7 @@ class InitializersLowering(val context: JvmBackendContext) : ClassLoweringPass {
Modality.FINAL, Visibilities.PUBLIC
)
irClass.declarations.add(
IrFunctionImpl(irClass.startOffset, irClass.endOffset, JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER,
IrFunctionImpl(irClass.startOffset, irClass.endOffset, declarationOrigin,
staticInitializerDescriptor,
IrBlockBodyImpl(irClass.startOffset, irClass.endOffset,
staticInitializerStatements.map { it.copy() }))

View File

@@ -1,20 +1,9 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 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.backend.jvm.lower
package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.ir.IrStatement
@@ -31,7 +20,7 @@ import java.util.*
class PropertiesLowering : IrElementTransformerVoid(), FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(this)
irFile.accept(this, null)
}
override fun visitFile(declaration: IrFile): IrFile {

View File

@@ -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")
}

View File

@@ -5,4 +5,109 @@
package org.jetbrains.kotlin.ir.backend.js
class JsIrBackendContext
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)
}
}

View File

@@ -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")
}
}

View File

@@ -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)
}

View File

@@ -51,7 +51,7 @@ class JvmLower(val context: JvmBackendContext) {
EnumClassLowering(context).runOnFilePostfix(irFile)
//Should be before SyntheticAccessorLowering cause of synthetic accessor for companion constructor
ObjectClassLowering(context).lower(irFile)
InitializersLowering(context).runOnFilePostfix(irFile)
InitializersLowering(context, JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER).runOnFilePostfix(irFile)
SingletonReferencesLowering(context).runOnFilePostfix(irFile)
SyntheticAccessorLowering(context).lower(irFile)
BridgeLowering(context).runOnFilePostfix(irFile)

View File

@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.backend.jvm.codegen
import org.jetbrains.kotlin.backend.common.lower.DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER
import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDescriptorWithExtraFlags
import org.jetbrains.kotlin.backend.jvm.lower.InitializersLowering
import org.jetbrains.kotlin.backend.common.lower.InitializersLowering
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.FunctionCodegen
import org.jetbrains.kotlin.codegen.state.GenerationState

View File

@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.common.lower.DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.descriptors.DefaultImplsClassDescriptorImpl
import org.jetbrains.kotlin.backend.jvm.lower.InitializersLowering.Companion.clinitName
import org.jetbrains.kotlin.backend.common.lower.InitializersLowering.Companion.clinitName
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper

View File

@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.lower.InitializersLowering
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl

View File

@@ -42,7 +42,7 @@ class IrBuiltIns(val builtIns: KotlinBuiltIns) {
private val stubBuilder = DeclarationStubGenerator(SymbolTable(), IrDeclarationOrigin.IR_BUILTINS_STUB)
private fun defineOperator(name: String, returnType: KotlinType, valueParameterTypes: List<KotlinType>): IrSimpleFunction {
fun defineOperator(name: String, returnType: KotlinType, valueParameterTypes: List<KotlinType>): IrSimpleFunction {
val operatorDescriptor = IrSimpleBuiltinOperatorDescriptorImpl(packageFragment, Name.identifier(name), returnType)
for ((i, valueParameterType) in valueParameterTypes.withIndex()) {
operatorDescriptor.addValueParameter(