Skip to content

[cxx-interop] Add SIL function representation cxx_method; Support extending C++ types. #34993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ Types
FUNCTION-KIND ::= 'B' // objc block function type
FUNCTION-KIND ::= 'zB' C-TYPE // objc block type with non-canonical C type
FUNCTION-KIND ::= 'L' // objc block function type with canonical C type (escaping) (DWARF only; otherwise use 'B' or 'zB' C-TYPE)
FUNCTION-KIND ::= 'C' // C function pointer type
FUNCTION-KIND ::= 'zC' C-TYPE // C function pointer type with with non-canonical C type
FUNCTION-KIND ::= 'C' // C function pointer / C++ method type
FUNCTION-KIND ::= 'zC' C-TYPE // C function pointer / C++ method type with with non-canonical C type
FUNCTION-KIND ::= 'A' // @auto_closure function type (escaping)
FUNCTION-KIND ::= 'E' // function type (noescape)

Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/ExtInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ enum class SILFunctionTypeRepresentation : uint8_t {

/// A closure invocation function that has not been bound to a context.
Closure,

/// A C++ method that takes a "this" argument (not a static C++ method or
/// constructor). Except for
/// handling the "this" argument, has the same behavior as "CFunctionPointer".
CXXMethod,
};

/// Returns true if the function with this convention doesn't carry a context.
Expand Down Expand Up @@ -196,6 +201,7 @@ isThinRepresentation(SILFunctionTypeRepresentation rep) {
case SILFunctionTypeRepresentation::WitnessMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Closure:
case SILFunctionTypeRepresentation::CXXMethod:
return true;
}
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
Expand Down Expand Up @@ -232,6 +238,7 @@ convertRepresentation(SILFunctionTypeRepresentation rep) {
return {FunctionTypeRepresentation::Block};
case SILFunctionTypeRepresentation::Thin:
return {FunctionTypeRepresentation::Thin};
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
return {FunctionTypeRepresentation::CFunctionPointer};
case SILFunctionTypeRepresentation::Method:
Expand All @@ -252,6 +259,7 @@ constexpr bool canBeCalledIndirectly(SILFunctionTypeRepresentation rep) {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Block:
case SILFunctionTypeRepresentation::Closure:
case SILFunctionTypeRepresentation::CXXMethod:
return false;
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::Method:
Expand All @@ -269,6 +277,7 @@ template <typename Repr> constexpr bool shouldStoreClangType(Repr repr) {
switch (static_cast<SILFunctionTypeRepresentation>(repr)) {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Block:
case SILFunctionTypeRepresentation::CXXMethod:
return true;
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::Thick:
Expand Down Expand Up @@ -392,6 +401,7 @@ class ASTExtInfoBuilder {
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::Method:
case SILFunctionTypeRepresentation::WitnessMethod:
case SILFunctionTypeRepresentation::CXXMethod:
return true;
}
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
Expand Down Expand Up @@ -618,6 +628,7 @@ SILFunctionLanguage getSILFunctionLanguage(SILFunctionTypeRepresentation rep) {
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Block:
case SILFunctionTypeRepresentation::CXXMethod:
return SILFunctionLanguage::C;
case SILFunctionTypeRepresentation::Thick:
case SILFunctionTypeRepresentation::Thin:
Expand Down Expand Up @@ -750,6 +761,7 @@ class SILExtInfoBuilder {
case Representation::ObjCMethod:
case Representation::Method:
case Representation::WitnessMethod:
case SILFunctionTypeRepresentation::CXXMethod:
return true;
}
llvm_unreachable("Unhandled Representation in switch.");
Expand All @@ -767,6 +779,7 @@ class SILExtInfoBuilder {
case Representation::Method:
case Representation::WitnessMethod:
case Representation::Closure:
case SILFunctionTypeRepresentation::CXXMethod:
return false;
}
llvm_unreachable("Unhandled Representation in switch.");
Expand Down
1 change: 1 addition & 0 deletions include/swift/SIL/ApplySite.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class ApplySite {
bool isCalleeThin() const {
switch (getSubstCalleeType()->getRepresentation()) {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::Thin:
case SILFunctionTypeRepresentation::Method:
case SILFunctionTypeRepresentation::ObjCMethod:
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ getSILFunctionTypeRepresentationString(SILFunctionType::Representation value) {
case SILFunctionType::Representation::Thick: return "thick";
case SILFunctionType::Representation::Block: return "block";
case SILFunctionType::Representation::CFunctionPointer: return "c";
case SILFunctionType::Representation::CXXMethod:
return "cxx_method";
case SILFunctionType::Representation::Thin: return "thin";
case SILFunctionType::Representation::Method: return "method";
case SILFunctionType::Representation::ObjCMethod: return "objc_method";
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
OpArgs.push_back('B');
appendClangTypeToVec(OpArgs);
break;
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
if (!mangleClangType) {
OpArgs.push_back('C');
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4885,6 +4885,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
case SILFunctionType::Representation::Method:
Printer << "method";
break;
case SILFunctionType::Representation::CXXMethod:
Printer << "cxx_method";
break;
case SILFunctionType::Representation::ObjCMethod:
Printer << "objc_method";
break;
Expand Down Expand Up @@ -4963,6 +4966,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
case SILFunctionType::Representation::Method:
Printer << "method";
break;
case SILFunctionType::Representation::CXXMethod:
Printer << "cxx_method";
break;
case SILFunctionType::Representation::ObjCMethod:
Printer << "objc_method";
break;
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const clang::Type *ClangTypeConverter::getFunctionType(
return nullptr;

switch (repr) {
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::CFunctionPointer:
return ClangASTContext.getPointerType(fn).getTypePtr();
case SILFunctionType::Representation::Block:
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ExtInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Optional<UnexpectedClangTypeError> UnexpectedClangTypeError::checkClangType(
#else
bool isBlock = true;
switch (silRep) {
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
isBlock = false;
LLVM_FALLTHROUGH;
Expand Down
12 changes: 7 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4085,18 +4085,20 @@ namespace {
templateParams);

if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
// Subscripts and call operators are imported as normal methods.
bool staticOperator = mdecl->isOverloadedOperator() &&
mdecl->getOverloadedOperator() != clang::OO_Call &&
mdecl->getOverloadedOperator() != clang::OO_Subscript;
if (mdecl->isStatic() ||
// C++ operators that are implemented as non-static member
// functions get imported into Swift as static member functions
// that use an additional parameter for the left-hand side operand
// instead of the receiver object.
(mdecl->getDeclName().getNameKind() ==
clang::DeclarationName::CXXOperatorName &&
isImportedAsStatic(mdecl->getOverloadedOperator()))) {
staticOperator) {
selfIdx = None;
} else {
selfIdx = 0;
// Don't import members of a class decl as mutating.
// Swift imports the "self" param last, even for clang functions.
selfIdx = bodyParams ? bodyParams->size() : 0;
// If the method is imported as mutating, this implicitly makes the
// parameter indirect.
selfIsInOut =
Expand Down
6 changes: 5 additions & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,11 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
// imported into Swift as static methods that have an additional
// parameter for the left-hand side operand instead of the receiver object.
if (auto CMD = dyn_cast<clang::CXXMethodDecl>(clangDecl)) {
if (clangDecl->isOverloadedOperator() && isImportedAsStatic(clangDecl->getOverloadedOperator())) {
// Subscripts and call operators are imported as normal methods.
bool staticOperator = clangDecl->isOverloadedOperator() &&
clangDecl->getOverloadedOperator() != clang::OO_Call &&
clangDecl->getOverloadedOperator() != clang::OO_Subscript;
if (staticOperator) {
auto param = new (SwiftContext)
ParamDecl(SourceLoc(), SourceLoc(), Identifier(), SourceLoc(),
SwiftContext.getIdentifier("lhs"), dc);
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/Callee.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ namespace irgen {
/// Given that this callee is a block, return the block pointer.
llvm::Value *getBlockObject() const;

/// Given that this callee is a C++ method, return the self argument.
llvm::Value *getCXXMethodSelf() const;

/// Given that this callee is an ObjC method, return the receiver
/// argument. This might not be 'self' anymore.
llvm::Value *getObjCMethodReceiver() const;
Expand Down
50 changes: 40 additions & 10 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
switch (convention) {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::Block:
return llvm::CallingConv::C;

Expand Down Expand Up @@ -1326,6 +1327,7 @@ void SignatureExpansion::expandExternalSignatureTypes() {
paramTys.push_back(clangCtx.VoidPtrTy);
break;

case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
// No implicit arguments.
break;
Expand Down Expand Up @@ -1642,6 +1644,7 @@ void SignatureExpansion::expandParameters() {
case SILFunctionType::Representation::Method:
case SILFunctionType::Representation::WitnessMethod:
case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::Thin:
case SILFunctionType::Representation::Closure:
return FnType->hasErrorResult();
Expand Down Expand Up @@ -1809,6 +1812,7 @@ void SignatureExpansion::expandAsyncEntryType() {
case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::Thin:
case SILFunctionType::Representation::Closure:
case SILFunctionType::Representation::CXXMethod:
return false;

case SILFunctionType::Representation::Thick:
Expand Down Expand Up @@ -2203,7 +2207,20 @@ class SyncCallEmission final : public CallEmission {
break;

case SILFunctionTypeRepresentation::Block:
adjusted.add(getCallee().getBlockObject());
case SILFunctionTypeRepresentation::CXXMethod:
if (getCallee().getRepresentation() == SILFunctionTypeRepresentation::Block) {
adjusted.add(getCallee().getBlockObject());
} else {
auto selfParam = origCalleeType->getSelfParameter();
auto *arg = getCallee().getCXXMethodSelf();
// We might need to fix the level of indirection for foreign reference types.
if (selfParam.getInterfaceType().isForeignReferenceType() &&
isIndirectFormalParameter(selfParam.getConvention()))
arg = IGF.Builder.CreateLoad(arg, IGF.IGM.getPointerAlignment());

adjusted.add(arg);
}

LLVM_FALLTHROUGH;

case SILFunctionTypeRepresentation::CFunctionPointer:
Expand Down Expand Up @@ -2462,14 +2479,9 @@ class AsyncCallEmission final : public CallEmission {
// Translate the formal arguments and handle any special arguments.
switch (getCallee().getRepresentation()) {
case SILFunctionTypeRepresentation::ObjCMethod:
assert(false && "Should not reach this");
break;

case SILFunctionTypeRepresentation::Block:
assert(false && "Should not reach this");
break;

case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::CXXMethod:
assert(false && "Should not reach this");
break;

Expand Down Expand Up @@ -3148,6 +3160,9 @@ Callee::Callee(CalleeInfo &&info, const FunctionPointer &fn,
case SILFunctionTypeRepresentation::CFunctionPointer:
assert(!FirstData && !SecondData);
break;
case SILFunctionTypeRepresentation::CXXMethod:
assert(FirstData && !SecondData);
break;
}
#endif

Expand All @@ -3160,6 +3175,7 @@ llvm::Value *Callee::getSwiftContext() const {
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Thin:
case SILFunctionTypeRepresentation::Closure:
case SILFunctionTypeRepresentation::CXXMethod:
return nullptr;

case SILFunctionTypeRepresentation::WitnessMethod:
Expand All @@ -3182,6 +3198,14 @@ llvm::Value *Callee::getBlockObject() const {
return FirstData;
}

llvm::Value *Callee::getCXXMethodSelf() const {
assert(Info.OrigFnType->getRepresentation() ==
SILFunctionTypeRepresentation::CXXMethod &&
"not a C++ method");
assert(FirstData && "no self object set on callee");
return FirstData;
}

llvm::Value *Callee::getObjCMethodReceiver() const {
assert(Info.OrigFnType->getRepresentation() ==
SILFunctionTypeRepresentation::ObjCMethod &&
Expand Down Expand Up @@ -3496,6 +3520,7 @@ static void externalizeArguments(IRGenFunction &IGF, const Callee &callee,
// The index of the first "physical" parameter from paramTys/FI that
// corresponds to a logical parameter from params.
unsigned firstParam = 0;
unsigned paramEnd = FI.arg_size();

// Handle the ObjC prefix.
if (callee.getRepresentation() == SILFunctionTypeRepresentation::ObjCMethod) {
Expand All @@ -3509,14 +3534,19 @@ static void externalizeArguments(IRGenFunction &IGF, const Callee &callee,
== SILFunctionTypeRepresentation::Block) {
// Ignore the physical block-object parameter.
firstParam += 1;
// Or the indirect result parameter.
} else if (fnType->getNumResults() > 0 &&
} else if (callee.getRepresentation() ==
SILFunctionTypeRepresentation::CXXMethod) {
// Skip the "self" param.
paramEnd--;
}

if (fnType->getNumResults() > 0 &&
fnType->getSingleResult().isFormalIndirect()) {
// Ignore the indirect result parameter.
firstParam += 1;
}

for (unsigned i = firstParam, e = FI.arg_size(); i != e; ++i) {
for (unsigned i = firstParam; i != paramEnd; ++i) {
auto clangParamTy = FI.arg_begin()[i].type;
auto &AI = FI.arg_begin()[i].info;

Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ const TypeInfo *TypeConverter::convertFunctionType(SILFunctionType *T) {

case SILFunctionType::Representation::Thin:
case SILFunctionType::Representation::Method:
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::WitnessMethod:
case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::CFunctionPointer:
Expand Down Expand Up @@ -583,6 +584,7 @@ getFuncSignatureInfoForLowered(IRGenModule &IGM, CanSILFunctionType type) {
case SILFunctionType::Representation::Thin:
case SILFunctionType::Representation::CFunctionPointer:
case SILFunctionType::Representation::Method:
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::WitnessMethod:
case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::Closure:
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/GenPointerAuth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ static const PointerAuthSchema &getFunctionPointerSchema(IRGenModule &IGM,
CanSILFunctionType fnType) {
auto &options = IGM.getOptions().PointerAuth;
switch (fnType->getRepresentation()) {
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
return options.FunctionPointers;

Expand Down Expand Up @@ -583,6 +584,7 @@ PointerAuthEntity::getTypeDiscriminator(IRGenModule &IGM) const {
}

// C function pointers are undiscriminated.
case SILFunctionTypeRepresentation::CXXMethod:
case SILFunctionTypeRepresentation::CFunctionPointer:
return llvm::ConstantInt::get(IGM.Int64Ty, 0);

Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,7 @@ bool irgen::hasPolymorphicParameters(CanSILFunctionType ty) {

case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CXXMethod:
// May be polymorphic at the SIL level, but no type metadata is actually
// passed.
return false;
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,7 @@ Callee LoweredValue::getCallee(IRGenFunction &IGF,
return getBlockPointerCallee(IGF, functionValue, std::move(calleeInfo));

case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::Thick:
llvm_unreachable("unexpected function with singleton representation");

Expand Down Expand Up @@ -2991,6 +2992,7 @@ static std::unique_ptr<CallEmission> getCallEmissionForLoweredValue(
}

case SILFunctionType::Representation::ObjCMethod:
case SILFunctionType::Representation::CXXMethod:
case SILFunctionType::Representation::Thick:
case SILFunctionType::Representation::Block:
case SILFunctionType::Representation::Thin:
Expand Down Expand Up @@ -3363,6 +3365,7 @@ getPartialApplicationFunction(IRGenSILFunction &IGF, SILValue v,
case SILFunctionTypeRepresentation::CFunctionPointer:
case SILFunctionTypeRepresentation::Block:
case SILFunctionTypeRepresentation::ObjCMethod:
case SILFunctionTypeRepresentation::CXXMethod:
llvm_unreachable("partial_apply of foreign functions not implemented");

case SILFunctionTypeRepresentation::WitnessMethod:
Expand Down
Loading