Home | History | Annotate | Download | only in types
      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.sym.TyVarSymbol;
     23 import com.google.turbine.type.Type;
     24 import com.google.turbine.type.Type.TyVar;
     25 
     26 /** Generic type erasure. */
     27 public class Erasure {
     28   public static Type erase(Type ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) {
     29     switch (ty.tyKind()) {
     30       case PRIM_TY:
     31       case VOID_TY:
     32         return ty;
     33       case CLASS_TY:
     34         return eraseClassTy((Type.ClassTy) ty);
     35       case ARRAY_TY:
     36         return eraseArrayTy((Type.ArrayTy) ty, tenv);
     37       case TY_VAR:
     38         return eraseTyVar((TyVar) ty, tenv);
     39       default:
     40         throw new AssertionError(ty.tyKind());
     41     }
     42   }
     43 
     44   private static Type eraseTyVar(
     45       TyVar ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) {
     46     SourceTypeBoundClass.TyVarInfo info = tenv.apply(ty.sym());
     47     if (info.superClassBound() != null) {
     48       return erase(info.superClassBound(), tenv);
     49     }
     50     if (!info.interfaceBounds().isEmpty()) {
     51       return erase(info.interfaceBounds().get(0), tenv);
     52     }
     53     return Type.ClassTy.OBJECT;
     54   }
     55 
     56   private static Type.ArrayTy eraseArrayTy(
     57       Type.ArrayTy ty, Function<TyVarSymbol, SourceTypeBoundClass.TyVarInfo> tenv) {
     58     return new Type.ArrayTy(erase(ty.elementType(), tenv), ty.annos());
     59   }
     60 
     61   public static Type.ClassTy eraseClassTy(Type.ClassTy ty) {
     62     ImmutableList.Builder<Type.ClassTy.SimpleClassTy> classes = ImmutableList.builder();
     63     for (Type.ClassTy.SimpleClassTy c : ty.classes) {
     64       if (c.targs().isEmpty()) {
     65         classes.add(c);
     66       } else {
     67         classes.add(new Type.ClassTy.SimpleClassTy(c.sym(), ImmutableList.of(), c.annos()));
     68       }
     69     }
     70     return new Type.ClassTy(classes.build());
     71   }
     72 }
     73