Skip to content

Commit a3e0b63

Browse files
committed
Reflection: Split off TypeRefBuilder.h from ReflectionContext.h, NFC
ReflectionContext is getting too large, and having to thread a <Remote> template parameter through all code that wants to construct typerefs is getting tricky. This is the first patch in a refactoring to move some stuff out of ReflectionContext.
1 parent 67e031c commit a3e0b63

File tree

2 files changed

+197
-162
lines changed

2 files changed

+197
-162
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 1 addition & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -22,180 +22,19 @@
2222
#include "swift/Remote/MetadataReader.h"
2323
#include "swift/Reflection/Records.h"
2424
#include "swift/Reflection/TypeRef.h"
25+
#include "swift/Reflection/TypeRefBuilder.h"
2526
#include "swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h"
2627

2728
#include <iostream>
2829
#include <vector>
2930
#include <unordered_map>
3031

31-
class NodePointer;
32-
3332
namespace swift {
3433
namespace reflection {
3534

3635
using swift::remote::MemoryReader;
3736
using swift::remote::RemoteAddress;
3837

39-
template <typename Iterator>
40-
class ReflectionSection {
41-
using const_iterator = Iterator;
42-
const void * Begin;
43-
const void * End;
44-
45-
public:
46-
ReflectionSection(const void * Begin,
47-
const void * End)
48-
: Begin(Begin), End(End) {}
49-
50-
ReflectionSection(uint64_t Begin, uint64_t End)
51-
: Begin(reinterpret_cast<const void *>(Begin)),
52-
End(reinterpret_cast<const void *>(End)) {}
53-
54-
void *startAddress() {
55-
return const_cast<void *>(Begin);
56-
}
57-
58-
const_iterator begin() const {
59-
return const_iterator(Begin, End);
60-
}
61-
62-
const_iterator end() const {
63-
return const_iterator(End, End);
64-
}
65-
66-
size_t size() const {
67-
return (char *)End - (char *)Begin;
68-
}
69-
};
70-
71-
/// An implementation of MetadataReader's BuilderType concept for
72-
/// building TypeRefs.
73-
class TypeRefBuilder {
74-
public:
75-
using Type = const TypeRef *;
76-
77-
private:
78-
std::vector<std::unique_ptr<const TypeRef>> TypeRefPool;
79-
80-
public:
81-
template <typename TypeRefTy, typename... Args>
82-
TypeRefTy *make_typeref(Args... args) {
83-
auto TR = new TypeRefTy(::std::forward<Args>(args)...);
84-
TypeRefPool.push_back(std::unique_ptr<const TypeRef>(TR));
85-
return TR;
86-
}
87-
88-
const BuiltinTypeRef *createBuiltinType(const std::string &mangledName) {
89-
return BuiltinTypeRef::create(*this, mangledName);
90-
}
91-
92-
const NominalTypeRef *createNominalType(const std::string &mangledName,
93-
const TypeRef *parent) {
94-
return NominalTypeRef::create(*this, mangledName, parent);
95-
}
96-
97-
const BoundGenericTypeRef *
98-
createBoundGenericType(const std::string &mangledName,
99-
const std::vector<const TypeRef *> &args,
100-
const TypeRef *parent) {
101-
return BoundGenericTypeRef::create(*this, mangledName, args, parent);
102-
}
103-
104-
const TupleTypeRef *
105-
createTupleType(const std::vector<const TypeRef *> &elements,
106-
bool isVariadic) {
107-
return TupleTypeRef::create(*this, elements, isVariadic);
108-
}
109-
110-
const FunctionTypeRef *
111-
createFunctionType(const std::vector<const TypeRef *> &args,
112-
const std::vector<bool> &inOutArgs,
113-
const TypeRef *result,
114-
FunctionTypeFlags flags) {
115-
// FIXME: don't ignore inOutArgs
116-
// FIXME: don't ignore flags
117-
return FunctionTypeRef::create(*this, args, result);
118-
}
119-
120-
const ProtocolTypeRef *createProtocolType(const std::string &moduleName,
121-
const std::string &protocolName) {
122-
return ProtocolTypeRef::create(*this, moduleName, protocolName);
123-
}
124-
125-
const ProtocolCompositionTypeRef *
126-
createProtocolCompositionType(const std::vector<const TypeRef*> &protocols) {
127-
for (auto protocol : protocols) {
128-
if (!isa<ProtocolTypeRef>(protocol))
129-
return nullptr;
130-
}
131-
return ProtocolCompositionTypeRef::create(*this, protocols);
132-
}
133-
134-
const ExistentialMetatypeTypeRef *
135-
createExistentialMetatypeType(const TypeRef *instance) {
136-
return ExistentialMetatypeTypeRef::create(*this, instance);
137-
}
138-
139-
const MetatypeTypeRef *createMetatypeType(const TypeRef *instance) {
140-
return MetatypeTypeRef::create(*this, instance);
141-
}
142-
143-
const GenericTypeParameterTypeRef *
144-
createGenericTypeParameterType(unsigned depth, unsigned index) {
145-
return GenericTypeParameterTypeRef::create(*this, depth, index);
146-
}
147-
148-
const DependentMemberTypeRef *
149-
createDependentMemberType(const std::string &member,
150-
const TypeRef *base,
151-
const TypeRef *protocol) {
152-
if (!isa<ProtocolTypeRef>(protocol))
153-
return nullptr;
154-
return DependentMemberTypeRef::create(*this, member, base, protocol);
155-
}
156-
157-
const UnownedStorageTypeRef *createUnownedStorageType(const TypeRef *base) {
158-
return UnownedStorageTypeRef::create(*this, base);
159-
}
160-
161-
const UnmanagedStorageTypeRef *
162-
createUnmanagedStorageType(const TypeRef *base) {
163-
return UnmanagedStorageTypeRef::create(*this, base);
164-
}
165-
166-
const WeakStorageTypeRef *createWeakStorageType(const TypeRef *base) {
167-
return WeakStorageTypeRef::create(*this, base);
168-
}
169-
170-
const ObjCClassTypeRef *getUnnamedObjCClassType() {
171-
return ObjCClassTypeRef::getUnnamed();
172-
}
173-
174-
const ForeignClassTypeRef *getUnnamedForeignClassType() {
175-
return ForeignClassTypeRef::getUnnamed();
176-
}
177-
178-
const OpaqueTypeRef *getOpaqueType() {
179-
return OpaqueTypeRef::get();
180-
}
181-
};
182-
183-
using FieldSection = ReflectionSection<FieldDescriptorIterator>;
184-
using AssociatedTypeSection = ReflectionSection<AssociatedTypeIterator>;
185-
using BuiltinTypeSection = ReflectionSection<BuiltinTypeDescriptorIterator>;
186-
using GenericSection = ReflectionSection<const void *>;
187-
188-
struct ReflectionInfo {
189-
std::string ImageName;
190-
FieldSection fieldmd;
191-
AssociatedTypeSection assocty;
192-
BuiltinTypeSection builtin;
193-
GenericSection typeref;
194-
GenericSection reflstr;
195-
};
196-
197-
198-
19938
template <typename Runtime>
20039
class ReflectionContext
20140
: public remote::MetadataReader<Runtime, TypeRefBuilder> {
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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

Comments
 (0)