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
;
190 sep
= strchr(ptr
, ':');
197 n
= php_scandir(ptr
, &entries
, psi_select_dirent
, alphasort
);
200 for (i
= 0; i
< n
; ++i
) {
201 char psi
[MAXPATHLEN
];
204 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", ptr
, entries
[i
]->d_name
)) {
205 C
->error(C
, NULL
, PSI_WARNING
, "Path to PSI file too long: %s/%s",
206 ptr
, entries
[i
]->d_name
);
208 if (!PSI_ParserInit(&P
, psi
, C
->error
, C
->flags
)) {
209 C
->error(C
, NULL
, PSI_WARNING
, "Failed to init PSI parser (%s): %s",
210 psi
, strerror(errno
));
214 while (0 < PSI_ParserScan(&P
)) {
215 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
216 if (P
.num
== PSI_T_EOF
) {
221 PSI_ParserParse(&P
, NULL
);
222 PSI_ContextValidate(C
, &P
);
228 for (i
= 0; i
< n
; ++i
) {
238 if (PSI_ContextCompile(C
) && SUCCESS
!= zend_register_functions(NULL
, C
->closures
, NULL
, MODULE_PERSISTENT
)) {
239 C
->error(C
, NULL
, PSI_WARNING
, "Failed to register functions!");
246 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
)
251 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
252 zc
.module_number
= EG(current_module
)->module_number
;
255 for (i
= 0; i
< C
->consts
->count
; ++i
) {
256 constant
*c
= C
->consts
->list
[i
];
258 zc
.name
= zend_string_init(c
->name
+ (c
->name
[0] == '\\'), strlen(c
->name
) - (c
->name
[0] == '\\'), 1);
259 ZVAL_NEW_STR(&zc
.value
, zend_string_init(c
->val
->text
, strlen(c
->val
->text
), 1));
261 switch (c
->type
->type
) {
263 convert_to_boolean(&zc
.value
);
266 convert_to_long(&zc
.value
);
269 convert_to_double(&zc
.value
);
272 zend_register_constant(&zc
);
276 for (i
= 0; i
< C
->enums
->count
; ++i
) {
277 decl_enum
*e
= C
->enums
->list
[i
];
280 for (j
= 0; j
< e
->items
->count
; ++j
) {
281 decl_enum_item
*i
= e
->items
->list
[j
];
282 zend_string
*name
= strpprintf(0, "psi\\%s\\%s", e
->name
, i
->name
);
284 zc
.name
= zend_string_dup(name
, 1);
285 ZVAL_LONG(&zc
.value
, psi_long_num_exp(i
->num
, NULL
));
286 zend_register_constant(&zc
);
287 zend_string_release(name
);
292 return C
->closures
= C
->ops
->compile(C
);
296 void PSI_ContextCall(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
)
298 C
->ops
->call(C
, decl_call
, va
);
302 void PSI_ContextDtor(PSI_Context
*C
)
305 zend_function_entry
*zfe
;
311 free_decl_libs(&C
->psi
.libs
);
314 for (i
= 0; i
< C
->count
; ++i
) {
315 PSI_DataDtor(&C
->data
[i
]);
321 for (zfe
= C
->closures
; zfe
->fname
; ++zfe
) {
322 free((void *) zfe
->arg_info
);
328 if (C
->consts
->list
) {
329 free(C
->consts
->list
);
340 if (C
->structs
->list
) {
341 free(C
->structs
->list
);
346 if (C
->unions
->list
) {
347 free(C
->unions
->list
);
352 if (C
->enums
->list
) {
353 free(C
->enums
->list
);
358 if (C
->decls
->list
) {
359 free(C
->decls
->list
);
364 if (C
->impls
->list
) {
365 free(C
->impls
->list
);
370 memset(C
, 0, sizeof(*C
));
373 void PSI_ContextFree(PSI_Context
**C
)