|
| 1 | +//===--- TypeRefBuilder.h - Swift Type Reference Builder --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Implements utilities for constructing TypeRefs and looking up field and |
| 14 | +// enum case types. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_REFLECTION_TYPEREFBUILDER_H |
| 19 | +#define SWIFT_REFLECTION_TYPEREFBUILDER_H |
| 20 | + |
| 21 | +#include "swift/Remote/MetadataReader.h" |
| 22 | +#include "swift/Reflection/Records.h" |
| 23 | +#include "swift/Reflection/TypeRef.h" |
| 24 | + |
| 25 | +#include <iostream> |
| 26 | +#include <vector> |
| 27 | +#include <unordered_map> |
| 28 | + |
| 29 | +class NodePointer; |
| 30 | + |
| 31 | +namespace swift { |
| 32 | +namespace reflection { |
| 33 | + |
| 34 | +template <typename Iterator> |
| 35 | +class ReflectionSection { |
| 36 | + using const_iterator = Iterator; |
| 37 | + const void * Begin; |
| 38 | + const void * End; |
| 39 | + |
| 40 | +public: |
| 41 | + ReflectionSection(const void * Begin, |
| 42 | + const void * End) |
| 43 | + : Begin(Begin), End(End) {} |
| 44 | + |
| 45 | + ReflectionSection(uint64_t Begin, uint64_t End) |
| 46 | + : Begin(reinterpret_cast<const void *>(Begin)), |
| 47 | + End(reinterpret_cast<const void *>(End)) {} |
| 48 | + |
| 49 | + void *startAddress() { |
| 50 | + return const_cast<void *>(Begin); |
| 51 | + } |
| 52 | + |
| 53 | + const_iterator begin() const { |
| 54 | + return const_iterator(Begin, End); |
| 55 | + } |
| 56 | + |
| 57 | + const_iterator end() const { |
| 58 | + return const_iterator(End, End); |
| 59 | + } |
| 60 | + |
| 61 | + size_t size() const { |
| 62 | + return (char *)End - (char *)Begin; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +/// An implementation of MetadataReader's BuilderType concept for |
| 67 | +/// building TypeRefs. |
| 68 | +class TypeRefBuilder { |
| 69 | +public: |
| 70 | + using Type = const TypeRef *; |
| 71 | + |
| 72 | +private: |
| 73 | + std::vector<std::unique_ptr<const TypeRef>> TypeRefPool; |
| 74 | + |
| 75 | +public: |
| 76 | + template <typename TypeRefTy, typename... Args> |
| 77 | + TypeRefTy *make_typeref(Args... args) { |
| 78 | + auto TR = new TypeRefTy(::std::forward<Args>(args)...); |
| 79 | + TypeRefPool.push_back(std::unique_ptr<const TypeRef>(TR)); |
| 80 | + return TR; |
| 81 | + } |
| 82 | + |
| 83 | + const BuiltinTypeRef *createBuiltinType(const std::string &mangledName) { |
| 84 | + return BuiltinTypeRef::create(*this, mangledName); |
| 85 | + } |
| 86 | + |
| 87 | + const NominalTypeRef *createNominalType(const std::string &mangledName, |
| 88 | + const TypeRef *parent) { |
| 89 | + return NominalTypeRef::create(*this, mangledName, parent); |
| 90 | + } |
| 91 | + |
| 92 | + const BoundGenericTypeRef * |
| 93 | + createBoundGenericType(const std::string &mangledName, |
| 94 | + const std::vector<const TypeRef *> &args, |
| 95 | + const TypeRef *parent) { |
| 96 | + return BoundGenericTypeRef::create(*this, mangledName, args, parent); |
| 97 | + } |
| 98 | + |
| 99 | + const TupleTypeRef * |
| 100 | + createTupleType(const std::vector<const TypeRef *> &elements, |
| 101 | + bool isVariadic) { |
| 102 | + return TupleTypeRef::create(*this, elements, isVariadic); |
| 103 | + } |
| 104 | + |
| 105 | + const FunctionTypeRef * |
| 106 | + createFunctionType(const std::vector<const TypeRef *> &args, |
| 107 | + const std::vector<bool> &inOutArgs, |
| 108 | + const TypeRef *result, |
| 109 | + FunctionTypeFlags flags) { |
| 110 | + // FIXME: don't ignore inOutArgs |
| 111 | + // FIXME: don't ignore flags |
| 112 | + return FunctionTypeRef::create(*this, args, result); |
| 113 | + } |
| 114 | + |
| 115 | + const ProtocolTypeRef *createProtocolType(const std::string &moduleName, |
| 116 | + const std::string &protocolName) { |
| 117 | + return ProtocolTypeRef::create(*this, moduleName, protocolName); |
| 118 | + } |
| 119 | + |
| 120 | + const ProtocolCompositionTypeRef * |
| 121 | + createProtocolCompositionType(const std::vector<const TypeRef*> &protocols) { |
| 122 | + for (auto protocol : protocols) { |
| 123 | + if (!isa<ProtocolTypeRef>(protocol)) |
| 124 | + return nullptr; |
| 125 | + } |
| 126 | + return ProtocolCompositionTypeRef::create(*this, protocols); |
| 127 | + } |
| 128 | + |
| 129 | + const ExistentialMetatypeTypeRef * |
| 130 | + createExistentialMetatypeType(const TypeRef *instance) { |
| 131 | + return ExistentialMetatypeTypeRef::create(*this, instance); |
| 132 | + } |
| 133 | + |
| 134 | + const MetatypeTypeRef *createMetatypeType(const TypeRef *instance) { |
| 135 | + return MetatypeTypeRef::create(*this, instance); |
| 136 | + } |
| 137 | + |
| 138 | + const GenericTypeParameterTypeRef * |
| 139 | + createGenericTypeParameterType(unsigned depth, unsigned index) { |
| 140 | + return GenericTypeParameterTypeRef::create(*this, depth, index); |
| 141 | + } |
| 142 | + |
| 143 | + const DependentMemberTypeRef * |
| 144 | + createDependentMemberType(const std::string &member, |
| 145 | + const TypeRef *base, |
| 146 | + const TypeRef *protocol) { |
| 147 | + if (!isa<ProtocolTypeRef>(protocol)) |
| 148 | + return nullptr; |
| 149 | + return DependentMemberTypeRef::create(*this, member, base, protocol); |
| 150 | + } |
| 151 | + |
| 152 | + const UnownedStorageTypeRef *createUnownedStorageType(const TypeRef *base) { |
| 153 | + return UnownedStorageTypeRef::create(*this, base); |
| 154 | + } |
| 155 | + |
| 156 | + const UnmanagedStorageTypeRef * |
| 157 | + createUnmanagedStorageType(const TypeRef *base) { |
| 158 | + return UnmanagedStorageTypeRef::create(*this, base); |
| 159 | + } |
| 160 | + |
| 161 | + const WeakStorageTypeRef *createWeakStorageType(const TypeRef *base) { |
| 162 | + return WeakStorageTypeRef::create(*this, base); |
| 163 | + } |
| 164 | + |
| 165 | + const ObjCClassTypeRef *getUnnamedObjCClassType() { |
| 166 | + return ObjCClassTypeRef::getUnnamed(); |
| 167 | + } |
| 168 | + |
| 169 | + const ForeignClassTypeRef *getUnnamedForeignClassType() { |
| 170 | + return ForeignClassTypeRef::getUnnamed(); |
| 171 | + } |
| 172 | + |
| 173 | + const OpaqueTypeRef *getOpaqueType() { |
| 174 | + return OpaqueTypeRef::get(); |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +using FieldSection = ReflectionSection<FieldDescriptorIterator>; |
| 179 | +using AssociatedTypeSection = ReflectionSection<AssociatedTypeIterator>; |
| 180 | +using BuiltinTypeSection = ReflectionSection<BuiltinTypeDescriptorIterator>; |
| 181 | +using GenericSection = ReflectionSection<const void *>; |
| 182 | + |
| 183 | +struct ReflectionInfo { |
| 184 | + std::string ImageName; |
| 185 | + FieldSection fieldmd; |
| 186 | + AssociatedTypeSection assocty; |
| 187 | + BuiltinTypeSection builtin; |
| 188 | + GenericSection typeref; |
| 189 | + GenericSection reflstr; |
| 190 | +}; |
| 191 | + |
| 192 | + |
| 193 | +} // end namespace reflection |
| 194 | +} // end namespace swift |
| 195 | + |
| 196 | +#endif // SWIFT_REFLECTION_TYPEREFBUILDER_H |
0 commit comments