Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// New Compiler API. This API is under construction, use only internally or | 5 /// New Compiler API. This API is under construction, use only internally or |
| 6 /// in unittests. | 6 /// in unittests. |
| 7 | 7 |
| 8 library compiler_new; | 8 library compiler_new; |
| 9 | 9 |
| 10 import 'dart:async'; | 10 import 'dart:async'; |
| 11 import 'src/apiimpl.dart'; | 11 import 'src/apiimpl.dart'; |
| 12 import 'src/commandline_options.dart'; | |
| 13 import 'src/diagnostics/diagnostic_listener.dart' show DiagnosticOptions; | |
| 14 | |
| 12 import 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; | 15 import 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; |
| 13 export 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; | 16 export 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; |
| 14 | 17 |
| 15 // Unless explicitly allowed, passing `null` for any argument to the | 18 // Unless explicitly allowed, passing `null` for any argument to the |
| 16 // methods of library will result in an Error being thrown. | 19 // methods of library will result in an Error being thrown. |
| 17 | 20 |
| 18 /// Interface for providing the compiler with input. That is, Dart source files, | 21 /// Interface for providing the compiler with input. That is, Dart source files, |
| 19 /// package config files, etc. | 22 /// package config files, etc. |
| 20 abstract class CompilerInput { | 23 abstract class CompilerInput { |
| 21 /// Returns a future that completes to the source corresponding to [uri]. | 24 /// Returns a future that completes to the source corresponding to [uri]. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 /// [begin] and [end]. No other arguments may be `null`. If [uri] is not | 67 /// [begin] and [end]. No other arguments may be `null`. If [uri] is not |
| 65 /// `null`, neither are [begin] and [end]. [uri] indicates the compilation | 68 /// `null`, neither are [begin] and [end]. [uri] indicates the compilation |
| 66 /// unit from where the diagnostic originates. [begin] and [end] are | 69 /// unit from where the diagnostic originates. [begin] and [end] are |
| 67 /// zero-based character offsets from the beginning of the compilation unit. | 70 /// zero-based character offsets from the beginning of the compilation unit. |
| 68 /// [message] is the diagnostic message, and [kind] indicates indicates what | 71 /// [message] is the diagnostic message, and [kind] indicates indicates what |
| 69 /// kind of diagnostic it is. | 72 /// kind of diagnostic it is. |
| 70 /// | 73 /// |
| 71 /// Experimental: [code] gives access to an id for the messages. Currently it | 74 /// Experimental: [code] gives access to an id for the messages. Currently it |
| 72 /// is the [Message] used to create the diagnostic, if available, from which | 75 /// is the [Message] used to create the diagnostic, if available, from which |
| 73 /// the [MessageKind] is accessible. | 76 /// the [MessageKind] is accessible. |
| 74 void report(var code, | 77 void report( |
| 75 Uri uri, int begin, int end, String text, Diagnostic kind); | 78 var code, Uri uri, int begin, int end, String text, Diagnostic kind); |
| 76 } | 79 } |
| 77 | 80 |
| 78 /// Information resulting from the compilation. | 81 /// Information resulting from the compilation. |
| 79 class CompilationResult { | 82 class CompilationResult { |
| 80 /// `true` if the compilation succeeded, that is, compilation didn't fail due | 83 /// `true` if the compilation succeeded, that is, compilation didn't fail due |
| 81 /// to compile-time errors and/or internal errors. | 84 /// to compile-time errors and/or internal errors. |
| 82 final bool isSuccess; | 85 final bool isSuccess; |
| 83 | 86 |
| 84 /// The compiler object used for the compilation. | 87 /// The compiler object used for the compilation. |
| 85 /// | 88 /// |
| 86 /// Note: The type of [compiler] is implementation dependent and may vary. | 89 /// Note: The type of [compiler] is implementation dependent and may vary. |
| 87 /// Use only for debugging and testing. | 90 /// Use only for debugging and testing. |
| 88 final compiler; | 91 final compiler; |
| 89 | 92 |
| 90 CompilationResult(this.compiler, {this.isSuccess: true}); | 93 CompilationResult(this.compiler, {this.isSuccess: true}); |
| 91 } | 94 } |
| 92 | 95 |
| 93 /// Object for passing options to the compiler. | 96 /// Object for passing options to the compiler. |
| 94 class CompilerOptions { | 97 class CompilerOptions { |
| 98 /// The entry point of the application that is being compiled. | |
| 95 final Uri entryPoint; | 99 final Uri entryPoint; |
| 100 | |
| 101 /// Root location where SDK libraries are found. | |
| 96 final Uri libraryRoot; | 102 final Uri libraryRoot; |
| 103 | |
| 104 /// Package root location. | |
| 105 /// | |
| 106 /// if not null then [packageConfig] should be null. | |
|
Johnni Winther
2016/03/16 09:37:27
'if' -> 'If'
Siggi Cherem (dart-lang)
2016/03/17 22:57:13
Done.
| |
| 97 final Uri packageRoot; | 107 final Uri packageRoot; |
| 108 | |
| 109 /// Location of the package configuration file. | |
| 110 /// | |
| 111 /// if not null then [packageRoot] should be null. | |
|
Johnni Winther
2016/03/16 09:37:27
Ditto.
Siggi Cherem (dart-lang)
2016/03/17 22:57:14
Done.
| |
| 98 final Uri packageConfig; | 112 final Uri packageConfig; |
| 113 | |
| 114 // TODO(sigmund): Move out of here, maybe to CompilerInput. Options should not | |
| 115 // hold code, just configuration options. | |
| 99 final PackagesDiscoveryProvider packagesDiscoveryProvider; | 116 final PackagesDiscoveryProvider packagesDiscoveryProvider; |
| 100 final List<String> options; | 117 |
| 118 /// Resolved constant "environment" values passed to the compiler via the `-D` | |
| 119 /// flags. | |
| 101 final Map<String, dynamic> environment; | 120 final Map<String, dynamic> environment; |
| 102 | 121 |
| 122 /// Whether we allow mocking compilation of libraries such as dart:io and | |
| 123 /// dart:html for unit testing purposes. | |
| 124 final bool allowMockCompilation; | |
| 125 | |
| 126 /// Whether the native extension syntax is supported by the frontend. | |
| 127 final bool allowNativeExtensions; | |
| 128 | |
| 129 /// Whether to resolve all functions in the program, not just those reachable | |
| 130 /// from main. This implies [analyzeOnly] is true as well. | |
| 131 final bool analyzeAll; | |
| 132 | |
| 133 /// Whether to disable tree-shaking for the main script. This marks all | |
| 134 /// functions in the main script as reachable (not just a function named | |
| 135 /// `main`). | |
| 136 // TODO(sigmund): rename. The current name seems to indicate that only the | |
| 137 // main function is retained, which is the opposite of what this does. | |
|
Johnni Winther
2016/03/16 09:37:27
When used together with analyzeOnly it doesn't eve
Siggi Cherem (dart-lang)
2016/03/17 22:57:13
Acknowledged.
| |
| 138 final bool analyzeMain; | |
| 139 | |
| 140 /// Whether to run the compiler just for the purpose of analysis. That is, to | |
| 141 /// run resolution and type-checking alone, but otherwise do not generate any | |
| 142 /// code. | |
| 143 final bool analyzeOnly; | |
| 144 | |
| 145 /// Whether to skip analysis of method bodies and field initializers. Implies | |
| 146 /// [analyzeOnly]. | |
| 147 final bool analyzeSignaturesOnly; | |
| 148 | |
| 149 /// ID associated with this sdk build. | |
| 150 final String buildId; | |
| 151 | |
| 152 /// Whether there is a build-id available so we can use it on error messages | |
| 153 /// and in the emitted output of the compiler. | |
| 154 bool get hasBuildId => buildId != _UNDETERMINED_BUILD_ID; | |
| 155 | |
| 156 /// Location where to generate a map containing details of how deferred | |
| 157 /// libraries are subdivided. | |
| 158 final Uri deferredMapUri; | |
| 159 | |
| 160 /// Whether to disable inlining during the backend optimizations. | |
| 161 // TODO(sigmund): negate, so all flags are positive | |
| 162 final bool disableInlining; | |
| 163 | |
| 164 /// Several options to configure diagnostic messages. | |
| 165 // TODO(sigmund): should we simply embed those options here? | |
| 166 final DiagnosticOptions diagnosticOptions; | |
| 167 | |
| 168 /// Whether to disable global type inference. | |
| 169 final bool disableTypeInference; | |
| 170 | |
| 171 /// Whether to emit a .json file with a summary of the information used by the | |
| 172 /// compiler during optimization. This includes resolution details, | |
| 173 /// dependencies between elements, results of type inference, and the output | |
| 174 /// code for each function. | |
| 175 final bool dumpInfo; | |
| 176 | |
| 177 /// Whether we allow passing an extra argument to `assert`, containing a | |
| 178 /// reason for why an assertion fails. (experimental) | |
| 179 final bool enableAssertMessage; | |
| 180 | |
| 181 /// Whether to enable the experimental conditional directives feature. | |
| 182 final bool enableConditionalDirectives; | |
| 183 | |
| 184 /// Whether the user specified a flag to allow the use of dart:mirrors. This | |
| 185 /// silences a warning produced by the compiler. | |
| 186 final bool enableExperimentalMirrors; | |
| 187 | |
| 188 /// Whether to enable minification | |
| 189 // TODO(sigmund): rename to minify | |
| 190 final bool enableMinification; | |
| 191 | |
| 192 /// Whether to model which native classes are live based on annotations on the | |
| 193 /// core libraries. If false, all native classes will be included by default. | |
| 194 final bool enableNativeLiveTypeAnalysis; | |
| 195 | |
| 196 /// Whether to generate code containing checked-mode assignability checks. | |
| 197 final bool enableTypeAssertions; | |
| 198 | |
| 199 /// Whether to generate code containing user's `assert` statements. | |
| 200 final bool enableUserAssertions; | |
| 201 | |
| 202 /// Generate output even when there are compile-time errors. | |
|
Johnni Winther
2016/03/16 09:37:27
'Generate' -> 'Whether to generate'
Siggi Cherem (dart-lang)
2016/03/17 22:57:14
Done.
| |
| 203 final bool generateCodeWithCompileTimeErrors; | |
| 204 | |
| 205 /// Whether to generate a source-map file together with the output program. | |
| 206 final bool generateSourceMap; | |
| 207 | |
| 208 /// Whether some values are cached for reuse in incremental compilation. | |
| 209 /// Incremental compilation is allows calling `Compiler.run` more than once. | |
|
Johnni Winther
2016/03/16 09:37:27
Remove 'is '. Add '(experimental)'.
Siggi Cherem (dart-lang)
2016/03/17 22:57:13
Done.
| |
| 210 final bool hasIncrementalSupport; | |
| 211 | |
| 212 /// URI of the main output if the compiler is generating source maps. | |
| 213 final Uri outputUri; | |
| 214 | |
| 215 /// Location of the plaform configuration file. | |
|
Johnni Winther
2016/03/16 09:37:27
'plaform' -> 'platform'
Siggi Cherem (dart-lang)
2016/03/17 22:57:14
Done.
| |
| 216 final Uri platformConfigUri; | |
| 217 | |
| 218 /// Whether to emit URIs in the reflection metadata. | |
| 219 final bool preserveUris; | |
| 220 | |
| 221 /// URI where the compiler should generate the output source map file. | |
| 222 final Uri sourceMapUri; | |
| 223 | |
| 224 /// The compiler is run from the build bot. | |
| 225 final bool testMode; | |
| 226 | |
| 227 /// Whether to trust JS-interop annotations. (experimental) | |
| 228 final bool trustJSInteropTypeAnnotations; | |
| 229 | |
| 230 /// Whether to trust primitive types during inference and optimizations. | |
| 231 final bool trustPrimitives; | |
| 232 | |
| 233 /// Whether to trust type annotations during inference and optimizations. | |
| 234 final bool trustTypeAnnotations; | |
| 235 | |
| 236 /// Whether to generate code compliant with content security policy (CSP). | |
| 237 final bool useContentSecurityPolicy; | |
| 238 | |
| 239 /// Use the new CPS based backend end. This flag works for both the Dart and | |
|
Johnni Winther
2016/03/16 09:37:27
Remove mentioning of backends.
Siggi Cherem (dart-lang)
2016/03/17 22:57:13
Done.
| |
| 240 /// JavaScript backend. | |
| 241 final bool useCpsIr; | |
| 242 | |
| 243 /// When obfuscating for minification, whether to use the frequency of a name | |
| 244 /// as an heuristic to pick shorter names. | |
| 245 final bool useFrequencyNamer; | |
| 246 | |
| 247 /// Whether to use the new source-information implementation for source-maps. | |
| 248 /// (experimental) | |
| 249 final bool useNewSourceInfo; | |
| 250 | |
| 251 /// Whether the user requested to use the fast startup emitter. The full | |
| 252 /// emitter might still be used if the program uses dart:mirrors. | |
| 253 final bool useStartupEmitter; | |
| 254 | |
| 255 /// Enable verbose printing during compilation. Includes progress messages | |
| 256 /// during each phase and a time-breakdown between phases at the end. | |
| 257 final bool verbose; | |
| 258 | |
| 259 | |
| 260 // ------------------------------------------------- | |
| 261 // Options for deprecated features | |
| 262 // ------------------------------------------------- | |
| 263 // TODO(sigmund): delete these as we delete the underlying features | |
| 264 | |
| 265 /// Whether to preserve comments while scanning (only use for dart:mirrors). | |
| 266 final bool preserveComments; | |
| 267 | |
| 268 /// Whether to emit JavaScript (false enables dart2dart). | |
| 269 final bool emitJavaScript; | |
| 270 | |
| 271 /// When using dart2dart, whether to use the multi file format. | |
| 272 final bool dart2dartMultiFile; | |
| 273 | |
| 274 /// Strip option used by dart2dart. | |
| 275 final List<String> strips; | |
| 276 | |
| 277 /// Create an options object by parsing flags from [options]. | |
| 278 factory CompilerOptions.parse( | |
| 279 {Uri entryPoint, | |
| 280 Uri libraryRoot, | |
| 281 Uri packageRoot, | |
| 282 Uri packageConfig, | |
| 283 PackagesDiscoveryProvider packagesDiscoveryProvider, | |
| 284 Map<String, dynamic> environment: const <String, dynamic>{}, | |
| 285 List<String> options, | |
| 286 bool disableInlining: false}) { | |
|
Johnni Winther
2016/03/16 09:37:27
Add '--disable-inlining' to [Flags] and parse it h
Siggi Cherem (dart-lang)
2016/03/17 22:57:13
Done.
| |
| 287 return new CompilerOptions( | |
| 288 entryPoint: entryPoint, | |
| 289 libraryRoot: libraryRoot, | |
| 290 packageRoot: packageRoot, | |
| 291 packageConfig: packageConfig, | |
| 292 packagesDiscoveryProvider: packagesDiscoveryProvider, | |
| 293 environment: environment, | |
| 294 allowMockCompilation: _hasOption(options, Flags.allowMockCompilation), | |
| 295 allowNativeExtensions: _hasOption(options, Flags.allowNativeExtensions), | |
| 296 analyzeAll: _hasOption(options, Flags.analyzeAll), | |
| 297 analyzeMain: _hasOption(options, Flags.analyzeMain), | |
| 298 analyzeOnly: _hasOption(options, Flags.analyzeOnly), | |
| 299 analyzeSignaturesOnly: _hasOption(options, Flags.analyzeSignaturesOnly), | |
| 300 buildId: _extractStringOption( | |
| 301 options, '--build-id=', _UNDETERMINED_BUILD_ID), | |
| 302 dart2dartMultiFile: _hasOption(options, '--output-type=dart-multi'), | |
| 303 deferredMapUri: _extractUriOption(options, '--deferred-map='), | |
| 304 diagnosticOptions: new DiagnosticOptions( | |
| 305 suppressWarnings: _hasOption(options, Flags.suppressWarnings), | |
| 306 fatalWarnings: _hasOption(options, Flags.fatalWarnings), | |
| 307 suppressHints: _hasOption(options, Flags.suppressHints), | |
| 308 terseDiagnostics: _hasOption(options, Flags.terse), | |
| 309 shownPackageWarnings: | |
| 310 _extractOptionalCsvOption(options, Flags.showPackageWarnings)), | |
| 311 disableInlining: disableInlining, | |
| 312 disableTypeInference: _hasOption(options, Flags.disableTypeInference), | |
| 313 dumpInfo: _hasOption(options, Flags.dumpInfo), | |
| 314 emitJavaScript: !(_hasOption(options, '--output-type=dart') || | |
| 315 _hasOption(options, '--output-type=dart-multi')), | |
| 316 enableAssertMessage: _hasOption(options, Flags.enableAssertMessage), | |
| 317 enableConditionalDirectives: | |
| 318 _hasOption(options, Flags.conditionalDirectives), | |
| 319 enableExperimentalMirrors: | |
| 320 _hasOption(options, Flags.enableExperimentalMirrors), | |
| 321 enableMinification: _hasOption(options, Flags.minify), | |
| 322 enableNativeLiveTypeAnalysis: | |
| 323 !_hasOption(options, Flags.disableNativeLiveTypeAnalysis), | |
| 324 enableTypeAssertions: _hasOption(options, Flags.enableCheckedMode), | |
| 325 enableUserAssertions: _hasOption(options, Flags.enableCheckedMode), | |
| 326 generateCodeWithCompileTimeErrors: | |
| 327 _hasOption(options, Flags.generateCodeWithCompileTimeErrors), | |
| 328 generateSourceMap: !_hasOption(options, Flags.noSourceMaps), | |
| 329 hasIncrementalSupport: _forceIncrementalSupport || | |
| 330 _hasOption(options, Flags.incrementalSupport), | |
| 331 outputUri: _extractUriOption(options, '--out='), | |
| 332 platformConfigUri: _resolvePlatformConfigFromOptions( | |
| 333 libraryRoot, options), | |
| 334 preserveComments: _hasOption(options, Flags.preserveComments), | |
| 335 preserveUris: _hasOption(options, Flags.preserveUris), | |
| 336 sourceMapUri: _extractUriOption(options, '--source-map='), | |
| 337 strips: _extractCsvOption(options, '--force-strip='), | |
| 338 testMode: _hasOption(options, Flags.testMode), | |
| 339 trustJSInteropTypeAnnotations: | |
| 340 _hasOption(options, Flags.trustJSInteropTypeAnnotations), | |
| 341 trustPrimitives: _hasOption(options, Flags.trustPrimitives), | |
| 342 trustTypeAnnotations: _hasOption(options, Flags.trustTypeAnnotations), | |
| 343 useContentSecurityPolicy: | |
| 344 _hasOption(options, Flags.useContentSecurityPolicy), | |
| 345 useCpsIr: _hasOption(options, Flags.useCpsIr), | |
| 346 useFrequencyNamer: | |
| 347 !_hasOption(options, Flags.noFrequencyBasedMinification), | |
| 348 useNewSourceInfo: _hasOption(options, Flags.useNewSourceInfo), | |
| 349 useStartupEmitter: _hasOption(options, Flags.fastStartup), | |
| 350 verbose: _hasOption(options, Flags.verbose)); | |
| 351 } | |
| 352 | |
| 103 /// Creates an option object for the compiler. | 353 /// Creates an option object for the compiler. |
| 104 // TODO(johnniwinther): Expand comment when [options] are explicit as named | 354 /// |
| 105 // arguments. | 355 /// This validates and normalizes dependent options to be consistent. For |
| 356 /// example, if [analyzeAll] is true, the resulting options object will also | |
| 357 /// have [analyzeOnly] as true. | |
| 106 factory CompilerOptions( | 358 factory CompilerOptions( |
| 107 {Uri entryPoint, | 359 {Uri entryPoint, |
| 108 Uri libraryRoot, | 360 Uri libraryRoot, |
| 109 Uri packageRoot, | 361 Uri packageRoot, |
| 110 Uri packageConfig, | 362 Uri packageConfig, |
| 111 PackagesDiscoveryProvider packagesDiscoveryProvider, | 363 PackagesDiscoveryProvider packagesDiscoveryProvider, |
| 112 List<String> options: const <String>[], | 364 Map<String, dynamic> environment: const <String, dynamic>{}, |
| 113 Map<String, dynamic> environment: const <String, dynamic>{}}) { | 365 bool allowMockCompilation: false, |
| 114 if (entryPoint == null) { | 366 bool allowNativeExtensions: false, |
| 115 throw new ArgumentError("entryPoint must be non-null"); | 367 bool analyzeAll: false, |
| 368 bool analyzeMain: false, | |
| 369 bool analyzeOnly: false, | |
| 370 bool analyzeSignaturesOnly: false, | |
| 371 String buildId: _UNDETERMINED_BUILD_ID, | |
| 372 bool dart2dartMultiFile: false, | |
| 373 Uri deferredMapUri: null, | |
| 374 DiagnosticOptions diagnosticOptions: const DiagnosticOptions(), | |
| 375 bool disableInlining: false, | |
| 376 bool disableTypeInference: false, | |
| 377 bool dumpInfo: false, | |
| 378 bool emitJavaScript: true, | |
| 379 bool enableAssertMessage: false, | |
| 380 bool enableConditionalDirectives: false, | |
| 381 bool enableExperimentalMirrors: false, | |
| 382 bool enableMinification: false, | |
| 383 bool enableNativeLiveTypeAnalysis: true, | |
| 384 bool enableTypeAssertions: false, | |
| 385 bool enableUserAssertions: false, | |
| 386 bool generateCodeWithCompileTimeErrors: false, | |
| 387 bool generateSourceMap: true, | |
| 388 bool hasIncrementalSupport: false, | |
| 389 Uri outputUri: null, | |
| 390 Uri platformConfigUri: null, | |
| 391 bool preserveComments: false, | |
| 392 bool preserveUris: false, | |
| 393 Uri sourceMapUri: null, | |
| 394 List<String> strips: const [], | |
| 395 bool testMode: false, | |
| 396 bool trustJSInteropTypeAnnotations: false, | |
| 397 bool trustPrimitives: false, | |
| 398 bool trustTypeAnnotations: false, | |
| 399 bool useContentSecurityPolicy: false, | |
| 400 bool useCpsIr: false, | |
| 401 bool useFrequencyNamer: true, | |
| 402 bool useNewSourceInfo: false, | |
| 403 bool useStartupEmitter: false, | |
| 404 bool verbose: false}) { | |
| 405 // TODO(sigmund): should entrypoint be here? should we validate it is not | |
| 406 // null? In unittests we use the same compiler to analyze or build multiple | |
| 407 // entrypoints. | |
| 408 if (libraryRoot == null) { | |
| 409 throw new ArgumentError("[libraryRoot] is null."); | |
| 116 } | 410 } |
| 117 if (!libraryRoot.path.endsWith("/")) { | 411 if (!libraryRoot.path.endsWith("/")) { |
| 118 throw new ArgumentError("libraryRoot must end with a /"); | 412 throw new ArgumentError("[libraryRoot] must end with a /"); |
| 413 } | |
| 414 if (packageRoot != null && packageConfig != null) { | |
| 415 throw new ArgumentError("Only one of [packageRoot] or [packageConfig] " | |
| 416 "may be given."); | |
| 119 } | 417 } |
| 120 if (packageRoot != null && !packageRoot.path.endsWith("/")) { | 418 if (packageRoot != null && !packageRoot.path.endsWith("/")) { |
| 121 throw new ArgumentError("packageRoot must end with a /"); | 419 throw new ArgumentError("[packageRoot] must end with a /"); |
| 122 } | 420 } |
| 123 return new CompilerOptions._( | 421 if (!analyzeOnly) { |
| 124 entryPoint, | 422 if (allowNativeExtensions) { |
| 125 libraryRoot, | 423 throw new ArgumentError( |
| 126 packageRoot, | 424 "${Flags.allowNativeExtensions} is only supported in combination " |
| 127 packageConfig, | 425 "with ${Flags.analyzeOnly}"); |
| 128 packagesDiscoveryProvider, | 426 } |
| 129 options, | 427 } |
| 130 environment); | 428 return new CompilerOptions._(entryPoint, libraryRoot, packageRoot, |
| 429 packageConfig, packagesDiscoveryProvider, environment, | |
| 430 allowMockCompilation: allowMockCompilation, | |
| 431 allowNativeExtensions: allowNativeExtensions, | |
| 432 analyzeAll: analyzeAll, | |
| 433 analyzeMain: analyzeMain, | |
| 434 analyzeOnly: analyzeOnly || analyzeSignaturesOnly || analyzeAll, | |
| 435 analyzeSignaturesOnly: analyzeSignaturesOnly, | |
| 436 buildId: buildId, | |
| 437 dart2dartMultiFile: dart2dartMultiFile, | |
| 438 deferredMapUri: deferredMapUri, | |
| 439 diagnosticOptions: diagnosticOptions, | |
| 440 disableInlining: disableInlining || hasIncrementalSupport, | |
| 441 disableTypeInference: disableTypeInference || !emitJavaScript, | |
| 442 dumpInfo: dumpInfo, | |
| 443 emitJavaScript: emitJavaScript, | |
| 444 enableAssertMessage: enableAssertMessage, | |
| 445 enableConditionalDirectives: enableConditionalDirectives, | |
| 446 enableExperimentalMirrors: enableExperimentalMirrors, | |
| 447 enableMinification: enableMinification, | |
| 448 enableNativeLiveTypeAnalysis: enableNativeLiveTypeAnalysis, | |
| 449 enableTypeAssertions: enableTypeAssertions, | |
| 450 enableUserAssertions: enableUserAssertions, | |
| 451 generateCodeWithCompileTimeErrors: generateCodeWithCompileTimeErrors, | |
| 452 generateSourceMap: generateSourceMap, | |
| 453 hasIncrementalSupport: hasIncrementalSupport, | |
| 454 outputUri: outputUri, | |
| 455 platformConfigUri: platformConfigUri ?? _resolvePlatformConfig( | |
| 456 libraryRoot, null, !emitJavaScript, const []), | |
| 457 preserveComments: preserveComments, | |
| 458 preserveUris: preserveUris, | |
| 459 sourceMapUri: sourceMapUri, | |
| 460 strips: strips, | |
| 461 testMode: testMode, | |
| 462 trustJSInteropTypeAnnotations: trustJSInteropTypeAnnotations, | |
| 463 trustPrimitives: trustPrimitives, | |
| 464 trustTypeAnnotations: trustTypeAnnotations, | |
| 465 useContentSecurityPolicy: useContentSecurityPolicy, | |
| 466 useCpsIr: useCpsIr, | |
| 467 useFrequencyNamer: useFrequencyNamer, | |
| 468 useNewSourceInfo: useNewSourceInfo, | |
| 469 useStartupEmitter: useStartupEmitter, | |
| 470 verbose: verbose); | |
| 131 } | 471 } |
| 132 | 472 |
| 133 CompilerOptions._( | 473 CompilerOptions._(this.entryPoint, this.libraryRoot, this.packageRoot, |
| 134 this.entryPoint, | 474 this.packageConfig, this.packagesDiscoveryProvider, this.environment, |
| 135 this.libraryRoot, | 475 {this.allowMockCompilation: false, |
| 136 this.packageRoot, | 476 this.allowNativeExtensions: false, |
| 137 this.packageConfig, | 477 this.analyzeAll: false, |
| 138 this.packagesDiscoveryProvider, | 478 this.analyzeMain: false, |
| 139 this.options, | 479 this.analyzeOnly: false, |
| 140 this.environment); | 480 this.analyzeSignaturesOnly: false, |
| 481 this.buildId: _UNDETERMINED_BUILD_ID, | |
| 482 this.dart2dartMultiFile: false, | |
| 483 this.deferredMapUri: null, | |
| 484 this.diagnosticOptions: null, | |
| 485 this.disableInlining: false, | |
| 486 this.disableTypeInference: false, | |
| 487 this.dumpInfo: false, | |
| 488 this.emitJavaScript: true, | |
| 489 this.enableAssertMessage: false, | |
| 490 this.enableConditionalDirectives: false, | |
| 491 this.enableExperimentalMirrors: false, | |
| 492 this.enableMinification: false, | |
| 493 this.enableNativeLiveTypeAnalysis: false, | |
| 494 this.enableTypeAssertions: false, | |
| 495 this.enableUserAssertions: false, | |
| 496 this.generateCodeWithCompileTimeErrors: false, | |
| 497 this.generateSourceMap: true, | |
| 498 this.hasIncrementalSupport: false, | |
| 499 this.outputUri: null, | |
| 500 this.platformConfigUri: null, | |
| 501 this.preserveComments: false, | |
| 502 this.preserveUris: false, | |
| 503 this.sourceMapUri: null, | |
| 504 this.strips: const [], | |
| 505 this.testMode: false, | |
| 506 this.trustJSInteropTypeAnnotations: false, | |
| 507 this.trustPrimitives: false, | |
| 508 this.trustTypeAnnotations: false, | |
| 509 this.useContentSecurityPolicy: false, | |
| 510 this.useCpsIr: false, | |
| 511 this.useFrequencyNamer: false, | |
| 512 this.useNewSourceInfo: false, | |
| 513 this.useStartupEmitter: false, | |
| 514 this.verbose: false}); | |
| 141 } | 515 } |
| 142 | 516 |
| 143 /// Returns a future that completes to a [CompilationResult] when the Dart | 517 /// Returns a future that completes to a [CompilationResult] when the Dart |
| 144 /// sources in [options] have been compiled. | 518 /// sources in [options] have been compiled. |
| 145 /// | 519 /// |
| 146 /// The generated compiler output is obtained by providing a [compilerOutput]. | 520 /// The generated compiler output is obtained by providing a [compilerOutput]. |
| 147 /// | 521 /// |
| 148 /// If the compilation fails, the future's `CompilationResult.isSuccess` is | 522 /// If the compilation fails, the future's `CompilationResult.isSuccess` is |
| 149 /// `false` and [CompilerDiagnostics.report] on [compilerDiagnostics] | 523 /// `false` and [CompilerDiagnostics.report] on [compilerDiagnostics] |
| 150 /// is invoked at least once with `kind == Diagnostic.ERROR` or | 524 /// is invoked at least once with `kind == Diagnostic.ERROR` or |
| 151 /// `kind == Diagnostic.CRASH`. | 525 /// `kind == Diagnostic.CRASH`. |
| 152 Future<CompilationResult> compile( | 526 Future<CompilationResult> compile( |
| 153 CompilerOptions compilerOptions, | 527 CompilerOptions compilerOptions, |
| 154 CompilerInput compilerInput, | 528 CompilerInput compilerInput, |
| 155 CompilerDiagnostics compilerDiagnostics, | 529 CompilerDiagnostics compilerDiagnostics, |
| 156 CompilerOutput compilerOutput) { | 530 CompilerOutput compilerOutput) { |
| 157 | |
| 158 if (compilerOptions == null) { | 531 if (compilerOptions == null) { |
| 159 throw new ArgumentError("compilerOptions must be non-null"); | 532 throw new ArgumentError("compilerOptions must be non-null"); |
| 160 } | 533 } |
| 161 if (compilerInput == null) { | 534 if (compilerInput == null) { |
| 162 throw new ArgumentError("compilerInput must be non-null"); | 535 throw new ArgumentError("compilerInput must be non-null"); |
| 163 } | 536 } |
| 164 if (compilerDiagnostics == null) { | 537 if (compilerDiagnostics == null) { |
| 165 throw new ArgumentError("compilerDiagnostics must be non-null"); | 538 throw new ArgumentError("compilerDiagnostics must be non-null"); |
| 166 } | 539 } |
| 167 if (compilerOutput == null) { | 540 if (compilerOutput == null) { |
| 168 throw new ArgumentError("compilerOutput must be non-null"); | 541 throw new ArgumentError("compilerOutput must be non-null"); |
| 169 } | 542 } |
| 170 | 543 |
| 171 CompilerImpl compiler = new CompilerImpl( | 544 CompilerImpl compiler = new CompilerImpl( |
| 172 compilerInput, | 545 compilerInput, compilerOutput, compilerDiagnostics, compilerOptions); |
| 173 compilerOutput, | |
| 174 compilerDiagnostics, | |
| 175 compilerOptions.libraryRoot, | |
| 176 compilerOptions.packageRoot, | |
| 177 compilerOptions.options, | |
| 178 compilerOptions.environment, | |
| 179 compilerOptions.packageConfig, | |
| 180 compilerOptions.packagesDiscoveryProvider); | |
| 181 return compiler.run(compilerOptions.entryPoint).then((bool success) { | 546 return compiler.run(compilerOptions.entryPoint).then((bool success) { |
| 182 return new CompilationResult(compiler, isSuccess: success); | 547 return new CompilationResult(compiler, isSuccess: success); |
| 183 }); | 548 }); |
| 184 } | 549 } |
| 550 | |
| 551 String _extractStringOption( | |
| 552 List<String> options, String prefix, String defaultValue) { | |
| 553 for (String option in options) { | |
| 554 if (option.startsWith(prefix)) { | |
| 555 return option.substring(prefix.length); | |
| 556 } | |
| 557 } | |
| 558 return defaultValue; | |
| 559 } | |
| 560 | |
| 561 Uri _extractUriOption(List<String> options, String prefix) { | |
| 562 var option = _extractStringOption(options, prefix, null); | |
| 563 return (option == null) ? null : Uri.parse(option); | |
| 564 } | |
| 565 | |
| 566 // CSV: Comma separated values. | |
| 567 List<String> _extractCsvOption(List<String> options, String prefix) { | |
| 568 for (String option in options) { | |
| 569 if (option.startsWith(prefix)) { | |
| 570 return option.substring(prefix.length).split(','); | |
| 571 } | |
| 572 } | |
| 573 return const <String>[]; | |
| 574 } | |
| 575 | |
| 576 /// Extract list of comma separated values provided for [flag]. Returns an | |
| 577 /// empty list if [option] contain [flag] without arguments. Returns `null` if | |
| 578 /// [option] doesn't contain [flag] with or without arguments. | |
| 579 List<String> _extractOptionalCsvOption(List<String> options, String flag) { | |
| 580 String prefix = '$flag='; | |
| 581 for (String option in options) { | |
| 582 if (option == flag) { | |
| 583 return const <String>[]; | |
| 584 } | |
| 585 if (option.startsWith(flag)) { | |
| 586 return option.substring(prefix.length).split(','); | |
| 587 } | |
| 588 } | |
| 589 return null; | |
| 590 } | |
| 591 | |
| 592 Uri _resolvePlatformConfigFromOptions(Uri libraryRoot, List<String> options) { | |
| 593 return _resolvePlatformConfig(libraryRoot, | |
| 594 _extractStringOption(options, "--platform-config=", null), | |
| 595 _hasOption(options, '--output-type=dart'), | |
| 596 _extractCsvOption(options, '--categories=')); | |
| 597 } | |
| 598 | |
| 599 Uri _resolvePlatformConfig(Uri libraryRoot, | |
| 600 String platformConfigPath, bool isDart2Dart, Iterable<String> categories) { | |
| 601 if (platformConfigPath != null) { | |
| 602 return libraryRoot.resolve(platformConfigPath); | |
| 603 } else if (isDart2Dart) { | |
| 604 return libraryRoot.resolve(_dart2dartPlatform); | |
| 605 } else { | |
| 606 if (categories.length == 0) { | |
| 607 return libraryRoot.resolve(_clientPlatform); | |
| 608 } | |
| 609 assert(categories.length <= 2); | |
| 610 if (categories.contains("Client")) { | |
| 611 if (categories.contains("Server")) { | |
| 612 return libraryRoot.resolve(_sharedPlatform); | |
| 613 } | |
| 614 return libraryRoot.resolve(_clientPlatform); | |
| 615 } | |
| 616 assert(categories.contains("Server")); | |
| 617 return libraryRoot.resolve(_serverPlatform); | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 bool _hasOption(List<String> options, String option) { | |
| 622 return options.indexOf(option) >= 0; | |
| 623 } | |
| 624 | |
| 625 /// Locations of the platform descriptor files relative to the library root. | |
| 626 const String _clientPlatform = "lib/dart_client.platform"; | |
| 627 const String _serverPlatform = "lib/dart_server.platform"; | |
| 628 const String _sharedPlatform = "lib/dart_shared.platform"; | |
| 629 const String _dart2dartPlatform = "lib/dart2dart.platform"; | |
| 630 | |
| 631 const String _UNDETERMINED_BUILD_ID = "build number could not be determined"; | |
| 632 const bool _forceIncrementalSupport = | |
| 633 const bool.fromEnvironment('DART2JS_EXPERIMENTAL_INCREMENTAL_SUPPORT'); | |
| OLD | NEW |