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