1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.types; 18 19 import com.google.common.base.Function; 20 import com.google.common.collect.ImmutableList; 21 import com.google.turbine.binder.bound.SourceTypeBoundClass; 22 import com.google.turbine.binder.bound.TypeBoundClass.TyVarInfo; 23 import com.google.turbine.binder.sym.TyVarSymbol; 24 import com.google.turbine.type.Type; 25 import com.google.turbine.type.Type.ArrayTy; 26 import com.google.turbine.type.Type.ClassTy; 27 import com.google.turbine.type.Type.ClassTy.SimpleClassTy; 28 import com.google.turbine.type.Type.IntersectionTy; 29 import com.google.turbine.type.Type.TyVar; 30 31 /** Generic type erasure. */ 32 public class Erasure { erase(Type ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv)33 public static Type erase(Type ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) { 34 switch (ty.tyKind()) { 35 case PRIM_TY: 36 case VOID_TY: 37 return ty; 38 case CLASS_TY: 39 return eraseClassTy((Type.ClassTy) ty); 40 case ARRAY_TY: 41 return eraseArrayTy((Type.ArrayTy) ty, tenv); 42 case TY_VAR: 43 return eraseTyVar((TyVar) ty, tenv); 44 case INTERSECTION_TY: 45 return eraseIntersectionTy((Type.IntersectionTy) ty, tenv); 46 default: 47 throw new AssertionError(ty.tyKind()); 48 } 49 } 50 eraseIntersectionTy( IntersectionTy ty, Function<TyVarSymbol, TyVarInfo> tenv)51 private static Type eraseIntersectionTy( 52 IntersectionTy ty, Function<TyVarSymbol, TyVarInfo> tenv) { 53 return erase(ty.bounds().get(0), tenv); 54 } 55 eraseTyVar( TyVar ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv)56 private static Type eraseTyVar( 57 TyVar ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) { 58 SourceTypeBoundClass.TyVarInfo info = tenv.apply(ty.sym()); 59 return erase(info.bound(), tenv); 60 } 61 eraseArrayTy( Type.ArrayTy ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv)62 private static Type.ArrayTy eraseArrayTy( 63 Type.ArrayTy ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) { 64 return ArrayTy.create(erase(ty.elementType(), tenv), ty.annos()); 65 } 66 eraseClassTy(Type.ClassTy ty)67 public static Type.ClassTy eraseClassTy(Type.ClassTy ty) { 68 ImmutableList.Builder<Type.ClassTy.SimpleClassTy> classes = ImmutableList.builder(); 69 for (Type.ClassTy.SimpleClassTy c : ty.classes()) { 70 if (c.targs().isEmpty()) { 71 classes.add(c); 72 } else { 73 classes.add(SimpleClassTy.create(c.sym(), ImmutableList.of(), c.annos())); 74 } 75 } 76 return ClassTy.create(classes.build()); 77 } 78 } 79