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