<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 6, 2016, at 12:57 PM, Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com" class="">austinzheng@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hey Slava,<div class=""><br class=""></div><div class="">Thanks a lot for your detailed responses; it definitely helps to understand how structs are passed to the C++ function.</div><div class=""><br class=""></div><div class="">In a separate email, Joe Groff mentioned that there was a difference between passing the enum value and passing a pointer to it. I think that might be the root of my problem. I'll try a few things and send over a better code sample tonight, if there are still issues.</div></div></div></blockquote><div><br class=""></div>Yeah, that sounds like it could be the problem.</div><div><br class=""></div><div>In general, expecting the Swift calling convention to line up with C++ in this manner is really not something we want to officially support, and I believe JoeG is currently working on refactoring the runtime reflection interface to be a bit more minimal and orthogonal on the runtime side.</div><div><br class=""></div><div>It would be great to see what you're working on, because I suspect we should be able to fold your use-case into the general reflection API.</div><div><br class=""></div><div>Slava</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Jan 6, 2016 at 11:37 AM, Slava Pestov <span dir="ltr" class="">&lt;<a href="mailto:spestov@apple.com" target="_blank" class="">spestov@apple.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Austin,<br class="">
<div class=""><div class="h5"><br class="">
&gt; On Jan 1, 2016, at 10:58 PM, Austin Zheng via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; Hello,<br class="">
&gt;<br class="">
&gt; I'm trying to better understand how calls are made between the Swift standard library code and the runtime entry points. I've read through most of the documentation in the repo but still have some questions.<br class="">
&gt;<br class="">
&gt; More specifically, here's an example: the '_EnumMirror' struct below represents a mirror reflecting a value whose type is an enum.<br class="">
&gt;<br class="">
&gt; ------<br class="">
&gt;<br class="">
&gt; struct _EnumMirror : _MirrorType {<br class="">
&gt;&nbsp; let data: _MagicMirrorData<br class="">
&gt;&nbsp; var value: Any { return data.value }<br class="">
&gt;&nbsp; var valueType: Any.Type { return data.valueType }<br class="">
&gt;&nbsp; // ... more stuff<br class="">
&gt;<br class="">
&gt;&nbsp; var caseName: UnsafePointer&lt;CChar&gt; {<br class="">
&gt;&nbsp; &nbsp; @_silgen_name("swift_EnumMirror_caseName")get<br class="">
&gt;&nbsp; }<br class="">
&gt;&nbsp; // ... (more stuff)<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt; ------<br class="">
&gt;<br class="">
&gt; The 'caseName' property represents the name of the enum value's case (e.g. "FirstCase" in Foo.FirstCase) as a C string. This property's getter calls into a C++ runtime function named "swift_EnumMirror_caseName", which is reproduced below (from Reflection.mm):<br class="">
&gt;<br class="">
&gt; extern "C"<br class="">
&gt; const char *swift_EnumMirror_caseName(HeapObject *owner,<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const OpaqueValue *value,<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const Metadata *type) {<br class="">
&gt;&nbsp; if (!isEnumReflectable(type))<br class="">
&gt;&nbsp; &nbsp; return nullptr;<br class="">
&gt;<br class="">
&gt;&nbsp; const auto Enum = static_cast&lt;const EnumMetadata *&gt;(type);<br class="">
&gt;&nbsp; const auto &amp;Description = Enum-&gt;Description-&gt;Enum;<br class="">
&gt;<br class="">
&gt;&nbsp; unsigned tag;<br class="">
&gt;&nbsp; getEnumMirrorInfo(value, type, &amp;tag, nullptr, nullptr);&nbsp; &nbsp; // effectively, same as "tag = type-&gt;vw_getEnumTag(value);"<br class="">
&gt;&nbsp; return getFieldName(Description.CaseNames, tag);<br class="">
&gt; }<br class="">
&gt;<br class="">
&gt; Now, I had a few questions about exactly how this interoperation works, because I'd like to be able to get the name of an enum case using this entry point from a different context (not from within an _EnumMirror property).<br class="">
&gt;<br class="">
&gt; * swift_EnumMirror_caseName takes three arguments, but the Swift call site doesn't seem to specify what gets passed into the function.<br class="">
<br class="">
</div></div>The three arguments together form the 'self' value of the call. That is, an EnumMirror is a struct containing a pointer to the owner object, a pointer to the value being reflected, and runtime type information for the value. You can see this if you look at how the _MagicMirrorData struct is defined on the swift side.<br class="">
<span class=""><br class="">
&gt; Is there a convention that is implicitly passing properties on _EnumMirror as arguments into the C++ function when it's being called? I did note that there were other runtime entry points (like "swift_MagicMirrorData_summary") where the number of arguments in the Swift function matched the number of arguments in the C++ function, but in those cases the Swift function was a free function and not a method.<br class="">
<br class="">
</span>Right.<br class="">
<span class=""><br class="">
&gt;<br class="">
&gt; * What I really want to do is to get the tag of an enum. I wrote a different entry point that omits the unused "owner" property and simply calls swift_EnumMirror_caseName with nullptr as the first argument. This other C++ function takes 'value' (an OpaqueValue*) and 'type' (a Metadata*). I've surmised that 'type' should be the Swift metatype of the enum instance (e.g. myEnum.dynamicType), and I do get the case names table. However, if I pass in the enum instance itself as 'value', my tag is always retrieved as 0.<br class="">
<br class="">
</span>The value should indeed be a pointer to the enum value itself. Not sure why it's not working for you, maybe you can share more code?<br class="">
<span class=""><br class="">
&gt; I noticed that there's some sort of indirection in the form of "vw_getEnumTag", which goes through something called the "value witness". Is there somewhere I can read up about the value witness concept? I assume the reason the 'original' code worked was because it was passing in a different object as 'value', maybe one that could serve as a value witness for the reflected-upon instance's type.<br class="">
<br class="">
</span>The value witness table is a member of the type metadata. It contains entry points for runtime manipulation of values of that type. The value witness table is used for runtime generics (when I have a generic parameter 'T' and a value of type 'T', the value witness functions are used for copying/moving/destroying/etc values of type 'T'). They are also used for reflection.<br class="">
<br class="">
They're not really documented anywhere except for in the source code itself -- see here:<br class="">
<br class="">
include/swift/Runtime/Metadata.h<br class="">
lib/IRGen/ValueWitness.h<br class="">
<br class="">
Slava<br class="">
<span class=""><br class="">
&gt;<br class="">
&gt; Thanks a lot for your time.<br class="">
&gt;<br class="">
&gt; Best,<br class="">
&gt; Austin<br class="">
&gt;<br class="">
</span>&gt; _______________________________________________<br class="">
&gt; swift-dev mailing list<br class="">
&gt; <a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-dev" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-dev</a><br class="">
<br class="">
</blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>