OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart_types; | 5 library dart_types; |
6 | 6 |
7 import 'dart2jslib.dart' show Compiler, invariant, Script, Message; | 7 import 'dart2jslib.dart' show Compiler, invariant, Script, Message; |
8 import 'elements/modelx.dart' | 8 import 'elements/modelx.dart' |
9 show VoidElementX, LibraryElementX, BaseClassElementX; | 9 show VoidElementX, LibraryElementX, BaseClassElementX; |
10 import 'elements/elements.dart'; | 10 import 'elements/elements.dart'; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 /** | 87 /** |
88 * Calls [f] with each [MalformedType] within this type. | 88 * Calls [f] with each [MalformedType] within this type. |
89 * | 89 * |
90 * If [f] returns [: false :], the traversal stops prematurely. | 90 * If [f] returns [: false :], the traversal stops prematurely. |
91 * | 91 * |
92 * [forEachMalformedType] returns [: false :] if the traversal was stopped | 92 * [forEachMalformedType] returns [: false :] if the traversal was stopped |
93 * prematurely. | 93 * prematurely. |
94 */ | 94 */ |
95 bool forEachMalformedType(bool f(MalformedType type)) => true; | 95 bool forEachMalformedType(bool f(MalformedType type)) => true; |
96 | 96 |
97 // TODO(ahe): This is implicitly inherited from Object. What is the purpose | |
98 // of duplicating it here? | |
99 bool operator ==(other); | |
100 | |
101 /** | 97 /** |
102 * Is [: true :] if this type has no explict type arguments. | 98 * Is [: true :] if this type has no explict type arguments. |
103 */ | 99 */ |
104 bool get isRaw => true; | 100 bool get isRaw => true; |
105 | 101 |
106 DartType asRaw() => this; | 102 DartType asRaw() => this; |
107 | 103 |
108 /** | 104 /** |
109 * Is [: true :] if this type is the dynamic type. | 105 * Is [: true :] if this type is the dynamic type. |
110 */ | 106 */ |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 bool get isMalformed => true; | 335 bool get isMalformed => true; |
340 | 336 |
341 bool forEachMalformedType(bool f(MalformedType type)) => f(this); | 337 bool forEachMalformedType(bool f(MalformedType type)) => f(this); |
342 | 338 |
343 DartType unalias(Compiler compiler) => this; | 339 DartType unalias(Compiler compiler) => this; |
344 | 340 |
345 accept(DartTypeVisitor visitor, var argument) { | 341 accept(DartTypeVisitor visitor, var argument) { |
346 return visitor.visitMalformedType(this, argument); | 342 return visitor.visitMalformedType(this, argument); |
347 } | 343 } |
348 | 344 |
349 // TODO(ahe): This is the default implementation that would be inherited if | |
350 // DartType didn't declare an abstract method. What is the purpose? | |
351 bool operator ==(other) => identical(this, other); | |
352 | |
353 String toString() { | 345 String toString() { |
354 var sb = new StringBuffer(); | 346 var sb = new StringBuffer(); |
355 if (typeArguments != null) { | 347 if (typeArguments != null) { |
356 if (userProvidedBadType != null) { | 348 if (userProvidedBadType != null) { |
357 sb.write(userProvidedBadType.name.slowToString()); | 349 sb.write(userProvidedBadType.name.slowToString()); |
358 } else { | 350 } else { |
359 sb.write(element.name.slowToString()); | 351 sb.write(element.name.slowToString()); |
360 } | 352 } |
361 if (!typeArguments.isEmpty) { | 353 if (!typeArguments.isEmpty) { |
362 sb.write('<'); | 354 sb.write('<'); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 !arguments.isEmpty; | 441 !arguments.isEmpty; |
450 arguments = arguments.tail) { | 442 arguments = arguments.tail) { |
451 int argumentHash = arguments.head != null ? arguments.head.hashCode : 0; | 443 int argumentHash = arguments.head != null ? arguments.head.hashCode : 0; |
452 hash = 17 * hash + 3 * argumentHash; | 444 hash = 17 * hash + 3 * argumentHash; |
453 } | 445 } |
454 return hash; | 446 return hash; |
455 } | 447 } |
456 | 448 |
457 bool operator ==(other) { | 449 bool operator ==(other) { |
458 if (other is !GenericType) return false; | 450 if (other is !GenericType) return false; |
459 return identical(element, other.element) | 451 return kind == other.kind |
| 452 && element == other.element |
460 && typeArguments == other.typeArguments; | 453 && typeArguments == other.typeArguments; |
461 } | 454 } |
462 | 455 |
463 bool get isRaw => typeArguments.isEmpty || identical(this, element.rawType); | 456 bool get isRaw => typeArguments.isEmpty || identical(this, element.rawType); |
464 | 457 |
465 GenericType asRaw() => element.rawType; | 458 GenericType asRaw() => element.rawType; |
466 } | 459 } |
467 | 460 |
468 // TODO(johnniwinther): Add common supertype for InterfaceType and TypedefType. | |
469 class InterfaceType extends GenericType { | 461 class InterfaceType extends GenericType { |
470 final ClassElement element; | 462 final ClassElement element; |
471 | 463 |
472 InterfaceType(this.element, | 464 InterfaceType(this.element, |
473 [Link<DartType> typeArguments = const Link<DartType>()]) | 465 [Link<DartType> typeArguments = const Link<DartType>()]) |
474 : super(typeArguments, hasMalformed(typeArguments)) { | 466 : super(typeArguments, hasMalformed(typeArguments)) { |
475 assert(invariant(element, element.isDeclaration)); | 467 assert(invariant(element, element.isDeclaration)); |
476 assert(invariant(element, element.thisType == null || | 468 assert(invariant(element, element.thisType == null || |
477 typeArguments.slowLength() == element.typeVariables.slowLength(), | 469 typeArguments.slowLength() == element.typeVariables.slowLength(), |
478 message: () => 'Invalid type argument count on ${element.thisType}. ' | 470 message: () => 'Invalid type argument count on ${element.thisType}. ' |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 declarer = supertype; | 538 declarer = supertype; |
547 ClassElement lookupTarget = declarer.element; | 539 ClassElement lookupTarget = declarer.element; |
548 member = lookupTarget.implementation.lookupLocalMember(name); | 540 member = lookupTarget.implementation.lookupLocalMember(name); |
549 if (member != null) { | 541 if (member != null) { |
550 return createMember(receiver, declarer, member); | 542 return createMember(receiver, declarer, member); |
551 } | 543 } |
552 } | 544 } |
553 return null; | 545 return null; |
554 } | 546 } |
555 | 547 |
556 bool operator ==(other) { | |
557 // TODO(johnniwinther,karlklose): This is a bad implementation of | |
558 // operator==. This implementation is not compatible with the | |
559 // implementation in the superclass: another subclass of GenericType might | |
560 // compare equal to an instance of this class if the other subclass forgets | |
561 // to implement operator==. This is brittle and easy to avoid, ask ahe@ | |
562 // for concrete suggestions. | |
563 if (other is !InterfaceType) return false; | |
564 return super == other; | |
565 } | |
566 | |
567 int get hashCode => super.hashCode; | 548 int get hashCode => super.hashCode; |
568 | 549 |
569 InterfaceType asRaw() => super.asRaw(); | 550 InterfaceType asRaw() => super.asRaw(); |
570 | 551 |
571 accept(DartTypeVisitor visitor, var argument) { | 552 accept(DartTypeVisitor visitor, var argument) { |
572 return visitor.visitInterfaceType(this, argument); | 553 return visitor.visitInterfaceType(this, argument); |
573 } | 554 } |
574 } | 555 } |
575 | 556 |
576 class FunctionType extends DartType { | 557 class FunctionType extends DartType { |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 SourceString get name => element.name; | 800 SourceString get name => element.name; |
820 | 801 |
821 DartType unalias(Compiler compiler) { | 802 DartType unalias(Compiler compiler) { |
822 // TODO(ahe): This should be [ensureResolved]. | 803 // TODO(ahe): This should be [ensureResolved]. |
823 compiler.resolveTypedef(element); | 804 compiler.resolveTypedef(element); |
824 DartType definition = element.alias.unalias(compiler); | 805 DartType definition = element.alias.unalias(compiler); |
825 TypedefType declaration = element.computeType(compiler); | 806 TypedefType declaration = element.computeType(compiler); |
826 return definition.subst(typeArguments, declaration.typeArguments); | 807 return definition.subst(typeArguments, declaration.typeArguments); |
827 } | 808 } |
828 | 809 |
829 bool operator ==(other) { | |
830 // TODO(johnniwinther,karlklose): See InterfaceType.operator==. | |
831 if (other is !TypedefType) return false; | |
832 return super == other; | |
833 } | |
834 | |
835 int get hashCode => super.hashCode; | 810 int get hashCode => super.hashCode; |
836 | 811 |
837 TypedefType asRaw() => super.asRaw(); | 812 TypedefType asRaw() => super.asRaw(); |
838 | 813 |
839 accept(DartTypeVisitor visitor, var argument) { | 814 accept(DartTypeVisitor visitor, var argument) { |
840 return visitor.visitTypedefType(this, argument); | 815 return visitor.visitTypedefType(this, argument); |
841 } | 816 } |
842 } | 817 } |
843 | 818 |
844 /** | 819 /** |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1263 : super(compiler, dynamicType, voidType); | 1238 : super(compiler, dynamicType, voidType); |
1264 | 1239 |
1265 | 1240 |
1266 bool isSubtype(DartType t, DartType s) { | 1241 bool isSubtype(DartType t, DartType s) { |
1267 if (t is TypeVariableType || s is TypeVariableType) { | 1242 if (t is TypeVariableType || s is TypeVariableType) { |
1268 return true; | 1243 return true; |
1269 } | 1244 } |
1270 return super.isSubtype(t, s); | 1245 return super.isSubtype(t, s); |
1271 } | 1246 } |
1272 } | 1247 } |
OLD | NEW |