diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleDependenciesGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleDependenciesGenerator.kt deleted file mode 100644 index bca38e42168..00000000000 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleDependenciesGenerator.kt +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.ClassDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor -import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin -import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment -import org.jetbrains.kotlin.ir.declarations.IrModuleFragment -import org.jetbrains.kotlin.ir.symbols.IrSymbol -import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator -import org.jetbrains.kotlin.ir.util.SymbolTable -import org.jetbrains.kotlin.resolve.DescriptorUtils - -class ModuleDependenciesGenerator(override val context: GeneratorContext) : Generator { - private val stubGenerator = DeclarationStubGenerator(context.symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB) - - private class DependenciesCollector { - private val modulesForDependencyDescriptors = LinkedHashSet() - private val packageFragmentsForDependencyDescriptors = LinkedHashMap>() - private val topLevelDescriptors = LinkedHashMap>() - - val dependencyModules: Collection get() = modulesForDependencyDescriptors - - fun getPackageFragments(moduleDescriptor: ModuleDescriptor): Collection = - packageFragmentsForDependencyDescriptors[moduleDescriptor] ?: emptyList() - - fun getTopLevelDescriptors(packageFragmentDescriptor: PackageFragmentDescriptor): Collection = - 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.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): IrModuleFragment = - stubGenerator.generateEmptyModuleFragmentStub(moduleDescriptor, context.irBuiltIns).also { irDependencyModule -> - collector.getPackageFragments(moduleDescriptor).mapTo(irDependencyModule.externalPackageFragments) { packageFragmentDescriptor -> - generatePackageStub(packageFragmentDescriptor, collector.getTopLevelDescriptors(packageFragmentDescriptor)) - } - } - - private fun generatePackageStub(packageFragmentDescriptor: PackageFragmentDescriptor, topLevelDescriptors: Collection): IrExternalPackageFragment = - stubGenerator.generateEmptyExternalPackageFragmentStub(packageFragmentDescriptor).also { irExternalPackageFragment -> - topLevelDescriptors.mapTo(irExternalPackageFragment.declarations) { - stubGenerator.generateMemberStub(it) - } - } - -} \ No newline at end of file diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt index 9c4d215750a..d90e5d223d1 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleGenerator.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl +import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.BindingContext @@ -35,7 +36,7 @@ class ModuleGenerator(override val context: GeneratorContext) : Generator { } private fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) { - ModuleDependenciesGenerator(context).generateUnboundSymbolsAsDependencies(irModule) + ExternalDependenciesGenerator(context.symbolTable, context.irBuiltIns).generateUnboundSymbolsAsDependencies(irModule) } private fun generateFiles(ktFiles: Collection): List { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DependenciesCollector.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DependenciesCollector.kt new file mode 100644 index 00000000000..7bf85f75c69 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DependenciesCollector.kt @@ -0,0 +1,79 @@ +/* + * 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.ir.util + +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.symbols.IrSymbol +import org.jetbrains.kotlin.resolve.DescriptorUtils + +class DependenciesCollector { + private val modulesForDependencyDescriptors = LinkedHashSet() + private val packageFragmentsForDependencyDescriptors = LinkedHashMap>() + private val topLevelDescriptors = LinkedHashMap>() + + val dependencyModules: Collection get() = modulesForDependencyDescriptors + + fun getPackageFragments(moduleDescriptor: ModuleDescriptor): Collection = + packageFragmentsForDependencyDescriptors[moduleDescriptor] ?: emptyList() + + fun getTopLevelDescriptors(packageFragmentDescriptor: PackageFragmentDescriptor): Collection = + 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.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) + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ExternalDependenciesGenerator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ExternalDependenciesGenerator.kt new file mode 100644 index 00000000000..0c912268d4a --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/ExternalDependenciesGenerator.kt @@ -0,0 +1,53 @@ +/* + * 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.ir.util + +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin +import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment +import org.jetbrains.kotlin.ir.declarations.IrModuleFragment +import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns + +class ExternalDependenciesGenerator(val symbolTable: SymbolTable, val irBuiltIns: IrBuiltIns) { + private val stubGenerator = DeclarationStubGenerator(symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB) + + fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment) { + val collector = DependenciesCollector() + collector.collectTopLevelDescriptorsForUnboundSymbols(symbolTable) + + collector.dependencyModules.mapTo(irModule.dependencyModules) { moduleDescriptor -> + generateModuleStub(collector, moduleDescriptor) + } + } + + private fun generateModuleStub(collector: DependenciesCollector, moduleDescriptor: ModuleDescriptor): IrModuleFragment = + stubGenerator.generateEmptyModuleFragmentStub(moduleDescriptor, irBuiltIns).also { irDependencyModule -> + collector.getPackageFragments(moduleDescriptor).mapTo(irDependencyModule.externalPackageFragments) { packageFragmentDescriptor -> + generatePackageStub(packageFragmentDescriptor, collector.getTopLevelDescriptors(packageFragmentDescriptor)) + } + } + + private fun generatePackageStub(packageFragmentDescriptor: PackageFragmentDescriptor, topLevelDescriptors: Collection): IrExternalPackageFragment = + stubGenerator.generateEmptyExternalPackageFragmentStub(packageFragmentDescriptor).also { irExternalPackageFragment -> + topLevelDescriptors.mapTo(irExternalPackageFragment.declarations) { + stubGenerator.generateMemberStub(it) + } + } + +} \ No newline at end of file