types refactoring
[m6w6/ext-psi] / src / types / decl_type.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 "token.h"
36 #include "php_psi_stdtypes.h"
37
38 #include "data.h"
39
40 decl_type *init_decl_type(token_t type, const char *name) {
41 decl_type *t = calloc(1, sizeof(*t));
42 t->type = type;
43 t->name = strdup(name);
44 return t;
45 }
46
47 void free_decl_type(decl_type *type) {
48 if (type->token) {
49 free(type->token);
50 }
51 if (type->type == PSI_T_FUNCTION) {
52 free_decl(type->real.func);
53 }
54 free(type->name);
55 free(type);
56 }
57
58 decl_args *extract_decl_type_args(decl_type *dtyp, decl_type **real_typ_ptr) {
59 decl_type *var_typ;
60 var_typ = real_decl_type(dtyp);
61 if (real_typ_ptr) {
62 *real_typ_ptr = var_typ;
63 }
64 switch (var_typ->type) {
65 case PSI_T_STRUCT:
66 return var_typ->real.strct->args;
67 case PSI_T_UNION:
68 return var_typ->real.unn->args;
69 default:
70 return NULL;
71 }
72 }
73
74
75 int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
76 size_t i;
77 struct psi_std_type *stdtyp;
78
79 if (type->real.def) {
80 return 1;
81 }
82 if (defs) for (i = 0; i < defs->count; ++i) {
83 decl_arg *def = defs->list[i];
84
85 if (def->type->type != type->type && !strcmp(def->var->name, type->name)) {
86 type->real.def = def;
87 return 1;
88 }
89 }
90 for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) {
91 if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) {
92 type->type = stdtyp->type_tag;
93 return 1;
94 }
95 }
96
97 return 0;
98 }
99
100 int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
101 size_t i;
102
103 if (type->real.strct) {
104 return 1;
105 }
106 if (structs) for (i = 0; i < structs->count; ++i) {
107 if (!strcmp(structs->list[i]->name, type->name)) {
108 type->real.strct = structs->list[i];
109 return 1;
110 }
111 }
112 return 0;
113 }
114
115 int locate_decl_type_union(decl_unions *unions, decl_type *type) {
116 size_t i;
117
118 if (type->real.unn) {
119 return 1;
120 }
121 if (unions) for (i = 0; i < unions->count; ++i) {
122 if (!strcmp(unions->list[i]->name, type->name)) {
123 type->real.unn = unions->list[i];
124 return 1;
125 }
126 }
127 return 0;
128 }
129
130 int locate_decl_type_enum(decl_enums *enums, decl_type *type) {
131 size_t i;
132
133 if (type->real.enm) {
134 return 1;
135 }
136 if (enums) for (i = 0; i < enums->count; ++i) {
137 if (!strcmp(enums->list[i]->name, type->name)) {
138 type->real.enm = enums->list[i];
139 return 1;
140 }
141 }
142 return 0;
143 }
144
145 int locate_decl_type_decl(decls *decls, decl_type *type) {
146 size_t i;
147
148 if (type->real.func) {
149 return 1;
150 }
151 if (decls) for (i = 0; i < decls->count; ++i) {
152 if (!strcmp(decls->list[i]->func->var->name, type->name)) {
153 type->real.func = decls->list[i];
154 return 1;
155 }
156 }
157
158 return 0;
159 }
160
161 int validate_decl_type(struct psi_data *data, decl_type *type, decl_arg *def) {
162 if (weak_decl_type(type)) {
163 if (!locate_decl_type_alias(data->defs, type)) {
164 return 0;
165 }
166 if (type->real.def) {
167 return validate_decl_type(data, type->real.def->type, type->real.def);
168 }
169 return 1;
170 }
171
172 switch (type->type) {
173 case PSI_T_STRUCT:
174 if (!locate_decl_type_struct(data->structs, type)) {
175 return 0;
176 }
177 break;
178 case PSI_T_UNION:
179 if (!locate_decl_type_union(data->unions, type)) {
180 return 0;
181 }
182 break;
183 case PSI_T_ENUM:
184 if (!locate_decl_type_enum(data->enums, type)) {
185 return 0;
186 }
187 break;
188 case PSI_T_FUNCTION:
189 if (!locate_decl_type_decl(data->decls, type)) {
190 return 0;
191 }
192 if (!validate_decl_nodl(data, type->real.func)) {
193 return 0;
194 }
195 break;
196 }
197 return 1;
198 }
199
200 void dump_decl_type(int fd, decl_type *t, unsigned level) {
201 switch (t->type) {
202 case PSI_T_POINTER:
203 dprintf(fd, "%s *", t->name);
204 return;
205
206 case PSI_T_ENUM:
207 dprintf(fd, "enum ");
208 if (is_anon_type(t->name, "enum")) {
209 dump_decl_enum_items(fd, t->real.enm->items, level);
210 return;
211 }
212 break;
213
214 case PSI_T_STRUCT:
215 dprintf(fd, "struct ");
216 if (is_anon_type(t->name, "struct")) {
217 dump_decl_args_with_layout(fd, t->real.strct->args, level);
218 return;
219 }
220 break;
221
222 case PSI_T_UNION:
223 dprintf(fd, "union ");
224 if (is_anon_type(t->name, "union")) {
225 dump_decl_args_with_layout(fd, t->real.unn->args, level);
226 return;
227 }
228 break;
229 }
230 dprintf(fd, "%s", t->name);
231 }
232
233 int weak_decl_type(decl_type *type) {
234 switch (type->type) {
235 case PSI_T_CHAR:
236 case PSI_T_SHORT:
237 case PSI_T_INT:
238 case PSI_T_LONG:
239 case PSI_T_NAME:
240 return type->type;
241 default:
242 return 0;
243 }
244 }
245
246 decl_type *real_decl_type(decl_type *type) {
247 while (weak_decl_type(type)) {
248 type = type->real.def->type;
249 }
250 return type;
251 }
252
253 size_t alignof_decl_type(decl_type *t) {
254 decl_type *real = real_decl_type(t);
255 size_t align;
256
257 switch (real->type) {
258 case PSI_T_STRUCT:
259 align = alignof_decl_struct(real->real.strct);
260 break;
261 case PSI_T_UNION:
262 align = alignof_decl_union(real->real.unn);
263 break;
264 case PSI_T_ENUM:
265 default:
266 align = psi_t_alignment(real->type);
267 }
268
269 return align;
270 }