1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//===--- ClassTypeInfo.h - The layout info for class types. -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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 contains layout information for class types.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_CLASSTYPEINFO_H
#define SWIFT_IRGEN_CLASSTYPEINFO_H
#include "ClassLayout.h"
#include "HeapTypeInfo.h"
#include "swift/ClangImporter/ClangImporterRequests.h"
namespace swift {
namespace irgen {
/// Layout information for class types.
class ClassTypeInfo : public HeapTypeInfo<ClassTypeInfo> {
ClassDecl *TheClass;
mutable llvm::StructType *classLayoutType;
// The resilient layout of the class, without making any assumptions
// that violate resilience boundaries. This is used to allocate
// and deallocate instances of the class, and to access fields.
mutable std::optional<ClassLayout> ResilientLayout;
// A completely fragile layout, used for metadata emission.
mutable std::optional<ClassLayout> FragileLayout;
/// Can we use swift reference-counting, or do we have to use
/// objc_retain/release?
const ReferenceCounting Refcount;
ClassLayout generateLayout(IRGenModule &IGM, SILType classType,
bool forBackwardDeployment) const;
public:
ClassTypeInfo(llvm::PointerType *irType, Size size, SpareBitVector spareBits,
Alignment align, ClassDecl *theClass,
ReferenceCounting refcount, llvm::StructType *classLayoutType)
: HeapTypeInfo(refcount, irType, size, std::move(spareBits), align),
TheClass(theClass), classLayoutType(classLayoutType),
Refcount(refcount) {}
ReferenceCounting getReferenceCounting() const { return Refcount; }
ClassDecl *getClass() const { return TheClass; }
llvm::Type *getClassLayoutType() const { return classLayoutType; }
const ClassLayout &getClassLayout(IRGenModule &IGM, SILType type,
bool forBackwardDeployment) const;
StructLayout *createLayoutWithTailElems(IRGenModule &IGM, SILType classType,
ArrayRef<SILType> tailTypes) const;
void emitScalarRelease(IRGenFunction &IGF, llvm::Value *value,
Atomicity atomicity) const override {
if (getReferenceCounting() == ReferenceCounting::Custom) {
auto releaseFn =
evaluateOrDefault(
getClass()->getASTContext().evaluator,
CustomRefCountingOperation(
{getClass(), CustomRefCountingOperationKind::release}),
{})
.operation;
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value);
return;
}
HeapTypeInfo::emitScalarRelease(IGF, value, atomicity);
}
void emitScalarRetain(IRGenFunction &IGF, llvm::Value *value,
Atomicity atomicity) const override {
if (getReferenceCounting() == ReferenceCounting::Custom) {
auto retainFn =
evaluateOrDefault(
getClass()->getASTContext().evaluator,
CustomRefCountingOperation(
{getClass(), CustomRefCountingOperationKind::retain}),
{})
.operation;
IGF.emitForeignReferenceTypeLifetimeOperation(retainFn, value);
return;
}
HeapTypeInfo::emitScalarRetain(IGF, value, atomicity);
}
void strongCustomRetain(IRGenFunction &IGF, Explosion &e,
bool needsNullCheck) const {
assert(getReferenceCounting() == ReferenceCounting::Custom &&
"only supported for custom ref-counting");
llvm::Value *value = e.claimNext();
auto retainFn =
evaluateOrDefault(
getClass()->getASTContext().evaluator,
CustomRefCountingOperation(
{getClass(), CustomRefCountingOperationKind::retain}),
{})
.operation;
IGF.emitForeignReferenceTypeLifetimeOperation(retainFn, value,
needsNullCheck);
}
// Implement the primary retain/release operations of ReferenceTypeInfo
// using basic reference counting.
void strongRetain(IRGenFunction &IGF, Explosion &e,
Atomicity atomicity) const override {
if (getReferenceCounting() == ReferenceCounting::Custom) {
strongCustomRetain(IGF, e, /*needsNullCheck*/ false);
return;
}
HeapTypeInfo::strongRetain(IGF, e, atomicity);
}
void strongCustomRelease(IRGenFunction &IGF, Explosion &e,
bool needsNullCheck) const {
assert(getReferenceCounting() == ReferenceCounting::Custom &&
"only supported for custom ref-counting");
llvm::Value *value = e.claimNext();
auto releaseFn =
evaluateOrDefault(
getClass()->getASTContext().evaluator,
CustomRefCountingOperation(
{getClass(), CustomRefCountingOperationKind::release}),
{})
.operation;
IGF.emitForeignReferenceTypeLifetimeOperation(releaseFn, value,
needsNullCheck);
}
void strongRelease(IRGenFunction &IGF, Explosion &e,
Atomicity atomicity) const override {
if (getReferenceCounting() == ReferenceCounting::Custom) {
strongCustomRelease(IGF, e, /*needsNullCheck*/ false);
return;
}
HeapTypeInfo::strongRelease(IGF, e, atomicity);
}
};
} // namespace irgen
} // namespace swift
#endif
|