<commit_before>//===--- MetadataSource.h - Swift Metadata Sources for Reflection ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements a description of a "metadata source": at runtime, emission of
// metadata pointers that you can directly follow may be omitted as an
// optimization, because the compiler knows you can get to metadata by some
// other means. For example, all heap objects have a pointer to some metadata
// describing it, so pointers to class instances can eventually lead to their
// metadata. These nodes describe those kinds of paths to metadata at runtime.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_REFLECTION_METADATASOURCE_H
#define SWIFT_REFLECTION_METADATASOURCE_H

#include "llvm/ADT/Optional.h"
#include "llvm/Support/Casting.h"

using llvm::cast;

#include <iostream>

namespace swift {
namespace reflection {

enum class MetadataSourceKind {
#define METADATA_SOURCE(Id, Parent) Id,
#include "swift/Reflection/MetadataSources.def"
#undef METADATA_SOURCE
};

class MetadataSource {
  MetadataSourceKind Kind;

  static bool decodeNatural(std::string::const_iterator &it,
                            const std::string::const_iterator &end,
                            unsigned &result) {
    auto begin = it;
    while (it != end) {
      if (*it >= '0' && *it <= '9')
        ++it;
      else
        break;
    }

    std::string natural(begin, it);
    if (natural.empty())
      return false;

    result = std::stoi(natural);
    return true;
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeClosureBinding(Allocator &A,
                       std::string::const_iterator &it,
                       const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'B')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createClosureBinding(Index);
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeReferenceCapture(Allocator &A,
                         std::string::const_iterator &it,
                         const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'R')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createReferenceCapture(Index);
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeMetadataCapture(Allocator &A,
                       std::string::const_iterator &it,
                       const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'M')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createMetadataCapture(Index);
  }

  template <typename Allocator>
  static const MetadataSource *<commit_msg>Reflection: Formatting fix, NFC<commit_after>//===--- MetadataSource.h - Swift Metadata Sources for Reflection ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements a description of a "metadata source": at runtime, emission of
// metadata pointers that you can directly follow may be omitted as an
// optimization, because the compiler knows you can get to metadata by some
// other means. For example, all heap objects have a pointer to some metadata
// describing it, so pointers to class instances can eventually lead to their
// metadata. These nodes describe those kinds of paths to metadata at runtime.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_REFLECTION_METADATASOURCE_H
#define SWIFT_REFLECTION_METADATASOURCE_H

#include "llvm/ADT/Optional.h"
#include "llvm/Support/Casting.h"

using llvm::cast;

#include <iostream>

namespace swift {
namespace reflection {

enum class MetadataSourceKind {
#define METADATA_SOURCE(Id, Parent) Id,
#include "swift/Reflection/MetadataSources.def"
#undef METADATA_SOURCE
};

class MetadataSource {
  MetadataSourceKind Kind;

  static bool decodeNatural(std::string::const_iterator &it,
                            const std::string::const_iterator &end,
                            unsigned &result) {
    auto begin = it;
    while (it != end) {
      if (*it >= '0' && *it <= '9')
        ++it;
      else
        break;
    }

    std::string natural(begin, it);
    if (natural.empty())
      return false;

    result = std::stoi(natural);
    return true;
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeClosureBinding(Allocator &A,
                       std::string::const_iterator &it,
                       const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'B')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createClosureBinding(Index);
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeReferenceCapture(Allocator &A,
                         std::string::const_iterator &it,
                         const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'R')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createReferenceCapture(Index);
  }

  template <typename Allocator>
  static const MetadataSource *
  decodeMetadataCapture(Allocator &A,
                        std::string::const_iterator &it,
                        const std::string::const_iterator &end) {
    if (it == end)
      return nullptr;

    if (*it == 'M')
      ++it;
    else
      return nullptr;

    unsigned Index;
    if (!decodeNatural(it, end, Index))
      return nullptr;
    return A.template createMetadataCapture(Index);
  }

  template <typename Allocator>
  static const MetadataSource *
{'fraction_non_alphanumeric': 0.14547038327526132, 'fraction_numerical': 0.003484320557491289, 'mean_word_length': 2.29302103250478, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, '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': '2966506', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1814, 'n_words': 609}