| OLD | NEW |
| 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 EventSink, Future; | 7 import 'dart:async' show EventSink, Future; |
| 8 | 8 |
| 9 import '../compiler_new.dart' as api; | 9 import '../compiler_new.dart' as api; |
| 10 import 'cache_strategy.dart' show CacheStrategy; | 10 import 'cache_strategy.dart' show CacheStrategy; |
| 11 import 'closure.dart' as closureMapping show ClosureTask; | 11 import 'closure.dart' as closureMapping show ClosureTask; |
| 12 import 'common/backend_api.dart' show Backend; | 12 import 'common/backend_api.dart' show Backend; |
| 13 import 'common/codegen.dart' show CodegenWorkItem; | 13 import 'common/codegen.dart' show CodegenWorkItem; |
| 14 import 'common/names.dart' show Selectors; | 14 import 'common/names.dart' show Selectors; |
| 15 import 'common/names.dart' show Identifiers, Uris; | 15 import 'common/names.dart' show Identifiers, Uris; |
| 16 import 'common/registry.dart' show EagerRegistry, Registry; | 16 import 'common/registry.dart' show EagerRegistry, Registry; |
| 17 import 'common/resolution.dart' | 17 import 'common/resolution.dart' |
| 18 show | 18 show |
| 19 ParsingContext, | 19 ParsingContext, |
| 20 Resolution, | 20 Resolution, |
| 21 ResolutionWorkItem, | 21 ResolutionWorkItem, |
| 22 ResolutionImpact, | 22 ResolutionImpact, |
| 23 Target; | 23 Target; |
| 24 import 'common/tasks.dart' show CompilerTask, GenericTask, Measurer; | 24 import 'common/tasks.dart' show CompilerTask, GenericTask, Measurer; |
| 25 import 'common/work.dart' show ItemCompilationContext, WorkItem; | 25 import 'common/work.dart' show ItemCompilationContext, WorkItem; |
| 26 import 'common.dart'; | 26 import 'common.dart'; |
| 27 import 'compile_time_constants.dart'; | 27 import 'compile_time_constants.dart'; |
| 28 import 'constants/values.dart'; | 28 import 'constants/values.dart'; |
| 29 import 'core_types.dart' show CoreClasses, CoreTypes; | 29 import 'core_types.dart' show CoreClasses, CommonElements, CoreTypes; |
| 30 import 'dart_types.dart' show DartType, DynamicType, InterfaceType, Types; | 30 import 'dart_types.dart' show DartType, DynamicType, InterfaceType, Types; |
| 31 import 'deferred_load.dart' show DeferredLoadTask; | 31 import 'deferred_load.dart' show DeferredLoadTask; |
| 32 import 'diagnostics/code_location.dart'; | 32 import 'diagnostics/code_location.dart'; |
| 33 import 'diagnostics/diagnostic_listener.dart' show DiagnosticReporter; | 33 import 'diagnostics/diagnostic_listener.dart' show DiagnosticReporter; |
| 34 import 'diagnostics/invariant.dart' show REPORT_EXCESS_RESOLUTION; | 34 import 'diagnostics/invariant.dart' show REPORT_EXCESS_RESOLUTION; |
| 35 import 'diagnostics/messages.dart' show Message, MessageTemplate; | 35 import 'diagnostics/messages.dart' show Message, MessageTemplate; |
| 36 import 'dump_info.dart' show DumpInfoTask; | 36 import 'dump_info.dart' show DumpInfoTask; |
| 37 import 'elements/elements.dart'; | 37 import 'elements/elements.dart'; |
| 38 import 'elements/modelx.dart' show ErroneousElementX; | 38 import 'elements/modelx.dart' show ErroneousElementX; |
| 39 import 'enqueue.dart' | 39 import 'enqueue.dart' |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 138 |
| 139 /// Output provider from user of Compiler API. | 139 /// Output provider from user of Compiler API. |
| 140 api.CompilerOutput userOutputProvider; | 140 api.CompilerOutput userOutputProvider; |
| 141 | 141 |
| 142 List<Uri> librariesToAnalyzeWhenRun; | 142 List<Uri> librariesToAnalyzeWhenRun; |
| 143 | 143 |
| 144 ResolvedUriTranslator get resolvedUriTranslator; | 144 ResolvedUriTranslator get resolvedUriTranslator; |
| 145 | 145 |
| 146 Tracer tracer; | 146 Tracer tracer; |
| 147 | 147 |
| 148 LibraryElement coreLibrary; | |
| 149 LibraryElement asyncLibrary; | |
| 150 | |
| 151 LibraryElement mainApp; | 148 LibraryElement mainApp; |
| 152 FunctionElement mainFunction; | 149 FunctionElement mainFunction; |
| 153 | 150 |
| 154 /// Initialized when dart:mirrors is loaded. | |
| 155 LibraryElement mirrorsLibrary; | |
| 156 | |
| 157 /// Initialized when dart:typed_data is loaded. | |
| 158 LibraryElement typedDataLibrary; | |
| 159 | |
| 160 DiagnosticReporter get reporter => _reporter; | 151 DiagnosticReporter get reporter => _reporter; |
| 152 CommonElements get commonElements => _coreTypes; |
| 161 CoreClasses get coreClasses => _coreTypes; | 153 CoreClasses get coreClasses => _coreTypes; |
| 162 CoreTypes get coreTypes => _coreTypes; | 154 CoreTypes get coreTypes => _coreTypes; |
| 163 Resolution get resolution => _resolution; | 155 Resolution get resolution => _resolution; |
| 164 ParsingContext get parsingContext => _parsingContext; | 156 ParsingContext get parsingContext => _parsingContext; |
| 165 | 157 |
| 166 ClassElement typedDataClass; | |
| 167 | |
| 168 // TODO(johnniwinther): Move this to the JavaScriptBackend. | |
| 169 /// The class for patch annotation defined in dart:_js_helper. | |
| 170 ClassElement patchAnnotationClass; | |
| 171 | |
| 172 // TODO(johnniwinther): Move this to the JavaScriptBackend. | |
| 173 ClassElement nativeAnnotationClass; | |
| 174 | |
| 175 ConstructorElement _symbolConstructor; | |
| 176 ConstructorElement get symbolConstructor { | |
| 177 if (_symbolConstructor == null) { | |
| 178 ClassElement symbolClass = coreClasses.symbolClass; | |
| 179 symbolClass.ensureResolved(resolution); | |
| 180 _symbolConstructor = symbolClass.lookupConstructor(''); | |
| 181 assert(invariant(symbolClass, _symbolConstructor != null, | |
| 182 message: "Default constructor not found ${symbolClass}.")); | |
| 183 } | |
| 184 return _symbolConstructor; | |
| 185 } | |
| 186 | |
| 187 // Initialized when dart:mirrors is loaded. | |
| 188 ClassElement mirrorSystemClass; | |
| 189 | |
| 190 // Initialized when dart:mirrors is loaded. | |
| 191 ClassElement mirrorsUsedClass; | |
| 192 | |
| 193 // Initialized after mirrorSystemClass has been resolved. | |
| 194 FunctionElement mirrorSystemGetNameFunction; | |
| 195 | |
| 196 // Initialized when mirrorsUsedClass has been resolved. | |
| 197 FunctionElement mirrorsUsedConstructor; | |
| 198 | |
| 199 // Initialized when dart:mirrors is loaded. | |
| 200 ClassElement deferredLibraryClass; | |
| 201 | |
| 202 Element identicalFunction; | |
| 203 Element loadLibraryFunction; | |
| 204 Element functionApplyMethod; | |
| 205 | |
| 206 // TODO(zarah): Remove this map and incorporate compile-time errors | 158 // TODO(zarah): Remove this map and incorporate compile-time errors |
| 207 // in the model. | 159 // in the model. |
| 208 /// Tracks elements with compile-time errors. | 160 /// Tracks elements with compile-time errors. |
| 209 final Map<Element, List<DiagnosticMessage>> elementsWithCompileTimeErrors = | 161 final Map<Element, List<DiagnosticMessage>> elementsWithCompileTimeErrors = |
| 210 new Map<Element, List<DiagnosticMessage>>(); | 162 new Map<Element, List<DiagnosticMessage>>(); |
| 211 | 163 |
| 212 final Environment environment; | 164 final Environment environment; |
| 213 // TODO(sigmund): delete once we migrate the rest of the compiler to use | 165 // TODO(sigmund): delete once we migrate the rest of the compiler to use |
| 214 // `environment` directly. | 166 // `environment` directly. |
| 215 @deprecated | 167 @deprecated |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 : outputProvider { | 230 : outputProvider { |
| 279 world = new World(this); | 231 world = new World(this); |
| 280 if (makeReporter != null) { | 232 if (makeReporter != null) { |
| 281 _reporter = makeReporter(this, options); | 233 _reporter = makeReporter(this, options); |
| 282 } else { | 234 } else { |
| 283 _reporter = new CompilerDiagnosticReporter(this, options); | 235 _reporter = new CompilerDiagnosticReporter(this, options); |
| 284 } | 236 } |
| 285 _resolution = new _CompilerResolution(this); | 237 _resolution = new _CompilerResolution(this); |
| 286 // TODO(johnniwinther): Initialize core types in [initializeCoreClasses] and | 238 // TODO(johnniwinther): Initialize core types in [initializeCoreClasses] and |
| 287 // make its field final. | 239 // make its field final. |
| 288 _coreTypes = new _CompilerCoreTypes(_resolution); | 240 _coreTypes = new _CompilerCoreTypes(_resolution, reporter); |
| 289 types = new Types(_resolution); | 241 types = new Types(_resolution); |
| 290 tracer = new Tracer(this, this.outputProvider); | 242 tracer = new Tracer(this, this.outputProvider); |
| 291 | 243 |
| 292 if (options.verbose) { | 244 if (options.verbose) { |
| 293 progress = new Stopwatch()..start(); | 245 progress = new Stopwatch()..start(); |
| 294 } | 246 } |
| 295 | 247 |
| 296 // TODO(johnniwinther): Separate the dependency tracking from the enqueuing | 248 // TODO(johnniwinther): Separate the dependency tracking from the enqueuing |
| 297 // for global dependencies. | 249 // for global dependencies. |
| 298 globalDependencies = new GlobalDependencyRegistry(this); | 250 globalDependencies = new GlobalDependencyRegistry(this); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 314 } | 266 } |
| 315 | 267 |
| 316 tasks = [ | 268 tasks = [ |
| 317 dietParser = | 269 dietParser = |
| 318 new DietParserTask(options, idGenerator, backend, reporter, measurer), | 270 new DietParserTask(options, idGenerator, backend, reporter, measurer), |
| 319 scanner = createScannerTask(), | 271 scanner = createScannerTask(), |
| 320 serialization = new SerializationTask(this), | 272 serialization = new SerializationTask(this), |
| 321 libraryLoader = new LibraryLoaderTask( | 273 libraryLoader = new LibraryLoaderTask( |
| 322 resolvedUriTranslator, | 274 resolvedUriTranslator, |
| 323 options.compileOnly | 275 options.compileOnly |
| 324 ? new _NoScriptLoader(this) : new _ScriptLoader(this), | 276 ? new _NoScriptLoader(this) |
| 277 : new _ScriptLoader(this), |
| 325 new _ElementScanner(scanner), | 278 new _ElementScanner(scanner), |
| 326 serialization, | 279 serialization, |
| 327 this, | 280 this, |
| 328 environment, | 281 environment, |
| 329 reporter, | 282 reporter, |
| 330 measurer), | 283 measurer), |
| 331 parser = new ParserTask(this, options), | 284 parser = new ParserTask(this, options), |
| 332 patchParser = new PatchParserTask(this, options), | 285 patchParser = new PatchParserTask(this, options), |
| 333 resolver = createResolverTask(), | 286 resolver = createResolverTask(), |
| 334 closureToClassMapper = new closureMapping.ClosureTask(this), | 287 closureToClassMapper = new closureMapping.ClosureTask(this), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 }); | 352 }); |
| 400 }); | 353 }); |
| 401 | 354 |
| 402 /// This method is called immediately after the [LibraryElement] [library] has | 355 /// This method is called immediately after the [LibraryElement] [library] has |
| 403 /// been created. | 356 /// been created. |
| 404 /// | 357 /// |
| 405 /// Use this callback method to store references to specific libraries. | 358 /// Use this callback method to store references to specific libraries. |
| 406 /// Note that [library] has not been scanned yet, nor has its imports/exports | 359 /// Note that [library] has not been scanned yet, nor has its imports/exports |
| 407 /// been resolved. | 360 /// been resolved. |
| 408 void onLibraryCreated(LibraryElement library) { | 361 void onLibraryCreated(LibraryElement library) { |
| 409 Uri uri = library.canonicalUri; | 362 _coreTypes.onLibraryCreated(library); |
| 410 if (uri == Uris.dart_core) { | |
| 411 coreLibrary = library; | |
| 412 } else if (uri == Uris.dart__native_typed_data) { | |
| 413 typedDataLibrary = library; | |
| 414 } else if (uri == Uris.dart_mirrors) { | |
| 415 mirrorsLibrary = library; | |
| 416 } | |
| 417 backend.onLibraryCreated(library); | 363 backend.onLibraryCreated(library); |
| 418 } | 364 } |
| 419 | 365 |
| 420 /// This method is called immediately after the [library] and its parts have | 366 /// This method is called immediately after the [library] and its parts have |
| 421 /// been scanned. | 367 /// been scanned. |
| 422 /// | 368 /// |
| 423 /// Use this callback method to store references to specific member declared | 369 /// Use this callback method to store references to specific member declared |
| 424 /// in certain libraries. Note that [library] has not been patched yet, nor | 370 /// in certain libraries. Note that [library] has not been patched yet, nor |
| 425 /// has its imports/exports been resolved. | 371 /// has its imports/exports been resolved. |
| 426 /// | 372 /// |
| 427 /// Use [loader] to register the creation and scanning of a patch library | 373 /// Use [loader] to register the creation and scanning of a patch library |
| 428 /// for [library]. | 374 /// for [library]. |
| 429 Future onLibraryScanned(LibraryElement library, LibraryLoader loader) { | 375 Future onLibraryScanned(LibraryElement library, LibraryLoader loader) { |
| 430 Uri uri = library.canonicalUri; | |
| 431 // If the script of the library is synthesized, the library does not exist | |
| 432 // and we do not try to load the helpers. | |
| 433 // | |
| 434 // This could for example happen if dart:async is disabled, then loading it | |
| 435 // should not try to find the given element. | |
| 436 if (!library.isSynthesized) { | |
| 437 if (uri == Uris.dart_core) { | |
| 438 initializeCoreClasses(); | |
| 439 identicalFunction = coreLibrary.find('identical'); | |
| 440 } else if (uri == Uris.dart_mirrors) { | |
| 441 mirrorSystemClass = findRequiredElement(library, 'MirrorSystem'); | |
| 442 mirrorsUsedClass = findRequiredElement(library, 'MirrorsUsed'); | |
| 443 } else if (uri == Uris.dart_async) { | |
| 444 asyncLibrary = library; | |
| 445 deferredLibraryClass = findRequiredElement(library, 'DeferredLibrary'); | |
| 446 _coreTypes.futureClass = findRequiredElement(library, 'Future'); | |
| 447 _coreTypes.streamClass = findRequiredElement(library, 'Stream'); | |
| 448 } else if (uri == Uris.dart__native_typed_data) { | |
| 449 typedDataClass = findRequiredElement(library, 'NativeTypedData'); | |
| 450 } else if (uri == js_backend.BackendHelpers.DART_JS_HELPER) { | |
| 451 patchAnnotationClass = findRequiredElement(library, '_Patch'); | |
| 452 nativeAnnotationClass = findRequiredElement(library, 'Native'); | |
| 453 } | |
| 454 } | |
| 455 return backend.onLibraryScanned(library, loader); | 376 return backend.onLibraryScanned(library, loader); |
| 456 } | 377 } |
| 457 | 378 |
| 458 /// Compute the set of distinct import chains to the library at [uri] within | 379 /// Compute the set of distinct import chains to the library at [uri] within |
| 459 /// [loadedLibraries]. | 380 /// [loadedLibraries]. |
| 460 /// | 381 /// |
| 461 /// The chains are strings of the form | 382 /// The chains are strings of the form |
| 462 /// | 383 /// |
| 463 /// <main-uri> => <intermediate-uri1> => <intermediate-uri2> => <uri> | 384 /// <main-uri> => <intermediate-uri1> => <intermediate-uri2> => <uri> |
| 464 /// | 385 /// |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 }); | 472 }); |
| 552 } else if (importsMirrorsLibrary && !options.enableExperimentalMirrors) { | 473 } else if (importsMirrorsLibrary && !options.enableExperimentalMirrors) { |
| 553 Set<String> importChains = | 474 Set<String> importChains = |
| 554 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); | 475 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); |
| 555 reporter.reportWarningMessage( | 476 reporter.reportWarningMessage( |
| 556 NO_LOCATION_SPANNABLE, MessageKind.IMPORT_EXPERIMENTAL_MIRRORS, { | 477 NO_LOCATION_SPANNABLE, MessageKind.IMPORT_EXPERIMENTAL_MIRRORS, { |
| 557 'importChain': importChains | 478 'importChain': importChains |
| 558 .join(MessageTemplate.IMPORT_EXPERIMENTAL_MIRRORS_PADDING) | 479 .join(MessageTemplate.IMPORT_EXPERIMENTAL_MIRRORS_PADDING) |
| 559 }); | 480 }); |
| 560 } | 481 } |
| 561 | |
| 562 coreClasses.functionClass.ensureResolved(resolution); | |
| 563 functionApplyMethod = | |
| 564 coreClasses.functionClass.lookupLocalMember('apply'); | |
| 565 }).then((_) => backend.onLibrariesLoaded(loadedLibraries)); | 482 }).then((_) => backend.onLibrariesLoaded(loadedLibraries)); |
| 566 } | 483 } |
| 567 | 484 |
| 568 Element findRequiredElement(LibraryElement library, String name) { | |
| 569 var element = library.find(name); | |
| 570 if (element == null) { | |
| 571 reporter.internalError( | |
| 572 library, | |
| 573 "The library '${library.canonicalUri}' does not contain required " | |
| 574 "element: '$name'."); | |
| 575 } | |
| 576 return element; | |
| 577 } | |
| 578 | |
| 579 // TODO(johnniwinther): Move this to [PatchParser] when it is moved to the | 485 // TODO(johnniwinther): Move this to [PatchParser] when it is moved to the |
| 580 // [JavaScriptBackend]. Currently needed for testing. | 486 // [JavaScriptBackend]. Currently needed for testing. |
| 581 String get patchVersion => backend.patchVersion; | 487 String get patchVersion => backend.patchVersion; |
| 582 | 488 |
| 583 // TODO(johnniwinther): Remove this. All elements should be looked up on | |
| 584 // demand. | |
| 585 void onClassResolved(ClassElement cls) { | |
| 586 if (mirrorSystemClass == cls) { | |
| 587 mirrorSystemGetNameFunction = cls.lookupLocalMember('getName'); | |
| 588 } else if (mirrorsUsedClass == cls) { | |
| 589 mirrorsUsedConstructor = cls.constructors.head; | |
| 590 } | |
| 591 } | |
| 592 | |
| 593 void initializeCoreClasses() { | |
| 594 final List missingCoreClasses = []; | |
| 595 ClassElement lookupCoreClass(String name) { | |
| 596 ClassElement result = coreLibrary.find(name); | |
| 597 if (result == null) { | |
| 598 missingCoreClasses.add(name); | |
| 599 } | |
| 600 return result; | |
| 601 } | |
| 602 | |
| 603 _coreTypes.objectClass = lookupCoreClass('Object'); | |
| 604 _coreTypes.boolClass = lookupCoreClass('bool'); | |
| 605 _coreTypes.numClass = lookupCoreClass('num'); | |
| 606 _coreTypes.intClass = lookupCoreClass('int'); | |
| 607 _coreTypes.doubleClass = lookupCoreClass('double'); | |
| 608 _coreTypes.resourceClass = lookupCoreClass('Resource'); | |
| 609 _coreTypes.stringClass = lookupCoreClass('String'); | |
| 610 _coreTypes.functionClass = lookupCoreClass('Function'); | |
| 611 _coreTypes.listClass = lookupCoreClass('List'); | |
| 612 _coreTypes.typeClass = lookupCoreClass('Type'); | |
| 613 _coreTypes.mapClass = lookupCoreClass('Map'); | |
| 614 _coreTypes.nullClass = lookupCoreClass('Null'); | |
| 615 _coreTypes.stackTraceClass = lookupCoreClass('StackTrace'); | |
| 616 _coreTypes.iterableClass = lookupCoreClass('Iterable'); | |
| 617 _coreTypes.symbolClass = lookupCoreClass('Symbol'); | |
| 618 if (!missingCoreClasses.isEmpty) { | |
| 619 reporter.internalError( | |
| 620 coreLibrary, | |
| 621 'dart:core library does not contain required classes: ' | |
| 622 '$missingCoreClasses'); | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 Element _unnamedListConstructor; | 489 Element _unnamedListConstructor; |
| 627 Element get unnamedListConstructor { | 490 Element get unnamedListConstructor { |
| 628 if (_unnamedListConstructor != null) return _unnamedListConstructor; | 491 if (_unnamedListConstructor != null) return _unnamedListConstructor; |
| 629 return _unnamedListConstructor = | 492 return _unnamedListConstructor = |
| 630 coreClasses.listClass.lookupDefaultConstructor(); | 493 coreClasses.listClass.lookupDefaultConstructor(); |
| 631 } | 494 } |
| 632 | 495 |
| 633 Element _filledListConstructor; | 496 Element _filledListConstructor; |
| 634 Element get filledListConstructor { | 497 Element get filledListConstructor { |
| 635 if (_filledListConstructor != null) return _filledListConstructor; | 498 if (_filledListConstructor != null) return _filledListConstructor; |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1292 return userOutputProvider.createEventSink(name, extension); | 1155 return userOutputProvider.createEventSink(name, extension); |
| 1293 } | 1156 } |
| 1294 } | 1157 } |
| 1295 | 1158 |
| 1296 /// Information about suppressed warnings and hints for a given library. | 1159 /// Information about suppressed warnings and hints for a given library. |
| 1297 class SuppressionInfo { | 1160 class SuppressionInfo { |
| 1298 int warnings = 0; | 1161 int warnings = 0; |
| 1299 int hints = 0; | 1162 int hints = 0; |
| 1300 } | 1163 } |
| 1301 | 1164 |
| 1302 class _CompilerCoreTypes implements CoreTypes, CoreClasses { | 1165 class _CompilerCoreTypes implements CoreTypes, CoreClasses, CommonElements { |
| 1303 final Resolution resolution; | 1166 final Resolution resolution; |
| 1167 final DiagnosticReporter reporter; |
| 1304 | 1168 |
| 1305 ClassElement objectClass; | 1169 LibraryElement coreLibrary; |
| 1306 ClassElement boolClass; | 1170 LibraryElement asyncLibrary; |
| 1307 ClassElement numClass; | 1171 LibraryElement mirrorsLibrary; |
| 1308 ClassElement intClass; | 1172 LibraryElement typedDataLibrary; |
| 1309 ClassElement doubleClass; | |
| 1310 ClassElement stringClass; | |
| 1311 ClassElement functionClass; | |
| 1312 ClassElement nullClass; | |
| 1313 ClassElement listClass; | |
| 1314 ClassElement typeClass; | |
| 1315 ClassElement mapClass; | |
| 1316 ClassElement symbolClass; | |
| 1317 ClassElement stackTraceClass; | |
| 1318 ClassElement futureClass; | |
| 1319 ClassElement iterableClass; | |
| 1320 ClassElement streamClass; | |
| 1321 ClassElement resourceClass; | |
| 1322 | 1173 |
| 1323 _CompilerCoreTypes(this.resolution); | 1174 // TODO(sigmund): possibly move this to target-specific collection of |
| 1175 // elements, or refactor the library so that the helpers we need are in a |
| 1176 // target-agnostic place. Currently we are using @patch and @Native from |
| 1177 // here. We hope we can make those independent of the backend and generic |
| 1178 // enough so the patching algorithm can work without being configured for a |
| 1179 // specific backend. |
| 1180 LibraryElement jsHelperLibrary; |
| 1181 |
| 1182 _CompilerCoreTypes(this.resolution, this.reporter); |
| 1183 |
| 1184 // From dart:core |
| 1185 |
| 1186 ClassElement _objectClass; |
| 1187 ClassElement get objectClass => |
| 1188 _objectClass ??= _findRequired(coreLibrary, 'Object'); |
| 1189 |
| 1190 ClassElement _boolClass; |
| 1191 ClassElement get boolClass => |
| 1192 _boolClass ??= _findRequired(coreLibrary, 'bool'); |
| 1193 |
| 1194 ClassElement _numClass; |
| 1195 ClassElement get numClass => _numClass ??= _findRequired(coreLibrary, 'num'); |
| 1196 |
| 1197 ClassElement _intClass; |
| 1198 ClassElement get intClass => _intClass ??= _findRequired(coreLibrary, 'int'); |
| 1199 |
| 1200 ClassElement _doubleClass; |
| 1201 ClassElement get doubleClass => |
| 1202 _doubleClass ??= _findRequired(coreLibrary, 'double'); |
| 1203 |
| 1204 ClassElement _stringClass; |
| 1205 ClassElement get stringClass => |
| 1206 _stringClass ??= _findRequired(coreLibrary, 'String'); |
| 1207 |
| 1208 ClassElement _functionClass; |
| 1209 ClassElement get functionClass => |
| 1210 _functionClass ??= _findRequired(coreLibrary, 'Function'); |
| 1211 |
| 1212 Element _functionApplyMethod; |
| 1213 Element get functionApplyMethod { |
| 1214 if (_functionApplyMethod == null) { |
| 1215 functionClass.ensureResolved(resolution); |
| 1216 _functionApplyMethod = functionClass.lookupLocalMember('apply'); |
| 1217 assert(invariant(functionClass, _functionApplyMethod != null, |
| 1218 message: "Member `apply` not found in ${functionClass}.")); |
| 1219 } |
| 1220 return _functionApplyMethod; |
| 1221 } |
| 1222 |
| 1223 bool isFunctionApplyMethod(Element element) => |
| 1224 element.name == 'apply' && element.enclosingClass == functionClass; |
| 1225 |
| 1226 ClassElement _nullClass; |
| 1227 ClassElement get nullClass => |
| 1228 _nullClass ??= _findRequired(coreLibrary, 'Null'); |
| 1229 |
| 1230 ClassElement _listClass; |
| 1231 ClassElement get listClass => |
| 1232 _listClass ??= _findRequired(coreLibrary, 'List'); |
| 1233 |
| 1234 ClassElement _typeClass; |
| 1235 ClassElement get typeClass => |
| 1236 _typeClass ??= _findRequired(coreLibrary, 'Type'); |
| 1237 |
| 1238 ClassElement _mapClass; |
| 1239 ClassElement get mapClass => _mapClass ??= _findRequired(coreLibrary, 'Map'); |
| 1240 |
| 1241 ClassElement _symbolClass; |
| 1242 ClassElement get symbolClass => |
| 1243 _symbolClass ??= _findRequired(coreLibrary, 'Symbol'); |
| 1244 |
| 1245 ConstructorElement _symbolConstructor; |
| 1246 ConstructorElement get symbolConstructor { |
| 1247 if (_symbolConstructor == null) { |
| 1248 symbolClass.ensureResolved(resolution); |
| 1249 _symbolConstructor = symbolClass.lookupConstructor(''); |
| 1250 assert(invariant(symbolClass, _symbolConstructor != null, |
| 1251 message: "Default constructor not found ${symbolClass}.")); |
| 1252 } |
| 1253 return _symbolConstructor; |
| 1254 } |
| 1255 |
| 1256 bool isSymbolConstructor(Element e) => |
| 1257 e.enclosingClass == symbolClass && e == symbolConstructor; |
| 1258 |
| 1259 ClassElement _stackTraceClass; |
| 1260 ClassElement get stackTraceClass => |
| 1261 _stackTraceClass ??= _findRequired(coreLibrary, 'StackTrace'); |
| 1262 |
| 1263 ClassElement _iterableClass; |
| 1264 ClassElement get iterableClass => |
| 1265 _iterableClass ??= _findRequired(coreLibrary, 'Iterable'); |
| 1266 |
| 1267 ClassElement _resourceClass; |
| 1268 ClassElement get resourceClass => |
| 1269 _resourceClass ??= _findRequired(coreLibrary, 'Resource'); |
| 1270 |
| 1271 Element _identicalFunction; |
| 1272 Element get identicalFunction => |
| 1273 _identicalFunction ??= coreLibrary.find('identical'); |
| 1274 |
| 1275 // From dart:async |
| 1276 |
| 1277 ClassElement _futureClass; |
| 1278 ClassElement get futureClass => |
| 1279 _futureClass ??= _findRequired(asyncLibrary, 'Future'); |
| 1280 |
| 1281 ClassElement _streamClass; |
| 1282 ClassElement get streamClass => |
| 1283 _streamClass ??= _findRequired(asyncLibrary, 'Stream'); |
| 1284 |
| 1285 ClassElement _deferredLibraryClass; |
| 1286 ClassElement get deferredLibraryClass => |
| 1287 _deferredLibraryClass ??= _findRequired(asyncLibrary, "DeferredLibrary"); |
| 1288 |
| 1289 // From dart:mirrors |
| 1290 |
| 1291 ClassElement _mirrorSystemClass; |
| 1292 ClassElement get mirrorSystemClass => |
| 1293 _mirrorSystemClass ??= _findRequired(mirrorsLibrary, 'MirrorSystem'); |
| 1294 |
| 1295 FunctionElement _mirrorSystemGetNameFunction; |
| 1296 bool isMirrorSystemGetNameFunction(Element element) { |
| 1297 if (_mirrorSystemGetNameFunction == null) { |
| 1298 if (!element.isFunction || mirrorsLibrary == null) return false; |
| 1299 ClassElement cls = mirrorSystemClass; |
| 1300 if (element.enclosingClass != cls) return false; |
| 1301 if (cls != null) { |
| 1302 cls.ensureResolved(resolution); |
| 1303 _mirrorSystemGetNameFunction = cls.lookupLocalMember('getName'); |
| 1304 } |
| 1305 } |
| 1306 return element == _mirrorSystemGetNameFunction; |
| 1307 } |
| 1308 |
| 1309 ClassElement _mirrorsUsedClass; |
| 1310 ClassElement get mirrorsUsedClass => |
| 1311 _mirrorsUsedClass ??= _findRequired(mirrorsLibrary, 'MirrorsUsed'); |
| 1312 |
| 1313 bool isMirrorsUsedConstructor(ConstructorElement element) => |
| 1314 mirrorsLibrary != null && mirrorsUsedClass == element.enclosingClass; |
| 1315 |
| 1316 ConstructorElement _mirrorsUsedConstructor; |
| 1317 @override |
| 1318 ConstructorElement get mirrorsUsedConstructor { |
| 1319 if (_mirrorsUsedConstructor == null) { |
| 1320 ClassElement cls = mirrorsUsedClass; |
| 1321 if (cls != null) { |
| 1322 cls.ensureResolved(resolution); |
| 1323 _mirrorsUsedConstructor = cls.constructors.head; |
| 1324 } |
| 1325 } |
| 1326 return _mirrorsUsedConstructor; |
| 1327 } |
| 1328 |
| 1329 // From dart:typed_data |
| 1330 |
| 1331 ClassElement _typedDataClass; |
| 1332 ClassElement get typedDataClass => |
| 1333 _typedDataClass ??= _findRequired(typedDataLibrary, 'NativeTypedData'); |
| 1334 |
| 1335 // From dart:_js_helper |
| 1336 // TODO(sigmund,johnniwinther): refactor needed: either these move to a |
| 1337 // backend-specific collection of helpers, or the helper code moves to a |
| 1338 // backend agnostic library (see commend above on [jsHelperLibrary]. |
| 1339 |
| 1340 ClassElement _patchAnnotationClass; |
| 1341 ClassElement get patchAnnotationClass => |
| 1342 _patchAnnotationClass ??= _findRequired(jsHelperLibrary, '_Patch'); |
| 1343 |
| 1344 ClassElement _nativeAnnotationClass; |
| 1345 ClassElement get nativeAnnotationClass => |
| 1346 _nativeAnnotationClass ??= _findRequired(jsHelperLibrary, 'Native'); |
| 1324 | 1347 |
| 1325 @override | 1348 @override |
| 1326 InterfaceType get objectType { | 1349 InterfaceType get objectType { |
| 1327 objectClass.ensureResolved(resolution); | 1350 objectClass.ensureResolved(resolution); |
| 1328 return objectClass.rawType; | 1351 return objectClass.rawType; |
| 1329 } | 1352 } |
| 1330 | 1353 |
| 1331 @override | 1354 @override |
| 1332 InterfaceType get boolType { | 1355 InterfaceType get boolType { |
| 1333 boolClass.ensureResolved(resolution); | 1356 boolClass.ensureResolved(resolution); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1440 | 1463 |
| 1441 @override | 1464 @override |
| 1442 InterfaceType streamType([DartType elementType]) { | 1465 InterfaceType streamType([DartType elementType]) { |
| 1443 streamClass.ensureResolved(resolution); | 1466 streamClass.ensureResolved(resolution); |
| 1444 InterfaceType type = streamClass.rawType; | 1467 InterfaceType type = streamClass.rawType; |
| 1445 if (elementType == null) { | 1468 if (elementType == null) { |
| 1446 return type; | 1469 return type; |
| 1447 } | 1470 } |
| 1448 return type.createInstantiation([elementType]); | 1471 return type.createInstantiation([elementType]); |
| 1449 } | 1472 } |
| 1473 |
| 1474 void onLibraryCreated(LibraryElement library) { |
| 1475 Uri uri = library.canonicalUri; |
| 1476 if (uri == Uris.dart_core) { |
| 1477 coreLibrary = library; |
| 1478 } else if (uri == Uris.dart_async) { |
| 1479 asyncLibrary = library; |
| 1480 } else if (uri == Uris.dart__native_typed_data) { |
| 1481 typedDataLibrary = library; |
| 1482 } else if (uri == Uris.dart_mirrors) { |
| 1483 mirrorsLibrary = library; |
| 1484 } else if (uri == js_backend.BackendHelpers.DART_JS_HELPER) { |
| 1485 jsHelperLibrary = library; |
| 1486 } |
| 1487 } |
| 1488 |
| 1489 Element _findRequired(LibraryElement library, String name) { |
| 1490 // If the script of the library is synthesized, the library does not exist |
| 1491 // and we do not try to load the helpers. |
| 1492 // |
| 1493 // This could for example happen if dart:async is disabled, then loading it |
| 1494 // should not try to find the given element. |
| 1495 if (library == null || library.isSynthesized) return null; |
| 1496 |
| 1497 var element = library.find(name); |
| 1498 if (element == null) { |
| 1499 reporter.internalError( |
| 1500 library, |
| 1501 "The library '${library.canonicalUri}' does not contain required " |
| 1502 "element: '$name'."); |
| 1503 } |
| 1504 return element; |
| 1505 } |
| 1450 } | 1506 } |
| 1451 | 1507 |
| 1452 class CompilerDiagnosticReporter extends DiagnosticReporter { | 1508 class CompilerDiagnosticReporter extends DiagnosticReporter { |
| 1453 final Compiler compiler; | 1509 final Compiler compiler; |
| 1454 final DiagnosticOptions options; | 1510 final DiagnosticOptions options; |
| 1455 | 1511 |
| 1456 Element _currentElement; | 1512 Element _currentElement; |
| 1457 bool hasCrashed = false; | 1513 bool hasCrashed = false; |
| 1458 | 1514 |
| 1459 /// `true` if the last diagnostic was filtered, in which case the | 1515 /// `true` if the last diagnostic was filtered, in which case the |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1886 @override | 1942 @override |
| 1887 ParsingContext get parsingContext => compiler.parsingContext; | 1943 ParsingContext get parsingContext => compiler.parsingContext; |
| 1888 | 1944 |
| 1889 @override | 1945 @override |
| 1890 CoreClasses get coreClasses => compiler.coreClasses; | 1946 CoreClasses get coreClasses => compiler.coreClasses; |
| 1891 | 1947 |
| 1892 @override | 1948 @override |
| 1893 CoreTypes get coreTypes => compiler.coreTypes; | 1949 CoreTypes get coreTypes => compiler.coreTypes; |
| 1894 | 1950 |
| 1895 @override | 1951 @override |
| 1952 CommonElements get commonElements => compiler.commonElements; |
| 1953 |
| 1954 @override |
| 1896 Types get types => compiler.types; | 1955 Types get types => compiler.types; |
| 1897 | 1956 |
| 1898 @override | 1957 @override |
| 1899 Target get target => compiler.backend; | 1958 Target get target => compiler.backend; |
| 1900 | 1959 |
| 1901 @override | 1960 @override |
| 1902 ResolverTask get resolver => compiler.resolver; | 1961 ResolverTask get resolver => compiler.resolver; |
| 1903 | 1962 |
| 1904 @override | 1963 @override |
| 1905 ResolutionEnqueuer get enqueuer => compiler.enqueuer.resolution; | 1964 ResolutionEnqueuer get enqueuer => compiler.enqueuer.resolution; |
| 1906 | 1965 |
| 1907 @override | 1966 @override |
| 1908 CompilerOptions get options => compiler.options; | 1967 CompilerOptions get options => compiler.options; |
| 1909 | 1968 |
| 1910 @override | 1969 @override |
| 1911 IdGenerator get idGenerator => compiler.idGenerator; | 1970 IdGenerator get idGenerator => compiler.idGenerator; |
| 1912 | 1971 |
| 1913 @override | 1972 @override |
| 1914 ConstantEnvironment get constants => compiler.constants; | 1973 ConstantEnvironment get constants => compiler.constants; |
| 1915 | 1974 |
| 1916 @override | 1975 @override |
| 1917 MirrorUsageAnalyzerTask get mirrorUsageAnalyzerTask => | 1976 MirrorUsageAnalyzerTask get mirrorUsageAnalyzerTask => |
| 1918 compiler.mirrorUsageAnalyzerTask; | 1977 compiler.mirrorUsageAnalyzerTask; |
| 1919 | 1978 |
| 1920 @override | 1979 @override |
| 1921 LibraryElement get coreLibrary => compiler.coreLibrary; | 1980 LibraryElement get coreLibrary => compiler._coreTypes.coreLibrary; |
| 1922 | 1981 |
| 1923 @override | 1982 @override |
| 1924 FunctionElement get identicalFunction => compiler.identicalFunction; | 1983 bool get wasProxyConstantComputedTestingOnly => _proxyConstant != null; |
| 1925 | |
| 1926 @override | |
| 1927 ClassElement get mirrorSystemClass => compiler.mirrorSystemClass; | |
| 1928 | |
| 1929 @override | |
| 1930 FunctionElement get mirrorSystemGetNameFunction => | |
| 1931 compiler.mirrorSystemGetNameFunction; | |
| 1932 | |
| 1933 @override | |
| 1934 ConstructorElement get mirrorsUsedConstructor => | |
| 1935 compiler.mirrorsUsedConstructor; | |
| 1936 | |
| 1937 @override | |
| 1938 ConstructorElement get symbolConstructor => compiler.symbolConstructor; | |
| 1939 | |
| 1940 @override | |
| 1941 ConstantValue proxyConstant; | |
| 1942 | 1984 |
| 1943 @override | 1985 @override |
| 1944 void registerClass(ClassElement cls) { | 1986 void registerClass(ClassElement cls) { |
| 1945 compiler.world.registerClass(cls); | 1987 compiler.world.registerClass(cls); |
| 1946 } | 1988 } |
| 1947 | 1989 |
| 1948 @override | 1990 @override |
| 1949 void resolveClass(ClassElement cls) { | 1991 void resolveClass(ClassElement cls) { |
| 1950 compiler.resolver.resolveClass(cls); | 1992 compiler.resolver.resolveClass(cls); |
| 1951 } | 1993 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1972 | 2014 |
| 1973 @override | 2015 @override |
| 1974 void ensureResolved(Element element) { | 2016 void ensureResolved(Element element) { |
| 1975 if (compiler.serialization.isDeserialized(element)) { | 2017 if (compiler.serialization.isDeserialized(element)) { |
| 1976 return; | 2018 return; |
| 1977 } | 2019 } |
| 1978 computeWorldImpact(element); | 2020 computeWorldImpact(element); |
| 1979 } | 2021 } |
| 1980 | 2022 |
| 1981 @override | 2023 @override |
| 1982 void onClassResolved(ClassElement element) => | |
| 1983 compiler.onClassResolved(element); | |
| 1984 | |
| 1985 @override | |
| 1986 void registerCompileTimeError(Element element, DiagnosticMessage message) => | 2024 void registerCompileTimeError(Element element, DiagnosticMessage message) => |
| 1987 compiler.registerCompileTimeError(element, message); | 2025 compiler.registerCompileTimeError(element, message); |
| 1988 | 2026 |
| 1989 @override | 2027 @override |
| 1990 bool hasResolvedAst(ExecutableElement element) { | 2028 bool hasResolvedAst(ExecutableElement element) { |
| 1991 assert(invariant(element, element.isDeclaration, | 2029 assert(invariant(element, element.isDeclaration, |
| 1992 message: "Element $element must be the declaration.")); | 2030 message: "Element $element must be the declaration.")); |
| 1993 if (compiler.serialization.isDeserialized(element)) { | 2031 if (compiler.serialization.isDeserialized(element)) { |
| 1994 return compiler.serialization.hasResolvedAst(element); | 2032 return compiler.serialization.hasResolvedAst(element); |
| 1995 } | 2033 } |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2124 return new ResolutionWorkItem(element, compilationContext); | 2162 return new ResolutionWorkItem(element, compilationContext); |
| 2125 } | 2163 } |
| 2126 } | 2164 } |
| 2127 | 2165 |
| 2128 @override | 2166 @override |
| 2129 void forgetElement(Element element) { | 2167 void forgetElement(Element element) { |
| 2130 _worldImpactCache.remove(element); | 2168 _worldImpactCache.remove(element); |
| 2131 _resolutionImpactCache.remove(element); | 2169 _resolutionImpactCache.remove(element); |
| 2132 } | 2170 } |
| 2133 | 2171 |
| 2172 ConstantValue _proxyConstant; |
| 2173 |
| 2134 @override | 2174 @override |
| 2135 bool isProxyConstant(ConstantValue value) { | 2175 bool isProxyConstant(ConstantValue value) { |
| 2136 FieldElement field = coreLibrary.find('proxy'); | 2176 FieldElement field = coreLibrary.find('proxy'); |
| 2137 if (field == null) return false; | 2177 if (field == null) return false; |
| 2138 if (!hasBeenResolved(field)) return false; | 2178 if (!hasBeenResolved(field)) return false; |
| 2139 if (proxyConstant == null) { | 2179 if (_proxyConstant == null) { |
| 2140 proxyConstant = constants | 2180 _proxyConstant = constants |
| 2141 .getConstantValue(resolver.constantCompiler.compileConstant(field)); | 2181 .getConstantValue(resolver.constantCompiler.compileConstant(field)); |
| 2142 } | 2182 } |
| 2143 return proxyConstant == value; | 2183 return _proxyConstant == value; |
| 2144 } | 2184 } |
| 2145 } | 2185 } |
| 2146 | 2186 |
| 2147 class GlobalDependencyRegistry extends EagerRegistry { | 2187 class GlobalDependencyRegistry extends EagerRegistry { |
| 2148 final Compiler compiler; | 2188 final Compiler compiler; |
| 2149 Setlet<Element> _otherDependencies; | 2189 Setlet<Element> _otherDependencies; |
| 2150 | 2190 |
| 2151 GlobalDependencyRegistry(this.compiler) : super('GlobalDependencies', null); | 2191 GlobalDependencyRegistry(this.compiler) : super('GlobalDependencies', null); |
| 2152 | 2192 |
| 2153 // TODO(johnniwinther): Rename world/universe/enqueuer through out the | 2193 // TODO(johnniwinther): Rename world/universe/enqueuer through out the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 2176 compiler.readScript(uri, spannable); | 2216 compiler.readScript(uri, spannable); |
| 2177 } | 2217 } |
| 2178 | 2218 |
| 2179 /// [ScriptLoader] used to ensure that scripts are not loaded accidentally | 2219 /// [ScriptLoader] used to ensure that scripts are not loaded accidentally |
| 2180 /// through the [LibraryLoader] when `CompilerOptions.compileOnly` is `true`. | 2220 /// through the [LibraryLoader] when `CompilerOptions.compileOnly` is `true`. |
| 2181 class _NoScriptLoader implements ScriptLoader { | 2221 class _NoScriptLoader implements ScriptLoader { |
| 2182 Compiler compiler; | 2222 Compiler compiler; |
| 2183 _NoScriptLoader(this.compiler); | 2223 _NoScriptLoader(this.compiler); |
| 2184 | 2224 |
| 2185 Future<Script> readScript(Uri uri, [Spannable spannable]) { | 2225 Future<Script> readScript(Uri uri, [Spannable spannable]) { |
| 2186 compiler.reporter.internalError(spannable, | 2226 compiler.reporter |
| 2187 "Script loading of '$uri' is not enabled."); | 2227 .internalError(spannable, "Script loading of '$uri' is not enabled."); |
| 2188 } | 2228 } |
| 2189 } | 2229 } |
| 2190 | 2230 |
| 2191 class _ElementScanner implements ElementScanner { | 2231 class _ElementScanner implements ElementScanner { |
| 2192 ScannerTask scanner; | 2232 ScannerTask scanner; |
| 2193 _ElementScanner(this.scanner); | 2233 _ElementScanner(this.scanner); |
| 2194 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library); | 2234 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library); |
| 2195 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit); | 2235 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit); |
| 2196 } | 2236 } |
| 2197 | 2237 |
| 2198 class _EmptyEnvironment implements Environment { | 2238 class _EmptyEnvironment implements Environment { |
| 2199 const _EmptyEnvironment(); | 2239 const _EmptyEnvironment(); |
| 2200 | 2240 |
| 2201 String valueOf(String key) => null; | 2241 String valueOf(String key) => null; |
| 2202 } | 2242 } |
| OLD | NEW |