types refactoring
[m6w6/ext-psi] / src / types / free_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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #include "data.h"
36
37 free_stmt *init_free_stmt(free_calls *calls) {
38 free_stmt *f = calloc(1, sizeof(*f));
39 f->calls = calls;
40 return f;
41 }
42
43 void free_free_stmt(free_stmt *f) {
44 free_free_calls(f->calls);
45 free(f);
46 }
47
48 void dump_free_stmt(int fd, free_stmt *fre) {
49 size_t k;
50
51 dprintf(fd, "\tfree ");
52 for (k = 0; k < fre->calls->count; ++k) {
53 free_call *call = fre->calls->list[k];
54
55 if (k) {
56 dprintf(fd, ", ");
57 }
58 dump_free_call(fd, call);
59 dprintf(fd, "\n");
60 }
61 }
62
63 static inline decl *locate_free_decl(decls *decls, free_call *f) {
64 if (decls) {
65 size_t i;
66
67 for (i = 0; i < decls->count; ++i) {
68 if (!strcmp(decls->list[i]->func->var->name, f->func)) {
69 f->decl = decls->list[i];
70 return decls->list[i];
71 }
72 }
73 }
74
75 return NULL;
76 }
77
78 int validate_free_stmts(struct psi_data *data, impl *impl) {
79 size_t i, j, k, l;
80 /* we can have any count of free stmts; freeing any out vars */
81 for (i = 0; i < impl->stmts->fre.count; ++i) {
82 free_stmt *fre = impl->stmts->fre.list[i];
83
84 for (j = 0; j < fre->calls->count; ++j) {
85 free_call *free_call = fre->calls->list[j];
86
87 /* first find the decl of the free func */
88 if (!locate_free_decl(data->decls, free_call)) {
89 data->error(data, free_call->token, PSI_WARNING,
90 "Missing declaration '%s' in `free` statement"
91 " of implementation '%s'",
92 free_call->func, impl->func->name);
93 return 0;
94 }
95
96
97
98 /* now check for known vars */
99 for (l = 0; l < free_call->vars->count; ++l) {
100 int check = 0;
101 decl_var *free_var = free_call->vars->vars[l];
102
103 if (!strcmp(free_var->name, impl->decl->func->var->name)) {
104 check = 1;
105 free_var->arg = impl->decl->func;
106 } else if (impl->decl->args) {
107 for (k = 0; k < impl->decl->args->count; ++k) {
108 decl_arg *free_arg = impl->decl->args->args[k];
109
110 if (!strcmp(free_var->name, free_arg->var->name)) {
111 check = 1;
112 free_var->arg = free_arg;
113 break;
114 }
115 }
116 }
117
118 if (!check) {
119 data->error(data, free_var->token, PSI_WARNING,
120 "Unknown variable '%s' of `free` statement"
121 " of implementation '%s'",
122 free_var->name, impl->func->name);
123 return 0;
124 }
125 }
126 }
127 }
128 return 1;
129 }