| 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.ImmutableMap; |
| 21 |
import com.google.common.collect.Maps; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.Scope.Var; |
| 24 |
import com.google.javascript.jscomp.type.FlowScope; |
| 25 |
import com.google.javascript.rhino.Node; |
| 26 |
import com.google.javascript.rhino.jstype.JSType; |
| 27 |
import com.google.javascript.rhino.jstype.SimpleSlot; |
| 28 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 29 |
import com.google.javascript.rhino.jstype.StaticSlot; |
| 30 |
|
| 31 |
import java.util.Iterator; |
| 32 |
import java.util.Map; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class LinkedFlowScope implements FlowScope { |
| 42 |
|
| 43 |
private final FlatFlowScopeCache cache; |
| 44 |
|
| 45 |
|
| 46 |
private final LinkedFlowScope parent; |
| 47 |
|
| 48 |
|
| 49 |
private int depth; |
| 50 |
|
| 51 |
static final int MAX_DEPTH = 250; |
| 52 |
|
| 53 |
|
| 54 |
private FlatFlowScopeCache flattened; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private boolean frozen = false; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private LinkedFlowSlot lastSlot; |
| 63 |
|
| 64 |
private LinkedFlowScope(FlatFlowScopeCache cache, |
| 65 |
LinkedFlowScope directParent) { |
| 66 |
this.cache = cache; |
| 67 |
if (directParent == null) { |
| 68 |
this.lastSlot = null; |
| 69 |
this.depth = 0; |
| 70 |
this.parent = cache.linkedEquivalent; |
| 71 |
} else { |
| 72 |
this.lastSlot = directParent.lastSlot; |
| 73 |
this.depth = directParent.depth + 1; |
| 74 |
this.parent = directParent; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
LinkedFlowScope(FlatFlowScopeCache cache) { |
| 79 |
this(cache, null); |
| 80 |
} |
| 81 |
|
| 82 |
LinkedFlowScope(LinkedFlowScope directParent) { |
| 83 |
this(directParent.cache, directParent); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
private Scope getFunctionScope() { |
| 88 |
return cache.functionScope; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
private boolean flowsFromBottom() { |
| 93 |
return getFunctionScope().isBottom(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static LinkedFlowScope createEntryLattice(Scope scope) { |
| 100 |
return new LinkedFlowScope(new FlatFlowScopeCache(scope)); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void inferSlotType(String symbol, JSType type) { |
| 105 |
Preconditions.checkState(!frozen); |
| 106 |
lastSlot = new LinkedFlowSlot(symbol, type, lastSlot); |
| 107 |
depth++; |
| 108 |
cache.dirtySymbols.add(symbol); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void inferQualifiedSlot(Node node, String symbol, JSType bottomType, |
| 113 |
JSType inferredType) { |
| 114 |
Scope functionScope = getFunctionScope(); |
| 115 |
if (functionScope.isLocal()) { |
| 116 |
if (functionScope.getVar(symbol) == null && !functionScope.isBottom()) { |
| 117 |
functionScope.declare(symbol, node, bottomType, null); |
| 118 |
} |
| 119 |
|
| 120 |
inferSlotType(symbol, inferredType); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public JSType getTypeOfThis() { |
| 126 |
return cache.functionScope.getTypeOfThis(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public Node getRootNode() { |
| 131 |
return getFunctionScope().getRootNode(); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public StaticScope<JSType> getParentScope() { |
| 136 |
return getFunctionScope().getParentScope(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public StaticSlot<JSType> getSlot(String name) { |
| 144 |
if (cache.dirtySymbols.contains(name)) { |
| 145 |
for (LinkedFlowSlot slot = lastSlot; |
| 146 |
slot != null; slot = slot.parent) { |
| 147 |
if (slot.getName().equals(name)) { |
| 148 |
return slot; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
return cache.getSlot(name); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public StaticSlot<JSType> getOwnSlot(String name) { |
| 157 |
throw new UnsupportedOperationException(); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public FlowScope createChildFlowScope() { |
| 162 |
frozen = true; |
| 163 |
|
| 164 |
if (depth > MAX_DEPTH) { |
| 165 |
if (flattened == null) { |
| 166 |
flattened = new FlatFlowScopeCache(this); |
| 167 |
} |
| 168 |
return new LinkedFlowScope(flattened); |
| 169 |
} |
| 170 |
|
| 171 |
return new LinkedFlowScope(this); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@Override |
| 180 |
public StaticSlot<JSType> findUniqueRefinedSlot(FlowScope blindScope) { |
| 181 |
StaticSlot<JSType> result = null; |
| 182 |
|
| 183 |
for (LinkedFlowScope currentScope = this; |
| 184 |
currentScope != blindScope; |
| 185 |
currentScope = currentScope.parent) { |
| 186 |
for (LinkedFlowSlot currentSlot = currentScope.lastSlot; |
| 187 |
currentSlot != null && |
| 188 |
(currentScope.parent == null || |
| 189 |
currentScope.parent.lastSlot != currentSlot); |
| 190 |
currentSlot = currentSlot.parent) { |
| 191 |
if (result == null) { |
| 192 |
result = currentSlot; |
| 193 |
} else if (!currentSlot.getName().equals(result.getName())) { |
| 194 |
return null; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return result; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
public void completeScope(StaticScope<JSType> staticScope) { |
| 209 |
Scope scope = (Scope) staticScope; |
| 210 |
for (Iterator<Var> it = scope.getVars(); it.hasNext();) { |
| 211 |
Var var = it.next(); |
| 212 |
if (var.isTypeInferred()) { |
| 213 |
JSType type = var.getType(); |
| 214 |
if (type == null || type.isUnknownType()) { |
| 215 |
JSType flowType = getSlot(var.getName()).getType(); |
| 216 |
var.setType(flowType); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
@Override |
| 232 |
public LinkedFlowScope optimize() { |
| 233 |
LinkedFlowScope current; |
| 234 |
for (current = this; |
| 235 |
current.parent != null && |
| 236 |
current.lastSlot == current.parent.lastSlot; |
| 237 |
current = current.parent) {} |
| 238 |
return current; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
static class FlowScopeJoinOp extends JoinOp.BinaryJoinOp<FlowScope> { |
| 243 |
@SuppressWarnings("unchecked") |
| 244 |
@Override |
| 245 |
public FlowScope apply(FlowScope a, FlowScope b) { |
| 246 |
|
| 247 |
LinkedFlowScope linkedA = (LinkedFlowScope) a; |
| 248 |
LinkedFlowScope linkedB = (LinkedFlowScope) b; |
| 249 |
linkedA.frozen = true; |
| 250 |
linkedB.frozen = true; |
| 251 |
if (linkedA.optimize() == linkedB.optimize()) { |
| 252 |
return linkedA.createChildFlowScope(); |
| 253 |
} |
| 254 |
return new LinkedFlowScope(new FlatFlowScopeCache(linkedA, linkedB)); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public boolean equals(Object other) { |
| 260 |
if (other instanceof LinkedFlowScope) { |
| 261 |
LinkedFlowScope that = (LinkedFlowScope) other; |
| 262 |
if (this.optimize() == that.optimize()) { |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (this.getFunctionScope() != that.getFunctionScope()) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
if (cache == that.cache) { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
for (String name : cache.dirtySymbols) { |
| 283 |
if (diffSlots(getSlot(name), that.getSlot(name))) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
Map<String, StaticSlot<JSType>> myFlowSlots = allFlowSlots(); |
| 292 |
Map<String, StaticSlot<JSType>> otherFlowSlots = that.allFlowSlots(); |
| 293 |
|
| 294 |
for (StaticSlot<JSType> slot : myFlowSlots.values()) { |
| 295 |
if (diffSlots(slot, otherFlowSlots.get(slot.getName()))) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
otherFlowSlots.remove(slot.getName()); |
| 299 |
} |
| 300 |
for (StaticSlot<JSType> slot : otherFlowSlots.values()) { |
| 301 |
if (diffSlots(slot, myFlowSlots.get(slot.getName()))) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
} |
| 305 |
return true; |
| 306 |
} |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
private boolean diffSlots(StaticSlot<JSType> slotA, |
| 315 |
StaticSlot<JSType> slotB) { |
| 316 |
boolean aIsNull = slotA == null || slotA.getType() == null; |
| 317 |
boolean bIsNull = slotB == null || slotB.getType() == null; |
| 318 |
if (aIsNull && bIsNull) { |
| 319 |
return false; |
| 320 |
} else if (aIsNull ^ bIsNull) { |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
return slotA.getType().differsFrom(slotB.getType()); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private Map<String, StaticSlot<JSType>> allFlowSlots() { |
| 347 |
Map<String, StaticSlot<JSType>> slots = Maps.newHashMap(); |
| 348 |
for (LinkedFlowSlot slot = lastSlot; |
| 349 |
slot != null; slot = slot.parent) { |
| 350 |
if (!slots.containsKey(slot.getName())) { |
| 351 |
slots.put(slot.getName(), slot); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
for (Map.Entry<String, StaticSlot<JSType>> symbolEntry : cache.symbols.entrySet()) { |
| 356 |
if (!slots.containsKey(symbolEntry.getKey())) { |
| 357 |
slots.put(symbolEntry.getKey(), symbolEntry.getValue()); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return slots; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private static class LinkedFlowSlot extends SimpleSlot { |
| 368 |
final LinkedFlowSlot parent; |
| 369 |
|
| 370 |
LinkedFlowSlot(String name, JSType type, LinkedFlowSlot parent) { |
| 371 |
super(name, type, true); |
| 372 |
this.parent = parent; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private static class FlatFlowScopeCache { |
| 381 |
|
| 382 |
private final Scope functionScope; |
| 383 |
|
| 384 |
|
| 385 |
private final LinkedFlowScope linkedEquivalent; |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
private Map<String, StaticSlot<JSType>> symbols = Maps.newHashMap(); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
final Set<String> dirtySymbols = Sets.newHashSet(); |
| 400 |
|
| 401 |
|
| 402 |
FlatFlowScopeCache(Scope functionScope) { |
| 403 |
this.functionScope = functionScope; |
| 404 |
symbols = ImmutableMap.of(); |
| 405 |
linkedEquivalent = null; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
FlatFlowScopeCache(LinkedFlowScope directParent) { |
| 410 |
FlatFlowScopeCache cache = directParent.cache; |
| 411 |
|
| 412 |
functionScope = cache.functionScope; |
| 413 |
symbols = directParent.allFlowSlots(); |
| 414 |
linkedEquivalent = directParent; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
FlatFlowScopeCache(LinkedFlowScope joinedScopeA, |
| 419 |
LinkedFlowScope joinedScopeB) { |
| 420 |
linkedEquivalent = null; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
functionScope = joinedScopeA.flowsFromBottom() ? |
| 425 |
joinedScopeB.getFunctionScope() : joinedScopeA.getFunctionScope(); |
| 426 |
|
| 427 |
Map<String, StaticSlot<JSType>> slotsA = joinedScopeA.allFlowSlots(); |
| 428 |
Map<String, StaticSlot<JSType>> slotsB = joinedScopeB.allFlowSlots(); |
| 429 |
|
| 430 |
symbols = slotsA; |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
Set<String> symbolNames = Sets.newHashSet(symbols.keySet()); |
| 444 |
symbolNames.addAll(slotsB.keySet()); |
| 445 |
|
| 446 |
for (String name : symbolNames) { |
| 447 |
StaticSlot<JSType> slotA = slotsA.get(name); |
| 448 |
StaticSlot<JSType> slotB = slotsB.get(name); |
| 449 |
|
| 450 |
JSType joinedType = null; |
| 451 |
if (slotB == null || slotB.getType() == null) { |
| 452 |
StaticSlot<JSType> fnSlot |
| 453 |
= joinedScopeB.getFunctionScope().getSlot(name); |
| 454 |
JSType fnSlotType = fnSlot == null ? null : fnSlot.getType(); |
| 455 |
if (fnSlotType == null) { |
| 456 |
|
| 457 |
} else { |
| 458 |
|
| 459 |
joinedType = slotA.getType().getLeastSupertype(fnSlotType); |
| 460 |
} |
| 461 |
} else if (slotA == null || slotA.getType() == null) { |
| 462 |
StaticSlot<JSType> fnSlot |
| 463 |
= joinedScopeA.getFunctionScope().getSlot(name); |
| 464 |
JSType fnSlotType = fnSlot == null ? null : fnSlot.getType(); |
| 465 |
if (fnSlotType == null) { |
| 466 |
|
| 467 |
symbols.put(name, slotB); |
| 468 |
} else { |
| 469 |
|
| 470 |
joinedType = slotB.getType().getLeastSupertype(fnSlotType); |
| 471 |
} |
| 472 |
} else { |
| 473 |
|
| 474 |
joinedType = |
| 475 |
slotA.getType().getLeastSupertype(slotB.getType()); |
| 476 |
} |
| 477 |
|
| 478 |
if (joinedType != null) { |
| 479 |
symbols.put(name, new SimpleSlot(name, joinedType, true)); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
public StaticSlot<JSType> getSlot(String name) { |
| 488 |
if (symbols.containsKey(name)) { |
| 489 |
return symbols.get(name); |
| 490 |
} else { |
| 491 |
return functionScope.getSlot(name); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
} |