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

Side by Side Diff: lib/dartdoc/mirrors/dart2js_mirror.dart

Issue 10701091: Dartdoc and Apidoc updated to use dart2js through the mirror system. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed cf. rnystrom's comments. Created 8 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.dart2js'); 5 #library('mirrors.dart2js');
6 6
7 #import('../../compiler/compiler.dart', prefix: 'diagnostics'); 7 #import('../../compiler/compiler.dart', prefix: 'diagnostics');
8 #import('../../compiler/implementation/elements/elements.dart'); 8 #import('../../compiler/implementation/elements/elements.dart');
9 #import('../../compiler/implementation/apiimpl.dart', prefix: 'api'); 9 #import('../../compiler/implementation/apiimpl.dart', prefix: 'api');
10 #import('../../compiler/implementation/scanner/scannerlib.dart'); 10 #import('../../compiler/implementation/scanner/scannerlib.dart');
(...skipping 12 matching lines...) Expand all
23 23
24 //------------------------------------------------------------------------------ 24 //------------------------------------------------------------------------------
25 // Utility types and functions for the dart2js mirror system 25 // Utility types and functions for the dart2js mirror system
26 //------------------------------------------------------------------------------ 26 //------------------------------------------------------------------------------
27 27
28 bool _isPrivate(String name) { 28 bool _isPrivate(String name) {
29 return name.startsWith('_'); 29 return name.startsWith('_');
30 } 30 }
31 31
32 List<ParameterMirror> _parametersFromFunctionSignature( 32 List<ParameterMirror> _parametersFromFunctionSignature(
33 Dart2jsMirrorSystem system, 33 Dart2JsMirrorSystem system,
34 Dart2jsMethodMirror method, 34 Dart2JsMethodMirror method,
35 FunctionSignature signature) { 35 FunctionSignature signature) {
36 var parameters = <ParameterMirror>[]; 36 var parameters = <ParameterMirror>[];
37 Link<Element> link = signature.requiredParameters; 37 Link<Element> link = signature.requiredParameters;
38 while (!link.isEmpty()) { 38 while (!link.isEmpty()) {
39 parameters.add(new Dart2jsParameterMirror(system, method, 39 parameters.add(new Dart2JsParameterMirror(system, method,
40 link.head, false)); 40 link.head, false));
41 link = link.tail; 41 link = link.tail;
42 } 42 }
43 link = signature.optionalParameters; 43 link = signature.optionalParameters;
44 while (!link.isEmpty()) { 44 while (!link.isEmpty()) {
45 parameters.add(new Dart2jsParameterMirror(system, method, 45 parameters.add(new Dart2JsParameterMirror(system, method,
46 link.head, true)); 46 link.head, true));
47 link = link.tail; 47 link = link.tail;
48 } 48 }
49 return parameters; 49 return parameters;
50 } 50 }
51 51
52 Dart2jsTypeMirror _convertTypeToTypeMirror( 52 Dart2JsTypeMirror _convertTypeToTypeMirror(
53 Dart2jsMirrorSystem system, 53 Dart2JsMirrorSystem system,
54 Type type, 54 Type type,
55 InterfaceType defaultType, 55 InterfaceType defaultType,
56 [FunctionSignature functionSignature]) { 56 [FunctionSignature functionSignature]) {
57 if (type === null) { 57 if (type === null) {
58 return new Dart2jsInterfaceTypeMirror(system, defaultType); 58 return new Dart2JsInterfaceTypeMirror(system, defaultType);
59 } else if (type is InterfaceType) { 59 } else if (type is InterfaceType) {
60 return new Dart2jsInterfaceTypeMirror(system, type); 60 return new Dart2JsInterfaceTypeMirror(system, type);
61 } else if (type is TypeVariableType) { 61 } else if (type is TypeVariableType) {
62 return new Dart2jsTypeVariableMirror(system, type); 62 return new Dart2JsTypeVariableMirror(system, type);
63 } else if (type is FunctionType) { 63 } else if (type is FunctionType) {
64 if (type.element is TypedefElement) { 64 if (type.element is TypedefElement) {
65 return new Dart2jsTypedefMirror(system, type.element); 65 return new Dart2JsTypedefMirror(system, type.element);
66 } else { 66 } else {
67 return new Dart2jsFunctionTypeMirror(system, type, functionSignature); 67 return new Dart2JsFunctionTypeMirror(system, type, functionSignature);
68 } 68 }
69 } else if (type is VoidType) { 69 } else if (type is VoidType) {
70 return new Dart2jsVoidMirror(system, type); 70 return new Dart2JsVoidMirror(system, type);
71 } 71 }
72 throw new IllegalArgumentException("Unexpected interface type $type"); 72 throw new IllegalArgumentException("Unexpected interface type $type");
73 } 73 }
74 74
75 Collection<Dart2jsMemberMirror> _convertElementMemberToMemberMirrors( 75 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors(
76 Dart2jsObjectMirror library, Element element) { 76 Dart2JsObjectMirror library, Element element) {
77 if (element is SynthesizedConstructorElement) { 77 if (element is SynthesizedConstructorElement) {
78 return const <Dart2jsMemberMirror>[]; 78 return const <Dart2JsMemberMirror>[];
79 } else if (element is VariableElement) { 79 } else if (element is VariableElement) {
80 return <Dart2jsMemberMirror>[new Dart2jsFieldMirror(library, element)]; 80 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)];
81 } else if (element is FunctionElement) { 81 } else if (element is FunctionElement) {
82 return <Dart2jsMemberMirror>[new Dart2jsMethodMirror(library, element)]; 82 return <Dart2JsMemberMirror>[new Dart2JsMethodMirror(library, element)];
83 } else if (element is AbstractFieldElement) { 83 } else if (element is AbstractFieldElement) {
84 var members = <Dart2jsMemberMirror>[]; 84 var members = <Dart2JsMemberMirror>[];
85 if (element.getter !== null) { 85 if (element.getter !== null) {
86 members.add(new Dart2jsMethodMirror(library, element.getter, 86 members.add(new Dart2JsMethodMirror(library, element.getter,
87 Dart2jsMethodKind.GETTER)); 87 Dart2JsMethodKind.GETTER));
88 } 88 }
89 if (element.setter !== null) { 89 if (element.setter !== null) {
90 members.add(new Dart2jsMethodMirror(library, element.setter, 90 members.add(new Dart2JsMethodMirror(library, element.setter,
91 Dart2jsMethodKind.SETTER)); 91 Dart2JsMethodKind.SETTER));
92 } 92 }
93 return members; 93 return members;
94 } 94 }
95 throw new IllegalArgumentException( 95 throw new IllegalArgumentException(
96 "Unexpected member type $element ${element.kind}"); 96 "Unexpected member type $element ${element.kind}");
97 } 97 }
98 98
99 MethodMirror _convertElementMethodToMethodMirror(Dart2jsObjectMirror library, 99 MethodMirror _convertElementMethodToMethodMirror(Dart2JsObjectMirror library,
100 Element element) { 100 Element element) {
101 if (element is FunctionElement) { 101 if (element is FunctionElement) {
102 return new Dart2jsMethodMirror(library, element); 102 return new Dart2JsMethodMirror(library, element);
103 } else { 103 } else {
104 return null; 104 return null;
105 } 105 }
106 } 106 }
107 107
108 class Dart2jsMethodKind { 108 class Dart2JsMethodKind {
109 static final Dart2jsMethodKind NORMAL = const Dart2jsMethodKind("normal"); 109 static final Dart2JsMethodKind NORMAL = const Dart2JsMethodKind("normal");
110 static final Dart2jsMethodKind CONSTRUCTOR 110 static final Dart2JsMethodKind CONSTRUCTOR
111 = const Dart2jsMethodKind("constructor"); 111 = const Dart2JsMethodKind("constructor");
112 static final Dart2jsMethodKind CONST = const Dart2jsMethodKind("const"); 112 static final Dart2JsMethodKind CONST = const Dart2JsMethodKind("const");
113 static final Dart2jsMethodKind FACTORY = const Dart2jsMethodKind("factory"); 113 static final Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory");
114 static final Dart2jsMethodKind GETTER = const Dart2jsMethodKind("getter"); 114 static final Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter");
115 static final Dart2jsMethodKind SETTER = const Dart2jsMethodKind("setter"); 115 static final Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter");
116 static final Dart2jsMethodKind OPERATOR = const Dart2jsMethodKind("operator"); 116 static final Dart2JsMethodKind OPERATOR = const Dart2JsMethodKind("operator");
117 117
118 final String text; 118 final String text;
119 119
120 const Dart2jsMethodKind(this.text); 120 const Dart2JsMethodKind(this.text);
121 121
122 String toString() => text; 122 String toString() => text;
123 } 123 }
124 124
125 125
126 String _getOperatorFromOperatorName(String name) { 126 String _getOperatorFromOperatorName(String name) {
127 Map<String, String> mapping = const { 127 Map<String, String> mapping = const {
128 'eq': '==', 128 'eq': '==',
129 'not': '~', 129 'not': '~',
130 'negate': 'negate', // Will change. 130 'negate': 'negate', // Will change.
(...skipping 16 matching lines...) Expand all
147 'or': '|', 147 'or': '|',
148 }; 148 };
149 String newName = mapping[name]; 149 String newName = mapping[name];
150 if (newName === null) { 150 if (newName === null) {
151 throw new Exception('Unhandled operator name: $name'); 151 throw new Exception('Unhandled operator name: $name');
152 } 152 }
153 return newName; 153 return newName;
154 } 154 }
155 155
156 DiagnosticListener get _diagnosticListener() { 156 DiagnosticListener get _diagnosticListener() {
157 return const Dart2jsDiagnosticListener(); 157 return const Dart2JsDiagnosticListener();
158 } 158 }
159 159
160 class Dart2jsDiagnosticListener implements DiagnosticListener { 160 class Dart2JsDiagnosticListener implements DiagnosticListener {
161 const Dart2jsDiagnosticListener(); 161 const Dart2JsDiagnosticListener();
162 162
163 void cancel([String reason, node, token, instruction, element]) { 163 void cancel([String reason, node, token, instruction, element]) {
164 print(reason); 164 print(reason);
165 } 165 }
166 166
167 void log(message) { 167 void log(message) {
168 print(message); 168 print(message);
169 } 169 }
170 } 170 }
171 171
172 //------------------------------------------------------------------------------ 172 //------------------------------------------------------------------------------
173 // Compiler extension for apidoc.
174 //------------------------------------------------------------------------------
175
176 /**
177 * Extension of the compiler that enables the analysis of several libraries with
178 * no particular entry point.
179 */
180 class LibraryCompiler extends api.Compiler {
181 LibraryCompiler(diagnostics.ReadStringFromUri provider,
182 diagnostics.DiagnosticHandler handler,
183 Uri libraryRoot, Uri packageRoot,
184 List<String> options)
185 : super(provider, handler, libraryRoot, packageRoot, options) {
186 checker = new LibraryTypeCheckerTask(this);
187 resolver = new LibraryResolverTask(this);
188 }
189
190 // TODO(johnniwinther): The following methods are added to enable the analysis
191 // of a collection of libraries to be used for apidoc. Most of the methods
192 // are based on copies of existing methods and could probably be implemented
193 // such that the duplicate code is avoided. Not to affect the correctness and
194 // speed of dart2js as is, the redundancy is accepted temporarily.
195
196 /**
197 * Run the compiler on a list of libraries. No entry point is used.
198 */
199 bool runList(List<Uri> uriList) {
200 bool success = _runList(uriList);
201 for (final task in tasks) {
202 log('${task.name} took ${task.timing}msec');
203 }
204 return success;
205 }
206
207 bool _runList(List<Uri> uriList) {
208 try {
209 runCompilerList(uriList);
210 } catch (CompilerCancelledException exception) {
211 log(exception.toString());
212 log('compilation failed');
213 return false;
214 }
215 tracer.close();
216 log('compilation succeeded');
217 return true;
218 }
219
220 void runCompilerList(List<Uri> uriList) {
221 scanBuiltinLibraries();
222 var elementList = <LibraryElement>[];
223 for (var uri in uriList) {
224 elementList.add(scanner.loadLibrary(uri, null));
225 }
226
227 world.populate(this, libraries.getValues());
228
229 log('Resolving...');
230 phase = Compiler.PHASE_RESOLVING;
231 backend.enqueueHelpers(enqueuer.resolution);
232 processQueueList(enqueuer.resolution, elementList);
233 log('Resolved ${enqueuer.resolution.resolvedElements.length} elements.');
234 }
235
236 void processQueueList(Enqueuer world, List<LibraryElement> elements) {
237 backend.processNativeClasses(world, libraries.getValues());
238 for (var library in elements) {
239 library.elements.forEach((_, element) {
240 world.addToWorkList(element);
241 });
242 }
243 progress.reset();
244 world.forEach((WorkItem work) {
245 withCurrentElement(work.element, () => work.run(this, world));
246 });
247 //world.queueIsClosed = true;
248 assert(world.checkNoEnqueuedInvokedInstanceMethods());
249 world.registerFieldClosureInvocations();
250 }
251
252 String codegen(WorkItem work, Enqueuer world) {
253 return null;
254 }
255 }
256
257 // TODO(johnniwinther): The source for the apidoc includes calls to methods on
258 // for instance [MathPrimitives] which are not resolved by dart2js. Since we
259 // do not need to analyse the body of functions to produce the documenation
260 // we use a specialized resolver which bypasses method bodies.
261 class LibraryResolverTask extends ResolverTask {
262 LibraryResolverTask(api.Compiler compiler) : super(compiler);
263
264 void visitBody(ResolverVisitor visitor, Statement body) {}
265 }
266
267 // TODO(johnniwinther): As a side-effect of bypassing method bodies in
268 // [LibraryResolveTask] we can not perform the typecheck.
269 class LibraryTypeCheckerTask extends TypeCheckerTask {
270 LibraryTypeCheckerTask(api.Compiler compiler) : super(compiler);
271
272 void check(Node tree, TreeElements elements) {}
273 }
274
275 //------------------------------------------------------------------------------
173 // Compilation implementation 276 // Compilation implementation
174 //------------------------------------------------------------------------------ 277 //------------------------------------------------------------------------------
175 278
176 class Dart2jsCompilation implements Compilation { 279 class Dart2JsCompilation implements Compilation {
177 api.Compiler _compiler; 280 api.Compiler _compiler;
178 Uri cwd; 281 Uri cwd;
179 bool isAborting = false; 282 bool isAborting = false;
180 Map<String, SourceFile> sourceFiles; 283 Map<String, SourceFile> sourceFiles;
181 284
182 Future<String> provider(Uri uri) { 285 Future<String> provider(Uri uri) {
183 if (uri.scheme != 'file') { 286 if (uri.scheme != 'file') {
184 throw new IllegalArgumentException(uri); 287 throw new IllegalArgumentException(uri);
185 } 288 }
186 String source; 289 String source;
(...skipping 19 matching lines...) Expand all
206 } 309 }
207 print(message); 310 print(message);
208 throw message; 311 throw message;
209 } else if (fatal) { 312 } else if (fatal) {
210 SourceFile file = sourceFiles[uri.toString()]; 313 SourceFile file = sourceFiles[uri.toString()];
211 print(file.getLocationMessage(message, begin, end, true, (s) => s)); 314 print(file.getLocationMessage(message, begin, end, true, (s) => s));
212 throw message; 315 throw message;
213 } 316 }
214 } 317 }
215 318
216 Dart2jsCompilation(String script, String libraryRoot, 319 Dart2JsCompilation(String script, String libraryRoot,
217 [String packageRoot, List<String> opts = const <String>[]]) 320 [String packageRoot, List<String> opts = const <String>[]])
218 : cwd = getCurrentDirectory(), sourceFiles = <SourceFile>{} { 321 : cwd = getCurrentDirectory(), sourceFiles = <SourceFile>{} {
219 var libraryUri = cwd.resolve(nativeToUriPath(libraryRoot)); 322 var libraryUri = cwd.resolve(nativeToUriPath(libraryRoot));
220 var packageUri; 323 var packageUri;
221 if (packageRoot !== null) { 324 if (packageRoot !== null) {
222 packageUri = cwd.resolve(nativeToUriPath(packageRoot)); 325 packageUri = cwd.resolve(nativeToUriPath(packageRoot));
223 } else { 326 } else {
224 packageUri = libraryUri; 327 packageUri = libraryUri;
225 } 328 }
226 _compiler = new api.Compiler(provider, handler, 329 _compiler = new api.Compiler(provider, handler,
227 libraryUri, packageUri, <String>[]); 330 libraryUri, packageUri, <String>[]);
228 var scriptUri = cwd.resolve(nativeToUriPath(script)); 331 var scriptUri = cwd.resolve(nativeToUriPath(script));
229 // TODO(johnniwinther): Detect file not found 332 // TODO(johnniwinther): Detect file not found
230 _compiler.run(scriptUri); 333 _compiler.run(scriptUri);
231 } 334 }
232 335
336 Dart2JsCompilation.library(List<String> libraries, String libraryRoot,
337 [String packageRoot, List<String> opts = const []])
338 : cwd = getCurrentDirectory(), sourceFiles = <SourceFile>{} {
339 var libraryUri = cwd.resolve(nativeToUriPath(libraryRoot));
340 var packageUri;
341 if (packageRoot !== null) {
342 packageUri = cwd.resolve(nativeToUriPath(packageRoot));
343 } else {
344 packageUri = libraryUri;
345 }
346 _compiler = new LibraryCompiler(provider, handler,
347 libraryUri, packageUri, <String>[]);
348 var librariesUri = <Uri>[];
349 for (var library in libraries) {
350 librariesUri.add(cwd.resolve(nativeToUriPath(library)));
351 // TODO(johnniwinther): Detect file not found
352 }
353 _compiler.runList(librariesUri);
354 }
355
233 void addLibrary(String path) { 356 void addLibrary(String path) {
234 var uri = cwd.resolve(nativeToUriPath(path)); 357 var uri = cwd.resolve(nativeToUriPath(path));
235 _compiler.scanner.loadLibrary(uri, null); 358 _compiler.scanner.loadLibrary(uri, null);
236 } 359 }
237 360
238 MirrorSystem mirrors() => new Dart2jsMirrorSystem(_compiler); 361 MirrorSystem mirrors() => new Dart2JsMirrorSystem(_compiler);
239 } 362 }
240 363
241 364
242 //------------------------------------------------------------------------------ 365 //------------------------------------------------------------------------------
243 // Dart2js specific extensions of mirror interfaces 366 // Dart2Js specific extensions of mirror interfaces
244 //------------------------------------------------------------------------------ 367 //------------------------------------------------------------------------------
245 368
246 interface Dart2jsMirror extends Mirror { 369 interface Dart2JsMirror extends Mirror {
247 /** 370 /**
248 * A unique name used as the key in maps. 371 * A unique name used as the key in maps.
249 */ 372 */
250 final String canonicalName; 373 final String canonicalName;
251 final Dart2jsMirrorSystem system; 374 final Dart2JsMirrorSystem system;
252 } 375 }
253 376
254 interface Dart2jsMemberMirror extends Dart2jsMirror, MemberMirror { 377 interface Dart2JsMemberMirror extends Dart2JsMirror, MemberMirror {
255 378
256 } 379 }
257 380
258 interface Dart2jsTypeMirror extends Dart2jsMirror, TypeMirror { 381 interface Dart2JsTypeMirror extends Dart2JsMirror, TypeMirror {
259 382
260 } 383 }
261 384
262 abstract class Dart2jsElementMirror implements Dart2jsMirror { 385 abstract class Dart2JsElementMirror implements Dart2JsMirror {
263 final Dart2jsMirrorSystem system; 386 final Dart2JsMirrorSystem system;
264 final Element _element; 387 final Element _element;
265 388
266 Dart2jsElementMirror(this.system, this._element) { 389 Dart2JsElementMirror(this.system, this._element) {
267 assert (system !== null); 390 assert (system !== null);
268 assert (_element !== null); 391 assert (_element !== null);
269 } 392 }
270 393
271 String simpleName() => _element.name.slowToString(); 394 String simpleName() => _element.name.slowToString();
272 395
273 Location location() => new Dart2jsLocation( 396 Location location() => new Dart2JsLocation(
274 _element.getCompilationUnit().script, 397 _element.getCompilationUnit().script,
275 system.compiler.spanFromElement(_element)); 398 system.compiler.spanFromElement(_element));
276 399
277 String toString() => _element.toString(); 400 String toString() => _element.toString();
401
402 int hashCode() => qualifiedName().hashCode();
278 } 403 }
279 404
280 abstract class Dart2jsProxyMirror implements Dart2jsMirror { 405 abstract class Dart2JsProxyMirror implements Dart2JsMirror {
281 final Dart2jsMirrorSystem system; 406 final Dart2JsMirrorSystem system;
282 407
283 Dart2jsProxyMirror(this.system); 408 Dart2JsProxyMirror(this.system);
409
410 int hashCode() => qualifiedName().hashCode();
284 } 411 }
285 412
286 /////////////////////////////////////////////////////// 413 //------------------------------------------------------------------------------
287 // implementation 414 // Mirror system implementation.
288 /////////////////////////////////////////////////////// 415 //------------------------------------------------------------------------------
289 416
290 class Dart2jsMirrorSystem implements MirrorSystem, Dart2jsMirror { 417 class Dart2JsMirrorSystem implements MirrorSystem, Dart2JsMirror {
291 final api.Compiler compiler; 418 final api.Compiler compiler;
292 Map<String, Dart2jsLibraryMirror> _libraries; 419 Map<String, Dart2JsLibraryMirror> _libraries;
293 Map<LibraryElement, Dart2jsLibraryMirror> _libraryMap; 420 Map<LibraryElement, Dart2JsLibraryMirror> _libraryMap;
294 421
295 Dart2jsMirrorSystem(this.compiler) 422 Dart2JsMirrorSystem(this.compiler)
296 : _libraryMap = new Map<LibraryElement, Dart2jsLibraryMirror>(); 423 : _libraryMap = new Map<LibraryElement, Dart2JsLibraryMirror>();
297 424
298 void _ensureLibraries() { 425 void _ensureLibraries() {
299 if (_libraries == null) { 426 if (_libraries == null) {
300 _libraries = <Dart2jsLibraryMirror>{}; 427 _libraries = <Dart2JsLibraryMirror>{};
301 compiler.libraries.forEach((_, LibraryElement v) { 428 compiler.libraries.forEach((_, LibraryElement v) {
302 var mirror = new Dart2jsLibraryMirror(system, v); 429 var mirror = new Dart2JsLibraryMirror(system, v);
303 _libraries[mirror.canonicalName] = mirror; 430 _libraries[mirror.canonicalName] = mirror;
304 _libraryMap[v] = mirror; 431 _libraryMap[v] = mirror;
305 }); 432 });
306 } 433 }
307 } 434 }
308 435
309 Map<Object, LibraryMirror> libraries() { 436 Map<Object, LibraryMirror> libraries() {
310 _ensureLibraries(); 437 _ensureLibraries();
311 return new ImmutableMapWrapper<Object, LibraryMirror>(_libraries); 438 return new ImmutableMapWrapper<Object, LibraryMirror>(_libraries);
312 } 439 }
313 440
314 Dart2jsLibraryMirror getLibrary(LibraryElement element) { 441 Dart2JsLibraryMirror getLibrary(LibraryElement element) {
315 return _libraryMap[element]; 442 return _libraryMap[element];
316 } 443 }
317 444
318 Dart2jsMirrorSystem get system() => this; 445 Dart2JsMirrorSystem get system() => this;
319 446
320 String simpleName() => "mirror"; 447 String simpleName() => "mirror";
321 String qualifiedName() => simpleName(); 448 String qualifiedName() => simpleName();
322 449
323 String get canonicalName() => simpleName(); 450 String get canonicalName() => simpleName();
451
452 // TODO(johnniwinther): Hack! Dart2JsMirrorSystem need not be a Mirror.
453 int hashCode() => qualifiedName().hashCode();
324 } 454 }
325 455
326 abstract class Dart2jsObjectMirror extends Dart2jsElementMirror 456 abstract class Dart2JsObjectMirror extends Dart2JsElementMirror
327 implements ObjectMirror { 457 implements ObjectMirror {
328 Dart2jsObjectMirror(Dart2jsMirrorSystem system, Element element) 458 Dart2JsObjectMirror(Dart2JsMirrorSystem system, Element element)
329 : super(system, element); 459 : super(system, element);
330 } 460 }
331 461
332 class Dart2jsLibraryMirror extends Dart2jsObjectMirror 462 class Dart2JsLibraryMirror extends Dart2JsObjectMirror
333 implements LibraryMirror { 463 implements LibraryMirror {
334 Map<String, InterfaceMirror> _types; 464 Map<String, InterfaceMirror> _types;
335 Map<String, MemberMirror> _members; 465 Map<String, MemberMirror> _members;
336 466
337 Dart2jsLibraryMirror(Dart2jsMirrorSystem system, LibraryElement library) 467 Dart2JsLibraryMirror(Dart2JsMirrorSystem system, LibraryElement library)
338 : super(system, library); 468 : super(system, library);
339 469
340 LibraryElement get _library() => _element; 470 LibraryElement get _library() => _element;
341 471
472 LibraryMirror library() => this;
473
342 String get canonicalName() => simpleName(); 474 String get canonicalName() => simpleName();
343 475
344 /** 476 /**
345 * Returns the library name (for libraries with a #library tag) or the script 477 * Returns the library name (for libraries with a #library tag) or the script
346 * file name (for scripts without a #library tag). The latter case is used to 478 * file name (for scripts without a #library tag). The latter case is used to
347 * provide a 'library name' for scripts, to use for instance in dartdoc. 479 * provide a 'library name' for scripts, to use for instance in dartdoc.
348 */ 480 */
349 String simpleName() { 481 String simpleName() {
350 if (_library.libraryTag !== null) { 482 if (_library.libraryTag !== null) {
351 return _library.libraryTag.argument.dartString.slowToString(); 483 return _library.libraryTag.argument.dartString.slowToString();
352 } else { 484 } else {
353 // Use the file name as script name. 485 // Use the file name as script name.
354 String path = _library.script.uri.path; 486 String path = _library.script.uri.path;
355 return path.substring(path.lastIndexOf('/') + 1); 487 return path.substring(path.lastIndexOf('/') + 1);
356 } 488 }
357 } 489 }
358 490
359 String qualifiedName() => simpleName(); 491 String qualifiedName() => simpleName();
360 492
361 void _ensureTypes() { 493 void _ensureTypes() {
362 if (_types == null) { 494 if (_types == null) {
363 _types = <InterfaceMirror>{}; 495 _types = <InterfaceMirror>{};
364 _library.forEachExport((Element e) { 496 _library.forEachExport((Element e) {
365 if (e.getLibrary() == _library) { 497 if (e.getLibrary() == _library) {
366 if (e.isClass()) { 498 if (e.isClass()) {
367 var type = new Dart2jsInterfaceMirror.fromLibrary(this, e); 499 var type = new Dart2JsInterfaceMirror.fromLibrary(this, e);
368 _types[type.canonicalName] = type; 500 _types[type.canonicalName] = type;
369 } else if (e.isTypedef()) { 501 } else if (e.isTypedef()) {
370 var type = new Dart2jsTypedefMirror.fromLibrary(this, e); 502 var type = new Dart2JsTypedefMirror.fromLibrary(this, e);
371 _types[type.canonicalName] = type; 503 _types[type.canonicalName] = type;
372 } 504 }
373 } 505 }
374 }); 506 });
375 } 507 }
376 } 508 }
377 509
378 void _ensureMembers() { 510 void _ensureMembers() {
379 if (_members == null) { 511 if (_members == null) {
380 _members = <MemberMirror>{}; 512 _members = <MemberMirror>{};
(...skipping 12 matching lines...) Expand all
393 return new ImmutableMapWrapper<Object, MemberMirror>(_members); 525 return new ImmutableMapWrapper<Object, MemberMirror>(_members);
394 } 526 }
395 527
396 Map<Object, InterfaceMirror> types() { 528 Map<Object, InterfaceMirror> types() {
397 _ensureTypes(); 529 _ensureTypes();
398 return new ImmutableMapWrapper<Object, InterfaceMirror>(_types); 530 return new ImmutableMapWrapper<Object, InterfaceMirror>(_types);
399 } 531 }
400 532
401 Location location() { 533 Location location() {
402 var script = _library.getCompilationUnit().script; 534 var script = _library.getCompilationUnit().script;
403 return new Dart2jsLocation( 535 return new Dart2JsLocation(
404 script, 536 script,
405 new SourceSpan(script.uri, 0, script.text.length)); 537 new SourceSpan(script.uri, 0, script.text.length));
406 } 538 }
407 } 539 }
408 540
409 class Dart2jsLocation implements Location { 541 class Dart2JsLocation implements Location {
410 Script _script; 542 Script _script;
411 SourceSpan _span; 543 SourceSpan _span;
412 544
413 Dart2jsLocation(this._script, this._span); 545 Dart2JsLocation(this._script, this._span);
414 546
415 int start() => _span.begin; 547 int start() => _span.begin;
416 int end() => _span.end; 548 int end() => _span.end;
417 Source source() => new Dart2jsSource(_script); 549 Source source() => new Dart2JsSource(_script);
418 550
419 String text() => _script.text.substring(start(), end()); 551 String text() => _script.text.substring(start(), end());
420 } 552 }
421 553
422 class Dart2jsSource implements Source { 554 class Dart2JsSource implements Source {
423 Script _script; 555 Script _script;
424 556
425 Dart2jsSource(this._script); 557 Dart2JsSource(this._script);
426 558
427 Uri uri() => _script.uri; 559 Uri uri() => _script.uri;
428 String text() => _script.text; 560 String text() => _script.text;
429 } 561 }
430 562
431 class Dart2jsParameterMirror extends Dart2jsElementMirror 563 class Dart2JsParameterMirror extends Dart2JsElementMirror
432 implements ParameterMirror { 564 implements ParameterMirror {
433 final MethodMirror _method; 565 final MethodMirror _method;
434 final bool _isOptional; 566 final bool _isOptional;
435 567
436 Dart2jsParameterMirror(Dart2jsMirrorSystem system, 568 Dart2JsParameterMirror(Dart2JsMirrorSystem system,
437 this._method, 569 this._method,
438 VariableElement element, 570 VariableElement element,
439 this._isOptional) 571 this._isOptional)
440 : super(system, element); 572 : super(system, element);
441 573
442 VariableElement get _variableElement() => _element; 574 VariableElement get _variableElement() => _element;
443 575
444 String get canonicalName() => simpleName(); 576 String get canonicalName() => simpleName();
445 577
446 String qualifiedName() => '${_method.qualifiedName()}#${simpleName()}'; 578 String qualifiedName() => '${_method.qualifiedName()}#${simpleName()}';
447 579
448 // TODO(johnniwinther): Provide 580 // TODO(johnniwinther): Provide
449 // [:_variableElement.variables.functionSignature:] instead of [:null:]. 581 // [:_variableElement.variables.functionSignature:] instead of [:null:].
450 TypeMirror type() => _convertTypeToTypeMirror(system, 582 TypeMirror type() => _convertTypeToTypeMirror(system,
451 _variableElement.computeType(system.compiler), 583 _variableElement.computeType(system.compiler),
452 system.compiler.dynamicClass.computeType(system.compiler), 584 system.compiler.dynamicClass.computeType(system.compiler),
453 null); 585 null);
454 586
455 String defaultValue() => null; // TODO(johnniwinther): How to compute this? 587 String defaultValue() => null; // TODO(johnniwinther): How to compute this?
456 588
457 bool hasDefaultValue() => false; // TODO(johnniwinther): How to compute this? 589 bool hasDefaultValue() => false; // TODO(johnniwinther): How to compute this?
458 590
459 bool isOptional() => _isOptional; 591 bool isOptional() => _isOptional;
460 } 592 }
461 593
462 //------------------------------------------------------------------------------ 594 //------------------------------------------------------------------------------
463 // Declarations 595 // Declarations
464 //------------------------------------------------------------------------------ 596 //------------------------------------------------------------------------------
465 class Dart2jsInterfaceMirror extends Dart2jsObjectMirror 597 class Dart2JsInterfaceMirror extends Dart2JsObjectMirror
466 implements Dart2jsTypeMirror, InterfaceMirror { 598 implements Dart2JsTypeMirror, InterfaceMirror {
467 final Dart2jsLibraryMirror _library; 599 final Dart2JsLibraryMirror _library;
468 Map<String, Dart2jsMemberMirror> _members; 600 Map<String, Dart2JsMemberMirror> _members;
469 List<TypeVariableMirror> _typeVariables; 601 List<TypeVariableMirror> _typeVariables;
470 602
471 Dart2jsInterfaceMirror(Dart2jsMirrorSystem system, ClassElement _class) 603 Dart2JsInterfaceMirror(Dart2JsMirrorSystem system, ClassElement _class)
472 : this._library = system.getLibrary(_class.getLibrary()), 604 : this._library = system.getLibrary(_class.getLibrary()),
473 super(system, _class); 605 super(system, _class);
474 606
475 ClassElement get _class() => _element; 607 ClassElement get _class() => _element;
476 608
477 609
478 Dart2jsInterfaceMirror.fromLibrary(Dart2jsLibraryMirror library, 610 Dart2JsInterfaceMirror.fromLibrary(Dart2JsLibraryMirror library,
479 ClassElement _class) 611 ClassElement _class)
480 : this._library = library, 612 : this._library = library,
481 super(library.system, _class); 613 super(library.system, _class);
482 614
483 String get canonicalName() => simpleName(); 615 String get canonicalName() => simpleName();
484 616
485 String qualifiedName() => '${library().qualifiedName()}.${simpleName()}'; 617 String qualifiedName() => '${library().qualifiedName()}.${simpleName()}';
486 618
487 Location location() { 619 Location location() {
488 if (_class is PartialClassElement) { 620 if (_class is PartialClassElement) {
489 var node = _class.parseNode(_diagnosticListener); 621 var node = _class.parseNode(_diagnosticListener);
490 if (node !== null) { 622 if (node !== null) {
491 var script = _class.getCompilationUnit().script; 623 var script = _class.getCompilationUnit().script;
492 var span = system.compiler.spanFromNode(node, script.uri); 624 var span = system.compiler.spanFromNode(node, script.uri);
493 return new Dart2jsLocation(script, span); 625 return new Dart2JsLocation(script, span);
494 } 626 }
495 } 627 }
496 return super.location(); 628 return super.location();
497 } 629 }
498 630
499 void _ensureMembers() { 631 void _ensureMembers() {
500 if (_members == null) { 632 if (_members == null) {
501 _members = <Dart2jsMemberMirror>{}; 633 _members = <Dart2JsMemberMirror>{};
502 _class.constructors.forEach((_, e) { 634 _class.constructors.forEach((_, e) {
503 for (var member in _convertElementMemberToMemberMirrors(this, e)) { 635 for (var member in _convertElementMemberToMemberMirrors(this, e)) {
504 _members[member.canonicalName] = member; 636 _members[member.canonicalName] = member;
505 } 637 }
506 }); 638 });
507 _class.localMembers.forEach((_, e) { 639 _class.localMembers.forEach((_, e) {
508 for (var member in _convertElementMemberToMemberMirrors(this, e)) { 640 for (var member in _convertElementMemberToMemberMirrors(this, e)) {
509 _members[member.canonicalName] = member; 641 _members[member.canonicalName] = member;
510 } 642 }
511 }); 643 });
(...skipping 18 matching lines...) Expand all
530 bool get isTypeVariable() => false; 662 bool get isTypeVariable() => false;
531 663
532 bool get isTypedef() => false; 664 bool get isTypedef() => false;
533 665
534 bool get isFunction() => false; 666 bool get isFunction() => false;
535 667
536 InterfaceMirror get declaration() => this; 668 InterfaceMirror get declaration() => this;
537 669
538 InterfaceMirror superclass() { 670 InterfaceMirror superclass() {
539 if (_class.supertype != null) { 671 if (_class.supertype != null) {
540 return new Dart2jsInterfaceTypeMirror(system, _class.supertype); 672 return new Dart2JsInterfaceTypeMirror(system, _class.supertype);
541 } 673 }
542 return null; 674 return null;
543 } 675 }
544 676
545 Map<Object, InterfaceMirror> interfaces() { 677 Map<Object, InterfaceMirror> interfaces() {
546 var map = new Map<String, InterfaceMirror>(); 678 var map = new Map<String, InterfaceMirror>();
547 Link<Type> link = _class.interfaces; 679 Link<Type> link = _class.interfaces;
548 while (!link.isEmpty()) { 680 while (!link.isEmpty()) {
549 var type = _convertTypeToTypeMirror(system, link.head, 681 var type = _convertTypeToTypeMirror(system, link.head,
550 system.compiler.dynamicClass.computeType(system.compiler)); 682 system.compiler.dynamicClass.computeType(system.compiler));
(...skipping 14 matching lines...) Expand all
565 List<TypeMirror> typeArguments() { 697 List<TypeMirror> typeArguments() {
566 throw new UnsupportedOperationException( 698 throw new UnsupportedOperationException(
567 'Declarations do not have type arguments'); 699 'Declarations do not have type arguments');
568 } 700 }
569 701
570 List<TypeVariableMirror> typeVariables() { 702 List<TypeVariableMirror> typeVariables() {
571 if (_typeVariables == null) { 703 if (_typeVariables == null) {
572 _typeVariables = <TypeVariableMirror>[]; 704 _typeVariables = <TypeVariableMirror>[];
573 _class.typeParameters.forEach((_,parameter) { 705 _class.typeParameters.forEach((_,parameter) {
574 _typeVariables.add( 706 _typeVariables.add(
575 new Dart2jsTypeVariableMirror(system, 707 new Dart2JsTypeVariableMirror(system,
576 parameter.computeType(system.compiler))); 708 parameter.computeType(system.compiler)));
577 }); 709 });
578 } 710 }
579 return _typeVariables; 711 return _typeVariables;
580 } 712 }
581 713
582 Map<Object, MethodMirror> constructors() { 714 Map<Object, MethodMirror> constructors() {
583 _ensureMembers(); 715 _ensureMembers();
584 return new AsFilteredImmutableMap<Object, MemberMirror, MethodMirror>( 716 return new AsFilteredImmutableMap<Object, MemberMirror, MethodMirror>(
585 _members, (m) => m.isConstructor ? m : null); 717 _members, (m) => m.isConstructor ? m : null);
586 } 718 }
587 719
588 /** 720 /**
589 * Returns the default type for this interface. 721 * Returns the default type for this interface.
590 */ 722 */
591 InterfaceMirror defaultType() { 723 InterfaceMirror defaultType() {
592 if (_class.defaultClass != null) { 724 if (_class.defaultClass != null) {
593 return new Dart2jsInterfaceTypeMirror(system, _class.defaultClass); 725 return new Dart2JsInterfaceTypeMirror(system, _class.defaultClass);
594 } 726 }
595 return null; 727 return null;
596 } 728 }
597 729
598 bool operator ==(Object other) { 730 bool operator ==(Object other) {
599 if (this === other) { 731 if (this === other) {
600 return true; 732 return true;
601 } 733 }
602 if (other is! InterfaceMirror) { 734 if (other is! InterfaceMirror) {
603 return false; 735 return false;
604 } 736 }
605 if (library() != other.library()) { 737 if (library() != other.library()) {
606 return false; 738 return false;
607 } 739 }
608 if (isDeclaration !== other.isDeclaration) { 740 if (isDeclaration !== other.isDeclaration) {
609 return false; 741 return false;
610 } 742 }
611 return qualifiedName() == other.qualifiedName(); 743 return qualifiedName() == other.qualifiedName();
612 } 744 }
613 } 745 }
614 746
615 class Dart2jsTypedefMirror extends Dart2jsElementMirror 747 class Dart2JsTypedefMirror extends Dart2JsElementMirror
616 implements Dart2jsTypeMirror, TypedefMirror { 748 implements Dart2JsTypeMirror, TypedefMirror {
617 final Dart2jsLibraryMirror _library; 749 final Dart2JsLibraryMirror _library;
618 List<TypeVariableMirror> _typeVariables; 750 List<TypeVariableMirror> _typeVariables;
619 TypeMirror _definition; 751 TypeMirror _definition;
620 752
621 Dart2jsTypedefMirror(Dart2jsMirrorSystem system, TypedefElement _typedef) 753 Dart2JsTypedefMirror(Dart2JsMirrorSystem system, TypedefElement _typedef)
622 : this._library = system.getLibrary(_typedef.getLibrary()), 754 : this._library = system.getLibrary(_typedef.getLibrary()),
623 super(system, _typedef); 755 super(system, _typedef);
624 756
625 Dart2jsTypedefMirror.fromLibrary(Dart2jsLibraryMirror library, 757 Dart2JsTypedefMirror.fromLibrary(Dart2JsLibraryMirror library,
626 TypedefElement _typedef) 758 TypedefElement _typedef)
627 : this._library = library, 759 : this._library = library,
628 super(library.system, _typedef); 760 super(library.system, _typedef);
629 761
630 TypedefElement get _typedef() => _element; 762 TypedefElement get _typedef() => _element;
631 763
632 String get canonicalName() => simpleName(); 764 String get canonicalName() => simpleName();
633 765
634 String qualifiedName() => '${library().qualifiedName()}.${simpleName()}'; 766 String qualifiedName() => '${library().qualifiedName()}.${simpleName()}';
635 767
636 Location location() { 768 Location location() {
637 var node = _typedef.parseNode(_diagnosticListener); 769 var node = _typedef.parseNode(_diagnosticListener);
638 if (node !== null) { 770 if (node !== null) {
639 var script = _typedef.getCompilationUnit().script; 771 var script = _typedef.getCompilationUnit().script;
640 var span = system.compiler.spanFromNode(node, script.uri); 772 var span = system.compiler.spanFromNode(node, script.uri);
641 return new Dart2jsLocation(script, span); 773 return new Dart2JsLocation(script, span);
642 } 774 }
643 return super.location(); 775 return super.location();
644 } 776 }
645 777
646 LibraryMirror library() => _library; 778 LibraryMirror library() => _library;
647 779
648 bool get isObject() => false; 780 bool get isObject() => false;
649 781
650 bool get isDynamic() => false; 782 bool get isDynamic() => false;
651 783
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 826
695 bool get isPrivate() => _isPrivate(simpleName()); 827 bool get isPrivate() => _isPrivate(simpleName());
696 828
697 bool get isDeclaration() => true; 829 bool get isDeclaration() => true;
698 830
699 Map<Object, MethodMirror> constructors() => const <MethodMirror>{}; 831 Map<Object, MethodMirror> constructors() => const <MethodMirror>{};
700 832
701 InterfaceMirror defaultType() => null; 833 InterfaceMirror defaultType() => null;
702 } 834 }
703 835
704 class Dart2jsTypeVariableMirror extends Dart2jsTypeElementMirror 836 class Dart2JsTypeVariableMirror extends Dart2JsTypeElementMirror
705 implements TypeVariableMirror { 837 implements TypeVariableMirror {
706 final TypeVariableType _typeVariableType; 838 final TypeVariableType _typeVariableType;
707 InterfaceMirror _declarer; 839 InterfaceMirror _declarer;
708 840
709 Dart2jsTypeVariableMirror(Dart2jsMirrorSystem system, 841 Dart2JsTypeVariableMirror(Dart2JsMirrorSystem system,
710 TypeVariableType typeVariableType) 842 TypeVariableType typeVariableType)
711 : this._typeVariableType = typeVariableType, 843 : this._typeVariableType = typeVariableType,
712 super(system, typeVariableType) { 844 super(system, typeVariableType) {
713 assert(_typeVariableType !== null); 845 assert(_typeVariableType !== null);
714 } 846 }
715 847
716 848
717 String qualifiedName() => '${declarer().qualifiedName()}.${simpleName()}'; 849 String qualifiedName() => '${declarer().qualifiedName()}.${simpleName()}';
718 850
719 InterfaceMirror declarer() { 851 InterfaceMirror declarer() {
720 if (_declarer === null) { 852 if (_declarer === null) {
721 if (_typeVariableType.element.enclosingElement.isClass()) { 853 if (_typeVariableType.element.enclosingElement.isClass()) {
722 _declarer = new Dart2jsInterfaceMirror(system, 854 _declarer = new Dart2JsInterfaceMirror(system,
723 _typeVariableType.element.enclosingElement); 855 _typeVariableType.element.enclosingElement);
724 } else if (_typeVariableType.element.enclosingElement.isTypedef()) { 856 } else if (_typeVariableType.element.enclosingElement.isTypedef()) {
725 _declarer = new Dart2jsTypedefMirror(system, 857 _declarer = new Dart2JsTypedefMirror(system,
726 _typeVariableType.element.enclosingElement); 858 _typeVariableType.element.enclosingElement);
727 } 859 }
728 } 860 }
729 return _declarer; 861 return _declarer;
730 } 862 }
731 863
732 LibraryMirror library() => declarer().library(); 864 LibraryMirror library() => declarer().library();
733 865
734 bool get isObject() => false; 866 bool get isObject() => false;
735 867
(...skipping 24 matching lines...) Expand all
760 } 892 }
761 return qualifiedName() == other.qualifiedName(); 893 return qualifiedName() == other.qualifiedName();
762 } 894 }
763 } 895 }
764 896
765 897
766 //------------------------------------------------------------------------------ 898 //------------------------------------------------------------------------------
767 // Types 899 // Types
768 //------------------------------------------------------------------------------ 900 //------------------------------------------------------------------------------
769 901
770 abstract class Dart2jsTypeElementMirror extends Dart2jsProxyMirror 902 abstract class Dart2JsTypeElementMirror extends Dart2JsProxyMirror
771 implements Dart2jsTypeMirror { 903 implements Dart2JsTypeMirror {
772 final Type _type; 904 final Type _type;
773 905
774 Dart2jsTypeElementMirror(Dart2jsMirrorSystem system, this._type) 906 Dart2JsTypeElementMirror(Dart2JsMirrorSystem system, this._type)
775 : super(system); 907 : super(system);
776 908
777 String simpleName() => _type.name.slowToString(); 909 String simpleName() => _type.name.slowToString();
778 910
779 String get canonicalName() => simpleName(); 911 String get canonicalName() => simpleName();
780 912
781 Location location() { 913 Location location() {
782 var script = _type.element.getCompilationUnit().script; 914 var script = _type.element.getCompilationUnit().script;
783 return new Dart2jsLocation(script, 915 return new Dart2JsLocation(script,
784 system.compiler.spanFromElement(_type.element)); 916 system.compiler.spanFromElement(_type.element));
785 } 917 }
786 918
787 LibraryMirror library() { 919 LibraryMirror library() {
788 return system.getLibrary(_type.element.getLibrary()); 920 return system.getLibrary(_type.element.getLibrary());
789 } 921 }
790 922
791 String toString() => _type.element.toString(); 923 String toString() => _type.element.toString();
792 } 924 }
793 925
794 class Dart2jsInterfaceTypeMirror extends Dart2jsTypeElementMirror 926 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror
795 implements InterfaceMirror { 927 implements InterfaceMirror {
796 List<TypeMirror> _typeArguments; 928 List<TypeMirror> _typeArguments;
797 929
798 Dart2jsInterfaceTypeMirror(Dart2jsMirrorSystem system, 930 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system,
799 InterfaceType interfaceType) 931 InterfaceType interfaceType)
800 : super(system, interfaceType); 932 : super(system, interfaceType);
801 933
802 InterfaceType get _interfaceType() => _type; 934 InterfaceType get _interfaceType() => _type;
803 935
804 String qualifiedName() => declaration.qualifiedName(); 936 String qualifiedName() => declaration.qualifiedName();
805 937
806 // TODO(johnniwinther): Substitute type arguments for type variables. 938 // TODO(johnniwinther): Substitute type arguments for type variables.
807 Map<Object, MemberMirror> declaredMembers() => declaration.declaredMembers(); 939 Map<Object, MemberMirror> declaredMembers() => declaration.declaredMembers();
808 940
809 bool get isObject() => system.compiler.objectClass == _type.element; 941 bool get isObject() => system.compiler.objectClass == _type.element;
810 942
811 bool get isDynamic() => system.compiler.dynamicClass == _type.element; 943 bool get isDynamic() => system.compiler.dynamicClass == _type.element;
812 944
813 bool get isTypeVariable() => false; 945 bool get isTypeVariable() => false;
814 946
815 bool get isVoid() => false; 947 bool get isVoid() => false;
816 948
817 bool get isTypedef() => false; 949 bool get isTypedef() => false;
818 950
819 bool get isFunction() => false; 951 bool get isFunction() => false;
820 952
821 InterfaceMirror get declaration() 953 InterfaceMirror get declaration()
822 => new Dart2jsInterfaceMirror(system, _type.element); 954 => new Dart2JsInterfaceMirror(system, _type.element);
823 955
824 // TODO(johnniwinther): Substitute type arguments for type variables. 956 // TODO(johnniwinther): Substitute type arguments for type variables.
825 InterfaceMirror superclass() => declaration.superclass(); 957 InterfaceMirror superclass() => declaration.superclass();
826 958
827 // TODO(johnniwinther): Substitute type arguments for type variables. 959 // TODO(johnniwinther): Substitute type arguments for type variables.
828 Map<Object, InterfaceMirror> interfaces() => declaration.interfaces(); 960 Map<Object, InterfaceMirror> interfaces() => declaration.interfaces();
829 961
830 bool get isClass() => declaration.isClass; 962 bool get isClass() => declaration.isClass;
831 963
832 bool get isInterface() => declaration.isInterface; 964 bool get isInterface() => declaration.isInterface;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 while (thisTypeArguments.hasNext() && otherTypeArguments.hasNext()) { 1006 while (thisTypeArguments.hasNext() && otherTypeArguments.hasNext()) {
875 if (thisTypeArguments.next() != otherTypeArguments.next()) { 1007 if (thisTypeArguments.next() != otherTypeArguments.next()) {
876 return false; 1008 return false;
877 } 1009 }
878 } 1010 }
879 return !thisTypeArguments.hasNext() && !otherTypeArguments.hasNext(); 1011 return !thisTypeArguments.hasNext() && !otherTypeArguments.hasNext();
880 } 1012 }
881 } 1013 }
882 1014
883 1015
884 class Dart2jsFunctionTypeMirror extends Dart2jsTypeElementMirror 1016 class Dart2JsFunctionTypeMirror extends Dart2JsTypeElementMirror
885 implements FunctionTypeMirror { 1017 implements FunctionTypeMirror {
886 final FunctionSignature _functionSignature; 1018 final FunctionSignature _functionSignature;
887 List<ParameterMirror> _parameters; 1019 List<ParameterMirror> _parameters;
888 1020
889 Dart2jsFunctionTypeMirror(Dart2jsMirrorSystem system, 1021 Dart2JsFunctionTypeMirror(Dart2JsMirrorSystem system,
890 FunctionType functionType, this._functionSignature) 1022 FunctionType functionType, this._functionSignature)
891 : super(system, functionType) { 1023 : super(system, functionType) {
892 assert (_functionSignature !== null); 1024 assert (_functionSignature !== null);
893 } 1025 }
894 1026
895 FunctionType get _functionType() => _type; 1027 FunctionType get _functionType() => _type;
896 1028
897 // TODO(johnniwinther): Is this the qualified name of a function type? 1029 // TODO(johnniwinther): Is this the qualified name of a function type?
898 String qualifiedName() => declaration.qualifiedName(); 1030 String qualifiedName() => declaration.qualifiedName();
899 1031
(...skipping 21 matching lines...) Expand all
921 1053
922 bool get isTypedef() => false; 1054 bool get isTypedef() => false;
923 1055
924 bool get isFunction() => true; 1056 bool get isFunction() => true;
925 1057
926 MethodMirror callMethod() => _convertElementMethodToMethodMirror( 1058 MethodMirror callMethod() => _convertElementMethodToMethodMirror(
927 system.getLibrary(_functionType.element.getLibrary()), 1059 system.getLibrary(_functionType.element.getLibrary()),
928 _functionType.element); 1060 _functionType.element);
929 1061
930 InterfaceMirror get declaration() 1062 InterfaceMirror get declaration()
931 => new Dart2jsInterfaceMirror(system, system.compiler.functionClass); 1063 => new Dart2JsInterfaceMirror(system, system.compiler.functionClass);
932 1064
933 // TODO(johnniwinther): Substitute type arguments for type variables. 1065 // TODO(johnniwinther): Substitute type arguments for type variables.
934 InterfaceMirror superclass() => declaration.superclass(); 1066 InterfaceMirror superclass() => declaration.superclass();
935 1067
936 // TODO(johnniwinther): Substitute type arguments for type variables. 1068 // TODO(johnniwinther): Substitute type arguments for type variables.
937 Map<Object, InterfaceMirror> interfaces() => declaration.interfaces(); 1069 Map<Object, InterfaceMirror> interfaces() => declaration.interfaces();
938 1070
939 bool get isClass() => declaration.isClass; 1071 bool get isClass() => declaration.isClass;
940 1072
941 bool get isInterface() => declaration.isInterface; 1073 bool get isInterface() => declaration.isInterface;
(...skipping 17 matching lines...) Expand all
959 1091
960 List<ParameterMirror> parameters() { 1092 List<ParameterMirror> parameters() {
961 if (_parameters === null) { 1093 if (_parameters === null) {
962 _parameters = _parametersFromFunctionSignature(system, callMethod(), 1094 _parameters = _parametersFromFunctionSignature(system, callMethod(),
963 _functionSignature); 1095 _functionSignature);
964 } 1096 }
965 return _parameters; 1097 return _parameters;
966 } 1098 }
967 } 1099 }
968 1100
969 class Dart2jsVoidMirror extends Dart2jsTypeElementMirror { 1101 class Dart2JsVoidMirror extends Dart2JsTypeElementMirror {
970 1102
971 Dart2jsVoidMirror(Dart2jsMirrorSystem system, VoidType voidType) 1103 Dart2JsVoidMirror(Dart2JsMirrorSystem system, VoidType voidType)
972 : super(system, voidType); 1104 : super(system, voidType);
973 1105
974 VoidType get _voidType() => _type; 1106 VoidType get _voidType() => _type;
975 1107
976 String qualifiedName() => simpleName(); 1108 String qualifiedName() => simpleName();
977 1109
978 /** 1110 /**
979 * The void type has no location. 1111 * The void type has no location.
980 */ 1112 */
981 Location location() => null; 1113 Location location() => null;
(...skipping 19 matching lines...) Expand all
1001 if (this === other) { 1133 if (this === other) {
1002 return true; 1134 return true;
1003 } 1135 }
1004 if (other is! TypeMirror) { 1136 if (other is! TypeMirror) {
1005 return false; 1137 return false;
1006 } 1138 }
1007 return other.isVoid; 1139 return other.isVoid;
1008 } 1140 }
1009 } 1141 }
1010 1142
1011 /////////////////////////////////////////////////////// 1143 //------------------------------------------------------------------------------
1012 // members 1144 // Member mirrors implementation.
1013 /////////////////////////////////////////////////////// 1145 //------------------------------------------------------------------------------
1014 1146
1015 class Dart2jsMethodMirror extends Dart2jsElementMirror 1147 class Dart2JsMethodMirror extends Dart2JsElementMirror
1016 implements Dart2jsMemberMirror, MethodMirror { 1148 implements Dart2JsMemberMirror, MethodMirror {
1017 final Dart2jsObjectMirror _objectMirror; 1149 final Dart2JsObjectMirror _objectMirror;
1018 String _name; 1150 String _name;
1019 String _constructorName; 1151 String _constructorName;
1020 String _operatorName; 1152 String _operatorName;
1021 Dart2jsMethodKind _kind; 1153 Dart2JsMethodKind _kind;
1022 String _canonicalName; 1154 String _canonicalName;
1023 1155
1024 Dart2jsMethodMirror(Dart2jsObjectMirror objectMirror, 1156 Dart2JsMethodMirror(Dart2JsObjectMirror objectMirror,
1025 FunctionElement function, 1157 FunctionElement function,
1026 [Dart2jsMethodKind kind = null]) 1158 [Dart2JsMethodKind kind = null])
1027 : this._objectMirror = objectMirror, 1159 : this._objectMirror = objectMirror,
1028 this._kind = kind, 1160 this._kind = kind,
1029 super(objectMirror.system, function) { 1161 super(objectMirror.system, function) {
1030 _name = _element.name.slowToString(); 1162 _name = _element.name.slowToString();
1031 if (kind == null) { 1163 if (kind == null) {
1032 if (_function.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 1164 if (_function.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
1033 _constructorName = ''; 1165 _constructorName = '';
1034 int dollarPos = _name.indexOf('\$'); 1166 int dollarPos = _name.indexOf('\$');
1035 if (dollarPos != -1) { 1167 if (dollarPos != -1) {
1036 _constructorName = _name.substring(dollarPos+1); 1168 _constructorName = _name.substring(dollarPos+1);
1037 _name = _name.substring(0, dollarPos); 1169 _name = _name.substring(0, dollarPos);
1038 // canonical name is TypeName.constructorName 1170 // canonical name is TypeName.constructorName
1039 _canonicalName = '$_name.$_constructorName'; 1171 _canonicalName = '$_name.$_constructorName';
1040 } else { 1172 } else {
1041 // canonical name is TypeName 1173 // canonical name is TypeName
1042 _canonicalName = _name; 1174 _canonicalName = _name;
1043 } 1175 }
1044 if (_function.modifiers !== null && _function.modifiers.isConst()) { 1176 if (_function.modifiers !== null && _function.modifiers.isConst()) {
1045 _kind = Dart2jsMethodKind.CONST; 1177 _kind = Dart2JsMethodKind.CONST;
1046 } else { 1178 } else {
1047 _kind = Dart2jsMethodKind.CONSTRUCTOR; 1179 _kind = Dart2JsMethodKind.CONSTRUCTOR;
1048 } 1180 }
1049 } else if (_function.modifiers !== null 1181 } else if (_function.modifiers !== null
1050 && _function.modifiers.isFactory()) { 1182 && _function.modifiers.isFactory()) {
1051 _constructorName = ''; 1183 _constructorName = '';
1052 int dollarPos = _name.indexOf('\$'); 1184 int dollarPos = _name.indexOf('\$');
1053 if (dollarPos != -1) { 1185 if (dollarPos != -1) {
1054 _constructorName = _name.substring(dollarPos+1); 1186 _constructorName = _name.substring(dollarPos+1);
1055 _name = _name.substring(0, dollarPos); 1187 _name = _name.substring(0, dollarPos);
1056 } 1188 }
1057 _kind = Dart2jsMethodKind.FACTORY; 1189 _kind = Dart2JsMethodKind.FACTORY;
1058 // canonical name is TypeName.constructorName 1190 // canonical name is TypeName.constructorName
1059 _canonicalName = '$_name.$_constructorName'; 1191 _canonicalName = '$_name.$_constructorName';
1060 } else if (_name.startsWith('operator\$')) { 1192 } else if (_name.startsWith('operator\$')) {
1061 String str = _name.substring(9); 1193 String str = _name.substring(9);
1062 _name = 'operator'; 1194 _name = 'operator';
1063 _kind = Dart2jsMethodKind.OPERATOR; 1195 _kind = Dart2JsMethodKind.OPERATOR;
1064 _operatorName = _getOperatorFromOperatorName(str); 1196 _operatorName = _getOperatorFromOperatorName(str);
1065 // canonical name is 'operator operatorName' 1197 // canonical name is 'operator operatorName'
1066 _canonicalName = 'operator $_operatorName'; 1198 _canonicalName = 'operator $_operatorName';
1067 } else { 1199 } else {
1068 _kind = Dart2jsMethodKind.NORMAL; 1200 _kind = Dart2JsMethodKind.NORMAL;
1069 _canonicalName = _name; 1201 _canonicalName = _name;
1070 } 1202 }
1071 } else if (kind == Dart2jsMethodKind.GETTER) { 1203 } else if (kind == Dart2JsMethodKind.GETTER) {
1072 _canonicalName = _name; 1204 _canonicalName = _name;
1073 } else if (kind == Dart2jsMethodKind.SETTER) { 1205 } else if (kind == Dart2JsMethodKind.SETTER) {
1074 _canonicalName = '$_name='; 1206 _canonicalName = '$_name=';
1075 } else { 1207 } else {
1076 assert(false); 1208 assert(false);
1077 } 1209 }
1078 } 1210 }
1079 1211
1080 FunctionElement get _function() => _element; 1212 FunctionElement get _function() => _element;
1081 1213
1082 String simpleName() => _name; 1214 String simpleName() => _name;
1083 1215
1084 String qualifiedName() 1216 String qualifiedName()
1085 => '${surroundingDeclaration().qualifiedName()}.$canonicalName'; 1217 => '${surroundingDeclaration().qualifiedName()}.$canonicalName';
1086 1218
1087 String get canonicalName() => _canonicalName; 1219 String get canonicalName() => _canonicalName;
1088 1220
1089 ObjectMirror surroundingDeclaration() => _objectMirror; 1221 ObjectMirror surroundingDeclaration() => _objectMirror;
1090 1222
1091 bool get isTopLevel() => _objectMirror is LibraryMirror; 1223 bool get isTopLevel() => _objectMirror is LibraryMirror;
1092 1224
1093 bool get isConstructor() 1225 bool get isConstructor()
1094 => _kind == Dart2jsMethodKind.CONSTRUCTOR || isConst || isFactory; 1226 => _kind == Dart2JsMethodKind.CONSTRUCTOR || isConst || isFactory;
1095 1227
1096 bool get isField() => false; 1228 bool get isField() => false;
1097 1229
1098 bool get isMethod() => !isConstructor; 1230 bool get isMethod() => !isConstructor;
1099 1231
1100 bool get isPrivate() => _isPrivate(simpleName()); 1232 bool get isPrivate() => _isPrivate(simpleName());
1101 1233
1102 bool get isStatic() => 1234 bool get isStatic() =>
1103 _function.modifiers !== null && _function.modifiers.isStatic(); 1235 _function.modifiers !== null && _function.modifiers.isStatic();
1104 1236
1105 List<ParameterMirror> parameters() { 1237 List<ParameterMirror> parameters() {
1106 return _parametersFromFunctionSignature(system, this, 1238 return _parametersFromFunctionSignature(system, this,
1107 _function.computeSignature(system.compiler)); 1239 _function.computeSignature(system.compiler));
1108 } 1240 }
1109 1241
1110 TypeMirror returnType() => _convertTypeToTypeMirror( 1242 TypeMirror returnType() => _convertTypeToTypeMirror(
1111 system, _function.computeSignature(system.compiler).returnType, 1243 system, _function.computeSignature(system.compiler).returnType,
1112 system.compiler.dynamicClass.computeType(system.compiler)); 1244 system.compiler.dynamicClass.computeType(system.compiler));
1113 1245
1114 bool get isConst() => _kind == Dart2jsMethodKind.CONST; 1246 bool get isConst() => _kind == Dart2JsMethodKind.CONST;
1115 1247
1116 bool get isFactory() => _kind == Dart2jsMethodKind.FACTORY; 1248 bool get isFactory() => _kind == Dart2JsMethodKind.FACTORY;
1117 1249
1118 String get constructorName() => _constructorName; 1250 String get constructorName() => _constructorName;
1119 1251
1120 bool get isGetter() => _kind == Dart2jsMethodKind.GETTER; 1252 bool get isGetter() => _kind == Dart2JsMethodKind.GETTER;
1121 1253
1122 bool get isSetter() => _kind == Dart2jsMethodKind.SETTER; 1254 bool get isSetter() => _kind == Dart2JsMethodKind.SETTER;
1123 1255
1124 bool get isOperator() => _kind == Dart2jsMethodKind.OPERATOR; 1256 bool get isOperator() => _kind == Dart2JsMethodKind.OPERATOR;
1125 1257
1126 String get operatorName() => _operatorName; 1258 String get operatorName() => _operatorName;
1127 1259
1128 Location location() { 1260 Location location() {
1129 var node = _function.parseNode(_diagnosticListener); 1261 var node = _function.parseNode(_diagnosticListener);
1130 if (node !== null) { 1262 if (node !== null) {
1131 var script = _function.getCompilationUnit().script; 1263 var script = _function.getCompilationUnit().script;
1132 var span = system.compiler.spanFromNode(node, script.uri); 1264 var span = system.compiler.spanFromNode(node, script.uri);
1133 return new Dart2jsLocation(script, span); 1265 return new Dart2JsLocation(script, span);
1134 } 1266 }
1135 return super.location(); 1267 return super.location();
1136 } 1268 }
1137 1269
1138 } 1270 }
1139 1271
1140 class Dart2jsFieldMirror extends Dart2jsElementMirror 1272 class Dart2JsFieldMirror extends Dart2JsElementMirror
1141 implements Dart2jsMemberMirror, FieldMirror { 1273 implements Dart2JsMemberMirror, FieldMirror {
1142 Dart2jsObjectMirror _objectMirror; 1274 Dart2JsObjectMirror _objectMirror;
1143 VariableElement _variable; 1275 VariableElement _variable;
1144 1276
1145 Dart2jsFieldMirror(Dart2jsObjectMirror objectMirror, 1277 Dart2JsFieldMirror(Dart2JsObjectMirror objectMirror,
1146 VariableElement variable) 1278 VariableElement variable)
1147 : this._objectMirror = objectMirror, 1279 : this._objectMirror = objectMirror,
1148 this._variable = variable, 1280 this._variable = variable,
1149 super(objectMirror.system, variable); 1281 super(objectMirror.system, variable);
1150 1282
1151 String qualifiedName() 1283 String qualifiedName()
1152 => '${surroundingDeclaration().qualifiedName()}.$canonicalName'; 1284 => '${surroundingDeclaration().qualifiedName()}.$canonicalName';
1153 1285
1154 String get canonicalName() => simpleName(); 1286 String get canonicalName() => simpleName();
1155 1287
(...skipping 15 matching lines...) Expand all
1171 1303
1172 TypeMirror type() => _convertTypeToTypeMirror(system, 1304 TypeMirror type() => _convertTypeToTypeMirror(system,
1173 _variable.computeType(system.compiler), 1305 _variable.computeType(system.compiler),
1174 system.compiler.dynamicClass.computeType(system.compiler)); 1306 system.compiler.dynamicClass.computeType(system.compiler));
1175 1307
1176 Location location() { 1308 Location location() {
1177 var script = _variable.getCompilationUnit().script; 1309 var script = _variable.getCompilationUnit().script;
1178 var node = _variable.variables.parseNode(_diagnosticListener); 1310 var node = _variable.variables.parseNode(_diagnosticListener);
1179 if (node !== null) { 1311 if (node !== null) {
1180 var span = system.compiler.spanFromNode(node, script.uri); 1312 var span = system.compiler.spanFromNode(node, script.uri);
1181 return new Dart2jsLocation(script, span); 1313 return new Dart2JsLocation(script, span);
1182 } else { 1314 } else {
1183 var span = system.compiler.spanFromElement(_variable); 1315 var span = system.compiler.spanFromElement(_variable);
1184 return new Dart2jsLocation(script, span); 1316 return new Dart2JsLocation(script, span);
1185 } 1317 }
1186 } 1318 }
1187 } 1319 }
1188 1320
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698