Minor changes in preparation for IR deserialization.
This commit is contained in:
committed by
alexander-gorshenev
parent
01ab4965b1
commit
cea32bd4e3
@@ -16,10 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.builders
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
|
||||
@@ -29,8 +26,10 @@ import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptor
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.createFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
|
||||
@@ -90,6 +89,26 @@ class Scope(val scopeOwnerSymbol: IrSymbol) {
|
||||
parent = getLocalDeclarationParent()
|
||||
}
|
||||
}
|
||||
|
||||
fun createTemporaryVariableWithGivenDescriptor(
|
||||
irExpression: IrExpression,
|
||||
nameHint: String? = null,
|
||||
isMutable: Boolean = false,
|
||||
origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE,
|
||||
descriptor: VariableDescriptor
|
||||
): IrVariable {
|
||||
return IrVariableImpl(
|
||||
irExpression.startOffset, irExpression.endOffset, origin,
|
||||
IrVariableSymbolImpl(descriptor),
|
||||
Name.identifier(getNameForTemporary(nameHint)),
|
||||
irExpression.type,
|
||||
isVar = isMutable,
|
||||
isConst = false,
|
||||
isLateinit = false
|
||||
).also {
|
||||
it.initializer = irExpression
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
||||
|
||||
@@ -42,10 +42,10 @@ val IrType.classifierOrFail: IrClassifierSymbol
|
||||
val IrType.classifierOrNull: IrClassifierSymbol?
|
||||
get() = safeAs<IrSimpleType>()?.classifier
|
||||
|
||||
fun IrType.makeNotNull() =
|
||||
fun IrType.makeNotNull(addKotlinType:Boolean = true) =
|
||||
if (this is IrSimpleType && this.hasQuestionMark)
|
||||
IrSimpleTypeImpl(
|
||||
makeKotlinType(classifier, arguments, false),
|
||||
if (addKotlinType) makeKotlinType(classifier, arguments, false) else null,
|
||||
classifier,
|
||||
false,
|
||||
arguments,
|
||||
@@ -55,10 +55,10 @@ fun IrType.makeNotNull() =
|
||||
else
|
||||
this
|
||||
|
||||
fun IrType.makeNullable() =
|
||||
fun IrType.makeNullable(addKotlinType:Boolean = true) =
|
||||
if (this is IrSimpleType && !this.hasQuestionMark)
|
||||
IrSimpleTypeImpl(
|
||||
makeKotlinType(classifier, arguments, true),
|
||||
if (addKotlinType) makeKotlinType(classifier, arguments, true) else null,
|
||||
classifier,
|
||||
true,
|
||||
arguments,
|
||||
|
||||
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
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.IrFieldImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.lazy.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
|
||||
@@ -34,7 +36,8 @@ class DeclarationStubGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
val origin: IrDeclarationOrigin,
|
||||
val languageVersionSettings: LanguageVersionSettings
|
||||
val languageVersionSettings: LanguageVersionSettings,
|
||||
val deserializer: IrDeserializer? = null
|
||||
) {
|
||||
|
||||
private val lazyTable = symbolTable.lazyWrapper
|
||||
@@ -83,6 +86,7 @@ class DeclarationStubGenerator(
|
||||
descriptor: PropertyDescriptor,
|
||||
bindingContext: BindingContext? = null
|
||||
): IrProperty = symbolTable.referenceProperty(descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(descriptor) as IrProperty? ?:
|
||||
IrLazyProperty(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor,
|
||||
this, typeTranslator, bindingContext
|
||||
@@ -106,8 +110,10 @@ class DeclarationStubGenerator(
|
||||
origin,
|
||||
descriptor.original,
|
||||
descriptor.type.toIrType()
|
||||
).apply {
|
||||
correspondingProperty = generatePropertyStub(descriptor, bindingContext)
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as IrField? ?:
|
||||
IrFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, descriptor.type.toIrType())
|
||||
}.apply {
|
||||
initializer = descriptor.compileTimeInitializer?.let {
|
||||
IrExpressionBodyImpl(
|
||||
constantValueGenerator.generateConstantValueAsExpression(
|
||||
@@ -142,7 +148,10 @@ class DeclarationStubGenerator(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
origin,
|
||||
descriptor.original
|
||||
) { IrLazyFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator) }
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as IrSimpleFunction? ?:
|
||||
IrLazyFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun generateConstructorStub(descriptor: ClassConstructorDescriptor): IrConstructor {
|
||||
@@ -153,7 +162,10 @@ class DeclarationStubGenerator(
|
||||
|
||||
return symbolTable.declareConstructor(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor.original
|
||||
) { IrLazyConstructor(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator) }
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as IrConstructor? ?:
|
||||
IrLazyConstructor(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
|
||||
@@ -182,7 +194,10 @@ class DeclarationStubGenerator(
|
||||
}
|
||||
return symbolTable.declareClass(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor
|
||||
) { IrLazyClass(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator) }
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenceClass) as IrClass? ?:
|
||||
IrLazyClass(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun generateEnumEntryStub(descriptor: ClassDescriptor): IrEnumEntry {
|
||||
@@ -191,6 +206,7 @@ class DeclarationStubGenerator(
|
||||
return referenced.owner
|
||||
}
|
||||
return symbolTable.declareEnumEntry(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as IrEnumEntry? ?:
|
||||
IrLazyEnumEntryImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
@@ -201,6 +217,7 @@ class DeclarationStubGenerator(
|
||||
return referenced.owner
|
||||
}
|
||||
return symbolTable.declareGlobalTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as IrTypeParameter? ?:
|
||||
IrLazyTypeParameter(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
|
||||
it, this, typeTranslator
|
||||
@@ -220,4 +237,4 @@ class DeclarationStubGenerator(
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -200,16 +202,25 @@ open class DeepCopyIrTreeWithSymbols(
|
||||
}
|
||||
|
||||
override fun visitProperty(declaration: IrProperty): IrProperty =
|
||||
IrPropertyImpl(
|
||||
declaration.startOffset, declaration.endOffset,
|
||||
IrPropertyImpl(declaration.startOffset, declaration.endOffset,
|
||||
mapDeclarationOrigin(declaration.origin),
|
||||
declaration.descriptor,
|
||||
declaration.name,
|
||||
declaration.visibility,
|
||||
declaration.modality,
|
||||
declaration.isVar,
|
||||
declaration.isConst,
|
||||
declaration.isLateinit,
|
||||
declaration.isDelegated,
|
||||
declaration.descriptor, // TODO
|
||||
declaration.backingField?.transform(),
|
||||
declaration.getter?.transform(),
|
||||
declaration.setter?.transform()
|
||||
declaration.isExternal
|
||||
).apply {
|
||||
transformAnnotations(declaration)
|
||||
this.backingField = declaration.backingField?.transform()
|
||||
this.getter = declaration.getter?.transform()
|
||||
this.setter = declaration.setter?.transform()
|
||||
this.backingField?.let {
|
||||
it.correspondingProperty = this
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitField(declaration: IrField): IrField =
|
||||
|
||||
@@ -81,7 +81,10 @@ open class DeepCopySymbolRemapper(
|
||||
|
||||
override fun visitField(declaration: IrField) {
|
||||
remapSymbol(fields, declaration) {
|
||||
IrFieldSymbolImpl(descriptorsRemapper.remapDeclaredField(it.descriptor))
|
||||
if (declaration.correspondingProperty == null)
|
||||
IrFieldSymbolImpl(descriptorsRemapper.remapDeclaredField(it.descriptor))
|
||||
else
|
||||
IrFieldSymbolImpl(descriptorsRemapper.remapDeclaredProperty(it.descriptor))
|
||||
}
|
||||
declaration.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ interface DescriptorsRemapper {
|
||||
fun remapDeclaredExternalPackageFragment(descriptor: PackageFragmentDescriptor): PackageFragmentDescriptor = descriptor
|
||||
fun remapDeclaredField(descriptor: PropertyDescriptor): PropertyDescriptor = descriptor
|
||||
fun remapDeclaredFilePackageFragment(descriptor: PackageFragmentDescriptor): PackageFragmentDescriptor = descriptor
|
||||
fun remapDeclaredProperty(descriptor: PropertyDescriptor): PropertyDescriptor = descriptor
|
||||
fun remapDeclaredSimpleFunction(descriptor: FunctionDescriptor): FunctionDescriptor = descriptor
|
||||
fun remapDeclaredTypeParameter(descriptor: TypeParameterDescriptor): TypeParameterDescriptor = descriptor
|
||||
fun remapDeclaredValueParameter(descriptor: ParameterDescriptor): ParameterDescriptor = descriptor
|
||||
|
||||
@@ -178,8 +178,9 @@ class DumpIrTreeVisitor(out: Appendable) : IrElementVisitor<Unit, String> {
|
||||
dumpTypeArguments(expression)
|
||||
expression.dispatchReceiver?.accept(this, "\$this")
|
||||
expression.extensionReceiver?.accept(this, "\$receiver")
|
||||
for (valueParameter in expression.descriptor.valueParameters) {
|
||||
expression.getValueArgument(valueParameter.index)?.accept(this, valueParameter.name.asString())
|
||||
for (i in 0..expression.valueArgumentsCount - 1) {
|
||||
val valueArgument = expression.getValueArgument(i)
|
||||
valueArgument?.accept(this, "argument $i")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-2
@@ -25,10 +25,11 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
class ExternalDependenciesGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
val irBuiltIns: IrBuiltIns
|
||||
val irBuiltIns: IrBuiltIns,
|
||||
val deserializer: IrDeserializer? = null
|
||||
) {
|
||||
private val stubGenerator = DeclarationStubGenerator(
|
||||
moduleDescriptor, symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, irBuiltIns.languageVersionSettings
|
||||
moduleDescriptor, symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, irBuiltIns.languageVersionSettings, deserializer
|
||||
)
|
||||
|
||||
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment, bindingContext: BindingContext? = null) {
|
||||
@@ -58,6 +59,8 @@ class ExternalDependenciesGenerator(
|
||||
stubGenerator.generateOrGetTypeParameterStub(it.descriptor)
|
||||
}
|
||||
|
||||
deserializer?.declareForwardDeclarations()
|
||||
|
||||
assert(symbolTable.unboundClasses.isEmpty())
|
||||
assert(symbolTable.unboundConstructors.isEmpty())
|
||||
assert(symbolTable.unboundEnumEntries.isEmpty())
|
||||
|
||||
@@ -32,6 +32,12 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
|
||||
interface IrDeserializer {
|
||||
fun findDeserializedDeclaration(symbol: IrSymbol): IrDeclaration?
|
||||
fun findDeserializedDeclaration(propertyDescriptor: PropertyDescriptor): IrProperty?
|
||||
fun declareForwardDeclarations()
|
||||
}
|
||||
|
||||
interface ReferenceSymbolTable {
|
||||
fun referenceClass(descriptor: ClassDescriptor): IrClassSymbol
|
||||
|
||||
|
||||
Reference in New Issue
Block a user