Files
kotlin-fork/compiler/fir/analysis-tests/testData/resolve/smartcasts/lambdas/inPlaceLambdas.dot
T
pyos a9be27e330 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.
2022-12-08 10:19:29 +00:00

242 lines
8.2 KiB
Plaintext
Vendored

digraph inPlaceLambdas_kt {
graph [nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
1 [label="Exit function foo" style="filled" fillcolor=red];
}
0 -> {1};
subgraph cluster_1 {
color=red
2 [label="Enter class A" style="filled" fillcolor=red];
3 [label="Exit class A" style="filled" fillcolor=red];
}
2 -> {3} [color=green];
subgraph cluster_2 {
color=red
4 [label="Enter function bar" style="filled" fillcolor=red];
5 [label="Exit function bar" style="filled" fillcolor=red];
}
4 -> {5};
subgraph cluster_3 {
color=red
6 [label="Enter class B" style="filled" fillcolor=red];
7 [label="Exit class B" style="filled" fillcolor=red];
}
6 -> {7} [color=green];
subgraph cluster_4 {
color=red
8 [label="Enter function test_1" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
9 [label="Enter block"];
subgraph cluster_6 {
color=blue
10 [label="Enter when"];
subgraph cluster_7 {
color=blue
11 [label="Enter when branch condition "];
12 [label="Access variable R|<local>/x|"];
13 [label="Type operator: (R|<local>/x| is R|A|)"];
14 [label="Exit when branch condition"];
}
15 [label="Synthetic else branch"];
16 [label="Enter when branch result"];
subgraph cluster_8 {
color=blue
17 [label="Enter block"];
18 [label="Postponed enter to lambda"];
subgraph cluster_9 {
color=blue
26 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
27 [label="Enter block"];
28 [label="Access variable R|<local>/x|"];
29 [label="Smart cast: R|<local>/x|"];
30 [label="Function call: R|<local>/x|.R|/A.foo|()" style="filled" fillcolor=yellow];
31 [label="Exit block"];
}
32 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
19 [label="Postponed exit from lambda"];
20 [label="Function call: R|kotlin/run|<R|kotlin/Unit|>(...)" style="filled" fillcolor=yellow];
21 [label="Exit block"];
}
22 [label="Exit when branch result"];
23 [label="Exit when"];
}
24 [label="Exit block"];
}
25 [label="Exit function test_1" style="filled" fillcolor=red];
}
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {16 15};
15 -> {23};
16 -> {17};
17 -> {18};
18 -> {26};
18 -> {19} [color=red];
18 -> {26} [style=dashed];
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {20} [color=red];
32 -> {19} [color=green];
subgraph cluster_11 {
color=red
33 [label="Enter function test_2" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
34 [label="Enter block"];
35 [label="Postponed enter to lambda"];
subgraph cluster_13 {
color=blue
43 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
44 [label="Enter block"];
45 [label="Access variable R|<local>/x|"];
46 [label="Type operator: (R|<local>/x| as R|B|)"];
47 [label="Exit block"];
}
48 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
36 [label="Postponed exit from lambda"];
37 [label="Function call: R|kotlin/run|<R|B|>(...)" style="filled" fillcolor=yellow];
38 [label="Access variable R|<local>/x|"];
39 [label="Smart cast: R|<local>/x|"];
40 [label="Function call: R|<local>/x|.R|/B.bar|()" style="filled" fillcolor=yellow];
41 [label="Exit block"];
}
42 [label="Exit function test_2" style="filled" fillcolor=red];
}
33 -> {34};
34 -> {35};
35 -> {43};
35 -> {36} [color=red];
35 -> {43} [style=dashed];
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {37} [color=red];
48 -> {36} [color=green];
subgraph cluster_15 {
color=red
49 [label="Enter function test_3" style="filled" fillcolor=red];
subgraph cluster_16 {
color=blue
50 [label="Enter block"];
subgraph cluster_17 {
color=blue
51 [label="Enter when"];
subgraph cluster_18 {
color=blue
52 [label="Enter when branch condition "];
53 [label="Access variable R|<local>/x|"];
54 [label="Type operator: (R|<local>/x| is R|A|)"];
55 [label="Exit when branch condition"];
}
56 [label="Synthetic else branch"];
57 [label="Enter when branch result"];
subgraph cluster_19 {
color=blue
58 [label="Enter block"];
59 [label="Postponed enter to lambda"];
subgraph cluster_20 {
color=blue
70 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_21 {
color=blue
71 [label="Enter block"];
72 [label="Access variable R|<local>/x|"];
73 [label="Smart cast: R|<local>/x|"];
74 [label="Function call: R|<local>/x|.R|/A.foo|()" style="filled" fillcolor=yellow];
75 [label="Access variable R|<local>/x|"];
76 [label="Smart cast: R|<local>/x|"];
77 [label="Type operator: (R|<local>/x| as R|B|)"];
78 [label="Exit block"];
}
79 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
60 [label="Postponed exit from lambda"];
61 [label="Function call: R|kotlin/run|<R|B|>(...)" style="filled" fillcolor=yellow];
62 [label="Access variable R|<local>/x|"];
63 [label="Smart cast: R|<local>/x|"];
64 [label="Function call: R|<local>/x|.R|/B.bar|()" style="filled" fillcolor=yellow];
65 [label="Exit block"];
}
66 [label="Exit when branch result"];
67 [label="Exit when"];
}
68 [label="Exit block"];
}
69 [label="Exit function test_3" style="filled" fillcolor=red];
}
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {54};
54 -> {55};
55 -> {57 56};
56 -> {67};
57 -> {58};
58 -> {59};
59 -> {70};
59 -> {60} [color=red];
59 -> {70} [style=dashed];
60 -> {61};
61 -> {62};
62 -> {63};
63 -> {64};
64 -> {65};
65 -> {66};
66 -> {67};
67 -> {68};
68 -> {69};
70 -> {71};
71 -> {72};
72 -> {73};
73 -> {74};
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {61} [color=red];
79 -> {60} [color=green];
}