OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.outline; | 5 library fasta.outline; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:io' show exitCode; | 9 import 'dart:io' show exitCode; |
10 | 10 |
11 import 'kernel/verifier.dart' show verifyProgram; | 11 import 'kernel/verifier.dart' show verifyProgram; |
12 | 12 |
13 import 'compiler_command_line.dart' show CompilerCommandLine; | 13 import 'compiler_command_line.dart' show CompilerCommandLine; |
14 | 14 |
15 import 'compiler_context.dart' show CompilerContext; | 15 import 'compiler_context.dart' show CompilerContext; |
16 | 16 |
17 import 'errors.dart' show InputError, inputError; | 17 import 'errors.dart' show InputError, inputError; |
18 | 18 |
19 import 'kernel/kernel_target.dart' show KernelTarget; | 19 import 'kernel/kernel_target.dart' show KernelTarget; |
20 | 20 |
21 import 'dill/dill_target.dart' show DillTarget; | 21 import 'dill/dill_target.dart' show DillTarget; |
22 | 22 |
23 import 'ticker.dart' show Ticker; | 23 import 'ticker.dart' show Ticker; |
24 | 24 |
25 import 'translate_uri.dart' show TranslateUri; | 25 import 'translate_uri.dart' show TranslateUri; |
26 | 26 |
27 import 'ast_kind.dart' show AstKind; | |
28 | |
29 Future<KernelTarget> outline(List<String> arguments) async { | 27 Future<KernelTarget> outline(List<String> arguments) async { |
30 try { | 28 try { |
31 return await CompilerCommandLine.withGlobalOptions("outline", arguments, | 29 return await CompilerCommandLine.withGlobalOptions("outline", arguments, |
32 (CompilerContext c) async { | 30 (CompilerContext c) async { |
33 if (c.options.verbose) { | 31 if (c.options.verbose) { |
34 print("Building outlines for ${arguments.join(' ')}"); | 32 print("Building outlines for ${arguments.join(' ')}"); |
35 } | 33 } |
36 return await doOutline( | 34 CompileTask task = |
37 c, new Ticker(isVerbose: c.options.verbose), c.options.output); | 35 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
| 36 return await task.buildOutline(c.options.output); |
38 }); | 37 }); |
39 } on InputError catch (e) { | 38 } on InputError catch (e) { |
40 exitCode = 1; | 39 exitCode = 1; |
41 print(e.format()); | 40 print(e.format()); |
42 return null; | 41 return null; |
43 } | 42 } |
44 } | 43 } |
45 | 44 |
46 Future<Uri> compile(List<String> arguments) async { | 45 Future<Uri> compile(List<String> arguments) async { |
47 try { | 46 try { |
48 return await CompilerCommandLine.withGlobalOptions("compile", arguments, | 47 return await CompilerCommandLine.withGlobalOptions("compile", arguments, |
49 (CompilerContext c) async { | 48 (CompilerContext c) async { |
50 if (c.options.verbose) { | 49 if (c.options.verbose) { |
51 print("Compiling directly to Kernel: ${arguments.join(' ')}"); | 50 print("Compiling directly to Kernel: ${arguments.join(' ')}"); |
52 } | 51 } |
53 return await doCompile( | 52 CompileTask task = |
54 c, new Ticker(isVerbose: c.options.verbose), AstKind.Kernel); | 53 new CompileTask(c, new Ticker(isVerbose: c.options.verbose)); |
| 54 return await task.compile(); |
55 }); | 55 }); |
56 } on InputError catch (e) { | 56 } on InputError catch (e) { |
57 exitCode = 1; | 57 exitCode = 1; |
58 print(e.format()); | 58 print(e.format()); |
59 return null; | 59 return null; |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 Future<Uri> kompile(List<String> arguments) async { | 63 class CompileTask { |
64 try { | 64 final CompilerContext c; |
65 return await CompilerCommandLine.withGlobalOptions("kompile", arguments, | 65 final Ticker ticker; |
66 (CompilerContext c) async { | 66 |
67 if (c.options.verbose) { | 67 CompileTask(this.c, this.ticker); |
68 print("Compiling via analyzer: ${arguments.join(' ')}"); | 68 |
| 69 KernelTarget createKernelTarget( |
| 70 DillTarget dillTarget, TranslateUri uriTranslator) { |
| 71 return new KernelTarget(dillTarget, uriTranslator, c.uriToSource); |
| 72 } |
| 73 |
| 74 Future<KernelTarget> buildOutline([Uri output]) async { |
| 75 TranslateUri uriTranslator = await TranslateUri.parse(c.options.sdk); |
| 76 ticker.logMs("Read packages file"); |
| 77 DillTarget dillTarget = new DillTarget(ticker, uriTranslator); |
| 78 KernelTarget kernelTarget = createKernelTarget(dillTarget, uriTranslator); |
| 79 Uri platform = c.options.platform; |
| 80 if (platform != null) { |
| 81 dillTarget.read(platform); |
| 82 } |
| 83 String argument = c.options.arguments.first; |
| 84 Uri uri = Uri.base.resolve(argument); |
| 85 String path = uriTranslator.translate(uri)?.path ?? argument; |
| 86 if (path.endsWith(".dart")) { |
| 87 kernelTarget.read(uri); |
| 88 } else { |
| 89 inputError(uri, -1, "Unexpected input: $uri"); |
| 90 } |
| 91 await dillTarget.writeOutline(null); |
| 92 await kernelTarget.writeOutline(output); |
| 93 if (c.options.dumpIr && output != null) { |
| 94 kernelTarget.dumpIr(); |
| 95 } |
| 96 return kernelTarget; |
| 97 } |
| 98 |
| 99 Future<Uri> compile() async { |
| 100 KernelTarget kernelTarget = await buildOutline(); |
| 101 if (exitCode != 0) return null; |
| 102 Uri uri = c.options.output; |
| 103 await kernelTarget.writeProgram(uri); |
| 104 if (c.options.dumpIr) { |
| 105 kernelTarget.dumpIr(); |
| 106 } |
| 107 if (c.options.verify) { |
| 108 try { |
| 109 verifyProgram(kernelTarget.program); |
| 110 ticker.logMs("Verified program"); |
| 111 } catch (e, s) { |
| 112 exitCode = 1; |
| 113 print("Verification of program failed: $e"); |
| 114 if (s != null && c.options.verbose) { |
| 115 print(s); |
| 116 } |
69 } | 117 } |
70 return await doCompile( | 118 } |
71 c, new Ticker(isVerbose: c.options.verbose), AstKind.Analyzer); | 119 return uri; |
72 }); | |
73 } on InputError catch (e) { | |
74 exitCode = 1; | |
75 print(e.format()); | |
76 return null; | |
77 } | 120 } |
78 } | 121 } |
79 | |
80 Future<KernelTarget> doOutline(CompilerContext c, Ticker ticker, | |
81 [Uri output]) async { | |
82 TranslateUri uriTranslator = await TranslateUri.parse(c.options.sdk); | |
83 ticker.logMs("Read packages file"); | |
84 DillTarget dillTarget = new DillTarget(ticker, uriTranslator); | |
85 KernelTarget kernelTarget = | |
86 new KernelTarget(dillTarget, uriTranslator, c.uriToSource); | |
87 Uri platform = c.options.platform; | |
88 if (platform != null) { | |
89 dillTarget.read(platform); | |
90 } | |
91 String argument = c.options.arguments.first; | |
92 Uri uri = Uri.base.resolve(argument); | |
93 String path = uriTranslator.translate(uri)?.path ?? argument; | |
94 if (path.endsWith(".dart")) { | |
95 kernelTarget.read(uri); | |
96 } else { | |
97 inputError(uri, -1, "Unexpected input: $uri"); | |
98 } | |
99 await dillTarget.writeOutline(null); | |
100 await kernelTarget.writeOutline(output); | |
101 if (c.options.dumpIr && output != null) { | |
102 kernelTarget.dumpIr(); | |
103 } | |
104 return kernelTarget; | |
105 } | |
106 | |
107 Future<Uri> doCompile(CompilerContext c, Ticker ticker, AstKind kind) async { | |
108 KernelTarget kernelTarget = await doOutline(c, ticker); | |
109 if (exitCode != 0) return null; | |
110 Uri uri = c.options.output; | |
111 await kernelTarget.writeProgram(uri, kind); | |
112 if (c.options.dumpIr) { | |
113 kernelTarget.dumpIr(); | |
114 } | |
115 if (c.options.verify) { | |
116 try { | |
117 verifyProgram(kernelTarget.program); | |
118 ticker.logMs("Verified program"); | |
119 } catch (e, s) { | |
120 exitCode = 1; | |
121 print("Verification of program failed: $e"); | |
122 if (s != null && c.options.verbose) { | |
123 print(s); | |
124 } | |
125 } | |
126 } | |
127 return uri; | |
128 } | |
OLD | NEW |