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

Unified 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: Created 7 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart
diff --git a/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart b/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart
index 14934380c2eefe897c3adf54387cb698f95c116d..cd251fdd91ad8bd822eba9c50419c94bdb7f38bd 100644
--- a/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart
+++ b/sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirror.dart
@@ -11,6 +11,7 @@ import '../../compiler.dart' as api;
import '../elements/elements.dart';
import '../apiimpl.dart' as apiimpl;
import '../scanner/scannerlib.dart' hide SourceString;
+import '../resolution/resolution.dart' show Scope;
import '../dart2jslib.dart';
import '../dart_types.dart';
import '../source_file.dart';
@@ -38,14 +39,14 @@ List<ParameterMirror> _parametersFromFunctionSignature(
Link<Element> link = signature.requiredParameters;
while (!link.isEmpty) {
parameters.add(new Dart2JsParameterMirror(
- system, method, link.head, false, false));
+ system, method, link.head, isOptional: false, isNamed: false));
link = link.tail;
}
link = signature.optionalParameters;
bool isNamed = signature.optionalParametersAreNamed;
while (!link.isEmpty) {
parameters.add(new Dart2JsParameterMirror(
- system, method, link.head, true, isNamed));
+ system, method, link.head, isOptional: true, isNamed: isNamed));
link = link.tail;
}
return parameters;
@@ -348,8 +349,6 @@ abstract class Dart2JsElementMirror extends Dart2JsDeclarationMirror {
String toString() => _element.toString();
- int get hashCode => qualifiedName.hashCode;
-
void _appendCommentTokens(Token commentToken) {
while (commentToken != null && commentToken.kind == COMMENT_TOKEN) {
_metadata.add(new Dart2JsCommentInstanceMirror(
@@ -372,6 +371,35 @@ abstract class Dart2JsElementMirror extends Dart2JsDeclarationMirror {
// TODO(johnniwinther): Return an unmodifiable list instead.
return new List<InstanceMirror>.from(_metadata);
}
+
+ /**
+ * 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.
+ * For methods and constructors, the scope includes the parameters.
+ */
+ DeclarationMirror lookupInScope(String name) {
+ // 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.
+ SourceString sourceName = new SourceString(name);
+ Scope scope = _element.buildScope();
+ Element result = scope.lookup(sourceName);
+ 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.
+ if (result.isPrefix()) {
+ PrefixElement prefix = result;
+ result = prefix.lookupLocalMember(sourceName);
+ }
+ if (result == null) return null;
+ return _convertElementToDeclarationMirror(mirrors, result);
+ }
+
+ bool operator ==(var other) {
+ if (identical(this, other)) return true;
+ if (other == null) return false;
+ if (other is! Dart2JsElementMirror) return false;
+ 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.
+ }
+
+ int get hashCode {
+ return 13 * _element.hashCode + 17 * owner.hashCode;
+ }
}
abstract class Dart2JsMemberMirror extends Dart2JsElementMirror
@@ -665,8 +693,8 @@ class Dart2JsParameterMirror extends Dart2JsMemberMirror
factory Dart2JsParameterMirror(Dart2JsMirrorSystem system,
MethodMirror method,
VariableElement element,
- bool isOptional,
- bool isNamed) {
+ {bool isOptional: false,
+ bool isNamed: false}) {
if (element is FieldParameterElement) {
return new Dart2JsFieldParameterMirror(system,
method, element, isOptional, isNamed);
@@ -1385,6 +1413,15 @@ class Dart2JsMethodMirror extends Dart2JsMemberMirror
bool get isSetter => _kind == Dart2JsMethodKind.SETTER;
bool get isOperator => _kind == Dart2JsMethodKind.OPERATOR;
+
+ 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
+ for (ParameterMirror parameter in parameters) {
+ if (parameter.simpleName == name) {
+ return parameter;
+ }
+ }
+ return super.lookupInScope(name);
+ }
}
class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror {
@@ -1655,6 +1692,43 @@ _convertElementToMembers(Dart2JsLibraryMirror library, Element e) {
}
}
+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?
+ Element element) {
+ if (element.isTypeVariable()) {
+ return new Dart2JsTypeVariableMirror(
+ system, element.computeType(system.compiler));
+ }
+
+ Dart2JsLibraryMirror library = system._libraryMap[element.getLibrary()];
+ if (element.isLibrary()) {
Andrei Mouravski 2013/07/01 17:46:26 Nit: One line this.
Johnni Winther 2013/07/03 05:47:57 Done.
+ return library;
+ }
+ if (element.isTypedef()) {
+ return new Dart2JsTypedefMirror.fromLibrary(
+ library, element.computeType(system.compiler));
+ }
+
+ Dart2JsContainerMirror container = library;
+ if (element.getEnclosingClass() != null) {
+ container = new Dart2JsClassMirror.fromLibrary(
+ library, element.getEnclosingClass());
+ }
+ if (element.isClass()) {
Andrei Mouravski 2013/07/01 17:46:26 Nit: One line this.
Johnni Winther 2013/07/03 05:47:57 Done.
+ return container;
+ }
+ if (element.isParameter()) {
+ MethodMirror method = _convertElementMethodToMethodMirror(
+ container, element.getOutermostEnclosingMemberOrTopLevel());
+ // TODO(johnniwinther): Find the right info for [isOptional] and [isNamed].
+ return new Dart2JsParameterMirror(
+ system, method, element, isOptional: false, isNamed: false);
+ }
+ 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
+ _convertElementMemberToMemberMirrors(container, element);
+ if (members.isEmpty) return null;
+ return members.first;
+}
+
/**
* Experimental API for accessing compilation units defined in a
* library.

Powered by Google App Engine
This is Rietveld 408576698