FIR CFA: add control flow between default parameter values

function enter -> default 1 -> default 2 -> rest of function
                   \----------^ \----------^

This probably has no effect (in non-stupid code, at least), but it makes
graph construction more architecturally correct (now value parameters'
subgraphs get attached to a node).
This commit is contained in:
pyos
2022-12-09 13:48:15 +01:00
committed by Dmitriy Novozhilov
parent 74758278d7
commit 5d4fb3ead8
9 changed files with 656 additions and 599 deletions
@@ -27,59 +27,69 @@ digraph defaultArguments_kt {
subgraph cluster_2 {
color=red
7 [label="Enter function test" style="filled" fillcolor=red];
8 [label="Enter default value of y"];
subgraph cluster_3 {
color=blue
16 [label="Enter default value of z" style="filled" fillcolor=red];
17 [label="Postponed enter to lambda"];
subgraph cluster_4 {
color=blue
21 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
22 [label="Enter block"];
23 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
24 [label="Exit block"];
}
25 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
18 [label="Postponed exit from lambda"];
19 [label="Function call: R|kotlin/run|<R|kotlin/Int|>(...)" style="filled" fillcolor=yellow];
20 [label="Exit default value of z" style="filled" fillcolor=red];
16 [label="Enter default value of y" style="filled" fillcolor=red];
17 [label="Access variable R|<local>/x|"];
18 [label="Type operator: (R|<local>/x| as R|kotlin/String|)"];
19 [label="Exit default value of y" style="filled" fillcolor=red];
}
subgraph cluster_6 {
9 [label="Exit default value of y"];
10 [label="Enter default value of z"];
subgraph cluster_4 {
color=blue
12 [label="Enter default value of y" style="filled" fillcolor=red];
13 [label="Access variable R|<local>/x|"];
14 [label="Type operator: (R|<local>/x| as R|kotlin/String|)"];
15 [label="Exit default value of y" style="filled" fillcolor=red];
20 [label="Enter default value of z" style="filled" fillcolor=red];
21 [label="Postponed enter to lambda"];
subgraph cluster_5 {
color=blue
25 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_6 {
color=blue
26 [label="Enter block"];
27 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
28 [label="Exit block"];
}
29 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
22 [label="Postponed exit from lambda"];
23 [label="Function call: R|kotlin/run|<R|kotlin/Int|>(...)" style="filled" fillcolor=yellow];
24 [label="Exit default value of z" style="filled" fillcolor=red];
}
11 [label="Exit default value of z"];
subgraph cluster_7 {
color=blue
8 [label="Enter block"];
9 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
10 [label="Exit block"];
12 [label="Enter block"];
13 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
14 [label="Exit block"];
}
11 [label="Exit function test" style="filled" fillcolor=red];
15 [label="Exit function test" style="filled" fillcolor=red];
}
7 -> {8 12 16};
8 -> {9};
7 -> {8};
8 -> {9 16};
8 -> {16 16} [style=dashed];
9 -> {10};
10 -> {11};
10 -> {11 20};
10 -> {20 20} [style=dashed];
11 -> {12};
12 -> {13};
12 -> {12} [style=dashed];
13 -> {14};
14 -> {15};
16 -> {17};
16 -> {16} [style=dashed];
17 -> {19 21};
17 -> {18} [style=dotted];
17 -> {21} [style=dashed];
17 -> {18};
18 -> {19};
19 -> {20};
21 -> {22};
19 -> {9};
20 -> {21};
21 -> {23 25};
21 -> {22} [style=dotted];
21 -> {25} [style=dashed];
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {18};
24 -> {11};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {22};
}
@@ -22,26 +22,28 @@ digraph lambdaInWhenBranch_kt {
subgraph cluster_2 {
color=red
18 [label="Enter class SubClass1" style="filled" fillcolor=red];
19 [label="Part of class initialization"];
20 [label="Enter class SubClass1" style="filled" fillcolor=red];
21 [label="Part of class initialization"];
subgraph cluster_3 {
color=blue
8 [label="Enter property" style="filled" fillcolor=red];
9 [label="Access variable R|<local>/t|"];
10 [label="Exit property" style="filled" fillcolor=red];
}
20 [label="Exit class SubClass1" style="filled" fillcolor=red];
22 [label="Exit class SubClass1" style="filled" fillcolor=red];
}
subgraph cluster_4 {
color=blue
13 [label="Enter function copy" style="filled" fillcolor=red];
14 [label="Exit function copy" style="filled" fillcolor=red];
}
subgraph cluster_5 {
color=blue
15 [label="Enter default value of t" style="filled" fillcolor=red];
16 [label="Access variable R|/SubClass1.t|"];
17 [label="Exit default value of t" style="filled" fillcolor=red];
14 [label="Enter default value of t"];
subgraph cluster_5 {
color=blue
17 [label="Enter default value of t" style="filled" fillcolor=red];
18 [label="Access variable R|/SubClass1.t|"];
19 [label="Exit default value of t" style="filled" fillcolor=red];
}
15 [label="Exit default value of t"];
16 [label="Exit function copy" style="filled" fillcolor=red];
}
subgraph cluster_6 {
color=blue
@@ -54,174 +56,175 @@ digraph lambdaInWhenBranch_kt {
6 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
7 [label="Exit function <init>" style="filled" fillcolor=red];
}
18 -> {19} [color=green];
19 -> {8} [color=green];
19 -> {20} [style=dotted];
19 -> {8} [style=dashed];
20 -> {5 11 13} [color=green];
20 -> {5 11 13} [style=dashed];
20 -> {21} [color=green];
21 -> {8} [color=green];
21 -> {22} [style=dotted];
21 -> {8} [style=dashed];
22 -> {5 11 13} [color=green];
22 -> {5 11 13} [style=dashed];
8 -> {9};
9 -> {10};
10 -> {20} [color=green];
10 -> {22} [color=green];
5 -> {6};
6 -> {7};
11 -> {12};
13 -> {14 15};
13 -> {14};
14 -> {15 17};
14 -> {17 17} [style=dashed];
15 -> {16};
15 -> {15} [style=dashed];
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {15};
subgraph cluster_8 {
color=red
26 [label="Enter class SubClass2" style="filled" fillcolor=red];
27 [label="Exit class SubClass2" style="filled" fillcolor=red];
28 [label="Enter class SubClass2" style="filled" fillcolor=red];
29 [label="Exit class SubClass2" style="filled" fillcolor=red];
}
subgraph cluster_9 {
color=blue
24 [label="Enter function copy" style="filled" fillcolor=red];
25 [label="Exit function copy" style="filled" fillcolor=red];
26 [label="Enter function copy" style="filled" fillcolor=red];
27 [label="Exit function copy" style="filled" fillcolor=red];
}
subgraph cluster_10 {
color=blue
21 [label="Enter function <init>" style="filled" fillcolor=red];
22 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
23 [label="Exit function <init>" style="filled" fillcolor=red];
23 [label="Enter function <init>" style="filled" fillcolor=red];
24 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
25 [label="Exit function <init>" style="filled" fillcolor=red];
}
26 -> {27} [color=green];
27 -> {21 24} [color=green];
27 -> {21 24} [style=dashed];
21 -> {22};
22 -> {23};
28 -> {29} [color=green];
29 -> {23 26} [color=green];
29 -> {23 26} [style=dashed];
23 -> {24};
24 -> {25};
26 -> {27};
subgraph cluster_11 {
color=red
28 [label="Enter function foo" style="filled" fillcolor=red];
30 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
29 [label="Enter block"];
31 [label="Enter block"];
subgraph cluster_13 {
color=blue
30 [label="Enter when"];
31 [label="Access variable R|<local>/p|"];
32 [label="Enter when"];
33 [label="Access variable R|<local>/p|"];
subgraph cluster_14 {
color=blue
32 [label="Enter when branch condition "];
33 [label="Exit $subj"];
34 [label="Type operator: ($subj$ is R|SubClass1|)"];
35 [label="Exit when branch condition"];
34 [label="Enter when branch condition "];
35 [label="Exit $subj"];
36 [label="Type operator: ($subj$ is R|SubClass1|)"];
37 [label="Exit when branch condition"];
}
subgraph cluster_15 {
color=blue
36 [label="Enter when branch condition "];
37 [label="Exit $subj"];
38 [label="Type operator: ($subj$ is R|SubClass2|)"];
39 [label="Exit when branch condition"];
38 [label="Enter when branch condition "];
39 [label="Exit $subj"];
40 [label="Type operator: ($subj$ is R|SubClass2|)"];
41 [label="Exit when branch condition"];
}
40 [label="Enter when branch result"];
42 [label="Enter when branch result"];
subgraph cluster_16 {
color=blue
41 [label="Enter block"];
42 [label="Const: String()"];
43 [label="Exit block"];
43 [label="Enter block"];
44 [label="Const: String()"];
45 [label="Exit block"];
}
44 [label="Exit when branch result"];
45 [label="Enter when branch result"];
46 [label="Exit when branch result"];
47 [label="Enter when branch result"];
subgraph cluster_17 {
color=blue
46 [label="Enter block"];
47 [label="Const: String()"];
48 [label="Postponed enter to lambda"];
48 [label="Enter block"];
49 [label="Const: String()"];
50 [label="Postponed enter to lambda"];
subgraph cluster_18 {
color=blue
82 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
84 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_19 {
color=blue
83 [label="Enter block"];
84 [label="Access variable R|<local>/it|"];
85 [label="Exit block"];
85 [label="Enter block"];
86 [label="Access variable R|<local>/it|"];
87 [label="Exit block"];
}
86 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
88 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
49 [label="Postponed exit from lambda"];
50 [label="Function call: String().R|kotlin/let|<R|kotlin/String|, R|kotlin/String|>(...)" style="filled" fillcolor=yellow];
51 [label="Exit block"];
51 [label="Postponed exit from lambda"];
52 [label="Function call: String().R|kotlin/let|<R|kotlin/String|, R|kotlin/String|>(...)" style="filled" fillcolor=yellow];
53 [label="Exit block"];
}
52 [label="Exit when branch result"];
53 [label="Exit when"];
54 [label="Exit when branch result"];
55 [label="Exit when"];
}
54 [label="Access variable R|<local>/p|"];
55 [label="Access variable <Unresolved name: t>#"];
56 [label="Access variable R|<local>/p|"];
57 [label="Access variable <Unresolved name: t>#"];
subgraph cluster_20 {
color=blue
56 [label="Enter when"];
57 [label="Access variable R|<local>/p|"];
58 [label="Enter when"];
59 [label="Access variable R|<local>/p|"];
subgraph cluster_21 {
color=blue
58 [label="Enter when branch condition "];
59 [label="Exit $subj"];
60 [label="Type operator: ($subj$ is R|SubClass1|)"];
61 [label="Exit when branch condition"];
60 [label="Enter when branch condition "];
61 [label="Exit $subj"];
62 [label="Type operator: ($subj$ is R|SubClass1|)"];
63 [label="Exit when branch condition"];
}
subgraph cluster_22 {
color=blue
62 [label="Enter when branch condition "];
63 [label="Exit $subj"];
64 [label="Type operator: ($subj$ is R|SubClass2|)"];
65 [label="Exit when branch condition"];
64 [label="Enter when branch condition "];
65 [label="Exit $subj"];
66 [label="Type operator: ($subj$ is R|SubClass2|)"];
67 [label="Exit when branch condition"];
}
66 [label="Enter when branch result"];
68 [label="Enter when branch result"];
subgraph cluster_23 {
color=blue
67 [label="Enter block"];
68 [label="Const: String(2)"];
69 [label="Exit block"];
69 [label="Enter block"];
70 [label="Const: String(2)"];
71 [label="Exit block"];
}
70 [label="Exit when branch result"];
71 [label="Enter when branch result"];
72 [label="Exit when branch result"];
73 [label="Enter when branch result"];
subgraph cluster_24 {
color=blue
72 [label="Enter block"];
73 [label="Access variable R|<local>/p|"];
74 [label="Smart cast: R|<local>/p|"];
75 [label="Access variable R|/SubClass1.t|"];
76 [label="Exit block"];
74 [label="Enter block"];
75 [label="Access variable R|<local>/p|"];
76 [label="Smart cast: R|<local>/p|"];
77 [label="Access variable R|/SubClass1.t|"];
78 [label="Exit block"];
}
77 [label="Exit when branch result"];
78 [label="Exit when"];
79 [label="Exit when branch result"];
80 [label="Exit when"];
}
79 [label="Access variable R|kotlin/String.length|"];
80 [label="Exit block"];
81 [label="Access variable R|kotlin/String.length|"];
82 [label="Exit block"];
}
81 [label="Exit function foo" style="filled" fillcolor=red];
83 [label="Exit function foo" style="filled" fillcolor=red];
}
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {36 45};
35 -> {36};
36 -> {37};
37 -> {38};
37 -> {38 47};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {53};
44 -> {45};
45 -> {46};
46 -> {47};
46 -> {55};
47 -> {48};
48 -> {50 82};
48 -> {49} [style=dotted];
48 -> {82} [style=dashed];
49 -> {50} [color=green];
49 -> {53} [color=red];
50 -> {51};
51 -> {52};
48 -> {49};
49 -> {50};
50 -> {52 84};
50 -> {51} [style=dotted];
50 -> {84} [style=dashed];
51 -> {52} [color=green];
51 -> {55} [color=red];
52 -> {53};
53 -> {54};
54 -> {55};
@@ -231,18 +234,18 @@ digraph lambdaInWhenBranch_kt {
58 -> {59};
59 -> {60};
60 -> {61};
61 -> {62 71};
61 -> {62};
62 -> {63};
63 -> {64};
63 -> {64 73};
64 -> {65};
65 -> {66};
66 -> {67};
67 -> {68};
68 -> {69};
69 -> {70};
70 -> {78};
70 -> {71};
71 -> {72};
72 -> {73};
72 -> {80};
73 -> {74};
74 -> {75};
75 -> {76};
@@ -251,10 +254,12 @@ digraph lambdaInWhenBranch_kt {
78 -> {79};
79 -> {80};
80 -> {81};
81 -> {82};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {49};
86 -> {87};
87 -> {88};
88 -> {51};
}
@@ -252,12 +252,15 @@ abstract class FirDataFlowAnalyzer(
// ----------------------------------- Value parameters (and it's defaults) -----------------------------------
fun enterValueParameter(valueParameter: FirValueParameter) {
graphBuilder.enterValueParameter(valueParameter)?.mergeIncomingFlow()
val (outerNode, innerNode) = graphBuilder.enterValueParameter(valueParameter) ?: return
outerNode.mergeIncomingFlow()
innerNode.mergeIncomingFlow()
}
fun exitValueParameter(valueParameter: FirValueParameter): ControlFlowGraph? {
val (node, graph) = graphBuilder.exitValueParameter(valueParameter) ?: return null
node.mergeIncomingFlow()
val (innerNode, outerNode, graph) = graphBuilder.exitValueParameter(valueParameter) ?: return null
innerNode.mergeIncomingFlow()
outerNode.mergeIncomingFlow()
return graph
}
@@ -542,29 +542,35 @@ class ControlFlowGraphBuilder {
// ----------------------------------- Value parameters (and it's defaults) -----------------------------------
fun enterValueParameter(valueParameter: FirValueParameter): EnterDefaultArgumentsNode? {
fun enterValueParameter(valueParameter: FirValueParameter): Pair<EnterValueParameterNode, EnterDefaultArgumentsNode>? {
if (valueParameter.defaultValue == null) return null
val outerEnterNode = createEnterValueParameterNode(valueParameter)
val graph = ControlFlowGraph(valueParameter, "default value of ${valueParameter.name}", ControlFlowGraph.Kind.DefaultArgument)
currentGraph.addSubGraph(graph)
pushGraph(graph, Mode.Body)
createExitDefaultArgumentsNode(valueParameter).also {
exitTargetsForTry.push(it)
}
return createEnterDefaultArgumentsNode(valueParameter).also {
addEdge(lastNode, it)
lastNodes.push(it)
}
val innerExitNode = createExitDefaultArgumentsNode(valueParameter)
val innerEnterNode = createEnterDefaultArgumentsNode(valueParameter)
addNewSimpleNode(outerEnterNode)
addEdge(outerEnterNode, innerEnterNode)
lastNodes.push(innerEnterNode)
exitTargetsForTry.push(innerExitNode)
return outerEnterNode to innerEnterNode
}
fun exitValueParameter(valueParameter: FirValueParameter): Pair<ExitDefaultArgumentsNode, ControlFlowGraph>? {
fun exitValueParameter(valueParameter: FirValueParameter): Triple<ExitDefaultArgumentsNode, ExitValueParameterNode, ControlFlowGraph>? {
if (valueParameter.defaultValue == null) return null
val exitNode = exitTargetsForTry.pop() as ExitDefaultArgumentsNode
popAndAddEdge(exitNode)
val graph = popGraph()
require(exitNode == graph.exitNode)
return exitNode to graph
val outerEnterNode = lastNode as EnterValueParameterNode
val outerExitNode = createExitValueParameterNode(valueParameter)
addNewSimpleNode(outerExitNode)
addEdge(exitNode, outerExitNode, propagateDeadness = false)
outerEnterNode.addSubGraph(graph)
currentGraph.addSubGraph(graph)
return Triple(exitNode, outerExitNode, graph)
}
// ----------------------------------- Block -----------------------------------
@@ -256,12 +256,18 @@ fun ControlFlowGraphBuilder.createClassExitNode(fir: FirClass): ClassExitNode =
fun ControlFlowGraphBuilder.createLocalClassExitNode(fir: FirRegularClass): LocalClassExitNode =
LocalClassExitNode(currentGraph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createEnterValueParameterNode(fir: FirValueParameter): EnterValueParameterNode =
EnterValueParameterNode(currentGraph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createEnterDefaultArgumentsNode(fir: FirValueParameter): EnterDefaultArgumentsNode =
EnterDefaultArgumentsNode(currentGraph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createExitDefaultArgumentsNode(fir: FirValueParameter): ExitDefaultArgumentsNode =
ExitDefaultArgumentsNode(currentGraph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createExitValueParameterNode(fir: FirValueParameter): ExitValueParameterNode =
ExitValueParameterNode(currentGraph, fir, levelCounter, createId())
fun ControlFlowGraphBuilder.createComparisonExpressionNode(fir: FirComparisonExpression): ComparisonExpressionNode =
ComparisonExpressionNode(currentGraph, fir, levelCounter, createId())
@@ -182,8 +182,14 @@ class LocalFunctionDeclarationNode(owner: ControlFlowGraph, override val fir: Fi
// ----------------------------------- Default arguments -----------------------------------
class EnterValueParameterNode(owner: ControlFlowGraph, override val fir: FirValueParameter, level: Int, id: Int) : CFGNodeWithCfgOwner<FirValueParameter>(owner, level, id) {
override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R {
return visitor.visitEnterValueParameterNode(this, data)
}
}
@OptIn(CfgInternals::class)
class EnterDefaultArgumentsNode(owner: ControlFlowGraph, override val fir: FirValueParameter, level: Int, id: Int) : CFGNodeWithCfgOwner<FirValueParameter>(owner, level, id),
class EnterDefaultArgumentsNode(owner: ControlFlowGraph, override val fir: FirValueParameter, level: Int, id: Int) : CFGNode<FirValueParameter>(owner, level, id),
EnterNodeMarker {
init {
owner.enterNode = this
@@ -206,6 +212,12 @@ class ExitDefaultArgumentsNode(owner: ControlFlowGraph, override val fir: FirVal
}
}
class ExitValueParameterNode(owner: ControlFlowGraph, override val fir: FirValueParameter, level: Int, id: Int) : CFGNode<FirValueParameter>(owner, level, id) {
override fun <R, D> accept(visitor: ControlFlowGraphVisitor<R, D>, data: D): R {
return visitor.visitExitValueParameterNode(this, data)
}
}
// ----------------------------------- Anonymous function -----------------------------------
class SplitPostponedLambdasNode(owner: ControlFlowGraph, override val fir: FirStatement, val lambdas: List<FirAnonymousFunction>, level: Int, id: Int)
@@ -115,8 +115,10 @@ fun CFGNode<*>.render(): String =
is ContractDescriptionEnterNode -> "Enter contract description"
is EnterValueParameterNode -> "Enter default value of ${fir.name}"
is EnterDefaultArgumentsNode -> "Enter default value of ${fir.name}"
is ExitDefaultArgumentsNode -> "Exit default value of ${fir.name}"
is ExitValueParameterNode -> "Exit default value of ${fir.name}"
is ElvisLhsExitNode -> "Exit lhs of ?:"
is ElvisLhsIsNotNullNode -> "Lhs of ?: is not null"
@@ -26,7 +26,7 @@ abstract class ControlFlowGraphVisitor<out R, in D> {
// ----------------------------------- Default arguments -----------------------------------
open fun visitExitDefaultArgumentsNode(node: ExitDefaultArgumentsNode, data: D): R {
open fun visitEnterValueParameterNode(node: EnterValueParameterNode, data: D): R {
return visitNode(node, data)
}
@@ -34,6 +34,14 @@ abstract class ControlFlowGraphVisitor<out R, in D> {
return visitNode(node, data)
}
open fun visitExitDefaultArgumentsNode(node: ExitDefaultArgumentsNode, data: D): R {
return visitNode(node, data)
}
open fun visitExitValueParameterNode(node: ExitValueParameterNode, data: D): R {
return visitNode(node, data)
}
// ----------------------------------- Anonymous function -----------------------------------
open fun visitSplitPostponedLambdasNode(node: SplitPostponedLambdasNode, data: D): R {
File diff suppressed because it is too large Load Diff