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