@_silgen_name
最近从 @asmname
(请参见以下提交) 更名,附带的提交信息如下:
这反映了该属性仅供编译器内部使用,并不真正等同于C的asm属性,因为它不会改变调用约定以使其与C兼容。
因此,作为一名普通的Swift开发人员,除非与其他平台进行移植工作,否则不会遇到这种属性。
现在,
@_silgen_name
是
SILGenNameAttr
类的一个带有特定选项的属性(宏),后者是 Swift 的
抽象语法树 (AST) 的一部分。可以从
swift/AST/Attr.def
源代码(也可以参考
swift/lib/AST/Attr.cpp)中了解更多信息。
DECL_ATTR(_silgen_name, SILGenName,
OnFunc | OnConstructor | OnDestructor | LongAttribute |
UserInaccessible, 0)
我们可以在
swift/AST/Attr.h文件中找到
SILGeneNameAttr
的声明:
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(), true) {}
const StringRef Name;
static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_SILGenName;
}
};
总之,这与为C函数提供Swift接口有关。您很可能会发现在开发者库中找不到有关的详细信息,可以将其视为未记录的功能。
最后,以下与Russ Bishop的交谈可能会对您有所帮助:
The Toolbox: Here there be dragons (34:20)
I would not ship this in a production application under any
circumstances. But if you’re feeling adventurous, here they are.
@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
Q&A (37:08)
Q: Obviously not all of these are documented by Apple, so what’s your process of discovering all these behaviors?
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: 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的博客中有一篇稍早的帖子:
...
首先请注意 @asmname
属性。这相当于 DllImport
或 extern
。它告诉 Swift 我们将链接某个库,该库定义了一个具有给定名称和匹配给定参数的函数。"请相信我,Swift,我知道我在做什么"。提示:你最好知道你在做什么。