First of all, thanks a lot for doing this.  Maybe one day we'll have
the same in rtl :-)

But...

David Malcolm <dmalc...@redhat.com> writes:
> In doing the checked downcasts I ran into the verbosity of the as_a <>
> API (in our "is-a.h").  I first tried simplifying them with custom
> functions e.g.:
>
>    static inline gimple_bind as_a_gimple_bind (gimple gs)
>    {
>      return as_a <gimple_statement_bind> (gs);
>    }
>
> but the approach I've gone with makes these checked casts be *methods* of
> the gimple_statement_base class, so that e.g. in a switch statement you
> can write:
>
>     case GIMPLE_SWITCH:
>       dump_gimple_switch (buffer, gs->as_a_gimple_switch (), spc, flags);
>       break;
>
> where the ->as_a_gimple_switch is a no-op cast from "gimple" to the more
> concrete "gimple_switch" in a release build, with runtime checking for
> code == GIMPLE_SWITCH added in a checked build (it uses as_a <>
> internally).
>
> This is much less verbose than trying to do it with as_a <> directly, and
> I think doing it as a method reads better aloud (to my English-speaking
> mind, at-least):
>   "gs as a gimple switch",
> as opposed to:
>   "as a gimple switch... gs",
> which I find clunky.
>
> It makes the base class a little cluttered, but IMHO it hits a sweet-spot
> of readability and type-safety with relatively little verbosity (only 8
> more characters than doing it with a raw C-style cast).   Another
> advantage of having the checked cast as a *method* is that it implicitly
> documents the requirement that the input must be non-NULL.

...FWIW I really don't like these cast members.  The counterarguments are:

- as_a <...> (...) and dyn_cast <...> (...) follow the C++ syntax
  for other casts.

- the type you get is obvious, rather than being a contraction of
  the type name.

- having them as methods means that the base class needs to aware of
  all subclasses.  I realise that's probably inherently true of gimple
  due to the enum, but it seems like bad design.  You could potentially
  have different subclasses for the same enum, selected by a secondary field.

Maybe I've just been reading C code too long, but "as a gimple switch...gs"
doesn't seem any less natural than "is constant...address".

Another way of reducing the verbosity of as_a would be to shorten the
type names.  E.g. "gimple_statement" contracts to "gimple_stmt" in some
places, so "gimple_statement_bind" could become "gimple_stmt_bind" or
just "gimple_bind".  "gimple_bind" is probably better since it matches
the names of the accessors.

If the thing after "as_a_" matches the type name, the "X->as_a_foo ()"
takes the same number of characters as "as_a <foo> (X)".

Thanks,
Richard

Reply via email to