| 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.ArrayListMultimap; |
| 21 |
import com.google.common.collect.Maps; |
| 22 |
import com.google.common.collect.Multimap; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.jstype.JSType; |
| 25 |
import com.google.javascript.rhino.jstype.SimpleReference; |
| 26 |
import com.google.javascript.rhino.jstype.SimpleSlot; |
| 27 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 28 |
import com.google.javascript.rhino.jstype.StaticSymbolTable; |
| 29 |
|
| 30 |
import java.util.Collections; |
| 31 |
import java.util.Map; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
final class PreprocessorSymbolTable |
| 40 |
implements StaticScope<JSType>, |
| 41 |
StaticSymbolTable<SimpleSlot, |
| 42 |
PreprocessorSymbolTable.Reference> { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private final Map<String, SimpleSlot> symbols = Maps.newHashMap(); |
| 48 |
|
| 49 |
private final Multimap<String, Reference> refs = |
| 50 |
ArrayListMultimap.create(); |
| 51 |
|
| 52 |
private final Node root; |
| 53 |
|
| 54 |
PreprocessorSymbolTable(Node root) { |
| 55 |
this.root = root; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public Node getRootNode() { return root; } |
| 60 |
|
| 61 |
@Override |
| 62 |
public JSType getTypeOfThis() { return null; } |
| 63 |
|
| 64 |
@Override |
| 65 |
public StaticScope<JSType> getParentScope() { return null; } |
| 66 |
|
| 67 |
@Override |
| 68 |
public SimpleSlot getSlot(String name) { |
| 69 |
return symbols.get(name); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public SimpleSlot getOwnSlot(String name) { |
| 74 |
return getSlot(name); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Iterable<Reference> getReferences(SimpleSlot symbol) { |
| 79 |
return Collections.unmodifiableCollection(refs.get(symbol.getName())); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public Iterable<SimpleSlot> getAllSymbols() { |
| 84 |
return Collections.unmodifiableCollection(symbols.values()); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public StaticScope<JSType> getScope(SimpleSlot slot) { |
| 89 |
return this; |
| 90 |
} |
| 91 |
|
| 92 |
void addReference(Node node) { |
| 93 |
String name = node.getQualifiedName(); |
| 94 |
Preconditions.checkNotNull(name); |
| 95 |
|
| 96 |
if (!symbols.containsKey(name)) { |
| 97 |
symbols.put(name, new SimpleSlot(name, null, true)); |
| 98 |
} |
| 99 |
|
| 100 |
refs.put(name, new Reference(symbols.get(name), node)); |
| 101 |
} |
| 102 |
|
| 103 |
static final class Reference extends SimpleReference<SimpleSlot> { |
| 104 |
Reference(SimpleSlot symbol, Node node) { |
| 105 |
super(symbol, node); |
| 106 |
} |
| 107 |
} |
| 108 |
} |