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