1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import jdk.incubator.code.Quotable;
25 import jdk.incubator.code.Quoted;
26 import jdk.incubator.code.CodeReflection;
27 import java.util.function.IntUnaryOperator;
28
29 /*
30 * @test
31 * @summary Smoke test for code reflection with unreachable areas.
32 * @modules jdk.incubator.code
33 * @enablePreview
34 * @build UnreachableTest
35 * @build CodeReflectionTester
36 * @run main CodeReflectionTester UnreachableTest
37 */
38
39 public class UnreachableTest {
40
41 @CodeReflection
42 @IR("""
43 func @"test1" ()java.type:"void" -> {
44 java.block ()java.type:"void" -> {
45 return;
46 };
47 unreachable;
48 };
49 """)
50 static void test1() {
51 {
52 return;
53 }
54 }
55
56 @CodeReflection
57 @IR("""
58 func @"test2" (%0 : java.type:"int")java.type:"int" -> {
59 %1 : Var<java.type:"int"> = var %0 @"i";
60 java.block ()java.type:"void" -> {
61 %2 : java.type:"int" = var.load %1;
62 return %2;
63 };
64 unreachable;
65 };
66 """)
67 static int test2(int i) {
68 {
69 return i;
70 }
71 }
72
73 @CodeReflection
74 @IR("""
75 func @"test3" (%0 : java.type:"int")java.type:"int" -> {
76 %1 : Var<java.type:"int"> = var %0 @"i";
77 java.if
78 ()java.type:"boolean" -> {
79 %2 : java.type:"boolean" = constant @true;
80 yield %2;
81 }
82 ()java.type:"void" -> {
83 %3 : java.type:"int" = var.load %1;
84 return %3;
85 }
86 ()java.type:"void" -> {
87 %4 : java.type:"int" = var.load %1;
88 return %4;
89 };
90 unreachable;
91 };
92 """)
93 static int test3(int i) {
94 if (true) {
95 return i;
96 } else {
97 return i;
98 }
99 }
100
101
102 @CodeReflection
103 @IR("""
104 func @"test4" ()java.type:"void" -> {
105 %0 : java.type:"java.util.function.IntUnaryOperator" = lambda @lambda.isQuotable=false (%1 : java.type:"int")java.type:"int" -> {
106 %2 : Var<java.type:"int"> = var %1 @"i";
107 java.if
108 ()java.type:"boolean" -> {
109 %3 : java.type:"boolean" = constant @true;
110 yield %3;
111 }
112 ()java.type:"void" -> {
113 %4 : java.type:"int" = var.load %2;
114 return %4;
115 }
116 ()java.type:"void" -> {
117 %5 : java.type:"int" = var.load %2;
118 return %5;
119 };
120 unreachable;
121 };
122 %6 : Var<java.type:"java.util.function.IntUnaryOperator"> = var %0 @"f";
123 return;
124 };
125 """)
126 static void test4() {
127 IntUnaryOperator f = (int i) -> {
128 if (true) {
129 return i;
130 } else {
131 return i;
132 }
133 };
134 }
135
136 @CodeReflection
137 @IR("""
138 func @"test5" (%0 : java.type:"int")java.type:"void" -> {
139 %1 : Var<java.type:"int"> = var %0 @"n";
140 %2 : java.type:"int" = var.load %1;
141 %3 : java.type:"java.lang.String" = java.switch.expression %2
142 (%4 : java.type:"int")java.type:"boolean" -> {
143 %5 : java.type:"int" = constant @42;
144 %6 : java.type:"boolean" = eq %4 %5;
145 yield %6;
146 }
147 ()java.type:"java.lang.String" -> {
148 java.while
149 ()java.type:"boolean" -> {
150 %7 : java.type:"boolean" = constant @true;
151 yield %7;
152 }
153 ()java.type:"void" -> {
154 java.continue;
155 };
156 unreachable;
157 }
158 ()java.type:"boolean" -> {
159 %8 : java.type:"boolean" = constant @true;
160 yield %8;
161 }
162 ()java.type:"java.lang.String" -> {
163 %9 : java.type:"java.lang.String" = constant @"";
164 yield %9;
165 };
166 %10 : Var<java.type:"java.lang.String"> = var %3 @"s";
167 return;
168 };
169 """)
170 static void test5(int n) {
171 String s = switch (n) {
172 case 42 -> { while (true); }
173 default -> "";
174 };
175 }
176
177 @IR("""
178 func @"f" ()java.type:"void" -> {
179 %0 : java.type:"java.util.function.IntUnaryOperator" = lambda @lambda.isQuotable=true (%1 : java.type:"int")java.type:"int" -> {
180 %2 : Var<java.type:"int"> = var %1 @"i";
181 java.if
182 ()java.type:"boolean" -> {
183 %3 : java.type:"boolean" = constant @true;
184 yield %3;
185 }
186 ()java.type:"void" -> {
187 %4 : java.type:"int" = var.load %2;
188 return %4;
189 }
190 ()java.type:"void" -> {
191 %5 : java.type:"int" = var.load %2;
192 return %5;
193 };
194 unreachable;
195 };
196 return;
197 };
198 """)
199 static final Quotable QUOTABLE_TEST = (IntUnaryOperator & Quotable) (int i) -> {
200 if (true) {
201 return i;
202 } else {
203 return i;
204 }
205 };
206
207 @IR("""
208 func @"f" ()java.type:"void" -> {
209 %0 : func<java.type:"int", java.type:"int"> = closure (%1 : java.type:"int")java.type:"int" -> {
210 %2 : Var<java.type:"int"> = var %1 @"i";
211 java.if
212 ()java.type:"boolean" -> {
213 %3 : java.type:"boolean" = constant @true;
214 yield %3;
215 }
216 ()java.type:"void" -> {
217 %4 : java.type:"int" = var.load %2;
218 return %4;
219 }
220 ()java.type:"void" -> {
221 %5 : java.type:"int" = var.load %2;
222 return %5;
223 };
224 unreachable;
225 };
226 return;
227 };
228 """)
229 static final Quoted QUOTED_TEST = (int i) -> {
230 if (true) {
231 return i;
232 } else {
233 return i;
234 }
235 };
236 }