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
)
92 static char *append_namespace(char *ptr
, struct namespace *namespace, const char *separator
)
94 if(is_global_namespace(namespace)) {
95 if(!use_abi_namespace
)
98 strcat(ptr
, separator
);
99 return ptr
+ strlen(ptr
);
102 ptr
= append_namespace(ptr
, namespace->parent
, separator
);
103 strcpy(ptr
, namespace->name
);
104 strcat(ptr
, separator
);
105 return ptr
+ strlen(ptr
);
108 char *format_namespace(struct namespace *namespace, const char *prefix
, const char *separator
, const char *suffix
)
110 unsigned len
= strlen(prefix
) + strlen(suffix
);
111 unsigned sep_len
= strlen(separator
);
112 struct namespace *iter
;
115 if(use_abi_namespace
&& !is_global_namespace(namespace))
116 len
+= 3 /* strlen("ABI") */ + 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
);
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
);
219 type_t
*type_new_array(const char *name
, const decl_spec_t
*element
, int declptr
,
220 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
)
222 type_t
*t
= make_type(TYPE_ARRAY
);
223 if (name
) t
->name
= xstrdup(name
);
224 t
->details
.array
.declptr
= declptr
;
225 t
->details
.array
.length_is
= length_is
;
227 t
->details
.array
.size_is
= size_is
;
229 t
->details
.array
.dim
= dim
;
231 t
->details
.array
.elem
= *element
;
235 type_t
*type_new_basic(enum type_basic_type basic_type
)
237 type_t
*t
= make_type(TYPE_BASIC
);
238 t
->details
.basic
.type
= basic_type
;
239 t
->details
.basic
.sign
= 0;
243 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
245 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
247 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
249 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
250 if (!int_types
[basic_type
][sign
+ 1])
252 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
253 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
255 return int_types
[basic_type
][sign
+ 1];
258 type_t
*type_new_void(void)
260 static type_t
*void_type
= NULL
;
262 void_type
= make_type(TYPE_VOID
);
266 type_t
*type_new_enum(const char *name
, struct namespace *namespace, int defined
, var_list_t
*enums
)
268 type_t
*tag_type
= name
? find_type(name
, namespace, tsENUM
) : NULL
;
269 type_t
*t
= make_type(TYPE_ENUM
);
271 t
->namespace = namespace;
273 if (tag_type
&& tag_type
->details
.enumeration
)
274 t
->details
.enumeration
= tag_type
->details
.enumeration
;
277 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
278 t
->details
.enumeration
->enums
= enums
;
285 reg_type(t
, name
, namespace, tsENUM
);
292 type_t
*type_new_struct(char *name
, struct namespace *namespace, int defined
, var_list_t
*fields
)
294 type_t
*tag_type
= name
? find_type(name
, namespace, tsSTRUCT
) : NULL
;
297 /* avoid creating duplicate typelib type entries */
298 if (tag_type
&& do_typelib
) return tag_type
;
300 t
= make_type(TYPE_STRUCT
);
302 t
->namespace = namespace;
304 if (tag_type
&& tag_type
->details
.structure
)
305 t
->details
.structure
= tag_type
->details
.structure
;
308 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
309 t
->details
.structure
->fields
= fields
;
315 reg_type(t
, name
, namespace, tsSTRUCT
);
322 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
324 type_t
*tag_type
= name
? find_type(name
, NULL
, tsUNION
) : NULL
;
325 type_t
*t
= make_type(TYPE_UNION
);
327 if (tag_type
&& tag_type
->details
.structure
)
328 t
->details
.structure
= tag_type
->details
.structure
;
331 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
332 t
->details
.structure
->fields
= fields
;
338 reg_type(t
, name
, NULL
, tsUNION
);
345 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
347 type_t
*t
= get_type(TYPE_ENCAPSULATED_UNION
, name
, NULL
, tsUNION
);
348 if (!union_field
) union_field
= make_var( xstrdup("tagged_union") );
349 union_field
->declspec
.type
= type_new_nonencapsulated_union(NULL
, TRUE
, cases
);
350 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
351 t
->details
.structure
->fields
= append_var( NULL
, switch_field
);
352 t
->details
.structure
->fields
= append_var( t
->details
.structure
->fields
, union_field
);
357 static int is_valid_bitfield_type(const type_t
*type
)
359 switch (type_get_type(type
))
364 switch (type_basic_get_type(type
))
366 case TYPE_BASIC_INT8
:
367 case TYPE_BASIC_INT16
:
368 case TYPE_BASIC_INT32
:
369 case TYPE_BASIC_INT64
:
371 case TYPE_BASIC_INT3264
:
372 case TYPE_BASIC_LONG
:
373 case TYPE_BASIC_CHAR
:
374 case TYPE_BASIC_HYPER
:
375 case TYPE_BASIC_BYTE
:
376 case TYPE_BASIC_WCHAR
:
377 case TYPE_BASIC_ERROR_STATUS_T
:
379 case TYPE_BASIC_FLOAT
:
380 case TYPE_BASIC_DOUBLE
:
381 case TYPE_BASIC_HANDLE
:
390 type_t
*type_new_bitfield(type_t
*field
, const expr_t
*bits
)
394 if (!is_valid_bitfield_type(field
))
395 error_loc("bit-field has invalid type\n");
398 error_loc("negative width for bit-field\n");
400 /* FIXME: validate bits->cval <= memsize(field) * 8 */
402 t
= make_type(TYPE_BITFIELD
);
403 t
->details
.bitfield
.field
= field
;
404 t
->details
.bitfield
.bits
= bits
;
408 static unsigned int compute_method_indexes(type_t
*iface
)
413 if (!iface
->details
.iface
)
416 if (type_iface_get_inherit(iface
))
417 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
421 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
423 var_t
*func
= stmt
->u
.var
;
424 if (!is_callas(func
->attrs
))
425 func
->func_idx
= idx
++;
431 void type_interface_define(type_t
*iface
, type_t
*inherit
, statement_list_t
*stmts
)
433 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
434 iface
->details
.iface
->disp_props
= NULL
;
435 iface
->details
.iface
->disp_methods
= NULL
;
436 iface
->details
.iface
->stmts
= stmts
;
437 iface
->details
.iface
->inherit
= inherit
;
438 iface
->details
.iface
->disp_inherit
= NULL
;
439 iface
->details
.iface
->async_iface
= NULL
;
440 iface
->defined
= TRUE
;
441 compute_method_indexes(iface
);
444 void type_dispinterface_define(type_t
*iface
, var_list_t
*props
, var_list_t
*methods
)
446 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
447 iface
->details
.iface
->disp_props
= props
;
448 iface
->details
.iface
->disp_methods
= methods
;
449 iface
->details
.iface
->stmts
= NULL
;
450 iface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
451 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
452 iface
->details
.iface
->disp_inherit
= NULL
;
453 iface
->details
.iface
->async_iface
= NULL
;
454 iface
->defined
= TRUE
;
455 compute_method_indexes(iface
);
458 void type_dispinterface_define_from_iface(type_t
*dispiface
, type_t
*iface
)
460 dispiface
->details
.iface
= xmalloc(sizeof(*dispiface
->details
.iface
));
461 dispiface
->details
.iface
->disp_props
= NULL
;
462 dispiface
->details
.iface
->disp_methods
= NULL
;
463 dispiface
->details
.iface
->stmts
= NULL
;
464 dispiface
->details
.iface
->inherit
= find_type("IDispatch", NULL
, 0);
465 if (!dispiface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
466 dispiface
->details
.iface
->disp_inherit
= iface
;
467 dispiface
->details
.iface
->async_iface
= NULL
;
468 dispiface
->defined
= TRUE
;
469 compute_method_indexes(dispiface
);
472 void type_module_define(type_t
*module
, statement_list_t
*stmts
)
474 if (module
->details
.module
) error_loc("multiple definition error\n");
475 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
476 module
->details
.module
->stmts
= stmts
;
477 module
->defined
= TRUE
;
480 type_t
*type_coclass_define(type_t
*coclass
, ifref_list_t
*ifaces
)
482 coclass
->details
.coclass
.ifaces
= ifaces
;
483 coclass
->defined
= TRUE
;
487 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
489 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
492 if (type1
->name
&& type2
->name
)
493 return !strcmp(type1
->name
, type2
->name
);
494 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
497 /* FIXME: do deep inspection of types to determine if they are equal */