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