1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
31 #include "php_psi_posix.h"
33 struct psi_decl_type
*psi_decl_type_init(token_t type
, const char *name
)
35 struct psi_decl_type
*t
= calloc(1, sizeof(*t
));
37 t
->name
= strdup(name
);
41 void psi_decl_type_free(struct psi_decl_type
**type_ptr
)
44 struct psi_decl_type
*type
= *type_ptr
;
50 if (type
->type
== PSI_T_FUNCTION
) {
51 psi_decl_free(&type
->real
.func
);
58 struct psi_plist
*psi_decl_type_get_args(struct psi_decl_type
*dtyp
,
59 struct psi_decl_type
**real_typ_ptr
)
61 struct psi_decl_type
*var_typ
;
63 var_typ
= psi_decl_type_get_real(dtyp
);
65 *real_typ_ptr
= var_typ
;
68 switch (var_typ
->type
) {
70 return var_typ
->real
.strct
->args
;
72 return var_typ
->real
.unn
->args
;
78 size_t psi_decl_type_get_size(struct psi_decl_type
*dtyp
,
79 struct psi_decl_type
**real_typ_ptr
)
81 struct psi_decl_type
*var_typ
;
83 var_typ
= psi_decl_type_get_real(dtyp
);
85 *real_typ_ptr
= var_typ
;
88 switch (var_typ
->type
) {
90 return var_typ
->real
.strct
->size
;
92 return var_typ
->real
.unn
->size
;
94 return psi_t_size(var_typ
->type
);
98 bool psi_decl_type_get_alias(struct psi_decl_type
*type
, struct psi_plist
*defs
)
101 struct psi_std_type
*stdtyp
;
102 struct psi_decl_arg
*def
;
104 if (type
->real
.def
) {
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
;
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
;
125 bool psi_decl_type_get_struct(struct psi_decl_type
*type
, struct psi_plist
*structs
)
128 struct psi_decl_struct
*s
;
130 if (type
->real
.strct
) {
134 while (psi_plist_get(structs
, i
++, &s
)) {
135 if (!strcmp(s
->name
, type
->name
)) {
136 type
->real
.strct
= s
;
144 bool psi_decl_type_get_union(struct psi_decl_type
*type
, struct psi_plist
*unions
)
147 struct psi_decl_union
*u
;
149 if (type
->real
.unn
) {
153 while (psi_plist_get(unions
, i
++, &u
)) {
154 if (!strcmp(u
->name
, type
->name
)) {
163 bool psi_decl_type_get_enum(struct psi_decl_type
*type
, struct psi_plist
*enums
)
166 struct psi_decl_enum
*e
;
168 if (type
->real
.enm
) {
172 while (psi_plist_get(enums
, i
++, &e
)) {
173 if (!strcmp(e
->name
, type
->name
)) {
182 bool psi_decl_type_get_decl(struct psi_decl_type
*type
, struct psi_plist
*decls
)
185 struct psi_decl
*decl
;
187 if (type
->real
.func
) {
191 while (psi_plist_get(decls
, i
++, &decl
)) {
192 if (!strcmp(decl
->func
->var
->name
, type
->name
)) {
193 type
->real
.func
= decl
;
201 bool psi_decl_type_validate(struct psi_data
*data
, struct psi_decl_type
*type
,
202 struct psi_decl_arg
*def
)
204 if (psi_decl_type_is_weak(type
)) {
205 if (!psi_decl_type_get_alias(type
, data
->types
)) {
208 if (type
->real
.def
) {
209 return psi_decl_type_validate(data
, type
->real
.def
->type
,
215 switch (type
->type
) {
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
);
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
);
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
);
238 if (!psi_decl_type_get_decl(type
, data
->decls
)) {
239 data
->error(data
, type
->token
, PSI_WARNING
,
240 "Unknown decl '%s'", type
->name
);
243 if (!psi_decl_validate_nodl(data
, type
->real
.func
)) {
251 void psi_decl_type_dump_args_with_layout(int fd
, struct psi_plist
*args
,
258 struct psi_decl_arg
*sarg
;
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
,
269 dprintf(fd
, "%s", psi_t_indent(level
));
273 void psi_decl_type_dump(int fd
, struct psi_decl_type
*t
, unsigned level
)
277 dprintf(fd
, "%s *", t
->name
);
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
;
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
);
292 dprintf(fd
, "%s\n", i
< c
? "," : "");
296 dprintf(fd
, "%s} ", psi_t_indent(level
));
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
);
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
);
317 dprintf(fd
, "%s", t
->name
);
320 int psi_decl_type_is_weak(struct psi_decl_type
*type
)
322 switch (type
->type
) {
334 struct psi_decl_type
*psi_decl_type_get_real(struct psi_decl_type
*type
)
336 while (psi_decl_type_is_weak(type
) && type
->real
.def
) {
337 type
= type
->real
.def
->type
;
342 size_t psi_decl_type_get_align(struct psi_decl_type
*t
)
344 struct psi_decl_type
*real
= psi_decl_type_get_real(t
);
347 switch (real
->type
) {
349 align
= psi_decl_struct_get_align(real
->real
.strct
);
352 align
= psi_decl_union_get_align(real
->real
.unn
);
356 align
= psi_t_alignment(real
->type
);
362 size_t psi_decl_type_get_args_align(struct psi_plist
*args
)
364 size_t i
= 0, maxalign
= 0;
365 struct psi_decl_arg
*darg
;
367 while (psi_plist_get(args
, i
++, &darg
)) {
368 size_t align
= psi_decl_arg_get_align(darg
);
370 if (align
> maxalign
) {