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 /*
 25  * @test
 26  * @summary Smoke test for code reflection with constant values.
 27  * @modules jdk.incubator.code
 28  * @build ConstantsTest
 29  * @build CodeReflectionTester
 30  * @run main CodeReflectionTester ConstantsTest
 31  */
 32 
 33 import jdk.incubator.code.CodeReflection;
 34 import java.util.function.Function;
 35 
 36 public class ConstantsTest {
 37     @CodeReflection
 38     @IR("""
 39             func @"test1" (%0 : ConstantsTest)void -> {
 40                 %1 : java.lang.String = constant @"";
 41                 %2 : Var<java.lang.String> = var %1 @"s";
 42                 return;
 43             };
 44             """)
 45     void test1() {
 46         String s = "";
 47     }
 48 
 49     @CodeReflection
 50     @IR("""
 51             func @"test2" (%0 : ConstantsTest)void -> {
 52                 %1 : java.lang.String = constant @"Hello World";
 53                 %2 : Var<java.lang.String> = var %1 @"s";
 54                 return;
 55             };
 56             """)
 57     void test2() {
 58         String s = "Hello World";
 59     }
 60 
 61     @CodeReflection
 62     @IR("""
 63             func @"test3" (%0 : ConstantsTest)void -> {
 64                 %1 : java.lang.String = constant @null;
 65                 %2 : Var<java.lang.String> = var %1 @"s";
 66                 return;
 67             };
 68             """)
 69     void test3() {
 70         String s = null;
 71     }
 72 
 73     @IR("""
 74             func @"test4" (%0 : ConstantsTest)void -> {
 75                 %1 : java.lang.Class = constant @"java.util.function.Function";
 76                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
 77                 return;
 78             };
 79             """)
 80     @CodeReflection
 81     void test4() {
 82         Class<?> s = Function.class;
 83     }
 84 
 85     @IR("""
 86             func @"test5" (%0 : ConstantsTest)void -> {
 87                 %1 : int = constant @"42";
 88                 %2 : byte = conv %1;
 89                 %3 : Var<byte> = var %2 @"v";
 90                 %4 : int = constant @"-42";
 91                 %5 : byte = conv %4;
 92                 var.store %3 %5;
 93                 return;
 94             };
 95             """)
 96     @CodeReflection
 97     void test5() {
 98         byte v = 42;
 99         v = -42;
100     }
101 
102     @IR("""
103             func @"test6" (%0 : ConstantsTest)void -> {
104                 %1 : int = constant @"42";
105                 %2 : short = conv %1;
106                 %3 : Var<short> = var %2 @"v";
107                 %4 : int = constant @"-42";
108                 %5 : short = conv %4;
109                 var.store %3 %5;
110                 return;
111             };
112             """)
113     @CodeReflection
114     void test6() {
115         short v = 42;
116         v = -42;
117     }
118 
119     @IR("""
120             func @"test7" (%0 : ConstantsTest)void -> {
121                 %1 : int = constant @"42";
122                 %2 : Var<int> = var %1 @"v";
123                 %3 : int = constant @"-42";
124                 var.store %2 %3;
125                 return;
126             };
127             """)
128     @CodeReflection
129     void test7() {
130         int v = 42;
131         v = -42;
132     }
133 
134     @IR("""
135             func @"test8" (%0 : ConstantsTest)void -> {
136                 %1 : long = constant @"42";
137                 %2 : Var<long> = var %1 @"v";
138                 %3 : long = constant @"-42";
139                 var.store %2 %3;
140                 return;
141             };
142             """)
143     @CodeReflection
144     void test8() {
145         long v = 42L;
146         v = -42L;
147     }
148 
149     @IR("""
150             func @"test9" (%0 : ConstantsTest)void -> {
151                 %1 : float = constant @"42.0";
152                 %2 : Var<float> = var %1 @"v";
153                 %3 : float = constant @"42.0";
154                 %4 : float = neg %3;
155                 var.store %2 %4;
156                 return;
157             };
158             """)
159     @CodeReflection
160     void test9() {
161         float v = 42.0f;
162         v = -42.0f;
163     }
164 
165     @IR("""
166             func @"test10" (%0 : ConstantsTest)void -> {
167                 %1 : double = constant @"42.0";
168                 %2 : Var<double> = var %1 @"v";
169                 %3 : double = constant @"42.0";
170                 %4 : double = neg %3;
171                 var.store %2 %4;
172                 return;
173             };
174             """)
175     @CodeReflection
176     void test10() {
177         double v = 42.0;
178         v = -42.0;
179     }
180 
181     @IR("""
182             func @"test11" (%0 : ConstantsTest)void -> {
183                 %1 : char = constant @"a";
184                 %2 : Var<char> = var %1 @"v";
185                 return;
186             };
187             """)
188     @CodeReflection
189     void test11() {
190         char v = 'a';
191     }
192 
193     @IR("""
194             func @"test12" (%0 : ConstantsTest)void -> {
195                 %1 : boolean = constant @"true";
196                 %2 : Var<boolean> = var %1 @"b";
197                 %3 : boolean = constant @"false";
198                 var.store %2 %3;
199                 return;
200             };
201             """)
202     @CodeReflection
203     void test12() {
204         boolean b = true;
205         b = false;
206     }
207 
208     @IR("""
209             func @"test13" (%0 : ConstantsTest)void -> {
210                 %1 : java.lang.Class = constant @"float";
211                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
212                 return;
213             };
214             """)
215     @CodeReflection
216     void test13() {
217         Class<?> s = float.class;
218     }
219 
220     @IR("""
221             func @"test14" (%0 : ConstantsTest)void -> {
222                 %1 : java.lang.Class = constant @"java.lang.String[]";
223                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
224                 return;
225             };
226             """)
227     @CodeReflection
228     void test14() {
229         Class<?> s = String[].class;
230     }
231 
232     @IR("""
233             func @"test15" (%0 : ConstantsTest)void -> {
234                 %1 : java.lang.Class = constant @"java.lang.String[][]";
235                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
236                 return;
237             };
238             """)
239     @CodeReflection
240     void test15() {
241         Class<?> s = String[][].class;
242     }
243 
244     @IR("""
245             func @"test16" (%0 : ConstantsTest)void -> {
246                 %1 : java.lang.Class = constant @"java.lang.String[][][]";
247                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
248                 return;
249             };
250             """)
251     @CodeReflection
252     void test16() {
253         Class<?> s = String[][][].class;
254     }
255 
256     @IR("""
257             func @"test17" (%0 : ConstantsTest)void -> {
258                 %1 : java.lang.Class = constant @"boolean[]";
259                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
260                 return;
261             };
262             """)
263     @CodeReflection
264     void test17() {
265         Class<?> s = boolean[].class;
266     }
267 
268     @IR("""
269             func @"test18" (%0 : ConstantsTest)void -> {
270                 %1 : java.lang.Class = constant @"boolean[][][]";
271                 %2 : Var<java.lang.Class<+<java.lang.Object>>> = var %1 @"s";
272                 return;
273             };
274             """)
275     @CodeReflection
276     void test18() {
277         Class<?> s = boolean[][][].class;
278     }
279 }