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

Side by Side Diff: pkg/compiler/lib/src/compiler.dart

Issue 1819053002: Split loader from the rest of the compiler. This adds several abstractions to (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: add environment.dart Created 4 years, 9 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
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 dart2js.compiler_base; 5 library dart2js.compiler_base;
6 6
7 import 'dart:async' show 7 import 'dart:async' show
8 EventSink, 8 EventSink,
9 Future; 9 Future;
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 DeferredLoaderGetterElementX, 66 DeferredLoaderGetterElementX,
67 MethodElementX, 67 MethodElementX,
68 LibraryElementX, 68 LibraryElementX,
69 PrefixElementX; 69 PrefixElementX;
70 import 'enqueue.dart' show 70 import 'enqueue.dart' show
71 CodegenEnqueuer, 71 CodegenEnqueuer,
72 Enqueuer, 72 Enqueuer,
73 EnqueueTask, 73 EnqueueTask,
74 ResolutionEnqueuer, 74 ResolutionEnqueuer,
75 QueueFilter; 75 QueueFilter;
76 import 'environment.dart';
76 import 'io/source_information.dart' show 77 import 'io/source_information.dart' show
77 SourceInformation; 78 SourceInformation;
78 import 'js_backend/backend_helpers.dart' as js_backend show 79 import 'js_backend/backend_helpers.dart' as js_backend show
79 BackendHelpers; 80 BackendHelpers;
80 import 'js_backend/js_backend.dart' as js_backend show 81 import 'js_backend/js_backend.dart' as js_backend show
81 JavaScriptBackend; 82 JavaScriptBackend;
82 import 'library_loader.dart' show 83 import 'library_loader.dart' show
84 ElementScanner,
83 LibraryLoader, 85 LibraryLoader,
84 LibraryLoaderTask, 86 LibraryLoaderTask,
85 LoadedLibraries; 87 LoadedLibraries,
88 LibraryLoaderListener,
89 ResolvedUriTranslator,
90 ScriptLoader;
86 import 'mirrors_used.dart' show 91 import 'mirrors_used.dart' show
87 MirrorUsageAnalyzerTask; 92 MirrorUsageAnalyzerTask;
88 import 'common/names.dart' show 93 import 'common/names.dart' show
89 Selectors; 94 Selectors;
90 import 'null_compiler_output.dart' show 95 import 'null_compiler_output.dart' show
91 NullCompilerOutput, 96 NullCompilerOutput,
92 NullSink; 97 NullSink;
93 import 'parser/diet_parser_task.dart' show 98 import 'parser/diet_parser_task.dart' show
94 DietParserTask; 99 DietParserTask;
95 import 'parser/element_listener.dart' show 100 import 'parser/element_listener.dart' show
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 StaticUse; 144 StaticUse;
140 import 'universe/world_impact.dart' show 145 import 'universe/world_impact.dart' show
141 ImpactStrategy, 146 ImpactStrategy,
142 WorldImpact; 147 WorldImpact;
143 import 'util/util.dart' show 148 import 'util/util.dart' show
144 Link, 149 Link,
145 Setlet; 150 Setlet;
146 import 'world.dart' show 151 import 'world.dart' show
147 World; 152 World;
148 153
149 abstract class Compiler { 154 abstract class Compiler implements LibraryLoaderListener {
150 155
151 final Stopwatch totalCompileTime = new Stopwatch(); 156 final Stopwatch totalCompileTime = new Stopwatch();
152 int nextFreeClassId = 0; 157 int nextFreeClassId = 0;
153 World world; 158 World world;
154 Types types; 159 Types types;
155 _CompilerCoreTypes _coreTypes; 160 _CompilerCoreTypes _coreTypes;
156 _CompilerDiagnosticReporter _reporter; 161 _CompilerDiagnosticReporter _reporter;
157 _CompilerResolution _resolution; 162 _CompilerResolution _resolution;
158 _CompilerParsing _parsing; 163 _CompilerParsing _parsing;
159 164
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 278
274 /// The [bool.fromEnvironment] constructor. 279 /// The [bool.fromEnvironment] constructor.
275 ConstructorElement boolEnvironment; 280 ConstructorElement boolEnvironment;
276 281
277 /// The [String.fromEnvironment] constructor. 282 /// The [String.fromEnvironment] constructor.
278 ConstructorElement stringEnvironment; 283 ConstructorElement stringEnvironment;
279 284
280 /// Tracks elements with compile-time errors. 285 /// Tracks elements with compile-time errors.
281 final Set<Element> elementsWithCompileTimeErrors = new Set<Element>(); 286 final Set<Element> elementsWithCompileTimeErrors = new Set<Element>();
282 287
283 fromEnvironment(String name) => null; 288 final Environment environment;
289 // TODO(sigmund): delete once we migrate the rest of the compiler to use
290 // `environment` directly.
291 fromEnvironment(String name) => environment.valueOf(name);
Harry Terkelsen 2016/03/22 21:49:28 add @deprecated
Siggi Cherem (dart-lang) 2016/03/23 22:51:05 Done.
284 292
285 Element get currentElement => _reporter.currentElement; 293 Element get currentElement => _reporter.currentElement;
286 294
287 List<CompilerTask> tasks; 295 List<CompilerTask> tasks;
288 ScannerTask scanner; 296 ScannerTask scanner;
289 DietParserTask dietParser; 297 DietParserTask dietParser;
290 ParserTask parser; 298 ParserTask parser;
291 PatchParserTask patchParser; 299 PatchParserTask patchParser;
292 LibraryLoaderTask libraryLoader; 300 LibraryLoaderTask libraryLoader;
293 SerializationTask serialization; 301 SerializationTask serialization;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 bool get compilationFailed => compilationFailedInternal; 349 bool get compilationFailed => compilationFailedInternal;
342 350
343 void set compilationFailed(bool value) { 351 void set compilationFailed(bool value) {
344 if (value) { 352 if (value) {
345 elementsWithCompileTimeErrors.add(currentElement); 353 elementsWithCompileTimeErrors.add(currentElement);
346 } 354 }
347 compilationFailedInternal = value; 355 compilationFailedInternal = value;
348 } 356 }
349 357
350 Compiler({api.CompilerOptions options, 358 Compiler({api.CompilerOptions options,
351 api.CompilerOutput outputProvider}) 359 api.CompilerOutput outputProvider,
360 this.environment: const _EmptyEnvironment()})
352 : this.options = options, 361 : this.options = options,
353 this.cacheStrategy = new CacheStrategy(options.hasIncrementalSupport), 362 this.cacheStrategy = new CacheStrategy(options.hasIncrementalSupport),
354 this.userOutputProvider = outputProvider == null 363 this.userOutputProvider = outputProvider == null
355 ? const NullCompilerOutput() : outputProvider { 364 ? const NullCompilerOutput() : outputProvider {
356 365
357 world = new World(this); 366 world = new World(this);
358 // TODO(johnniwinther): Initialize core types in [initializeCoreClasses] and 367 // TODO(johnniwinther): Initialize core types in [initializeCoreClasses] and
359 // make its field final. 368 // make its field final.
360 _reporter = new _CompilerDiagnosticReporter( 369 _reporter = new _CompilerDiagnosticReporter(
361 this, options.diagnosticOptions); 370 this, options.diagnosticOptions);
(...skipping 20 matching lines...) Expand all
382 backend = jsBackend; 391 backend = jsBackend;
383 } else { 392 } else {
384 backend = new dart_backend.DartBackend(this, options.strips, 393 backend = new dart_backend.DartBackend(this, options.strips,
385 multiFile: options.dart2dartMultiFile); 394 multiFile: options.dart2dartMultiFile);
386 if (options.dumpInfo) { 395 if (options.dumpInfo) {
387 throw new ArgumentError('--dump-info is not supported for dart2dart.'); 396 throw new ArgumentError('--dump-info is not supported for dart2dart.');
388 } 397 }
389 } 398 }
390 399
391 tasks = [ 400 tasks = [
392 libraryLoader = new LibraryLoaderTask(this),
393 serialization = new SerializationTask(this),
394 scanner = new ScannerTask(this),
395 dietParser = new DietParserTask( 401 dietParser = new DietParserTask(
396 this, enableConditionalDirectives: options.enableConditionalDirectives ), 402 this, enableConditionalDirectives: options.enableConditionalDirectives ),
Harry Terkelsen 2016/03/22 21:49:28 long line
403 scanner = new ScannerTask(this),
404 serialization = new SerializationTask(this),
405 libraryLoader = new LibraryLoaderTask(this,
406 new _ResolvedUriTranslator(this),
407 new _ScriptLoader(this),
408 new _ElementScanner(scanner),
409 this.serialization,
410 this,
411 environment),
397 parser = new ParserTask( 412 parser = new ParserTask(
398 this, enableConditionalDirectives: options.enableConditionalDirectives ), 413 this, enableConditionalDirectives: options.enableConditionalDirectives ),
Harry Terkelsen 2016/03/22 21:49:28 ditto
Siggi Cherem (dart-lang) 2016/03/23 22:51:05 Done.
399 patchParser = new PatchParserTask( 414 patchParser = new PatchParserTask(
400 this, enableConditionalDirectives: options.enableConditionalDirectives ), 415 this, enableConditionalDirectives: options.enableConditionalDirectives ),
Harry Terkelsen 2016/03/22 21:49:27 ditto
Siggi Cherem (dart-lang) 2016/03/23 22:51:05 Done.
401 resolver = new ResolverTask(this, backend.constantCompilerTask), 416 resolver = new ResolverTask(this, backend.constantCompilerTask),
402 closureToClassMapper = new closureMapping.ClosureTask(this), 417 closureToClassMapper = new closureMapping.ClosureTask(this),
403 checker = new TypeCheckerTask(this), 418 checker = new TypeCheckerTask(this),
404 typesTask = new ti.TypesTask(this), 419 typesTask = new ti.TypesTask(this),
405 constants = backend.constantCompilerTask, 420 constants = backend.constantCompilerTask,
406 deferredLoadTask = new DeferredLoadTask(this), 421 deferredLoadTask = new DeferredLoadTask(this),
407 mirrorUsageAnalyzerTask = new MirrorUsageAnalyzerTask(this), 422 mirrorUsageAnalyzerTask = new MirrorUsageAnalyzerTask(this),
408 enqueuer = new EnqueueTask(this), 423 enqueuer = new EnqueueTask(this),
409 dumpInfoTask = new DumpInfoTask(this), 424 dumpInfoTask = new DumpInfoTask(this),
410 reuseLibraryTask = new GenericTask('Reuse library', this), 425 reuseLibraryTask = new GenericTask('Reuse library', this),
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 } 1159 }
1145 1160
1146 void fatalDiagnosticReported(DiagnosticMessage message, 1161 void fatalDiagnosticReported(DiagnosticMessage message,
1147 List<DiagnosticMessage> infos, 1162 List<DiagnosticMessage> infos,
1148 api.Diagnostic kind) { 1163 api.Diagnostic kind) {
1149 if (markCompilationAsFailed(message, kind)) { 1164 if (markCompilationAsFailed(message, kind)) {
1150 compilationFailed = true; 1165 compilationFailed = true;
1151 } 1166 }
1152 } 1167 }
1153 1168
1169 // TODO(sigmund): move this dart doc somewhere else too.
1154 /** 1170 /**
1155 * Translates the [resolvedUri] into a readable URI. 1171 * Translates the [resolvedUri] into a readable URI.
1156 * 1172 *
1157 * The [importingLibrary] holds the library importing [resolvedUri] or 1173 * The [importingLibrary] holds the library importing [resolvedUri] or
1158 * [:null:] if [resolvedUri] is loaded as the main library. The 1174 * [:null:] if [resolvedUri] is loaded as the main library. The
1159 * [importingLibrary] is used to grant access to internal libraries from 1175 * [importingLibrary] is used to grant access to internal libraries from
1160 * platform libraries and patch libraries. 1176 * platform libraries and patch libraries.
1161 * 1177 *
1162 * If the [resolvedUri] is not accessible from [importingLibrary], this method 1178 * If the [resolvedUri] is not accessible from [importingLibrary], this method
1163 * is responsible for reporting errors. 1179 * is responsible for reporting errors.
1164 * 1180 *
1165 * See [LibraryLoader] for terminology on URIs. 1181 * See [LibraryLoader] for terminology on URIs.
1166 */ 1182 */
1167 Uri translateResolvedUri(LibraryElement importingLibrary, 1183 Uri translateResolvedUri(LibraryElement importingLibrary,
1168 Uri resolvedUri, Spannable spannable) { 1184 Uri resolvedUri, Spannable spannable) {
1169 unimplemented(importingLibrary, 'Compiler.translateResolvedUri'); 1185 unimplemented(importingLibrary, 'Compiler.translateResolvedUri');
1170 return null; 1186 return null;
1171 } 1187 }
1172 1188
1173 /** 1189 /**
1174 * Reads the script specified by the [readableUri]. 1190 * Reads the script specified by the [readableUri].
1175 * 1191 *
1176 * See [LibraryLoader] for terminology on URIs. 1192 * See [LibraryLoader] for terminology on URIs.
1177 */ 1193 */
1178 Future<Script> readScript(Spannable node, Uri readableUri) { 1194 Future<Script> readScript(Uri readableUri, [Spannable node]) {
1179 unimplemented(node, 'Compiler.readScript'); 1195 unimplemented(node, 'Compiler.readScript');
1180 return null; 1196 return null;
1181 } 1197 }
1182 1198
1183 /// Compatible with [readScript] and used by [LibraryLoader] to create
1184 /// synthetic scripts to recover from read errors and bad URIs.
1185 Future<Script> synthesizeScript(Spannable node, Uri readableUri) {
1186 unimplemented(node, 'Compiler.synthesizeScript');
1187 return null;
1188 }
1189
1190 Element lookupElementIn(ScopeContainerElement container, String name) { 1199 Element lookupElementIn(ScopeContainerElement container, String name) {
1191 Element element = container.localLookup(name); 1200 Element element = container.localLookup(name);
1192 if (element == null) { 1201 if (element == null) {
1193 throw 'Could not find $name in $container'; 1202 throw 'Could not find $name in $container';
1194 } 1203 }
1195 return element; 1204 return element;
1196 } 1205 }
1197 1206
1198 bool get isMockCompilation => false; 1207 bool get isMockCompilation => false;
1199 1208
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1314 if (libraryUri.scheme == 'package') { 1323 if (libraryUri.scheme == 'package') {
1315 int slashPos = libraryUri.path.indexOf('/'); 1324 int slashPos = libraryUri.path.indexOf('/');
1316 if (slashPos != -1) { 1325 if (slashPos != -1) {
1317 String packageName = libraryUri.path.substring(0, slashPos); 1326 String packageName = libraryUri.path.substring(0, slashPos);
1318 return new Uri(scheme: 'package', path: packageName); 1327 return new Uri(scheme: 'package', path: packageName);
1319 } 1328 }
1320 } 1329 }
1321 return libraryUri; 1330 return libraryUri;
1322 } 1331 }
1323 1332
1324 void diagnoseCrashInUserCode(String message, exception, stackTrace) {
1325 // Overridden by Compiler in apiimpl.dart.
1326 }
1327
1328 void forgetElement(Element element) { 1333 void forgetElement(Element element) {
1329 enqueuer.forgetElement(element); 1334 enqueuer.forgetElement(element);
1330 if (element is MemberElement) { 1335 if (element is MemberElement) {
1331 for (Element closure in element.nestedClosures) { 1336 for (Element closure in element.nestedClosures) {
1332 // TODO(ahe): It would be nice to reuse names of nested closures. 1337 // TODO(ahe): It would be nice to reuse names of nested closures.
1333 closureToClassMapper.forgetElement(closure); 1338 closureToClassMapper.forgetElement(closure);
1334 } 1339 }
1335 } 1340 }
1336 backend.forgetElement(element); 1341 backend.forgetElement(element);
1337 } 1342 }
(...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after
2087 if (_otherDependencies == null) { 2092 if (_otherDependencies == null) {
2088 _otherDependencies = new Setlet<Element>(); 2093 _otherDependencies = new Setlet<Element>();
2089 } 2094 }
2090 _otherDependencies.add(element.implementation); 2095 _otherDependencies.add(element.implementation);
2091 } 2096 }
2092 2097
2093 Iterable<Element> get otherDependencies { 2098 Iterable<Element> get otherDependencies {
2094 return _otherDependencies != null ? _otherDependencies : const <Element>[]; 2099 return _otherDependencies != null ? _otherDependencies : const <Element>[];
2095 } 2100 }
2096 } 2101 }
2102
2103 // TODO(sigmund): in the future, each of these classes should be self contained
2104 // and not use references to `compiler`.
2105 class _ResolvedUriTranslator implements ResolvedUriTranslator {
2106 Compiler compiler;
2107 _ResolvedUriTranslator(this.compiler);
2108
2109 Uri translate(LibraryElement importingLibrary, Uri resolvedUri,
2110 [Spannable spannable]) =>
2111 compiler.translateResolvedUri(importingLibrary, resolvedUri, spannable);
2112 }
2113
2114 class _ScriptLoader implements ScriptLoader {
2115 Compiler compiler;
2116 _ScriptLoader(this.compiler);
2117
2118 Future<Script> readScript(Uri uri, [Spannable spannable]) =>
2119 compiler.readScript(uri, spannable);
2120 }
2121
2122 class _ElementScanner implements ElementScanner {
2123 ScannerTask scanner;
2124 _ElementScanner(this.scanner);
2125 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library);
2126 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit);
2127 }
2128
2129 class _EmptyEnvironment implements Environment {
2130 const _EmptyEnvironment();
2131
2132 String valueOf(String key) => null;
2133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698