Skip to content

Commit 0496358

Browse files
committed
Get rid of cycles in DtoType()
Fixes ldc-developers#4734.
1 parent ba9af8d commit 0496358

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

ir/irtype.cpp

+8-27
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,12 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
117117
auto &ctype = getIrType(dt);
118118
assert(!ctype);
119119

120-
LLType *elemType;
121-
unsigned addressSpace = 0;
122-
if (dt->ty == TY::Tnull) {
123-
elemType = llvm::Type::getInt8Ty(getGlobalContext());
124-
} else {
125-
elemType = DtoMemType(dt->nextOf());
126-
if (dt->nextOf()->ty == TY::Tfunction) {
127-
addressSpace = gDataLayout->getProgramAddressSpace();
128-
}
129-
130-
// DtoType could have already created the same type, e.g. for
131-
// dt == Node* in struct Node { Node* n; }.
132-
if (ctype) {
133-
return ctype->isPointer();
134-
}
135-
}
120+
unsigned addressSpace =
121+
dt->ty == TY::Tpointer && dt->nextOf()->ty == TY::Tfunction
122+
? gDataLayout->getProgramAddressSpace()
123+
: 0;
136124

137-
auto t =
138-
new IrTypePointer(dt, llvm::PointerType::get(elemType, addressSpace));
125+
auto t = new IrTypePointer(dt, getOpaquePtrType(addressSpace));
139126
ctype = t;
140127
return t;
141128
}
@@ -173,15 +160,9 @@ IrTypeArray *IrTypeArray::get(Type *dt) {
173160
auto &ctype = getIrType(dt);
174161
assert(!ctype);
175162

176-
LLType *elemType = DtoMemType(dt->nextOf());
177-
178-
// Could have already built the type as part of a struct forward reference,
179-
// just as for pointers.
180-
if (!ctype) {
181-
llvm::Type *types[] = {DtoSize_t(), llvm::PointerType::get(elemType, 0)};
182-
LLType *at = llvm::StructType::get(getGlobalContext(), types, false);
183-
ctype = new IrTypeArray(dt, at);
184-
}
163+
llvm::Type *types[] = {DtoSize_t(), getOpaquePtrType()};
164+
LLType *at = llvm::StructType::get(getGlobalContext(), types, false);
165+
ctype = new IrTypeArray(dt, at);
185166

186167
return ctype->isArray();
187168
}

ir/irtypeaggr.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,18 @@ void AggrTypeBuilder::addAggregate(
212212
// add default type
213213
m_defaultTypes.push_back(llType);
214214

215-
unsigned fieldAlignment, fieldSize;
216215
if (!llType->isSized()) {
217-
// forward reference in a cycle or similar, we need to trust the D type
218-
fieldAlignment = DtoAlignment(vd->type);
219-
fieldSize = af.size;
220-
} else {
221-
fieldAlignment = getABITypeAlign(llType);
222-
fieldSize = getTypeAllocSize(llType);
223-
assert(fieldSize <= af.size);
216+
error(vd->loc,
217+
"unexpected IR type forward declaration for aggregate member of "
218+
"type `%s`. This is an ICE, please file an LDC issue.",
219+
vd->type->toPrettyChars());
220+
fatal();
224221
}
225222

223+
const unsigned fieldAlignment = getABITypeAlign(llType);
224+
const unsigned fieldSize = getTypeAllocSize(llType);
225+
assert(fieldSize <= af.size);
226+
226227
// advance offset to right past this field
227228
if (!m_packed) {
228229
assert(fieldAlignment);

0 commit comments

Comments
 (0)