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