OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Additions to Fasta for generating .dill (Kernel IR) files with dart2js patch |
| 6 /// files and native hooks. |
| 7 library compiler.src.kernel.fasta_support; |
| 8 |
| 9 // TODO(sigmund): get rid of this file. Fasta should be agnostic of the |
| 10 // target platform, at which point this should not be necessary. In particular, |
| 11 // we need to: |
| 12 // - add a fasta flag to configure the platform library location. |
| 13 // - add a fasta flag to specify which sdk libraries should be built-in |
| 14 // (that would replace `loadExtraRequiredLibraries`). |
| 15 // - add flags to fasta to turn on various transformations. |
| 16 // - get rid of `native` in dart2js patches or unify the syntax with the VM. |
| 17 |
| 18 import 'dart:async' show Future; |
| 19 import 'dart:io' show exitCode; |
| 20 |
| 21 import 'package:kernel/ast.dart' show Source; |
| 22 |
| 23 import 'package:front_end/src/fasta/compiler_context.dart' show CompilerContext; |
| 24 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
| 25 import 'package:front_end/src/fasta/fasta.dart' show CompileTask; |
| 26 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
| 27 show KernelTarget; |
| 28 import 'package:front_end/src/fasta/loader.dart' show Loader; |
| 29 import 'package:front_end/src/fasta/parser/parser.dart' show optional; |
| 30 import 'package:front_end/src/fasta/scanner/token.dart' show Token; |
| 31 import 'package:front_end/src/fasta/ticker.dart' show Ticker; |
| 32 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; |
| 33 |
| 34 /// Generates a platform.dill file containing the compiled Kernel IR of the |
| 35 /// dart2js SDK. |
| 36 Future compilePlatform(Uri patchedSdk, Uri output, {Uri packages}) async { |
| 37 Uri deps = Uri.base.resolveUri(new Uri.file("${output.toFilePath()}.d")); |
| 38 TranslateUri uriTranslator = await TranslateUri.parse(patchedSdk, packages); |
| 39 var ticker = new Ticker(isVerbose: false); |
| 40 var dillTarget = new DillTargetForDart2js(ticker, uriTranslator); |
| 41 var kernelTarget = |
| 42 new KernelTargetForDart2js(dillTarget, uriTranslator, false); |
| 43 |
| 44 kernelTarget.read(Uri.parse("dart:core")); |
| 45 await dillTarget.writeOutline(null); |
| 46 await kernelTarget.writeOutline(output); |
| 47 |
| 48 if (exitCode != 0) return null; |
| 49 await kernelTarget.writeProgram(output); |
| 50 await kernelTarget.writeDepsFile(output, deps); |
| 51 } |
| 52 |
| 53 /// Extends the internal fasta [CompileTask] to use a dart2js-aware [DillTarget] |
| 54 /// and [KernelTarget]. |
| 55 class Dart2jsCompileTask extends CompileTask { |
| 56 Dart2jsCompileTask(CompilerContext c, Ticker ticker) : super(c, ticker); |
| 57 |
| 58 @override |
| 59 DillTarget createDillTarget(TranslateUri uriTranslator) { |
| 60 return new DillTargetForDart2js(ticker, uriTranslator); |
| 61 } |
| 62 |
| 63 @override |
| 64 KernelTarget createKernelTarget( |
| 65 DillTarget dillTarget, TranslateUri uriTranslator, bool strongMode) { |
| 66 return new KernelTargetForDart2js( |
| 67 dillTarget, uriTranslator, strongMode, c.uriToSource); |
| 68 } |
| 69 } |
| 70 |
| 71 /// Specializes [KernelTarget] to build kernel for dart2js: no transformations |
| 72 /// are run, JS-specific libraries are included in the SDK, and native clauses |
| 73 /// have no string parameter. |
| 74 class KernelTargetForDart2js extends KernelTarget { |
| 75 KernelTargetForDart2js( |
| 76 DillTarget target, TranslateUri uriTranslator, bool strongMode, |
| 77 [Map<String, Source> uriToSource]) |
| 78 : super(target, uriTranslator, strongMode, uriToSource); |
| 79 |
| 80 @override |
| 81 Token skipNativeClause(Token token) => _skipNative(token); |
| 82 |
| 83 @override |
| 84 String extractNativeMethodName(Token token) => null; |
| 85 |
| 86 @override |
| 87 void loadExtraRequiredLibraries(Loader loader) => _loadExtras(loader); |
| 88 |
| 89 @override |
| 90 void runBuildTransformations() {} |
| 91 |
| 92 @override |
| 93 void runLinkTransformations(_) {} |
| 94 } |
| 95 |
| 96 /// Specializes [DillTarget] to build kernel for dart2js: JS-specific libraries |
| 97 /// are included in the SDK, and native clauses have no string parameter. |
| 98 class DillTargetForDart2js extends DillTarget { |
| 99 DillTargetForDart2js(Ticker ticker, TranslateUri uriTranslator) |
| 100 : super(ticker, uriTranslator); |
| 101 |
| 102 @override |
| 103 Token skipNativeClause(Token token) => _skipNative(token); |
| 104 |
| 105 @override |
| 106 String extractNativeMethodName(Token token) => null; |
| 107 |
| 108 @override |
| 109 void loadExtraRequiredLibraries(Loader loader) => _loadExtras(loader); |
| 110 } |
| 111 |
| 112 /// We use native clauses of this form in our dart2js patch files: |
| 113 /// |
| 114 /// methodDeclaration() native; |
| 115 /// |
| 116 /// The default front_end parser doesn't support this, so it will trigger an |
| 117 /// error recovery condition. This function is used while parsing to detect this |
| 118 /// form and continue parsing. |
| 119 /// |
| 120 /// Note that `native` isn't part of the Dart Language Specification, and the VM |
| 121 /// uses it a slightly different form. We hope to remove this syntax in our |
| 122 /// dart2js patch files and replace it with the external modifier. |
| 123 Token _skipNative(Token token) { |
| 124 if (!optional("native", token)) return null; |
| 125 if (!optional(";", token.next)) return null; |
| 126 return token; |
| 127 } |
| 128 |
| 129 void _loadExtras(Loader loader) { |
| 130 for (String uri in _extraDart2jsLibraries) { |
| 131 loader.read(Uri.parse(uri)); |
| 132 } |
| 133 } |
| 134 |
| 135 const _extraDart2jsLibraries = const <String>[ |
| 136 'dart:async', |
| 137 'dart:collection', |
| 138 'dart:mirrors', |
| 139 'dart:_native_typed_data', |
| 140 'dart:_internal', |
| 141 'dart:_js_helper', |
| 142 'dart:_interceptors', |
| 143 'dart:_foreign_helper', |
| 144 'dart:_js_mirrors', |
| 145 'dart:_js_names', |
| 146 'dart:_js_embedded_names', |
| 147 'dart:_isolate_helper', |
| 148 ]; |
OLD | NEW |