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"
29 #include "php_globals.h"
34 struct psi_data
*psi_data_ctor_with_dtors(struct psi_data
*data
,
35 psi_error_cb error
, unsigned flags
)
38 data
= calloc(1, sizeof(*data
));
45 data
->consts
= psi_plist_init((psi_plist_dtor
) psi_const_free
);
48 data
->types
= psi_plist_init((psi_plist_dtor
) psi_decl_arg_free
);
51 data
->structs
= psi_plist_init((psi_plist_dtor
) psi_decl_struct_free
);
54 data
->unions
= psi_plist_init((psi_plist_dtor
) psi_decl_union_free
);
57 data
->enums
= psi_plist_init((psi_plist_dtor
) psi_decl_enum_free
);
60 data
->decls
= psi_plist_init((psi_plist_dtor
) psi_decl_free
);
63 data
->vars
= psi_plist_init((psi_plist_dtor
) psi_decl_extvar_free
);
66 data
->impls
= psi_plist_init((psi_plist_dtor
) psi_impl_free
);
69 data
->libs
= psi_plist_init((psi_plist_dtor
) psi_libs_free
);
74 struct psi_data
*psi_data_ctor(struct psi_data
*data
, psi_error_cb error
,
78 data
= calloc(1, sizeof(*data
));
85 data
->consts
= psi_plist_init(NULL
);
88 data
->types
= psi_plist_init(NULL
);
91 data
->structs
= psi_plist_init(NULL
);
94 data
->unions
= psi_plist_init(NULL
);
97 data
->enums
= psi_plist_init(NULL
);
100 data
->decls
= psi_plist_init(NULL
);
103 data
->vars
= psi_plist_init(NULL
);
106 data
->impls
= psi_plist_init(NULL
);
109 data
->libs
= psi_plist_init(NULL
);
114 struct psi_data
*psi_data_exchange(struct psi_data
*dest
, struct psi_data
*src
)
117 dest
= malloc(sizeof(*dest
));
120 memset(src
, 0, sizeof(*src
));
124 void psi_data_dtor(struct psi_data
*data
)
127 psi_plist_free(data
->consts
);
130 psi_plist_free(data
->types
);
133 psi_plist_free(data
->structs
);
136 psi_plist_free(data
->unions
);
139 psi_plist_free(data
->enums
);
142 psi_plist_free(data
->decls
);
145 psi_plist_free(data
->vars
);
148 psi_plist_free(data
->impls
);
151 psi_plist_free(data
->libs
);
154 psi_decl_file_dtor(&data
->file
);
157 void psi_data_dump(int fd
, struct psi_data
*D
)
160 dprintf(fd
, "// filename=%s (%u errors)\n", D
->file
.fn
, D
->errors
);
162 dprintf(fd
, "lib \"%s\";\n", D
->file
.ln
);
165 dprintf(fd
, "// builtin predef\n");
167 if (psi_plist_count(D
->types
)) {
169 struct psi_decl_arg
*def
;
171 while (psi_plist_get(D
->types
, i
++, &def
)) {
172 dprintf(fd
, "typedef ");
173 psi_decl_arg_dump(fd
, def
, 0);
178 if (psi_plist_count(D
->unions
)) {
180 struct psi_decl_union
*unn
;
182 while (psi_plist_get(D
->unions
, i
++, &unn
)) {
183 if (!psi_decl_type_is_anon(unn
->name
, "union")) {
184 psi_decl_union_dump(fd
, unn
);
190 if (psi_plist_count(D
->structs
)) {
192 struct psi_decl_struct
*strct
;
194 while (psi_plist_get(D
->structs
, i
++, &strct
)) {
195 if (!psi_decl_type_is_anon(strct
->name
, "struct")) {
196 psi_decl_struct_dump(fd
, strct
);
202 if (psi_plist_count(D
->enums
)) {
204 struct psi_decl_enum
*enm
;
206 while (psi_plist_get(D
->enums
, i
++, &enm
)) {
207 if (!psi_decl_type_is_anon(enm
->name
, "enum")) {
208 psi_decl_enum_dump(fd
, enm
, 0);
214 if (psi_plist_count(D
->consts
)) {
218 while (psi_plist_get(D
->consts
, i
++, &c
)) {
219 psi_const_dump(fd
, c
);
224 if (psi_plist_count(D
->decls
)) {
226 struct psi_decl
*decl
;
228 while (psi_plist_get(D
->decls
, i
++, &decl
)) {
229 psi_decl_dump(fd
, decl
);
234 if (psi_plist_count(D
->vars
)) {
236 struct psi_decl_extvar
*evar
;
238 while (psi_plist_get(D
->vars
, i
++, &evar
)) {
239 psi_decl_extvar_dump(fd
, evar
);
243 if (psi_plist_count(D
->impls
)) {
245 struct psi_impl
*impl
;
247 while (psi_plist_get(D
->impls
, i
++, &impl
)) {
248 psi_impl_dump(fd
, impl
);