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 quoted lambdas.
 27  * @build QuotedTest
 28  * @build CodeReflectionTester
 29  * @run main CodeReflectionTester QuotedTest
 30  */
 31 
 32 import java.lang.reflect.code.Quoted;
 33 import java.lang.runtime.CodeReflection;
 34 
 35 public class QuotedTest {
 36     @IR("""
 37             func @"f" ()void -> {
 38                  %0 : func<void> = closure ()void -> {
 39                     return;
 40                 };
 41                 return;
 42             };
 43             """)
 44     static final Quoted QUOTED_NO_PARAM_VOID = () -> {
 45     };
 46 
 47     @IR("""
 48             func @"f" ()void -> {
 49                 %0 : func<int> = closure ()int -> {
 50                     %2 : int = constant @"1";
 51                     return %2;
 52                 };
 53                 return;
 54             };
 55             """)
 56     static final Quoted QUOTED_NO_PARAM_CONST = () -> 1;
 57 
 58     @IR("""
 59             func @"f" ()void -> {
 60                 %0 : func<int, int> = closure (%2 : int)int -> {
 61                     %3 : Var<int> = var %2 @"x";
 62                     %4 : int = var.load %3;
 63                     return %4;
 64                 };
 65                 return;
 66             };
 67             """)
 68     static final Quoted QUOTED_ID = (int x) -> x;
 69 
 70     @IR("""
 71             func @"f" ()void -> {
 72                 %0 : func<int, int, int> = closure (%2 : int, %3 : int)int -> {
 73                     %4 : Var<int> = var %2 @"x";
 74                     %5 : Var<int> = var %3 @"y";
 75                     %6 : int = var.load %4;
 76                     %7 : int = var.load %5;
 77                     %8 : int = add %6 %7;
 78                     return %8;
 79                 };
 80                 return;
 81             };
 82             """)
 83     static final Quoted QUOTED_PLUS = (int x, int y) -> x + y;
 84 
 85     @IR("""
 86             func @"f" ()void -> {
 87                 %0 : func<java.lang.Object> = closure ()java.lang.Object -> {
 88                     %2 : java.lang.AssertionError = new @"func<java.lang.AssertionError>";
 89                     throw %2;
 90                 };
 91                 return;
 92             };
 93             """)
 94     static final Quoted QUOTED_THROW_NO_PARAM = () -> {
 95         throw new AssertionError();
 96     };
 97 
 98     // can we write out the root op then extract the closure ?
 99 
100     @IR("""
101             func @"f" (%1: Var<int>)void -> {
102                 %0 : func<int, int> = closure (%4 : int)int -> {
103                     %5 : Var<int> = var %4 @"y";
104                     %6 : int = var.load %1;
105                     %7 : int = var.load %5;
106                     %8 : int = add %6 %7;
107                     return %8;
108                 };
109                 return;
110             };
111             """)
112     static final Quoted QUOTED_CAPTURE_PARAM = new Object() {
113         Quoted captureContext(int x) {
114             return (int y) -> x + y;
115         }
116     }.captureContext(42);
117 
118     static class Context {
119         int x, y;
120 
121         Quoted capture() {
122             return (int z) -> x + y + z;
123         }
124     }
125 
126     @IR("""
127             func @"f" (%0 : QuotedTest$Context)void -> {
128                 %1 : func<int, int> = closure (%3 : int)int -> {
129                     %4 : Var<int> = var %3 @"z";
130                     %5 : int = field.load %0 @"QuotedTest$Context::x()int";
131                     %6 : int = field.load %0 @"QuotedTest$Context::y()int";
132                     %7 : int = add %5 %6;
133                     %8 : int = var.load %4;
134                     %9 : int = add %7 %8;
135                     return %9;
136                 };
137                 return;
138             };
139             """)
140     static final Quoted QUOTED_CAPTURE_FIELD = new Context().capture();
141 
142     @CodeReflection
143     @IR("""
144             func @"captureParam" (%0 : int)void -> {
145                 %1 : Var<int> = var %0 @"x";
146                 %2 : java.lang.reflect.code.Quoted = quoted ()void -> {
147                     %3 : func<int, int> = closure (%4 : int)int -> {
148                         %5 : Var<int> = var %4 @"y";
149                         %6 : int = var.load %1;
150                         %7 : int = var.load %5;
151                         %8 : int = add %6 %7;
152                         return %8;
153                     };
154                     yield %3;
155                 };
156                 %9 : Var<java.lang.reflect.code.Quoted> = var %2 @"op";
157                 return;
158             };
159             """)
160     static void captureParam(int x) {
161         Quoted op = (int y) -> x + y;
162     }
163 
164     int x, y;
165 
166     @CodeReflection
167     @IR("""
168             func @"captureField" (%0 : QuotedTest)void -> {
169                 %1 : java.lang.reflect.code.Quoted = quoted ()void -> {
170                     %2 : func<int, int> = closure (%3 : int)int -> {
171                         %4 : Var<int> = var %3 @"z";
172                         %5 : int = field.load %0 @"QuotedTest::x()int";
173                         %6 : int = field.load %0 @"QuotedTest::y()int";
174                         %7 : int = add %5 %6;
175                         %8 : int = var.load %4;
176                         %9 : int = add %7 %8;
177                         return %9;
178                     };
179                     yield %2;
180                 };
181                 %10 : Var<java.lang.reflect.code.Quoted> = var %1 @"op";
182                 return;
183             };
184             """)
185     void captureField() {
186         Quoted op = (int z) -> x + y + z;
187     }
188 }