pe*alloc
[m6w6/ext-psi] / src / types / return_stmt.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_return_stmt *psi_return_stmt_init(struct psi_return_exp *exp)
31 {
32 struct psi_return_stmt *ret = pecalloc(1, sizeof(*ret), 1);
33 ret->exp = exp;
34 return ret;
35 }
36
37 void psi_return_stmt_exec(struct psi_return_stmt *ret, zval *return_value,
38 struct psi_call_frame *frame)
39 {
40 psi_return_exp_exec(ret->exp, return_value, frame);
41 }
42
43 void psi_return_stmt_free(struct psi_return_stmt **ret_ptr)
44 {
45 if (*ret_ptr) {
46 struct psi_return_stmt *ret = *ret_ptr;
47
48 *ret_ptr = NULL;
49 psi_token_free(&ret->token);
50 psi_return_exp_free(&ret->exp);
51 free(ret);
52 }
53 }
54
55 void psi_return_stmt_dump(int fd, struct psi_return_stmt *ret)
56 {
57 dprintf(fd, "\treturn ");
58 psi_return_exp_dump(fd, ret->exp);
59 dprintf(fd, ";\n");
60 }
61
62 bool psi_return_stmt_validate(struct psi_data *data,
63 struct psi_validate_scope *scope)
64 {
65 struct psi_return_stmt *ret;
66 size_t count = psi_plist_count(scope->impl->stmts.ret);
67
68 psi_plist_get(scope->impl->stmts.ret, 0, &ret);
69
70 /*
71 * we must have exactly one ret stmt declaring the native func to call
72 * and which type cast to apply
73 */
74 switch (count) {
75 default:
76 data->error(data, ret->token, PSI_WARNING,
77 "Too many `return` statements for implementation %s;"
78 " found %zu, exactly one is required",
79 scope->impl->func->name->val, count);
80 return false;
81 case 0:
82 data->error(data, scope->impl->func->token, PSI_WARNING,
83 "Missing `return` statement for implementation %s",
84 scope->impl->func->name->val);
85 return false;
86 case 1:
87 break;
88 }
89
90 if (!psi_return_exp_validate(data, ret->exp, scope)) {
91 return false;
92 }
93
94 return true;
95 }