travis: update
[m6w6/ext-psi] / src / types / decl_struct.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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31 #include "data.h"
32
33 #include <assert.h>
34
35 struct psi_decl_struct* psi_decl_struct_init(zend_string *name,
36 struct psi_plist *args)
37 {
38 struct psi_decl_struct *s = pecalloc(1, sizeof(*s), 1);
39 s->name = zend_string_copy(name);
40 s->args = args;
41 return s;
42 }
43
44 void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
45 {
46 if (*s_ptr) {
47 struct psi_decl_struct *s = *s_ptr;
48
49 *s_ptr = NULL;
50 psi_token_free(&s->token);
51 if (s->args) {
52 psi_plist_free(s->args);
53 }
54 zend_string_release(s->name);
55 free(s);
56 }
57 }
58
59 void psi_decl_struct_dump(struct psi_dump *dump, struct psi_decl_struct *strct)
60 {
61 PSI_DUMP(dump, "struct %s::(%zu, %zu)", strct->name->val, strct->align,
62 strct->size);
63 if (psi_plist_count(strct->args)) {
64 psi_decl_type_dump_args_with_layout(dump, strct->args, 0);
65 } else {
66 PSI_DUMP(dump, ";");
67 }
68 }
69
70 struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
71 struct psi_decl_var *var)
72 {
73 if (s->args) {
74 return psi_decl_arg_get_by_var(var, s->args, NULL);
75 }
76
77 return NULL;
78 }
79
80 bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s,
81 struct psi_validate_scope *scope)
82 {
83 size_t i, pos = 0, len = 0;
84 struct psi_decl_arg *darg, *prev_arg;
85
86 if (!s) {
87 return false;
88 }
89 if (psi_validate_scope_has_struct(scope, s->name)) {
90 return true;
91 }
92
93 if (!s->size && !psi_plist_count(s->args)) {
94 /* TODO: return true and check those structs are only used by address */
95 /* suppress needless warning
96 data->error(data, s->token, PSI_WARNING, "Empty struct %s",
97 s->name);
98 */
99 return false;
100 }
101
102 psi_validate_scope_add_struct(scope, s->name, s);
103
104 for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
105 size_t align;
106
107 darg->var->arg = darg;
108
109 if (!psi_decl_arg_validate(data, darg, scope)) {
110 psi_validate_scope_del_struct(scope, s->name);
111 return false;
112 }
113
114 if (darg->layout && darg->layout->len) {
115 pos = darg->layout->pos;
116 align = psi_decl_arg_align(darg, &pos, &len);
117
118 if (!align) {
119 data->error(data, darg->token, PSI_WARNING,
120 "Computed zero alignment of %s.%s of type '%s'",
121 len, s->name->val, darg->var->name->val,
122 darg->type->name->val);
123 psi_validate_scope_del_struct(scope, s->name);
124 return false;
125 }
126
127 if (darg->layout->len != len) {
128 data->error(data, darg->token, PSI_WARNING,
129 "Computed size %zu of %s.%s does not match"
130 " pre-defined size %zu of type '%s'",
131 len, s->name->val, darg->var->name->val,
132 darg->layout->len, darg->type->name->val);
133 }
134 if (darg->layout->pos != pos) {
135 data->error(data, darg->token, PSI_WARNING,
136 "Computed offset %zu of %s.%s does not match"
137 " pre-defined offset %zu",
138 pos, s->name->val, darg->var->name->val,
139 darg->layout->pos);
140 }
141 } else {
142 if (i) {
143 if (prev_arg->layout && prev_arg->layout->bfw && darg->layout && darg->layout->bfw) {
144 struct psi_decl_type *real = NULL;
145 size_t max_bfw = 8 * psi_decl_type_get_size(prev_arg->type, &real);
146
147 switch (real->type) {
148 case PSI_T_INT8:
149 case PSI_T_UINT8:
150 case PSI_T_INT16:
151 case PSI_T_UINT16:
152 case PSI_T_INT32:
153 case PSI_T_UINT32:
154 case PSI_T_INT64:
155 case PSI_T_UINT64:
156 break;
157 default:
158 data->error(data, darg->token, PSI_WARNING,
159 "Unsupported type for bit field: %s",
160 real->name->val);
161 psi_validate_scope_del_struct(scope, s->name);
162 return false;
163 }
164 darg->layout->bfw->pos = prev_arg->layout->bfw->pos + prev_arg->layout->bfw->len;
165 if (max_bfw >= darg->layout->bfw->pos + darg->layout->bfw->len) {
166 pos = prev_arg->layout->pos;
167 } else {
168 darg->layout->bfw->pos = 0;
169 pos = prev_arg->layout->pos + prev_arg->layout->len;
170 }
171 } else {
172 pos = prev_arg->layout->pos + prev_arg->layout->len;
173 }
174 } else {
175 pos = 0;
176 }
177
178 align = psi_decl_arg_align(darg, &pos, &len);
179
180 if (darg->layout) {
181 if (darg->layout->pos != pos && !darg->layout->bfw) {
182 data->error(data, darg->token, PSI_WARNING,
183 "Computed offset %zu of %s.%s does not match"
184 " pre-defined offset %zu",
185 pos, s->name->val, darg->var->name->val,
186 darg->layout->pos);
187 }
188 darg->layout->pos = pos;
189 darg->layout->len = len;
190 } else {
191 darg->layout = psi_layout_init(pos, len, NULL);
192 }
193 }
194
195 if (align > s->align) {
196 s->align = align;
197 }
198 prev_arg = darg;
199 }
200
201 if (psi_plist_count(s->args)) {
202 size_t size;
203
204 psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
205 psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
206
207 size = darg->layout->pos + darg->layout->len;
208 if (s->size < size) {
209 s->size = psi_align(size, s->align);
210 }
211 }
212
213 assert(s->size);
214
215 return true;
216 }
217
218 size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
219 {
220 if (!s) {
221 return 0;
222 }
223 if (!s->align) {
224 s->align = psi_decl_type_get_args_align(s->args);
225 }
226 return s->align;
227 }