Swift 语言中的“@_silgen_name"是什么? [英] What is '@_silgen_name' in Swift language?
问题描述
在 Swift 2.2 中阅读 Darwin 库时,我发现了以下代码.
While reading Darwin library in Swift 2.2, I found below codes.
@warn_unused_result
@_silgen_name("_swift_Darwin_sem_open2")
internal func _swift_Darwin_sem_open2(
name: UnsafePointer<CChar>,
_ oflag: CInt
) -> UnsafeMutablePointer<sem_t>
第二行中的@_silgen_name"是什么?
What is '@_silgen_name' in the 2nd line?
我从这里找到了以下信息,但我想要更多详细信息,例如 Apple Developer Library.
I found a below information from here, but I want more detail information, like the Apple Developer Library.
如果它只是您想从 C 调用的一组特定 Swift 函数,您可以使用 @_silgen_name 属性来覆盖损坏的名称,和/或使它们 @convention(c) 以使用 C 调用约定.
If it’s just a specific set of Swift functions you want to call from C, you can use the @_silgen_name attribute to override the mangled name, and/or make them @convention(c) to use the C calling convention.
推荐答案
@_silgen_name
最近刚刚从 @asmname
(查看以下提交),并带有以下提交消息:
@_silgen_name
was just recently renamed from @asmname
(see the following commit) with the following commit message:
这反映了一个事实,即该属性的仅用于编译器内部使用,并不真正等同于 C 的 asm属性,因为它不会将调用约定更改为C 兼容.
This reflects the fact that the attribute's only for compiler-internal use, and isn't really equivalent to C's asm attribute, since it doesn't change the calling convention to be C-compatible.
因此,作为一般的 Swift 开发人员,除非使用,例如,将 Swift 移植到其他平台,否则不会遇到此属性.
So as a general Swift developer, one wouldn't come across this attribute, unless working with, say, porting Swift to some other platform.
现在,@_silgen_name
是类 SILGenNameAttr
的属性(宏),带有某些选项,后者是 Swifts 抽象语法树 (AST).来自 swift/AST/Attr.def
源代码(另见 swift/lib/AST/Attr.cpp)
Now, @_silgen_name
is an attribute (macro) for the class SILGenNameAttr
with certain options, where the latter is part of Swifts abstract syntax tree (AST). From the swift/AST/Attr.def
source code (see also swift/lib/AST/Attr.cpp)
// Schema for DECL_ATTR:
//
// - Attribute name.
// - Class name without the 'Attr' suffix (ignored for
// - Options for the attribute, including:
// * the declarations the attribute can appear on
// * whether duplicates are allowed
// * whether the attribute is considered a decl modifier or not (no '@')
// - Unique attribute identifier used for serialization. This
// can never be changed.
//
// SIMPLE_DECL_ATTR is the same, but the class becomes
// SimpleDeclAttr<DAK_##NAME>.
//
DECL_ATTR(_silgen_name, SILGenName,
OnFunc | OnConstructor | OnDestructor | LongAttribute |
UserInaccessible, 0)
我们在 SILGeneNameAttr 的声明"nofollow noreferrer">swift/AST/Attr.h:
We find the declaration of SILGeneNameAttr
in swift/AST/Attr.h:
/// Defines the @_silgen_name attribute.
class SILGenNameAttr : public DeclAttribute {
public:
SILGenNameAttr(StringRef Name, SourceLoc AtLoc, SourceRange Range, bool Implicit)
: DeclAttribute(DAK_SILGenName, AtLoc, Range, Implicit),
Name(Name) {}
SILGenNameAttr(StringRef Name, bool Implicit)
: SILGenNameAttr(Name, SourceLoc(), SourceRange(), /*Implicit=*/true) {}
/// The symbol name.
const StringRef Name;
static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_SILGenName;
}
};
总结;它与为 C 函数提供 Swift 接口有关.您很可能很难在开发人员库中找到有关 SILGenNameAttr
的任何详细信息,并且可以将其视为未记录的功能.
To sum up; it's related to provide a Swift interface for C functions. You will most likely have a hard time finding any details regarding SILGenNameAttr
in the developer library, and can consider it an undocumented feature.
最后,您可能会对以下与 Russ Bishop 的谈话感兴趣:
Finally, the following talk with Russ Bishop might be of interest to you:
工具箱:这里有龙 (34:20)
我不会在任何情况下在生产应用程序中发布它情况.但如果您喜欢冒险,那就来这里吧.
I would not ship this in a production application under any circumstances. But if you’re feeling adventurous, here they are.
@asmname
是装饰函数的属性.你肯定需要了解 ABI 使用这个.斯威夫特帮不了你marshall 参数非常多.编译器不会非常宽容,所以你必须确保你已经操纵了你的参数转换成它兼容的格式.以下代码显示如何声明它.给出函数属性,@asmname
,和字符串符号;然后以返回类型函数其参数.这如果参数错误或返回,编译器不会抱怨输入错误.它只期望那个符号存在,并且当它跳转到它,最好采用这些参数并具有该返回类型.
@asmname
is an attribute to decorate a function. You definitely need
to understand the ABI to use this one. Swift will not help you
marshall the parameters very much. The compiler is not going to be
very forgiving, so you have to make sure that you’ve manipulated your
parameters into a format it’s compatible with. The following code
shows how to declare it. Give the function attribute, @asmname
, and
the string symbol; then function its arguments in return type. The
compiler will not complain if you get the arguments wrong or return
type incorrectly. It only expects that that symbol exists, and when it
jumps to it, it better take those arguments and have that return type.
@asmname("dispatch_get_current_queue") func _get_current_queue() -> dispatch_queue_t
问答 (37:08)
问:显然,Apple 并未记录所有这些行为,那么您发现所有这些行为的过程是什么?
Russ:我会先查看文档,但您是对的,里面有很多东西没有.如果你去斯威夫特REPL 并使用标志 - 我认为它类似于-deprecated-integrated-repl
— 你可以要求它打印 Swift 模块和 Xcode 没有显示的所有位.如果你深入研究工具链目录 Xcode,你也可以在 libswiftCore 和libswift运行时.
Russ: I’ll look at the documentation first, but you’re right that there is a lot of stuff that isn’t in there. If you go to the Swift
REPL and use the flag — I think it’s something like
-deprecated-integrated-repl
— you can ask it to print the Swift module and all the bits that Xcode doesn’t show you. If you dig into the
toolchain directory Xcode, you can also find stuff in libswiftCore and
libswiftRuntime.
JP:如果你有兴趣用 Swift 做一些更不安全的事情,你可以在 GitHub 中搜索 @asmname
和语言的代码Swift",你会看到一堆非常糟糕但有趣的东西.
JP: If you’re interested in doing some more unsafe things with Swift, you can do a code search in GitHub for @asmname
and language
"Swift", and you’ll see a bunch of really bad but interesting stuff.
来自 Bishops 博客的一篇稍早的帖子:
A slightly older post from Bishops' blog:
...
首先注意 @asmname
属性.这相当于DllImport
或 extern
.它告诉 Swift 我们将要链接一些定义具有给定名称和匹配的函数的库给定的参数.相信我,Swift,我知道我在做什么".提示:你最好知道你在做什么.
First off notice the @asmname
attribute. This is the equivalent of
DllImport
or extern
. It tells Swift that we are going to link in
some library that defines a function with the given name and matching
the given arguments. "Just trust me Swift, I know what I'm doing".
Hint: You had better know what you're doing.
这篇关于Swift 语言中的“@_silgen_name"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!