Merge branch 'slimconfigure'
[m6w6/ext-psi] / src / types / return_exp.c
1 /*******************************************************************************
2 Copyright (c) 2017, 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_return_exp *psi_return_exp_init(struct psi_decl_var *func,
31 struct psi_plist *args, struct psi_set_exp *set)
32 {
33 struct psi_return_exp *exp = calloc(1, sizeof(*exp));
34
35 exp->func = func;
36 exp->args = args;
37 exp->set = set;
38
39 return exp;
40 }
41
42 void psi_return_exp_free(struct psi_return_exp **exp_ptr)
43 {
44 if (*exp_ptr) {
45 struct psi_return_exp *exp = *exp_ptr;
46
47 *exp_ptr = NULL;
48 if (exp->token) {
49 psi_token_free(&exp->token);
50 }
51 if (exp->func) {
52 psi_decl_var_free(&exp->func);
53 }
54 if (exp->args) {
55 psi_plist_free(exp->args);
56 exp->args = NULL;
57 }
58 if (exp->set) {
59 psi_set_exp_free(&exp->set);
60 }
61 free(exp);
62 }
63 }
64
65 void psi_return_exp_dump(int fd, struct psi_return_exp *exp)
66 {
67 if (exp->func) {
68 psi_decl_var_dump(fd, exp->func);
69 dprintf(fd, "(");
70 if (exp->args) {
71 size_t i = 0;
72 struct psi_decl_var *arg;
73
74 while (psi_plist_get(exp->args, i++, &arg)) {
75 if (i > 1) {
76 dprintf(fd, ", ");
77 }
78 psi_decl_var_dump(fd, arg);
79 }
80 }
81 dprintf(fd, ")");
82 }
83 if (exp->set) {
84 if (exp->func) {
85 dprintf(fd, " as ");
86 }
87
88 psi_set_exp_dump(fd, exp->set, 1, 1);
89 }
90 }
91
92 void psi_return_exp_exec(struct psi_return_exp *exp, zval *return_value,
93 struct psi_call_frame *frame)
94 {
95 if (exp->set) {
96 void *rpointer = psi_call_frame_get_rpointer(frame);
97
98 psi_set_exp_exec_ex(exp->set, return_value, rpointer, frame);
99 }
100 }
101
102 static inline bool psi_return_exp_validate_decl_args(struct psi_data *data,
103 struct psi_return_exp *exp, struct psi_impl *impl)
104 {
105 size_t i;
106 struct psi_decl_var *call_arg;
107
108 if (exp->args) {
109 if (psi_plist_count(exp->args) != psi_plist_count(impl->decl->args)) {
110 data->error(data, exp->token, PSI_WARNING,
111 "Argument count of return statement of implementation '%s'"
112 "does not match argument count of declaration '%s'",
113 impl->func->name, impl->decl->func->var->name);
114 return false;
115 }
116
117 for (i = 0; psi_plist_get(exp->args, i, &call_arg); ++i) {
118 psi_plist_get(impl->decl->args, i, &call_arg->arg);
119 }
120 }
121 return true;
122 }
123
124 bool psi_return_exp_validate(struct psi_data *data, struct psi_return_exp *exp,
125 struct psi_validate_scope *scope)
126 {
127 size_t i = 0;
128 struct psi_decl *decl;
129 const char *name = psi_return_exp_get_decl_name(exp);
130
131
132
133 while (psi_plist_get(data->decls, i++, &decl)) {
134 if (!strcmp(decl->func->var->name, name)) {
135 scope->impl->decl = decl;
136 if (psi_return_exp_validate_decl_args(data, exp, scope->impl)) {
137 scope->current_set = exp->set;
138 if (psi_set_exp_validate(data, exp->set, scope)) {
139 scope->current_set = NULL;
140 return true;
141 }
142 scope->current_set = NULL;
143 }
144 return false;
145 }
146 }
147
148 data->error(data, exp->token, PSI_WARNING,
149 "Missing declaration '%s' for `return` statement of implementation %s",
150 name, scope->impl->func->name);
151 return false;
152 }
153
154 const char *psi_return_exp_get_decl_name(struct psi_return_exp *exp)
155 {
156 return exp->func ? exp->func->name : exp->set->data.func->var->name;
157 }
158