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" ()void -> {
 44                 java.block ()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 : int)int -> {
 59                 %1 : Var<int> = var %0 @"i";
 60                 java.block ()void -> {
 61                     %2 : 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 : int)int -> {
 76                 %1 : Var<int> = var %0 @"i";
 77                 java.if
 78                     ()boolean -> {
 79                         %2 : boolean = constant @"true";
 80                         yield %2;
 81                     }
 82                     ()void -> {
 83                         %3 : int = var.load %1;
 84                         return %3;
 85                     }
 86                     ()void -> {
 87                         %4 : 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" ()void -> {
105                 %0 : java.util.function.IntUnaryOperator = lambda (%1 : int)int -> {
106                     %2 : Var<int> = var %1 @"i";
107                     java.if
108                         ()boolean -> {
109                             %3 : boolean = constant @"true";
110                             yield %3;
111                         }
112                         ()void -> {
113                             %4 : int = var.load %2;
114                             return %4;
115                         }
116                         ()void -> {
117                             %5 : int = var.load %2;
118                             return %5;
119                         };
120                     unreachable;
121                 };
122                 %6 : Var<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 : int)void -> {
139                 %1 : Var<int> = var %0 @"n";
140                 %2 : int = var.load %1;
141                 %3 : java.lang.String = java.switch.expression %2
142                     (%4 : int)boolean -> {
143                         %5 : int = constant @"42";
144                         %6 : boolean = eq %4 %5;
145                         yield %6;
146                     }
147                     ()java.lang.String -> {
148                         java.while
149                             ()boolean -> {
150                                 %7 : boolean = constant @"true";
151                                 yield %7;
152                             }
153                             ()void -> {
154                                 java.continue;
155                             };
156                         unreachable;
157                     }
158                     ()boolean -> {
159                         %8 : boolean = constant @"true";
160                         yield %8;
161                     }
162                     ()java.lang.String -> {
163                         %9 : java.lang.String = constant @"";
164                         yield %9;
165                     };
166                 %10 : Var<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" ()void -> {
179                 %1 : java.util.function.IntUnaryOperator = lambda (%2 : int)int -> {
180                     %3 : Var<int> = var %2 @"i";
181                     java.if
182                         ()boolean -> {
183                             %4 : boolean = constant @"true";
184                             yield %4;
185                         }
186                         ()void -> {
187                             %5 : int = var.load %3;
188                             return %5;
189                         }
190                         ()void -> {
191                             %6 : int = var.load %3;
192                             return %6;
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" ()void -> {
209                 %1 : func<int, int> = closure (%2 : int)int -> {
210                     %3 : Var<int> = var %2 @"i";
211                     java.if
212                         ()boolean -> {
213                             %4 : boolean = constant @"true";
214                             yield %4;
215                         }
216                         ()void -> {
217                             %5 : int = var.load %3;
218                             return %5;
219                         }
220                         ()void -> {
221                             %6 : int = var.load %3;
222                             return %6;
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 }