travis: update
[m6w6/ext-psi] / src / types / let_callback.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
33 struct psi_let_callback *psi_let_callback_init(struct psi_let_func *func,
34 struct psi_plist *args, struct psi_plist *cb_args)
35 {
36 struct psi_let_callback *cb = pecalloc(1, sizeof(*cb), 1);
37 cb->func = func;
38 cb->args = args;
39 cb->cb_args = cb_args;
40 return cb;
41 }
42
43 void psi_let_callback_free(struct psi_let_callback **cb_ptr)
44 {
45 if (*cb_ptr) {
46 struct psi_let_callback *cb = *cb_ptr;
47
48 *cb_ptr = NULL;
49 psi_let_func_free(&cb->func);
50 psi_plist_free(cb->args);
51 if (cb->cb_args) {
52 psi_plist_free(cb->cb_args);
53 }
54 psi_token_free(&cb->token);
55 free(cb);
56 }
57 }
58
59
60 static inline bool psi_let_callback_validate_decl_args(struct psi_data *data,
61 struct psi_let_callback *cb, struct psi_decl *cb_decl,
62 struct psi_impl *impl)
63 {
64 size_t i;
65 struct psi_decl_var *call_arg;
66
67 if (cb->cb_args) {
68 if (psi_plist_count(cb->cb_args) != psi_plist_count(cb_decl->args)) {
69 data->error(data, cb->token, PSI_WARNING,
70 "Argument count of callback statement of implementation '%s'"
71 "does not match argument count of callback declaration '%s'",
72 impl->func->name->val, cb_decl->func->var->name->val);
73 return false;
74 }
75
76 for (i = 0; psi_plist_get(cb->cb_args, i, &call_arg); ++i) {
77 psi_plist_get(cb_decl->args, i, &call_arg->arg);
78 }
79 }
80 return true;
81 }
82
83 bool psi_let_callback_validate(struct psi_data *data, struct psi_let_callback *cb,
84 struct psi_validate_scope *scope)
85 {
86 size_t i = 0;
87 struct psi_decl_type *cb_type;
88 struct psi_let_exp *exp = scope->current_let;
89 struct psi_decl_var *cb_var = exp->var;
90 struct psi_set_exp *set_exp, *parent_set = scope->current_set;
91
92 cb_type = psi_decl_type_get_real(cb_var->arg->type);
93 if (cb_type->type != PSI_T_FUNCTION) {
94 data->error(data, cb_var->token, PSI_WARNING,
95 "Expected a function: %s",
96 cb_var->name->val);
97 return false;
98 }
99 cb->decl = cb_type->real.func;
100
101 if (!psi_decl_validate_nodl(data, cb->decl, scope)) {
102 return false;
103 }
104 if (!psi_let_callback_validate_decl_args(data, cb, cb->decl, scope->impl)) {
105 return false;
106 }
107 while (psi_plist_get(cb->args, i++, &set_exp)) {
108 if (cb->cb_args) {
109 struct psi_decl_var *cb_var;
110
111 if (psi_plist_get(cb->cb_args, i - 1, &cb_var)) {
112 struct psi_decl_var *dvar = psi_set_exp_get_decl_var(set_exp);
113 dvar->arg = cb_var->arg;
114 }
115 }
116
117 scope->current_set = set_exp;
118 scope->cb_decl = cb->decl;
119
120 if (!psi_set_exp_validate(data, set_exp, scope)) {
121 scope->cb_decl = NULL;
122 scope->current_set = parent_set;
123 return false;
124 }
125 scope->cb_decl = NULL;
126 scope->current_set = parent_set;
127 }
128
129 return true;
130 }
131
132 void psi_let_callback_dump(struct psi_dump *dump, struct psi_let_callback *callback,
133 unsigned level)
134 {
135 PSI_DUMP(dump, "callback(");
136 if (callback->cb_args) {
137 size_t i = 0;
138 struct psi_decl_var *cb_arg;
139
140 while (psi_plist_get(callback->cb_args, i++, &cb_arg)) {
141 if (i > 1) {
142 PSI_DUMP(dump, ", ");
143 }
144 psi_decl_var_dump(dump, cb_arg);
145 }
146 }
147 PSI_DUMP(dump, ") as %s(%s(",
148 callback->func->name->val,
149 callback->func->var->name->val);
150
151 if (callback->args) {
152 size_t i = 0, last = psi_plist_count(callback->args);
153 struct psi_set_exp *set;
154
155 PSI_DUMP(dump, "\n");
156 ++level;
157 while (psi_plist_get(callback->args, i++, &set)) {
158 psi_set_exp_dump(dump, set, level, i == last);
159 PSI_DUMP(dump, "\n");
160 }
161 --level;
162 PSI_DUMP(dump, "%s", psi_t_indent(level));
163 }
164 PSI_DUMP(dump, "))");
165 }