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
@@ -24,119 +24,115 @@ digraph complexPostponedCfg_kt {
color=blue
5 [label="Enter block"];
6 [label="Access variable R|<local>/statements|"];
7 [label="Function call: R|<local>/statements|.R|kotlin/collections/last|<R|FirBase|>()"];
7 [label="Function call: R|<local>/statements|.R|kotlin/collections/last|<R|FirBase|>()" style="filled" fillcolor=yellow];
8 [label="Type operator: (R|<local>/statements|.R|kotlin/collections/last|<R|FirBase|>() as R|FirFunctionCall|)"];
9 [label="Postponed enter to lambda"];
subgraph cluster_4 {
color=blue
19 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
18 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
20 [label="Enter block"];
21 [label="Postponed enter to lambda"];
19 [label="Enter block"];
20 [label="Postponed enter to lambda"];
subgraph cluster_6 {
color=blue
26 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
25 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_7 {
color=blue
27 [label="Enter block"];
28 [label="Access variable this@R|special/anonymous|"];
29 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)"];
30 [label="Access variable R|<local>/arguments|"];
31 [label="Function call: R|<local>/arguments|.R|kotlin/collections/last|<R|FirBase|>()"];
32 [label="Type operator: (R|<local>/arguments|.R|kotlin/collections/last|<R|FirBase|>() as R|FirFunctionCall|)"];
33 [label="Postponed enter to lambda"];
26 [label="Enter block"];
27 [label="Access variable this@R|special/anonymous|"];
28 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)" style="filled" fillcolor=yellow];
29 [label="Access variable R|<local>/arguments|"];
30 [label="Function call: R|<local>/arguments|.R|kotlin/collections/last|<R|FirBase|>()" style="filled" fillcolor=yellow];
31 [label="Type operator: (R|<local>/arguments|.R|kotlin/collections/last|<R|FirBase|>() as R|FirFunctionCall|)"];
32 [label="Postponed enter to lambda"];
subgraph cluster_8 {
color=blue
39 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
37 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_9 {
color=blue
40 [label="Enter block"];
41 [label="Access variable this@R|special/anonymous|"];
42 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)"];
43 [label="Access variable R|<local>/explicitReceiver|"];
44 [label="Type operator: (R|<local>/explicitReceiver| as R|FirFunctionCall|)"];
45 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)"];
46 [label="Exit block"];
38 [label="Enter block"];
39 [label="Access variable this@R|special/anonymous|"];
40 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)" style="filled" fillcolor=yellow];
41 [label="Access variable R|<local>/explicitReceiver|"];
42 [label="Type operator: (R|<local>/explicitReceiver| as R|FirFunctionCall|)"];
43 [label="Function call: this@R|special/anonymous|.R|SubstitutionOverride<kotlin/collections/MutableList.add: R|kotlin/Boolean|>|(...)" style="filled" fillcolor=yellow];
44 [label="Exit block"];
}
47 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
45 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
34 [label="Call arguments union" style="filled" fillcolor=yellow];
35 [label="Postponed exit from lambda"];
36 [label="Function call: R|kotlin/with|<R|FirFunctionCall|, R|kotlin/Unit|>(...)"];
37 [label="Exit block"];
33 [label="Postponed exit from lambda"];
34 [label="Function call: R|kotlin/with|<R|FirFunctionCall|, R|kotlin/Unit|>(...)" style="filled" fillcolor=yellow];
35 [label="Exit block"];
}
38 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
36 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
22 [label="Postponed exit from lambda"];
23 [label="Function call: R|kotlin/collections/buildList|<R|FirFunctionCall|>(...)"];
24 [label="Exit block"];
21 [label="Postponed exit from lambda"];
22 [label="Function call: R|kotlin/collections/buildList|<R|FirFunctionCall|>(...)" style="filled" fillcolor=yellow];
23 [label="Exit block"];
}
25 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
24 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
10 [label="Call arguments union" style="filled" fillcolor=yellow];
11 [label="Postponed exit from lambda"];
12 [label="Function call: R|kotlin/with|<R|FirFunctionCall|, R|kotlin/collections/List<FirFunctionCall>|>(...)"];
13 [label="Variable declaration: lval firstCalls: R|kotlin/collections/List<FirFunctionCall>|"];
14 [label="Access variable R|<local>/firstCalls|"];
15 [label="Jump: ^foo R|<local>/firstCalls|"];
16 [label="Stub" style="filled" fillcolor=gray];
17 [label="Exit block" style="filled" fillcolor=gray];
10 [label="Postponed exit from lambda"];
11 [label="Function call: R|kotlin/with|<R|FirFunctionCall|, R|kotlin/collections/List<FirFunctionCall>|>(...)" style="filled" fillcolor=yellow];
12 [label="Variable declaration: lval firstCalls: R|kotlin/collections/List<FirFunctionCall>|"];
13 [label="Access variable R|<local>/firstCalls|"];
14 [label="Jump: ^foo R|<local>/firstCalls|"];
15 [label="Stub" style="filled" fillcolor=gray];
16 [label="Exit block" style="filled" fillcolor=gray];
}
18 [label="Exit function foo" style="filled" fillcolor=red];
17 [label="Exit function foo" style="filled" fillcolor=red];
}
4 -> {5};
5 -> {6};
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {19};
9 -> {11} [color=red];
9 -> {19} [style=dashed];
10 -> {12} [color=red];
11 -> {12} [color=green];
9 -> {18};
9 -> {10} [color=red];
9 -> {18} [style=dashed];
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {15};
15 -> {18};
14 -> {17};
14 -> {15} [style=dotted];
15 -> {16} [style=dotted];
16 -> {17} [style=dotted];
17 -> {18} [style=dotted];
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {26};
21 -> {22} [color=red];
21 -> {26} [style=dashed];
20 -> {25};
20 -> {21} [color=red];
20 -> {25} [style=dashed];
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {10} [color=red];
25 -> {11} [color=green];
24 -> {11} [color=red];
24 -> {10} [color=green];
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
33 -> {39};
33 -> {35} [color=red];
33 -> {39} [style=dashed];
34 -> {36} [color=red];
35 -> {36} [color=green];
36 -> {37};
32 -> {37};
32 -> {33} [color=red];
32 -> {37} [style=dashed];
33 -> {34};
34 -> {35};
35 -> {36};
36 -> {11} [color=red];
36 -> {21} [color=green];
37 -> {38};
38 -> {10} [color=red];
38 -> {22} [color=green];
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {34} [color=red];
47 -> {35} [color=green];
45 -> {34} [color=red];
45 -> {33} [color=green];
}