| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.common.collect.ImmutableSet; |
| 22 |
import com.google.common.collect.Iterables; |
| 23 |
import com.google.common.collect.Lists; |
| 24 |
import com.google.common.collect.Maps; |
| 25 |
import com.google.javascript.jscomp.Scope.Var; |
| 26 |
import com.google.javascript.rhino.Node; |
| 27 |
import com.google.javascript.rhino.jstype.StaticSymbolTable; |
| 28 |
|
| 29 |
import java.util.Collection; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Map; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class MemoizedScopeCreator |
| 47 |
implements ScopeCreator, StaticSymbolTable<Var, Var> { |
| 48 |
|
| 49 |
private final Map<Node, Scope> scopes = Maps.newHashMap(); |
| 50 |
private final ScopeCreator delegate; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
MemoizedScopeCreator(ScopeCreator delegate) { |
| 56 |
this.delegate = delegate; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Iterable<Var> getReferences(Var var) { |
| 61 |
return ImmutableList.of(var); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public Scope getScope(Var var) { |
| 66 |
return var.scope; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public Iterable<Var> getAllSymbols() { |
| 71 |
List<Var> vars = Lists.newArrayList(); |
| 72 |
for (Scope s : scopes.values()) { |
| 73 |
Iterables.addAll(vars, s.getAllSymbols()); |
| 74 |
} |
| 75 |
return vars; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public Scope createScope(Node n, Scope parent) { |
| 80 |
Scope scope = scopes.get(n); |
| 81 |
if (scope == null) { |
| 82 |
scope = delegate.createScope(n, parent); |
| 83 |
scopes.put(n, scope); |
| 84 |
} else { |
| 85 |
Preconditions.checkState(parent == scope.getParent()); |
| 86 |
} |
| 87 |
return scope; |
| 88 |
} |
| 89 |
|
| 90 |
Collection<Scope> getAllMemoizedScopes() { |
| 91 |
return Collections.unmodifiableCollection(scopes.values()); |
| 92 |
} |
| 93 |
|
| 94 |
Scope getScopeIfMemoized(Node n) { |
| 95 |
return scopes.get(n); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
void removeScopesForScript(String scriptName) { |
| 104 |
for (Node scopeRoot : ImmutableSet.copyOf(scopes.keySet())) { |
| 105 |
if (scriptName.equals(scopeRoot.getSourceFileName())) { |
| 106 |
scopes.remove(scopeRoot); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |