Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:front_end/file_system.dart'; |
| 8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | |
| 9 import 'package:analyzer/dart/element/element.dart'; | |
| 10 import 'package:analyzer/error/error.dart'; | |
| 11 import 'package:analyzer/src/generated/engine.dart'; | |
| 12 import 'package:analyzer/src/generated/source.dart'; | |
| 13 import 'package:front_end/incremental_kernel_generator.dart'; | 8 import 'package:front_end/incremental_kernel_generator.dart'; |
| 14 import 'package:front_end/incremental_resolved_ast_generator.dart'; | 9 import 'package:front_end/incremental_resolved_ast_generator.dart'; |
| 15 import 'package:front_end/src/base/processed_options.dart'; | 10 import 'package:front_end/src/base/processed_options.dart'; |
| 16 import 'package:front_end/src/base/source.dart'; | 11 import 'package:front_end/src/fasta/dill/dill_target.dart'; |
| 17 import 'package:front_end/src/incremental_resolved_ast_generator_impl.dart'; | 12 import 'package:front_end/src/fasta/kernel/kernel_target.dart'; |
| 18 import 'package:analyzer/src/kernel/loader.dart'; | 13 import 'package:front_end/src/fasta/ticker.dart'; |
| 14 import 'package:front_end/src/fasta/translate_uri.dart'; | |
| 19 import 'package:kernel/kernel.dart' hide Source; | 15 import 'package:kernel/kernel.dart' hide Source; |
| 20 | 16 |
| 21 dynamic unimplemented() { | 17 dynamic unimplemented() { |
| 22 // TODO(paulberry): get rid of this. | 18 // TODO(paulberry): get rid of this. |
| 23 throw new UnimplementedError(); | 19 throw new UnimplementedError(); |
| 24 } | 20 } |
| 25 | 21 |
| 26 DartOptions _convertOptions(ProcessedOptions options) { | |
| 27 // TODO(paulberry): make sure options.compileSdk is handled correctly. | |
| 28 return new DartOptions( | |
| 29 strongMode: true, // TODO(paulberry): options.strongMode, | |
| 30 sdk: null, // TODO(paulberry): _uriToPath(options.sdkRoot, options), | |
| 31 sdkSummary: | |
| 32 null, // TODO(paulberry): options.compileSdk ? null : _uriToPath(optio ns.sdkSummary, options), | |
| 33 packagePath: | |
| 34 null, // TODO(paulberry): _uriToPath(options.packagesFileUri, options) , | |
| 35 declaredVariables: null // TODO(paulberry): options.declaredVariables | |
| 36 ); | |
| 37 } | |
| 38 | |
| 39 /// Implementation of [IncrementalKernelGenerator]. | 22 /// Implementation of [IncrementalKernelGenerator]. |
| 40 /// | 23 /// |
| 24 /// TODO(scheglov) Update the documentation. | |
| 25 /// | |
| 41 /// Theory of operation: an instance of [IncrementalResolvedAstGenerator] is | 26 /// Theory of operation: an instance of [IncrementalResolvedAstGenerator] is |
| 42 /// used to obtain resolved ASTs, and these are fed into kernel code generation | 27 /// used to obtain resolved ASTs, and these are fed into kernel code generation |
| 43 /// logic. | 28 /// logic. |
| 44 /// | |
| 45 /// Note that the kernel doesn't expect to take resolved ASTs as a direct input; | |
| 46 /// it expects to request resolved ASTs from an [AnalysisContext]. To deal with | |
| 47 /// this, we create [_AnalysisContextProxy] which returns the resolved ASTs when | |
| 48 /// requested. TODO(paulberry): Make this unnecessary. | |
| 49 class IncrementalKernelGeneratorImpl implements IncrementalKernelGenerator { | 29 class IncrementalKernelGeneratorImpl implements IncrementalKernelGenerator { |
| 50 final IncrementalResolvedAstGenerator _resolvedAstGenerator; | 30 /// The URI of the program entry point. |
| 31 final Uri _entryPoint; | |
| 32 | |
| 33 /// The compiler options, such as the [FileSystem], the SDK dill location, | |
| 34 /// etc. | |
| 51 final ProcessedOptions _options; | 35 final ProcessedOptions _options; |
| 52 | 36 |
| 53 IncrementalKernelGeneratorImpl(Uri source, ProcessedOptions options) | 37 /// The object that knows how to resolve "package:" and "dart:" URIs. |
| 54 : _resolvedAstGenerator = | 38 TranslateUri _uriTranslator; |
| 55 new IncrementalResolvedAstGeneratorImpl(source, options), | 39 |
| 56 _options = options; | 40 /// The cached SDK kernel. |
| 41 DillTarget _sdkDillTarget; | |
| 42 | |
| 43 IncrementalKernelGeneratorImpl(this._entryPoint, this._options); | |
| 57 | 44 |
| 58 @override | 45 @override |
| 59 Future<DeltaProgram> computeDelta( | 46 Future<DeltaProgram> computeDelta( |
| 60 {Future<Null> watch(Uri uri, bool used)}) async { | 47 {Future<Null> watch(Uri uri, bool used)}) async { |
| 61 var deltaLibraries = await _resolvedAstGenerator.computeDelta(); | 48 _uriTranslator ??= await _options.getUriTranslator(); |
| 62 var kernelOptions = _convertOptions(_options); | 49 |
| 63 var packages = null; // TODO(paulberry) | 50 DillTarget sdkTarget = await _getSdkDillTarget(); |
| 64 var kernels = <Uri, Program>{}; | 51 // TODO(scheglov) Use it to also serve other package kernels. |
| 65 for (Uri uri in deltaLibraries.newState.keys) { | 52 |
| 66 // The kernel generation code doesn't currently support building a kernel | 53 KernelTarget kernelTarget = new KernelTarget(_options.fileSystem, sdkTarget, |
| 67 // directly from resolved ASTs--it wants to query an analysis context. So | 54 _uriTranslator, _options.strongMode, null); |
| 68 // we provide it with a proxy analysis context that feeds it the resolved | 55 kernelTarget.read(_entryPoint); |
| 69 // ASTs. | 56 |
| 70 var strongMode = true; // TODO(paulberry): set this correctly | 57 // TODO(scheglov) Replace with a better API. |
| 71 var analysisOptions = new _AnalysisOptionsProxy(strongMode); | 58 // Firstly, we don't "write" anything here. |
|
Siggi Cherem (dart-lang)
2017/05/08 17:44:29
+1 :) it should not be called 'write'.
| |
| 72 var context = | 59 // Secondly, it catches all the exceptions and write them to `stderr`. |
| 73 new _AnalysisContextProxy(deltaLibraries.newState, analysisOptions); | 60 // This is too interactive and not API-clients friendly. |
|
Siggi Cherem (dart-lang)
2017/05/08 17:44:29
agree - we need to add a diagnostic API and pass i
| |
| 74 var program = new Program(); | 61 await kernelTarget.writeOutline(null); |
| 75 var loader = | 62 |
| 76 new DartLoader(program, kernelOptions, packages, context: context); | 63 // TODO(scheglov) Replace with a better API. |
| 77 loader.loadLibrary(uri); | 64 Program program = await kernelTarget.writeProgram(null); |
| 78 kernels[uri] = program; | 65 return new DeltaProgram({_entryPoint: program}); |
| 79 // TODO(paulberry) rework watch invocation to eliminate race condition, | |
| 80 // include part source files, and prevent watch from being a bottleneck | |
| 81 if (watch != null) await watch(uri, true); | |
| 82 } | |
| 83 // TODO(paulberry) invoke watch with used=false for each unused source | |
| 84 return new DeltaProgram(kernels); | |
| 85 } | 66 } |
| 86 | 67 |
| 87 @override | 68 @override |
| 88 void invalidate(String path) => _resolvedAstGenerator.invalidate(path); | 69 void invalidate(String path) => unimplemented(); |
| 89 | 70 |
| 90 @override | 71 @override |
| 91 void invalidateAll() => _resolvedAstGenerator.invalidateAll(); | 72 void invalidateAll() => unimplemented(); |
| 92 } | |
| 93 | 73 |
| 94 class _AnalysisContextProxy implements AnalysisContext { | 74 /// Return the [DillTarget] that is used inside of [KernelTarget] to |
| 95 final Map<Uri, Map<Uri, CompilationUnit>> _resolvedLibraries; | 75 /// resynthesize SDK libraries. |
| 96 | 76 Future<DillTarget> _getSdkDillTarget() async { |
| 97 @override | 77 if (_sdkDillTarget == null) { |
| 98 final _SourceFactoryProxy sourceFactory = new _SourceFactoryProxy(); | 78 _sdkDillTarget = |
| 99 | 79 new DillTarget(new Ticker(isVerbose: false), _uriTranslator); |
| 100 @override | 80 // TODO(scheglov) Read the SDK kernel. |
| 101 final AnalysisOptions analysisOptions; | 81 // _sdkDillTarget.read(options.sdkSummary); |
| 102 | 82 // await _sdkDillTarget.writeOutline(null); |
| 103 _AnalysisContextProxy(this._resolvedLibraries, this.analysisOptions); | 83 } else { |
| 104 | 84 // Program sdkProgram = _sdkDillTarget.loader.program; |
| 105 List<AnalysisError> computeErrors(Source source) { | 85 // sdkProgram.visitChildren(new _ClearCanonicalNamesVisitor()); |
| 106 // TODO(paulberry): do we need to return errors sometimes? | 86 } |
| 107 return []; | 87 return _sdkDillTarget; |
| 108 } | |
| 109 | |
| 110 LibraryElement computeLibraryElement(Source source) { | |
| 111 assert(_resolvedLibraries.containsKey(source.uri)); | |
| 112 return resolutionMap | |
| 113 .elementDeclaredByCompilationUnit( | |
| 114 _resolvedLibraries[source.uri][source.uri]) | |
| 115 .library; | |
| 116 } | |
| 117 | |
| 118 noSuchMethod(Invocation invocation) => unimplemented(); | |
| 119 | |
| 120 CompilationUnit resolveCompilationUnit( | |
| 121 Source unitSource, LibraryElement library) { | |
| 122 var unit = _resolvedLibraries[library.source.uri][unitSource.uri]; | |
| 123 assert(unit != null); | |
| 124 return unit; | |
| 125 } | 88 } |
| 126 } | 89 } |
| 127 | 90 |
| 128 class _AnalysisOptionsProxy implements AnalysisOptions { | 91 ///// Clears canonical names of [NamedNode] references. |
| 129 final bool strongMode; | 92 //class _ClearCanonicalNamesVisitor extends Visitor { |
| 130 | 93 // defaultNode(Node node) { |
| 131 _AnalysisOptionsProxy(this.strongMode); | 94 // if (node is NamedNode) { |
| 132 | 95 // node.reference.canonicalName = null; |
| 133 noSuchMethod(Invocation invocation) => unimplemented(); | 96 // } |
| 134 } | 97 // node.visitChildren(this); |
| 135 | 98 // } |
| 136 class _SourceFactoryProxy implements SourceFactory { | 99 //} |
| 137 Source forUri2(Uri absoluteUri) => new _SourceProxy(absoluteUri); | |
| 138 | |
| 139 noSuchMethod(Invocation invocation) => unimplemented(); | |
| 140 } | |
| 141 | |
| 142 class _SourceProxy extends BasicSource { | |
| 143 _SourceProxy(Uri uri) : super(uri); | |
| 144 | |
| 145 noSuchMethod(Invocation invocation) => unimplemented(); | |
| 146 } | |
| OLD | NEW |