Minor. Reformat

This commit is contained in:
Mikhael Bogdanov
2018-05-07 14:32:37 +02:00
parent 9277d6a160
commit 9fad3f25f3
6 changed files with 222 additions and 194 deletions
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.OperatorNameConventions
// This is what Context collects about IR. // This is what Context collects about IR.
abstract class Ir<out T: CommonBackendContext>(val context: T, val irModule: IrModuleFragment) { abstract class Ir<out T : CommonBackendContext>(val context: T, val irModule: IrModuleFragment) {
abstract val symbols: Symbols<T> abstract val symbols: Symbols<T>
@@ -26,13 +26,13 @@ abstract class Ir<out T: CommonBackendContext>(val context: T, val irModule: IrM
open fun shouldGenerateHandlerParameterForDefaultBodyFun() = false open fun shouldGenerateHandlerParameterForDefaultBodyFun() = false
} }
abstract class Symbols<out T: CommonBackendContext>(val context: T, private val symbolTable: SymbolTable) { abstract class Symbols<out T : CommonBackendContext>(val context: T, private val symbolTable: SymbolTable) {
protected val builtIns protected val builtIns
get() = context.builtIns get() = context.builtIns
protected fun builtInsPackage(vararg packageNameSegments: String) = protected fun builtInsPackage(vararg packageNameSegments: String) =
context.builtIns.builtInsModule.getPackage(FqName.fromSegments(listOf(*packageNameSegments))).memberScope context.builtIns.builtInsModule.getPackage(FqName.fromSegments(listOf(*packageNameSegments))).memberScope
// This hack allows to disable related symbol instantiation in descendants // This hack allows to disable related symbol instantiation in descendants
@@ -56,22 +56,23 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
abstract val stringBuilder: IrClassSymbol abstract val stringBuilder: IrClassSymbol
val iterator = symbolTable.referenceClass( val iterator = symbolTable.referenceClass(
builtInsPackage("kotlin", "collections").getContributedClassifier( builtInsPackage("kotlin", "collections").getContributedClassifier(
Name.identifier("Iterator"), NoLookupLocation.FROM_BACKEND Name.identifier("Iterator"), NoLookupLocation.FROM_BACKEND
) as ClassDescriptor) ) as ClassDescriptor
)
val asserts = builtInsPackage("kotlin") val asserts = builtInsPackage("kotlin")
.getContributedFunctions(Name.identifier("assert"), NoLookupLocation.FROM_BACKEND) .getContributedFunctions(Name.identifier("assert"), NoLookupLocation.FROM_BACKEND)
.map { symbolTable.referenceFunction(it) } .map { symbolTable.referenceFunction(it) }
private fun progression(name: String) = symbolTable.referenceClass( private fun progression(name: String) = symbolTable.referenceClass(
builtInsPackage("kotlin", "ranges").getContributedClassifier( builtInsPackage("kotlin", "ranges").getContributedClassifier(
Name.identifier(name), NoLookupLocation.FROM_BACKEND Name.identifier(name), NoLookupLocation.FROM_BACKEND
) as ClassDescriptor ) as ClassDescriptor
) )
val charProgression = progression("CharProgression") val charProgression = progression("CharProgression")
val intProgression = progression("IntProgression") val intProgression = progression("IntProgression")
val longProgression = progression("LongProgression") val longProgression = progression("LongProgression")
val progressionClasses = listOf(charProgression, intProgression, longProgression) val progressionClasses = listOf(charProgression, intProgression, longProgression)
val progressionClassesTypes = progressionClasses.map { it.descriptor.defaultType }.toSet() val progressionClassesTypes = progressionClasses.map { it.descriptor.defaultType }.toSet()
@@ -97,15 +98,15 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
val integerClassesTypes = integerClasses.map { it.descriptor.defaultType } val integerClassesTypes = integerClasses.map { it.descriptor.defaultType }
val arrayOf = symbolTable.referenceSimpleFunction( val arrayOf = symbolTable.referenceSimpleFunction(
builtInsPackage("kotlin").getContributedFunctions( builtInsPackage("kotlin").getContributedFunctions(
Name.identifier("arrayOf"), NoLookupLocation.FROM_BACKEND Name.identifier("arrayOf"), NoLookupLocation.FROM_BACKEND
).single() ).single()
) )
val array = symbolTable.referenceClass(builtIns.array) val array = symbolTable.referenceClass(builtIns.array)
private fun primitiveArrayClass(type: PrimitiveType) = private fun primitiveArrayClass(type: PrimitiveType) =
symbolTable.referenceClass(builtIns.getPrimitiveArrayClassDescriptor(type)) symbolTable.referenceClass(builtIns.getPrimitiveArrayClassDescriptor(type))
val byteArray = primitiveArrayClass(PrimitiveType.BYTE) val byteArray = primitiveArrayClass(PrimitiveType.BYTE)
val charArray = primitiveArrayClass(PrimitiveType.CHAR) val charArray = primitiveArrayClass(PrimitiveType.CHAR)
@@ -120,24 +121,26 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
protected fun arrayExtensionFun(type: KotlinType, name: String): IrSimpleFunctionSymbol { protected fun arrayExtensionFun(type: KotlinType, name: String): IrSimpleFunctionSymbol {
val descriptor = builtInsPackage("kotlin") val descriptor = builtInsPackage("kotlin")
.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BACKEND) .getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BACKEND)
.singleOrNull { it.valueParameters.isEmpty() .singleOrNull {
&& (it.extensionReceiverParameter?.type?.constructor?.declarationDescriptor as? ClassDescriptor)?.defaultType == type } it.valueParameters.isEmpty()
&& (it.extensionReceiverParameter?.type?.constructor?.declarationDescriptor as? ClassDescriptor)?.defaultType == type
}
?: throw Error(type.toString()) ?: throw Error(type.toString())
return symbolTable.referenceSimpleFunction(descriptor) return symbolTable.referenceSimpleFunction(descriptor)
} }
protected val arrayTypes = arrayOf( protected val arrayTypes = arrayOf(
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.BYTE), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.BYTE),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.CHAR), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.CHAR),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.SHORT), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.SHORT),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.INT), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.INT),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.LONG), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.LONG),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.FLOAT), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.FLOAT),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.DOUBLE), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.DOUBLE),
builtIns.getPrimitiveArrayKotlinType(PrimitiveType.BOOLEAN), builtIns.getPrimitiveArrayKotlinType(PrimitiveType.BOOLEAN),
builtIns.array.defaultType builtIns.array.defaultType
) )
// val arrayContentToString = arrayTypes.associateBy({ it }, { arrayExtensionFun(it, "contentToString") }) // val arrayContentToString = arrayTypes.associateBy({ it }, { arrayExtensionFun(it, "contentToString") })
// val arrayContentHashCode = arrayTypes.associateBy({ it }, { arrayExtensionFun(it, "contentHashCode") }) // val arrayContentHashCode = arrayTypes.associateBy({ it }, { arrayExtensionFun(it, "contentHashCode") })
@@ -145,17 +148,17 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
abstract val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol> abstract val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
val intAnd = symbolTable.referenceSimpleFunction( val intAnd = symbolTable.referenceSimpleFunction(
builtIns.intType.memberScope builtIns.intType.memberScope
.getContributedFunctions(OperatorNameConventions.AND, NoLookupLocation.FROM_BACKEND) .getContributedFunctions(OperatorNameConventions.AND, NoLookupLocation.FROM_BACKEND)
.single() .single()
) )
val intPlusInt = symbolTable.referenceSimpleFunction( val intPlusInt = symbolTable.referenceSimpleFunction(
builtIns.intType.memberScope builtIns.intType.memberScope
.getContributedFunctions(OperatorNameConventions.PLUS, NoLookupLocation.FROM_BACKEND) .getContributedFunctions(OperatorNameConventions.PLUS, NoLookupLocation.FROM_BACKEND)
.single { .single {
it.valueParameters.single().type == builtIns.intType it.valueParameters.single().type == builtIns.intType
} }
) )
// val valuesForEnum = symbolTable.referenceSimpleFunction( // val valuesForEnum = symbolTable.referenceSimpleFunction(
@@ -184,10 +187,10 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
fun getFunction(name: Name, receiverType: KotlinType, vararg argTypes: KotlinType) = fun getFunction(name: Name, receiverType: KotlinType, vararg argTypes: KotlinType) =
symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND)
.single { .single {
var i = 0 var i = 0
it.valueParameters.size == argTypes.size && it.valueParameters.all { type -> type == argTypes[i++] } it.valueParameters.size == argTypes.size && it.valueParameters.all { type -> type == argTypes[i++] }
} }
) )
private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrFunctionSymbol>() private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrFunctionSymbol>()
@@ -196,7 +199,7 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
var result = binaryOperatorCache[key] var result = binaryOperatorCache[key]
if (result == null) { if (result == null) {
result = symbolTable.referenceFunction(lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) result = symbolTable.referenceFunction(lhsType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND)
.single { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType } .single { it.valueParameters.size == 1 && it.valueParameters[0].type == rhsType }
) )
binaryOperatorCache[key] = result binaryOperatorCache[key] = result
} }
@@ -209,7 +212,7 @@ abstract class Symbols<out T: CommonBackendContext>(val context: T, private val
var result = unaryOperatorCache[key] var result = unaryOperatorCache[key]
if (result == null) { if (result == null) {
result = symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND) result = symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND)
.single { it.valueParameters.isEmpty() } .single { it.valueParameters.isEmpty() }
) )
unaryOperatorCache[key] = result unaryOperatorCache[key] = result
} }
@@ -45,14 +45,14 @@ import org.jetbrains.kotlin.utils.Printer
class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContext(backendContext.irBuiltIns) class IrLoweringContext(backendContext: BackendContext) : IrGeneratorContext(backendContext.irBuiltIns)
class DeclarationIrBuilder( class DeclarationIrBuilder(
backendContext: BackendContext, backendContext: BackendContext,
symbol: IrSymbol, symbol: IrSymbol,
startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET
) : IrBuilderWithScope( ) : IrBuilderWithScope(
IrLoweringContext(backendContext), IrLoweringContext(backendContext),
Scope(symbol), Scope(symbol),
startOffset, startOffset,
endOffset endOffset
) )
abstract class AbstractVariableRemapper : IrElementTransformerVoid() { abstract class AbstractVariableRemapper : IrElementTransformerVoid() {
@@ -69,10 +69,12 @@ class VariableRemapper(val mapping: Map<ValueDescriptor, IrValueParameter>) : Ab
mapping[value] mapping[value]
} }
fun BackendContext.createIrBuilder(symbol: IrSymbol, fun BackendContext.createIrBuilder(
startOffset: Int = UNDEFINED_OFFSET, symbol: IrSymbol,
endOffset: Int = UNDEFINED_OFFSET) = startOffset: Int = UNDEFINED_OFFSET,
DeclarationIrBuilder(this, symbol, startOffset, endOffset) endOffset: Int = UNDEFINED_OFFSET
) =
DeclarationIrBuilder(this, symbol, startOffset, endOffset)
fun <T : IrBuilder> T.at(element: IrElement) = this.at(element.startOffset, element.endOffset) fun <T : IrBuilder> T.at(element: IrElement) = this.at(element.startOffset, element.endOffset)
@@ -80,41 +82,45 @@ fun <T : IrBuilder> T.at(element: IrElement) = this.at(element.startOffset, elem
/** /**
* Builds [IrBlock] to be used instead of given expression. * Builds [IrBlock] to be used instead of given expression.
*/ */
inline fun IrGeneratorWithScope.irBlock(expression: IrExpression, origin: IrStatementOrigin? = null, inline fun IrGeneratorWithScope.irBlock(
resultType: KotlinType? = expression.type, expression: IrExpression, origin: IrStatementOrigin? = null,
body: IrBlockBuilder.() -> Unit) = resultType: KotlinType? = expression.type,
this.irBlock(expression.startOffset, expression.endOffset, origin, resultType, body) body: IrBlockBuilder.() -> Unit
) =
this.irBlock(expression.startOffset, expression.endOffset, origin, resultType, body)
inline fun IrGeneratorWithScope.irBlockBody(irElement: IrElement, body: IrBlockBodyBuilder.() -> Unit) = inline fun IrGeneratorWithScope.irBlockBody(irElement: IrElement, body: IrBlockBodyBuilder.() -> Unit) =
this.irBlockBody(irElement.startOffset, irElement.endOffset, body) this.irBlockBody(irElement.startOffset, irElement.endOffset, body)
fun IrBuilderWithScope.irIfThen(condition: IrExpression, thenPart: IrExpression) = fun IrBuilderWithScope.irIfThen(condition: IrExpression, thenPart: IrExpression) =
IrIfThenElseImpl(startOffset, endOffset, context.builtIns.unitType, condition, thenPart, null) IrIfThenElseImpl(startOffset, endOffset, context.builtIns.unitType, condition, thenPart, null)
fun IrBuilderWithScope.irNot(arg: IrExpression) = fun IrBuilderWithScope.irNot(arg: IrExpression) =
primitiveOp1(startOffset, endOffset, context.irBuiltIns.booleanNotSymbol, IrStatementOrigin.EXCL, arg) primitiveOp1(startOffset, endOffset, context.irBuiltIns.booleanNotSymbol, IrStatementOrigin.EXCL, arg)
fun IrBuilderWithScope.irThrow(arg: IrExpression) = fun IrBuilderWithScope.irThrow(arg: IrExpression) =
IrThrowImpl(startOffset, endOffset, context.builtIns.nothingType, arg) IrThrowImpl(startOffset, endOffset, context.builtIns.nothingType, arg)
fun IrBuilderWithScope.irCatch(catchParameter: IrVariable) = fun IrBuilderWithScope.irCatch(catchParameter: IrVariable) =
IrCatchImpl( IrCatchImpl(
startOffset, endOffset, startOffset, endOffset,
catchParameter catchParameter
) )
fun IrBuilderWithScope.irCast(arg: IrExpression, type: KotlinType, typeOperand: KotlinType) = fun IrBuilderWithScope.irCast(arg: IrExpression, type: KotlinType, typeOperand: KotlinType) =
IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.CAST, typeOperand, arg) IrTypeOperatorCallImpl(startOffset, endOffset, type, IrTypeOperator.CAST, typeOperand, arg)
fun IrBuilderWithScope.irImplicitCoercionToUnit(arg: IrExpression) = fun IrBuilderWithScope.irImplicitCoercionToUnit(arg: IrExpression) =
IrTypeOperatorCallImpl(startOffset, endOffset, context.builtIns.unitType, IrTypeOperatorCallImpl(
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT, context.builtIns.unitType, arg) startOffset, endOffset, context.builtIns.unitType,
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT, context.builtIns.unitType, arg
)
fun IrBuilderWithScope.irGetField(receiver: IrExpression, symbol: IrFieldSymbol) = fun IrBuilderWithScope.irGetField(receiver: IrExpression, symbol: IrFieldSymbol) =
IrGetFieldImpl(startOffset, endOffset, symbol, receiver) IrGetFieldImpl(startOffset, endOffset, symbol, receiver)
fun IrBuilderWithScope.irSetField(receiver: IrExpression, symbol: IrFieldSymbol, value: IrExpression) = fun IrBuilderWithScope.irSetField(receiver: IrExpression, symbol: IrFieldSymbol, value: IrExpression) =
IrSetFieldImpl(startOffset, endOffset, symbol, receiver, value) IrSetFieldImpl(startOffset, endOffset, symbol, receiver, value)
open class IrBuildingTransformer(private val context: BackendContext) : IrElementTransformerVoid() { open class IrBuildingTransformer(private val context: BackendContext) : IrElementTransformerVoid() {
private var currentBuilder: IrBuilderWithScope? = null private var currentBuilder: IrBuilderWithScope? = null
@@ -157,24 +163,24 @@ fun computeOverrides(current: ClassDescriptor, functionsFromCurrent: List<Callab
val result = mutableListOf<DeclarationDescriptor>() val result = mutableListOf<DeclarationDescriptor>()
val allSuperDescriptors = current.typeConstructor.supertypes val allSuperDescriptors = current.typeConstructor.supertypes
.flatMap { it.memberScope.getContributedDescriptors() } .flatMap { it.memberScope.getContributedDescriptors() }
.filterIsInstance<CallableMemberDescriptor>() .filterIsInstance<CallableMemberDescriptor>()
for ((name, group) in allSuperDescriptors.groupBy { it.name }) { for ((name, group) in allSuperDescriptors.groupBy { it.name }) {
OverridingUtil.generateOverridesInFunctionGroup( OverridingUtil.generateOverridesInFunctionGroup(
name, name,
/* membersFromSupertypes = */ group, /* membersFromSupertypes = */ group,
/* membersFromCurrent = */ functionsFromCurrent.filter { it.name == name }, /* membersFromCurrent = */ functionsFromCurrent.filter { it.name == name },
current, current,
object : NonReportingOverrideStrategy() { object : NonReportingOverrideStrategy() {
override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) { override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) {
result.add(fakeOverride) result.add(fakeOverride)
}
override fun conflict(fromSuper: CallableMemberDescriptor, fromCurrent: CallableMemberDescriptor) {
error("Conflict in scope of $current: $fromSuper vs $fromCurrent")
}
} }
override fun conflict(fromSuper: CallableMemberDescriptor, fromCurrent: CallableMemberDescriptor) {
error("Conflict in scope of $current: $fromSuper vs $fromCurrent")
}
}
) )
} }
@@ -184,20 +190,22 @@ fun computeOverrides(current: ClassDescriptor, functionsFromCurrent: List<Callab
class SimpleMemberScope(val members: List<DeclarationDescriptor>) : MemberScopeImpl() { class SimpleMemberScope(val members: List<DeclarationDescriptor>) : MemberScopeImpl() {
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? =
members.filterIsInstance<ClassifierDescriptor>() members.filterIsInstance<ClassifierDescriptor>()
.atMostOne { it.name == name } .atMostOne { it.name == name }
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> = override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> =
members.filterIsInstance<PropertyDescriptor>() members.filterIsInstance<PropertyDescriptor>()
.filter { it.name == name } .filter { it.name == name }
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> =
members.filterIsInstance<SimpleFunctionDescriptor>() members.filterIsInstance<SimpleFunctionDescriptor>()
.filter { it.name == name } .filter { it.name == name }
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, override fun getContributedDescriptors(
nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = kindFilter: DescriptorKindFilter,
members.filter { kindFilter.accepts(it) && nameFilter(it.name) } nameFilter: (Name) -> Boolean
): Collection<DeclarationDescriptor> =
members.filter { kindFilter.accepts(it) && nameFilter(it.name) }
override fun printScopeStructure(p: Printer) = TODO("not implemented") override fun printScopeStructure(p: Printer) = TODO("not implemented")
@@ -222,29 +230,30 @@ fun IrConstructor.callsSuper(): Boolean {
if (expression.descriptor.constructedClass == superClass) if (expression.descriptor.constructedClass == superClass)
callsSuper = true callsSuper = true
else if (expression.descriptor.constructedClass != constructedClass) else if (expression.descriptor.constructedClass != constructedClass)
throw AssertionError("Expected either call to another constructor of the class being constructed or" + throw AssertionError(
" call to super class constructor. But was: ${expression.descriptor.constructedClass}") "Expected either call to another constructor of the class being constructed or" +
" call to super class constructor. But was: ${expression.descriptor.constructedClass}"
)
} }
}) })
assert(numberOfCalls == 1, { "Expected exactly one delegating constructor call but none encountered: $descriptor" }) assert(numberOfCalls == 1, { "Expected exactly one delegating constructor call but none encountered: $descriptor" })
return callsSuper return callsSuper
} }
fun ParameterDescriptor.copyAsValueParameter(newOwner: CallableDescriptor, index: Int) fun ParameterDescriptor.copyAsValueParameter(newOwner: CallableDescriptor, index: Int) = when (this) {
= when (this) {
is ValueParameterDescriptor -> this.copy(newOwner, name, index) is ValueParameterDescriptor -> this.copy(newOwner, name, index)
is ReceiverParameterDescriptor -> ValueParameterDescriptorImpl( is ReceiverParameterDescriptor -> ValueParameterDescriptorImpl(
containingDeclaration = newOwner, containingDeclaration = newOwner,
original = null, original = null,
index = index, index = index,
annotations = annotations, annotations = annotations,
name = name, name = name,
outType = type, outType = type,
declaresDefaultValue = false, declaresDefaultValue = false,
isCrossinline = false, isCrossinline = false,
isNoinline = false, isNoinline = false,
varargElementType = null, varargElementType = null,
source = source source = source
) )
else -> throw Error("Unexpected parameter descriptor: $this") else -> throw Error("Unexpected parameter descriptor: $this")
} }
@@ -25,13 +25,14 @@ import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irGet import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irTemporary import org.jetbrains.kotlin.ir.builders.irTemporary
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSymbolDeclaration
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.functions import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.type
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -54,7 +55,7 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
private val builtIns = context.builtIns private val builtIns = context.builtIns
private val typesWithSpecialAppendFunction = private val typesWithSpecialAppendFunction =
PrimitiveType.values().map { builtIns.getPrimitiveKotlinType(it) } + builtIns.stringType PrimitiveType.values().map { builtIns.getPrimitiveKotlinType(it) } + builtIns.stringType
private val nameToString = Name.identifier("toString") private val nameToString = Name.identifier("toString")
private val nameAppend = Name.identifier("append") private val nameAppend = Name.identifier("append")
@@ -77,16 +78,16 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
private val appendFunctions: Map<KotlinType, IrFunctionSymbol?> = private val appendFunctions: Map<KotlinType, IrFunctionSymbol?> =
typesWithSpecialAppendFunction.map { type -> typesWithSpecialAppendFunction.map { type ->
type to stringBuilder.functions.toList().atMostOne { type to stringBuilder.functions.toList().atMostOne {
it.descriptor.name == nameAppend && it.descriptor.name == nameAppend &&
it.owner.valueParameters.size == 1 && it.owner.valueParameters.size == 1 &&
it.owner.valueParameters.single().type == type it.owner.valueParameters.single().type == type
} }
}.toMap() }.toMap()
private fun typeToAppendFunction(type : KotlinType) : IrFunctionSymbol { private fun typeToAppendFunction(type: KotlinType): IrFunctionSymbol {
return appendFunctions[type]?:defaultAppendFunction return appendFunctions[type] ?: defaultAppendFunction
} }
override fun visitStringConcatenation(expression: IrStringConcatenation): IrExpression { override fun visitStringConcatenation(expression: IrStringConcatenation): IrExpression {
@@ -116,7 +117,7 @@ private class StringConcatenationTransformer(val lower: StringConcatenationLower
with(declaration) { with(declaration) {
buildersStack.add( buildersStack.add(
context.createIrBuilder(declaration.symbol, startOffset, endOffset) context.createIrBuilder(declaration.symbol, startOffset, endOffset)
) )
transformChildrenVoid(this@StringConcatenationTransformer) transformChildrenVoid(this@StringConcatenationTransformer)
buildersStack.removeAt(buildersStack.lastIndex) buildersStack.removeAt(buildersStack.lastIndex)
@@ -27,10 +27,18 @@ interface DefaultImplsClassDescriptor : ClassDescriptor {
} }
class DefaultImplsClassDescriptorImpl( class DefaultImplsClassDescriptorImpl(
name: Name, name: Name,
override val correspondingInterface: ClassDescriptor, override val correspondingInterface: ClassDescriptor,
sourceElement: SourceElement sourceElement: SourceElement
) : DefaultImplsClassDescriptor, KnownClassDescriptor(name, correspondingInterface, sourceElement, ClassKind.CLASS, Modality.FINAL, Visibilities.PUBLIC, Annotations.EMPTY) { ) : DefaultImplsClassDescriptor, KnownClassDescriptor(
name,
correspondingInterface,
sourceElement,
ClassKind.CLASS,
Modality.FINAL,
Visibilities.PUBLIC,
Annotations.EMPTY
) {
init { init {
initialize(emptyList(), listOf(correspondingInterface.module.builtIns.anyType)) initialize(emptyList(), listOf(correspondingInterface.module.builtIns.anyType))
} }
@@ -24,15 +24,15 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
class FileClassDescriptorImpl( class FileClassDescriptorImpl(
name: Name, name: Name,
containingDeclaration: PackageFragmentDescriptor, containingDeclaration: PackageFragmentDescriptor,
supertypes: List<KotlinType>, supertypes: List<KotlinType>,
sourceElement: SourceElement, sourceElement: SourceElement,
annotations: Annotations annotations: Annotations
) : FileClassDescriptor, KnownClassDescriptor( ) : FileClassDescriptor, KnownClassDescriptor(
name, containingDeclaration, sourceElement, name, containingDeclaration, sourceElement,
ClassKind.CLASS, Modality.FINAL, Visibilities.PUBLIC, ClassKind.CLASS, Modality.FINAL, Visibilities.PUBLIC,
annotations annotations
) { ) {
init { init {
initialize(emptyList(), supertypes) initialize(emptyList(), supertypes)
@@ -28,85 +28,92 @@ interface JvmDescriptorWithExtraFlags {
} }
class JvmPropertyDescriptorImpl private constructor( class JvmPropertyDescriptorImpl private constructor(
containingDeclaration: DeclarationDescriptor, containingDeclaration: DeclarationDescriptor,
original: PropertyDescriptor?, original: PropertyDescriptor?,
annotations: Annotations, annotations: Annotations,
modality: Modality, modality: Modality,
visibility: Visibility, visibility: Visibility,
override val extraFlags: Int, override val extraFlags: Int,
isVar: Boolean, isVar: Boolean,
name: Name, name: Name,
kind: CallableMemberDescriptor.Kind, kind: CallableMemberDescriptor.Kind,
source: SourceElement, source: SourceElement,
isLateInit: Boolean, isLateInit: Boolean,
isConst: Boolean, isConst: Boolean,
isExpect: Boolean, isExpect: Boolean,
isActual: Boolean isActual: Boolean
) : JvmDescriptorWithExtraFlags, PropertyDescriptorImpl( ) : JvmDescriptorWithExtraFlags, PropertyDescriptorImpl(
containingDeclaration, original, annotations, modality, visibility, isVar, containingDeclaration, original, annotations, modality, visibility, isVar,
name, kind, source, isLateInit, isConst, isExpect, isActual, /* isExternal = */ false, false name, kind, source, isLateInit, isConst, isExpect, isActual, /* isExternal = */ false, false
) { ) {
override fun createSubstitutedCopy( override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor, newOwner: DeclarationDescriptor,
newModality: Modality, newModality: Modality,
newVisibility: Visibility, newVisibility: Visibility,
original: PropertyDescriptor?, original: PropertyDescriptor?,
kind: CallableMemberDescriptor.Kind, kind: CallableMemberDescriptor.Kind,
newName: Name newName: Name
): PropertyDescriptorImpl = ): PropertyDescriptorImpl =
JvmPropertyDescriptorImpl( JvmPropertyDescriptorImpl(
newOwner, original, annotations, newModality, newVisibility, extraFlags, isVar, newName, kind, newOwner, original, annotations, newModality, newVisibility, extraFlags, isVar, newName, kind,
SourceElement.NO_SOURCE, isLateInit, isConst, isExpect, isActual SourceElement.NO_SOURCE, isLateInit, isConst, isExpect, isActual
) )
companion object { companion object {
fun createStaticVal( fun createStaticVal(
name: Name, name: Name,
type: KotlinType, type: KotlinType,
containingDeclaration: DeclarationDescriptor, containingDeclaration: DeclarationDescriptor,
annotations: Annotations, annotations: Annotations,
modality: Modality, modality: Modality,
visibility: Visibility, visibility: Visibility,
extraFlags: Int, extraFlags: Int,
source: SourceElement source: SourceElement
): PropertyDescriptorImpl = ): PropertyDescriptorImpl =
JvmPropertyDescriptorImpl( JvmPropertyDescriptorImpl(
containingDeclaration, null, annotations, modality, visibility, extraFlags, false, name, containingDeclaration, null, annotations, modality, visibility, extraFlags, false, name,
CallableMemberDescriptor.Kind.SYNTHESIZED, source, false, false, false, false CallableMemberDescriptor.Kind.SYNTHESIZED, source, false, false, false, false
).initialize(type) ).initialize(type)
fun createFinalField( fun createFinalField(
name: Name, name: Name,
type: KotlinType, type: KotlinType,
classDescriptor: ClassDescriptor, classDescriptor: ClassDescriptor,
annotations: Annotations, annotations: Annotations,
visibility: Visibility, visibility: Visibility,
extraFlags: Int, extraFlags: Int,
source: SourceElement source: SourceElement
): PropertyDescriptorImpl = ): PropertyDescriptorImpl =
JvmPropertyDescriptorImpl( JvmPropertyDescriptorImpl(
classDescriptor, null, annotations, Modality.FINAL, visibility, extraFlags, false, name, classDescriptor, null, annotations, Modality.FINAL, visibility, extraFlags, false, name,
CallableMemberDescriptor.Kind.SYNTHESIZED, source, false, false, false, false CallableMemberDescriptor.Kind.SYNTHESIZED, source, false, false, false, false
).initialize(type, dispatchReceiverParameter = classDescriptor.thisAsReceiverParameter) ).initialize(type, dispatchReceiverParameter = classDescriptor.thisAsReceiverParameter)
} }
} }
class JvmFunctionDescriptorImpl( class JvmFunctionDescriptorImpl(
containingDeclaration: DeclarationDescriptor, containingDeclaration: DeclarationDescriptor,
original: FunctionDescriptor?, original: FunctionDescriptor?,
annotations: Annotations, annotations: Annotations,
name: Name, name: Name,
kind: CallableMemberDescriptor.Kind, kind: CallableMemberDescriptor.Kind,
source: SourceElement, source: SourceElement,
override val extraFlags: Int override val extraFlags: Int
) : JvmDescriptorWithExtraFlags, FunctionDescriptorImpl( ) : JvmDescriptorWithExtraFlags, FunctionDescriptorImpl(
containingDeclaration, original, annotations, containingDeclaration, original, annotations,
name, kind, source name, kind, source
) { ) {
override fun createSubstitutedCopy(newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind, newName: Name?, annotations: Annotations, source: SourceElement): FunctionDescriptorImpl { override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor,
original: FunctionDescriptor?,
kind: CallableMemberDescriptor.Kind,
newName: Name?,
annotations: Annotations,
source: SourceElement
): FunctionDescriptorImpl {
return JvmFunctionDescriptorImpl( return JvmFunctionDescriptorImpl(
newOwner, original, annotations, name, kind, newOwner, original, annotations, name, kind,
SourceElement.NO_SOURCE, extraFlags SourceElement.NO_SOURCE, extraFlags
) )
} }
} }