IR: Introduce IrTypeAlias element

This commit is contained in:
Dmitry Petrov
2019-07-22 16:48:05 +03:00
parent 499f9330d2
commit 48ae76ac28
30 changed files with 440 additions and 31 deletions
@@ -1607,6 +1607,11 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("abbreviatedTypes.kt")
public void testAbbreviatedTypes() throws Exception {
runTest("compiler/testData/ir/irText/types/abbreviatedTypes.kt");
}
public void testAllFilesPresentInTypes() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText/types"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrErrorDeclarationImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrTypeAliasImpl
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.withScope
@@ -49,7 +50,7 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
is KtClassOrObject ->
generateClassOrObjectDeclaration(ktDeclaration)
is KtTypeAlias ->
null
generateTypeAliasDeclaration(ktDeclaration)
else ->
IrErrorDeclarationImpl(
ktDeclaration.startOffsetSkippingComments, ktDeclaration.endOffset,
@@ -79,6 +80,19 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
fun generateClassOrObjectDeclaration(ktClassOrObject: KtPureClassOrObject): IrClass =
ClassGenerator(this).generateClass(ktClassOrObject)
private fun generateTypeAliasDeclaration(ktTypeAlias: KtTypeAlias): IrTypeAlias {
val typeAliasDescriptor = getOrFail(BindingContext.TYPE_ALIAS, ktTypeAlias)
val irTypeAlias = context.symbolTable.declareTypeAlias(typeAliasDescriptor) { symbol ->
IrTypeAliasImpl.fromSymbolDescriptor(
ktTypeAlias.startOffsetSkippingComments, ktTypeAlias.endOffset,
symbol,
typeAliasDescriptor.expandedType.toIrType(),
IrDeclarationOrigin.DEFINED
)
}
generateGlobalTypeParametersDeclarations(irTypeAlias, typeAliasDescriptor.declaredTypeParameters)
return irTypeAlias
}
fun generateGlobalTypeParametersDeclarations(
irTypeParametersOwner: IrTypeParametersContainer,
@@ -0,0 +1,20 @@
/*
* 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.declarations
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.IrType
interface IrTypeAlias :
IrSymbolDeclaration<IrTypeAliasSymbol>,
IrDeclarationWithName,
IrDeclarationWithVisibility,
IrTypeParametersContainer {
override val descriptor: TypeAliasDescriptor
val expandedType: IrType
}
@@ -0,0 +1,71 @@
/*
* 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.declarations.impl
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrTypeAlias
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeAliasSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.transform
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.SmartList
class IrTypeAliasImpl(
startOffset: Int,
endOffset: Int,
override val symbol: IrTypeAliasSymbol,
override val name: Name,
override val visibility: Visibility,
override val expandedType: IrType,
origin: IrDeclarationOrigin
) :
IrDeclarationBase(startOffset, endOffset, origin),
IrTypeAlias {
init {
symbol.bind(this)
}
override val descriptor: TypeAliasDescriptor
get() = symbol.descriptor
override val typeParameters: MutableList<IrTypeParameter> = SmartList()
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitTypeAlias(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
typeParameters.forEach { it.accept(visitor, data) }
}
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
typeParameters.transform { it.transform(transformer, data) }
}
companion object {
fun fromSymbolDescriptor(
startOffset: Int,
endOffset: Int,
symbol: IrTypeAliasSymbol,
expandedType: IrType,
origin: IrDeclarationOrigin
) =
IrTypeAliasImpl(
startOffset, endOffset,
symbol,
symbol.descriptor.name,
symbol.descriptor.visibility,
expandedType,
origin
)
}
}
@@ -0,0 +1,69 @@
/*
* 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.declarations.lazy
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrTypeAlias
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.util.transform
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.Name
class IrLazyTypeAlias(
startOffset: Int,
endOffset: Int,
origin: IrDeclarationOrigin,
override val symbol: IrTypeAliasSymbol,
stubGenerator: DeclarationStubGenerator,
typeTranslator: TypeTranslator
) :
IrLazyDeclarationBase(startOffset, endOffset, origin, stubGenerator, typeTranslator),
IrTypeAlias {
init {
symbol.bind(this)
}
override val descriptor: TypeAliasDescriptor
get() = symbol.descriptor
override val name: Name
get() = descriptor.name
override val visibility: Visibility
get() = descriptor.visibility
override val typeParameters: MutableList<IrTypeParameter> by lazy {
descriptor.declaredTypeParameters.mapTo(arrayListOf()) {
stubGenerator.generateOrGetTypeParameterStub(it)
}
}
override val expandedType: IrType by lazy {
typeTranslator.buildWithScope(this) {
descriptor.expandedType.toIrType()
}
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitTypeAlias(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
typeParameters.forEach { it.accept(visitor, data) }
}
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
typeParameters.transform { it.transform(transformer, data) }
}
}
@@ -178,4 +178,11 @@ interface IrLocalDelegatedPropertySymbol :
override fun <D, R> accept(visitor: IrSymbolVisitor<R, D>, data: D): R =
visitor.visitLocalDelegatedPropertySymbol(this, data)
}
interface IrTypeAliasSymbol :
IrBindableSymbol<TypeAliasDescriptor, IrTypeAlias> {
override fun <D, R> accept(visitor: IrSymbolVisitor<R, D>, data: D): R =
visitor.visitTypeAliasSymbol(this, data)
}
@@ -110,4 +110,8 @@ class IrPropertySymbolImpl(descriptor: PropertyDescriptor) :
class IrLocalDelegatedPropertySymbolImpl(descriptor: VariableDescriptorWithAccessors) :
IrBindableSymbolBase<VariableDescriptorWithAccessors, IrLocalDelegatedProperty>(descriptor),
IrLocalDelegatedPropertySymbol
IrLocalDelegatedPropertySymbol
class IrTypeAliasSymbolImpl(descriptor: TypeAliasDescriptor) :
IrBindableSymbolBase<TypeAliasDescriptor, IrTypeAlias>(descriptor),
IrTypeAliasSymbol
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.*
@@ -33,9 +34,10 @@ interface IrSimpleType : IrType, SimpleTypeMarker, TypeArgumentListMarker {
val classifier: IrClassifierSymbol
val hasQuestionMark: Boolean
val arguments: List<IrTypeArgument>
val abbreviation: IrTypeAbbreviation?
}
interface IrTypeArgument: TypeArgumentMarker {
interface IrTypeArgument : TypeArgumentMarker {
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
@@ -46,4 +48,10 @@ interface IrStarProjection : IrTypeArgument
interface IrTypeProjection : IrTypeArgument {
val variance: Variance
val type: IrType
}
interface IrTypeAbbreviation : IrAnnotationContainer {
val typeAlias: IrTypeAliasSymbol
val hasQuestionMark: Boolean
val arguments: List<IrTypeArgument>
}
@@ -17,15 +17,17 @@ class IrSimpleTypeImpl(
override val classifier: IrClassifierSymbol,
override val hasQuestionMark: Boolean,
override val arguments: List<IrTypeArgument>,
annotations: List<IrConstructorCall>
annotations: List<IrConstructorCall>,
override val abbreviation: IrTypeAbbreviation? = null
) : IrTypeBase(kotlinType, annotations, Variance.INVARIANT), IrSimpleType, IrTypeProjection {
constructor(
classifier: IrClassifierSymbol,
hasQuestionMark: Boolean,
arguments: List<IrTypeArgument>,
annotations: List<IrConstructorCall>
) : this(null, classifier, hasQuestionMark, arguments, annotations)
annotations: List<IrConstructorCall>,
abbreviation: IrTypeAbbreviation? = null
) : this(null, classifier, hasQuestionMark, arguments, annotations, abbreviation)
override fun equals(other: Any?): Boolean =
other is IrSimpleTypeImpl &&
@@ -45,6 +47,7 @@ class IrSimpleTypeBuilder {
var hasQuestionMark = false
var arguments: List<IrTypeArgument> = emptyList()
var annotations: List<IrConstructorCall> = emptyList()
var abbreviation: IrTypeAbbreviation? = null
var variance = Variance.INVARIANT
}
@@ -55,6 +58,7 @@ fun IrSimpleType.toBuilder() =
b.hasQuestionMark = hasQuestionMark
b.arguments = arguments
b.annotations = annotations
b.abbreviation = abbreviation
}
fun IrSimpleTypeBuilder.buildSimpleType() =
@@ -63,7 +67,8 @@ fun IrSimpleTypeBuilder.buildSimpleType() =
classifier ?: throw AssertionError("Classifier not provided"),
hasQuestionMark,
arguments,
annotations
annotations,
abbreviation
)
fun IrSimpleTypeBuilder.buildTypeProjection() =
@@ -0,0 +1,39 @@
/*
* 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.types.impl
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.IrTypeAbbreviation
import org.jetbrains.kotlin.ir.types.IrTypeArgument
class IrTypeAbbreviationImpl(
override val typeAlias: IrTypeAliasSymbol,
override val hasQuestionMark: Boolean,
override val arguments: List<IrTypeArgument>,
override val annotations: List<IrConstructorCall>
) : IrTypeAbbreviation
class IrTypeAbbreviationBuilder {
var typeAlias: IrTypeAliasSymbol? = null
var hasQuestionMark: Boolean = false
var arguments: List<IrTypeArgument> = emptyList()
var annotations: List<IrConstructorCall> = emptyList()
}
fun IrTypeAbbreviationImpl.toBuilder() =
IrTypeAbbreviationBuilder().also { b ->
b.typeAlias = typeAlias
b.hasQuestionMark = hasQuestionMark
b.arguments = arguments
b.annotations = annotations
}
fun IrTypeAbbreviationBuilder.build() =
IrTypeAbbreviationImpl(
typeAlias ?: throw AssertionError("typeAlias not provided"),
hasQuestionMark, arguments, annotations
)
@@ -243,4 +243,15 @@ class DeclarationStubGenerator(
IrLazyTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
}
}
internal fun generateTypeAliasStub(descriptor: TypeAliasDescriptor): IrTypeAlias {
val referenced = symbolTable.referenceTypeAlias(descriptor)
if (referenced.isBound) {
return referenced.owner
}
val origin = computeOrigin(descriptor)
return symbolTable.declareTypeAlias(descriptor) {
IrLazyTypeAlias(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
}
}
}
@@ -51,6 +51,7 @@ interface SymbolRenamer {
fun getVariableName(symbol: IrVariableSymbol): Name = symbol.owner.name
fun getTypeParameterName(symbol: IrTypeParameterSymbol): Name = symbol.owner.name
fun getValueParameterName(symbol: IrValueParameterSymbol): Name = symbol.owner.name
fun getTypeAliasName(symbol: IrTypeAliasSymbol): Name = symbol.owner.name
object DEFAULT : SymbolRenamer
}
@@ -333,6 +334,19 @@ open class DeepCopyIrTreeWithSymbols(
defaultValue = declaration.defaultValue?.transform()
}
override fun visitTypeAlias(declaration: IrTypeAlias): IrTypeAlias =
IrTypeAliasImpl(
declaration.startOffset, declaration.endOffset,
symbolRemapper.getDeclaredTypeAlias(declaration.symbol),
symbolRenamer.getTypeAliasName(declaration.symbol),
declaration.visibility,
declaration.expandedType.remapType(),
mapDeclarationOrigin(declaration.origin)
).apply {
transformAnnotations(declaration)
copyTypeParametersFrom(declaration)
}
override fun visitBody(body: IrBody): IrBody =
throw IllegalArgumentException("Unsupported body type: $body")
@@ -43,6 +43,7 @@ open class DeepCopySymbolRemapper(
private val valueParameters = hashMapOf<IrValueParameterSymbol, IrValueParameterSymbol>()
private val variables = hashMapOf<IrVariableSymbol, IrVariableSymbol>()
private val localDelegatedProperties = hashMapOf<IrLocalDelegatedPropertySymbol, IrLocalDelegatedPropertySymbol>()
private val typeAliases = hashMapOf<IrTypeAliasSymbol, IrTypeAliasSymbol>()
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
@@ -138,6 +139,13 @@ open class DeepCopySymbolRemapper(
declaration.acceptChildrenVoid(this)
}
override fun visitTypeAlias(declaration: IrTypeAlias) {
remapSymbol(typeAliases, declaration) {
IrTypeAliasSymbolImpl(descriptorsRemapper.remapDeclaredTypeAlias(it.descriptor))
}
declaration.acceptChildrenVoid(this)
}
override fun visitBlock(expression: IrBlock) {
if (expression is IrReturnableBlock) {
remapSymbol(returnableBlocks, expression) {
@@ -171,6 +179,8 @@ open class DeepCopySymbolRemapper(
override fun getDeclaredLocalDelegatedProperty(symbol: IrLocalDelegatedPropertySymbol): IrLocalDelegatedPropertySymbol =
localDelegatedProperties.getDeclared(symbol)
override fun getDeclaredTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol = typeAliases.getDeclared(symbol)
override fun getReferencedClass(symbol: IrClassSymbol): IrClassSymbol = classes.getReferenced(symbol)
override fun getReferencedClassOrNull(symbol: IrClassSymbol?): IrClassSymbol? = symbol?.let { classes.getReferenced(it) }
override fun getReferencedEnumEntry(symbol: IrEnumEntrySymbol): IrEnumEntrySymbol = enumEntries.getReferenced(symbol)
@@ -205,4 +215,6 @@ open class DeepCopySymbolRemapper(
is IrTypeParameterSymbol -> typeParameters.getReferenced(symbol)
else -> throw IllegalArgumentException("Unexpected symbol $symbol ${symbol.descriptor}")
}
override fun getReferencedTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol = typeAliases.getReferenced(symbol)
}
@@ -7,11 +7,9 @@ package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrTypeProjectionImpl
import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
class DeepCopyTypeRemapper(
@@ -28,27 +26,30 @@ class DeepCopyTypeRemapper(
// TODO
}
// TODO This is a hack
override fun remapType(type: IrType): IrType {
if (type !is IrSimpleType) return type
override fun remapType(type: IrType): IrType =
if (type !is IrSimpleType)
type
else
IrSimpleTypeImpl(
null,
symbolRemapper.getReferencedClassifier(type.classifier),
type.hasQuestionMark,
type.arguments.map { remapTypeArgument(it) },
type.annotations.map { it.transform(deepCopy, null) as IrConstructorCall },
type.abbreviation?.remapTypeAbbreviation()
)
val arguments = type.arguments.map {
if (it is IrTypeProjection) {
makeTypeProjection(this.remapType(it.type), it.variance)
} else {
it
}
}
private fun remapTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument =
if (typeArgument is IrTypeProjection)
makeTypeProjection(this.remapType(typeArgument.type), typeArgument.variance)
else
typeArgument
val annotations = type.annotations.map { it.transform(deepCopy, null) as IrConstructorCall }
return IrSimpleTypeImpl(
null,
symbolRemapper.getReferencedClassifier(type.classifier),
type.hasQuestionMark,
arguments,
private fun IrTypeAbbreviation.remapTypeAbbreviation() =
IrTypeAbbreviationImpl(
symbolRemapper.getReferencedTypeAlias(typeAlias),
hasQuestionMark,
arguments.map { remapTypeArgument(it) },
annotations
)
}
}
@@ -31,6 +31,7 @@ interface DescriptorsRemapper {
fun remapDeclaredValueParameter(descriptor: ParameterDescriptor): ParameterDescriptor = descriptor
fun remapDeclaredVariable(descriptor: VariableDescriptor): VariableDescriptor = descriptor
fun remapDeclaredLocalDelegatedProperty(descriptor: VariableDescriptorWithAccessors): VariableDescriptorWithAccessors = descriptor
fun remapDeclaredTypeAlias(descriptor: TypeAliasDescriptor): TypeAliasDescriptor = descriptor
object Default : DescriptorsRemapper
}
@@ -77,6 +77,13 @@ class DumpIrTreeVisitor(
}
}
override fun visitTypeAlias(declaration: IrTypeAlias, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
declaration.typeParameters.dumpElements()
}
}
override fun visitTypeParameter(declaration: IrTypeParameter, data: String) {
declaration.dumpLabeledElementWith(data) {
dumpAnnotations(declaration)
@@ -60,6 +60,9 @@ class ExternalDependenciesGenerator(
ArrayList(symbolTable.unboundTypeParameters).forEach {
stubGenerator.generateOrGetTypeParameterStub(it.descriptor)
}
ArrayList(symbolTable.unboundTypeAliases).forEach {
stubGenerator.generateTypeAliasStub(it.descriptor)
}
deserializer?.declareForwardDeclarations()
@@ -70,6 +73,7 @@ class ExternalDependenciesGenerator(
assertEmpty(symbolTable.unboundSimpleFunctions, "simple functions")
assertEmpty(symbolTable.unboundProperties, "properties")
assertEmpty(symbolTable.unboundTypeParameters, "type parameters")
assertEmpty(symbolTable.unboundTypeAliases, "type aliases")
}
private fun assertEmpty(s: Set<IrSymbol>, marker: String) {
@@ -66,4 +66,7 @@ interface IrSymbolVisitor<out R, in D> {
fun visitLocalDelegatedPropertySymbol(symbol: IrLocalDelegatedPropertySymbol, data: D) =
visitSymbol(symbol, data)
fun visitTypeAliasSymbol(symbol: IrTypeAliasSymbol, data: D) =
visitSymbol(symbol, data)
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.renderer.DescriptorRenderer
@@ -113,11 +114,32 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
if (hasQuestionMark) {
append('?')
}
abbreviation?.let {
append(it.renderTypeAbbreviation())
}
}
else -> "{${javaClass.simpleName} $this}"
}
private fun IrTypeAbbreviation.renderTypeAbbreviation(): String =
buildString {
append("{ ")
append(renderTypeAnnotations(annotations))
append(typeAlias.renderTypeAliasFqn())
if (arguments.isNotEmpty()) {
append(
arguments.joinToString(prefix = "<", postfix = ">", separator = ", ") {
it.renderTypeArgument()
}
)
}
if (hasQuestionMark) {
append('?')
}
append(" }")
}
private fun IrTypeArgument.renderTypeArgument(): String =
when (this) {
is IrStarProjection -> "*"
@@ -473,6 +495,12 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
"name:$name type:${type.render()} flags:${renderLocalDelegatedPropertyFlags()}"
}
override fun visitTypeAlias(declaration: IrTypeAlias, data: Nothing?): String =
declaration.run {
"TYPEALIAS ${declaration.renderOriginIfNonTrivial()}" +
"name:$name visibility:$visibility expandedType:${expandedType.render()}"
}
private fun IrLocalDelegatedProperty.renderLocalDelegatedPropertyFlags() =
if (isVar) "var" else "val"
@@ -667,7 +695,14 @@ internal fun IrClassifierSymbol.renderClassifierFqn(): String =
is IrTypeParameter -> owner.renderTypeParameterFqn()
else -> "`unexpected classifier: ${owner.render()}`"
}
else "<unbound ${this.javaClass.simpleName}>"
else
"<unbound ${this.javaClass.simpleName}>"
internal fun IrTypeAliasSymbol.renderTypeAliasFqn(): String =
if (isBound)
StringBuilder().also { owner.renderDeclarationFqn(it) }.toString()
else
"<unbound $this: ${this.descriptor}>"
internal fun IrClass.renderClassFqn(): String =
StringBuilder().also { renderDeclarationFqn(it) }.toString()
@@ -31,6 +31,8 @@ interface SymbolRemapper {
fun getDeclaredLocalDelegatedProperty(symbol: IrLocalDelegatedPropertySymbol): IrLocalDelegatedPropertySymbol
fun getDeclaredTypeParameter(symbol: IrTypeParameterSymbol): IrTypeParameterSymbol
fun getDeclaredValueParameter(symbol: IrValueParameterSymbol): IrValueParameterSymbol
fun getDeclaredTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol
fun getReferencedClass(symbol: IrClassSymbol): IrClassSymbol
fun getReferencedClassOrNull(symbol: IrClassSymbol?): IrClassSymbol?
fun getReferencedEnumEntry(symbol: IrEnumEntrySymbol): IrEnumEntrySymbol
@@ -44,4 +46,5 @@ interface SymbolRemapper {
fun getReferencedSimpleFunction(symbol: IrSimpleFunctionSymbol): IrSimpleFunctionSymbol
fun getReferencedReturnableBlock(symbol: IrReturnableBlockSymbol): IrReturnableBlockSymbol
fun getReferencedClassifier(symbol: IrClassifierSymbol): IrClassifierSymbol
fun getReferencedTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol
}
@@ -14,6 +14,8 @@
* limitations under the License.
*/
@file:Suppress("DEPRECATION")
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.*
@@ -48,6 +50,8 @@ interface ReferenceSymbolTable {
fun referenceTypeParameter(classifier: TypeParameterDescriptor): IrTypeParameterSymbol
fun referenceVariable(descriptor: VariableDescriptor): IrVariableSymbol
fun referenceTypeAlias(descriptor: TypeAliasDescriptor): IrTypeAliasSymbol
fun enterScope(owner: DeclarationDescriptor)
fun leaveScope(owner: DeclarationDescriptor)
@@ -55,6 +59,7 @@ interface ReferenceSymbolTable {
open class SymbolTable : ReferenceSymbolTable {
@Suppress("LeakingThis")
val lazyWrapper = IrLazySymbolTable(this)
private abstract class SymbolTableBase<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> {
@@ -191,6 +196,7 @@ open class SymbolTable : ReferenceSymbolTable {
private val fieldSymbolTable = FlatSymbolTable<PropertyDescriptor, IrField, IrFieldSymbol>()
private val simpleFunctionSymbolTable = FlatSymbolTable<FunctionDescriptor, IrSimpleFunction, IrSimpleFunctionSymbol>()
private val propertySymbolTable = FlatSymbolTable<PropertyDescriptor, IrProperty, IrPropertySymbol>()
private val typeAliasSymbolTable = FlatSymbolTable<TypeAliasDescriptor, IrTypeAlias, IrTypeAliasSymbol>()
private val globalTypeParameterSymbolTable = FlatSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
private val scopedTypeParameterSymbolTable = ScopedSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
@@ -343,6 +349,14 @@ open class SymbolTable : ReferenceSymbolTable {
val unboundProperties: Set<IrPropertySymbol> get() = propertySymbolTable.unboundSymbols
override fun referenceTypeAlias(descriptor: TypeAliasDescriptor): IrTypeAliasSymbol =
typeAliasSymbolTable.referenced(descriptor) { IrTypeAliasSymbolImpl(descriptor) }
fun declareTypeAlias(descriptor: TypeAliasDescriptor, factory: (IrTypeAliasSymbol) -> IrTypeAlias): IrTypeAlias =
typeAliasSymbolTable.declare(descriptor, { IrTypeAliasSymbolImpl(descriptor) }, factory)
val unboundTypeAliases: Set<IrTypeAliasSymbol> get() = typeAliasSymbolTable.unboundSymbols
fun declareSimpleFunction(
startOffset: Int,
endOffset: Int,
@@ -9,11 +9,13 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeAbbreviation
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.impl.*
import org.jetbrains.kotlin.types.*
@@ -79,6 +81,7 @@ class TypeTranslator(
this.kotlinType = kotlinType
hasQuestionMark = approximatedType.isMarkedNullable
this.variance = variance
this.abbreviation = approximatedType.getAbbreviation()?.toIrTypeAbbreviation()
when (ktTypeDescriptor) {
is TypeParameterDescriptor -> {
classifier = resolveTypeParameter(ktTypeDescriptor)
@@ -97,6 +100,19 @@ class TypeTranslator(
}.buildTypeProjection()
}
private fun SimpleType.toIrTypeAbbreviation(): IrTypeAbbreviation {
val typeAliasDescriptor = constructor.declarationDescriptor.let {
it as? TypeAliasDescriptor
?: throw AssertionError("TypeAliasDescriptor expected: $it")
}
return IrTypeAbbreviationImpl(
symbolTable.referenceTypeAlias(typeAliasDescriptor),
isMarkedNullable,
translateTypeArguments(this.arguments),
translateTypeAnnotations(this.annotations)
)
}
private inner class LegacyTypeApproximation {
fun approximate(ktType: KotlinType): KotlinType {
@@ -49,6 +49,7 @@ interface IrElementTransformer<in D> : IrElementVisitor<IrElement, D> {
override fun visitVariable(declaration: IrVariable, data: D) = visitDeclaration(declaration, data)
override fun visitTypeParameter(declaration: IrTypeParameter, data: D) = visitDeclaration(declaration, data)
override fun visitValueParameter(declaration: IrValueParameter, data: D) = visitDeclaration(declaration, data)
override fun visitTypeAlias(declaration: IrTypeAlias, data: D) = visitDeclaration(declaration, data)
override fun visitBody(body: IrBody, data: D): IrBody =
body.also { it.transformChildren(this, data) }
@@ -84,6 +84,9 @@ abstract class IrElementTransformerVoid : IrElementTransformer<Nothing?> {
open fun visitVariable(declaration: IrVariable) = visitDeclaration(declaration)
final override fun visitVariable(declaration: IrVariable, data: Nothing?) = visitVariable(declaration)
open fun visitTypeAlias(declaration: IrTypeAlias) = visitDeclaration(declaration)
final override fun visitTypeAlias(declaration: IrTypeAlias, data: Nothing?) = visitTypeAlias(declaration)
open fun visitBody(body: IrBody): IrBody = body.transformChildren()
final override fun visitBody(body: IrBody, data: Nothing?): IrBody = visitBody(body)
@@ -40,6 +40,7 @@ interface IrElementVisitor<out R, in D> {
fun visitAnonymousInitializer(declaration: IrAnonymousInitializer, data: D) = visitDeclaration(declaration, data)
fun visitTypeParameter(declaration: IrTypeParameter, data: D) = visitDeclaration(declaration, data)
fun visitValueParameter(declaration: IrValueParameter, data: D) = visitDeclaration(declaration, data)
fun visitTypeAlias(declaration: IrTypeAlias, data: D) = visitDeclaration(declaration, data)
fun visitBody(body: IrBody, data: D) = visitElement(body, data)
fun visitExpressionBody(body: IrExpressionBody, data: D) = visitBody(body, data)
@@ -77,6 +77,9 @@ interface IrElementVisitorVoid : IrElementVisitor<Unit, Nothing?> {
fun visitValueParameter(declaration: IrValueParameter) = visitDeclaration(declaration)
override fun visitValueParameter(declaration: IrValueParameter, data: Nothing?) = visitValueParameter(declaration)
fun visitTypeAlias(declaration: IrTypeAlias) = visitDeclaration(declaration)
override fun visitTypeAlias(declaration: IrTypeAlias, data: Nothing?) = visitTypeAlias(declaration)
fun visitBody(body: IrBody) = visitElement(body)
override fun visitBody(body: IrBody, data: Nothing?) = visitBody(body)
@@ -1 +1,3 @@
FILE fqName:<root> fileName:/fileWithTypeAliasesOnly.kt
TYPEALIAS name:Bar visibility:public expandedType:kotlin.Function1<T of <root>.Bar, kotlin.String>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?]
+7
View File
@@ -0,0 +1,7 @@
typealias I = Int
typealias L<T> = List<T>
fun test1(x: L<I>) = x
fun test2(x: List<L<I>>) = x
fun test3(x: L<List<I>>) = x
fun test4(x: L<L<I>>) = x
+24
View File
@@ -0,0 +1,24 @@
FILE fqName:<root> fileName:/abbreviatedTypes.kt
TYPEALIAS name:I visibility:public expandedType:kotlin.Int
TYPEALIAS name:L visibility:public expandedType:kotlin.collections.List<T of <root>.L>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?]
FUN name:test1 visibility:public modality:FINAL <> (x:kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }) returnType:kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }
VALUE_PARAMETER name:x index:0 type:kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test1 (x: kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }): kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> } declared in <root>'
GET_VAR 'x: kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> } declared in <root>.test1' type=kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> } origin=null
FUN name:test2 visibility:public modality:FINAL <> (x:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }>) returnType:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }>
VALUE_PARAMETER name:x index:0 type:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test2 (x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }>): kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> declared in <root>'
GET_VAR 'x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> declared in <root>.test2' type=kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> origin=null
FUN name:test3 visibility:public modality:FINAL <> (x:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> }) returnType:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> }
VALUE_PARAMETER name:x index:0 type:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> }
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test3 (x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> }): kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> } declared in <root>'
GET_VAR 'x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> } declared in <root>.test3' type=kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>> } origin=null
FUN name:test4 visibility:public modality:FINAL <> (x:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> }) returnType:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> }
VALUE_PARAMETER name:x index:0 type:kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> }
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test4 (x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> }): kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> } declared in <root>'
GET_VAR 'x: kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> } declared in <root>.test4' type=kotlin.collections.List<kotlin.collections.List<kotlin.Int{ <root>.I }>>{ <root>.L<kotlin.collections.List<kotlin.Int{ <root>.I }>{ <root>.L<kotlin.Int{ <root>.I }> }> } origin=null
@@ -1607,6 +1607,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
@TestMetadata("abbreviatedTypes.kt")
public void testAbbreviatedTypes() throws Exception {
runTest("compiler/testData/ir/irText/types/abbreviatedTypes.kt");
}
public void testAllFilesPresentInTypes() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText/types"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}