fix warning
[m6w6/ext-psi] / src / types / decl_arg.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 <assert.h>
28
29 #include "data.h"
30
31 struct psi_decl_arg *psi_decl_arg_init(struct psi_decl_type *type,
32 struct psi_decl_var *var)
33 {
34 struct psi_decl_arg *arg = calloc(1, sizeof(*arg));
35 arg->token = var->token;
36 arg->type = type;
37 arg->var = var;
38 var->arg = arg;
39 return arg;
40 }
41
42 void psi_decl_arg_free(struct psi_decl_arg **arg_ptr)
43 {
44 if (*arg_ptr) {
45 struct psi_decl_arg *arg = *arg_ptr;
46
47 *arg_ptr = NULL;
48 if (arg->token && arg->token != arg->var->token) {
49 free(arg->token);
50 }
51 psi_decl_type_free(&arg->type);
52 psi_decl_var_free(&arg->var);
53 if (arg->layout) {
54 psi_layout_free(&arg->layout);
55 }
56 free(arg);
57 }
58 }
59
60 void psi_decl_arg_dump(int fd, struct psi_decl_arg *arg, unsigned level)
61 {
62 if (arg->type->type == PSI_T_FUNCTION) {
63 psi_decl_type_dump(fd, arg->type->real.func->func->type, level);
64 if (arg->type->real.func->func->type->type == PSI_T_FUNCTION) {
65 dprintf(fd, "(");
66 }
67 dprintf(fd, " %s(*%s)",
68 psi_t_indirection(arg->var->pointer_level - !! arg->var->array_size),
69 arg->var->name);
70 dprintf(fd, "(");
71 if (arg->type->real.func->args) {
72 size_t j = 0;
73 struct psi_decl_arg *farg;
74
75 ++level;
76 while (psi_plist_get(arg->type->real.func->args, j++, &farg)) {
77 if (j > 1) {
78 dprintf(fd, ", ");
79 }
80 psi_decl_arg_dump(fd, farg, level);
81 }
82 --level;
83 if (arg->type->real.func->varargs) {
84 dprintf(fd, ", ...");
85 }
86 }
87 dprintf(fd, ")");
88 if (arg->type->real.func->func->type->type == PSI_T_FUNCTION) {
89 struct psi_decl *decl = arg->type->real.func->func->type->real.func;
90
91 dprintf(fd, "(");
92 if (decl->args) {
93 size_t i;
94 struct psi_decl_arg *arg;
95
96 for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
97 if (i) {
98 dprintf(fd, ", ");
99 }
100 psi_decl_arg_dump(fd, arg, 0);
101 }
102 if (decl->varargs) {
103 dprintf(fd, ", ...");
104 }
105 }
106 dprintf(fd, "))");
107 }
108 } else {
109 psi_decl_type_dump(fd, arg->type, level);
110 dprintf(fd, " ");
111 psi_decl_var_dump(fd, arg->var);
112 }
113 }
114
115 bool psi_decl_arg_validate(struct psi_data *data, struct psi_decl_arg *arg,
116 struct psi_validate_scope *scope)
117 {
118 if (!psi_decl_type_validate(data, arg->type, NULL, scope)) {
119 if (!arg->var->pointer_level) {
120 data->error(data, arg->type->token, PSI_WARNING,
121 "Cannot use '%s' as type for '%s'%s%s", arg->type->name,
122 arg->var->name, *data->last_error ? ": " : "", data->last_error);
123 return false;
124 }
125 }
126 return true;
127 }
128
129 bool psi_decl_arg_validate_typedef(struct psi_data *data,
130 struct psi_decl_arg *def, struct psi_validate_scope *scope)
131 {
132 if (psi_validate_scope_has_type(scope, def->var->name)) {
133 return true;
134 }
135 psi_validate_scope_add_type(scope, def->var->name, def);
136
137 if (def->type->type == PSI_T_VOID) {
138 if (def->var->pointer_level) {
139 def->type->type = PSI_T_POINTER;
140 } else {
141 data->error(data, def->token, PSI_WARNING,
142 "Type '%s' cannot be aliased to 'void'", def->type->name);
143 return false;
144 }
145 } else if (!psi_decl_type_validate(data, def->type, def, scope)) {
146 const char *pre;
147
148 switch (def->type->type) {
149 case PSI_T_STRUCT:
150 pre = "struct ";
151 break;
152 case PSI_T_UNION:
153 pre = "union ";
154 break;
155 case PSI_T_ENUM:
156 pre = "enum ";
157 break;
158 default:
159 pre = "";
160 break;
161 }
162 data->error(data, def->token, PSI_WARNING,
163 "Type '%s' cannot be aliased to '%s%s'%s%s", def->var->name, pre,
164 def->type->name, *data->last_error ? ": " : "", data->last_error);
165 return false;
166 }
167
168 return true;
169 }
170
171 size_t psi_decl_arg_align(struct psi_decl_arg *darg, size_t *pos, size_t *len)
172 {
173 size_t align = psi_decl_arg_get_align(darg);
174
175 if (align) {
176 *len = psi_decl_arg_get_size(darg);
177 *pos = psi_align(align, *pos);
178 }
179
180 return align;
181 }
182
183 size_t psi_decl_arg_get_align(struct psi_decl_arg *darg)
184 {
185 size_t align;
186
187 if (darg->var->pointer_level
188 && (!darg->var->array_size || darg->var->pointer_level > 1)) {
189 align = psi_t_alignment(PSI_T_POINTER);
190 } else {
191 align = psi_decl_type_get_align(darg->type);
192 }
193
194 return align;
195 }
196
197 size_t psi_decl_arg_get_size(struct psi_decl_arg *darg)
198 {
199 size_t size;
200 struct psi_decl_type *real = psi_decl_type_get_real(darg->type);
201
202 if (darg->var->array_size && darg->var->pointer_level > 1) {
203 size = psi_t_size(PSI_T_POINTER) * darg->var->array_size;
204 } else if (!darg->var->array_size && darg->var->pointer_level) {
205 size = psi_t_size(PSI_T_POINTER);
206 } else {
207 switch (real->type) {
208 case PSI_T_UNION:
209 size = real->real.unn->size;
210 break;
211 case PSI_T_STRUCT:
212 size = real->real.strct->size;
213 break;
214 case PSI_T_ENUM:
215 default:
216 size = psi_t_size(real->type);
217 break;
218 }
219
220 if (darg->var->array_size) {
221 size *= darg->var->array_size;
222 }
223 }
224
225 return size;
226 }
227
228 struct psi_decl_arg *psi_decl_arg_get_by_name(struct psi_plist *args,
229 const char *name)
230 {
231 size_t i = 0;
232 struct psi_decl_arg *arg;
233
234 if (args)
235 while (psi_plist_get(args, i++, &arg)) {
236 if (!strcmp(name, arg->var->name)) {
237 return arg;
238 }
239 }
240
241 return NULL;
242 }
243
244 struct psi_decl_arg *psi_decl_arg_get_by_var(struct psi_decl_var *var,
245 struct psi_plist *args, struct psi_decl_arg *func)
246 {
247 struct psi_decl_arg *arg = psi_decl_arg_get_by_name(args, var->name);
248
249 if (arg) {
250 assert(!var->arg || var->arg == arg);
251
252 return var->arg = arg;
253 }
254
255 if (func && !strcmp(var->name, func->var->name)) {
256 return var->arg = func;
257 }
258
259 return NULL;
260 }