c7b19b68c4a53c067d84ecc475309138b965ec7e
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
;
74 size_t extract_decl_type_size(decl_type
*dtyp
, decl_type
**real_typ_ptr
) {
76 var_typ
= real_decl_type(dtyp
);
78 *real_typ_ptr
= var_typ
;
80 switch (var_typ
->type
) {
82 return var_typ
->real
.strct
->size
;
84 return var_typ
->real
.unn
->size
;
86 return psi_t_size(var_typ
->type
);
92 int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
94 struct psi_std_type
*stdtyp
;
99 if (defs
) for (i
= 0; i
< defs
->count
; ++i
) {
100 decl_arg
*def
= defs
->list
[i
];
102 if (def
->type
->type
!= type
->type
&& !strcmp(def
->var
->name
, type
->name
)) {
103 type
->real
.def
= def
;
107 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
108 if (!strcmp(type
->name
, stdtyp
->alias
?: stdtyp
->type_name
)) {
109 type
->type
= stdtyp
->type_tag
;
117 int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
120 if (type
->real
.strct
) {
123 if (structs
) for (i
= 0; i
< structs
->count
; ++i
) {
124 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
125 type
->real
.strct
= structs
->list
[i
];
132 int locate_decl_type_union(decl_unions
*unions
, decl_type
*type
) {
135 if (type
->real
.unn
) {
138 if (unions
) for (i
= 0; i
< unions
->count
; ++i
) {
139 if (!strcmp(unions
->list
[i
]->name
, type
->name
)) {
140 type
->real
.unn
= unions
->list
[i
];
147 int locate_decl_type_enum(decl_enums
*enums
, decl_type
*type
) {
150 if (type
->real
.enm
) {
153 if (enums
) for (i
= 0; i
< enums
->count
; ++i
) {
154 if (!strcmp(enums
->list
[i
]->name
, type
->name
)) {
155 type
->real
.enm
= enums
->list
[i
];
162 int locate_decl_type_decl(decls
*decls
, decl_type
*type
) {
165 if (type
->real
.func
) {
168 if (decls
) for (i
= 0; i
< decls
->count
; ++i
) {
169 if (!strcmp(decls
->list
[i
]->func
->var
->name
, type
->name
)) {
170 type
->real
.func
= decls
->list
[i
];
178 int validate_decl_type(struct psi_data
*data
, decl_type
*type
, decl_arg
*def
) {
179 if (weak_decl_type(type
)) {
180 if (!locate_decl_type_alias(data
->defs
, type
)) {
183 if (type
->real
.def
) {
184 return validate_decl_type(data
, type
->real
.def
->type
, type
->real
.def
);
189 switch (type
->type
) {
191 if (!locate_decl_type_struct(data
->structs
, type
)) {
196 if (!locate_decl_type_union(data
->unions
, type
)) {
201 if (!locate_decl_type_enum(data
->enums
, type
)) {
206 if (!locate_decl_type_decl(data
->decls
, type
)) {
209 if (!validate_decl_nodl(data
, type
->real
.func
)) {
217 void dump_decl_type(int fd
, decl_type
*t
, unsigned level
) {
220 dprintf(fd
, "%s *", t
->name
);
224 dprintf(fd
, "enum ");
225 if (is_anon_type(t
->name
, "enum")) {
226 dump_decl_enum_items(fd
, t
->real
.enm
->items
, level
);
232 dprintf(fd
, "struct ");
233 if (is_anon_type(t
->name
, "struct")) {
234 dump_decl_args_with_layout(fd
, t
->real
.strct
->args
, level
);
240 dprintf(fd
, "union ");
241 if (is_anon_type(t
->name
, "union")) {
242 dump_decl_args_with_layout(fd
, t
->real
.unn
->args
, level
);
247 dprintf(fd
, "%s", t
->name
);
250 int weak_decl_type(decl_type
*type
) {
251 switch (type
->type
) {
263 decl_type
*real_decl_type(decl_type
*type
) {
264 while (weak_decl_type(type
) && type
->real
.def
) {
265 type
= type
->real
.def
->type
;
270 size_t alignof_decl_type(decl_type
*t
) {
271 decl_type
*real
= real_decl_type(t
);
274 switch (real
->type
) {
276 align
= alignof_decl_struct(real
->real
.strct
);
279 align
= alignof_decl_union(real
->real
.unn
);
283 align
= psi_t_alignment(real
->type
);