-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDebugTypeInfo.cpp
186 lines (164 loc) · 5.91 KB
/
DebugTypeInfo.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//===--- DebugTypeInfo.cpp - Type Info for Debugging ----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the data structure that holds all the debug info
// we want to emit for types.
//
//===----------------------------------------------------------------------===//
#include "DebugTypeInfo.h"
#include "IRGen.h"
#include "FixedTypeInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
using namespace irgen;
DebugTypeInfo::DebugTypeInfo(swift::Type Ty,
llvm::Type *StorageTy,
uint64_t SizeInBytes,
uint32_t AlignInBytes,
DeclContext *DC)
: DeclOrContext(DC),
Type(Ty.getPointer()),
StorageType(StorageTy),
size(SizeInBytes),
align(AlignInBytes) {
assert(StorageType && "StorageType is a nullptr");
assert(align.getValue() != 0);
}
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
Size size, Alignment align,
DeclContext *DC)
: DeclOrContext(DC),
Type(Ty.getPointer()),
StorageType(StorageTy),
size(size),
align(align) {
assert(StorageType && "StorageType is a nullptr");
assert(align.getValue() != 0);
}
static void
initFromTypeInfo(Size &size, Alignment &align, llvm::Type *&StorageType,
const TypeInfo &Info) {
StorageType = Info.getStorageType();
if (Info.isFixedSize()) {
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
size = FixTy.getFixedSize();
} else {
// FIXME: Handle NonFixedTypeInfo here or assert that we won't
// encounter one.
size = Size(0);
}
align = Info.getBestKnownAlignment();
assert(align.getValue() != 0);
assert(StorageType && "StorageType is a nullptr");
}
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, const TypeInfo &Info,
DeclContext *DC)
: DeclOrContext(DC),
Type(Ty.getPointer()) {
initFromTypeInfo(size, align, StorageType, Info);
}
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, const TypeInfo &Info)
: DeclOrContext(Decl) {
// Use the sugared version of the type, if there is one.
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
Type = AliasDecl->getAliasType();
else
Type = Decl->getType().getPointer();
initFromTypeInfo(size, align, StorageType, Info);
}
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, llvm::Type *StorageTy,
Size size, Alignment align)
: DeclOrContext(Decl),
StorageType(StorageTy),
size(size),
align(align) {
// Use the sugared version of the type, if there is one.
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
Type = AliasDecl->getAliasType();
else
Type = Decl->getType().getPointer();
assert(StorageType && "StorageType is a nullptr");
assert(align.getValue() != 0);
}
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, swift::Type Ty,
const TypeInfo &Info, bool Unwrap)
: DeclOrContext(Decl) {
// Prefer the original, potentially sugared version of the type if
// the type hasn't been mucked with by an optimization pass.
CanType DeclType = Decl->getType()->getCanonicalType();
CanType RealType = Ty.getCanonicalTypeOrNull();
if (Unwrap) {
DeclType = DeclType.getLValueOrInOutObjectType();
RealType = RealType.getLValueOrInOutObjectType();
}
// Desugar for comparison.
DeclType = DeclType->getDesugaredType()->getCanonicalType();
// DynamicSelfType is also sugar as far as debug info is concerned.
if (auto DynSelfTy = dyn_cast<DynamicSelfType>(DeclType))
DeclType = DynSelfTy->getSelfType()->getCanonicalType();
RealType = RealType->getDesugaredType()->getCanonicalType();
if ((DeclType == RealType) || isa<AnyFunctionType>(DeclType))
Type = Unwrap ? Decl->getType()->getLValueOrInOutObjectType().getPointer()
: Decl->getType().getPointer();
else
Type = RealType.getPointer();
initFromTypeInfo(size, align, StorageType, Info);
}
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, swift::Type Ty,
llvm::Type *StorageTy,
Size size, Alignment align)
: DeclOrContext(Decl),
StorageType(StorageTy),
size(size),
align(align) {
// Prefer the original, potentially sugared version of the type if
// the type hasn't been mucked with by an optimization pass.
if (Decl->getType().getCanonicalTypeOrNull() == Ty.getCanonicalTypeOrNull())
Type = Decl->getType().getPointer();
else
Type = Ty.getPointer();
}
static bool typesEqual(Type A, Type B) {
if (A.getPointer() == B.getPointer())
return true;
// nullptr.
if (A.isNull() || B.isNull())
return false;
// Tombstone.
auto Tombstone =
llvm::DenseMapInfo<swift::Type>::getTombstoneKey().getPointer();
if ((A.getPointer() == Tombstone) || (B.getPointer() == Tombstone))
return false;
// Pointers are safe, do the real comparison.
return A->isSpelledLike(B.getPointer());
}
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
return typesEqual(getType(), T.getType())
&& size == T.size
&& align == T.align;
}
bool DebugTypeInfo::operator!=(DebugTypeInfo T) const {
return !operator==(T);
}
void DebugTypeInfo::dump() const {
llvm::errs() << "[Size " << size.getValue()
<< " Alignment " << align.getValue()<<"] ";
if (getDecl())
getDecl()->dump(llvm::errs());
else
getType()->dump();
if (StorageType) {
llvm::errs() << "StorageType=";
StorageType->dump();
}
}