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