Skip to content

Commit 8e292da

Browse files
committed
[SILGen] Factor out an emitPointerToPointer helper.
Groundwork for SE-0055: Making pointer nullability explicit.
1 parent b0334cb commit 8e292da

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,30 @@ SILGenFunction::OpaqueValueRAII::~OpaqueValueRAII() {
374374
Self.OpaqueValues.erase(entry);
375375
}
376376

377+
RValue
378+
SILGenFunction::emitPointerToPointer(SILLocation loc,
379+
ManagedValue input,
380+
CanType inputType,
381+
CanType outputType,
382+
SGFContext C) {
383+
auto converter = getASTContext().getConvertPointerToPointerArgument(nullptr);
384+
385+
// The generic function currently always requires indirection, but pointers
386+
// are always loadable.
387+
auto origBuf = emitTemporaryAllocation(loc, input.getType());
388+
B.createStore(loc, input.forward(*this), origBuf);
389+
auto origValue = emitManagedBufferWithCleanup(origBuf);
390+
391+
// Invoke the conversion intrinsic to convert to the destination type.
392+
Substitution subs[2] = {
393+
getPointerSubstitution(inputType),
394+
getPointerSubstitution(outputType),
395+
};
396+
397+
return emitApplyOfLibraryIntrinsic(loc, converter, subs, origValue, C);
398+
}
399+
400+
377401
namespace {
378402

379403
/// This is an initialization for an address-only existential in memory.

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3364,21 +3364,12 @@ RValue RValueEmitter::visitPointerToPointerExpr(PointerToPointerExpr *E,
33643364
// expected level.
33653365
AbstractionPattern origTy(converter->getType()->castTo<AnyFunctionType>()
33663366
->getInput());
3367-
auto &origTL = SGF.getTypeLowering(origTy, E->getSubExpr()->getType());
3367+
CanType inputTy = E->getSubExpr()->getType()->getCanonicalType();
3368+
auto &origTL = SGF.getTypeLowering(origTy, inputTy);
33683369
ManagedValue orig = SGF.emitRValueAsOrig(E->getSubExpr(), origTy, origTL);
3369-
// The generic function currently always requires indirection, but pointers
3370-
// are always loadable.
3371-
auto origBuf = SGF.emitTemporaryAllocation(E, orig.getType());
3372-
SGF.B.createStore(E, orig.forward(SGF), origBuf);
3373-
orig = SGF.emitManagedBufferWithCleanup(origBuf);
3374-
3375-
// Invoke the conversion intrinsic to convert to the destination type.
3376-
Substitution subs[2] = {
3377-
SGF.getPointerSubstitution(E->getSubExpr()->getType()),
3378-
SGF.getPointerSubstitution(E->getType()),
3379-
};
3380-
3381-
return SGF.emitApplyOfLibraryIntrinsic(E, converter, subs, orig, C);
3370+
3371+
CanType outputTy = E->getType()->getCanonicalType();
3372+
return SGF.emitPointerToPointer(E, orig, inputTy, outputTy, C);
33823373
}
33833374

33843375
RValue RValueEmitter::visitForeignObjectConversionExpr(

lib/SILGen/SILGenFunction.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,14 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
871871
SILType loweredResultTy,
872872
const ValueTransform &transform);
873873

874+
/// Emit a reinterpret-cast from one pointer type to another, using a library
875+
/// intrinsic.
876+
RValue emitPointerToPointer(SILLocation loc,
877+
ManagedValue input,
878+
CanType inputTy,
879+
CanType outputTy,
880+
SGFContext C);
881+
874882
ManagedValue emitClassMetatypeToObject(SILLocation loc,
875883
ManagedValue v,
876884
SILType resultTy);

0 commit comments

Comments
 (0)