• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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.lookup;
18 
19 import com.google.turbine.binder.sym.ClassSymbol;
20 import com.google.turbine.tree.Tree;
21 import org.jspecify.nullness.Nullable;
22 
23 /**
24  * A scope for imports. Non-canonical imports depend on hierarchy analysis, so to break the cycle we
25  * defer non-canonical resolution to a {@link ResolveFunction} that is provided once hierarchy
26  * analysis is underway.
27  */
28 public interface ImportScope {
29 
30   /**
31    * A function that performs non-canonical resolution, see {@link
32    * com.google.turbine.binder.Resolve#resolve}.
33    */
34   @FunctionalInterface
35   interface ResolveFunction {
36     @Nullable
resolveOne(ClassSymbol base, Tree.Ident name)37     ClassSymbol resolveOne(ClassSymbol base, Tree.Ident name);
38   }
39 
40   /** See {@link Scope#lookup(LookupKey)}. */
41   @Nullable
lookup(LookupKey lookupKey, ResolveFunction resolve)42   LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve);
43 
44   /** Adds a scope to the chain, in the manner of {@link CompoundScope#append(Scope)}. */
append(ImportScope next)45   default ImportScope append(ImportScope next) {
46     return new ImportScope() {
47       @Override
48       public @Nullable LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve) {
49         LookupResult result = next.lookup(lookupKey, resolve);
50         if (result != null) {
51           return result;
52         }
53         return ImportScope.this.lookup(lookupKey, resolve);
54       }
55     };
56   }
57 
58   /**
59    * Creates a trivial {@link ImportScope} from a {@link Scope}, which ignores the provided {@link
60    * ResolveFunction} and calls the underlying scope's lookup method. Used to chain {@link Scope}s
61    * and {@link ImportScope}s together.
62    */
63   static ImportScope fromScope(Scope scope) {
64     return new ImportScope() {
65       @Override
66       public @Nullable LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve) {
67         return scope.lookup(lookupKey);
68       }
69     };
70   }
71 
72   /** Partially applies the given {@link ResolveFunction} to this {@link ImportScope}. */
73   default CompoundScope toScope(ResolveFunction resolve) {
74     return CompoundScope.base(
75         new Scope() {
76           @Override
77           public @Nullable LookupResult lookup(LookupKey lookupKey) {
78             return ImportScope.this.lookup(lookupKey, resolve);
79           }
80         });
81   }
82 }
83