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 java.lang.reflect.code.Quotable;
 25 import java.lang.reflect.code.Quoted;
 26 import java.lang.runtime.CodeReflection;
 27 import java.util.function.IntUnaryOperator;
 28 
 29 /*
 30  * @test
 31  * @summary Smoke test for code reflection with unreachable areas.
 32  * @enablePreview
 33  * @build UnreachableTest
 34  * @build CodeReflectionTester
 35  * @run main CodeReflectionTester UnreachableTest
 36  */
 37 
 38 public class UnreachableTest {
 39 
 40     @CodeReflection
 41     @IR("""
 42             func @"test1" ()void -> {
 43                 java.block ()void -> {
 44                     return;
 45                 };
 46                 unreachable;
 47             };
 48             """)
 49     static void test1() {
 50         {
 51             return;
 52         }
 53     }
 54 
 55     @CodeReflection
 56     @IR("""
 57             func @"test2" (%0 : int)int -> {
 58                 %1 : Var<int> = var %0 @"i";
 59                 java.block ()void -> {
 60                     %2 : int = var.load %1;
 61                     return %2;
 62                 };
 63                 unreachable;
 64             };
 65             """)
 66     static int test2(int i) {
 67         {
 68             return i;
 69         }
 70     }
 71 
 72     @CodeReflection
 73     @IR("""
 74             func @"test3" (%0 : int)int -> {
 75                 %1 : Var<int> = var %0 @"i";
 76                 java.if
 77                     ()boolean -> {
 78                         %2 : boolean = constant @"true";
 79                         yield %2;
 80                     }
 81                     ()void -> {
 82                         %3 : int = var.load %1;
 83                         return %3;
 84                     }
 85                     ()void -> {
 86                         %4 : int = var.load %1;
 87                         return %4;
 88                     };
 89                 unreachable;
 90             };
 91             """)
 92     static int test3(int i) {
 93         if (true) {
 94             return i;
 95         } else {
 96             return i;
 97         }
 98     }
 99 
100 
101     @CodeReflection
102     @IR("""
103             func @"test4" ()void -> {
104                 %0 : java.util.function.IntUnaryOperator = lambda (%1 : int)int -> {
105                     %2 : Var<int> = var %1 @"i";
106                     java.if
107                         ()boolean -> {
108                             %3 : boolean = constant @"true";
109                             yield %3;
110                         }
111                         ()void -> {
112                             %4 : int = var.load %2;
113                             return %4;
114                         }
115                         ()void -> {
116                             %5 : int = var.load %2;
117                             return %5;
118                         };
119                     unreachable;
120                 };
121                 %6 : Var<java.util.function.IntUnaryOperator> = var %0 @"f";
122                 return;
123             };
124             """)
125     static void test4() {
126         IntUnaryOperator f = (int i) -> {
127             if (true) {
128                 return i;
129             } else {
130                 return i;
131             }
132         };
133     }
134 
135     @IR("""
136             func @"f" ()void -> {
137                 %1 : java.util.function.IntUnaryOperator = lambda (%2 : int)int -> {
138                     %3 : Var<int> = var %2 @"i";
139                     java.if
140                         ()boolean -> {
141                             %4 : boolean = constant @"true";
142                             yield %4;
143                         }
144                         ()void -> {
145                             %5 : int = var.load %3;
146                             return %5;
147                         }
148                         ()void -> {
149                             %6 : int = var.load %3;
150                             return %6;
151                         };
152                     unreachable;
153                 };
154                 return;
155             };
156             """)
157     static final Quotable QUOTABLE_TEST = (IntUnaryOperator & Quotable) (int i) -> {
158         if (true) {
159             return i;
160         } else {
161             return i;
162         }
163     };
164 
165     @IR("""
166             func @"f" ()void -> {
167                 %1 : func<int, int> = closure (%2 : int)int -> {
168                     %3 : Var<int> = var %2 @"i";
169                     java.if
170                         ()boolean -> {
171                             %4 : boolean = constant @"true";
172                             yield %4;
173                         }
174                         ()void -> {
175                             %5 : int = var.load %3;
176                             return %5;
177                         }
178                         ()void -> {
179                             %6 : int = var.load %3;
180                             return %6;
181                         };
182                     unreachable;
183                 };
184                 return;
185             };
186             """)
187     static final Quoted QUOTED_TEST = (int i) -> {
188         if (true) {
189             return i;
190         } else {
191             return i;
192         }
193     };
194 }