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"
34 #include "php_globals.h"
35 #include "zend_types.h"
40 #if PSI_THREADED_PARSER
43 static pthread_mutex_t psi_string_mutex
= PTHREAD_MUTEX_INITIALIZER
;
45 zend_string
*psi_string_init_interned(const char *buf
, size_t len
, int p
)
49 pthread_mutex_lock(&psi_string_mutex
);
50 str
= zend_string_init_interned(buf
, len
, p
);
51 pthread_mutex_unlock(&psi_string_mutex
);
56 zend_string
*psi_new_interned_string(zend_string
*str
)
60 pthread_mutex_lock(&psi_string_mutex
);
61 new_str
= zend_new_interned_string(str
);
62 pthread_mutex_unlock(&psi_string_mutex
);
69 static void psi_data_ctor_internal(struct psi_data
*data
,
70 psi_error_cb error
, unsigned flags
)
75 if (data
->flags
& PSI_DEBUG
) {
76 int fd
= psi_fdopen(getenv("PSI_DEBUG"));
81 data
->debug_fd
= STDERR_FILENO
;
86 struct psi_data
*psi_data_ctor_with_dtors(struct psi_data
*data
,
87 psi_error_cb error
, unsigned flags
)
90 data
= pecalloc(1, sizeof(*data
), 1);
93 psi_data_ctor_internal(data
, error
, flags
);
95 if (!data
->file
.libnames
) {
96 data
->file
.libnames
= psi_plist_init((psi_plist_dtor
) psi_names_free
);
98 if (!data
->file
.dlopened
) {
99 data
->file
.dlopened
= psi_plist_init((psi_plist_dtor
) psi_libs_free
);
103 data
->consts
= psi_plist_init((psi_plist_dtor
) psi_const_free
);
106 data
->types
= psi_plist_init((psi_plist_dtor
) psi_decl_arg_free
);
108 if (!data
->structs
) {
109 data
->structs
= psi_plist_init((psi_plist_dtor
) psi_decl_struct_free
);
112 data
->unions
= psi_plist_init((psi_plist_dtor
) psi_decl_union_free
);
115 data
->enums
= psi_plist_init((psi_plist_dtor
) psi_decl_enum_free
);
118 data
->decls
= psi_plist_init((psi_plist_dtor
) psi_decl_free
);
121 data
->vars
= psi_plist_init((psi_plist_dtor
) psi_decl_extvar_free
);
124 data
->impls
= psi_plist_init((psi_plist_dtor
) psi_impl_free
);
129 struct psi_data
*psi_data_ctor(struct psi_data
*data
, psi_error_cb error
,
133 data
= pecalloc(1, sizeof(*data
), 1);
136 psi_data_ctor_internal(data
, error
, flags
);
138 if (!data
->file
.libnames
) {
139 data
->file
.libnames
= psi_plist_init(NULL
);
141 if (!data
->file
.dlopened
) {
142 data
->file
.dlopened
= psi_plist_init(NULL
);
146 data
->consts
= psi_plist_init(NULL
);
149 data
->types
= psi_plist_init(NULL
);
151 if (!data
->structs
) {
152 data
->structs
= psi_plist_init(NULL
);
155 data
->unions
= psi_plist_init(NULL
);
158 data
->enums
= psi_plist_init(NULL
);
161 data
->decls
= psi_plist_init(NULL
);
164 data
->vars
= psi_plist_init(NULL
);
167 data
->impls
= psi_plist_init(NULL
);
172 struct psi_data
*psi_data_exchange(struct psi_data
*dest
, struct psi_data
*src
)
175 dest
= pemalloc(sizeof(*dest
), 1);
178 memset(src
, 0, sizeof(*src
));
182 void psi_data_dtor(struct psi_data
*data
)
184 if (data
->debug_fd
) {
185 close(data
->debug_fd
);
188 psi_plist_free(data
->consts
);
191 psi_plist_free(data
->types
);
194 psi_plist_free(data
->structs
);
197 psi_plist_free(data
->unions
);
200 psi_plist_free(data
->enums
);
203 psi_plist_free(data
->decls
);
206 psi_plist_free(data
->vars
);
209 psi_plist_free(data
->impls
);
212 psi_decl_file_dtor(&data
->file
);
215 void psi_data_dump(struct psi_dump
*dump
, struct psi_data
*D
)
218 zend_string
*libname
;
220 if (D
->file
.filename
) {
221 PSI_DUMP(dump
, "// filename=%s (%u errors)\n", D
->file
.filename
->val
, D
->errors
);
223 while (psi_plist_get(D
->file
.libnames
, i
++, &libname
)) {
224 PSI_DUMP(dump
, "#pragma lib \"%s\"\n", libname
->val
);
226 if (psi_plist_count(D
->types
)) {
228 struct psi_decl_arg
*def
;
230 while (psi_plist_get(D
->types
, i
++, &def
)) {
231 PSI_DUMP(dump
, "typedef ");
232 psi_decl_arg_dump(dump
, def
, 0);
233 PSI_DUMP(dump
, ";\n");
235 PSI_DUMP(dump
, "\n");
237 if (psi_plist_count(D
->unions
)) {
239 struct psi_decl_union
*unn
;
241 while (psi_plist_get(D
->unions
, i
++, &unn
)) {
242 if (!psi_decl_type_is_anon(unn
->name
, "union")) {
243 psi_decl_union_dump(dump
, unn
);
244 PSI_DUMP(dump
, "\n");
247 PSI_DUMP(dump
, "\n");
249 if (psi_plist_count(D
->structs
)) {
251 struct psi_decl_struct
*strct
;
253 while (psi_plist_get(D
->structs
, i
++, &strct
)) {
254 if (!psi_decl_type_is_anon(strct
->name
, "struct")) {
255 psi_decl_struct_dump(dump
, strct
);
256 PSI_DUMP(dump
, "\n");
259 PSI_DUMP(dump
, "\n");
261 if (psi_plist_count(D
->enums
)) {
263 struct psi_decl_enum
*enm
;
265 while (psi_plist_get(D
->enums
, i
++, &enm
)) {
266 if (true || !psi_decl_type_is_anon(enm
->name
, "enum")) {
267 psi_decl_enum_dump(dump
, enm
, 0);
268 PSI_DUMP(dump
, "\n");
271 PSI_DUMP(dump
, "\n");
273 if (psi_plist_count(D
->consts
)) {
277 while (psi_plist_get(D
->consts
, i
++, &c
)) {
278 psi_const_dump(dump
, c
);
279 PSI_DUMP(dump
, "\n");
281 PSI_DUMP(dump
, "\n");
283 if (psi_plist_count(D
->decls
)) {
285 struct psi_decl
*decl
;
287 while (psi_plist_get(D
->decls
, i
++, &decl
)) {
289 PSI_DUMP(dump
, "/* extvar accessor \n");
291 psi_decl_dump(dump
, decl
);
293 PSI_DUMP(dump
, " */");
295 PSI_DUMP(dump
, "\n");
297 PSI_DUMP(dump
, "\n");
299 if (psi_plist_count(D
->vars
)) {
301 struct psi_decl_extvar
*evar
;
303 while (psi_plist_get(D
->vars
, i
++, &evar
)) {
304 psi_decl_extvar_dump(dump
, evar
);
306 PSI_DUMP(dump
, "\n");
308 if (psi_plist_count(D
->impls
)) {
310 struct psi_impl
*impl
;
312 while (psi_plist_get(D
->impls
, i
++, &impl
)) {
313 psi_impl_dump(dump
, impl
);
314 PSI_DUMP(dump
, "\n");
316 PSI_DUMP(dump
, "\n");