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"
28 #include "php_psi_stdtypes.h"
31 struct psi_decl_type
*psi_decl_type_init(token_t type
, const char *name
)
33 struct psi_decl_type
*t
= calloc(1, sizeof(*t
));
35 t
->name
= strdup(name
);
39 void psi_decl_type_free(struct psi_decl_type
**type_ptr
)
42 struct psi_decl_type
*type
= *type_ptr
;
48 if (type
->type
== PSI_T_FUNCTION
) {
49 psi_decl_free(&type
->real
.func
);
56 struct psi_plist
*psi_decl_type_get_args(struct psi_decl_type
*dtyp
,
57 struct psi_decl_type
**real_typ_ptr
)
59 struct psi_decl_type
*var_typ
;
61 var_typ
= psi_decl_type_get_real(dtyp
);
63 *real_typ_ptr
= var_typ
;
66 switch (var_typ
->type
) {
68 return var_typ
->real
.strct
->args
;
70 return var_typ
->real
.unn
->args
;
76 size_t psi_decl_type_get_size(struct psi_decl_type
*dtyp
,
77 struct psi_decl_type
**real_typ_ptr
)
79 struct psi_decl_type
*var_typ
;
81 var_typ
= psi_decl_type_get_real(dtyp
);
83 *real_typ_ptr
= var_typ
;
86 switch (var_typ
->type
) {
88 return var_typ
->real
.strct
->size
;
90 return var_typ
->real
.unn
->size
;
92 return psi_t_size(var_typ
->type
);
96 bool psi_decl_type_get_alias(struct psi_decl_type
*type
, struct psi_plist
*defs
)
99 struct psi_std_type
*stdtyp
;
100 struct psi_decl_arg
*def
;
102 if (type
->real
.def
) {
106 while (psi_plist_get(defs
, i
++, &def
)) {
107 if (def
->type
->type
!= type
->type
108 && !strcmp(def
->var
->name
, type
->name
)) {
109 type
->real
.def
= def
;
113 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
114 if (!strcmp(type
->name
, stdtyp
->alias
?: stdtyp
->type_name
)) {
115 type
->type
= stdtyp
->type_tag
;
123 bool psi_decl_type_get_struct(struct psi_decl_type
*type
, struct psi_plist
*structs
)
126 struct psi_decl_struct
*s
;
128 if (type
->real
.strct
) {
132 while (psi_plist_get(structs
, i
++, &s
)) {
133 if (!strcmp(s
->name
, type
->name
)) {
134 type
->real
.strct
= s
;
142 bool psi_decl_type_get_union(struct psi_decl_type
*type
, struct psi_plist
*unions
)
145 struct psi_decl_union
*u
;
147 if (type
->real
.unn
) {
151 while (psi_plist_get(unions
, i
++, &u
)) {
152 if (!strcmp(u
->name
, type
->name
)) {
161 bool psi_decl_type_get_enum(struct psi_decl_type
*type
, struct psi_plist
*enums
)
164 struct psi_decl_enum
*e
;
166 if (type
->real
.enm
) {
170 while (psi_plist_get(enums
, i
++, &e
)) {
171 if (!strcmp(e
->name
, type
->name
)) {
180 bool psi_decl_type_get_decl(struct psi_decl_type
*type
, struct psi_plist
*decls
)
183 struct psi_decl
*decl
;
185 if (type
->real
.func
) {
189 while (psi_plist_get(decls
, i
++, &decl
)) {
190 if (!strcmp(decl
->func
->var
->name
, type
->name
)) {
191 type
->real
.func
= decl
;
199 bool psi_decl_type_validate(struct psi_data
*data
, struct psi_decl_type
*type
,
200 struct psi_decl_arg
*def
)
202 if (psi_decl_type_is_weak(type
)) {
203 if (!psi_decl_type_get_alias(type
, data
->types
)) {
206 if (type
->real
.def
) {
207 return psi_decl_type_validate(data
, type
->real
.def
->type
,
213 switch (type
->type
) {
215 if (!psi_decl_type_get_struct(type
, data
->structs
) && !def
) {
216 data
->error(data
, type
->token
, PSI_WARNING
,
217 "Unknown struct '%s'", type
->name
);
222 if (!psi_decl_type_get_union(type
, data
->unions
) && !def
) {
223 data
->error(data
, type
->token
, PSI_WARNING
,
224 "Unknown union '%s'", type
->name
);
229 if (!psi_decl_type_get_enum(type
, data
->enums
) && !def
) {
230 data
->error(data
, type
->token
, PSI_WARNING
,
231 "Unknown enum '%s'", type
->name
);
236 if (!psi_decl_type_get_decl(type
, data
->decls
)) {
237 data
->error(data
, type
->token
, PSI_WARNING
,
238 "Unknown decl '%s'", type
->name
);
241 if (!psi_decl_validate_nodl(data
, type
->real
.func
)) {
249 void psi_decl_type_dump_args_with_layout(int fd
, struct psi_plist
*args
,
256 struct psi_decl_arg
*sarg
;
259 while (psi_plist_get(args
, i
++, &sarg
)) {
260 dprintf(fd
, "%s", psi_t_indent(level
));
261 psi_decl_arg_dump(fd
, sarg
, level
);
262 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
,
267 dprintf(fd
, "%s", psi_t_indent(level
));
271 void psi_decl_type_dump(int fd
, struct psi_decl_type
*t
, unsigned level
)
275 dprintf(fd
, "%s *", t
->name
);
279 dprintf(fd
, "enum ");
280 if (psi_decl_type_is_anon(t
->name
, "enum")) {
281 size_t i
= 0, c
= psi_plist_count(t
->real
.enm
->items
);
282 struct psi_decl_enum_item
*item
;
286 while (psi_plist_get(t
->real
.enm
->items
, i
++, &item
)) {
287 dprintf(fd
, "%s", psi_t_indent(level
));
288 psi_decl_enum_item_dump(fd
, item
);
290 dprintf(fd
, "%s\n", i
< c
? "," : "");
294 dprintf(fd
, "%s} ", psi_t_indent(level
));
300 dprintf(fd
, "struct ");
301 if (psi_decl_type_is_anon(t
->name
, "struct")) {
302 psi_decl_type_dump_args_with_layout(fd
, t
->real
.strct
->args
, level
);
308 dprintf(fd
, "union ");
309 if (psi_decl_type_is_anon(t
->name
, "union")) {
310 psi_decl_type_dump_args_with_layout(fd
, t
->real
.unn
->args
, level
);
315 dprintf(fd
, "%s", t
->name
);
318 int psi_decl_type_is_weak(struct psi_decl_type
*type
)
320 switch (type
->type
) {
332 struct psi_decl_type
*psi_decl_type_get_real(struct psi_decl_type
*type
)
334 while (psi_decl_type_is_weak(type
) && type
->real
.def
) {
335 type
= type
->real
.def
->type
;
340 size_t psi_decl_type_get_align(struct psi_decl_type
*t
)
342 struct psi_decl_type
*real
= psi_decl_type_get_real(t
);
345 switch (real
->type
) {
347 align
= psi_decl_struct_get_align(real
->real
.strct
);
350 align
= psi_decl_union_get_align(real
->real
.unn
);
354 align
= psi_t_alignment(real
->type
);
360 size_t psi_decl_type_get_args_align(struct psi_plist
*args
)
362 size_t i
= 0, maxalign
= 0;
363 struct psi_decl_arg
*darg
;
365 while (psi_plist_get(args
, i
++, &darg
)) {
366 size_t align
= psi_decl_arg_get_align(darg
);
368 if (align
> maxalign
) {