Debian Codegraph @

src/com/google/javascript/jscomp/PreprocessorSymbolTable.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
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.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 * A symbol table for references that are removed by preprocessor passes
35 * (like {@code ProcessClosurePrimitives}).
36 *
37 * @author [email protected] (Nick Santos)
38 */
39 final class PreprocessorSymbolTable
40 implements StaticScope<JSType>,
41 StaticSymbolTable<SimpleSlot,
42 PreprocessorSymbolTable.Reference> {
43
44 /**
45 * All preprocessor symbols are globals.
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 }