Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.modelx; | 5 library elements.modelx; |
| 6 | 6 |
| 7 import 'elements.dart'; | 7 import 'elements.dart'; |
| 8 import '../../compiler.dart' as api; | 8 import '../../compiler.dart' as api; |
| 9 import '../tree/tree.dart'; | 9 import '../tree/tree.dart'; |
| 10 import '../util/util.dart'; | 10 import '../util/util.dart'; |
| 11 import '../resolution/resolution.dart'; | 11 import '../resolution/resolution.dart'; |
| 12 | 12 |
| 13 import '../dart2jslib.dart' show invariant, | 13 import '../dart2jslib.dart' show invariant, |
| 14 InterfaceType, | 14 InterfaceType, |
| 15 DartType, | 15 DartType, |
| 16 TypeVariableType, | 16 TypeVariableType, |
| 17 TypedefType, | 17 TypedefType, |
| 18 DualKind, | 18 DualKind, |
| 19 MessageKind, | 19 MessageKind, |
| 20 DiagnosticListener, | 20 DiagnosticListener, |
| 21 Script, | 21 Script, |
| 22 FunctionType, | 22 FunctionType, |
| 23 Selector, | 23 Selector, |
| 24 Constant, | 24 Constant, |
| 25 Compiler, | 25 Compiler, |
| 26 isPrivateName; | 26 isPrivateName, |
| 27 CodeBuffer; | |
|
ahe
2013/11/27 19:28:53
This import doesn't belong here.
sigurdm
2013/11/29 10:39:37
Done.
| |
| 27 | 28 |
| 28 import '../dart_types.dart'; | 29 import '../dart_types.dart'; |
| 29 | 30 |
| 30 import '../scanner/scannerlib.dart' show Token, EOF_TOKEN; | 31 import '../scanner/scannerlib.dart' show Token, EOF_TOKEN; |
| 31 | 32 |
| 32 import '../ordered_typeset.dart' show OrderedTypeSet; | 33 import '../ordered_typeset.dart' show OrderedTypeSet; |
| 33 | 34 |
| 34 class ElementX implements Element { | 35 class ElementX implements Element { |
| 35 static int elementHashCode = 0; | 36 static int elementHashCode = 0; |
| 36 | 37 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 FunctionElement asFunctionElement() => null; | 287 FunctionElement asFunctionElement() => null; |
| 287 | 288 |
| 288 static bool isInvalid(Element e) => e == null || e.isErroneous(); | 289 static bool isInvalid(Element e) => e == null || e.isErroneous(); |
| 289 | 290 |
| 290 bool get isAbstract => modifiers.isAbstract(); | 291 bool get isAbstract => modifiers.isAbstract(); |
| 291 bool isForeign(Compiler compiler) => getLibrary() == compiler.foreignLibrary; | 292 bool isForeign(Compiler compiler) => getLibrary() == compiler.foreignLibrary; |
| 292 | 293 |
| 293 FunctionElement get targetConstructor => null; | 294 FunctionElement get targetConstructor => null; |
| 294 | 295 |
| 295 void diagnose(Element context, DiagnosticListener listener) {} | 296 void diagnose(Element context, DiagnosticListener listener) {} |
| 297 | |
| 298 Map collectInferredTypes(Compiler compiler) { | |
| 299 compiler.internalError("This element does not support dumping of types", | |
| 300 token: position()); | |
| 301 } | |
| 296 } | 302 } |
| 297 | 303 |
| 298 /** | 304 /** |
| 299 * Represents an unresolvable or duplicated element. | 305 * Represents an unresolvable or duplicated element. |
| 300 * | 306 * |
| 301 * An [ErroneousElement] is used instead of [null] to provide additional | 307 * An [ErroneousElement] is used instead of [null] to provide additional |
| 302 * information about the error that caused the element to be unresolvable | 308 * information about the error that caused the element to be unresolvable |
| 303 * or otherwise invalid. | 309 * or otherwise invalid. |
| 304 * | 310 * |
| 305 * Accessing any field or calling any method defined on [ErroneousElement] | 311 * Accessing any field or calling any method defined on [ErroneousElement] |
| (...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 return 'origin library(${getLibraryOrScriptName()})'; | 993 return 'origin library(${getLibraryOrScriptName()})'; |
| 988 } else { | 994 } else { |
| 989 return 'library(${getLibraryOrScriptName()})'; | 995 return 'library(${getLibraryOrScriptName()})'; |
| 990 } | 996 } |
| 991 } | 997 } |
| 992 | 998 |
| 993 int compareTo(LibraryElement other) { | 999 int compareTo(LibraryElement other) { |
| 994 if (this == other) return 0; | 1000 if (this == other) return 0; |
| 995 return getLibraryOrScriptName().compareTo(other.getLibraryOrScriptName()); | 1001 return getLibraryOrScriptName().compareTo(other.getLibraryOrScriptName()); |
| 996 } | 1002 } |
| 1003 | |
| 1004 Map collectInferredTypes(Compiler compiler) { | |
|
ahe
2013/11/27 19:28:53
This really doesn't seem to be an essential part o
sigurdm
2013/11/29 10:39:37
I agree
I have made a visitor
| |
| 1005 List contents = []; | |
| 1006 int size = compiler.backend.codeSizeCounter.generatedSize(this); | |
| 1007 if (size == 0) return null; | |
| 1008 | |
| 1009 forEachLocalMember((Element element) { | |
| 1010 Map info = element.collectInferredTypes(compiler); | |
| 1011 if (info != null) { | |
| 1012 contents.add(info); | |
| 1013 } | |
| 1014 }); | |
| 1015 | |
| 1016 String nameString = getLibraryName() == "" ? "<unnamed>" : getLibraryName(); | |
| 1017 contents.sort((Map m1, Map m2) => m1["name"].compareTo(m2["name"])); | |
| 1018 return { | |
| 1019 "type" : this.canonicalUri.toString(), | |
|
ahe
2013/11/27 19:28:53
Generally, try to create classes instead of simple
sigurdm
2013/11/29 10:39:37
Yes - the map'o'maps was more for drafting purpose
| |
| 1020 "kind" : "library", | |
| 1021 "name" : nameString, | |
| 1022 "size" : size, | |
| 1023 "modifiers" : "", | |
| 1024 "contents" : contents | |
| 1025 }; | |
| 1026 } | |
| 997 } | 1027 } |
| 998 | 1028 |
| 999 class PrefixElementX extends ElementX implements PrefixElement { | 1029 class PrefixElementX extends ElementX implements PrefixElement { |
| 1000 Token firstPosition; | 1030 Token firstPosition; |
| 1001 | 1031 |
| 1002 final ImportScope importScope = new ImportScope(); | 1032 final ImportScope importScope = new ImportScope(); |
| 1003 | 1033 |
| 1004 PrefixElementX(String prefix, Element enclosing, this.firstPosition) | 1034 PrefixElementX(String prefix, Element enclosing, this.firstPosition) |
| 1005 : super(prefix, ElementKind.PREFIX, enclosing); | 1035 : super(prefix, ElementKind.PREFIX, enclosing); |
| 1006 | 1036 |
| 1007 Element lookupLocalMember(String memberName) => importScope[memberName]; | 1037 Element lookupLocalMember(String memberName) => importScope[memberName]; |
| 1008 | 1038 |
| 1009 DartType computeType(Compiler compiler) => compiler.types.dynamicType; | 1039 DartType computeType(Compiler compiler) => compiler.types.dynamicType; |
| 1010 | 1040 |
| 1011 Token position() => firstPosition; | 1041 Token position() => firstPosition; |
| 1012 | 1042 |
| 1013 void addImport(Element element, Import import, DiagnosticListener listener) { | 1043 void addImport(Element element, Import import, DiagnosticListener listener) { |
| 1014 importScope.addImport(this, element, import, listener); | 1044 importScope.addImport(this, element, import, listener); |
| 1015 } | 1045 } |
| 1046 | |
| 1047 | |
|
ahe
2013/11/27 19:28:53
Extra lines.
sigurdm
2013/11/29 10:39:37
Done.
| |
| 1016 } | 1048 } |
| 1017 | 1049 |
| 1018 class TypedefElementX extends ElementX implements TypedefElement { | 1050 class TypedefElementX extends ElementX implements TypedefElement { |
| 1019 Typedef cachedNode; | 1051 Typedef cachedNode; |
| 1020 | 1052 |
| 1021 /** | 1053 /** |
| 1022 * The type of this typedef in which the type arguments are the type | 1054 * The type of this typedef in which the type arguments are the type |
| 1023 * variables. | 1055 * variables. |
| 1024 * | 1056 * |
| 1025 * This resembles the [ClassElement.thisType] though a typedef has no notion | 1057 * This resembles the [ClassElement.thisType] though a typedef has no notion |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1089 Scope buildScope() { | 1121 Scope buildScope() { |
| 1090 return new TypeDeclarationScope(enclosingElement.buildScope(), this); | 1122 return new TypeDeclarationScope(enclosingElement.buildScope(), this); |
| 1091 } | 1123 } |
| 1092 | 1124 |
| 1093 void checkCyclicReference(Compiler compiler) { | 1125 void checkCyclicReference(Compiler compiler) { |
| 1094 if (hasBeenCheckedForCycles) return; | 1126 if (hasBeenCheckedForCycles) return; |
| 1095 var visitor = new TypedefCyclicVisitor(compiler, this); | 1127 var visitor = new TypedefCyclicVisitor(compiler, this); |
| 1096 computeType(compiler).accept(visitor, null); | 1128 computeType(compiler).accept(visitor, null); |
| 1097 hasBeenCheckedForCycles = true; | 1129 hasBeenCheckedForCycles = true; |
| 1098 } | 1130 } |
| 1131 | |
| 1132 Map collectInferredTypes(Compiler compiler) { | |
| 1133 if (thisType == null) { | |
|
karlklose
2013/11/27 14:19:23
Merge this int l. 1136 using '?:' ?
sigurdm
2013/11/29 10:39:37
Done.
| |
| 1134 return null; | |
| 1135 } | |
| 1136 return { | |
| 1137 "type" : thisType.toString(), | |
| 1138 "kind" : "typedef", | |
| 1139 "name" : name, | |
| 1140 "modifiers" : "", | |
| 1141 }; | |
| 1142 } | |
| 1099 } | 1143 } |
| 1100 | 1144 |
| 1101 class VariableElementX extends ElementX implements VariableElement { | 1145 class VariableElementX extends ElementX implements VariableElement { |
| 1102 final VariableListElement variables; | 1146 final VariableListElement variables; |
| 1103 Expression cachedNode; // The send or the identifier in the variables list. | 1147 Expression cachedNode; // The send or the identifier in the variables list. |
| 1104 | 1148 |
| 1105 Modifiers get modifiers => variables.modifiers; | 1149 Modifiers get modifiers => variables.modifiers; |
| 1106 | 1150 |
| 1107 VariableElementX(String name, | 1151 VariableElementX(String name, |
| 1108 VariableListElement variables, | 1152 VariableListElement variables, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1133 return variables.computeType(compiler); | 1177 return variables.computeType(compiler); |
| 1134 } | 1178 } |
| 1135 | 1179 |
| 1136 DartType get type => variables.type; | 1180 DartType get type => variables.type; |
| 1137 | 1181 |
| 1138 bool isInstanceMember() => variables.isInstanceMember(); | 1182 bool isInstanceMember() => variables.isInstanceMember(); |
| 1139 | 1183 |
| 1140 // Note: cachedNode.getBeginToken() will not be correct in all | 1184 // Note: cachedNode.getBeginToken() will not be correct in all |
| 1141 // cases, for example, for function typed parameters. | 1185 // cases, for example, for function typed parameters. |
| 1142 Token position() => findMyName(variables.position()); | 1186 Token position() => findMyName(variables.position()); |
| 1187 | |
| 1188 Map collectInferredTypes(Compiler compiler) { | |
| 1189 if (type == null) { | |
| 1190 return null; | |
| 1191 } | |
| 1192 String modifiersString = modifiers.toString() == "" ? | |
| 1193 "" : modifiers.toString()+" "; | |
| 1194 List inferredType = [{ | |
| 1195 "kind" : "inferred type", | |
| 1196 "name" : "", | |
| 1197 "type" : compiler.typesTask.getGuaranteedTypeOfElement(this).toString(), | |
| 1198 "modifiers" : "" | |
| 1199 }]; | |
| 1200 return { | |
| 1201 "kind" : "field", | |
| 1202 "type" : type.toString(), | |
| 1203 "name" : name, | |
| 1204 "modifiers" : modifiersString, | |
| 1205 "contents" : inferredType | |
| 1206 }; | |
| 1207 } | |
| 1143 } | 1208 } |
| 1144 | 1209 |
| 1145 /** | 1210 /** |
| 1146 * Parameters in constructors that directly initialize fields. For example: | 1211 * Parameters in constructors that directly initialize fields. For example: |
| 1147 * [:A(this.field):]. | 1212 * [:A(this.field):]. |
| 1148 */ | 1213 */ |
| 1149 class FieldParameterElementX extends VariableElementX | 1214 class FieldParameterElementX extends VariableElementX |
| 1150 implements FieldParameterElement { | 1215 implements FieldParameterElement { |
| 1151 VariableElement fieldElement; | 1216 VariableElement fieldElement; |
| 1152 | 1217 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1542 } else { | 1607 } else { |
| 1543 return super.toString(); | 1608 return super.toString(); |
| 1544 } | 1609 } |
| 1545 } | 1610 } |
| 1546 | 1611 |
| 1547 bool get isAbstract { | 1612 bool get isAbstract { |
| 1548 return !modifiers.isExternal() && | 1613 return !modifiers.isExternal() && |
| 1549 (isFunction() || isAccessor()) && | 1614 (isFunction() || isAccessor()) && |
| 1550 _hasNoBody; | 1615 _hasNoBody; |
| 1551 } | 1616 } |
| 1617 | |
| 1618 Map collectInferredTypes(Compiler compiler) { | |
| 1619 CodeBuffer emittedCode = compiler.backend.emitCodeFor(this); | |
| 1620 if (emittedCode == null) { | |
| 1621 return null; | |
| 1622 } | |
| 1623 String modifiersString = modifiers.toString() == "" ? | |
| 1624 "" : modifiers.toString()+" "; | |
| 1625 String kindString = "function"; | |
| 1626 String nameString = name; | |
| 1627 if (isConstructor()) { | |
| 1628 nameString = name == "" ? "${enclosingElement.name}()" : | |
| 1629 "${enclosingElement.name}.$name"; | |
| 1630 kindString = "constructor"; | |
| 1631 } | |
| 1632 if (type == null) { | |
| 1633 return { | |
| 1634 "type" : "not used", | |
| 1635 "kind" : kindString, | |
| 1636 "name" : nameString, | |
| 1637 "modifiers" : modifiersString | |
| 1638 }; | |
| 1639 } | |
| 1640 List contents = []; | |
| 1641 FunctionSignature signature = computeSignature(compiler); | |
| 1642 signature.forEachParameter((parameter) { | |
| 1643 contents.add({ | |
| 1644 "kind" : "inferred", | |
| 1645 "name" : parameter.name, | |
| 1646 "modifiers" : "parameter type", | |
| 1647 "type" : | |
| 1648 compiler.typesTask.getGuaranteedTypeOfElement(parameter).toString() | |
| 1649 }); | |
| 1650 }); | |
| 1651 contents.add({ | |
| 1652 "kind" : "inferred", | |
| 1653 "name" : "", | |
| 1654 "modifiers" : "return type", | |
| 1655 "type" : | |
| 1656 compiler.typesTask.getGuaranteedReturnTypeOfElement(this).toString() | |
| 1657 }); | |
| 1658 contents.add({ | |
| 1659 "kind" : "inferred", | |
| 1660 "name" : "", | |
| 1661 "modifiers" : "side effects", | |
| 1662 "type" : compiler.world.getSideEffectsOfElement(this).toString() | |
| 1663 }); | |
| 1664 contents.add({ | |
| 1665 "name" : "Generated code", | |
| 1666 "presentation" : "code", | |
| 1667 "text" : emittedCode.getText() | |
| 1668 }); | |
| 1669 return { | |
| 1670 "type" : type.toString(), | |
| 1671 "kind" : kindString, | |
| 1672 "name" : nameString, | |
| 1673 "size" : emittedCode.length, | |
| 1674 "modifiers" : modifiersString, | |
| 1675 "contents" : contents | |
| 1676 }; | |
| 1677 } | |
| 1552 } | 1678 } |
| 1553 | 1679 |
| 1554 class ConstructorBodyElementX extends FunctionElementX | 1680 class ConstructorBodyElementX extends FunctionElementX |
| 1555 implements ConstructorBodyElement { | 1681 implements ConstructorBodyElement { |
| 1556 FunctionElement constructor; | 1682 FunctionElement constructor; |
| 1557 | 1683 |
| 1558 ConstructorBodyElementX(FunctionElement constructor) | 1684 ConstructorBodyElementX(FunctionElement constructor) |
| 1559 : this.constructor = constructor, | 1685 : this.constructor = constructor, |
| 1560 super(constructor.name, | 1686 super(constructor.name, |
| 1561 ElementKind.GENERATIVE_CONSTRUCTOR_BODY, | 1687 ElementKind.GENERATIVE_CONSTRUCTOR_BODY, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1701 int supertypeLoadState; | 1827 int supertypeLoadState; |
| 1702 int resolutionState; | 1828 int resolutionState; |
| 1703 bool get isResolved => resolutionState == STATE_DONE; | 1829 bool get isResolved => resolutionState == STATE_DONE; |
| 1704 | 1830 |
| 1705 // backendMembers are members that have been added by the backend to simplify | 1831 // backendMembers are members that have been added by the backend to simplify |
| 1706 // compilation. They don't have any user-side counter-part. | 1832 // compilation. They don't have any user-side counter-part. |
| 1707 Link<Element> backendMembers = const Link<Element>(); | 1833 Link<Element> backendMembers = const Link<Element>(); |
| 1708 | 1834 |
| 1709 OrderedTypeSet allSupertypesAndSelf; | 1835 OrderedTypeSet allSupertypesAndSelf; |
| 1710 | 1836 |
| 1711 Link<DartType> get allSupertypes => allSupertypesAndSelf.supertypes; | 1837 Link<DartType> get allSupertypes { |
| 1838 assert(invariant(this, allSupertypesAndSelf != null, | |
|
ahe
2013/11/27 19:28:53
This invariant is unnecessary. You get a null poin
sigurdm
2013/11/29 10:39:37
Done.
| |
| 1839 message: "This class has not been resolved " | |
| 1840 "(trying to access allSupertypes)")); | |
| 1841 return allSupertypesAndSelf.supertypes; | |
| 1842 } | |
| 1712 | 1843 |
| 1713 int get hierarchyDepth => allSupertypesAndSelf.maxDepth; | 1844 int get hierarchyDepth => allSupertypesAndSelf.maxDepth; |
| 1714 | 1845 |
| 1715 BaseClassElementX(String name, | 1846 BaseClassElementX(String name, |
| 1716 Element enclosing, | 1847 Element enclosing, |
| 1717 this.id, | 1848 this.id, |
| 1718 int initialState) | 1849 int initialState) |
| 1719 : supertypeLoadState = initialState, | 1850 : supertypeLoadState = initialState, |
| 1720 resolutionState = initialState, | 1851 resolutionState = initialState, |
| 1721 super(name, ElementKind.CLASS, enclosing); | 1852 super(name, ElementKind.CLASS, enclosing); |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2094 for (ClassElement s = declaration; s != null; s = s.superclass) { | 2225 for (ClassElement s = declaration; s != null; s = s.superclass) { |
| 2095 if (identical(s, cls)) return true; | 2226 if (identical(s, cls)) return true; |
| 2096 } | 2227 } |
| 2097 return false; | 2228 return false; |
| 2098 } | 2229 } |
| 2099 | 2230 |
| 2100 bool isNative() => nativeTagInfo != null; | 2231 bool isNative() => nativeTagInfo != null; |
| 2101 void setNative(String name) { | 2232 void setNative(String name) { |
| 2102 nativeTagInfo = name; | 2233 nativeTagInfo = name; |
| 2103 } | 2234 } |
| 2235 | |
| 2236 Map collectInferredTypes(Compiler compiler) { | |
| 2237 String modifiersString = modifiers.toString() == "" ? | |
| 2238 "" : modifiers.toString()+" "; | |
| 2239 if (!isResolved) return null; | |
| 2240 String supersString = allSupertypes == null ? "" : | |
| 2241 "implements $allSupertypes"; | |
| 2242 List contents = []; | |
| 2243 forEachLocalMember((Element element) { | |
| 2244 Map info = element.collectInferredTypes(compiler); | |
| 2245 if (info != null) { | |
| 2246 contents.add(info); | |
| 2247 } | |
| 2248 }); | |
| 2249 if (contents.isEmpty) { | |
| 2250 return null; | |
| 2251 } | |
| 2252 contents.sort((Map m1, Map m2) => m1["name"].compareTo(m2["name"])); | |
| 2253 return { | |
| 2254 "kind" : "class", | |
| 2255 "name" : name, | |
| 2256 "type" : supersString, | |
| 2257 "modifiers" : modifiersString, | |
| 2258 "contents" : contents | |
| 2259 }; | |
| 2260 } | |
| 2104 } | 2261 } |
| 2105 | 2262 |
| 2106 abstract class ClassElementX extends BaseClassElementX { | 2263 abstract class ClassElementX extends BaseClassElementX { |
| 2107 // Lazily applied patch of class members. | 2264 // Lazily applied patch of class members. |
| 2108 ClassElement patch = null; | 2265 ClassElement patch = null; |
| 2109 ClassElement origin = null; | 2266 ClassElement origin = null; |
| 2110 | 2267 |
| 2111 Link<Element> localMembers = const Link<Element>(); | 2268 Link<Element> localMembers = const Link<Element>(); |
| 2112 final ScopeX localScope = new ScopeX(); | 2269 final ScopeX localScope = new ScopeX(); |
| 2113 | 2270 |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2386 | 2543 |
| 2387 MetadataAnnotation ensureResolved(Compiler compiler) { | 2544 MetadataAnnotation ensureResolved(Compiler compiler) { |
| 2388 if (resolutionState == STATE_NOT_STARTED) { | 2545 if (resolutionState == STATE_NOT_STARTED) { |
| 2389 compiler.resolver.resolveMetadataAnnotation(this); | 2546 compiler.resolver.resolveMetadataAnnotation(this); |
| 2390 } | 2547 } |
| 2391 return this; | 2548 return this; |
| 2392 } | 2549 } |
| 2393 | 2550 |
| 2394 String toString() => 'MetadataAnnotation($value, $resolutionState)'; | 2551 String toString() => 'MetadataAnnotation($value, $resolutionState)'; |
| 2395 } | 2552 } |
| OLD | NEW |