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