darwin - What is '@_silgen_name' in Swift language? -


while reading darwin library in swift 2.2, 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> 

what '@_silgen_name' in 2nd line?

i found below information here, want more detail information, apple developer library.

if it’s specific set of swift functions want call c, can use @_silgen_name attribute override mangled name, and/or make them @convention(c) use c calling convention.

@_silgen_name renamed @asmname (see following commit) following commit message:

this reflects fact attribute's only compiler-internal use, , isn't equivalent c's asm attribute, since doesn't change calling convention c-compatible.

so general swift developer, 1 wouldn't come across attribute, unless working with, say, porting swift other platform.

now, @_silgen_name attribute (macro) class silgennameattr options, latter part of swifts abstract syntax tree (ast). swift/ast/attr.def source code (see swift/lib/ast/attr.cpp)

// schema decl_attr: // // - attribute name. // - class name without 'attr' suffix (ignored // - options attribute, including: //    * declarations attribute can appear on //    * whether duplicates allowed //    * whether attribute considered decl modifier or not (no '@') // - unique attribute identifier used serialization.  //   can never changed. // // simple_decl_attr same, class becomes // simpledeclattr<dak_##name>. //  decl_attr(_silgen_name, silgenname,           onfunc | onconstructor | ondestructor | longattribute |           userinaccessible, 0) 

we find declaration of silgenenameattr in swift/ast/attr.h:

/// defines @_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) {}    /// symbol name.   const stringref name;    static bool classof(const declattribute *da) {     return da->getkind() == dak_silgenname;   } }; 

to sum up; it's related provide swift interface c functions. have hard time finding details regarding silgennameattr in developer library, , can consider undocumented feature.


finally, following talk russ bishop might of interest you:

the toolbox: here there dragons (34:20)

i not ship in production application under circumstances. if you’re feeling adventurous, here are.

@asmname attribute decorate function. need understand abi use one. swift not marshall parameters much. compiler not going forgiving, have make sure you’ve manipulated parameters format it’s compatible with. following code shows how declare it. give function attribute, @asmname, , string symbol; function arguments in return type. compiler not complain if arguments wrong or return type incorrectly. expects that symbol exists, , when jumps it, better take arguments , have return type.

@asmname("dispatch_get_current_queue") func _get_current_queue() -> dispatch_queue_t 

q&a (37:08)

q: obviously not of these documented apple, what’s process of discovering these behaviors?

russ: i’ll @ documentation first, you’re right there lot of stuff isn’t in there. if go swift repl , use flag — think it’s -deprecated-integrated-repl — can ask print swift module , bits xcode doesn’t show you. if dig toolchain directory xcode, can find stuff in libswiftcore , libswiftruntime.

jp: if you’re interested in doing more unsafe things swift, can code search in github @asmname , language “swift”, , you’ll see bunch of bad interesting stuff.

a older post bishops' blog:

...

first off notice @asmname attribute. equivalent of dllimport or extern. tells swift going link in library defines function given name , matching given arguments. "just trust me swift, know i'm doing". hint: had better know you're doing.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -