widl: Rename ifref_t iface member to type.
[wine/zf.git] / tools / widl / typetree.c
blob2f49a92511aeeb99cd11987a80d82f927a98e6cd
1 /*
2 * IDL Type Tree
4 * Copyright 2008 Robert Shearman
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.
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.
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
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "widl.h"
28 #include "utils.h"
29 #include "parser.h"
30 #include "typetree.h"
31 #include "header.h"
33 type_t *duptype(type_t *t, int dupname)
35 type_t *d = alloc_type();
37 *d = *t;
38 if (dupname && t->name)
39 d->name = xstrdup(t->name);
41 return d;
44 type_t *make_type(enum type_type type)
46 type_t *t = alloc_type();
47 t->name = NULL;
48 t->namespace = NULL;
49 t->type_type = type;
50 t->attrs = NULL;
51 t->c_name = NULL;
52 memset(&t->details, 0, sizeof(t->details));
53 t->typestring_offset = 0;
54 t->ptrdesc = 0;
55 t->ignore = (parse_only != 0);
56 t->defined = FALSE;
57 t->written = FALSE;
58 t->user_types_registered = FALSE;
59 t->tfswrite = FALSE;
60 t->checked = FALSE;
61 t->typelib_idx = -1;
62 init_loc_info(&t->loc_info);
63 return t;
66 static const var_t *find_arg(const var_list_t *args, const char *name)
68 const var_t *arg;
70 if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
72 if (arg->name && !strcmp(name, arg->name))
73 return arg;
76 return NULL;
79 const char *type_get_name(const type_t *type, enum name_type name_type)
81 switch(name_type) {
82 case NAME_DEFAULT:
83 return type->name;
84 case NAME_C:
85 return type->c_name ? type->c_name : type->name;
88 assert(0);
89 return NULL;
92 static size_t append_namespace(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *separator, const char *abi_prefix)
94 int nested = namespace && !is_global_namespace(namespace);
95 const char *name = nested ? namespace->name : abi_prefix;
96 size_t n = 0;
97 if (!name) return 0;
98 if (nested) n += append_namespace(buf, len, pos + n, namespace->parent, separator, abi_prefix);
99 n += strappend(buf, len, pos + n, "%s%s", name, separator);
100 return n;
103 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix, const char *abi_prefix)
105 size_t len = 0, pos = 0;
106 char *buf = NULL;
107 pos += strappend(&buf, &len, pos, "%s", prefix);
108 pos += append_namespace(&buf, &len, pos, namespace, separator, abi_prefix);
109 pos += strappend(&buf, &len, pos, "%s", suffix);
110 return buf;
113 type_t *type_new_function(var_list_t *args)
115 var_t *arg;
116 type_t *t;
117 unsigned int i = 0;
119 if (args)
121 arg = LIST_ENTRY(list_head(args), var_t, entry);
122 if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
124 list_remove(&arg->entry);
125 free(arg);
126 free(args);
127 args = NULL;
130 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
132 if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
133 error_loc("argument '%s' has void type\n", arg->name);
134 if (!arg->name)
136 if (i > 26 * 26)
137 error_loc("too many unnamed arguments\n");
138 else
140 int unique;
143 char name[3];
144 name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
145 name[1] = i > 26 ? 'a' + i % 26 : 0;
146 name[2] = 0;
147 unique = !find_arg(args, name);
148 if (unique)
149 arg->name = xstrdup(name);
150 i++;
151 } while (!unique);
156 t = make_type(TYPE_FUNCTION);
157 t->details.function = xmalloc(sizeof(*t->details.function));
158 t->details.function->args = args;
159 t->details.function->retval = make_var(xstrdup("_RetVal"));
160 return t;
163 type_t *type_new_pointer(type_t *ref)
165 type_t *t = make_type(TYPE_POINTER);
166 t->details.pointer.ref.type = ref;
167 return t;
170 type_t *type_new_alias(const decl_spec_t *t, const char *name)
172 type_t *a = make_type(TYPE_ALIAS);
174 a->name = xstrdup(name);
175 a->attrs = NULL;
176 a->details.alias.aliasee = *t;
177 init_loc_info(&a->loc_info);
179 return a;
182 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
183 unsigned int dim, expr_t *size_is, expr_t *length_is)
185 type_t *t = make_type(TYPE_ARRAY);
186 if (name) t->name = xstrdup(name);
187 t->details.array.declptr = declptr;
188 t->details.array.length_is = length_is;
189 if (size_is)
190 t->details.array.size_is = size_is;
191 else
192 t->details.array.dim = dim;
193 if (element)
194 t->details.array.elem = *element;
195 return t;
198 type_t *type_new_basic(enum type_basic_type basic_type)
200 type_t *t = make_type(TYPE_BASIC);
201 t->details.basic.type = basic_type;
202 t->details.basic.sign = 0;
203 return t;
206 type_t *type_new_int(enum type_basic_type basic_type, int sign)
208 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
210 assert(basic_type <= TYPE_BASIC_INT_MAX);
212 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
213 if (!int_types[basic_type][sign + 1])
215 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
216 int_types[basic_type][sign + 1]->details.basic.sign = sign;
218 return int_types[basic_type][sign + 1];
221 type_t *type_new_void(void)
223 static type_t *void_type = NULL;
224 if (!void_type)
225 void_type = make_type(TYPE_VOID);
226 return void_type;
229 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
231 type_t *t = NULL;
233 if (name)
234 t = find_type(name, namespace,tsENUM);
236 if (!t)
238 t = make_type(TYPE_ENUM);
239 t->name = name;
240 t->namespace = namespace;
241 if (name)
242 reg_type(t, name, namespace, tsENUM);
245 if (!t->defined && defined)
247 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
248 t->details.enumeration->enums = enums;
249 t->defined = TRUE;
251 else if (defined)
252 error_loc("redefinition of enum %s\n", name);
254 return t;
257 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
259 type_t *t = NULL;
261 if (name)
262 t = find_type(name, namespace, tsSTRUCT);
264 if (!t)
266 t = make_type(TYPE_STRUCT);
267 t->name = name;
268 t->namespace = namespace;
269 if (name)
270 reg_type(t, name, namespace, tsSTRUCT);
273 if (!t->defined && defined)
275 t->details.structure = xmalloc(sizeof(*t->details.structure));
276 t->details.structure->fields = fields;
277 t->defined = TRUE;
279 else if (defined)
280 error_loc("redefinition of struct %s\n", name);
282 return t;
285 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
287 type_t *t = NULL;
289 if (name)
290 t = find_type(name, NULL, tsUNION);
292 if (!t)
294 t = make_type(TYPE_UNION);
295 t->name = name;
296 if (name)
297 reg_type(t, name, NULL, tsUNION);
300 if (!t->defined && defined)
302 t->details.structure = xmalloc(sizeof(*t->details.structure));
303 t->details.structure->fields = fields;
304 t->defined = TRUE;
306 else if (defined)
307 error_loc("redefinition of union %s\n", name);
309 return t;
312 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
314 type_t *t = NULL;
316 if (name)
317 t = find_type(name, NULL, tsUNION);
319 if (!t)
321 t = make_type(TYPE_ENCAPSULATED_UNION);
322 t->name = name;
323 if (name)
324 reg_type(t, name, NULL, tsUNION);
326 t->type_type = TYPE_ENCAPSULATED_UNION;
328 if (!t->defined)
330 if (!union_field)
331 union_field = make_var(xstrdup("tagged_union"));
332 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
334 t->details.structure = xmalloc(sizeof(*t->details.structure));
335 t->details.structure->fields = append_var(NULL, switch_field);
336 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
337 t->defined = TRUE;
339 else
340 error_loc("redefinition of union %s\n", name);
342 return t;
345 static int is_valid_bitfield_type(const type_t *type)
347 switch (type_get_type(type))
349 case TYPE_ENUM:
350 return TRUE;
351 case TYPE_BASIC:
352 switch (type_basic_get_type(type))
354 case TYPE_BASIC_INT8:
355 case TYPE_BASIC_INT16:
356 case TYPE_BASIC_INT32:
357 case TYPE_BASIC_INT64:
358 case TYPE_BASIC_INT:
359 case TYPE_BASIC_INT3264:
360 case TYPE_BASIC_LONG:
361 case TYPE_BASIC_CHAR:
362 case TYPE_BASIC_HYPER:
363 case TYPE_BASIC_BYTE:
364 case TYPE_BASIC_WCHAR:
365 case TYPE_BASIC_ERROR_STATUS_T:
366 return TRUE;
367 case TYPE_BASIC_FLOAT:
368 case TYPE_BASIC_DOUBLE:
369 case TYPE_BASIC_HANDLE:
370 return FALSE;
372 return FALSE;
373 default:
374 return FALSE;
378 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
380 type_t *t;
382 if (!is_valid_bitfield_type(field))
383 error_loc("bit-field has invalid type\n");
385 if (bits->cval < 0)
386 error_loc("negative width for bit-field\n");
388 /* FIXME: validate bits->cval <= memsize(field) * 8 */
390 t = make_type(TYPE_BITFIELD);
391 t->details.bitfield.field = field;
392 t->details.bitfield.bits = bits;
393 return t;
396 static unsigned int compute_method_indexes(type_t *iface)
398 unsigned int idx;
399 statement_t *stmt;
401 if (!iface->details.iface)
402 return 0;
404 if (type_iface_get_inherit(iface))
405 idx = compute_method_indexes(type_iface_get_inherit(iface));
406 else
407 idx = 0;
409 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
411 var_t *func = stmt->u.var;
412 if (!is_callas(func->attrs))
413 func->func_idx = idx++;
416 return idx;
419 type_t *type_interface_declare(char *name, struct namespace *namespace)
421 type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
422 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
423 error_loc("interface %s previously not declared an interface at %s:%d\n",
424 type->name, type->loc_info.input_name, type->loc_info.line_number);
425 return type;
428 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, ifref_list_t *requires)
430 if (iface->defined)
431 error_loc("interface %s already defined at %s:%d\n",
432 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
433 if (iface == inherit)
434 error_loc("interface %s can't inherit from itself\n",
435 iface->name);
436 iface->attrs = check_interface_attrs(iface->name, attrs);
437 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
438 iface->details.iface->disp_props = NULL;
439 iface->details.iface->disp_methods = NULL;
440 iface->details.iface->stmts = stmts;
441 iface->details.iface->inherit = inherit;
442 iface->details.iface->disp_inherit = NULL;
443 iface->details.iface->async_iface = NULL;
444 iface->details.iface->requires = requires;
445 iface->defined = TRUE;
446 compute_method_indexes(iface);
447 return iface;
450 type_t *type_dispinterface_declare(char *name)
452 type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
453 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
454 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
455 type->name, type->loc_info.input_name, type->loc_info.line_number);
456 return type;
459 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
461 if (iface->defined)
462 error_loc("dispinterface %s already defined at %s:%d\n",
463 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
464 iface->attrs = check_dispiface_attrs(iface->name, attrs);
465 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
466 iface->details.iface->disp_props = props;
467 iface->details.iface->disp_methods = methods;
468 iface->details.iface->stmts = NULL;
469 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
470 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
471 iface->details.iface->disp_inherit = NULL;
472 iface->details.iface->async_iface = NULL;
473 iface->details.iface->requires = NULL;
474 iface->defined = TRUE;
475 compute_method_indexes(iface);
476 return iface;
479 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
481 if (dispiface->defined)
482 error_loc("dispinterface %s already defined at %s:%d\n",
483 dispiface->name, dispiface->loc_info.input_name, dispiface->loc_info.line_number);
484 dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
485 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
486 dispiface->details.iface->disp_props = NULL;
487 dispiface->details.iface->disp_methods = NULL;
488 dispiface->details.iface->stmts = NULL;
489 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
490 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
491 dispiface->details.iface->disp_inherit = iface;
492 dispiface->details.iface->async_iface = NULL;
493 dispiface->details.iface->requires = NULL;
494 dispiface->defined = TRUE;
495 compute_method_indexes(dispiface);
496 return dispiface;
499 type_t *type_module_declare(char *name)
501 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
502 if (type_get_type_detect_alias(type) != TYPE_MODULE)
503 error_loc("module %s previously not declared a module at %s:%d\n",
504 type->name, type->loc_info.input_name, type->loc_info.line_number);
505 return type;
508 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
510 if (module->defined)
511 error_loc("module %s already defined at %s:%d\n",
512 module->name, module->loc_info.input_name, module->loc_info.line_number);
513 module->attrs = check_module_attrs(module->name, attrs);
514 module->details.module = xmalloc(sizeof(*module->details.module));
515 module->details.module->stmts = stmts;
516 module->defined = TRUE;
517 return module;
520 type_t *type_coclass_declare(char *name)
522 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
523 if (type_get_type_detect_alias(type) != TYPE_COCLASS)
524 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
525 type->name, type->loc_info.input_name, type->loc_info.line_number);
526 return type;
529 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, ifref_list_t *ifaces)
531 if (coclass->defined)
532 error_loc("coclass %s already defined at %s:%d\n",
533 coclass->name, coclass->loc_info.input_name, coclass->loc_info.line_number);
534 coclass->attrs = check_coclass_attrs(coclass->name, attrs);
535 coclass->details.coclass.ifaces = ifaces;
536 coclass->defined = TRUE;
537 return coclass;
540 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
542 type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
543 if (type_get_type_detect_alias(type) != TYPE_RUNTIMECLASS)
544 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
545 type->name, type->loc_info.input_name, type->loc_info.line_number);
546 return type;
549 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, ifref_list_t *ifaces)
551 ifref_t *ifref, *required, *tmp;
552 ifref_list_t *requires;
554 if (runtimeclass->defined)
555 error_loc("runtimeclass %s already defined at %s:%d\n",
556 runtimeclass->name, runtimeclass->loc_info.input_name, runtimeclass->loc_info.line_number);
557 runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
558 runtimeclass->details.runtimeclass.ifaces = ifaces;
559 runtimeclass->defined = TRUE;
560 if (!type_runtimeclass_get_default_iface(runtimeclass))
561 error_loc("missing default interface on runtimeclass %s\n", runtimeclass->name);
563 LIST_FOR_EACH_ENTRY(ifref, ifaces, ifref_t, entry)
565 /* FIXME: this should probably not be allowed, here or in coclass, */
566 /* but for now there's too many places in Wine IDL where it is to */
567 /* even print a warning. */
568 if (!(ifref->type->defined)) continue;
569 if (!(requires = type_iface_get_requires(ifref->type))) continue;
570 LIST_FOR_EACH_ENTRY(required, requires, ifref_t, entry)
572 int found = 0;
574 LIST_FOR_EACH_ENTRY(tmp, ifaces, ifref_t, entry)
575 if ((found = type_is_equal(tmp->type, required->type))) break;
577 if (!found)
578 error_loc("interface '%s' also requires interface '%s', "
579 "but runtimeclass '%s' does not implement it.\n",
580 ifref->type->name, required->type->name, runtimeclass->name);
584 return runtimeclass;
587 type_t *type_apicontract_declare(char *name, struct namespace *namespace)
589 type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
590 if (type_get_type_detect_alias(type) != TYPE_APICONTRACT)
591 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
592 type->name, type->loc_info.input_name, type->loc_info.line_number);
593 return type;
596 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
598 if (apicontract->defined)
599 error_loc("apicontract %s already defined at %s:%d\n",
600 apicontract->name, apicontract->loc_info.input_name, apicontract->loc_info.line_number);
601 apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
602 apicontract->defined = TRUE;
603 return apicontract;
606 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, type_list_t *params)
608 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
609 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
610 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
611 type->name, type->loc_info.input_name, type->loc_info.line_number);
612 type->details.parameterized.type = make_type(TYPE_INTERFACE);
613 type->details.parameterized.params = params;
614 return type;
617 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, ifref_list_t *requires)
619 type_t *iface;
620 if (type->defined)
621 error_loc("pinterface %s already defined at %s:%d\n",
622 type->name, type->loc_info.input_name, type->loc_info.line_number);
624 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
625 * a new type GUID with the rules described in:
626 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
627 * TODO: store type signatures for generated interfaces, and generate their GUIDs
629 type->attrs = check_interface_attrs(type->name, attrs);
631 iface = type->details.parameterized.type;
632 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
633 iface->details.iface->disp_props = NULL;
634 iface->details.iface->disp_methods = NULL;
635 iface->details.iface->stmts = stmts;
636 iface->details.iface->inherit = inherit;
637 iface->details.iface->disp_inherit = NULL;
638 iface->details.iface->async_iface = NULL;
639 iface->details.iface->requires = requires;
641 type->defined = TRUE;
642 return type;
645 int type_is_equal(const type_t *type1, const type_t *type2)
647 if (type1 == type2)
648 return TRUE;
649 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
650 return FALSE;
651 if (type1->namespace != type2->namespace)
652 return FALSE;
654 if (type1->name && type2->name)
655 return !strcmp(type1->name, type2->name);
656 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
657 return FALSE;
659 /* FIXME: do deep inspection of types to determine if they are equal */
661 return FALSE;