raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / decl_union.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_decl_union* psi_decl_union_init(const char *name,
32 struct psi_plist *args)
33 {
34 struct psi_decl_union *u = calloc(1, sizeof(*u));
35 u->name = strdup(name);
36 u->args = args;
37 return u;
38 }
39
40 void psi_decl_union_free(struct psi_decl_union **u_ptr)
41 {
42 if (*u_ptr) {
43 struct psi_decl_union *u = *u_ptr;
44
45 *u_ptr = NULL;
46 if (u->token) {
47 free(u->token);
48 }
49 if (u->args) {
50 psi_plist_free(u->args);
51 }
52 free(u->name);
53 free(u);
54 }
55 }
56
57 void psi_decl_union_dump(int fd, struct psi_decl_union *unn)
58 {
59 dprintf(fd, "union %s::(%zu, %zu)", unn->name, unn->align, unn->size);
60 if (psi_plist_count(unn->args)) {
61 psi_decl_type_dump_args_with_layout(fd, unn->args, 0);
62 } else {
63 dprintf(fd, ";");
64 }
65 }
66
67 struct psi_decl_arg *psi_decl_union_get_arg(struct psi_decl_union *u,
68 struct psi_decl_var *var)
69 {
70 if (u->args) {
71 return psi_decl_arg_get_by_var(var, u->args, NULL);
72 }
73
74 return NULL;
75 }
76
77 bool psi_decl_union_validate(struct psi_data *data, struct psi_decl_union *u)
78 {
79 size_t i, pos, len, size = 0, align;
80 struct psi_decl_arg *darg;
81
82 if (!u->size && !psi_plist_count(u->args)) {
83 data->error(data, u->token, PSI_WARNING,
84 "Cannot compute size of empty union %s", u->name);
85 return false;
86 }
87
88 for (i = 0; psi_plist_get(u->args, i, &darg); ++i) {
89 darg->var->arg = darg;
90
91 if (!psi_decl_arg_validate(data, darg)) {
92 return false;
93 }
94
95 if (darg->layout) {
96 pos = darg->layout->pos;
97
98 align = psi_decl_arg_align(darg, &pos, &len);
99
100 if (darg->layout->pos != 0) {
101 data->error(data, darg->token, PSI_WARNING,
102 "Offset of %s.%s should be 0", u->name,
103 darg->var->name);
104 darg->layout->pos = 0;
105 }
106 if (darg->layout->len != len) {
107 data->error(data, darg->token, PSI_WARNING,
108 "Computed size %zu of %s.%s does not match"
109 " pre-defined size %zu of type '%s'",
110 len, u->name, darg->var->name, darg->layout->len,
111 darg->type->name);
112 }
113 } else {
114 pos = 0;
115
116 align = psi_decl_arg_align(darg, &pos, &len);
117 darg->layout = psi_layout_init(pos, len);
118 }
119 if (len > size) {
120 size = len;
121 }
122 if (align > u->align) {
123 u->align = align;
124 }
125 }
126
127 psi_plist_sort(u->args, psi_layout_sort_cmp, NULL);
128
129 if (u->size < size) {
130 u->size = psi_align(size, u->align);
131 }
132
133 return true;
134 }
135
136 size_t psi_decl_union_get_align(struct psi_decl_union *u)
137 {
138 if (!u->align) {
139 u->align = psi_decl_type_get_args_align(u->args);
140 }
141 return u->align;
142 }