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 import jdk.incubator.code.CodeReflection; 25 import java.util.List; 26 import java.util.function.Supplier; 27 28 /* 29 * @test 30 * @summary Smoke test for code reflection with conditional expressions. 31 * @modules jdk.incubator.code 32 * @build ConditionalExpressionTest 33 * @build CodeReflectionTester 34 * @run main CodeReflectionTester ConditionalExpressionTest 35 */ 36 37 public class ConditionalExpressionTest { 38 39 @IR(""" 40 func @"test1" (%0 : ConditionalExpressionTest, %1 : boolean, %2 : int, %3 : int)void -> { 41 %4 : Var<boolean> = var %1 @"b"; 42 %5 : Var<int> = var %2 @"x"; 43 %6 : Var<int> = var %3 @"y"; 44 %7 : int = java.cexpression 45 ^cond()boolean -> { 46 %8 : boolean = var.load %4; 47 yield %8; 48 } 49 ^truepart()int -> { 50 %9 : int = var.load %5; 51 yield %9; 52 } 53 ^falsepart()int -> { 54 %10 : int = var.load %6; 55 yield %10; 56 }; 57 %11 : Var<int> = var %7 @"z"; 58 return; 59 }; 60 """) 61 @CodeReflection 62 void test1(boolean b, int x, int y) { 63 int z = b ? x : y; 64 } 65 66 @IR(""" 67 func @"test2" (%0 : ConditionalExpressionTest, %1 : boolean, %2 : int, %3 : double)void -> { 68 %4 : Var<boolean> = var %1 @"b"; 69 %5 : Var<int> = var %2 @"x"; 70 %6 : Var<double> = var %3 @"y"; 71 %7 : double = java.cexpression 72 ^cond()boolean -> { 73 %8 : boolean = var.load %4; 74 %9 : boolean = not %8; 75 yield %9; 76 } 77 ^truepart()double -> { 78 %10 : int = var.load %5; 79 %11 : double = conv %10; 80 yield %11; 81 } 82 ^falsepart()double -> { 83 %12 : double = var.load %6; 84 yield %12; 85 }; 86 %13 : Var<double> = var %7 @"z"; 87 return; 88 }; 89 """) 90 @CodeReflection 91 void test2(boolean b, int x, double y) { 92 double z = !b ? x : y; 93 } 94 95 @IR(""" 96 func @"test3" (%0 : ConditionalExpressionTest, %1 : boolean, %2 : int, %3 : double)void -> { 97 %4 : Var<boolean> = var %1 @"b"; 98 %5 : Var<int> = var %2 @"x"; 99 %6 : Var<double> = var %3 @"y"; 100 %7 : java.util.function.Supplier<java.lang.Double> = java.cexpression 101 ^cond()boolean -> { 102 %8 : boolean = var.load %4; 103 yield %8; 104 } 105 ^truepart()java.util.function.Supplier<java.lang.Double> -> { 106 %9 : java.util.function.Supplier<java.lang.Double> = lambda ()java.lang.Double -> { 107 %10 : int = var.load %5; 108 %11 : double = conv %10; 109 %12 : java.lang.Double = invoke %11 @"java.lang.Double::valueOf(double)java.lang.Double"; 110 return %12; 111 }; 112 yield %9; 113 } 114 ^falsepart()java.util.function.Supplier<java.lang.Double> -> { 115 %13 : java.util.function.Supplier<java.lang.Double> = lambda ()java.lang.Double -> { 116 %14 : double = var.load %6; 117 %15 : java.lang.Double = invoke %14 @"java.lang.Double::valueOf(double)java.lang.Double"; 118 return %15; 119 }; 120 yield %13; 121 }; 122 %16 : Var<java.util.function.Supplier<java.lang.Double>> = var %7 @"z"; 123 return; 124 }; 125 """) 126 @CodeReflection 127 void test3(boolean b, int x, double y) { 128 Supplier<Double> z = b ? () -> (double) x : () -> y; 129 } 130 131 @IR(""" 132 func @"test4" (%0 : ConditionalExpressionTest, %1 : boolean, %2 : boolean, %3 : int, %4 : double, %5 : double)void -> { 133 %6 : Var<boolean> = var %1 @"b1"; 134 %7 : Var<boolean> = var %2 @"b2"; 135 %8 : Var<int> = var %3 @"x"; 136 %9 : Var<double> = var %4 @"y"; 137 %10 : Var<double> = var %5 @"z"; 138 %11 : double = java.cexpression 139 ^cond()boolean -> { 140 %12 : boolean = var.load %6; 141 yield %12; 142 } 143 ^truepart()double -> { 144 %13 : double = java.cexpression 145 ^cond()boolean -> { 146 %14 : boolean = var.load %7; 147 yield %14; 148 } 149 ^truepart()double -> { 150 %15 : int = var.load %8; 151 %16 : double = conv %15; 152 yield %16; 153 } 154 ^falsepart()double -> { 155 %17 : double = var.load %9; 156 yield %17; 157 }; 158 yield %13; 159 } 160 ^falsepart()double -> { 161 %18 : double = var.load %10; 162 yield %18; 163 }; 164 %19 : Var<double> = var %11 @"r"; 165 return; 166 }; 167 """) 168 @CodeReflection 169 void test4(boolean b1, boolean b2, int x, double y, double z) { 170 double r = b1 ? (b2 ? x : y) : z; 171 } 172 173 }