flush
[m6w6/ext-psi] / m4 / psi_struct.m4
1 # psi_add_struct(struct members)
2 # Add a pre-defined struct to $PSI_STRUCTS.
3 psi_add_struct() {
4 cat >>$PSI_STRUCTS <<<" $1, {0}, "
5 }
6
7 # psi_add_union(union/struct members)
8 # Add a pre-defined union to $PSI_UNIONS.
9 psi_add_union() {
10 cat >>$PSI_UNIONS <<<" $1, {0}, "
11 }
12
13 dnl PSI_STRUCT_MEMBER(struct name, decl member)
14 dnl INTERNAL: build $psi_struct_members
15 AC_DEFUN(PSI_STRUCT_MEMBER, [
16 m4_define([member_name], PSI_VAR_NAME($2))
17 m4_define([member_type], PSI_VAR_TYPE($2))
18 PSI_CHECK_SIZEOF(AS_TR_SH($1)[_]member_name,
19 [#define ]AS_TR_SH($1)[_]member_name (($1 *)0)->member_name
20 )
21 if PSI_SH_TEST_SIZEOF($1 member_name); then
22 PSI_CHECK_OFFSETOF($1, member_name)
23 PSI_TYPE_INDIRECTION($2, [PSI_SH_SIZEOF([$1 member_name])], pl, as)
24
25 psi_member_sizeof=PSI_SH_SIZEOF($1 member_name)
26 psi_member_offsetof=PSI_SH_OFFSETOF($1 member_name)
27
28 if test $pl -gt 0 && test $as -eq 0; then
29 check_size=PSI_SH_SIZEOF(void *)
30 elif test $pl -eq 1 && test $as -gt 0; then
31 check_size=`expr PSI_SH_SIZEOF(member_type) \* $as`
32 else
33 check_size=PSI_SH_SIZEOF(member_type)
34 fi
35 if test $psi_member_sizeof != "$check_size"; then
36 psi_member_basic_type=PSI_SH_BASIC_TYPE(member_type)
37 psi_member_type_pair="`psi_type_pair $psi_member_basic_type $psi_member_sizeof`"
38 psi_struct_members="$psi_struct_members, {$psi_member_type_pair, \"[]member_name[]\", $psi_member_offsetof, $psi_member_sizeof, $pl, $as}"
39 AC_MSG_WARN(pre-defined size $check_size of $2 in $1 does not match computed size $psi_member_sizeof; adjusting to $psi_member_type_pair)
40 else
41 psi_struct_members="[$psi_struct_members, {]PSI_TYPE_PAIR(member_type)[, \"]member_name[\", $psi_member_offsetof, $psi_member_sizeof, $pl, $as}]"
42 fi
43 fi
44 ])
45
46 dnl PSI_STRUCT(struct name, struct members)
47 dnl Check a struct and its members and add a pre-defined struct and possibly a
48 dnl pre-defined type for this struct.
49 dnl Calls PSI_CHECK_SIZEOF and PSI_CHECK_ALIGNOF for the struct.
50 dnl Calls PSI_CHECK_SIZEOF, PSI_CHECK_OFFSETOF and PSI_TYPE_INDIRECTON for each member.
51 AC_DEFUN(PSI_STRUCT, [
52 PSI_CHECK_SIZEOF($1)
53 PSI_CHECK_ALIGNOF($1)
54 psi_struct_name=m4_bregexp([$1], [^\(struct \)?\(\w+\)], [\2])
55 psi_struct_members="{PSI_T_STRUCT, \"struct\", \"$psi_struct_name\", PSI_SH_ALIGNOF($1), PSI_SH_SIZEOF($1), 0, 0}"
56 ifelse([$2],,,[m4_map_args_sep([PSI_STRUCT_MEMBER($1, m4_normalize(], [))], [], $2)])
57 psi_add_struct "$psi_struct_members"
58 if test "$1" = "$psi_struct_name"; then
59 psi_add_type "{PSI_T_STRUCT, \"$1\", \"$1\"}"
60 fi
61 ])
62
63 dnl PSI_UNION(union name, union/struct members)
64 dnl Check a union and its members and add a pre-defined union and possibly a
65 dnl pre-defined type for this union.
66 dnl Calls PSI_CHECK_SIZEOF for the union and each member.
67 dnl Calls PSI_CHECK_OFFSETOF and PSI_TYPE_INDIRECTON for each member.
68 AC_DEFUN(PSI_UNION, [
69 PSI_CHECK_SIZEOF($1)
70 PSI_CHECK_ALIGNOF($1)
71 psi_struct_name=m4_bregexp([$1], [^\(union \)?\(\w+\)], [\2])
72 psi_struct_members="{PSI_T_UNION, \"union\", \"$psi_struct_name\", PSI_SH_ALIGNOF($1), PSI_SH_SIZEOF($1), 0, 0}"
73 ifelse([$2],,,[m4_map_args_sep([PSI_STRUCT_MEMBER($1, m4_normalize(], [))], [], $2)])
74 psi_add_union "$psi_struct_members"
75 if test "$1" = "$psi_struct_name"; then
76 psi_add_type "{PSI_T_UNION, \"$1\", \"$1\"}"
77 fi
78 ])