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

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

Issue 11729004: Refactor location computation in dart2js mirrors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 426
427 427
428 //------------------------------------------------------------------------------ 428 //------------------------------------------------------------------------------
429 // Dart2Js specific extensions of mirror interfaces 429 // Dart2Js specific extensions of mirror interfaces
430 //------------------------------------------------------------------------------ 430 //------------------------------------------------------------------------------
431 431
432 abstract class Dart2JsMirror implements Mirror { 432 abstract class Dart2JsMirror implements Mirror {
433 Dart2JsMirrorSystem get mirrors; 433 Dart2JsMirrorSystem get mirrors;
434 } 434 }
435 435
436 abstract class Dart2JsDeclarationMirror 436 abstract class Dart2JsDeclarationMirror extends Dart2JsMirror
437 implements Dart2JsMirror, DeclarationMirror { 437 implements DeclarationMirror {
438 438
439 bool get isTopLevel => owner != null && owner is LibraryMirror; 439 bool get isTopLevel => owner != null && owner is LibraryMirror;
440 440
441 bool get isPrivate => _isPrivate(simpleName); 441 bool get isPrivate => _isPrivate(simpleName);
442
443 /**
444 * Returns the first token for the source of this declaration.
445 */
446 Token getBeginToken();
447
448 /**
449 * Returns the last token for the source of this declaration.
450 */
451 Token getEndToken();
452
453 /**
454 * Returns the script for the source of this declaration.
455 */
456 Script getScript();
457
458 SourceLocation get location {
459 Token beginToken = getBeginToken();
460 Script script = getScript();
461 SourceSpan span;
462 if (beginToken == null) {
463 span = new SourceSpan(script.uri, 0, 0);
464 } else {
465 Token endToken = getEndToken();
466 span = mirrors.compiler.spanFromTokens(beginToken, endToken, script.uri);
467 }
468 return new Dart2JsSourceLocation(script, span);
469 }
442 } 470 }
443 471
444 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror 472 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror
445 implements MemberMirror { 473 implements MemberMirror {
446 474
447 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element) 475 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element)
448 : super(system, element); 476 : super(system, element);
449 477
450 bool get isConstructor => false; 478 bool get isConstructor => false;
451 479
(...skipping 18 matching lines...) Expand all
470 498
471 Dart2JsElementMirror(this.mirrors, this._element) { 499 Dart2JsElementMirror(this.mirrors, this._element) {
472 assert (mirrors != null); 500 assert (mirrors != null);
473 assert (_element != null); 501 assert (_element != null);
474 } 502 }
475 503
476 String get simpleName => _element.name.slowToString(); 504 String get simpleName => _element.name.slowToString();
477 505
478 String get displayName => simpleName; 506 String get displayName => simpleName;
479 507
480 SourceLocation get location => new Dart2JsSourceLocation( 508 Token getBeginToken() {
481 _element.getCompilationUnit().script, 509 if (!_element.metadata.isEmpty) {
482 mirrors.compiler.spanFromElement(_element)); 510 for (MetadataAnnotation metadata in _element.metadata) {
511 if (metadata.beginToken != null) {
512 return metadata.beginToken;
513 }
514 }
515 }
516 Node node = _element.parseNode(mirrors.compiler);
ahe 2013/01/03 18:58:21 This is brittle as it can cause a parse error. Id
Johnni Winther 2013/01/04 09:57:55 Added a TODO.
517 if (node == null) {
518 return _element.position();
519 }
520 return node.getBeginToken();
521 }
522
523 Token getEndToken() {
524 Node node = _element.parseNode(mirrors.compiler);
525 if (node == null) {
526 return _element.position();
527 }
528 return node.getBeginToken();
529 }
530
531 Script getScript() => _element.getCompilationUnit().script;
483 532
484 String toString() => _element.toString(); 533 String toString() => _element.toString();
485 534
486 int get hashCode => qualifiedName.hashCode; 535 int get hashCode => qualifiedName.hashCode;
487 536
488 List<InstanceMirror> get metadata { 537 List<InstanceMirror> get metadata {
489 if (_metadata == null) { 538 if (_metadata == null) {
490 _metadata = <InstanceMirror>[]; 539 _metadata = <InstanceMirror>[];
491 for (MetadataAnnotation metadata in _element.metadata) { 540 for (MetadataAnnotation metadata in _element.metadata) {
492 metadata.ensureResolved(mirrors.compiler); 541 metadata.ensureResolved(mirrors.compiler);
493 _metadata.add( 542 _metadata.add(
494 _convertConstantToInstanceMirror(mirrors, metadata.value)); 543 _convertConstantToInstanceMirror(mirrors, metadata.value));
495 } 544 }
496 } 545 }
497 // TODO(johnniwinther): Return an unmodifiable list instead. 546 // TODO(johnniwinther): Return an unmodifiable list instead.
498 return new List<InstanceMirror>.from(_metadata); 547 return new List<InstanceMirror>.from(_metadata);
499 } 548 }
500 } 549 }
501 550
502 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror {
503 final Dart2JsMirrorSystem mirrors;
504
505 Dart2JsProxyMirror(this.mirrors);
506
507 String get displayName => simpleName;
508
509 int get hashCode => qualifiedName.hashCode;
510 }
511
512 //------------------------------------------------------------------------------ 551 //------------------------------------------------------------------------------
513 // Mirror system implementation. 552 // Mirror system implementation.
514 //------------------------------------------------------------------------------ 553 //------------------------------------------------------------------------------
515 554
516 class Dart2JsMirrorSystem implements MirrorSystem, Dart2JsMirror { 555 class Dart2JsMirrorSystem implements MirrorSystem {
517 final api.Compiler compiler; 556 final api.Compiler compiler;
518 Map<String, Dart2JsLibraryMirror> _libraries; 557 Map<String, Dart2JsLibraryMirror> _libraries;
519 Map<LibraryElement, Dart2JsLibraryMirror> _libraryMap; 558 Map<LibraryElement, Dart2JsLibraryMirror> _libraryMap;
520 559
521 Dart2JsMirrorSystem(this.compiler) 560 Dart2JsMirrorSystem(this.compiler)
522 : _libraryMap = new Map<LibraryElement, Dart2JsLibraryMirror>(); 561 : _libraryMap = new Map<LibraryElement, Dart2JsLibraryMirror>();
523 562
524 void _ensureLibraries() { 563 void _ensureLibraries() {
525 if (_libraries == null) { 564 if (_libraries == null) {
526 _libraries = <String, Dart2JsLibraryMirror>{}; 565 _libraries = <String, Dart2JsLibraryMirror>{};
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 } 711 }
673 }); 712 });
674 } 713 }
675 } 714 }
676 715
677 Map<String, ClassMirror> get classes { 716 Map<String, ClassMirror> get classes {
678 _ensureClasses(); 717 _ensureClasses();
679 return new ImmutableMapWrapper<String, ClassMirror>(_classes); 718 return new ImmutableMapWrapper<String, ClassMirror>(_classes);
680 } 719 }
681 720
682 SourceLocation get location { 721 Token getBeginToken() {
ahe 2013/01/03 18:58:21 Document what you do here.
Johnni Winther 2013/01/04 09:57:55 Done.
683 var script = _library.getCompilationUnit().script;
684 SourceSpan span;
685 if (_library.libraryTag != null) { 722 if (_library.libraryTag != null) {
686 span = mirrors.compiler.spanFromNode(_library.libraryTag, script.uri); 723 return _library.libraryTag.getBeginToken();
687 } else { 724 } else if (!_library.tags.isEmpty) {
688 span = new SourceSpan(script.uri, 0, 0); 725 return _library.tags.reverse().head.getBeginToken();
689 } 726 }
690 return new Dart2JsSourceLocation(script, span); 727 return null;
728 }
729
730 Token getEndToken() {
731 if (!_library.tags.isEmpty) {
732 return _library.tags.head.getEndToken();
733 }
734 return null;
691 } 735 }
692 } 736 }
693 737
694 class Dart2JsSourceLocation implements SourceLocation { 738 class Dart2JsSourceLocation implements SourceLocation {
695 final Script _script; 739 final Script _script;
696 final SourceSpan _span; 740 final SourceSpan _span;
697 int _line; 741 int _line;
698 int _column; 742 int _column;
699 743
700 Dart2JsSourceLocation(this._script, this._span); 744 Dart2JsSourceLocation(this._script, this._span);
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 906
863 Dart2JsClassMirror.fromLibrary(Dart2JsLibraryMirror library, 907 Dart2JsClassMirror.fromLibrary(Dart2JsLibraryMirror library,
864 ClassElement _class) 908 ClassElement _class)
865 : this.library = library, 909 : this.library = library,
866 super(library.mirrors, _class); 910 super(library.mirrors, _class);
867 911
868 DeclarationMirror get owner => library; 912 DeclarationMirror get owner => library;
869 913
870 String get qualifiedName => '${library.qualifiedName}.${simpleName}'; 914 String get qualifiedName => '${library.qualifiedName}.${simpleName}';
871 915
872 SourceLocation get location {
873 if (_class is PartialClassElement) {
874 var node = _class.parseNode(mirrors.compiler);
875 if (node != null) {
876 var script = _class.getCompilationUnit().script;
877 var span = mirrors.compiler.spanFromNode(node, script.uri);
878 return new Dart2JsSourceLocation(script, span);
879 }
880 }
881 return super.location;
882 }
883
884 void _ensureMembers() { 916 void _ensureMembers() {
885 if (_members == null) { 917 if (_members == null) {
886 _members = <String, Dart2JsMemberMirror>{}; 918 _members = <String, Dart2JsMemberMirror>{};
887 _class.forEachMember((_, e) { 919 _class.forEachMember((_, e) {
888 for (var member in _convertElementMemberToMemberMirrors(this, e)) { 920 for (var member in _convertElementMemberToMemberMirrors(this, e)) {
889 assert(!_members.containsKey(member.simpleName)); 921 assert(!_members.containsKey(member.simpleName));
890 _members[member.simpleName] = member; 922 _members[member.simpleName] = member;
891 } 923 }
892 }); 924 });
893 } 925 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 1030
999 Dart2JsTypedefMirror.fromLibrary(Dart2JsLibraryMirror library, 1031 Dart2JsTypedefMirror.fromLibrary(Dart2JsLibraryMirror library,
1000 TypedefType _typedef) 1032 TypedefType _typedef)
1001 : this._library = library, 1033 : this._library = library,
1002 super(library.mirrors, _typedef); 1034 super(library.mirrors, _typedef);
1003 1035
1004 TypedefType get _typedef => _type; 1036 TypedefType get _typedef => _type;
1005 1037
1006 String get qualifiedName => '${library.qualifiedName}.${simpleName}'; 1038 String get qualifiedName => '${library.qualifiedName}.${simpleName}';
1007 1039
1008 SourceLocation get location {
1009 var node = _typedef.element.parseNode(_diagnosticListener);
1010 if (node != null) {
1011 var script = _typedef.element.getCompilationUnit().script;
1012 var span = mirrors.compiler.spanFromNode(node, script.uri);
1013 return new Dart2JsSourceLocation(script, span);
1014 }
1015 return super.location;
1016 }
1017
1018 LibraryMirror get library => _library; 1040 LibraryMirror get library => _library;
1019 1041
1020 bool get isTypedef => true; 1042 bool get isTypedef => true;
1021 1043
1022 List<TypeMirror> get typeArguments { 1044 List<TypeMirror> get typeArguments {
1023 throw new UnsupportedError( 1045 throw new UnsupportedError(
1024 'Declarations do not have type arguments'); 1046 'Declarations do not have type arguments');
1025 } 1047 }
1026 1048
1027 List<TypeVariableMirror> get typeVariables { 1049 List<TypeVariableMirror> get typeVariables {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 } 1138 }
1117 return qualifiedName == other.qualifiedName; 1139 return qualifiedName == other.qualifiedName;
1118 } 1140 }
1119 } 1141 }
1120 1142
1121 1143
1122 //------------------------------------------------------------------------------ 1144 //------------------------------------------------------------------------------
1123 // Types 1145 // Types
1124 //------------------------------------------------------------------------------ 1146 //------------------------------------------------------------------------------
1125 1147
1126 abstract class Dart2JsTypeElementMirror extends Dart2JsProxyMirror 1148 abstract class Dart2JsTypeElementMirror extends Dart2JsElementMirror
1127 implements Dart2JsTypeMirror { 1149 implements Dart2JsTypeMirror {
1128 final DartType _type; 1150 final DartType _type;
1129 List<InstanceMirror> _metadata;
1130 1151
1131 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type) 1152 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, DartType type)
1132 : super(system); 1153 : super(system, type.element),
1154 this._type = type;
1133 1155
1134 String get simpleName => _type.name.slowToString(); 1156 String get simpleName => _type.name.slowToString();
1135 1157
1136 SourceLocation get location {
1137 var script = _type.element.getCompilationUnit().script;
1138 return new Dart2JsSourceLocation(script,
1139 mirrors.compiler.spanFromElement(_type.element));
1140 }
1141
1142 DeclarationMirror get owner => library; 1158 DeclarationMirror get owner => library;
1143 1159
1144 LibraryMirror get library { 1160 LibraryMirror get library {
1145 return mirrors._getLibrary(_type.element.getLibrary()); 1161 return mirrors._getLibrary(_type.element.getLibrary());
1146 } 1162 }
1147 1163
1148 bool get isObject => false; 1164 bool get isObject => false;
1149 1165
1150 bool get isVoid => false; 1166 bool get isVoid => false;
1151 1167
1152 bool get isDynamic => false; 1168 bool get isDynamic => false;
1153 1169
1154 bool get isTypeVariable => false; 1170 bool get isTypeVariable => false;
1155 1171
1156 bool get isTypedef => false; 1172 bool get isTypedef => false;
1157 1173
1158 bool get isFunction => false; 1174 bool get isFunction => false;
1159 1175
1160 String toString() => _type.element.toString(); 1176 String toString() => _type.toString();
1161 1177
1162 Map<String, MemberMirror> get members => const <String, MemberMirror>{}; 1178 Map<String, MemberMirror> get members => const <String, MemberMirror>{};
1163 1179
1164 Map<String, MethodMirror> get constructors => const <String, MethodMirror>{}; 1180 Map<String, MethodMirror> get constructors => const <String, MethodMirror>{};
1165 1181
1166 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; 1182 Map<String, MethodMirror> get methods => const <String, MethodMirror>{};
1167 1183
1168 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; 1184 Map<String, MethodMirror> get getters => const <String, MethodMirror>{};
1169 1185
1170 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; 1186 Map<String, MethodMirror> get setters => const <String, MethodMirror>{};
1171 1187
1172 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; 1188 Map<String, VariableMirror> get variables => const <String, VariableMirror>{};
1173 1189
1174 ClassMirror get defaultFactory => null; 1190 ClassMirror get defaultFactory => null;
1175
1176 // TODO(johnniwinther): Should a type show the metadata of its declaration?
1177 List<InstanceMirror> get metadata {
1178 if (_metadata == null) {
1179 var _metadata = <InstanceMirror>[];
1180 for (MetadataAnnotation metadata in _type.element.metadata) {
1181 metadata.ensureResolved(mirrors.compiler);
1182 _metadata.add(
1183 _convertConstantToInstanceMirror(mirrors, metadata.value));
1184 }
1185 }
1186 // TODO(johnniwinther): Return an unmodifiable list instead.
1187 return new List<InstanceMirror>.from(_metadata);
1188 }
1189 } 1191 }
1190 1192
1191 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror 1193 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror
1192 implements ClassMirror { 1194 implements ClassMirror {
1193 List<TypeMirror> _typeArguments; 1195 List<TypeMirror> _typeArguments;
1194 1196
1195 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, 1197 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system,
1196 InterfaceType interfaceType) 1198 InterfaceType interfaceType)
1197 : super(system, interfaceType); 1199 : super(system, interfaceType);
1198 1200
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
1559 1561
1560 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; 1562 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING;
1561 1563
1562 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY; 1564 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY;
1563 1565
1564 bool get isGetter => _kind == Dart2JsMethodKind.GETTER; 1566 bool get isGetter => _kind == Dart2JsMethodKind.GETTER;
1565 1567
1566 bool get isSetter => _kind == Dart2JsMethodKind.SETTER; 1568 bool get isSetter => _kind == Dart2JsMethodKind.SETTER;
1567 1569
1568 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR; 1570 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR;
1569
1570 SourceLocation get location {
1571 var node = _function.parseNode(_diagnosticListener);
1572 if (node != null) {
1573 var script = _function.getCompilationUnit().script;
1574 var span = mirrors.compiler.spanFromNode(node, script.uri);
1575 return new Dart2JsSourceLocation(script, span);
1576 }
1577 return super.location;
1578 }
1579
1580 } 1571 }
1581 1572
1582 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { 1573 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror {
1583 Dart2JsContainerMirror _objectMirror; 1574 Dart2JsContainerMirror _objectMirror;
1584 VariableElement _variable; 1575 VariableElement _variable;
1585 1576
1586 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror, 1577 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror,
1587 VariableElement variable) 1578 VariableElement variable)
1588 : this._objectMirror = objectMirror, 1579 : this._objectMirror = objectMirror,
1589 this._variable = variable, 1580 this._variable = variable,
(...skipping 10 matching lines...) Expand all
1600 1591
1601 bool get isStatic => _variable.modifiers.isStatic(); 1592 bool get isStatic => _variable.modifiers.isStatic();
1602 1593
1603 bool get isFinal => _variable.modifiers.isFinal(); 1594 bool get isFinal => _variable.modifiers.isFinal();
1604 1595
1605 bool get isConst => _variable.modifiers.isConst(); 1596 bool get isConst => _variable.modifiers.isConst();
1606 1597
1607 TypeMirror get type => _convertTypeToTypeMirror(mirrors, 1598 TypeMirror get type => _convertTypeToTypeMirror(mirrors,
1608 _variable.computeType(mirrors.compiler), 1599 _variable.computeType(mirrors.compiler),
1609 mirrors.compiler.types.dynamicType); 1600 mirrors.compiler.types.dynamicType);
1610
1611 SourceLocation get location {
1612 var script = _variable.getCompilationUnit().script;
1613 var node = _variable.variables.parseNode(_diagnosticListener);
1614 if (node != null) {
1615 var span = mirrors.compiler.spanFromNode(node, script.uri);
1616 return new Dart2JsSourceLocation(script, span);
1617 } else {
1618 var span = mirrors.compiler.spanFromElement(_variable);
1619 return new Dart2JsSourceLocation(script, span);
1620 }
1621 }
1622 } 1601 }
1623 1602
1624 //////////////////////////////////////////////////////////////////////////////// 1603 ////////////////////////////////////////////////////////////////////////////////
1625 // Mirrors on constant values used for metadata. 1604 // Mirrors on constant values used for metadata.
1626 //////////////////////////////////////////////////////////////////////////////// 1605 ////////////////////////////////////////////////////////////////////////////////
1627 1606
1628 class Dart2JsConstantMirror extends InstanceMirror { 1607 class Dart2JsConstantMirror extends InstanceMirror {
1629 final Dart2JsMirrorSystem mirrors; 1608 final Dart2JsMirrorSystem mirrors;
1630 final Constant _constant; 1609 final Constant _constant;
1631 1610
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1791 Future<InstanceMirror> getField(String fieldName) { 1770 Future<InstanceMirror> getField(String fieldName) {
1792 _ensureFieldMap(); 1771 _ensureFieldMap();
1793 Constant fieldConstant = _fieldMap[fieldName]; 1772 Constant fieldConstant = _fieldMap[fieldName];
1794 if (fieldConstant != null) { 1773 if (fieldConstant != null) {
1795 return new Future<InstanceMirror>.immediate( 1774 return new Future<InstanceMirror>.immediate(
1796 _convertConstantToInstanceMirror(mirrors, fieldConstant)); 1775 _convertConstantToInstanceMirror(mirrors, fieldConstant));
1797 } 1776 }
1798 return super.getField(fieldName); 1777 return super.getField(fieldName);
1799 } 1778 }
1800 } 1779 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698