<commit_before>// Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:kernel/ast.dart';
import 'package:kernel/binary/ast_to_binary.dart';

/// Writes libraries that satisfy the [predicate].
///
/// Only the referenced subset of canonical names is indexed and written,
/// so we don't waste time indexing all libraries of a program, when only
/// a tiny subset is used.
class LimitedBinaryPrinter extends BinaryPrinter {
  final LibraryFilter predicate;

  LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate)
      : super(sink, stringIndexer: new ReferencesStringIndexer());

  @override
  void computeCanonicalNames(Program program) {
    for (var library in program.libraries) {
      if (predicate(library)) {
        program.root
            .getChildFromUri(library.importUri)
            .bindTo(library.reference);
        library.computeCanonicalNames();
      }
    }
  }

  @override
  void addCanonicalNamesForLinkTable(List<CanonicalName> list) {
    ReferencesStringIndexer stringIndexer = this.stringIndexer;
    stringIndexer.referencedNames.forEach((name) {
      if (name.index != -1) return;
      name.index = list.length;
      list.add(name);
    });
  }

  @override
  void buildStringIndex(Program program) {
    program.libraries.where(predicate).forEach((library) {
      stringIndexer.scanLibrary(library);
    });
    stringIndexer.finish();
  }

  @override
  bool shouldWriteLibraryCanonicalNames(Library library) {
    return predicate(library);
  }

  @override
  void writeLibraries(Program program) {
    var librariesToWrite = program.libraries.where(predicate).toList();
    writeList(librariesToWrite, writeNode);
  }

  @override
  void writeNode(Node node) {
    if (node is Library && !predicate(node)) return;
    node.accept(this);
  }
}

/// Extension of [StringIndexer] that also indexes canonical names of
/// referenced classes and members.
class ReferencesStringIndexer extends StringIndexer {
  final List<CanonicalName> referencedNames = <CanonicalName>[];

  @override
  defaultMemberReference(Member node) {
    _handleReferencedName(node.canonicalName);
  }

  @override
  visitClassReference(Class node) {
    _handleReferencedName(node.canonicalName);
  }

  @override
  visitMethodInvocation(MethodInvocation node) {
    _handleReferencedName(node.interfaceTargetReference?.canonicalName);
    return super.visitMethodInvocation(node);
  }

  @override
  visitPropertyGet(PropertyGet node) {
    _handleReferencedName(node.interfaceTargetReference?.canonicalName);<commit_msg>[kernel] Fix front_end after previous commit<commit_after>// Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:kernel/ast.dart';
import 'package:kernel/binary/ast_to_binary.dart';

/// Writes libraries that satisfy the [predicate].
///
/// Only the referenced subset of canonical names is indexed and written,
/// so we don't waste time indexing all libraries of a program, when only
/// a tiny subset is used.
class LimitedBinaryPrinter extends BinaryPrinter {
  final LibraryFilter predicate;

  LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate)
      : super(sink, stringIndexer: new ReferencesStringIndexer());

  @override
  void computeCanonicalNames(Program program) {
    for (var library in program.libraries) {
      if (predicate(library)) {
        program.root
            .getChildFromUri(library.importUri)
            .bindTo(library.reference);
        library.computeCanonicalNames();
      }
    }
  }

  @override
  void addCanonicalNamesForLinkTable(List<CanonicalName> list) {
    ReferencesStringIndexer stringIndexer = this.stringIndexer;
    stringIndexer.referencedNames.forEach((name) {
      if (name.index != -1) return;
      name.index = list.length;
      list.add(name);
    });
  }

  @override
  void buildStringIndex(Program program) {
    program.libraries.where(predicate).forEach((library) {
      stringIndexer.scanLibrary(library);
    });
    stringIndexer.finish();
  }

  @override
  bool shouldWriteLibraryCanonicalNames(Library library) {
    return predicate(library);
  }

  @override
  void writeLibraries(Program program) {
    var librariesToWrite = program.libraries.where(predicate).toList();
    writeList(librariesToWrite, writeNode);
  }

  @override
  void writeNode(Node node) {
    if (node is Library && !predicate(node)) return;
    node.accept(this);
  }

  @override
  void writeProgramIndex(Program program, List<Library> libraries) {
    var librariesToWrite = libraries.where(predicate).toList();
    super.writeProgramIndex(program, librariesToWrite);
  }
}

/// Extension of [StringIndexer] that also indexes canonical names of
/// referenced classes and members.
class ReferencesStringIndexer extends StringIndexer {
  final List<CanonicalName> referencedNames = <CanonicalName>[];

  @override
  defaultMemberReference(Member node) {
    _handleReferencedName(node.canonicalName);
  }

  @override
  visitClassReference(Class node) {
    _handleReferencedName(node.canonicalName);
  }

  @override
  visitMethodInvocation(MethodInvocation node) {
    _handleReferencedName(node.interfaceTargetReference?.canonicalName);
    return super.visitMethodInvocation(node);
  }

  @override
  visitPropertyGet(PropertyGet node) {
    _handleReferencedName(node.interfaceTargetReference?.canonicalName);
{'fraction_non_alphanumeric': 0.09719527253483859, 'fraction_numerical': 0.0017639795378373612, 'mean_word_length': 4.053475935828877, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '407789', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1567, 'n_words': 399}