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
@@ -33,19 +33,18 @@ digraph defaultArguments_kt {
17 [label="Postponed enter to lambda"];
subgraph cluster_4 {
color=blue
22 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
21 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
23 [label="Enter block"];
24 [label="Function call: R|/foo|()"];
25 [label="Exit block"];
22 [label="Enter block"];
23 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
24 [label="Exit block"];
}
26 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
25 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
18 [label="Call arguments union" style="filled" fillcolor=yellow];
19 [label="Postponed exit from lambda"];
20 [label="Function call: R|kotlin/run|<R|kotlin/Int|>(...)"];
21 [label="Exit default value of z" 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];
}
subgraph cluster_6 {
color=blue
@@ -57,7 +56,7 @@ digraph defaultArguments_kt {
subgraph cluster_7 {
color=blue
8 [label="Enter block"];
9 [label="Function call: R|/foo|()"];
9 [label="Function call: R|/foo|()" style="filled" fillcolor=yellow];
10 [label="Exit block"];
}
11 [label="Exit function test" style="filled" fillcolor=red];
@@ -72,17 +71,16 @@ digraph defaultArguments_kt {
14 -> {15};
16 -> {17};
16 -> {16} [style=dashed];
17 -> {22};
17 -> {19} [color=red];
17 -> {22} [style=dashed];
18 -> {20} [color=red];
19 -> {20} [color=green];
20 -> {21};
17 -> {21};
17 -> {18} [color=red];
17 -> {21} [style=dashed];
18 -> {19};
19 -> {20};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {18} [color=red];
26 -> {19} [color=green];
25 -> {19} [color=red];
25 -> {18} [color=green];
}