| 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.collect.Sets; |
| 20 |
import com.google.javascript.jscomp.NameReferenceGraph.Name; |
| 21 |
import com.google.javascript.jscomp.NameReferenceGraph.Reference; |
| 22 |
import com.google.javascript.jscomp.graph.FixedPointGraphTraversal; |
| 23 |
import com.google.javascript.jscomp.graph.Annotation; |
| 24 |
import com.google.javascript.jscomp.graph.GraphNode; |
| 25 |
import com.google.javascript.jscomp.graph.FixedPointGraphTraversal.EdgeCallback; |
| 26 |
import com.google.javascript.rhino.Node; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class AnalyzeNameReferences implements CompilerPass { |
| 43 |
|
| 44 |
private NameReferenceGraph graph; |
| 45 |
private final JSModuleGraph moduleGraph; |
| 46 |
private final AbstractCompiler compiler; |
| 47 |
|
| 48 |
AnalyzeNameReferences(AbstractCompiler compiler) { |
| 49 |
this.compiler = compiler; |
| 50 |
this.moduleGraph = compiler.getModuleGraph(); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void process(Node externs, Node root) { |
| 55 |
NameReferenceGraphConstruction gc = |
| 56 |
new NameReferenceGraphConstruction(compiler); |
| 57 |
gc.process(externs, root); |
| 58 |
graph = gc.getNameReferenceGraph(); |
| 59 |
FixedPointGraphTraversal<Name, Reference> t = |
| 60 |
FixedPointGraphTraversal.newTraversal(new PropagateReferences()); |
| 61 |
getInfo(graph.MAIN).markReference(null); |
| 62 |
t.computeFixedPoint(graph, Sets.newHashSet(graph.MAIN)); |
| 63 |
} |
| 64 |
|
| 65 |
public NameReferenceGraph getGraph() { |
| 66 |
return graph; |
| 67 |
} |
| 68 |
|
| 69 |
private class PropagateReferences implements EdgeCallback<Name, Reference> { |
| 70 |
@Override |
| 71 |
public boolean traverseEdge(Name start, Reference edge, Name dest) { |
| 72 |
NameInfo startInfo = getInfo(start); |
| 73 |
NameInfo destInfo = getInfo(dest); |
| 74 |
if (startInfo.isReferenced()) { |
| 75 |
JSModule startModule = startInfo.getDeepestCommonModuleRef(); |
| 76 |
if (startModule != null && |
| 77 |
moduleGraph.dependsOn(startModule, edge.getModule())) { |
| 78 |
return destInfo.markReference(startModule); |
| 79 |
} else { |
| 80 |
return destInfo.markReference(edge.getModule()); |
| 81 |
} |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private NameInfo getInfo(Name symbol) { |
| 88 |
GraphNode<Name, Reference> name = graph.getNode(symbol); |
| 89 |
NameInfo info = name.getAnnotation(); |
| 90 |
if (info == null) { |
| 91 |
info = new NameInfo(); |
| 92 |
name.setAnnotation(info); |
| 93 |
} |
| 94 |
return info; |
| 95 |
} |
| 96 |
|
| 97 |
final class NameInfo implements Annotation { |
| 98 |
private boolean referenced = false; |
| 99 |
private JSModule deepestCommonModuleRef = null; |
| 100 |
|
| 101 |
|
| 102 |
boolean isReferenced() { |
| 103 |
return referenced; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
JSModule getDeepestCommonModuleRef() { |
| 111 |
return deepestCommonModuleRef; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
boolean markReference(JSModule module) { |
| 121 |
boolean hasChanged = false; |
| 122 |
if (!referenced) { |
| 123 |
referenced = true; |
| 124 |
hasChanged = true; |
| 125 |
} |
| 126 |
|
| 127 |
if (moduleGraph != null) { |
| 128 |
JSModule originalDeepestCommon = deepestCommonModuleRef; |
| 129 |
|
| 130 |
if (deepestCommonModuleRef == null) { |
| 131 |
deepestCommonModuleRef = module; |
| 132 |
} else { |
| 133 |
deepestCommonModuleRef = |
| 134 |
moduleGraph.getDeepestCommonDependencyInclusive( |
| 135 |
deepestCommonModuleRef, module); |
| 136 |
} |
| 137 |
|
| 138 |
if (originalDeepestCommon != deepestCommonModuleRef) { |
| 139 |
hasChanged = true; |
| 140 |
} |
| 141 |
} |
| 142 |
return hasChanged; |
| 143 |
} |
| 144 |
} |
| 145 |
} |