raising the head after a three-weeks refactoring
[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(const char *func, struct psi_plist *vars)
31 {
32 struct psi_free_exp *f = calloc(1, sizeof(*f));
33 f->func = strdup(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 if (f->token) {
45 free(f->token);
46 }
47 free(f->func);
48 psi_plist_free(f->vars);
49 if (f->let) {
50 free(f->let);
51 }
52 free(f);
53 }
54 }
55
56 void psi_free_exp_dump(int fd, struct psi_free_exp *call)
57 {
58 size_t l = 0, c = psi_plist_count(call->vars);
59 struct psi_decl_var *fvar;
60
61 dprintf(fd, "%s(", call->func);
62 while (psi_plist_get(call->vars, l++, &fvar)) {
63 psi_decl_var_dump(fd, fvar);
64 if (l < c) {
65 dprintf(fd, ", ");
66 }
67 }
68 dprintf(fd, ")");
69 }
70
71 static inline struct psi_decl *locate_free_decl(struct psi_plist *decls,
72 struct psi_free_exp *f)
73 {
74 if (decls) {
75 size_t i = 0;
76 struct psi_decl *decl;
77
78 while (psi_plist_get(decls, i++, &decl)) {
79 if (!strcmp(decl->func->var->name, f->func)) {
80 return f->decl = decl;
81 }
82 }
83 }
84
85 return NULL;
86 }
87
88 bool psi_free_exp_validate(struct psi_data *data, struct psi_free_exp *exp,
89 struct psi_impl *impl)
90 {
91 size_t i;
92 struct psi_decl_var *free_var;
93
94 /* first find the decl of the free func */
95 if (!locate_free_decl(data->decls, exp)) {
96 data->error(data, exp->token, PSI_WARNING,
97 "Missing declaration '%s' in `free` statement"
98 " of implementation '%s'", exp->func, impl->func->name);
99 return false;
100 }
101
102 /* now check for known vars */
103 exp->let = calloc(psi_plist_count(exp->vars), sizeof(*exp->let));
104 for (i = 0; psi_plist_get(exp->vars, i, &free_var); ++i) {
105 if (!psi_decl_arg_get_by_var(free_var, impl->decl->args,
106 impl->decl->func)) {
107 data->error(data, free_var->token, PSI_WARNING,
108 "Unknown variable '%s' of `free` statement"
109 " of implementation '%s'",
110 free_var->name, impl->func->name);
111 return false;
112 }
113
114 exp->let[i] = psi_impl_get_let(impl, free_var);
115 assert(exp->let[i]);
116 }
117
118 return true;
119 }
120
121 void psi_free_exp_exec(struct psi_free_exp *f, struct psi_call_frame *frame)
122 {
123 size_t i;
124 void **args;
125 struct psi_decl_var *dvar;
126 struct psi_call_frame *free_call;
127
128 free_call = psi_call_frame_init(frame->context, f->decl, NULL);
129 psi_call_frame_enter(free_call);
130
131 args = psi_call_frame_get_arg_pointers(free_call);
132 for (i = 0; psi_plist_get(f->vars, i, &dvar); ++i) {
133 struct psi_call_frame_symbol *frame_sym;
134 struct psi_let_exp *let = f->let[i]->exp;
135
136 frame_sym = psi_call_frame_fetch_symbol(frame, let->var);
137 args[i] = deref_impl_val(frame_sym->ptr, dvar);
138 }
139
140 psi_call_frame_do_call(free_call);
141 psi_call_frame_free(free_call);
142 }