Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/elements/elements.dart

Issue 11413219: Canonicalize raw type (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comment Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 elements; 5 library elements;
6 6
7 import 'dart:uri'; 7 import 'dart:uri';
8 8
9 // TODO(ahe): Rename prefix to 'api' when VM bug is fixed. 9 // TODO(ahe): Rename prefix to 'api' when VM bug is fixed.
10 import '../../compiler.dart' as api_e; 10 import '../../compiler.dart' as api_e;
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 lookupLocalMember(SourceString memberName) => imported[memberName]; 831 lookupLocalMember(SourceString memberName) => imported[memberName];
832 832
833 DartType computeType(Compiler compiler) => compiler.types.dynamicType; 833 DartType computeType(Compiler compiler) => compiler.types.dynamicType;
834 834
835 Token position() => firstPosition; 835 Token position() => firstPosition;
836 } 836 }
837 837
838 class TypedefElement extends Element implements TypeDeclarationElement { 838 class TypedefElement extends Element implements TypeDeclarationElement {
839 Typedef cachedNode; 839 Typedef cachedNode;
840 TypedefType cachedType; 840 TypedefType cachedType;
841
842 /**
843 * Canonicalize raw version of [cachedType].
844 *
845 * See [ClassElement.rawType] for motivation.
846 *
847 * The [rawType] is computed together with [cachedType] in [computeType].
848 */
849 TypedefType rawType;
850
851 /**
852 * The type annotation which defines this typedef.
853 */
841 DartType alias; 854 DartType alias;
842 855
843 bool isResolved = false; 856 bool isResolved = false;
844 bool isBeingResolved = false; 857 bool isBeingResolved = false;
845 858
846 TypedefElement(SourceString name, Element enclosing) 859 TypedefElement(SourceString name, Element enclosing)
847 : super(name, ElementKind.TYPEDEF, enclosing); 860 : super(name, ElementKind.TYPEDEF, enclosing);
848 861
849 /** 862 /**
850 * Function signature for a typedef of a function type. The signature is 863 * Function signature for a typedef of a function type. The signature is
851 * kept to provide full information about parameter names through the mirror 864 * kept to provide full information about parameter names through the mirror
852 * system. 865 * system.
853 * 866 *
854 * The [functionSignature] is not available until the typedef element has been 867 * The [functionSignature] is not available until the typedef element has been
855 * resolved. 868 * resolved.
856 */ 869 */
857 FunctionSignature functionSignature; 870 FunctionSignature functionSignature;
858 871
859 TypedefType computeType(Compiler compiler) { 872 TypedefType computeType(Compiler compiler) {
860 if (cachedType != null) return cachedType; 873 if (cachedType != null) return cachedType;
861 Typedef node = parseNode(compiler); 874 Typedef node = parseNode(compiler);
862 Link<DartType> parameters = 875 Link<DartType> parameters =
863 TypeDeclarationElement.createTypeVariables(this, node.typeParameters); 876 TypeDeclarationElement.createTypeVariables(this, node.typeParameters);
864 cachedType = new TypedefType(this, parameters); 877 cachedType = new TypedefType(this, parameters);
878 if (parameters.isEmpty) {
879 rawType = cachedType;
880 } else {
881 var dynamicParameters = const Link<DartType>();
882 parameters.forEach((_) {
883 dynamicParameters =
884 dynamicParameters.prepend(compiler.types.dynamicType);
885 });
886 rawType = new TypedefType(this, dynamicParameters);
887 }
865 compiler.resolveTypedef(this); 888 compiler.resolveTypedef(this);
866 return cachedType; 889 return cachedType;
867 } 890 }
868 891
869 Link<DartType> get typeVariables => cachedType.typeArguments; 892 Link<DartType> get typeVariables => cachedType.typeArguments;
870 893
871 Scope buildScope() { 894 Scope buildScope() {
872 return new TypeDeclarationScope(enclosingElement.buildScope(), this); 895 return new TypeDeclarationScope(enclosingElement.buildScope(), this);
873 } 896 }
874 } 897 }
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 variableElement.type = variableType; 1399 variableElement.type = variableType;
1377 arguments.addLast(variableType); 1400 arguments.addLast(variableType);
1378 } 1401 }
1379 return arguments.toLink(); 1402 return arguments.toLink();
1380 } 1403 }
1381 } 1404 }
1382 1405
1383 abstract class ClassElement extends ScopeContainerElement 1406 abstract class ClassElement extends ScopeContainerElement
1384 implements TypeDeclarationElement { 1407 implements TypeDeclarationElement {
1385 final int id; 1408 final int id;
1386 InterfaceType type; 1409 /**
1410 * The type of [:this:] for this class declaration.
1411 *
1412 * The type of [:this:] is the interface type based on this element in which
1413 * the type arguments are the declared type variables. For instance,
1414 * [:List<E>:] for [:List:] and [:Map<K,V>:] for [:Map:].
1415 *
1416 * This type is computed in [computeType].
1417 */
1418 InterfaceType thisType;
1419
1420 /**
1421 * The raw type for this class declaration.
1422 *
1423 * The raw type is the interface type base on this element in which the type
1424 * arguments are all [dynamic]. For instance [:List<dynamic>:] for [:List:]
1425 * and [:Map<dynamic,dynamic>:] for [:Map:]. For non-generic classes [rawType]
1426 * is the same as [thisType].
1427 *
1428 * The [rawType] field is a canonicalization of the raw type and should be
1429 * used to distinguish explicit and implicit uses of the [dynamic]
1430 * type arguments. For instance should [:List:] be the [rawType] of the
1431 * [:List:] class element whereas [:List<dynamic>:] should be its own
1432 * instantiation of [InterfaceType] with [:dynamic:] as type argument. Using
1433 * this distinction, we can print the raw type with type arguments only when
1434 * the input source has used explicit type arguments.
1435 *
1436 * This type is computed together with [thisType] in [computeType].
ahe 2012/11/29 10:09:08 Awesome comments.
1437 */
1438 InterfaceType rawType;
1387 DartType supertype; 1439 DartType supertype;
1388 DartType defaultClass; 1440 DartType defaultClass;
1389 Link<DartType> interfaces; 1441 Link<DartType> interfaces;
1390 SourceString nativeName; 1442 SourceString nativeName;
1391 int supertypeLoadState; 1443 int supertypeLoadState;
1392 int resolutionState; 1444 int resolutionState;
1393 1445
1394 // backendMembers are members that have been added by the backend to simplify 1446 // backendMembers are members that have been added by the backend to simplify
1395 // compilation. They don't have any user-side counter-part. 1447 // compilation. They don't have any user-side counter-part.
1396 Link<Element> backendMembers = const Link<Element>(); 1448 Link<Element> backendMembers = const Link<Element>();
1397 1449
1398 Link<DartType> allSupertypes; 1450 Link<DartType> allSupertypes;
1399 1451
1400 // Lazily applied patch of class members. 1452 // Lazily applied patch of class members.
1401 ClassElement patch = null; 1453 ClassElement patch = null;
1402 ClassElement origin = null; 1454 ClassElement origin = null;
1403 1455
1404 ClassElement(SourceString name, Element enclosing, this.id, int initialState) 1456 ClassElement(SourceString name, Element enclosing, this.id, int initialState)
1405 : supertypeLoadState = initialState, 1457 : supertypeLoadState = initialState,
1406 resolutionState = initialState, 1458 resolutionState = initialState,
1407 super(name, ElementKind.CLASS, enclosing); 1459 super(name, ElementKind.CLASS, enclosing);
1408 1460
1409 ClassNode parseNode(Compiler compiler); 1461 ClassNode parseNode(Compiler compiler);
1410 1462
1411 InterfaceType computeType(compiler) { 1463 InterfaceType computeType(compiler) {
1412 if (type == null) { 1464 if (thisType == null) {
1413 if (origin == null) { 1465 if (origin == null) {
1414 ClassNode node = parseNode(compiler); 1466 ClassNode node = parseNode(compiler);
1415 Link<DartType> parameters = 1467 Link<DartType> parameters =
1416 TypeDeclarationElement.createTypeVariables(this, 1468 TypeDeclarationElement.createTypeVariables(this,
1417 node.typeParameters); 1469 node.typeParameters);
1418 type = new InterfaceType(this, parameters); 1470 thisType = new InterfaceType(this, parameters);
1471 if (parameters.isEmpty) {
1472 rawType = thisType;
1473 } else {
1474 var dynamicParameters = const Link<DartType>();
1475 parameters.forEach((_) {
1476 dynamicParameters =
1477 dynamicParameters.prepend(compiler.types.dynamicType);
1478 });
ahe 2012/11/29 10:09:08 Code duplication detected. This loop can be shared
1479 rawType = new InterfaceType(this, dynamicParameters);
1480 }
1419 } else { 1481 } else {
1420 type = origin.computeType(compiler); 1482 thisType = origin.computeType(compiler);
1483 rawType = origin.rawType;
1421 } 1484 }
1422 } 1485 }
1423 return type; 1486 return thisType;
1424 } 1487 }
1425 1488
1426 bool get isPatched => patch != null; 1489 bool get isPatched => patch != null;
1427 bool get isPatch => origin != null; 1490 bool get isPatch => origin != null;
1428 1491
1429 ClassElement get declaration => super.declaration; 1492 ClassElement get declaration => super.declaration;
1430 ClassElement get implementation => super.implementation; 1493 ClassElement get implementation => super.implementation;
1431 1494
1432 /** 1495 /**
1433 * Return [:true:] if this element is the [:Object:] class for the [compiler]. 1496 * Return [:true:] if this element is the [:Object:] class for the [compiler].
1434 */ 1497 */
1435 bool isObject(Compiler compiler) => 1498 bool isObject(Compiler compiler) =>
1436 identical(declaration, compiler.objectClass); 1499 identical(declaration, compiler.objectClass);
1437 1500
1438 Link<DartType> get typeVariables => type.typeArguments; 1501 Link<DartType> get typeVariables => thisType.typeArguments;
1439 1502
1440 ClassElement ensureResolved(Compiler compiler) { 1503 ClassElement ensureResolved(Compiler compiler) {
1441 if (resolutionState == STATE_NOT_STARTED) { 1504 if (resolutionState == STATE_NOT_STARTED) {
1442 compiler.resolver.resolveClass(this); 1505 compiler.resolver.resolveClass(this);
1443 } 1506 }
1444 return this; 1507 return this;
1445 } 1508 }
1446 1509
1447 /** 1510 /**
1448 * Lookup local members in the class. This will ignore constructors. 1511 * Lookup local members in the class. This will ignore constructors.
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
1722 } 1785 }
1723 return false; 1786 return false;
1724 } 1787 }
1725 1788
1726 bool isInterface() => false; 1789 bool isInterface() => false;
1727 bool isNative() => nativeName != null; 1790 bool isNative() => nativeName != null;
1728 int get hashCode => id; 1791 int get hashCode => id;
1729 1792
1730 Scope buildScope() => new ClassScope(enclosingElement.buildScope(), this); 1793 Scope buildScope() => new ClassScope(enclosingElement.buildScope(), this);
1731 1794
1732 Link<DartType> get allSupertypesAndSelf {
1733 return allSupertypes.prepend(new InterfaceType(this));
1734 }
1735
1736 String toString() { 1795 String toString() {
1737 if (origin != null) { 1796 if (origin != null) {
1738 return 'patch ${super.toString()}'; 1797 return 'patch ${super.toString()}';
1739 } else if (patch != null) { 1798 } else if (patch != null) {
1740 return 'origin ${super.toString()}'; 1799 return 'origin ${super.toString()}';
1741 } else { 1800 } else {
1742 return super.toString(); 1801 return super.toString();
1743 } 1802 }
1744 } 1803 }
1745 } 1804 }
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
2075 2134
2076 MetadataAnnotation ensureResolved(Compiler compiler) { 2135 MetadataAnnotation ensureResolved(Compiler compiler) {
2077 if (resolutionState == STATE_NOT_STARTED) { 2136 if (resolutionState == STATE_NOT_STARTED) {
2078 compiler.resolver.resolveMetadataAnnotation(this); 2137 compiler.resolver.resolveMetadataAnnotation(this);
2079 } 2138 }
2080 return this; 2139 return this;
2081 } 2140 }
2082 2141
2083 String toString() => 'MetadataAnnotation($value, $resolutionState)'; 2142 String toString() => 'MetadataAnnotation($value, $resolutionState)';
2084 } 2143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698