~ohdeno/+junk/closure-compiler-svn-trunk

« back to all changes in this revision

Viewing changes to src/com/google/javascript/rhino/jstype/JSType.java

  • Committer: dimvar at google
  • Date: 2012-09-11 23:17:52 UTC
  • Revision ID: svn-v4:b0f006be-c8cd-11de-a2e8-8d36a3108c74:trunk:2184

Automated g4 rollback

*** Reason for rollback ***

Failures don't seem related to jscompiler. Resubmitting for now.

*** Original change description ***

Automated g4 rollback

*** Reason for rollback ***

Change caused tests to stop building.

*** Original change description ***

New @struct and @dict annotations for constructors.
With these annotations, one can enforce only dot or only bracket access on object properties.

R=nicksantos
DELTA=476  (391 added, 34 deleted, 51 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=5491

Show diffs side-by-side

added added

removed removed

Lines of Context:
249
249
  }
250
250
 
251
251
  /**
 
252
   * Returns true iff {@code this} can be a {@code struct}.
 
253
   * UnionType overrides the method, assume {@code this} is not a union here.
 
254
   */
 
255
  public boolean isStruct() {
 
256
    if (isObject()) {
 
257
      FunctionType ctor = toObjectType().getConstructor();
 
258
      // getConstructor can return an *interface* type, so it's not safe to
 
259
      // assume that makesStructs is only called on constructors.
 
260
      return ctor != null && ctor.makesStructs();
 
261
    }
 
262
    return false;
 
263
  }
 
264
 
 
265
  /**
 
266
   * Returns true iff {@code this} can be a {@code dict}.
 
267
   * UnionType overrides the method, assume {@code this} is not a union here.
 
268
   */
 
269
  public boolean isDict() {
 
270
    if (isObject()) {
 
271
      FunctionType ctor = toObjectType().getConstructor();
 
272
      return ctor != null && ctor.makesDicts();
 
273
    }
 
274
    return false;
 
275
  }
 
276
 
 
277
  /**
252
278
   * Downcasts this to a UnionType, or returns null if this is not a UnionType.
253
279
   *
254
280
   * Named in honor of Haskell's Maybe type constructor.
579
605
  }
580
606
 
581
607
  /**
582
 
   * Gets the type to which this type auto-boxes.
 
608
   * Turn a scalar type to the corresponding object type.
583
609
   *
584
 
   * @return the auto-boxed type or {@code null} if this type does not auto-box
 
610
   * @return the auto-boxed type or {@code null} if this type is not a scalar.
585
611
   */
586
612
  public JSType autoboxesTo() {
587
613
    return null;
588
614
  }
589
615
 
590
616
  /**
591
 
   * Gets the type to which this type unboxes.
 
617
   * Turn an object type to its corresponding scalar type.
592
618
   *
593
619
   * @return the unboxed type or {@code null} if this type does not unbox.
594
620
   */
598
624
 
599
625
  /**
600
626
   * Casts this to an ObjectType, or returns null if this is not an ObjectType.
601
 
   *
602
 
   * Does not change the underlying JS type. If you want to simulate JS
603
 
   * autoboxing or dereferencing, you should use autoboxesTo() or dereference().
604
 
   * Those methods may change the underlying JS type.
 
627
   * If this is a scalar type, it will *not* be converted to an object type.
 
628
   * If you want to simulate JS autoboxing or dereferencing, you should use
 
629
   * autoboxesTo() or dereference().
605
630
   */
606
631
  public ObjectType toObjectType() {
607
632
    return this instanceof ObjectType ? (ObjectType) this : null;
610
635
  /**
611
636
   * Dereference a type for property access.
612
637
   *
613
 
   * Autoboxes the type, and filters null/undefined, and returns the result.
 
638
   * Filters null/undefined and autoboxes the resulting type.
 
639
   * Never returns null.
614
640
   */
615
641
  public JSType autobox() {
616
642
    JSType restricted = restrictByNotNullOrUndefined();
621
647
  /**
622
648
   * Dereference a type for property access.
623
649
   *
624
 
   * Autoboxes the type, filters null/undefined, and returns the result
 
650
   * Filters null/undefined, autoboxes the resulting type, and returns it
625
651
   * iff it's an object.
626
652
   */
627
653
  public final ObjectType dereference() {
628
 
    return ObjectType.cast(autobox());
 
654
    return autobox().toObjectType();
629
655
  }
630
656
 
631
657
  /**
1082
1108
    if (thatType.isUnknownType()) {
1083
1109
      return true;
1084
1110
    }
 
1111
    // all type
 
1112
    if (thatType.isAllType()) {
 
1113
      return true;
 
1114
    }
1085
1115
    // equality
1086
1116
    if (thisType.isEquivalentTo(thatType)) {
1087
1117
      return true;
1088
1118
    }
1089
 
    // all type
1090
 
    if (thatType.isAllType()) {
1091
 
      return true;
1092
 
    }
1093
1119
    // unions
1094
1120
    if (thatType.isUnionType()) {
1095
1121
      UnionType union = thatType.toMaybeUnionType();