| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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 fasta.compiler_context; | 5 library fasta.compiler_context; |
| 6 | 6 |
| 7 import 'dart:async' show Zone, runZoned; | 7 import 'dart:async' show Zone, runZoned; |
| 8 | 8 |
| 9 import 'package:front_end/file_system.dart'; | 9 import 'package:front_end/file_system.dart'; |
| 10 import 'package:front_end/physical_file_system.dart'; | 10 import 'package:front_end/physical_file_system.dart'; |
| 11 import 'package:front_end/src/fasta/fasta_codes.dart'; |
| 11 import 'package:kernel/ast.dart' show Source; | 12 import 'package:kernel/ast.dart' show Source; |
| 12 | 13 |
| 13 import 'compiler_command_line.dart' show CompilerCommandLine; | 14 import 'compiler_command_line.dart' show CompilerCommandLine; |
| 14 | 15 |
| 15 import 'colors.dart' show computeEnableColors; | 16 import 'colors.dart' show computeEnableColors; |
| 16 | 17 |
| 17 import 'fasta_codes.dart' show LocatedMessage, Message; | 18 import 'fasta_codes.dart' show LocatedMessage, Message; |
| 18 | 19 |
| 19 import 'severity.dart' show Severity; | 20 import 'severity.dart' show Severity; |
| 20 | 21 |
| 21 final Object compilerContextKey = new Object(); | 22 final Object compilerContextKey = new Object(); |
| 22 | 23 |
| 23 final CompilerContext rootContext = | |
| 24 new CompilerContext(CompilerCommandLine.forRootContext()); | |
| 25 | |
| 26 class CompilerContext { | 24 class CompilerContext { |
| 27 final FileSystem fileSystem = PhysicalFileSystem.instance; | 25 final FileSystem fileSystem = PhysicalFileSystem.instance; |
| 28 | 26 |
| 29 final CompilerCommandLine options; | 27 final CompilerCommandLine options; |
| 30 | 28 |
| 31 final Map<String, Source> uriToSource = <String, Source>{}; | 29 final Map<String, Source> uriToSource = <String, Source>{}; |
| 32 | 30 |
| 33 bool enableColorsCached = null; | 31 bool enableColorsCached = null; |
| 34 | 32 |
| 35 CompilerContext(this.options); | 33 CompilerContext(this.options); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 52 String format(LocatedMessage message, Severity severity) { | 50 String format(LocatedMessage message, Severity severity) { |
| 53 return options.format(message, severity); | 51 return options.format(message, severity); |
| 54 } | 52 } |
| 55 | 53 |
| 56 /// Format [message] as a text string that can be included in generated code. | 54 /// Format [message] as a text string that can be included in generated code. |
| 57 String formatWithoutLocation(Message message, Severity severity) { | 55 String formatWithoutLocation(Message message, Severity severity) { |
| 58 return options.formatWithoutLocation(message, severity); | 56 return options.formatWithoutLocation(message, severity); |
| 59 } | 57 } |
| 60 | 58 |
| 61 static CompilerContext get current { | 59 static CompilerContext get current { |
| 62 return Zone.current[compilerContextKey] ?? rootContext; | 60 var context = Zone.current[compilerContextKey]; |
| 61 if (context == null) { |
| 62 // Note: we throw directly and don't use internalProblem, because |
| 63 // internalProblem depends on having a compiler context available. |
| 64 var message = messageInternalProblemMissingContext.message; |
| 65 var tip = messageInternalProblemMissingContext.tip; |
| 66 throw "Internal problem: $message\nTip: $tip"; |
| 67 } |
| 68 return context; |
| 63 } | 69 } |
| 64 | 70 |
| 65 /// Perform [action] in a [Zone] where [cl] will be available as | 71 /// Perform [action] in a [Zone] where [cl] will be available as |
| 66 /// `CompilerContext.current.options`. | 72 /// `CompilerContext.current.options`. |
| 67 static dynamic withGlobalOptions( | 73 static dynamic withGlobalOptions( |
| 68 CompilerCommandLine cl, dynamic action(CompilerContext c)) { | 74 CompilerCommandLine cl, dynamic action(CompilerContext c)) { |
| 69 CompilerContext c = new CompilerContext(cl); | 75 CompilerContext c = new CompilerContext(cl); |
| 70 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); | 76 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); |
| 71 } | 77 } |
| 72 | 78 |
| 73 static bool get enableColors { | 79 static bool get enableColors { |
| 74 return current.enableColorsCached ??= computeEnableColors(current); | 80 return current.enableColorsCached ??= computeEnableColors(current); |
| 75 } | 81 } |
| 76 } | 82 } |
| OLD | NEW |