[GSoC] Header Generation for C/C++

Manu turkeyman at gmail.com
Mon Jul 22 18:12:01 UTC 2019


On Mon, Jul 22, 2019 at 2:51 AM Jacob Carlborg via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On 2019-07-17 22:59, Manu wrote:
>
> > Okay... but it's also the worst thing ever, and no real user would
> > want this code to be emit from the compiler ;)
> > Safe to say this is a strictly DMD-specific naming pattern, and I
> > don't think that should be our benchmark for a public-facing feature.
> >
> > A better non-C++11 name might be `TestEnum_aa`?
>
> In Apple's frameworks, in particular the Objective-C frameworks, they
> define enums like this:
>
> typedef NS_ENUM(int, MKAnnotationViewDragState)
> {
>      MKAnnotationViewDragStateNone = 0,
>      MKAnnotationViewDragStateStarting,
>      MKAnnotationViewDragStateDragging,
>      MKAnnotationViewDragStateCanceling,
>      MKAnnotationViewDragStateEnding
> }
>
> Where NS_ENUM expands, in the above case, to:
>
> typedef int MKAnnotationViewDragState; enum
> {
>      MKAnnotationViewDragStateNone = 0,
>      MKAnnotationViewDragStateStarting,
>      MKAnnotationViewDragStateDragging,
>      MKAnnotationViewDragStateCanceling,
>      MKAnnotationViewDragStateEnding
> }
>
> Probably makes it easier to present a nice interface in Swift.
>
> --
> /Jacob Carlborg

Great idea!
We can make the generated header emit this:

#ifndef D_ENUM
# define D_ENUM(name, type) enum name
#endif
#ifndef D_ENUM_KEY
# define D_ENUM_KEY(key, enumType) enumType##_##key
#endif
#ifndef D_ENUM_KEY_VAL
# define D_ENUM_KEY_VAL(key, value, enumType) enumType##_##key = value
#endif

D_ENUM(DEnum, int)
{
  D_ENUM_KEY(A, DEnum),
  D_ENUM_KEY_VAL(B, 10, DEnum),
};



And then someone can override those with:

#define D_ENUM(name, type) enum class name : type
#define D_ENUM_KEY(name, enumType) name
#define D_ENUM_KEY_VAL(name, value, enumType) name = value

#include "my_d_header.h"



And for over-powered win, define the macros like this:

# define D_ENUM(name, lower, upper, type) ...
# define D_ENUM_KEY(key, keyLower, keyUpper, type, typeLower, typeUpper) ...

And then the header emits:
D_ENUM(DEnum, denum, DENUM, int)
{
  D_ENUM_KEY(MyKey, mykey, MYKEY, DEnum, denum, DENUM),
}

This allows maximum flexibility! It can support the D compilers weird
naming strategy without adding additional options to the compiler.


More information about the Digitalmars-d mailing list