fixup! riscv: Implement large addend for global address
[tinycc.git] / tests / tests2 / 94_generic.c
blob7ad9dd067c2732d3ae36e6c5e5241c67078527bc
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 typedef int T[4];
34 int f(T t)
36 return _Generic(t, __typeof__( ((void)0, (T){0}) ) : 1 );
39 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
41 int main()
43 int i = 0;
44 signed long int l = 2;
45 struct b titi;
46 const int * const ptr;
47 const char *ti;
48 int_type1 i2;
49 T t;
51 i = _Generic(a, int: a_f, const int: b_f)();
52 printf("%d\n", i);
53 i = _Generic(a, int: a_f() / 2, const int: b_f() / 2);
54 printf("%d\n", i);
55 i = _Generic(ptr, int *:1, int * const:2, default:20);
56 printf("%d\n", i);
57 i = gen_sw(a);
58 printf("%d\n", i);
59 i = _Generic(titi, struct a:1, struct b:2, default:20);
60 printf("%d\n", i);
61 i = _Generic(i2, char: 1, int : 0);
62 printf("%d\n", i);
63 i = _Generic(a, char:1, int[4]:2, default:5);
64 printf("%d\n", i);
65 i = _Generic(17, int :1, int **:2);
66 printf("%d\n", i);
67 i = _Generic(17L, int :1, long :2, long long : 3);
68 printf("%d\n", i);
69 i = _Generic("17, io", char *: 3, const char *: 1);
70 printf("%d\n", i);
71 i = _Generic(ti, const unsigned char *:1, const char *:4, char *:3,
72 const signed char *:2);
73 printf("%d\n", i);
74 printf("%s\n", _Generic(i + 2L, long: "long", int: "int",
75 long long: "long long"));
76 i = _Generic(l, long: 1, int: 2);
77 printf("%d\n", i);
78 i = _Generic(foo, fptr: 3, int: 4, vfptr: 5);
79 printf("%d\n", i);
80 i = _Generic(void_foo, fptr: 3, int: 4, vfptr: 5);
81 printf("%d\n", i);
83 (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice
85 //should accept ({ }) in the controlling expr of _Generic even in const_wanted contexts
86 struct { _Bool x_0: _Generic(({0;}),default:1); } my_x;
88 _Generic((__typeof((float const)((float const){42}))*){0}, float*: 0); //casts lose top-level qualifiers
89 int const x = 42; __typeof((__typeof(x))x) *xp = 0; (void)_Generic(xp, int*: 0); //casts lose top-level qualifiers
91 //TEST TERNARY:
92 //Same type
93 _Generic( 0?(long*)0:(long*)0, long*: (void)0);
94 //combining of qualifiers
95 _Generic( 0?(long volatile*)0:(long const*)0, long const volatile*: (void)0);
96 //nul-ptr constant selects other type
97 _Generic( 0?(long*)0:0, long*: (void)0);
98 _Generic( 0?(long*)0:(void*)0, long*: (void)0);
100 //void ptrs get chosen preferentially; qualifs still combine
101 _Generic( 0?(int volatile*)0: (void const*)1, void volatile const*: (void)0);
102 //but this is no null-ptr constant, so fallback to void-choice
103 i = 0;
104 _Generic( 1?(void*)(i*0LL):&i, void*:0);
105 //like gcc but not clang, don't treat (void* const as the null-ptr constant)
106 _Generic( 0?(int volatile*)0: (void const*)0, void volatile const*: (void)0);
108 //ptrs to incomplete types get completed
109 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
110 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
113 /* completion shouldn't affect the type of decl */
114 char **argv;
115 _Generic(argv, char**: (void)0);
116 _Generic(0?(char const*)0:argv[0], char const*: (void)0);
117 _Generic(argv, char**: (void)0);
120 extern int (*ar)[];
121 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : (int (*)[])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
122 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[])0 : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
123 (void)(sizeof(struct { int x:_Generic( 0?ar : (int (*)[4])0, int (*)[4]:+1, int (*)[5]:(void)0); }));
124 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[4])0 : ar, int (*)[4]:+1, int (*)[5]:(void)0); }));
125 (void)(sizeof(struct { int x:_Generic( 0?(int (*)[5])0 : ar, int (*)[5]:+1, int (*)[4]:(void)0); }));
128 printf ("%d\n", f(t));
130 return 0;