• 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.annotations.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   interface ResolveFunction {
resolveOne(ClassSymbol base, Tree.Ident name)35     @Nullable ClassSymbol resolveOne(ClassSymbol base, Tree.Ident name);
36 
visible(ClassSymbol sym)37     boolean visible(ClassSymbol sym);
38   }
39 
40   /** See {@link Scope#lookup(LookupKey)}. */
lookup(LookupKey lookupKey, ResolveFunction resolve)41   @Nullable LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve);
42 
43   /** Adds a scope to the chain, in the manner of {@link CompoundScope#append(Scope)}. */
append(ImportScope next)44   default ImportScope append(ImportScope next) {
45     return new ImportScope() {
46       @Override
47       public @Nullable LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve) {
48         LookupResult result = next.lookup(lookupKey, resolve);
49         if (result != null) {
50           return result;
51         }
52         return ImportScope.this.lookup(lookupKey, resolve);
53       }
54     };
55   }
56 
57   /**
58    * Creates a trivial {@link ImportScope} from a {@link Scope}, which ignores the provided {@link
59    * ResolveFunction} and calls the underlying scope's lookup method. Used to chain {@link Scope}s
60    * and {@link ImportScope}s together.
61    */
62   static ImportScope fromScope(Scope scope) {
63     return new ImportScope() {
64       @Override
65       public @Nullable LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve) {
66         return scope.lookup(lookupKey);
67       }
68     };
69   }
70 
71   /** Partially applies the given {@link ResolveFunction} to this {@link ImportScope}. */
72   default CompoundScope toScope(ResolveFunction resolve) {
73     return CompoundScope.base(
74         new Scope() {
75           @Override
76           public @Nullable LookupResult lookup(LookupKey lookupKey) {
77             return ImportScope.this.lookup(lookupKey, resolve);
78           }
79         });
80   }
81 }
82