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  * @build NewArrayTest
 28  * @build CodeReflectionTester
 29  * @run main CodeReflectionTester NewArrayTest
 30  */
 31 
 32 import java.lang.runtime.CodeReflection;
 33 import java.util.function.Function;
 34 
 35 public class NewArrayTest {
 36 
 37     @CodeReflection
 38     @IR("""
 39             func @"test1" (%0 : NewArrayTest)void -> {
 40                 %1 : int = constant @"10";
 41                 %2 : int[] = new %1 @"func<int[], int>";
 42                 %3 : Var<int[]> = var %2 @"a";
 43                 return;
 44             };
 45             """)
 46     void test1() {
 47         int[] a = new int[10];
 48     }
 49 
 50     @CodeReflection
 51     @IR("""
 52             func @"test2" (%0 : NewArrayTest, %1 : int)void -> {
 53                 %2 : Var<int> = var %1 @"l";
 54                 %3 : int = var.load %2;
 55                 %4 : int = constant @"10";
 56                 %5 : int = add %3 %4;
 57                 %6 : int[] = new %5 @"func<int[], int>";
 58                 %7 : Var<int[]> = var %6 @"a";
 59                 return;
 60             };
 61             """)
 62     void test2(int l) {
 63         int[] a = new int[l + 10];
 64     }
 65 
 66     @CodeReflection
 67     @IR("""
 68             func @"test3" (%0 : NewArrayTest)void -> {
 69                 %1 : int = constant @"10";
 70                 %2 : java.lang.String[] = new %1 @"func<java.lang.String[], int>";
 71                 %3 : Var<java.lang.String[]> = var %2 @"a";
 72                 return;
 73             };
 74             """)
 75     void test3() {
 76         String[] a = new String[10];
 77     }
 78 
 79     @CodeReflection
 80     @IR("""
 81             func @"test4" (%0 : NewArrayTest)void -> {
 82                 %1 : int = constant @"10";
 83                 %2 : java.lang.String[][] = new %1 @"func<java.lang.String[][], int>";
 84                 %3 : Var<java.lang.String[][]> = var %2 @"a";
 85                 return;
 86             };
 87             """)
 88     void test4() {
 89         String[][] a = new String[10][];
 90     }
 91 
 92     @CodeReflection
 93     @IR("""
 94             func @"test5" (%0 : NewArrayTest)void -> {
 95                 %1 : int = constant @"10";
 96                 %2 : int = constant @"10";
 97                 %3 : java.lang.String[][] = new %1 %2 @"func<java.lang.String[][], int, int>";
 98                 %4 : Var<java.lang.String[][]> = var %3 @"a";
 99                 return;
100             };
101             """)
102     void test5() {
103         String[][] a = new String[10][10];
104     }
105 
106     @CodeReflection
107     @IR("""
108             func @"test6" (%0 : NewArrayTest)void -> {
109                 %1 : int = constant @"3";
110                 %2 : java.lang.String[][] = new %1 @"func<java.lang.String[][], int>";
111                 %3 : int = constant @"2";
112                 %4 : java.lang.String[] = new %3 @"func<java.lang.String[], int>";
113                 %5 : java.lang.String = constant @"one";
114                 %6 : int = constant @"0";
115                 array.store %4 %6 %5;
116                 %7 : java.lang.String = constant @"two";
117                 %8 : int = constant @"1";
118                 array.store %4 %8 %7;
119                 %9 : int = constant @"0";
120                 array.store %2 %9 %4;
121                 %10 : int = constant @"1";
122                 %11 : java.lang.String[] = new %10 @"func<java.lang.String[], int>";
123                 %12 : java.lang.String = constant @"three";
124                 %13 : int = constant @"0";
125                 array.store %11 %13 %12;
126                 %14 : int = constant @"1";
127                 array.store %2 %14 %11;
128                 %15 : java.lang.String[] = constant @null;
129                 %16 : int = constant @"2";
130                 array.store %2 %16 %15;
131                 %17 : Var<java.lang.String[][]> = var %2 @"a";
132                 return;
133             };
134             """)
135     void test6() {
136         String[][] a = { { "one", "two" }, { "three" }, null };
137     }
138 
139     @CodeReflection
140     @IR("""
141             func @"test7" (%0 : NewArrayTest)void -> {
142                 %1 : int = constant @"3";
143                 %2 : java.lang.String[][] = new %1 @"func<java.lang.String[][], int>";
144                 %3 : int = constant @"2";
145                 %4 : java.lang.String[] = new %3 @"func<java.lang.String[], int>";
146                 %5 : java.lang.String = constant @"one";
147                 %6 : int = constant @"0";
148                 array.store %4 %6 %5;
149                 %7 : java.lang.String = constant @"two";
150                 %8 : int = constant @"1";
151                 array.store %4 %8 %7;
152                 %9 : int = constant @"0";
153                 array.store %2 %9 %4;
154                 %10 : int = constant @"1";
155                 %11 : java.lang.String[] = new %10 @"func<java.lang.String[], int>";
156                 %12 : java.lang.String = constant @"three";
157                 %13 : int = constant @"0";
158                 array.store %11 %13 %12;
159                 %14 : int = constant @"1";
160                 array.store %2 %14 %11;
161                 %15 : java.lang.String[] = constant @null;
162                 %16 : int = constant @"2";
163                 array.store %2 %16 %15;
164                 %17 : Var<java.lang.String[][]> = var %2 @"a";
165                 return;
166             };
167             """)
168     void test7() {
169         String[][] a = new String[][] { { "one", "two" }, { "three" }, null };
170     }
171 }