Skip to content

Commit

Permalink
The type of a temporary for a function argument might be different fr…
Browse files Browse the repository at this point in the history
…om the type of the expression it gets initialized from, so bitcast it.

In functionParameters() arguments get cast to the parameter type, and this cast may be stripped later by optimize() (which simply alters the expression type instead). In the case of the in-place construction of a temporary through a function call with a sret argument the sret argument needs to have the correct type.
  • Loading branch information
Syniurge committed Oct 28, 2019
1 parent 53ebf2e commit f4636ea
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions gen/cpp/cpptoir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,18 @@ DValue* LangPlugin::toCallFunction(Loc& loc, Type* resulttype, DValue* fnval,
Args.add(clangCG::RValue::get(val), ArgTy);
}

auto GD = getGlobalDecl(CGM.get(), FD);
auto& FInfo = CGM->getTypes().arrangeGlobalDeclaration(GD);

if (retvar && FInfo.getReturnInfo().getKind() == clangCG::ABIArgInfo::Indirect)
{
assert(isa<llvm::PointerType>(retvar->getType()));

auto SretTy = DtoAggregateHandleType(fd->type->nextOf());
if (retvar->getType() != SretTy)
retvar = DtoBitCast(retvar, SretTy);
}

clangCG::Address Addr(retvar,
CGF()->getNaturalTypeAlignment(FD->getReturnType()));
clangCG::ReturnValueSlot ReturnValue(Addr, false);
Expand All @@ -834,8 +846,6 @@ DValue* LangPlugin::toCallFunction(Loc& loc, Type* resulttype, DValue* fnval,

updateCGFInsertPoint(); // emitLandingPad() may have run the cleanups and call C++ dtors, hence changing the insert point

auto GD = getGlobalDecl(CGM.get(), FD);
auto &FInfo = CGM->getTypes().arrangeGlobalDeclaration(GD);
clangCG::CGCalleeInfo calleeInfo(FD->getType()->getAs<clang::FunctionProtoType>(), GD);

llvm::CallBase *callOrInvoke;
Expand Down

0 comments on commit f4636ea

Please sign in to comment.