Files
swift-mirror/lib/IRGen/NecessaryBindings.h
David Farler 09d0cfee8d Hang closure capture descriptors from their heap metadata
Now we can discern the types of values in heap boxes at runtime!
Closure reference captures are a common way of creating reference
cycles, so this provides some basic infrastructure for detecting those
someday.

A closure capture descriptor has the following:

- The number of captures.
- The number of sources of metadata reachable from the closure.
  This is important for substituting generics at runtime since we
  can't know precisely what will get captured until we observe a
  closure.
- The number of types in the NecessaryBindings structure.
  This is a holding tank in a closure for sources of metadata that
  can't be gotten from the captured values themselves.
- The metadata source map, a list of pairs, for each
  source of metadata for every generic argument needed to perform
  substitution at runtime.
  Key: The typeref for the generic parameter visible from the closure
  in the Swift source.
  Value: The metadata source, which describes how to crawl the heap from
  the closure to get to the metadata for that generic argument.
- A list of typerefs for the captured values themselves.

Follow-up: IRGen tests for various capture scenarios, which will include
MetadataSource encoding tests.

rdar://problem/24989531
2016-04-22 19:09:06 -07:00

93 lines
2.9 KiB
C++

//===--- NecessaryBindings.h - Optimizing archetype bindings ----*- C++ -*-===//
//
// 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 defines a utility class for saving and restoring the
// archetype metadata necessary in order to carry out value operations
// on a type.
//
// This is a supplemental API of GenProto.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_NECESSARYBINDINGS_H
#define SWIFT_IRGEN_NECESSARYBINDINGS_H
#include "GenericRequirement.h"
#include "llvm/ADT/SetVector.h"
#include "swift/AST/Types.h"
namespace swift {
class CanType;
class ProtocolDecl;
class ProtocolConformanceRef;
namespace irgen {
class Address;
class IRGenFunction;
class IRGenModule;
class Size;
/// NecessaryBindings - The set of metadata that must be saved in
/// order to perform some set of operations on a type.
class NecessaryBindings {
llvm::SetVector<GenericRequirement> Requirements;
public:
NecessaryBindings() = default;
/// Collect the necessary bindings to invoke a function with the given
/// signature.
static NecessaryBindings forFunctionInvocations(IRGenModule &IGM,
CanSILFunctionType origType,
CanSILFunctionType substType,
ArrayRef<Substitution> subs);
/// Add whatever information is necessary to reconstruct type metadata
/// for the given type.
void addTypeMetadata(CanType type);
/// Get the requirement from the bindings at index i.
const GenericRequirement &operator[](size_t i) const {
return Requirements[i];
}
size_t size() const {
return Requirements.size();
}
/// Add whatever information is necessary to reconstruct a witness table
/// reference for the given type.
void addProtocolConformance(CanType type, ProtocolConformanceRef conf);
/// Is the work to do trivial?
bool empty() const { return Requirements.empty(); }
/// Returns the required size of the bindings.
/// Pointer alignment is sufficient.
Size getBufferSize(IRGenModule &IGM) const;
/// Save the necessary bindings to the given buffer.
void save(IRGenFunction &IGF, Address buffer) const;
/// Restore the necessary bindings from the given buffer.
void restore(IRGenFunction &IGF, Address buffer) const;
const llvm::SetVector<GenericRequirement> &getRequirements() const {
return Requirements;
}
};
} // end namespace irgen
} // end namespace swift
#endif