| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import static com.google.common.base.Preconditions.checkState; |
| 19 |
|
| 20 |
import com.google.common.base.Joiner; |
| 21 |
import com.google.common.base.Preconditions; |
| 22 |
import com.google.common.collect.HashMultimap; |
| 23 |
import com.google.common.collect.ImmutableSet; |
| 24 |
import com.google.common.collect.LinkedHashMultimap; |
| 25 |
import com.google.common.collect.Lists; |
| 26 |
import com.google.common.collect.Maps; |
| 27 |
import com.google.common.collect.Multimap; |
| 28 |
import com.google.common.collect.Sets; |
| 29 |
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage; |
| 30 |
import com.google.javascript.jscomp.ConcreteType.ConcreteFunctionType; |
| 31 |
import com.google.javascript.jscomp.ConcreteType.ConcreteInstanceType; |
| 32 |
import com.google.javascript.jscomp.ConcreteType.ConcreteUnionType; |
| 33 |
import com.google.javascript.jscomp.ConcreteType.ConcreteUniqueType; |
| 34 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 35 |
import com.google.javascript.jscomp.TypeValidator.TypeMismatch; |
| 36 |
import com.google.javascript.jscomp.graph.StandardUnionFind; |
| 37 |
import com.google.javascript.jscomp.graph.UnionFind; |
| 38 |
import com.google.javascript.rhino.Node; |
| 39 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 40 |
import com.google.javascript.rhino.jstype.JSType; |
| 41 |
import com.google.javascript.rhino.jstype.JSTypeNative; |
| 42 |
import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 43 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 44 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 45 |
|
| 46 |
import java.util.Collection; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Map; |
| 49 |
import java.util.Set; |
| 50 |
import java.util.Stack; |
| 51 |
import java.util.logging.Logger; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
class DisambiguateProperties<T> implements CompilerPass { |
| 79 |
|
| 80 |
|
| 81 |
private static final int MAX_INVALDIATION_WARNINGS_PER_PROPERTY = 10; |
| 82 |
|
| 83 |
private static final Logger logger = Logger.getLogger( |
| 84 |
DisambiguateProperties.class.getName()); |
| 85 |
|
| 86 |
static class Warnings { |
| 87 |
|
| 88 |
static final DiagnosticType INVALIDATION = DiagnosticType.disabled( |
| 89 |
"JSC_INVALIDATION", |
| 90 |
"Property disambiguator skipping all instances of property {0} " |
| 91 |
+ "because of type {1} node {2}. {3}"); |
| 92 |
|
| 93 |
static final DiagnosticType INVALIDATION_ON_TYPE = DiagnosticType.disabled( |
| 94 |
"JSC_INVALIDATION_TYPE", |
| 95 |
"Property disambiguator skipping instances of property {0} " |
| 96 |
+ "on type {1}. {2}"); |
| 97 |
} |
| 98 |
|
| 99 |
private final AbstractCompiler compiler; |
| 100 |
private final TypeSystem<T> typeSystem; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private Multimap<Object, JSError> invalidationMap; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private final Map<String, CheckLevel> propertiesToErrorFor; |
| 118 |
|
| 119 |
private class Property { |
| 120 |
|
| 121 |
final String name; |
| 122 |
|
| 123 |
|
| 124 |
private UnionFind<T> types; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
Set<T> typesToSkip = Sets.newHashSet(); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
boolean skipRenaming; |
| 137 |
|
| 138 |
|
| 139 |
Set<Node> renameNodes = Sets.newHashSet(); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
final Map<Node, T> rootTypes = Maps.newHashMap(); |
| 147 |
|
| 148 |
Property(String name) { |
| 149 |
this.name = name; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
UnionFind<T> getTypes() { |
| 154 |
if (types == null) { |
| 155 |
types = new StandardUnionFind<T>(); |
| 156 |
} |
| 157 |
return types; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
boolean addType(T type, T top, T relatedType) { |
| 166 |
checkState(!skipRenaming, "Attempt to record skipped property: %s", name); |
| 167 |
if (typeSystem.isInvalidatingType(top)) { |
| 168 |
invalidate(); |
| 169 |
return false; |
| 170 |
} else { |
| 171 |
if (typeSystem.isTypeToSkip(top)) { |
| 172 |
addTypeToSkip(top); |
| 173 |
} |
| 174 |
|
| 175 |
if (relatedType == null) { |
| 176 |
getTypes().add(top); |
| 177 |
} else { |
| 178 |
getTypes().union(top, relatedType); |
| 179 |
} |
| 180 |
typeSystem.recordInterfaces(type, top, this); |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void addTypeToSkip(T type) { |
| 187 |
for (T skipType : typeSystem.getTypesToSkipForType(type)) { |
| 188 |
typesToSkip.add(skipType); |
| 189 |
getTypes().union(skipType, type); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
void expandTypesToSkip() { |
| 195 |
|
| 196 |
|
| 197 |
if (shouldRename()) { |
| 198 |
int count = 0; |
| 199 |
while (true) { |
| 200 |
|
| 201 |
checkState(++count < 10, "Stuck in loop expanding types to skip."); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
Set<T> rootTypesToSkip = Sets.newHashSet(); |
| 206 |
for (T subType : typesToSkip) { |
| 207 |
rootTypesToSkip.add(types.find(subType)); |
| 208 |
} |
| 209 |
typesToSkip.addAll(rootTypesToSkip); |
| 210 |
|
| 211 |
Set<T> newTypesToSkip = Sets.newHashSet(); |
| 212 |
Set<T> allTypes = types.elements(); |
| 213 |
int originalTypesSize = allTypes.size(); |
| 214 |
for (T subType : allTypes) { |
| 215 |
if (!typesToSkip.contains(subType) |
| 216 |
&& typesToSkip.contains(types.find(subType))) { |
| 217 |
newTypesToSkip.add(subType); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
for (T newType : newTypesToSkip) { |
| 222 |
addTypeToSkip(newType); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (types.elements().size() == originalTypesSize) { |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
boolean shouldRename() { |
| 235 |
return !skipRenaming && types != null |
| 236 |
&& types.allEquivalenceClasses().size() > 1; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
boolean shouldRename(T type) { |
| 245 |
return !skipRenaming && !typesToSkip.contains(type); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
boolean invalidate() { |
| 253 |
boolean changed = !skipRenaming; |
| 254 |
skipRenaming = true; |
| 255 |
types = null; |
| 256 |
return changed; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
boolean scheduleRenaming(Node node, T type) { |
| 269 |
if (!skipRenaming) { |
| 270 |
if (typeSystem.isInvalidatingType(type)) { |
| 271 |
invalidate(); |
| 272 |
return false; |
| 273 |
} |
| 274 |
renameNodes.add(node); |
| 275 |
rootTypes.put(node, type); |
| 276 |
} |
| 277 |
return true; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
private Map<String, Property> properties = Maps.newHashMap(); |
| 282 |
|
| 283 |
static DisambiguateProperties<JSType> forJSTypeSystem( |
| 284 |
AbstractCompiler compiler, |
| 285 |
Map<String, CheckLevel> propertiesToErrorFor) { |
| 286 |
return new DisambiguateProperties<JSType>( |
| 287 |
compiler, new JSTypeSystem(compiler), propertiesToErrorFor); |
| 288 |
} |
| 289 |
|
| 290 |
static DisambiguateProperties<ConcreteType> forConcreteTypeSystem( |
| 291 |
AbstractCompiler compiler, TightenTypes tt, |
| 292 |
Map<String, CheckLevel> propertiesToErrorFor) { |
| 293 |
return new DisambiguateProperties<ConcreteType>( |
| 294 |
compiler, new ConcreteTypeSystem(tt, compiler.getCodingConvention()), |
| 295 |
propertiesToErrorFor); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
private DisambiguateProperties(AbstractCompiler compiler, |
| 303 |
TypeSystem<T> typeSystem, Map<String, CheckLevel> propertiesToErrorFor) { |
| 304 |
this.compiler = compiler; |
| 305 |
this.typeSystem = typeSystem; |
| 306 |
this.propertiesToErrorFor = propertiesToErrorFor; |
| 307 |
if (!this.propertiesToErrorFor.isEmpty()) { |
| 308 |
this.invalidationMap = LinkedHashMultimap.create(); |
| 309 |
} else { |
| 310 |
this.invalidationMap = null; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public void process(Node externs, Node root) { |
| 316 |
Preconditions.checkState( |
| 317 |
compiler.getLifeCycleStage() == LifeCycleStage.NORMALIZED); |
| 318 |
for (TypeMismatch mis : compiler.getTypeValidator().getMismatches()) { |
| 319 |
addInvalidatingType(mis.typeA, mis.src); |
| 320 |
addInvalidatingType(mis.typeB, mis.src); |
| 321 |
} |
| 322 |
|
| 323 |
NodeTraversal.traverse(compiler, externs, new FindExternProperties()); |
| 324 |
NodeTraversal.traverse(compiler, root, new FindRenameableProperties()); |
| 325 |
renameProperties(); |
| 326 |
} |
| 327 |
|
| 328 |
private void recordInvalidationError(JSType t, JSError error) { |
| 329 |
if (!t.isObject()) { |
| 330 |
return; |
| 331 |
} |
| 332 |
if (invalidationMap != null) { |
| 333 |
invalidationMap.put(t, error); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private void addInvalidatingType(JSType type, JSError error) { |
| 341 |
type = type.restrictByNotNullOrUndefined(); |
| 342 |
if (type.isUnionType()) { |
| 343 |
for (JSType alt : type.toMaybeUnionType().getAlternates()) { |
| 344 |
addInvalidatingType(alt, error); |
| 345 |
} |
| 346 |
} else if (type.isEnumElementType()) { |
| 347 |
addInvalidatingType( |
| 348 |
type.toMaybeEnumElementType().getPrimitiveType(), error); |
| 349 |
} else { |
| 350 |
typeSystem.addInvalidatingType(type); |
| 351 |
recordInvalidationError(type, error); |
| 352 |
ObjectType objType = ObjectType.cast(type); |
| 353 |
if (objType != null && objType.getImplicitPrototype() != null) { |
| 354 |
typeSystem.addInvalidatingType(objType.getImplicitPrototype()); |
| 355 |
recordInvalidationError(objType.getImplicitPrototype(), error); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
protected Property getProperty(String name) { |
| 363 |
if (!properties.containsKey(name)) { |
| 364 |
properties.put(name, new Property(name)); |
| 365 |
} |
| 366 |
return properties.get(name); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
T getTypeWithProperty(String field, T type) { |
| 371 |
return typeSystem.getTypeWithProperty(field, type); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
private abstract class AbstractScopingCallback implements ScopedCallback { |
| 376 |
protected final Stack<StaticScope<T>> scopes = |
| 377 |
new Stack<StaticScope<T>>(); |
| 378 |
|
| 379 |
@Override |
| 380 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public void enterScope(NodeTraversal t) { |
| 386 |
if (t.inGlobalScope()) { |
| 387 |
scopes.push(typeSystem.getRootScope()); |
| 388 |
} else { |
| 389 |
scopes.push(typeSystem.getFunctionScope(t.getScopeRoot())); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
@Override |
| 394 |
public void exitScope(NodeTraversal t) { |
| 395 |
scopes.pop(); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
protected StaticScope<T> getScope() { |
| 400 |
return scopes.peek(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
private class FindExternProperties extends AbstractScopingCallback { |
| 409 |
@Override public void visit(NodeTraversal t, Node n, Node parent) { |
| 410 |
|
| 411 |
if (n.isGetProp()) { |
| 412 |
String field = n.getLastChild().getString(); |
| 413 |
T type = typeSystem.getType(getScope(), n.getFirstChild(), field); |
| 414 |
Property prop = getProperty(field); |
| 415 |
if (typeSystem.isInvalidatingType(type)) { |
| 416 |
prop.invalidate(); |
| 417 |
} else { |
| 418 |
prop.addTypeToSkip(type); |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
if ((type = typeSystem.getInstanceFromPrototype(type)) != null) { |
| 424 |
prop.getTypes().add(type); |
| 425 |
prop.typesToSkip.add(type); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
private class FindRenameableProperties extends AbstractScopingCallback { |
| 437 |
@Override |
| 438 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 439 |
if (n.isGetProp()) { |
| 440 |
handleGetProp(t, n); |
| 441 |
} else if (n.isObjectLit()) { |
| 442 |
handleObjectLit(t, n); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
private void handleGetProp(NodeTraversal t, Node n) { |
| 450 |
String name = n.getLastChild().getString(); |
| 451 |
T type = typeSystem.getType(getScope(), n.getFirstChild(), name); |
| 452 |
|
| 453 |
Property prop = getProperty(name); |
| 454 |
if (!prop.scheduleRenaming(n.getLastChild(), |
| 455 |
processProperty(t, prop, type, null))) { |
| 456 |
if (propertiesToErrorFor.containsKey(name)) { |
| 457 |
String suggestion = ""; |
| 458 |
if (type instanceof JSType) { |
| 459 |
JSType jsType = (JSType) type; |
| 460 |
if (jsType.isAllType() || jsType.isUnknownType()) { |
| 461 |
if (n.getFirstChild().isThis()) { |
| 462 |
suggestion = "The \"this\" object is unknown in the function,"+ |
| 463 |
"consider using @this"; |
| 464 |
} else { |
| 465 |
String qName = n.getFirstChild().getQualifiedName(); |
| 466 |
suggestion = "Consider casting " + qName + |
| 467 |
" if you know it's type."; |
| 468 |
} |
| 469 |
} else { |
| 470 |
List<String> errors = Lists.newArrayList(); |
| 471 |
printErrorLocations(errors, jsType); |
| 472 |
if (!errors.isEmpty()) { |
| 473 |
suggestion = "Consider fixing errors for the following types:\n"; |
| 474 |
suggestion += Joiner.on("\n").join(errors); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
compiler.report(JSError.make( |
| 479 |
t.getSourceName(), n, propertiesToErrorFor.get(name), |
| 480 |
Warnings.INVALIDATION, name, |
| 481 |
(type == null ? "null" : type.toString()), |
| 482 |
n.toString(), suggestion)); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
private void handleObjectLit(NodeTraversal t, Node n) { |
| 491 |
Node child = n.getFirstChild(); |
| 492 |
while (child != null) { |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
String name = child.getString(); |
| 497 |
T type = typeSystem.getType(getScope(), n, name); |
| 498 |
|
| 499 |
Property prop = getProperty(name); |
| 500 |
if (!prop.scheduleRenaming(child, |
| 501 |
processProperty(t, prop, type, null))) { |
| 502 |
|
| 503 |
|
| 504 |
if (propertiesToErrorFor.containsKey(name)) { |
| 505 |
compiler.report(JSError.make( |
| 506 |
t.getSourceName(), child, propertiesToErrorFor.get(name), |
| 507 |
Warnings.INVALIDATION, name, |
| 508 |
(type == null ? "null" : type.toString()), n.toString(), "")); |
| 509 |
} |
| 510 |
} |
| 511 |
child = child.getNext(); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
private void printErrorLocations(List<String> errors, JSType t) { |
| 516 |
if (!t.isObject() || t.isAllType()) { |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
if (t.isUnionType()) { |
| 521 |
for (JSType alt : t.toMaybeUnionType().getAlternates()) { |
| 522 |
printErrorLocations(errors, alt); |
| 523 |
} |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
for (JSError error : invalidationMap.get(t)) { |
| 528 |
if (errors.size() > MAX_INVALDIATION_WARNINGS_PER_PROPERTY) { |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
errors.add( |
| 533 |
t.toString() + " at " + error.sourceName + ":" + error.lineNumber); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
private T processProperty( |
| 545 |
NodeTraversal t, Property prop, T type, T relatedType) { |
| 546 |
type = typeSystem.restrictByNotNullOrUndefined(type); |
| 547 |
if (prop.skipRenaming || typeSystem.isInvalidatingType(type)) { |
| 548 |
return null; |
| 549 |
} |
| 550 |
|
| 551 |
Iterable<T> alternatives = typeSystem.getTypeAlternatives(type); |
| 552 |
if (alternatives != null) { |
| 553 |
T firstType = relatedType; |
| 554 |
for (T subType : alternatives) { |
| 555 |
T lastType = processProperty(t, prop, subType, firstType); |
| 556 |
if (lastType != null) { |
| 557 |
firstType = firstType == null ? lastType : firstType; |
| 558 |
} |
| 559 |
} |
| 560 |
return firstType; |
| 561 |
} else { |
| 562 |
T topType = typeSystem.getTypeWithProperty(prop.name, type); |
| 563 |
if (typeSystem.isInvalidatingType(topType)) { |
| 564 |
return null; |
| 565 |
} |
| 566 |
prop.addType(type, topType, relatedType); |
| 567 |
return topType; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
void renameProperties() { |
| 574 |
int propsRenamed = 0, propsSkipped = 0, instancesRenamed = 0, |
| 575 |
instancesSkipped = 0, singleTypeProps = 0; |
| 576 |
|
| 577 |
Set<String> reported = Sets.newHashSet(); |
| 578 |
for (Property prop : properties.values()) { |
| 579 |
if (prop.shouldRename()) { |
| 580 |
Map<T, String> propNames = buildPropNames(prop.getTypes(), prop.name); |
| 581 |
|
| 582 |
++propsRenamed; |
| 583 |
prop.expandTypesToSkip(); |
| 584 |
for (Node node : prop.renameNodes) { |
| 585 |
T rootType = prop.rootTypes.get(node); |
| 586 |
if (prop.shouldRename(rootType)) { |
| 587 |
String newName = propNames.get(rootType); |
| 588 |
node.setString(newName); |
| 589 |
compiler.reportCodeChange(); |
| 590 |
++instancesRenamed; |
| 591 |
} else { |
| 592 |
++instancesSkipped; |
| 593 |
|
| 594 |
CheckLevel checkLevelForProp = propertiesToErrorFor.get(prop.name); |
| 595 |
if (checkLevelForProp != null && |
| 596 |
checkLevelForProp != CheckLevel.OFF && |
| 597 |
!reported.contains(prop.name)) { |
| 598 |
reported.add(prop.name); |
| 599 |
compiler.report(JSError.make( |
| 600 |
NodeUtil.getSourceName(node), node, |
| 601 |
checkLevelForProp, |
| 602 |
Warnings.INVALIDATION_ON_TYPE, prop.name, |
| 603 |
rootType.toString(), "")); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
} else { |
| 608 |
if (prop.skipRenaming) { |
| 609 |
++propsSkipped; |
| 610 |
} else { |
| 611 |
++singleTypeProps; |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
logger.fine("Renamed " + instancesRenamed + " instances of " |
| 616 |
+ propsRenamed + " properties."); |
| 617 |
logger.fine("Skipped renaming " + instancesSkipped + " invalidated " |
| 618 |
+ "properties, " + propsSkipped + " instances of properties " |
| 619 |
+ "that were skipped for specific types and " + singleTypeProps |
| 620 |
+ " properties that were referenced from only one type."); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
private Map<T, String> buildPropNames(UnionFind<T> types, String name) { |
| 628 |
Map<T, String> names = Maps.newHashMap(); |
| 629 |
for (Set<T> set : types.allEquivalenceClasses()) { |
| 630 |
checkState(!set.isEmpty()); |
| 631 |
|
| 632 |
String typeName = null; |
| 633 |
for (T type : set) { |
| 634 |
if (typeName == null || type.toString().compareTo(typeName) < 0) { |
| 635 |
typeName = type.toString(); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
String newName; |
| 640 |
if ("{...}".equals(typeName)) { |
| 641 |
newName = name; |
| 642 |
} else { |
| 643 |
newName = typeName.replaceAll("[^\\w$]", "_") + "$" + name; |
| 644 |
} |
| 645 |
|
| 646 |
for (T type : set) { |
| 647 |
names.put(type, newName); |
| 648 |
} |
| 649 |
} |
| 650 |
return names; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
Multimap<String, Collection<T>> getRenamedTypesForTesting() { |
| 655 |
Multimap<String, Collection<T>> ret = HashMultimap.create(); |
| 656 |
for (Map.Entry<String, Property> entry: properties.entrySet()) { |
| 657 |
Property prop = entry.getValue(); |
| 658 |
if (!prop.skipRenaming) { |
| 659 |
for (Collection<T> c : prop.getTypes().allEquivalenceClasses()) { |
| 660 |
if (!c.isEmpty() && !prop.typesToSkip.contains(c.iterator().next())) { |
| 661 |
ret.put(entry.getKey(), c); |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
return ret; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
private interface TypeSystem<T> { |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
StaticScope<T> getRootScope(); |
| 676 |
|
| 677 |
|
| 678 |
StaticScope<T> getFunctionScope(Node node); |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
T getType(StaticScope<T> scope, Node node, String prop); |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
boolean isInvalidatingType(T type); |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
void addInvalidatingType(JSType type); |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
ImmutableSet<T> getTypesToSkipForType(T type); |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
boolean isTypeToSkip(T type); |
| 716 |
|
| 717 |
|
| 718 |
T restrictByNotNullOrUndefined(T type); |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
Iterable<T> getTypeAlternatives(T type); |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
T getTypeWithProperty(String field, T type); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
T getInstanceFromPrototype(T type); |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
void recordInterfaces(T type, T relatedType, |
| 744 |
DisambiguateProperties<T>.Property p); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
private static class JSTypeSystem implements TypeSystem<JSType> { |
| 749 |
private final Set<JSType> invalidatingTypes; |
| 750 |
private JSTypeRegistry registry; |
| 751 |
|
| 752 |
public JSTypeSystem(AbstractCompiler compiler) { |
| 753 |
registry = compiler.getTypeRegistry(); |
| 754 |
invalidatingTypes = Sets.newHashSet( |
| 755 |
registry.getNativeType(JSTypeNative.ALL_TYPE), |
| 756 |
registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE), |
| 757 |
registry.getNativeType(JSTypeNative.NO_TYPE), |
| 758 |
registry.getNativeType(JSTypeNative.FUNCTION_PROTOTYPE), |
| 759 |
registry.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE), |
| 760 |
registry.getNativeType(JSTypeNative.OBJECT_PROTOTYPE), |
| 761 |
registry.getNativeType(JSTypeNative.TOP_LEVEL_PROTOTYPE), |
| 762 |
registry.getNativeType(JSTypeNative.UNKNOWN_TYPE)); |
| 763 |
|
| 764 |
} |
| 765 |
|
| 766 |
@Override public void addInvalidatingType(JSType type) { |
| 767 |
checkState(!type.isUnionType()); |
| 768 |
invalidatingTypes.add(type); |
| 769 |
} |
| 770 |
|
| 771 |
@Override public StaticScope<JSType> getRootScope() { return null; } |
| 772 |
|
| 773 |
@Override public StaticScope<JSType> getFunctionScope(Node node) { |
| 774 |
return null; |
| 775 |
} |
| 776 |
|
| 777 |
@Override public JSType getType( |
| 778 |
StaticScope<JSType> scope, Node node, String prop) { |
| 779 |
if (node.getJSType() == null) { |
| 780 |
return registry.getNativeType(JSTypeNative.UNKNOWN_TYPE); |
| 781 |
} |
| 782 |
return node.getJSType(); |
| 783 |
} |
| 784 |
|
| 785 |
@Override public boolean isInvalidatingType(JSType type) { |
| 786 |
if (type == null || invalidatingTypes.contains(type) || |
| 787 |
type.isUnknownType() ) { |
| 788 |
return true; |
| 789 |
} |
| 790 |
|
| 791 |
ObjectType objType = ObjectType.cast(type); |
| 792 |
return objType != null && !objType.hasReferenceName(); |
| 793 |
} |
| 794 |
|
| 795 |
@Override public ImmutableSet<JSType> getTypesToSkipForType(JSType type) { |
| 796 |
type = type.restrictByNotNullOrUndefined(); |
| 797 |
if (type.isUnionType()) { |
| 798 |
Set<JSType> types = Sets.newHashSet(type); |
| 799 |
for (JSType alt : type.toMaybeUnionType().getAlternates()) { |
| 800 |
types.addAll(getTypesToSkipForTypeNonUnion(alt)); |
| 801 |
} |
| 802 |
return ImmutableSet.copyOf(types); |
| 803 |
} else if (type.isEnumElementType()) { |
| 804 |
return getTypesToSkipForType( |
| 805 |
type.toMaybeEnumElementType().getPrimitiveType()); |
| 806 |
} |
| 807 |
return ImmutableSet.copyOf(getTypesToSkipForTypeNonUnion(type)); |
| 808 |
} |
| 809 |
|
| 810 |
private Set<JSType> getTypesToSkipForTypeNonUnion(JSType type) { |
| 811 |
Set<JSType> types = Sets.newHashSet(); |
| 812 |
JSType skipType = type; |
| 813 |
while (skipType != null) { |
| 814 |
types.add(skipType); |
| 815 |
|
| 816 |
ObjectType objSkipType = skipType.toObjectType(); |
| 817 |
if (objSkipType != null) { |
| 818 |
skipType = objSkipType.getImplicitPrototype(); |
| 819 |
} else { |
| 820 |
break; |
| 821 |
} |
| 822 |
} |
| 823 |
return types; |
| 824 |
} |
| 825 |
|
| 826 |
@Override public boolean isTypeToSkip(JSType type) { |
| 827 |
return type.isEnumType() || (type.autoboxesTo() != null); |
| 828 |
} |
| 829 |
|
| 830 |
@Override public JSType restrictByNotNullOrUndefined(JSType type) { |
| 831 |
return type.restrictByNotNullOrUndefined(); |
| 832 |
} |
| 833 |
|
| 834 |
@Override public Iterable<JSType> getTypeAlternatives(JSType type) { |
| 835 |
if (type.isUnionType()) { |
| 836 |
return type.toMaybeUnionType().getAlternates(); |
| 837 |
} else { |
| 838 |
ObjectType objType = type.toObjectType(); |
| 839 |
if (objType != null && |
| 840 |
objType.getConstructor() != null && |
| 841 |
objType.getConstructor().isInterface()) { |
| 842 |
List<JSType> list = Lists.newArrayList(); |
| 843 |
for (FunctionType impl |
| 844 |
: registry.getDirectImplementors(objType)) { |
| 845 |
list.add(impl.getInstanceType()); |
| 846 |
} |
| 847 |
return list; |
| 848 |
} else { |
| 849 |
return null; |
| 850 |
} |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
@Override public ObjectType getTypeWithProperty(String field, JSType type) { |
| 855 |
if (type == null) { |
| 856 |
return null; |
| 857 |
} |
| 858 |
|
| 859 |
if (type.isEnumElementType()) { |
| 860 |
return getTypeWithProperty( |
| 861 |
field, type.toMaybeEnumElementType().getPrimitiveType()); |
| 862 |
} |
| 863 |
|
| 864 |
if (!(type instanceof ObjectType)) { |
| 865 |
if (type.autoboxesTo() != null) { |
| 866 |
type = type.autoboxesTo(); |
| 867 |
} else { |
| 868 |
return null; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
if ("prototype".equals(field)) { |
| 874 |
return null; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
ObjectType foundType = null; |
| 881 |
ObjectType objType = ObjectType.cast(type); |
| 882 |
if (objType != null && objType.getConstructor() != null |
| 883 |
&& objType.getConstructor().isInterface()) { |
| 884 |
ObjectType topInterface = FunctionType.getTopDefiningInterface( |
| 885 |
objType, field); |
| 886 |
if (topInterface != null && topInterface.getConstructor() != null) { |
| 887 |
foundType = topInterface.getConstructor().getPrototype(); |
| 888 |
} |
| 889 |
} else { |
| 890 |
while (objType != null && objType.getImplicitPrototype() != objType) { |
| 891 |
if (objType.hasOwnProperty(field)) { |
| 892 |
foundType = objType; |
| 893 |
} |
| 894 |
objType = objType.getImplicitPrototype(); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
if (foundType == null) { |
| 901 |
ObjectType maybeType = ObjectType.cast( |
| 902 |
registry.getGreatestSubtypeWithProperty(type, field)); |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
if (maybeType != null && maybeType.hasOwnProperty(field)) { |
| 907 |
foundType = maybeType; |
| 908 |
} |
| 909 |
} |
| 910 |
return foundType; |
| 911 |
} |
| 912 |
|
| 913 |
@Override public JSType getInstanceFromPrototype(JSType type) { |
| 914 |
if (type.isFunctionPrototypeType()) { |
| 915 |
ObjectType prototype = (ObjectType) type; |
| 916 |
FunctionType owner = prototype.getOwnerFunction(); |
| 917 |
if (owner.isConstructor() || owner.isInterface()) { |
| 918 |
return prototype.getOwnerFunction().getInstanceType(); |
| 919 |
} |
| 920 |
} |
| 921 |
return null; |
| 922 |
} |
| 923 |
|
| 924 |
@Override |
| 925 |
public void recordInterfaces(JSType type, JSType relatedType, |
| 926 |
DisambiguateProperties<JSType>.Property p) { |
| 927 |
ObjectType objType = ObjectType.cast(type); |
| 928 |
if (objType != null) { |
| 929 |
FunctionType constructor; |
| 930 |
if (objType.isFunctionType()) { |
| 931 |
constructor = objType.toMaybeFunctionType(); |
| 932 |
} else if (objType.isFunctionPrototypeType()) { |
| 933 |
constructor = objType.getOwnerFunction(); |
| 934 |
} else { |
| 935 |
constructor = objType.getConstructor(); |
| 936 |
} |
| 937 |
while (constructor != null) { |
| 938 |
for (ObjectType itype : constructor.getImplementedInterfaces()) { |
| 939 |
JSType top = getTypeWithProperty(p.name, itype); |
| 940 |
if (top != null) { |
| 941 |
p.addType(itype, top, relatedType); |
| 942 |
} else { |
| 943 |
recordInterfaces(itype, relatedType, p); |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
if (p.skipRenaming) return; |
| 948 |
} |
| 949 |
if (constructor.isInterface() || constructor.isConstructor()) { |
| 950 |
constructor = constructor.getSuperClassConstructor(); |
| 951 |
} else { |
| 952 |
constructor = null; |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
private static class ConcreteTypeSystem implements TypeSystem<ConcreteType> { |
| 961 |
private final TightenTypes tt; |
| 962 |
private int nextUniqueId; |
| 963 |
private CodingConvention codingConvention; |
| 964 |
private final Set<JSType> invalidatingTypes = Sets.newHashSet(); |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
private static final JSTypeNative [] nativeTypes = new JSTypeNative[] { |
| 969 |
JSTypeNative.BOOLEAN_OBJECT_TYPE, |
| 970 |
JSTypeNative.NUMBER_OBJECT_TYPE, |
| 971 |
JSTypeNative.STRING_OBJECT_TYPE |
| 972 |
}; |
| 973 |
|
| 974 |
public ConcreteTypeSystem(TightenTypes tt, CodingConvention convention) { |
| 975 |
this.tt = tt; |
| 976 |
this.codingConvention = convention; |
| 977 |
} |
| 978 |
|
| 979 |
@Override public void addInvalidatingType(JSType type) { |
| 980 |
checkState(!type.isUnionType()); |
| 981 |
invalidatingTypes.add(type); |
| 982 |
} |
| 983 |
|
| 984 |
@Override public StaticScope<ConcreteType> getRootScope() { |
| 985 |
return tt.getTopScope(); |
| 986 |
} |
| 987 |
|
| 988 |
@Override public StaticScope<ConcreteType> getFunctionScope(Node decl) { |
| 989 |
ConcreteFunctionType func = tt.getConcreteFunction(decl); |
| 990 |
return (func != null) ? |
| 991 |
func.getScope() : (StaticScope<ConcreteType>) null; |
| 992 |
} |
| 993 |
|
| 994 |
@Override |
| 995 |
public ConcreteType getType( |
| 996 |
StaticScope<ConcreteType> scope, Node node, String prop) { |
| 997 |
if (scope != null) { |
| 998 |
ConcreteType c = tt.inferConcreteType( |
| 999 |
(TightenTypes.ConcreteScope) scope, node); |
| 1000 |
return maybeAddAutoboxes(c, node, prop); |
| 1001 |
} else { |
| 1002 |
return null; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
private ConcreteType maybeAddAutoboxes( |
| 1012 |
ConcreteType cType, Node node, String prop) { |
| 1013 |
JSType jsType = node.getJSType(); |
| 1014 |
if (jsType == null) { |
| 1015 |
return cType; |
| 1016 |
} else if (jsType.isUnknownType()) { |
| 1017 |
for (JSTypeNative nativeType : nativeTypes) { |
| 1018 |
ConcreteType concrete = tt.getConcreteInstance( |
| 1019 |
tt.getTypeRegistry().getNativeObjectType(nativeType)); |
| 1020 |
if (concrete != null && !concrete.getPropertyType(prop).isNone()) { |
| 1021 |
cType = cType.unionWith(concrete); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
return cType; |
| 1025 |
} |
| 1026 |
|
| 1027 |
return maybeAddAutoboxes(cType, jsType, prop); |
| 1028 |
} |
| 1029 |
|
| 1030 |
private ConcreteType maybeAddAutoboxes( |
| 1031 |
ConcreteType cType, JSType jsType, String prop) { |
| 1032 |
jsType = jsType.restrictByNotNullOrUndefined(); |
| 1033 |
if (jsType.isUnionType()) { |
| 1034 |
for (JSType alt : jsType.toMaybeUnionType().getAlternates()) { |
| 1035 |
cType = maybeAddAutoboxes(cType, alt, prop); |
| 1036 |
} |
| 1037 |
return cType; |
| 1038 |
} else if (jsType.isEnumElementType()) { |
| 1039 |
return maybeAddAutoboxes( |
| 1040 |
cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop); |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (jsType.autoboxesTo() != null) { |
| 1044 |
JSType autoboxed = jsType.autoboxesTo(); |
| 1045 |
return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed)); |
| 1046 |
} else if (jsType.unboxesTo() != null) { |
| 1047 |
return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType)); |
| 1048 |
} |
| 1049 |
|
| 1050 |
return cType; |
| 1051 |
} |
| 1052 |
|
| 1053 |
@Override public boolean isInvalidatingType(ConcreteType type) { |
| 1054 |
|
| 1055 |
|
| 1056 |
return (type == null) || type.isAll() || type.isFunction() |
| 1057 |
|| (type.isInstance() |
| 1058 |
&& invalidatingTypes.contains(type.toInstance().instanceType)); |
| 1059 |
} |
| 1060 |
|
| 1061 |
@Override |
| 1062 |
public ImmutableSet<ConcreteType> getTypesToSkipForType(ConcreteType type) { |
| 1063 |
return ImmutableSet.of(type); |
| 1064 |
} |
| 1065 |
|
| 1066 |
@Override public boolean isTypeToSkip(ConcreteType type) { |
| 1067 |
|
| 1068 |
return type.isInstance() |
| 1069 |
&& !(type.toInstance().isFunctionPrototype() |
| 1070 |
|| type.toInstance().instanceType.isInstanceType()); |
| 1071 |
} |
| 1072 |
|
| 1073 |
@Override |
| 1074 |
public ConcreteType restrictByNotNullOrUndefined(ConcreteType type) { |
| 1075 |
|
| 1076 |
return type; |
| 1077 |
} |
| 1078 |
|
| 1079 |
@Override |
| 1080 |
public Iterable<ConcreteType> getTypeAlternatives(ConcreteType type) { |
| 1081 |
if (type.isUnion()) { |
| 1082 |
return ((ConcreteUnionType) type).getAlternatives(); |
| 1083 |
} else { |
| 1084 |
return null; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
@Override public ConcreteType getTypeWithProperty(String field, |
| 1089 |
ConcreteType type) { |
| 1090 |
if (type.isInstance()) { |
| 1091 |
ConcreteInstanceType instanceType = (ConcreteInstanceType) type; |
| 1092 |
return instanceType.getInstanceTypeWithProperty(field); |
| 1093 |
} else if (type.isFunction()) { |
| 1094 |
if ("prototype".equals(field) |
| 1095 |
|| codingConvention.isSuperClassReference(field)) { |
| 1096 |
return type; |
| 1097 |
} |
| 1098 |
} else if (type.isNone()) { |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
return new ConcreteUniqueType(++nextUniqueId); |
| 1103 |
} else if (type.isUnion()) { |
| 1104 |
|
| 1105 |
for (ConcreteType t : ((ConcreteUnionType) type).getAlternatives()) { |
| 1106 |
ConcreteType ret = getTypeWithProperty(field, t); |
| 1107 |
if (ret != null) { |
| 1108 |
return ret; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
} |
| 1112 |
return null; |
| 1113 |
} |
| 1114 |
|
| 1115 |
@Override public ConcreteType getInstanceFromPrototype(ConcreteType type) { |
| 1116 |
if (type.isInstance()) { |
| 1117 |
ConcreteInstanceType instanceType = (ConcreteInstanceType) type; |
| 1118 |
if (instanceType.isFunctionPrototype()) { |
| 1119 |
return instanceType.getConstructorType().getInstanceType(); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
return null; |
| 1123 |
} |
| 1124 |
|
| 1125 |
@Override |
| 1126 |
public void recordInterfaces(ConcreteType type, ConcreteType relatedType, |
| 1127 |
DisambiguateProperties<ConcreteType>.Property p) { |
| 1128 |
|
| 1129 |
} |
| 1130 |
} |
| 1131 |
} |