winegstreamer: Use a GstAtomicQueue for wg_transform output queue.
[wine.git] / tools / widl / typetree.c
blob800ddf772192099d825a9771ca8ad78f388f058f
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"
32 #include "hash.h"
34 type_t *duptype(type_t *t, int dupname)
36 type_t *d = alloc_type();
38 *d = *t;
39 if (dupname && t->name)
40 d->name = xstrdup(t->name);
42 return d;
45 type_t *make_type(enum type_type type)
47 type_t *t = alloc_type();
48 t->name = NULL;
49 t->namespace = NULL;
50 t->type_type = type;
51 t->attrs = NULL;
52 t->c_name = NULL;
53 t->signature = NULL;
54 t->qualified_name = NULL;
55 t->impl_name = NULL;
56 t->param_name = NULL;
57 t->short_name = NULL;
58 memset(&t->details, 0, sizeof(t->details));
59 t->typestring_offset = 0;
60 t->ptrdesc = 0;
61 t->ignore = (parse_only != 0);
62 t->defined = FALSE;
63 t->written = FALSE;
64 t->user_types_registered = FALSE;
65 t->tfswrite = FALSE;
66 t->checked = FALSE;
67 t->typelib_idx = -1;
68 init_loc_info(&t->loc_info);
69 return t;
72 static const var_t *find_arg(const var_list_t *args, const char *name)
74 const var_t *arg;
76 if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
78 if (arg->name && !strcmp(name, arg->name))
79 return arg;
82 return NULL;
85 const char *type_get_decl_name(const type_t *type, enum name_type name_type)
87 switch(name_type) {
88 case NAME_DEFAULT:
89 return type->name;
90 case NAME_C:
91 return type->c_name ? type->c_name : type->name;
94 assert(0);
95 return NULL;
98 const char *type_get_name(const type_t *type, enum name_type name_type)
100 switch(name_type) {
101 case NAME_DEFAULT:
102 return type->qualified_name ? type->qualified_name : type->name;
103 case NAME_C:
104 return type->c_name ? type->c_name : type->name;
107 assert(0);
108 return NULL;
111 static size_t append_namespace(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *separator, const char *abi_prefix)
113 int nested = namespace && !is_global_namespace(namespace);
114 const char *name = nested ? namespace->name : abi_prefix;
115 size_t n = 0;
116 if (!name) return 0;
117 if (nested) n += append_namespace(buf, len, pos + n, namespace->parent, separator, abi_prefix);
118 n += strappend(buf, len, pos + n, "%s%s", name, separator);
119 return n;
122 static size_t append_namespaces(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *prefix,
123 const char *separator, const char *suffix, const char *abi_prefix)
125 int nested = namespace && !is_global_namespace(namespace);
126 size_t n = 0;
127 n += strappend(buf, len, pos + n, "%s", prefix);
128 if (nested) n += append_namespace(buf, len, pos + n, namespace, separator, abi_prefix);
129 if (suffix) n += strappend(buf, len, pos + n, "%s", suffix);
130 else if (nested)
132 n -= strlen(separator);
133 (*buf)[n] = 0;
135 return n;
138 static size_t append_pointer_stars(char **buf, size_t *len, size_t pos, type_t *type)
140 size_t n = 0;
141 for (; type && type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) n += strappend(buf, len, pos + n, "*");
142 return n;
145 static size_t append_type_signature(char **buf, size_t *len, size_t pos, type_t *type);
147 static size_t append_var_list_signature(char **buf, size_t *len, size_t pos, var_list_t *var_list)
149 var_t *var;
150 size_t n = 0;
152 if (!var_list) n += strappend(buf, len, pos + n, ";");
153 else LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
155 n += strappend(buf, len, pos + n, ";");
156 n += append_type_signature(buf, len, pos + n, var->declspec.type);
159 return n;
162 static size_t append_type_signature(char **buf, size_t *len, size_t pos, type_t *type)
164 const struct uuid *uuid;
165 size_t n = 0;
167 if (!type) return 0;
168 switch (type->type_type)
170 case TYPE_INTERFACE:
171 if (type->signature) n += strappend(buf, len, pos + n, "%s", type->signature);
172 else
174 if (!(uuid = get_attrp(type->attrs, ATTR_UUID)))
175 error_loc_info(&type->loc_info, "cannot compute type signature, no uuid found for type %s.\n", type->name);
177 n += strappend(buf, len, pos + n, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
178 uuid->Data1, uuid->Data2, uuid->Data3,
179 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
180 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
182 return n;
183 case TYPE_DELEGATE:
184 n += strappend(buf, len, pos + n, "delegate(");
185 n += append_type_signature(buf, len, pos + n, type_delegate_get_iface(type));
186 n += strappend(buf, len, pos + n, ")");
187 return n;
188 case TYPE_RUNTIMECLASS:
189 n += strappend(buf, len, pos + n, "rc(");
190 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
191 n += strappend(buf, len, pos + n, ";");
192 n += append_type_signature(buf, len, pos + n, type_runtimeclass_get_default_iface(type, TRUE));
193 n += strappend(buf, len, pos + n, ")");
194 return n;
195 case TYPE_POINTER:
196 n += append_type_signature(buf, len, pos + n, type->details.pointer.ref.type);
197 return n;
198 case TYPE_ALIAS:
199 if (!strcmp(type->name, "boolean")) n += strappend(buf, len, pos + n, "b1");
200 else if (!strcmp(type->name, "HSTRING")) n += strappend(buf, len, pos + n, "string");
201 else n += append_type_signature(buf, len, pos + n, type->details.alias.aliasee.type);
202 return n;
203 case TYPE_STRUCT:
204 n += strappend(buf, len, pos + n, "struct(");
205 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
206 n += append_var_list_signature(buf, len, pos + n, type->details.structure->fields);
207 n += strappend(buf, len, pos + n, ")");
208 return n;
209 case TYPE_BASIC:
210 switch (type_basic_get_type(type))
212 case TYPE_BASIC_INT:
213 case TYPE_BASIC_INT32:
214 n += strappend(buf, len, pos + n, type_basic_get_sign(type) <= 0 ? "i4" : "u4");
215 return n;
216 case TYPE_BASIC_INT64:
217 n += strappend(buf, len, pos + n, type_basic_get_sign(type) <= 0 ? "i8" : "u8");
218 return n;
219 case TYPE_BASIC_INT8:
220 assert(type_basic_get_sign(type) > 0); /* signature string for signed char isn't specified */
221 n += strappend(buf, len, pos + n, "u1");
222 return n;
223 case TYPE_BASIC_FLOAT:
224 n += strappend(buf, len, pos + n, "f4");
225 return n;
226 case TYPE_BASIC_DOUBLE:
227 n += strappend(buf, len, pos + n, "f8");
228 return n;
229 case TYPE_BASIC_BYTE:
230 n += strappend(buf, len, pos + n, "u1");
231 return n;
232 case TYPE_BASIC_INT16:
233 case TYPE_BASIC_INT3264:
234 case TYPE_BASIC_LONG:
235 case TYPE_BASIC_CHAR:
236 case TYPE_BASIC_HYPER:
237 case TYPE_BASIC_WCHAR:
238 case TYPE_BASIC_ERROR_STATUS_T:
239 case TYPE_BASIC_HANDLE:
240 error_loc_info(&type->loc_info, "unimplemented type signature for basic type %d.\n", type_basic_get_type(type));
241 break;
243 case TYPE_ENUM:
244 n += strappend(buf, len, pos + n, "enum(");
245 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
246 if (is_attr(type->attrs, ATTR_FLAGS)) n += strappend(buf, len, pos + n, ";u4");
247 else n += strappend(buf, len, pos + n, ";i4");
248 n += strappend(buf, len, pos + n, ")");
249 return n;
250 case TYPE_ARRAY:
251 case TYPE_ENCAPSULATED_UNION:
252 case TYPE_UNION:
253 case TYPE_COCLASS:
254 case TYPE_VOID:
255 case TYPE_FUNCTION:
256 case TYPE_BITFIELD:
257 case TYPE_MODULE:
258 case TYPE_APICONTRACT:
259 error_loc_info(&type->loc_info, "unimplemented type signature for type %s of type %d.\n", type->name, type->type_type);
260 break;
261 case TYPE_PARAMETERIZED_TYPE:
262 case TYPE_PARAMETER:
263 assert(0); /* should not be there */
264 break;
267 return n;
270 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix, const char *abi_prefix)
272 size_t len = 0;
273 char *buf = NULL;
274 append_namespaces(&buf, &len, 0, namespace, prefix, separator, suffix, abi_prefix);
275 return buf;
278 char *format_parameterized_type_name(type_t *type, typeref_list_t *params)
280 size_t len = 0, pos = 0;
281 char *buf = NULL;
282 typeref_t *ref;
284 pos += strappend(&buf, &len, pos, "%s<", type->name);
285 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
287 type = type_pointer_get_root_type(ref->type);
288 pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
289 pos += append_pointer_stars(&buf, &len, pos, ref->type);
290 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ",");
292 pos += strappend(&buf, &len, pos, " >");
294 return buf;
297 static char const *parameterized_type_shorthands[][2] = {
298 {"Windows__CFoundation__CCollections__C", "__F"},
299 {"Windows_CFoundation_CCollections_C", "__F"},
300 {"Windows__CFoundation__C", "__F"},
301 {"Windows_CFoundation_C", "__F"},
304 static char *format_parameterized_type_c_name(type_t *type, typeref_list_t *params, const char *prefix, const char *separator)
306 const char *tmp, *ns_prefix = "__x_", *abi_prefix = NULL;
307 size_t len = 0, pos = 0;
308 char *buf = NULL;
309 int i, count = params ? list_count(params) : 0;
310 typeref_t *ref;
312 if (!strcmp(separator, "__C")) ns_prefix = "_C";
313 else if (use_abi_namespace) abi_prefix = "ABI";
315 pos += append_namespaces(&buf, &len, pos, type->namespace, ns_prefix, separator, "", abi_prefix);
316 pos += strappend(&buf, &len, pos, "%s%s_%d", prefix, type->name, count);
317 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
319 type = type_pointer_get_root_type(ref->type);
320 if ((tmp = type->param_name)) pos += strappend(&buf, &len, pos, "_%s", tmp);
321 else pos += append_namespaces(&buf, &len, pos, type->namespace, "_", "__C", type->name, NULL);
324 for (i = 0; i < ARRAY_SIZE(parameterized_type_shorthands); ++i)
326 if ((tmp = strstr(buf, parameterized_type_shorthands[i][0])) &&
327 (tmp - buf) == strlen(ns_prefix) + (abi_prefix ? 5 : 0))
329 tmp += strlen(parameterized_type_shorthands[i][0]);
330 strcpy(buf, parameterized_type_shorthands[i][1]);
331 memmove(buf + 3, tmp, len - (tmp - buf));
335 return buf;
338 static char *format_parameterized_type_signature(type_t *type, typeref_list_t *params)
340 size_t len = 0, pos = 0;
341 char *buf = NULL;
342 typeref_t *ref;
343 const struct uuid *uuid;
345 if (!(uuid = get_attrp(type->attrs, ATTR_UUID)))
346 error_loc_info(&type->loc_info, "cannot compute type signature, no uuid found for type %s.\n", type->name);
348 pos += strappend(&buf, &len, pos, "pinterface({%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
349 uuid->Data1, uuid->Data2, uuid->Data3,
350 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
351 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
352 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
354 pos += strappend(&buf, &len, pos, ";");
355 pos += append_type_signature(&buf, &len, pos, ref->type);
357 pos += strappend(&buf, &len, pos, ")");
359 return buf;
362 static char *format_parameterized_type_short_name(type_t *type, typeref_list_t *params, const char *prefix)
364 size_t len = 0, pos = 0;
365 char *buf = NULL;
366 typeref_t *ref;
368 pos += strappend(&buf, &len, pos, "%s%s", prefix, type->name);
369 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
371 type = type_pointer_get_root_type(ref->type);
372 if (type->short_name) pos += strappend(&buf, &len, pos, "_%s", type->short_name);
373 else pos += strappend(&buf, &len, pos, "_%s", type->name);
376 return buf;
379 static char *format_parameterized_type_impl_name(type_t *type, typeref_list_t *params, const char *prefix)
381 size_t len = 0, pos = 0;
382 char *buf = NULL;
383 typeref_t *ref;
384 type_t *iface;
386 pos += strappend(&buf, &len, pos, "%s%s_impl<", prefix, type->name);
387 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
389 type = type_pointer_get_root_type(ref->type);
390 if (type->type_type == TYPE_RUNTIMECLASS)
392 pos += strappend(&buf, &len, pos, "ABI::Windows::Foundation::Internal::AggregateType<%s", type->qualified_name);
393 pos += append_pointer_stars(&buf, &len, pos, ref->type);
394 iface = type_runtimeclass_get_default_iface(type, TRUE);
395 pos += strappend(&buf, &len, pos, ", %s", iface->qualified_name);
396 pos += append_pointer_stars(&buf, &len, pos, ref->type);
397 pos += strappend(&buf, &len, pos, " >");
399 else
401 pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
402 pos += append_pointer_stars(&buf, &len, pos, ref->type);
404 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
406 pos += strappend(&buf, &len, pos, " >");
408 return buf;
411 type_t *type_new_function(var_list_t *args)
413 var_t *arg;
414 type_t *t;
415 unsigned int i = 0;
417 if (args)
419 arg = LIST_ENTRY(list_head(args), var_t, entry);
420 if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
422 list_remove(&arg->entry);
423 free(arg);
424 free(args);
425 args = NULL;
428 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
430 if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
431 error_loc("argument '%s' has void type\n", arg->name);
432 if (!arg->name)
434 if (i > 26 * 26)
435 error_loc("too many unnamed arguments\n");
436 else
438 int unique;
441 char name[3];
442 name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
443 name[1] = i > 26 ? 'a' + i % 26 : 0;
444 name[2] = 0;
445 unique = !find_arg(args, name);
446 if (unique)
447 arg->name = xstrdup(name);
448 i++;
449 } while (!unique);
454 t = make_type(TYPE_FUNCTION);
455 t->details.function = xmalloc(sizeof(*t->details.function));
456 t->details.function->args = args;
457 t->details.function->retval = make_var(xstrdup("_RetVal"));
458 return t;
461 type_t *type_new_pointer(type_t *ref)
463 type_t *t = make_type(TYPE_POINTER);
464 t->details.pointer.ref.type = ref;
465 return t;
468 type_t *type_new_alias(const decl_spec_t *t, const char *name)
470 type_t *a = make_type(TYPE_ALIAS);
472 a->name = xstrdup(name);
473 a->attrs = NULL;
474 a->details.alias.aliasee = *t;
475 init_loc_info(&a->loc_info);
477 return a;
480 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
481 unsigned int dim, expr_t *size_is, expr_t *length_is)
483 type_t *t = make_type(TYPE_ARRAY);
484 if (name) t->name = xstrdup(name);
485 t->details.array.declptr = declptr;
486 t->details.array.length_is = length_is;
487 if (size_is)
488 t->details.array.size_is = size_is;
489 else
490 t->details.array.dim = dim;
491 if (element)
492 t->details.array.elem = *element;
493 return t;
496 type_t *type_new_basic(enum type_basic_type basic_type)
498 type_t *t = make_type(TYPE_BASIC);
499 t->details.basic.type = basic_type;
500 t->details.basic.sign = 0;
501 return t;
504 type_t *type_new_int(enum type_basic_type basic_type, int sign)
506 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
508 assert(basic_type <= TYPE_BASIC_INT_MAX);
510 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
511 if (!int_types[basic_type][sign + 1])
513 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
514 int_types[basic_type][sign + 1]->details.basic.sign = sign;
516 return int_types[basic_type][sign + 1];
519 type_t *type_new_void(void)
521 static type_t *void_type = NULL;
522 if (!void_type)
523 void_type = make_type(TYPE_VOID);
524 return void_type;
527 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
529 type_t *t = NULL;
531 if (name)
532 t = find_type(name, namespace,tsENUM);
534 if (!t)
536 t = make_type(TYPE_ENUM);
537 t->name = name;
538 t->namespace = namespace;
539 if (name)
540 reg_type(t, name, namespace, tsENUM);
543 if (!t->defined && defined)
545 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
546 t->details.enumeration->enums = enums;
547 t->defined = TRUE;
549 else if (defined)
550 error_loc("redefinition of enum %s\n", name);
552 return t;
555 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
557 type_t *t = NULL;
559 if (name)
560 t = find_type(name, namespace, tsSTRUCT);
562 if (!t)
564 t = make_type(TYPE_STRUCT);
565 t->name = name;
566 t->namespace = namespace;
567 if (name)
568 reg_type(t, name, namespace, tsSTRUCT);
571 if (!t->defined && defined)
573 t->details.structure = xmalloc(sizeof(*t->details.structure));
574 t->details.structure->fields = fields;
575 t->defined = TRUE;
577 else if (defined)
578 error_loc("redefinition of struct %s\n", name);
580 return t;
583 type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
585 type_t *t = NULL;
587 if (name)
588 t = find_type(name, NULL, tsUNION);
590 if (!t)
592 t = make_type(TYPE_UNION);
593 t->name = name;
594 if (name)
595 reg_type(t, name, NULL, tsUNION);
598 if (!t->defined && defined)
600 t->details.structure = xmalloc(sizeof(*t->details.structure));
601 t->details.structure->fields = fields;
602 t->defined = TRUE;
604 else if (defined)
605 error_loc("redefinition of union %s\n", name);
607 return t;
610 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
612 type_t *t = NULL;
614 if (name)
615 t = find_type(name, NULL, tsUNION);
617 if (!t)
619 t = make_type(TYPE_ENCAPSULATED_UNION);
620 t->name = name;
621 if (name)
622 reg_type(t, name, NULL, tsUNION);
624 t->type_type = TYPE_ENCAPSULATED_UNION;
626 if (!t->defined)
628 if (!union_field)
629 union_field = make_var(xstrdup("tagged_union"));
630 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
632 t->details.structure = xmalloc(sizeof(*t->details.structure));
633 t->details.structure->fields = append_var(NULL, switch_field);
634 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
635 t->defined = TRUE;
637 else
638 error_loc("redefinition of union %s\n", name);
640 return t;
643 static int is_valid_bitfield_type(const type_t *type)
645 switch (type_get_type(type))
647 case TYPE_ENUM:
648 return TRUE;
649 case TYPE_BASIC:
650 switch (type_basic_get_type(type))
652 case TYPE_BASIC_INT8:
653 case TYPE_BASIC_INT16:
654 case TYPE_BASIC_INT32:
655 case TYPE_BASIC_INT64:
656 case TYPE_BASIC_INT:
657 case TYPE_BASIC_INT3264:
658 case TYPE_BASIC_LONG:
659 case TYPE_BASIC_CHAR:
660 case TYPE_BASIC_HYPER:
661 case TYPE_BASIC_BYTE:
662 case TYPE_BASIC_WCHAR:
663 case TYPE_BASIC_ERROR_STATUS_T:
664 return TRUE;
665 case TYPE_BASIC_FLOAT:
666 case TYPE_BASIC_DOUBLE:
667 case TYPE_BASIC_HANDLE:
668 return FALSE;
670 return FALSE;
671 default:
672 return FALSE;
676 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
678 type_t *t;
680 if (!is_valid_bitfield_type(field))
681 error_loc("bit-field has invalid type\n");
683 if (bits->cval < 0)
684 error_loc("negative width for bit-field\n");
686 /* FIXME: validate bits->cval <= memsize(field) * 8 */
688 t = make_type(TYPE_BITFIELD);
689 t->details.bitfield.field = field;
690 t->details.bitfield.bits = bits;
691 return t;
694 static unsigned int compute_method_indexes(type_t *iface)
696 unsigned int idx;
697 statement_t *stmt;
699 if (!iface->details.iface)
700 return 0;
702 if (type_iface_get_inherit(iface))
703 idx = compute_method_indexes(type_iface_get_inherit(iface));
704 else
705 idx = 0;
707 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
709 var_t *func = stmt->u.var;
710 if (!is_callas(func->attrs))
711 func->func_idx = idx++;
714 return idx;
717 type_t *type_interface_declare(char *name, struct namespace *namespace)
719 type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
720 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
721 error_loc("interface %s previously not declared an interface at %s:%d\n",
722 type->name, type->loc_info.input_name, type->loc_info.line_number);
723 return type;
726 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
728 if (iface->defined)
729 error_loc("interface %s already defined at %s:%d\n",
730 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
731 if (iface == inherit)
732 error_loc("interface %s can't inherit from itself\n",
733 iface->name);
734 iface->attrs = check_interface_attrs(iface->name, attrs);
735 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
736 iface->details.iface->disp_props = NULL;
737 iface->details.iface->disp_methods = NULL;
738 iface->details.iface->stmts = stmts;
739 iface->details.iface->inherit = inherit;
740 iface->details.iface->disp_inherit = NULL;
741 iface->details.iface->async_iface = NULL;
742 iface->details.iface->requires = requires;
743 iface->defined = TRUE;
744 compute_method_indexes(iface);
745 return iface;
748 type_t *type_dispinterface_declare(char *name)
750 type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
751 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
752 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
753 type->name, type->loc_info.input_name, type->loc_info.line_number);
754 return type;
757 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
759 if (iface->defined)
760 error_loc("dispinterface %s already defined at %s:%d\n",
761 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
762 iface->attrs = check_dispiface_attrs(iface->name, attrs);
763 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
764 iface->details.iface->disp_props = props;
765 iface->details.iface->disp_methods = methods;
766 iface->details.iface->stmts = NULL;
767 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
768 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
769 iface->details.iface->disp_inherit = NULL;
770 iface->details.iface->async_iface = NULL;
771 iface->details.iface->requires = NULL;
772 iface->defined = TRUE;
773 compute_method_indexes(iface);
774 return iface;
777 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
779 if (dispiface->defined)
780 error_loc("dispinterface %s already defined at %s:%d\n",
781 dispiface->name, dispiface->loc_info.input_name, dispiface->loc_info.line_number);
782 dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
783 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
784 dispiface->details.iface->disp_props = NULL;
785 dispiface->details.iface->disp_methods = NULL;
786 dispiface->details.iface->stmts = NULL;
787 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
788 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
789 dispiface->details.iface->disp_inherit = iface;
790 dispiface->details.iface->async_iface = NULL;
791 dispiface->details.iface->requires = NULL;
792 dispiface->defined = TRUE;
793 compute_method_indexes(dispiface);
794 return dispiface;
797 type_t *type_module_declare(char *name)
799 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
800 if (type_get_type_detect_alias(type) != TYPE_MODULE)
801 error_loc("module %s previously not declared a module at %s:%d\n",
802 type->name, type->loc_info.input_name, type->loc_info.line_number);
803 return type;
806 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
808 if (module->defined)
809 error_loc("module %s already defined at %s:%d\n",
810 module->name, module->loc_info.input_name, module->loc_info.line_number);
811 module->attrs = check_module_attrs(module->name, attrs);
812 module->details.module = xmalloc(sizeof(*module->details.module));
813 module->details.module->stmts = stmts;
814 module->defined = TRUE;
815 return module;
818 type_t *type_coclass_declare(char *name)
820 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
821 if (type_get_type_detect_alias(type) != TYPE_COCLASS)
822 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
823 type->name, type->loc_info.input_name, type->loc_info.line_number);
824 return type;
827 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces)
829 if (coclass->defined)
830 error_loc("coclass %s already defined at %s:%d\n",
831 coclass->name, coclass->loc_info.input_name, coclass->loc_info.line_number);
832 coclass->attrs = check_coclass_attrs(coclass->name, attrs);
833 coclass->details.coclass.ifaces = ifaces;
834 coclass->defined = TRUE;
835 return coclass;
838 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
840 type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
841 if (type_get_type_detect_alias(type) != TYPE_RUNTIMECLASS)
842 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
843 type->name, type->loc_info.input_name, type->loc_info.line_number);
844 return type;
847 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces)
849 typeref_t *ref, *required, *tmp;
850 typeref_list_t *requires;
852 if (runtimeclass->defined)
853 error_loc("runtimeclass %s already defined at %s:%d\n",
854 runtimeclass->name, runtimeclass->loc_info.input_name, runtimeclass->loc_info.line_number);
855 runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
856 runtimeclass->details.runtimeclass.ifaces = ifaces;
857 runtimeclass->defined = TRUE;
858 if (!type_runtimeclass_get_default_iface(runtimeclass, FALSE) &&
859 !get_attrp(runtimeclass->attrs, ATTR_STATIC))
860 error_loc("runtimeclass %s must have a default interface or static factory\n", runtimeclass->name);
862 if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
864 /* FIXME: this should probably not be allowed, here or in coclass, */
865 /* but for now there's too many places in Wine IDL where it is to */
866 /* even print a warning. */
867 if (!(ref->type->defined)) continue;
868 if (!(requires = type_iface_get_requires(ref->type))) continue;
869 LIST_FOR_EACH_ENTRY(required, requires, typeref_t, entry)
871 int found = 0;
873 LIST_FOR_EACH_ENTRY(tmp, ifaces, typeref_t, entry)
874 if ((found = type_is_equal(tmp->type, required->type))) break;
876 if (!found)
877 error_loc("interface '%s' also requires interface '%s', "
878 "but runtimeclass '%s' does not implement it.\n",
879 ref->type->name, required->type->name, runtimeclass->name);
883 return runtimeclass;
886 type_t *type_apicontract_declare(char *name, struct namespace *namespace)
888 type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
889 if (type_get_type_detect_alias(type) != TYPE_APICONTRACT)
890 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
891 type->name, type->loc_info.input_name, type->loc_info.line_number);
892 return type;
895 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
897 if (apicontract->defined)
898 error_loc("apicontract %s already defined at %s:%d\n",
899 apicontract->name, apicontract->loc_info.input_name, apicontract->loc_info.line_number);
900 apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
901 apicontract->defined = TRUE;
902 return apicontract;
905 static void compute_delegate_iface_names(type_t *delegate, type_t *type, typeref_list_t *params)
907 type_t *iface = delegate->details.delegate.iface;
908 iface->namespace = delegate->namespace;
909 iface->name = strmake("I%s", delegate->name);
910 if (type) iface->c_name = format_parameterized_type_c_name(type, params, "I", "_C");
911 else iface->c_name = format_namespace(delegate->namespace, "__x_", "_C", iface->name, use_abi_namespace ? "ABI" : NULL);
912 if (type) iface->param_name = format_parameterized_type_c_name(type, params, "I", "__C");
913 else iface->param_name = format_namespace(delegate->namespace, "_", "__C", iface->name, NULL);
914 iface->qualified_name = format_namespace(delegate->namespace, "", "::", iface->name, use_abi_namespace ? "ABI" : NULL);
917 type_t *type_delegate_declare(char *name, struct namespace *namespace)
919 type_t *type = get_type(TYPE_DELEGATE, name, namespace, 0);
920 if (type_get_type_detect_alias(type) != TYPE_DELEGATE)
921 error_loc("delegate %s previously not declared a delegate at %s:%d\n",
922 type->name, type->loc_info.input_name, type->loc_info.line_number);
923 return type;
926 type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts)
928 type_t *iface;
930 if (delegate->defined)
931 error_loc("delegate %s already defined at %s:%d\n",
932 delegate->name, delegate->loc_info.input_name, delegate->loc_info.line_number);
934 delegate->attrs = check_interface_attrs(delegate->name, attrs);
936 iface = make_type(TYPE_INTERFACE);
937 iface->attrs = delegate->attrs;
938 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
939 iface->details.iface->disp_props = NULL;
940 iface->details.iface->disp_methods = NULL;
941 iface->details.iface->stmts = stmts;
942 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
943 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
944 iface->details.iface->disp_inherit = NULL;
945 iface->details.iface->async_iface = NULL;
946 iface->details.iface->requires = NULL;
947 iface->defined = TRUE;
948 compute_method_indexes(iface);
950 delegate->details.delegate.iface = iface;
951 delegate->defined = TRUE;
952 compute_delegate_iface_names(delegate, NULL, NULL);
954 return delegate;
957 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params)
959 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
960 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
961 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
962 type->name, type->loc_info.input_name, type->loc_info.line_number);
963 type->details.parameterized.type = make_type(TYPE_INTERFACE);
964 type->details.parameterized.params = params;
965 return type;
968 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
970 type_t *iface;
971 if (type->defined)
972 error_loc("pinterface %s already defined at %s:%d\n",
973 type->name, type->loc_info.input_name, type->loc_info.line_number);
975 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
976 * a new type GUID with the rules described in:
977 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
978 * TODO: store type signatures for generated interfaces, and generate their GUIDs
980 type->attrs = check_interface_attrs(type->name, attrs);
982 iface = type->details.parameterized.type;
983 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
984 iface->details.iface->disp_props = NULL;
985 iface->details.iface->disp_methods = NULL;
986 iface->details.iface->stmts = stmts;
987 iface->details.iface->inherit = inherit;
988 iface->details.iface->disp_inherit = NULL;
989 iface->details.iface->async_iface = NULL;
990 iface->details.iface->requires = requires;
992 iface->name = type->name;
994 type->defined = TRUE;
995 return type;
998 type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params)
1000 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
1001 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
1002 error_loc("pdelegate %s previously not declared a pdelegate at %s:%d\n",
1003 type->name, type->loc_info.input_name, type->loc_info.line_number);
1004 type->details.parameterized.type = make_type(TYPE_DELEGATE);
1005 type->details.parameterized.params = params;
1006 return type;
1009 type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts)
1011 type_t *iface, *delegate;
1013 if (type->defined)
1014 error_loc("pdelegate %s already defined at %s:%d\n",
1015 type->name, type->loc_info.input_name, type->loc_info.line_number);
1017 type->attrs = check_interface_attrs(type->name, attrs);
1019 delegate = type->details.parameterized.type;
1020 delegate->attrs = type->attrs;
1021 delegate->details.delegate.iface = make_type(TYPE_INTERFACE);
1023 iface = delegate->details.delegate.iface;
1024 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1025 iface->details.iface->disp_props = NULL;
1026 iface->details.iface->disp_methods = NULL;
1027 iface->details.iface->stmts = stmts;
1028 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
1029 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
1030 iface->details.iface->disp_inherit = NULL;
1031 iface->details.iface->async_iface = NULL;
1032 iface->details.iface->requires = NULL;
1034 delegate->name = type->name;
1035 compute_delegate_iface_names(delegate, type, type->details.parameterized.params);
1037 type->defined = TRUE;
1038 return type;
1041 type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params)
1043 type_t *new_type = duptype(type, 0);
1044 new_type->details.parameterized.type = type;
1045 new_type->details.parameterized.params = params;
1046 return new_type;
1049 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl);
1051 static typeref_list_t *replace_type_parameters_in_type_list(typeref_list_t *list, typeref_list_t *orig, typeref_list_t *repl)
1053 typeref_list_t *new_list = NULL;
1054 typeref_t *ref;
1056 if (!list) return list;
1058 LIST_FOR_EACH_ENTRY(ref, list, typeref_t, entry)
1060 type_t *new_type = replace_type_parameters_in_type(ref->type, orig, repl);
1061 new_list = append_typeref(new_list, make_typeref(new_type));
1064 return new_list;
1067 static var_t *replace_type_parameters_in_var(var_t *var, typeref_list_t *orig, typeref_list_t *repl)
1069 var_t *new_var = xmalloc(sizeof(*new_var));
1070 *new_var = *var;
1071 list_init(&new_var->entry);
1072 new_var->declspec.type = replace_type_parameters_in_type(var->declspec.type, orig, repl);
1073 return new_var;
1076 static var_list_t *replace_type_parameters_in_var_list(var_list_t *var_list, typeref_list_t *orig, typeref_list_t *repl)
1078 var_list_t *new_var_list;
1079 var_t *var, *new_var;
1081 if (!var_list) return var_list;
1083 new_var_list = xmalloc(sizeof(*new_var_list));
1084 list_init(new_var_list);
1086 LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
1088 new_var = replace_type_parameters_in_var(var, orig, repl);
1089 list_add_tail(new_var_list, &new_var->entry);
1092 return new_var_list;
1095 static statement_t *replace_type_parameters_in_statement(statement_t *stmt, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
1097 statement_t *new_stmt = xmalloc(sizeof(*new_stmt));
1098 *new_stmt = *stmt;
1099 list_init(&new_stmt->entry);
1101 switch (stmt->type)
1103 case STMT_DECLARATION:
1104 new_stmt->u.var = replace_type_parameters_in_var(stmt->u.var, orig, repl);
1105 break;
1106 case STMT_TYPE:
1107 case STMT_TYPEREF:
1108 new_stmt->u.type = replace_type_parameters_in_type(stmt->u.type, orig, repl);
1109 break;
1110 case STMT_TYPEDEF:
1111 new_stmt->u.type_list = replace_type_parameters_in_type_list(stmt->u.type_list, orig, repl);
1112 break;
1113 case STMT_MODULE:
1114 case STMT_LIBRARY:
1115 case STMT_IMPORT:
1116 case STMT_IMPORTLIB:
1117 case STMT_PRAGMA:
1118 case STMT_CPPQUOTE:
1119 error_loc_info(loc, "unimplemented parameterized type replacement for statement type %d.\n", stmt->type);
1120 break;
1123 return new_stmt;
1126 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)
1128 statement_list_t *new_stmt_list;
1129 statement_t *stmt, *new_stmt;
1131 if (!stmt_list) return stmt_list;
1133 new_stmt_list = xmalloc(sizeof(*new_stmt_list));
1134 list_init(new_stmt_list);
1136 LIST_FOR_EACH_ENTRY(stmt, stmt_list, statement_t, entry)
1138 new_stmt = replace_type_parameters_in_statement(stmt, orig, repl, loc);
1139 list_add_tail(new_stmt_list, &new_stmt->entry);
1142 return new_stmt_list;
1145 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl)
1147 struct list *o, *r;
1148 type_t *t;
1150 if (!type) return type;
1151 switch (type->type_type)
1153 case TYPE_VOID:
1154 case TYPE_BASIC:
1155 case TYPE_ENUM:
1156 case TYPE_BITFIELD:
1157 case TYPE_INTERFACE:
1158 case TYPE_RUNTIMECLASS:
1159 case TYPE_DELEGATE:
1160 return type;
1161 case TYPE_PARAMETER:
1162 if (!orig || !repl) return NULL;
1163 for (o = list_head(orig), r = list_head(repl); o && r;
1164 o = list_next(orig, o), r = list_next(repl, r))
1165 if (type == LIST_ENTRY(o, typeref_t, entry)->type)
1166 return LIST_ENTRY(r, typeref_t, entry)->type;
1167 return type;
1168 case TYPE_POINTER:
1169 t = replace_type_parameters_in_type(type->details.pointer.ref.type, orig, repl);
1170 if (t == type->details.pointer.ref.type) return type;
1171 type = duptype(type, 0);
1172 type->details.pointer.ref.type = t;
1173 return type;
1174 case TYPE_ALIAS:
1175 t = replace_type_parameters_in_type(type->details.alias.aliasee.type, orig, repl);
1176 if (t == type->details.alias.aliasee.type) return type;
1177 type = duptype(type, 0);
1178 type->details.alias.aliasee.type = t;
1179 return type;
1180 case TYPE_ARRAY:
1181 t = replace_type_parameters_in_type(type->details.array.elem.type, orig, repl);
1182 if (t == t->details.array.elem.type) return type;
1183 type = duptype(type, 0);
1184 t->details.array.elem.type = t;
1185 return type;
1186 case TYPE_FUNCTION:
1187 t = duptype(type, 0);
1188 t->details.function = xmalloc(sizeof(*t->details.function));
1189 t->details.function->args = replace_type_parameters_in_var_list(type->details.function->args, orig, repl);
1190 t->details.function->retval = replace_type_parameters_in_var(type->details.function->retval, orig, repl);
1191 return t;
1192 case TYPE_PARAMETERIZED_TYPE:
1193 t = type->details.parameterized.type;
1194 if (t->type_type != TYPE_PARAMETERIZED_TYPE) return find_parameterized_type(type, repl);
1195 repl = replace_type_parameters_in_type_list(type->details.parameterized.params, orig, repl);
1196 return replace_type_parameters_in_type(t, t->details.parameterized.params, repl);
1197 case TYPE_STRUCT:
1198 case TYPE_ENCAPSULATED_UNION:
1199 case TYPE_UNION:
1200 case TYPE_MODULE:
1201 case TYPE_COCLASS:
1202 case TYPE_APICONTRACT:
1203 error_loc_info(&type->loc_info, "unimplemented parameterized type replacement for type %s of type %d.\n", type->name, type->type_type);
1204 break;
1207 return type;
1210 static void type_parameterized_interface_specialize(type_t *tmpl, type_t *iface, typeref_list_t *orig, typeref_list_t *repl)
1212 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1213 iface->details.iface->disp_methods = NULL;
1214 iface->details.iface->disp_props = NULL;
1215 iface->details.iface->stmts = replace_type_parameters_in_statement_list(tmpl->details.iface->stmts, orig, repl, &tmpl->loc_info);
1216 iface->details.iface->inherit = replace_type_parameters_in_type(tmpl->details.iface->inherit, orig, repl);
1217 iface->details.iface->disp_inherit = NULL;
1218 iface->details.iface->async_iface = NULL;
1219 iface->details.iface->requires = NULL;
1222 static void type_parameterized_delegate_specialize(type_t *tmpl, type_t *delegate, typeref_list_t *orig, typeref_list_t *repl)
1224 type_parameterized_interface_specialize(tmpl->details.delegate.iface, delegate->details.delegate.iface, orig, repl);
1227 type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params)
1229 type_t *tmpl = type->details.parameterized.type;
1230 type_t *new_type = duptype(tmpl, 0);
1232 new_type->namespace = type->namespace;
1233 new_type->name = format_parameterized_type_name(type, params);
1234 reg_type(new_type, new_type->name, new_type->namespace, 0);
1235 new_type->c_name = format_parameterized_type_c_name(type, params, "", "_C");
1236 new_type->short_name = format_parameterized_type_short_name(type, params, "");
1237 new_type->param_name = format_parameterized_type_c_name(type, params, "", "__C");
1239 if (new_type->type_type == TYPE_DELEGATE)
1241 new_type->details.delegate.iface = duptype(tmpl->details.delegate.iface, 0);
1242 compute_delegate_iface_names(new_type, type, params);
1243 new_type->details.delegate.iface->short_name = format_parameterized_type_short_name(type, params, "I");
1246 return new_type;
1249 static void compute_interface_signature_uuid(type_t *iface)
1251 static const char winrt_pinterface_namespace[] = {0x11,0xf4,0x7a,0xd5,0x7b,0x73,0x42,0xc0,0xab,0xae,0x87,0x8b,0x1e,0x16,0xad,0xee};
1252 static const int version = 5;
1253 struct sha1_context ctx;
1254 unsigned char hash[20];
1255 struct uuid *uuid;
1257 if (!(uuid = get_attrp(iface->attrs, ATTR_UUID)))
1259 uuid = xmalloc(sizeof(*uuid));
1260 iface->attrs = append_attr(iface->attrs, make_attrp(ATTR_UUID, uuid));
1263 sha1_init(&ctx);
1264 sha1_update(&ctx, winrt_pinterface_namespace, sizeof(winrt_pinterface_namespace));
1265 sha1_update(&ctx, iface->signature, strlen(iface->signature));
1266 sha1_finalize(&ctx, (unsigned int *)hash);
1268 /* https://tools.ietf.org/html/rfc4122:
1270 * Set the four most significant bits (bits 12 through 15) of the
1271 time_hi_and_version field to the appropriate 4-bit version number
1272 from Section 4.1.3.
1274 * Set the two most significant bits (bits 6 and 7) of the
1275 clock_seq_hi_and_reserved to zero and one, respectively.
1278 hash[6] = ((hash[6] & 0x0f) | (version << 4));
1279 hash[8] = ((hash[8] & 0x3f) | 0x80);
1281 uuid->Data1 = ((unsigned int)hash[0] << 24) | ((unsigned int)hash[1] << 16) | ((unsigned int)hash[2] << 8) | hash[3];
1282 uuid->Data2 = ((unsigned short)hash[4] << 8) | hash[5];
1283 uuid->Data3 = ((unsigned short)hash[6] << 8) | hash[7];
1284 memcpy(&uuid->Data4, hash + 8, sizeof(*uuid) - offsetof(struct uuid, Data4));
1287 type_t *type_parameterized_type_specialize_define(type_t *type)
1289 type_t *tmpl = type->details.parameterized.type;
1290 typeref_list_t *orig = tmpl->details.parameterized.params;
1291 typeref_list_t *repl = type->details.parameterized.params;
1292 type_t *iface = find_parameterized_type(tmpl, repl);
1294 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE ||
1295 type_get_type_detect_alias(tmpl) != TYPE_PARAMETERIZED_TYPE)
1296 error_loc("cannot define non-parameterized type %s, declared at %s:%d\n",
1297 type->name, type->loc_info.input_name, type->loc_info.line_number);
1299 if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_INTERFACE &&
1300 type_get_type_detect_alias(iface) == TYPE_INTERFACE)
1301 type_parameterized_interface_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1302 else if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_DELEGATE &&
1303 type_get_type_detect_alias(iface) == TYPE_DELEGATE)
1304 type_parameterized_delegate_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1305 else
1306 error_loc("pinterface/pdelegate %s previously not declared a pinterface/pdelegate at %s:%d\n",
1307 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
1309 iface->impl_name = format_parameterized_type_impl_name(type, repl, "");
1310 iface->signature = format_parameterized_type_signature(type, repl);
1311 iface->defined = TRUE;
1312 if (iface->type_type == TYPE_DELEGATE)
1314 iface = iface->details.delegate.iface;
1315 iface->impl_name = format_parameterized_type_impl_name(type, repl, "I");
1316 iface->signature = format_parameterized_type_signature(type, repl);
1317 iface->defined = TRUE;
1319 compute_interface_signature_uuid(iface);
1320 compute_method_indexes(iface);
1321 return iface;
1324 int type_is_equal(const type_t *type1, const type_t *type2)
1326 if (type1 == type2)
1327 return TRUE;
1328 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
1329 return FALSE;
1330 if (type1->namespace != type2->namespace)
1331 return FALSE;
1333 if (type1->name && type2->name)
1334 return !strcmp(type1->name, type2->name);
1335 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
1336 return FALSE;
1338 /* FIXME: do deep inspection of types to determine if they are equal */
1340 return FALSE;