826ae7e8dc6ba356ac4e82935c56e75c0658b0a7
[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 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_FUNCTOR_TYPE(type functor_name, args)
131 dnl Forwards to PSI_DECL_TYPE.
132 AC_DEFUN(PSI_FUNCTOR_TYPE, [
133 dnl psi_add_type "{PSI_T_POINTER, \"void\", \"PSI_VAR_NAME($1)\"}"
134 AS_TR_SH([ac_cv_sizeof_]PSI_VAR_NAME($1))=PSI_SH_SIZEOF(void *)
135 PSI_DECL_TYPE([$1], [$2])
136 if test "$PHP_DEBUG" = "1"; then
137 AC_CHECK_TYPE(PSI_VAR_NAME($1), [], [
138 psi_add_macro ["#undef ]PSI_VAR_NAME($1)["]
139 psi_add_macro ["typedef ]PSI_VAR_TYPE($1)[ (*]PSI_VAR_NAME($1)[)]$2;"
140 ])
141 fi
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_NAME(decl arg)
149 dnl Extracts the var name of a decl arg, e.g. unsigned char* buf[16] -> buf.
150 AC_DEFUN(PSI_VAR_NAME, [m4_bregexp(m4_bregexp([$1], [\([^ ]+\)$], [\1]), [\w+], [\&])])
151
152 dnl PSI_TYPE_INDIRECTION(decl arg, size, pointer_level_var, array_size_var)
153 dnl Calculates and assigns pointer_level and array_size of a decl arg to sh vars.
154 AC_DEFUN(PSI_TYPE_INDIRECTION, [
155 m4_define([psi_pointer_level], m4_len(m4_bpatsubst([PSI_VAR_TYPE($1)], [[^*]])))
156 m4_define([psi_array_size], [m4_bregexp([PSI_VAR_TYPE($1)], [@<:@\([0-9]+\)@:>@], [\1])])
157
158 ifelse(psi_array_size.$2,0., [
159 AC_MSG_ERROR([cannot compute dynamic array size of a non-struct member])
160 ], [
161 ifelse(psi_pointer_level,0,[
162 m4_define([psi_type_size],[$]AS_TR_SH([ac_cv_sizeof_]m4_bregexp(PSI_VAR_TYPE([$1]), [^\( \|\w\)+], [\&])))
163 ],[
164 m4_define([psi_type_size],$ac_cv_sizeof_void_p)
165 ])
166 ])
167
168 m4_case(psi_array_size,,[
169 $3=psi_pointer_level
170 $4=0]
171 ,0,[
172 $3=m4_incr(psi_pointer_level)
173 $4="`expr $2 / psi_type_size`"
174 ], [
175 $3=m4_incr(psi_pointer_level)
176 $4=psi_array_size
177 ])
178 ])
179
180 dnl PSI_TYPE_PAIR(type)
181 dnl Expand to a PSI_T_<TYPE>, \\"<TYPENAME>\\" tuple.
182 dnl FIXME: There is also psi_type_pair()?
183 AC_DEFUN(PSI_TYPE_PAIR, [m4_case(m4_bregexp([$1], [^\w+], [\&]),
184 [void], [PSI_T_VOID, \"void\"],
185 [struct], [PSI_T_STRUCT, \"m4_bregexp([$1], [^struct \(\w+\)], [\1])\"],
186 [union], [PSI_T_UNION, \"m4_bregexp([$1], [^union \(\w+\)], [\1])\"],
187 [PSI_T_NAME, \"m4_bregexp([$1], [^\(\w+ \)*\w+], [\&])\"])])
188
189 dnl PSI_CHECK_STD_TYPES()
190 dnl Checks for standard ANSI-C and stdint types.
191 AC_DEFUN(PSI_CHECK_STD_TYPES, [
192 AC_CHECK_HEADERS(stdint.h)
193
194 AC_TYPE_INT8_T
195 PSI_CHECK_SIZEOF(int8_t)
196 AC_CHECK_ALIGNOF(int8_t)
197 AC_TYPE_UINT8_T
198 PSI_CHECK_SIZEOF(uint8_t)
199 AC_CHECK_ALIGNOF(uint8_t)
200 AC_TYPE_INT16_T
201 PSI_CHECK_SIZEOF(int16_t)
202 AC_CHECK_ALIGNOF(int16_t)
203 AC_TYPE_UINT16_T
204 PSI_CHECK_SIZEOF(uint16_t)
205 AC_CHECK_ALIGNOF(uint16_t)
206 AC_TYPE_INT32_T
207 PSI_CHECK_SIZEOF(int32_t)
208 AC_CHECK_ALIGNOF(int32_t)
209 AC_TYPE_UINT32_T
210 PSI_CHECK_SIZEOF(uint32_t)
211 AC_CHECK_ALIGNOF(uint32_t)
212 AC_TYPE_INT64_T
213 PSI_CHECK_SIZEOF(int64_t)
214 AC_CHECK_ALIGNOF(int64_t)
215 AC_TYPE_UINT64_T
216 PSI_CHECK_SIZEOF(uint64_t)
217 AC_CHECK_ALIGNOF(uint64_t)
218
219 PSI_CHECK_SIZEOF(void *)
220 AC_CHECK_ALIGNOF(void *)
221
222 PSI_STDTYPE(float)
223 AC_CHECK_ALIGNOF(float)
224 PSI_STDTYPE(double)
225 AC_CHECK_ALIGNOF(double)
226 PSI_STDTYPE(long double)
227 AC_CHECK_ALIGNOF(long double)
228
229 PSI_STDTYPE(char, int)
230 AC_CHECK_ALIGNOF(char)
231 PSI_STDTYPE(signed char, int)
232 PSI_STDTYPE(unsigned char, uint)
233 PSI_STDTYPE(short, int)
234 AC_CHECK_ALIGNOF(short)
235 PSI_STDTYPE(short int, int)
236 PSI_STDTYPE(signed short, int)
237 PSI_STDTYPE(signed short int, int)
238 PSI_STDTYPE(unsigned short, uint)
239 PSI_STDTYPE(unsigned short int, uint)
240 PSI_STDTYPE(int, int)
241 AC_CHECK_ALIGNOF(int)
242 PSI_STDTYPE(signed int, int)
243 PSI_STDTYPE(signed, int)
244 PSI_STDTYPE(unsigned int, uint)
245 PSI_STDTYPE(unsigned, uint)
246 PSI_STDTYPE(long, int)
247 AC_CHECK_ALIGNOF(long)
248 PSI_STDTYPE(long int, int)
249 PSI_STDTYPE(signed long int, int)
250 PSI_STDTYPE(unsigned long, uint)
251 PSI_STDTYPE(unsigned long int, uint)
252 PSI_STDTYPE(long long, int)
253 AC_CHECK_ALIGNOF(long long)
254 PSI_STDTYPE(signed long long, int)
255 PSI_STDTYPE(signed long long int, int)
256 PSI_STDTYPE(unsigned long long, uint)
257 PSI_STDTYPE(unsigned long long int, uint)
258 dnl this must come after the check fo "unsigned long long int"; autoconf, wth?
259 PSI_STDTYPE(long long int, int)
260 ])