Replies: 3 comments 6 replies
-
|
I agree that all the current way requires quite a bit of typing. However, it is simple code and use (IMHO) one of the most powerful features of C-compilers: warn on missing enum cases. So if you add an enum case, the compiler will warn you about all the places you have to add it (provided there is no default case). With a dispatch tables/mechanism, the function prototypes need to be the same, so the casts would have to happen in the functions themselves. The syntax you propose looks a bit strange. You pass an enum type as an array index and that should become a dispatch table? |
Beta Was this translation helpful? Give feedback.
-
|
This is still talking about manually tagged types, right? |
Beta Was this translation helpful? Give feedback.
-
|
I still dont get how you propose to couple the enum type to the Types. I didn't see any syntax for that. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Dispatching a method to specialized handlers defined for derived types is a classic object oriented feature that we must emulate in c2 with cumbersome
switchstatements such as this:The pattern is so obvious that the code could be written automatically as if by a macro.
Thanks to the namespace isolation, the enum constants returned by
t.getKind()could actually be identical to the type names, which would make the intent even more obvious:This could be implemented with a dispatch table:
I propose for the compiler to handle this automatically with a specific syntax:
Whether the code generated uses a dispatch table, a
switchstatement or some other efficient scheme is an implementation choice depending on the number of enumerated types. Generating a switch statement allows the compiler to expand the code for the handlers inline for maximum efficiency and generate tests instead of a jump table if they are more efficient on the target CPU, thanks to jump prediction cacheing.It might help to make the
enummore explicit with an implementation type astype, but this does not seem necessary. The explicit (small) integer type helps cram it in a small corner of the base type, which could even be a scalar.Beta Was this translation helpful? Give feedback.
All reactions