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:
pyos
2022-11-17 12:53:35 +01:00
committed by teamcity
parent 99bebfa183
commit a9be27e330
108 changed files with 4050 additions and 4239 deletions
@@ -6,7 +6,7 @@ digraph postponedLambdaInConstructor_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};
@@ -26,90 +26,88 @@ digraph postponedLambdaInConstructor_kt {
7 [label="Postponed enter to lambda"];
subgraph cluster_3 {
color=blue
13 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
12 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_4 {
color=blue
14 [label="Enter block"];
15 [label="Postponed enter to lambda"];
13 [label="Enter block"];
14 [label="Postponed enter to lambda"];
subgraph cluster_5 {
color=blue
19 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
18 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_6 {
color=blue
20 [label="Enter block"];
21 [label="Access variable R|<local>/it|"];
22 [label="Exit block"];
19 [label="Enter block"];
20 [label="Access variable R|<local>/it|"];
21 [label="Exit block"];
}
23 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
22 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
16 [label="Postponed exit from lambda"];
17 [label="Exit block"];
15 [label="Postponed exit from lambda"];
16 [label="Exit block"];
}
18 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
17 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
8 [label="Postponed exit from lambda"];
9 [label="Function call: R|<local>/s|.R|kotlin/let|<R|kotlin/String|, R|() -> kotlin/String|>(...)"];
10 [label="Call arguments union" style="filled" fillcolor=yellow];
11 [label="Delegated constructor call: super<R|A|>(...)"];
12 [label="Exit function <init>" style="filled" fillcolor=red];
9 [label="Function call: R|<local>/s|.R|kotlin/let|<R|kotlin/String|, R|() -> kotlin/String|>(...)" style="filled" fillcolor=yellow];
10 [label="Delegated constructor call: super<R|A|>(...)" style="filled" fillcolor=yellow];
11 [label="Exit function <init>" style="filled" fillcolor=red];
}
5 -> {6};
6 -> {7};
7 -> {13};
7 -> {12};
7 -> {8} [color=red];
7 -> {13} [style=dashed];
7 -> {12} [style=dashed];
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {15};
15 -> {16 19};
15 -> {19} [style=dashed];
14 -> {15 18};
14 -> {18} [style=dashed];
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {10} [color=red];
18 -> {8} [color=green];
17 -> {10} [color=red];
17 -> {8} [color=green];
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
subgraph cluster_7 {
color=red
24 [label="Enter property" style="filled" fillcolor=red];
25 [label="Access variable R|<local>/s|"];
26 [label="Exit property" style="filled" fillcolor=red];
23 [label="Enter property" style="filled" fillcolor=red];
24 [label="Access variable R|<local>/s|"];
25 [label="Exit property" style="filled" fillcolor=red];
}
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {34} [color=green];
25 -> {33} [color=green];
subgraph cluster_8 {
color=red
27 [label="Enter function foo" style="filled" fillcolor=red];
26 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_9 {
color=blue
28 [label="Enter block"];
29 [label="Function call: this@R|/B|.R|/B.foo|()"];
30 [label="Exit block"];
27 [label="Enter block"];
28 [label="Function call: this@R|/B|.R|/B.foo|()" style="filled" fillcolor=yellow];
29 [label="Exit block"];
}
31 [label="Exit function foo" style="filled" fillcolor=red];
30 [label="Exit function foo" style="filled" fillcolor=red];
}
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
subgraph cluster_10 {
color=red
32 [label="Enter class B" style="filled" fillcolor=red];
33 [label="Part of class initialization"];
34 [label="Exit class B" style="filled" fillcolor=red];
31 [label="Enter class B" style="filled" fillcolor=red];
32 [label="Part of class initialization"];
33 [label="Exit class B" style="filled" fillcolor=red];
}
32 -> {33} [color=green];
33 -> {34} [style=dotted];
33 -> {24} [color=green];
33 -> {24} [style=dashed];
31 -> {32} [color=green];
32 -> {33} [style=dotted];
32 -> {23} [color=green];
32 -> {23} [style=dashed];
}