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

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

Issue 18323004: Add lookupInScope to DeclarationMirror. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test + fix implementation. 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
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_util; 5 library mirrors_util;
6 6
7 import 'dart:collection' show Queue, IterableBase; 7 import 'dart:collection' show Queue, IterableBase;
8 8
9 // TODO(rnystrom): Use "package:" URL (#4968). 9 // TODO(rnystrom): Use "package:" URL (#4968).
10 import 'mirrors.dart'; 10 import 'mirrors.dart';
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 // * Foo 210 // * Foo
211 // */ 211 // */
212 // as "\nFoo\n" and not as "\nFoo\n ". 212 // as "\nFoo\n" and not as "\nFoo\n ".
213 sb.write(line); 213 sb.write(line);
214 } 214 }
215 } 215 }
216 return sb.toString(); 216 return sb.toString();
217 } 217 }
218 throw new ArgumentError('Invalid comment $comment'); 218 throw new ArgumentError('Invalid comment $comment');
219 } 219 }
220
221 /**
222 * Looks up [name] in the scope [declaration].
223 *
224 * If [name] is of the form 'a.b.c', 'a' is looked up in the scope of
225 * [declaration] and if unresolved 'a.b' is looked in the scope of
226 * [declaration]. Each identifier of the remaining suffix, 'c' or 'b.c', is
227 * then looked up in the local scope of the previous result.
228 *
229 * For instance, assumming that [:Iterable:] is imported into the scope of
230 * [declaration] via the prefix 'col', 'col.Iterable.E' finds the type
231 * variable of [:Iterable:] and 'col.Iterable.contains.element' finds the
232 * [:element:] parameter of the [:contains:] method on [:Iterable:].
233 */
234 DeclarationMirror lookupQualifiedInScope(DeclarationMirror declaration,
235 String name) {
236 // TODO(11653): Support lookup of constructors using the [:new Foo:]
237 // syntax.
238 int offset = 1;
239 List<String> parts = name.split('.');
240 DeclarationMirror result = declaration.lookupInScope(parts[0]);
241 if (result == null && parts.length > 1) {
242 // Try lookup of `prefix.id`.
243 result = declaration.lookupInScope('${parts[0]}.${parts[1]}');
244 offset = 2;
245 }
246 if (result == null) return null;
247 while (result != null && offset < parts.length) {
248 result = _lookupLocal(result, parts[offset++]);
249 }
250 return result;
251 }
252
253 DeclarationMirror _lookupLocal(Mirror mirror, String id) {
254 DeclarationMirror result;
255 if (mirror is ContainerMirror) {
256 // Try member lookup.
257 result = mirror.members[id];
258 }
259 if (result != null) return result;
260 if (mirror is ClassMirror) {
261 // Try type variables.
262 result = mirror.typeVariables.firstWhere(
263 (TypeVariableMirror v) => v.simpleName == id, orElse: () => null);
264 } else if (mirror is MethodMirror) {
265 result = mirror.parameters.firstWhere(
266 (ParameterMirror p) => p.simpleName == id, orElse: () => null);
267 }
268 return result;
269
270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698