Provide parent reference for temporary variables immediately

This commit is contained in:
Dmitry Petrov
2018-12-20 12:34:05 +03:00
parent 4e53e7d663
commit 57c12476d7
9 changed files with 191 additions and 8 deletions
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
@@ -31,11 +31,13 @@ class AnonymousInitializerGenerator(
fun generateAnonymousInitializerDeclaration(
ktAnonymousInitializer: KtAnonymousInitializer,
classDescriptor: ClassDescriptor
irClass: IrClass
): IrDeclaration =
context.symbolTable.declareAnonymousInitializer(
ktAnonymousInitializer.startOffsetSkippingComments, ktAnonymousInitializer.endOffset, IrDeclarationOrigin.DEFINED, classDescriptor
ktAnonymousInitializer.startOffsetSkippingComments, ktAnonymousInitializer.endOffset,
IrDeclarationOrigin.DEFINED, irClass.descriptor
).buildWithScope { irAnonymousInitializer ->
irAnonymousInitializer.parent = irClass
val bodyGenerator = createBodyGenerator(irAnonymousInitializer.symbol)
val statementGenerator = bodyGenerator.createStatementGenerator()
val ktBody = ktAnonymousInitializer.body!!
@@ -365,9 +365,9 @@ class ClassGenerator(
private fun generateMembersDeclaredInClassBody(irClass: IrClass, ktClassOrObject: KtPureClassOrObject) {
// generate real body declarations
ktClassOrObject.getBody()?.let { ktClassBody ->
ktClassOrObject.body?.let { ktClassBody ->
ktClassBody.declarations.mapTo(irClass.declarations) { ktDeclaration ->
declarationGenerator.generateClassMemberDeclaration(ktDeclaration, irClass.descriptor)
declarationGenerator.generateClassMemberDeclaration(ktDeclaration, irClass)
}
}
@@ -384,12 +384,20 @@ class ClassGenerator(
fun generateEnumEntry(ktEnumEntry: KtEnumEntry): IrEnumEntry {
val enumEntryDescriptor = getOrFail(BindingContext.CLASS, ktEnumEntry)
// TODO this is a hack, pass declaration parent through generator chain instead
val enumClassDescriptor = enumEntryDescriptor.containingDeclaration as ClassDescriptor
val enumClassSymbol = context.symbolTable.referenceClass(enumClassDescriptor)
val irEnumClass = enumClassSymbol.owner
return context.symbolTable.declareEnumEntry(
ktEnumEntry.startOffsetSkippingComments,
ktEnumEntry.endOffset,
IrDeclarationOrigin.DEFINED,
enumEntryDescriptor
).buildWithScope { irEnumEntry ->
irEnumEntry.parent = irEnumClass
if (!enumEntryDescriptor.isExpect) {
irEnumEntry.initializerExpression =
createBodyGenerator(irEnumEntry.symbol)
@@ -62,10 +62,10 @@ class DeclarationGenerator(override val context: GeneratorContext) : Generator {
return generateClassOrObjectDeclaration(syntheticDeclaration)
}
fun generateClassMemberDeclaration(ktDeclaration: KtDeclaration, classDescriptor: ClassDescriptor): IrDeclaration =
fun generateClassMemberDeclaration(ktDeclaration: KtDeclaration, irClass: IrClass): IrDeclaration =
when (ktDeclaration) {
is KtAnonymousInitializer ->
AnonymousInitializerGenerator(this).generateAnonymousInitializerDeclaration(ktDeclaration, classDescriptor)
AnonymousInitializerGenerator(this).generateAnonymousInitializerDeclaration(ktDeclaration, irClass)
is KtSecondaryConstructor ->
FunctionGenerator(this).generateSecondaryConstructor(ktDeclaration)
is KtEnumEntry ->
@@ -20,7 +20,9 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptor
@@ -39,6 +41,16 @@ import org.jetbrains.kotlin.types.KotlinType
class Scope(val scopeOwnerSymbol: IrSymbol) {
val scopeOwner: DeclarationDescriptor get() = scopeOwnerSymbol.descriptor
private fun getVariableParent(): IrDeclarationParent {
if (!scopeOwnerSymbol.isBound) throw AssertionError("Unbound symbol: $scopeOwner")
val scopeOwnerElement = scopeOwnerSymbol.owner
return when (scopeOwnerElement) {
is IrDeclarationParent -> scopeOwnerElement
!is IrDeclaration -> throw AssertionError("Not a declaration: $scopeOwnerElement")
else -> scopeOwnerElement.parent
}
}
@Deprecated("Creates unbound symbol")
constructor(descriptor: DeclarationDescriptor) : this(createSymbolForScopeOwner(descriptor))
@@ -74,7 +86,9 @@ class Scope(val scopeOwnerSymbol: IrSymbol) {
),
irType ?: irExpression.type,
irExpression
)
).apply {
parent = getVariableParent()
}
}
}
@@ -0,0 +1,5 @@
val n: Any? = null
enum class En(val x: String?) {
ENTRY(n?.toString())
}
@@ -0,0 +1,91 @@
FILE fqName:<root> fileName:/temporaryInEnumEntryInitializer.kt
PROPERTY name:n visibility:public modality:FINAL flags:val
FIELD PROPERTY_BACKING_FIELD name:n type:kotlin.Any? visibility:public flags:final,static
EXPRESSION_BODY
CONST Null type=kotlin.Nothing? value=null
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-n> visibility:public modality:FINAL <> () returnType:kotlin.Any? flags:
correspondingProperty: PROPERTY name:n visibility:public modality:FINAL flags:val
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-n>(): Any?'
GET_FIELD 'n: Any?' type=kotlin.Any? origin=null
CLASS ENUM_CLASS name:En modality:FINAL visibility:public flags: superTypes:[kotlin.Enum<En>]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:En flags:
CONSTRUCTOR visibility:private <> (x:kotlin.String?) returnType:En flags:primary
VALUE_PARAMETER name:x index:0 type:kotlin.String? flags:
BLOCK_BODY
ENUM_CONSTRUCTOR_CALL 'constructor Enum(String, Int)'
<E : Enum<E>>: En
INSTANCE_INITIALIZER_CALL classDescriptor='En'
PROPERTY name:x visibility:public modality:FINAL flags:val
FIELD PROPERTY_BACKING_FIELD name:x type:kotlin.String? visibility:public flags:final
EXPRESSION_BODY
GET_VAR 'value-parameter x: String?' type=kotlin.String? origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-x> visibility:public modality:FINAL <> ($this:En) returnType:kotlin.String? flags:
correspondingProperty: PROPERTY name:x visibility:public modality:FINAL flags:val
$this: VALUE_PARAMETER name:<this> type:En flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-x>(): String?'
GET_FIELD 'x: String?' type=kotlin.String? origin=null
receiver: GET_VAR 'this@En: En' type=En origin=null
ENUM_ENTRY name:ENTRY
init: ENUM_CONSTRUCTOR_CALL 'constructor En(String?)'
x: BLOCK type=kotlin.String? origin=SAFE_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp0_safe_receiver type:kotlin.Any? flags:val
CALL '<get-n>(): Any?' type=kotlin.Any? origin=GET_PROPERTY
WHEN type=kotlin.String? origin=null
BRANCH
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'tmp0_safe_receiver: Any?' type=kotlin.Any? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST Null type=kotlin.Nothing? value=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CALL 'toString(): String' type=kotlin.String origin=null
$this: GET_VAR 'tmp0_safe_receiver: Any?' type=kotlin.Any? origin=null
FUN FAKE_OVERRIDE name:clone visibility:protected modality:FINAL <> ($this:kotlin.Enum<En>) returnType:kotlin.Any flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:clone visibility:protected modality:FINAL <> ($this:kotlin.Enum<E>) returnType:kotlin.Any flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
FUN FAKE_OVERRIDE name:finalize visibility:protected/*protected and package*/ modality:FINAL <> ($this:kotlin.Enum<En>) returnType:kotlin.Unit flags:
overridden:
FUN IR_EXTERNAL_JAVA_DECLARATION_STUB name:finalize visibility:protected/*protected and package*/ modality:FINAL <> ($this:kotlin.Enum<E>) returnType:kotlin.Unit flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
FUN FAKE_OVERRIDE name:getDeclaringClass visibility:public modality:FINAL <> ($this:kotlin.Enum<En>) returnType:java.lang.Class<En?>? flags:
overridden:
FUN IR_EXTERNAL_JAVA_DECLARATION_STUB name:getDeclaringClass visibility:public modality:FINAL <> ($this:kotlin.Enum<E>) returnType:java.lang.Class<E?>? flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
FUN FAKE_OVERRIDE name:compareTo visibility:public modality:FINAL <> ($this:kotlin.Enum<En>, other:En) returnType:kotlin.Int flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:compareTo visibility:public modality:FINAL <> ($this:kotlin.Enum<E>, other:E) returnType:kotlin.Int flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
VALUE_PARAMETER name:other index:0 type:En flags:
FUN FAKE_OVERRIDE name:equals visibility:public modality:FINAL <> ($this:kotlin.Enum<En>, other:kotlin.Any?) returnType:kotlin.Boolean flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:FINAL <> ($this:kotlin.Enum<E>, other:kotlin.Any?) returnType:kotlin.Boolean flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags:
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:FINAL <> ($this:kotlin.Enum<En>) returnType:kotlin.Int flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:FINAL <> ($this:kotlin.Enum<E>) returnType:kotlin.Int flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
PROPERTY FAKE_OVERRIDE name:name visibility:public modality:FINAL flags:val
FUN FAKE_OVERRIDE name:<get-name> visibility:public modality:FINAL <> ($this:kotlin.Enum<En>) returnType:kotlin.String flags:
correspondingProperty: PROPERTY FAKE_OVERRIDE name:name visibility:public modality:FINAL flags:val
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-name> visibility:public modality:FINAL <> ($this:kotlin.Enum<E>) returnType:kotlin.String flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
PROPERTY FAKE_OVERRIDE name:ordinal visibility:public modality:FINAL flags:val
FUN FAKE_OVERRIDE name:<get-ordinal> visibility:public modality:FINAL <> ($this:kotlin.Enum<En>) returnType:kotlin.Int flags:
correspondingProperty: PROPERTY FAKE_OVERRIDE name:ordinal visibility:public modality:FINAL flags:val
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-ordinal> visibility:public modality:FINAL <> ($this:kotlin.Enum<E>) returnType:kotlin.Int flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Enum<En>) returnType:kotlin.String flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Enum<E>) returnType:kotlin.String flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Enum<En> flags:
FUN ENUM_CLASS_SPECIAL_MEMBER name:values visibility:public modality:FINAL <> () returnType:kotlin.Array<En> flags:
SYNTHETIC_BODY kind=ENUM_VALUES
FUN ENUM_CLASS_SPECIAL_MEMBER name:valueOf visibility:public modality:FINAL <> (value:kotlin.String) returnType:En flags:
VALUE_PARAMETER name:value index:0 type:kotlin.String flags:
SYNTHETIC_BODY kind=ENUM_VALUEOF
@@ -0,0 +1,6 @@
class C(x: Any?) {
val s: String?
init {
s = x?.toString()
}
}
@@ -0,0 +1,47 @@
FILE fqName:<root> fileName:/temporaryInInitBlock.kt
CLASS CLASS name:C modality:FINAL visibility:public flags: superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:C flags:
CONSTRUCTOR visibility:public <> (x:kotlin.Any?) returnType:C flags:primary
VALUE_PARAMETER name:x index:0 type:kotlin.Any? flags:
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'constructor Any()'
INSTANCE_INITIALIZER_CALL classDescriptor='C'
PROPERTY name:s visibility:public modality:FINAL flags:val
FIELD PROPERTY_BACKING_FIELD name:s type:kotlin.String? visibility:public flags:final
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-s> visibility:public modality:FINAL <> ($this:C) returnType:kotlin.String? flags:
correspondingProperty: PROPERTY name:s visibility:public modality:FINAL flags:val
$this: VALUE_PARAMETER name:<this> type:C flags:
BLOCK_BODY
RETURN type=kotlin.Nothing from='<get-s>(): String?'
GET_FIELD 's: String?' type=kotlin.String? origin=null
receiver: GET_VAR 'this@C: C' type=C origin=null
ANONYMOUS_INITIALIZER C
BLOCK_BODY
SET_FIELD 's: String?' type=kotlin.Unit origin=null
receiver: GET_VAR 'this@C: C' type=C origin=null
value: BLOCK type=kotlin.String? origin=SAFE_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp0_safe_receiver type:kotlin.Any? flags:val
GET_VAR 'value-parameter x: Any?' type=kotlin.Any? origin=null
WHEN type=kotlin.String? origin=null
BRANCH
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'tmp0_safe_receiver: Any?' type=kotlin.Any? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST Null type=kotlin.Nothing? value=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CALL 'toString(): String' type=kotlin.String origin=null
$this: GET_VAR 'tmp0_safe_receiver: Any?' type=kotlin.Any? origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags:
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags:
overridden:
FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String flags:
$this: VALUE_PARAMETER name:<this> type:kotlin.Any flags:
@@ -1117,6 +1117,16 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/expressions/stringTemplates.kt");
}
@TestMetadata("temporaryInEnumEntryInitializer.kt")
public void testTemporaryInEnumEntryInitializer() throws Exception {
runTest("compiler/testData/ir/irText/expressions/temporaryInEnumEntryInitializer.kt");
}
@TestMetadata("temporaryInInitBlock.kt")
public void testTemporaryInInitBlock() throws Exception {
runTest("compiler/testData/ir/irText/expressions/temporaryInInitBlock.kt");
}
@TestMetadata("thisOfGenericOuterClass.kt")
public void testThisOfGenericOuterClass() throws Exception {
runTest("compiler/testData/ir/irText/expressions/thisOfGenericOuterClass.kt");