Skip to content
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
27 changes: 22 additions & 5 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4424,12 +4424,29 @@ TypeConverter::getDeclRefRepresentation(SILDeclRef c) {
if (!c.hasDecl())
return SILFunctionTypeRepresentation::CFunctionPointer;

if (auto method =
dyn_cast_or_null<clang::CXXMethodDecl>(c.getDecl()->getClangDecl()))
return isa<clang::CXXConstructorDecl>(method) || method->isStatic()
? SILFunctionTypeRepresentation::CFunctionPointer
: SILFunctionTypeRepresentation::CXXMethod;
if (auto clangDecl = c.getDecl()->getClangDecl()) {
if (auto method = dyn_cast<clang::CXXMethodDecl>(clangDecl)) {
return isa<clang::CXXConstructorDecl>(method) || method->isStatic()
? SILFunctionTypeRepresentation::CFunctionPointer
: SILFunctionTypeRepresentation::CXXMethod;
}

if (auto function = dyn_cast<clang::FunctionDecl>(clangDecl)) {
if (auto fnType = function->getType()->getAs<clang::FunctionType>()) {
switch (fnType->getCallConv()) {
case clang::CC_Swift:
return SILFunctionTypeRepresentation::Thin;

case clang::CC_C:
return SILFunctionTypeRepresentation::CFunctionPointer;

default:
// Fall back to heuristics below.
break;
}
}
}
}

// For example, if we have a function in a namespace:
if (c.getDecl()->isImportAsMember())
Expand Down
26 changes: 26 additions & 0 deletions test/IRGen/c_calling_conventions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -I%t %t/caller.swift -emit-ir | %FileCheck %s

//--- c_funcs.h

__attribute__((swiftcall))
extern void with_swiftcc(void);

//--- module.modulemap

module c_funcs {
header "c_funcs.h"
}

//--- caller.swift

import c_funcs

func test() {
// CHECK: call swiftcc void @with_swiftcc()
with_swiftcc()
}
// CHECK: declare {{.*}}swiftcc void @with_swiftcc()

test()