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