| 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; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 LibraryElement typedDataLibrary; | 159 LibraryElement typedDataLibrary; |
| 160 | 160 |
| 161 DiagnosticReporter get reporter => _reporter; | 161 DiagnosticReporter get reporter => _reporter; |
| 162 CoreClasses get coreClasses => _coreTypes; | 162 CoreClasses get coreClasses => _coreTypes; |
| 163 CoreTypes get coreTypes => _coreTypes; | 163 CoreTypes get coreTypes => _coreTypes; |
| 164 Resolution get resolution => _resolution; | 164 Resolution get resolution => _resolution; |
| 165 ParsingContext get parsingContext => _parsingContext; | 165 ParsingContext get parsingContext => _parsingContext; |
| 166 | 166 |
| 167 ClassElement typedDataClass; | 167 ClassElement typedDataClass; |
| 168 | 168 |
| 169 /// The constant for the [proxy] variable defined in dart:core. | |
| 170 ConstantValue proxyConstant; | |
| 171 | |
| 172 // TODO(johnniwinther): Move this to the JavaScriptBackend. | 169 // TODO(johnniwinther): Move this to the JavaScriptBackend. |
| 173 /// The class for patch annotation defined in dart:_js_helper. | 170 /// The class for patch annotation defined in dart:_js_helper. |
| 174 ClassElement patchAnnotationClass; | 171 ClassElement patchAnnotationClass; |
| 175 | 172 |
| 176 // TODO(johnniwinther): Move this to the JavaScriptBackend. | 173 // TODO(johnniwinther): Move this to the JavaScriptBackend. |
| 177 ClassElement nativeAnnotationClass; | 174 ClassElement nativeAnnotationClass; |
| 178 | 175 |
| 179 ConstructorElement _symbolConstructor; | 176 ConstructorElement _symbolConstructor; |
| 180 ConstructorElement get symbolConstructor { | 177 ConstructorElement get symbolConstructor { |
| 181 if (_symbolConstructor == null) { | 178 if (_symbolConstructor == null) { |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 /// | 363 /// |
| 367 /// Override this to mock the scanner for testing. | 364 /// Override this to mock the scanner for testing. |
| 368 ScannerTask createScannerTask() => | 365 ScannerTask createScannerTask() => |
| 369 new ScannerTask(dietParser, reporter, measurer, | 366 new ScannerTask(dietParser, reporter, measurer, |
| 370 preserveComments: options.preserveComments, commentMap: commentMap); | 367 preserveComments: options.preserveComments, commentMap: commentMap); |
| 371 | 368 |
| 372 /// Creates the resolver task. | 369 /// Creates the resolver task. |
| 373 /// | 370 /// |
| 374 /// Override this to mock the resolver for testing. | 371 /// Override this to mock the resolver for testing. |
| 375 ResolverTask createResolverTask() { | 372 ResolverTask createResolverTask() { |
| 376 return new ResolverTask(this, backend.constantCompilerTask); | 373 return new ResolverTask( |
| 374 resolution, backend.constantCompilerTask, world, measurer); |
| 377 } | 375 } |
| 378 | 376 |
| 379 Universe get resolverWorld => enqueuer.resolution.universe; | 377 Universe get resolverWorld => enqueuer.resolution.universe; |
| 380 Universe get codegenWorld => enqueuer.codegen.universe; | 378 Universe get codegenWorld => enqueuer.codegen.universe; |
| 381 | 379 |
| 382 bool get analyzeAll => options.analyzeAll || compileAll; | 380 bool get analyzeAll => options.analyzeAll || compileAll; |
| 383 | 381 |
| 384 bool get compileAll => false; | 382 bool get compileAll => false; |
| 385 | 383 |
| 386 bool get disableTypeInference => | 384 bool get disableTypeInference => |
| 387 options.disableTypeInference || compilationFailed; | 385 options.disableTypeInference || compilationFailed; |
| 388 | 386 |
| 387 // TODO(het): remove this from here. Either inline at all use sites or add it |
| 388 // to Reporter. |
| 389 void unimplemented(Spannable spannable, String methodName) { | 389 void unimplemented(Spannable spannable, String methodName) { |
| 390 reporter.internalError(spannable, "$methodName not implemented."); | 390 reporter.internalError(spannable, "$methodName not implemented."); |
| 391 } | 391 } |
| 392 | 392 |
| 393 // Compiles the dart script at [uri]. | 393 // Compiles the dart script at [uri]. |
| 394 // | 394 // |
| 395 // The resulting future will complete with true if the compilation | 395 // The resulting future will complete with true if the compilation |
| 396 // succeeded. | 396 // succeeded. |
| 397 Future<bool> run(Uri uri) => selfTask.measureSubtask("Compiler.run", () { | 397 Future<bool> run(Uri uri) => selfTask.measureSubtask("Compiler.run", () { |
| 398 measurer.startWallClock(); | 398 measurer.startWallClock(); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 if (options.preserveComments) { | 574 if (options.preserveComments) { |
| 575 return libraryLoader | 575 return libraryLoader |
| 576 .loadLibrary(Uris.dart_mirrors) | 576 .loadLibrary(Uris.dart_mirrors) |
| 577 .then((LibraryElement libraryElement) { | 577 .then((LibraryElement libraryElement) { |
| 578 documentClass = libraryElement.find('Comment'); | 578 documentClass = libraryElement.find('Comment'); |
| 579 }); | 579 }); |
| 580 } | 580 } |
| 581 }).then((_) => backend.onLibrariesLoaded(loadedLibraries)); | 581 }).then((_) => backend.onLibrariesLoaded(loadedLibraries)); |
| 582 } | 582 } |
| 583 | 583 |
| 584 bool isProxyConstant(ConstantValue value) { | |
| 585 FieldElement field = coreLibrary.find('proxy'); | |
| 586 if (field == null) return false; | |
| 587 if (!resolution.hasBeenResolved(field)) return false; | |
| 588 if (proxyConstant == null) { | |
| 589 proxyConstant = constants | |
| 590 .getConstantValue(resolver.constantCompiler.compileConstant(field)); | |
| 591 } | |
| 592 return proxyConstant == value; | |
| 593 } | |
| 594 | |
| 595 Element findRequiredElement(LibraryElement library, String name) { | 584 Element findRequiredElement(LibraryElement library, String name) { |
| 596 var element = library.find(name); | 585 var element = library.find(name); |
| 597 if (element == null) { | 586 if (element == null) { |
| 598 reporter.internalError( | 587 reporter.internalError( |
| 599 library, | 588 library, |
| 600 "The library '${library.canonicalUri}' does not contain required " | 589 "The library '${library.canonicalUri}' does not contain required " |
| 601 "element: '$name'."); | 590 "element: '$name'."); |
| 602 } | 591 } |
| 603 return element; | 592 return element; |
| 604 } | 593 } |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 return true; | 1111 return true; |
| 1123 } | 1112 } |
| 1124 return !BENIGN_ERRORS.contains(message.message.kind); | 1113 return !BENIGN_ERRORS.contains(message.message.kind); |
| 1125 } | 1114 } |
| 1126 | 1115 |
| 1127 void fatalDiagnosticReported(DiagnosticMessage message, | 1116 void fatalDiagnosticReported(DiagnosticMessage message, |
| 1128 List<DiagnosticMessage> infos, api.Diagnostic kind) { | 1117 List<DiagnosticMessage> infos, api.Diagnostic kind) { |
| 1129 if (markCompilationAsFailed(message, kind)) { | 1118 if (markCompilationAsFailed(message, kind)) { |
| 1130 compilationFailed = true; | 1119 compilationFailed = true; |
| 1131 } | 1120 } |
| 1132 registerCompiletimeError(currentElement, message); | 1121 registerCompileTimeError(currentElement, message); |
| 1133 } | 1122 } |
| 1134 | 1123 |
| 1135 /** | 1124 /** |
| 1136 * Reads the script specified by the [readableUri]. | 1125 * Reads the script specified by the [readableUri]. |
| 1137 * | 1126 * |
| 1138 * See [LibraryLoader] for terminology on URIs. | 1127 * See [LibraryLoader] for terminology on URIs. |
| 1139 */ | 1128 */ |
| 1140 Future<Script> readScript(Uri readableUri, [Spannable node]) { | 1129 Future<Script> readScript(Uri readableUri, [Spannable node]) { |
| 1141 unimplemented(node, 'Compiler.readScript'); | 1130 unimplemented(node, 'Compiler.readScript'); |
| 1142 return null; | 1131 return null; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1265 } | 1254 } |
| 1266 backend.forgetElement(element); | 1255 backend.forgetElement(element); |
| 1267 } | 1256 } |
| 1268 | 1257 |
| 1269 /// Returns [true] if a compile-time error has been reported for element. | 1258 /// Returns [true] if a compile-time error has been reported for element. |
| 1270 bool elementHasCompileTimeError(Element element) { | 1259 bool elementHasCompileTimeError(Element element) { |
| 1271 return elementsWithCompileTimeErrors.containsKey(element); | 1260 return elementsWithCompileTimeErrors.containsKey(element); |
| 1272 } | 1261 } |
| 1273 | 1262 |
| 1274 /// Associate [element] with a compile-time error [message]. | 1263 /// Associate [element] with a compile-time error [message]. |
| 1275 void registerCompiletimeError(Element element, DiagnosticMessage message) { | 1264 void registerCompileTimeError(Element element, DiagnosticMessage message) { |
| 1276 // The information is only needed if [generateCodeWithCompileTimeErrors]. | 1265 // The information is only needed if [generateCodeWithCompileTimeErrors]. |
| 1277 if (options.generateCodeWithCompileTimeErrors) { | 1266 if (options.generateCodeWithCompileTimeErrors) { |
| 1278 if (element == null) { | 1267 if (element == null) { |
| 1279 // Record as global error. | 1268 // Record as global error. |
| 1280 // TODO(zarah): Extend element model to represent compile-time | 1269 // TODO(zarah): Extend element model to represent compile-time |
| 1281 // errors instead of using a map. | 1270 // errors instead of using a map. |
| 1282 element = mainFunction; | 1271 element = mainFunction; |
| 1283 } | 1272 } |
| 1284 elementsWithCompileTimeErrors | 1273 elementsWithCompileTimeErrors |
| 1285 .putIfAbsent(element, () => <DiagnosticMessage>[]) | 1274 .putIfAbsent(element, () => <DiagnosticMessage>[]) |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1546 compiler.reportDiagnostic(message, infos, kind); | 1535 compiler.reportDiagnostic(message, infos, kind); |
| 1547 if (kind == api.Diagnostic.ERROR || | 1536 if (kind == api.Diagnostic.ERROR || |
| 1548 kind == api.Diagnostic.CRASH || | 1537 kind == api.Diagnostic.CRASH || |
| 1549 (options.fatalWarnings && kind == api.Diagnostic.WARNING)) { | 1538 (options.fatalWarnings && kind == api.Diagnostic.WARNING)) { |
| 1550 Element errorElement; | 1539 Element errorElement; |
| 1551 if (message.spannable is Element) { | 1540 if (message.spannable is Element) { |
| 1552 errorElement = message.spannable; | 1541 errorElement = message.spannable; |
| 1553 } else { | 1542 } else { |
| 1554 errorElement = currentElement; | 1543 errorElement = currentElement; |
| 1555 } | 1544 } |
| 1556 compiler.registerCompiletimeError(errorElement, message); | 1545 compiler.registerCompileTimeError(errorElement, message); |
| 1557 compiler.fatalDiagnosticReported(message, infos, kind); | 1546 compiler.fatalDiagnosticReported(message, infos, kind); |
| 1558 } | 1547 } |
| 1559 } | 1548 } |
| 1560 | 1549 |
| 1561 @override | 1550 @override |
| 1562 bool get hasReportedError => compiler.compilationFailed; | 1551 bool get hasReportedError => compiler.compilationFailed; |
| 1563 | 1552 |
| 1564 /** | 1553 /** |
| 1565 * Perform an operation, [f], returning the return value from [f]. If an | 1554 * Perform an operation, [f], returning the return value from [f]. If an |
| 1566 * error occurs then report it as having occurred during compilation of | 1555 * error occurs then report it as having occurred during compilation of |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1899 @override | 1888 @override |
| 1900 CoreTypes get coreTypes => compiler.coreTypes; | 1889 CoreTypes get coreTypes => compiler.coreTypes; |
| 1901 | 1890 |
| 1902 @override | 1891 @override |
| 1903 Types get types => compiler.types; | 1892 Types get types => compiler.types; |
| 1904 | 1893 |
| 1905 @override | 1894 @override |
| 1906 Target get target => compiler.backend; | 1895 Target get target => compiler.backend; |
| 1907 | 1896 |
| 1908 @override | 1897 @override |
| 1898 ResolverTask get resolver => compiler.resolver; |
| 1899 |
| 1900 @override |
| 1901 ResolutionEnqueuer get enqueuer => compiler.enqueuer.resolution; |
| 1902 |
| 1903 @override |
| 1904 CompilerOptions get options => compiler.options; |
| 1905 |
| 1906 @override |
| 1907 IdGenerator get idGenerator => compiler.idGenerator; |
| 1908 |
| 1909 @override |
| 1910 ConstantEnvironment get constants => compiler.constants; |
| 1911 |
| 1912 @override |
| 1913 MirrorUsageAnalyzerTask get mirrorUsageAnalyzerTask => |
| 1914 compiler.mirrorUsageAnalyzerTask; |
| 1915 |
| 1916 @override |
| 1917 LibraryElement get coreLibrary => compiler.coreLibrary; |
| 1918 |
| 1919 @override |
| 1920 FunctionElement get identicalFunction => compiler.identicalFunction; |
| 1921 |
| 1922 @override |
| 1923 ClassElement get mirrorSystemClass => compiler.mirrorSystemClass; |
| 1924 |
| 1925 @override |
| 1926 FunctionElement get mirrorSystemGetNameFunction => |
| 1927 compiler.mirrorSystemGetNameFunction; |
| 1928 |
| 1929 @override |
| 1930 ConstructorElement get mirrorsUsedConstructor => |
| 1931 compiler.mirrorsUsedConstructor; |
| 1932 |
| 1933 @override |
| 1934 ConstructorElement get symbolConstructor => compiler.symbolConstructor; |
| 1935 |
| 1936 @override |
| 1937 ConstantValue proxyConstant; |
| 1938 |
| 1939 @override |
| 1909 void registerClass(ClassElement cls) { | 1940 void registerClass(ClassElement cls) { |
| 1910 compiler.world.registerClass(cls); | 1941 compiler.world.registerClass(cls); |
| 1911 } | 1942 } |
| 1912 | 1943 |
| 1913 @override | 1944 @override |
| 1914 void resolveClass(ClassElement cls) { | 1945 void resolveClass(ClassElement cls) { |
| 1915 compiler.resolver.resolveClass(cls); | 1946 compiler.resolver.resolveClass(cls); |
| 1916 } | 1947 } |
| 1917 | 1948 |
| 1918 @override | 1949 @override |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1937 | 1968 |
| 1938 @override | 1969 @override |
| 1939 void ensureResolved(Element element) { | 1970 void ensureResolved(Element element) { |
| 1940 if (compiler.serialization.isDeserialized(element)) { | 1971 if (compiler.serialization.isDeserialized(element)) { |
| 1941 return; | 1972 return; |
| 1942 } | 1973 } |
| 1943 computeWorldImpact(element); | 1974 computeWorldImpact(element); |
| 1944 } | 1975 } |
| 1945 | 1976 |
| 1946 @override | 1977 @override |
| 1978 void onClassResolved(ClassElement element) => |
| 1979 compiler.onClassResolved(element); |
| 1980 |
| 1981 @override |
| 1982 void registerCompileTimeError(Element element, DiagnosticMessage message) => |
| 1983 compiler.registerCompileTimeError(element, message); |
| 1984 |
| 1985 @override |
| 1947 bool hasResolvedAst(ExecutableElement element) { | 1986 bool hasResolvedAst(ExecutableElement element) { |
| 1948 assert(invariant(element, element.isDeclaration, | 1987 assert(invariant(element, element.isDeclaration, |
| 1949 message: "Element $element must be the declaration.")); | 1988 message: "Element $element must be the declaration.")); |
| 1950 if (compiler.serialization.isDeserialized(element)) { | 1989 if (compiler.serialization.isDeserialized(element)) { |
| 1951 return compiler.serialization.hasResolvedAst(element); | 1990 return compiler.serialization.hasResolvedAst(element); |
| 1952 } | 1991 } |
| 1953 return hasBeenResolved(element.memberContext.declaration) && | 1992 return hasBeenResolved(element.memberContext.declaration) && |
| 1954 element.hasResolvedAst; | 1993 element.hasResolvedAst; |
| 1955 } | 1994 } |
| 1956 | 1995 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2077 } else { | 2116 } else { |
| 2078 return new ResolutionWorkItem(element, compilationContext); | 2117 return new ResolutionWorkItem(element, compilationContext); |
| 2079 } | 2118 } |
| 2080 } | 2119 } |
| 2081 | 2120 |
| 2082 @override | 2121 @override |
| 2083 void forgetElement(Element element) { | 2122 void forgetElement(Element element) { |
| 2084 _worldImpactCache.remove(element); | 2123 _worldImpactCache.remove(element); |
| 2085 _resolutionImpactCache.remove(element); | 2124 _resolutionImpactCache.remove(element); |
| 2086 } | 2125 } |
| 2126 |
| 2127 @override |
| 2128 bool isProxyConstant(ConstantValue value) { |
| 2129 FieldElement field = coreLibrary.find('proxy'); |
| 2130 if (field == null) return false; |
| 2131 if (!hasBeenResolved(field)) return false; |
| 2132 if (proxyConstant == null) { |
| 2133 proxyConstant = constants |
| 2134 .getConstantValue(resolver.constantCompiler.compileConstant(field)); |
| 2135 } |
| 2136 return proxyConstant == value; |
| 2137 } |
| 2087 } | 2138 } |
| 2088 | 2139 |
| 2089 class GlobalDependencyRegistry extends EagerRegistry { | 2140 class GlobalDependencyRegistry extends EagerRegistry { |
| 2090 final Compiler compiler; | 2141 final Compiler compiler; |
| 2091 Setlet<Element> _otherDependencies; | 2142 Setlet<Element> _otherDependencies; |
| 2092 | 2143 |
| 2093 GlobalDependencyRegistry(this.compiler) : super('GlobalDependencies', null); | 2144 GlobalDependencyRegistry(this.compiler) : super('GlobalDependencies', null); |
| 2094 | 2145 |
| 2095 // TODO(johnniwinther): Rename world/universe/enqueuer through out the | 2146 // TODO(johnniwinther): Rename world/universe/enqueuer through out the |
| 2096 // compiler. | 2147 // compiler. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 2123 _ElementScanner(this.scanner); | 2174 _ElementScanner(this.scanner); |
| 2124 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library); | 2175 void scanLibrary(LibraryElement library) => scanner.scanLibrary(library); |
| 2125 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit); | 2176 void scanUnit(CompilationUnitElement unit) => scanner.scan(unit); |
| 2126 } | 2177 } |
| 2127 | 2178 |
| 2128 class _EmptyEnvironment implements Environment { | 2179 class _EmptyEnvironment implements Environment { |
| 2129 const _EmptyEnvironment(); | 2180 const _EmptyEnvironment(); |
| 2130 | 2181 |
| 2131 String valueOf(String key) => null; | 2182 String valueOf(String key) => null; |
| 2132 } | 2183 } |
| OLD | NEW |