Skip to content

Commit 6fd762c

Browse files
committed
Provide DeclarationStubGenerator as a separate class in IR utilities
1 parent 7204a92 commit 6fd762c

File tree

4 files changed

+159
-107
lines changed

4 files changed

+159
-107
lines changed

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ClassGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.psi.KtDelegatedSuperTypeEntry
2828
import org.jetbrains.kotlin.psi.KtEnumEntry
2929
import org.jetbrains.kotlin.psi.psiUtil.endOffset
3030
import org.jetbrains.kotlin.psi.psiUtil.startOffset
31-
import org.jetbrains.kotlin.psi2ir.StableDescriptorsComparator
31+
import org.jetbrains.kotlin.ir.util.StableDescriptorsComparator
3232
import org.jetbrains.kotlin.resolve.BindingContext
3333
import org.jetbrains.kotlin.resolve.DescriptorUtils
3434
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ModuleDependenciesGenerator.kt

Lines changed: 13 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
package org.jetbrains.kotlin.psi2ir.generators
1818

19-
import org.jetbrains.kotlin.descriptors.*
20-
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
21-
import org.jetbrains.kotlin.ir.declarations.*
22-
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
23-
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
24-
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
25-
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
19+
import org.jetbrains.kotlin.descriptors.ClassDescriptor
20+
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
21+
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
22+
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
23+
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
24+
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
2625
import org.jetbrains.kotlin.ir.symbols.IrSymbol
26+
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
2727
import org.jetbrains.kotlin.ir.util.SymbolTable
28-
import org.jetbrains.kotlin.psi2ir.StableDescriptorsComparator
2928
import org.jetbrains.kotlin.resolve.DescriptorUtils
30-
import org.jetbrains.kotlin.resolve.scopes.MemberScope
3129

3230
class ModuleDependenciesGenerator(override val context: GeneratorContext) : Generator {
31+
private val stubGenerator = DeclarationStubGenerator(context.symbolTable)
32+
3333
private class DependenciesCollector {
3434
private val modulesForDependencyDescriptors = LinkedHashSet<ModuleDescriptor>()
3535
private val packageFragmentsForDependencyDescriptors = LinkedHashMap<ModuleDescriptor, MutableSet<PackageFragmentDescriptor>>()
@@ -94,110 +94,18 @@ class ModuleDependenciesGenerator(override val context: GeneratorContext) : Gene
9494
}
9595
}
9696

97-
private fun generateModuleStub(collector: DependenciesCollector, moduleDescriptor: ModuleDescriptor): IrModuleFragmentImpl =
98-
IrModuleFragmentImpl(moduleDescriptor, context.irBuiltIns).also { irDependencyModule ->
97+
private fun generateModuleStub(collector: DependenciesCollector, moduleDescriptor: ModuleDescriptor): IrModuleFragment =
98+
stubGenerator.generateEmptyModuleFragmentStub(moduleDescriptor, context.irBuiltIns).also { irDependencyModule ->
9999
collector.getPackageFragments(moduleDescriptor).mapTo(irDependencyModule.externalPackageFragments) { packageFragmentDescriptor ->
100100
generatePackageStub(packageFragmentDescriptor, collector.getTopLevelDescriptors(packageFragmentDescriptor))
101101
}
102102
}
103103

104104
private fun generatePackageStub(packageFragmentDescriptor: PackageFragmentDescriptor, topLevelDescriptors: Collection<DeclarationDescriptor>): IrExternalPackageFragment =
105-
context.symbolTable.declareExternalPackageFragment(packageFragmentDescriptor).also { irExternalPackageFragment ->
105+
stubGenerator.generateEmptyExternalPackageFragmentStub(packageFragmentDescriptor).also { irExternalPackageFragment ->
106106
topLevelDescriptors.mapTo(irExternalPackageFragment.declarations) {
107-
generateStub(it)
107+
stubGenerator.generateMemberStub(it)
108108
}
109109
}
110110

111-
private fun generateStub(descriptor: DeclarationDescriptor): IrDeclaration =
112-
when (descriptor) {
113-
is ClassDescriptor ->
114-
if (DescriptorUtils.isEnumEntry(descriptor))
115-
generateEnumEntryStub(descriptor)
116-
else
117-
generateClassStub(descriptor)
118-
is ClassConstructorDescriptor ->
119-
generateConstructorStub(descriptor)
120-
is FunctionDescriptor ->
121-
generateFunctionStub(descriptor)
122-
is PropertyDescriptor ->
123-
generatePropertyStub(descriptor)
124-
else ->
125-
throw AssertionError("Unexpected top-level descriptor: $descriptor")
126-
}
127-
128-
private fun MemberScope.generateChildStubs(irParent: IrDeclarationContainer) {
129-
getContributedDescriptors().sortedWith(StableDescriptorsComparator).generateChildStubs (irParent)
130-
}
131-
132-
private fun Collection<DeclarationDescriptor>.generateChildStubs(irParent: IrDeclarationContainer) {
133-
mapTo(irParent.declarations) { generateStub(it) }
134-
}
135-
136-
private fun Collection<TypeParameterDescriptor>.generateTypeParameterStubs(irParent: IrTypeParametersContainer) {
137-
mapTo(irParent.typeParameters) { generateTypeParameterStub(it) }
138-
}
139-
140-
private fun Collection<ValueParameterDescriptor>.generateValueParametersStubs(irParent: IrFunction) {
141-
mapTo(irParent.valueParameters) { generateValueParameterStub(it) }
142-
}
143-
144-
private fun generateClassStub(classDescriptor: ClassDescriptor): IrClass =
145-
context.symbolTable.declareClass(
146-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
147-
classDescriptor
148-
).also { irClass ->
149-
classDescriptor.declaredTypeParameters.generateTypeParameterStubs(irClass)
150-
classDescriptor.constructors.generateChildStubs(irClass)
151-
classDescriptor.defaultType.memberScope.generateChildStubs(irClass)
152-
classDescriptor.staticScope.generateChildStubs(irClass)
153-
}
154-
155-
private fun generateEnumEntryStub(enumEntryDescriptor: ClassDescriptor): IrEnumEntry =
156-
context.symbolTable.declareEnumEntry(
157-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
158-
enumEntryDescriptor
159-
)
160-
161-
private fun generateTypeParameterStub(typeParameterDescriptor: TypeParameterDescriptor): IrTypeParameter =
162-
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, typeParameterDescriptor)
163-
164-
private fun generateValueParameterStub(valueParameterDescriptor: ValueParameterDescriptor): IrValueParameter =
165-
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, valueParameterDescriptor)
166-
167-
private fun generateConstructorStub(constructorDescriptor: ClassConstructorDescriptor): IrConstructor =
168-
context.symbolTable.declareConstructor(
169-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
170-
constructorDescriptor
171-
).also { irConstructor ->
172-
constructorDescriptor.valueParameters.generateValueParametersStubs(irConstructor)
173-
}
174-
175-
private fun generateFunctionStub(functionDescriptor: FunctionDescriptor): IrFunction =
176-
context.symbolTable.declareSimpleFunction(
177-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
178-
functionDescriptor
179-
).also { irFunction ->
180-
functionDescriptor.typeParameters.generateTypeParameterStubs(irFunction)
181-
functionDescriptor.valueParameters.generateValueParametersStubs(irFunction)
182-
}
183-
184-
private fun generatePropertyStub(propertyDescriptor: PropertyDescriptor): IrProperty =
185-
IrPropertyImpl(
186-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
187-
propertyDescriptor
188-
).also { irProperty ->
189-
val getterDescriptor = propertyDescriptor.getter
190-
if (getterDescriptor == null) {
191-
irProperty.backingField =
192-
context.symbolTable.declareField(
193-
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
194-
propertyDescriptor
195-
)
196-
}
197-
else {
198-
irProperty.getter = generateFunctionStub(getterDescriptor)
199-
}
200-
201-
irProperty.setter = propertyDescriptor.setter?.let { generateFunctionStub(it) }
202-
}
203111
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2010-2017 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.ir.util
18+
19+
import org.jetbrains.kotlin.descriptors.*
20+
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
21+
import org.jetbrains.kotlin.ir.declarations.*
22+
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
23+
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
24+
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
25+
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
26+
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
27+
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
28+
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
29+
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
30+
import org.jetbrains.kotlin.resolve.DescriptorUtils
31+
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
32+
import org.jetbrains.kotlin.resolve.scopes.MemberScope
33+
34+
class DeclarationStubGenerator(val symbolTable: SymbolTable) {
35+
fun generateEmptyModuleFragmentStub(descriptor: ModuleDescriptor, irBuiltIns: IrBuiltIns): IrModuleFragment =
36+
IrModuleFragmentImpl(descriptor, irBuiltIns)
37+
38+
fun generateEmptyExternalPackageFragmentStub(descriptor: PackageFragmentDescriptor): IrExternalPackageFragment =
39+
symbolTable.declareExternalPackageFragment(descriptor)
40+
41+
fun generateMemberStub(descriptor: DeclarationDescriptor): IrDeclaration =
42+
when (descriptor) {
43+
is ClassDescriptor ->
44+
if (DescriptorUtils.isEnumEntry(descriptor))
45+
generateEnumEntryStub(descriptor)
46+
else
47+
generateClassStub(descriptor)
48+
is ClassConstructorDescriptor ->
49+
generateConstructorStub(descriptor)
50+
is FunctionDescriptor ->
51+
generateFunctionStub(descriptor)
52+
is PropertyDescriptor ->
53+
generatePropertyStub(descriptor)
54+
else ->
55+
throw AssertionError("Unexpected member descriptor: $descriptor")
56+
}
57+
58+
fun generatePropertyStub(descriptor: PropertyDescriptor): IrProperty =
59+
IrPropertyImpl(
60+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
61+
descriptor
62+
).also { irProperty ->
63+
val getterDescriptor = descriptor.getter
64+
if (getterDescriptor == null) {
65+
irProperty.backingField =
66+
symbolTable.declareField(
67+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
68+
descriptor
69+
)
70+
}
71+
else {
72+
irProperty.getter = generateFunctionStub(getterDescriptor)
73+
}
74+
75+
irProperty.setter = descriptor.setter?.let { generateFunctionStub(it) }
76+
}
77+
78+
fun generateFunctionStub(descriptor: FunctionDescriptor): IrSimpleFunction =
79+
symbolTable.declareSimpleFunction(
80+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
81+
descriptor
82+
).also { irFunction ->
83+
generateTypeParameterStubs(descriptor.typeParameters, irFunction)
84+
generateValueParametersStubs(descriptor.valueParameters, irFunction)
85+
}
86+
87+
fun generateConstructorStub(descriptor: ClassConstructorDescriptor): IrConstructor =
88+
symbolTable.declareConstructor(
89+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
90+
descriptor
91+
).also { irConstructor ->
92+
generateValueParametersStubs(descriptor.valueParameters, irConstructor)
93+
}
94+
95+
fun generateValueParametersStubs(valueParameters: Collection<ValueParameterDescriptor>, function: IrFunction) {
96+
valueParameters.mapTo(function.valueParameters) { generateValueParameterStub(it) }
97+
}
98+
99+
fun generateValueParameterStub(descriptor: ValueParameterDescriptor): IrValueParameter =
100+
IrValueParameterImpl(
101+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
102+
descriptor
103+
).also { irValueParameter ->
104+
if (descriptor.declaresDefaultValue()) {
105+
irValueParameter.defaultValue =
106+
IrExpressionBodyImpl(IrErrorExpressionImpl(
107+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type,
108+
"Stub expression for default value of ${descriptor.name}"
109+
))
110+
}
111+
}
112+
113+
fun generateClassStub(descriptor: ClassDescriptor): IrClass =
114+
symbolTable.declareClass(
115+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
116+
descriptor
117+
).also { irClass ->
118+
generateTypeParameterStubs(descriptor.declaredTypeParameters, irClass)
119+
generateChildStubs(descriptor.constructors, irClass)
120+
generateMemberStubs(descriptor.defaultType.memberScope, irClass)
121+
generateMemberStubs(descriptor.staticScope, irClass)
122+
}
123+
124+
fun generateEnumEntryStub(descriptor: ClassDescriptor): IrEnumEntry =
125+
symbolTable.declareEnumEntry(
126+
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
127+
descriptor
128+
)
129+
130+
fun generateTypeParameterStubs(typeParameters: List<TypeParameterDescriptor>, container: IrTypeParametersContainer) {
131+
typeParameters.mapTo(container.typeParameters) { generateTypeParameterStub(it) }
132+
}
133+
134+
fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter =
135+
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, descriptor)
136+
137+
fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) {
138+
generateChildStubs(memberScope.getContributedDescriptors(), container)
139+
}
140+
141+
fun generateChildStubs(descriptors: Collection<DeclarationDescriptor>, container: IrDeclarationContainer) {
142+
descriptors.sortedWith(StableDescriptorsComparator).mapTo(container.declarations) { generateMemberStub(it) }
143+
}
144+
}

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/StableDescriptorsComparator.kt renamed to compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/StableDescriptorsComparator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.jetbrains.kotlin.psi2ir
17+
package org.jetbrains.kotlin.ir.util
1818

1919
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
2020
import org.jetbrains.kotlin.renderer.ClassifierNamePolicy

0 commit comments

Comments
 (0)