<commit_before>//===--- GenReflection.cpp - IR generation for nominal type 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
//
//===----------------------------------------------------------------------===//
//
//  This file implements IR generation of type metadata for struct/class
//  stored properties and enum cases for use with reflection.
//===----------------------------------------------------------------------===//

#include "swift/AST/ArchetypeBuilder.h"
#include "swift/AST/Decl.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Reflection/Records.h"

#include "ConstantBuilder.h"
#include "IRGenModule.h"

using namespace swift;
using namespace irgen;

class ReflectionMetadataBuilder : public ConstantBuilder<> {
protected:
  void addTypeRef(Module *ModuleContext, CanType type) {
    Mangle::Mangler mangler;
    mangler.setModuleContext(ModuleContext);
    mangler.mangleType(type, 0);
    auto mangledName = IGM.getAddrOfStringForTypeRef(mangler.finalize());
    addRelativeAddress(mangledName);
  }

public:
  ReflectionMetadataBuilder(IRGenModule &IGM) : ConstantBuilder(IGM) {}
};

class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
  static const uint32_t AssociatedTypeRecordSize = 8;
  ArrayRef<const NominalTypeDecl *> NominalTypeDecls;

  void addDecl(const NominalTypeDecl *Decl) {
    for (auto Conformance : Decl->getAllConformances()) {
      SmallVector<std::pair<StringRef, CanType>, 2> AssociatedTypes;

      auto collectTypeWitness = [&](const AssociatedTypeDecl *AssocTy,
                                    const Substitution &Sub,
                                    const TypeDecl *TD) -> bool {

        Type Subst = Sub.getReplacement();
        if (auto InterfaceTy = ArchetypeBuilder::mapTypeOutOfContext(
            Conformance->getDeclContext(), Subst)) {
          Subst = InterfaceTy;
        }

        AssociatedTypes.push_back({
          AssocTy->getNameStr(),
          Subst->getCanonicalType()
        });
        return false;
      };

      auto ModuleContext = Decl->getModuleContext();

      auto ConformingTy = Conformance->getInterfaceType();
      addTypeRef(ModuleContext, ConformingTy->getCanonicalType());

      auto ProtoTy = Conformance->getProtocol()->getInterfaceType();
      addTypeRef(ModuleContext, ProtoTy->getCanonicalType());

      Conformance->forEachTypeWitness(/*resolver*/ nullptr, collectTypeWitness);

      addConstantInt32(AssociatedTypes.size());
      addConstantInt32(AssociatedTypeRecordSize);

      for (auto AssocTy : AssociatedTypes) {
        auto NameGlobal = IGM.getAddrOfStringForTypeRef(AssocTy.first);
        addRelativeAddress(NameGlobal);<commit_msg>Remove unnecessary if auto check - NFC<commit_after>//===--- GenReflection.cpp - IR generation for nominal type 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
//
//===----------------------------------------------------------------------===//
//
//  This file implements IR generation of type metadata for struct/class
//  stored properties and enum cases for use with reflection.
//===----------------------------------------------------------------------===//

#include "swift/AST/ArchetypeBuilder.h"
#include "swift/AST/Decl.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Reflection/Records.h"

#include "ConstantBuilder.h"
#include "IRGenModule.h"

using namespace swift;
using namespace irgen;

class ReflectionMetadataBuilder : public ConstantBuilder<> {
protected:
  void addTypeRef(Module *ModuleContext, CanType type) {
    Mangle::Mangler mangler;
    mangler.setModuleContext(ModuleContext);
    mangler.mangleType(type, 0);
    auto mangledName = IGM.getAddrOfStringForTypeRef(mangler.finalize());
    addRelativeAddress(mangledName);
  }

public:
  ReflectionMetadataBuilder(IRGenModule &IGM) : ConstantBuilder(IGM) {}
};

class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
  static const uint32_t AssociatedTypeRecordSize = 8;
  ArrayRef<const NominalTypeDecl *> NominalTypeDecls;

  void addDecl(const NominalTypeDecl *Decl) {
    for (auto Conformance : Decl->getAllConformances()) {
      SmallVector<std::pair<StringRef, CanType>, 2> AssociatedTypes;

      auto collectTypeWitness = [&](const AssociatedTypeDecl *AssocTy,
                                    const Substitution &Sub,
                                    const TypeDecl *TD) -> bool {

        auto Subst = ArchetypeBuilder::mapTypeOutOfContext(
            Conformance->getDeclContext(), Sub.getReplacement());

        AssociatedTypes.push_back({
          AssocTy->getNameStr(),
          Subst->getCanonicalType()
        });
        return false;
      };

      auto ModuleContext = Decl->getModuleContext();

      auto ConformingTy = Conformance->getInterfaceType();
      addTypeRef(ModuleContext, ConformingTy->getCanonicalType());

      auto ProtoTy = Conformance->getProtocol()->getInterfaceType();
      addTypeRef(ModuleContext, ProtoTy->getCanonicalType());

      Conformance->forEachTypeWitness(/*resolver*/ nullptr, collectTypeWitness);

      addConstantInt32(AssociatedTypes.size());
      addConstantInt32(AssociatedTypeRecordSize);

      for (auto AssocTy : AssociatedTypes) {
        auto NameGlobal = IGM.getAddrOfStringForTypeRef(AssocTy.first);
        addRelativeAddress(NameGlobal);
{'fraction_non_alphanumeric': 0.15221707478491064, 'fraction_numerical': 0.00628722700198544, 'mean_word_length': 2.9202334630350193, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315293', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1583, 'n_words': 384}