This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Request of new __attribute__ for switch statements (elimination of the bounds check)


On Tue, Oct 15, 2002 at 02:41:01AM +0100, Jamie Lokier wrote:
> I would prefer to have an attribute on enumurated types that says a
> value of that type is always one of the enum values:
> 
>   enum __attribute__ ((strict_enum)) { CAT, FISH, RABBIT } Pet;
> 
> (It would be appropriate to add a warning when an enum with this
> attribute is converted to an integer).
> 
> Then any switch statement, without adornment, would be able to assume
> a value of that type is in the range.  _If_ every enum label is
> mentioned in the switch, there is no need for the bounds check.  (GCC
> already checks enum labels in a switch if `-Wswitch' is used, which
> may be helpful).

If there is an enumerated type that doesn't exhaust the domain of its
underlying integral type, then I confidently expect that a data
corruption bug will cause the switch to receive a selector outside the
domain of the enumeration; in that case I want there to be a
default:abort() in there so it gets caught early.

This does not mean that your idea is a bad one; the attribute could be
used for stricter type checking and more effective warnings, which is
a good thing.  I just don't like the idea of using it to optimize out
bounds checks.  (Instead, how about transforming your example

>   Pet my_pet;
>   /* ... */
>   switch (my_pet) {
>     case CAT:    /* ... */
>     case FISH:   /* ... */
>     case RABBIT: /* ... */
>   }

by inserting the default:abort() for the programmer?)

> You can use this attribute to achieve Kevin's goal of faster threaded
> interpretation, but it is a bit ugly.  For a byte-code dispatch, you'd
> have to define an enum with 256 scratch names, and cast your byte to
> that type in the switch.  For a sparse dispatch, you'd have to use a
> different enum type.  It's a bit ugly but might be ok with macros.

I would far rather solve this problem by having us notice when a
dispatch switch() really has exhausted the domain of the integral
type of its argument (before conversion to int).

zw


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]