fb94d302b0bd20b905783f8730cd0b49d4a1c750
[m6w6/ext-psi] / src / data.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
13
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 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include "php_globals.h"
30
31 #include <dlfcn.h>
32 #include <ctype.h>
33
34 static void psi_data_ctor_internal(struct psi_data *data,
35 psi_error_cb error, unsigned flags)
36 {
37 data->error = error;
38 data->flags = flags;
39
40 if (data->flags & PSI_DEBUG) {
41 int fd = psi_fdopen(getenv("PSI_DEBUG"));
42
43 if (fd > 0) {
44 data->debug_fd = fd;
45 } else {
46 data->debug_fd = STDERR_FILENO;
47 }
48 }
49 }
50
51 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data,
52 psi_error_cb error, unsigned flags)
53 {
54 if (!data) {
55 data = pecalloc(1, sizeof(*data), 1);
56 }
57
58 psi_data_ctor_internal(data, error, flags);
59
60 if (!data->file.libnames) {
61 data->file.libnames = psi_plist_init((psi_plist_dtor) psi_names_free);
62 }
63 if (!data->file.dlopened) {
64 data->file.dlopened = psi_plist_init((psi_plist_dtor) psi_libs_free);
65 }
66
67 if (!data->consts) {
68 data->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
69 }
70 if (!data->types) {
71 data->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
72 }
73 if (!data->structs) {
74 data->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
75 }
76 if (!data->unions) {
77 data->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
78 }
79 if (!data->enums) {
80 data->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
81 }
82 if (!data->decls) {
83 data->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
84 }
85 if (!data->vars) {
86 data->vars = psi_plist_init((psi_plist_dtor) psi_decl_extvar_free);
87 }
88 if (!data->impls) {
89 data->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
90 }
91 return data;
92 }
93
94 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error,
95 unsigned flags)
96 {
97 if (!data) {
98 data = pecalloc(1, sizeof(*data), 1);
99 }
100
101 psi_data_ctor_internal(data, error, flags);
102
103 if (!data->file.libnames) {
104 data->file.libnames = psi_plist_init(NULL);
105 }
106 if (!data->file.dlopened) {
107 data->file.dlopened = psi_plist_init(NULL);
108 }
109
110 if (!data->consts) {
111 data->consts = psi_plist_init(NULL);
112 }
113 if (!data->types) {
114 data->types = psi_plist_init(NULL);
115 }
116 if (!data->structs) {
117 data->structs = psi_plist_init(NULL);
118 }
119 if (!data->unions) {
120 data->unions = psi_plist_init(NULL);
121 }
122 if (!data->enums) {
123 data->enums = psi_plist_init(NULL);
124 }
125 if (!data->decls) {
126 data->decls = psi_plist_init(NULL);
127 }
128 if (!data->vars) {
129 data->vars = psi_plist_init(NULL);
130 }
131 if (!data->impls) {
132 data->impls = psi_plist_init(NULL);
133 }
134 return data;
135 }
136
137 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src)
138 {
139 if (!dest) {
140 dest = pemalloc(sizeof(*dest), 1);
141 }
142 *dest = *src;
143 memset(src, 0, sizeof(*src));
144 return dest;
145 }
146
147 void psi_data_dtor(struct psi_data *data)
148 {
149 if (data->debug_fd) {
150 close(data->debug_fd);
151 }
152 if (data->consts) {
153 psi_plist_free(data->consts);
154 }
155 if (data->types) {
156 psi_plist_free(data->types);
157 }
158 if (data->structs) {
159 psi_plist_free(data->structs);
160 }
161 if (data->unions) {
162 psi_plist_free(data->unions);
163 }
164 if (data->enums) {
165 psi_plist_free(data->enums);
166 }
167 if (data->decls) {
168 psi_plist_free(data->decls);
169 }
170 if (data->vars) {
171 psi_plist_free(data->vars);
172 }
173 if (data->impls) {
174 psi_plist_free(data->impls);
175 }
176
177 psi_decl_file_dtor(&data->file);
178 }
179
180 void psi_data_dump(struct psi_dump *dump, struct psi_data *D)
181 {
182 size_t i = 0;
183 char *libname;
184
185 if (D->file.filename) {
186 PSI_DUMP(dump, "// filename=%s (%u errors)\n", D->file.filename->val, D->errors);
187 }
188 while (psi_plist_get(D->file.libnames, i++, &libname)) {
189 PSI_DUMP(dump, "lib \"%s\";\n", libname);
190 }
191 if (psi_plist_count(D->types)) {
192 size_t i = 0;
193 struct psi_decl_arg *def;
194
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");
199 }
200 PSI_DUMP(dump, "\n");
201 }
202 if (psi_plist_count(D->unions)) {
203 size_t i = 0;
204 struct psi_decl_union *unn;
205
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");
210 }
211 }
212 PSI_DUMP(dump, "\n");
213 }
214 if (psi_plist_count(D->structs)) {
215 size_t i = 0;
216 struct psi_decl_struct *strct;
217
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");
222 }
223 }
224 PSI_DUMP(dump, "\n");
225 }
226 if (psi_plist_count(D->enums)) {
227 size_t i = 0;
228 struct psi_decl_enum *enm;
229
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");
234 }
235 }
236 PSI_DUMP(dump, "\n");
237 }
238 if (psi_plist_count(D->consts)) {
239 size_t i = 0;
240 struct psi_const *c;
241
242 while (psi_plist_get(D->consts, i++, &c)) {
243 psi_const_dump(dump, c);
244 PSI_DUMP(dump, "\n");
245 }
246 PSI_DUMP(dump, "\n");
247 }
248 if (psi_plist_count(D->decls)) {
249 size_t i = 0;
250 struct psi_decl *decl;
251
252 while (psi_plist_get(D->decls, i++, &decl)) {
253 if (decl->extvar) {
254 PSI_DUMP(dump, "/* extvar accessor\n");
255 }
256 psi_decl_dump(dump, decl);
257 PSI_DUMP(dump, "\n");
258 if (decl->extvar) {
259 PSI_DUMP(dump, " extvar accessor */\n");
260 }
261 }
262 PSI_DUMP(dump, "\n");
263 }
264 if (psi_plist_count(D->vars)) {
265 size_t i = 0;
266 struct psi_decl_extvar *evar;
267
268 while (psi_plist_get(D->vars, i++, &evar)) {
269 psi_decl_extvar_dump(dump, evar);
270 }
271 PSI_DUMP(dump, "\n");
272 }
273 if (psi_plist_count(D->impls)) {
274 size_t i = 0;
275 struct psi_impl *impl;
276
277 while (psi_plist_get(D->impls, i++, &impl)) {
278 psi_impl_dump(dump, impl);
279 PSI_DUMP(dump, "\n");
280 }
281 PSI_DUMP(dump, "\n");
282 }
283 }