OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library kernel.transformations.reify.standalone_runner; |
| 6 |
| 7 import 'analysis/program_analysis.dart'; |
| 8 import 'dart:io' show File, IOSink; |
| 9 |
| 10 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; |
| 11 |
| 12 import 'package:kernel/ast.dart'; |
| 13 |
| 14 import 'package:kernel/verifier.dart'; |
| 15 import 'package:kernel/text/ast_to_text.dart' show Printer; |
| 16 |
| 17 import 'package:kernel/repository.dart' show Repository; |
| 18 |
| 19 import 'package:kernel/binary/loader.dart' show BinaryLoader; |
| 20 |
| 21 import 'transformation/remove_generics.dart'; |
| 22 import 'transformation/transformer.dart' |
| 23 show ReifyVisitor, RuntimeLibrary, RuntimeTypeSupportBuilder; |
| 24 |
| 25 import 'package:kernel/core_types.dart' show CoreTypes; |
| 26 |
| 27 RuntimeLibrary findRuntimeTypeLibrary(Program p) { |
| 28 Library findLibraryEndingWith(String postfix) { |
| 29 Iterable<Library> candidates = p.libraries.where((Library l) { |
| 30 return l.importUri.toString().endsWith(postfix); |
| 31 }); |
| 32 if (candidates.length != 1) { |
| 33 String howMany = candidates.isEmpty ? "No" : "Multiple"; |
| 34 throw new Exception( |
| 35 "$howMany candidates for runtime support library found."); |
| 36 } |
| 37 return candidates.single; |
| 38 } |
| 39 |
| 40 Library types = findLibraryEndingWith("reify/types.dart"); |
| 41 Library declarations = findLibraryEndingWith("reify/declarations.dart"); |
| 42 Library interceptors = findLibraryEndingWith("reify/interceptors.dart"); |
| 43 return new RuntimeLibrary(types, declarations, interceptors); |
| 44 } |
| 45 |
| 46 Program transformProgramUsingLibraries( |
| 47 Program program, RuntimeLibrary runtimeLibrary, |
| 48 [Library libraryToTransform]) { |
| 49 LibraryFilter filter = libraryToTransform != null |
| 50 ? (Library library) => library == libraryToTransform |
| 51 : (_) => true; |
| 52 ProgramKnowledge knowledge = analyze(program, analyzeLibrary: filter); |
| 53 Library mainLibrary = program.mainMethod.parent; |
| 54 RuntimeTypeSupportBuilder builder = new RuntimeTypeSupportBuilder( |
| 55 runtimeLibrary, new CoreTypes(program), mainLibrary); |
| 56 ReifyVisitor transformer = |
| 57 new ReifyVisitor(runtimeLibrary, builder, knowledge, libraryToTransform); |
| 58 // Transform the main program. |
| 59 program = program.accept(transformer); |
| 60 if (!filter(runtimeLibrary.interceptorsLibrary)) { |
| 61 // We need to transform the interceptor function in any case to make sure |
| 62 // that the type literals in the interceptor function are rewritten. |
| 63 runtimeLibrary.interceptorFunction.accept(transformer); |
| 64 } |
| 65 builder.createDeclarations(); |
| 66 program = program.accept(new Erasure(transformer)); |
| 67 // TODO(karlklose): skip checks in debug mode |
| 68 verifyProgram(program); |
| 69 return program; |
| 70 } |
| 71 |
| 72 Program transformProgram(Program program) { |
| 73 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); |
| 74 Library mainLibrary = program.mainMethod.enclosingLibrary; |
| 75 return transformProgramUsingLibraries(program, runtimeLibrary, mainLibrary); |
| 76 } |
| 77 |
| 78 main(List<String> arguments) async { |
| 79 String path = arguments.first; |
| 80 Uri output; |
| 81 if (arguments.length > 1) { |
| 82 output = Uri.base.resolve(arguments[1]); |
| 83 } |
| 84 Uri uri = Uri.base.resolve(path); |
| 85 Repository repository = new Repository(); |
| 86 Program program = new BinaryLoader(repository).loadProgram(uri.toFilePath()); |
| 87 |
| 88 RuntimeLibrary runtimeLibrary = findRuntimeTypeLibrary(program); |
| 89 Library mainLibrary = program.mainMethod.enclosingLibrary; |
| 90 program = |
| 91 transformProgramUsingLibraries(program, runtimeLibrary, mainLibrary); |
| 92 |
| 93 if (output == null) { |
| 94 // Print result |
| 95 StringBuffer sb = new StringBuffer(); |
| 96 Printer printer = new Printer(sb); |
| 97 printer.writeLibraryFile(mainLibrary); |
| 98 print("$sb"); |
| 99 } else { |
| 100 IOSink sink = new File.fromUri(output).openWrite(); |
| 101 try { |
| 102 new BinaryPrinter(sink).writeProgramFile(program); |
| 103 } finally { |
| 104 await sink.close(); |
| 105 } |
| 106 try { |
| 107 // Check that we can read the binary file. |
| 108 new BinaryLoader(new Repository()).loadProgram(output.toFilePath()); |
| 109 } catch (e) { |
| 110 print("Error when attempting to read $output."); |
| 111 rethrow; |
| 112 } |
| 113 } |
| 114 } |
OLD | NEW |