| 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.Lists; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.deps.DependencyInfo; |
| 24 |
import com.google.javascript.jscomp.deps.JsFileParser; |
| 25 |
import com.google.javascript.rhino.InputId; |
| 26 |
import com.google.javascript.rhino.Node; |
| 27 |
import java.io.IOException; |
| 28 |
import java.util.Collection; |
| 29 |
import java.util.Collections; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Set; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class CompilerInput |
| 40 |
implements SourceAst, DependencyInfo { |
| 41 |
|
| 42 |
private static final long serialVersionUID = 1L; |
| 43 |
|
| 44 |
|
| 45 |
private JSModule module; |
| 46 |
final private InputId id; |
| 47 |
|
| 48 |
|
| 49 |
private final SourceAst ast; |
| 50 |
|
| 51 |
|
| 52 |
private final Set<String> provides = Sets.newHashSet(); |
| 53 |
private final Set<String> requires = Sets.newHashSet(); |
| 54 |
private boolean generatedDependencyInfoFromSource = false; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private transient AbstractCompiler compiler; |
| 59 |
|
| 60 |
public CompilerInput(SourceAst ast) { |
| 61 |
this(ast, ast.getSourceFile().getName(), false); |
| 62 |
} |
| 63 |
|
| 64 |
public CompilerInput(SourceAst ast, boolean isExtern) { |
| 65 |
this(ast, ast.getInputId(), isExtern); |
| 66 |
} |
| 67 |
|
| 68 |
public CompilerInput(SourceAst ast, String inputId, boolean isExtern) { |
| 69 |
this(ast, new InputId(inputId), isExtern); |
| 70 |
} |
| 71 |
|
| 72 |
public CompilerInput(SourceAst ast, InputId inputId, boolean isExtern) { |
| 73 |
this.ast = ast; |
| 74 |
this.id = inputId; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if (ast != null && ast.getSourceFile() != null) { |
| 79 |
ast.getSourceFile().setIsExtern(isExtern); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public CompilerInput(SourceFile file) { |
| 84 |
this(file, false); |
| 85 |
} |
| 86 |
|
| 87 |
public CompilerInput(SourceFile file, boolean isExtern) { |
| 88 |
this(new JsAst(file), isExtern); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
@Override |
| 93 |
public InputId getInputId() { |
| 94 |
return id; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public String getName() { |
| 100 |
return id.getIdName(); |
| 101 |
} |
| 102 |
|
| 103 |
public SourceAst getAst() { |
| 104 |
return ast; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
@Override |
| 109 |
public String getPathRelativeToClosureBase() { |
| 110 |
|
| 111 |
throw new UnsupportedOperationException(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Node getAstRoot(AbstractCompiler compiler) { |
| 116 |
Node root = ast.getAstRoot(compiler); |
| 117 |
|
| 118 |
if (root != null) { |
| 119 |
Preconditions.checkState(root.isScript()); |
| 120 |
Preconditions.checkNotNull(root.getInputId()); |
| 121 |
} |
| 122 |
return root; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void clearAst() { |
| 127 |
ast.clearAst(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public SourceFile getSourceFile() { |
| 132 |
return ast.getSourceFile(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void setSourceFile(SourceFile file) { |
| 137 |
ast.setSourceFile(file); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public SourceAst getSourceAst() { |
| 142 |
return ast; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public void setCompiler(AbstractCompiler compiler) { |
| 147 |
this.compiler = compiler; |
| 148 |
} |
| 149 |
|
| 150 |
private void checkErrorManager() { |
| 151 |
Preconditions.checkNotNull(compiler, |
| 152 |
"Expected setCompiler to be called first: " + this); |
| 153 |
Preconditions.checkNotNull(compiler.getErrorManager(), |
| 154 |
"Expected compiler to call an error manager: " + this); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
@Override |
| 159 |
public Collection<String> getRequires() { |
| 160 |
checkErrorManager(); |
| 161 |
try { |
| 162 |
regenerateDependencyInfoIfNecessary(); |
| 163 |
return Collections.<String>unmodifiableSet(requires); |
| 164 |
} catch (IOException e) { |
| 165 |
compiler.getErrorManager().report(CheckLevel.ERROR, |
| 166 |
JSError.make(AbstractCompiler.READ_ERROR, getName())); |
| 167 |
return ImmutableList.<String>of(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
@Override |
| 173 |
public Collection<String> getProvides() { |
| 174 |
checkErrorManager(); |
| 175 |
try { |
| 176 |
regenerateDependencyInfoIfNecessary(); |
| 177 |
return Collections.<String>unmodifiableSet(provides); |
| 178 |
} catch (IOException e) { |
| 179 |
compiler.getErrorManager().report(CheckLevel.ERROR, |
| 180 |
JSError.make(AbstractCompiler.READ_ERROR, getName())); |
| 181 |
return ImmutableList.<String>of(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void addProvide(String provide) { |
| 188 |
getProvides(); |
| 189 |
provides.add(provide); |
| 190 |
} |
| 191 |
|
| 192 |
void addRequire(String require) { |
| 193 |
getRequires(); |
| 194 |
requires.add(require); |
| 195 |
} |
| 196 |
|
| 197 |
public void removeRequire(String require) { |
| 198 |
getRequires(); |
| 199 |
requires.remove(require); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private void regenerateDependencyInfoIfNecessary() throws IOException { |
| 206 |
|
| 207 |
|
| 208 |
if (!(ast instanceof JsAst)) { |
| 209 |
Preconditions.checkNotNull(compiler, |
| 210 |
"Expected setCompiler to be called first"); |
| 211 |
DepsFinder finder = new DepsFinder(); |
| 212 |
Node root = getAstRoot(compiler); |
| 213 |
if (root == null) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
finder.visitTree(getAstRoot(compiler)); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
provides.addAll(finder.provides); |
| 231 |
requires.addAll(finder.requires); |
| 232 |
} else { |
| 233 |
|
| 234 |
if (!generatedDependencyInfoFromSource) { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
DependencyInfo info = |
| 240 |
(new JsFileParser(compiler.getErrorManager())) |
| 241 |
.setIncludeGoogBase(true) |
| 242 |
.parseFile(getName(), getName(), getCode()); |
| 243 |
|
| 244 |
provides.addAll(info.getProvides()); |
| 245 |
requires.addAll(info.getRequires()); |
| 246 |
|
| 247 |
generatedDependencyInfoFromSource = true; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private static class DepsFinder { |
| 253 |
private final List<String> provides = Lists.newArrayList(); |
| 254 |
private final List<String> requires = Lists.newArrayList(); |
| 255 |
private final CodingConvention codingConvention = |
| 256 |
new ClosureCodingConvention(); |
| 257 |
|
| 258 |
void visitTree(Node n) { |
| 259 |
visitSubtree(n, null); |
| 260 |
} |
| 261 |
|
| 262 |
void visitSubtree(Node n, Node parent) { |
| 263 |
if (n.isCall()) { |
| 264 |
String require = |
| 265 |
codingConvention.extractClassNameIfRequire(n, parent); |
| 266 |
if (require != null) { |
| 267 |
requires.add(require); |
| 268 |
} |
| 269 |
|
| 270 |
String provide = |
| 271 |
codingConvention.extractClassNameIfProvide(n, parent); |
| 272 |
if (provide != null) { |
| 273 |
provides.add(provide); |
| 274 |
} |
| 275 |
return; |
| 276 |
} else if (parent != null && |
| 277 |
!parent.isExprResult() && |
| 278 |
!parent.isScript()) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
for (Node child = n.getFirstChild(); |
| 283 |
child != null; child = child.getNext()) { |
| 284 |
visitSubtree(child, n); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public String getLine(int lineNumber) { |
| 298 |
return getSourceFile().getLine(lineNumber); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public Region getRegion(int lineNumber) { |
| 311 |
return getSourceFile().getRegion(lineNumber); |
| 312 |
} |
| 313 |
|
| 314 |
public String getCode() throws IOException { |
| 315 |
return getSourceFile().getCode(); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
public JSModule getModule() { |
| 320 |
return module; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
public void setModule(JSModule module) { |
| 325 |
|
| 326 |
Preconditions.checkArgument( |
| 327 |
module == null || this.module == null || this.module == module); |
| 328 |
this.module = module; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
void overrideModule(JSModule module) { |
| 333 |
this.module = module; |
| 334 |
} |
| 335 |
|
| 336 |
public boolean isExtern() { |
| 337 |
if (ast == null || ast.getSourceFile() == null) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
return ast.getSourceFile().isExtern(); |
| 341 |
} |
| 342 |
|
| 343 |
void setIsExtern(boolean isExtern) { |
| 344 |
if (ast == null || ast.getSourceFile() == null) { |
| 345 |
return; |
| 346 |
} |
| 347 |
ast.getSourceFile().setIsExtern(isExtern); |
| 348 |
} |
| 349 |
|
| 350 |
public int getLineOffset(int lineno) { |
| 351 |
return ast.getSourceFile().getLineOffset(lineno); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
public int getNumLines() { |
| 356 |
return ast.getSourceFile().getNumLines(); |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
public String toString() { |
| 361 |
return getName(); |
| 362 |
} |
| 363 |
} |