aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/binder/Resolve.java
blob: 25ffe119cf0ee745383e89022d63c6c87a915196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.binder;

import com.google.turbine.binder.bound.BoundClass;
import com.google.turbine.binder.bound.HeaderBoundClass;
import com.google.turbine.binder.env.CompoundEnv;
import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.lookup.CanonicalSymbolResolver;
import com.google.turbine.binder.lookup.LookupResult;
import com.google.turbine.binder.sym.ClassSymbol;

/** Qualified name resolution. */
public class Resolve {

  /**
   * Performs JLS 6.5.5.2 qualified type name resolution of a type with the given simple name,
   * qualified by the given symbol. The search considers members that are inherited from
   * superclasses or interfaces.
   */
  public static ClassSymbol resolve(
      Env<ClassSymbol, ? extends HeaderBoundClass> env, ClassSymbol sym, String simpleName) {
    ClassSymbol result;
    HeaderBoundClass bound = env.get(sym);
    if (bound == null) {
      return null;
    }
    result = bound.children().get(simpleName);
    if (result != null) {
      return result;
    }
    if (bound.superclass() != null) {
      result = resolve(env, bound.superclass(), simpleName);
      if (result != null) {
        return result;
      }
    }
    for (ClassSymbol i : bound.interfaces()) {
      result = resolve(env, i, simpleName);
      if (result != null) {
        return result;
      }
    }
    return null;
  }

  static class CanonicalResolver implements CanonicalSymbolResolver {
    private final CompoundEnv<ClassSymbol, BoundClass> env;

    public CanonicalResolver(CompoundEnv<ClassSymbol, BoundClass> env) {
      this.env = env;
    }

    @Override
    public ClassSymbol resolve(LookupResult result) {
      ClassSymbol sym = (ClassSymbol) result.sym();
      for (String bit : result.remaining()) {
        sym = resolveOne(sym, bit);
        if (sym == null) {
          return null;
        }
      }
      return sym;
    }

    @Override
    public ClassSymbol resolveOne(ClassSymbol sym, String bit) {
      BoundClass ci = env.get(sym);
      if (ci == null) {
        return null;
      }
      sym = ci.children().get(bit);
      if (sym == null) {
        return null;
      }
      return sym;
    }
  }
}