widl: Support WinRT parameterized interface type.
[wine.git] / tools / widl / typetree.c
blob3f90fcbe014adb9913d01eda16317ed0697a7d22
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 static size_t append_namespaces(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *prefix,
104 const char *separator, const char *suffix, const char *abi_prefix)
106 size_t n = 0;
107 n += strappend(buf, len, pos + n, "%s", prefix);
108 n += append_namespace(buf, len, pos + n, namespace, separator, abi_prefix);
109 n += strappend(buf, len, pos + n, "%s", suffix);
110 return n;
113 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix, const char *abi_prefix)
115 size_t len = 0;
116 char *buf = NULL;
117 append_namespaces(&buf, &len, 0, namespace, prefix, separator, suffix, abi_prefix);
118 return buf;
121 char *format_parameterized_type_name(type_t *type, typeref_list_t *params)
123 size_t len = 0, pos = 0;
124 char *buf = NULL;
125 typeref_t *ref;
127 pos += strappend(&buf, &len, pos, "%s<", type->name);
128 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
130 for (type = ref->type; type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) {}
131 pos += append_namespaces(&buf, &len, pos, type->namespace, "", "::", type->name, use_abi_namespace ? "ABI" : NULL);
132 for (type = ref->type; type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) pos += strappend(&buf, &len, pos, "*");
133 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ",");
135 pos += strappend(&buf, &len, pos, ">");
137 return buf;
140 static char const *parameterized_type_shorthands[][2] = {
141 {"Windows_CFoundation_CCollections_C", "__F"},
142 {"Windows_CFoundation_C", "__F"},
145 static char *format_parameterized_type_c_name(type_t *type, typeref_list_t *params)
147 size_t len = 0, pos = 0;
148 char *buf = NULL, *tmp;
149 int i, count = params ? list_count(params) : 0;
150 typeref_t *ref;
152 pos += append_namespaces(&buf, &len, pos, type->namespace, "__x_", "_C", type->name, use_abi_namespace ? "ABI" : NULL);
153 pos += strappend(&buf, &len, pos, "_%d", count);
154 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
156 for (type = ref->type; type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) {}
157 pos += append_namespaces(&buf, &len, pos, type->namespace, "_", "__C", type->name, NULL);
160 for (i = 0; i < ARRAY_SIZE(parameterized_type_shorthands); ++i)
162 if ((tmp = strstr(buf, parameterized_type_shorthands[i][0])) &&
163 (tmp - buf) == strlen(use_abi_namespace ? "__x_ABI_C" : "__x_C"))
165 tmp += strlen(parameterized_type_shorthands[i][0]);
166 strcpy(buf, parameterized_type_shorthands[i][1]);
167 memmove(buf + 3, tmp, len - (tmp - buf));
171 return buf;
174 type_t *type_new_function(var_list_t *args)
176 var_t *arg;
177 type_t *t;
178 unsigned int i = 0;
180 if (args)
182 arg = LIST_ENTRY(list_head(args), var_t, entry);
183 if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
185 list_remove(&arg->entry);
186 free(arg);
187 free(args);
188 args = NULL;
191 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
193 if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
194 error_loc("argument '%s' has void type\n", arg->name);
195 if (!arg->name)
197 if (i > 26 * 26)
198 error_loc("too many unnamed arguments\n");
199 else
201 int unique;
204 char name[3];
205 name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
206 name[1] = i > 26 ? 'a' + i % 26 : 0;
207 name[2] = 0;
208 unique = !find_arg(args, name);
209 if (unique)
210 arg->name = xstrdup(name);
211 i++;
212 } while (!unique);
217 t = make_type(TYPE_FUNCTION);
218 t->details.function = xmalloc(sizeof(*t->details.function));
219 t->details.function->args = args;
220 t->details.function->retval = make_var(xstrdup("_RetVal"));
221 return t;
224 type_t *type_new_pointer(type_t *ref)
226 type_t *t = make_type(TYPE_POINTER);
227 t->details.pointer.ref.type = ref;
228 return t;
231 type_t *type_new_alias(const decl_spec_t *t, const char *name)
233 type_t *a = make_type(TYPE_ALIAS);
235 a->name = xstrdup(name);
236 a->attrs = NULL;
237 a->details.alias.aliasee = *t;
238 init_loc_info(&a->loc_info);
240 return a;
243 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
244 unsigned int dim, expr_t *size_is, expr_t *length_is)
246 type_t *t = make_type(TYPE_ARRAY);
247 if (name) t->name = xstrdup(name);
248 t->details.array.declptr = declptr;
249 t->details.array.length_is = length_is;
250 if (size_is)
251 t->details.array.size_is = size_is;
252 else
253 t->details.array.dim = dim;
254 if (element)
255 t->details.array.elem = *element;
256 return t;
259 type_t *type_new_basic(enum type_basic_type basic_type)
261 type_t *t = make_type(TYPE_BASIC);
262 t->details.basic.type = basic_type;
263 t->details.basic.sign = 0;
264 return t;
267 type_t *type_new_int(enum type_basic_type basic_type, int sign)
269 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
271 assert(basic_type <= TYPE_BASIC_INT_MAX);
273 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
274 if (!int_types[basic_type][sign + 1])
276 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
277 int_types[basic_type][sign + 1]->details.basic.sign = sign;
279 return int_types[basic_type][sign + 1];
282 type_t *type_new_void(void)
284 static type_t *void_type = NULL;
285 if (!void_type)
286 void_type = make_type(TYPE_VOID);
287 return void_type;
290 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
292 type_t *t = NULL;
294 if (name)
295 t = find_type(name, namespace,tsENUM);
297 if (!t)
299 t = make_type(TYPE_ENUM);
300 t->name = name;
301 t->namespace = namespace;
302 if (name)
303 reg_type(t, name, namespace, tsENUM);
306 if (!t->defined && defined)
308 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
309 t->details.enumeration->enums = enums;
310 t->defined = TRUE;
312 else if (defined)
313 error_loc("redefinition of enum %s\n", name);
315 return t;
318 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
320 type_t *t = NULL;
322 if (name)
323 t = find_type(name, namespace, tsSTRUCT);
325 if (!t)
327 t = make_type(TYPE_STRUCT);
328 t->name = name;
329 t->namespace = namespace;
330 if (name)
331 reg_type(t, name, namespace, tsSTRUCT);
334 if (!t->defined && defined)
336 t->details.structure = xmalloc(sizeof(*t->details.structure));
337 t->details.structure->fields = fields;
338 t->defined = TRUE;
340 else if (defined)
341 error_loc("redefinition of struct %s\n", name);
343 return t;
346 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
348 type_t *t = NULL;
350 if (name)
351 t = find_type(name, NULL, tsUNION);
353 if (!t)
355 t = make_type(TYPE_UNION);
356 t->name = name;
357 if (name)
358 reg_type(t, name, NULL, tsUNION);
361 if (!t->defined && defined)
363 t->details.structure = xmalloc(sizeof(*t->details.structure));
364 t->details.structure->fields = fields;
365 t->defined = TRUE;
367 else if (defined)
368 error_loc("redefinition of union %s\n", name);
370 return t;
373 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
375 type_t *t = NULL;
377 if (name)
378 t = find_type(name, NULL, tsUNION);
380 if (!t)
382 t = make_type(TYPE_ENCAPSULATED_UNION);
383 t->name = name;
384 if (name)
385 reg_type(t, name, NULL, tsUNION);
387 t->type_type = TYPE_ENCAPSULATED_UNION;
389 if (!t->defined)
391 if (!union_field)
392 union_field = make_var(xstrdup("tagged_union"));
393 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
395 t->details.structure = xmalloc(sizeof(*t->details.structure));
396 t->details.structure->fields = append_var(NULL, switch_field);
397 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
398 t->defined = TRUE;
400 else
401 error_loc("redefinition of union %s\n", name);
403 return t;
406 static int is_valid_bitfield_type(const type_t *type)
408 switch (type_get_type(type))
410 case TYPE_ENUM:
411 return TRUE;
412 case TYPE_BASIC:
413 switch (type_basic_get_type(type))
415 case TYPE_BASIC_INT8:
416 case TYPE_BASIC_INT16:
417 case TYPE_BASIC_INT32:
418 case TYPE_BASIC_INT64:
419 case TYPE_BASIC_INT:
420 case TYPE_BASIC_INT3264:
421 case TYPE_BASIC_LONG:
422 case TYPE_BASIC_CHAR:
423 case TYPE_BASIC_HYPER:
424 case TYPE_BASIC_BYTE:
425 case TYPE_BASIC_WCHAR:
426 case TYPE_BASIC_ERROR_STATUS_T:
427 return TRUE;
428 case TYPE_BASIC_FLOAT:
429 case TYPE_BASIC_DOUBLE:
430 case TYPE_BASIC_HANDLE:
431 return FALSE;
433 return FALSE;
434 default:
435 return FALSE;
439 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
441 type_t *t;
443 if (!is_valid_bitfield_type(field))
444 error_loc("bit-field has invalid type\n");
446 if (bits->cval < 0)
447 error_loc("negative width for bit-field\n");
449 /* FIXME: validate bits->cval <= memsize(field) * 8 */
451 t = make_type(TYPE_BITFIELD);
452 t->details.bitfield.field = field;
453 t->details.bitfield.bits = bits;
454 return t;
457 static unsigned int compute_method_indexes(type_t *iface)
459 unsigned int idx;
460 statement_t *stmt;
462 if (!iface->details.iface)
463 return 0;
465 if (type_iface_get_inherit(iface))
466 idx = compute_method_indexes(type_iface_get_inherit(iface));
467 else
468 idx = 0;
470 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
472 var_t *func = stmt->u.var;
473 if (!is_callas(func->attrs))
474 func->func_idx = idx++;
477 return idx;
480 type_t *type_interface_declare(char *name, struct namespace *namespace)
482 type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
483 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
484 error_loc("interface %s previously not declared an interface at %s:%d\n",
485 type->name, type->loc_info.input_name, type->loc_info.line_number);
486 return type;
489 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
491 if (iface->defined)
492 error_loc("interface %s already defined at %s:%d\n",
493 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
494 if (iface == inherit)
495 error_loc("interface %s can't inherit from itself\n",
496 iface->name);
497 iface->attrs = check_interface_attrs(iface->name, attrs);
498 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
499 iface->details.iface->disp_props = NULL;
500 iface->details.iface->disp_methods = NULL;
501 iface->details.iface->stmts = stmts;
502 iface->details.iface->inherit = inherit;
503 iface->details.iface->disp_inherit = NULL;
504 iface->details.iface->async_iface = NULL;
505 iface->details.iface->requires = requires;
506 iface->defined = TRUE;
507 compute_method_indexes(iface);
508 return iface;
511 type_t *type_dispinterface_declare(char *name)
513 type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
514 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
515 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
516 type->name, type->loc_info.input_name, type->loc_info.line_number);
517 return type;
520 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
522 if (iface->defined)
523 error_loc("dispinterface %s already defined at %s:%d\n",
524 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
525 iface->attrs = check_dispiface_attrs(iface->name, attrs);
526 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
527 iface->details.iface->disp_props = props;
528 iface->details.iface->disp_methods = methods;
529 iface->details.iface->stmts = NULL;
530 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
531 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
532 iface->details.iface->disp_inherit = NULL;
533 iface->details.iface->async_iface = NULL;
534 iface->details.iface->requires = NULL;
535 iface->defined = TRUE;
536 compute_method_indexes(iface);
537 return iface;
540 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
542 if (dispiface->defined)
543 error_loc("dispinterface %s already defined at %s:%d\n",
544 dispiface->name, dispiface->loc_info.input_name, dispiface->loc_info.line_number);
545 dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
546 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
547 dispiface->details.iface->disp_props = NULL;
548 dispiface->details.iface->disp_methods = NULL;
549 dispiface->details.iface->stmts = NULL;
550 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
551 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
552 dispiface->details.iface->disp_inherit = iface;
553 dispiface->details.iface->async_iface = NULL;
554 dispiface->details.iface->requires = NULL;
555 dispiface->defined = TRUE;
556 compute_method_indexes(dispiface);
557 return dispiface;
560 type_t *type_module_declare(char *name)
562 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
563 if (type_get_type_detect_alias(type) != TYPE_MODULE)
564 error_loc("module %s previously not declared a module at %s:%d\n",
565 type->name, type->loc_info.input_name, type->loc_info.line_number);
566 return type;
569 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
571 if (module->defined)
572 error_loc("module %s already defined at %s:%d\n",
573 module->name, module->loc_info.input_name, module->loc_info.line_number);
574 module->attrs = check_module_attrs(module->name, attrs);
575 module->details.module = xmalloc(sizeof(*module->details.module));
576 module->details.module->stmts = stmts;
577 module->defined = TRUE;
578 return module;
581 type_t *type_coclass_declare(char *name)
583 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
584 if (type_get_type_detect_alias(type) != TYPE_COCLASS)
585 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
586 type->name, type->loc_info.input_name, type->loc_info.line_number);
587 return type;
590 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces)
592 if (coclass->defined)
593 error_loc("coclass %s already defined at %s:%d\n",
594 coclass->name, coclass->loc_info.input_name, coclass->loc_info.line_number);
595 coclass->attrs = check_coclass_attrs(coclass->name, attrs);
596 coclass->details.coclass.ifaces = ifaces;
597 coclass->defined = TRUE;
598 return coclass;
601 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
603 type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
604 if (type_get_type_detect_alias(type) != TYPE_RUNTIMECLASS)
605 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
606 type->name, type->loc_info.input_name, type->loc_info.line_number);
607 return type;
610 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces)
612 typeref_t *ref, *required, *tmp;
613 typeref_list_t *requires;
615 if (runtimeclass->defined)
616 error_loc("runtimeclass %s already defined at %s:%d\n",
617 runtimeclass->name, runtimeclass->loc_info.input_name, runtimeclass->loc_info.line_number);
618 runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
619 runtimeclass->details.runtimeclass.ifaces = ifaces;
620 runtimeclass->defined = TRUE;
621 if (!type_runtimeclass_get_default_iface(runtimeclass))
622 error_loc("missing default interface on runtimeclass %s\n", runtimeclass->name);
624 LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
626 /* FIXME: this should probably not be allowed, here or in coclass, */
627 /* but for now there's too many places in Wine IDL where it is to */
628 /* even print a warning. */
629 if (!(ref->type->defined)) continue;
630 if (!(requires = type_iface_get_requires(ref->type))) continue;
631 LIST_FOR_EACH_ENTRY(required, requires, typeref_t, entry)
633 int found = 0;
635 LIST_FOR_EACH_ENTRY(tmp, ifaces, typeref_t, entry)
636 if ((found = type_is_equal(tmp->type, required->type))) break;
638 if (!found)
639 error_loc("interface '%s' also requires interface '%s', "
640 "but runtimeclass '%s' does not implement it.\n",
641 ref->type->name, required->type->name, runtimeclass->name);
645 return runtimeclass;
648 type_t *type_apicontract_declare(char *name, struct namespace *namespace)
650 type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
651 if (type_get_type_detect_alias(type) != TYPE_APICONTRACT)
652 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
653 type->name, type->loc_info.input_name, type->loc_info.line_number);
654 return type;
657 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
659 if (apicontract->defined)
660 error_loc("apicontract %s already defined at %s:%d\n",
661 apicontract->name, apicontract->loc_info.input_name, apicontract->loc_info.line_number);
662 apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
663 apicontract->defined = TRUE;
664 return apicontract;
667 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params)
669 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
670 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
671 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
672 type->name, type->loc_info.input_name, type->loc_info.line_number);
673 type->details.parameterized.type = make_type(TYPE_INTERFACE);
674 type->details.parameterized.params = params;
675 return type;
678 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
680 type_t *iface;
681 if (type->defined)
682 error_loc("pinterface %s already defined at %s:%d\n",
683 type->name, type->loc_info.input_name, type->loc_info.line_number);
685 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
686 * a new type GUID with the rules described in:
687 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
688 * TODO: store type signatures for generated interfaces, and generate their GUIDs
690 type->attrs = check_interface_attrs(type->name, attrs);
692 iface = type->details.parameterized.type;
693 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
694 iface->details.iface->disp_props = NULL;
695 iface->details.iface->disp_methods = NULL;
696 iface->details.iface->stmts = stmts;
697 iface->details.iface->inherit = inherit;
698 iface->details.iface->disp_inherit = NULL;
699 iface->details.iface->async_iface = NULL;
700 iface->details.iface->requires = requires;
702 type->defined = TRUE;
703 return type;
706 type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params)
708 type_t *new_type = duptype(type, 0);
709 new_type->details.parameterized.type = type;
710 new_type->details.parameterized.params = params;
711 return new_type;
714 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl);
716 static typeref_list_t *replace_type_parameters_in_type_list(typeref_list_t *list, typeref_list_t *orig, typeref_list_t *repl)
718 typeref_list_t *new_list = NULL;
719 typeref_t *ref;
721 if (!list) return list;
723 LIST_FOR_EACH_ENTRY(ref, list, typeref_t, entry)
725 type_t *new_type = replace_type_parameters_in_type(ref->type, orig, repl);
726 new_list = append_typeref(new_list, make_typeref(new_type));
729 return new_list;
732 static var_t *replace_type_parameters_in_var(var_t *var, typeref_list_t *orig, typeref_list_t *repl)
734 var_t *new_var = xmalloc(sizeof(*new_var));
735 *new_var = *var;
736 list_init(&new_var->entry);
737 new_var->declspec.type = replace_type_parameters_in_type(var->declspec.type, orig, repl);
738 return new_var;
741 static var_list_t *replace_type_parameters_in_var_list(var_list_t *var_list, typeref_list_t *orig, typeref_list_t *repl)
743 var_list_t *new_var_list;
744 var_t *var, *new_var;
746 if (!var_list) return var_list;
748 new_var_list = xmalloc(sizeof(*new_var_list));
749 list_init(new_var_list);
751 LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
753 new_var = replace_type_parameters_in_var(var, orig, repl);
754 list_add_tail(new_var_list, &new_var->entry);
757 return new_var_list;
760 static statement_t *replace_type_parameters_in_statement(statement_t *stmt, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
762 statement_t *new_stmt = xmalloc(sizeof(*new_stmt));
763 *new_stmt = *stmt;
764 list_init(&new_stmt->entry);
766 switch (stmt->type)
768 case STMT_DECLARATION:
769 new_stmt->u.var = replace_type_parameters_in_var(stmt->u.var, orig, repl);
770 break;
771 case STMT_TYPE:
772 case STMT_TYPEREF:
773 new_stmt->u.type = replace_type_parameters_in_type(stmt->u.type, orig, repl);
774 break;
775 case STMT_TYPEDEF:
776 new_stmt->u.type_list = replace_type_parameters_in_type_list(stmt->u.type_list, orig, repl);
777 break;
778 case STMT_MODULE:
779 case STMT_LIBRARY:
780 case STMT_IMPORT:
781 case STMT_IMPORTLIB:
782 case STMT_PRAGMA:
783 case STMT_CPPQUOTE:
784 error_loc_info(loc, "unimplemented parameterized type replacement for statement type %d.\n", stmt->type);
785 break;
788 return new_stmt;
791 static statement_list_t *replace_type_parameters_in_statement_list(statement_list_t *stmt_list, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
793 statement_list_t *new_stmt_list;
794 statement_t *stmt, *new_stmt;
796 if (!stmt_list) return stmt_list;
798 new_stmt_list = xmalloc(sizeof(*new_stmt_list));
799 list_init(new_stmt_list);
801 LIST_FOR_EACH_ENTRY(stmt, stmt_list, statement_t, entry)
803 new_stmt = replace_type_parameters_in_statement(stmt, orig, repl, loc);
804 list_add_tail(new_stmt_list, &new_stmt->entry);
807 return new_stmt_list;
810 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl)
812 struct list *o, *r;
813 type_t *t;
815 if (!type) return type;
816 switch (type->type_type)
818 case TYPE_VOID:
819 case TYPE_BASIC:
820 case TYPE_ENUM:
821 case TYPE_BITFIELD:
822 case TYPE_INTERFACE:
823 case TYPE_RUNTIMECLASS:
824 return type;
825 case TYPE_PARAMETER:
826 if (!orig || !repl) return NULL;
827 for (o = list_head(orig), r = list_head(repl); o && r;
828 o = list_next(orig, o), r = list_next(repl, r))
829 if (type == LIST_ENTRY(o, typeref_t, entry)->type)
830 return LIST_ENTRY(r, typeref_t, entry)->type;
831 return type;
832 case TYPE_POINTER:
833 t = replace_type_parameters_in_type(type->details.pointer.ref.type, orig, repl);
834 if (t == type->details.pointer.ref.type) return type;
835 type = duptype(type, 0);
836 type->details.pointer.ref.type = t;
837 return type;
838 case TYPE_ALIAS:
839 t = replace_type_parameters_in_type(type->details.alias.aliasee.type, orig, repl);
840 if (t == type->details.alias.aliasee.type) return type;
841 type = duptype(type, 0);
842 type->details.alias.aliasee.type = t;
843 return type;
844 case TYPE_ARRAY:
845 t = replace_type_parameters_in_type(type->details.array.elem.type, orig, repl);
846 if (t == t->details.array.elem.type) return type;
847 type = duptype(type, 0);
848 t->details.array.elem.type = t;
849 return type;
850 case TYPE_FUNCTION:
851 t = duptype(type, 0);
852 t->details.function = xmalloc(sizeof(*t->details.function));
853 t->details.function->args = replace_type_parameters_in_var_list(type->details.function->args, orig, repl);
854 t->details.function->retval = replace_type_parameters_in_var(type->details.function->retval, orig, repl);
855 return t;
856 case TYPE_PARAMETERIZED_TYPE:
857 t = type->details.parameterized.type;
858 if (t->type_type != TYPE_PARAMETERIZED_TYPE) return find_parameterized_type(type, repl);
859 repl = replace_type_parameters_in_type_list(type->details.parameterized.params, orig, repl);
860 return replace_type_parameters_in_type(t, t->details.parameterized.params, repl);
861 case TYPE_STRUCT:
862 case TYPE_ENCAPSULATED_UNION:
863 case TYPE_UNION:
864 case TYPE_MODULE:
865 case TYPE_COCLASS:
866 case TYPE_APICONTRACT:
867 error_loc_info(&type->loc_info, "unimplemented parameterized type replacement for type %s of type %d.\n", type->name, type->type_type);
868 break;
871 return type;
874 static void type_parameterized_interface_specialize(type_t *tmpl, type_t *iface, typeref_list_t *orig, typeref_list_t *repl)
876 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
877 iface->details.iface->disp_methods = NULL;
878 iface->details.iface->disp_props = NULL;
879 iface->details.iface->stmts = replace_type_parameters_in_statement_list(tmpl->details.iface->stmts, orig, repl, &tmpl->loc_info);
880 iface->details.iface->inherit = replace_type_parameters_in_type(tmpl->details.iface->inherit, orig, repl);
881 iface->details.iface->disp_inherit = NULL;
882 iface->details.iface->async_iface = NULL;
883 iface->details.iface->requires = NULL;
886 type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params)
888 type_t *tmpl = type->details.parameterized.type;
889 type_t *new_type = duptype(tmpl, 0);
891 new_type->namespace = type->namespace;
892 new_type->name = format_parameterized_type_name(type, params);
893 reg_type(new_type, new_type->name, new_type->namespace, 0);
894 new_type->c_name = format_parameterized_type_c_name(type, params);
896 return new_type;
899 type_t *type_parameterized_type_specialize_define(type_t *type)
901 type_t *tmpl = type->details.parameterized.type;
902 typeref_list_t *orig = tmpl->details.parameterized.params;
903 typeref_list_t *repl = type->details.parameterized.params;
904 type_t *iface = find_parameterized_type(tmpl, repl);
906 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE ||
907 type_get_type_detect_alias(tmpl) != TYPE_PARAMETERIZED_TYPE)
908 error_loc("cannot define non-parameterized type %s, declared at %s:%d\n",
909 type->name, type->loc_info.input_name, type->loc_info.line_number);
911 if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_INTERFACE &&
912 type_get_type_detect_alias(iface) == TYPE_INTERFACE)
913 type_parameterized_interface_specialize(tmpl->details.parameterized.type, iface, orig, repl);
914 else
915 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
916 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
918 iface->defined = TRUE;
919 compute_method_indexes(iface);
920 return iface;
923 int type_is_equal(const type_t *type1, const type_t *type2)
925 if (type1 == type2)
926 return TRUE;
927 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
928 return FALSE;
929 if (type1->namespace != type2->namespace)
930 return FALSE;
932 if (type1->name && type2->name)
933 return !strcmp(type1->name, type2->name);
934 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
935 return FALSE;
937 /* FIXME: do deep inspection of types to determine if they are equal */
939 return FALSE;