<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 Feb 17, 2017, at 7:05 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk" class="">richard@metafoo.co.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote">On 17 February 2017 at 10:21, Akira Hatanaka via cfe-dev<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class="">I’d like to propose a new attribute for enums.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">### The proposed attribute ###</div><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class="">The attribute is tentatively named “enum_style” and takes two arguments. The first argument determines the style of the enum, either “option” or “choice".</div></div></div></div></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">These seem like a bad choice of names to me, since they are synonyms in English. Maybe call the flag form "flag"? But I don't actually see why we need or want to merge these two orthogonal arguments into a single attribute at all. Why not instead:<br class=""></div><div class=""><br class=""></div><div class=""><div class="">__attribute__((open_enum))</div><div class="">__attribute__((closed_enum))</div>__attribute__((flag_enum, open_enum))</div><div class="">__attribute__((flag_enum, closed_enum))<br class=""></div><div class=""><br class=""></div><div class="">?</div><div class=""><br class=""></div></div></div></div></div></blockquote><div><br class=""></div><div>Yes, I agree that “option” and “choice” might be too close in meaning. Any set of arguments or names should work as long as it’s possible to distinguish between</div><div><br class=""></div><div>1) “option” and “choice”</div><div>2) “closed” and “open”</div><div>3) annotated and unannotated</div><div><br class=""></div><div>so your idea is fine too.</div><div><br class=""></div></div><div><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class="">“option” implies that the enum can be used as a one-bit flag, just like “flag_enum", and it’s OK to OR the values to create a new value. “choice” implies the enum cannot be used like “option” enums. The second argument is used to indicate whether or not the enum can be extended. If an enum is marked “closed”, clang can assume a variable of the enum type always has a value that is in the range determined by the enumerators listed in the enum definition.</div></div></div></div></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">What exactly do you mean by this? The C++ rule for unscoped enums is that the range of representable values is (roughly) the values that fit in the smallest bit-field that can contain the enum. I assume that would be the rule for flag-style enums; for enumeration-style enums, would you restrict the range further to just lowest-declared-value to highest-declared-value (inclusive)?</div><div class=""> </div></div></div></div></div></blockquote><div><br class=""></div><div>Just to confirm, I thought the rule in the standard you are taking about applies to enums that don’t have underlying types, not unscoped enums? The range of an unscoped but fixed enum is the range of the underlying type, no? </div><div><br class=""></div><div>The range of an open enum is the same as the range determined by the standard. So for a fixed enum, the range is the range of the underlying type, and for an unfixed enum, the range is determined by the rules you mentioned.</div><div><br class=""></div><div>The ranges of closed enums are basically the subsets of the ranges of open enums.</div><div><br class=""></div><div>“choice,closed” enums can only take values of the enumerators defined in the enum definition.</div><div><br class=""></div><div>So if we had an enum like this:</div><div><br class=""></div><div>enum E1 {</div><div>  b = 1, e = 10 </div><div>};</div><div><br class=""></div><div>clang would issue a warning when it sees the following assignment even though the value assigned is in range according to the standard:</div><div><br class=""></div><div>enum E1 x = 2;</div><div><br class=""></div><div><div>The range of an “option,closed” enum would be the range of a flag_enum, excluding values that aren’t in the range determined by the standard.</div><div><br class=""></div><div>If we had an enum like this:</div><div><br class=""></div><div>enum E2 : int {</div><div>  a = 1, b = 2, c = 4, d = 16</div><div>};</div><div><br class=""></div><div>This would be a valid assignment:</div><div><br class=""></div><div>enum E2 x = (enum E2)(a | b);</div><div><br class=""></div><div><div>and masks (bitwise NOT of valid values) are valid too:</div><div><br class=""></div><div>enum E2 x = (enum E2)(~a);</div><div class=""><br class=""></div></div><div>but a warning would be issued for this:</div><div><br class=""></div><div>enum E2 x = (enum E2)(a | 8);</div><div><br class=""></div><div>For enums that don’t have fixed underlying types, mask values can be invalid if they aren't listed in the enum definition.</div><div><br class=""></div><div>For example:</div><div><br class=""></div><div><div>enum E3  {</div><div>  a = 1</div><div>};</div><div class=""><br class=""></div><div class="">clang would issue a warning for the following assignment since ~a is not in the range:</div><div class=""><br class=""></div></div><div>enum E3 x = (enum E3)(~a);</div><div><br class=""></div></div><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class="">If it’s marked “open”, it doesn’t have the restriction. </div></div></div></div></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class="">There are four possible combinations:</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div class="">1. "choice, closed"</div><div class="">2. "choice, open"</div><div class=""><div class="">3. "option, closed"</div><div class="">4. "option, open"</div></div><div class=""><br class=""></div><div class="">Attribute “flag_enum” we have today is equivalent to "option,closed".</div><div class=""><br class=""></div><div class="">In addition, I’m considering adding a command line option that specifies the default enum-style for unannotated enums.</div></div></div></div></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">A command-line option amounts to adding a new language dialect; that seems like a bad idea to me.</div><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><div class="">### Motivation for the new attribute ###</div></div><div dir="auto" style="word-wrap: break-word;" class="">There are several areas that can be improved using the new attribute and command line option.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">1. Warnings</div><div dir="auto" style="word-wrap: break-word;" class="">The new attribute can improve the accuracy of enum-related warnings such as -Wassign-enum and give better control over when the warnings are issued.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">-Wassign-enum currently warns whenever a value that is out of the range determined by the enumerators is assigned to an enum variable. For a flag-enum, a value is in range if it can be created by ORing the enumerators listed in the enum definition or is a complement of one of the in-range values. For enums that are not flag-enums, only the values of the enumerators listed in the enum definition are considered to be in range. The warning is helpful in catching out-of-range values that are unintentionally assigned, but would be too strict if a project intentionally extended an enum by defining out-of-range “private” values (this does happen often). If the compiler knows an enum is “open”, it can choose not to issue warnings.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">Another problem with the current approach is that all the enums the compiler sees have to be classified into flag-enums or non-flag-enums and the flag-enums have to be annotated. This requires a lot of work up front to determine whether or not an enum is a flag-enum, and sometimes it’s not even possible to annotate the enums if they are defined in a third party library that cannot be modified. With the command line option for specifying the default enum-style, users can instruct the compiler not to issue warnings if the enum is unannotated (the default can be either "choice,open” or “option,open”) and add the attributes to the enum definitions in an incremental fashion.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">2. Code-completion and debugging</div><div dir="auto" style="word-wrap: break-word;" class="">Code-completion tools can offer better suggestions based on whether the enum is a choice or an option. For example, if we had an "option" enum like this:</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">enum __attribute((enum_style(<wbr class="">option, closed)))__ MyEnum {</div><div dir="auto" style="word-wrap: break-word;" class=""> <span class="Apple-converted-space"> </span>E1 = 1, E2 = 2,</div><div dir="auto" style="word-wrap: break-word;" class="">};</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">and the user requests a code completion for this:</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">MyEnum = E1 | <esc></div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">code completion would recognize MyEnum is an “option" enum and could offer E2 as a suggestion. If MyEnum were a “choice" enum, it would offer no suggestions.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">Debugging experience can be improved too. For example, when a MyEnum variable is set to 3, the debugger could show “E1 | E2” instead of showing the raw value 3.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">3. clang importer</div><div dir="auto" style="word-wrap: break-word;" class="">In order to determine whether a C or ObjC enum maps to an enum or option set in swift, swift’s clang importer looks at whether the enum was declared using macros such as CF_ENUM or CF_OPTIONS (the macros are explained in the link below). With the proposed attribute, this hack can be removed.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class=""><div dir="auto" style="word-wrap: break-word;" class=""><a href="https://developer.apple.com/library/content/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html" target="_blank" class="">https://developer.apple.com/<wbr class="">library/content/releasenotes/<wbr class="">ObjectiveC/ModernizationObjC/<wbr class="">AdoptingModernObjective-C/<wbr class="">AdoptingModernObjective-C.html</a></div><div class=""><br class=""></div></div><div dir="auto" style="word-wrap: break-word;" class="">4. Optimization</div><div dir="auto" style="word-wrap: break-word;" class="">There is a command line option named -fstrict-enums, which allows the compiler to optimize code using the assumption that the value of an enum variable is in range. This option can safely be used only if it is known that the values the variables of enum types can take are always in range. With the new attribute, the compiler can focus on variables of enum types that are marked “closed” and optimize them and leave other variables unoptimized.</div><div dir="auto" style="word-wrap: break-word;" class=""><br class=""></div><div dir="auto" style="word-wrap: break-word;" class="">### What will change ###</div><div dir="auto" style="word-wrap: break-word;" class="">I plan to add support for the new attribute in Sema and change the code that issues warning. I’m not planning to work on the IRGen optimization that takes advantage of the information the new attribute provides. It will be left as future work.</div></div></div></div></div></div></div></div><br class="">______________________________<wbr class="">_________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/<wbr class="">mailman/listinfo/cfe-dev</a></blockquote></div></div></div></div></blockquote></div><br class=""></body></html>