cake icon indicating copy to clipboard operation
cake copied to clipboard

Feature: C23 improved tag compatibility missing implementation

Open ib00 opened this issue 1 year ago • 3 comments

Not many compilers (currently only gcc trunk) support C23 improved tag compatibility: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3037.pdf

Some data structures would be easier with this feature supported.

ib00 avatar Apr 08 '24 17:04 ib00

The idea for this feature is just remove the struct members from struct type. For instance

struct X { int i; };
void f(struct X { int i; } x){ }

generated code

struct X { int i; };
void f(struct X x) {}

What is missing is the comparison with the previous tag X.

thradams avatar Apr 08 '24 20:04 thradams

Here's an example that I have been plating with:

#define Array(TYPE) struct TYPE##Array {TYPE* elements; int count;}

void foo(Array(int) myArr) {
    // do something
}

int main() {
    Array(int) myInts;
    myInts.elements = (int*) (int[3]) {3, 2, 1};
    myInts.count = 3;
    foo(myInts);
    return 0;
}

This should compile with C23. At the moment only gcc trunk supports this.

ib00 avatar Apr 08 '24 20:04 ib00

There are some details. structs declared inside arguments does not have global scope. Then cake would have to introduce one declaration. or, if the user puts the like here

#define Array(TYPE) struct TYPE##Array {TYPE* elements; int count;}
#pragma expand Array

Array(int);

void foo(Array(int) myArr) {
    // do something
}

int main() {
    Array(int) myInts;
    myInts.elements = (int*) (int[3]) {3, 2, 1};
    myInts.count = 3;
    foo(myInts);
    return 0;
}

then the generated code would be


```c
#define Array(TYPE) struct TYPE##Array {TYPE* elements; int count;}
#pragma expand Array


struct intArray {int* elements; int count;};

void foo(struct intArray myArr) {

}

int main() {
    struct intArray myInts;
    myInts.elements = (int*) (int[3]) {3, 2, 1};
    myInts.count = 3;
    foo(myInts);
    return 0;
}

thradams avatar Apr 09 '24 00:04 thradams