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