Chromium Code Reviews| 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 mirrors_dart2js; | 5 library mirrors_dart2js; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection' show LinkedHashMap; | 8 import 'dart:collection' show LinkedHashMap; |
| 9 | 9 |
| 10 import '../../compiler.dart' as api; | 10 import '../../compiler.dart' as api; |
| 11 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
| 12 import '../apiimpl.dart' as apiimpl; | 12 import '../apiimpl.dart' as apiimpl; |
| 13 import '../scanner/scannerlib.dart' hide SourceString; | 13 import '../scanner/scannerlib.dart' hide SourceString; |
| 14 import '../resolution/resolution.dart' show Scope; | |
| 14 import '../dart2jslib.dart'; | 15 import '../dart2jslib.dart'; |
| 15 import '../dart_types.dart'; | 16 import '../dart_types.dart'; |
| 16 import '../source_file.dart'; | 17 import '../source_file.dart'; |
| 17 import '../tree/tree.dart'; | 18 import '../tree/tree.dart'; |
| 18 import '../util/util.dart' show Spannable, Link; | 19 import '../util/util.dart' show Spannable, Link; |
| 19 import '../util/characters.dart' show $CR, $LF; | 20 import '../util/characters.dart' show $CR, $LF; |
| 20 | 21 |
| 21 import 'mirrors.dart'; | 22 import 'mirrors.dart'; |
| 22 import 'mirrors_util.dart'; | 23 import 'mirrors_util.dart'; |
| 23 import 'util.dart'; | 24 import 'util.dart'; |
| 24 | 25 |
| 25 //------------------------------------------------------------------------------ | 26 //------------------------------------------------------------------------------ |
| 26 // Utility types and functions for the dart2js mirror system | 27 // Utility types and functions for the dart2js mirror system |
| 27 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
| 28 | 29 |
| 29 bool _isPrivate(String name) { | 30 bool _isPrivate(String name) { |
| 30 return name.startsWith('_'); | 31 return name.startsWith('_'); |
| 31 } | 32 } |
| 32 | 33 |
| 33 List<ParameterMirror> _parametersFromFunctionSignature( | 34 List<ParameterMirror> _parametersFromFunctionSignature( |
| 34 Dart2JsMirrorSystem system, | 35 Dart2JsMirrorSystem system, |
| 35 Dart2JsMethodMirror method, | 36 Dart2JsMethodMirror method, |
| 36 FunctionSignature signature) { | 37 FunctionSignature signature) { |
| 37 var parameters = <ParameterMirror>[]; | 38 var parameters = <ParameterMirror>[]; |
| 38 Link<Element> link = signature.requiredParameters; | 39 Link<Element> link = signature.requiredParameters; |
| 39 while (!link.isEmpty) { | 40 while (!link.isEmpty) { |
| 40 parameters.add(new Dart2JsParameterMirror( | 41 parameters.add(new Dart2JsParameterMirror( |
| 41 system, method, link.head, false, false)); | 42 system, method, link.head, isOptional: false, isNamed: false)); |
| 42 link = link.tail; | 43 link = link.tail; |
| 43 } | 44 } |
| 44 link = signature.optionalParameters; | 45 link = signature.optionalParameters; |
| 45 bool isNamed = signature.optionalParametersAreNamed; | 46 bool isNamed = signature.optionalParametersAreNamed; |
| 46 while (!link.isEmpty) { | 47 while (!link.isEmpty) { |
| 47 parameters.add(new Dart2JsParameterMirror( | 48 parameters.add(new Dart2JsParameterMirror( |
| 48 system, method, link.head, true, isNamed)); | 49 system, method, link.head, isOptional: true, isNamed: isNamed)); |
| 49 link = link.tail; | 50 link = link.tail; |
| 50 } | 51 } |
| 51 return parameters; | 52 return parameters; |
| 52 } | 53 } |
| 53 | 54 |
| 54 Dart2JsTypeMirror _convertTypeToTypeMirror( | 55 Dart2JsTypeMirror _convertTypeToTypeMirror( |
| 55 Dart2JsMirrorSystem system, | 56 Dart2JsMirrorSystem system, |
| 56 DartType type, | 57 DartType type, |
| 57 InterfaceType defaultType, | 58 InterfaceType defaultType, |
| 58 [FunctionSignature functionSignature]) { | 59 [FunctionSignature functionSignature]) { |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 span = new SourceSpan(script.uri, 0, 0); | 342 span = new SourceSpan(script.uri, 0, 0); |
| 342 } else { | 343 } else { |
| 343 Token endToken = getEndToken(); | 344 Token endToken = getEndToken(); |
| 344 span = mirrors.compiler.spanFromTokens(beginToken, endToken, script.uri); | 345 span = mirrors.compiler.spanFromTokens(beginToken, endToken, script.uri); |
| 345 } | 346 } |
| 346 return new Dart2JsSourceLocation(script, span); | 347 return new Dart2JsSourceLocation(script, span); |
| 347 } | 348 } |
| 348 | 349 |
| 349 String toString() => _element.toString(); | 350 String toString() => _element.toString(); |
| 350 | 351 |
| 351 int get hashCode => qualifiedName.hashCode; | |
| 352 | |
| 353 void _appendCommentTokens(Token commentToken) { | 352 void _appendCommentTokens(Token commentToken) { |
| 354 while (commentToken != null && commentToken.kind == COMMENT_TOKEN) { | 353 while (commentToken != null && commentToken.kind == COMMENT_TOKEN) { |
| 355 _metadata.add(new Dart2JsCommentInstanceMirror( | 354 _metadata.add(new Dart2JsCommentInstanceMirror( |
| 356 mirrors, commentToken.slowToString())); | 355 mirrors, commentToken.slowToString())); |
| 357 commentToken = commentToken.next; | 356 commentToken = commentToken.next; |
| 358 } | 357 } |
| 359 } | 358 } |
| 360 | 359 |
| 361 List<InstanceMirror> get metadata { | 360 List<InstanceMirror> get metadata { |
| 362 if (_metadata == null) { | 361 if (_metadata == null) { |
| 363 _metadata = <InstanceMirror>[]; | 362 _metadata = <InstanceMirror>[]; |
| 364 for (MetadataAnnotation metadata in _element.metadata) { | 363 for (MetadataAnnotation metadata in _element.metadata) { |
| 365 _appendCommentTokens(mirrors.compiler.commentMap[metadata.beginToken]); | 364 _appendCommentTokens(mirrors.compiler.commentMap[metadata.beginToken]); |
| 366 metadata.ensureResolved(mirrors.compiler); | 365 metadata.ensureResolved(mirrors.compiler); |
| 367 _metadata.add( | 366 _metadata.add( |
| 368 _convertConstantToInstanceMirror(mirrors, metadata.value)); | 367 _convertConstantToInstanceMirror(mirrors, metadata.value)); |
| 369 } | 368 } |
| 370 _appendCommentTokens(mirrors.compiler.commentMap[getBeginToken()]); | 369 _appendCommentTokens(mirrors.compiler.commentMap[getBeginToken()]); |
| 371 } | 370 } |
| 372 // TODO(johnniwinther): Return an unmodifiable list instead. | 371 // TODO(johnniwinther): Return an unmodifiable list instead. |
| 373 return new List<InstanceMirror>.from(_metadata); | 372 return new List<InstanceMirror>.from(_metadata); |
| 374 } | 373 } |
| 374 | |
| 375 /** | |
| 376 * Looks up the potentially qualified [name] in the scope of this declaration. | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: I think you can omit this doc comment since i
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 377 * For methods and constructors, the scope includes the parameters. | |
| 378 */ | |
| 379 DeclarationMirror lookupInScope(String name) { | |
| 380 // TODO(johnniwinther): Support lookup of constructors. | |
|
Andrei Mouravski
2013/07/01 17:46:26
Could you file a tracking bug for this and cc me?
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 381 SourceString sourceName = new SourceString(name); | |
| 382 Scope scope = _element.buildScope(); | |
| 383 Element result = scope.lookup(sourceName); | |
| 384 if (result == null) return null; | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: How about:
if (result != null && result.isPr
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 385 if (result.isPrefix()) { | |
| 386 PrefixElement prefix = result; | |
| 387 result = prefix.lookupLocalMember(sourceName); | |
| 388 } | |
| 389 if (result == null) return null; | |
| 390 return _convertElementToDeclarationMirror(mirrors, result); | |
| 391 } | |
| 392 | |
| 393 bool operator ==(var other) { | |
| 394 if (identical(this, other)) return true; | |
| 395 if (other == null) return false; | |
| 396 if (other is! Dart2JsElementMirror) return false; | |
| 397 return _element == other._element && owner == other.owner; | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: I'd move the everything past the && to a new
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 398 } | |
| 399 | |
| 400 int get hashCode { | |
| 401 return 13 * _element.hashCode + 17 * owner.hashCode; | |
| 402 } | |
| 375 } | 403 } |
| 376 | 404 |
| 377 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror | 405 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror |
| 378 implements MemberMirror { | 406 implements MemberMirror { |
| 379 | 407 |
| 380 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element) | 408 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element) |
| 381 : super(system, element); | 409 : super(system, element); |
| 382 | 410 |
| 383 bool get isConstructor => false; | 411 bool get isConstructor => false; |
| 384 | 412 |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 658 | 686 |
| 659 class Dart2JsParameterMirror extends Dart2JsMemberMirror | 687 class Dart2JsParameterMirror extends Dart2JsMemberMirror |
| 660 implements ParameterMirror { | 688 implements ParameterMirror { |
| 661 final MethodMirror _method; | 689 final MethodMirror _method; |
| 662 final bool isOptional; | 690 final bool isOptional; |
| 663 final bool isNamed; | 691 final bool isNamed; |
| 664 | 692 |
| 665 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system, | 693 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system, |
| 666 MethodMirror method, | 694 MethodMirror method, |
| 667 VariableElement element, | 695 VariableElement element, |
| 668 bool isOptional, | 696 {bool isOptional: false, |
| 669 bool isNamed) { | 697 bool isNamed: false}) { |
| 670 if (element is FieldParameterElement) { | 698 if (element is FieldParameterElement) { |
| 671 return new Dart2JsFieldParameterMirror(system, | 699 return new Dart2JsFieldParameterMirror(system, |
| 672 method, element, isOptional, isNamed); | 700 method, element, isOptional, isNamed); |
| 673 } | 701 } |
| 674 return new Dart2JsParameterMirror._normal(system, | 702 return new Dart2JsParameterMirror._normal(system, |
| 675 method, element, isOptional, isNamed); | 703 method, element, isOptional, isNamed); |
| 676 } | 704 } |
| 677 | 705 |
| 678 Dart2JsParameterMirror._normal(Dart2JsMirrorSystem system, | 706 Dart2JsParameterMirror._normal(Dart2JsMirrorSystem system, |
| 679 this._method, | 707 this._method, |
| (...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1378 | 1406 |
| 1379 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; | 1407 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; |
| 1380 | 1408 |
| 1381 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY; | 1409 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY; |
| 1382 | 1410 |
| 1383 bool get isGetter => _kind == Dart2JsMethodKind.GETTER; | 1411 bool get isGetter => _kind == Dart2JsMethodKind.GETTER; |
| 1384 | 1412 |
| 1385 bool get isSetter => _kind == Dart2JsMethodKind.SETTER; | 1413 bool get isSetter => _kind == Dart2JsMethodKind.SETTER; |
| 1386 | 1414 |
| 1387 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR; | 1415 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR; |
| 1416 | |
| 1417 DeclarationMirror lookupInScope(String name) { | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit:
lookupInScope(String name) =>
parameters
Johnni Winther
2013/07/03 05:47:57
I find such use of untyped inlined closures very h
Andrei Mouravski
2013/07/03 07:18:57
Well, you can type them, but I think the actual se
Johnni Winther
2013/07/03 12:58:01
Tried to change this, but I got weird type errors!
Andrei Mouravski
2013/07/03 17:49:48
That's strange. Could I ask you to file a bug for
| |
| 1418 for (ParameterMirror parameter in parameters) { | |
| 1419 if (parameter.simpleName == name) { | |
| 1420 return parameter; | |
| 1421 } | |
| 1422 } | |
| 1423 return super.lookupInScope(name); | |
| 1424 } | |
| 1388 } | 1425 } |
| 1389 | 1426 |
| 1390 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { | 1427 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { |
| 1391 Dart2JsContainerMirror _objectMirror; | 1428 Dart2JsContainerMirror _objectMirror; |
| 1392 VariableElement _variable; | 1429 VariableElement _variable; |
| 1393 | 1430 |
| 1394 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror, | 1431 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror, |
| 1395 VariableElement variable) | 1432 VariableElement variable) |
| 1396 : this._objectMirror = objectMirror, | 1433 : this._objectMirror = objectMirror, |
| 1397 this._variable = variable, | 1434 this._variable = variable, |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1648 classElement.ensureResolved(library.mirrors.compiler); | 1685 classElement.ensureResolved(library.mirrors.compiler); |
| 1649 return [new Dart2JsClassMirror.fromLibrary(library, classElement)]; | 1686 return [new Dart2JsClassMirror.fromLibrary(library, classElement)]; |
| 1650 } else if (e.isTypedef()) { | 1687 } else if (e.isTypedef()) { |
| 1651 return [new Dart2JsTypedefMirror.fromLibrary( | 1688 return [new Dart2JsTypedefMirror.fromLibrary( |
| 1652 library, e.computeType(library.mirrors.compiler))]; | 1689 library, e.computeType(library.mirrors.compiler))]; |
| 1653 } else { | 1690 } else { |
| 1654 return _convertElementMemberToMemberMirrors(library, e); | 1691 return _convertElementMemberToMemberMirrors(library, e); |
| 1655 } | 1692 } |
| 1656 } | 1693 } |
| 1657 | 1694 |
| 1695 DeclarationMirror _convertElementToDeclarationMirror(Dart2JsMirrorSystem system, | |
|
Andrei Mouravski
2013/07/01 17:46:26
A doc comment would be nice for readers of the cod
Andrei Mouravski
2013/07/01 17:46:26
While you're here anyway, is it possible to get th
Johnni Winther
2013/07/03 05:47:57
Done.
Johnni Winther
2013/07/03 05:47:57
No.
Andrei Mouravski
2013/07/03 07:18:57
Aww. :[
Johnni Winther
2013/07/03 07:21:44
What use-case do you have for such feature?
| |
| 1696 Element element) { | |
| 1697 if (element.isTypeVariable()) { | |
| 1698 return new Dart2JsTypeVariableMirror( | |
| 1699 system, element.computeType(system.compiler)); | |
| 1700 } | |
| 1701 | |
| 1702 Dart2JsLibraryMirror library = system._libraryMap[element.getLibrary()]; | |
| 1703 if (element.isLibrary()) { | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: One line this.
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 1704 return library; | |
| 1705 } | |
| 1706 if (element.isTypedef()) { | |
| 1707 return new Dart2JsTypedefMirror.fromLibrary( | |
| 1708 library, element.computeType(system.compiler)); | |
| 1709 } | |
| 1710 | |
| 1711 Dart2JsContainerMirror container = library; | |
| 1712 if (element.getEnclosingClass() != null) { | |
| 1713 container = new Dart2JsClassMirror.fromLibrary( | |
| 1714 library, element.getEnclosingClass()); | |
| 1715 } | |
| 1716 if (element.isClass()) { | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: One line this.
Johnni Winther
2013/07/03 05:47:57
Done.
| |
| 1717 return container; | |
| 1718 } | |
| 1719 if (element.isParameter()) { | |
| 1720 MethodMirror method = _convertElementMethodToMethodMirror( | |
| 1721 container, element.getOutermostEnclosingMemberOrTopLevel()); | |
| 1722 // TODO(johnniwinther): Find the right info for [isOptional] and [isNamed]. | |
| 1723 return new Dart2JsParameterMirror( | |
| 1724 system, method, element, isOptional: false, isNamed: false); | |
| 1725 } | |
| 1726 Iterable<MemberMirror> members = | |
|
Andrei Mouravski
2013/07/01 17:46:26
Nit: Also, why are these local variables not vars?
Johnni Winther
2013/07/03 05:47:57
In dart2js we deviate from the style guide at this
| |
| 1727 _convertElementMemberToMemberMirrors(container, element); | |
| 1728 if (members.isEmpty) return null; | |
| 1729 return members.first; | |
| 1730 } | |
| 1731 | |
| 1658 /** | 1732 /** |
| 1659 * Experimental API for accessing compilation units defined in a | 1733 * Experimental API for accessing compilation units defined in a |
| 1660 * library. | 1734 * library. |
| 1661 */ | 1735 */ |
| 1662 // TODO(ahe): Superclasses? Is this really a mirror? | 1736 // TODO(ahe): Superclasses? Is this really a mirror? |
| 1663 class Dart2JsCompilationUnitMirror extends Dart2JsMirror { | 1737 class Dart2JsCompilationUnitMirror extends Dart2JsMirror { |
| 1664 final Dart2JsLibraryMirror _library; | 1738 final Dart2JsLibraryMirror _library; |
| 1665 final CompilationUnitElement _element; | 1739 final CompilationUnitElement _element; |
| 1666 | 1740 |
| 1667 Dart2JsCompilationUnitMirror(this._element, this._library); | 1741 Dart2JsCompilationUnitMirror(this._element, this._library); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 1688 * | 1762 * |
| 1689 * All API in this class is experimental. | 1763 * All API in this class is experimental. |
| 1690 */ | 1764 */ |
| 1691 class BackDoor { | 1765 class BackDoor { |
| 1692 /// Return the compilation units comprising [library]. | 1766 /// Return the compilation units comprising [library]. |
| 1693 static List<Mirror> compilationUnitsOf(Dart2JsLibraryMirror library) { | 1767 static List<Mirror> compilationUnitsOf(Dart2JsLibraryMirror library) { |
| 1694 return library._library.compilationUnits.toList().map( | 1768 return library._library.compilationUnits.toList().map( |
| 1695 (cu) => new Dart2JsCompilationUnitMirror(cu, library)).toList(); | 1769 (cu) => new Dart2JsCompilationUnitMirror(cu, library)).toList(); |
| 1696 } | 1770 } |
| 1697 } | 1771 } |
| OLD | NEW |