Binding: all current tests are green.
This commit is contained in:
+1
-1
@@ -187,7 +187,7 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
||||
enumEntriesByField[fieldPropertyDescriptor] = enumEntry.descriptor
|
||||
enumEntryFields.add(fieldPropertyDescriptor)
|
||||
|
||||
val enumEntryInitializer = enumEntry.initializerExpression
|
||||
val enumEntryInitializer = enumEntry.initializerExpression!!
|
||||
return IrFieldImpl(
|
||||
enumEntry.startOffset, enumEntry.endOffset, JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_ENTRY,
|
||||
fieldPropertyDescriptor,
|
||||
|
||||
+1
-1
@@ -139,7 +139,7 @@ class SyntheticAccessorLowering(val state: GenerationState) : FileLoweringPass,
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR,
|
||||
accessorDescriptor, body
|
||||
)
|
||||
val calleeDescriptor = accessor.calleeDescriptor
|
||||
val calleeDescriptor = accessor.calleeDescriptor as FunctionDescriptor
|
||||
val returnExpr = IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, calleeDescriptor)
|
||||
copyAllArgsToValueParams(returnExpr, accessorDescriptor)
|
||||
body.statements.add(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, accessor, returnExpr))
|
||||
|
||||
@@ -90,8 +90,10 @@ class CallGenerator(statementGenerator: StatementGenerator): StatementGeneratorE
|
||||
) =
|
||||
@Suppress("DEPRECATION")
|
||||
if (descriptor is LocalVariableDescriptor && descriptor.isDelegated) {
|
||||
val getter = context.symbolTable.referenceFunction(descriptor.getter!!)
|
||||
IrCallImpl(startOffset, endOffset, descriptor.type, getter, typeArguments, origin ?: IrStatementOrigin.GET_LOCAL_PROPERTY)
|
||||
val getterDescriptor = descriptor.getter!!
|
||||
val getterSymbol = context.symbolTable.referenceFunction(getterDescriptor.original)
|
||||
IrCallImpl(startOffset, endOffset, descriptor.type, getterSymbol, getterDescriptor,
|
||||
typeArguments, origin ?: IrStatementOrigin.GET_LOCAL_PROPERTY)
|
||||
}
|
||||
else
|
||||
IrGetValueImpl(startOffset, endOffset, context.symbolTable.referenceValue(descriptor), origin)
|
||||
@@ -100,7 +102,7 @@ class CallGenerator(statementGenerator: StatementGenerator): StatementGeneratorE
|
||||
call.callReceiver.call { dispatchReceiver, extensionReceiver ->
|
||||
val descriptor = call.descriptor as? ClassConstructorDescriptor
|
||||
?: throw AssertionError("Class constructor expected: ${call.descriptor}")
|
||||
val constructorSymbol = context.symbolTable.referenceConstructor(descriptor)
|
||||
val constructorSymbol = context.symbolTable.referenceConstructor(descriptor.original)
|
||||
val irCall = IrDelegatingConstructorCallImpl(startOffset, endOffset, constructorSymbol, getTypeArguments(call.original))
|
||||
irCall.dispatchReceiver = dispatchReceiver?.load()
|
||||
irCall.extensionReceiver = extensionReceiver?.load()
|
||||
@@ -158,20 +160,21 @@ class CallGenerator(statementGenerator: StatementGenerator): StatementGeneratorE
|
||||
}
|
||||
|
||||
private fun generateFunctionCall(
|
||||
descriptor: FunctionDescriptor,
|
||||
functionDescriptor: FunctionDescriptor,
|
||||
startOffset: Int,
|
||||
endOffset: Int,
|
||||
origin: IrStatementOrigin?,
|
||||
call: CallBuilder
|
||||
): IrExpression =
|
||||
call.callReceiver.call { dispatchReceiverValue, extensionReceiverValue ->
|
||||
val returnType = descriptor.returnType!!
|
||||
val functionSymbol = context.symbolTable.referenceFunction(descriptor)
|
||||
val returnType = functionDescriptor.returnType!!
|
||||
val functionSymbol = context.symbolTable.referenceFunction(functionDescriptor.original)
|
||||
val superQualifierSymbol = call.superQualifier?.let { context.symbolTable.referenceClass(it) }
|
||||
val irCall = IrCallImpl(
|
||||
startOffset, endOffset,
|
||||
returnType,
|
||||
functionSymbol,
|
||||
functionDescriptor,
|
||||
getTypeArguments(call.original),
|
||||
origin,
|
||||
superQualifierSymbol
|
||||
|
||||
@@ -210,7 +210,7 @@ class ClassGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGe
|
||||
val endOffset = irDelegate.endOffset
|
||||
val irBlockBody = IrBlockBodyImpl(startOffset, endOffset)
|
||||
val returnType = overridden.returnType!!
|
||||
val irCall = IrCallImpl(startOffset, endOffset, returnType, context.symbolTable.referenceFunction(overridden), null)
|
||||
val irCall = IrCallImpl(startOffset, endOffset, returnType, context.symbolTable.referenceFunction(overridden.original), overridden, null)
|
||||
irCall.dispatchReceiver =
|
||||
IrGetFieldImpl(
|
||||
startOffset, endOffset, irDelegate.symbol,
|
||||
|
||||
+7
-1
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi2ir.generators
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrErrorDeclarationImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeAliasImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
|
||||
@@ -114,7 +115,12 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
|
||||
IrDeclarationOrigin.FAKE_OVERRIDE,
|
||||
false,
|
||||
propertyDescriptor,
|
||||
null,
|
||||
if (propertyDescriptor.getter == null)
|
||||
context.symbolTable.declareField(
|
||||
ktElement.startOffsetOrUndefined, ktElement.endOffsetOrUndefined, IrDeclarationOrigin.FAKE_OVERRIDE,
|
||||
propertyDescriptor
|
||||
)
|
||||
else null,
|
||||
propertyDescriptor.getter?.let { generateFakeOverrideFunction(it, ktElement) },
|
||||
propertyDescriptor.setter?.let { generateFakeOverrideFunction(it, ktElement) }
|
||||
)
|
||||
|
||||
+12
-12
@@ -113,18 +113,18 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D
|
||||
kPropertyType: KotlinType,
|
||||
ktDelegate: KtPropertyDelegate
|
||||
): IrField {
|
||||
val irActualDelegateInitializer = generateInitializerBodyForPropertyDelegate(
|
||||
propertyDescriptor, kPropertyType, ktDelegate,
|
||||
context.symbolTable.referenceField(propertyDescriptor)
|
||||
)
|
||||
|
||||
val delegateType = irActualDelegateInitializer.expression.type
|
||||
val delegateType = getDelegatedPropertyDelegateType(propertyDescriptor, ktDelegate)
|
||||
val delegateDescriptor = createPropertyDelegateDescriptor(propertyDescriptor, delegateType, kPropertyType)
|
||||
|
||||
return context.symbolTable.declareField(
|
||||
ktDelegate.startOffset, ktDelegate.endOffset, IrDeclarationOrigin.DELEGATE,
|
||||
delegateDescriptor, irActualDelegateInitializer
|
||||
)
|
||||
delegateDescriptor
|
||||
).also { irDelegate ->
|
||||
irDelegate.initializer = generateInitializerBodyForPropertyDelegate(
|
||||
propertyDescriptor, kPropertyType, ktDelegate,
|
||||
irDelegate.symbol
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateInitializerBodyForPropertyDelegate(
|
||||
@@ -257,7 +257,7 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D
|
||||
kPropertyType: KotlinType,
|
||||
scopeOwner: IrSymbol
|
||||
): IrVariable {
|
||||
val delegateType = getLocalDelegatedPropertyDelegateType(variableDescriptor, ktDelegate)
|
||||
val delegateType = getDelegatedPropertyDelegateType(variableDescriptor, ktDelegate)
|
||||
val delegateDescriptor = createLocalPropertyDelegatedDescriptor(variableDescriptor, delegateType, kPropertyType)
|
||||
|
||||
return context.symbolTable.declareVariable(
|
||||
@@ -272,11 +272,11 @@ class DelegatedPropertyGenerator(declarationGenerator: DeclarationGenerator) : D
|
||||
}
|
||||
}
|
||||
|
||||
private fun getLocalDelegatedPropertyDelegateType(
|
||||
variableDescriptor: VariableDescriptorWithAccessors,
|
||||
private fun getDelegatedPropertyDelegateType(
|
||||
delegatedPropertyDescriptor: VariableDescriptorWithAccessors,
|
||||
ktDelegate: KtPropertyDelegate
|
||||
): KotlinType {
|
||||
val provideDelegateResolvedCall = get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, variableDescriptor)
|
||||
val provideDelegateResolvedCall = get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, delegatedPropertyDescriptor)
|
||||
return if (provideDelegateResolvedCall != null)
|
||||
provideDelegateResolvedCall.resultingDescriptor.returnType!!
|
||||
else
|
||||
|
||||
+9
-5
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.psi2ir.generators
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Mod
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
|
||||
@@ -114,11 +115,14 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
||||
}
|
||||
|
||||
private fun generateDefaultAccessorBody(ktProperty: KtElement, accessor: PropertyAccessorDescriptor, irAccessor: IrSimpleFunction) =
|
||||
when (accessor) {
|
||||
is PropertyGetterDescriptor -> generateDefaultGetterBody(ktProperty, accessor, irAccessor)
|
||||
is PropertySetterDescriptor -> generateDefaultSetterBody(ktProperty, accessor, irAccessor)
|
||||
else -> throw AssertionError("Should be getter or setter: $accessor")
|
||||
}
|
||||
if (accessor.modality == Modality.ABSTRACT)
|
||||
null
|
||||
else
|
||||
when (accessor) {
|
||||
is PropertyGetterDescriptor -> generateDefaultGetterBody(ktProperty, accessor, irAccessor)
|
||||
is PropertySetterDescriptor -> generateDefaultSetterBody(ktProperty, accessor, irAccessor)
|
||||
else -> throw AssertionError("Should be getter or setter: $accessor")
|
||||
}
|
||||
|
||||
private fun generateDefaultGetterBody(ktProperty: KtElement, getter: PropertyGetterDescriptor, irAccessor: IrSimpleFunction): IrBlockBody {
|
||||
val property = getter.correspondingProperty
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi2ir.generators
|
||||
import org.jetbrains.kotlin.builtins.ReflectionTypes
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.builders.IrGeneratorContext
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
||||
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
|
||||
|
||||
+3
-1
@@ -49,7 +49,9 @@ class LocalClassGenerator(statementGenerator: StatementGenerator): StatementGene
|
||||
IrCallImpl(
|
||||
ktObjectLiteral.startOffset, ktObjectLiteral.endOffset, objectLiteralType,
|
||||
context.symbolTable.referenceConstructor(objectConstructor),
|
||||
null, IrStatementOrigin.OBJECT_LITERAL
|
||||
objectConstructor,
|
||||
null,
|
||||
IrStatementOrigin.OBJECT_LITERAL
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.psi2ir.generators
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrDeclarationStubBuilder
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
|
||||
class ModuleDependenciesGenerator(override val context: GeneratorContext) : Generator {
|
||||
private class DependenciesCollector {
|
||||
private val modulesForDependencyDescriptors = LinkedHashSet<ModuleDescriptor>()
|
||||
private val packageFragmentsForDependencyDescriptors = LinkedHashMap<ModuleDescriptor, MutableSet<PackageFragmentDescriptor>>()
|
||||
private val topLevelDescriptors = LinkedHashMap<PackageFragmentDescriptor, MutableSet<DeclarationDescriptor>>()
|
||||
|
||||
val dependencyModules: Collection<ModuleDescriptor> get() = modulesForDependencyDescriptors
|
||||
|
||||
fun getPackageFragments(moduleDescriptor: ModuleDescriptor): Collection<PackageFragmentDescriptor> =
|
||||
packageFragmentsForDependencyDescriptors[moduleDescriptor] ?: emptyList()
|
||||
|
||||
fun getTopLevelDescriptors(packageFragmentDescriptor: PackageFragmentDescriptor): Collection<DeclarationDescriptor> =
|
||||
topLevelDescriptors[packageFragmentDescriptor] ?: emptyList()
|
||||
|
||||
fun collectTopLevelDescriptorsForUnboundSymbols(symbolTable: SymbolTable) {
|
||||
assert(symbolTable.unboundTypeParameters.isEmpty()) { "Unbound type parameters: ${symbolTable.unboundTypeParameters}" }
|
||||
assert(symbolTable.unboundValueParameters.isEmpty()) { "Unbound value parameters: ${symbolTable.unboundValueParameters}" }
|
||||
assert(symbolTable.unboundVariables.isEmpty()) { "Unbound variables: ${symbolTable.unboundVariables}" }
|
||||
|
||||
symbolTable.unboundClasses.addTopLevelDeclarations()
|
||||
symbolTable.unboundConstructors.addTopLevelDeclarations()
|
||||
symbolTable.unboundEnumEntries.addTopLevelDeclarations()
|
||||
symbolTable.unboundFields.addTopLevelDeclarations()
|
||||
symbolTable.unboundSimpleFunctions.addTopLevelDeclarations()
|
||||
}
|
||||
|
||||
private fun Collection<IrSymbol>.addTopLevelDeclarations() {
|
||||
forEach { addTopLevelDeclaration(it) }
|
||||
}
|
||||
|
||||
fun addTopLevelDeclaration(symbol: IrSymbol) {
|
||||
val descriptor = symbol.descriptor
|
||||
val topLevelDeclaration = getTopLevelDeclaration(descriptor)
|
||||
addTopLevelDescriptor(topLevelDeclaration)
|
||||
}
|
||||
|
||||
private fun getTopLevelDeclaration(descriptor: DeclarationDescriptor): DeclarationDescriptor {
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
return when (containingDeclaration) {
|
||||
is PackageFragmentDescriptor -> descriptor
|
||||
is ClassDescriptor -> getTopLevelDeclaration(containingDeclaration)
|
||||
else -> throw AssertionError("Package or class expected: $containingDeclaration")
|
||||
}
|
||||
}
|
||||
|
||||
private fun addTopLevelDescriptor(descriptor: DeclarationDescriptor) {
|
||||
val packageFragmentDescriptor = DescriptorUtils.getParentOfType(descriptor, PackageFragmentDescriptor::class.java)!!
|
||||
|
||||
val moduleDescriptor = packageFragmentDescriptor.containingDeclaration
|
||||
modulesForDependencyDescriptors.add(moduleDescriptor)
|
||||
|
||||
packageFragmentsForDependencyDescriptors.getOrPut(moduleDescriptor) { LinkedHashSet() }.add(packageFragmentDescriptor)
|
||||
topLevelDescriptors.getOrPut(packageFragmentDescriptor) { LinkedHashSet() }.add(descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) {
|
||||
val collector = DependenciesCollector()
|
||||
collector.collectTopLevelDescriptorsForUnboundSymbols(context.symbolTable)
|
||||
|
||||
collector.dependencyModules.mapTo(irModule.dependencyModules) { moduleDescriptor ->
|
||||
generateModuleStub(collector, moduleDescriptor)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateModuleStub(collector: DependenciesCollector, moduleDescriptor: ModuleDescriptor): IrModuleFragmentImpl =
|
||||
IrModuleFragmentImpl(moduleDescriptor, context.irBuiltIns).also { irDependencyModule ->
|
||||
collector.getPackageFragments(moduleDescriptor).mapTo(irDependencyModule.externalPackageFragments) { packageFragmentDescriptor ->
|
||||
generatePackageStub(packageFragmentDescriptor, collector.getTopLevelDescriptors(packageFragmentDescriptor))
|
||||
}
|
||||
}
|
||||
|
||||
private fun generatePackageStub(packageFragmentDescriptor: PackageFragmentDescriptor, topLevelDescriptors: Collection<DeclarationDescriptor>): IrExternalPackageFragment =
|
||||
context.symbolTable.declareExternalPackageFragment(packageFragmentDescriptor).also { irExternalPackageFragment ->
|
||||
topLevelDescriptors.mapTo(irExternalPackageFragment.declarations) {
|
||||
generateStub(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateStub(descriptor: DeclarationDescriptor): IrDeclaration =
|
||||
when (descriptor) {
|
||||
is ClassDescriptor ->
|
||||
if (DescriptorUtils.isEnumEntry(descriptor))
|
||||
generateEnumEntryStub(descriptor)
|
||||
else
|
||||
generateClassStub(descriptor)
|
||||
is ClassConstructorDescriptor ->
|
||||
generateConstructorStub(descriptor)
|
||||
is FunctionDescriptor ->
|
||||
generateFunctionStub(descriptor)
|
||||
is PropertyDescriptor ->
|
||||
generatePropertyStub(descriptor)
|
||||
else ->
|
||||
throw AssertionError("Unexpected top-level descriptor: $descriptor")
|
||||
}
|
||||
|
||||
private fun MemberScope.generateChildStubs(irParent: IrDeclarationContainer) {
|
||||
getContributedDescriptors().generateChildStubs(irParent)
|
||||
}
|
||||
|
||||
private fun Collection<DeclarationDescriptor>.generateChildStubs(irParent: IrDeclarationContainer) {
|
||||
mapTo(irParent.declarations) { generateStub(it) }
|
||||
}
|
||||
|
||||
private fun Collection<TypeParameterDescriptor>.generateTypeParameterStubs(irParent: IrTypeParametersContainer) {
|
||||
mapTo(irParent.typeParameters) { generateTypeParameterStub(it) }
|
||||
}
|
||||
|
||||
private fun Collection<ValueParameterDescriptor>.generateValueParametersStubs(irParent: IrFunction) {
|
||||
mapTo(irParent.valueParameters) { generateValueParameterStub(it) }
|
||||
}
|
||||
|
||||
private fun generateClassStub(classDescriptor: ClassDescriptor): IrClass =
|
||||
context.symbolTable.declareClass(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
classDescriptor
|
||||
).also { irClass ->
|
||||
classDescriptor.declaredTypeParameters.generateTypeParameterStubs(irClass)
|
||||
classDescriptor.constructors.generateChildStubs(irClass)
|
||||
classDescriptor.defaultType.memberScope.generateChildStubs(irClass)
|
||||
classDescriptor.staticScope.generateChildStubs(irClass)
|
||||
}
|
||||
|
||||
private fun generateEnumEntryStub(enumEntryDescriptor: ClassDescriptor): IrEnumEntry =
|
||||
context.symbolTable.declareEnumEntry(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
enumEntryDescriptor
|
||||
)
|
||||
|
||||
private fun generateTypeParameterStub(typeParameterDescriptor: TypeParameterDescriptor): IrTypeParameter =
|
||||
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, typeParameterDescriptor)
|
||||
|
||||
private fun generateValueParameterStub(valueParameterDescriptor: ValueParameterDescriptor): IrValueParameter =
|
||||
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, valueParameterDescriptor)
|
||||
|
||||
private fun generateConstructorStub(constructorDescriptor: ClassConstructorDescriptor): IrConstructor =
|
||||
context.symbolTable.declareConstructor(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
constructorDescriptor
|
||||
).also { irConstructor ->
|
||||
constructorDescriptor.valueParameters.generateValueParametersStubs(irConstructor)
|
||||
}
|
||||
|
||||
private fun generateFunctionStub(functionDescriptor: FunctionDescriptor): IrFunction =
|
||||
context.symbolTable.declareSimpleFunction(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
functionDescriptor
|
||||
).also { irFunction ->
|
||||
functionDescriptor.typeParameters.generateTypeParameterStubs(irFunction)
|
||||
functionDescriptor.valueParameters.generateValueParametersStubs(irFunction)
|
||||
}
|
||||
|
||||
private fun generatePropertyStub(propertyDescriptor: PropertyDescriptor): IrProperty =
|
||||
IrPropertyImpl(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
propertyDescriptor
|
||||
).also { irProperty ->
|
||||
val getterDescriptor = propertyDescriptor.getter
|
||||
if (getterDescriptor == null) {
|
||||
irProperty.backingField =
|
||||
context.symbolTable.declareField(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
|
||||
propertyDescriptor
|
||||
)
|
||||
}
|
||||
else {
|
||||
irProperty.getter = generateFunctionStub(getterDescriptor)
|
||||
}
|
||||
|
||||
irProperty.setter = propertyDescriptor.setter?.let { generateFunctionStub(it) }
|
||||
}
|
||||
}
|
||||
+14
-8
@@ -25,10 +25,20 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
class ModuleGenerator(override val context: GeneratorContext) : Generator {
|
||||
fun generateModuleFragment(ktFiles: Collection<KtFile>): IrModuleFragment =
|
||||
IrModuleFragmentImpl(context.moduleDescriptor, context.irBuiltIns,
|
||||
generateFiles(ktFiles))
|
||||
generateModuleFragmentWithoutDependencies(ktFiles).also { irModule ->
|
||||
generateUnboundSymbolsAsDependencies(irModule)
|
||||
}
|
||||
|
||||
fun generateFiles(ktFiles: Collection<KtFile>): List<IrFile> {
|
||||
fun generateModuleFragmentWithoutDependencies(ktFiles: Collection<KtFile>): IrModuleFragment =
|
||||
IrModuleFragmentImpl(context.moduleDescriptor, context.irBuiltIns).also { irModule ->
|
||||
irModule.files.addAll(generateFiles(ktFiles))
|
||||
}
|
||||
|
||||
private fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) {
|
||||
ModuleDependenciesGenerator(context).generateUnboundSymbolsAsDependencies(irModule)
|
||||
}
|
||||
|
||||
private fun generateFiles(ktFiles: Collection<KtFile>): List<IrFile> {
|
||||
val irDeclarationGenerator = DeclarationGenerator(context)
|
||||
|
||||
return ktFiles.map { ktFile ->
|
||||
@@ -36,10 +46,6 @@ class ModuleGenerator(override val context: GeneratorContext) : Generator {
|
||||
}
|
||||
}
|
||||
|
||||
fun generateSingleFileFragment(ktFile: KtFile) =
|
||||
IrModuleFragmentImpl(context.moduleDescriptor, context.irBuiltIns,
|
||||
listOf(generateSingleFile(DeclarationGenerator(context), ktFile)))
|
||||
|
||||
private fun generateSingleFile(irDeclarationGenerator: DeclarationGenerator, ktFile: KtFile): IrFileImpl {
|
||||
val irFile = createEmptyIrFile(ktFile)
|
||||
|
||||
@@ -54,7 +60,7 @@ class ModuleGenerator(override val context: GeneratorContext) : Generator {
|
||||
return irFile
|
||||
}
|
||||
|
||||
fun createEmptyIrFile(ktFile: KtFile): IrFileImpl {
|
||||
private fun createEmptyIrFile(ktFile: KtFile): IrFileImpl {
|
||||
val fileEntry = context.sourceManager.getOrCreateFileEntry(ktFile)
|
||||
val packageFragmentDescriptor = getOrFail(BindingContext.FILE_TO_PACKAGE_FRAGMENT, ktFile)
|
||||
val irFile = IrFileImpl(fileEntry, packageFragmentDescriptor)
|
||||
|
||||
@@ -66,6 +66,7 @@ class SymbolTable {
|
||||
val s = get(d)
|
||||
if (s == null) {
|
||||
val new = createSymbol().also { markAsUnbound(it) }
|
||||
set(d, new)
|
||||
return new
|
||||
}
|
||||
return s
|
||||
@@ -173,6 +174,9 @@ class SymbolTable {
|
||||
fun declareFile(fileEntry: SourceManager.FileEntry, packageFragmentDescriptor: PackageFragmentDescriptor): IrFile =
|
||||
IrFileImpl(fileEntry, IrFileSymbolImpl(packageFragmentDescriptor))
|
||||
|
||||
fun declareExternalPackageFragment(packageFragmentDescriptor: PackageFragmentDescriptor): IrExternalPackageFragment =
|
||||
IrExternalPackageFragmentImpl(IrExternalPackageFragmentSymbolImpl(packageFragmentDescriptor))
|
||||
|
||||
fun declareAnonymousInitializer(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor): IrAnonymousInitializer =
|
||||
IrAnonymousInitializerImpl(
|
||||
startOffset, endOffset, origin,
|
||||
@@ -186,7 +190,6 @@ class SymbolTable {
|
||||
{ IrClassImpl(startOffset, endOffset, origin, it) }
|
||||
)
|
||||
|
||||
|
||||
fun referenceClass(descriptor: ClassDescriptor) =
|
||||
classSymbolTable.referenced(descriptor) { IrClassSymbolImpl(descriptor) }
|
||||
|
||||
|
||||
+2
-2
@@ -31,10 +31,10 @@ class DelegatedLocalPropertyLValue(
|
||||
val origin: IrStatementOrigin? = null
|
||||
) : LValue, AssignmentReceiver {
|
||||
override fun load(): IrExpression =
|
||||
IrCallImpl(startOffset, endOffset, type, getterSymbol!!, null, origin)
|
||||
IrCallImpl(startOffset, endOffset, type, getterSymbol!!, getterSymbol.descriptor, null, origin)
|
||||
|
||||
override fun store(irExpression: IrExpression): IrExpression =
|
||||
IrCallImpl(startOffset, endOffset, type, setterSymbol!!, null, origin).apply {
|
||||
IrCallImpl(startOffset, endOffset, type, setterSymbol!!, setterSymbol.descriptor, null, origin).apply {
|
||||
putValueArgument(0, irExpression)
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ fun IrBuilderWithScope.irGet(receiver: IrExpression, getterSymbol: IrFunctionSym
|
||||
IrGetterCallImpl(startOffset, endOffset, getterSymbol, null, receiver, null, IrStatementOrigin.GET_PROPERTY)
|
||||
|
||||
fun IrBuilderWithScope.irCall(callee: IrFunctionSymbol, type: KotlinType): IrCall =
|
||||
IrCallImpl(startOffset, endOffset, type, callee, null)
|
||||
IrCallImpl(startOffset, endOffset, type, callee, callee.descriptor, null)
|
||||
|
||||
fun IrBuilderWithScope.irCallOp(callee: IrFunctionSymbol, dispatchReceiver: IrExpression, argument: IrExpression): IrCall =
|
||||
irCall(callee, callee.descriptor.returnType!!).apply {
|
||||
|
||||
@@ -31,6 +31,8 @@ interface IrDeclarationOrigin {
|
||||
object NEW_INSTANCE_RECEIVER : IrDeclarationOriginImpl("NEW_INSTANCE_RECEIVER")
|
||||
object PRIMARY_CONSTRUCTOR_PARAMETER : IrDeclarationOriginImpl("PRIMARY_CONSTRUCTOR_PARAMETER")
|
||||
object IR_TEMPORARY_VARIABLE : IrDeclarationOriginImpl("IR_TEMPORARY_VARIABLE")
|
||||
object IR_EXTERNAL_DECLARATION_STUB : IrDeclarationOriginImpl("IR_EXTERNAL_DECLARATION_STUB")
|
||||
object IR_BUILTINS_STUB : IrDeclarationOriginImpl("IR_BUILTINS_STUB")
|
||||
}
|
||||
|
||||
abstract class IrDeclarationOriginImpl(val name: String): IrDeclarationOrigin {
|
||||
|
||||
@@ -26,6 +26,6 @@ interface IrEnumEntry : IrSymbolDeclaration<IrEnumEntrySymbol> {
|
||||
override val descriptor: ClassDescriptor
|
||||
|
||||
var correspondingClass: IrClass?
|
||||
var initializerExpression: IrExpression
|
||||
var initializerExpression: IrExpression?
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,9 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
interface IrModuleFragment : IrElement {
|
||||
val descriptor: ModuleDescriptor
|
||||
val irBuiltins: IrBuiltIns
|
||||
val files: List<IrFile>
|
||||
val files: MutableList<IrFile>
|
||||
val externalPackageFragments: MutableList<IrExternalPackageFragment>
|
||||
val dependencyModules: MutableList<IrModuleFragment>
|
||||
|
||||
override val startOffset: Int get() = UNDEFINED_OFFSET
|
||||
override val endOffset: Int get() = UNDEFINED_OFFSET
|
||||
|
||||
+3
-3
@@ -46,19 +46,19 @@ class IrEnumEntryImpl(
|
||||
|
||||
override val descriptor: ClassDescriptor get() = symbol.descriptor
|
||||
override var correspondingClass: IrClass? = null
|
||||
override lateinit var initializerExpression: IrExpression
|
||||
override var initializerExpression: IrExpression? = null
|
||||
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
|
||||
return visitor.visitEnumEntry(this, data)
|
||||
}
|
||||
|
||||
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
|
||||
initializerExpression.accept(visitor, data)
|
||||
initializerExpression?.accept(visitor, data)
|
||||
correspondingClass?.accept(visitor, data)
|
||||
}
|
||||
|
||||
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
|
||||
initializerExpression = initializerExpression.transform(transformer, data)
|
||||
initializerExpression = initializerExpression?.transform(transformer, data)
|
||||
correspondingClass = correspondingClass?.transform(transformer, data) as? IrClass
|
||||
}
|
||||
}
|
||||
+5
-8
@@ -17,30 +17,27 @@
|
||||
package org.jetbrains.kotlin.ir.declarations.impl
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
|
||||
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.visitors.IrElementTransformer
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class IrModuleFragmentImpl(
|
||||
override val descriptor: ModuleDescriptor,
|
||||
override val irBuiltins: IrBuiltIns
|
||||
) : IrModuleFragment {
|
||||
constructor(descriptor: ModuleDescriptor, irBuiltins: IrBuiltIns, files: List<IrFile>) : this(descriptor, irBuiltins) {
|
||||
this.addAll(files)
|
||||
this.files.addAll(files)
|
||||
}
|
||||
|
||||
override val files: MutableList<IrFile> = ArrayList()
|
||||
|
||||
fun addFile(file: IrFile) {
|
||||
files.add(file)
|
||||
}
|
||||
override val externalPackageFragments: MutableList<IrExternalPackageFragment> = ArrayList()
|
||||
|
||||
fun addAll(newFiles: List<IrFile>) {
|
||||
files.addAll(newFiles)
|
||||
}
|
||||
override val dependencyModules: MutableList<IrModuleFragment> = ArrayList()
|
||||
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
|
||||
visitor.visitModuleFragment(this, data)
|
||||
|
||||
@@ -30,6 +30,11 @@ class IrPropertyImpl(
|
||||
override val isDelegated: Boolean,
|
||||
override val descriptor: PropertyDescriptor
|
||||
) : IrDeclarationBase(startOffset, endOffset, origin), IrProperty {
|
||||
constructor(
|
||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin,
|
||||
descriptor: PropertyDescriptor
|
||||
) : this(startOffset, endOffset, origin, descriptor.isDelegated, descriptor)
|
||||
|
||||
constructor(
|
||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, isDelegated: Boolean, descriptor: PropertyDescriptor,
|
||||
backingField: IrField?
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||
@@ -37,9 +38,7 @@ class IrBuiltIns(val builtIns: KotlinBuiltIns) {
|
||||
private val packageFragment = IrBuiltinsPackageFragmentDescriptorImpl(builtIns.builtInsModule, KOTLIN_INTERNAL_IR_FQN)
|
||||
val irBuiltInsExternalPackageFragment = IrExternalPackageFragmentImpl(IrExternalPackageFragmentSymbolImpl(packageFragment))
|
||||
|
||||
object IR_BUILTINS_STUB : IrDeclarationOriginImpl("IR_BUILTINS_STUB")
|
||||
|
||||
private val stubBuilder = IrDeclarationStubBuilder(IR_BUILTINS_STUB)
|
||||
private val stubBuilder = IrDeclarationStubBuilder(IrDeclarationOrigin.IR_BUILTINS_STUB)
|
||||
|
||||
private fun defineOperator(name: String, returnType: KotlinType, valueParameterTypes: List<KotlinType>): IrSimpleFunction {
|
||||
val operatorDescriptor = IrSimpleBuiltinOperatorDescriptorImpl(packageFragment, Name.identifier(name), returnType)
|
||||
|
||||
+2
-10
@@ -51,11 +51,7 @@ class IrDeclarationStubBuilder(val defaultOrigin: IrDeclarationOrigin) {
|
||||
private fun <T : IrTypeParametersContainer>
|
||||
T.buildTypeParameterStubs(typeParameterDescriptors: List<TypeParameterDescriptor>, origin: IrDeclarationOrigin): T =
|
||||
apply {
|
||||
typeParameterDescriptors.forEach { typeParameterDescriptor ->
|
||||
typeParameters.add(
|
||||
buildTypeParameterStub(IrTypeParameterSymbolImpl(typeParameterDescriptor), origin)
|
||||
)
|
||||
}
|
||||
typeParameterDescriptors.mapTo(typeParameters) { buildTypeParameterStub(IrTypeParameterSymbolImpl(it), origin) }
|
||||
}
|
||||
|
||||
private fun <T : IrFunction>
|
||||
@@ -66,11 +62,7 @@ class IrDeclarationStubBuilder(val defaultOrigin: IrDeclarationOrigin) {
|
||||
addIfNotNull(functionDescriptor.extensionReceiverParameter)
|
||||
addAll(functionDescriptor.valueParameters)
|
||||
}
|
||||
valueParameterDescriptors.forEach { valueParameterDescriptor ->
|
||||
valueParameters.add(
|
||||
buildValueParameterStub(IrValueParameterSymbolImpl(valueParameterDescriptor), origin)
|
||||
)
|
||||
}
|
||||
valueParameterDescriptors.mapTo(valueParameters) { buildValueParameterStub(IrValueParameterSymbolImpl(it), origin) }
|
||||
}
|
||||
|
||||
private val DeclarationDescriptorWithSource.startOffset
|
||||
|
||||
@@ -32,6 +32,7 @@ class IrCallImpl(
|
||||
endOffset: Int,
|
||||
type: KotlinType,
|
||||
override val symbol: IrFunctionSymbol,
|
||||
override val descriptor: FunctionDescriptor,
|
||||
typeArguments: Map<TypeParameterDescriptor, KotlinType>?,
|
||||
override val origin: IrStatementOrigin? = null,
|
||||
override val superQualifierSymbol: IrClassSymbol? = null
|
||||
@@ -42,14 +43,15 @@ class IrCallImpl(
|
||||
constructor(
|
||||
startOffset: Int,
|
||||
endOffset: Int,
|
||||
calleeDescriptor: CallableMemberDescriptor,
|
||||
calleeDescriptor: FunctionDescriptor,
|
||||
typeArguments: Map<TypeParameterDescriptor, KotlinType>? = null,
|
||||
origin: IrStatementOrigin? = null,
|
||||
superQualifierDescriptor: ClassDescriptor? = null
|
||||
) : this(
|
||||
startOffset, endOffset,
|
||||
calleeDescriptor.returnType!!,
|
||||
createFunctionSymbol(calleeDescriptor),
|
||||
createFunctionSymbol(calleeDescriptor.original),
|
||||
calleeDescriptor,
|
||||
typeArguments, origin,
|
||||
createClassSymbolOrNull(superQualifierDescriptor)
|
||||
)
|
||||
@@ -57,12 +59,16 @@ class IrCallImpl(
|
||||
constructor(
|
||||
startOffset: Int, endOffset: Int,
|
||||
symbol: IrFunctionSymbol,
|
||||
descriptor: FunctionDescriptor,
|
||||
typeArguments: Map<TypeParameterDescriptor, KotlinType>? = null,
|
||||
origin: IrStatementOrigin? = null,
|
||||
superQualifierSymbol: IrClassSymbol? = null
|
||||
) : this(startOffset, endOffset, symbol.descriptor.returnType!!, symbol, typeArguments, origin, superQualifierSymbol)
|
||||
) : this(startOffset, endOffset, symbol.descriptor.returnType!!, symbol, descriptor, typeArguments, origin, superQualifierSymbol)
|
||||
|
||||
constructor(startOffset: Int, endOffset: Int, symbol: IrFunctionSymbol) :
|
||||
this(startOffset, endOffset, symbol, symbol.descriptor)
|
||||
|
||||
|
||||
override val descriptor: FunctionDescriptor = symbol.descriptor
|
||||
override val superQualifier: ClassDescriptor? = superQualifierSymbol?.descriptor
|
||||
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
|
||||
|
||||
@@ -32,7 +32,7 @@ abstract class IrBindableSymbolBase<out D : DeclarationDescriptor, B : IrSymbolO
|
||||
if (_owner == null)
|
||||
_owner = owner
|
||||
else
|
||||
throw IllegalStateException("Symbol for $descriptor is already bound")
|
||||
throw IllegalStateException("${javaClass.simpleName} for $descriptor is already bound")
|
||||
}
|
||||
|
||||
override val isBound: Boolean
|
||||
|
||||
@@ -159,7 +159,7 @@ class DeepCopyIrTree(private val symbolsRemapper: DeepCopySymbolsRemapper) : IrE
|
||||
symbolsRemapper.getDeclaredEnumEntry(declaration.symbol)
|
||||
).apply {
|
||||
correspondingClass = declaration.correspondingClass?.transform()
|
||||
initializerExpression = declaration.initializerExpression.transform()
|
||||
initializerExpression = declaration.initializerExpression?.transform()
|
||||
}
|
||||
|
||||
override fun visitAnonymousInitializer(declaration: IrAnonymousInitializer): IrAnonymousInitializer =
|
||||
@@ -317,6 +317,7 @@ class DeepCopyIrTree(private val symbolsRemapper: DeepCopySymbolsRemapper) : IrE
|
||||
expression.startOffset, expression.endOffset,
|
||||
expression.type,
|
||||
symbolsRemapper.getReferencedFunction(expression.symbol),
|
||||
expression.descriptor, // TODO substitute referenced descriptor
|
||||
expression.getTypeArgumentsMap(),
|
||||
mapStatementOrigin(expression.origin),
|
||||
symbolsRemapper.getReferencedClassOrNull(expression.superQualifierSymbol)
|
||||
|
||||
@@ -54,6 +54,13 @@ class DumpIrTreeVisitor(out: Appendable): IrElementVisitor<Unit, String> {
|
||||
element.dumpLabeledSubTree(data)
|
||||
}
|
||||
|
||||
override fun visitModuleFragment(declaration: IrModuleFragment, data: String) {
|
||||
declaration.dumpLabeledElementWith(data) {
|
||||
declaration.files.dumpElements()
|
||||
declaration.externalPackageFragments.dumpElements()
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitFile(declaration: IrFile, data: String) {
|
||||
declaration.dumpLabeledElementWith(data) {
|
||||
if (declaration.fileAnnotations.isNotEmpty()) {
|
||||
@@ -117,7 +124,7 @@ class DumpIrTreeVisitor(out: Appendable): IrElementVisitor<Unit, String> {
|
||||
|
||||
override fun visitEnumEntry(declaration: IrEnumEntry, data: String) {
|
||||
declaration.dumpLabeledElementWith(data) {
|
||||
declaration.initializerExpression.accept(this, "init")
|
||||
declaration.initializerExpression?.accept(this, "init")
|
||||
declaration.correspondingClass?.accept(this, "class")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user