1 /*
   2  * Copyright (c) 2005, 2015, 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 package jdk.test.lib.jittester;
  25 
  26 import java.util.ArrayList;
  27 import java.util.Collection;
  28 import java.util.List;
  29 import java.util.function.Predicate;
  30 import jdk.test.lib.jittester.types.TypeArray;
  31 import jdk.test.lib.jittester.types.TypeBoolean;
  32 import jdk.test.lib.jittester.types.TypeByte;
  33 import jdk.test.lib.jittester.types.TypeChar;
  34 import jdk.test.lib.jittester.types.TypeDouble;
  35 import jdk.test.lib.jittester.types.TypeFloat;
  36 import jdk.test.lib.jittester.types.TypeInt;
  37 import jdk.test.lib.jittester.types.TypeLong;
  38 import jdk.test.lib.jittester.types.TypeShort;
  39 import jdk.test.lib.jittester.types.TypeVoid;
  40 
  41 public class TypeList {
  42     private static final TypeVoid TYPE_VOID = new TypeVoid();
  43     private static final List<Type> TYPES = new ArrayList<>();
  44     private static final List<Type> BUILTIN_TYPES = new ArrayList<>();
  45     private static final List<Type> BUILTIN_INT_TYPES = new ArrayList<>();
  46     private static final List<Type> BUILTIN_FP_TYPES = new ArrayList<>();
  47     private static final List<Type> REFERENCE_TYPES = new ArrayList<>();
  48 
  49     static {
  50         BUILTIN_INT_TYPES.add(new TypeBoolean());
  51         BUILTIN_INT_TYPES.add(new TypeByte());
  52         BUILTIN_INT_TYPES.add(new TypeChar());
  53         BUILTIN_INT_TYPES.add(new TypeShort());
  54         BUILTIN_INT_TYPES.add(new TypeInt());
  55         BUILTIN_INT_TYPES.add(new TypeLong());
  56         BUILTIN_FP_TYPES.add(new TypeFloat());
  57         BUILTIN_FP_TYPES.add(new TypeDouble());
  58 
  59         BUILTIN_TYPES.addAll(BUILTIN_INT_TYPES);
  60         BUILTIN_TYPES.addAll(BUILTIN_FP_TYPES);
  61 
  62         TYPES.addAll(BUILTIN_TYPES);
  63 
  64         if (!ProductionParams.disableArrays.value()) {
  65             REFERENCE_TYPES.add(new TypeArray().produce());
  66             TYPES.addAll(REFERENCE_TYPES);
  67         }
  68     }
  69 
  70     public static TypeVoid getVoid() {
  71         return TYPE_VOID;
  72     }
  73 
  74     public static Collection<Type> getAll() {
  75         return TYPES;
  76     }
  77 
  78     public static Collection<Type> getBuiltIn() {
  79         return BUILTIN_TYPES;
  80     }
  81 
  82     public static Collection<Type> getBuiltInInt() {
  83         return BUILTIN_INT_TYPES;
  84     }
  85 
  86     protected static Collection<Type> getBuiltInFP() {
  87         return BUILTIN_FP_TYPES;
  88     }
  89 
  90     protected static Collection<Type> getReferenceTypes() {
  91         return REFERENCE_TYPES;
  92     }
  93 
  94     protected static boolean isBuiltInFP(Type t) {
  95         return BUILTIN_FP_TYPES.contains(t);
  96     }
  97 
  98     public static boolean isBuiltInInt(Type t) {
  99         return BUILTIN_INT_TYPES.contains(t);
 100     }
 101 
 102     public static boolean isBuiltIn(Type t) {
 103         return isBuiltInInt(t) || isBuiltInFP(t);
 104     }
 105 
 106     protected static boolean isIn(Type t) {
 107         return TYPES.contains(t);
 108     }
 109 
 110     protected static boolean isReferenceType(Type t) {
 111         return REFERENCE_TYPES.contains(t);
 112     }
 113 
 114     public static Type find(Type t) {
 115         int i = TYPES.indexOf(t);
 116         if (i != -1) {
 117             return TYPES.get(i);
 118         }
 119         return null;
 120     }
 121 
 122     protected static Type findReferenceType(Type t) {
 123         int i = REFERENCE_TYPES.indexOf(t);
 124         if (i != -1) {
 125             return REFERENCE_TYPES.get(i);
 126         }
 127         return null;
 128     }
 129 
 130     public static Type find(String name) {
 131         for (Type t : TYPES) {
 132             if (t.getName().equals(name)) {
 133                 return t;
 134             }
 135         }
 136         return null;
 137     }
 138 
 139     public static void add(Type t) {
 140         REFERENCE_TYPES.add(t);
 141         TYPES.add(t);
 142     }
 143 
 144     protected static void remove(Type t) {
 145         REFERENCE_TYPES.remove(t);
 146         TYPES.remove(t);
 147     }
 148 
 149     public static void removeAll() {
 150         Predicate<? super Type> isNotBasic = t -> t.getName().startsWith("Test_");
 151         TYPES.removeIf(isNotBasic);
 152         REFERENCE_TYPES.removeIf(isNotBasic);
 153     }
 154 }