1 1.7 rillig /* $NetBSD: decl_direct_abstract.c,v 1.7 2022/06/17 18:54:53 rillig Exp $ */ 2 1.1 rillig # 3 "decl_direct_abstract.c" 3 1.1 rillig 4 1.1 rillig /* 5 1.1 rillig * Test parsing of direct-abstract-declarator (C99 6.7.6), which are a tricky 6 1.1 rillig * part of the C standard since they require lookahead and are so complicated 7 1.1 rillig * that GCC's parser dedicates 34 lines of comments to this topic. 8 1.1 rillig * 9 1.1 rillig * See msg_155.c. 10 1.1 rillig */ 11 1.1 rillig 12 1.1 rillig /* 13 1.1 rillig * The following tests do not use int, to avoid confusion with the implicit 14 1.1 rillig * return type. 15 1.1 rillig */ 16 1.1 rillig 17 1.1 rillig char func0001(short (*)(long)); 18 1.1 rillig 19 1.1 rillig /* GCC says 'char (*)(short int (*)(long int))' */ 20 1.1 rillig /* Clang says 'char (short (*)(long))' */ 21 1.1 rillig /* cdecl says 'function (pointer to function (long) returning short) returning char' */ 22 1.7 rillig /* expect+1: error: cannot initialize 'double' from 'pointer to function(pointer to function(long) returning short) returning char' [185] */ 23 1.1 rillig double type_of_func0001 = func0001; 24 1.1 rillig 25 1.1 rillig char func0002(short *(long)); 26 1.1 rillig 27 1.1 rillig /* GCC says 'char (*)(short int * (*)(long int))' */ 28 1.1 rillig /* Clang says 'char (short *(*)(long))' */ 29 1.1 rillig /* cdecl says 'syntax error' */ 30 1.1 rillig /* FIXME: lint is wrong, it discards the 'short *' */ 31 1.7 rillig /* expect+1: error: cannot initialize 'double' from 'pointer to function(long) returning char' [185] */ 32 1.1 rillig double type_of_func0002 = func0002; 33 1.1 rillig 34 1.1 rillig void c99_6_7_6_example_a(int); 35 1.1 rillig void c99_6_7_6_example_b(int *); 36 1.1 rillig void c99_6_7_6_example_c(int *[3]); 37 1.1 rillig void c99_6_7_6_example_d(int (*)[3]); 38 1.1 rillig void c99_6_7_6_example_e(int (*)[*]); 39 1.1 rillig void c99_6_7_6_example_f(int *()); 40 1.1 rillig void c99_6_7_6_example_g(int (*)(void)); 41 1.1 rillig void c99_6_7_6_example_h(int (*const[])(unsigned int, ...)); 42 1.1 rillig 43 1.1 rillig struct incompatible { 44 1.1 rillig int member; 45 1.1 rillig } x; 46 1.1 rillig 47 1.7 rillig /* expect+1: ... 'pointer to function(int) returning void' ... */ 48 1.1 rillig double type_of_c99_6_7_6_example_a = c99_6_7_6_example_a; 49 1.7 rillig /* expect+1: ... 'pointer to function(pointer to int) returning void' ... */ 50 1.1 rillig double type_of_c99_6_7_6_example_b = c99_6_7_6_example_b; 51 1.7 rillig /* expect+1: ... 'pointer to function(pointer to pointer to int) returning void' ... */ 52 1.1 rillig double type_of_c99_6_7_6_example_c = c99_6_7_6_example_c; 53 1.7 rillig /* expect+1: ... 'pointer to function(pointer to array[3] of int) returning void' ... */ 54 1.1 rillig double type_of_c99_6_7_6_example_d = c99_6_7_6_example_d; 55 1.7 rillig /* expect+1: ... 'pointer to function(pointer to array[unknown_size] of int) returning void' ... */ 56 1.1 rillig double type_of_c99_6_7_6_example_e = c99_6_7_6_example_e; 57 1.5 rillig /* Wrong type before decl.c 1.256 from 2022-04-01. */ 58 1.7 rillig /* expect+1: ... 'pointer to function(pointer to function() returning pointer to int) returning void' ... */ 59 1.1 rillig double type_of_c99_6_7_6_example_f = c99_6_7_6_example_f; 60 1.7 rillig /* expect+1: ... 'pointer to function(pointer to function(void) returning int) returning void' ... */ 61 1.1 rillig double type_of_c99_6_7_6_example_g = c99_6_7_6_example_g; 62 1.7 rillig /* expect+1: ... 'pointer to function(pointer to const pointer to function(unsigned int, ...) returning int) returning void' ... */ 63 1.1 rillig double type_of_c99_6_7_6_example_h = c99_6_7_6_example_h; 64 1.2 rillig 65 1.2 rillig void int_array(int[]); 66 1.2 rillig void int_array_3(int[3]); 67 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 68 1.2 rillig void int_array_ast(int[*]); 69 1.2 rillig /* expect+1: error: null dimension [17] */ 70 1.2 rillig void int_array_7_array(int[7][]); 71 1.2 rillig void int_array_7_array_3(int[7][3]); 72 1.2 rillig /* expect+1: error: null dimension [17] */ 73 1.2 rillig void int_array_7_array_ast(int[7][*]); 74 1.2 rillig 75 1.2 rillig void int_array_array(int[][7]); 76 1.2 rillig void int_array_3_array(int[3][7]); 77 1.4 rillig /* supported since cgram.y 1.363 from 2021-09-14 */ 78 1.2 rillig void int_array_ast_array(int[*][7]); 79 1.5 rillig 80 1.6 rillig /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */ 81 1.5 rillig unsigned long size_unspecified_args = sizeof(int()); 82 1.5 rillig /* FIXME: Must be 'of function', not 'of void'. */ 83 1.5 rillig /* expect+1: error: cannot take size/alignment of void [146] */ 84 1.5 rillig unsigned long size_prototype_void = sizeof(int(void)); 85 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 86 1.5 rillig unsigned long size_prototype_unnamed = sizeof(int(double)); 87 1.6 rillig /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */ 88 1.5 rillig unsigned long size_prototype_named = sizeof(int(double dbl)); 89 1.6 rillig 90 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */ 91 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 92 1.6 rillig int size_unspecified_args_return_int[-1000 - (int)sizeof(int())]; 93 1.6 rillig 94 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */ 95 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 96 1.6 rillig int size_unspecified_args_return_char[-1000 - (int)sizeof(char())]; 97 1.6 rillig 98 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 99 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 100 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 101 1.6 rillig int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))]; 102 1.6 rillig 103 1.6 rillig /* FIXME: 'of void' must be 'of function'. */ 104 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 105 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 106 1.6 rillig int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))]; 107 1.6 rillig 108 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 109 1.6 rillig int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))]; 110 1.6 rillig 111 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 112 1.6 rillig int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))]; 113 1.6 rillig 114 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 115 1.6 rillig int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))]; 116 1.6 rillig 117 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 118 1.6 rillig int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))]; 119 1.6 rillig 120 1.6 rillig 121 1.6 rillig typedef struct { 122 1.6 rillig char a[1]; 123 1.6 rillig } a01; 124 1.6 rillig 125 1.6 rillig typedef struct { 126 1.6 rillig char a[4]; 127 1.6 rillig } a04; 128 1.6 rillig 129 1.6 rillig typedef struct { 130 1.6 rillig char a[8]; 131 1.6 rillig } a08; 132 1.6 rillig 133 1.6 rillig typedef struct { 134 1.6 rillig char a[32]; 135 1.6 rillig } a32; 136 1.6 rillig 137 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */ 138 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 139 1.6 rillig int unspecified_args_return_01[-1000 - (int)sizeof(a01())]; 140 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */ 141 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 142 1.6 rillig int unspecified_args_return_04[-1000 - (int)sizeof(a04())]; 143 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */ 144 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 145 1.6 rillig int unspecified_args_return_08[-1000 - (int)sizeof(a08())]; 146 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */ 147 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 148 1.6 rillig int unspecified_args_return_32[-1000 - (int)sizeof(a32())]; 149 1.6 rillig 150 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 151 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 152 1.6 rillig int prototype_void_return_01[-1000 - (int)sizeof(a01(void))]; 153 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 154 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 155 1.6 rillig int prototype_void_return_04[-1000 - (int)sizeof(a04(void))]; 156 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 157 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 158 1.6 rillig int prototype_void_return_08[-1000 - (int)sizeof(a08(void))]; 159 1.6 rillig /* expect+2: error: cannot take size/alignment of void [146] */ 160 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 161 1.6 rillig int prototype_void_return_32[-1000 - (int)sizeof(a32(void))]; 162 1.6 rillig 163 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 164 1.6 rillig int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))]; 165 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 166 1.6 rillig int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))]; 167 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 168 1.6 rillig int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))]; 169 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 170 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 171 1.6 rillig int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))]; 172 1.6 rillig 173 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 174 1.6 rillig int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))]; 175 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 176 1.6 rillig int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))]; 177 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 178 1.6 rillig int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))]; 179 1.6 rillig 180 1.6 rillig /* expect+1: error: negative array dimension (-1001) [20] */ 181 1.6 rillig int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))]; 182 1.6 rillig /* expect+1: error: negative array dimension (-1004) [20] */ 183 1.6 rillig int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))]; 184 1.6 rillig /* expect+1: error: negative array dimension (-1008) [20] */ 185 1.6 rillig int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))]; 186 1.6 rillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */ 187 1.6 rillig /* expect+1: error: negative array dimension (-1000) [20] */ 188 1.6 rillig int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))]; 189 1.6 rillig 190 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 191 1.6 rillig int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))]; 192 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 193 1.6 rillig int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))]; 194 1.6 rillig /* expect+1: error: negative array dimension (-1032) [20] */ 195 1.6 rillig int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))]; 196