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 jdk.test.lib.jittester.types.TypeBoolean;
27 import jdk.test.lib.jittester.types.TypeByte;
28 import jdk.test.lib.jittester.types.TypeChar;
29 import jdk.test.lib.jittester.types.TypeDouble;
30 import jdk.test.lib.jittester.types.TypeFloat;
31 import jdk.test.lib.jittester.types.TypeInt;
32 import jdk.test.lib.jittester.types.TypeKlass;
33 import jdk.test.lib.jittester.types.TypeLong;
34 import jdk.test.lib.jittester.types.TypeShort;
35 import jdk.test.lib.jittester.types.TypeVoid;
36 
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.function.Predicate;
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<TypeKlass> 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 
OBJECT.getName()80         STRING.addParent(OBJECT.getName());
81         STRING.setParent(OBJECT);
82         add(STRING);
83         add(OBJECT);
84     }
85 
getAll()86     public static Collection<Type> getAll() {
87         return TYPES;
88     }
89 
getBuiltIn()90     public static Collection<Type> getBuiltIn() {
91         return BUILTIN_TYPES;
92     }
93 
getBuiltInInt()94     public static Collection<Type> getBuiltInInt() {
95         return BUILTIN_INT_TYPES;
96     }
97 
getBuiltInFP()98     protected static Collection<Type> getBuiltInFP() {
99         return BUILTIN_FP_TYPES;
100     }
101 
getReferenceTypes()102     protected static Collection<TypeKlass> getReferenceTypes() {
103         return REFERENCE_TYPES;
104     }
105 
isBuiltInFP(Type t)106     protected static boolean isBuiltInFP(Type t) {
107         return BUILTIN_FP_TYPES.contains(t);
108     }
109 
isBuiltInInt(Type t)110     public static boolean isBuiltInInt(Type t) {
111         return BUILTIN_INT_TYPES.contains(t);
112     }
113 
isBuiltIn(Type t)114     public static boolean isBuiltIn(Type t) {
115         return isBuiltInInt(t) || isBuiltInFP(t) || t.equals(VOID);
116     }
117 
isIn(Type t)118     protected static boolean isIn(Type t) {
119         return TYPES.contains(t);
120     }
121 
isReferenceType(Type t)122     public static boolean isReferenceType(Type t) {
123         return REFERENCE_TYPES.contains(t);
124     }
125 
find(Type t)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 
findReferenceType(Type t)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 
find(String name)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 
add(TypeKlass t)151     public static void add(TypeKlass t) {
152         REFERENCE_TYPES.add(t);
153         TYPES.add(t);
154     }
155 
remove(Type t)156     protected static void remove(Type t) {
157         REFERENCE_TYPES.remove(t);
158         TYPES.remove(t);
159     }
160 
removeAll()161     public static void removeAll() {
162         Predicate<? super String> isNotBasic = s -> s.startsWith("Test_");
163         Predicate<? super Type> isNotBasicType = t -> isNotBasic.test(t.getName());
164         REFERENCE_TYPES.stream()
165                        .map(TypeKlass::getChildrenNames)
166                        .forEach(l -> l.removeIf(isNotBasic));
167         TYPES.removeIf(isNotBasicType);
168         REFERENCE_TYPES.removeIf(isNotBasicType);
169     }
170 }
171