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

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

Issue 11571058: DeclarationMirror extended with metadata. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments. 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 mirrors_dart2js; 5 library mirrors_dart2js;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:uri'; 8 import 'dart:uri';
9 9
10 import '../../compiler.dart' as diagnostics; 10 import '../../compiler.dart' as diagnostics;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 } else if (type is FunctionType) { 72 } else if (type is FunctionType) {
73 return new Dart2JsFunctionTypeMirror(system, type, functionSignature); 73 return new Dart2JsFunctionTypeMirror(system, type, functionSignature);
74 } else if (type is VoidType) { 74 } else if (type is VoidType) {
75 return new Dart2JsVoidMirror(system, type); 75 return new Dart2JsVoidMirror(system, type);
76 } else if (type is TypedefType) { 76 } else if (type is TypedefType) {
77 return new Dart2JsTypedefMirror(system, type); 77 return new Dart2JsTypedefMirror(system, type);
78 } else if (type is MalformedType) { 78 } else if (type is MalformedType) {
79 // TODO(johnniwinther): We need a mirror on malformed types. 79 // TODO(johnniwinther): We need a mirror on malformed types.
80 return system.dynamicType; 80 return system.dynamicType;
81 } 81 }
82 throw new ArgumentError("Unexpected type $type of kind ${type.kind}"); 82 throw new InternalError("Unexpected type $type of kind ${type.kind}");
83 } 83 }
84 84
85 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors( 85 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors(
86 Dart2JsContainerMirror library, Element element) { 86 Dart2JsContainerMirror library, Element element) {
87 if (element is SynthesizedConstructorElement) { 87 if (element is SynthesizedConstructorElement) {
88 return const <Dart2JsMemberMirror>[]; 88 return const <Dart2JsMemberMirror>[];
89 } else if (element is VariableElement) { 89 } else if (element is VariableElement) {
90 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)]; 90 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)];
91 } else if (element is FunctionElement) { 91 } else if (element is FunctionElement) {
92 return <Dart2JsMemberMirror>[new Dart2JsMethodMirror(library, element)]; 92 return <Dart2JsMemberMirror>[new Dart2JsMethodMirror(library, element)];
93 } else if (element is AbstractFieldElement) { 93 } else if (element is AbstractFieldElement) {
94 var members = <Dart2JsMemberMirror>[]; 94 var members = <Dart2JsMemberMirror>[];
95 if (element.getter != null) { 95 if (element.getter != null) {
96 members.add(new Dart2JsMethodMirror(library, element.getter)); 96 members.add(new Dart2JsMethodMirror(library, element.getter));
97 } 97 }
98 if (element.setter != null) { 98 if (element.setter != null) {
99 members.add(new Dart2JsMethodMirror(library, element.setter)); 99 members.add(new Dart2JsMethodMirror(library, element.setter));
100 } 100 }
101 return members; 101 return members;
102 } 102 }
103 throw new ArgumentError( 103 throw new InternalError("Unexpected member type $element ${element.kind}");
104 "Unexpected member type $element ${element.kind}");
105 } 104 }
106 105
107 MethodMirror _convertElementMethodToMethodMirror(Dart2JsContainerMirror library, 106 MethodMirror _convertElementMethodToMethodMirror(Dart2JsContainerMirror library,
108 Element element) { 107 Element element) {
109 if (element is FunctionElement) { 108 if (element is FunctionElement) {
110 return new Dart2JsMethodMirror(library, element); 109 return new Dart2JsMethodMirror(library, element);
111 } else { 110 } else {
112 return null; 111 return null;
113 } 112 }
114 } 113 }
115 114
115 InstanceMirror _convertConstantToInstanceMirror(Dart2JsMirrorSystem mirrors,
116 Constant constant) {
117 if (constant is BoolConstant) {
118 return new Dart2JsBoolConstantMirror(mirrors, constant);
119 } else if (constant is NumConstant) {
120 return new Dart2JsNumConstantMirror(mirrors, constant);
121 } else if (constant is StringConstant) {
122 return new Dart2JsStringConstantMirror(mirrors, constant);
123 } else if (constant is ListConstant) {
124 return new Dart2JsListConstantMirror(mirrors, constant);
125 } else if (constant is MapConstant) {
126 return new Dart2JsMapConstantMirror(mirrors, constant);
127 } else if (constant is TypeConstant) {
128 return new Dart2JsTypeConstantMirror(mirrors, constant);
129 } else if (constant is FunctionConstant) {
130 return new Dart2JsConstantMirror(mirrors, constant);
131 } else if (constant is NullConstant) {
132 return new Dart2JsNullConstantMirror(mirrors, constant);
133 } else if (constant is ConstructedConstant) {
134 return new Dart2JsConstructedConstantMirror(mirrors, constant);
135 }
136 throw new InternalError("Unexpected constant $constant");
137 }
138
116 class Dart2JsMethodKind { 139 class Dart2JsMethodKind {
117 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular"); 140 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular");
118 static const Dart2JsMethodKind GENERATIVE = 141 static const Dart2JsMethodKind GENERATIVE =
119 const Dart2JsMethodKind("generative"); 142 const Dart2JsMethodKind("generative");
120 static const Dart2JsMethodKind REDIRECTING = 143 static const Dart2JsMethodKind REDIRECTING =
121 const Dart2JsMethodKind("redirecting"); 144 const Dart2JsMethodKind("redirecting");
122 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const"); 145 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const");
123 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory"); 146 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory");
124 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter"); 147 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter");
125 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter"); 148 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter");
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 } 456 }
434 457
435 abstract class Dart2JsTypeMirror extends Dart2JsDeclarationMirror 458 abstract class Dart2JsTypeMirror extends Dart2JsDeclarationMirror
436 implements TypeMirror { 459 implements TypeMirror {
437 460
438 } 461 }
439 462
440 abstract class Dart2JsElementMirror extends Dart2JsDeclarationMirror { 463 abstract class Dart2JsElementMirror extends Dart2JsDeclarationMirror {
441 final Dart2JsMirrorSystem mirrors; 464 final Dart2JsMirrorSystem mirrors;
442 final Element _element; 465 final Element _element;
466 List<InstanceMirror> _metadata;
443 467
444 Dart2JsElementMirror(this.mirrors, this._element) { 468 Dart2JsElementMirror(this.mirrors, this._element) {
445 assert (mirrors != null); 469 assert (mirrors != null);
446 assert (_element != null); 470 assert (_element != null);
447 } 471 }
448 472
449 String get simpleName => _element.name.slowToString(); 473 String get simpleName => _element.name.slowToString();
450 474
451 String get displayName => simpleName; 475 String get displayName => simpleName;
452 476
453 SourceLocation get location => new Dart2JsSourceLocation( 477 SourceLocation get location => new Dart2JsSourceLocation(
454 _element.getCompilationUnit().script, 478 _element.getCompilationUnit().script,
455 mirrors.compiler.spanFromElement(_element)); 479 mirrors.compiler.spanFromElement(_element));
456 480
457 String toString() => _element.toString(); 481 String toString() => _element.toString();
458 482
459 int get hashCode => qualifiedName.hashCode; 483 int get hashCode => qualifiedName.hashCode;
484
485 List<InstanceMirror> get metadata {
486 if (_metadata == null) {
487 _metadata = <InstanceMirror>[];
488 for (MetadataAnnotation metadata in _element.metadata) {
489 metadata.ensureResolved(mirrors.compiler);
490 _metadata.add(
491 _convertConstantToInstanceMirror(mirrors, metadata.value));
492 }
493 }
494 // TODO(johnniwinther): Return an unmodifiable list instead.
495 return new List<InstanceMirror>.from(_metadata);
496 }
460 } 497 }
461 498
462 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror { 499 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror {
463 final Dart2JsMirrorSystem mirrors; 500 final Dart2JsMirrorSystem mirrors;
464 501
465 Dart2JsProxyMirror(this.mirrors); 502 Dart2JsProxyMirror(this.mirrors);
466 503
467 String get displayName => simpleName; 504 String get displayName => simpleName;
468 505
469 int get hashCode => qualifiedName.hashCode; 506 int get hashCode => qualifiedName.hashCode;
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
1079 } 1116 }
1080 1117
1081 1118
1082 //------------------------------------------------------------------------------ 1119 //------------------------------------------------------------------------------
1083 // Types 1120 // Types
1084 //------------------------------------------------------------------------------ 1121 //------------------------------------------------------------------------------
1085 1122
1086 abstract class Dart2JsTypeElementMirror extends Dart2JsProxyMirror 1123 abstract class Dart2JsTypeElementMirror extends Dart2JsProxyMirror
1087 implements Dart2JsTypeMirror { 1124 implements Dart2JsTypeMirror {
1088 final DartType _type; 1125 final DartType _type;
1126 List<InstanceMirror> _metadata;
1089 1127
1090 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type) 1128 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type)
1091 : super(system); 1129 : super(system);
1092 1130
1093 String get simpleName => _type.name.slowToString(); 1131 String get simpleName => _type.name.slowToString();
1094 1132
1095 SourceLocation get location { 1133 SourceLocation get location {
1096 var script = _type.element.getCompilationUnit().script; 1134 var script = _type.element.getCompilationUnit().script;
1097 return new Dart2JsSourceLocation(script, 1135 return new Dart2JsSourceLocation(script,
1098 mirrors.compiler.spanFromElement(_type.element)); 1136 mirrors.compiler.spanFromElement(_type.element));
(...skipping 25 matching lines...) Expand all
1124 1162
1125 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; 1163 Map<String, MethodMirror> get methods => const <String, MethodMirror>{};
1126 1164
1127 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; 1165 Map<String, MethodMirror> get getters => const <String, MethodMirror>{};
1128 1166
1129 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; 1167 Map<String, MethodMirror> get setters => const <String, MethodMirror>{};
1130 1168
1131 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; 1169 Map<String, VariableMirror> get variables => const <String, VariableMirror>{};
1132 1170
1133 ClassMirror get defaultFactory => null; 1171 ClassMirror get defaultFactory => null;
1172
1173 // TODO(johnniwinther): Should a type show the metadata of its declaration?
1174 List<InstanceMirror> get metadata {
1175 if (_metadata == null) {
1176 var _metadata = <InstanceMirror>[];
1177 for (MetadataAnnotation metadata in _type.element.metadata) {
1178 metadata.ensureResolved(mirrors.compiler);
1179 _metadata.add(
1180 _convertConstantToInstanceMirror(mirrors, metadata.value));
1181 }
1182 }
1183 // TODO(johnniwinther): Return an unmodifiable list instead.
1184 return new List<InstanceMirror>.from(_metadata);
1185 }
1134 } 1186 }
1135 1187
1136 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror 1188 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror
1137 implements ClassMirror { 1189 implements ClassMirror {
1138 List<TypeMirror> _typeArguments; 1190 List<TypeMirror> _typeArguments;
1139 1191
1140 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, 1192 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system,
1141 InterfaceType interfaceType) 1193 InterfaceType interfaceType)
1142 : super(system, interfaceType); 1194 : super(system, interfaceType);
1143 1195
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
1558 var node = _variable.variables.parseNode(_diagnosticListener); 1610 var node = _variable.variables.parseNode(_diagnosticListener);
1559 if (node != null) { 1611 if (node != null) {
1560 var span = mirrors.compiler.spanFromNode(node, script.uri); 1612 var span = mirrors.compiler.spanFromNode(node, script.uri);
1561 return new Dart2JsSourceLocation(script, span); 1613 return new Dart2JsSourceLocation(script, span);
1562 } else { 1614 } else {
1563 var span = mirrors.compiler.spanFromElement(_variable); 1615 var span = mirrors.compiler.spanFromElement(_variable);
1564 return new Dart2JsSourceLocation(script, span); 1616 return new Dart2JsSourceLocation(script, span);
1565 } 1617 }
1566 } 1618 }
1567 } 1619 }
1620
1621 ////////////////////////////////////////////////////////////////////////////////
1622 // Mirrors on constant values used for metadata.
1623 ////////////////////////////////////////////////////////////////////////////////
1624
1625 class Dart2JsConstantMirror extends InstanceMirror {
1626 final Dart2JsMirrorSystem mirrors;
1627 final Constant _constant;
1628
1629 Dart2JsConstantMirror(this.mirrors, this._constant);
1630
1631 ClassMirror get type {
1632 return new Dart2JsClassMirror(mirrors,
1633 _constant.computeType(mirrors.compiler).element);
1634 }
1635
1636 bool get hasReflectee => false;
1637
1638 get reflectee {
1639 // TODO(johnniwinther): Which exception/error should be thrown here?
1640 throw new UnsupportedError('InstanceMirror does not have a reflectee');
1641 }
1642
1643 Future<InstanceMirror> getField(String fieldName) {
1644 // TODO(johnniwinther): Which exception/error should be thrown here?
1645 throw new UnsupportedError('InstanceMirror does not have a reflectee');
1646 }
1647 }
1648
1649 class Dart2JsNullConstantMirror extends Dart2JsConstantMirror {
1650 Dart2JsNullConstantMirror(Dart2JsMirrorSystem mirrors, NullConstant constant)
1651 : super(mirrors, constant);
1652
1653 NullConstant get _constant => super._constant;
1654
1655 bool get hasReflectee => true;
1656
1657 get reflectee => null;
1658 }
1659
1660 class Dart2JsBoolConstantMirror extends Dart2JsConstantMirror {
1661 Dart2JsBoolConstantMirror(Dart2JsMirrorSystem mirrors, BoolConstant constant)
1662 : super(mirrors, constant);
1663
1664 BoolConstant get _constant => super._constant;
1665
1666 bool get hasReflectee => true;
1667
1668 get reflectee => _constant is TrueConstant;
1669 }
1670
1671 class Dart2JsStringConstantMirror extends Dart2JsConstantMirror {
1672 Dart2JsStringConstantMirror(Dart2JsMirrorSystem mirrors,
1673 StringConstant constant)
1674 : super(mirrors, constant);
1675
1676 StringConstant get _constant => super._constant;
1677
1678 bool get hasReflectee => true;
1679
1680 get reflectee => _constant.value.slowToString();
1681 }
1682
1683 class Dart2JsNumConstantMirror extends Dart2JsConstantMirror {
1684 Dart2JsNumConstantMirror(Dart2JsMirrorSystem mirrors,
1685 NumConstant constant)
1686 : super(mirrors, constant);
1687
1688 NumConstant get _constant => super._constant;
1689
1690 bool get hasReflectee => true;
1691
1692 get reflectee => _constant.value;
1693 }
1694
1695 class Dart2JsListConstantMirror extends Dart2JsConstantMirror
1696 implements ListInstanceMirror {
1697 Dart2JsListConstantMirror(Dart2JsMirrorSystem mirrors,
1698 ListConstant constant)
1699 : super(mirrors, constant);
1700
1701 ListConstant get _constant => super._constant;
1702
1703 int get length => _constant.length;
1704
1705 Future<InstanceMirror> operator[](int index) {
1706 if (index < 0) throw new RangeError('Negative index');
1707 if (index >= _constant.length) throw new RangeError('Index out of bounds');
1708 return new Future<InstanceMirror>.immediate(
1709 _convertConstantToInstanceMirror(mirrors, _constant.entries[index]));
1710 }
1711 }
1712
1713 class Dart2JsMapConstantMirror extends Dart2JsConstantMirror
1714 implements MapInstanceMirror {
1715 List<String> _list;
1716
1717 Dart2JsMapConstantMirror(Dart2JsMirrorSystem mirrors,
1718 MapConstant constant)
1719 : super(mirrors, constant);
1720
1721 MapConstant get _constant => super._constant;
1722
1723 void _ensureKeyList() {
1724 if (_list == null) {
1725 _list = new List<String>(_constant.keys.entries.length);
1726 int index = 0;
1727 for (StringConstant keyConstant in _constant.keys.entries) {
1728 _list[index] = keyConstant.value.slowToString();
1729 index++;
1730 }
1731 }
1732 }
1733
1734 int get length => _constant.length;
1735
1736 Collection<String> get keys {
1737 _ensureKeyList();
1738 // TODO(johnniwinther): Return an unmodifiable list instead.
1739 return new List<String>.from(_list);
1740 }
1741
1742 Future<InstanceMirror> operator[](String key) {
1743 _ensureKeyList();
1744 int index = _list.indexOf(key);
1745 if (index == -1) return null;
1746 return new Future<InstanceMirror>.immediate(
1747 _convertConstantToInstanceMirror(mirrors, _constant.values[index]));
1748 }
1749 }
1750
1751 class Dart2JsTypeConstantMirror extends Dart2JsConstantMirror
1752 implements TypeInstanceMirror {
1753
1754 Dart2JsTypeConstantMirror(Dart2JsMirrorSystem mirrors,
1755 TypeConstant constant)
1756 : super(mirrors, constant);
1757
1758 TypeConstant get _constant => super._constant;
1759
1760 TypeMirror get representedType => _convertTypeToTypeMirror(
1761 mirrors, _constant.representedType, mirrors.compiler.types.dynamicType);
1762 }
1763
1764 class Dart2JsConstructedConstantMirror extends Dart2JsConstantMirror {
1765 Map<String,Constant> _fieldMap;
1766
1767 Dart2JsConstructedConstantMirror(Dart2JsMirrorSystem mirrors,
1768 ConstructedConstant constant)
1769 : super(mirrors, constant);
1770
1771 ConstructedConstant get _constant => super._constant;
1772
1773 void _ensureFieldMap() {
ahe 2013/01/03 12:23:02 Inelegant cf my comment on _ensureKeyList.
Johnni Winther 2013/01/04 09:14:39 Changed _ensureFieldMap to _getFieldMap which retu
1774 if (_fieldMap == null) {
1775 _fieldMap = new LinkedHashMap<String,Constant>();
1776 if (identical(_constant.type.element.kind, ElementKind.CLASS)) {
1777 var index = 0;
1778 ClassElement element = _constant.type.element;
1779 element.forEachInstanceField((_, Element field) {
1780 String fieldName = field.name.slowToString();
1781 _fieldMap.putIfAbsent(fieldName, () => _constant.fields[index]);
1782 index++;
1783 }, includeBackendMembers: true, includeSuperMembers: true);
1784 }
1785 }
1786 }
1787
1788 Future<InstanceMirror> getField(String fieldName) {
1789 _ensureFieldMap();
1790 Constant fieldConstant = _fieldMap[fieldName];
1791 if (fieldConstant != null) {
1792 return new Future<InstanceMirror>.immediate(
1793 _convertConstantToInstanceMirror(mirrors, fieldConstant));
1794 }
1795 return super.getField(fieldName);
1796 }
1797 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698