[FIR] Add anonymous objects to CFG

This commit is contained in:
Dmitriy Novozhilov
2020-02-28 17:45:51 +03:00
parent e3f1f18ca3
commit 70ee51d88c
11 changed files with 286 additions and 9 deletions
@@ -145,6 +145,12 @@ abstract class FirDataFlowAnalyzer<FLOW : Flow>(
exitNode.mergeIncomingFlow()
}
// ----------------------------------- Anonymous object -----------------------------------
fun exitAnonymousObject(anonymousObject: FirAnonymousObject) {
graphBuilder.exitAnonymousObject(anonymousObject).mergeIncomingFlow()
}
// ----------------------------------- Property -----------------------------------
fun enterProperty(property: FirProperty) {
@@ -9,10 +9,7 @@ package org.jetbrains.kotlin.fir.resolve.dfa.cfg
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction
import org.jetbrains.kotlin.fir.declarations.FirAnonymousInitializer
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.FirVisitor
@@ -108,6 +105,10 @@ class FunctionExitNode(owner: ControlFlowGraph, override val fir: FirFunction<*>
class PostponedLambdaEnterNode(owner: ControlFlowGraph, override val fir: FirAnonymousFunction, level: Int, id: Int) : CFGNode<FirAnonymousFunction>(owner, level, id)
class PostponedLambdaExitNode(owner: ControlFlowGraph, override val fir: FirAnonymousFunction, level: Int, id: Int) : CFGNode<FirAnonymousFunction>(owner, level, id)
// ----------------------------------- Anonymous function -----------------------------------
class AnonymousObjectExitNode(owner: ControlFlowGraph, override val fir: FirAnonymousObject, level: Int, id: Int) : CFGNode<FirAnonymousObject>(owner, level, id)
// ----------------------------------- Property -----------------------------------
class PropertyInitializerEnterNode(owner: ControlFlowGraph, override val fir: FirProperty, level: Int, id: Int) : CFGNode<FirProperty>(owner, level, id), EnterNodeMarker
@@ -194,6 +194,15 @@ class ControlFlowGraphBuilder {
return enterNode to exitNode
}
// ----------------------------------- Anonymous object -----------------------------------
fun exitAnonymousObject(anonymousObject: FirAnonymousObject): AnonymousObjectExitNode {
return createAnonymousObjectExitNode(anonymousObject).also {
// Hack for initializers of enum entries
addNewSimpleNodeIfPossible(it)
}
}
// ----------------------------------- Block -----------------------------------
fun enterBlock(block: FirBlock): BlockEnterNode? {
@@ -712,6 +721,11 @@ class ControlFlowGraphBuilder {
return oldNode
}
private fun addNewSimpleNodeIfPossible(newNode: CFGNode<*>, isDead: Boolean = false): CFGNode<*>? {
if (lastNodes.isEmpty) return null
return addNewSimpleNode(newNode, isDead)
}
private fun addEdge(from: CFGNode<*>, to: CFGNode<*>, propagateDeadness: Boolean = true, isDead: Boolean = false) {
val kind = if (isDead || from.isDead || to.isDead) EdgeKind.Dead else EdgeKind.Simple
CFGNode.addEdge(from, to, kind, propagateDeadness)
@@ -5,10 +5,7 @@
package org.jetbrains.kotlin.fir.resolve.dfa.cfg
import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction
import org.jetbrains.kotlin.fir.declarations.FirAnonymousInitializer
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.*
fun ControlFlowGraphBuilder.createStubNode(): StubNode = StubNode(graph, levelCounter, createId())
@@ -187,3 +184,6 @@ fun ControlFlowGraphBuilder.createPostponedLambdaExitNode(fir: FirAnonymousFunct
fun ControlFlowGraphBuilder.createPostponedLambdaEnterNode(fir: FirAnonymousFunction): PostponedLambdaEnterNode =
PostponedLambdaEnterNode(graph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createAnonymousObjectExitNode(fir: FirAnonymousObject): AnonymousObjectExitNode =
AnonymousObjectExitNode(graph, fir, levelCounter, createId())
@@ -214,6 +214,8 @@ private fun CFGNode<*>.render(): String =
is PostponedLambdaEnterNode -> "Postponed enter to lambda"
is PostponedLambdaExitNode -> "Postponed exit from lambda"
is AnonymousObjectExitNode -> "Exit anonymous object"
else -> TODO(this@render.toString())
},
)
@@ -331,6 +331,7 @@ class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransformer)
val result = withLabelAndReceiverType(null, anonymousObject, type) {
transformDeclaration(anonymousObject, data)
}
dataFlowAnalyzer.exitAnonymousObject(result.single as FirAnonymousObject)
@Suppress("UNCHECKED_CAST")
result as CompositeTransformResult<FirStatement>
}
@@ -0,0 +1,170 @@
digraph delegateWithAnonymousObject_kt {
graph [splines=ortho nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function <init>" style="filled" fillcolor=red];
1 [label="Exit function <init>" style="filled" fillcolor=red];
}
0 -> {1};
subgraph cluster_1 {
color=red
2 [label="Enter function delegate" style="filled" fillcolor=red];
3 [label="Const: Null(null)"];
4 [label="Check not null: Null(null)!!"];
5 [label="Jump: ^delegate Null(null)!!"];
6 [label="Stub" style="filled" fillcolor=gray];
7 [label="Exit function delegate" style="filled" fillcolor=red];
}
2 -> {3};
3 -> {4};
4 -> {5};
5 -> {7};
5 -> {6} [style=dotted];
6 -> {7} [style=dotted];
subgraph cluster_2 {
color=red
8 [label="Enter function <init>" style="filled" fillcolor=red];
9 [label="Exit function <init>" style="filled" fillcolor=red];
}
8 -> {9};
subgraph cluster_3 {
color=red
10 [label="Enter function updateFrom" style="filled" fillcolor=red];
11 [label="Exit function updateFrom" style="filled" fillcolor=red];
}
10 -> {11};
subgraph cluster_4 {
color=red
12 [label="Enter function <init>" style="filled" fillcolor=red];
13 [label="Exit function <init>" style="filled" fillcolor=red];
}
12 -> {13};
subgraph cluster_5 {
color=red
14 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
15 [label="Exit anonymous object"];
16 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
14 -> {15};
15 -> {16};
subgraph cluster_6 {
color=red
17 [label="Enter function <init>" style="filled" fillcolor=red];
18 [label="Exit function <init>" style="filled" fillcolor=red];
}
17 -> {18};
subgraph cluster_7 {
color=red
19 [label="Enter function getValue" style="filled" fillcolor=red];
20 [label="Function call: R|/IssueListView.IssueListView|()"];
21 [label="Jump: ^getValue R|/IssueListView.IssueListView|()"];
22 [label="Stub" style="filled" fillcolor=gray];
23 [label="Exit function getValue" style="filled" fillcolor=red];
}
19 -> {20};
20 -> {21};
21 -> {23};
21 -> {22} [style=dotted];
22 -> {23} [style=dotted];
subgraph cluster_8 {
color=red
24 [label="Enter function setValue" style="filled" fillcolor=red];
25 [label="Function call: R|/IssueListView.IssueListView|()"];
26 [label="Access variable R|<local>/value|"];
27 [label="Function call: R|/IssueListView.IssueListView|().R|/IssueListView.updateFrom|(R|<local>/value|)"];
28 [label="Jump: ^setValue R|/IssueListView.IssueListView|().R|/IssueListView.updateFrom|(R|<local>/value|)"];
29 [label="Stub" style="filled" fillcolor=gray];
30 [label="Exit function setValue" style="filled" fillcolor=red];
}
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {30};
28 -> {29} [style=dotted];
29 -> {30} [style=dotted];
subgraph cluster_9 {
color=red
31 [label="Enter function getter" style="filled" fillcolor=red];
32 [label="Access variable D|/IssuesListUserProfile.issueListView|"];
33 [label="Access variable this@R|/IssuesListUserProfile|"];
34 [label="Function call: D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)"];
35 [label="Jump: ^ D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)"];
36 [label="Stub" style="filled" fillcolor=gray];
37 [label="Exit function getter" style="filled" fillcolor=red];
}
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {37};
35 -> {36} [style=dotted];
36 -> {37} [style=dotted];
subgraph cluster_10 {
color=red
38 [label="Enter function setter" style="filled" fillcolor=red];
39 [label="Access variable D|/IssuesListUserProfile.issueListView|"];
40 [label="Access variable this@R|/IssuesListUserProfile|"];
41 [label="Access variable R|<local>/issueListView|"];
42 [label="Function call: D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|, R|<local>/issueListView|)"];
43 [label="Exit function setter" style="filled" fillcolor=red];
}
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
subgraph cluster_11 {
color=red
44 [label="Enter property" style="filled" fillcolor=red];
45 [label="Postponed enter to lambda"];
46 [label="Postponed exit from lambda"];
47 [label="Function call: this@R|/IssuesListUserProfile|.R|/delegate|<R|IssuesListUserProfile|, R|IssuesListUserProfile|, R|IssueListView|>(<L> = delegate@fun <anonymous>(): R|kotlin/properties/ReadWriteProperty<IssuesListUserProfile, IssueListView>|)"];
48 [label="Access variable this@R|/IssuesListUserProfile|"];
49 [label="Access variable this@R|/IssuesListUserProfile|"];
50 [label="Access variable this@R|/IssuesListUserProfile|"];
51 [label="Function call: this@R|/IssuesListUserProfile|.R|/delegate|<R|IssuesListUserProfile|, R|IssuesListUserProfile|, R|IssueListView|>(<L> = delegate@fun <anonymous>(): R|kotlin/properties/ReadWriteProperty<IssuesListUserProfile, IssueListView>|).<Unresolved name: provideDelegate>#(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)"];
52 [label="Postponed enter to lambda"];
53 [label="Postponed exit from lambda"];
54 [label="Function call: this@R|/IssuesListUserProfile|.R|/delegate|<R|IssuesListUserProfile|, R|IssuesListUserProfile|, R|IssueListView|>(<L> = delegate@fun <anonymous>(): R|kotlin/properties/ReadWriteProperty<IssuesListUserProfile, IssueListView>|)"];
55 [label="Exit property" style="filled" fillcolor=red];
}
44 -> {45};
45 -> {46 46} [color=green];
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53 53} [color=green];
53 -> {54};
54 -> {55};
}
@@ -0,0 +1,28 @@
// !DUMP_CFG
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
abstract class DelegateProvider<in Type>
fun <Type : Base, Base : DelegateProvider<Base>, Target : Any> Type.delegate(
factory: () -> ReadWriteProperty<Type, Target>
): ReadWriteProperty<Type, Target> = null!!
class IssueListView : DelegateProvider<IssueListView>() {
fun updateFrom(any: Any) {}
}
class IssuesListUserProfile : DelegateProvider<IssuesListUserProfile>() {
var issueListView by delegate {
object : ReadWriteProperty<IssuesListUserProfile, IssueListView> {
override fun getValue(thisRef: IssuesListUserProfile, property: KProperty<*>): IssueListView {
return IssueListView()
}
override fun setValue(thisRef: IssuesListUserProfile, property: KProperty<*>, value: IssueListView) {
return IssueListView().updateFrom(value)
}
}
}
}
@@ -0,0 +1,50 @@
FILE: delegateWithAnonymousObject.kt
public abstract class DelegateProvider<in Type> : R|kotlin/Any| {
public constructor<in Type>(): R|DelegateProvider<Type>| {
super<R|kotlin/Any|>()
}
}
public final fun <Type : R|Base|, Base : R|DelegateProvider<Base>|, Target : R|kotlin/Any|> R|Type|.delegate(factory: R|() -> kotlin/properties/ReadWriteProperty<Type, Target>|): R|kotlin/properties/ReadWriteProperty<Type, Target>| {
^delegate Null(null)!!
}
public final class IssueListView : R|DelegateProvider<IssueListView>| {
public constructor(): R|IssueListView| {
super<R|DelegateProvider<IssueListView>|>()
}
public final fun updateFrom(any: R|kotlin/Any|): R|kotlin/Unit| {
}
}
public final class IssuesListUserProfile : R|DelegateProvider<IssuesListUserProfile>| {
public constructor(): R|IssuesListUserProfile| {
super<R|DelegateProvider<IssuesListUserProfile>|>()
}
public final var issueListView: R|IssueListView|by this@R|/IssuesListUserProfile|.R|/delegate|<R|IssuesListUserProfile|, R|IssuesListUserProfile|, R|IssueListView|>(<L> = delegate@fun <anonymous>(): R|kotlin/properties/ReadWriteProperty<IssuesListUserProfile, IssueListView>| {
^ object : R|kotlin/properties/ReadWriteProperty<IssuesListUserProfile, IssueListView>| {
private constructor(): R|kotlin/Any| {
super<R|kotlin/Any|>()
}
public final override fun getValue(thisRef: R|IssuesListUserProfile|, property: R|kotlin/reflect/KProperty<*>|): R|IssueListView| {
^getValue R|/IssueListView.IssueListView|()
}
public final override fun setValue(thisRef: R|IssuesListUserProfile|, property: R|kotlin/reflect/KProperty<*>|, value: R|IssueListView|): R|kotlin/Unit| {
^setValue R|/IssueListView.IssueListView|().R|/IssueListView.updateFrom|(R|<local>/value|)
}
}
}
)
public get(): R|IssueListView| {
^ D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.getValue: R|IssueListView|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|)
}
public set(<set-?>: R|IssueListView|): R|kotlin/Unit| {
D|/IssuesListUserProfile.issueListView|.R|FakeOverride<kotlin/properties/ReadWriteProperty.setValue: R|kotlin/Unit|>|(this@R|/IssuesListUserProfile|, ::R|/IssuesListUserProfile.issueListView|, R|<local>/issueListView|)
}
}
@@ -88,6 +88,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
runTest("compiler/fir/resolve/testData/resolveWithStdlib/delegateTypeMismatch.kt");
}
@TestMetadata("delegateWithAnonymousObject.kt")
public void testDelegateWithAnonymousObject() throws Exception {
runTest("compiler/fir/resolve/testData/resolveWithStdlib/delegateWithAnonymousObject.kt");
}
@TestMetadata("delegationByMap.kt")
public void testDelegationByMap() throws Exception {
runTest("compiler/fir/resolve/testData/resolveWithStdlib/delegationByMap.kt");
@@ -16,5 +16,5 @@ private val privateObj by MyDelegate {
}
fun test() {
privateObj.<!UNRESOLVED_REFERENCE!>x<!>
privateObj.x
}