README: Mention Gitlab.
[wine.git] / tools / widl / typetree.c
blobfef9d2e5c03066095e7192a77fe740ef10912946
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, struct namespace *namespace, int defined, var_list_t *fields)
585 type_t *t = NULL;
587 if (name)
588 t = find_type(name, namespace, tsUNION);
590 if (!t)
592 t = make_type(TYPE_UNION);
593 t->name = name;
594 t->namespace = namespace;
595 if (name)
596 reg_type(t, name, namespace, tsUNION);
599 if (!t->defined && defined)
601 t->details.structure = xmalloc(sizeof(*t->details.structure));
602 t->details.structure->fields = fields;
603 t->defined = TRUE;
605 else if (defined)
606 error_loc("redefinition of union %s\n", name);
608 return t;
611 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
613 type_t *t = NULL;
615 if (name)
616 t = find_type(name, NULL, tsUNION);
618 if (!t)
620 t = make_type(TYPE_ENCAPSULATED_UNION);
621 t->name = name;
622 if (name)
623 reg_type(t, name, NULL, tsUNION);
625 t->type_type = TYPE_ENCAPSULATED_UNION;
627 if (!t->defined)
629 if (!union_field)
630 union_field = make_var(xstrdup("tagged_union"));
631 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), NULL, TRUE, cases);
633 t->details.structure = xmalloc(sizeof(*t->details.structure));
634 t->details.structure->fields = append_var(NULL, switch_field);
635 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
636 t->defined = TRUE;
638 else
639 error_loc("redefinition of union %s\n", name);
641 return t;
644 static int is_valid_bitfield_type(const type_t *type)
646 switch (type_get_type(type))
648 case TYPE_ENUM:
649 return TRUE;
650 case TYPE_BASIC:
651 switch (type_basic_get_type(type))
653 case TYPE_BASIC_INT8:
654 case TYPE_BASIC_INT16:
655 case TYPE_BASIC_INT32:
656 case TYPE_BASIC_INT64:
657 case TYPE_BASIC_INT:
658 case TYPE_BASIC_INT3264:
659 case TYPE_BASIC_LONG:
660 case TYPE_BASIC_CHAR:
661 case TYPE_BASIC_HYPER:
662 case TYPE_BASIC_BYTE:
663 case TYPE_BASIC_WCHAR:
664 case TYPE_BASIC_ERROR_STATUS_T:
665 return TRUE;
666 case TYPE_BASIC_FLOAT:
667 case TYPE_BASIC_DOUBLE:
668 case TYPE_BASIC_HANDLE:
669 return FALSE;
671 return FALSE;
672 default:
673 return FALSE;
677 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
679 type_t *t;
681 if (!is_valid_bitfield_type(field))
682 error_loc("bit-field has invalid type\n");
684 if (bits->cval < 0)
685 error_loc("negative width for bit-field\n");
687 /* FIXME: validate bits->cval <= memsize(field) * 8 */
689 t = make_type(TYPE_BITFIELD);
690 t->details.bitfield.field = field;
691 t->details.bitfield.bits = bits;
692 return t;
695 static unsigned int compute_method_indexes(type_t *iface)
697 unsigned int idx;
698 statement_t *stmt;
700 if (!iface->details.iface)
701 return 0;
703 if (type_iface_get_inherit(iface))
704 idx = compute_method_indexes(type_iface_get_inherit(iface));
705 else
706 idx = 0;
708 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
710 var_t *func = stmt->u.var;
711 if (!is_callas(func->attrs))
712 func->func_idx = idx++;
715 return idx;
718 type_t *type_interface_declare(char *name, struct namespace *namespace)
720 type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
721 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
722 error_loc("interface %s previously not declared an interface at %s:%d\n",
723 type->name, type->loc_info.input_name, type->loc_info.line_number);
724 return type;
727 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
729 if (iface->defined)
730 error_loc("interface %s already defined at %s:%d\n",
731 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
732 if (iface == inherit)
733 error_loc("interface %s can't inherit from itself\n",
734 iface->name);
735 iface->attrs = check_interface_attrs(iface->name, attrs);
736 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
737 iface->details.iface->disp_props = NULL;
738 iface->details.iface->disp_methods = NULL;
739 iface->details.iface->stmts = stmts;
740 iface->details.iface->inherit = inherit;
741 iface->details.iface->disp_inherit = NULL;
742 iface->details.iface->async_iface = NULL;
743 iface->details.iface->requires = requires;
744 iface->defined = TRUE;
745 compute_method_indexes(iface);
746 return iface;
749 type_t *type_dispinterface_declare(char *name)
751 type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
752 if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
753 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
754 type->name, type->loc_info.input_name, type->loc_info.line_number);
755 return type;
758 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
760 if (iface->defined)
761 error_loc("dispinterface %s already defined at %s:%d\n",
762 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
763 iface->attrs = check_dispiface_attrs(iface->name, attrs);
764 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
765 iface->details.iface->disp_props = props;
766 iface->details.iface->disp_methods = methods;
767 iface->details.iface->stmts = NULL;
768 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
769 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
770 iface->details.iface->disp_inherit = NULL;
771 iface->details.iface->async_iface = NULL;
772 iface->details.iface->requires = NULL;
773 iface->defined = TRUE;
774 compute_method_indexes(iface);
775 return iface;
778 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
780 if (dispiface->defined)
781 error_loc("dispinterface %s already defined at %s:%d\n",
782 dispiface->name, dispiface->loc_info.input_name, dispiface->loc_info.line_number);
783 dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
784 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
785 dispiface->details.iface->disp_props = NULL;
786 dispiface->details.iface->disp_methods = NULL;
787 dispiface->details.iface->stmts = NULL;
788 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
789 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
790 dispiface->details.iface->disp_inherit = iface;
791 dispiface->details.iface->async_iface = NULL;
792 dispiface->details.iface->requires = NULL;
793 dispiface->defined = TRUE;
794 compute_method_indexes(dispiface);
795 return dispiface;
798 type_t *type_module_declare(char *name)
800 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
801 if (type_get_type_detect_alias(type) != TYPE_MODULE)
802 error_loc("module %s previously not declared a module at %s:%d\n",
803 type->name, type->loc_info.input_name, type->loc_info.line_number);
804 return type;
807 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
809 if (module->defined)
810 error_loc("module %s already defined at %s:%d\n",
811 module->name, module->loc_info.input_name, module->loc_info.line_number);
812 module->attrs = check_module_attrs(module->name, attrs);
813 module->details.module = xmalloc(sizeof(*module->details.module));
814 module->details.module->stmts = stmts;
815 module->defined = TRUE;
816 return module;
819 type_t *type_coclass_declare(char *name)
821 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
822 if (type_get_type_detect_alias(type) != TYPE_COCLASS)
823 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
824 type->name, type->loc_info.input_name, type->loc_info.line_number);
825 return type;
828 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces)
830 if (coclass->defined)
831 error_loc("coclass %s already defined at %s:%d\n",
832 coclass->name, coclass->loc_info.input_name, coclass->loc_info.line_number);
833 coclass->attrs = check_coclass_attrs(coclass->name, attrs);
834 coclass->details.coclass.ifaces = ifaces;
835 coclass->defined = TRUE;
836 return coclass;
839 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
841 type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
842 if (type_get_type_detect_alias(type) != TYPE_RUNTIMECLASS)
843 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
844 type->name, type->loc_info.input_name, type->loc_info.line_number);
845 return type;
848 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces)
850 typeref_t *ref, *required, *tmp;
851 typeref_list_t *requires;
853 if (runtimeclass->defined)
854 error_loc("runtimeclass %s already defined at %s:%d\n",
855 runtimeclass->name, runtimeclass->loc_info.input_name, runtimeclass->loc_info.line_number);
856 runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
857 runtimeclass->details.runtimeclass.ifaces = ifaces;
858 runtimeclass->defined = TRUE;
859 if (!type_runtimeclass_get_default_iface(runtimeclass, FALSE) &&
860 !get_attrp(runtimeclass->attrs, ATTR_STATIC))
861 error_loc("runtimeclass %s must have a default interface or static factory\n", runtimeclass->name);
863 if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
865 /* FIXME: this should probably not be allowed, here or in coclass, */
866 /* but for now there's too many places in Wine IDL where it is to */
867 /* even print a warning. */
868 if (!(ref->type->defined)) continue;
869 if (!(requires = type_iface_get_requires(ref->type))) continue;
870 LIST_FOR_EACH_ENTRY(required, requires, typeref_t, entry)
872 int found = 0;
874 LIST_FOR_EACH_ENTRY(tmp, ifaces, typeref_t, entry)
875 if ((found = type_is_equal(tmp->type, required->type))) break;
877 if (!found)
878 error_loc("interface '%s' also requires interface '%s', "
879 "but runtimeclass '%s' does not implement it.\n",
880 ref->type->name, required->type->name, runtimeclass->name);
884 return runtimeclass;
887 type_t *type_apicontract_declare(char *name, struct namespace *namespace)
889 type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
890 if (type_get_type_detect_alias(type) != TYPE_APICONTRACT)
891 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
892 type->name, type->loc_info.input_name, type->loc_info.line_number);
893 return type;
896 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
898 if (apicontract->defined)
899 error_loc("apicontract %s already defined at %s:%d\n",
900 apicontract->name, apicontract->loc_info.input_name, apicontract->loc_info.line_number);
901 apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
902 apicontract->defined = TRUE;
903 return apicontract;
906 static void compute_delegate_iface_names(type_t *delegate, type_t *type, typeref_list_t *params)
908 type_t *iface = delegate->details.delegate.iface;
909 iface->namespace = delegate->namespace;
910 iface->name = strmake("I%s", delegate->name);
911 if (type) iface->c_name = format_parameterized_type_c_name(type, params, "I", "_C");
912 else iface->c_name = format_namespace(delegate->namespace, "__x_", "_C", iface->name, use_abi_namespace ? "ABI" : NULL);
913 if (type) iface->param_name = format_parameterized_type_c_name(type, params, "I", "__C");
914 else iface->param_name = format_namespace(delegate->namespace, "_", "__C", iface->name, NULL);
915 iface->qualified_name = format_namespace(delegate->namespace, "", "::", iface->name, use_abi_namespace ? "ABI" : NULL);
918 type_t *type_delegate_declare(char *name, struct namespace *namespace)
920 type_t *type = get_type(TYPE_DELEGATE, name, namespace, 0);
921 if (type_get_type_detect_alias(type) != TYPE_DELEGATE)
922 error_loc("delegate %s previously not declared a delegate at %s:%d\n",
923 type->name, type->loc_info.input_name, type->loc_info.line_number);
924 return type;
927 type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts)
929 type_t *iface;
931 if (delegate->defined)
932 error_loc("delegate %s already defined at %s:%d\n",
933 delegate->name, delegate->loc_info.input_name, delegate->loc_info.line_number);
935 delegate->attrs = check_interface_attrs(delegate->name, attrs);
937 iface = make_type(TYPE_INTERFACE);
938 iface->attrs = delegate->attrs;
939 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
940 iface->details.iface->disp_props = NULL;
941 iface->details.iface->disp_methods = NULL;
942 iface->details.iface->stmts = stmts;
943 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
944 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
945 iface->details.iface->disp_inherit = NULL;
946 iface->details.iface->async_iface = NULL;
947 iface->details.iface->requires = NULL;
948 iface->defined = TRUE;
949 compute_method_indexes(iface);
951 delegate->details.delegate.iface = iface;
952 delegate->defined = TRUE;
953 compute_delegate_iface_names(delegate, NULL, NULL);
955 return delegate;
958 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params)
960 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
961 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
962 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
963 type->name, type->loc_info.input_name, type->loc_info.line_number);
964 type->details.parameterized.type = make_type(TYPE_INTERFACE);
965 type->details.parameterized.params = params;
966 return type;
969 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
971 type_t *iface;
972 if (type->defined)
973 error_loc("pinterface %s already defined at %s:%d\n",
974 type->name, type->loc_info.input_name, type->loc_info.line_number);
976 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
977 * a new type GUID with the rules described in:
978 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
979 * TODO: store type signatures for generated interfaces, and generate their GUIDs
981 type->attrs = check_interface_attrs(type->name, attrs);
983 iface = type->details.parameterized.type;
984 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
985 iface->details.iface->disp_props = NULL;
986 iface->details.iface->disp_methods = NULL;
987 iface->details.iface->stmts = stmts;
988 iface->details.iface->inherit = inherit;
989 iface->details.iface->disp_inherit = NULL;
990 iface->details.iface->async_iface = NULL;
991 iface->details.iface->requires = requires;
993 iface->name = type->name;
995 type->defined = TRUE;
996 return type;
999 type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params)
1001 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
1002 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
1003 error_loc("pdelegate %s previously not declared a pdelegate at %s:%d\n",
1004 type->name, type->loc_info.input_name, type->loc_info.line_number);
1005 type->details.parameterized.type = make_type(TYPE_DELEGATE);
1006 type->details.parameterized.params = params;
1007 return type;
1010 type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts)
1012 type_t *iface, *delegate;
1014 if (type->defined)
1015 error_loc("pdelegate %s already defined at %s:%d\n",
1016 type->name, type->loc_info.input_name, type->loc_info.line_number);
1018 type->attrs = check_interface_attrs(type->name, attrs);
1020 delegate = type->details.parameterized.type;
1021 delegate->attrs = type->attrs;
1022 delegate->details.delegate.iface = make_type(TYPE_INTERFACE);
1024 iface = delegate->details.delegate.iface;
1025 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1026 iface->details.iface->disp_props = NULL;
1027 iface->details.iface->disp_methods = NULL;
1028 iface->details.iface->stmts = stmts;
1029 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
1030 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
1031 iface->details.iface->disp_inherit = NULL;
1032 iface->details.iface->async_iface = NULL;
1033 iface->details.iface->requires = NULL;
1035 delegate->name = type->name;
1036 compute_delegate_iface_names(delegate, type, type->details.parameterized.params);
1038 type->defined = TRUE;
1039 return type;
1042 type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params)
1044 type_t *new_type = duptype(type, 0);
1045 new_type->details.parameterized.type = type;
1046 new_type->details.parameterized.params = params;
1047 return new_type;
1050 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl);
1052 static typeref_list_t *replace_type_parameters_in_type_list(typeref_list_t *list, typeref_list_t *orig, typeref_list_t *repl)
1054 typeref_list_t *new_list = NULL;
1055 typeref_t *ref;
1057 if (!list) return list;
1059 LIST_FOR_EACH_ENTRY(ref, list, typeref_t, entry)
1061 type_t *new_type = replace_type_parameters_in_type(ref->type, orig, repl);
1062 new_list = append_typeref(new_list, make_typeref(new_type));
1065 return new_list;
1068 static var_t *replace_type_parameters_in_var(var_t *var, typeref_list_t *orig, typeref_list_t *repl)
1070 var_t *new_var = xmalloc(sizeof(*new_var));
1071 *new_var = *var;
1072 list_init(&new_var->entry);
1073 new_var->declspec.type = replace_type_parameters_in_type(var->declspec.type, orig, repl);
1074 return new_var;
1077 static var_list_t *replace_type_parameters_in_var_list(var_list_t *var_list, typeref_list_t *orig, typeref_list_t *repl)
1079 var_list_t *new_var_list;
1080 var_t *var, *new_var;
1082 if (!var_list) return var_list;
1084 new_var_list = xmalloc(sizeof(*new_var_list));
1085 list_init(new_var_list);
1087 LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
1089 new_var = replace_type_parameters_in_var(var, orig, repl);
1090 list_add_tail(new_var_list, &new_var->entry);
1093 return new_var_list;
1096 static statement_t *replace_type_parameters_in_statement(statement_t *stmt, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
1098 statement_t *new_stmt = xmalloc(sizeof(*new_stmt));
1099 *new_stmt = *stmt;
1100 list_init(&new_stmt->entry);
1102 switch (stmt->type)
1104 case STMT_DECLARATION:
1105 new_stmt->u.var = replace_type_parameters_in_var(stmt->u.var, orig, repl);
1106 break;
1107 case STMT_TYPE:
1108 case STMT_TYPEREF:
1109 new_stmt->u.type = replace_type_parameters_in_type(stmt->u.type, orig, repl);
1110 break;
1111 case STMT_TYPEDEF:
1112 new_stmt->u.type_list = replace_type_parameters_in_type_list(stmt->u.type_list, orig, repl);
1113 break;
1114 case STMT_MODULE:
1115 case STMT_LIBRARY:
1116 case STMT_IMPORT:
1117 case STMT_IMPORTLIB:
1118 case STMT_PRAGMA:
1119 case STMT_CPPQUOTE:
1120 error_loc_info(loc, "unimplemented parameterized type replacement for statement type %d.\n", stmt->type);
1121 break;
1124 return new_stmt;
1127 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)
1129 statement_list_t *new_stmt_list;
1130 statement_t *stmt, *new_stmt;
1132 if (!stmt_list) return stmt_list;
1134 new_stmt_list = xmalloc(sizeof(*new_stmt_list));
1135 list_init(new_stmt_list);
1137 LIST_FOR_EACH_ENTRY(stmt, stmt_list, statement_t, entry)
1139 new_stmt = replace_type_parameters_in_statement(stmt, orig, repl, loc);
1140 list_add_tail(new_stmt_list, &new_stmt->entry);
1143 return new_stmt_list;
1146 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl)
1148 struct list *o, *r;
1149 type_t *t;
1151 if (!type) return type;
1152 switch (type->type_type)
1154 case TYPE_VOID:
1155 case TYPE_BASIC:
1156 case TYPE_ENUM:
1157 case TYPE_BITFIELD:
1158 case TYPE_INTERFACE:
1159 case TYPE_RUNTIMECLASS:
1160 case TYPE_DELEGATE:
1161 case TYPE_STRUCT:
1162 case TYPE_ENCAPSULATED_UNION:
1163 case TYPE_UNION:
1164 return type;
1165 case TYPE_PARAMETER:
1166 if (!orig || !repl) return NULL;
1167 for (o = list_head(orig), r = list_head(repl); o && r;
1168 o = list_next(orig, o), r = list_next(repl, r))
1169 if (type == LIST_ENTRY(o, typeref_t, entry)->type)
1170 return LIST_ENTRY(r, typeref_t, entry)->type;
1171 return type;
1172 case TYPE_POINTER:
1173 t = replace_type_parameters_in_type(type->details.pointer.ref.type, orig, repl);
1174 if (t == type->details.pointer.ref.type) return type;
1175 type = duptype(type, 0);
1176 type->details.pointer.ref.type = t;
1177 return type;
1178 case TYPE_ALIAS:
1179 t = replace_type_parameters_in_type(type->details.alias.aliasee.type, orig, repl);
1180 if (t == type->details.alias.aliasee.type) return type;
1181 type = duptype(type, 0);
1182 type->details.alias.aliasee.type = t;
1183 return type;
1184 case TYPE_ARRAY:
1185 t = replace_type_parameters_in_type(type->details.array.elem.type, orig, repl);
1186 if (t == t->details.array.elem.type) return type;
1187 type = duptype(type, 0);
1188 t->details.array.elem.type = t;
1189 return type;
1190 case TYPE_FUNCTION:
1191 t = duptype(type, 0);
1192 t->details.function = xmalloc(sizeof(*t->details.function));
1193 t->details.function->args = replace_type_parameters_in_var_list(type->details.function->args, orig, repl);
1194 t->details.function->retval = replace_type_parameters_in_var(type->details.function->retval, orig, repl);
1195 return t;
1196 case TYPE_PARAMETERIZED_TYPE:
1197 t = type->details.parameterized.type;
1198 if (t->type_type != TYPE_PARAMETERIZED_TYPE) return find_parameterized_type(type, repl);
1199 repl = replace_type_parameters_in_type_list(type->details.parameterized.params, orig, repl);
1200 return replace_type_parameters_in_type(t, t->details.parameterized.params, repl);
1201 case TYPE_MODULE:
1202 case TYPE_COCLASS:
1203 case TYPE_APICONTRACT:
1204 error_loc_info(&type->loc_info, "unimplemented parameterized type replacement for type %s of type %d.\n", type->name, type->type_type);
1205 break;
1208 return type;
1211 static void type_parameterized_interface_specialize(type_t *tmpl, type_t *iface, typeref_list_t *orig, typeref_list_t *repl)
1213 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1214 iface->details.iface->disp_methods = NULL;
1215 iface->details.iface->disp_props = NULL;
1216 iface->details.iface->stmts = replace_type_parameters_in_statement_list(tmpl->details.iface->stmts, orig, repl, &tmpl->loc_info);
1217 iface->details.iface->inherit = replace_type_parameters_in_type(tmpl->details.iface->inherit, orig, repl);
1218 iface->details.iface->disp_inherit = NULL;
1219 iface->details.iface->async_iface = NULL;
1220 iface->details.iface->requires = NULL;
1223 static void type_parameterized_delegate_specialize(type_t *tmpl, type_t *delegate, typeref_list_t *orig, typeref_list_t *repl)
1225 type_parameterized_interface_specialize(tmpl->details.delegate.iface, delegate->details.delegate.iface, orig, repl);
1228 type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params)
1230 type_t *tmpl = type->details.parameterized.type;
1231 type_t *new_type = duptype(tmpl, 0);
1233 new_type->namespace = type->namespace;
1234 new_type->name = format_parameterized_type_name(type, params);
1235 reg_type(new_type, new_type->name, new_type->namespace, 0);
1236 new_type->c_name = format_parameterized_type_c_name(type, params, "", "_C");
1237 new_type->short_name = format_parameterized_type_short_name(type, params, "");
1238 new_type->param_name = format_parameterized_type_c_name(type, params, "", "__C");
1240 if (new_type->type_type == TYPE_DELEGATE)
1242 new_type->details.delegate.iface = duptype(tmpl->details.delegate.iface, 0);
1243 compute_delegate_iface_names(new_type, type, params);
1244 new_type->details.delegate.iface->short_name = format_parameterized_type_short_name(type, params, "I");
1247 return new_type;
1250 static void compute_interface_signature_uuid(type_t *iface)
1252 static const char winrt_pinterface_namespace[] = {0x11,0xf4,0x7a,0xd5,0x7b,0x73,0x42,0xc0,0xab,0xae,0x87,0x8b,0x1e,0x16,0xad,0xee};
1253 static const int version = 5;
1254 struct sha1_context ctx;
1255 unsigned char hash[20];
1256 struct uuid *uuid;
1258 if (!(uuid = get_attrp(iface->attrs, ATTR_UUID)))
1260 uuid = xmalloc(sizeof(*uuid));
1261 iface->attrs = append_attr(iface->attrs, make_attrp(ATTR_UUID, uuid));
1264 sha1_init(&ctx);
1265 sha1_update(&ctx, winrt_pinterface_namespace, sizeof(winrt_pinterface_namespace));
1266 sha1_update(&ctx, iface->signature, strlen(iface->signature));
1267 sha1_finalize(&ctx, (unsigned int *)hash);
1269 /* https://tools.ietf.org/html/rfc4122:
1271 * Set the four most significant bits (bits 12 through 15) of the
1272 time_hi_and_version field to the appropriate 4-bit version number
1273 from Section 4.1.3.
1275 * Set the two most significant bits (bits 6 and 7) of the
1276 clock_seq_hi_and_reserved to zero and one, respectively.
1279 hash[6] = ((hash[6] & 0x0f) | (version << 4));
1280 hash[8] = ((hash[8] & 0x3f) | 0x80);
1282 uuid->Data1 = ((unsigned int)hash[0] << 24) | ((unsigned int)hash[1] << 16) | ((unsigned int)hash[2] << 8) | hash[3];
1283 uuid->Data2 = ((unsigned short)hash[4] << 8) | hash[5];
1284 uuid->Data3 = ((unsigned short)hash[6] << 8) | hash[7];
1285 memcpy(&uuid->Data4, hash + 8, sizeof(*uuid) - offsetof(struct uuid, Data4));
1288 type_t *type_parameterized_type_specialize_define(type_t *type)
1290 type_t *tmpl = type->details.parameterized.type;
1291 typeref_list_t *orig = tmpl->details.parameterized.params;
1292 typeref_list_t *repl = type->details.parameterized.params;
1293 type_t *iface = find_parameterized_type(tmpl, repl);
1295 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE ||
1296 type_get_type_detect_alias(tmpl) != TYPE_PARAMETERIZED_TYPE)
1297 error_loc("cannot define non-parameterized type %s, declared at %s:%d\n",
1298 type->name, type->loc_info.input_name, type->loc_info.line_number);
1300 if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_INTERFACE &&
1301 type_get_type_detect_alias(iface) == TYPE_INTERFACE)
1302 type_parameterized_interface_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1303 else if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_DELEGATE &&
1304 type_get_type_detect_alias(iface) == TYPE_DELEGATE)
1305 type_parameterized_delegate_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1306 else
1307 error_loc("pinterface/pdelegate %s previously not declared a pinterface/pdelegate at %s:%d\n",
1308 iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
1310 iface->impl_name = format_parameterized_type_impl_name(type, repl, "");
1311 iface->signature = format_parameterized_type_signature(type, repl);
1312 iface->defined = TRUE;
1313 if (iface->type_type == TYPE_DELEGATE)
1315 iface = iface->details.delegate.iface;
1316 iface->impl_name = format_parameterized_type_impl_name(type, repl, "I");
1317 iface->signature = format_parameterized_type_signature(type, repl);
1318 iface->defined = TRUE;
1320 compute_interface_signature_uuid(iface);
1321 compute_method_indexes(iface);
1322 return iface;
1325 int type_is_equal(const type_t *type1, const type_t *type2)
1327 if (type1 == type2)
1328 return TRUE;
1329 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
1330 return FALSE;
1331 if (type1->namespace != type2->namespace)
1332 return FALSE;
1334 if (type1->name && type2->name)
1335 return !strcmp(type1->name, type2->name);
1336 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
1337 return FALSE;
1339 /* FIXME: do deep inspection of types to determine if they are equal */
1341 return FALSE;