FIR CFG: add union nodes
Quick quiz:
Q: In a CFG, what does `a -> b -> c -> d` mean?
A: `a`, then `b`, then `c`, then `d`.
Q: In a CFG, what does `a -> b -> d; a -> c -> d` mean?
A: `a`, then `b` or `c`, then `d`.
Q: So how do you encode "a, then (b, then c) or (c, then b), then d`?
A: You can't.
Problem is, you need to, because that's what `a; run2({ b }, { c }); d`
does when `run2` has a contract that it calls both its lambda arguments
in-place: `shuffle(listOf(block1, block2)).forEach { it() }` is a
perfectly valid implementation for it, as little sense as that makes.
So that's what union nodes solve. When a node implements
`UnionNodeMarker`, its inputs are interpreted as "all visited in some
order" instead of the normal "one of the inputs is visited".
Currently this is used for data flow. It *should* also be used for
control flow, but it isn't. But it should be. But that's not so easy.
BTW, `try` exit is NOT a union node; although lambdas in one branch can
be completed according to types' of lambdas in another, data does not
flow between the branches anyway (since we don't know how much of the
`try` executed before jumping into `catch`, and `catch`es are mutually
exclusive) so a `try` expression is more like `when` than a function
call with called-in-place-exactly-once arguments. The fact that
`exitTryExpression` used `processUnionOfArguments` in a weird way
should've hinted at that, but now we know for certain.
This commit is contained in:
+15
-16
@@ -6,7 +6,7 @@ digraph lambdaInWhenBranch_kt {
|
||||
subgraph cluster_0 {
|
||||
color=red
|
||||
0 [label="Enter function <init>" style="filled" fillcolor=red];
|
||||
1 [label="Delegated constructor call: super<R|kotlin/Any|>()"];
|
||||
1 [label="Delegated constructor call: super<R|kotlin/Any|>()" style="filled" fillcolor=yellow];
|
||||
2 [label="Exit function <init>" style="filled" fillcolor=red];
|
||||
}
|
||||
0 -> {1};
|
||||
@@ -22,7 +22,7 @@ digraph lambdaInWhenBranch_kt {
|
||||
subgraph cluster_2 {
|
||||
color=red
|
||||
5 [label="Enter function <init>" style="filled" fillcolor=red];
|
||||
6 [label="Delegated constructor call: super<R|Sealed|>()"];
|
||||
6 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
|
||||
7 [label="Exit function <init>" style="filled" fillcolor=red];
|
||||
}
|
||||
5 -> {6};
|
||||
@@ -75,7 +75,7 @@ digraph lambdaInWhenBranch_kt {
|
||||
subgraph cluster_8 {
|
||||
color=red
|
||||
21 [label="Enter function <init>" style="filled" fillcolor=red];
|
||||
22 [label="Delegated constructor call: super<R|Sealed|>()"];
|
||||
22 [label="Delegated constructor call: super<R|Sealed|>()" style="filled" fillcolor=yellow];
|
||||
23 [label="Exit function <init>" style="filled" fillcolor=red];
|
||||
}
|
||||
21 -> {22};
|
||||
@@ -135,17 +135,17 @@ digraph lambdaInWhenBranch_kt {
|
||||
48 [label="Postponed enter to lambda"];
|
||||
subgraph cluster_18 {
|
||||
color=blue
|
||||
83 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
|
||||
82 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
|
||||
subgraph cluster_19 {
|
||||
color=blue
|
||||
84 [label="Enter block"];
|
||||
85 [label="Access variable R|<local>/it|"];
|
||||
86 [label="Exit block"];
|
||||
83 [label="Enter block"];
|
||||
84 [label="Access variable R|<local>/it|"];
|
||||
85 [label="Exit block"];
|
||||
}
|
||||
87 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
|
||||
86 [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|>(...)"];
|
||||
50 [label="Function call: String().R|kotlin/let|<R|kotlin/String|, R|kotlin/String|>(...)" style="filled" fillcolor=yellow];
|
||||
51 [label="Exit block"];
|
||||
}
|
||||
52 [label="Exit when branch result"];
|
||||
@@ -196,7 +196,6 @@ digraph lambdaInWhenBranch_kt {
|
||||
}
|
||||
81 [label="Exit function foo" style="filled" fillcolor=red];
|
||||
}
|
||||
82 [label="Merge postponed lambda exits"];
|
||||
28 -> {29};
|
||||
29 -> {30};
|
||||
30 -> {31};
|
||||
@@ -217,14 +216,14 @@ digraph lambdaInWhenBranch_kt {
|
||||
45 -> {46};
|
||||
46 -> {47};
|
||||
47 -> {48};
|
||||
48 -> {83};
|
||||
48 -> {82};
|
||||
48 -> {49} [color=red];
|
||||
48 -> {83} [style=dashed];
|
||||
48 -> {82} [style=dashed];
|
||||
49 -> {50};
|
||||
50 -> {51};
|
||||
51 -> {52};
|
||||
52 -> {53};
|
||||
53 -> {82 54};
|
||||
53 -> {54};
|
||||
54 -> {55};
|
||||
55 -> {56};
|
||||
56 -> {57};
|
||||
@@ -252,11 +251,11 @@ digraph lambdaInWhenBranch_kt {
|
||||
78 -> {79};
|
||||
79 -> {80};
|
||||
80 -> {81};
|
||||
82 -> {83};
|
||||
83 -> {84};
|
||||
84 -> {85};
|
||||
85 -> {86};
|
||||
86 -> {87};
|
||||
87 -> {82} [color=red];
|
||||
87 -> {49} [color=green];
|
||||
86 -> {53} [color=red];
|
||||
86 -> {49} [color=green];
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user