commit after reset fuckup
[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 = pecalloc(1, sizeof(*exp), 1);
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 psi_token_free(&exp->token);
49 if (exp->func) {
50 psi_decl_var_free(&exp->func);
51 }
52 if (exp->args) {
53 psi_plist_free(exp->args);
54 exp->args = NULL;
55 }
56 if (exp->set) {
57 psi_set_exp_free(&exp->set);
58 }
59 free(exp);
60 }
61 }
62
63 void psi_return_exp_dump(struct psi_dump *dump, struct psi_return_exp *exp)
64 {
65 if (exp->func) {
66 psi_decl_var_dump(dump, exp->func);
67 PSI_DUMP(dump, "(");
68 if (exp->args) {
69 size_t i = 0;
70 struct psi_decl_var *arg;
71
72 while (psi_plist_get(exp->args, i++, &arg)) {
73 if (i > 1) {
74 PSI_DUMP(dump, ", ");
75 }
76 psi_decl_var_dump(dump, arg);
77 }
78 }
79 PSI_DUMP(dump, ")");
80 }
81 if (exp->set) {
82 if (exp->func) {
83 PSI_DUMP(dump, " as ");
84 }
85
86 psi_set_exp_dump(dump, exp->set, 1, 1);
87 }
88 }
89
90 void psi_return_exp_exec(struct psi_return_exp *exp, zval *return_value,
91 struct psi_call_frame *frame)
92 {
93 if (exp->set) {
94 void *rpointer = psi_call_frame_get_rpointer(frame);
95
96 psi_set_exp_exec_ex(exp->set, return_value, rpointer, frame);
97 }
98 }
99
100 static inline bool psi_return_exp_validate_decl_args(struct psi_data *data,
101 struct psi_return_exp *exp, struct psi_impl *impl)
102 {
103 size_t i;
104 struct psi_decl_var *call_arg;
105
106 if (exp->args) {
107 if (psi_plist_count(exp->args) != psi_plist_count(impl->decl->args)) {
108 data->error(data, exp->token, PSI_WARNING,
109 "Argument count of return statement of implementation '%s' "
110 "does not match argument count of declaration '%s'",
111 impl->func->name->val, impl->decl->func->var->name->val);
112 return false;
113 }
114
115 for (i = 0; psi_plist_get(exp->args, i, &call_arg); ++i) {
116 psi_plist_get(impl->decl->args, i, &call_arg->arg);
117 }
118 }
119 return true;
120 }
121
122 bool psi_return_exp_validate(struct psi_data *data, struct psi_return_exp *exp,
123 struct psi_validate_scope *scope)
124 {
125 size_t i = 0;
126 struct psi_decl *decl;
127 zend_string *name = psi_return_exp_get_decl_name(exp);
128
129
130 while (psi_plist_get(data->decls, i++, &decl)) {
131 if (zend_string_equals(decl->func->var->name, name)) {
132 scope->impl->decl = decl;
133 if (psi_return_exp_validate_decl_args(data, exp, scope->impl)) {
134 scope->current_set = exp->set;
135 if (psi_set_exp_validate(data, exp->set, scope)) {
136 scope->current_set = NULL;
137 return true;
138 }
139 scope->current_set = NULL;
140 }
141 return false;
142 }
143 }
144
145 data->error(data, exp->token, PSI_WARNING,
146 "Missing declaration '%s' for `return` statement of implementation %s",
147 name->val, scope->impl->func->name->val);
148 return false;
149 }
150
151 zend_string *psi_return_exp_get_decl_name(struct psi_return_exp *exp)
152 {
153 return exp->func ? exp->func->name : exp->set->data.func->var->name;
154 }
155