widl: Add support for interfaces to SLTG typelib generator.
[wine.git] / tools / widl / typetree.c
blob7e4c4fd3a38fa51a986a0c6dcaf66ad1ca38b126
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_location( &t->where, NULL, NULL );
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 (!strcmp(type->name, "IInspectable")) n += strappend(buf, len, pos + n, "cinterface(IInspectable)");
172 else if (type->signature) n += strappend(buf, len, pos + n, "%s", type->signature);
173 else
175 if (!(uuid = get_attrp( type->attrs, ATTR_UUID )))
176 error_at( &type->where, "cannot compute type signature, no uuid found for type %s.\n", type->name );
178 n += strappend(buf, len, pos + n, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
179 uuid->Data1, uuid->Data2, uuid->Data3,
180 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
181 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
183 return n;
184 case TYPE_DELEGATE:
185 n += strappend(buf, len, pos + n, "delegate(");
186 n += append_type_signature(buf, len, pos + n, type_delegate_get_iface(type));
187 n += strappend(buf, len, pos + n, ")");
188 return n;
189 case TYPE_RUNTIMECLASS:
190 n += strappend(buf, len, pos + n, "rc(");
191 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
192 n += strappend(buf, len, pos + n, ";");
193 n += append_type_signature(buf, len, pos + n, type_runtimeclass_get_default_iface(type, TRUE));
194 n += strappend(buf, len, pos + n, ")");
195 return n;
196 case TYPE_POINTER:
197 n += append_type_signature(buf, len, pos + n, type->details.pointer.ref.type);
198 return n;
199 case TYPE_ALIAS:
200 if (!strcmp(type->name, "boolean")) n += strappend(buf, len, pos + n, "b1");
201 else if (!strcmp(type->name, "GUID")) n += strappend(buf, len, pos + n, "g16");
202 else if (!strcmp(type->name, "HSTRING")) n += strappend(buf, len, pos + n, "string");
203 else n += append_type_signature(buf, len, pos + n, type->details.alias.aliasee.type);
204 return n;
205 case TYPE_STRUCT:
206 n += strappend(buf, len, pos + n, "struct(");
207 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
208 n += append_var_list_signature(buf, len, pos + n, type->details.structure->fields);
209 n += strappend(buf, len, pos + n, ")");
210 return n;
211 case TYPE_BASIC:
212 switch (type_basic_get_type(type))
214 case TYPE_BASIC_INT16:
215 n += strappend(buf, len, pos + n, type_basic_get_sign(type) <= 0 ? "i2" : "u2");
216 return n;
217 case TYPE_BASIC_INT:
218 case TYPE_BASIC_INT32:
219 case TYPE_BASIC_LONG:
220 n += strappend(buf, len, pos + n, type_basic_get_sign(type) <= 0 ? "i4" : "u4");
221 return n;
222 case TYPE_BASIC_INT64:
223 n += strappend(buf, len, pos + n, type_basic_get_sign(type) <= 0 ? "i8" : "u8");
224 return n;
225 case TYPE_BASIC_INT8:
226 assert(type_basic_get_sign(type) > 0); /* signature string for signed char isn't specified */
227 n += strappend(buf, len, pos + n, "u1");
228 return n;
229 case TYPE_BASIC_FLOAT:
230 n += strappend(buf, len, pos + n, "f4");
231 return n;
232 case TYPE_BASIC_DOUBLE:
233 n += strappend(buf, len, pos + n, "f8");
234 return n;
235 case TYPE_BASIC_BYTE:
236 n += strappend(buf, len, pos + n, "u1");
237 return n;
238 case TYPE_BASIC_INT3264:
239 case TYPE_BASIC_CHAR:
240 case TYPE_BASIC_HYPER:
241 case TYPE_BASIC_WCHAR:
242 case TYPE_BASIC_ERROR_STATUS_T:
243 case TYPE_BASIC_HANDLE:
244 error_at( &type->where, "unimplemented type signature for basic type %d.\n",
245 type_basic_get_type( type ) );
246 break;
248 case TYPE_ENUM:
249 n += strappend(buf, len, pos + n, "enum(");
250 n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
251 if (is_attr(type->attrs, ATTR_FLAGS)) n += strappend(buf, len, pos + n, ";u4");
252 else n += strappend(buf, len, pos + n, ";i4");
253 n += strappend(buf, len, pos + n, ")");
254 return n;
255 case TYPE_ARRAY:
256 case TYPE_ENCAPSULATED_UNION:
257 case TYPE_UNION:
258 case TYPE_COCLASS:
259 case TYPE_VOID:
260 case TYPE_FUNCTION:
261 case TYPE_BITFIELD:
262 case TYPE_MODULE:
263 case TYPE_APICONTRACT:
264 error_at( &type->where, "unimplemented type signature for type %s of type %d.\n",
265 type->name, type->type_type );
266 break;
267 case TYPE_PARAMETERIZED_TYPE:
268 case TYPE_PARAMETER:
269 assert(0); /* should not be there */
270 break;
273 return n;
276 char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix, const char *abi_prefix)
278 size_t len = 0;
279 char *buf = NULL;
280 append_namespaces(&buf, &len, 0, namespace, prefix, separator, suffix, abi_prefix);
281 return buf;
284 char *format_parameterized_type_name(type_t *type, typeref_list_t *params)
286 size_t len = 0, pos = 0;
287 char *buf = NULL;
288 typeref_t *ref;
290 pos += strappend(&buf, &len, pos, "%s<", type->name);
291 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
293 type = type_pointer_get_root_type(ref->type);
294 pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
295 pos += append_pointer_stars(&buf, &len, pos, ref->type);
296 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ",");
298 pos += strappend(&buf, &len, pos, " >");
300 return buf;
303 static char const *parameterized_type_shorthands[][2] = {
304 {"Windows__CFoundation__CCollections__C", "__F"},
305 {"Windows_CFoundation_CCollections_C", "__F"},
306 {"Windows__CFoundation__C", "__F"},
307 {"Windows_CFoundation_C", "__F"},
310 static char *format_parameterized_type_c_name(type_t *type, typeref_list_t *params, const char *prefix, const char *separator)
312 const char *tmp, *ns_prefix = "__x_", *abi_prefix = NULL;
313 size_t len = 0, pos = 0;
314 char *buf = NULL;
315 int i, count = params ? list_count(params) : 0;
316 typeref_t *ref;
318 if (!strcmp(separator, "__C")) ns_prefix = "_C";
319 else if (use_abi_namespace) abi_prefix = "ABI";
321 pos += append_namespaces(&buf, &len, pos, type->namespace, ns_prefix, separator, "", abi_prefix);
322 pos += strappend(&buf, &len, pos, "%s%s_%d", prefix, type->name, count);
323 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
325 type = type_pointer_get_root_type(ref->type);
326 if ((tmp = type->param_name)) pos += strappend(&buf, &len, pos, "_%s", tmp);
327 else pos += append_namespaces(&buf, &len, pos, type->namespace, "_", "__C", type->name, NULL);
330 for (i = 0; i < ARRAY_SIZE(parameterized_type_shorthands); ++i)
332 if ((tmp = strstr(buf, parameterized_type_shorthands[i][0])) &&
333 (tmp - buf) == strlen(ns_prefix) + (abi_prefix ? 5 : 0))
335 tmp += strlen(parameterized_type_shorthands[i][0]);
336 strcpy(buf, parameterized_type_shorthands[i][1]);
337 memmove(buf + 3, tmp, len - (tmp - buf));
341 return buf;
344 static char *format_parameterized_type_signature(type_t *type, typeref_list_t *params)
346 size_t len = 0, pos = 0;
347 char *buf = NULL;
348 typeref_t *ref;
349 const struct uuid *uuid;
351 if (!(uuid = get_attrp( type->attrs, ATTR_UUID )))
352 error_at( &type->where, "cannot compute type signature, no uuid found for type %s.\n", type->name );
354 pos += strappend(&buf, &len, pos, "pinterface({%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
355 uuid->Data1, uuid->Data2, uuid->Data3,
356 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
357 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
358 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
360 pos += strappend(&buf, &len, pos, ";");
361 pos += append_type_signature(&buf, &len, pos, ref->type);
363 pos += strappend(&buf, &len, pos, ")");
365 return buf;
368 static char *format_parameterized_type_short_name(type_t *type, typeref_list_t *params, const char *prefix)
370 size_t len = 0, pos = 0;
371 char *buf = NULL;
372 typeref_t *ref;
374 pos += strappend(&buf, &len, pos, "%s%s", prefix, type->name);
375 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
377 type = type_pointer_get_root_type(ref->type);
378 if (type->short_name) pos += strappend(&buf, &len, pos, "_%s", type->short_name);
379 else pos += strappend(&buf, &len, pos, "_%s", type->name);
382 return buf;
385 static char *format_parameterized_type_impl_name(type_t *type, typeref_list_t *params, const char *prefix)
387 size_t len = 0, pos = 0;
388 char *buf = NULL;
389 typeref_t *ref;
390 type_t *iface;
392 pos += strappend(&buf, &len, pos, "%s%s_impl<", prefix, type->name);
393 if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
395 type = type_pointer_get_root_type(ref->type);
396 if (type->type_type == TYPE_RUNTIMECLASS)
398 pos += strappend(&buf, &len, pos, "ABI::Windows::Foundation::Internal::AggregateType<%s", type->qualified_name);
399 pos += append_pointer_stars(&buf, &len, pos, ref->type);
400 iface = type_runtimeclass_get_default_iface(type, TRUE);
401 pos += strappend(&buf, &len, pos, ", %s", iface->qualified_name);
402 pos += append_pointer_stars(&buf, &len, pos, ref->type);
403 pos += strappend(&buf, &len, pos, " >");
405 else
407 pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
408 pos += append_pointer_stars(&buf, &len, pos, ref->type);
410 if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
412 pos += strappend(&buf, &len, pos, " >");
414 return buf;
417 type_t *type_new_function(var_list_t *args)
419 var_t *arg;
420 type_t *t;
421 unsigned int i = 0;
423 if (args)
425 arg = LIST_ENTRY(list_head(args), var_t, entry);
426 if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
428 list_remove(&arg->entry);
429 free(arg);
430 free(args);
431 args = NULL;
434 if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
436 if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
437 error_loc("argument '%s' has void type\n", arg->name);
438 if (!arg->name)
440 if (i > 26 * 26)
441 error_loc("too many unnamed arguments\n");
442 else
444 int unique;
447 char name[3];
448 name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
449 name[1] = i > 26 ? 'a' + i % 26 : 0;
450 name[2] = 0;
451 unique = !find_arg(args, name);
452 if (unique)
453 arg->name = xstrdup(name);
454 i++;
455 } while (!unique);
460 t = make_type(TYPE_FUNCTION);
461 t->details.function = xmalloc(sizeof(*t->details.function));
462 t->details.function->args = args;
463 t->details.function->retval = make_var(xstrdup("_RetVal"));
464 return t;
467 type_t *type_new_pointer(type_t *ref)
469 type_t *t = make_type(TYPE_POINTER);
470 t->details.pointer.ref.type = ref;
471 return t;
474 type_t *type_new_alias(const decl_spec_t *t, const char *name)
476 type_t *a = make_type(TYPE_ALIAS);
478 a->name = xstrdup(name);
479 a->attrs = NULL;
480 a->details.alias.aliasee = *t;
481 init_location( &a->where, NULL, NULL );
483 return a;
486 type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
487 unsigned int dim, expr_t *size_is, expr_t *length_is)
489 type_t *t = make_type(TYPE_ARRAY);
490 if (name) t->name = xstrdup(name);
491 t->details.array.declptr = declptr;
492 t->details.array.length_is = length_is;
493 if (size_is)
494 t->details.array.size_is = size_is;
495 else
496 t->details.array.dim = dim;
497 if (element)
498 t->details.array.elem = *element;
499 return t;
502 type_t *type_new_basic(enum type_basic_type basic_type)
504 type_t *t = make_type(TYPE_BASIC);
505 t->details.basic.type = basic_type;
506 t->details.basic.sign = 0;
507 return t;
510 type_t *type_new_int(enum type_basic_type basic_type, int sign)
512 static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
514 assert(basic_type <= TYPE_BASIC_INT_MAX);
516 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
517 if (!int_types[basic_type][sign + 1])
519 int_types[basic_type][sign + 1] = type_new_basic(basic_type);
520 int_types[basic_type][sign + 1]->details.basic.sign = sign;
522 return int_types[basic_type][sign + 1];
525 type_t *type_new_void(void)
527 static type_t *void_type = NULL;
528 if (!void_type)
529 void_type = make_type(TYPE_VOID);
530 return void_type;
533 type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
535 type_t *t = NULL;
537 if (name)
538 t = find_type(name, namespace,tsENUM);
540 if (!t)
542 t = make_type(TYPE_ENUM);
543 t->name = name;
544 t->namespace = namespace;
545 if (name)
546 reg_type(t, name, namespace, tsENUM);
549 if (!t->defined && defined)
551 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
552 t->details.enumeration->enums = enums;
553 t->defined = TRUE;
555 else if (defined)
556 error_loc("redefinition of enum %s\n", name);
558 return t;
561 type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
563 type_t *t = NULL;
565 if (name)
566 t = find_type(name, namespace, tsSTRUCT);
568 if (!t)
570 t = make_type(TYPE_STRUCT);
571 t->name = name;
572 t->namespace = namespace;
573 if (name)
574 reg_type(t, name, namespace, tsSTRUCT);
577 if (!t->defined && defined)
579 t->details.structure = xmalloc(sizeof(*t->details.structure));
580 t->details.structure->fields = fields;
581 t->defined = TRUE;
583 else if (defined)
584 error_loc("redefinition of struct %s\n", name);
586 return t;
589 type_t *type_new_nonencapsulated_union(const char *name, struct namespace *namespace, int defined, var_list_t *fields)
591 type_t *t = NULL;
593 if (name)
594 t = find_type(name, namespace, tsUNION);
596 if (!t)
598 t = make_type(TYPE_UNION);
599 t->name = name;
600 t->namespace = namespace;
601 if (name)
602 reg_type(t, name, namespace, tsUNION);
605 if (!t->defined && defined)
607 t->details.structure = xmalloc(sizeof(*t->details.structure));
608 t->details.structure->fields = fields;
609 t->defined = TRUE;
611 else if (defined)
612 error_loc("redefinition of union %s\n", name);
614 return t;
617 type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
619 type_t *t = NULL;
621 if (name)
622 t = find_type(name, NULL, tsUNION);
624 if (!t)
626 t = make_type(TYPE_ENCAPSULATED_UNION);
627 t->name = name;
628 if (name)
629 reg_type(t, name, NULL, tsUNION);
631 t->type_type = TYPE_ENCAPSULATED_UNION;
633 if (!t->defined)
635 if (!union_field)
636 union_field = make_var(xstrdup("tagged_union"));
637 union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), NULL, TRUE, cases);
639 t->details.structure = xmalloc(sizeof(*t->details.structure));
640 t->details.structure->fields = append_var(NULL, switch_field);
641 t->details.structure->fields = append_var(t->details.structure->fields, union_field);
642 t->defined = TRUE;
644 else
645 error_loc("redefinition of union %s\n", name);
647 return t;
650 static int is_valid_bitfield_type(const type_t *type)
652 switch (type_get_type(type))
654 case TYPE_ENUM:
655 return TRUE;
656 case TYPE_BASIC:
657 switch (type_basic_get_type(type))
659 case TYPE_BASIC_INT8:
660 case TYPE_BASIC_INT16:
661 case TYPE_BASIC_INT32:
662 case TYPE_BASIC_INT64:
663 case TYPE_BASIC_INT:
664 case TYPE_BASIC_INT3264:
665 case TYPE_BASIC_LONG:
666 case TYPE_BASIC_CHAR:
667 case TYPE_BASIC_HYPER:
668 case TYPE_BASIC_BYTE:
669 case TYPE_BASIC_WCHAR:
670 case TYPE_BASIC_ERROR_STATUS_T:
671 return TRUE;
672 case TYPE_BASIC_FLOAT:
673 case TYPE_BASIC_DOUBLE:
674 case TYPE_BASIC_HANDLE:
675 return FALSE;
677 return FALSE;
678 default:
679 return FALSE;
683 type_t *type_new_bitfield(type_t *field, const expr_t *bits)
685 type_t *t;
687 if (!is_valid_bitfield_type(field))
688 error_loc("bit-field has invalid type\n");
690 if (bits->cval < 0)
691 error_loc("negative width for bit-field\n");
693 /* FIXME: validate bits->cval <= memsize(field) * 8 */
695 t = make_type(TYPE_BITFIELD);
696 t->details.bitfield.field = field;
697 t->details.bitfield.bits = bits;
698 return t;
701 static unsigned int compute_method_indexes(type_t *iface)
703 unsigned int idx;
704 statement_t *stmt;
706 if (!iface->details.iface)
707 return 0;
709 if (type_iface_get_inherit(iface))
710 idx = compute_method_indexes(type_iface_get_inherit(iface));
711 else
712 idx = 0;
714 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
716 var_t *func = stmt->u.var;
717 if (!is_callas(func->attrs))
718 func->func_idx = idx++;
721 return idx;
724 type_t *type_interface_declare(char *name, struct namespace *namespace)
726 type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
727 if (type_get_type_detect_alias( type ) != TYPE_INTERFACE)
728 error_loc( "interface %s previously not declared an interface at %s:%d\n", type->name,
729 type->where.input_name, type->where.first_line );
730 return type;
733 type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
735 if (iface->defined)
736 error_loc( "interface %s already defined at %s:%d\n", iface->name,
737 iface->where.input_name, iface->where.first_line );
738 if (iface == inherit)
739 error_loc("interface %s can't inherit from itself\n",
740 iface->name);
741 iface->attrs = check_interface_attrs(iface->name, attrs);
742 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
743 iface->details.iface->disp_props = NULL;
744 iface->details.iface->disp_methods = NULL;
745 iface->details.iface->stmts = stmts;
746 iface->details.iface->inherit = inherit;
747 iface->details.iface->disp_inherit = NULL;
748 iface->details.iface->async_iface = NULL;
749 iface->details.iface->requires = requires;
750 iface->defined = TRUE;
751 compute_method_indexes(iface);
752 return iface;
755 type_t *type_dispinterface_declare(char *name)
757 type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
758 if (type_get_type_detect_alias( type ) != TYPE_INTERFACE)
759 error_loc( "dispinterface %s previously not declared a dispinterface at %s:%d\n",
760 type->name, type->where.input_name, type->where.first_line );
761 return type;
764 type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
766 if (iface->defined)
767 error_loc( "dispinterface %s already defined at %s:%d\n", iface->name,
768 iface->where.input_name, iface->where.first_line );
769 iface->attrs = check_dispiface_attrs(iface->name, attrs);
770 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
771 iface->details.iface->disp_props = props;
772 iface->details.iface->disp_methods = methods;
773 iface->details.iface->stmts = NULL;
774 iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
775 if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
776 iface->details.iface->disp_inherit = NULL;
777 iface->details.iface->async_iface = NULL;
778 iface->details.iface->requires = NULL;
779 iface->defined = TRUE;
780 compute_method_indexes(iface);
781 return iface;
784 type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
786 if (dispiface->defined)
787 error_loc( "dispinterface %s already defined at %s:%d\n", dispiface->name,
788 dispiface->where.input_name, dispiface->where.first_line );
789 dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
790 dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
791 dispiface->details.iface->disp_props = NULL;
792 dispiface->details.iface->disp_methods = NULL;
793 dispiface->details.iface->stmts = NULL;
794 dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
795 if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
796 dispiface->details.iface->disp_inherit = iface;
797 dispiface->details.iface->async_iface = NULL;
798 dispiface->details.iface->requires = NULL;
799 dispiface->defined = TRUE;
800 compute_method_indexes(dispiface);
801 return dispiface;
804 type_t *type_module_declare(char *name)
806 type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
807 if (type_get_type_detect_alias( type ) != TYPE_MODULE)
808 error_loc( "module %s previously not declared a module at %s:%d\n", type->name,
809 type->where.input_name, type->where.first_line );
810 return type;
813 type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
815 if (module->defined)
816 error_loc( "module %s already defined at %s:%d\n", module->name,
817 module->where.input_name, module->where.first_line );
818 module->attrs = check_module_attrs(module->name, attrs);
819 module->details.module = xmalloc(sizeof(*module->details.module));
820 module->details.module->stmts = stmts;
821 module->defined = TRUE;
822 return module;
825 type_t *type_coclass_declare(char *name)
827 type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
828 if (type_get_type_detect_alias( type ) != TYPE_COCLASS)
829 error_loc( "coclass %s previously not declared a coclass at %s:%d\n", type->name,
830 type->where.input_name, type->where.first_line );
831 return type;
834 type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces)
836 if (coclass->defined)
837 error_loc( "coclass %s already defined at %s:%d\n", coclass->name,
838 coclass->where.input_name, coclass->where.first_line );
839 coclass->attrs = check_coclass_attrs(coclass->name, attrs);
840 coclass->details.coclass.ifaces = ifaces;
841 coclass->defined = TRUE;
842 return coclass;
845 type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
847 type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
848 if (type_get_type_detect_alias( type ) != TYPE_RUNTIMECLASS)
849 error_loc( "runtimeclass %s previously not declared a runtimeclass at %s:%d\n", type->name,
850 type->where.input_name, type->where.first_line );
851 return type;
854 type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces)
856 typeref_t *ref, *required, *tmp;
857 typeref_list_t *requires;
859 if (runtimeclass->defined)
860 error_loc( "runtimeclass %s already defined at %s:%d\n", runtimeclass->name,
861 runtimeclass->where.input_name, runtimeclass->where.first_line );
862 runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
863 runtimeclass->details.runtimeclass.ifaces = ifaces;
864 runtimeclass->defined = TRUE;
865 if (!type_runtimeclass_get_default_iface(runtimeclass, FALSE) &&
866 !get_attrp(runtimeclass->attrs, ATTR_STATIC))
867 error_loc("runtimeclass %s must have a default interface or static factory\n", runtimeclass->name);
869 if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
871 /* FIXME: this should probably not be allowed, here or in coclass, */
872 /* but for now there's too many places in Wine IDL where it is to */
873 /* even print a warning. */
874 if (!(ref->type->defined)) continue;
875 if (!(requires = type_iface_get_requires(ref->type))) continue;
876 LIST_FOR_EACH_ENTRY(required, requires, typeref_t, entry)
878 int found = 0;
880 LIST_FOR_EACH_ENTRY(tmp, ifaces, typeref_t, entry)
881 if ((found = type_is_equal(tmp->type, required->type))) break;
883 if (!found)
884 error_loc("interface '%s' also requires interface '%s', "
885 "but runtimeclass '%s' does not implement it.\n",
886 ref->type->name, required->type->name, runtimeclass->name);
890 return runtimeclass;
893 type_t *type_apicontract_declare(char *name, struct namespace *namespace)
895 type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
896 if (type_get_type_detect_alias( type ) != TYPE_APICONTRACT)
897 error_loc( "apicontract %s previously not declared a apicontract at %s:%d\n", type->name,
898 type->where.input_name, type->where.first_line );
899 return type;
902 type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
904 if (apicontract->defined)
905 error_loc( "apicontract %s already defined at %s:%d\n", apicontract->name,
906 apicontract->where.input_name, apicontract->where.first_line );
907 apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
908 apicontract->defined = TRUE;
909 return apicontract;
912 static void compute_delegate_iface_names(type_t *delegate, type_t *type, typeref_list_t *params)
914 type_t *iface = delegate->details.delegate.iface;
915 iface->namespace = delegate->namespace;
916 iface->name = strmake("I%s", delegate->name);
917 if (type) iface->c_name = format_parameterized_type_c_name(type, params, "I", "_C");
918 else iface->c_name = format_namespace(delegate->namespace, "__x_", "_C", iface->name, use_abi_namespace ? "ABI" : NULL);
919 if (type) iface->param_name = format_parameterized_type_c_name(type, params, "I", "__C");
920 else iface->param_name = format_namespace(delegate->namespace, "_", "__C", iface->name, NULL);
921 iface->qualified_name = format_namespace(delegate->namespace, "", "::", iface->name, use_abi_namespace ? "ABI" : NULL);
924 type_t *type_delegate_declare(char *name, struct namespace *namespace)
926 type_t *type = get_type(TYPE_DELEGATE, name, namespace, 0);
927 if (type_get_type_detect_alias( type ) != TYPE_DELEGATE)
928 error_loc( "delegate %s previously not declared a delegate at %s:%d\n", type->name,
929 type->where.input_name, type->where.first_line );
930 return type;
933 type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts)
935 type_t *iface;
937 if (delegate->defined)
938 error_loc( "delegate %s already defined at %s:%d\n", delegate->name,
939 delegate->where.input_name, delegate->where.first_line );
941 delegate->attrs = check_interface_attrs(delegate->name, attrs);
943 iface = make_type(TYPE_INTERFACE);
944 iface->attrs = delegate->attrs;
945 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
946 iface->details.iface->disp_props = NULL;
947 iface->details.iface->disp_methods = NULL;
948 iface->details.iface->stmts = stmts;
949 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
950 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
951 iface->details.iface->disp_inherit = NULL;
952 iface->details.iface->async_iface = NULL;
953 iface->details.iface->requires = NULL;
954 iface->defined = TRUE;
955 compute_method_indexes(iface);
957 delegate->details.delegate.iface = iface;
958 delegate->defined = TRUE;
959 compute_delegate_iface_names(delegate, NULL, NULL);
961 return delegate;
964 type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params)
966 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
967 if (type_get_type_detect_alias( type ) != TYPE_PARAMETERIZED_TYPE)
968 error_loc( "pinterface %s previously not declared a pinterface at %s:%d\n", type->name,
969 type->where.input_name, type->where.first_line );
970 type->details.parameterized.type = make_type(TYPE_INTERFACE);
971 type->details.parameterized.params = params;
972 return type;
975 type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
977 type_t *iface;
978 if (type->defined)
979 error_loc( "pinterface %s already defined at %s:%d\n", type->name,
980 type->where.input_name, type->where.first_line );
982 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
983 * a new type GUID with the rules described in:
984 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
985 * TODO: store type signatures for generated interfaces, and generate their GUIDs
987 type->attrs = check_interface_attrs(type->name, attrs);
989 iface = type->details.parameterized.type;
990 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
991 iface->details.iface->disp_props = NULL;
992 iface->details.iface->disp_methods = NULL;
993 iface->details.iface->stmts = stmts;
994 iface->details.iface->inherit = inherit;
995 iface->details.iface->disp_inherit = NULL;
996 iface->details.iface->async_iface = NULL;
997 iface->details.iface->requires = requires;
999 iface->name = type->name;
1001 type->defined = TRUE;
1002 return type;
1005 type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params)
1007 type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
1008 if (type_get_type_detect_alias( type ) != TYPE_PARAMETERIZED_TYPE)
1009 error_loc( "pdelegate %s previously not declared a pdelegate at %s:%d\n", type->name,
1010 type->where.input_name, type->where.first_line );
1011 type->details.parameterized.type = make_type(TYPE_DELEGATE);
1012 type->details.parameterized.params = params;
1013 return type;
1016 type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts)
1018 type_t *iface, *delegate;
1020 if (type->defined)
1021 error_loc( "pdelegate %s already defined at %s:%d\n", type->name,
1022 type->where.input_name, type->where.first_line );
1024 type->attrs = check_interface_attrs(type->name, attrs);
1026 delegate = type->details.parameterized.type;
1027 delegate->attrs = type->attrs;
1028 delegate->details.delegate.iface = make_type(TYPE_INTERFACE);
1030 iface = delegate->details.delegate.iface;
1031 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1032 iface->details.iface->disp_props = NULL;
1033 iface->details.iface->disp_methods = NULL;
1034 iface->details.iface->stmts = stmts;
1035 iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
1036 if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
1037 iface->details.iface->disp_inherit = NULL;
1038 iface->details.iface->async_iface = NULL;
1039 iface->details.iface->requires = NULL;
1041 delegate->name = type->name;
1042 compute_delegate_iface_names(delegate, type, type->details.parameterized.params);
1044 type->defined = TRUE;
1045 return type;
1048 type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params)
1050 type_t *new_type = duptype(type, 0);
1051 new_type->details.parameterized.type = type;
1052 new_type->details.parameterized.params = params;
1053 return new_type;
1056 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl);
1058 static typeref_list_t *replace_type_parameters_in_type_list(typeref_list_t *list, typeref_list_t *orig, typeref_list_t *repl)
1060 typeref_list_t *new_list = NULL;
1061 typeref_t *ref;
1063 if (!list) return list;
1065 LIST_FOR_EACH_ENTRY(ref, list, typeref_t, entry)
1067 type_t *new_type = replace_type_parameters_in_type(ref->type, orig, repl);
1068 new_list = append_typeref(new_list, make_typeref(new_type));
1071 return new_list;
1074 static var_t *replace_type_parameters_in_var(var_t *var, typeref_list_t *orig, typeref_list_t *repl)
1076 var_t *new_var = xmalloc(sizeof(*new_var));
1077 *new_var = *var;
1078 list_init(&new_var->entry);
1079 new_var->declspec.type = replace_type_parameters_in_type(var->declspec.type, orig, repl);
1080 return new_var;
1083 static var_list_t *replace_type_parameters_in_var_list(var_list_t *var_list, typeref_list_t *orig, typeref_list_t *repl)
1085 var_list_t *new_var_list;
1086 var_t *var, *new_var;
1088 if (!var_list) return var_list;
1090 new_var_list = xmalloc(sizeof(*new_var_list));
1091 list_init(new_var_list);
1093 LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
1095 new_var = replace_type_parameters_in_var(var, orig, repl);
1096 list_add_tail(new_var_list, &new_var->entry);
1099 return new_var_list;
1102 static statement_t *replace_type_parameters_in_statement( statement_t *stmt, typeref_list_t *orig,
1103 typeref_list_t *repl, struct location *where )
1105 statement_t *new_stmt = xmalloc(sizeof(*new_stmt));
1106 *new_stmt = *stmt;
1107 list_init(&new_stmt->entry);
1109 switch (stmt->type)
1111 case STMT_DECLARATION:
1112 new_stmt->u.var = replace_type_parameters_in_var(stmt->u.var, orig, repl);
1113 break;
1114 case STMT_TYPE:
1115 case STMT_TYPEREF:
1116 new_stmt->u.type = replace_type_parameters_in_type(stmt->u.type, orig, repl);
1117 break;
1118 case STMT_TYPEDEF:
1119 new_stmt->u.type_list = replace_type_parameters_in_type_list(stmt->u.type_list, orig, repl);
1120 break;
1121 case STMT_MODULE:
1122 case STMT_LIBRARY:
1123 case STMT_IMPORT:
1124 case STMT_IMPORTLIB:
1125 case STMT_PRAGMA:
1126 case STMT_CPPQUOTE:
1127 error_at( where, "unimplemented parameterized type replacement for statement type %d.\n", stmt->type );
1128 break;
1131 return new_stmt;
1134 static statement_list_t *replace_type_parameters_in_statement_list( statement_list_t *stmt_list, typeref_list_t *orig,
1135 typeref_list_t *repl, struct location *where )
1137 statement_list_t *new_stmt_list;
1138 statement_t *stmt, *new_stmt;
1140 if (!stmt_list) return stmt_list;
1142 new_stmt_list = xmalloc(sizeof(*new_stmt_list));
1143 list_init(new_stmt_list);
1145 LIST_FOR_EACH_ENTRY(stmt, stmt_list, statement_t, entry)
1147 new_stmt = replace_type_parameters_in_statement( stmt, orig, repl, where );
1148 list_add_tail(new_stmt_list, &new_stmt->entry);
1151 return new_stmt_list;
1154 static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl)
1156 struct list *o, *r;
1157 type_t *t;
1159 if (!type) return type;
1160 switch (type->type_type)
1162 case TYPE_VOID:
1163 case TYPE_BASIC:
1164 case TYPE_ENUM:
1165 case TYPE_BITFIELD:
1166 case TYPE_INTERFACE:
1167 case TYPE_RUNTIMECLASS:
1168 case TYPE_DELEGATE:
1169 case TYPE_STRUCT:
1170 case TYPE_ENCAPSULATED_UNION:
1171 case TYPE_UNION:
1172 return type;
1173 case TYPE_PARAMETER:
1174 if (!orig || !repl) return NULL;
1175 for (o = list_head(orig), r = list_head(repl); o && r;
1176 o = list_next(orig, o), r = list_next(repl, r))
1177 if (type == LIST_ENTRY(o, typeref_t, entry)->type)
1178 return LIST_ENTRY(r, typeref_t, entry)->type;
1179 return type;
1180 case TYPE_POINTER:
1181 t = replace_type_parameters_in_type(type->details.pointer.ref.type, orig, repl);
1182 if (t == type->details.pointer.ref.type) return type;
1183 type = duptype(type, 0);
1184 type->details.pointer.ref.type = t;
1185 return type;
1186 case TYPE_ALIAS:
1187 t = replace_type_parameters_in_type(type->details.alias.aliasee.type, orig, repl);
1188 if (t == type->details.alias.aliasee.type) return type;
1189 type = duptype(type, 0);
1190 type->details.alias.aliasee.type = t;
1191 return type;
1192 case TYPE_ARRAY:
1193 t = replace_type_parameters_in_type(type->details.array.elem.type, orig, repl);
1194 if (t == t->details.array.elem.type) return type;
1195 type = duptype(type, 0);
1196 t->details.array.elem.type = t;
1197 return type;
1198 case TYPE_FUNCTION:
1199 t = duptype(type, 0);
1200 t->details.function = xmalloc(sizeof(*t->details.function));
1201 t->details.function->args = replace_type_parameters_in_var_list(type->details.function->args, orig, repl);
1202 t->details.function->retval = replace_type_parameters_in_var(type->details.function->retval, orig, repl);
1203 return t;
1204 case TYPE_PARAMETERIZED_TYPE:
1205 t = type->details.parameterized.type;
1206 if (t->type_type != TYPE_PARAMETERIZED_TYPE) return find_parameterized_type(type, repl);
1207 repl = replace_type_parameters_in_type_list(type->details.parameterized.params, orig, repl);
1208 return replace_type_parameters_in_type(t, t->details.parameterized.params, repl);
1209 case TYPE_MODULE:
1210 case TYPE_COCLASS:
1211 case TYPE_APICONTRACT:
1212 error_at( &type->where, "unimplemented parameterized type replacement for type %s of type %d.\n",
1213 type->name, type->type_type );
1214 break;
1217 return type;
1220 static void type_parameterized_interface_specialize(type_t *tmpl, type_t *iface, typeref_list_t *orig, typeref_list_t *repl)
1222 iface->details.iface = xmalloc(sizeof(*iface->details.iface));
1223 iface->details.iface->disp_methods = NULL;
1224 iface->details.iface->disp_props = NULL;
1225 iface->details.iface->stmts = replace_type_parameters_in_statement_list( tmpl->details.iface->stmts,
1226 orig, repl, &tmpl->where );
1227 iface->details.iface->inherit = replace_type_parameters_in_type(tmpl->details.iface->inherit, orig, repl);
1228 iface->details.iface->disp_inherit = NULL;
1229 iface->details.iface->async_iface = NULL;
1230 iface->details.iface->requires = NULL;
1233 static void type_parameterized_delegate_specialize(type_t *tmpl, type_t *delegate, typeref_list_t *orig, typeref_list_t *repl)
1235 type_parameterized_interface_specialize(tmpl->details.delegate.iface, delegate->details.delegate.iface, orig, repl);
1238 type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params)
1240 type_t *tmpl = type->details.parameterized.type;
1241 type_t *new_type = duptype(tmpl, 0);
1243 new_type->namespace = type->namespace;
1244 new_type->name = format_parameterized_type_name(type, params);
1245 reg_type(new_type, new_type->name, new_type->namespace, 0);
1246 new_type->c_name = format_parameterized_type_c_name(type, params, "", "_C");
1247 new_type->short_name = format_parameterized_type_short_name(type, params, "");
1248 new_type->param_name = format_parameterized_type_c_name(type, params, "", "__C");
1250 if (new_type->type_type == TYPE_DELEGATE)
1252 new_type->details.delegate.iface = duptype(tmpl->details.delegate.iface, 0);
1253 compute_delegate_iface_names(new_type, type, params);
1254 new_type->details.delegate.iface->short_name = format_parameterized_type_short_name(type, params, "I");
1257 return new_type;
1260 static void compute_interface_signature_uuid(type_t *iface)
1262 static const char winrt_pinterface_namespace[] = {0x11,0xf4,0x7a,0xd5,0x7b,0x73,0x42,0xc0,0xab,0xae,0x87,0x8b,0x1e,0x16,0xad,0xee};
1263 static const int version = 5;
1264 struct sha1_context ctx;
1265 unsigned char hash[20];
1266 struct uuid *uuid;
1268 if (!(uuid = get_attrp(iface->attrs, ATTR_UUID)))
1270 uuid = xmalloc(sizeof(*uuid));
1271 iface->attrs = append_attr( iface->attrs, attr_ptr( iface->where, ATTR_UUID, uuid ) );
1274 sha1_init(&ctx);
1275 sha1_update(&ctx, winrt_pinterface_namespace, sizeof(winrt_pinterface_namespace));
1276 sha1_update(&ctx, iface->signature, strlen(iface->signature));
1277 sha1_finalize(&ctx, (unsigned int *)hash);
1279 /* https://tools.ietf.org/html/rfc4122:
1281 * Set the four most significant bits (bits 12 through 15) of the
1282 time_hi_and_version field to the appropriate 4-bit version number
1283 from Section 4.1.3.
1285 * Set the two most significant bits (bits 6 and 7) of the
1286 clock_seq_hi_and_reserved to zero and one, respectively.
1289 hash[6] = ((hash[6] & 0x0f) | (version << 4));
1290 hash[8] = ((hash[8] & 0x3f) | 0x80);
1292 uuid->Data1 = ((unsigned int)hash[0] << 24) | ((unsigned int)hash[1] << 16) | ((unsigned int)hash[2] << 8) | hash[3];
1293 uuid->Data2 = ((unsigned short)hash[4] << 8) | hash[5];
1294 uuid->Data3 = ((unsigned short)hash[6] << 8) | hash[7];
1295 memcpy(&uuid->Data4, hash + 8, sizeof(*uuid) - offsetof(struct uuid, Data4));
1298 type_t *type_parameterized_type_specialize_define(type_t *type)
1300 type_t *tmpl = type->details.parameterized.type;
1301 typeref_list_t *orig = tmpl->details.parameterized.params;
1302 typeref_list_t *repl = type->details.parameterized.params;
1303 type_t *iface = find_parameterized_type(tmpl, repl);
1305 if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE ||
1306 type_get_type_detect_alias(tmpl) != TYPE_PARAMETERIZED_TYPE)
1307 error_loc( "cannot define non-parameterized type %s, declared at %s:%d\n", type->name,
1308 type->where.input_name, type->where.first_line );
1310 if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_INTERFACE &&
1311 type_get_type_detect_alias(iface) == TYPE_INTERFACE)
1312 type_parameterized_interface_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1313 else if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_DELEGATE &&
1314 type_get_type_detect_alias(iface) == TYPE_DELEGATE)
1315 type_parameterized_delegate_specialize(tmpl->details.parameterized.type, iface, orig, repl);
1316 else
1317 error_loc("pinterface/pdelegate %s previously not declared a pinterface/pdelegate at %s:%d\n",
1318 iface->name, iface->where.input_name, iface->where.first_line);
1320 iface->impl_name = format_parameterized_type_impl_name(type, repl, "");
1321 iface->signature = format_parameterized_type_signature(type, repl);
1322 iface->defined = TRUE;
1323 if (iface->type_type == TYPE_DELEGATE)
1325 iface = iface->details.delegate.iface;
1326 iface->impl_name = format_parameterized_type_impl_name(type, repl, "I");
1327 iface->signature = format_parameterized_type_signature(type, repl);
1328 iface->defined = TRUE;
1330 compute_interface_signature_uuid(iface);
1331 compute_method_indexes(iface);
1332 return iface;
1335 int type_is_equal(const type_t *type1, const type_t *type2)
1337 if (type1 == type2)
1338 return TRUE;
1339 if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
1340 return FALSE;
1341 if (type1->namespace != type2->namespace)
1342 return FALSE;
1344 if (type1->name && type2->name)
1345 return !strcmp(type1->name, type2->name);
1346 else if ((!type1->name && type2->name) || (type1->name && !type2->name))
1347 return FALSE;
1349 /* FIXME: do deep inspection of types to determine if they are equal */
1351 return FALSE;