fb94d302b0bd20b905783f8730cd0b49d4a1c750
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 static void psi_data_ctor_internal(struct psi_data
*data
,
35 psi_error_cb error
, unsigned flags
)
40 if (data
->flags
& PSI_DEBUG
) {
41 int fd
= psi_fdopen(getenv("PSI_DEBUG"));
46 data
->debug_fd
= STDERR_FILENO
;
51 struct psi_data
*psi_data_ctor_with_dtors(struct psi_data
*data
,
52 psi_error_cb error
, unsigned flags
)
55 data
= pecalloc(1, sizeof(*data
), 1);
58 psi_data_ctor_internal(data
, error
, flags
);
60 if (!data
->file
.libnames
) {
61 data
->file
.libnames
= psi_plist_init((psi_plist_dtor
) psi_names_free
);
63 if (!data
->file
.dlopened
) {
64 data
->file
.dlopened
= psi_plist_init((psi_plist_dtor
) psi_libs_free
);
68 data
->consts
= psi_plist_init((psi_plist_dtor
) psi_const_free
);
71 data
->types
= psi_plist_init((psi_plist_dtor
) psi_decl_arg_free
);
74 data
->structs
= psi_plist_init((psi_plist_dtor
) psi_decl_struct_free
);
77 data
->unions
= psi_plist_init((psi_plist_dtor
) psi_decl_union_free
);
80 data
->enums
= psi_plist_init((psi_plist_dtor
) psi_decl_enum_free
);
83 data
->decls
= psi_plist_init((psi_plist_dtor
) psi_decl_free
);
86 data
->vars
= psi_plist_init((psi_plist_dtor
) psi_decl_extvar_free
);
89 data
->impls
= psi_plist_init((psi_plist_dtor
) psi_impl_free
);
94 struct psi_data
*psi_data_ctor(struct psi_data
*data
, psi_error_cb error
,
98 data
= pecalloc(1, sizeof(*data
), 1);
101 psi_data_ctor_internal(data
, error
, flags
);
103 if (!data
->file
.libnames
) {
104 data
->file
.libnames
= psi_plist_init(NULL
);
106 if (!data
->file
.dlopened
) {
107 data
->file
.dlopened
= psi_plist_init(NULL
);
111 data
->consts
= psi_plist_init(NULL
);
114 data
->types
= psi_plist_init(NULL
);
116 if (!data
->structs
) {
117 data
->structs
= psi_plist_init(NULL
);
120 data
->unions
= psi_plist_init(NULL
);
123 data
->enums
= psi_plist_init(NULL
);
126 data
->decls
= psi_plist_init(NULL
);
129 data
->vars
= psi_plist_init(NULL
);
132 data
->impls
= psi_plist_init(NULL
);
137 struct psi_data
*psi_data_exchange(struct psi_data
*dest
, struct psi_data
*src
)
140 dest
= pemalloc(sizeof(*dest
), 1);
143 memset(src
, 0, sizeof(*src
));
147 void psi_data_dtor(struct psi_data
*data
)
149 if (data
->debug_fd
) {
150 close(data
->debug_fd
);
153 psi_plist_free(data
->consts
);
156 psi_plist_free(data
->types
);
159 psi_plist_free(data
->structs
);
162 psi_plist_free(data
->unions
);
165 psi_plist_free(data
->enums
);
168 psi_plist_free(data
->decls
);
171 psi_plist_free(data
->vars
);
174 psi_plist_free(data
->impls
);
177 psi_decl_file_dtor(&data
->file
);
180 void psi_data_dump(struct psi_dump
*dump
, struct psi_data
*D
)
185 if (D
->file
.filename
) {
186 PSI_DUMP(dump
, "// filename=%s (%u errors)\n", D
->file
.filename
->val
, D
->errors
);
188 while (psi_plist_get(D
->file
.libnames
, i
++, &libname
)) {
189 PSI_DUMP(dump
, "lib \"%s\";\n", libname
);
191 if (psi_plist_count(D
->types
)) {
193 struct psi_decl_arg
*def
;
195 while (psi_plist_get(D
->types
, i
++, &def
)) {
196 PSI_DUMP(dump
, "typedef ");
197 psi_decl_arg_dump(dump
, def
, 0);
198 PSI_DUMP(dump
, ";\n");
200 PSI_DUMP(dump
, "\n");
202 if (psi_plist_count(D
->unions
)) {
204 struct psi_decl_union
*unn
;
206 while (psi_plist_get(D
->unions
, i
++, &unn
)) {
207 if (!psi_decl_type_is_anon(unn
->name
, "union")) {
208 psi_decl_union_dump(dump
, unn
);
209 PSI_DUMP(dump
, "\n");
212 PSI_DUMP(dump
, "\n");
214 if (psi_plist_count(D
->structs
)) {
216 struct psi_decl_struct
*strct
;
218 while (psi_plist_get(D
->structs
, i
++, &strct
)) {
219 if (!psi_decl_type_is_anon(strct
->name
, "struct")) {
220 psi_decl_struct_dump(dump
, strct
);
221 PSI_DUMP(dump
, "\n");
224 PSI_DUMP(dump
, "\n");
226 if (psi_plist_count(D
->enums
)) {
228 struct psi_decl_enum
*enm
;
230 while (psi_plist_get(D
->enums
, i
++, &enm
)) {
231 if (!psi_decl_type_is_anon(enm
->name
, "enum")) {
232 psi_decl_enum_dump(dump
, enm
, 0);
233 PSI_DUMP(dump
, "\n");
236 PSI_DUMP(dump
, "\n");
238 if (psi_plist_count(D
->consts
)) {
242 while (psi_plist_get(D
->consts
, i
++, &c
)) {
243 psi_const_dump(dump
, c
);
244 PSI_DUMP(dump
, "\n");
246 PSI_DUMP(dump
, "\n");
248 if (psi_plist_count(D
->decls
)) {
250 struct psi_decl
*decl
;
252 while (psi_plist_get(D
->decls
, i
++, &decl
)) {
254 PSI_DUMP(dump
, "/* extvar accessor\n");
256 psi_decl_dump(dump
, decl
);
257 PSI_DUMP(dump
, "\n");
259 PSI_DUMP(dump
, " extvar accessor */\n");
262 PSI_DUMP(dump
, "\n");
264 if (psi_plist_count(D
->vars
)) {
266 struct psi_decl_extvar
*evar
;
268 while (psi_plist_get(D
->vars
, i
++, &evar
)) {
269 psi_decl_extvar_dump(dump
, evar
);
271 PSI_DUMP(dump
, "\n");
273 if (psi_plist_count(D
->impls
)) {
275 struct psi_impl
*impl
;
277 while (psi_plist_get(D
->impls
, i
++, &impl
)) {
278 psi_impl_dump(dump
, impl
);
279 PSI_DUMP(dump
, "\n");
281 PSI_DUMP(dump
, "\n");