gdbinit
[m6w6/ext-psi] / m4 / psi / psi_type.m4
1 # psi_add_type(type triplet)
2 # Add a pre-defined type to $PSI_TYPES_H.
3 psi_add_type() {
4 PSI_TYPES="$PSI_TYPES
5 $1,"
6 }
7
8 psi_add_stdtype() {
9 PSI_STDTYPES="$PSI_STDTYPES
10 $1,"
11 }
12
13 # psi_type_pair(type, size)
14 # Output a PSI_T_<TYPE>, \"<TYPENAME>\" tuple.
15 # Uses stdint types when possible.
16 psi_type_pair() {
17 local psi_type_name=`printf "%s" "$1" | tr -cd A-Za-z0-9_`
18 local psi_type_lower=`printf "%s" "$1" | tr A-Z a-z`
19 while expr "$psi_type_lower" : const >/dev/null; do
20 psi_type_lower=`printf "%s" "$psi_type_lower" | cut -d " " -f2-`
21 done
22 case $psi_type_lower in
23 int*|uint*)
24 local psi_type_upper=`printf "%s" "$psi_type_name" | tr a-z A-Z`
25 local psi_type_bits=`expr $2 \* 8`
26 echo "PSI_T_${psi_type_upper}${psi_type_bits}, \"${psi_type_lower}${psi_type_bits}_t\""
27 ;;
28 struct*)
29 echo "PSI_T_STRUCT, \"$2\""
30 ;;
31 union*)
32 echo "PSI_T_UNION, \"$2\""
33 ;;
34 void)
35 echo "PSI_T_VOID, \"void\""
36 ;;
37 *)
38 echo "PSI_T_NAME, \"$psi_type_name\""
39 ;;
40 esac
41 }
42
43 dnl PSI_TYPE(type name, basic type)
44 dnl Check for a specific type, optionally referring to a basic type.
45 dnl Calls AC_TYPE_<TYPE> (if defined) and PSI_CHECK_SIZEOF.
46 dnl If the basic type is just specified as "int" (in contrast to "sint" or
47 dnl "uint"), AX_CHECK_SIGN is used to discover signedness of the type.
48 dnl Defines a pre-defined type in $PSI_TYPES_H.
49 AC_DEFUN(PSI_TYPE, [
50 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
51 PSI_CHECK_SIZEOF($1)
52 psi_basic_type=AS_TR_SH($2)
53 case $psi_basic_type in
54 int)
55 AX_CHECK_SIGN($1, :, [psi_basic_type=uint], PSI_INCLUDES)
56 ;;
57 sint)
58 psi_basic_type=int
59 ;;
60 esac
61 if test "$2" && PSI_SH_TEST_SIZEOF($1); then
62 AS_TR_SH(psi_basic_type_$1)=$psi_basic_type
63 psi_add_type "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
64 fi
65 ])
66
67 AC_DEFUN(PSI_STDTYPE, [
68 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
69 PSI_CHECK_SIZEOF($1)
70 if PSI_SH_TEST_SIZEOF($1); then
71 m4_case(ifelse(,[$2],[$1],[$2]),
72 [bool],[psi_add_stdtype "{PSI_T_BOOL, \"bool\", NULL}"],
73 [float],[psi_add_stdtype "{PSI_T_FLOAT, \"float\", NULL}"],
74 [double],[psi_add_stdtype "{PSI_T_DOUBLE, \"double\", NULL}"],
75 [long double],[psi_add_stdtype "{PSI_T_LONG_DOUBLE, \"long double\", NULL}"],
76 [
77 AX_CHECK_SIGN($1, psi_basic_type=int, psi_basic_type=uint, PSI_INCLUDES)
78 AS_TR_SH(psi_basic_type_$1)=$psi_basic_type
79 psi_add_stdtype "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
80 ])
81 fi
82 ])
83
84 dnl PSI_SH_BASIC_TYPE(type)
85 dnl Expand to the basic type (int/uint) of a distinct type
86 AC_DEFUN(PSI_SH_BASIC_TYPE, [$AS_TR_SH([psi_basic_type_]$1)])
87
88 dnl PSI_OPAQUE_TYPE(type name)
89 dnl Checks a type for being a scalar, a struct or a pointer type.
90 dnl Calls AC_TYPE_<TYPE> (if defined) and PSI_CHECK_SIZEOF.
91 dnl Defines a pre-defined type in $PSI_TYPES_H and a pre-defined struct in
92 dnl $PSI_STRUCTS if the type is a struct.
93 AC_DEFUN(PSI_OPAQUE_TYPE, [
94 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
95 PSI_CHECK_SIZEOF($1)
96 if PSI_SH_TEST_SIZEOF($1); then
97 psi_type_class=
98 AC_CACHE_CHECK(kind of $1, AS_TR_SH([psi_cv_type_class_]$1), [
99 AC_TRY_COMPILE(PSI_INCLUDES, [char test@<:@($1)1@:>@;], [
100 psi_type_class=scalar
101 ], [
102 AC_TRY_COMPILE(PSI_INCLUDES, [$1 test = 0;], [
103 AC_TRY_COMPILE(PSI_INCLUDES, [$1 test = (($1)0)+1;], [
104 psi_type_class="pointer of known type"
105 ], [
106 psi_type_class="pointer of opaque type"
107 ])
108 ], [
109 psi_type_class=struct
110 ])
111 ])
112 AS_TR_SH([psi_cv_type_class_]$1)="$psi_type_class"
113 ])
114 case "$AS_TR_SH([psi_cv_type_class_]$1)" in
115 scalar)
116 AX_CHECK_SIGN($1, [psi_basic_type=int], [psi_basic_type=uint], PSI_INCLUDES)
117 psi_add_type "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
118 ;;
119 struct)
120 PSI_STRUCT($1)
121 ;;
122 pointer*)
123 psi_add_type "{PSI_T_POINTER, \"void\", \"$1\"}"
124 ;;
125 *)
126 AC_MSG_WARN(could not detect kind of $1)
127 ;;
128 esac
129 fi
130 ])
131
132 dnl PSI_FUNCTOR_TYPE(type functor_name, args)
133 dnl Forwards to PSI_DECL_TYPE.
134 AC_DEFUN(PSI_FUNCTOR_TYPE, [
135 dnl psi_add_type "{PSI_T_POINTER, \"void\", \"PSI_VAR_NAME($1)\"}"
136 AS_TR_SH([ac_cv_sizeof_]PSI_VAR_NAME($1))=PSI_SH_SIZEOF(void *)
137 PSI_DECL_TYPE([$1], [$2])
138 AC_CHECK_TYPE(PSI_VAR_NAME($1), [], [
139 psi_add_macro ["#undef ]PSI_VAR_NAME($1)["]
140 psi_add_macro ["typedef ]PSI_VAR_TYPE($1)[ (*]PSI_VAR_NAME($1)[)]$2;"
141 ])
142 ])
143
144 dnl PSI_VAR_TYPE(decl arg)
145 dnl Extracts the type of a decl arg, e.g. dnl unsigned char* buf[16] -> unsigned char*.
146 AC_DEFUN(PSI_VAR_TYPE, [m4_bregexp([$1], [^\(const \)?\(.*\) \([*]*\)[^ ]+$], [\2\3])])
147
148 dnl PSI_VAR_TYPE_RETURN(decl arg)
149 dnl Extracts the type of a decl arg usable for return types, e.g. dnl unsigned char* buf[16] -> unsigned char**.
150 AC_DEFUN(PSI_VAR_TYPE_RETURN, [PSI_VAR_TYPE(m4_bpatsubst([$1], [\([^ ]+\) *@<:@[0-9]+@:>@], [* \1]))])
151
152 dnl PSI_VAR_NAME(decl arg)
153 dnl Extracts the var name of a decl arg, e.g. unsigned char* buf[16] -> buf.
154 AC_DEFUN(PSI_VAR_NAME, [m4_bregexp(m4_bregexp([$1], [\([^ ]+\)$], [\1]), [\w+], [\&])])
155
156 dnl PSI_TYPE_INDIRECTION(decl arg, size, pointer_level_var, array_size_var)
157 dnl Calculates and assigns pointer_level and array_size of a decl arg to sh vars.
158 AC_DEFUN(PSI_TYPE_INDIRECTION, [
159 m4_define([psi_pointer_level], m4_len(m4_bpatsubst([PSI_VAR_TYPE($1)], [[^*]])))
160 m4_define([psi_array_size], [m4_bregexp([PSI_VAR_TYPE($1)], [@<:@\([0-9]+\)@:>@], [\1])])
161
162 ifelse(psi_array_size.$2,0., [
163 AC_MSG_ERROR([cannot compute dynamic array size of a non-struct member])
164 ], [
165 ifelse(psi_pointer_level,0,[
166 m4_define([psi_type_size],[$]AS_TR_SH([ac_cv_sizeof_]m4_bregexp(PSI_VAR_TYPE([$1]), [^\( \|\w\)+], [\&])))
167 ],[
168 m4_define([psi_type_size],$ac_cv_sizeof_void_p)
169 ])
170 ])
171
172 m4_case(psi_array_size,,[
173 $3=psi_pointer_level
174 $4=0]
175 ,0,[
176 $3=m4_incr(psi_pointer_level)
177 $4="`expr $2 / psi_type_size`"
178 ], [
179 $3=m4_incr(psi_pointer_level)
180 $4=psi_array_size
181 ])
182 ])
183
184 dnl PSI_TYPE_PAIR(type)
185 dnl Expand to a PSI_T_<TYPE>, \\"<TYPENAME>\\" tuple.
186 dnl FIXME: There is also psi_type_pair()?
187 AC_DEFUN(PSI_TYPE_PAIR, [m4_case(m4_bregexp([$1], [^\w+], [\&]),
188 [void], [PSI_T_VOID, \"void\"],
189 [struct], [PSI_T_STRUCT, \"m4_bregexp([$1], [^struct \(\w+\)], [\1])\"],
190 [union], [PSI_T_UNION, \"m4_bregexp([$1], [^union \(\w+\)], [\1])\"],
191 [PSI_T_NAME, \"m4_bregexp([$1], [^\(\w+ \)*\w+], [\&])\"])])
192
193 dnl PSI_CHECK_STD_TYPES()
194 dnl Checks for standard ANSI-C, stdint and stdbool types.
195 AC_DEFUN(PSI_CHECK_STD_TYPES, [
196
197 AC_HEADER_STDBOOL
198
199 PSI_CHECK_SIZEOF(void *)
200 AC_CHECK_ALIGNOF(void *)
201
202 PSI_STDTYPE(float)
203 AC_CHECK_ALIGNOF(float)
204 PSI_STDTYPE(double)
205 AC_CHECK_ALIGNOF(double)
206 PSI_STDTYPE(long double)
207 AC_CHECK_ALIGNOF(long double)
208
209 PSI_STDTYPE(bool)
210 AC_CHECK_ALIGNOF(bool, PSI_INCLUDES)
211
212 PSI_STDTYPE(char, int)
213 AC_CHECK_ALIGNOF(char)
214 PSI_STDTYPE(signed char, int)
215 PSI_STDTYPE(unsigned char, uint)
216 PSI_STDTYPE(short, int)
217 AC_CHECK_ALIGNOF(short)
218 PSI_STDTYPE(short int, int)
219 PSI_STDTYPE(signed short, int)
220 PSI_STDTYPE(signed short int, int)
221 PSI_STDTYPE(short signed, int)
222 PSI_STDTYPE(short signed int, int)
223 PSI_STDTYPE(unsigned short, uint)
224 PSI_STDTYPE(unsigned short int, uint)
225 PSI_STDTYPE(short unsigned, uint)
226 PSI_STDTYPE(short unsigned int, uint)
227 PSI_STDTYPE(int, int)
228 AC_CHECK_ALIGNOF(int)
229 PSI_STDTYPE(signed int, int)
230 PSI_STDTYPE(signed, int)
231 PSI_STDTYPE(unsigned int, uint)
232 PSI_STDTYPE(unsigned, uint)
233 PSI_STDTYPE(long, int)
234 AC_CHECK_ALIGNOF(long)
235 PSI_STDTYPE(long int, int)
236 PSI_STDTYPE(signed long int, int)
237 PSI_STDTYPE(long signed int, int)
238 PSI_STDTYPE(unsigned long, uint)
239 PSI_STDTYPE(unsigned long int, uint)
240 PSI_STDTYPE(long unsigned, uint)
241 PSI_STDTYPE(long unsigned int, uint)
242 PSI_STDTYPE(long long, int)
243 AC_CHECK_ALIGNOF(long long)
244 PSI_STDTYPE(signed long long, int)
245 PSI_STDTYPE(signed long long int, int)
246 PSI_STDTYPE(long signed long, int)
247 PSI_STDTYPE(long signed long int, int)
248 PSI_STDTYPE(long long signed, int)
249 PSI_STDTYPE(long long signed int, int)
250 PSI_STDTYPE(unsigned long long, uint)
251 PSI_STDTYPE(unsigned long long int, uint)
252 PSI_STDTYPE(long unsigned long, uint)
253 PSI_STDTYPE(long unsigned long int, uint)
254 PSI_STDTYPE(long long unsigned, uint)
255 PSI_STDTYPE(long long unsigned int, uint)
256 dnl this must come after the check for "unsigned long long int"; autoconf, wth?
257 PSI_STDTYPE(long long int, int)
258
259 AC_CHECK_TYPE(__int128, [
260 AC_DEFINE([HAVE_INT128], [1], [ ])
261 AC_CHECK_ALIGNOF(__int128)
262 PSI_STDTYPE(__int128, int)
263 PSI_STDTYPE(signed __int128, int)
264 PSI_STDTYPE(unsigned __int128, uint)
265 ])
266
267 PSI_STDTYPE(_Float128)
268 ])