什么是Swift语言中的’@_silgen_name’?

什么是Swift语言中的’@_silgen_name’?,第1张

概述在Swift 2.2中阅读Darwin库时,我发现下面的代码. @warn_unused_result@_silgen_name("_swift_Darwin_sem_open2")internal func _swift_Darwin_sem_open2( name: UnsafePointer<CChar>, _ oflag: CInt) -> UnsafeMutablePoin 在Swift 2.2中阅读Darwin库时,我发现下面的代码.
@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’是什么?

我从here找到了以下信息,但我想要更多详细信息,比如Apple Developer library.

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( see the following commit)重命名,并带有以下提交消息:

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移植到其他平台,否则不会遇到此属性.

现在,@ _ silgen_name是具有某些选项的SILGenNameattr类的属性(宏),后者是Swifts abstract syntax tree (AST)的一部分.从swift/AST/Attr.def source code开始(另请参见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)

我们在swift/AST/Attr.h找到了SILGeneNameattr的声明:

/// 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的任何详细信息,并且可以将其视为未记录的功能.

最后,您可能会对以下与Russ Bishop的谈话感兴趣:

> Unsafe Swift: For Fun & Profit

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.

06002

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博客的一篇略微更老的帖子:

> Swift – Don’t do this.

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’?全部内容,希望文章能够帮你解决什么是Swift语言中的’@_silgen_name’?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1049547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存