don't register constants with its anon prefix
[m6w6/ext-psi] / src / context.c
index b98ecc2e4940cab596d9c5ce1632d804abda114a..6408ebeec29f374138237de87301bf4292110d38 100644 (file)
@@ -97,10 +97,25 @@ struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_o
                T.types = psi_plist_add(T.types, &def);
        }
        for (predef_const = &psi_predef_consts[0]; predef_const->type_tag; ++predef_const) {
-               struct psi_impl_def_val *val = psi_impl_def_val_init(predef_const->val_type_tag, predef_const->val_text);
                struct psi_const_type *type = psi_const_type_init(predef_const->type_tag, predef_const->type_name);
-               struct psi_const *constant = psi_const_init(type, predef_const->var_name, val);
+               struct psi_impl_def_val *val;
+               struct psi_const *constant;
 
+               switch (type->type) {
+               case PSI_T_INT:
+                       val = psi_impl_def_val_init(PSI_T_INT, NULL);
+                       val->ival.zend.lval = predef_const->value.lval;
+                       break;
+               case PSI_T_STRING:
+                       val = psi_impl_def_val_init(PSI_T_STRING, NULL);
+                       val->ival.zend.str = zend_string_init(predef_const->value.ptr, strlen(predef_const->value.ptr), 1);
+                       break;
+               default:
+                       assert(0);
+                       break;
+               }
+
+               constant = psi_const_init(type, predef_const->var_name, val);
                T.consts = psi_plist_add(T.consts, &constant);
        }
        for (predef_composite = &psi_predef_composites[0]; predef_composite->type_tag; ++predef_composite) {
@@ -131,7 +146,7 @@ struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_o
                        type = psi_decl_type_init(member->type_tag, member->type_name);
                        dvar = psi_decl_var_init(member->var_name, member->pointer_level, member->array_size);
                        darg = psi_decl_arg_init(type, dvar);
-                       darg->layout = psi_layout_init(member->offset, member->size);
+                       darg->layout = psi_layout_init(member->offset, member->size, NULL);
 
                        switch (predef_composite->type_tag) {
                        case PSI_T_STRUCT:
@@ -163,7 +178,7 @@ struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_o
                struct psi_decl_var *fname = psi_decl_var_init(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
                struct psi_decl_arg *tdef, *func = psi_decl_arg_init(ftype, fname);
                struct psi_plist *args = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
-               struct psi_decl *decl = psi_decl_init(psi_decl_abi_init("default"), func, args);
+               struct psi_decl *decl = psi_decl_init(func, args);
 
                for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
                        struct psi_decl_type *arg_type = psi_decl_type_init(farg->type_tag, farg->type_name);
@@ -225,6 +240,7 @@ void psi_context_build(struct psi_context *C, const char *paths)
                        for (i = 0; i < n; ++i) {
                                char psi[MAXPATHLEN];
                                struct psi_parser P;
+                               struct psi_parser_input *I;
 
                                if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) {
                                        C->error(PSI_DATA(C), NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
@@ -235,22 +251,15 @@ void psi_context_build(struct psi_context *C, const char *paths)
                                                psi, strerror(errno));
                                        continue;
                                }
-                               if (!psi_parser_open_file(&P, psi)) {
+                               if (!(I = psi_parser_open_file(&P, psi, true))) {
                                        C->error(PSI_DATA(C), NULL, PSI_WARNING, "Failed to open PSI file (%s): %s",
                                                psi, strerror(errno));
                                        continue;
                                }
-
-                               while (0 < psi_parser_scan(&P)) {
-                                       psi_parser_parse(&P, psi_token_alloc(&P));
-                                       if (P.num == PSI_T_EOF) {
-                                               break;
-                                       }
-                               }
-
-                               psi_parser_parse(&P, NULL);
+                               psi_parser_parse(&P, I);
                                psi_context_add_data(C, PSI_DATA(&P));
                                psi_parser_dtor(&P);
+                               free(I);
                        }
                }
 
@@ -284,34 +293,36 @@ zend_function_entry *psi_context_compile(struct psi_context *C)
                struct psi_const *c;
 
                while (psi_plist_get(C->consts, i++, &c)) {
-                       zc.name = zend_string_init(c->name + (c->name[0] == '\\'), strlen(c->name) - (c->name[0] == '\\'), 1);
 
-                       if (zend_get_constant(zc.name)) {
-                               zend_string_release(zc.name);
+                       if (zend_get_constant_str(c->name, strlen(c->name))) {
                                continue;
                        }
 
-                       ZVAL_NEW_STR(&zc.value, zend_string_init(c->val->text, strlen(c->val->text), 1));
+                       zc.name = zend_string_init(c->name, strlen(c->name), 1);
 
                        switch (c->type->type) {
                        case PSI_T_BOOL:
-                               convert_to_boolean(&zc.value);
+                               ZVAL_BOOL(&zc.value, c->val->ival.zend.bval);
                                break;
                        case PSI_T_INT:
-                               convert_to_long(&zc.value);
+                               ZVAL_LONG(&zc.value, c->val->ival.zend.lval);
                                break;
                        case PSI_T_FLOAT:
-                               convert_to_double(&zc.value);
+                               ZVAL_DOUBLE(&zc.value, c->val->ival.dval);
                                break;
                        case PSI_T_STRING:
                        case PSI_T_QUOTED_STRING:
+                               ZVAL_NEW_STR(&zc.value, zend_string_copy(c->val->ival.zend.str));
                                break;
                        default:
                                assert(0);
+                               break;
                        }
+
                        zend_register_constant(&zc);
                }
        }
+
        if (C->enums) {
                size_t i = 0;
                struct psi_decl_enum *e;
@@ -321,10 +332,16 @@ zend_function_entry *psi_context_compile(struct psi_context *C)
                        struct psi_decl_enum_item *item;
 
                        while (psi_plist_get(e->items, j++, &item)) {
-                               zend_string *name = strpprintf(0, "psi\\%s\\%s", e->name, item->name);
+                               zend_string *name;
+
+                               if (psi_decl_type_is_anon(e->name, "enum")) {
+                                       name = strpprintf(0, "psi\\%s", item->name);
+                               } else {
+                                       name = strpprintf(0, "psi\\%s\\%s", e->name, item->name);
+                               }
 
                                zc.name = zend_string_dup(name, 1);
-                               ZVAL_LONG(&zc.value, psi_long_num_exp(item->num, NULL));
+                               ZVAL_LONG(&zc.value, psi_long_num_exp(item->num, NULL, NULL));
                                zend_register_constant(&zc);
                                zend_string_release(name);
                        }
@@ -356,7 +373,22 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec
                return FAILURE;
        }
 
+       if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_PRE)) {
+               psi_call_frame_do_return(frame, return_value);
+               psi_call_frame_free(frame);
+
+               return FAILURE;
+       }
+
        psi_call_frame_do_call(frame);
+
+       if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) {
+               psi_call_frame_do_return(frame, return_value);
+               psi_call_frame_free(frame);
+
+               return FAILURE;
+       }
+
        psi_call_frame_do_return(frame, return_value);
        psi_call_frame_do_set(frame);
        psi_call_frame_do_free(frame);