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 type_t
*type_new_function(var_list_t
*args
)
68 type_t
*t
= make_type(TYPE_FUNCTION
);
69 t
->details
.function
= xmalloc(sizeof(*t
->details
.function
));
70 t
->details
.function
->args
= args
;
71 t
->details
.function
->idx
= -1;
75 type_t
*type_new_pointer(unsigned char pointer_default
, type_t
*ref
, attr_list_t
*attrs
)
77 type_t
*t
= make_type(TYPE_POINTER
);
78 t
->details
.pointer
.def_fc
= pointer_default
;
79 t
->details
.pointer
.ref
= ref
;
84 type_t
*type_new_alias(type_t
*t
, const char *name
)
86 type_t
*a
= duptype(t
, 0);
88 a
->name
= xstrdup(name
);
92 /* for pointer types */
93 a
->details
= t
->details
;
94 init_loc_info(&a
->loc_info
);
99 type_t
*type_new_module(char *name
)
101 type_t
*type
= get_type(TYPE_MODULE
, name
, 0);
102 if (type
->type_type
!= TYPE_MODULE
|| type
->defined
)
103 error_loc("%s: redefinition error; original definition was at %s:%d\n",
104 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
109 type_t
*type_new_coclass(char *name
)
111 type_t
*type
= get_type(TYPE_COCLASS
, name
, 0);
112 if (type
->type_type
!= TYPE_COCLASS
|| type
->defined
)
113 error_loc("%s: redefinition error; original definition was at %s:%d\n",
114 type
->name
, type
->loc_info
.input_name
, type
->loc_info
.line_number
);
120 type_t
*type_new_array(const char *name
, type_t
*element
, int declptr
,
121 unsigned int dim
, expr_t
*size_is
, expr_t
*length_is
,
122 unsigned char ptr_default_fc
)
124 type_t
*t
= make_type(TYPE_ARRAY
);
125 if (name
) t
->name
= xstrdup(name
);
126 t
->details
.array
.declptr
= declptr
;
127 t
->details
.array
.length_is
= length_is
;
129 t
->details
.array
.size_is
= size_is
;
131 t
->details
.array
.dim
= dim
;
132 t
->details
.array
.elem
= element
;
133 t
->details
.array
.ptr_def_fc
= ptr_default_fc
;
137 type_t
*type_new_basic(enum type_basic_type basic_type
)
139 type_t
*t
= make_type(TYPE_BASIC
);
140 t
->details
.basic
.type
= basic_type
;
141 t
->details
.basic
.sign
= 0;
145 type_t
*type_new_int(enum type_basic_type basic_type
, int sign
)
147 static type_t
*int_types
[TYPE_BASIC_INT_MAX
+1][3];
149 assert(basic_type
<= TYPE_BASIC_INT_MAX
);
151 /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
152 if (!int_types
[basic_type
][sign
+ 1])
154 int_types
[basic_type
][sign
+ 1] = type_new_basic(basic_type
);
155 int_types
[basic_type
][sign
+ 1]->details
.basic
.sign
= sign
;
157 return int_types
[basic_type
][sign
+ 1];
160 type_t
*type_new_void(void)
162 static type_t
*void_type
= NULL
;
164 void_type
= make_type(TYPE_VOID
);
168 type_t
*type_new_enum(const char *name
, int defined
, var_list_t
*enums
)
170 type_t
*tag_type
= name
? find_type(name
, tsENUM
) : NULL
;
171 type_t
*t
= make_type(TYPE_ENUM
);
174 if (tag_type
&& tag_type
->details
.enumeration
)
175 t
->details
.enumeration
= tag_type
->details
.enumeration
;
178 t
->details
.enumeration
= xmalloc(sizeof(*t
->details
.enumeration
));
179 t
->details
.enumeration
->enums
= enums
;
186 reg_type(t
, name
, tsENUM
);
193 type_t
*type_new_struct(char *name
, int defined
, var_list_t
*fields
)
195 type_t
*tag_type
= name
? find_type(name
, tsSTRUCT
) : NULL
;
196 type_t
*t
= make_type(TYPE_STRUCT
);
198 if (tag_type
&& tag_type
->details
.structure
)
199 t
->details
.structure
= tag_type
->details
.structure
;
202 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
203 t
->details
.structure
->fields
= fields
;
209 reg_type(t
, name
, tsSTRUCT
);
216 type_t
*type_new_nonencapsulated_union(const char *name
, int defined
, var_list_t
*fields
)
218 type_t
*tag_type
= name
? find_type(name
, tsUNION
) : NULL
;
219 type_t
*t
= make_type(TYPE_UNION
);
221 if (tag_type
&& tag_type
->details
.structure
)
222 t
->details
.structure
= tag_type
->details
.structure
;
225 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
226 t
->details
.structure
->fields
= fields
;
232 reg_type(t
, name
, tsUNION
);
239 type_t
*type_new_encapsulated_union(char *name
, var_t
*switch_field
, var_t
*union_field
, var_list_t
*cases
)
241 type_t
*t
= get_type(TYPE_ENCAPSULATED_UNION
, name
, tsUNION
);
242 if (!union_field
) union_field
= make_var( xstrdup("tagged_union") );
243 union_field
->type
= type_new_nonencapsulated_union(NULL
, TRUE
, cases
);
244 t
->details
.structure
= xmalloc(sizeof(*t
->details
.structure
));
245 t
->details
.structure
->fields
= append_var( NULL
, switch_field
);
246 t
->details
.structure
->fields
= append_var( t
->details
.structure
->fields
, union_field
);
251 static int compute_method_indexes(type_t
*iface
)
256 if (!iface
->details
.iface
)
259 if (type_iface_get_inherit(iface
))
260 idx
= compute_method_indexes(type_iface_get_inherit(iface
));
264 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
266 var_t
*func
= stmt
->u
.var
;
267 if (!is_callas(func
->attrs
))
268 func
->type
->details
.function
->idx
= idx
++;
274 void type_interface_define(type_t
*iface
, type_t
*inherit
, statement_list_t
*stmts
)
276 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
277 iface
->details
.iface
->disp_props
= NULL
;
278 iface
->details
.iface
->disp_methods
= NULL
;
279 iface
->details
.iface
->stmts
= stmts
;
280 iface
->details
.iface
->inherit
= inherit
;
281 iface
->defined
= TRUE
;
282 compute_method_indexes(iface
);
285 void type_dispinterface_define(type_t
*iface
, var_list_t
*props
, func_list_t
*methods
)
287 iface
->details
.iface
= xmalloc(sizeof(*iface
->details
.iface
));
288 iface
->details
.iface
->disp_props
= props
;
289 iface
->details
.iface
->disp_methods
= methods
;
290 iface
->details
.iface
->stmts
= NULL
;
291 iface
->details
.iface
->inherit
= find_type("IDispatch", 0);
292 if (!iface
->details
.iface
->inherit
) error_loc("IDispatch is undefined\n");
293 iface
->defined
= TRUE
;
294 compute_method_indexes(iface
);
297 void type_dispinterface_define_from_iface(type_t
*dispiface
, type_t
*iface
)
299 type_dispinterface_define(dispiface
, iface
->details
.iface
->disp_props
,
300 iface
->details
.iface
->disp_methods
);
303 void type_module_define(type_t
*module
, statement_list_t
*stmts
)
305 if (module
->details
.module
) error_loc("multiple definition error\n");
306 module
->details
.module
= xmalloc(sizeof(*module
->details
.module
));
307 module
->details
.module
->stmts
= stmts
;
308 module
->defined
= TRUE
;
311 type_t
*type_coclass_define(type_t
*coclass
, ifref_list_t
*ifaces
)
313 coclass
->details
.coclass
.ifaces
= ifaces
;
314 coclass
->defined
= TRUE
;
318 int type_is_equal(const type_t
*type1
, const type_t
*type2
)
320 if (type_get_type_detect_alias(type1
) != type_get_type_detect_alias(type2
))
323 if (type1
->name
&& type2
->name
)
324 return !strcmp(type1
->name
, type2
->name
);
325 else if ((!type1
->name
&& type2
->name
) || (type1
->name
&& !type2
->name
))
328 /* FIXME: do deep inspection of types to determine if they are equal */