[FIR] Analyze all statements in block except last one in independent mode

Some broken tests will be fixed in next commit
#KT-37176 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-03-04 14:10:18 +03:00
parent 1c728e8184
commit 770dfb69ba
23 changed files with 252 additions and 174 deletions
@@ -183,11 +183,15 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
override fun transformBlock(block: FirBlock, data: ResolutionMode): CompositeTransformResult<FirStatement> { override fun transformBlock(block: FirBlock, data: ResolutionMode): CompositeTransformResult<FirStatement> {
dataFlowAnalyzer.enterBlock(block) dataFlowAnalyzer.enterBlock(block)
@Suppress("NAME_SHADOWING") val numberOfStatements = block.statements.size
val block = block.transformChildren(transformer, data) as FirBlock
val statement = block.statements.lastOrNull()
val resultExpression = when (statement) { block.transformStatementsIndexed(transformer) { index ->
if (index == numberOfStatements - 1) data else ResolutionMode.ContextIndependent
}
block.transformAllStatementsExceptLast(integerLiteralTypeApproximator, builtinTypes.intType.type)
block.transformOtherChildren(transformer, data)
val resultExpression = when (val statement = block.statements.lastOrNull()) {
is FirReturnExpression -> statement.result is FirReturnExpression -> statement.result
is FirExpression -> statement is FirExpression -> statement
else -> null else -> null
+4 -4
View File
@@ -183,7 +183,7 @@ digraph loops_kt {
subgraph cluster_19 { subgraph cluster_19 {
color=blue color=blue
63 [label="Enter block"]; 63 [label="Enter block"];
64 [label="Const: Int(1)"]; 64 [label="Const: IntegerLiteral(1)"];
65 [label="Exit block"]; 65 [label="Exit block"];
} }
66 [label="Exit loop block"]; 66 [label="Exit loop block"];
@@ -301,7 +301,7 @@ digraph loops_kt {
subgraph cluster_32 { subgraph cluster_32 {
color=blue color=blue
100 [label="Enter block" style="filled" fillcolor=gray]; 100 [label="Enter block" style="filled" fillcolor=gray];
101 [label="Const: Int(1)" style="filled" fillcolor=gray]; 101 [label="Const: IntegerLiteral(1)" style="filled" fillcolor=gray];
102 [label="Exit block" style="filled" fillcolor=gray]; 102 [label="Exit block" style="filled" fillcolor=gray];
} }
103 [label="Exit loop block" style="filled" fillcolor=gray]; 103 [label="Exit loop block" style="filled" fillcolor=gray];
@@ -338,7 +338,7 @@ digraph loops_kt {
subgraph cluster_36 { subgraph cluster_36 {
color=blue color=blue
110 [label="Enter block"]; 110 [label="Enter block"];
111 [label="Const: Int(1)"]; 111 [label="Const: IntegerLiteral(1)"];
112 [label="Exit block"]; 112 [label="Exit block"];
} }
113 [label="Exit loop block"]; 113 [label="Exit loop block"];
@@ -456,7 +456,7 @@ digraph loops_kt {
subgraph cluster_49 { subgraph cluster_49 {
color=blue color=blue
147 [label="Enter block"]; 147 [label="Enter block"];
148 [label="Const: Int(1)"]; 148 [label="Const: IntegerLiteral(1)"];
149 [label="Exit block"]; 149 [label="Exit block"];
} }
150 [label="Exit loop block"]; 150 [label="Exit loop block"];
+4 -4
View File
@@ -24,7 +24,7 @@ FILE: loops.kt
} }
public final fun testWhileTrue(): R|kotlin/Unit| { public final fun testWhileTrue(): R|kotlin/Unit| {
while(Boolean(true)) { while(Boolean(true)) {
Int(1) IntegerLiteral(1)
} }
Int(1) Int(1)
@@ -43,14 +43,14 @@ FILE: loops.kt
} }
public final fun testWhileFalse(): R|kotlin/Unit| { public final fun testWhileFalse(): R|kotlin/Unit| {
while(Boolean(false)) { while(Boolean(false)) {
Int(1) IntegerLiteral(1)
} }
Int(1) Int(1)
} }
public final fun testDoWhileTrue(): R|kotlin/Unit| { public final fun testDoWhileTrue(): R|kotlin/Unit| {
do { do {
Int(1) IntegerLiteral(1)
} }
while(Boolean(true)) while(Boolean(true))
Int(1) Int(1)
@@ -69,7 +69,7 @@ FILE: loops.kt
} }
public final fun testDoWhileFalse(): R|kotlin/Unit| { public final fun testDoWhileFalse(): R|kotlin/Unit| {
do { do {
Int(1) IntegerLiteral(1)
} }
while(Boolean(false)) while(Boolean(false))
Int(1) Int(1)
@@ -16,10 +16,11 @@ digraph callsInPlace_kt {
6 [label="Exit function anonymousFunction"]; 6 [label="Exit function anonymousFunction"];
} }
7 [label="Postponed exit from lambda"]; 7 [label="Postponed exit from lambda"];
8 [label="Function call: R|kotlin/run|<R|kotlin/Unit|>(<L> = run@fun <anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> )"]; 8 [label="Function call: R|kotlin/run|<R|kotlin/Nothing|>(<L> = run@fun <anonymous>(): R|kotlin/Nothing| <kind=EXACTLY_ONCE> )"];
9 [label="Access variable R|<local>/x|"]; 9 [label="Stub" style="filled" fillcolor=gray];
10 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()"]; 10 [label="Access variable R|<local>/x|" style="filled" fillcolor=gray];
11 [label="Exit function test" style="filled" fillcolor=red]; 11 [label="Function call: R|<local>/x|.R|kotlin/Int.inc|()" style="filled" fillcolor=gray];
12 [label="Exit function test" style="filled" fillcolor=red];
} }
0 -> {1}; 0 -> {1};
@@ -31,256 +32,258 @@ digraph callsInPlace_kt {
5 -> {6}; 5 -> {6};
6 -> {7} [color=green]; 6 -> {7} [color=green];
7 -> {8}; 7 -> {8};
8 -> {9}; 8 -> {12};
9 -> {10}; 8 -> {9} [style=dotted];
10 -> {11}; 9 -> {10} [style=dotted];
10 -> {11} [style=dotted];
11 -> {12} [style=dotted];
subgraph cluster_2 { subgraph cluster_2 {
color=red color=red
12 [label="Enter function test_2" style="filled" fillcolor=red]; 13 [label="Enter function test_2" style="filled" fillcolor=red];
13 [label="Const: Int(10)"]; 14 [label="Const: Int(10)"];
14 [label="Postponed enter to lambda"]; 15 [label="Postponed enter to lambda"];
subgraph cluster_3 { subgraph cluster_3 {
color=blue color=blue
15 [label="Enter function anonymousFunction"]; 16 [label="Enter function anonymousFunction"];
16 [label="Const: String(test_2)"]; 17 [label="Const: String(test_2)"];
17 [label="Exit function anonymousFunction"]; 18 [label="Exit function anonymousFunction"];
} }
18 [label="Postponed exit from lambda"]; 19 [label="Postponed exit from lambda"];
19 [label="Function call: R|kotlin/repeat|(Int(10), <L> = repeat@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Unit| <kind=UNKNOWN> )"]; 20 [label="Function call: R|kotlin/repeat|(Int(10), <L> = repeat@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Unit| <kind=UNKNOWN> )"];
20 [label="Exit function test_2" style="filled" fillcolor=red]; 21 [label="Exit function test_2" style="filled" fillcolor=red];
} }
12 -> {13};
13 -> {14}; 13 -> {14};
14 -> {15}; 14 -> {15};
14 -> {18} [color=red]; 15 -> {16};
15 -> {17 16}; 15 -> {19} [color=red];
16 -> {17}; 16 -> {18 17};
17 -> {15}; 17 -> {18};
17 -> {18} [color=green]; 18 -> {16};
18 -> {19}; 18 -> {19} [color=green];
19 -> {20}; 19 -> {20};
20 -> {21};
subgraph cluster_4 { subgraph cluster_4 {
color=red color=red
21 [label="Enter function test_3" style="filled" fillcolor=red]; 22 [label="Enter function test_3" style="filled" fillcolor=red];
22 [label="Postponed enter to lambda"]; 23 [label="Postponed enter to lambda"];
subgraph cluster_5 { subgraph cluster_5 {
color=blue color=blue
23 [label="Enter function anonymousFunction"]; 24 [label="Enter function anonymousFunction"];
24 [label="Const: String(test_3)"]; 25 [label="Const: String(test_3)"];
25 [label="Exit function anonymousFunction"]; 26 [label="Exit function anonymousFunction"];
} }
26 [label="Postponed exit from lambda"]; 27 [label="Postponed exit from lambda"];
27 [label="Const: Int(10)"]; 28 [label="Const: Int(10)"];
28 [label="Function call: R|kotlin/repeat|(action = repeat@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Unit| <kind=UNKNOWN> , times = Int(10))"]; 29 [label="Function call: R|kotlin/repeat|(action = repeat@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Unit| <kind=UNKNOWN> , times = Int(10))"];
29 [label="Exit function test_3" style="filled" fillcolor=red]; 30 [label="Exit function test_3" style="filled" fillcolor=red];
} }
21 -> {22};
22 -> {23}; 22 -> {23};
22 -> {26} [color=red]; 23 -> {24};
23 -> {25 24}; 23 -> {27} [color=red];
24 -> {25}; 24 -> {26 25};
25 -> {23}; 25 -> {26};
25 -> {26} [color=green]; 26 -> {24};
26 -> {27}; 26 -> {27} [color=green];
27 -> {28}; 27 -> {28};
28 -> {29}; 28 -> {29};
29 -> {30};
subgraph cluster_6 { subgraph cluster_6 {
color=red color=red
30 [label="Enter function test_4" style="filled" fillcolor=red]; 31 [label="Enter function test_4" style="filled" fillcolor=red];
31 [label="Const: Int(1)"]; 32 [label="Const: Int(1)"];
32 [label="Postponed enter to lambda"]; 33 [label="Postponed enter to lambda"];
subgraph cluster_7 { subgraph cluster_7 {
color=blue color=blue
33 [label="Enter function anonymousFunction"]; 34 [label="Enter function anonymousFunction"];
34 [label="Const: String(test_4)"]; 35 [label="Const: String(test_4)"];
35 [label="Access variable R|<local>/it|"]; 36 [label="Access variable R|<local>/it|"];
36 [label="Const: Int(0)"]; 37 [label="Const: Int(0)"];
37 [label="Function call: R|<local>/it|.R|kotlin/Int.compareTo|(Int(0))"]; 38 [label="Function call: R|<local>/it|.R|kotlin/Int.compareTo|(Int(0))"];
38 [label="Comparison >"]; 39 [label="Comparison >"];
39 [label="Exit function anonymousFunction"]; 40 [label="Exit function anonymousFunction"];
} }
40 [label="Postponed exit from lambda"]; 41 [label="Postponed exit from lambda"];
41 [label="Function call: Int(1).R|kotlin/takeUnless|<R|kotlin/Int|>(<L> = takeUnless@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Boolean| <kind=EXACTLY_ONCE> )"]; 42 [label="Function call: Int(1).R|kotlin/takeUnless|<R|kotlin/Int|>(<L> = takeUnless@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Boolean| <kind=EXACTLY_ONCE> )"];
42 [label="Exit function test_4" style="filled" fillcolor=red]; 43 [label="Exit function test_4" style="filled" fillcolor=red];
} }
30 -> {31};
31 -> {32}; 31 -> {32};
32 -> {33}; 32 -> {33};
32 -> {40} [color=red];
33 -> {34}; 33 -> {34};
33 -> {41} [color=red];
34 -> {35}; 34 -> {35};
35 -> {36}; 35 -> {36};
36 -> {37}; 36 -> {37};
37 -> {38}; 37 -> {38};
38 -> {39}; 38 -> {39};
39 -> {40} [color=green]; 39 -> {40};
40 -> {41}; 40 -> {41} [color=green];
41 -> {42}; 41 -> {42};
42 -> {43};
subgraph cluster_8 { subgraph cluster_8 {
color=red color=red
43 [label="Enter function test_5" style="filled" fillcolor=red]; 44 [label="Enter function test_5" style="filled" fillcolor=red];
44 [label="Const: Int(1)"]; 45 [label="Const: Int(1)"];
45 [label="Postponed enter to lambda"]; 46 [label="Postponed enter to lambda"];
subgraph cluster_9 { subgraph cluster_9 {
color=blue color=blue
46 [label="Enter function anonymousFunction"]; 47 [label="Enter function anonymousFunction"];
47 [label="Const: String(test_5)"]; 48 [label="Const: String(test_5)"];
48 [label="Access variable R|<local>/it|"]; 49 [label="Access variable R|<local>/it|"];
49 [label="Const: Int(0)"]; 50 [label="Const: Int(0)"];
50 [label="Function call: R|<local>/it|.R|kotlin/Int.compareTo|(Int(0))"]; 51 [label="Function call: R|<local>/it|.R|kotlin/Int.compareTo|(Int(0))"];
51 [label="Comparison >"]; 52 [label="Comparison >"];
52 [label="Exit function anonymousFunction"]; 53 [label="Exit function anonymousFunction"];
} }
53 [label="Postponed exit from lambda"]; 54 [label="Postponed exit from lambda"];
54 [label="Function call: Int(1).R|kotlin/takeUnless|<R|kotlin/Int|>(predicate = takeUnless@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Boolean| <kind=EXACTLY_ONCE> )"]; 55 [label="Function call: Int(1).R|kotlin/takeUnless|<R|kotlin/Int|>(predicate = takeUnless@fun <anonymous>(it: R|kotlin/Int|): R|kotlin/Boolean| <kind=EXACTLY_ONCE> )"];
55 [label="Exit function test_5" style="filled" fillcolor=red]; 56 [label="Exit function test_5" style="filled" fillcolor=red];
} }
43 -> {44};
44 -> {45}; 44 -> {45};
45 -> {46}; 45 -> {46};
45 -> {53} [color=red];
46 -> {47}; 46 -> {47};
46 -> {54} [color=red];
47 -> {48}; 47 -> {48};
48 -> {49}; 48 -> {49};
49 -> {50}; 49 -> {50};
50 -> {51}; 50 -> {51};
51 -> {52}; 51 -> {52};
52 -> {53} [color=green]; 52 -> {53};
53 -> {54}; 53 -> {54} [color=green];
54 -> {55}; 54 -> {55};
55 -> {56};
subgraph cluster_10 { subgraph cluster_10 {
color=red color=red
56 [label="Enter function myRun" style="filled" fillcolor=red]; 57 [label="Enter function myRun" style="filled" fillcolor=red];
57 [label="Function call: R|<local>/block1|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"]; 58 [label="Function call: R|<local>/block1|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
58 [label="Function call: R|<local>/block2|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"]; 59 [label="Function call: R|<local>/block2|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
59 [label="Exit function myRun" style="filled" fillcolor=red]; 60 [label="Exit function myRun" style="filled" fillcolor=red];
} }
56 -> {57};
57 -> {58}; 57 -> {58};
58 -> {59}; 58 -> {59};
59 -> {60};
subgraph cluster_11 { subgraph cluster_11 {
color=red color=red
60 [label="Enter function test_6" style="filled" fillcolor=red]; 61 [label="Enter function test_6" style="filled" fillcolor=red];
61 [label="Postponed enter to lambda"]; 62 [label="Postponed enter to lambda"];
subgraph cluster_12 { subgraph cluster_12 {
color=blue color=blue
62 [label="Enter function anonymousFunction"]; 63 [label="Enter function anonymousFunction"];
63 [label="Const: String(test_6_1)"]; 64 [label="Const: String(test_6_1)"];
64 [label="Exit function anonymousFunction"]; 65 [label="Exit function anonymousFunction"];
} }
65 [label="Postponed exit from lambda"]; 66 [label="Postponed exit from lambda"];
66 [label="Postponed enter to lambda"]; 67 [label="Postponed enter to lambda"];
subgraph cluster_13 { subgraph cluster_13 {
color=blue color=blue
67 [label="Enter function anonymousFunction"]; 68 [label="Enter function anonymousFunction"];
68 [label="Const: String(test_6_2)"]; 69 [label="Const: String(test_6_2)"];
69 [label="Exit function anonymousFunction"]; 70 [label="Exit function anonymousFunction"];
} }
70 [label="Postponed exit from lambda"]; 71 [label="Postponed exit from lambda"];
71 [label="Function call: R|/myRun|(myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> , <L> = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> )"]; 72 [label="Function call: R|/myRun|(myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> , <L> = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> )"];
72 [label="Exit function test_6" style="filled" fillcolor=red]; 73 [label="Exit function test_6" style="filled" fillcolor=red];
} }
60 -> {61};
61 -> {62}; 61 -> {62};
61 -> {65} [color=red]; 62 -> {63};
62 -> {64 63}; 62 -> {66} [color=red];
63 -> {64}; 63 -> {65 64};
64 -> {62}; 64 -> {65};
64 -> {65} [color=green]; 65 -> {63};
65 -> {66}; 65 -> {66} [color=green];
66 -> {67}; 66 -> {67};
66 -> {70} [color=red]; 67 -> {68};
67 -> {69 68}; 67 -> {71} [color=red];
68 -> {69}; 68 -> {70 69};
69 -> {67}; 69 -> {70};
69 -> {70} [color=green]; 70 -> {68};
70 -> {71}; 70 -> {71} [color=green];
71 -> {72}; 71 -> {72};
72 -> {73};
subgraph cluster_14 { subgraph cluster_14 {
color=red color=red
73 [label="Enter function test_7" style="filled" fillcolor=red]; 74 [label="Enter function test_7" style="filled" fillcolor=red];
74 [label="Postponed enter to lambda"]; 75 [label="Postponed enter to lambda"];
subgraph cluster_15 { subgraph cluster_15 {
color=blue color=blue
75 [label="Enter function anonymousFunction"]; 76 [label="Enter function anonymousFunction"];
76 [label="Const: String(test_7_2)"]; 77 [label="Const: String(test_7_2)"];
77 [label="Exit function anonymousFunction"]; 78 [label="Exit function anonymousFunction"];
} }
78 [label="Postponed exit from lambda"]; 79 [label="Postponed exit from lambda"];
79 [label="Postponed enter to lambda"]; 80 [label="Postponed enter to lambda"];
subgraph cluster_16 { subgraph cluster_16 {
color=blue color=blue
80 [label="Enter function anonymousFunction"]; 81 [label="Enter function anonymousFunction"];
81 [label="Const: String(test_7_1)"]; 82 [label="Const: String(test_7_1)"];
82 [label="Exit function anonymousFunction"]; 83 [label="Exit function anonymousFunction"];
} }
83 [label="Postponed exit from lambda"]; 84 [label="Postponed exit from lambda"];
84 [label="Function call: R|/myRun|(block2 = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> , block1 = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> )"]; 85 [label="Function call: R|/myRun|(block2 = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> , block1 = myRun@fun <anonymous>(): R|kotlin/Unit| <kind=UNKNOWN> )"];
85 [label="Exit function test_7" style="filled" fillcolor=red]; 86 [label="Exit function test_7" style="filled" fillcolor=red];
} }
73 -> {74};
74 -> {75}; 74 -> {75};
74 -> {78} [color=red]; 75 -> {76};
75 -> {77 76}; 75 -> {79} [color=red];
76 -> {77}; 76 -> {78 77};
77 -> {75}; 77 -> {78};
77 -> {78} [color=green]; 78 -> {76};
78 -> {79}; 78 -> {79} [color=green];
79 -> {80}; 79 -> {80};
79 -> {83} [color=red]; 80 -> {81};
80 -> {82 81}; 80 -> {84} [color=red];
81 -> {82}; 81 -> {83 82};
82 -> {80}; 82 -> {83};
82 -> {83} [color=green]; 83 -> {81};
83 -> {84}; 83 -> {84} [color=green];
84 -> {85}; 84 -> {85};
85 -> {86};
subgraph cluster_17 { subgraph cluster_17 {
color=red color=red
86 [label="Enter function myDummyRun" style="filled" fillcolor=red]; 87 [label="Enter function myDummyRun" style="filled" fillcolor=red];
87 [label="Function call: R|<local>/block|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"]; 88 [label="Function call: R|<local>/block|.R|FakeOverride<kotlin/Function0.invoke: R|kotlin/Unit|>|()"];
88 [label="Exit function myDummyRun" style="filled" fillcolor=red]; 89 [label="Exit function myDummyRun" style="filled" fillcolor=red];
} }
86 -> {87};
87 -> {88}; 87 -> {88};
88 -> {89};
subgraph cluster_18 { subgraph cluster_18 {
color=red color=red
89 [label="Enter function test_8" style="filled" fillcolor=red]; 90 [label="Enter function test_8" style="filled" fillcolor=red];
90 [label="Postponed enter to lambda"]; 91 [label="Postponed enter to lambda"];
91 [label="Postponed exit from lambda"]; 92 [label="Postponed exit from lambda"];
92 [label="Function call: R|/myDummyRun|(<L> = myDummyRun@fun <anonymous>(): R|kotlin/Unit|)"]; 93 [label="Function call: R|/myDummyRun|(<L> = myDummyRun@fun <anonymous>(): R|kotlin/Unit|)"];
93 [label="Exit function test_8" style="filled" fillcolor=red]; 94 [label="Exit function test_8" style="filled" fillcolor=red];
} }
89 -> {90}; 90 -> {91};
90 -> {91 91} [color=green]; 91 -> {92 92} [color=green];
91 -> {92};
92 -> {93}; 92 -> {93};
93 -> {94};
subgraph cluster_19 { subgraph cluster_19 {
color=red color=red
94 [label="Enter function anonymousFunction" style="filled" fillcolor=red]; 95 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
95 [label="Const: String(test_8)"]; 96 [label="Const: String(test_8)"];
96 [label="Exit function anonymousFunction" style="filled" fillcolor=red]; 97 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
} }
94 -> {95};
95 -> {96}; 95 -> {96};
96 -> {97};
} }
@@ -1,7 +1,7 @@
FILE: callsInPlace.kt FILE: callsInPlace.kt
public final fun test(): R|kotlin/Unit| { public final fun test(): R|kotlin/Unit| {
lval x: R|kotlin/Int| lval x: R|kotlin/Int|
R|kotlin/run|<R|kotlin/Unit|>(<L> = run@fun <anonymous>(): R|kotlin/Unit| <kind=EXACTLY_ONCE> { R|kotlin/run|<R|kotlin/Nothing|>(<L> = run@fun <anonymous>(): R|kotlin/Nothing| <kind=EXACTLY_ONCE> {
R|<local>/x| = Int(1) R|<local>/x| = Int(1)
} }
) )
@@ -23,4 +23,8 @@ abstract class FirBlock : FirExpression() {
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R = visitor.visitBlock(this, data) override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R = visitor.visitBlock(this, data)
abstract override fun replaceTypeRef(newTypeRef: FirTypeRef) abstract override fun replaceTypeRef(newTypeRef: FirTypeRef)
abstract fun <D> transformStatements(transformer: FirTransformer<D>, data: D): FirBlock
abstract fun <D> transformOtherChildren(transformer: FirTransformer<D>, data: D): FirBlock
} }
@@ -32,8 +32,18 @@ internal class FirBlockImpl(
} }
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirBlockImpl { override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirBlockImpl {
annotations.transformInplace(transformer, data) transformStatements(transformer, data)
transformOtherChildren(transformer, data)
return this
}
override fun <D> transformStatements(transformer: FirTransformer<D>, data: D): FirBlockImpl {
statements.transformInplace(transformer, data) statements.transformInplace(transformer, data)
return this
}
override fun <D> transformOtherChildren(transformer: FirTransformer<D>, data: D): FirBlockImpl {
annotations.transformInplace(transformer, data)
typeRef = typeRef.transformSingle(transformer, data) typeRef = typeRef.transformSingle(transformer, data)
return this return this
} }
@@ -30,6 +30,15 @@ class FirEmptyExpressionBlock : FirBlock() {
} }
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirEmptyExpressionBlock { override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirEmptyExpressionBlock {
transformOtherChildren(transformer, data)
return this
}
override fun <D> transformStatements(transformer: FirTransformer<D>, data: D): FirEmptyExpressionBlock {
return this
}
override fun <D> transformOtherChildren(transformer: FirTransformer<D>, data: D): FirEmptyExpressionBlock {
typeRef = typeRef.transformSingle(transformer, data) typeRef = typeRef.transformSingle(transformer, data)
return this return this
} }
@@ -48,6 +48,7 @@ class BuiltinTypes {
val enumType: FirImplicitBuiltinTypeRef = FirImplicitEnumTypeRef(null) val enumType: FirImplicitBuiltinTypeRef = FirImplicitEnumTypeRef(null)
val annotationType: FirImplicitBuiltinTypeRef = FirImplicitAnnotationTypeRef(null) val annotationType: FirImplicitBuiltinTypeRef = FirImplicitAnnotationTypeRef(null)
val booleanType: FirImplicitBuiltinTypeRef = FirImplicitBooleanTypeRef(null) val booleanType: FirImplicitBuiltinTypeRef = FirImplicitBooleanTypeRef(null)
val intType: FirImplicitBuiltinTypeRef = FirImplicitIntTypeRef(null)
val nothingType: FirImplicitBuiltinTypeRef = FirImplicitNothingTypeRef(null) val nothingType: FirImplicitBuiltinTypeRef = FirImplicitNothingTypeRef(null)
val nullableNothingType: FirImplicitBuiltinTypeRef = FirImplicitNullableNothingTypeRef(null) val nullableNothingType: FirImplicitBuiltinTypeRef = FirImplicitNullableNothingTypeRef(null)
val stringType: FirImplicitBuiltinTypeRef = FirImplicitStringTypeRef(null) val stringType: FirImplicitBuiltinTypeRef = FirImplicitStringTypeRef(null)
@@ -10,13 +10,14 @@ import org.jetbrains.kotlin.fir.diagnostics.FirDiagnostic
import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression
import org.jetbrains.kotlin.fir.expressions.builder.buildErrorExpression import org.jetbrains.kotlin.fir.expressions.builder.buildErrorExpression
import org.jetbrains.kotlin.fir.expressions.builder.buildErrorLoop import org.jetbrains.kotlin.fir.expressions.builder.buildErrorLoop
import org.jetbrains.kotlin.fir.expressions.impl.FirConstExpressionImpl import org.jetbrains.kotlin.fir.expressions.impl.*
import org.jetbrains.kotlin.fir.expressions.impl.FirErrorExpressionImpl import org.jetbrains.kotlin.fir.expressions.impl.FirBlockImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirErrorLoopImpl
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.fir.visitors.transformInplace
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
inline val FirAnnotationCall.coneClassLikeType: ConeClassLikeType? inline val FirAnnotationCall.coneClassLikeType: ConeClassLikeType?
@@ -56,3 +57,20 @@ fun buildErrorExpression(source: FirSourceElement?, diagnostic: FirDiagnostic):
this.diagnostic = diagnostic this.diagnostic = diagnostic
} }
} }
fun <D : Any> FirBlock.transformStatementsIndexed(transformer: FirTransformer<D>, dataProducer: (Int) -> D?): FirBlock {
when (this) {
is FirBlockImpl -> statements.transformInplace(transformer, dataProducer)
is FirSingleExpressionBlock -> {
dataProducer(0)?.let { transformStatements(transformer, it) }
}
}
return this
}
fun <D : Any> FirBlock.transformAllStatementsExceptLast(transformer: FirTransformer<D>, data: D): FirBlock {
val threshold = statements.size - 1
return transformStatementsIndexed(transformer) { index ->
data.takeIf { index < threshold }
}
}
@@ -32,15 +32,25 @@ class FirSingleExpressionBlock(
} }
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirSingleExpressionBlock { override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirSingleExpressionBlock {
annotations.transformInplace(transformer, data) transformStatements(transformer, data)
statement = statement.transformSingle(transformer, data) transformOtherChildren(transformer, data)
typeRef = typeRef.transformSingle(transformer, data)
return this return this
} }
override fun replaceTypeRef(newTypeRef: FirTypeRef) { override fun replaceTypeRef(newTypeRef: FirTypeRef) {
typeRef = newTypeRef typeRef = newTypeRef
} }
override fun <D> transformStatements(transformer: FirTransformer<D>, data: D): FirBlock {
statement = statement.transformSingle(transformer, data)
return this
}
override fun <D> transformOtherChildren(transformer: FirTransformer<D>, data: D): FirBlock {
annotations.transformInplace(transformer, data)
typeRef = typeRef.transformSingle(transformer, data)
return this
}
} }
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
@@ -63,6 +63,10 @@ class FirImplicitBooleanTypeRef(
source: FirSourceElement? source: FirSourceElement?
) : FirImplicitBuiltinTypeRef(source, StandardClassIds.Boolean) ) : FirImplicitBuiltinTypeRef(source, StandardClassIds.Boolean)
class FirImplicitIntTypeRef(
source: FirSourceElement?
) : FirImplicitBuiltinTypeRef(source, StandardClassIds.Int)
class FirImplicitNothingTypeRef( class FirImplicitNothingTypeRef(
source: FirSourceElement? source: FirSourceElement?
) : FirImplicitBuiltinTypeRef(source, StandardClassIds.Nothing) ) : FirImplicitBuiltinTypeRef(source, StandardClassIds.Nothing)
@@ -34,3 +34,13 @@ fun <T : FirElement, D> MutableList<T>.transformInplace(transformer: FirTransfor
} }
} }
inline fun <T : FirElement, D : Any> MutableList<T>.transformInplace(transformer: FirTransformer<D>, dataProducer: (Int) -> D?) {
val iterator = this.listIterator()
var index = 0
while (iterator.hasNext()) {
val next = iterator.next() as FirPureAbstractElement
val data = dataProducer(index++) ?: continue
val result = next.transform<T, D>(transformer, data).single
iterator.set(result)
}
}
@@ -110,8 +110,9 @@ object NodeConfigurator : AbstractFieldConfigurator<FirTreeBuilder>(FirTreeBuild
} }
block.configure { block.configure {
+fieldList(statement) +fieldList(statement).withTransform()
+typeRefField +typeRefField
needTransformOtherChildren()
} }
binaryLogicExpression.configure { binaryLogicExpression.configure {
@@ -1,3 +1,4 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME // WITH_RUNTIME
fun box(): String { fun box(): String {
@@ -1,3 +1,4 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME // WITH_RUNTIME
fun box(): String { fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FULL_JDK // FULL_JDK
// WITH_RUNTIME // WITH_RUNTIME
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
@@ -1,3 +1,4 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String { fun box(): String {
lateinit var ok: String lateinit var ok: String
run { run {
@@ -1,3 +1,4 @@
// IGNORE_BACKEND_FIR: JVM_IR
lateinit var ok: String lateinit var ok: String
fun box(): String { fun box(): String {
@@ -1,3 +1,4 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM // TARGET_BACKEND: JVM
// WITH_RUNTIME // WITH_RUNTIME
@@ -14,9 +14,9 @@ fun test() {
val x5: (Double) -> Unit = baz(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id(id(<!UNRESOLVED_REFERENCE!>::foo<!>))) val x5: (Double) -> Unit = baz(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id(id(<!UNRESOLVED_REFERENCE!>::foo<!>)))
id<(Int) -> Unit>(id(id(<!UNRESOLVED_REFERENCE!>::foo<!>))) id<(Int) -> Unit>(id(id(::foo)))
id(id<(Int) -> Unit>(::foo)) id(id<(Int) -> Unit>(::foo))
baz<(Int) -> Unit>(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id(id(<!UNRESOLVED_REFERENCE!>::foo<!>))) baz<(Int) -> Unit>(id(::foo), id(id(::foo)))
baz(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id(id<(Int) -> Unit>(::foo))) baz(id(::foo), id(id<(Int) -> Unit>(::foo)))
baz(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id<(Int) -> Unit>(id(::foo))) baz(id(<!UNRESOLVED_REFERENCE!>::foo<!>), id<(Int) -> Unit>(id(::foo)))
} }
@@ -22,7 +22,7 @@ FILE fqName:<root> fileName:/useNextParamInLambda.kt
BLOCK_BODY BLOCK_BODY
VAR name:result type:kotlin.String [var] VAR name:result type:kotlin.String [var]
CONST String type=kotlin.String value="fail" CONST String type=kotlin.String value="fail"
TRY type=kotlin.String TRY type=kotlin.Any
try: CALL 'public final fun f (f1: kotlin.Function0<kotlin.String>, f2: kotlin.Function0<kotlin.String>): kotlin.String declared in <root>' type=kotlin.String origin=null try: CALL 'public final fun f (f1: kotlin.Function0<kotlin.String>, f2: kotlin.Function0<kotlin.String>): kotlin.String declared in <root>' type=kotlin.String origin=null
CATCH parameter=val e: java.lang.Exception [val] declared in <root>.box CATCH parameter=val e: java.lang.Exception [val] declared in <root>.box
VAR name:e type:java.lang.Exception [val] VAR name:e type:java.lang.Exception [val]
+2 -2
View File
@@ -24,7 +24,7 @@ FILE fqName:<root> fileName:/ifElseIf.kt
FUN name:testEmptyBranches1 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit FUN name:testEmptyBranches1 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit
VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean
BLOCK_BODY BLOCK_BODY
WHEN type=kotlin.Unit origin=IF WHEN type=kotlin.Any origin=IF
BRANCH BRANCH
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches1' type=kotlin.Boolean origin=null if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches1' type=kotlin.Boolean origin=null
then: BLOCK type=kotlin.Unit origin=null then: BLOCK type=kotlin.Unit origin=null
@@ -38,7 +38,7 @@ FILE fqName:<root> fileName:/ifElseIf.kt
FUN name:testEmptyBranches2 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit FUN name:testEmptyBranches2 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit
VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean
BLOCK_BODY BLOCK_BODY
WHEN type=kotlin.Unit origin=IF WHEN type=kotlin.Any origin=IF
BRANCH BRANCH
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches2' type=kotlin.Boolean origin=null if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches2' type=kotlin.Boolean origin=null
then: BLOCK type=kotlin.Unit origin=null then: BLOCK type=kotlin.Unit origin=null