[IR] Mark Value Parameters which have default argument
This commit is contained in:
+19
-6
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
|
||||
import org.jetbrains.kotlin.ir.symbols.*
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
||||
import org.jetbrains.kotlin.ir.util.withScope
|
||||
@@ -120,7 +121,11 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) {
|
||||
)
|
||||
}
|
||||
|
||||
protected fun generateValueParameterDeclarations(irFunction: IrFunction, functionDescriptor: FunctionDescriptor) {
|
||||
protected fun generateValueParameterDeclarations(
|
||||
irFunction: IrFunction,
|
||||
functionDescriptor: FunctionDescriptor,
|
||||
defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody?
|
||||
) {
|
||||
|
||||
// TODO: KtElements
|
||||
|
||||
@@ -135,18 +140,23 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) {
|
||||
// Declare all the value parameters up first.
|
||||
irFunction.valueParameters = functionDescriptor.valueParameters.map { valueParameterDescriptor ->
|
||||
val ktParameter = DescriptorToSourceUtils.getSourceFromDescriptor(valueParameterDescriptor) as? KtParameter
|
||||
declareParameter(valueParameterDescriptor, ktParameter, irFunction)
|
||||
declareParameter(valueParameterDescriptor, ktParameter, irFunction).also {
|
||||
it.defaultValue = irFunction.defaultArgumentFactory(valueParameterDescriptor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun generateConstructor(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassConstructorDescriptor, symbol: IrConstructorSymbol): IrConstructor {
|
||||
fun generateConstructor(
|
||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassConstructorDescriptor, symbol: IrConstructorSymbol,
|
||||
defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { null }
|
||||
): IrConstructor {
|
||||
val irConstructor = IrConstructorImpl(startOffset, endOffset, origin, symbol, IrUninitializedType)
|
||||
irConstructor.metadata = MetadataSource.Function(descriptor)
|
||||
|
||||
symbolTable.withScope(descriptor) {
|
||||
val ctorTypeParameters = descriptor.typeParameters.filter { it.containingDeclaration === descriptor }
|
||||
generateScopedTypeParameterDeclarations(irConstructor, ctorTypeParameters)
|
||||
generateValueParameterDeclarations(irConstructor, descriptor)
|
||||
generateValueParameterDeclarations(irConstructor, descriptor, defaultArgumentFactory)
|
||||
irConstructor.returnType = descriptor.returnType.toIrType()
|
||||
}
|
||||
|
||||
@@ -157,14 +167,17 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) {
|
||||
irFunction.overriddenSymbols = overridens.map { symbolTable.referenceSimpleFunction(it.original) }
|
||||
}
|
||||
|
||||
fun generateSimpleFunction(startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: FunctionDescriptor, symbol: IrSimpleFunctionSymbol): IrSimpleFunction {
|
||||
fun generateSimpleFunction(
|
||||
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: FunctionDescriptor, symbol: IrSimpleFunctionSymbol,
|
||||
defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { null }
|
||||
): IrSimpleFunction {
|
||||
val irFunction = IrFunctionImpl(startOffset, endOffset, origin, symbol, IrUninitializedType)
|
||||
irFunction.metadata = MetadataSource.Function(descriptor)
|
||||
|
||||
symbolTable.withScope(descriptor) {
|
||||
generateOverridenSymbols(irFunction, descriptor.overriddenDescriptors)
|
||||
generateScopedTypeParameterDeclarations(irFunction, descriptor.typeParameters)
|
||||
generateValueParameterDeclarations(irFunction, descriptor)
|
||||
generateValueParameterDeclarations(irFunction, descriptor, defaultArgumentFactory)
|
||||
irFunction.returnType = descriptor.returnType?.toIrType() ?: error("Expected return type $descriptor")
|
||||
}
|
||||
|
||||
|
||||
+18
-2
@@ -8,8 +8,13 @@ package org.jetbrains.kotlin.psi2ir.generators
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.*
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrErrorTypeImpl
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDescriptorVisitor<Unit, IrDeclarationContainer?> {
|
||||
|
||||
@@ -26,6 +31,17 @@ class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDes
|
||||
return this
|
||||
}
|
||||
|
||||
private val errorType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT)
|
||||
|
||||
private fun IrFunction.defaultArgumentFactory(descriptor: ValueParameterDescriptor): IrExpressionBody? {
|
||||
if (!descriptor.declaresDefaultValue()) return null
|
||||
|
||||
val description = "Default Argument Value stub for ${descriptor.name}|${descriptor.index}"
|
||||
return IrExpressionBodyImpl(IrErrorExpressionImpl(startOffset, endOffset, errorType, description))
|
||||
}
|
||||
|
||||
private val defaultFactoryReference: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { defaultArgumentFactory(it) }
|
||||
|
||||
override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, data: IrDeclarationContainer?) {
|
||||
error("Unexpected declaration descriptor $descriptor")
|
||||
}
|
||||
@@ -39,7 +55,7 @@ class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDes
|
||||
}
|
||||
|
||||
private fun createFunctionStub(descriptor: FunctionDescriptor, symbol: IrSimpleFunctionSymbol): IrSimpleFunction {
|
||||
return generator.generateSimpleFunction(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
|
||||
return generator.generateSimpleFunction(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol, defaultFactoryReference)
|
||||
}
|
||||
|
||||
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: IrDeclarationContainer?) {
|
||||
@@ -97,7 +113,7 @@ class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDes
|
||||
}
|
||||
|
||||
private fun createConstructorStub(descriptor: ClassConstructorDescriptor, symbol: IrConstructorSymbol): IrConstructor {
|
||||
return generator.generateConstructor(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol)
|
||||
return generator.generateConstructor(offset, offset, IrDeclarationOrigin.DEFINED, descriptor, symbol, defaultFactoryReference)
|
||||
}
|
||||
|
||||
override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor, data: IrDeclarationContainer?) {
|
||||
|
||||
Reference in New Issue
Block a user