<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></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 Dec 14, 2015, at 6:52 PM, Kevin Ballard <<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class=""><div class="">(moved to swift-dev)<br class=""></div>
<div class=""> </div>
<div class="">I went ahead and implemented this over the weekend, but ran into some weird behavior when testing it. I defined a new macro<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"># if defined(__has_feature) && __has_feature(generalized_swift_name)</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"># define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME) enum _name : _type _name; enum SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_ENUM_EXTRA _name : _type</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"># else</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"># define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME) SWIFT_ENUM(_type, _name)</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"># endif</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""> </div>
<div class="">and taught ClangImporter to handle macros named SWIFT_ENUM_NAMED in addition to SWIFT_ENUM, and then I generated a header that contains<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">typedef SWIFT_ENUM_NAMED(NSInteger, FooBar, "Bar") {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> FooBarBaz = 0,<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> FooBarQux = 1,<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">};<br class=""></span></div>
<div class=""> </div>
<div class="">When I add a module.modulemap and import the module into the swift integrated REPL, :print_module shows the enum<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">enum Bar : Int {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> init?(rawValue: Int)<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> var rawValue: Int {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> get {}<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> }<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> case FooBarBaz<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> case FooBarQux<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span></div></div></div></blockquote><div><br class=""></div><div>Nifty.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">
<div class="">(I haven't touched case prefix stripping; presumably it's trying to strip the Swift name instead of the ObjC name)<br class=""></div></div></div></blockquote><div><br class=""></div><div>Probably. It’s not actually clear which prefix it should try stripping with.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">
<div class=""> </div>
<div class="">But if I actually try and reference the type Bar, it tells me it's an undeclared type! </div></div></div></blockquote><div><br class=""></div><div>Yeah, there is some known brokenness here: the swift_name attribute doesn’t actually work on global declarations, because the Clang importer currently makes assumptions about the mapping from Swift names to Objective-C names. That’s exactly why I’m introducing the Swift name lookup tables into the Clang importer now, because they make it possible for arbitrary renaming of Objective-C entities to actually work in Swift.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="">I tried dumping the importer lookup table with swift-ide-test, and while I don't actually understand the output format, from looking at the IDE/dump_swift_lookup_tables.swift test, I get the impression that the following is wrong (emphasis mine):<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><<Bridging header lookup table>><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">Base name -> entry mappings:<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> Bar:<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> TU: FooBar<br class=""><b class=""> FooBar:<br class=""></b></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><b class=""> TU: FooBar, FooBar</b><b class=""><br class=""></b></span></div></div></div></blockquote><div><br class=""></div><div>Your macro is swift_name’ing the enum but not the typedef, which is why there are entries for both Bar -> FooBar and FooBar -> FooBar. The duplication in the FooBar result is interesting, though: perhaps it’s related to the fact that the enum is first declared without the swift_name attribute?</div><div><br class=""></div><blockquote type="cite" class=""><div class="">
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><b class=""></b> FooBarBaz:<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> FooBar: FooBarBaz<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> FooBarQux:<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> FooBar: FooBarQux<br class=""></span></div>
<div class=""> </div>
<div class="">I suspect that using __attribute__((swift_name)) was never tested with enums, because there's no pre-existing enum macro that supports it. Should I just file this as a bug and submit my PR as-is? Or do you have any guidance on what might be going on here?</div></div></blockquote><blockquote type="cite" class=""><div class=""><div class=""> </div></div></blockquote><br class=""></div><div>It’s fine to submit a PR for this that tests what works (e.g., use FileCheck to verify that the Swift API dumps for the imported enum have the right names) and also have some tests that verify the bad behavior (“cannot find Bar”) with a FIXME to say that the test should succeed. That way, you can get your change in and move the needle forward a little, and when we start turning on the Swift name lookup tables, these tests will help us see that more things are working.</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>- Doug</div><div><br class=""></div></body></html>