Home | History | Annotate | Download | only in env
      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.env;
     18 
     19 import com.google.common.base.Joiner;
     20 import com.google.common.collect.ImmutableMap;
     21 import com.google.turbine.binder.sym.Symbol;
     22 import java.util.LinkedHashMap;
     23 import java.util.LinkedHashSet;
     24 import java.util.Map;
     25 
     26 /**
     27  * An env that permits an analysis pass to access information about symbols from the current pass,
     28  * recursively. Cycles are detected, and result in an {@link LazyBindingError} being thrown.
     29  *
     30  * <p>This is used primarily for resolving the supertype hierarchy in {@link HierarchyBinder}. The
     31  * supertype hierarchy forms a directed acyclic graph, and {@link HierarchyBinder} needs to process
     32  * classes in a topological sort order of that graph. Unfortuntately, we can't produce a suitable
     33  * sort order until the graph exists.
     34  *
     35  * @param <T> the interface type of the bound node {@link V}, shared by any underlying environments.
     36  * @param <V> a specific implementation of {@code T}. For example, during hierarchy binding {@link
     37  *     SourceHeaderBoundClass} nodes are being completed from the sources being compiled, and the
     38  *     analysis of a given symbol may require looking up {@link HeaderBoundClass} nodes that will
     39  *     either be backed by other {@link SourceHeaderBoundClass} nodes or {@link BytecodeBoundClass}
     40  *     nodes. So the phase uses an {@link LazyEnv<HeaderBoundClass, SourceHeaderBoundClass>}.
     41  */
     42 public class LazyEnv<S extends Symbol, T, V extends T> implements Env<S, V> {
     43 
     44   /** The list of symbols that are currently being processed, used to check for cycles. */
     45   private final LinkedHashSet<S> seen = new LinkedHashSet<>();
     46 
     47   /** Lazy value providers for the symbols in the environment. */
     48   private final ImmutableMap<S, Completer<S, T, V>> completers;
     49 
     50   /** Values that have already been computed. */
     51   private final Map<S, V> cache = new LinkedHashMap<>();
     52 
     53   /** An underlying env of already-computed {@code T}s that can be queried during completion. */
     54   private final Env<S, T> rec;
     55 
     56   public LazyEnv(ImmutableMap<S, Completer<S, T, V>> completers, Env<S, ? extends T> base) {
     57     this.completers = completers;
     58     this.rec = CompoundEnv.<S, T>of(base).append(this);
     59   }
     60 
     61   @Override
     62   public V get(S sym) {
     63     V v = cache.get(sym);
     64     if (v != null) {
     65       return v;
     66     }
     67     Completer<S, T, V> completer = completers.get(sym);
     68     if (completer != null) {
     69       if (!seen.add(sym)) {
     70         throw new LazyBindingError(Joiner.on(" -> ").join(seen) + " -> " + sym);
     71       }
     72       v = completer.complete(rec, sym);
     73       seen.remove(sym);
     74       cache.put(sym, v);
     75       return v;
     76     }
     77     return null;
     78   }
     79 
     80   /** A lazy value provider which is given access to the current environment. */
     81   public interface Completer<S extends Symbol, T, V extends T> {
     82     /** Provides the value for the given symbol in the current environment. */
     83     V complete(Env<S, T> env, S k);
     84   }
     85 
     86   /** Indicates that a completer tried to complete itself, possibly transitively. */
     87   public static class LazyBindingError extends Error {
     88     public LazyBindingError(String message) {
     89       super(message);
     90     }
     91   }
     92 }
     93