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();
51 memset(&t
->details
, 0, sizeof(t
->details
));
52 t
->typestring_offset
= 0;
54 t
->ignore
= (parse_only
!= 0);
57 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 type_t
*type_new_function(var_list_t
*args
)
87 arg
= LIST_ENTRY(list_head(args
), var_t
, entry
);
88 if (list_count(args
) == 1 && !arg
->name
&& arg
->type
&& type_get_type(arg
->type
) == TYPE_VOID
)
90 list_remove(&arg
->entry
);
96 if (args
) LIST_FOR_EACH_ENTRY(arg
, args
, var_t
, entry
)
98 if (arg
->type
&& type_get_type(arg
->type
) == TYPE_VOID
)
99 error_loc("argument '%s' has void type\n", arg
->name
);
103 error_loc("too many unnamed arguments\n");
110 name
[0] = i
> 26 ? 'a' + i
/ 26 : 'a' + i
;
111 name
[1] = i
> 26 ? 'a' + i
% 26 : 0;
113 unique
= !find_arg(args
, name
);
115 arg
->name
= xstrdup(name
);
122 t
= make_type(TYPE_FUNCTION
);
123 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
124 t
->details
.function
->args
= args
;
125 t
->details
.function
->idx
= -1;
129 type_t
*type_new_pointer(unsigned char pointer_default
, type_t
*ref
, attr_list_t
*attrs
)
131 type_t
*t
= make_type(TYPE_POINTER
);
132 t
->details
.pointer
.def_fc
= pointer_default
;
133 t
->details
.pointer
.ref
= ref
;
138 type_t
*type_new_alias(type_t
*t
, const char *name
)
140 type_t
*a
= duptype(t
, 0);
142 a
->name
= xstrdup(name
);
146 /* for pointer types */
147 a
->details
= t
->details
;
148 init_loc_info(&a
->loc_info
);
153 type_t
*type_new_module(char *name
)
155 type_t
*type
= get_type(TYPE_MODULE
, name
, 0);
156 if (type
->type_type
!= TYPE_MODULE
|| type
->defined
)
157 error_loc("%s: redefinition error; original definition was at %s:%d\n",
158 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
163 type_t
*type_new_coclass(char *name
)
165 type_t
*type
= get_type(TYPE_COCLASS
, name
, 0);
166 if (type
->type_type
!= TYPE_COCLASS
|| type
->defined
)
167 error_loc("%s: redefinition error; original definition was at %s:%d\n",
168 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
174 type_t
*type_new_array(const char *name
, type_t
*element
, int declptr
,
175 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
,
176 unsigned char ptr_default_fc
)
178 type_t
*t
= make_type(TYPE_ARRAY
);
179 if (name
) t
->name
= xstrdup(name
);
180 t
->details
.array
.declptr
= declptr
;
181 t
->details
.array
.length_is
= length_is
;
183 t
->details
.array
.size_is
= size_is
;
185 t
->details
.array
.dim
= dim
;
186 t
->details
.array
.elem
= element
;
187 t
->details
.array
.ptr_def_fc
= ptr_default_fc
;
191 type_t
*type_new_basic(enum type_basic_type basic_type
)
193 type_t
*t
= make_type(TYPE_BASIC
);
194 t
->details
.basic
.type
= basic_type
;
195 t
->details
.basic
.sign
= 0;
199 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
201 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
203 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
205 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
206 if (!int_types
[basic_type
][sign
+ 1])
208 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
209 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
211 return int_types
[basic_type
][sign
+ 1];
214 type_t
*type_new_void(void)
216 static type_t
*void_type
= NULL
;
218 void_type
= make_type(TYPE_VOID
);
222 type_t
*type_new_enum(const char *name
, int defined
, var_list_t
*enums
)
224 type_t
*tag_type
= name
? find_type(name
, tsENUM
) : NULL
;
225 type_t
*t
= make_type(TYPE_ENUM
);
228 if (tag_type
&& tag_type
->details
.enumeration
)
229 t
->details
.enumeration
= tag_type
->details
.enumeration
;
232 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
233 t
->details
.enumeration
->enums
= enums
;
240 reg_type(t
, name
, tsENUM
);
247 type_t
*type_new_struct(char *name
, int defined
, var_list_t
*fields
)
249 type_t
*tag_type
= name
? find_type(name
, tsSTRUCT
) : NULL
;
250 type_t
*t
= make_type(TYPE_STRUCT
);
252 if (tag_type
&& tag_type
->details
.structure
)
253 t
->details
.structure
= tag_type
->details
.structure
;
256 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
257 t
->details
.structure
->fields
= fields
;
263 reg_type(t
, name
, tsSTRUCT
);
270 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
272 type_t
*tag_type
= name
? find_type(name
, tsUNION
) : NULL
;
273 type_t
*t
= make_type(TYPE_UNION
);
275 if (tag_type
&& tag_type
->details
.structure
)
276 t
->details
.structure
= tag_type
->details
.structure
;
279 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
280 t
->details
.structure
->fields
= fields
;
286 reg_type(t
, name
, tsUNION
);
293 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
295 type_t
*t
= get_type(TYPE_ENCAPSULATED_UNION
, name
, tsUNION
);
296 if (!union_field
) union_field
= make_var( xstrdup("tagged_union") );
297 union_field
->type
= type_new_nonencapsulated_union(NULL
, TRUE
, cases
);
298 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
299 t
->details
.structure
->fields
= append_var( NULL
, switch_field
);
300 t
->details
.structure
->fields
= append_var( t
->details
.structure
->fields
, union_field
);
305 static int is_valid_bitfield_type(const type_t
*type
)
307 switch (type_get_type(type
))
312 switch (type_basic_get_type(type
))
314 case TYPE_BASIC_INT8
:
315 case TYPE_BASIC_INT16
:
316 case TYPE_BASIC_INT32
:
317 case TYPE_BASIC_INT64
:
319 case TYPE_BASIC_INT3264
:
320 case TYPE_BASIC_CHAR
:
321 case TYPE_BASIC_HYPER
:
322 case TYPE_BASIC_BYTE
:
323 case TYPE_BASIC_WCHAR
:
324 case TYPE_BASIC_ERROR_STATUS_T
:
326 case TYPE_BASIC_FLOAT
:
327 case TYPE_BASIC_DOUBLE
:
328 case TYPE_BASIC_HANDLE
:
337 type_t
*type_new_bitfield(type_t
*field
, const expr_t
*bits
)
341 if (!is_valid_bitfield_type(field
))
342 error_loc("bit-field has invalid type\n");
345 error_loc("negative width for bit-field\n");
347 /* FIXME: validate bits->cval <= memsize(field) * 8 */
349 t
= make_type(TYPE_BITFIELD
);
350 t
->details
.bitfield
.field
= field
;
351 t
->details
.bitfield
.bits
= bits
;
355 static int compute_method_indexes(type_t
*iface
)
360 if (!iface
->details
.iface
)
363 if (type_iface_get_inherit(iface
))
364 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
368 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
370 var_t
*func
= stmt
->u
.var
;
371 if (!is_callas(func
->attrs
))
372 func
->type
->details
.function
->idx
= idx
++;
378 void type_interface_define(type_t
*iface
, type_t
*inherit
, statement_list_t
*stmts
)
380 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
381 iface
->details
.iface
->disp_props
= NULL
;
382 iface
->details
.iface
->disp_methods
= NULL
;
383 iface
->details
.iface
->stmts
= stmts
;
384 iface
->details
.iface
->inherit
= inherit
;
385 iface
->defined
= TRUE
;
386 compute_method_indexes(iface
);
389 void type_dispinterface_define(type_t
*iface
, var_list_t
*props
, var_list_t
*methods
)
391 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
392 iface
->details
.iface
->disp_props
= props
;
393 iface
->details
.iface
->disp_methods
= methods
;
394 iface
->details
.iface
->stmts
= NULL
;
395 iface
->details
.iface
->inherit
= find_type("IDispatch", 0);
396 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
397 iface
->defined
= TRUE
;
398 compute_method_indexes(iface
);
401 void type_dispinterface_define_from_iface(type_t
*dispiface
, type_t
*iface
)
403 type_dispinterface_define(dispiface
, iface
->details
.iface
->disp_props
,
404 iface
->details
.iface
->disp_methods
);
407 void type_module_define(type_t
*module
, statement_list_t
*stmts
)
409 if (module
->details
.module
) error_loc("multiple definition error\n");
410 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
411 module
->details
.module
->stmts
= stmts
;
412 module
->defined
= TRUE
;
415 type_t
*type_coclass_define(type_t
*coclass
, ifref_list_t
*ifaces
)
417 coclass
->details
.coclass
.ifaces
= ifaces
;
418 coclass
->defined
= TRUE
;
422 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
424 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
427 if (type1
->name
&& type2
->name
)
428 return !strcmp(type1
->name
, type2
->name
);
429 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
432 /* FIXME: do deep inspection of types to determine if they are equal */