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

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

Issue 18323004: Add lookupInScope to DeclarationMirror. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased Created 7 years, 5 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 | sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart » ('j') | 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: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
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 DeclarationMirror lookupInScope(String name) {
376 // TODO(11653): Support lookup of constructors.
377 Scope scope = _element.buildScope();
378 Element result;
379 int index = name.indexOf('.');
380 if (index != -1) {
381 // Lookup [: prefix.id :].
382 String prefix = name.substring(0, index);
383 String id = name.substring(index+1);
384 result = scope.lookup(new SourceString(prefix));
385 if (result != null && result.isPrefix()) {
386 PrefixElement prefix = result;
387 result = prefix.lookupLocalMember(new SourceString(id));
388 } else {
389 result = null;
390 }
391 } else {
392 // Lookup [: id :].
393 result = scope.lookup(new SourceString(name));
394 }
395 if (result == null || result.isPrefix()) return null;
396 return _convertElementToDeclarationMirror(mirrors, result);
397 }
398
399 bool operator ==(var other) {
400 if (identical(this, other)) return true;
401 if (other == null) return false;
402 if (other is! Dart2JsElementMirror) return false;
403 return _element == other._element &&
404 owner == other.owner;
405 }
406
407 int get hashCode {
408 return 13 * _element.hashCode + 17 * owner.hashCode;
409 }
375 } 410 }
376 411
377 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror 412 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror
378 implements MemberMirror { 413 implements MemberMirror {
379 414
380 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element) 415 Dart2JsMemberMirror(Dart2JsMirrorSystem system, Element element)
381 : super(system, element); 416 : super(system, element);
382 417
383 bool get isConstructor => false; 418 bool get isConstructor => false;
384 419
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 693
659 class Dart2JsParameterMirror extends Dart2JsMemberMirror 694 class Dart2JsParameterMirror extends Dart2JsMemberMirror
660 implements ParameterMirror { 695 implements ParameterMirror {
661 final MethodMirror _method; 696 final MethodMirror _method;
662 final bool isOptional; 697 final bool isOptional;
663 final bool isNamed; 698 final bool isNamed;
664 699
665 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system, 700 factory Dart2JsParameterMirror(Dart2JsMirrorSystem system,
666 MethodMirror method, 701 MethodMirror method,
667 VariableElement element, 702 VariableElement element,
668 bool isOptional, 703 {bool isOptional: false,
669 bool isNamed) { 704 bool isNamed: false}) {
670 if (element is FieldParameterElement) { 705 if (element is FieldParameterElement) {
671 return new Dart2JsFieldParameterMirror(system, 706 return new Dart2JsFieldParameterMirror(system,
672 method, element, isOptional, isNamed); 707 method, element, isOptional, isNamed);
673 } 708 }
674 return new Dart2JsParameterMirror._normal(system, 709 return new Dart2JsParameterMirror._normal(system,
675 method, element, isOptional, isNamed); 710 method, element, isOptional, isNamed);
676 } 711 }
677 712
678 Dart2JsParameterMirror._normal(Dart2JsMirrorSystem system, 713 Dart2JsParameterMirror._normal(Dart2JsMirrorSystem system,
679 this._method, 714 this._method,
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
1378 1413
1379 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; 1414 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING;
1380 1415
1381 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY; 1416 bool get isFactoryConstructor => _kind == Dart2JsMethodKind.FACTORY;
1382 1417
1383 bool get isGetter => _kind == Dart2JsMethodKind.GETTER; 1418 bool get isGetter => _kind == Dart2JsMethodKind.GETTER;
1384 1419
1385 bool get isSetter => _kind == Dart2JsMethodKind.SETTER; 1420 bool get isSetter => _kind == Dart2JsMethodKind.SETTER;
1386 1421
1387 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR; 1422 bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR;
1423
1424 DeclarationMirror lookupInScope(String name) {
1425 for (ParameterMirror parameter in parameters) {
1426 if (parameter.simpleName == name) {
1427 return parameter;
1428 }
1429 }
1430 return super.lookupInScope(name);
1431 }
1388 } 1432 }
1389 1433
1390 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { 1434 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror {
1391 Dart2JsContainerMirror _objectMirror; 1435 Dart2JsContainerMirror _objectMirror;
1392 VariableElement _variable; 1436 VariableElement _variable;
1393 1437
1394 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror, 1438 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror,
1395 VariableElement variable) 1439 VariableElement variable)
1396 : this._objectMirror = objectMirror, 1440 : this._objectMirror = objectMirror,
1397 this._variable = variable, 1441 this._variable = variable,
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
1649 return [new Dart2JsClassMirror.fromLibrary(library, classElement)]; 1693 return [new Dart2JsClassMirror.fromLibrary(library, classElement)];
1650 } else if (e.isTypedef()) { 1694 } else if (e.isTypedef()) {
1651 return [new Dart2JsTypedefMirror.fromLibrary( 1695 return [new Dart2JsTypedefMirror.fromLibrary(
1652 library, e.computeType(library.mirrors.compiler))]; 1696 library, e.computeType(library.mirrors.compiler))];
1653 } else { 1697 } else {
1654 return _convertElementMemberToMemberMirrors(library, e); 1698 return _convertElementMemberToMemberMirrors(library, e);
1655 } 1699 }
1656 } 1700 }
1657 1701
1658 /** 1702 /**
1703 * Converts [element] into its corresponding [DeclarationMirror], if any.
1704 *
1705 * If [element] is an [AbstractFieldElement] the mirror for its getter is
1706 * returned or, if not present, the mirror for its setter.
1707 */
1708 DeclarationMirror _convertElementToDeclarationMirror(Dart2JsMirrorSystem system,
1709 Element element) {
1710 if (element.isTypeVariable()) {
1711 return new Dart2JsTypeVariableMirror(
1712 system, element.computeType(system.compiler));
1713 }
1714
1715 Dart2JsLibraryMirror library = system._libraryMap[element.getLibrary()];
1716 if (element.isLibrary()) return library;
1717 if (element.isTypedef()) {
1718 return new Dart2JsTypedefMirror.fromLibrary(
1719 library, element.computeType(system.compiler));
1720 }
1721
1722 Dart2JsContainerMirror container = library;
1723 if (element.getEnclosingClass() != null) {
1724 container = new Dart2JsClassMirror.fromLibrary(
1725 library, element.getEnclosingClass());
1726 }
1727 if (element.isClass()) return container;
1728 if (element.isParameter()) {
1729 MethodMirror method = _convertElementMethodToMethodMirror(
1730 container, element.getOutermostEnclosingMemberOrTopLevel());
1731 // TODO(johnniwinther): Find the right info for [isOptional] and [isNamed].
1732 return new Dart2JsParameterMirror(
1733 system, method, element, isOptional: false, isNamed: false);
1734 }
1735 Iterable<MemberMirror> members =
1736 _convertElementMemberToMemberMirrors(container, element);
1737 if (members.isEmpty) return null;
1738 return members.first;
1739 }
1740
1741 /**
1659 * Experimental API for accessing compilation units defined in a 1742 * Experimental API for accessing compilation units defined in a
1660 * library. 1743 * library.
1661 */ 1744 */
1662 // TODO(ahe): Superclasses? Is this really a mirror? 1745 // TODO(ahe): Superclasses? Is this really a mirror?
1663 class Dart2JsCompilationUnitMirror extends Dart2JsMirror { 1746 class Dart2JsCompilationUnitMirror extends Dart2JsMirror {
1664 final Dart2JsLibraryMirror _library; 1747 final Dart2JsLibraryMirror _library;
1665 final CompilationUnitElement _element; 1748 final CompilationUnitElement _element;
1666 1749
1667 Dart2JsCompilationUnitMirror(this._element, this._library); 1750 Dart2JsCompilationUnitMirror(this._element, this._library);
1668 1751
(...skipping 19 matching lines...) Expand all
1688 * 1771 *
1689 * All API in this class is experimental. 1772 * All API in this class is experimental.
1690 */ 1773 */
1691 class BackDoor { 1774 class BackDoor {
1692 /// Return the compilation units comprising [library]. 1775 /// Return the compilation units comprising [library].
1693 static List<Mirror> compilationUnitsOf(Dart2JsLibraryMirror library) { 1776 static List<Mirror> compilationUnitsOf(Dart2JsLibraryMirror library) {
1694 return library._library.compilationUnits.toList().map( 1777 return library._library.compilationUnits.toList().map(
1695 (cu) => new Dart2JsCompilationUnitMirror(cu, library)).toList(); 1778 (cu) => new Dart2JsCompilationUnitMirror(cu, library)).toList();
1696 } 1779 }
1697 } 1780 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698