<commit_before>//===--- ProtocolInfo.h - Abstract protocol witness layout ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines types for representing the abstract layout of a
// protocol.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_IRGEN_PROTOCOLINFO_H
#define SWIFT_IRGEN_PROTOCOLINFO_H

#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolAssociations.h"

#include "swift/IRGen/ValueWitness.h"
#include "WitnessIndex.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/TrailingObjects.h"

namespace swift {
  class CanType;
  class ProtocolConformance;

namespace irgen {
  class IRGenModule;
  class TypeInfo;

/// A witness to a specific element of a protocol.  Every
/// ProtocolTypeInfo stores one of these for each requirement
/// introduced by the protocol.
class WitnessTableEntry {
public:
  llvm::PointerUnion<Decl *, TypeBase *> MemberOrAssociatedType;
  ProtocolDecl *Protocol;

  WitnessTableEntry(llvm::PointerUnion<Decl *, TypeBase *> member,
                    ProtocolDecl *protocol)
    : MemberOrAssociatedType(member), Protocol(protocol) {}

public:
  WitnessTableEntry() = default;

  static WitnessTableEntry forOutOfLineBase(ProtocolDecl *proto) {
    assert(proto != nullptr);
    return WitnessTableEntry({}, proto);
  }

  /// Is this a base-protocol entry?
  bool isBase() const { return MemberOrAssociatedType.isNull(); }

  bool matchesBase(ProtocolDecl *proto) const {
    assert(proto != nullptr);
    return MemberOrAssociatedType.isNull() && Protocol == proto;
  }

  /// Given that this is a base-protocol entry, is the table
  /// "out of line"?
  bool isOutOfLineBase() const {
    assert(isBase());
    return true;
  }

  ProtocolDecl *getBase() const {
    assert(isBase());
    return Protocol;
  }

  static WitnessTableEntry forFunction(AbstractFunctionDecl *func) {
    assert(func != nullptr);
    return WitnessTableEntry(func, nullptr);
  }
  
  bool isFunction() const {
    auto decl = MemberOrAssociatedType.get<Decl*>();
    return Protocol == nullptr && isa<AbstractFunctionDecl>(decl);
  }

  bool matchesFunction(AbstractFunctionDecl *func) const {
    assert(func != nullptr);
    if (auto decl = MemberOrAssociatedType.dyn_cast<Decl*>())
      return decl == func && Protocol == nullptr;
    return false;
  }

  AbstractFunctionDecl *getFunction() const {
    assert(isFunction());
    auto decl = MemberOrAssociatedType.get<Decl*>();
    return static_cast<AbstractFunctionDecl*>(decl);
  }

  static WitnessTableEntry forAssociatedType(AssociatedType ty) {
    return WitnessTableEntry(ty.getAssociation(), nullptr);
  }<commit_msg>IRGen: WitnessTableEntry::isFunction() shouldn't crash if its not a function<commit_after>//===--- ProtocolInfo.h - Abstract protocol witness layout ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines types for representing the abstract layout of a
// protocol.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_IRGEN_PROTOCOLINFO_H
#define SWIFT_IRGEN_PROTOCOLINFO_H

#include "swift/AST/Decl.h"
#include "swift/AST/ProtocolAssociations.h"

#include "swift/IRGen/ValueWitness.h"
#include "WitnessIndex.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/TrailingObjects.h"

namespace swift {
  class CanType;
  class ProtocolConformance;

namespace irgen {
  class IRGenModule;
  class TypeInfo;

/// A witness to a specific element of a protocol.  Every
/// ProtocolTypeInfo stores one of these for each requirement
/// introduced by the protocol.
class WitnessTableEntry {
public:
  llvm::PointerUnion<Decl *, TypeBase *> MemberOrAssociatedType;
  ProtocolDecl *Protocol;

  WitnessTableEntry(llvm::PointerUnion<Decl *, TypeBase *> member,
                    ProtocolDecl *protocol)
    : MemberOrAssociatedType(member), Protocol(protocol) {}

public:
  WitnessTableEntry() = default;

  static WitnessTableEntry forOutOfLineBase(ProtocolDecl *proto) {
    assert(proto != nullptr);
    return WitnessTableEntry({}, proto);
  }

  /// Is this a base-protocol entry?
  bool isBase() const { return MemberOrAssociatedType.isNull(); }

  bool matchesBase(ProtocolDecl *proto) const {
    assert(proto != nullptr);
    return MemberOrAssociatedType.isNull() && Protocol == proto;
  }

  /// Given that this is a base-protocol entry, is the table
  /// "out of line"?
  bool isOutOfLineBase() const {
    assert(isBase());
    return true;
  }

  ProtocolDecl *getBase() const {
    assert(isBase());
    return Protocol;
  }

  static WitnessTableEntry forFunction(AbstractFunctionDecl *func) {
    assert(func != nullptr);
    return WitnessTableEntry(func, nullptr);
  }
  
  bool isFunction() const {
    auto decl = MemberOrAssociatedType.dyn_cast<Decl*>();
    return Protocol == nullptr && decl && isa<AbstractFunctionDecl>(decl);
  }

  bool matchesFunction(AbstractFunctionDecl *func) const {
    assert(func != nullptr);
    if (auto decl = MemberOrAssociatedType.dyn_cast<Decl*>())
      return decl == func && Protocol == nullptr;
    return false;
  }

  AbstractFunctionDecl *getFunction() const {
    assert(isFunction());
    auto decl = MemberOrAssociatedType.get<Decl*>();
    return static_cast<AbstractFunctionDecl*>(decl);
  }

  static WitnessTableEntry forAssociatedType(AssociatedType ty) {
    return WitnessTableEntry(ty.getAssociation(), nullptr);
  }
{'fraction_non_alphanumeric': 0.16191381495564006, 'fraction_numerical': 0.0031685678073510772, 'mean_word_length': 3.183565275016567, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, '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': '779307', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1726, 'n_words': 489}