parser: NAME fallback ERROR,WARNING
[m6w6/ext-psi] / src / types / decl_type.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 "token.h"
28 #include "data.h"
29
30 #define PSI_STD_TYPES
31 #include "php_psi_posix.h"
32
33 struct psi_decl_type *psi_decl_type_init(token_t type, const char *name)
34 {
35 struct psi_decl_type *t = calloc(1, sizeof(*t));
36 t->type = type;
37 t->name = strdup(name);
38 return t;
39 }
40
41 void psi_decl_type_free(struct psi_decl_type **type_ptr)
42 {
43 if (*type_ptr) {
44 struct psi_decl_type *type = *type_ptr;
45
46 *type_ptr = NULL;
47 if (type->token) {
48 free(type->token);
49 }
50 if (type->type == PSI_T_FUNCTION) {
51 psi_decl_free(&type->real.func);
52 }
53 free(type->name);
54 free(type);
55 }
56 }
57
58 struct psi_plist *psi_decl_type_get_args(struct psi_decl_type *dtyp,
59 struct psi_decl_type **real_typ_ptr)
60 {
61 struct psi_decl_type *var_typ;
62
63 var_typ = psi_decl_type_get_real(dtyp);
64 if (real_typ_ptr) {
65 *real_typ_ptr = var_typ;
66 }
67
68 switch (var_typ->type) {
69 case PSI_T_STRUCT:
70 return var_typ->real.strct->args;
71 case PSI_T_UNION:
72 return var_typ->real.unn->args;
73 default:
74 return NULL;
75 }
76 }
77
78 size_t psi_decl_type_get_size(struct psi_decl_type *dtyp,
79 struct psi_decl_type **real_typ_ptr)
80 {
81 struct psi_decl_type *var_typ;
82
83 var_typ = psi_decl_type_get_real(dtyp);
84 if (real_typ_ptr) {
85 *real_typ_ptr = var_typ;
86 }
87
88 switch (var_typ->type) {
89 case PSI_T_STRUCT:
90 return var_typ->real.strct->size;
91 case PSI_T_UNION:
92 return var_typ->real.unn->size;
93 default:
94 return psi_t_size(var_typ->type);
95 }
96 }
97
98 bool psi_decl_type_get_alias(struct psi_decl_type *type, struct psi_plist *defs)
99 {
100 size_t i = 0;
101 struct psi_std_type *stdtyp;
102 struct psi_decl_arg *def;
103
104 if (type->real.def) {
105 return true;
106 }
107 if (defs)
108 while (psi_plist_get(defs, i++, &def)) {
109 if (def->type->type != type->type
110 && !strcmp(def->var->name, type->name)) {
111 type->real.def = def;
112 return true;
113 }
114 }
115 for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) {
116 if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) {
117 type->type = stdtyp->type_tag;
118 return true;
119 }
120 }
121
122 return false;
123 }
124
125 bool psi_decl_type_get_struct(struct psi_decl_type *type, struct psi_plist *structs)
126 {
127 size_t i = 0;
128 struct psi_decl_struct *s;
129
130 if (type->real.strct) {
131 return true;
132 }
133 if (structs) {
134 while (psi_plist_get(structs, i++, &s)) {
135 if (!strcmp(s->name, type->name)) {
136 type->real.strct = s;
137 return true;
138 }
139 }
140 }
141 return false;
142 }
143
144 bool psi_decl_type_get_union(struct psi_decl_type *type, struct psi_plist *unions)
145 {
146 size_t i = 0;
147 struct psi_decl_union *u;
148
149 if (type->real.unn) {
150 return true;
151 }
152 if (unions) {
153 while (psi_plist_get(unions, i++, &u)) {
154 if (!strcmp(u->name, type->name)) {
155 type->real.unn = u;
156 return true;
157 }
158 }
159 }
160 return false;
161 }
162
163 bool psi_decl_type_get_enum(struct psi_decl_type *type, struct psi_plist *enums)
164 {
165 size_t i = 0;
166 struct psi_decl_enum *e;
167
168 if (type->real.enm) {
169 return true;
170 }
171 if (enums) {
172 while (psi_plist_get(enums, i++, &e)) {
173 if (!strcmp(e->name, type->name)) {
174 type->real.enm = e;
175 return true;
176 }
177 }
178 }
179 return false;
180 }
181
182 bool psi_decl_type_get_decl(struct psi_decl_type *type, struct psi_plist *decls)
183 {
184 size_t i = 0;
185 struct psi_decl *decl;
186
187 if (type->real.func) {
188 return true;
189 }
190 if (decls) {
191 while (psi_plist_get(decls, i++, &decl)) {
192 if (!strcmp(decl->func->var->name, type->name)) {
193 type->real.func = decl;
194 return true;
195 }
196 }
197 }
198 return false;
199 }
200
201 bool psi_decl_type_validate(struct psi_data *data, struct psi_decl_type *type,
202 struct psi_decl_arg *def)
203 {
204 if (psi_decl_type_is_weak(type)) {
205 if (!psi_decl_type_get_alias(type, data->types)) {
206 return false;
207 }
208 if (type->real.def) {
209 return psi_decl_type_validate(data, type->real.def->type,
210 type->real.def);
211 }
212 return true;
213 }
214
215 switch (type->type) {
216 case PSI_T_STRUCT:
217 if (!psi_decl_type_get_struct(type, data->structs) && !def) {
218 data->error(data, type->token, PSI_WARNING,
219 "Unknown struct '%s'", type->name);
220 return false;
221 }
222 break;
223 case PSI_T_UNION:
224 if (!psi_decl_type_get_union(type, data->unions) && !def) {
225 data->error(data, type->token, PSI_WARNING,
226 "Unknown union '%s'", type->name);
227 return false;
228 }
229 break;
230 case PSI_T_ENUM:
231 if (!psi_decl_type_get_enum(type, data->enums) && !def) {
232 data->error(data, type->token, PSI_WARNING,
233 "Unknown enum '%s'", type->name);
234 return false;
235 }
236 break;
237 case PSI_T_FUNCTION:
238 if (!psi_decl_type_get_decl(type, data->decls)) {
239 data->error(data, type->token, PSI_WARNING,
240 "Unknown decl '%s'", type->name);
241 return false;
242 }
243 if (!psi_decl_validate_nodl(data, type->real.func)) {
244 return false;
245 }
246 break;
247 }
248 return true;
249 }
250
251 void psi_decl_type_dump_args_with_layout(int fd, struct psi_plist *args,
252 unsigned level)
253 {
254 size_t i = 0;
255
256 dprintf(fd, " {\n");
257 if (args) {
258 struct psi_decl_arg *sarg;
259
260 ++level;
261 while (psi_plist_get(args, i++, &sarg)) {
262 dprintf(fd, "%s", psi_t_indent(level));
263 psi_decl_arg_dump(fd, sarg, level);
264 dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos,
265 sarg->layout->len);
266 }
267 --level;
268 }
269 dprintf(fd, "%s", psi_t_indent(level));
270 dprintf(fd, "}");
271 }
272
273 void psi_decl_type_dump(int fd, struct psi_decl_type *t, unsigned level)
274 {
275 switch (t->type) {
276 case PSI_T_POINTER:
277 dprintf(fd, "%s *", t->name);
278 return;
279
280 case PSI_T_ENUM:
281 dprintf(fd, "enum ");
282 if (psi_decl_type_is_anon(t->name, "enum")) {
283 size_t i = 0, c = psi_plist_count(t->real.enm->items);
284 struct psi_decl_enum_item *item;
285
286 dprintf(fd, "{\n");
287 ++level;
288 while (psi_plist_get(t->real.enm->items, i++, &item)) {
289 dprintf(fd, "%s", psi_t_indent(level));
290 psi_decl_enum_item_dump(fd, item);
291 if (i < c) {
292 dprintf(fd, "%s\n", i < c ? "," : "");
293 }
294 }
295 --level;
296 dprintf(fd, "%s} ", psi_t_indent(level));
297 return;
298 }
299 break;
300
301 case PSI_T_STRUCT:
302 dprintf(fd, "struct ");
303 if (psi_decl_type_is_anon(t->name, "struct")) {
304 psi_decl_type_dump_args_with_layout(fd, t->real.strct->args, level);
305 return;
306 }
307 break;
308
309 case PSI_T_UNION:
310 dprintf(fd, "union ");
311 if (psi_decl_type_is_anon(t->name, "union")) {
312 psi_decl_type_dump_args_with_layout(fd, t->real.unn->args, level);
313 return;
314 }
315 break;
316 }
317 dprintf(fd, "%s", t->name);
318 }
319
320 int psi_decl_type_is_weak(struct psi_decl_type *type)
321 {
322 switch (type->type) {
323 case PSI_T_CHAR:
324 case PSI_T_SHORT:
325 case PSI_T_INT:
326 case PSI_T_LONG:
327 case PSI_T_NAME:
328 return type->type;
329 default:
330 return 0;
331 }
332 }
333
334 struct psi_decl_type *psi_decl_type_get_real(struct psi_decl_type *type)
335 {
336 while (psi_decl_type_is_weak(type) && type->real.def) {
337 type = type->real.def->type;
338 }
339 return type;
340 }
341
342 size_t psi_decl_type_get_align(struct psi_decl_type *t)
343 {
344 struct psi_decl_type *real = psi_decl_type_get_real(t);
345 size_t align;
346
347 switch (real->type) {
348 case PSI_T_STRUCT:
349 align = psi_decl_struct_get_align(real->real.strct);
350 break;
351 case PSI_T_UNION:
352 align = psi_decl_union_get_align(real->real.unn);
353 break;
354 case PSI_T_ENUM:
355 default:
356 align = psi_t_alignment(real->type);
357 }
358
359 return align;
360 }
361
362 size_t psi_decl_type_get_args_align(struct psi_plist *args)
363 {
364 size_t i = 0, maxalign = 0;
365 struct psi_decl_arg *darg;
366
367 while (psi_plist_get(args, i++, &darg)) {
368 size_t align = psi_decl_arg_get_align(darg);
369
370 if (align > maxalign) {
371 maxalign = align;
372 }
373 }
374
375 return maxalign;
376 }
377