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 char *append_namespace(char *ptr
, struct namespace *namespace, const char *separator
, const char *abi_prefix
)
94 if(is_global_namespace(namespace)) {
95 if(!abi_prefix
) return ptr
;
96 strcpy(ptr
, abi_prefix
);
97 strcat(ptr
, separator
);
98 return ptr
+ strlen(ptr
);
101 ptr
= append_namespace(ptr
, namespace->parent
, separator
, abi_prefix
);
102 strcpy(ptr
, namespace->name
);
103 strcat(ptr
, separator
);
104 return ptr
+ strlen(ptr
);
107 char *format_namespace(struct namespace *namespace, const char *prefix
, const char *separator
, const char *suffix
,
108 const char *abi_prefix
)
110 unsigned len
= strlen(prefix
) + strlen(suffix
);
111 unsigned sep_len
= strlen(separator
);
112 struct namespace *iter
;
116 len
+= strlen(abi_prefix
) + sep_len
;
118 for(iter
= namespace; !is_global_namespace(iter
); iter
= iter
->parent
)
119 len
+= strlen(iter
->name
) + sep_len
;
121 ret
= xmalloc(len
+1);
123 ptr
= append_namespace(ret
+ strlen(ret
), namespace, separator
, abi_prefix
);
129 type_t
*type_new_function(var_list_t
*args
)
137 arg
= LIST_ENTRY(list_head(args
), var_t
, entry
);
138 if (list_count(args
) == 1 && !arg
->name
&& arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
140 list_remove(&arg
->entry
);
146 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, var_t
, entry
)
148 if (arg
->declspec
.type
&& type_get_type(arg
->declspec
.type
) == TYPE_VOID
)
149 error_loc("argument '%s' has void type\n", arg
->name
);
153 error_loc("too many unnamed arguments\n");
160 name
[0] = i
> 26 ? 'a' + i
/ 26 : 'a' + i
;
161 name
[1] = i
> 26 ? 'a' + i
% 26 : 0;
163 unique
= !find_arg(args
, name
);
165 arg
->name
= xstrdup(name
);
172 t
= make_type(TYPE_FUNCTION
);
173 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
174 t
->details
.function
->args
= args
;
175 t
->details
.function
->retval
= make_var(xstrdup("_RetVal"));
179 type_t
*type_new_pointer(type_t
*ref
)
181 type_t
*t
= make_type(TYPE_POINTER
);
182 t
->details
.pointer
.ref
.type
= ref
;
186 type_t
*type_new_alias(const decl_spec_t
*t
, const char *name
)
188 type_t
*a
= make_type(TYPE_ALIAS
);
190 a
->name
= xstrdup(name
);
192 a
->details
.alias
.aliasee
= *t
;
193 init_loc_info(&a
->loc_info
);
198 type_t
*type_new_module(char *name
)
200 type_t
*type
= get_type(TYPE_MODULE
, name
, NULL
, 0);
201 if (type
->type_type
!= TYPE_MODULE
|| type
->defined
)
202 error_loc("%s: redefinition error; original definition was at %s:%d\n",
203 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
208 type_t
*type_new_coclass(char *name
)
210 type_t
*type
= get_type(TYPE_COCLASS
, name
, NULL
, 0);
211 if (type
->type_type
!= TYPE_COCLASS
|| type
->defined
)
212 error_loc("%s: redefinition error; original definition was at %s:%d\n",
213 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
218 type_t
*type_new_runtimeclass(char *name
, struct namespace *namespace)
220 type_t
*type
= get_type(TYPE_RUNTIMECLASS
, name
, NULL
, 0);
221 if (type
->type_type
!= TYPE_RUNTIMECLASS
|| type
->defined
)
222 error_loc("%s: redefinition error; original definition was at %s:%d\n",
223 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
225 type
->namespace = namespace;
229 type_t
*type_new_array(const char *name
, const decl_spec_t
*element
, int declptr
,
230 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
)
232 type_t
*t
= make_type(TYPE_ARRAY
);
233 if (name
) t
->name
= xstrdup(name
);
234 t
->details
.array
.declptr
= declptr
;
235 t
->details
.array
.length_is
= length_is
;
237 t
->details
.array
.size_is
= size_is
;
239 t
->details
.array
.dim
= dim
;
241 t
->details
.array
.elem
= *element
;
245 type_t
*type_new_basic(enum type_basic_type basic_type
)
247 type_t
*t
= make_type(TYPE_BASIC
);
248 t
->details
.basic
.type
= basic_type
;
249 t
->details
.basic
.sign
= 0;
253 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
255 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
257 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
259 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
260 if (!int_types
[basic_type
][sign
+ 1])
262 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
263 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
265 return int_types
[basic_type
][sign
+ 1];
268 type_t
*type_new_void(void)
270 static type_t
*void_type
= NULL
;
272 void_type
= make_type(TYPE_VOID
);
276 type_t
*type_new_enum(const char *name
, struct namespace *namespace, int defined
, var_list_t
*enums
)
281 t
= find_type(name
, namespace,tsENUM
);
285 t
= make_type(TYPE_ENUM
);
287 t
->namespace = namespace;
289 reg_type(t
, name
, namespace, tsENUM
);
292 if (!t
->defined
&& defined
)
294 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
295 t
->details
.enumeration
->enums
= enums
;
299 error_loc("redefinition of enum %s\n", name
);
304 type_t
*type_new_struct(char *name
, struct namespace *namespace, int defined
, var_list_t
*fields
)
309 t
= find_type(name
, namespace, tsSTRUCT
);
313 t
= make_type(TYPE_STRUCT
);
315 t
->namespace = namespace;
317 reg_type(t
, name
, namespace, tsSTRUCT
);
320 if (!t
->defined
&& defined
)
322 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
323 t
->details
.structure
->fields
= fields
;
327 error_loc("redefinition of struct %s\n", name
);
332 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
337 t
= find_type(name
, NULL
, tsUNION
);
341 t
= make_type(TYPE_UNION
);
344 reg_type(t
, name
, NULL
, tsUNION
);
347 if (!t
->defined
&& defined
)
349 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
350 t
->details
.structure
->fields
= fields
;
354 error_loc("redefinition of union %s\n", name
);
359 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
364 t
= find_type(name
, NULL
, tsUNION
);
368 t
= make_type(TYPE_ENCAPSULATED_UNION
);
371 reg_type(t
, name
, NULL
, tsUNION
);
373 t
->type_type
= TYPE_ENCAPSULATED_UNION
;
378 union_field
= make_var(xstrdup("tagged_union"));
379 union_field
->declspec
.type
= type_new_nonencapsulated_union(gen_name(), TRUE
, cases
);
381 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
382 t
->details
.structure
->fields
= append_var(NULL
, switch_field
);
383 t
->details
.structure
->fields
= append_var(t
->details
.structure
->fields
, union_field
);
387 error_loc("redefinition of union %s\n", name
);
392 static int is_valid_bitfield_type(const type_t
*type
)
394 switch (type_get_type(type
))
399 switch (type_basic_get_type(type
))
401 case TYPE_BASIC_INT8
:
402 case TYPE_BASIC_INT16
:
403 case TYPE_BASIC_INT32
:
404 case TYPE_BASIC_INT64
:
406 case TYPE_BASIC_INT3264
:
407 case TYPE_BASIC_LONG
:
408 case TYPE_BASIC_CHAR
:
409 case TYPE_BASIC_HYPER
:
410 case TYPE_BASIC_BYTE
:
411 case TYPE_BASIC_WCHAR
:
412 case TYPE_BASIC_ERROR_STATUS_T
:
414 case TYPE_BASIC_FLOAT
:
415 case TYPE_BASIC_DOUBLE
:
416 case TYPE_BASIC_HANDLE
:
425 type_t
*type_new_bitfield(type_t
*field
, const expr_t
*bits
)
429 if (!is_valid_bitfield_type(field
))
430 error_loc("bit-field has invalid type\n");
433 error_loc("negative width for bit-field\n");
435 /* FIXME: validate bits->cval <= memsize(field) * 8 */
437 t
= make_type(TYPE_BITFIELD
);
438 t
->details
.bitfield
.field
= field
;
439 t
->details
.bitfield
.bits
= bits
;
443 static unsigned int compute_method_indexes(type_t
*iface
)
448 if (!iface
->details
.iface
)
451 if (type_iface_get_inherit(iface
))
452 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
456 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
458 var_t
*func
= stmt
->u
.var
;
459 if (!is_callas(func
->attrs
))
460 func
->func_idx
= idx
++;
466 void type_interface_define(type_t
*iface
, type_t
*inherit
, statement_list_t
*stmts
)
468 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
469 iface
->details
.iface
->disp_props
= NULL
;
470 iface
->details
.iface
->disp_methods
= NULL
;
471 iface
->details
.iface
->stmts
= stmts
;
472 iface
->details
.iface
->inherit
= inherit
;
473 iface
->details
.iface
->disp_inherit
= NULL
;
474 iface
->details
.iface
->async_iface
= NULL
;
475 iface
->defined
= TRUE
;
476 compute_method_indexes(iface
);
479 void type_dispinterface_define(type_t
*iface
, var_list_t
*props
, var_list_t
*methods
)
481 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
482 iface
->details
.iface
->disp_props
= props
;
483 iface
->details
.iface
->disp_methods
= methods
;
484 iface
->details
.iface
->stmts
= NULL
;
485 iface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
486 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
487 iface
->details
.iface
->disp_inherit
= NULL
;
488 iface
->details
.iface
->async_iface
= NULL
;
489 iface
->defined
= TRUE
;
490 compute_method_indexes(iface
);
493 void type_dispinterface_define_from_iface(type_t
*dispiface
, type_t
*iface
)
495 dispiface
->details
.iface
= xmalloc(sizeof(*dispiface
->details
.iface
));
496 dispiface
->details
.iface
->disp_props
= NULL
;
497 dispiface
->details
.iface
->disp_methods
= NULL
;
498 dispiface
->details
.iface
->stmts
= NULL
;
499 dispiface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
500 if (!dispiface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
501 dispiface
->details
.iface
->disp_inherit
= iface
;
502 dispiface
->details
.iface
->async_iface
= NULL
;
503 dispiface
->defined
= TRUE
;
504 compute_method_indexes(dispiface
);
507 void type_module_define(type_t
*module
, statement_list_t
*stmts
)
509 if (module
->details
.module
) error_loc("multiple definition error\n");
510 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
511 module
->details
.module
->stmts
= stmts
;
512 module
->defined
= TRUE
;
515 type_t
*type_coclass_define(type_t
*coclass
, ifref_list_t
*ifaces
)
517 coclass
->details
.coclass
.ifaces
= ifaces
;
518 coclass
->defined
= TRUE
;
522 type_t
*type_runtimeclass_define(type_t
*runtimeclass
, ifref_list_t
*ifaces
)
524 runtimeclass
->details
.runtimeclass
.ifaces
= ifaces
;
525 runtimeclass
->defined
= TRUE
;
526 if (!type_runtimeclass_get_default_iface(runtimeclass
))
527 error_loc("missing default interface on runtimeclass %s\n", runtimeclass
->name
);
531 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
533 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
536 if (type1
->name
&& type2
->name
)
537 return !strcmp(type1
->name
, type2
->name
);
538 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
541 /* FIXME: do deep inspection of types to determine if they are equal */