JVM_IR: JvmNameProvider

This commit is contained in:
Georgy Bronnikov
2020-04-20 20:59:41 +03:00
parent 52df54ae49
commit 9561cc8270
6 changed files with 39 additions and 91 deletions
@@ -34,7 +34,7 @@ object JvmBackendFacade {
val mangler = JvmManglerDesc(MainFunctionDetector(state.bindingContext, state.languageVersionSettings))
val signaturer = JvmIdSignatureDescriptor(mangler)
val psi2ir = Psi2IrTranslator(state.languageVersionSettings, signaturer = signaturer)
val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext, extensions = extensions)
val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext, JvmNameProvider, extensions = extensions)
val pluginExtensions = IrGenerationExtension.getInstances(state.project)
for (extension in pluginExtensions) {
@@ -270,7 +270,6 @@ private val kotlinNothingValueExceptionPhase = makeIrFilePhase(
@Suppress("Reformat")
private val jvmFilePhases =
renameAnonymousParametersLowering then
typeAliasAnnotationMethodsPhase then
stripTypeAliasDeclarationsPhase then
provisionalFunctionExpressionPhase then
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2020 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.backend.jvm
import org.jetbrains.kotlin.codegen.getNameForDestructuredParameterOrNull
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.util.NameProvider
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
object JvmNameProvider : NameProvider {
override fun nameForDeclaration(descriptor: DeclarationDescriptor): Name {
if (descriptor is ValueParameterDescriptor)
return nameForValueParameter(descriptor)
return NameProvider.DEFAULT.nameForDeclaration(descriptor)
}
private fun nameForValueParameter(descriptor: ValueParameterDescriptor): Name {
getNameForDestructuredParameterOrNull(descriptor)?.let { return Name.identifier(it) }
if (DescriptorToSourceUtils.getSourceFromDescriptor(descriptor)?.safeAs<KtParameter>()?.isSingleUnderscore == true) {
return Name.identifier("\$noName_${descriptor.index}")
}
return descriptor.name
}
}
@@ -1,85 +0,0 @@
/*
* Copyright 2010-2020 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.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.codegen.getNameForDestructuredParameterOrNull
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.descriptors.WrappedReceiverParameterDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedValueParameterDescriptor
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
internal val renameAnonymousParametersLowering = makeIrFilePhase(
::RenameAnonymousParametersLowering,
name = "RenameAnonymousParameters",
description = "Mangles variable names for anonymous parameters (only allowed in lambdas)"
)
private class RenameAnonymousParametersLowering(val context: JvmBackendContext) : FileLoweringPass, IrElementTransformerVoidWithContext() {
val oldParameterToNew: MutableMap<IrValueParameter, IrValueParameter> = mutableMapOf()
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid()
override fun visitValueParameterNew(declaration: IrValueParameter) =
declaration.computeNewParameterName()?.let { name ->
val descriptor = if (declaration.descriptor is ReceiverParameterDescriptor) {
WrappedReceiverParameterDescriptor(declaration.descriptor.annotations)
} else {
WrappedValueParameterDescriptor(declaration.descriptor.annotations)
}
IrValueParameterImpl(
declaration.startOffset, declaration.endOffset,
declaration.origin,
IrValueParameterSymbolImpl(descriptor),
name,
declaration.index,
declaration.type,
declaration.varargElementType,
declaration.isCrossinline,
declaration.isNoinline
).apply {
descriptor.bind(this)
parent = declaration.parent
defaultValue = declaration.defaultValue
annotations += declaration.annotations
oldParameterToNew[declaration] = this
}
} ?: super.visitValueParameterNew(declaration)
override fun visitGetValue(expression: IrGetValue): IrExpression {
val oldParameter = expression.symbol.owner
return oldParameterToNew[oldParameter]?.let {
IrGetValueImpl(expression.startOffset, expression.endOffset, it.type, it.symbol)
} ?: super.visitGetValue(expression)
}
private fun IrValueParameter.computeNewParameterName(): Name? {
// Consistent with naming on old (non-IR) backend; see FunctionCodegen.java.
descriptor.safeAs<ValueParameterDescriptor>()?.let {
getNameForDestructuredParameterOrNull(it)?.let { return Name.identifier(it) }
if (DescriptorToSourceUtils.getSourceFromDescriptor(it)?.safeAs<KtParameter>()?.isSingleUnderscore == true) {
return Name.identifier("\$noName_${it.index}")
}
}
return null
}
}
@@ -48,9 +48,10 @@ class Psi2IrTranslator(
moduleDescriptor: ModuleDescriptor,
ktFiles: Collection<KtFile>,
bindingContext: BindingContext,
generatorExtensions: GeneratorExtensions
generatorExtensions: GeneratorExtensions,
nameProvider: NameProvider = NameProvider.DEFAULT
): IrModuleFragment {
val context = createGeneratorContext(moduleDescriptor, bindingContext, extensions = generatorExtensions)
val context = createGeneratorContext(moduleDescriptor, bindingContext, nameProvider, extensions = generatorExtensions)
val irProviders = generateTypicalIrProviderList(
moduleDescriptor, context.irBuiltIns, context.symbolTable, extensions = generatorExtensions
)
@@ -60,7 +61,8 @@ class Psi2IrTranslator(
fun createGeneratorContext(
moduleDescriptor: ModuleDescriptor,
bindingContext: BindingContext,
symbolTable: SymbolTable = SymbolTable(signaturer),
nameProvider: NameProvider = NameProvider.DEFAULT,
symbolTable: SymbolTable = SymbolTable(signaturer, nameProvider),
extensions: GeneratorExtensions = GeneratorExtensions()
): GeneratorContext =
createGeneratorContext(
@@ -63,7 +63,7 @@ class JsCoreScriptingCompiler(
val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler)
val psi2ir = Psi2IrTranslator(environment.configuration.languageVersionSettings, signaturer = signaturer)
val psi2irContext = psi2ir.createGeneratorContext(module, bindingContext, symbolTable)
val psi2irContext = psi2ir.createGeneratorContext(module, bindingContext, symbolTable = symbolTable)
val providers = generateTypicalIrProviderList(module, psi2irContext.irBuiltIns, psi2irContext.symbolTable)
val irModuleFragment = psi2ir.generateModuleFragment(psi2irContext, files, providers, null) // TODO: deserializer