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 *******************************************************************************/
29 # include "php_config.h"
36 #include "php_psi_stdtypes.h"
40 decl_type
*init_decl_type(token_t type
, const char *name
) {
41 decl_type
*t
= calloc(1, sizeof(*t
));
43 t
->name
= strdup(name
);
47 void free_decl_type(decl_type
*type
) {
51 if (type
->type
== PSI_T_FUNCTION
) {
52 free_decl(type
->real
.func
);
58 decl_args
*extract_decl_type_args(decl_type
*dtyp
, decl_type
**real_typ_ptr
) {
60 var_typ
= real_decl_type(dtyp
);
62 *real_typ_ptr
= var_typ
;
64 switch (var_typ
->type
) {
66 return var_typ
->real
.strct
->args
;
68 return var_typ
->real
.unn
->args
;
75 int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
77 struct psi_std_type
*stdtyp
;
82 if (defs
) for (i
= 0; i
< defs
->count
; ++i
) {
83 decl_arg
*def
= defs
->list
[i
];
85 if (def
->type
->type
!= type
->type
&& !strcmp(def
->var
->name
, type
->name
)) {
90 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
91 if (!strcmp(type
->name
, stdtyp
->alias
?: stdtyp
->type_name
)) {
92 type
->type
= stdtyp
->type_tag
;
100 int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
103 if (type
->real
.strct
) {
106 if (structs
) for (i
= 0; i
< structs
->count
; ++i
) {
107 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
108 type
->real
.strct
= structs
->list
[i
];
115 int locate_decl_type_union(decl_unions
*unions
, decl_type
*type
) {
118 if (type
->real
.unn
) {
121 if (unions
) for (i
= 0; i
< unions
->count
; ++i
) {
122 if (!strcmp(unions
->list
[i
]->name
, type
->name
)) {
123 type
->real
.unn
= unions
->list
[i
];
130 int locate_decl_type_enum(decl_enums
*enums
, decl_type
*type
) {
133 if (type
->real
.enm
) {
136 if (enums
) for (i
= 0; i
< enums
->count
; ++i
) {
137 if (!strcmp(enums
->list
[i
]->name
, type
->name
)) {
138 type
->real
.enm
= enums
->list
[i
];
145 int locate_decl_type_decl(decls
*decls
, decl_type
*type
) {
148 if (type
->real
.func
) {
151 if (decls
) for (i
= 0; i
< decls
->count
; ++i
) {
152 if (!strcmp(decls
->list
[i
]->func
->var
->name
, type
->name
)) {
153 type
->real
.func
= decls
->list
[i
];
161 int validate_decl_type(struct psi_data
*data
, decl_type
*type
, decl_arg
*def
) {
162 if (weak_decl_type(type
)) {
163 if (!locate_decl_type_alias(data
->defs
, type
)) {
166 if (type
->real
.def
) {
167 return validate_decl_type(data
, type
->real
.def
->type
, type
->real
.def
);
172 switch (type
->type
) {
174 if (!locate_decl_type_struct(data
->structs
, type
)) {
179 if (!locate_decl_type_union(data
->unions
, type
)) {
184 if (!locate_decl_type_enum(data
->enums
, type
)) {
189 if (!locate_decl_type_decl(data
->decls
, type
)) {
192 if (!validate_decl_nodl(data
, type
->real
.func
)) {
200 void dump_decl_type(int fd
, decl_type
*t
, unsigned level
) {
203 dprintf(fd
, "%s *", t
->name
);
207 dprintf(fd
, "enum ");
208 if (is_anon_type(t
->name
, "enum")) {
209 dump_decl_enum_items(fd
, t
->real
.enm
->items
, level
);
215 dprintf(fd
, "struct ");
216 if (is_anon_type(t
->name
, "struct")) {
217 dump_decl_args_with_layout(fd
, t
->real
.strct
->args
, level
);
223 dprintf(fd
, "union ");
224 if (is_anon_type(t
->name
, "union")) {
225 dump_decl_args_with_layout(fd
, t
->real
.unn
->args
, level
);
230 dprintf(fd
, "%s", t
->name
);
233 int weak_decl_type(decl_type
*type
) {
234 switch (type
->type
) {
246 decl_type
*real_decl_type(decl_type
*type
) {
247 while (weak_decl_type(type
)) {
248 type
= type
->real
.def
->type
;
253 size_t alignof_decl_type(decl_type
*t
) {
254 decl_type
*real
= real_decl_type(t
);
257 switch (real
->type
) {
259 align
= alignof_decl_struct(real
->real
.strct
);
262 align
= alignof_decl_union(real
->real
.unn
);
266 align
= psi_t_alignment(real
->type
);