b5fe39f20978daaf7958e0a33f1c80760dd3af9b
[m6w6/ext-psi] / m4 / psi / 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 while expr "$psi_type_lower" : const >/dev/null; do
22 psi_type_lower=`printf "%s" "$psi_type_lower" | cut -d " " -f2-`
23 done
24 case $psi_type_lower in
25 int*|uint*)
26 local psi_type_upper=`printf "%s" "$psi_type_name" | tr a-z A-Z`
27 local psi_type_bits=`expr $2 \* 8`
28 echo "PSI_T_${psi_type_upper}${psi_type_bits}, \"${psi_type_lower}${psi_type_bits}_t\""
29 ;;
30 struct*)
31 echo "PSI_T_STRUCT, \"$2\""
32 ;;
33 union*)
34 echo "PSI_T_UNION, \"$2\""
35 ;;
36 void)
37 echo "PSI_T_VOID, \"void\""
38 ;;
39 *)
40 echo "PSI_T_NAME, \"$psi_type_name\""
41 ;;
42 esac
43 }
44
45 dnl PSI_TYPE(type name, basic type)
46 dnl Check for a specific type, optionally referring to a basic type.
47 dnl Calls AC_TYPE_<TYPE> (if defined) and PSI_CHECK_SIZEOF.
48 dnl If the basic type is just specified as "int" (in contrast to "sint" or
49 dnl "uint"), AX_CHECK_SIGN is used to discover signedness of the type.
50 dnl Defines a pre-defined type in $PSI_TYPES.
51 AC_DEFUN(PSI_TYPE, [
52 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
53 PSI_CHECK_SIZEOF($1)
54 psi_basic_type=AS_TR_SH($2)
55 case $psi_basic_type in
56 int)
57 AX_CHECK_SIGN($1, :, [psi_basic_type=uint], PSI_INCLUDES)
58 ;;
59 sint)
60 psi_basic_type=int
61 ;;
62 esac
63 if test "$2" && PSI_SH_TEST_SIZEOF($1); then
64 AS_TR_SH(psi_basic_type_$1)=$psi_basic_type
65 psi_add_type "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
66 fi
67 ])
68
69 AC_DEFUN(PSI_STDTYPE, [
70 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
71 PSI_CHECK_SIZEOF($1)
72 if PSI_SH_TEST_SIZEOF($1); then
73 m4_case([$1],
74 [bool],[psi_add_stdtype "{PSI_T_BOOL, \"bool\", NULL}"],
75 [float],[psi_add_stdtype "{PSI_T_FLOAT, \"float\", NULL}"],
76 [double],[psi_add_stdtype "{PSI_T_DOUBLE, \"double\", NULL}"],
77 [long double],[psi_add_stdtype "{PSI_T_LONG_DOUBLE, \"long double\", NULL}"],
78 [
79 AX_CHECK_SIGN($1, psi_basic_type=int, psi_basic_type=uint, PSI_INCLUDES)
80 AS_TR_SH(psi_basic_type_$1)=$psi_basic_type
81 psi_add_stdtype "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
82 ])
83 fi
84 ])
85
86 dnl PSI_SH_BASIC_TYPE(type)
87 dnl Expand to the basic type (int/uint) of a distinct type
88 AC_DEFUN(PSI_SH_BASIC_TYPE, [$AS_TR_SH([psi_basic_type_]$1)])
89
90 dnl PSI_OPAQUE_TYPE(type name)
91 dnl Checks a type for being a scalar, a struct or a pointer type.
92 dnl Calls AC_TYPE_<TYPE> (if defined) and PSI_CHECK_SIZEOF.
93 dnl Defines a pre-defined type in $PSI_TYPES and a pre-defined struct in
94 dnl $PSI_STRUCTS if the type is a struct.
95 AC_DEFUN(PSI_OPAQUE_TYPE, [
96 ifdef(AS_TR_CPP(AC_TYPE_$1), AS_TR_CPP(AC_TYPE_$1))
97 PSI_CHECK_SIZEOF($1)
98 if PSI_SH_TEST_SIZEOF($1); then
99 psi_type_class=
100 AC_CACHE_CHECK(kind of $1, AS_TR_SH([psi_cv_type_class_]$1), [
101 AC_TRY_COMPILE(PSI_INCLUDES, [char test@<:@($1)1@:>@;], [
102 psi_type_class=scalar
103 ], [
104 AC_TRY_COMPILE(PSI_INCLUDES, [$1 test = 0;], [
105 AC_TRY_COMPILE(PSI_INCLUDES, [$1 test = (($1)0)+1;], [
106 psi_type_class="pointer of known type"
107 ], [
108 psi_type_class="pointer of opaque type"
109 ])
110 ], [
111 psi_type_class=struct
112 ])
113 ])
114 AS_TR_SH([psi_cv_type_class_]$1)="$psi_type_class"
115 ])
116 case "$AS_TR_SH([psi_cv_type_class_]$1)" in
117 scalar)
118 AX_CHECK_SIGN($1, [psi_basic_type=int], [psi_basic_type=uint], PSI_INCLUDES)
119 psi_add_type "{`psi_type_pair $psi_basic_type PSI_SH_SIZEOF($1)`, \"$1\"}"
120 ;;
121 struct)
122 PSI_STRUCT($1)
123 ;;
124 pointer*)
125 psi_add_type "{PSI_T_POINTER, \"void\", \"$1\"}"
126 ;;
127 *)
128 AC_MSG_WARN(could not detect kind of $1)
129 ;;
130 esac
131 fi
132 ])
133
134 dnl PSI_FUNCTOR_TYPE(type functor_name, args)
135 dnl Forwards to PSI_DECL_TYPE.
136 AC_DEFUN(PSI_FUNCTOR_TYPE, [
137 dnl psi_add_type "{PSI_T_POINTER, \"void\", \"PSI_VAR_NAME($1)\"}"
138 AS_TR_SH([ac_cv_sizeof_]PSI_VAR_NAME($1))=PSI_SH_SIZEOF(void *)
139 PSI_DECL_TYPE([$1], [$2])
140 if test "$PHP_DEBUG" = "1"; then
141 AC_CHECK_TYPE(PSI_VAR_NAME($1), [], [
142 psi_add_macro ["#undef ]PSI_VAR_NAME($1)["]
143 psi_add_macro ["typedef ]PSI_VAR_TYPE($1)[ (*]PSI_VAR_NAME($1)[)]$2;"
144 ])
145 fi
146 ])
147
148 dnl PSI_VAR_TYPE(decl arg)
149 dnl Extracts the type of a decl arg, e.g. dnl unsigned char* buf[16] -> unsigned char*.
150 AC_DEFUN(PSI_VAR_TYPE, [m4_bregexp([$1], [^\(const \)?\(.*\) \([*]*\)[^ ]+$], [\2\3])])
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 AC_CHECK_HEADERS(stdint.h)
197 AC_HEADER_STDBOOL
198
199 AC_TYPE_INT8_T
200 PSI_CHECK_SIZEOF(int8_t)
201 AC_CHECK_ALIGNOF(int8_t)
202 AC_TYPE_UINT8_T
203 PSI_CHECK_SIZEOF(uint8_t)
204 AC_CHECK_ALIGNOF(uint8_t)
205 AC_TYPE_INT16_T
206 PSI_CHECK_SIZEOF(int16_t)
207 AC_CHECK_ALIGNOF(int16_t)
208 AC_TYPE_UINT16_T
209 PSI_CHECK_SIZEOF(uint16_t)
210 AC_CHECK_ALIGNOF(uint16_t)
211 AC_TYPE_INT32_T
212 PSI_CHECK_SIZEOF(int32_t)
213 AC_CHECK_ALIGNOF(int32_t)
214 AC_TYPE_UINT32_T
215 PSI_CHECK_SIZEOF(uint32_t)
216 AC_CHECK_ALIGNOF(uint32_t)
217 AC_TYPE_INT64_T
218 PSI_CHECK_SIZEOF(int64_t)
219 AC_CHECK_ALIGNOF(int64_t)
220 AC_TYPE_UINT64_T
221 PSI_CHECK_SIZEOF(uint64_t)
222 AC_CHECK_ALIGNOF(uint64_t)
223
224 PSI_CHECK_SIZEOF(void *)
225 AC_CHECK_ALIGNOF(void *)
226
227 PSI_STDTYPE(float)
228 AC_CHECK_ALIGNOF(float)
229 PSI_STDTYPE(double)
230 AC_CHECK_ALIGNOF(double)
231 PSI_STDTYPE(long double)
232 AC_CHECK_ALIGNOF(long double)
233
234 PSI_STDTYPE(bool)
235 AC_CHECK_ALIGNOF(bool, PSI_INCLUDES)
236
237 PSI_STDTYPE(char, int)
238 AC_CHECK_ALIGNOF(char)
239 PSI_STDTYPE(signed char, int)
240 PSI_STDTYPE(unsigned char, uint)
241 PSI_STDTYPE(short, int)
242 AC_CHECK_ALIGNOF(short)
243 PSI_STDTYPE(short int, int)
244 PSI_STDTYPE(signed short, int)
245 PSI_STDTYPE(signed short int, int)
246 PSI_STDTYPE(unsigned short, uint)
247 PSI_STDTYPE(unsigned short int, uint)
248 PSI_STDTYPE(int, int)
249 AC_CHECK_ALIGNOF(int)
250 PSI_STDTYPE(signed int, int)
251 PSI_STDTYPE(signed, int)
252 PSI_STDTYPE(unsigned int, uint)
253 PSI_STDTYPE(unsigned, uint)
254 PSI_STDTYPE(long, int)
255 AC_CHECK_ALIGNOF(long)
256 PSI_STDTYPE(long int, int)
257 PSI_STDTYPE(signed long int, int)
258 PSI_STDTYPE(unsigned long, uint)
259 PSI_STDTYPE(unsigned long int, uint)
260 PSI_STDTYPE(long long, int)
261 AC_CHECK_ALIGNOF(long long)
262 PSI_STDTYPE(signed long long, int)
263 PSI_STDTYPE(signed long long int, int)
264 PSI_STDTYPE(unsigned long long, uint)
265 PSI_STDTYPE(unsigned long long int, uint)
266 dnl this must come after the check for "unsigned long long int"; autoconf, wth?
267 PSI_STDTYPE(long long int, int)
268 ])