|
19 | 19 | #define SWIFT_REFLECTION_TYPELOWERING_H
|
20 | 20 |
|
21 | 21 | #include "llvm/ADT/DenseMap.h"
|
| 22 | +#include "llvm/Support/Casting.h" |
22 | 23 |
|
23 | 24 | #include <iostream>
|
24 | 25 | #include <memory>
|
25 | 26 |
|
26 | 27 | namespace swift {
|
27 | 28 | namespace reflection {
|
28 | 29 |
|
| 30 | +using llvm::cast; |
| 31 | +using llvm::dyn_cast; |
| 32 | + |
29 | 33 | class TypeRef;
|
30 | 34 | class TypeRefBuilder;
|
31 | 35 |
|
| 36 | +enum class RecordKind : unsigned { |
| 37 | + Tuple, |
| 38 | + Struct, |
| 39 | + ThickFunction, |
| 40 | +}; |
| 41 | + |
| 42 | +enum class ReferenceCounting : unsigned { |
| 43 | + Native, |
| 44 | + Unknown |
| 45 | +}; |
| 46 | + |
| 47 | +enum class ReferenceKind : unsigned { |
| 48 | + Strong, |
| 49 | + Unowned, |
| 50 | + Weak, |
| 51 | + Unmanaged |
| 52 | +}; |
| 53 | + |
32 | 54 | enum class TypeInfoKind : unsigned {
|
33 | 55 | Builtin,
|
34 |
| - Struct, |
35 |
| - Tuple, |
| 56 | + Record, |
| 57 | + Reference, |
36 | 58 | };
|
37 | 59 |
|
38 | 60 | class TypeInfo {
|
@@ -65,32 +87,84 @@ struct FieldInfo {
|
65 | 87 |
|
66 | 88 | /// Class instances, structs, tuples
|
67 | 89 | class RecordTypeInfo : public TypeInfo {
|
| 90 | + RecordKind SubKind; |
68 | 91 | std::vector<FieldInfo> Fields;
|
69 | 92 |
|
70 | 93 | public:
|
71 |
| - RecordTypeInfo(TypeInfoKind Kind, |
72 |
| - unsigned Size, unsigned Alignment, |
| 94 | + RecordTypeInfo(unsigned Size, unsigned Alignment, |
73 | 95 | unsigned Stride, unsigned NumExtraInhabitants,
|
74 |
| - const std::vector<FieldInfo> &Fields) |
75 |
| - : TypeInfo(Kind, Size, Alignment, Stride, NumExtraInhabitants), |
76 |
| - Fields(Fields) {} |
| 96 | + RecordKind SubKind, const std::vector<FieldInfo> &Fields) |
| 97 | + : TypeInfo(TypeInfoKind::Record, Size, Alignment, Stride, |
| 98 | + NumExtraInhabitants), |
| 99 | + SubKind(SubKind), Fields(Fields) {} |
77 | 100 |
|
| 101 | + RecordKind getRecordKind() const { return SubKind; } |
78 | 102 | unsigned getNumFields() const { return Fields.size(); }
|
79 | 103 | const std::vector<FieldInfo> &getFields() const { return Fields; }
|
| 104 | + |
| 105 | + static bool classof(const TypeInfo *TI) { |
| 106 | + return TI->getKind() == TypeInfoKind::Record; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +/// References to classes, closure contexts and anything else with an |
| 111 | +/// 'isa' pointer |
| 112 | +class ReferenceTypeInfo : public TypeInfo { |
| 113 | + ReferenceKind SubKind; |
| 114 | + ReferenceCounting Refcounting; |
| 115 | + |
| 116 | +public: |
| 117 | + ReferenceTypeInfo(unsigned Size, unsigned Alignment, |
| 118 | + unsigned Stride, unsigned NumExtraInhabitants, |
| 119 | + ReferenceKind SubKind, ReferenceCounting Refcounting) |
| 120 | + : TypeInfo(TypeInfoKind::Reference, Size, Alignment, Stride, |
| 121 | + NumExtraInhabitants), |
| 122 | + SubKind(SubKind), Refcounting(Refcounting) {} |
| 123 | + |
| 124 | + ReferenceKind getReferenceKind() const { |
| 125 | + return SubKind; |
| 126 | + } |
| 127 | + |
| 128 | + ReferenceCounting getReferenceCounting() const { |
| 129 | + return Refcounting; |
| 130 | + } |
| 131 | + |
| 132 | + static bool classof(const TypeInfo *TI) { |
| 133 | + return TI->getKind() == TypeInfoKind::Reference; |
| 134 | + } |
80 | 135 | };
|
81 | 136 |
|
82 | 137 | /// This class owns the memory for all TypeInfo instances that it vends.
|
83 | 138 | class TypeConverter {
|
84 | 139 | TypeRefBuilder &Builder;
|
85 | 140 | std::vector<std::unique_ptr<const TypeInfo>> Pool;
|
86 | 141 | llvm::DenseMap<const TypeRef *, const TypeInfo *> Cache;
|
| 142 | + llvm::DenseMap<std::pair<unsigned, unsigned>, |
| 143 | + const ReferenceTypeInfo *> ReferenceCache; |
| 144 | + const TypeInfo *RawPointerTI = nullptr; |
| 145 | + const TypeInfo *ThickFunctionTI = nullptr; |
87 | 146 |
|
88 | 147 | public:
|
89 | 148 | explicit TypeConverter(TypeRefBuilder &Builder) : Builder(Builder) {}
|
90 | 149 |
|
91 | 150 | TypeRefBuilder &getBuilder() { return Builder; }
|
92 | 151 |
|
93 | 152 | const TypeInfo *getTypeInfo(const TypeRef *TR);
|
| 153 | + |
| 154 | + /* Not really public */ |
| 155 | + const ReferenceTypeInfo * |
| 156 | + getReferenceTypeInfo(ReferenceKind Kind, |
| 157 | + ReferenceCounting Refcounting); |
| 158 | + |
| 159 | + const TypeInfo *getRawPointerTypeInfo(); |
| 160 | + const TypeInfo *getThickFunctionTypeInfo(); |
| 161 | + |
| 162 | + template <typename TypeInfoTy, typename... Args> |
| 163 | + const TypeInfoTy *makeTypeInfo(Args... args) { |
| 164 | + auto TI = new TypeInfoTy(::std::forward<Args>(args)...); |
| 165 | + Pool.push_back(std::unique_ptr<const TypeInfo>(TI)); |
| 166 | + return TI; |
| 167 | + } |
94 | 168 | };
|
95 | 169 |
|
96 | 170 | }
|
|
0 commit comments