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
+ }
0 commit comments