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.turbine.binder.sym.ClassSymbol; 20 import com.google.turbine.binder.sym.Symbol; 21 import org.jspecify.nullness.Nullable; 22 23 /** 24 * An environment that maps {@link Symbol}s {@code S} to bound nodes {@code V}. 25 * 26 * <p>For example, {@code BoundClass} represents superclasses as a {@link ClassSymbol}, which only 27 * contains the binary name of the type. To get the {@code BoundClass} for that supertype, an {@code 28 * Env<BoundClass>} is used. 29 * 30 * <p>The indirection through env makes it possible to represent a graph with cycles using immutable 31 * nodes, and makes it easy to reason about the information produced and consumed by each pass. 32 * 33 * <p>TODO(cushon): keep an eye on the cost of indirections, and consider caching lookups in bound 34 * nodes if it looks like it would make a difference. 35 */ 36 public interface Env<S extends Symbol, V> { 37 /** Returns the information associated with the given symbol in this environment. */ 38 @Nullable get(S sym)39 V get(S sym); 40 getNonNull(S sym)41 default V getNonNull(S sym) { 42 V result = get(sym); 43 if (result == null) { 44 throw new NullPointerException(sym.toString()); 45 } 46 return result; 47 } 48 } 49