Chachako
11/06/2021, 5:14 PMval
of property to var
in IrElementTransformerVoid.visitPropertyNew
, but I found that isVar
is a immutable value. What should I do?Chachako
11/06/2021, 5:17 PMoverride fun visitPropertyNew(declaration: IrProperty): IrStatement {
var newDeclaration =
// Some logic...
return super.visitPropertyNew(newDeclaration.copy(isVar = false))
}
fun IrProperty.copy(
startOffset: Int = this.startOffset,
endOffset: Int = this.endOffset,
origin: IrDeclarationOrigin = this.origin,
symbol: IrPropertySymbol = IrPropertyPublicSymbolImpl(this.symbol.signature!!, this.symbol.descriptor),
name: Name = this.name,
visibility: DescriptorVisibility = this.visibility,
modality: Modality = this.modality,
isVar: Boolean = this.isVar,
isConst: Boolean = this.isConst,
isLateinit: Boolean = this.isLateinit,
isDelegated: Boolean = this.isDelegated,
isExternal: Boolean = this.isExternal,
isExpect: Boolean = this.isExpect,
isFakeOverride: Boolean = this.isFakeOverride,
containerSource: DeserializedContainerSource? = this.containerSource,
parent: IrDeclarationParent = this.parent,
annotations: List<IrConstructorCall> = this.annotations,
backingField: IrField? = this.backingField,
getter: IrSimpleFunction? = this.getter,
setter: IrSimpleFunction? = this.setter,
overriddenSymbols: List<IrPropertySymbol> = this.overriddenSymbols,
metadata: MetadataSource? = this.metadata,
attributeOwnerId: IrAttributeContainer = this.attributeOwnerId,
) = IrPropertyImpl(
startOffset,
endOffset,
origin,
symbol,
name,
visibility,
modality,
isVar,
isConst,
isLateinit,
isDelegated,
isExternal,
isExpect,
isFakeOverride,
containerSource
).also {
it.parent = parent
it.annotations = annotations
it.backingField = backingField
it.getter = getter
it.setter = setter
it.overriddenSymbols = overriddenSymbols
it.metadata = metadata
it.attributeOwnerId = attributeOwnerId
}
Exception:
Caused by: java.lang.IllegalStateException: Parent of this declaration is not a class: PROPERTY name:lazy visibility:public modality:FINAL [var]
at org.jetbrains.kotlin.ir.util.IrUtilsKt.getParentAsClass(IrUtils.kt:268)
at org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper.mapFunctionName(MethodSignatureMapper.kt:86)
at org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper.mapFunctionName$default(MethodSignatureMapper.kt:70)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering.computeSyntheticMethodName(JvmPropertiesLowering.kt:221)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering.createSyntheticMethodForAnnotations(JvmPropertiesLowering.kt:164)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering.lowerProperty(JvmPropertiesLowering.kt:154)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering.access$lowerProperty(JvmPropertiesLowering.kt:37)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering$visitClassNew$1.invoke(JvmPropertiesLowering.kt:44)
at org.jetbrains.kotlin.backend.jvm.lower.JvmPropertiesLowering$visitClassNew$1.invoke(JvmPropertiesLowering.kt:44)
at org.jetbrains.kotlin.ir.util.TransformKt.transformDeclarationsFlat(transform.kt:94)
shikasd
11/06/2021, 5:52 PMlhwdev
11/07/2021, 6:39 AMlhwdev
11/07/2021, 6:42 AMIrElement.deepCopyIrWithSymbol
or something which I cannot remember the exact name..Chachako
11/07/2021, 6:48 AMlhwdev
11/07/2021, 6:56 AMr
)
2. Let root element e
, e.accept(r)
(as r itself is IrElementVisitor)
3. Create your class extending DeepCopyIrTreeWithSymbols. When you need a symbol, do not use the original symbol as-is, instead use symbol from symbolRemapper.getDeclared***
(from declaration) or symbolRemapper.getReferenced***
(from other). Start with copy-pasting corresponding functions from https://github.com/JetBrains/kotlin/blob/master/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeepCopyIrTreeWithSymbols.kt.
4. Transform ir element with 3.
Note that all the metadatas will be lost. You need to copy it by your hands if needed. (don't know a lot about this)lhwdev
11/07/2021, 6:56 AMChachako
11/07/2021, 8:04 AMsuper.visitPropertyNew(declaration.nonFinal())
fun IrProperty.nonFinal() = deepCopyWithSymbols(parent) { symbolRemapper, typeRemapper ->
object : DeepCopyIrTreeWithSymbols(symbolRemapper, typeRemapper) {
override fun visitField(declaration: IrField): IrField = declaration.factory.createField(
declaration.startOffset, declaration.endOffset,
mapDeclarationOrigin(declaration.origin),
symbolRemapper.getDeclaredField(declaration.symbol),
declaration.symbol.owner.name,
declaration.type.remapType(),
declaration.visibility,
isFinal = false,
isExternal = declaration.isExternal,
isStatic = declaration.isStatic,
).apply {
transformAnnotations(declaration)
annotations = declaration.annotations.transform()
initializer = declaration.initializer?.transform()
}
}
}
lhwdev
11/07/2021, 8:20 AMChachako
11/07/2021, 8:30 AMmajindong
09/03/2022, 9:05 AMmcpiroman
09/06/2022, 7:20 AMPHondogo
09/10/2022, 1:10 PM