fix gdbinit; postprocessing macros
[m6w6/ext-psi] / src / types / free_exp.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 #include "call.h"
29
30 struct psi_free_exp *psi_free_exp_init(zend_string *func, struct psi_plist *vars)
31 {
32 struct psi_free_exp *f = pecalloc(1, sizeof(*f), 1);
33 f->func = zend_string_copy(func);
34 f->vars = vars;
35 return f;
36 }
37
38 void psi_free_exp_free(struct psi_free_exp **f_ptr)
39 {
40 if (*f_ptr) {
41 struct psi_free_exp *f = *f_ptr;
42
43 *f_ptr = NULL;
44 psi_token_free(&f->token);
45 zend_string_release(f->func);
46 psi_plist_free(f->vars);
47 if (f->let) {
48 free(f->let);
49 }
50 free(f);
51 }
52 }
53
54 void psi_free_exp_dump(struct psi_dump *dump, struct psi_free_exp *call)
55 {
56 size_t l = 0, c = psi_plist_count(call->vars);
57 struct psi_decl_var *fvar;
58
59 PSI_DUMP(dump, "%s(", call->func->val);
60 while (psi_plist_get(call->vars, l++, &fvar)) {
61 psi_decl_var_dump(dump, fvar);
62 if (l < c) {
63 PSI_DUMP(dump, ", ");
64 }
65 }
66 PSI_DUMP(dump, ")");
67 }
68
69 static inline struct psi_decl *locate_free_decl(struct psi_plist *decls,
70 struct psi_free_exp *f)
71 {
72 if (decls) {
73 size_t i = 0;
74 struct psi_decl *decl;
75
76 while (psi_plist_get(decls, i++, &decl)) {
77 if (zend_string_equals(decl->func->var->name, f->func)) {
78 return f->decl = decl;
79 }
80 }
81 }
82
83 return NULL;
84 }
85
86 bool psi_free_exp_validate(struct psi_data *data, struct psi_free_exp *exp,
87 struct psi_validate_scope *scope)
88 {
89 size_t i;
90 struct psi_decl_var *free_var;
91
92 /* first find the decl of the free func */
93 if (!locate_free_decl(data->decls, exp)) {
94 data->error(data, exp->token, PSI_WARNING,
95 "Missing declaration '%s' in `free` statement"
96 " of implementation '%s'", exp->func->val,
97 scope->impl->func->name->val);
98 return false;
99 }
100
101 /* now check for known vars */
102 exp->let = pecalloc(psi_plist_count(exp->vars), sizeof(*exp->let), 1);
103 for (i = 0; psi_plist_get(exp->vars, i, &free_var); ++i) {
104 if (!psi_impl_get_decl_arg(scope->impl, free_var)) {
105 data->error(data, free_var->token, PSI_WARNING,
106 "Unknown variable '%s' of `free` statement"
107 " of implementation '%s'",
108 free_var->name->val, scope->impl->func->name->val);
109 return false;
110 }
111
112 exp->let[i] = psi_impl_get_let(scope->impl, free_var);
113 assert(exp->let[i]);
114 }
115
116 return true;
117 }
118
119 void psi_free_exp_exec(struct psi_free_exp *f, struct psi_call_frame *frame)
120 {
121 size_t i;
122 void **args;
123 struct psi_decl_var *dvar;
124 struct psi_call_frame *free_call;
125 struct psi_context *ctx = psi_call_frame_get_context(frame);
126
127 free_call = psi_call_frame_init(ctx, f->decl, NULL);
128 psi_call_frame_enter(free_call);
129
130 args = psi_call_frame_get_arg_pointers(free_call);
131 for (i = 0; psi_plist_get(f->vars, i, &dvar); ++i) {
132 struct psi_call_frame_symbol *frame_sym;
133 struct psi_let_exp *let = f->let[i]->exp;
134
135 frame_sym = psi_call_frame_fetch_symbol(frame, let->var);
136 args[i] = deref_impl_val(frame_sym->ptr, dvar);
137 }
138
139 psi_call_frame_do_call(free_call);
140 psi_call_frame_free(free_call);
141 }