Use DeclarationStubGenerator for built-ins initialization

This commit is contained in:
Dmitry Petrov
2017-05-23 16:06:46 +03:00
parent ec9427a36b
commit 952f53d37c
4 changed files with 18 additions and 111 deletions
@@ -20,6 +20,7 @@ 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
@@ -28,7 +29,7 @@ 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)
private val stubGenerator = DeclarationStubGenerator(context.symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB)
private class DependenciesCollector {
private val modulesForDependencyDescriptors = LinkedHashSet<ModuleDescriptor>()
@@ -23,11 +23,11 @@ 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
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
@@ -38,7 +38,7 @@ class IrBuiltIns(val builtIns: KotlinBuiltIns) {
private val packageFragment = IrBuiltinsPackageFragmentDescriptorImpl(builtIns.builtInsModule, KOTLIN_INTERNAL_IR_FQN)
val irBuiltInsExternalPackageFragment = IrExternalPackageFragmentImpl(IrExternalPackageFragmentSymbolImpl(packageFragment))
private val stubBuilder = IrDeclarationStubBuilder(IrDeclarationOrigin.IR_BUILTINS_STUB)
private val stubBuilder = DeclarationStubGenerator(SymbolTable(), IrDeclarationOrigin.IR_BUILTINS_STUB)
private fun defineOperator(name: String, returnType: KotlinType, valueParameterTypes: List<KotlinType>): IrSimpleFunction {
val operatorDescriptor = IrSimpleBuiltinOperatorDescriptorImpl(packageFragment, Name.identifier(name), returnType)
@@ -51,7 +51,7 @@ class IrBuiltIns(val builtIns: KotlinBuiltIns) {
}
private fun addStubToPackageFragment(descriptor: SimpleFunctionDescriptor): IrSimpleFunction {
val irSimpleFunction = stubBuilder.buildSimpleFunctionStub(IrSimpleFunctionSymbolImpl(descriptor))
val irSimpleFunction = stubBuilder.generateFunctionStub(descriptor)
irBuiltInsExternalPackageFragment.declarations.add(irSimpleFunction)
return irSimpleFunction
}
@@ -1,76 +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.ir.descriptors
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.ParameterDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class IrDeclarationStubBuilder(val defaultOrigin: IrDeclarationOrigin) {
fun buildSimpleFunctionStub(symbol: IrSimpleFunctionSymbol, origin: IrDeclarationOrigin = defaultOrigin): IrSimpleFunction =
IrFunctionImpl(symbol.descriptor.startOffset, symbol.descriptor.endOffset, origin, symbol)
.buildTypeParameterStubs(symbol.descriptor.typeParameters, origin)
.buildValueParameterStubs(symbol.descriptor, origin)
fun buildTypeParameterStub(symbol: IrTypeParameterSymbol, origin: IrDeclarationOrigin = defaultOrigin): IrTypeParameter =
IrTypeParameterImpl(symbol.descriptor.startOffset, symbol.descriptor.endOffset, origin, symbol)
fun buildValueParameterStub(symbol: IrValueParameterSymbol, origin: IrDeclarationOrigin = defaultOrigin): IrValueParameter =
IrValueParameterImpl(symbol.descriptor.startOffset, symbol.descriptor.endOffset, origin, symbol)
private fun <T : IrTypeParametersContainer>
T.buildTypeParameterStubs(typeParameterDescriptors: List<TypeParameterDescriptor>, origin: IrDeclarationOrigin): T =
apply {
typeParameterDescriptors.mapTo(typeParameters) { buildTypeParameterStub(IrTypeParameterSymbolImpl(it), origin) }
}
private fun <T : IrFunction>
T.buildValueParameterStubs(functionDescriptor: FunctionDescriptor, origin: IrDeclarationOrigin): T =
apply {
val valueParameterDescriptors = ArrayList<ParameterDescriptor>(functionDescriptor.valueParameters.size + 2).apply {
addIfNotNull(functionDescriptor.dispatchReceiverParameter)
addIfNotNull(functionDescriptor.extensionReceiverParameter)
addAll(functionDescriptor.valueParameters)
}
valueParameterDescriptors.mapTo(valueParameters) { buildValueParameterStub(IrValueParameterSymbolImpl(it), origin) }
}
private val DeclarationDescriptorWithSource.startOffset
get() = psiElement?.startOffset ?: UNDEFINED_OFFSET
private val DeclarationDescriptorWithSource.endOffset
get() = psiElement?.startOffset ?: UNDEFINED_OFFSET
private val DeclarationDescriptorWithSource.psiElement: PsiElement?
get() = source.safeAs<PsiSourceElement>()?.psi
}
@@ -31,7 +31,10 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.resolve.scopes.MemberScope
class DeclarationStubGenerator(val symbolTable: SymbolTable) {
class DeclarationStubGenerator(
val symbolTable: SymbolTable,
val origin: IrDeclarationOrigin
) {
fun generateEmptyModuleFragmentStub(descriptor: ModuleDescriptor, irBuiltIns: IrBuiltIns): IrModuleFragment =
IrModuleFragmentImpl(descriptor, irBuiltIns)
@@ -56,17 +59,11 @@ class DeclarationStubGenerator(val symbolTable: SymbolTable) {
}
fun generatePropertyStub(descriptor: PropertyDescriptor): IrProperty =
IrPropertyImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
).also { irProperty ->
IrPropertyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irProperty ->
val getterDescriptor = descriptor.getter
if (getterDescriptor == null) {
irProperty.backingField =
symbolTable.declareField(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
)
symbolTable.declareField(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor)
}
else {
irProperty.getter = generateFunctionStub(getterDescriptor)
@@ -76,19 +73,13 @@ class DeclarationStubGenerator(val symbolTable: SymbolTable) {
}
fun generateFunctionStub(descriptor: FunctionDescriptor): IrSimpleFunction =
symbolTable.declareSimpleFunction(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
).also { irFunction ->
symbolTable.declareSimpleFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irFunction ->
generateTypeParameterStubs(descriptor.typeParameters, irFunction)
generateValueParametersStubs(descriptor.valueParameters, irFunction)
}
fun generateConstructorStub(descriptor: ClassConstructorDescriptor): IrConstructor =
symbolTable.declareConstructor(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
).also { irConstructor ->
symbolTable.declareConstructor(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irConstructor ->
generateValueParametersStubs(descriptor.valueParameters, irConstructor)
}
@@ -97,10 +88,7 @@ class DeclarationStubGenerator(val symbolTable: SymbolTable) {
}
fun generateValueParameterStub(descriptor: ValueParameterDescriptor): IrValueParameter =
IrValueParameterImpl(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
).also { irValueParameter ->
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irValueParameter ->
if (descriptor.declaresDefaultValue()) {
irValueParameter.defaultValue =
IrExpressionBodyImpl(IrErrorExpressionImpl(
@@ -111,10 +99,7 @@ class DeclarationStubGenerator(val symbolTable: SymbolTable) {
}
fun generateClassStub(descriptor: ClassDescriptor): IrClass =
symbolTable.declareClass(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
).also { irClass ->
symbolTable.declareClass(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irClass ->
generateTypeParameterStubs(descriptor.declaredTypeParameters, irClass)
generateChildStubs(descriptor.constructors, irClass)
generateMemberStubs(descriptor.defaultType.memberScope, irClass)
@@ -122,17 +107,14 @@ class DeclarationStubGenerator(val symbolTable: SymbolTable) {
}
fun generateEnumEntryStub(descriptor: ClassDescriptor): IrEnumEntry =
symbolTable.declareEnumEntry(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB,
descriptor
)
symbolTable.declareEnumEntry(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor)
fun generateTypeParameterStubs(typeParameters: List<TypeParameterDescriptor>, container: IrTypeParametersContainer) {
typeParameters.mapTo(container.typeParameters) { generateTypeParameterStub(it) }
}
fun generateTypeParameterStub(descriptor: TypeParameterDescriptor): IrTypeParameter =
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, descriptor)
IrTypeParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor)
fun generateMemberStubs(memberScope: MemberScope, container: IrDeclarationContainer) {
generateChildStubs(memberScope.getContributedDescriptors(), container)