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 
  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.TypeKlass;
  38 import jdk.test.lib.jittester.types.TypeLong;
  39 import jdk.test.lib.jittester.types.TypeShort;
  40 import jdk.test.lib.jittester.types.TypeVoid;
  41 
  42 public class TypeList {
  43     public static final TypeVoid VOID = new TypeVoid();
  44     public static final TypeBoolean BOOLEAN = new TypeBoolean();
  45     public static final TypeByte BYTE = new TypeByte();
  46     public static final TypeChar CHAR = new TypeChar();
  47     public static final TypeShort SHORT = new TypeShort();
  48     public static final TypeInt INT = new TypeInt();
  49     public static final TypeLong LONG = new TypeLong();
  50     public static final TypeFloat FLOAT = new TypeFloat();
  51     public static final TypeDouble DOUBLE = new TypeDouble();
  52     public static final TypeKlass OBJECT = new TypeKlass("java.lang.Object");
  53     public static final TypeKlass STRING = new TypeKlass("java.lang.String", TypeKlass.FINAL);
  54 
  55     private static final List<Type> TYPES = new ArrayList<>();
  56     private static final List<Type> BUILTIN_TYPES = new ArrayList<>();
  57     private static final List<Type> BUILTIN_INT_TYPES = new ArrayList<>();
  58     private static final List<Type> BUILTIN_FP_TYPES = new ArrayList<>();
  59     private static final List<Type> REFERENCE_TYPES = new ArrayList<>();
  60 
  61     static {
  62         BUILTIN_INT_TYPES.add(BOOLEAN);
  63         BUILTIN_INT_TYPES.add(BYTE);
  64         BUILTIN_INT_TYPES.add(CHAR);
  65         BUILTIN_INT_TYPES.add(SHORT);
  66         BUILTIN_INT_TYPES.add(INT);
  67         BUILTIN_INT_TYPES.add(LONG);
  68         BUILTIN_FP_TYPES.add(FLOAT);
  69         BUILTIN_FP_TYPES.add(DOUBLE);
  70 
  71         BUILTIN_TYPES.addAll(BUILTIN_INT_TYPES);
  72         BUILTIN_TYPES.addAll(BUILTIN_FP_TYPES);
  73 
  74         TYPES.addAll(BUILTIN_TYPES);
  75 
  76         if (!ProductionParams.disableArrays.value()) {
  77             TYPES.addAll(REFERENCE_TYPES);
  78         }
  79 
  80         STRING.addParent(OBJECT.getName());
  81         STRING.setParent(OBJECT);
  82         add(STRING);
  83         add(OBJECT);
  84     }
  85 
  86     public static Collection<Type> getAll() {
  87         return TYPES;
  88     }
  89 
  90     public static Collection<Type> getBuiltIn() {
  91         return BUILTIN_TYPES;
  92     }
  93 
  94     public static Collection<Type> getBuiltInInt() {
  95         return BUILTIN_INT_TYPES;
  96     }
  97 
  98     protected static Collection<Type> getBuiltInFP() {
  99         return BUILTIN_FP_TYPES;
 100     }
 101 
 102     protected static Collection<Type> getReferenceTypes() {
 103         return REFERENCE_TYPES;
 104     }
 105 
 106     protected static boolean isBuiltInFP(Type t) {
 107         return BUILTIN_FP_TYPES.contains(t);
 108     }
 109 
 110     public static boolean isBuiltInInt(Type t) {
 111         return BUILTIN_INT_TYPES.contains(t);
 112     }
 113 
 114     public static boolean isBuiltIn(Type t) {
 115         return isBuiltInInt(t) || isBuiltInFP(t) || t.equals(VOID);
 116     }
 117 
 118     protected static boolean isIn(Type t) {
 119         return TYPES.contains(t);
 120     }
 121 
 122     public static boolean isReferenceType(Type t) {
 123         return REFERENCE_TYPES.contains(t);
 124     }
 125 
 126     public static Type find(Type t) {
 127         int i = TYPES.indexOf(t);
 128         if (i != -1) {
 129             return TYPES.get(i);
 130         }
 131         return null;
 132     }
 133 
 134     protected static Type findReferenceType(Type t) {
 135         int i = REFERENCE_TYPES.indexOf(t);
 136         if (i != -1) {
 137             return REFERENCE_TYPES.get(i);
 138         }
 139         return null;
 140     }
 141 
 142     public static Type find(String name) {
 143         for (Type t : TYPES) {
 144             if (t.getName().equals(name)) {
 145                 return t;
 146             }
 147         }
 148         return null;
 149     }
 150 
 151     public static void add(Type t) {
 152         REFERENCE_TYPES.add(t);
 153         TYPES.add(t);
 154     }
 155 
 156     protected static void remove(Type t) {
 157         REFERENCE_TYPES.remove(t);
 158         TYPES.remove(t);
 159     }
 160 
 161     public static void removeAll() {
 162         Predicate<? super Type> isNotBasic = t -> t.getName().startsWith("Test_");
 163         TYPES.removeIf(isNotBasic);
 164         REFERENCE_TYPES.removeIf(isNotBasic);
 165     }
 166 }