pe*alloc
[m6w6/ext-psi] / src / types / impl_func.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
29 struct psi_impl_func *psi_impl_func_init(zend_string *name,
30 struct psi_plist *args, struct psi_impl_type *type)
31 {
32 struct psi_impl_func *func = pecalloc(1, sizeof(*func), 1);
33
34 func->name = zend_string_copy(name);
35 func->args = args ? : psi_plist_init((psi_plist_dtor) psi_impl_arg_free);
36
37 func->return_type = type;
38
39 return func;
40 }
41
42 void psi_impl_func_free(struct psi_impl_func **f_ptr)
43 {
44 if (*f_ptr) {
45 struct psi_impl_func *f = *f_ptr;
46
47 *f_ptr = NULL;
48 psi_token_free(&f->token);
49
50 psi_impl_type_free(&f->return_type);
51 psi_plist_free(f->args);
52
53 if (f->vararg) {
54 psi_impl_arg_free(&f->vararg);
55 }
56
57 zend_string_release(f->name);
58 free(f);
59 }
60 }
61
62 bool psi_impl_func_validate(struct psi_data *data, struct psi_impl_func *func,
63 struct psi_validate_scope *scope)
64 {
65 int def = 0;
66 size_t i = 0;
67 struct psi_impl_arg *iarg;
68
69 while (psi_plist_get(func->args, i++, &iarg)) {
70 if (iarg->def) {
71 def = 1;
72 if (!psi_impl_def_val_validate(data, iarg->def, iarg->type, scope)) {
73 return 0;
74 }
75 } else if (def) {
76 data->error(data, func->token, PSI_WARNING,
77 "Non-optional argument %zu '$%s' of implementation '%s'"
78 " follows optional argument",
79 i + 1,
80 iarg->var->name->val, func->name->val);
81 return false;
82 }
83 }
84
85 return true;
86 }
87
88 void psi_impl_func_dump(int fd, struct psi_impl_func *func)
89 {
90 dprintf(fd, "function %s(", func->name->val);
91 if (func->args) {
92 size_t i = 0;
93 struct psi_impl_arg *iarg;
94
95 while (psi_plist_get(func->args, i++, &iarg)) {
96 if (i > 1) {
97 dprintf(fd, ", ");
98 }
99 psi_impl_arg_dump(fd, iarg, false);
100 }
101
102 if (func->vararg) {
103 dprintf(fd, ", ");
104 psi_impl_arg_dump(fd, func->vararg, true);
105 }
106 }
107 dprintf(fd, ") : %s%s", func->return_reference ? "&" : "",
108 func->return_type->name->val);
109 }