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/compiler_options.dart'; | |
9 import 'package:front_end/file_system.dart'; | 10 import 'package:front_end/file_system.dart'; |
10 import 'package:front_end/physical_file_system.dart'; | 11 import 'package:front_end/src/base/processed_options.dart'; |
11 import 'package:front_end/src/fasta/fasta_codes.dart'; | 12 import 'package:front_end/src/fasta/fasta_codes.dart'; |
12 import 'package:kernel/ast.dart' show Source; | 13 import 'package:kernel/ast.dart' show Source; |
13 | 14 import 'command_line_reporting.dart' as command_line_reporting; |
14 import 'compiler_command_line.dart' show CompilerCommandLine; | |
15 | 15 |
16 import 'colors.dart' show computeEnableColors; | 16 import 'colors.dart' show computeEnableColors; |
17 | 17 |
18 import 'fasta_codes.dart' show LocatedMessage, Message; | 18 import 'fasta_codes.dart' show LocatedMessage, Message; |
19 | 19 |
20 import 'severity.dart' show Severity; | 20 import 'severity.dart' show Severity; |
21 | 21 |
22 final Object compilerContextKey = new Object(); | 22 final Object compilerContextKey = new Object(); |
23 | 23 |
24 /// Shared context used throughout the compiler. | |
25 /// | |
26 /// The compiler works with a single instance of this class. To avoid | |
27 /// passing it around as an argument everywhere, it is stored as a zone-value. | |
28 /// | |
29 /// For convenience the static getter [CompilerContext.current] retrieves the | |
30 /// context stored in the current zone. | |
24 class CompilerContext { | 31 class CompilerContext { |
25 final FileSystem fileSystem = PhysicalFileSystem.instance; | 32 // TODO(sigmund): move here methods in ProcessedOptions that don't seem |
ahe
2017/07/18 10:40:51
"move" -> "Move".
Consider: "Move methods in Proc
Siggi Cherem (dart-lang)
2017/07/18 22:50:43
Done.
| |
33 // appropriate as "options", or consider merging ProcessedOptions entirely | |
34 // within this class, and depend only on the raw options here. | |
35 final ProcessedOptions options; | |
26 | 36 |
27 final CompilerCommandLine options; | 37 /// Sources seen by the compiler. |
38 /// | |
39 /// This is populated as the compiler loads files, and it is used for error | |
ahe
2017/07/18 10:40:51
loads -> reads
Siggi Cherem (dart-lang)
2017/07/18 22:50:44
Done.
| |
40 /// reporting and to generate source location information in the compiled | |
41 /// programs. | |
42 final Map<String, Source> uriToSource = <String, Source>{}; | |
28 | 43 |
29 final Map<String, Source> uriToSource = <String, Source>{}; | 44 FileSystem get fileSystem => options.fileSystem; |
30 | 45 |
31 bool enableColorsCached = null; | 46 bool enableColorsCached = null; |
32 | 47 |
33 CompilerContext(this.options); | 48 CompilerContext(this.options); |
34 | 49 |
35 void disableColors() { | 50 void disableColors() { |
36 enableColorsCached = false; | 51 enableColorsCached = false; |
37 } | 52 } |
38 | 53 |
39 /// Report [message], for example, by printing it. | 54 /// Report [message], for example, by printing it. |
40 void report(LocatedMessage message, Severity severity) { | 55 void report(LocatedMessage message, Severity severity) { |
41 options.report(message, severity); | 56 options.report(message, severity); |
57 // TODO: default to > command_line_reporting.report; | |
ahe
2017/07/18 10:40:50
I'm not sure we should submit this CL with this TO
ahe
2017/07/18 10:40:51
No name on TODO.
"default" -> "Default"
";" -> "."
Siggi Cherem (dart-lang)
2017/07/18 22:50:43
Sorry, I forgot to delete this line. It was a note
| |
42 } | 58 } |
43 | 59 |
44 /// Report [message], for example, by printing it. | 60 /// Report [message], for example, by printing it. |
45 void reportWithoutLocation(Message message, Severity severity) { | 61 void reportWithoutLocation(Message message, Severity severity) { |
46 options.reportWithoutLocation(message, severity); | 62 options.reportWithoutLocation(message, severity); |
63 // TODO: default to > command_line_reporting.reportWithoutLocation; | |
ahe
2017/07/18 10:40:50
Ditto.
Siggi Cherem (dart-lang)
2017/07/18 22:50:44
Done.
| |
47 } | 64 } |
48 | 65 |
49 /// Format [message] as a text string that can be included in generated code. | 66 /// Format [message] as a text string that can be included in generated code. |
50 String format(LocatedMessage message, Severity severity) { | 67 String format(LocatedMessage message, Severity severity) { |
51 return options.format(message, severity); | 68 return command_line_reporting.format(message, severity); |
Siggi Cherem (dart-lang)
2017/07/18 00:11:08
Peter - the default format has the notion of color
ahe
2017/07/18 10:40:51
I think command_line_reporting uses a format that'
Siggi Cherem (dart-lang)
2017/07/18 22:50:43
I see, I don't have other use cases in mind at the
| |
52 } | 69 } |
53 | 70 |
54 /// Format [message] as a text string that can be included in generated code. | 71 /// Format [message] as a text string that can be included in generated code. |
55 String formatWithoutLocation(Message message, Severity severity) { | 72 String formatWithoutLocation(Message message, Severity severity) { |
56 return options.formatWithoutLocation(message, severity); | 73 return command_line_reporting.formatWithoutLocation(message, severity); |
57 } | 74 } |
58 | 75 |
59 static CompilerContext get current { | 76 static CompilerContext get current { |
60 var context = Zone.current[compilerContextKey]; | 77 var context = Zone.current[compilerContextKey]; |
61 if (context == null) { | 78 if (context == null) { |
62 // Note: we throw directly and don't use internalProblem, because | 79 // Note: we throw directly and don't use internalProblem, because |
63 // internalProblem depends on having a compiler context available. | 80 // internalProblem depends on having a compiler context available. |
64 var message = messageInternalProblemMissingContext.message; | 81 var message = messageInternalProblemMissingContext.message; |
65 var tip = messageInternalProblemMissingContext.tip; | 82 var tip = messageInternalProblemMissingContext.tip; |
66 throw "Internal problem: $message\nTip: $tip"; | 83 throw "Internal problem: $message\nTip: $tip"; |
67 } | 84 } |
68 return context; | 85 return context; |
69 } | 86 } |
70 | 87 |
71 /// Perform [action] in a [Zone] where [cl] will be available as | 88 /// Perform [action] in a [Zone] where [this] will be available as |
72 /// `CompilerContext.current.options`. | 89 /// `CompilerContext.current.options`. |
73 static dynamic withGlobalOptions( | 90 T runInContext<T>(T action(CompilerContext c)) { |
74 CompilerCommandLine cl, dynamic action(CompilerContext c)) { | 91 return runZoned(() => action(this), zoneValues: {compilerContextKey: this}); |
75 CompilerContext c = new CompilerContext(cl); | 92 } |
76 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); | 93 |
94 /// Perform [action] in a [Zone] where [options] will be available as | |
95 /// `CompilerContext.current.options`. | |
96 static T runWithOptions<T>( | |
97 ProcessedOptions options, T action(CompilerContext c)) { | |
98 return new CompilerContext(options).runInContext(action); | |
99 } | |
100 | |
101 static T runWithDefaultOptions<T>(T action(CompilerContext c)) { | |
102 var options = new CompilerOptions(); | |
103 var pOptions = new ProcessedOptions(options); | |
ahe
2017/07/18 10:40:51
Consider:
var options = new ProcessedOptions(new
Siggi Cherem (dart-lang)
2017/07/18 22:50:44
Done.
| |
104 return new CompilerContext(pOptions).runInContext(action); | |
77 } | 105 } |
78 | 106 |
79 static bool get enableColors { | 107 static bool get enableColors { |
80 return current.enableColorsCached ??= computeEnableColors(current); | 108 return current.enableColorsCached ??= computeEnableColors(current); |
81 } | 109 } |
82 } | 110 } |
OLD | NEW |