diff --git a/gen/toir.cpp b/gen/toir.cpp index c75b5fd0..d038753f 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -3302,6 +3302,36 @@ DValue* RemoveExp::toElem(IRState* p) ////////////////////////////////////////////////////////////////////////////////////////// +/// Constructs an array initializer constant with the given constants as its +/// elements. If the element types differ (unions, …), an anonymous struct +/// literal is emitted (as for array constant initializers). +static llvm::Constant* arrayConst(std::vector& vals, + Type* nominalElemType) +{ + if (vals.size() == 0) + { + llvm::ArrayType* type = llvm::ArrayType::get(DtoType(nominalElemType), 0); + return llvm::ConstantArray::get(type, vals); + } + + llvm::Type* elementType = NULL; + bool differentTypes = false; + for (std::vector::iterator i = vals.begin(), end = vals.end(); + i != end; ++i) + { + if (!elementType) + elementType = (*i)->getType(); + else + differentTypes |= (elementType != (*i)->getType()); + } + + if (differentTypes) + return llvm::ConstantStruct::getAnon(vals, true); + + llvm::ArrayType *t = llvm::ArrayType::get(elementType, vals.size()); + return llvm::ConstantArray::get(t, vals); +} + DValue* AssocArrayLiteralExp::toElem(IRState* p) { Logger::print("AssocArrayLiteralExp::toElem: %s @ %s\n", toChars(), type->toChars()); @@ -3356,16 +3386,16 @@ DValue* AssocArrayLiteralExp::toElem(IRState* p) LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) }; - LLArrayType* arrtype = LLArrayType::get(DtoType(indexType), keys->dim); - LLConstant* initval = LLConstantArray::get(arrtype, keysInits); - LLConstant* globalstore = new LLGlobalVariable(*gIR->module, arrtype, false, LLGlobalValue::InternalLinkage, initval, ".aaKeysStorage"); + LLConstant* initval = arrayConst(keysInits, indexType); + LLConstant* globalstore = new LLGlobalVariable(*gIR->module, initval->getType(), + false, LLGlobalValue::InternalLinkage, initval, ".aaKeysStorage"); LLConstant* slice = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, true); slice = DtoConstSlice(DtoConstSize_t(keys->dim), slice); LLValue* keysArray = DtoAggrPaint(slice, funcTy->getParamType(1)); - arrtype = LLArrayType::get(DtoType(vtype), values->dim); - initval = LLConstantArray::get(arrtype, valuesInits); - globalstore = new LLGlobalVariable(*gIR->module, arrtype, false, LLGlobalValue::InternalLinkage, initval, ".aaValuesStorage"); + initval = arrayConst(valuesInits, vtype); + globalstore = new LLGlobalVariable(*gIR->module, initval->getType(), + false, LLGlobalValue::InternalLinkage, initval, ".aaValuesStorage"); slice = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, true); slice = DtoConstSlice(DtoConstSize_t(keys->dim), slice); LLValue* valuesArray = DtoAggrPaint(slice, funcTy->getParamType(2)); diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index 170f6e3e..07253bd1 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -382,6 +382,10 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym) llvm_unreachable("not global/function"); } + // The logic here should be sound in theory, but as long as the frontend + // keeps inserting templates into wrong modules, this yields to linking + // errors (see e.g. GitHub issue #558). +#if 0 // Check if sym is a nested function and we can declare it as internal. // // Nested naked functions and the implicitly generated __require/__ensure @@ -427,7 +431,7 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym) } } } - +#endif // default to external linkage return llvm::GlobalValue::ExternalLinkage; }