Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(919)

Unified Diff: pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Issue 2894283002: Reapply CL: first step for modular support in fasta (Closed)
Patch Set: new changes since first CL Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
index eb5eda79811f23c446c213e572802c438b138cff..a85366dcc3d5f3c787bf808e5522155859a0df34 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_target.dart
@@ -83,6 +83,8 @@ import 'kernel_builder.dart'
TypeVariableBuilder;
import 'verifier.dart' show verifyProgram;
+import 'kernel_outline_shaker.dart'
+ show trimProgram, RetainedDataBuilder, RootsMarker;
class KernelTarget extends TargetImplementation {
/// The [FileSystem] which should be used to access files.
@@ -214,7 +216,8 @@ class KernelTarget extends TargetImplementation {
builder.mixedInType = null;
}
- void handleInputError(InputError error, {bool isFullProgram}) {
+ void handleInputError(InputError error,
+ {bool isFullProgram, bool trimDependencies: false}) {
if (error != null) {
String message = error.format();
print(message);
@@ -256,13 +259,28 @@ class KernelTarget extends TargetImplementation {
return _program;
}
+ /// Build the kernel representation of the program loaded by this target. The
+ /// program will contain full bodies for the code loaded from sources, and
+ /// only references to the code loaded by the [DillTarget], which may or may
+ /// not include method bodies (depending on what was loaded into that target,
+ /// an outline or a full kernel program).
+ ///
+ /// When [trimDependencies] is true, this also runs a tree-shaker that deletes
ahe 2017/05/22 11:49:04 This documentation states what is currently going
Siggi Cherem (dart-lang) 2017/05/22 23:15:48 added a note about it.
+ /// anything from the [DillTarget] that is not needed for the source program,
+ /// this includes function bodies and types that are not reachable.
+ ///
+ /// If [verify], run the default kernel verification on the resulting program.
@override
- Future<Program> buildProgram({bool verify: false}) async {
+ Future<Program> buildProgram(
+ {bool verify: false, bool trimDependencies: false}) async {
if (loader.first == null) return null;
if (errors.isNotEmpty) {
- handleInputError(null, isFullProgram: true);
+ handleInputError(null,
+ isFullProgram: true, trimDependencies: trimDependencies);
+ if (trimDependencies) trimDependenciesInProgram();
return _program;
}
+
try {
await loader.buildBodies();
loader.finishStaticInvocations();
@@ -273,13 +291,16 @@ class KernelTarget extends TargetImplementation {
if (verify) this.verify();
errors.addAll(loader.collectCompileTimeErrors().map((e) => e.format()));
if (errors.isNotEmpty) {
- handleInputError(null, isFullProgram: true);
+ handleInputError(null,
+ isFullProgram: true, trimDependencies: trimDependencies);
}
} on InputError catch (e) {
- handleInputError(e, isFullProgram: true);
+ handleInputError(e,
+ isFullProgram: true, trimDependencies: trimDependencies);
} catch (e, s) {
return reportCrash(e, s, loader?.currentUriForCrashReporting);
}
+ if (trimDependencies) trimDependenciesInProgram();
return _program;
}
@@ -354,7 +375,8 @@ class KernelTarget extends TargetImplementation {
Map<String, Source> uriToSource =
new Map<String, Source>.from(this.uriToSource);
- libraries.addAll(dillTarget.loader.libraries);
+ List<Library> extraLibraries = dillTarget.loader.libraries;
+ libraries.addAll(extraLibraries);
ahe 2017/05/22 11:49:04 Seems like an unnecessary change.
Siggi Cherem (dart-lang) 2017/05/22 23:15:48 Ouch, my apologies. I undid a bunch of other chang
// TODO(scheglov) Should we also somehow update `uriToSource`?
// uriToSource.addAll(binary.uriToSource);
@@ -365,14 +387,13 @@ class KernelTarget extends TargetImplementation {
Program program = new Program(
nameRoot: nameRoot, libraries: libraries, uriToSource: uriToSource);
if (loader.first != null) {
+ // TODO(sigmund): do only for full program
Builder builder = loader.first.lookup("main", -1, null);
if (builder is KernelProcedureBuilder) {
program.mainMethod = builder.procedure;
}
}
- if (errors.isEmpty || dillTarget.isLoaded) {
- runLinkTransformations(program);
- }
+
ticker.logMs("Linked program");
return program;
}
@@ -638,9 +659,6 @@ class KernelTarget extends TargetImplementation {
otherTransformations();
}
- /// Run all transformations that are needed when linking a program.
- void runLinkTransformations(Program program) {}
-
void transformMixinApplications() {
new MixinFullResolution().transform(_program);
ticker.logMs("Transformed mixin applications");
@@ -662,6 +680,21 @@ class KernelTarget extends TargetImplementation {
errors.addAll(verifyErrors.map((error) => '$error'));
ticker.logMs("Verified program");
}
+
+ /// Tree-shakes most code from the [dillTarget] by visiting all other
+ /// libraries in [_program] and marking the APIs from the [dillTarget]
+ /// libraries that are in use.
+ trimDependenciesInProgram() {
+ var toShake =
+ dillTarget.loader.libraries.map((lib) => lib.importUri).toSet();
+ var isIncluded = (Uri uri) => !toShake.contains(uri);
+ var data = new RetainedDataBuilder();
+ // TODO(sigmund): replace this step with data that is directly computed from
+ // the builders: we should know the tree-shaking roots without having to do
+ // a second visit over the tree.
+ new RootsMarker(data).run(_program, isIncluded);
+ trimProgram(_program, data, isIncluded);
+ }
}
/// Looks for a constructor call that matches `super()` from a constructor in

Powered by Google App Engine
This is Rietveld 408576698