[Bug c/112101] feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments

2023-10-27 Thread malekwryyy at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

--- Comment #2 from Abdulmalek Almkainzi  ---
Another correction, I'm sorry, its a bit hard to write hypothetical code.
the macro print_func:

#define print_func(f) \
printf( \
_Generic( (__typeof_arg(f, 0)){0}, \
int: #f "(int)", \
long:#f "(long)", \
float:   #f "(float)", \
char*:   #f "(char*)", \
default: #f "(other)") \
)

[Bug c/112101] feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments

2023-10-26 Thread malekwryyy at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

--- Comment #1 from Abdulmalek Almkainzi  ---
correction for the gurantee_type macro:

#define gurantee_type(exp, type) \
_Generic(exp, type: exp, default: (type){0})

[Bug c/112101] New: feature request: typeof_arg for extracting the type of a function's (or function pointer's) arguments

2023-10-26 Thread malekwryyy at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112101

Bug ID: 112101
   Summary: feature request: typeof_arg for extracting the type of
a function's (or function pointer's) arguments
   Product: gcc
   Version: unknown
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: malekwryyy at gmail dot com
  Target Milestone: ---

C23 will add typeof (although gcc had a for a while) which gives the type of an
expression or a type. By using it, it is possible to get the return type of a
function like so:

```
int func();

typeof(func()) x; // int x; 
```

But there's no way to extract the type of the argument of a function:

```
void func(int);

?? x;
```
I think something like 'typeof_arg' would be a good addition. 

It takes 2 operands, first is function or function pointer, and second is an
integer constant for the index of the argument, which must be within [0,
arg_count).

for example:
```
#define print_func(f) \
printf(#f \
"(" \
_Generic( (__typeof_arg(f, 0)){0}, \
int: "int", \
long:"long", \
float:   "float", \
char*:   "char*", \
default: "other ") \
")")
```
this would print a single-argument function's name and arg type like this
"puts(char*)".

another example:
```
#define gurantee_type(exp, type) \
_Generic(exp, type: exp, default: (typeof(exp)){0})

#define call_with_empty(f) \
_Generic( (__typeof_arg(f, 0)){0}, \
char*: gurantee_type(f, void(*)(char*))(""), \
default: f( (__typeof_arg(f, 0)){0} ) \
)
```
which calls the function 'f' with empty string if it takes char*, or 0 of the
correct type otherwise.

this wouldn't work for variadic functions, so __typeof_arg(printf, 1) would be
an error.

I think a feature like this would be really helpful for generic programming in
C

[Bug c/111421] constexpr not working with array subscript

2023-09-15 Thread malekwryyy at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111421

--- Comment #3 from Abdulmalek Almkainzi  ---
(In reply to jos...@codesourcery.com from comment #1)
> See the definitions of "named constant" and "compound literal constant".  
> Array element accesses aren't allowed, and the example you have with "->" 
> shouldn't be accepted either (although the standard rules for 
> implementation-defined constant expressions probably allow implementations 
> to accept such an example if they so choose).

Alright, I see how this is not a GCC bug and instead part of the standard. 

But may I ask how this is even the case? the expression is constant and can be
evaluated at compile time.

[Bug c/111421] New: constexpr not working with array subscript

2023-09-14 Thread malekwryyy at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111421

Bug ID: 111421
   Summary: constexpr not working with array subscript
   Product: gcc
   Version: 13.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: malekwryyy at gmail dot com
  Target Milestone: ---

Created attachment 55903
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55903=edit
the c program that results in the error

compiled with: -std=c2x -Wall -Wextra

When trying to subscript a constexpr array, the compiler says the expression is
not constant. 

I would expect if the index is an integer literal (or a constexpr integer) then
the resulting expression should still be constant.

I tried this with -std=c2x and -std=gnu2x

// program start

typedef struct S { int i; } S;

constexpr int a = (constexpr S){.i = 3}.i; // works as expected

constexpr int b = ( &(constexpr S){.i = 3} )->i; // works as expected

constexpr int y = (constexpr int[]){3}[0]; // doesn't work. Compiler says
expression not constant

int main()
{
return 0;
}

// program end

the compiler error I get is:

:7:19: error: initializer element is not constant
7 | constexpr int y = (constexpr int[]){3}[0]; // doesn't work. Compiler
says expression not constant
  |   ^
:7:15: warning: 'y' defined but not used [-Wunused-const-variable=]
7 | constexpr int y = (constexpr int[]){3}[0]; // doesn't work. Compiler
says expression not constant
  |   ^
:5:15: warning: 'b' defined but not used [-Wunused-const-variable=]
5 | constexpr int b = ( &(constexpr S){.i = 3} )->i; // works as expected
  |   ^
:3:15: warning: 'a' defined but not used [-Wunused-const-variable=]
3 | constexpr int a = (constexpr S){.i = 3}.i; // works as expected
  |   ^
Compiler returned: 1