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:kernel/ast.dart' show Source; | 11 import 'package:kernel/ast.dart' show Source; |
12 | 12 |
13 import 'compiler_command_line.dart' show CompilerCommandLine; | 13 import 'compiler_command_line.dart' show CompilerCommandLine; |
14 | 14 |
15 import 'colors.dart' show computeEnableColors; | 15 import 'colors.dart' show computeEnableColors; |
16 | 16 |
| 17 import 'fasta_codes.dart' show LocatedMessage, Message; |
| 18 |
| 19 import 'severity.dart' show Severity; |
| 20 |
17 final Object compilerContextKey = new Object(); | 21 final Object compilerContextKey = new Object(); |
18 | 22 |
19 final CompilerContext rootContext = | 23 final CompilerContext rootContext = |
20 new CompilerContext(CompilerCommandLine.forRootContext()); | 24 new CompilerContext(CompilerCommandLine.forRootContext()); |
21 | 25 |
22 class CompilerContext { | 26 class CompilerContext { |
23 final FileSystem fileSystem = PhysicalFileSystem.instance; | 27 final FileSystem fileSystem = PhysicalFileSystem.instance; |
| 28 |
24 final CompilerCommandLine options; | 29 final CompilerCommandLine options; |
25 | 30 |
26 final Map<String, Source> uriToSource = <String, Source>{}; | 31 final Map<String, Source> uriToSource = <String, Source>{}; |
27 | 32 |
28 bool enableColorsCached = null; | 33 bool enableColorsCached = null; |
29 | 34 |
30 CompilerContext(this.options); | 35 CompilerContext(this.options); |
31 | 36 |
32 void disableColors() { | 37 void disableColors() { |
33 enableColorsCached = false; | 38 enableColorsCached = false; |
34 } | 39 } |
35 | 40 |
| 41 /// Report [message], for example, by printing it. |
| 42 void report(LocatedMessage message, Severity severity) { |
| 43 options.report(message, severity); |
| 44 } |
| 45 |
| 46 /// Report [message], for example, by printing it. |
| 47 void reportWithoutLocation(Message message, Severity severity) { |
| 48 options.reportWithoutLocation(message, severity); |
| 49 } |
| 50 |
| 51 /// Format [message] as a text string that can be included in generated code. |
| 52 String format(LocatedMessage message, Severity severity) { |
| 53 return options.format(message, severity); |
| 54 } |
| 55 |
| 56 /// Format [message] as a text string that can be included in generated code. |
| 57 String formatWithoutLocation(Message message, Severity severity) { |
| 58 return options.formatWithoutLocation(message, severity); |
| 59 } |
| 60 |
36 static CompilerContext get current { | 61 static CompilerContext get current { |
37 return Zone.current[compilerContextKey] ?? rootContext; | 62 return Zone.current[compilerContextKey] ?? rootContext; |
38 } | 63 } |
39 | 64 |
40 /// Perform [action] in a [Zone] where [cl] will be available as | 65 /// Perform [action] in a [Zone] where [cl] will be available as |
41 /// `CompilerContext.current.options`. | 66 /// `CompilerContext.current.options`. |
42 static dynamic withGlobalOptions( | 67 static dynamic withGlobalOptions( |
43 CompilerCommandLine cl, dynamic action(CompilerContext c)) { | 68 CompilerCommandLine cl, dynamic action(CompilerContext c)) { |
44 CompilerContext c = new CompilerContext(cl); | 69 CompilerContext c = new CompilerContext(cl); |
45 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); | 70 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); |
46 } | 71 } |
47 | 72 |
48 static bool get enableColors { | 73 static bool get enableColors { |
49 return current.enableColorsCached ??= computeEnableColors(current); | 74 return current.enableColorsCached ??= computeEnableColors(current); |
50 } | 75 } |
51 } | 76 } |
OLD | NEW |