mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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
106 lines
2.5 KiB
C++
106 lines
2.5 KiB
C++
//===--- MetadataSource.cpp - 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/Reflection/MetadataSource.h"
|
|
|
|
#include <sstream>
|
|
|
|
using namespace swift;
|
|
using namespace reflection;
|
|
|
|
class PrintMetadataSource
|
|
: public MetadataSourceVisitor<PrintMetadataSource, void> {
|
|
std::ostream &OS;
|
|
unsigned Indent;
|
|
|
|
std::ostream &indent(unsigned Amount) {
|
|
for (unsigned i = 0; i < Amount; ++i)
|
|
OS << ' ';
|
|
return OS;
|
|
}
|
|
|
|
std::ostream &printHeader(std::string Name) {
|
|
indent(Indent) << '(' << Name;
|
|
return OS;
|
|
}
|
|
|
|
template<typename T>
|
|
std::ostream &printField(std::string name, const T &value) {
|
|
if (!name.empty())
|
|
OS << " " << name << "=" << value;
|
|
else
|
|
OS << " " << value;
|
|
return OS;
|
|
}
|
|
|
|
void printRec(const MetadataSource *MS) {
|
|
OS << "\n";
|
|
|
|
Indent += 2;
|
|
visit(MS);
|
|
Indent -= 2;
|
|
}
|
|
|
|
void closeForm() {
|
|
OS << ')';
|
|
}
|
|
|
|
public:
|
|
PrintMetadataSource(std::ostream &OS, unsigned Indent)
|
|
: OS(OS), Indent(Indent) {}
|
|
|
|
void
|
|
visitClosureBindingMetadataSource(const ClosureBindingMetadataSource *CB) {
|
|
printHeader("closure-binding");
|
|
printField("index", CB->getIndex());
|
|
closeForm();
|
|
}
|
|
|
|
void
|
|
visitReferenceCaptureMetadataSource(const ReferenceCaptureMetadataSource *RC){
|
|
printHeader("reference-capture");
|
|
printField("index", RC->getIndex());
|
|
closeForm();
|
|
}
|
|
|
|
void
|
|
visitMetadataCaptureMetadataSource(const MetadataCaptureMetadataSource *MC){
|
|
printHeader("metadata-capture");
|
|
printField("index", MC->getIndex());
|
|
closeForm();
|
|
}
|
|
|
|
void
|
|
visitGenericArgumentMetadataSource(const GenericArgumentMetadataSource *GA) {
|
|
printHeader("generic-argument");
|
|
printField("index", GA->getIndex());
|
|
printRec(GA->getSource());
|
|
closeForm();
|
|
}
|
|
|
|
void
|
|
visitParentMetadataSource(const ParentMetadataSource *P) {
|
|
printHeader("parent-of");
|
|
printRec(P->getChild());
|
|
closeForm();
|
|
}
|
|
};
|
|
|
|
void MetadataSource::dump() const {
|
|
dump(std::cerr, 0);
|
|
}
|
|
|
|
void MetadataSource::dump(std::ostream &OS, unsigned Indent) const {
|
|
PrintMetadataSource(OS, Indent).visit(this);
|
|
OS << std::endl;
|
|
}
|