9 # define NAMLEN(dirent) strlen ((dirent)->d_name)
11 # define dirent direct
12 # define NAMLEN(dirent) ((dirent)->d_namlen)
13 # ifdef HAVE_SYS_NDIR_H
14 # include <sys/ndir.h>
16 # ifdef HAVE_SYS_DIR_H
27 #include "php_scandir.h"
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 #include "php_psi_unions.h"
40 PSI_Context
*PSI_ContextInit(PSI_Context
*C
, PSI_ContextOps
*ops
, PSI_ContextErrorFunc error
, unsigned flags
)
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_union
*predef_union
;
47 struct psi_predef_decl
*predef_decl
;
50 C
= malloc(sizeof(*C
));
52 memset(C
, 0, sizeof(*C
));
62 ZEND_ASSERT(ops
->call
!= NULL
);
63 ZEND_ASSERT(ops
->compile
!= NULL
);
65 /* build up predefs in a temporary PSI_Data for validation */
66 memset(&T
, 0, sizeof(T
));
69 for (predef_type
= &psi_predef_types
[0]; predef_type
->type_tag
; ++predef_type
) {
70 decl_type
*type
= init_decl_type(predef_type
->type_tag
, predef_type
->type_name
);
71 decl_var
*var
= init_decl_var(predef_type
->alias
, 0, 0); /* FIXME: indirection */
72 decl_arg
*def
= init_decl_arg(type
, var
);
74 T
.defs
= add_decl_typedef(T
.defs
, def
);
76 for (predef_const
= &psi_predef_consts
[0]; predef_const
->type_tag
; ++predef_const
) {
77 impl_def_val
*val
= init_impl_def_val(predef_const
->val_type_tag
, predef_const
->val_text
);
78 const_type
*type
= init_const_type(predef_const
->type_tag
, predef_const
->type_name
);
79 constant
*constant
= init_constant(type
, predef_const
->var_name
, val
);
81 T
.consts
= add_constant(T
.consts
, constant
);
83 for (predef_struct
= &psi_predef_structs
[0]; predef_struct
->type_tag
; ++predef_struct
) {
84 struct psi_predef_struct
*member
;
85 decl_args
*dargs
= init_decl_args(NULL
);
86 decl_struct
*dstruct
= init_decl_struct(predef_struct
->var_name
, dargs
);
88 dstruct
->size
= predef_struct
->size
;
89 dstruct
->align
= predef_struct
->offset
;
90 for (member
= &predef_struct
[1]; member
->type_tag
; ++member
) {
95 type
= init_decl_type(member
->type_tag
, member
->type_name
);
96 dvar
= init_decl_var(member
->var_name
, member
->pointer_level
, member
->array_size
);
97 darg
= init_decl_arg(type
, dvar
);
98 darg
->layout
= init_decl_struct_layout(member
->offset
, member
->size
);
99 dargs
= add_decl_arg(dargs
, darg
);
102 T
.structs
= add_decl_struct(T
.structs
, dstruct
);
103 predef_struct
= member
;
105 for (predef_union
= &psi_predef_unions
[0]; predef_union
->type_tag
; ++predef_union
) {
106 struct psi_predef_union
*member
;
107 decl_args
*dargs
= init_decl_args(NULL
);
108 decl_union
*dunion
= init_decl_union(predef_union
->var_name
, dargs
);
110 dunion
->size
= predef_union
->size
;
111 dunion
->align
= predef_union
->offset
;
112 for (member
= &predef_union
[1]; member
->type_tag
; ++member
) {
117 type
= init_decl_type(member
->type_tag
, member
->type_name
);
118 dvar
= init_decl_var(member
->var_name
, member
->pointer_level
, member
->array_size
);
119 darg
= init_decl_arg(type
, dvar
);
120 darg
->layout
= init_decl_struct_layout(member
->offset
, member
->size
);
121 dargs
= add_decl_arg(dargs
, darg
);
124 T
.unions
= add_decl_union(T
.unions
, dunion
);
125 predef_union
= member
;
127 for (predef_decl
= &psi_predef_decls
[0]; predef_decl
->type_tag
; ++predef_decl
) {
128 struct psi_predef_decl
*farg
;
129 decl_type
*ftype
= init_decl_type(predef_decl
->type_tag
, predef_decl
->type_name
);
130 decl_var
*fname
= init_decl_var(predef_decl
->var_name
, predef_decl
->pointer_level
, predef_decl
->array_size
);
131 decl_arg
*func
= init_decl_arg(ftype
, fname
);
132 decl_args
*args
= init_decl_args(NULL
);
133 decl
*decl
= init_decl(init_decl_abi("default"), func
, args
);
135 for (farg
= &predef_decl
[1]; farg
->type_tag
; ++farg
) {
136 decl_type
*arg_type
= init_decl_type(farg
->type_tag
, farg
->type_name
);
137 decl_var
*arg_var
= init_decl_var(farg
->var_name
, farg
->pointer_level
, farg
->array_size
);
138 decl_arg
*darg
= init_decl_arg(arg_type
, arg_var
);
139 args
= add_decl_arg(args
, darg
);
142 T
.decls
= add_decl(T
.decls
, decl
);
146 for (predef_decl
= &psi_predef_vararg_decls
[0]; predef_decl
->type_tag
; ++predef_decl
) {
147 struct psi_predef_decl
*farg
;
148 decl_type
*ftype
= init_decl_type(predef_decl
->type_tag
, predef_decl
->type_name
);
149 decl_var
*fname
= init_decl_var(predef_decl
->var_name
, predef_decl
->pointer_level
, predef_decl
->array_size
);
150 decl_arg
*func
= init_decl_arg(ftype
, fname
);
151 decl_args
*args
= init_decl_args(NULL
);
152 decl
*decl
= init_decl(init_decl_abi("default"), func
, args
);
154 for (farg
= &predef_decl
[1]; farg
->type_tag
; ++farg
) {
155 decl_type
*arg_type
= init_decl_type(farg
->type_tag
, farg
->type_name
);
156 decl_var
*arg_var
= init_decl_var(farg
->var_name
, farg
->pointer_level
, farg
->array_size
);
157 decl_arg
*darg
= init_decl_arg(arg_type
, arg_var
);
158 args
= add_decl_arg(args
, darg
);
162 T
.decls
= add_decl(T
.decls
, decl
);
166 PSI_ContextValidateData(PSI_DATA(C
), &T
);
169 C
->data
= malloc(sizeof(*C
->data
));
170 PSI_DataExchange(C
->data
, &T
);
175 static int psi_select_dirent(const struct dirent
*entry
)
178 #define FNM_CASEFOLD 0
180 return 0 == fnmatch("*.psi", entry
->d_name
, FNM_CASEFOLD
);
183 void PSI_ContextBuild(PSI_Context
*C
, const char *paths
)
186 char *sep
= NULL
, *cpy
= strdup(paths
), *ptr
= cpy
;
187 struct dirent
**entries
= NULL
;
190 sep
= strchr(ptr
, ':');
196 n
= php_scandir(ptr
, &entries
, psi_select_dirent
, alphasort
);
199 for (i
= 0; i
< n
; ++i
) {
200 char psi
[MAXPATHLEN
];
203 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", ptr
, entries
[i
]->d_name
)) {
204 C
->error(C
, NULL
, PSI_WARNING
, "Path to PSI file too long: %s/%s",
205 ptr
, entries
[i
]->d_name
);
207 if (!PSI_ParserInit(&P
, psi
, C
->error
, C
->flags
)) {
208 C
->error(C
, NULL
, PSI_WARNING
, "Failed to init PSI parser (%s): %s",
209 psi
, strerror(errno
));
213 while (0 < PSI_ParserScan(&P
)) {
214 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
215 if (P
.num
== PSI_T_EOF
) {
220 PSI_ParserParse(&P
, NULL
);
221 PSI_ContextValidate(C
, &P
);
227 for (i
= 0; i
< n
; ++i
) {
237 if (PSI_ContextCompile(C
) && SUCCESS
!= zend_register_functions(NULL
, C
->closures
, NULL
, MODULE_PERSISTENT
)) {
238 C
->error(C
, NULL
, PSI_WARNING
, "Failed to register functions!");
245 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
)
250 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
251 zc
.module_number
= EG(current_module
)->module_number
;
254 for (i
= 0; i
< C
->consts
->count
; ++i
) {
255 constant
*c
= C
->consts
->list
[i
];
257 zc
.name
= zend_string_init(c
->name
+ (c
->name
[0] == '\\'), strlen(c
->name
) - (c
->name
[0] == '\\'), 1);
258 ZVAL_NEW_STR(&zc
.value
, zend_string_init(c
->val
->text
, strlen(c
->val
->text
), 1));
260 switch (c
->type
->type
) {
262 convert_to_boolean(&zc
.value
);
265 convert_to_long(&zc
.value
);
268 convert_to_double(&zc
.value
);
271 zend_register_constant(&zc
);
275 for (i
= 0; i
< C
->enums
->count
; ++i
) {
276 decl_enum
*e
= C
->enums
->list
[i
];
279 for (j
= 0; j
< e
->items
->count
; ++j
) {
280 decl_enum_item
*i
= e
->items
->list
[j
];
281 zend_string
*name
= strpprintf(0, "psi\\%s\\%s", e
->name
, i
->name
);
283 zc
.name
= zend_string_dup(name
, 1);
284 ZVAL_LONG(&zc
.value
, psi_long_num_exp(i
->num
, NULL
));
285 zend_register_constant(&zc
);
286 zend_string_release(name
);
291 return C
->closures
= C
->ops
->compile(C
);
295 void PSI_ContextCall(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
)
297 C
->ops
->call(C
, decl_call
, va
);
301 void PSI_ContextDtor(PSI_Context
*C
)
304 zend_function_entry
*zfe
;
310 free_decl_libs(&C
->psi
.libs
);
313 for (i
= 0; i
< C
->count
; ++i
) {
314 PSI_DataDtor(&C
->data
[i
]);
320 for (zfe
= C
->closures
; zfe
->fname
; ++zfe
) {
321 free((void *) zfe
->arg_info
);
327 if (C
->consts
->list
) {
328 free(C
->consts
->list
);
339 if (C
->structs
->list
) {
340 free(C
->structs
->list
);
345 if (C
->enums
->list
) {
346 free(C
->enums
->list
);
351 if (C
->decls
->list
) {
352 free(C
->decls
->list
);
357 if (C
->impls
->list
) {
358 free(C
->impls
->list
);
363 memset(C
, 0, sizeof(*C
));
366 void PSI_ContextFree(PSI_Context
**C
)