silence first validation round
[m6w6/ext-psi] / src / context.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6
7 #ifdef HAVE_DIRENT_H
8 # include <dirent.h>
9 # define NAMLEN(dirent) strlen ((dirent)->d_name)
10 #else
11 # define dirent direct
12 # define NAMLEN(dirent) ((dirent)->d_namlen)
13 # ifdef HAVE_SYS_NDIR_H
14 # include <sys/ndir.h>
15 # endif
16 # ifdef HAVE_SYS_DIR_H
17 # include <sys/dir.h>
18 # endif
19 # ifdef HAVE_NDIR_H
20 # include <ndir.h>
21 # endif
22 #endif
23
24 #include <fnmatch.h>
25
26 #include "php.h"
27 #include "php_scandir.h"
28 #include "php_psi.h"
29 #include "calc.h"
30 #include "libjit.h"
31 #include "libffi.h"
32
33 #include "php_psi_types.h"
34 #include "php_psi_consts.h"
35 #include "php_psi_decls.h"
36 #include "php_psi_va_decls.h"
37 #include "php_psi_structs.h"
38
39
40 PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErrorFunc error, unsigned flags)
41 {
42 PSI_Data T;
43 struct psi_predef_type *predef_type;
44 struct psi_predef_const *predef_const;
45 struct psi_predef_struct *predef_struct;
46 struct psi_predef_decl *predef_decl;
47
48 if (!C) {
49 C = malloc(sizeof(*C));
50 }
51 memset(C, 0, sizeof(*C));
52
53 C->error = error;
54 C->flags = flags;
55 C->ops = ops;
56
57 if (ops->init) {
58 ops->init(C);
59 }
60
61 ZEND_ASSERT(ops->call != NULL);
62 ZEND_ASSERT(ops->compile != NULL);
63
64 /* build up predefs in a temporary PSI_Data for validation */
65 memset(&T, 0, sizeof(T));
66 T.error = error;
67
68 for (predef_type = &psi_predef_types[0]; predef_type->type_tag; ++predef_type) {
69 decl_type *type = init_decl_type(predef_type->type_tag, predef_type->type_name);
70 decl_var *var = init_decl_var(predef_type->alias, 0, 0); /* FIXME: indirection */
71 decl_arg *def = init_decl_arg(type, var);
72
73 T.defs = add_decl_typedef(T.defs, def);
74 }
75 for (predef_const = &psi_predef_consts[0]; predef_const->type_tag; ++predef_const) {
76 impl_def_val *val = init_impl_def_val(predef_const->val_type_tag, predef_const->val_text);
77 const_type *type = init_const_type(predef_const->type_tag, predef_const->type_name);
78 constant *constant = init_constant(type, predef_const->var_name, val);
79
80 T.consts = add_constant(T.consts, constant);
81 }
82 for (predef_struct = &psi_predef_structs[0]; predef_struct->type_tag; ++predef_struct) {
83 struct psi_predef_struct *member;
84 decl_args *dargs = init_decl_args(NULL);
85 decl_struct *dstruct = init_decl_struct(predef_struct->var_name, dargs);
86
87 dstruct->size = predef_struct->size;
88 for (member = &predef_struct[1]; member->type_tag; ++member) {
89 decl_type *type;
90 decl_var *dvar;
91 decl_arg *darg;
92
93 type = init_decl_type(member->type_tag, member->type_name);
94 dvar = init_decl_var(member->var_name, member->pointer_level, member->array_size);
95 darg = init_decl_arg(type, dvar);
96 darg->layout = init_decl_struct_layout(member->offset, member->size);
97 dargs = add_decl_arg(dargs, darg);
98 }
99
100 T.structs = add_decl_struct(T.structs, dstruct);
101 predef_struct = member;
102 }
103 for (predef_decl = &psi_predef_decls[0]; predef_decl->type_tag; ++predef_decl) {
104 struct psi_predef_decl *farg;
105 decl_type *ftype = init_decl_type(predef_decl->type_tag, predef_decl->type_name);
106 decl_var *fname = init_decl_var(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
107 decl_arg *func = init_decl_arg(ftype, fname);
108 decl_args *args = init_decl_args(NULL);
109 decl *decl = init_decl(init_decl_abi("default"), func, args);
110
111 for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
112 decl_type *arg_type = init_decl_type(farg->type_tag, farg->type_name);
113 decl_var *arg_var = init_decl_var(farg->var_name, farg->pointer_level, farg->array_size);
114 decl_arg *darg = init_decl_arg(arg_type, arg_var);
115 args = add_decl_arg(args, darg);
116 }
117
118 T.decls = add_decl(T.decls, decl);
119 predef_decl = farg;
120 }
121
122 for (predef_decl = &psi_predef_vararg_decls[0]; predef_decl->type_tag; ++predef_decl) {
123 struct psi_predef_decl *farg;
124 decl_type *ftype = init_decl_type(predef_decl->type_tag, predef_decl->type_name);
125 decl_var *fname = init_decl_var(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
126 decl_arg *func = init_decl_arg(ftype, fname);
127 decl_args *args = init_decl_args(NULL);
128 decl *decl = init_decl(init_decl_abi("default"), func, args);
129
130 for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
131 decl_type *arg_type = init_decl_type(farg->type_tag, farg->type_name);
132 decl_var *arg_var = init_decl_var(farg->var_name, farg->pointer_level, farg->array_size);
133 decl_arg *darg = init_decl_arg(arg_type, arg_var);
134 args = add_decl_arg(args, darg);
135 }
136 args->varargs = 1;
137
138 T.decls = add_decl(T.decls, decl);
139 predef_decl = farg;
140 }
141
142 PSI_ContextValidateData(PSI_DATA(C), &T);
143
144 C->count = 1;
145 C->data = malloc(sizeof(*C->data));
146 PSI_DataExchange(C->data, &T);
147
148 return C;
149 }
150
151 static int psi_select_dirent(const struct dirent *entry)
152 {
153 #ifndef FNM_CASEFOLD
154 #define FNM_CASEFOLD 0
155 #endif
156 return 0 == fnmatch("*.psi", entry->d_name, FNM_CASEFOLD);
157 }
158
159 void PSI_ContextBuild(PSI_Context *C, const char *paths)
160 {
161 int i, n;
162 char *sep = NULL, *cpy = strdup(paths), *ptr = cpy;
163 struct dirent **entries = NULL;
164
165 do {
166 sep = strchr(ptr, ':');
167
168 if (sep) {
169 *sep = 0;
170 }
171
172 n = php_scandir(ptr, &entries, psi_select_dirent, alphasort);
173
174 if (n > 0) {
175 for (i = 0; i < n; ++i) {
176 char psi[MAXPATHLEN];
177 PSI_Parser P;
178
179 if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) {
180 C->error(C, NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
181 ptr, entries[i]->d_name);
182 }
183 if (!PSI_ParserInit(&P, psi, C->error, C->flags)) {
184 C->error(C, NULL, PSI_WARNING, "Failed to init PSI parser (%s): %s",
185 psi, strerror(errno));
186 continue;
187 }
188
189 while (0 < PSI_ParserScan(&P)) {
190 PSI_ParserParse(&P, PSI_TokenAlloc(&P));
191 if (P.num == PSI_T_EOF) {
192 break;
193 }
194 }
195
196 PSI_ParserParse(&P, NULL);
197 PSI_ContextValidate(C, &P);
198 PSI_ParserDtor(&P);
199 }
200 }
201
202 if (entries) {
203 for (i = 0; i < n; ++i) {
204 free(entries[i]);
205 }
206 free(entries);
207 }
208
209 ptr = sep + 1;
210 } while (sep);
211
212
213 if (PSI_ContextCompile(C) && SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
214 C->error(C, NULL, PSI_WARNING, "Failed to register functions!");
215 }
216
217 free(cpy);
218
219 }
220
221 zend_function_entry *PSI_ContextCompile(PSI_Context *C)
222 {
223 size_t i;
224 zend_constant zc;
225
226 zc.flags = CONST_PERSISTENT|CONST_CS;
227 zc.module_number = EG(current_module)->module_number;
228
229 if (C->consts) {
230 for (i = 0; i < C->consts->count; ++i) {
231 constant *c = C->consts->list[i];
232
233 zc.name = zend_string_init(c->name + (c->name[0] == '\\'), strlen(c->name) - (c->name[0] == '\\'), 1);
234 ZVAL_NEW_STR(&zc.value, zend_string_init(c->val->text, strlen(c->val->text), 1));
235
236 switch (c->type->type) {
237 case PSI_T_BOOL:
238 convert_to_boolean(&zc.value);
239 break;
240 case PSI_T_INT:
241 convert_to_long(&zc.value);
242 break;
243 case PSI_T_FLOAT:
244 convert_to_double(&zc.value);
245 break;
246 }
247 zend_register_constant(&zc);
248 }
249 }
250 if (C->enums) {
251 for (i = 0; i < C->enums->count; ++i) {
252 decl_enum *e = C->enums->list[i];
253 size_t j;
254
255 for (j = 0; j < e->items->count; ++j) {
256 decl_enum_item *i = e->items->list[j];
257 zend_string *name = strpprintf(0, "psi\\%s\\%s", e->name, i->name);
258
259 zc.name = zend_string_dup(name, 1);
260 ZVAL_LONG(&zc.value, psi_long_num_exp(i->num, NULL));
261 zend_register_constant(&zc);
262 zend_string_release(name);
263 }
264 }
265 }
266
267 return C->closures = C->ops->compile(C);
268 }
269
270
271 void PSI_ContextCall(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va)
272 {
273 C->ops->call(C, decl_call, va);
274 }
275
276
277 void PSI_ContextDtor(PSI_Context *C)
278 {
279 size_t i;
280 zend_function_entry *zfe;
281
282 if (C->ops->dtor) {
283 C->ops->dtor(C);
284 }
285
286 free_decl_libs(&C->psi.libs);
287
288 if (C->data) {
289 for (i = 0; i < C->count; ++i) {
290 PSI_DataDtor(&C->data[i]);
291 }
292 free(C->data);
293 }
294
295 if (C->closures) {
296 for (zfe = C->closures; zfe->fname; ++zfe) {
297 free((void *) zfe->arg_info);
298 }
299 free(C->closures);
300 }
301
302 if (C->consts) {
303 if (C->consts->list) {
304 free(C->consts->list);
305 }
306 free(C->consts);
307 }
308 if (C->defs) {
309 if (C->defs->list) {
310 free(C->defs->list);
311 }
312 free(C->defs);
313 }
314 if (C->structs) {
315 if (C->structs->list) {
316 free(C->structs->list);
317 }
318 free(C->structs);
319 }
320 if (C->enums) {
321 if (C->enums->list) {
322 free(C->enums->list);
323 }
324 free(C->enums);
325 }
326 if (C->decls) {
327 if (C->decls->list) {
328 free(C->decls->list);
329 }
330 free(C->decls);
331 }
332 if (C->impls) {
333 if (C->impls->list) {
334 free(C->impls->list);
335 }
336 free(C->impls);
337 }
338
339 memset(C, 0, sizeof(*C));
340 }
341
342 void PSI_ContextFree(PSI_Context **C)
343 {
344 if (*C) {
345 PSI_ContextDtor(*C);
346 free(*C);
347 *C = NULL;
348 }
349 }