Home | History | Annotate | Download | only in bound
      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.binder.bound;
     18 
     19 import com.google.common.base.Function;
     20 import com.google.common.collect.ImmutableList;
     21 import com.google.common.collect.ImmutableMap;
     22 import com.google.common.collect.Iterables;
     23 import com.google.turbine.binder.lookup.CompoundScope;
     24 import com.google.turbine.binder.lookup.MemberImportIndex;
     25 import com.google.turbine.binder.sym.ClassSymbol;
     26 import com.google.turbine.binder.sym.TyVarSymbol;
     27 import com.google.turbine.diag.SourceFile;
     28 import com.google.turbine.model.TurbineTyKind;
     29 import com.google.turbine.type.AnnoInfo;
     30 import com.google.turbine.type.Type;
     31 import com.google.turbine.type.Type.ClassTy;
     32 import javax.annotation.Nullable;
     33 
     34 /** A HeaderBoundClass for classes compiled from source. */
     35 public class SourceTypeBoundClass implements TypeBoundClass {
     36 
     37   private final TurbineTyKind kind;
     38   private final ClassSymbol owner;
     39   private final ImmutableMap<String, ClassSymbol> children;
     40 
     41   private final int access;
     42   private final ImmutableMap<String, TyVarSymbol> typeParameters;
     43 
     44   private final ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes;
     45   private final Type.ClassTy superClassType;
     46   private final ImmutableList<Type.ClassTy> interfaceTypes;
     47   private final ImmutableList<MethodInfo> methods;
     48   private final ImmutableList<FieldInfo> fields;
     49   private final CompoundScope enclosingScope;
     50   private final CompoundScope scope;
     51   private final MemberImportIndex memberImports;
     52   private final AnnotationMetadata annotationMetadata;
     53   private final ImmutableList<AnnoInfo> annotations;
     54   private final SourceFile source;
     55 
     56   public SourceTypeBoundClass(
     57       ImmutableList<ClassTy> interfaceTypes,
     58       ClassTy superClassType,
     59       ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes,
     60       int access,
     61       ImmutableList<MethodInfo> methods,
     62       ImmutableList<FieldInfo> fields,
     63       ClassSymbol owner,
     64       TurbineTyKind kind,
     65       ImmutableMap<String, ClassSymbol> children,
     66       ImmutableMap<String, TyVarSymbol> typeParameters,
     67       CompoundScope enclosingScope,
     68       CompoundScope scope,
     69       MemberImportIndex memberImports,
     70       AnnotationMetadata annotationMetadata,
     71       ImmutableList<AnnoInfo> annotations,
     72       SourceFile source) {
     73     this.interfaceTypes = interfaceTypes;
     74     this.superClassType = superClassType;
     75     this.typeParameterTypes = typeParameterTypes;
     76     this.access = access;
     77     this.methods = methods;
     78     this.fields = fields;
     79     this.owner = owner;
     80     this.kind = kind;
     81     this.children = children;
     82     this.typeParameters = typeParameters;
     83     this.enclosingScope = enclosingScope;
     84     this.scope = scope;
     85     this.memberImports = memberImports;
     86     this.annotationMetadata = annotationMetadata;
     87     this.annotations = annotations;
     88     this.source = source;
     89   }
     90 
     91   @Override
     92   public ClassSymbol superclass() {
     93     return superClassType() != null ? superClassType().sym() : null;
     94   }
     95 
     96   @Override
     97   public ImmutableList<ClassSymbol> interfaces() {
     98     return ImmutableList.copyOf(
     99         Iterables.transform(
    100             interfaceTypes,
    101             new Function<ClassTy, ClassSymbol>() {
    102               @Override
    103               public ClassSymbol apply(ClassTy classTy) {
    104                 return classTy.sym();
    105               }
    106             }));
    107   }
    108 
    109   @Override
    110   public int access() {
    111     return access;
    112   }
    113 
    114   @Override
    115   public TurbineTyKind kind() {
    116     return kind;
    117   }
    118 
    119   @Nullable
    120   @Override
    121   public ClassSymbol owner() {
    122     return owner;
    123   }
    124 
    125   @Override
    126   public ImmutableMap<String, ClassSymbol> children() {
    127     return children;
    128   }
    129 
    130   @Override
    131   public ImmutableMap<String, TyVarSymbol> typeParameters() {
    132     return typeParameters;
    133   }
    134 
    135   /** Implemented interface types. */
    136   public ImmutableList<Type.ClassTy> interfaceTypes() {
    137     return interfaceTypes;
    138   }
    139 
    140   /** The super-class type. */
    141   @Override
    142   public Type.ClassTy superClassType() {
    143     return superClassType;
    144   }
    145 
    146   /** Declared methods. */
    147   @Override
    148   public ImmutableList<MethodInfo> methods() {
    149     return methods;
    150   }
    151 
    152   @Override
    153   public AnnotationMetadata annotationMetadata() {
    154     return annotationMetadata;
    155   }
    156 
    157   /** Declared fields. */
    158   @Override
    159   public ImmutableList<FieldInfo> fields() {
    160     return fields;
    161   }
    162 
    163   /** Declared type parameters. */
    164   @Override
    165   public ImmutableMap<TyVarSymbol, TyVarInfo> typeParameterTypes() {
    166     return typeParameterTypes;
    167   }
    168 
    169   /** The scope of the enclosing declaration or compilation unit. */
    170   public CompoundScope enclosingScope() {
    171     return enclosingScope;
    172   }
    173 
    174   /** The scope of the current class, including its members. */
    175   public CompoundScope scope() {
    176     return scope;
    177   }
    178 
    179   /** The static member import index for the enclosing compilation unit. */
    180   public MemberImportIndex memberImports() {
    181     return memberImports;
    182   }
    183 
    184   /** Declaration annotations. */
    185   public ImmutableList<AnnoInfo> annotations() {
    186     return annotations;
    187   }
    188 
    189   /** The source file. */
    190   public SourceFile source() {
    191     return source;
    192   }
    193 }
    194