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 
 26 /*
 27  * @test
 28  * @summary Smoke test for code reflection with conditional and/or expressions.
 29  * @modules jdk.incubator.code
 30  * @build ConditionalAndOrTest
 31  * @build CodeReflectionTester
 32  * @run main CodeReflectionTester ConditionalAndOrTest
 33  */
 34 
 35 public class ConditionalAndOrTest {
 36 
 37     @CodeReflection
 38     @IR("""
 39             func @"test1" (%0 : ConditionalAndOrTest, %1 : int)void -> {
 40                 %2 : Var<int> = var %1 @"i";
 41                 %3 : boolean = java.cand
 42                     ()boolean -> {
 43                         %4 : int = var.load %2;
 44                         %5 : int = constant @"1";
 45                         %6 : boolean = gt %4 %5;
 46                         yield %6;
 47                     }
 48                     ()boolean -> {
 49                         %7 : int = var.load %2;
 50                         %8 : int = constant @"10";
 51                         %9 : boolean = lt %7 %8;
 52                         yield %9;
 53                     };
 54                 %10 : Var<boolean> = var %3 @"b";
 55                 return;
 56             };
 57             """)
 58     void test1(int i) {
 59         boolean b = i > 1 && i < 10;
 60     }
 61 
 62     @CodeReflection
 63     @IR("""
 64             func @"test2" (%0 : ConditionalAndOrTest, %1 : int)void -> {
 65                 %2 : Var<int> = var %1 @"i";
 66                 %3 : boolean = java.cor
 67                     ()boolean -> {
 68                         %4 : int = var.load %2;
 69                         %5 : int = constant @"1";
 70                         %6 : boolean = gt %4 %5;
 71                         yield %6;
 72                     }
 73                     ()boolean -> {
 74                         %7 : int = var.load %2;
 75                         %8 : int = constant @"10";
 76                         %9 : boolean = lt %7 %8;
 77                         yield %9;
 78                     };
 79                 %10 : Var<boolean> = var %3 @"b";
 80                 return;
 81             };
 82             """)
 83     void test2(int i) {
 84         boolean b = i > 1 || i < 10;
 85     }
 86 
 87     @CodeReflection
 88     @IR("""
 89             func @"test3" (%0 : ConditionalAndOrTest, %1 : int)void -> {
 90                 %2 : Var<int> = var %1 @"i";
 91                 %3 : boolean = java.cor
 92                     ()boolean -> {
 93                         %4 : boolean = java.cand
 94                             ()boolean -> {
 95                                 %5 : int = var.load %2;
 96                                 %6 : int = constant @"1";
 97                                 %7 : boolean = gt %5 %6;
 98                                 yield %7;
 99                             }
100                             ()boolean -> {
101                                 %8 : int = var.load %2;
102                                 %9 : int = constant @"10";
103                                 %10 : boolean = lt %8 %9;
104                                 yield %10;
105                             };
106                         yield %4;
107                     }
108                     ()boolean -> {
109                         %11 : int = var.load %2;
110                         %12 : int = constant @"100";
111                         %13 : boolean = eq %11 %12;
112                         yield %13;
113                     };
114                 %14 : Var<boolean> = var %3 @"b";
115                 return;
116             };
117             """)
118     void test3(int i) {
119         boolean b = i > 1 && i < 10 || i == 100;
120     }
121 }