test/tools/javac/types/TypeHarness.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import com.sun.tools.javac.code.BoundKind;
aoqi@0 25 import com.sun.tools.javac.code.Flags;
aoqi@0 26 import com.sun.tools.javac.util.Context;
aoqi@0 27 import com.sun.tools.javac.code.Types;
aoqi@0 28 import com.sun.tools.javac.code.Symtab;
aoqi@0 29 import com.sun.tools.javac.code.Type;
aoqi@0 30 import com.sun.tools.javac.code.Type.*;
aoqi@0 31 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 32 import com.sun.tools.javac.comp.Check;
aoqi@0 33 import com.sun.tools.javac.util.List;
aoqi@0 34 import com.sun.tools.javac.util.ListBuffer;
aoqi@0 35 import com.sun.tools.javac.util.Name;
aoqi@0 36 import com.sun.tools.javac.util.Names;
aoqi@0 37 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Test harness whose goal is to simplify the task of writing type-system
aoqi@0 41 * regression test. It provides functionalities to build custom types as well
aoqi@0 42 * as to access the underlying javac's symbol table in order to retrieve
aoqi@0 43 * predefined types. Among the features supported by the harness are: type
aoqi@0 44 * substitution, type containment, subtyping, cast-conversion, assigment
aoqi@0 45 * conversion.
aoqi@0 46 *
aoqi@0 47 * This class is meant to be a common super class for all concrete type test
aoqi@0 48 * classes. A subclass can access the type-factory and the test methods so as
aoqi@0 49 * to write compact tests. An example is reported below:
aoqi@0 50 *
aoqi@0 51 * <pre>
aoqi@0 52 * Type X = fac.TypeVariable();
aoqi@0 53 * Type Y = fac.TypeVariable();
aoqi@0 54 * Type A_X_Y = fac.Class(0, X, Y);
aoqi@0 55 * Type A_Obj_Obj = fac.Class(0,
aoqi@0 56 * predef.objectType,
aoqi@0 57 * predef.objectType);
aoqi@0 58 * checkSameType(A_Obj_Obj, subst(A_X_Y,
aoqi@0 59 * Mapping(X, predef.objectType),
aoqi@0 60 * Mapping(Y, predef.objectType)));
aoqi@0 61 * </pre>
aoqi@0 62 *
aoqi@0 63 * The above code is used to create two class types, namely {@code A<X,Y>} and
aoqi@0 64 * {@code A<Object,Object>} where both {@code X} and {@code Y} are type-variables.
aoqi@0 65 * The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A<Object,Object>}.
aoqi@0 66 *
aoqi@0 67 * @author mcimadamore
aoqi@0 68 */
aoqi@0 69 public class TypeHarness {
aoqi@0 70
aoqi@0 71 protected Types types;
aoqi@0 72 protected Check chk;
aoqi@0 73 protected Symtab predef;
aoqi@0 74 protected Names names;
aoqi@0 75 protected Factory fac;
aoqi@0 76
aoqi@0 77 protected TypeHarness() {
aoqi@0 78 Context ctx = new Context();
aoqi@0 79 JavacFileManager.preRegister(ctx);
aoqi@0 80 types = Types.instance(ctx);
aoqi@0 81 chk = Check.instance(ctx);
aoqi@0 82 predef = Symtab.instance(ctx);
aoqi@0 83 names = Names.instance(ctx);
aoqi@0 84 fac = new Factory();
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 // <editor-fold defaultstate="collapsed" desc="type assertions">
aoqi@0 88
aoqi@0 89 /** assert that 's' is a subtype of 't' */
aoqi@0 90 public void assertSubtype(Type s, Type t) {
aoqi@0 91 assertSubtype(s, t, true);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 /** assert that 's' is/is not a subtype of 't' */
aoqi@0 95 public void assertSubtype(Type s, Type t, boolean expected) {
aoqi@0 96 if (types.isSubtype(s, t) != expected) {
aoqi@0 97 String msg = expected ?
aoqi@0 98 " is not a subtype of " :
aoqi@0 99 " is a subtype of ";
aoqi@0 100 error(s + msg + t);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 /** assert that 's' is the same type as 't' */
aoqi@0 105 public void assertSameType(Type s, Type t) {
aoqi@0 106 assertSameType(s, t, true);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 /** assert that 's' is/is not the same type as 't' */
aoqi@0 110 public void assertSameType(Type s, Type t, boolean expected) {
aoqi@0 111 if (types.isSameType(s, t) != expected) {
aoqi@0 112 String msg = expected ?
aoqi@0 113 " is not the same type as " :
aoqi@0 114 " is the same type as ";
aoqi@0 115 error(s + msg + t);
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 /** assert that 's' is castable to 't' */
aoqi@0 120 public void assertCastable(Type s, Type t) {
aoqi@0 121 assertCastable(s, t, true);
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 /** assert that 's' is/is not castable to 't' */
aoqi@0 125 public void assertCastable(Type s, Type t, boolean expected) {
aoqi@0 126 if (types.isCastable(s, t) != expected) {
aoqi@0 127 String msg = expected ?
aoqi@0 128 " is not castable to " :
aoqi@0 129 " is castable to ";
aoqi@0 130 error(s + msg + t);
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 /** assert that 's' is convertible (method invocation conversion) to 't' */
aoqi@0 135 public void assertConvertible(Type s, Type t) {
aoqi@0 136 assertCastable(s, t, true);
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 /** assert that 's' is/is not convertible (method invocation conversion) to 't' */
aoqi@0 140 public void assertConvertible(Type s, Type t, boolean expected) {
aoqi@0 141 if (types.isConvertible(s, t) != expected) {
aoqi@0 142 String msg = expected ?
aoqi@0 143 " is not convertible to " :
aoqi@0 144 " is convertible to ";
aoqi@0 145 error(s + msg + t);
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 /** assert that 's' is assignable to 't' */
aoqi@0 150 public void assertAssignable(Type s, Type t) {
aoqi@0 151 assertCastable(s, t, true);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 /** assert that 's' is/is not assignable to 't' */
aoqi@0 155 public void assertAssignable(Type s, Type t, boolean expected) {
aoqi@0 156 if (types.isAssignable(s, t) != expected) {
aoqi@0 157 String msg = expected ?
aoqi@0 158 " is not assignable to " :
aoqi@0 159 " is assignable to ";
aoqi@0 160 error(s + msg + t);
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /** assert that generic type 't' is well-formed */
aoqi@0 165 public void assertValidGenericType(Type t) {
aoqi@0 166 assertValidGenericType(t, true);
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 /** assert that 's' is/is not assignable to 't' */
aoqi@0 170 public void assertValidGenericType(Type t, boolean expected) {
aoqi@0 171 if (chk.checkValidGenericType(t) != expected) {
aoqi@0 172 String msg = expected ?
aoqi@0 173 " is not a valid generic type" :
aoqi@0 174 " is a valid generic type";
aoqi@0 175 error(t + msg + " " + t.tsym.type);
aoqi@0 176 }
aoqi@0 177 }
aoqi@0 178 // </editor-fold>
aoqi@0 179
aoqi@0 180 private void error(String msg) {
aoqi@0 181 throw new AssertionError("Unexpected result: " + msg);
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 // <editor-fold defaultstate="collapsed" desc="type functions">
aoqi@0 185
aoqi@0 186 /** compute the erasure of a type 't' */
aoqi@0 187 public Type erasure(Type t) {
aoqi@0 188 return types.erasure(t);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 /** compute the capture of a type 't' */
aoqi@0 192 public Type capture(Type t) {
aoqi@0 193 return types.capture(t);
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 /** compute the boxed type associated with 't' */
aoqi@0 197 public Type box(Type t) {
aoqi@0 198 if (!t.isPrimitive()) {
aoqi@0 199 throw new AssertionError("Cannot box non-primitive type: " + t);
aoqi@0 200 }
aoqi@0 201 return types.boxedClass(t).type;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 /** compute the unboxed type associated with 't' */
aoqi@0 205 public Type unbox(Type t) {
aoqi@0 206 Type u = types.unboxedType(t);
aoqi@0 207 if (t == null) {
aoqi@0 208 throw new AssertionError("Cannot unbox reference type: " + t);
aoqi@0 209 } else {
aoqi@0 210 return u;
aoqi@0 211 }
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 /** compute a type substitution on 't' given a list of type mappings */
aoqi@0 215 public Type subst(Type t, Mapping... maps) {
aoqi@0 216 ListBuffer<Type> from = new ListBuffer<>();
aoqi@0 217 ListBuffer<Type> to = new ListBuffer<>();
aoqi@0 218 for (Mapping tm : maps) {
aoqi@0 219 from.append(tm.from);
aoqi@0 220 to.append(tm.to);
aoqi@0 221 }
aoqi@0 222 return types.subst(t, from.toList(), to.toList());
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 /** create a fresh type mapping from a type to another */
aoqi@0 226 public Mapping Mapping(Type from, Type to) {
aoqi@0 227 return new Mapping(from, to);
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 public static class Mapping {
aoqi@0 231 Type from;
aoqi@0 232 Type to;
aoqi@0 233 private Mapping(Type from, Type to) {
aoqi@0 234 this.from = from;
aoqi@0 235 this.to = to;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 // </editor-fold>
aoqi@0 239
aoqi@0 240 // <editor-fold defaultstate="collapsed" desc="type factory">
aoqi@0 241
aoqi@0 242 /**
aoqi@0 243 * This class is used to create Java types in a simple way. All main
aoqi@0 244 * kinds of type are supported: primitive, reference, non-denotable. The
aoqi@0 245 * factory also supports creation of constant types (used by the compiler
aoqi@0 246 * to represent the type of a literal).
aoqi@0 247 */
aoqi@0 248 public class Factory {
aoqi@0 249
aoqi@0 250 private int synthNameCount = 0;
aoqi@0 251
aoqi@0 252 private Name syntheticName() {
aoqi@0 253 return names.fromString("A$" + synthNameCount++);
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public ClassType Class(long flags, Type... typeArgs) {
aoqi@0 257 ClassSymbol csym = new ClassSymbol(flags, syntheticName(), predef.noSymbol);
aoqi@0 258 csym.type = new ClassType(Type.noType, List.from(typeArgs), csym);
aoqi@0 259 ((ClassType)csym.type).supertype_field = predef.objectType;
aoqi@0 260 return (ClassType)csym.type;
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 public ClassType Class(Type... typeArgs) {
aoqi@0 264 return Class(0, typeArgs);
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 public ClassType Interface(Type... typeArgs) {
aoqi@0 268 return Class(Flags.INTERFACE, typeArgs);
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 public ClassType Interface(long flags, Type... typeArgs) {
aoqi@0 272 return Class(Flags.INTERFACE | flags, typeArgs);
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 public Type Constant(byte b) {
aoqi@0 276 return predef.byteType.constType(b);
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 public Type Constant(short s) {
aoqi@0 280 return predef.shortType.constType(s);
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 public Type Constant(int i) {
aoqi@0 284 return predef.intType.constType(i);
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 public Type Constant(long l) {
aoqi@0 288 return predef.longType.constType(l);
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 public Type Constant(float f) {
aoqi@0 292 return predef.floatType.constType(f);
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 public Type Constant(double d) {
aoqi@0 296 return predef.doubleType.constType(d);
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 public Type Constant(char c) {
aoqi@0 300 return predef.charType.constType(c + 0);
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 public ArrayType Array(Type elemType) {
aoqi@0 304 return new ArrayType(elemType, predef.arrayClass);
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 public TypeVar TypeVariable() {
aoqi@0 308 return TypeVariable(predef.objectType);
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 public TypeVar TypeVariable(Type bound) {
aoqi@0 312 TypeSymbol tvsym = new TypeVariableSymbol(0, syntheticName(), null, predef.noSymbol);
aoqi@0 313 tvsym.type = new TypeVar(tvsym, bound, null);
aoqi@0 314 return (TypeVar)tvsym.type;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 public WildcardType Wildcard(BoundKind bk, Type bound) {
aoqi@0 318 return new WildcardType(bound, bk, predef.boundClass);
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 public CapturedType CapturedVariable(Type upper, Type lower) {
aoqi@0 322 return new CapturedType(syntheticName(), predef.noSymbol, upper, lower, null);
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 public ClassType Intersection(Type classBound, Type... intfBounds) {
aoqi@0 326 ClassType ct = Class(Flags.COMPOUND);
aoqi@0 327 ct.supertype_field = classBound;
aoqi@0 328 ct.interfaces_field = List.from(intfBounds);
aoqi@0 329 return ct;
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332 // </editor-fold>
aoqi@0 333 }

mercurial