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
33 type_t
*duptype(type_t
*t
, int dupname
)
35 type_t
*d
= alloc_type();
38 if (dupname
&& t
->name
)
39 d
->name
= xstrdup(t
->name
);
44 type_t
*make_type(enum type_type type
)
46 type_t
*t
= alloc_type();
52 memset(&t
->details
, 0, sizeof(t
->details
));
53 t
->typestring_offset
= 0;
55 t
->ignore
= (parse_only
!= 0);
58 t
->user_types_registered
= FALSE
;
62 init_loc_info(&t
->loc_info
);
66 static const var_t
*find_arg(const var_list_t
*args
, const char *name
)
70 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, const var_t
, entry
)
72 if (arg
->name
&& !strcmp(name
, arg
->name
))
79 const char *type_get_name(const type_t
*type
, enum name_type name_type
)
85 return type
->c_name
? type
->c_name
: type
->name
;
92 static size_t append_namespace(char **buf
, size_t *len
, size_t pos
, struct namespace *namespace, const char *separator
, const char *abi_prefix
)
94 int nested
= namespace && !is_global_namespace(namespace);
95 const char *name
= nested
? namespace->name
: abi_prefix
;
98 if (nested
) n
+= append_namespace(buf
, len
, pos
+ n
, namespace->parent
, separator
, abi_prefix
);
99 n
+= strappend(buf
, len
, pos
+ n
, "%s%s", name
, separator
);
103 static size_t append_namespaces(char **buf
, size_t *len
, size_t pos
, struct namespace *namespace, const char *prefix
,
104 const char *separator
, const char *suffix
, const char *abi_prefix
)
107 n
+= strappend(buf
, len
, pos
+ n
, "%s", prefix
);
108 n
+= append_namespace(buf
, len
, pos
+ n
, namespace, separator
, abi_prefix
);
109 n
+= strappend(buf
, len
, pos
+ n
, "%s", suffix
);
113 char *format_namespace(struct namespace *namespace, const char *prefix
, const char *separator
, const char *suffix
, const char *abi_prefix
)
117 append_namespaces(&buf
, &len
, 0, namespace, prefix
, separator
, suffix
, abi_prefix
);
121 char *format_parameterized_type_name(type_t
*type
, typeref_list_t
*params
)
123 size_t len
= 0, pos
= 0;
127 pos
+= strappend(&buf
, &len
, pos
, "%s<", type
->name
);
128 if (params
) LIST_FOR_EACH_ENTRY(ref
, params
, typeref_t
, entry
)
130 for (type
= ref
->type
; type
->type_type
== TYPE_POINTER
; type
= type_pointer_get_ref_type(type
)) {}
131 pos
+= append_namespaces(&buf
, &len
, pos
, type
->namespace, "", "::", type
->name
, use_abi_namespace
? "ABI" : NULL
);
132 for (type
= ref
->type
; type
->type_type
== TYPE_POINTER
; type
= type_pointer_get_ref_type(type
)) pos
+= strappend(&buf
, &len
, pos
, "*");
133 if (list_next(params
, &ref
->entry
)) pos
+= strappend(&buf
, &len
, pos
, ",");
135 pos
+= strappend(&buf
, &len
, pos
, ">");
140 static char const *parameterized_type_shorthands
[][2] = {
141 {"Windows_CFoundation_CCollections_C", "__F"},
142 {"Windows_CFoundation_C", "__F"},
145 static char *format_parameterized_type_c_name(type_t
*type
, typeref_list_t
*params
)
147 size_t len
= 0, pos
= 0;
148 char *buf
= NULL
, *tmp
;
149 int i
, count
= params
? list_count(params
) : 0;
152 pos
+= append_namespaces(&buf
, &len
, pos
, type
->namespace, "__x_", "_C", type
->name
, use_abi_namespace
? "ABI" : NULL
);
153 pos
+= strappend(&buf
, &len
, pos
, "_%d", count
);
154 if (params
) LIST_FOR_EACH_ENTRY(ref
, params
, typeref_t
, entry
)
156 for (type
= ref
->type
; type
->type_type
== TYPE_POINTER
; type
= type_pointer_get_ref_type(type
)) {}
157 pos
+= append_namespaces(&buf
, &len
, pos
, type
->namespace, "_", "__C", type
->name
, NULL
);
160 for (i
= 0; i
< ARRAY_SIZE(parameterized_type_shorthands
); ++i
)
162 if ((tmp
= strstr(buf
, parameterized_type_shorthands
[i
][0])) &&
163 (tmp
- buf
) == strlen(use_abi_namespace
? "__x_ABI_C" : "__x_C"))
165 tmp
+= strlen(parameterized_type_shorthands
[i
][0]);
166 strcpy(buf
, parameterized_type_shorthands
[i
][1]);
167 memmove(buf
+ 3, tmp
, len
- (tmp
- buf
));
174 type_t
*type_new_function(var_list_t
*args
)
182 arg
= LIST_ENTRY(list_head(args
), var_t
, entry
);
183 if (list_count(args
) == 1 && !arg
->name
&& arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
185 list_remove(&arg
->entry
);
191 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, var_t
, entry
)
193 if (arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
194 error_loc("argument '%s' has void type\n", arg
->name
);
198 error_loc("too many unnamed arguments\n");
205 name
[0] = i
> 26 ? 'a' + i
/ 26 : 'a' + i
;
206 name
[1] = i
> 26 ? 'a' + i
% 26 : 0;
208 unique
= !find_arg(args
, name
);
210 arg
->name
= xstrdup(name
);
217 t
= make_type(TYPE_FUNCTION
);
218 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
219 t
->details
.function
->args
= args
;
220 t
->details
.function
->retval
= make_var(xstrdup("_RetVal"));
224 type_t
*type_new_pointer(type_t
*ref
)
226 type_t
*t
= make_type(TYPE_POINTER
);
227 t
->details
.pointer
.ref
.type
= ref
;
231 type_t
*type_new_alias(const decl_spec_t
*t
, const char *name
)
233 type_t
*a
= make_type(TYPE_ALIAS
);
235 a
->name
= xstrdup(name
);
237 a
->details
.alias
.aliasee
= *t
;
238 init_loc_info(&a
->loc_info
);
243 type_t
*type_new_array(const char *name
, const decl_spec_t
*element
, int declptr
,
244 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
)
246 type_t
*t
= make_type(TYPE_ARRAY
);
247 if (name
) t
->name
= xstrdup(name
);
248 t
->details
.array
.declptr
= declptr
;
249 t
->details
.array
.length_is
= length_is
;
251 t
->details
.array
.size_is
= size_is
;
253 t
->details
.array
.dim
= dim
;
255 t
->details
.array
.elem
= *element
;
259 type_t
*type_new_basic(enum type_basic_type basic_type
)
261 type_t
*t
= make_type(TYPE_BASIC
);
262 t
->details
.basic
.type
= basic_type
;
263 t
->details
.basic
.sign
= 0;
267 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
269 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
271 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
273 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
274 if (!int_types
[basic_type
][sign
+ 1])
276 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
277 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
279 return int_types
[basic_type
][sign
+ 1];
282 type_t
*type_new_void(void)
284 static type_t
*void_type
= NULL
;
286 void_type
= make_type(TYPE_VOID
);
290 type_t
*type_new_enum(const char *name
, struct namespace *namespace, int defined
, var_list_t
*enums
)
295 t
= find_type(name
, namespace,tsENUM
);
299 t
= make_type(TYPE_ENUM
);
301 t
->namespace = namespace;
303 reg_type(t
, name
, namespace, tsENUM
);
306 if (!t
->defined
&& defined
)
308 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
309 t
->details
.enumeration
->enums
= enums
;
313 error_loc("redefinition of enum %s\n", name
);
318 type_t
*type_new_struct(char *name
, struct namespace *namespace, int defined
, var_list_t
*fields
)
323 t
= find_type(name
, namespace, tsSTRUCT
);
327 t
= make_type(TYPE_STRUCT
);
329 t
->namespace = namespace;
331 reg_type(t
, name
, namespace, tsSTRUCT
);
334 if (!t
->defined
&& defined
)
336 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
337 t
->details
.structure
->fields
= fields
;
341 error_loc("redefinition of struct %s\n", name
);
346 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
351 t
= find_type(name
, NULL
, tsUNION
);
355 t
= make_type(TYPE_UNION
);
358 reg_type(t
, name
, NULL
, tsUNION
);
361 if (!t
->defined
&& defined
)
363 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
364 t
->details
.structure
->fields
= fields
;
368 error_loc("redefinition of union %s\n", name
);
373 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
378 t
= find_type(name
, NULL
, tsUNION
);
382 t
= make_type(TYPE_ENCAPSULATED_UNION
);
385 reg_type(t
, name
, NULL
, tsUNION
);
387 t
->type_type
= TYPE_ENCAPSULATED_UNION
;
392 union_field
= make_var(xstrdup("tagged_union"));
393 union_field
->declspec
.type
= type_new_nonencapsulated_union(gen_name(), TRUE
, cases
);
395 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
396 t
->details
.structure
->fields
= append_var(NULL
, switch_field
);
397 t
->details
.structure
->fields
= append_var(t
->details
.structure
->fields
, union_field
);
401 error_loc("redefinition of union %s\n", name
);
406 static int is_valid_bitfield_type(const type_t
*type
)
408 switch (type_get_type(type
))
413 switch (type_basic_get_type(type
))
415 case TYPE_BASIC_INT8
:
416 case TYPE_BASIC_INT16
:
417 case TYPE_BASIC_INT32
:
418 case TYPE_BASIC_INT64
:
420 case TYPE_BASIC_INT3264
:
421 case TYPE_BASIC_LONG
:
422 case TYPE_BASIC_CHAR
:
423 case TYPE_BASIC_HYPER
:
424 case TYPE_BASIC_BYTE
:
425 case TYPE_BASIC_WCHAR
:
426 case TYPE_BASIC_ERROR_STATUS_T
:
428 case TYPE_BASIC_FLOAT
:
429 case TYPE_BASIC_DOUBLE
:
430 case TYPE_BASIC_HANDLE
:
439 type_t
*type_new_bitfield(type_t
*field
, const expr_t
*bits
)
443 if (!is_valid_bitfield_type(field
))
444 error_loc("bit-field has invalid type\n");
447 error_loc("negative width for bit-field\n");
449 /* FIXME: validate bits->cval <= memsize(field) * 8 */
451 t
= make_type(TYPE_BITFIELD
);
452 t
->details
.bitfield
.field
= field
;
453 t
->details
.bitfield
.bits
= bits
;
457 static unsigned int compute_method_indexes(type_t
*iface
)
462 if (!iface
->details
.iface
)
465 if (type_iface_get_inherit(iface
))
466 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
470 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
472 var_t
*func
= stmt
->u
.var
;
473 if (!is_callas(func
->attrs
))
474 func
->func_idx
= idx
++;
480 type_t
*type_interface_declare(char *name
, struct namespace *namespace)
482 type_t
*type
= get_type(TYPE_INTERFACE
, name
, namespace, 0);
483 if (type_get_type_detect_alias(type
) != TYPE_INTERFACE
)
484 error_loc("interface %s previously not declared an interface at %s:%d\n",
485 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
489 type_t
*type_interface_define(type_t
*iface
, attr_list_t
*attrs
, type_t
*inherit
, statement_list_t
*stmts
, typeref_list_t
*requires
)
492 error_loc("interface %s already defined at %s:%d\n",
493 iface
->name
, iface
->loc_info
.input_name
, iface
->loc_info
.line_number
);
494 if (iface
== inherit
)
495 error_loc("interface %s can't inherit from itself\n",
497 iface
->attrs
= check_interface_attrs(iface
->name
, attrs
);
498 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
499 iface
->details
.iface
->disp_props
= NULL
;
500 iface
->details
.iface
->disp_methods
= NULL
;
501 iface
->details
.iface
->stmts
= stmts
;
502 iface
->details
.iface
->inherit
= inherit
;
503 iface
->details
.iface
->disp_inherit
= NULL
;
504 iface
->details
.iface
->async_iface
= NULL
;
505 iface
->details
.iface
->requires
= requires
;
506 iface
->defined
= TRUE
;
507 compute_method_indexes(iface
);
511 type_t
*type_dispinterface_declare(char *name
)
513 type_t
*type
= get_type(TYPE_INTERFACE
, name
, NULL
, 0);
514 if (type_get_type_detect_alias(type
) != TYPE_INTERFACE
)
515 error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
516 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
520 type_t
*type_dispinterface_define(type_t
*iface
, attr_list_t
*attrs
, var_list_t
*props
, var_list_t
*methods
)
523 error_loc("dispinterface %s already defined at %s:%d\n",
524 iface
->name
, iface
->loc_info
.input_name
, iface
->loc_info
.line_number
);
525 iface
->attrs
= check_dispiface_attrs(iface
->name
, attrs
);
526 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
527 iface
->details
.iface
->disp_props
= props
;
528 iface
->details
.iface
->disp_methods
= methods
;
529 iface
->details
.iface
->stmts
= NULL
;
530 iface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
531 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
532 iface
->details
.iface
->disp_inherit
= NULL
;
533 iface
->details
.iface
->async_iface
= NULL
;
534 iface
->details
.iface
->requires
= NULL
;
535 iface
->defined
= TRUE
;
536 compute_method_indexes(iface
);
540 type_t
*type_dispinterface_define_from_iface(type_t
*dispiface
, attr_list_t
*attrs
, type_t
*iface
)
542 if (dispiface
->defined
)
543 error_loc("dispinterface %s already defined at %s:%d\n",
544 dispiface
->name
, dispiface
->loc_info
.input_name
, dispiface
->loc_info
.line_number
);
545 dispiface
->attrs
= check_dispiface_attrs(dispiface
->name
, attrs
);
546 dispiface
->details
.iface
= xmalloc(sizeof(*dispiface
->details
.iface
));
547 dispiface
->details
.iface
->disp_props
= NULL
;
548 dispiface
->details
.iface
->disp_methods
= NULL
;
549 dispiface
->details
.iface
->stmts
= NULL
;
550 dispiface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
551 if (!dispiface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
552 dispiface
->details
.iface
->disp_inherit
= iface
;
553 dispiface
->details
.iface
->async_iface
= NULL
;
554 dispiface
->details
.iface
->requires
= NULL
;
555 dispiface
->defined
= TRUE
;
556 compute_method_indexes(dispiface
);
560 type_t
*type_module_declare(char *name
)
562 type_t
*type
= get_type(TYPE_MODULE
, name
, NULL
, 0);
563 if (type_get_type_detect_alias(type
) != TYPE_MODULE
)
564 error_loc("module %s previously not declared a module at %s:%d\n",
565 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
569 type_t
*type_module_define(type_t
* module
, attr_list_t
*attrs
, statement_list_t
*stmts
)
572 error_loc("module %s already defined at %s:%d\n",
573 module
->name
, module
->loc_info
.input_name
, module
->loc_info
.line_number
);
574 module
->attrs
= check_module_attrs(module
->name
, attrs
);
575 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
576 module
->details
.module
->stmts
= stmts
;
577 module
->defined
= TRUE
;
581 type_t
*type_coclass_declare(char *name
)
583 type_t
*type
= get_type(TYPE_COCLASS
, name
, NULL
, 0);
584 if (type_get_type_detect_alias(type
) != TYPE_COCLASS
)
585 error_loc("coclass %s previously not declared a coclass at %s:%d\n",
586 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
590 type_t
*type_coclass_define(type_t
*coclass
, attr_list_t
*attrs
, typeref_list_t
*ifaces
)
592 if (coclass
->defined
)
593 error_loc("coclass %s already defined at %s:%d\n",
594 coclass
->name
, coclass
->loc_info
.input_name
, coclass
->loc_info
.line_number
);
595 coclass
->attrs
= check_coclass_attrs(coclass
->name
, attrs
);
596 coclass
->details
.coclass
.ifaces
= ifaces
;
597 coclass
->defined
= TRUE
;
601 type_t
*type_runtimeclass_declare(char *name
, struct namespace *namespace)
603 type_t
*type
= get_type(TYPE_RUNTIMECLASS
, name
, namespace, 0);
604 if (type_get_type_detect_alias(type
) != TYPE_RUNTIMECLASS
)
605 error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
606 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
610 type_t
*type_runtimeclass_define(type_t
*runtimeclass
, attr_list_t
*attrs
, typeref_list_t
*ifaces
)
612 typeref_t
*ref
, *required
, *tmp
;
613 typeref_list_t
*requires
;
615 if (runtimeclass
->defined
)
616 error_loc("runtimeclass %s already defined at %s:%d\n",
617 runtimeclass
->name
, runtimeclass
->loc_info
.input_name
, runtimeclass
->loc_info
.line_number
);
618 runtimeclass
->attrs
= check_runtimeclass_attrs(runtimeclass
->name
, attrs
);
619 runtimeclass
->details
.runtimeclass
.ifaces
= ifaces
;
620 runtimeclass
->defined
= TRUE
;
621 if (!type_runtimeclass_get_default_iface(runtimeclass
))
622 error_loc("missing default interface on runtimeclass %s\n", runtimeclass
->name
);
624 LIST_FOR_EACH_ENTRY(ref
, ifaces
, typeref_t
, entry
)
626 /* FIXME: this should probably not be allowed, here or in coclass, */
627 /* but for now there's too many places in Wine IDL where it is to */
628 /* even print a warning. */
629 if (!(ref
->type
->defined
)) continue;
630 if (!(requires
= type_iface_get_requires(ref
->type
))) continue;
631 LIST_FOR_EACH_ENTRY(required
, requires
, typeref_t
, entry
)
635 LIST_FOR_EACH_ENTRY(tmp
, ifaces
, typeref_t
, entry
)
636 if ((found
= type_is_equal(tmp
->type
, required
->type
))) break;
639 error_loc("interface '%s' also requires interface '%s', "
640 "but runtimeclass '%s' does not implement it.\n",
641 ref
->type
->name
, required
->type
->name
, runtimeclass
->name
);
648 type_t
*type_apicontract_declare(char *name
, struct namespace *namespace)
650 type_t
*type
= get_type(TYPE_APICONTRACT
, name
, namespace, 0);
651 if (type_get_type_detect_alias(type
) != TYPE_APICONTRACT
)
652 error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
653 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
657 type_t
*type_apicontract_define(type_t
*apicontract
, attr_list_t
*attrs
)
659 if (apicontract
->defined
)
660 error_loc("apicontract %s already defined at %s:%d\n",
661 apicontract
->name
, apicontract
->loc_info
.input_name
, apicontract
->loc_info
.line_number
);
662 apicontract
->attrs
= check_apicontract_attrs(apicontract
->name
, attrs
);
663 apicontract
->defined
= TRUE
;
667 type_t
*type_parameterized_interface_declare(char *name
, struct namespace *namespace, typeref_list_t
*params
)
669 type_t
*type
= get_type(TYPE_PARAMETERIZED_TYPE
, name
, namespace, 0);
670 if (type_get_type_detect_alias(type
) != TYPE_PARAMETERIZED_TYPE
)
671 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
672 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
673 type
->details
.parameterized
.type
= make_type(TYPE_INTERFACE
);
674 type
->details
.parameterized
.params
= params
;
678 type_t
*type_parameterized_interface_define(type_t
*type
, attr_list_t
*attrs
, type_t
*inherit
, statement_list_t
*stmts
, typeref_list_t
*requires
)
682 error_loc("pinterface %s already defined at %s:%d\n",
683 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
685 /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
686 * a new type GUID with the rules described in:
687 * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
688 * TODO: store type signatures for generated interfaces, and generate their GUIDs
690 type
->attrs
= check_interface_attrs(type
->name
, attrs
);
692 iface
= type
->details
.parameterized
.type
;
693 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
694 iface
->details
.iface
->disp_props
= NULL
;
695 iface
->details
.iface
->disp_methods
= NULL
;
696 iface
->details
.iface
->stmts
= stmts
;
697 iface
->details
.iface
->inherit
= inherit
;
698 iface
->details
.iface
->disp_inherit
= NULL
;
699 iface
->details
.iface
->async_iface
= NULL
;
700 iface
->details
.iface
->requires
= requires
;
702 type
->defined
= TRUE
;
706 type_t
*type_parameterized_type_specialize_partial(type_t
*type
, typeref_list_t
*params
)
708 type_t
*new_type
= duptype(type
, 0);
709 new_type
->details
.parameterized
.type
= type
;
710 new_type
->details
.parameterized
.params
= params
;
714 static type_t
*replace_type_parameters_in_type(type_t
*type
, typeref_list_t
*orig
, typeref_list_t
*repl
);
716 static typeref_list_t
*replace_type_parameters_in_type_list(typeref_list_t
*list
, typeref_list_t
*orig
, typeref_list_t
*repl
)
718 typeref_list_t
*new_list
= NULL
;
721 if (!list
) return list
;
723 LIST_FOR_EACH_ENTRY(ref
, list
, typeref_t
, entry
)
725 type_t
*new_type
= replace_type_parameters_in_type(ref
->type
, orig
, repl
);
726 new_list
= append_typeref(new_list
, make_typeref(new_type
));
732 static var_t
*replace_type_parameters_in_var(var_t
*var
, typeref_list_t
*orig
, typeref_list_t
*repl
)
734 var_t
*new_var
= xmalloc(sizeof(*new_var
));
736 list_init(&new_var
->entry
);
737 new_var
->declspec
.type
= replace_type_parameters_in_type(var
->declspec
.type
, orig
, repl
);
741 static var_list_t
*replace_type_parameters_in_var_list(var_list_t
*var_list
, typeref_list_t
*orig
, typeref_list_t
*repl
)
743 var_list_t
*new_var_list
;
744 var_t
*var
, *new_var
;
746 if (!var_list
) return var_list
;
748 new_var_list
= xmalloc(sizeof(*new_var_list
));
749 list_init(new_var_list
);
751 LIST_FOR_EACH_ENTRY(var
, var_list
, var_t
, entry
)
753 new_var
= replace_type_parameters_in_var(var
, orig
, repl
);
754 list_add_tail(new_var_list
, &new_var
->entry
);
760 static statement_t
*replace_type_parameters_in_statement(statement_t
*stmt
, typeref_list_t
*orig
, typeref_list_t
*repl
, loc_info_t
*loc
)
762 statement_t
*new_stmt
= xmalloc(sizeof(*new_stmt
));
764 list_init(&new_stmt
->entry
);
768 case STMT_DECLARATION
:
769 new_stmt
->u
.var
= replace_type_parameters_in_var(stmt
->u
.var
, orig
, repl
);
773 new_stmt
->u
.type
= replace_type_parameters_in_type(stmt
->u
.type
, orig
, repl
);
776 new_stmt
->u
.type_list
= replace_type_parameters_in_type_list(stmt
->u
.type_list
, orig
, repl
);
784 error_loc_info(loc
, "unimplemented parameterized type replacement for statement type %d.\n", stmt
->type
);
791 static statement_list_t
*replace_type_parameters_in_statement_list(statement_list_t
*stmt_list
, typeref_list_t
*orig
, typeref_list_t
*repl
, loc_info_t
*loc
)
793 statement_list_t
*new_stmt_list
;
794 statement_t
*stmt
, *new_stmt
;
796 if (!stmt_list
) return stmt_list
;
798 new_stmt_list
= xmalloc(sizeof(*new_stmt_list
));
799 list_init(new_stmt_list
);
801 LIST_FOR_EACH_ENTRY(stmt
, stmt_list
, statement_t
, entry
)
803 new_stmt
= replace_type_parameters_in_statement(stmt
, orig
, repl
, loc
);
804 list_add_tail(new_stmt_list
, &new_stmt
->entry
);
807 return new_stmt_list
;
810 static type_t
*replace_type_parameters_in_type(type_t
*type
, typeref_list_t
*orig
, typeref_list_t
*repl
)
815 if (!type
) return type
;
816 switch (type
->type_type
)
823 case TYPE_RUNTIMECLASS
:
826 if (!orig
|| !repl
) return NULL
;
827 for (o
= list_head(orig
), r
= list_head(repl
); o
&& r
;
828 o
= list_next(orig
, o
), r
= list_next(repl
, r
))
829 if (type
== LIST_ENTRY(o
, typeref_t
, entry
)->type
)
830 return LIST_ENTRY(r
, typeref_t
, entry
)->type
;
833 t
= replace_type_parameters_in_type(type
->details
.pointer
.ref
.type
, orig
, repl
);
834 if (t
== type
->details
.pointer
.ref
.type
) return type
;
835 type
= duptype(type
, 0);
836 type
->details
.pointer
.ref
.type
= t
;
839 t
= replace_type_parameters_in_type(type
->details
.alias
.aliasee
.type
, orig
, repl
);
840 if (t
== type
->details
.alias
.aliasee
.type
) return type
;
841 type
= duptype(type
, 0);
842 type
->details
.alias
.aliasee
.type
= t
;
845 t
= replace_type_parameters_in_type(type
->details
.array
.elem
.type
, orig
, repl
);
846 if (t
== t
->details
.array
.elem
.type
) return type
;
847 type
= duptype(type
, 0);
848 t
->details
.array
.elem
.type
= t
;
851 t
= duptype(type
, 0);
852 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
853 t
->details
.function
->args
= replace_type_parameters_in_var_list(type
->details
.function
->args
, orig
, repl
);
854 t
->details
.function
->retval
= replace_type_parameters_in_var(type
->details
.function
->retval
, orig
, repl
);
856 case TYPE_PARAMETERIZED_TYPE
:
857 t
= type
->details
.parameterized
.type
;
858 if (t
->type_type
!= TYPE_PARAMETERIZED_TYPE
) return find_parameterized_type(type
, repl
);
859 repl
= replace_type_parameters_in_type_list(type
->details
.parameterized
.params
, orig
, repl
);
860 return replace_type_parameters_in_type(t
, t
->details
.parameterized
.params
, repl
);
862 case TYPE_ENCAPSULATED_UNION
:
866 case TYPE_APICONTRACT
:
867 error_loc_info(&type
->loc_info
, "unimplemented parameterized type replacement for type %s of type %d.\n", type
->name
, type
->type_type
);
874 static void type_parameterized_interface_specialize(type_t
*tmpl
, type_t
*iface
, typeref_list_t
*orig
, typeref_list_t
*repl
)
876 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
877 iface
->details
.iface
->disp_methods
= NULL
;
878 iface
->details
.iface
->disp_props
= NULL
;
879 iface
->details
.iface
->stmts
= replace_type_parameters_in_statement_list(tmpl
->details
.iface
->stmts
, orig
, repl
, &tmpl
->loc_info
);
880 iface
->details
.iface
->inherit
= replace_type_parameters_in_type(tmpl
->details
.iface
->inherit
, orig
, repl
);
881 iface
->details
.iface
->disp_inherit
= NULL
;
882 iface
->details
.iface
->async_iface
= NULL
;
883 iface
->details
.iface
->requires
= NULL
;
886 type_t
*type_parameterized_type_specialize_declare(type_t
*type
, typeref_list_t
*params
)
888 type_t
*tmpl
= type
->details
.parameterized
.type
;
889 type_t
*new_type
= duptype(tmpl
, 0);
891 new_type
->namespace = type
->namespace;
892 new_type
->name
= format_parameterized_type_name(type
, params
);
893 reg_type(new_type
, new_type
->name
, new_type
->namespace, 0);
894 new_type
->c_name
= format_parameterized_type_c_name(type
, params
);
899 type_t
*type_parameterized_type_specialize_define(type_t
*type
)
901 type_t
*tmpl
= type
->details
.parameterized
.type
;
902 typeref_list_t
*orig
= tmpl
->details
.parameterized
.params
;
903 typeref_list_t
*repl
= type
->details
.parameterized
.params
;
904 type_t
*iface
= find_parameterized_type(tmpl
, repl
);
906 if (type_get_type_detect_alias(type
) != TYPE_PARAMETERIZED_TYPE
||
907 type_get_type_detect_alias(tmpl
) != TYPE_PARAMETERIZED_TYPE
)
908 error_loc("cannot define non-parameterized type %s, declared at %s:%d\n",
909 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
911 if (type_get_type_detect_alias(tmpl
->details
.parameterized
.type
) == TYPE_INTERFACE
&&
912 type_get_type_detect_alias(iface
) == TYPE_INTERFACE
)
913 type_parameterized_interface_specialize(tmpl
->details
.parameterized
.type
, iface
, orig
, repl
);
915 error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
916 iface
->name
, iface
->loc_info
.input_name
, iface
->loc_info
.line_number
);
918 iface
->defined
= TRUE
;
919 compute_method_indexes(iface
);
923 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
927 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
929 if (type1
->namespace != type2
->namespace)
932 if (type1
->name
&& type2
->name
)
933 return !strcmp(type1
->name
, type2
->name
);
934 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
937 /* FIXME: do deep inspection of types to determine if they are equal */