| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.google.javascript.jscomp; |
| 17 |
|
| 18 |
import com.google.common.base.Joiner; |
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.jscomp.graph.LatticeElement; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 25 |
import com.google.javascript.rhino.jstype.JSType; |
| 26 |
import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 27 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 28 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 29 |
import com.google.javascript.rhino.jstype.StaticSlot; |
| 30 |
import com.google.javascript.rhino.jstype.UnknownType; |
| 31 |
|
| 32 |
import java.util.Collection; |
| 33 |
import java.util.Collections; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Set; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
abstract class ConcreteType implements LatticeElement { |
| 48 |
|
| 49 |
static final ConcreteType NONE = new ConcreteNoneType(); |
| 50 |
|
| 51 |
|
| 52 |
static final ConcreteType ALL = new ConcreteAll(); |
| 53 |
|
| 54 |
|
| 55 |
private static final List<ConcreteFunctionType> NO_FUNCTIONS = |
| 56 |
Lists.<ConcreteFunctionType>newArrayList(); |
| 57 |
|
| 58 |
|
| 59 |
private static final List<ConcreteInstanceType> NO_INSTANCES = |
| 60 |
Lists.<ConcreteInstanceType>newArrayList(); |
| 61 |
|
| 62 |
|
| 63 |
private static final List<StaticSlot<ConcreteType>> NO_SLOTS = |
| 64 |
Lists.<StaticSlot<ConcreteType>>newArrayList(); |
| 65 |
|
| 66 |
protected static ConcreteType createForTypes(Collection<ConcreteType> types) { |
| 67 |
if (types == null || types.size() == 0) { |
| 68 |
return NONE; |
| 69 |
} else if (types.size() == 1) { |
| 70 |
return types.iterator().next(); |
| 71 |
} else { |
| 72 |
return new ConcreteUnionType(Sets.newHashSet(types)); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
boolean isNone() { return false; } |
| 78 |
|
| 79 |
|
| 80 |
boolean isFunction() { return false; } |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
boolean isInstance() { return false; } |
| 87 |
|
| 88 |
|
| 89 |
boolean isUnion() { return false; } |
| 90 |
|
| 91 |
|
| 92 |
boolean isAll() { return false; } |
| 93 |
|
| 94 |
|
| 95 |
boolean isSingleton() { return !isNone() && !isUnion() && !isAll(); } |
| 96 |
|
| 97 |
|
| 98 |
ConcreteFunctionType toFunction() { return null; } |
| 99 |
|
| 100 |
|
| 101 |
ConcreteInstanceType toInstance() { return null; } |
| 102 |
|
| 103 |
|
| 104 |
ConcreteUnionType toUnion() { return null; } |
| 105 |
|
| 106 |
|
| 107 |
StaticScope<ConcreteType> getScope() { return null; } |
| 108 |
|
| 109 |
|
| 110 |
ConcreteType unionWith(ConcreteType other) { |
| 111 |
Preconditions.checkState(this.isSingleton()); |
| 112 |
if (!other.isSingleton()) { |
| 113 |
return other.unionWith(this); |
| 114 |
} else if (equals(other)) { |
| 115 |
return this; |
| 116 |
} else { |
| 117 |
return new ConcreteUnionType(this, other); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
ConcreteType intersectWith(ConcreteType other) { |
| 123 |
if (!other.isSingleton()) { |
| 124 |
return other.intersectWith(this); |
| 125 |
} else if (equals(other)) { |
| 126 |
return this; |
| 127 |
} else { |
| 128 |
return NONE; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private <C> List<C> getMatchingTypes(TypeFilter<C> filter) { |
| 137 |
C type = null; |
| 138 |
if (isUnion()) { |
| 139 |
List<C> list = Lists.newArrayList(); |
| 140 |
for (ConcreteType alt : toUnion().getAlternatives()) { |
| 141 |
if ((type = filter.filter(alt)) != null) { |
| 142 |
list.add(type); |
| 143 |
} |
| 144 |
} |
| 145 |
return list; |
| 146 |
} else if ((type = filter.filter(this)) != null) { |
| 147 |
List<C> list = Lists.newArrayList(); |
| 148 |
list.add(type); |
| 149 |
return list; |
| 150 |
} else { |
| 151 |
return filter.emptyList; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
abstract class TypeFilter<C> { |
| 161 |
|
| 162 |
final List<C> emptyList; |
| 163 |
|
| 164 |
TypeFilter(List<C> emptyList) { |
| 165 |
this.emptyList = emptyList; |
| 166 |
} |
| 167 |
|
| 168 |
abstract protected C filter(ConcreteType type); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
List<ConcreteFunctionType> getFunctions() { |
| 173 |
return getMatchingTypes(new TypeFilter<ConcreteFunctionType>(NO_FUNCTIONS) { |
| 174 |
@Override public ConcreteFunctionType filter(ConcreteType type) { |
| 175 |
return type.isFunction() ? type.toFunction() : null; |
| 176 |
} |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
List<ConcreteInstanceType> getInstances() { |
| 182 |
return getMatchingTypes(new TypeFilter<ConcreteInstanceType>(NO_INSTANCES) { |
| 183 |
@Override public ConcreteInstanceType filter(ConcreteType type) { |
| 184 |
return type.isInstance() ? type.toInstance() : null; |
| 185 |
} |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
List<ConcreteInstanceType> getFunctionInstanceTypes() { |
| 191 |
return getMatchingTypes(new TypeFilter<ConcreteInstanceType>(NO_INSTANCES) { |
| 192 |
@Override public ConcreteInstanceType filter(ConcreteType type) { |
| 193 |
if (type.isFunction()) { |
| 194 |
return type.toFunction().getInstanceType(); |
| 195 |
} |
| 196 |
return null; |
| 197 |
} |
| 198 |
}); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
List<ConcreteInstanceType> getPrototypeTypes() { |
| 203 |
return getMatchingTypes(new TypeFilter<ConcreteInstanceType>(NO_INSTANCES) { |
| 204 |
@Override public ConcreteInstanceType filter(ConcreteType type) { |
| 205 |
if (type.isInstance() |
| 206 |
&& type.toInstance().isFunctionPrototype()) { |
| 207 |
return type.toInstance(); |
| 208 |
} |
| 209 |
return null; |
| 210 |
} |
| 211 |
}); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
List<ConcreteFunctionType> getSuperclassTypes() { |
| 216 |
return getMatchingTypes(new TypeFilter<ConcreteFunctionType>(NO_FUNCTIONS) { |
| 217 |
@Override public ConcreteFunctionType filter(ConcreteType type) { |
| 218 |
return type.isFunction() |
| 219 |
&& type.toFunction().getSuperclassType() != null |
| 220 |
? type.toFunction().getSuperclassType() : null; |
| 221 |
} |
| 222 |
}); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
List<StaticSlot<ConcreteType>> getParameterSlots(final int index) { |
| 227 |
return getMatchingTypes(new TypeFilter<StaticSlot<ConcreteType>>(NO_SLOTS) { |
| 228 |
@Override public StaticSlot<ConcreteType> filter(ConcreteType type) { |
| 229 |
return type.isFunction() |
| 230 |
&& toFunction().getParameterSlot(index) != null |
| 231 |
? toFunction().getParameterSlot(index) : null; |
| 232 |
} |
| 233 |
}); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
List<StaticSlot<ConcreteType>> getPropertySlots(final String name) { |
| 241 |
return getMatchingTypes(new TypeFilter<StaticSlot<ConcreteType>>(NO_SLOTS) { |
| 242 |
@Override public StaticSlot<ConcreteType> filter(ConcreteType type) { |
| 243 |
StaticSlot<ConcreteType> slot = null; |
| 244 |
if (type.isInstance()) { |
| 245 |
slot = type.toInstance().getPropertySlot(name); |
| 246 |
} |
| 247 |
return slot; |
| 248 |
} |
| 249 |
}); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
ConcreteType getPropertyType(final String name) { |
| 258 |
ConcreteType ret = NONE; |
| 259 |
for (StaticSlot<ConcreteType> slot : getPropertySlots(name)) { |
| 260 |
ret = ret.unionWith(slot.getType()); |
| 261 |
} |
| 262 |
return ret; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
private static class ConcreteNoneType extends ConcreteType { |
| 267 |
@Override boolean isNone() { return true; } |
| 268 |
|
| 269 |
@Override ConcreteType unionWith(ConcreteType other) { return other; } |
| 270 |
|
| 271 |
@Override ConcreteType intersectWith(ConcreteType other) { return NONE; } |
| 272 |
|
| 273 |
@Override public String toString() { return "()"; } |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static class ConcreteFunctionType extends ConcreteType { |
| 286 |
|
| 287 |
static final String CALL_SLOT_NAME = ":call"; |
| 288 |
|
| 289 |
|
| 290 |
static final String THIS_SLOT_NAME = ":this"; |
| 291 |
|
| 292 |
|
| 293 |
static final String RETURN_SLOT_NAME = ":return"; |
| 294 |
|
| 295 |
private final Factory factory; |
| 296 |
private final Node declaration; |
| 297 |
private final StaticScope<ConcreteType> parentScope; |
| 298 |
private StaticScope<ConcreteType> bodyScope; |
| 299 |
private ConcreteInstanceType instanceType; |
| 300 |
private ConcreteInstanceType prototypeType; |
| 301 |
|
| 302 |
ConcreteFunctionType(Factory factory, |
| 303 |
Node declaration, |
| 304 |
StaticScope<ConcreteType> parentScope) { |
| 305 |
this.factory = factory; |
| 306 |
this.declaration = declaration; |
| 307 |
this.parentScope = parentScope; |
| 308 |
|
| 309 |
Preconditions.checkArgument(declaration.isFunction()); |
| 310 |
Preconditions.checkArgument(declaration.getJSType() != null); |
| 311 |
Preconditions.checkArgument(declaration.getJSType().isFunctionType()); |
| 312 |
} |
| 313 |
|
| 314 |
@Override boolean isFunction() { return true; } |
| 315 |
|
| 316 |
@Override ConcreteFunctionType toFunction() { return this; } |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
StaticSlot<ConcreteType> getCallSlot() { |
| 325 |
return getScope().getOwnSlot(CALL_SLOT_NAME); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
StaticSlot<ConcreteType> getThisSlot() { |
| 330 |
return getScope().getOwnSlot(THIS_SLOT_NAME); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
StaticSlot<ConcreteType> getReturnSlot() { |
| 335 |
return getScope().getOwnSlot(RETURN_SLOT_NAME); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
StaticSlot<ConcreteType> getParameterSlot(int index) { |
| 340 |
return getScope().getOwnSlot(getParameterName(index)); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
private String getParameterName(int index) { |
| 345 |
int count = 0; |
| 346 |
for (Node n = getFirstParameter(); n != null; n = n.getNext()) { |
| 347 |
if (count++ == index) { |
| 348 |
return n.getString(); |
| 349 |
} |
| 350 |
} |
| 351 |
return null; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
private Node getFirstParameter() { |
| 356 |
return declaration.getFirstChild().getNext().getFirstChild(); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
public FunctionType getJSType() { |
| 361 |
return JSType.toMaybeFunctionType(declaration.getJSType()); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
ConcreteInstanceType getInstanceType() { |
| 369 |
if (instanceType == null) { |
| 370 |
if (getJSType().isConstructor()) { |
| 371 |
instanceType = |
| 372 |
factory.createConcreteInstance(getJSType().getInstanceType()); |
| 373 |
} |
| 374 |
} |
| 375 |
return instanceType; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
ConcreteInstanceType getPrototypeType() { |
| 380 |
if (prototypeType == null) { |
| 381 |
prototypeType = |
| 382 |
factory.createConcreteInstance(getJSType().getPrototype()); |
| 383 |
} |
| 384 |
return prototypeType; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
ConcreteFunctionType getSuperclassType() { |
| 389 |
FunctionType superConstructor = getJSType().getSuperClassConstructor(); |
| 390 |
return (superConstructor != null) |
| 391 |
? factory.getConcreteFunction(superConstructor) : null; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
@Override StaticScope<ConcreteType> getScope() { |
| 396 |
if (bodyScope == null) { |
| 397 |
bodyScope = factory.createFunctionScope(declaration, parentScope); |
| 398 |
} |
| 399 |
return bodyScope; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
@Override public String toString() { |
| 409 |
StringBuilder b = new StringBuilder(32); |
| 410 |
b.append("function ("); |
| 411 |
boolean hasKnownTypeOfThis = !getThisSlot().getType().isNone(); |
| 412 |
if (hasKnownTypeOfThis) { |
| 413 |
b.append("this:"); |
| 414 |
b.append(getThisSlot().getType().toString()); |
| 415 |
} |
| 416 |
|
| 417 |
Node n = getFirstParameter(); |
| 418 |
if (hasKnownTypeOfThis && n != null) { |
| 419 |
b.append(", "); |
| 420 |
} |
| 421 |
for (int i = 0; n != null; ++i, n = n.getNext()) { |
| 422 |
String paramName = n.getString(); |
| 423 |
StaticSlot<ConcreteType> var = getScope().getOwnSlot(paramName); |
| 424 |
b.append(var.getType()); |
| 425 |
getParameterSlot(i).getType(); |
| 426 |
if (n.getNext() != null) { |
| 427 |
b.append(", "); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
b.append(")"); |
| 432 |
if (getReturnSlot().getType() != null) { |
| 433 |
b.append(": "); |
| 434 |
b.append(getReturnSlot().getType().toString()); |
| 435 |
} |
| 436 |
return b.toString(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static class ConcreteInstanceType extends ConcreteType { |
| 450 |
|
| 451 |
private final Factory factory; |
| 452 |
|
| 453 |
|
| 454 |
public final ObjectType instanceType; |
| 455 |
|
| 456 |
|
| 457 |
private ConcreteInstanceType prototype; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
private StaticScope<ConcreteType> scope; |
| 464 |
|
| 465 |
ConcreteInstanceType(Factory factory, ObjectType instanceType) { |
| 466 |
this.factory = factory; |
| 467 |
this.instanceType = instanceType; |
| 468 |
|
| 469 |
Preconditions.checkArgument(!(instanceType instanceof UnknownType)); |
| 470 |
} |
| 471 |
|
| 472 |
@Override boolean isInstance() { return true; } |
| 473 |
|
| 474 |
@Override ConcreteInstanceType toInstance() { return this; } |
| 475 |
|
| 476 |
|
| 477 |
boolean isFunctionPrototype() { |
| 478 |
return instanceType.isFunctionPrototypeType(); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
StaticSlot<ConcreteType> getPropertySlot(String propName) { |
| 483 |
return getScope().getSlot(propName); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
ConcreteInstanceType getInstanceTypeWithProperty(String propName) { |
| 491 |
if (getScope().getOwnSlot(propName) != null) { |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if (instanceType.getConstructor() != null) { |
| 503 |
return getConstructorType().getPrototypeType(); |
| 504 |
} |
| 505 |
return this; |
| 506 |
} else if (getImplicitPrototype() != null) { |
| 507 |
return getImplicitPrototype().getInstanceTypeWithProperty(propName); |
| 508 |
} else { |
| 509 |
return null; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
ConcreteInstanceType getImplicitPrototype() { |
| 515 |
if ((prototype == null) |
| 516 |
&& (instanceType.getImplicitPrototype() != null)) { |
| 517 |
ObjectType proto = instanceType.getImplicitPrototype(); |
| 518 |
if ((proto != instanceType) && !(proto instanceof UnknownType)) { |
| 519 |
prototype = factory.createConcreteInstance(proto); |
| 520 |
} |
| 521 |
} |
| 522 |
return prototype; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
ConcreteFunctionType getConstructorType() { |
| 527 |
if (instanceType.isFunctionPrototypeType()) { |
| 528 |
return factory.getConcreteFunction(instanceType.getOwnerFunction()); |
| 529 |
} else { |
| 530 |
FunctionType constructor = instanceType.getConstructor(); |
| 531 |
return (constructor != null) |
| 532 |
? factory.getConcreteFunction(constructor) : null; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
@Override StaticScope<ConcreteType> getScope() { |
| 538 |
if (scope == null) { |
| 539 |
scope = factory.createInstanceScope(instanceType); |
| 540 |
} |
| 541 |
return scope; |
| 542 |
} |
| 543 |
|
| 544 |
@Override public String toString() { return instanceType.toString(); } |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
static class ConcreteUnionType extends ConcreteType { |
| 553 |
private final Set<ConcreteType> alternatives; |
| 554 |
|
| 555 |
ConcreteUnionType(ConcreteType... alternatives) { |
| 556 |
this(Sets.newHashSet(alternatives)); |
| 557 |
} |
| 558 |
|
| 559 |
ConcreteUnionType(Set<ConcreteType> alternatives) { |
| 560 |
Preconditions.checkArgument(alternatives.size() > 1); |
| 561 |
this.alternatives = alternatives; |
| 562 |
} |
| 563 |
|
| 564 |
@Override boolean isUnion() { return true; } |
| 565 |
|
| 566 |
@Override ConcreteUnionType toUnion() { return this; } |
| 567 |
|
| 568 |
@Override ConcreteType unionWith(ConcreteType other) { |
| 569 |
if (other.isSingleton()) { |
| 570 |
if (alternatives.contains(other)) { |
| 571 |
return this; |
| 572 |
} else { |
| 573 |
Set<ConcreteType> alts = Sets.newHashSet(alternatives); |
| 574 |
alts.add(other); |
| 575 |
return new ConcreteUnionType(alts); |
| 576 |
} |
| 577 |
} else if (other.isUnion()) { |
| 578 |
ConcreteUnionType otherUnion = other.toUnion(); |
| 579 |
if (alternatives.containsAll(otherUnion.alternatives)) { |
| 580 |
return this; |
| 581 |
} else if (otherUnion.alternatives.containsAll(alternatives)) { |
| 582 |
return otherUnion; |
| 583 |
} else { |
| 584 |
Set<ConcreteType> alts = Sets.newHashSet(alternatives); |
| 585 |
alts.addAll(otherUnion.alternatives); |
| 586 |
return new ConcreteUnionType(alts); |
| 587 |
} |
| 588 |
} else { |
| 589 |
Preconditions.checkArgument(other.isNone() || other.isAll()); |
| 590 |
return other.unionWith(this); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
@Override ConcreteType intersectWith(ConcreteType other) { |
| 595 |
if (other.isSingleton()) { |
| 596 |
if (alternatives.contains(other)) { |
| 597 |
return other; |
| 598 |
} else { |
| 599 |
return NONE; |
| 600 |
} |
| 601 |
} else if (other.isUnion()) { |
| 602 |
Set<ConcreteType> types = Sets.newHashSet(alternatives); |
| 603 |
types.retainAll(other.toUnion().alternatives); |
| 604 |
return createForTypes(types); |
| 605 |
} else { |
| 606 |
Preconditions.checkArgument(other.isNone() || other.isAll()); |
| 607 |
return other.intersectWith(this); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
Set<ConcreteType> getAlternatives() { return alternatives; } |
| 613 |
|
| 614 |
@Override public boolean equals(Object obj) { |
| 615 |
return (obj instanceof ConcreteUnionType) |
| 616 |
&& alternatives.equals(((ConcreteUnionType) obj).alternatives); |
| 617 |
} |
| 618 |
|
| 619 |
@Override public int hashCode() { |
| 620 |
return alternatives.hashCode() ^ 0x5f6e7d8c; |
| 621 |
} |
| 622 |
|
| 623 |
@Override public String toString() { |
| 624 |
List<String> names = Lists.newArrayList(); |
| 625 |
for (ConcreteType type : alternatives) { |
| 626 |
names.add(type.toString()); |
| 627 |
} |
| 628 |
Collections.sort(names); |
| 629 |
|
| 630 |
return "(" + Joiner.on(",").join(names) + ")"; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
private static class ConcreteAll extends ConcreteType { |
| 636 |
@Override boolean isAll() { return true; } |
| 637 |
|
| 638 |
@Override ConcreteType unionWith(ConcreteType other) { return this; } |
| 639 |
|
| 640 |
@Override ConcreteType intersectWith(ConcreteType other) { return other; } |
| 641 |
|
| 642 |
@Override public String toString() { return "*"; } |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
static class ConcreteUniqueType extends ConcreteType { |
| 654 |
private final int id; |
| 655 |
|
| 656 |
ConcreteUniqueType(int id) { |
| 657 |
this.id = id; |
| 658 |
|
| 659 |
Preconditions.checkArgument(id >= 0); |
| 660 |
} |
| 661 |
|
| 662 |
@Override public boolean equals(Object o) { |
| 663 |
return (o instanceof ConcreteUniqueType) |
| 664 |
&& (id == ((ConcreteUniqueType) o).id); |
| 665 |
} |
| 666 |
|
| 667 |
@Override public int hashCode() { |
| 668 |
return ConcreteUniqueType.class.hashCode() ^ id; |
| 669 |
} |
| 670 |
|
| 671 |
@Override public String toString() { return "Unique$" + id; } |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
interface Factory { |
| 681 |
|
| 682 |
ConcreteFunctionType createConcreteFunction( |
| 683 |
Node declaration, StaticScope<ConcreteType> parent); |
| 684 |
|
| 685 |
|
| 686 |
ConcreteInstanceType createConcreteInstance(ObjectType instanceType); |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
ConcreteFunctionType getConcreteFunction(FunctionType function); |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
ConcreteInstanceType getConcreteInstance(ObjectType instance); |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
StaticScope<ConcreteType> createFunctionScope( |
| 706 |
Node declaration, StaticScope<ConcreteType> parent); |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
StaticScope<ConcreteType> createInstanceScope(ObjectType instanceType); |
| 714 |
|
| 715 |
|
| 716 |
JSTypeRegistry getTypeRegistry(); |
| 717 |
} |
| 718 |
} |