Merge branch 'slimconfigure'
[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 struct psi_data *psi_data_ctor_with_dtors(struct psi_data *data,
35 psi_error_cb error, unsigned flags)
36 {
37 if (!data) {
38 data = calloc(1, sizeof(*data));
39 }
40
41 data->error = error;
42 data->flags = flags;
43
44 if (!data->consts) {
45 data->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
46 }
47 if (!data->types) {
48 data->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
49 }
50 if (!data->structs) {
51 data->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
52 }
53 if (!data->unions) {
54 data->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
55 }
56 if (!data->enums) {
57 data->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
58 }
59 if (!data->decls) {
60 data->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
61 }
62 if (!data->vars) {
63 data->vars = psi_plist_init((psi_plist_dtor) psi_decl_extvar_free);
64 }
65 if (!data->impls) {
66 data->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
67 }
68 if (!data->libs) {
69 data->libs = psi_plist_init((psi_plist_dtor) psi_libs_free);
70 }
71 return data;
72 }
73
74 struct psi_data *psi_data_ctor(struct psi_data *data, psi_error_cb error,
75 unsigned flags)
76 {
77 if (!data) {
78 data = calloc(1, sizeof(*data));
79 }
80
81 data->error = error;
82 data->flags = flags;
83
84 if (!data->consts) {
85 data->consts = psi_plist_init(NULL);
86 }
87 if (!data->types) {
88 data->types = psi_plist_init(NULL);
89 }
90 if (!data->structs) {
91 data->structs = psi_plist_init(NULL);
92 }
93 if (!data->unions) {
94 data->unions = psi_plist_init(NULL);
95 }
96 if (!data->enums) {
97 data->enums = psi_plist_init(NULL);
98 }
99 if (!data->decls) {
100 data->decls = psi_plist_init(NULL);
101 }
102 if (!data->vars) {
103 data->vars = psi_plist_init(NULL);
104 }
105 if (!data->impls) {
106 data->impls = psi_plist_init(NULL);
107 }
108 if (!data->libs) {
109 data->libs = psi_plist_init(NULL);
110 }
111 return data;
112 }
113
114 struct psi_data *psi_data_exchange(struct psi_data *dest, struct psi_data *src)
115 {
116 if (!dest) {
117 dest = malloc(sizeof(*dest));
118 }
119 *dest = *src;
120 memset(src, 0, sizeof(*src));
121 return dest;
122 }
123
124 void psi_data_dtor(struct psi_data *data)
125 {
126 if (data->consts) {
127 psi_plist_free(data->consts);
128 }
129 if (data->types) {
130 psi_plist_free(data->types);
131 }
132 if (data->structs) {
133 psi_plist_free(data->structs);
134 }
135 if (data->unions) {
136 psi_plist_free(data->unions);
137 }
138 if (data->enums) {
139 psi_plist_free(data->enums);
140 }
141 if (data->decls) {
142 psi_plist_free(data->decls);
143 }
144 if (data->vars) {
145 psi_plist_free(data->vars);
146 }
147 if (data->impls) {
148 psi_plist_free(data->impls);
149 }
150 if (data->libs) {
151 psi_plist_free(data->libs);
152 }
153
154 psi_decl_file_dtor(&data->file);
155 }
156
157 void psi_data_dump(int fd, struct psi_data *D)
158 {
159 if (D->file.fn) {
160 dprintf(fd, "// filename=%s (%u errors)\n", D->file.fn, D->errors);
161 if (D->file.ln) {
162 dprintf(fd, "lib \"%s\";\n", D->file.ln);
163 }
164 } else {
165 dprintf(fd, "// builtin predef\n");
166 }
167 if (psi_plist_count(D->types)) {
168 size_t i = 0;
169 struct psi_decl_arg *def;
170
171 while (psi_plist_get(D->types, i++, &def)) {
172 dprintf(fd, "typedef ");
173 psi_decl_arg_dump(fd, def, 0);
174 dprintf(fd, ";\n");
175 }
176 dprintf(fd, "\n");
177 }
178 if (psi_plist_count(D->unions)) {
179 size_t i = 0;
180 struct psi_decl_union *unn;
181
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);
185 dprintf(fd, "\n");
186 }
187 }
188 dprintf(fd, "\n");
189 }
190 if (psi_plist_count(D->structs)) {
191 size_t i = 0;
192 struct psi_decl_struct *strct;
193
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);
197 dprintf(fd, "\n");
198 }
199 }
200 dprintf(fd, "\n");
201 }
202 if (psi_plist_count(D->enums)) {
203 size_t i = 0;
204 struct psi_decl_enum *enm;
205
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);
209 dprintf(fd, "\n");
210 }
211 }
212 dprintf(fd, "\n");
213 }
214 if (psi_plist_count(D->consts)) {
215 size_t i = 0;
216 struct psi_const *c;
217
218 while (psi_plist_get(D->consts, i++, &c)) {
219 psi_const_dump(fd, c);
220 dprintf(fd, "\n");
221 }
222 dprintf(fd, "\n");
223 }
224 if (psi_plist_count(D->decls)) {
225 size_t i = 0;
226 struct psi_decl *decl;
227
228 while (psi_plist_get(D->decls, i++, &decl)) {
229 psi_decl_dump(fd, decl);
230 dprintf(fd, "\n");
231 }
232 dprintf(fd, "\n");
233 }
234 if (psi_plist_count(D->vars)) {
235 size_t i = 0;
236 struct psi_decl_extvar *evar;
237
238 while (psi_plist_get(D->vars, i++, &evar)) {
239 psi_decl_extvar_dump(fd, evar);
240 }
241 dprintf(fd, "\n");
242 }
243 if (psi_plist_count(D->impls)) {
244 size_t i = 0;
245 struct psi_impl *impl;
246
247 while (psi_plist_get(D->impls, i++, &impl)) {
248 psi_impl_dump(fd, impl);
249 dprintf(fd, "\n");
250 }
251 dprintf(fd, "\n");
252 }
253 }