Make declaration containers mutable.
This commit is contained in:
committed by
Dmitry Petrov
parent
4b9c5effd3
commit
4c88ad408f
@@ -26,9 +26,9 @@ class JvmBackend(context: JvmBackendContext) {
|
||||
private val codegen = JvmCodegen(context)
|
||||
|
||||
fun generateFile(irFile: IrFile) {
|
||||
val loweredFile = lower.lower(irFile)
|
||||
lower.lower(irFile)
|
||||
|
||||
for (loweredClass in loweredFile.declarations) {
|
||||
for (loweredClass in irFile.declarations) {
|
||||
if (loweredClass !is IrClass) {
|
||||
throw AssertionError("File-level declaration should be IrClass after JvmLower, got: " + loweredClass.render())
|
||||
}
|
||||
|
||||
@@ -20,11 +20,7 @@ import org.jetbrains.kotlin.backend.jvm.lower.FileClassLowering
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
|
||||
class JvmLower(val context: JvmBackendContext) {
|
||||
fun lower(irFile: IrFile): IrFile {
|
||||
var newIrFile = irFile
|
||||
|
||||
newIrFile = FileClassLowering(context).lower(newIrFile)
|
||||
|
||||
return newIrFile
|
||||
fun lower(irFile: IrFile) {
|
||||
FileClassLowering(context).lower(irFile)
|
||||
}
|
||||
}
|
||||
|
||||
+4
-5
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
|
||||
import java.util.*
|
||||
|
||||
class FileClassLowering(val context: JvmBackendContext) {
|
||||
fun lower(irFile: IrFile): IrFile {
|
||||
fun lower(irFile: IrFile) {
|
||||
val classes = ArrayList<IrClass>()
|
||||
val fileClassMembers = ArrayList<IrDeclaration>()
|
||||
|
||||
@@ -36,16 +36,15 @@ class FileClassLowering(val context: JvmBackendContext) {
|
||||
fileClassMembers.add(it)
|
||||
}
|
||||
|
||||
if (fileClassMembers.isEmpty()) return irFile
|
||||
if (fileClassMembers.isEmpty()) return
|
||||
|
||||
val fileClassDescriptor = context.jvmFileClassProvider.createFileClassDescriptor(irFile.fileEntry, irFile.packageFragmentDescriptor)
|
||||
|
||||
val irFileClass = IrClassImpl(0, irFile.fileEntry.maxOffset, IrDeclarationOrigin.DEFINED, fileClassDescriptor, fileClassMembers)
|
||||
classes.add(irFileClass)
|
||||
|
||||
val newIrFile = irFile.toBuilder().apply { declarations.clear(); declarations.addAll(classes) }.build()
|
||||
|
||||
return newIrFile
|
||||
irFile.declarations.clear()
|
||||
irFile.declarations.addAll(classes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,11 +43,11 @@ class ModuleGenerator(override val context: GeneratorContext) : Generator {
|
||||
val irFile = createEmptyIrFile(ktFile)
|
||||
|
||||
for (ktAnnotationEntry in ktFile.annotationEntries) {
|
||||
irFile.addAnnotation(getOrFail(BindingContext.ANNOTATION, ktAnnotationEntry))
|
||||
irFile.fileAnnotations.add(getOrFail(BindingContext.ANNOTATION, ktAnnotationEntry))
|
||||
}
|
||||
|
||||
for (ktDeclaration in ktFile.declarations) {
|
||||
irFile.addDeclaration(irDeclarationGenerator.generateMemberDeclaration(ktDeclaration))
|
||||
irFile.declarations.add(irDeclarationGenerator.generateMemberDeclaration(ktDeclaration))
|
||||
}
|
||||
|
||||
return irFile
|
||||
|
||||
@@ -23,15 +23,6 @@ interface IrClass : IrDeclaration, IrDeclarationContainer {
|
||||
get() = IrDeclarationKind.CLASS
|
||||
|
||||
override val descriptor: ClassDescriptor
|
||||
|
||||
interface Builder : IrDeclarationContainer.Builder {
|
||||
val startOffset: Int
|
||||
val endOffset: Int
|
||||
var origin: IrDeclarationOrigin
|
||||
val descriptor: ClassDescriptor
|
||||
|
||||
override fun build(): IrClass
|
||||
}
|
||||
}
|
||||
|
||||
fun IrClass.getInstanceInitializerMembers() =
|
||||
|
||||
+1
-9
@@ -17,13 +17,5 @@
|
||||
package org.jetbrains.kotlin.ir.declarations
|
||||
|
||||
interface IrDeclarationContainer {
|
||||
val declarations: List<IrDeclaration>
|
||||
|
||||
fun toBuilder(): Builder
|
||||
|
||||
interface Builder {
|
||||
val declarations: MutableList<IrDeclaration>
|
||||
|
||||
fun build(): IrDeclarationContainer
|
||||
}
|
||||
val declarations: MutableList<IrDeclaration>
|
||||
}
|
||||
@@ -26,19 +26,9 @@ import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
interface IrFile : IrElement, IrDeclarationContainer {
|
||||
val fileEntry: SourceManager.FileEntry
|
||||
val fileAnnotations: List<AnnotationDescriptor>
|
||||
val fileAnnotations: MutableList<AnnotationDescriptor>
|
||||
val packageFragmentDescriptor: PackageFragmentDescriptor
|
||||
|
||||
override fun toBuilder(): Builder
|
||||
|
||||
interface Builder : IrDeclarationContainer.Builder {
|
||||
val fileEntry: SourceManager.FileEntry
|
||||
val fileAnnotations: MutableList<AnnotationDescriptor>
|
||||
val packageFragmentDescriptor: PackageFragmentDescriptor
|
||||
|
||||
override fun build(): IrFile
|
||||
}
|
||||
|
||||
override fun <D> transform(transformer: IrElementTransformer<D>, data: D): IrFile =
|
||||
accept(transformer, data) as IrFile
|
||||
}
|
||||
|
||||
@@ -48,9 +48,6 @@ class IrClassImpl(
|
||||
declarations.addAll(members)
|
||||
}
|
||||
|
||||
override fun toBuilder(): IrDeclarationContainer.Builder =
|
||||
IrClassBuilderImpl(this)
|
||||
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
|
||||
visitor.visitClass(this, data)
|
||||
|
||||
@@ -64,17 +61,3 @@ class IrClassImpl(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IrClassBuilderImpl(
|
||||
override var startOffset: Int,
|
||||
override var endOffset: Int,
|
||||
override var origin: IrDeclarationOrigin,
|
||||
override var descriptor: ClassDescriptor,
|
||||
override val declarations: MutableList<IrDeclaration>
|
||||
) : IrClass.Builder {
|
||||
constructor(irClass: IrClass) : this(irClass.startOffset, irClass.endOffset, irClass.origin, irClass.descriptor,
|
||||
irClass.declarations.toMutableList())
|
||||
|
||||
override fun build(): IrClass =
|
||||
IrClassImpl(startOffset, endOffset, origin, descriptor, declarations)
|
||||
}
|
||||
@@ -34,33 +34,14 @@ class IrFileImpl(
|
||||
fileEntry: SourceManager.FileEntry, packageFragmentDescriptor: PackageFragmentDescriptor,
|
||||
fileAnnotations: List<AnnotationDescriptor>, declarations: List<IrDeclaration>
|
||||
) : this(fileEntry, packageFragmentDescriptor) {
|
||||
addAllAnnotations(fileAnnotations)
|
||||
addAll(declarations)
|
||||
this.fileAnnotations.addAll(fileAnnotations)
|
||||
this.declarations.addAll(declarations)
|
||||
}
|
||||
|
||||
override val fileAnnotations: MutableList<AnnotationDescriptor> = SmartList()
|
||||
|
||||
fun addAnnotation(annotation: AnnotationDescriptor) {
|
||||
fileAnnotations.add(annotation)
|
||||
}
|
||||
|
||||
fun addAllAnnotations(annotations: List<AnnotationDescriptor>) {
|
||||
fileAnnotations.addAll(annotations)
|
||||
}
|
||||
|
||||
override val declarations: MutableList<IrDeclaration> = ArrayList()
|
||||
|
||||
fun addDeclaration(declaration: IrDeclaration) {
|
||||
declarations.add(declaration)
|
||||
}
|
||||
|
||||
fun addAll(newDeclarations: List<IrDeclaration>) {
|
||||
declarations.addAll(newDeclarations)
|
||||
}
|
||||
|
||||
override fun toBuilder(): IrFile.Builder =
|
||||
IrFileBuilderImpl(this)
|
||||
|
||||
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
|
||||
visitor.visitFile(this, data)
|
||||
|
||||
@@ -74,18 +55,3 @@ class IrFileImpl(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IrFileBuilderImpl(
|
||||
override val fileEntry: SourceManager.FileEntry,
|
||||
override val packageFragmentDescriptor: PackageFragmentDescriptor,
|
||||
override val fileAnnotations: MutableList<AnnotationDescriptor>,
|
||||
override val declarations: MutableList<IrDeclaration>
|
||||
) : IrFile.Builder {
|
||||
constructor(irFile: IrFile) : this(irFile.fileEntry,
|
||||
irFile.packageFragmentDescriptor,
|
||||
irFile.fileAnnotations.toMutableList(),
|
||||
irFile.declarations.toMutableList())
|
||||
|
||||
override fun build(): IrFile =
|
||||
IrFileImpl(fileEntry, packageFragmentDescriptor, fileAnnotations, declarations)
|
||||
}
|
||||
Reference in New Issue
Block a user