Search code examples
cdictionaryc11

Getting the Address of an RValue inside Macro Definition


I have been working on a map type implementation in C the last couple of days, and wanted to give the map definition the ability to have multiple types. Using void pointers and unions, I've been able to accomplish that effect well so far, but I've recently run into a problem while writing a function to grab a member of the map.

The problem is that I want the user to be able to pass rvalues to the "GetMapItemValue" function, to avoid the hassle of creating a temporary variable explicitly each time the function is called. The get function, however, takes in a void pointer as an argument, and you cannot get the address of an rvalue.

I can solve this problem by biting the bullet and writing a bunch of different functions for each type case (using some kind of _Generic macro or the like), but I would rather come up with a more pragmatic solution. My code is included below.

Map.h (header file for map implementation)

typedef struct KeyPair
{
    u8 set;
    AmbiguousType key, value;
} KeyPair;

typedef struct Map
{
    u32 max_size, filled_size;
    AmbiguousTypeSpecifier value_type, key_type;
    KeyPair* map_values;
} Map;

KeyPair CreateKeyPair(AmbiguousTypeSpecifier key_type,
                      AmbiguousTypeSpecifier value_type, void* key,
                      void* value);

#define GetMapItemValue(map, key_value)                                        \
    __GetMapItemValue(map, (void*)&key_value)
void* __GetMapItemValue(Map* map, void* key_value);

#define GetMapItemValueCharacter(map, key)  *((char*)GetMapItemValue(map, key))
#define GetMapItemValueUnsigned32(map, key) *((u32*)GetMapItemValue(map, key))
#define GetMapItemValueUnsigned64(map, key) *((u64*)GetMapItemValue(map, key))
#define GetMapItemValueSigned32(map, key)   *((i32*)GetMapItemValue(map, key))
#define GetMapItemValueSigned64(map, key)   *((i64*)GetMapItemValue(map, key))

Map.c (definition file for map implementation)

KeyPair CreateKeyPair(AmbiguousTypeSpecifier key_type,
                      AmbiguousTypeSpecifier value_type, void* key, void* value)
{
    KeyPair created = {SUCCESS};
    AssignAmbiguousType(&created.key, key_type, key);
    AssignAmbiguousType(&created.value, value_type, value);

    return created;
}

void* __GetMapItemValue(Map* map, void* key_value)
{
    for (u32 index = 0; index < map->max_size; index++)
    {
        if (!CompareAmbiguousType(&map->map_values[index].key, map->key_type,
                                  key_value))
            continue;

        return GetAmbiguousType(&map->map_values[index].value, map->value_type);
    }
    return NULL;
}

Declarations.h (header file for utilities)

#define SUCCESS 1
#define FAILURE 0

typedef enum AmbiguousTypeSpecifier
{
    character,
    unsigned32,
    unsigned64,
    signed32,
    signed64
} AmbiguousTypeSpecifier;

typedef union AmbiguousType
{
    char character;
    u32 unsigned32;
    u64 unsigned64;
    i32 signed32;
    i64 signed64;
} AmbiguousType;

void AssignAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                         void* value);

void* GetAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member);

u8 CompareAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                        void* value);

Declarations.c (definitions file for utilities)

void AssignAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                         void* value)
{
    switch (member)
    {
        case character:  affected->character = *((char*)value); return;
        case unsigned32: affected->unsigned32 = *((u32*)value); return;
        case unsigned64: affected->unsigned64 = *((u64*)value); return;
        case signed32:   affected->signed32 = *((i32*)value); return;
        case signed64:   affected->signed64 = *((i64*)value); return;
    }
}

void* GetAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member)
{
    switch (member)
    {
        case character:  return (void*)&affected->character;
        case unsigned32: return (void*)&affected->unsigned32;
        case unsigned64: return (void*)&affected->unsigned64;
        case signed32:   return (void*)&affected->signed32;
        case signed64:   return (void*)&affected->signed64;
    }
    return NULL;
}

u8 CompareAmbiguousType(AmbiguousType* affected, AmbiguousTypeSpecifier member,
                        void* value)
{
    switch (member)
    {
        case character:
            if (affected->character == *((char*)value)) return SUCCESS;
            return FAILURE;
        case unsigned32:
            if (affected->unsigned32 == *((u32*)value)) return SUCCESS;
            return FAILURE;
        case unsigned64:
            if (affected->unsigned64 == *((u64*)value)) return SUCCESS;
            return FAILURE;
        case signed32:
            if (affected->signed32 == *((i32*)value)) return SUCCESS;
            return FAILURE;
        case signed64:
            if (affected->signed64 == *((i64*)value)) return SUCCESS;
            return FAILURE;
    }
    return FAILURE;
}

Entry.c (entry file for application)

i32 main(void)
{
    ...

    Map* created_map = CreateMap(character, unsigned32, 10);

    AppendMapItem(created_map, 'c', 23);
    printf("%d\n", GetMapItemValueUnsigned32(created_map, 'c'));

    DestroyMap(created_map);

    ...

    return 0;
}

The problem rears its ugly head in Entry.c, when the rvalue 'c' (aka 99) is passed, and subsequently the address operator is called upon it (&key_value).

I have tried to do all kinds of very strange (to me, a C novice) pointer operations on the value to turn it into a lvalue. I can achieve what I want using the GNU statement expression extension, but this project is cross-platform, which means it must compile with the Visual Studio compiler (CL).

I previously solved this problem in a former function "AppendMapItem" by creating a sort of sub-scope to define temporary variables before getting the address, but since this function must return something, that's off the table.

Map.h (see above)

// ignore how ugly this is, please
#define AppendMapItem(map, key, value)                                         \
    {                                                                          \
        __typeof__(key) key_value = key;                                       \
        __typeof__(value) value_value = value;                                 \
        __AppendMapItem(map, (void*)&key_value, (void*)&value_value);          \
    }
void __AppendMapItem(Map* map, void* key, void* value);

Solution

  • The question is:

    is there a way to create a temporary variable with (or otherwise convert to an lvalue) an rvalue argument passed into a C macro? Specifically without using the GNU statement expression extension

    If you can constrain the count of types, then you can make pointers. A simple generic with temporary compound literals will do.

    #define MAKE_POINTER(x) \
        _Generic((x) \
           , char: (char[1]){x} \
           , uint32_t: (uint32_t[1]){x} \
           /* etc */ \
           )
    

    And then your call becomes:

    #define AppendMapItem(map, key, value)  \
            __AppendMapItem(map, MAKE_POINTER(key), MAKE_POINTER(value))
    

    Notes:

    • the type of 'c' is an int, not char
    • prefer to use standard types from stdint.h