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