Skip to content

Commit 2380f9f

Browse files
committed
Re-apply "Reflection: Preliminary implementation of struct and tuple type lowering"
Re-apply now that reflection metadata is emitted by default and the test can actually pass. Oops...
1 parent 98abbdb commit 2380f9f

File tree

8 files changed

+710
-114
lines changed

8 files changed

+710
-114
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//===--- TypeLowering.h - Swift Type Lowering for Reflection ----*- 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 logic for computing in-memory layouts from TypeRefs loaded from
14+
// reflection metadata.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_REFLECTION_TYPELOWERING_H
19+
#define SWIFT_REFLECTION_TYPELOWERING_H
20+
21+
#include "llvm/ADT/DenseMap.h"
22+
23+
#include <iostream>
24+
#include <memory>
25+
26+
namespace swift {
27+
namespace reflection {
28+
29+
class TypeRef;
30+
class TypeRefBuilder;
31+
32+
enum class TypeInfoKind : unsigned {
33+
Builtin,
34+
Struct,
35+
Tuple,
36+
};
37+
38+
class TypeInfo {
39+
TypeInfoKind Kind;
40+
unsigned Size, Alignment, Stride, NumExtraInhabitants;
41+
42+
public:
43+
TypeInfo(TypeInfoKind Kind,
44+
unsigned Size, unsigned Alignment,
45+
unsigned Stride, unsigned NumExtraInhabitants)
46+
: Kind(Kind), Size(Size), Alignment(Alignment), Stride(Stride),
47+
NumExtraInhabitants(NumExtraInhabitants) {}
48+
49+
TypeInfoKind getKind() const { return Kind; }
50+
51+
unsigned getSize() const { return Size; }
52+
unsigned getAlignment() const { return Alignment; }
53+
unsigned getStride() const { return Stride; }
54+
unsigned getNumExtraInhabitants() const { return NumExtraInhabitants; }
55+
56+
void dump() const;
57+
void dump(std::ostream &OS, unsigned Indent = 0) const;
58+
};
59+
60+
struct FieldInfo {
61+
std::string Name;
62+
unsigned Offset;
63+
const TypeInfo &TI;
64+
};
65+
66+
/// Class instances, structs, tuples
67+
class RecordTypeInfo : public TypeInfo {
68+
std::vector<FieldInfo> Fields;
69+
70+
public:
71+
RecordTypeInfo(TypeInfoKind Kind,
72+
unsigned Size, unsigned Alignment,
73+
unsigned Stride, unsigned NumExtraInhabitants,
74+
const std::vector<FieldInfo> &Fields)
75+
: TypeInfo(Kind, Size, Alignment, Stride, NumExtraInhabitants),
76+
Fields(Fields) {}
77+
78+
unsigned getNumFields() const { return Fields.size(); }
79+
const std::vector<FieldInfo> &getFields() const { return Fields; }
80+
};
81+
82+
/// This class owns the memory for all TypeInfo instances that it vends.
83+
class TypeConverter {
84+
TypeRefBuilder &Builder;
85+
std::vector<std::unique_ptr<const TypeInfo>> Pool;
86+
llvm::DenseMap<const TypeRef *, const TypeInfo *> Cache;
87+
88+
public:
89+
explicit TypeConverter(TypeRefBuilder &Builder) : Builder(Builder) {}
90+
91+
TypeRefBuilder &getBuilder() { return Builder; }
92+
93+
const TypeInfo *getTypeInfo(const TypeRef *TR);
94+
};
95+
96+
}
97+
}
98+
99+
#endif

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/Remote/MetadataReader.h"
2222
#include "swift/Reflection/Records.h"
23+
#include "swift/Reflection/TypeLowering.h"
2324
#include "swift/Reflection/TypeRef.h"
2425
#include "llvm/ADT/Optional.h"
2526

@@ -89,13 +90,14 @@ class TypeRefBuilder {
8990
using BuiltType = const TypeRef *;
9091
using BuiltNominalTypeDecl = Optional<std::string>;
9192

92-
TypeRefBuilder() {}
93+
TypeRefBuilder();
9394

9495
TypeRefBuilder(const TypeRefBuilder &other) = delete;
9596
TypeRefBuilder &operator=(const TypeRefBuilder &other) = delete;
9697

9798
private:
9899
std::vector<std::unique_ptr<const TypeRef>> TypeRefPool;
100+
TypeConverter TC;
99101

100102
public:
101103
template <typename TypeRefTy, typename... Args>
@@ -229,6 +231,8 @@ class TypeRefBuilder {
229231
const DependentMemberTypeRef *DependentMember);
230232

231233
public:
234+
TypeConverter &getTypeConverter() { return TC; }
235+
232236
const TypeRef *
233237
getDependentMemberTypeRef(const std::string &MangledTypeName,
234238
const DependentMemberTypeRef *DependentMember);

stdlib/public/Reflection/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_swift_library(swiftReflection IS_STDLIB IS_HOST
22
Demangle.cpp
33
MetadataSource.cpp
44
Remangle.cpp
5+
TypeLowering.cpp
56
TypeRef.cpp
67
TypeRefBuilder.cpp
78
INSTALL_IN_COMPONENT dev)

0 commit comments

Comments
 (0)