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

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: create-sdk.py updated 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 {
kasperl 2012/07/06 12:40:41 In general, I think I'd prefer calling these Dart2
Johnni Winther 2012/07/09 14:57:18 Done.
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;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
kasperl 2012/07/06 12:40:41 Terminate comment with .
Johnni Winther 2012/07/09 14:57:18 Done.
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.
Lasse Reichstein Nielsen 2012/07/09 10:39:33 Good choice. I expect to see MathNatives go away f
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) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(),
339 sourceFiles = <SourceFile>{}
340 {
Lasse Reichstein Nielsen 2012/07/09 10:39:33 Brace on previous line.
Johnni Winther 2012/07/09 14:57:18 Done.
341 var libraryUri = cwd.resolve(nativeToUriPath(libraryRoot));
342 var packageUri;
343 if (packageRoot !== null) {
344 packageUri = cwd.resolve(nativeToUriPath(packageRoot));
345 } else {
346 packageUri = libraryUri;
347 }
348 _compiler = new LibraryCompiler(provider, handler,
349 libraryUri, packageUri, <String>[]);
350 var librariesUri = <Uri>[];
351 for (var library in libraries) {
352 librariesUri.add(cwd.resolve(nativeToUriPath(library)));
353 // TODO(johnniwinther): Detect file not found
354 }
355 _compiler.runList(librariesUri);
356 }
357
233 void addLibrary(String path) { 358 void addLibrary(String path) {
234 var uri = cwd.resolve(nativeToUriPath(path)); 359 var uri = cwd.resolve(nativeToUriPath(path));
235 _compiler.scanner.loadLibrary(uri, null); 360 _compiler.scanner.loadLibrary(uri, null);
236 } 361 }
237 362
238 MirrorSystem mirrors() => new Dart2jsMirrorSystem(_compiler); 363 MirrorSystem mirrors() => new Dart2jsMirrorSystem(_compiler);
239 } 364 }
240 365
241 366
242 //------------------------------------------------------------------------------ 367 //------------------------------------------------------------------------------
(...skipping 25 matching lines...) Expand all
268 assert (_element !== null); 393 assert (_element !== null);
269 } 394 }
270 395
271 String simpleName() => _element.name.slowToString(); 396 String simpleName() => _element.name.slowToString();
272 397
273 Location location() => new Dart2jsLocation( 398 Location location() => new Dart2jsLocation(
274 _element.getCompilationUnit().script, 399 _element.getCompilationUnit().script,
275 system.compiler.spanFromElement(_element)); 400 system.compiler.spanFromElement(_element));
276 401
277 String toString() => _element.toString(); 402 String toString() => _element.toString();
403
404 int hashCode() => qualifiedName().hashCode();
278 } 405 }
279 406
280 abstract class Dart2jsProxyMirror implements Dart2jsMirror { 407 abstract class Dart2jsProxyMirror implements Dart2jsMirror {
281 final Dart2jsMirrorSystem system; 408 final Dart2jsMirrorSystem system;
282 409
283 Dart2jsProxyMirror(this.system); 410 Dart2jsProxyMirror(this.system);
411
412 int hashCode() => qualifiedName().hashCode();
284 } 413 }
285 414
286 /////////////////////////////////////////////////////// 415 ///////////////////////////////////////////////////////
287 // implementation 416 // implementation
288 /////////////////////////////////////////////////////// 417 ///////////////////////////////////////////////////////
289 418
290 class Dart2jsMirrorSystem implements MirrorSystem, Dart2jsMirror { 419 class Dart2jsMirrorSystem implements MirrorSystem, Dart2jsMirror {
291 final api.Compiler compiler; 420 final api.Compiler compiler;
292 Map<String, Dart2jsLibraryMirror> _libraries; 421 Map<String, Dart2jsLibraryMirror> _libraries;
293 Map<LibraryElement, Dart2jsLibraryMirror> _libraryMap; 422 Map<LibraryElement, Dart2jsLibraryMirror> _libraryMap;
(...skipping 20 matching lines...) Expand all
314 Dart2jsLibraryMirror getLibrary(LibraryElement element) { 443 Dart2jsLibraryMirror getLibrary(LibraryElement element) {
315 return _libraryMap[element]; 444 return _libraryMap[element];
316 } 445 }
317 446
318 Dart2jsMirrorSystem get system() => this; 447 Dart2jsMirrorSystem get system() => this;
319 448
320 String simpleName() => "mirror"; 449 String simpleName() => "mirror";
321 String qualifiedName() => simpleName(); 450 String qualifiedName() => simpleName();
322 451
323 String get canonicalName() => simpleName(); 452 String get canonicalName() => simpleName();
453
454 // TODO(johnniwinther): Hack! Dart2jsMirrorSystem need not be a Mirror.
455 int hashCode() => qualifiedName().hashCode();
324 } 456 }
325 457
326 abstract class Dart2jsObjectMirror extends Dart2jsElementMirror 458 abstract class Dart2jsObjectMirror extends Dart2jsElementMirror
327 implements ObjectMirror { 459 implements ObjectMirror {
328 Dart2jsObjectMirror(Dart2jsMirrorSystem system, Element element) 460 Dart2jsObjectMirror(Dart2jsMirrorSystem system, Element element)
329 : super(system, element); 461 : super(system, element);
330 } 462 }
331 463
332 class Dart2jsLibraryMirror extends Dart2jsObjectMirror 464 class Dart2jsLibraryMirror extends Dart2jsObjectMirror
333 implements LibraryMirror { 465 implements LibraryMirror {
334 Map<String, InterfaceMirror> _types; 466 Map<String, InterfaceMirror> _types;
335 Map<String, MemberMirror> _members; 467 Map<String, MemberMirror> _members;
336 468
337 Dart2jsLibraryMirror(Dart2jsMirrorSystem system, LibraryElement library) 469 Dart2jsLibraryMirror(Dart2jsMirrorSystem system, LibraryElement library)
338 : super(system, library); 470 : super(system, library);
339 471
340 LibraryElement get _library() => _element; 472 LibraryElement get _library() => _element;
341 473
474 LibraryMirror library() => this;
475
342 String get canonicalName() => simpleName(); 476 String get canonicalName() => simpleName();
343 477
344 /** 478 /**
345 * Returns the library name (for libraries with a #library tag) or the script 479 * 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 480 * 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. 481 * provide a 'library name' for scripts, to use for instance in dartdoc.
348 */ 482 */
349 String simpleName() { 483 String simpleName() {
350 if (_library.libraryTag !== null) { 484 if (_library.libraryTag !== null) {
351 return _library.libraryTag.argument.dartString.slowToString(); 485 return _library.libraryTag.argument.dartString.slowToString();
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
1179 if (node !== null) { 1313 if (node !== null) {
1180 var span = system.compiler.spanFromNode(node, script.uri); 1314 var span = system.compiler.spanFromNode(node, script.uri);
1181 return new Dart2jsLocation(script, span); 1315 return new Dart2jsLocation(script, span);
1182 } else { 1316 } else {
1183 var span = system.compiler.spanFromElement(_variable); 1317 var span = system.compiler.spanFromElement(_variable);
1184 return new Dart2jsLocation(script, span); 1318 return new Dart2jsLocation(script, span);
1185 } 1319 }
1186 } 1320 }
1187 } 1321 }
1188 1322
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698