widl: Remove func_t type.
[wine] / tools / widl / typetree.c
1 /*
2  * IDL Type Tree
3  *
4  * Copyright 2008 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "widl.h"
28 #include "utils.h"
29 #include "parser.h"
30 #include "typetree.h"
31 #include "header.h"
32
33 type_t *duptype(type_t *t, int dupname)
34 {
35   type_t *d = alloc_type();
36
37   *d = *t;
38   if (dupname && t->name)
39     d->name = xstrdup(t->name);
40
41   return d;
42 }
43
44 type_t *make_type(enum type_type type)
45 {
46     type_t *t = alloc_type();
47     t->name = NULL;
48     t->type_type = type;
49     t->attrs = NULL;
50     t->orig = NULL;
51     memset(&t->details, 0, sizeof(t->details));
52     t->typestring_offset = 0;
53     t->ptrdesc = 0;
54     t->ignore = (parse_only != 0);
55     t->defined = FALSE;
56     t->written = FALSE;
57     t->user_types_registered = FALSE;
58     t->tfswrite = FALSE;
59     t->checked = FALSE;
60     t->is_alias = FALSE;
61     t->typelib_idx = -1;
62     init_loc_info(&t->loc_info);
63     return t;
64 }
65
66 static const var_t *find_arg(const var_list_t *args, const char *name)
67 {
68     const var_t *arg;
69
70     if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
71     {
72         if (arg->name && !strcmp(name, arg->name))
73             return arg;
74     }
75
76     return NULL;
77 }
78
79 type_t *type_new_function(var_list_t *args)
80 {
81     var_t *arg;
82     type_t *t;
83     unsigned int i = 0;
84
85     if (args)
86     {
87         arg = LIST_ENTRY(list_head(args), var_t, entry);
88         if (list_count(args) == 1 && !arg->name && arg->type && type_get_type(arg->type) == TYPE_VOID)
89         {
90             list_remove(&arg->entry);
91             free(arg);
92             free(args);
93             args = NULL;
94         }
95     }
96     if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
97     {
98         if (arg->type && type_get_type(arg->type) == TYPE_VOID)
99             error_loc("argument '%s' has void type\n", arg->name);
100         if (!arg->name)
101         {
102             if (i > 26 * 26)
103                 error_loc("too many unnamed arguments\n");
104             else
105             {
106                 int unique;
107                 do
108                 {
109                     char name[3];
110                     name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
111                     name[1] = i > 26 ? 'a' + i % 26 : 0;
112                     name[2] = 0;
113                     unique = !find_arg(args, name);
114                     if (unique)
115                         arg->name = xstrdup(name);
116                     i++;
117                 } while (!unique);
118             }
119         }
120     }
121
122     t = make_type(TYPE_FUNCTION);
123     t->details.function = xmalloc(sizeof(*t->details.function));
124     t->details.function->args = args;
125     t->details.function->idx = -1;
126     return t;
127 }
128
129 type_t *type_new_pointer(unsigned char pointer_default, type_t *ref, attr_list_t *attrs)
130 {
131     type_t *t = make_type(TYPE_POINTER);
132     t->details.pointer.def_fc = pointer_default;
133     t->details.pointer.ref = ref;
134     t->attrs = attrs;
135     return t;
136 }
137
138 type_t *type_new_alias(type_t *t, const char *name)
139 {
140     type_t *a = duptype(t, 0);
141
142     a->name = xstrdup(name);
143     a->attrs = NULL;
144     a->orig = t;
145     a->is_alias = TRUE;
146     /* for pointer types */
147     a->details = t->details;
148     init_loc_info(&a->loc_info);
149
150     return a;
151 }
152
153 type_t *type_new_module(char *name)
154 {
155     type_t *type = get_type(TYPE_MODULE, name, 0);
156     if (type->type_type != TYPE_MODULE || type->defined)
157         error_loc("%s: redefinition error; original definition was at %s:%d\n",
158                   type->name, type->loc_info.input_name, type->loc_info.line_number);
159     type->name = name;
160     return type;
161 }
162
163 type_t *type_new_coclass(char *name)
164 {
165     type_t *type = get_type(TYPE_COCLASS, name, 0);
166     if (type->type_type != TYPE_COCLASS || type->defined)
167         error_loc("%s: redefinition error; original definition was at %s:%d\n",
168                   type->name, type->loc_info.input_name, type->loc_info.line_number);
169     type->name = name;
170     return type;
171 }
172
173
174 type_t *type_new_array(const char *name, type_t *element, int declptr,
175                        unsigned int dim, expr_t *size_is, expr_t *length_is,
176                        unsigned char ptr_default_fc)
177 {
178     type_t *t = make_type(TYPE_ARRAY);
179     if (name) t->name = xstrdup(name);
180     t->details.array.declptr = declptr;
181     t->details.array.length_is = length_is;
182     if (size_is)
183         t->details.array.size_is = size_is;
184     else
185         t->details.array.dim = dim;
186     t->details.array.elem = element;
187     t->details.array.ptr_def_fc = ptr_default_fc;
188     return t;
189 }
190
191 type_t *type_new_basic(enum type_basic_type basic_type)
192 {
193     type_t *t = make_type(TYPE_BASIC);
194     t->details.basic.type = basic_type;
195     t->details.basic.sign = 0;
196     return t;
197 }
198
199 type_t *type_new_int(enum type_basic_type basic_type, int sign)
200 {
201     static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
202
203     assert(basic_type <= TYPE_BASIC_INT_MAX);
204
205     /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
206     if (!int_types[basic_type][sign + 1])
207     {
208         int_types[basic_type][sign + 1] = type_new_basic(basic_type);
209         int_types[basic_type][sign + 1]->details.basic.sign = sign;
210     }
211     return int_types[basic_type][sign + 1];
212 }
213
214 type_t *type_new_void(void)
215 {
216     static type_t *void_type = NULL;
217     if (!void_type)
218         void_type = make_type(TYPE_VOID);
219     return void_type;
220 }
221
222 type_t *type_new_enum(const char *name, int defined, var_list_t *enums)
223 {
224     type_t *tag_type = name ? find_type(name, tsENUM) : NULL;
225     type_t *t = make_type(TYPE_ENUM);
226     t->name = name;
227
228     if (tag_type && tag_type->details.enumeration)
229         t->details.enumeration = tag_type->details.enumeration;
230     else if (defined)
231     {
232         t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
233         t->details.enumeration->enums = enums;
234         t->defined = TRUE;
235     }
236
237     if (name)
238     {
239         if (defined)
240             reg_type(t, name, tsENUM);
241         else
242             add_incomplete(t);
243     }
244     return t;
245 }
246
247 type_t *type_new_struct(char *name, int defined, var_list_t *fields)
248 {
249     type_t *tag_type = name ? find_type(name, tsSTRUCT) : NULL;
250     type_t *t = make_type(TYPE_STRUCT);
251     t->name = name;
252     if (tag_type && tag_type->details.structure)
253         t->details.structure = tag_type->details.structure;
254     else if (defined)
255     {
256         t->details.structure = xmalloc(sizeof(*t->details.structure));
257         t->details.structure->fields = fields;
258         t->defined = TRUE;
259     }
260     if (name)
261     {
262         if (defined)
263             reg_type(t, name, tsSTRUCT);
264         else
265             add_incomplete(t);
266     }
267     return t;
268 }
269
270 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
271 {
272     type_t *tag_type = name ? find_type(name, tsUNION) : NULL;
273     type_t *t = make_type(TYPE_UNION);
274     t->name = name;
275     if (tag_type && tag_type->details.structure)
276         t->details.structure = tag_type->details.structure;
277     else if (defined)
278     {
279         t->details.structure = xmalloc(sizeof(*t->details.structure));
280         t->details.structure->fields = fields;
281         t->defined = TRUE;
282     }
283     if (name)
284     {
285         if (defined)
286             reg_type(t, name, tsUNION);
287         else
288             add_incomplete(t);
289     }
290     return t;
291 }
292
293 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
294 {
295     type_t *t = get_type(TYPE_ENCAPSULATED_UNION, name, tsUNION);
296     if (!union_field) union_field = make_var( xstrdup("tagged_union") );
297     union_field->type = type_new_nonencapsulated_union(NULL, TRUE, cases);
298     t->details.structure = xmalloc(sizeof(*t->details.structure));
299     t->details.structure->fields = append_var( NULL, switch_field );
300     t->details.structure->fields = append_var( t->details.structure->fields, union_field );
301     t->defined = TRUE;
302     return t;
303 }
304
305 static int is_valid_bitfield_type(const type_t *type)
306 {
307     switch (type_get_type(type))
308     {
309     case TYPE_ENUM:
310         return TRUE;
311     case TYPE_BASIC:
312         switch (type_basic_get_type(type))
313         {
314         case TYPE_BASIC_INT8:
315         case TYPE_BASIC_INT16:
316         case TYPE_BASIC_INT32:
317         case TYPE_BASIC_INT64:
318         case TYPE_BASIC_INT:
319         case TYPE_BASIC_INT3264:
320         case TYPE_BASIC_CHAR:
321         case TYPE_BASIC_HYPER:
322         case TYPE_BASIC_BYTE:
323         case TYPE_BASIC_WCHAR:
324         case TYPE_BASIC_ERROR_STATUS_T:
325             return TRUE;
326         case TYPE_BASIC_FLOAT:
327         case TYPE_BASIC_DOUBLE:
328         case TYPE_BASIC_HANDLE:
329             return FALSE;
330         }
331         return FALSE;
332     default:
333         return FALSE;
334     }
335 }
336
337 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
338 {
339     type_t *t;
340
341     if (!is_valid_bitfield_type(field))
342         error_loc("bit-field has invalid type\n");
343
344     if (bits->cval < 0)
345         error_loc("negative width for bit-field\n");
346
347     /* FIXME: validate bits->cval <= memsize(field) * 8 */
348
349     t = make_type(TYPE_BITFIELD);
350     t->details.bitfield.field = field;
351     t->details.bitfield.bits = bits;
352     return t;
353 }
354
355 static int compute_method_indexes(type_t *iface)
356 {
357     int idx;
358     statement_t *stmt;
359
360     if (!iface->details.iface)
361         return 0;
362
363     if (type_iface_get_inherit(iface))
364         idx = compute_method_indexes(type_iface_get_inherit(iface));
365     else
366         idx = 0;
367
368     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
369     {
370         var_t *func = stmt->u.var;
371         if (!is_callas(func->attrs))
372             func->type->details.function->idx = idx++;
373     }
374
375     return idx;
376 }
377
378 void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
379 {
380     iface->details.iface = xmalloc(sizeof(*iface->details.iface));
381     iface->details.iface->disp_props = NULL;
382     iface->details.iface->disp_methods = NULL;
383     iface->details.iface->stmts = stmts;
384     iface->details.iface->inherit = inherit;
385     iface->defined = TRUE;
386     compute_method_indexes(iface);
387 }
388
389 void type_dispinterface_define(type_t *iface, var_list_t *props, var_list_t *methods)
390 {
391     iface->details.iface = xmalloc(sizeof(*iface->details.iface));
392     iface->details.iface->disp_props = props;
393     iface->details.iface->disp_methods = methods;
394     iface->details.iface->stmts = NULL;
395     iface->details.iface->inherit = find_type("IDispatch", 0);
396     if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
397     iface->defined = TRUE;
398     compute_method_indexes(iface);
399 }
400
401 void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
402 {
403     type_dispinterface_define(dispiface, iface->details.iface->disp_props,
404                               iface->details.iface->disp_methods);
405 }
406
407 void type_module_define(type_t *module, statement_list_t *stmts)
408 {
409     if (module->details.module) error_loc("multiple definition error\n");
410     module->details.module = xmalloc(sizeof(*module->details.module));
411     module->details.module->stmts = stmts;
412     module->defined = TRUE;
413 }
414
415 type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
416 {
417     coclass->details.coclass.ifaces = ifaces;
418     coclass->defined = TRUE;
419     return coclass;
420 }
421
422 int type_is_equal(const type_t *type1, const type_t *type2)
423 {
424     if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
425         return FALSE;
426
427     if (type1->name && type2->name)
428         return !strcmp(type1->name, type2->name);
429     else if ((!type1->name && type2->name) || (type1->name && !type2->name))
430         return FALSE;
431
432     /* FIXME: do deep inspection of types to determine if they are equal */
433
434     return FALSE;
435 }