Skip to content

Commit f267521

Browse files
Merge pull request #6632 from adrian-prantl/28859432
Clean up the constructors of DebugTypeInfo
2 parents f2387e5 + 6633ae0 commit f267521

File tree

10 files changed

+280
-209
lines changed

10 files changed

+280
-209
lines changed

lib/IDE/TypeReconstruction.cpp

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,45 @@ static void VisitNodeFunction(
13271327
}
13281328
}
13291329

1330+
static void CreateFunctionType(ASTContext *ast,
1331+
const VisitNodeResult &arg_type_result,
1332+
const VisitNodeResult &return_type_result,
1333+
bool throws,
1334+
VisitNodeResult &result) {
1335+
Type arg_clang_type;
1336+
Type return_clang_type;
1337+
1338+
switch (arg_type_result._types.size()) {
1339+
case 0:
1340+
arg_clang_type = TupleType::getEmpty(*ast);
1341+
break;
1342+
case 1:
1343+
arg_clang_type = arg_type_result._types.front().getPointer();
1344+
break;
1345+
default:
1346+
result._error = "too many argument types for a function type";
1347+
break;
1348+
}
1349+
1350+
switch (return_type_result._types.size()) {
1351+
case 0:
1352+
return_clang_type = TupleType::getEmpty(*ast);
1353+
break;
1354+
case 1:
1355+
return_clang_type = return_type_result._types.front().getPointer();
1356+
break;
1357+
default:
1358+
result._error = "too many return types for a function type";
1359+
break;
1360+
}
1361+
1362+
if (arg_clang_type && return_clang_type) {
1363+
result._types.push_back(
1364+
FunctionType::get(arg_clang_type, return_clang_type,
1365+
FunctionType::ExtInfo().withThrows(throws)));
1366+
}
1367+
}
1368+
13301369
static void VisitNodeFunctionType(
13311370
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
13321371
Demangle::NodePointer &cur_node, VisitNodeResult &result,
@@ -1364,40 +1403,52 @@ static void VisitNodeFunctionType(
13641403
break;
13651404
}
13661405
}
1367-
Type arg_clang_type;
1368-
Type return_clang_type;
1369-
1370-
switch (arg_type_result._types.size()) {
1371-
case 0:
1372-
arg_clang_type = TupleType::getEmpty(*ast);
1373-
break;
1374-
case 1:
1375-
arg_clang_type = arg_type_result._types.front().getPointer();
1376-
break;
1377-
default:
1378-
result._error = "too many argument types for a function type";
1379-
break;
1380-
}
1381-
1382-
switch (return_type_result._types.size()) {
1383-
case 0:
1384-
return_clang_type = TupleType::getEmpty(*ast);
1385-
break;
1386-
case 1:
1387-
return_clang_type = return_type_result._types.front().getPointer();
1388-
break;
1389-
default:
1390-
result._error = "too many return types for a function type";
1391-
break;
1392-
}
1406+
CreateFunctionType(ast, arg_type_result, return_type_result, throws, result);
1407+
}
13931408

1394-
if (arg_clang_type && return_clang_type) {
1395-
result._types.push_back(
1396-
FunctionType::get(arg_clang_type, return_clang_type,
1397-
FunctionType::ExtInfo().withThrows(throws)));
1409+
static void VisitNodeImplFunctionType(
1410+
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
1411+
Demangle::NodePointer &cur_node, VisitNodeResult &result,
1412+
const VisitNodeResult &generic_context) { // set by GenericType case
1413+
VisitNodeResult arg_type_result;
1414+
VisitNodeResult return_type_result;
1415+
Demangle::Node::iterator end = cur_node->end();
1416+
bool throws = false;
1417+
for (Demangle::Node::iterator pos = cur_node->begin(); pos != end; ++pos) {
1418+
const Demangle::Node::Kind child_node_kind = (*pos)->getKind();
1419+
switch (child_node_kind) {
1420+
case Demangle::Node::Kind::Class: {
1421+
VisitNodeResult class_type_result;
1422+
nodes.push_back(*pos);
1423+
VisitNode(ast, nodes, class_type_result, generic_context);
1424+
} break;
1425+
case Demangle::Node::Kind::Structure: {
1426+
VisitNodeResult class_type_result;
1427+
nodes.push_back(*pos);
1428+
VisitNode(ast, nodes, class_type_result, generic_context);
1429+
} break;
1430+
case Demangle::Node::Kind::ImplConvention:
1431+
// Ignore the ImplConvention it is only a hint for the SIL ARC optimizer.
1432+
break;
1433+
case Demangle::Node::Kind::ImplParameter:
1434+
nodes.push_back(*pos);
1435+
VisitNode(ast, nodes, arg_type_result, generic_context);
1436+
break;
1437+
case Demangle::Node::Kind::ThrowsAnnotation:
1438+
throws = true;
1439+
break;
1440+
case Demangle::Node::Kind::ImplResult:
1441+
nodes.push_back(*pos);
1442+
VisitNode(ast, nodes, return_type_result, generic_context);
1443+
break;
1444+
default:
1445+
break;
1446+
}
13981447
}
1448+
CreateFunctionType(ast, arg_type_result, return_type_result, throws, result);
13991449
}
14001450

1451+
14011452
static void VisitNodeSetterGetter(
14021453
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
14031454
Demangle::NodePointer &cur_node, VisitNodeResult &result,
@@ -2037,6 +2088,10 @@ static void visitNodeImpl(
20372088
VisitNodeFunctionType(ast, nodes, node, result, genericContext);
20382089
break;
20392090

2091+
case Demangle::Node::Kind::ImplFunctionType:
2092+
VisitNodeImplFunctionType(ast, nodes, node, result, genericContext);
2093+
break;
2094+
20402095
case Demangle::Node::Kind::DidSet:
20412096
case Demangle::Node::Kind::Getter:
20422097
case Demangle::Node::Kind::Setter:

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 68 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,28 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "DebugTypeInfo.h"
19-
#include "IRGen.h"
2019
#include "FixedTypeInfo.h"
20+
#include "IRGen.h"
21+
#include "swift/SIL/SILGlobalVariable.h"
2122
#include "llvm/Support/Debug.h"
2223
#include "llvm/Support/raw_ostream.h"
2324

2425
using namespace swift;
2526
using namespace irgen;
2627

27-
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
28-
uint64_t SizeInBytes, uint32_t AlignInBytes,
29-
DeclContext *DC)
30-
: DeclCtx(DC), Type(Ty.getPointer()), StorageType(StorageTy),
31-
size(SizeInBytes), align(AlignInBytes) {
32-
assert(StorageType && "StorageType is a nullptr");
33-
assert(align.getValue() != 0);
34-
}
35-
36-
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy, Size size,
37-
Alignment align, DeclContext *DC)
38-
: DeclCtx(DC), Type(Ty.getPointer()), StorageType(StorageTy),
39-
size(size), align(align) {
28+
DebugTypeInfo::DebugTypeInfo(DeclContext *DC, swift::Type Ty,
29+
llvm::Type *StorageTy, Size size, Alignment align)
30+
: DeclCtx(DC), Type(Ty.getPointer()), StorageType(StorageTy), size(size),
31+
align(align) {
32+
assert((!isArchetype() || (isArchetype() && DC)) &&
33+
"archetype without a declcontext");
4034
assert(StorageType && "StorageType is a nullptr");
4135
assert(align.getValue() != 0);
4236
}
4337

44-
static void
45-
initFromTypeInfo(Size &size, Alignment &align, llvm::Type *&StorageType,
46-
const TypeInfo &Info) {
47-
StorageType = Info.getStorageType();
38+
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(DeclContext *DC, swift::Type Ty,
39+
const TypeInfo &Info) {
40+
Size size;
4841
if (Info.isFixedSize()) {
4942
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
5043
size = FixTy.getFixedSize();
@@ -53,50 +46,20 @@ initFromTypeInfo(Size &size, Alignment &align, llvm::Type *&StorageType,
5346
// encounter one.
5447
size = Size(0);
5548
}
56-
align = Info.getBestKnownAlignment();
57-
assert(align.getValue() != 0);
58-
assert(StorageType && "StorageType is a nullptr");
59-
}
60-
61-
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, const TypeInfo &Info,
62-
DeclContext *DC)
63-
: DeclCtx(DC), Type(Ty.getPointer()) {
64-
initFromTypeInfo(size, align, StorageType, Info);
49+
return DebugTypeInfo(DC, Ty.getPointer(), Info.getStorageType(), size,
50+
Info.getBestKnownAlignment());
6551
}
6652

67-
DebugTypeInfo::DebugTypeInfo(TypeDecl *Decl, const TypeInfo &Info)
68-
: DeclCtx(Decl->getDeclContext()) {
69-
// Use the sugared version of the type, if there is one.
70-
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
71-
Type = AliasDecl->getDeclaredInterfaceType().getPointer();
72-
else
73-
Type = Decl->getInterfaceType().getPointer();
74-
75-
initFromTypeInfo(size, align, StorageType, Info);
76-
}
77-
78-
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, llvm::Type *StorageTy, Size size,
79-
Alignment align)
80-
: DeclCtx(Decl->getDeclContext()), StorageType(StorageTy), size(size),
81-
align(align) {
82-
// Use the sugared version of the type, if there is one.
83-
if (auto AliasDecl = dyn_cast<TypeAliasDecl>(Decl))
84-
Type = AliasDecl->getDeclaredInterfaceType().getPointer();
85-
else
86-
Type = Decl->getInterfaceType().getPointer();
87-
88-
assert(StorageType && "StorageType is a nullptr");
89-
assert(align.getValue() != 0);
90-
}
53+
DebugTypeInfo DebugTypeInfo::getLocalVariable(DeclContext *DeclCtx,
54+
VarDecl *Decl, swift::Type Ty,
55+
const TypeInfo &Info,
56+
bool Unwrap) {
9157

92-
DebugTypeInfo::DebugTypeInfo(VarDecl *Decl, swift::Type Ty,
93-
const TypeInfo &Info, bool Unwrap)
94-
: DeclCtx(Decl->getDeclContext()) {
95-
// Prefer the original, potentially sugared version of the type if
96-
// the type hasn't been mucked with by an optimization pass.
97-
auto DeclType = (Decl->hasType()
98-
? Decl->getType()
99-
: DeclCtx->mapTypeIntoContext(Decl->getInterfaceType()));
58+
auto DeclType = Ty;
59+
if (DeclCtx)
60+
DeclType = (Decl->hasType()
61+
? Decl->getType()
62+
: DeclCtx->mapTypeIntoContext(Decl->getInterfaceType()));
10063
auto RealType = Ty;
10164
if (Unwrap) {
10265
DeclType = DeclType->getInOutObjectType();
@@ -108,27 +71,51 @@ DebugTypeInfo::DebugTypeInfo(VarDecl *Decl, swift::Type Ty,
10871
if (auto DynSelfTy = DeclType->getAs<DynamicSelfType>())
10972
DeclSelfType = DynSelfTy->getSelfType();
11073

111-
if (DeclSelfType->isEqual(RealType) || DeclType->getAs<FunctionType>())
112-
Type = DeclType.getPointer();
113-
else
114-
Type = RealType.getPointer();
74+
// Prefer the original, potentially sugared version of the type if
75+
// the type hasn't been mucked with by an optimization pass.
76+
auto *Type = DeclSelfType->isEqual(RealType) ? DeclType.getPointer()
77+
: RealType.getPointer();
78+
return getFromTypeInfo(DeclCtx, Type, Info);
79+
}
11580

116-
initFromTypeInfo(size, align, StorageType, Info);
81+
DebugTypeInfo DebugTypeInfo::getMetadata(swift::Type Ty, llvm::Type *StorageTy,
82+
Size size, Alignment align) {
83+
DebugTypeInfo DbgTy = {nullptr, Ty.getPointer(), StorageTy, size, align};
84+
assert(!DbgTy.isArchetype() && "type metadata cannot contain an archetype");
85+
return DbgTy;
11786
}
11887

119-
DebugTypeInfo::DebugTypeInfo(VarDecl *Decl, swift::Type Ty,
120-
llvm::Type *StorageTy, Size size, Alignment align)
121-
: DeclCtx(Decl->getDeclContext()), StorageType(StorageTy), size(size),
122-
align(align) {
88+
DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
89+
llvm::Type *StorageTy, Size size,
90+
Alignment align) {
12391
// Prefer the original, potentially sugared version of the type if
12492
// the type hasn't been mucked with by an optimization pass.
125-
auto DeclType = (Decl->hasType()
126-
? Decl->getType()
127-
: DeclCtx->mapTypeIntoContext(Decl->getInterfaceType()));
128-
if (Ty && Decl->getInterfaceType()->isEqual(Ty))
129-
Type = DeclType.getPointer();
130-
else
131-
Type = Ty.getPointer();
93+
auto LowTy = GV->getLoweredType().getSwiftType();
94+
auto *Type = LowTy.getPointer();
95+
if (auto *Decl = GV->getDecl()) {
96+
auto DeclType =
97+
(Decl->hasType() ? Decl->getType()
98+
: Decl->getDeclContext()->mapTypeIntoContext(
99+
Decl->getInterfaceType()));
100+
if (DeclType->isEqual(LowTy))
101+
Type = DeclType.getPointer();
102+
}
103+
DebugTypeInfo DbgTy = {nullptr, Type, StorageTy, size, align};
104+
assert(StorageTy && "StorageType is a nullptr");
105+
assert(!DbgTy.isArchetype() &&
106+
"type of a global var cannot contain an archetype");
107+
assert(align.getValue() != 0);
108+
return DbgTy;
109+
}
110+
111+
DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
112+
llvm::Type *StorageType, Size size,
113+
Alignment align) {
114+
DebugTypeInfo DbgTy(nullptr, theClass->getInterfaceType().getPointer(),
115+
StorageType, size, align);
116+
assert(!DbgTy.isArchetype() &&
117+
"type of an objc class cannot contain an archetype");
118+
return DbgTy;
132119
}
133120

134121
static bool typesEqual(Type A, Type B) {
@@ -141,7 +128,7 @@ static bool typesEqual(Type A, Type B) {
141128

142129
// Tombstone.
143130
auto Tombstone =
144-
llvm::DenseMapInfo<swift::Type>::getTombstoneKey().getPointer();
131+
llvm::DenseMapInfo<swift::Type>::getTombstoneKey().getPointer();
145132
if ((A.getPointer() == Tombstone) || (B.getPointer() == Tombstone))
146133
return false;
147134

@@ -150,14 +137,11 @@ static bool typesEqual(Type A, Type B) {
150137
}
151138

152139
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
153-
return typesEqual(getType(), T.getType())
154-
&& size == T.size
155-
&& align == T.align;
140+
return typesEqual(getType(), T.getType()) && size == T.size &&
141+
align == T.align;
156142
}
157143

158-
bool DebugTypeInfo::operator!=(DebugTypeInfo T) const {
159-
return !operator==(T);
160-
}
144+
bool DebugTypeInfo::operator!=(DebugTypeInfo T) const { return !operator==(T); }
161145

162146
TypeDecl *DebugTypeInfo::getDecl() const {
163147
if (auto *N = dyn_cast<NominalType>(Type))
@@ -172,8 +156,8 @@ TypeDecl *DebugTypeInfo::getDecl() const {
172156
}
173157

174158
void DebugTypeInfo::dump() const {
175-
llvm::errs() << "[Size " << size.getValue()
176-
<< " Alignment " << align.getValue()<<"] ";
159+
llvm::errs() << "[Size " << size.getValue() << " Alignment "
160+
<< align.getValue() << "] ";
177161

178162
getType()->dump();
179163
if (StorageType) {

0 commit comments

Comments
 (0)