[IR] Implement new IrSymbol API

- Add predicate whether symbol is PublicAPI
 - Split symbols into 2 types (Public/Private)
 - Fix special implementations
This commit is contained in:
Roman Artemev
2020-01-27 15:40:25 +03:00
committed by romanart
parent bb04eae93e
commit cef9ed1dae
7 changed files with 138 additions and 66 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.FqName
private val BODILESS_BUILTIN_CLASSES = listOf(
@@ -55,11 +56,13 @@ private class DescriptorlessIrFileSymbol : IrFileSymbol {
private var _owner: IrFile? = null
override val owner get() = _owner!!
override var uniqId: UniqId
get() = error("Operation is unsupported")
set(value) { error("Operation is unsupported") }
override val isBound get() = _owner != null
override val isPublicApi: Boolean
get() = error("Operation is unsupported")
override val signature: IdSignature
get() = error("Operation is unsupported")
}
private fun isBuiltInClass(declaration: IrDeclaration): Boolean =
@@ -24,8 +24,8 @@ import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.UniqId
import org.jetbrains.kotlin.name.FqName
class WasmBackendContext(
@@ -90,15 +90,17 @@ class DescriptorlessExternalPackageFragmentSymbol : IrExternalPackageFragmentSym
private var _owner: IrExternalPackageFragment? = null
override val owner get() = _owner!!
override var uniqId: UniqId
get() = error("Operation is unsupported")
set(value) { error("Operation is unsupported") }
override val isBound get() = _owner != null
override fun bind(owner: IrExternalPackageFragment) {
_owner = owner
}
override val isPublicApi: Boolean
get() = error("Operation is unsupported")
override val signature: IdSignature
get() = error("Operation is unsupported")
}
@@ -2,22 +2,24 @@ package org.jetbrains.kotlin.ir.symbols
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.UniqId
import org.jetbrains.kotlin.ir.util.IdSignature
abstract class IrDelegatingSymbol<S: IrBindableSymbol<D, B>, B: IrSymbolOwner, D: DeclarationDescriptor>(var delegate: S)
: IrBindableSymbol<D, B> {
abstract class IrDelegatingSymbol<S : IrBindableSymbol<D, B>, B : IrSymbolOwner, D : DeclarationDescriptor>(var delegate: S) :
IrBindableSymbol<D, B> {
override val owner: B get() = delegate.owner
override val descriptor: D get() = delegate.descriptor
override val isBound: Boolean get() = delegate.isBound
override var uniqId: UniqId
get() = delegate.uniqId
set(value: UniqId) { delegate.uniqId = value }
override val isPublicApi: Boolean
get() = delegate.isPublicApi
override val signature: IdSignature
get() = delegate.signature
override fun bind(owner: B) = delegate.bind(owner)
override fun hashCode() = delegate.hashCode()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (this === delegate) return true
if (delegate === other) return true
return false
}
}
@@ -20,8 +20,8 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrScript
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.IrSymbolVisitor
import org.jetbrains.kotlin.ir.util.UniqId
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.types.model.TypeParameterMarker
@@ -30,7 +30,9 @@ interface IrSymbol {
val descriptor: DeclarationDescriptor
val isBound: Boolean
var uniqId: UniqId
val signature: IdSignature
val isPublicApi: Boolean
fun <D, R> accept(visitor: IrSymbolVisitor<R, D>, data: D): R
}
@@ -21,12 +21,12 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.*
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.UniqId
import org.jetbrains.kotlin.ir.util.IdSignature
abstract class IrSymbolBase<out D : DeclarationDescriptor>(override val descriptor: D, override var uniqId: UniqId = UniqId.NONE) : IrSymbol
abstract class IrSymbolBase<out D : DeclarationDescriptor>(override val descriptor: D) : IrSymbol
abstract class IrBindableSymbolBase<out D : DeclarationDescriptor, B : IrSymbolOwner>(descriptor: D, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbol<D, B>, IrSymbolBase<D>(descriptor, uniqId) {
abstract class IrBindableSymbolBase<out D : DeclarationDescriptor, B : IrSymbolOwner>(descriptor: D) :
IrBindableSymbol<D, B>, IrSymbolBase<D>(descriptor) {
init {
assert(isOriginalDescriptor(descriptor)) {
@@ -58,6 +58,11 @@ abstract class IrBindableSymbolBase<out D : DeclarationDescriptor, B : IrSymbolO
}
}
override val isPublicApi: Boolean = false
override val signature: IdSignature
get() = error("IdSignature is allowed only for PublicApi symbols")
override val isBound: Boolean
get() = _owner != null
}
@@ -76,70 +81,63 @@ class IrAnonymousInitializerSymbolImpl(descriptor: ClassDescriptor) :
constructor(irClassSymbol: IrClassSymbol) : this(irClassSymbol.descriptor) {}
}
class IrClassSymbolImpl(descriptor: ClassDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<ClassDescriptor, IrClass>(descriptor, uniqId),
class IrClassSymbolImpl(descriptor: ClassDescriptor) :
IrBindableSymbolBase<ClassDescriptor, IrClass>(descriptor),
IrClassSymbol {
constructor(uniqId: UniqId) : this(WrappedClassDescriptor(), uniqId)
}
class IrEnumEntrySymbolImpl(descriptor: ClassDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<ClassDescriptor, IrEnumEntry>(descriptor, uniqId),
class IrEnumEntrySymbolImpl(descriptor: ClassDescriptor) :
IrBindableSymbolBase<ClassDescriptor, IrEnumEntry>(descriptor),
IrEnumEntrySymbol {
constructor(uniqId: UniqId) : this(WrappedEnumEntryDescriptor(), uniqId)
}
class IrFieldSymbolImpl(descriptor: PropertyDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<PropertyDescriptor, IrField>(descriptor, uniqId),
class IrFieldSymbolImpl(descriptor: PropertyDescriptor) :
IrBindableSymbolBase<PropertyDescriptor, IrField>(descriptor),
IrFieldSymbol {
constructor(uniqId: UniqId) : this(WrappedFieldDescriptor(), uniqId)
}
class IrTypeParameterSymbolImpl(descriptor: TypeParameterDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<TypeParameterDescriptor, IrTypeParameter>(descriptor, uniqId),
class IrTypeParameterSymbolImpl(descriptor: TypeParameterDescriptor) :
IrBindableSymbolBase<TypeParameterDescriptor, IrTypeParameter>(descriptor),
IrTypeParameterSymbol {
constructor(uniqId: UniqId) : this(WrappedTypeParameterDescriptor(), uniqId)
}
class IrValueParameterSymbolImpl(descriptor: ParameterDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<ParameterDescriptor, IrValueParameter>(descriptor, uniqId),
class IrValueParameterSymbolImpl(descriptor: ParameterDescriptor) :
IrBindableSymbolBase<ParameterDescriptor, IrValueParameter>(descriptor),
IrValueParameterSymbol {
constructor(uniqId: UniqId) : this(WrappedValueParameterDescriptor(), uniqId)
}
class IrVariableSymbolImpl(descriptor: VariableDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<VariableDescriptor, IrVariable>(descriptor, uniqId),
class IrVariableSymbolImpl(descriptor: VariableDescriptor) :
IrBindableSymbolBase<VariableDescriptor, IrVariable>(descriptor),
IrVariableSymbol {
constructor(uniqId: UniqId) : this(WrappedVariableDescriptor(), uniqId)
}
class IrSimpleFunctionSymbolImpl(descriptor: FunctionDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<FunctionDescriptor, IrSimpleFunction>(descriptor, uniqId),
class IrSimpleFunctionSymbolImpl(descriptor: FunctionDescriptor) :
IrBindableSymbolBase<FunctionDescriptor, IrSimpleFunction>(descriptor),
IrSimpleFunctionSymbol {
constructor(uniqId: UniqId) : this(WrappedSimpleFunctionDescriptor(), uniqId)
}
class IrConstructorSymbolImpl(descriptor: ClassConstructorDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<ClassConstructorDescriptor, IrConstructor>(descriptor, uniqId),
class IrConstructorSymbolImpl(descriptor: ClassConstructorDescriptor) :
IrBindableSymbolBase<ClassConstructorDescriptor, IrConstructor>(descriptor),
IrConstructorSymbol {
constructor(uniqId: UniqId) : this(WrappedClassConstructorDescriptor(), uniqId)
}
class IrReturnableBlockSymbolImpl(descriptor: FunctionDescriptor) :
IrBindableSymbolBase<FunctionDescriptor, IrReturnableBlock>(descriptor),
IrReturnableBlockSymbol
class IrPropertySymbolImpl(descriptor: PropertyDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<PropertyDescriptor, IrProperty>(descriptor, uniqId),
class IrPropertySymbolImpl(descriptor: PropertyDescriptor) :
IrBindableSymbolBase<PropertyDescriptor, IrProperty>(descriptor),
IrPropertySymbol {
constructor(uniqId: UniqId) : this(WrappedPropertyDescriptor(), uniqId)
}
class IrLocalDelegatedPropertySymbolImpl(descriptor: VariableDescriptorWithAccessors) :
IrBindableSymbolBase<VariableDescriptorWithAccessors, IrLocalDelegatedProperty>(descriptor),
IrLocalDelegatedPropertySymbol
class IrTypeAliasSymbolImpl(descriptor: TypeAliasDescriptor, uniqId: UniqId = UniqId.NONE) :
IrBindableSymbolBase<TypeAliasDescriptor, IrTypeAlias>(descriptor, uniqId),
class IrTypeAliasSymbolImpl(descriptor: TypeAliasDescriptor) :
IrBindableSymbolBase<TypeAliasDescriptor, IrTypeAlias>(descriptor),
IrTypeAliasSymbol {
constructor(uniqId: UniqId) : this(WrappedTypeAliasDescriptor(), uniqId)
}
}
class IrScriptSymbolImpl(descriptor: ScriptDescriptor) :
IrScriptSymbol, IrBindableSymbolBase<ScriptDescriptor, IrScript>(descriptor)
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.symbols.impl
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.WrappedDeclarationDescriptor
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.IdSignature
abstract class IrPublicSymbolBase<out D : DeclarationDescriptor>(override val descriptor: D, override val signature: IdSignature) : IrSymbol
abstract class IrBindablePublicSymbolBase<out D : DeclarationDescriptor, B : IrSymbolOwner>(descriptor: D, sig: IdSignature) :
IrBindableSymbol<D, B>, IrPublicSymbolBase<D>(descriptor, sig) {
init {
assert(isOriginalDescriptor(descriptor)) {
"Substituted descriptor $descriptor for ${descriptor.original}"
}
assert(sig.isPublic)
}
private fun isOriginalDescriptor(descriptor: DeclarationDescriptor): Boolean =
descriptor is WrappedDeclarationDescriptor<*> ||
// TODO fix declaring/referencing value parameters: compute proper original descriptor
descriptor is ValueParameterDescriptor && isOriginalDescriptor(descriptor.containingDeclaration) ||
descriptor == descriptor.original
private var _owner: B? = null
override val owner: B
get() = _owner ?: throw IllegalStateException("Symbol for $signature is unbound")
override fun bind(owner: B) {
if (_owner == null) {
_owner = owner
} else {
throw IllegalStateException("${javaClass.simpleName} for $signature is already bound")
}
}
override val isPublicApi: Boolean = true
override val isBound: Boolean
get() = _owner != null
}
class IrClassPublicSymbolImpl(descriptor: ClassDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<ClassDescriptor, IrClass>(descriptor, sig),
IrClassSymbol {
}
class IrEnumEntryPublicSymbolImpl(descriptor: ClassDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<ClassDescriptor, IrEnumEntry>(descriptor, sig),
IrEnumEntrySymbol {
}
class IrSimpleFunctionPublicSymbolImpl(descriptor: FunctionDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<FunctionDescriptor, IrSimpleFunction>(descriptor, sig),
IrSimpleFunctionSymbol {
}
class IrConstructorPublicSymbolImpl(descriptor: ClassConstructorDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<ClassConstructorDescriptor, IrConstructor>(descriptor, sig),
IrConstructorSymbol {
}
class IrPropertyPublicSymbolImpl(descriptor: PropertyDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<PropertyDescriptor, IrProperty>(descriptor, sig),
IrPropertySymbol {
}
class IrTypeAliasPublicSymbolImpl(descriptor: TypeAliasDescriptor, sig: IdSignature) :
IrBindablePublicSymbolBase<TypeAliasDescriptor, IrTypeAlias>(descriptor, sig),
IrTypeAliasSymbol {
}
@@ -1,13 +0,0 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.symbols.impl
import org.jetbrains.kotlin.descriptors.ScriptDescriptor
import org.jetbrains.kotlin.ir.declarations.IrScript
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
class IrScriptSymbolImpl(descriptor: ScriptDescriptor) :
IrScriptSymbol, IrBindableSymbolBase<ScriptDescriptor, IrScript>(descriptor)