struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tcc / 94_generic.c
blob4a68bcdd351550e643e4bb366444df28e17d9c88
1 #include <stdio.h>
3 const int a = 0;
5 struct a {
6 int a;
7 };
9 struct b {
10 int a;
13 int a_f()
15 return 20;
18 int b_f()
20 return 10;
23 typedef int (*fptr)(int);
24 typedef void (*vfptr)(int);
25 int foo(int i)
27 return i;
29 void void_foo(int i) {}
31 typedef int int_type1;
33 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
35 int main()
37 int i = 0;
38 signed long int l = 2;
39 struct b titi;
40 const int * const ptr;
41 const char *ti;
42 int_type1 i2;
44 i = _Generic(a, int: a_f, const int: b_f)();
45 printf("%d\n", i);
46 i = _Generic(a, int: a_f() / 2, const int: b_f() / 2);
47 printf("%d\n", i);
48 i = _Generic(ptr, int *:1, int * const:2, default:20);
49 printf("%d\n", i);
50 i = gen_sw(a);
51 printf("%d\n", i);
52 i = _Generic(titi, struct a:1, struct b:2, default:20);
53 printf("%d\n", i);
54 i = _Generic(i2, char: 1, int : 0);
55 printf("%d\n", i);
56 i = _Generic(a, char:1, int[4]:2, default:5);
57 printf("%d\n", i);
58 i = _Generic(17, int :1, int **:2);
59 printf("%d\n", i);
60 i = _Generic(17L, int :1, long :2, long long : 3);
61 printf("%d\n", i);
62 i = _Generic("17, io", char *: 3, const char *: 1);
63 printf("%d\n", i);
64 i = _Generic(ti, const unsigned char *:1, const char *:4, char *:3,
65 const signed char *:2);
66 printf("%d\n", i);
67 printf("%s\n", _Generic(i + 2L, long: "long", int: "int",
68 long long: "long long"));
69 i = _Generic(l, long: 1, int: 2);
70 printf("%d\n", i);
71 i = _Generic(foo, fptr: 3, int: 4, vfptr: 5);
72 printf("%d\n", i);
73 i = _Generic(void_foo, fptr: 3, int: 4, vfptr: 5);
74 printf("%d\n", i);
76 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
78 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
79 struct { _Bool x_0: _Generic(({0;}),default:1); } my_x;
81 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
82 int const x = 42; __typeof((__typeof(x))x) *xp = 0; (void)_Generic(xp, int*: 0); //casts lose top-level qualifiers
84 //TEST TERNARY:
85 //Same type
86 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
87 //combining of qualifiers
88 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
89 //nul-ptr constant selects other type
90 _Generic( 0?(long*)0:0, long*: (void)0);
91 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
93 //void ptrs get chosen preferentially; qualifs still combine
94 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
95 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
96 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
98 //ptrs to incomplete types get completed
99 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
100 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
103 /* completion shouldn't affect the type of decl */
104 char **argv;
105 _Generic(argv, char**: (void)0);
106 _Generic(0?(char const*)0:argv[0], char const*: (void)0);
107 _Generic(argv, char**: (void)0);
110 extern int (*ar)[];
111 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
112 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
113 (void)(sizeof(struct { int x:_Generic( 0?ar : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
114 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : ar, int (*)[4]:+1, int (*)[5]:(void)0); }));
115 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[5])0 : ar, int (*)[5]:+1, int (*)[4]:(void)0); }));
118 return 0;