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 kernel.transformations.reify.standalone_runner; | 5 library kernel.transformations.reify.standalone_runner; |
6 | 6 |
7 import 'analysis/program_analysis.dart'; | 7 import 'analysis/program_analysis.dart'; |
8 import 'dart:io' show File, IOSink; | 8 import 'dart:io' show File, IOSink; |
9 | 9 |
10 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; | 10 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; |
(...skipping 23 matching lines...) Expand all Loading... |
34 return candidates.single; | 34 return candidates.single; |
35 } | 35 } |
36 | 36 |
37 Library types = findLibraryEndingWith("reify/types.dart"); | 37 Library types = findLibraryEndingWith("reify/types.dart"); |
38 Library declarations = findLibraryEndingWith("reify/declarations.dart"); | 38 Library declarations = findLibraryEndingWith("reify/declarations.dart"); |
39 Library interceptors = findLibraryEndingWith("reify/interceptors.dart"); | 39 Library interceptors = findLibraryEndingWith("reify/interceptors.dart"); |
40 return new RuntimeLibrary(types, declarations, interceptors); | 40 return new RuntimeLibrary(types, declarations, interceptors); |
41 } | 41 } |
42 | 42 |
43 Program transformProgramUsingLibraries( | 43 Program transformProgramUsingLibraries( |
44 Program program, RuntimeLibrary runtimeLibrary, | 44 CoreTypes coreTypes, Program program, RuntimeLibrary runtimeLibrary, |
45 [Library libraryToTransform]) { | 45 [Library libraryToTransform]) { |
46 LibraryFilter filter = libraryToTransform != null | 46 LibraryFilter filter = libraryToTransform != null |
47 ? (Library library) => library == libraryToTransform | 47 ? (Library library) => library == libraryToTransform |
48 : (_) => true; | 48 : (_) => true; |
49 ProgramKnowledge knowledge = analyze(program, analyzeLibrary: filter); | 49 ProgramKnowledge knowledge = analyze(program, analyzeLibrary: filter); |
50 Library mainLibrary = program.mainMethod.parent; | 50 Library mainLibrary = program.mainMethod.parent; |
51 RuntimeTypeSupportBuilder builder = new RuntimeTypeSupportBuilder( | 51 RuntimeTypeSupportBuilder builder = |
52 runtimeLibrary, new CoreTypes(program), mainLibrary); | 52 new RuntimeTypeSupportBuilder(runtimeLibrary, coreTypes, mainLibrary); |
53 ReifyVisitor transformer = | 53 ReifyVisitor transformer = |
54 new ReifyVisitor(runtimeLibrary, builder, knowledge, libraryToTransform); | 54 new ReifyVisitor(runtimeLibrary, builder, knowledge, libraryToTransform); |
55 // Transform the main program. | 55 // Transform the main program. |
56 program = program.accept(transformer); | 56 program = program.accept(transformer); |
57 if (!filter(runtimeLibrary.interceptorsLibrary)) { | 57 if (!filter(runtimeLibrary.interceptorsLibrary)) { |
58 // We need to transform the interceptor function in any case to make sure | 58 // We need to transform the interceptor function in any case to make sure |
59 // that the type literals in the interceptor function are rewritten. | 59 // that the type literals in the interceptor function are rewritten. |
60 runtimeLibrary.interceptorFunction.accept(transformer); | 60 runtimeLibrary.interceptorFunction.accept(transformer); |
61 } | 61 } |
62 builder.createDeclarations(); | 62 builder.createDeclarations(); |
63 program = program.accept(new Erasure(transformer)); | 63 program = program.accept(new Erasure(transformer)); |
64 // TODO(karlklose): skip checks in debug mode | 64 // TODO(karlklose): skip checks in debug mode |
65 verifyProgram(program); | 65 verifyProgram(program); |
66 return program; | 66 return program; |
67 } | 67 } |
68 | 68 |
69 Program transformProgram(Program program) { | 69 Program transformProgram(CoreTypes coreTypes, Program program) { |
70 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); | 70 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); |
71 Library mainLibrary = program.mainMethod.enclosingLibrary; | 71 Library mainLibrary = program.mainMethod.enclosingLibrary; |
72 return transformProgramUsingLibraries(program, runtimeLibrary, mainLibrary); | 72 return transformProgramUsingLibraries( |
| 73 coreTypes, program, runtimeLibrary, mainLibrary); |
73 } | 74 } |
74 | 75 |
75 main(List<String> arguments) async { | 76 main(List<String> arguments) async { |
76 String path = arguments.first; | 77 String path = arguments.first; |
77 Uri output; | 78 Uri output; |
78 if (arguments.length > 1) { | 79 if (arguments.length > 1) { |
79 output = Uri.base.resolve(arguments[1]); | 80 output = Uri.base.resolve(arguments[1]); |
80 } | 81 } |
81 Uri uri = Uri.base.resolve(path); | 82 Uri uri = Uri.base.resolve(path); |
82 Program program = loadProgramFromBinary(uri.toFilePath()); | 83 Program program = loadProgramFromBinary(uri.toFilePath()); |
| 84 CoreTypes coreTypes = new CoreTypes(program); |
83 | 85 |
84 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); | 86 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); |
85 Library mainLibrary = program.mainMethod.enclosingLibrary; | 87 Library mainLibrary = program.mainMethod.enclosingLibrary; |
86 program = | 88 program = transformProgramUsingLibraries( |
87 transformProgramUsingLibraries(program, runtimeLibrary, mainLibrary); | 89 coreTypes, program, runtimeLibrary, mainLibrary); |
88 | 90 |
89 if (output == null) { | 91 if (output == null) { |
90 // Print result | 92 // Print result |
91 StringBuffer sb = new StringBuffer(); | 93 StringBuffer sb = new StringBuffer(); |
92 Printer printer = new Printer(sb); | 94 Printer printer = new Printer(sb); |
93 printer.writeLibraryFile(mainLibrary); | 95 printer.writeLibraryFile(mainLibrary); |
94 print("$sb"); | 96 print("$sb"); |
95 } else { | 97 } else { |
96 IOSink sink = new File.fromUri(output).openWrite(); | 98 IOSink sink = new File.fromUri(output).openWrite(); |
97 try { | 99 try { |
98 new BinaryPrinter(sink).writeProgramFile(program); | 100 new BinaryPrinter(sink).writeProgramFile(program); |
99 } finally { | 101 } finally { |
100 await sink.close(); | 102 await sink.close(); |
101 } | 103 } |
102 try { | 104 try { |
103 // Check that we can read the binary file. | 105 // Check that we can read the binary file. |
104 loadProgramFromBinary(output.toFilePath()); | 106 loadProgramFromBinary(output.toFilePath()); |
105 } catch (e) { | 107 } catch (e) { |
106 print("Error when attempting to read $output."); | 108 print("Error when attempting to read $output."); |
107 rethrow; | 109 rethrow; |
108 } | 110 } |
109 } | 111 } |
110 } | 112 } |
OLD | NEW |