2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "msvcrt/eh.h"
23 #include "msvcrt/malloc.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
30 typedef void (*v_table_ptr
)();
32 static v_table_ptr exception_vtable
[2];
33 static v_table_ptr bad_typeid_vtable
[3];
34 static v_table_ptr __non_rtti_object_vtable
[3];
35 static v_table_ptr bad_cast_vtable
[3];
36 static v_table_ptr type_info_vtable
[1];
38 typedef struct __exception
42 int do_free
; /* FIXME: take string copy with char* ctor? */
45 typedef struct __bad_typeid
50 typedef struct ____non_rtti_object
55 typedef struct __bad_cast
60 typedef struct __type_info
67 typedef struct _rtti_base_descriptor
69 type_info
*type_descriptor
;
71 int base_class_offset
;
73 } rtti_base_descriptor
;
75 typedef struct _rtti_base_array
77 rtti_base_descriptor
*bases
[1]; /* First element is the class itself */
80 typedef struct _rtti_object_hierachy
84 int array_len
; /* Size of the array pointed to by 'base_classes' */
85 rtti_base_array
*base_classes
;
86 } rtti_object_hierachy
;
88 typedef struct _rtti_object_locator
91 int base_class_offset
;
93 type_info
*type_descriptor
;
94 rtti_object_hierachy
*type_hierachy
;
95 } rtti_object_locator
;
97 /******************************************************************
98 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
100 void MSVCRT_exception_ctor(exception
* _this
, const char ** name
)
102 TRACE("(%p %s)\n",_this
,*name
);
103 _this
->vtable
= exception_vtable
;
105 TRACE("name = %s\n",_this
->name
);
106 _this
->do_free
= 0; /* FIXME */
109 /******************************************************************
110 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
112 void MSVCRT_exception_copy_ctor(exception
* _this
, const exception
* rhs
)
114 TRACE("(%p %p)\n",_this
,rhs
);
116 memcpy (_this
, rhs
, sizeof (*_this
));
117 TRACE("name = %s\n",_this
->name
);
120 /******************************************************************
121 * ??0exception@@QAE@XZ (MSVCRT.@)
123 void MSVCRT_exception_default_ctor(exception
* _this
)
125 TRACE("(%p)\n",_this
);
126 _this
->vtable
= exception_vtable
;
128 _this
->do_free
= 0; /* FIXME */
131 /******************************************************************
132 * ??1exception@@UAE@XZ (MSVCRT.@)
134 void MSVCRT_exception_dtor(exception
* _this
)
136 TRACE("(%p)\n",_this
);
139 /******************************************************************
140 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
142 exception
* MSVCRT_exception_opequals(exception
* _this
, const exception
* rhs
)
144 TRACE("(%p %p)\n",_this
,rhs
);
145 memcpy (_this
, rhs
, sizeof (*_this
));
146 TRACE("name = %s\n",_this
->name
);
150 /******************************************************************
151 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
153 void * MSVCRT_exception__unknown_E(exception
* _this
, unsigned int arg1
)
155 TRACE("(%p %d)\n",_this
,arg1
);
160 /******************************************************************
161 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
163 void * MSVCRT_exception__unknown_G(exception
* _this
, unsigned int arg1
)
165 TRACE("(%p %d)\n",_this
,arg1
);
170 /******************************************************************
171 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
173 const char * MSVCRT_what_exception(exception
* _this
)
175 TRACE("(%p) returning %s\n",_this
,_this
->name
);
180 static terminate_function func_terminate
=NULL
;
181 static unexpected_function func_unexpected
=NULL
;
183 /******************************************************************
184 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
186 terminate_function
MSVCRT_set_terminate(terminate_function func
)
188 terminate_function previous
=func_terminate
;
189 TRACE("(%p) returning %p\n",func
,previous
);
194 /******************************************************************
195 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
197 unexpected_function
MSVCRT_set_unexpected(unexpected_function func
)
199 unexpected_function previous
=func_unexpected
;
200 TRACE("(%p) returning %p\n",func
,previous
);
201 func_unexpected
=func
;
205 /******************************************************************
206 * ?terminate@@YAXXZ (MSVCRT.@)
208 void MSVCRT_terminate()
213 /******************************************************************
214 * ?unexpected@@YAXXZ (MSVCRT.@)
216 void MSVCRT_unexpected()
218 (*func_unexpected
)();
222 /******************************************************************
223 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
225 void MSVCRT_bad_typeid_copy_ctor(bad_typeid
* _this
, const bad_typeid
* rhs
)
227 TRACE("(%p %p)\n",_this
,rhs
);
228 MSVCRT_exception_copy_ctor(&_this
->base
,&rhs
->base
);
231 /******************************************************************
232 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
234 void MSVCRT_bad_typeid_ctor(bad_typeid
* _this
, const char * name
)
236 TRACE("(%p %s)\n",_this
,name
);
237 MSVCRT_exception_ctor(&_this
->base
, &name
);
238 _this
->base
.vtable
= bad_typeid_vtable
;
241 /******************************************************************
242 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
244 void MSVCRT_bad_typeid_dtor(bad_typeid
* _this
)
246 TRACE("(%p)\n",_this
);
247 MSVCRT_exception_dtor(&_this
->base
);
250 /******************************************************************
251 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
253 bad_typeid
* MSVCRT_bad_typeid_opequals(bad_typeid
* _this
, const bad_typeid
* rhs
)
255 TRACE("(%p %p)\n",_this
,rhs
);
256 MSVCRT_exception_copy_ctor(&_this
->base
,&rhs
->base
);
260 /******************************************************************
261 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
263 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object
* _this
,
264 const __non_rtti_object
* rhs
)
266 TRACE("(%p %p)\n",_this
,rhs
);
267 MSVCRT_bad_typeid_copy_ctor(&_this
->base
,&rhs
->base
);
270 /******************************************************************
271 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
273 void MSVCRT___non_rtti_object_ctor(__non_rtti_object
* _this
,
276 TRACE("(%p %s)\n",_this
,name
);
277 MSVCRT_bad_typeid_ctor(&_this
->base
,name
);
278 _this
->base
.base
.vtable
= __non_rtti_object_vtable
;
281 /******************************************************************
282 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
284 void MSVCRT___non_rtti_object_dtor(__non_rtti_object
* _this
)
286 TRACE("(%p)\n",_this
);
287 MSVCRT_bad_typeid_dtor(&_this
->base
);
290 /******************************************************************
291 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
293 __non_rtti_object
* MSVCRT___non_rtti_object_opequals(__non_rtti_object
* _this
,
294 const __non_rtti_object
*rhs
)
296 TRACE("(%p %p)\n",_this
,rhs
);
297 memcpy (_this
, rhs
, sizeof (*_this
));
298 TRACE("name = %s\n",_this
->base
.base
.name
);
302 /******************************************************************
303 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
305 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object
* _this
, unsigned int arg1
)
307 TRACE("(%p %d)\n",_this
,arg1
);
312 /******************************************************************
313 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
315 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object
* _this
, unsigned int arg1
)
317 TRACE("(%p %d)\n",_this
,arg1
);
322 /******************************************************************
323 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
325 void MSVCRT_bad_cast_ctor(bad_cast
* _this
, const char ** name
)
327 TRACE("(%p %s)\n",_this
,*name
);
328 MSVCRT_exception_ctor(&_this
->base
, name
);
329 _this
->base
.vtable
= bad_cast_vtable
;
332 /******************************************************************
333 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
335 void MSVCRT_bad_cast_copy_ctor(bad_cast
* _this
, const bad_cast
* rhs
)
337 TRACE("(%p %p)\n",_this
,rhs
);
338 MSVCRT_exception_copy_ctor(&_this
->base
,&rhs
->base
);
341 /******************************************************************
342 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
344 void MSVCRT_bad_cast_dtor(bad_cast
* _this
)
346 TRACE("(%p)\n",_this
);
347 MSVCRT_exception_dtor(&_this
->base
);
350 /******************************************************************
351 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
353 bad_cast
* MSVCRT_bad_cast_opequals(bad_cast
* _this
, const bad_cast
* rhs
)
355 TRACE("(%p %p)\n",_this
,rhs
);
356 MSVCRT_exception_copy_ctor(&_this
->base
,&rhs
->base
);
360 /******************************************************************
361 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
363 int __stdcall
MSVCRT_type_info_opequals_equals(type_info
* _this
, const type_info
* rhs
)
365 TRACE("(%p %p) returning %d\n",_this
,rhs
,_this
->name
== rhs
->name
);
366 return _this
->name
== rhs
->name
;
369 /******************************************************************
370 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
372 int __stdcall
MSVCRT_type_info_opnot_equals(type_info
* _this
, const type_info
* rhs
)
374 TRACE("(%p %p) returning %d\n",_this
,rhs
,_this
->name
== rhs
->name
);
375 return _this
->name
!= rhs
->name
;
378 /******************************************************************
379 * ??1type_info@@UAE@XZ (MSVCRT.@)
381 void MSVCRT_type_info_dtor(type_info
* _this
)
383 TRACE("(%p)\n",_this
);
385 MSVCRT_free(_this
->data
);
388 /******************************************************************
389 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
391 const char * __stdcall
MSVCRT_type_info_name(type_info
* _this
)
393 TRACE("(%p) returning %s\n",_this
,_this
->name
);
397 /******************************************************************
398 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
400 const char * __stdcall
MSVCRT_type_info_raw_name(type_info
* _this
)
402 TRACE("(%p) returning %s\n",_this
,_this
->name
);
407 /******************************************************************
408 * __RTtypeid (MSVCRT.@)
410 type_info
* MSVCRT___RTtypeid(type_info
*cppobj
)
412 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
413 TRACE("(%p)\n",cppobj
);
415 if (!IsBadReadPtr(cppobj
, sizeof(void *)) &&
416 !IsBadReadPtr(cppobj
->vtable
- 1,sizeof(void *)) &&
417 !IsBadReadPtr((void*)cppobj
->vtable
[-1], sizeof(rtti_object_locator
)))
419 rtti_object_locator
*obj_locator
= (rtti_object_locator
*)cppobj
->vtable
[-1];
420 return obj_locator
->type_descriptor
;
422 /* FIXME: throw a C++ exception */
423 FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
427 /******************************************************************
428 * __RTDynamicCast (MSVCRT.@)
430 void* MSVCRT___RTDynamicCast(type_info
*cppobj
, int unknown
,
431 type_info
*src
, type_info
*dst
,
434 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
435 TRACE("(%p,%d,%p,%p,%d)\n",cppobj
, unknown
, src
, dst
, do_throw
);
438 FIXME("Unknown prameter is non-zero: please report\n");
440 /* To cast an object at runtime:
441 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
442 * 2.Search for the destination type in the class heirachy
443 * 3.If destination type is found, return base object address + dest offset
444 * Otherwise, fail the cast
446 if (!IsBadReadPtr(cppobj
, sizeof(void *)) &&
447 !IsBadReadPtr(cppobj
->vtable
- 1,sizeof(void *)) &&
448 !IsBadReadPtr((void*)cppobj
->vtable
[-1], sizeof(rtti_object_locator
)))
451 rtti_object_locator
*obj_locator
= (rtti_object_locator
*)cppobj
->vtable
[-1];
452 rtti_object_hierachy
*obj_bases
= obj_locator
->type_hierachy
;
453 rtti_base_descriptor
**base_desc
= obj_bases
->base_classes
->bases
;
454 int src_offset
= obj_locator
->base_class_offset
, dst_offset
= -1;
456 while (count
< obj_bases
->array_len
)
458 type_info
*typ
= (*base_desc
)->type_descriptor
;
460 if (!strcmp(typ
->name
, dst
->name
))
462 dst_offset
= (*base_desc
)->base_class_offset
;
469 return (void*)((unsigned long)cppobj
- src_offset
+ dst_offset
);
472 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
473 * to a reference, since references cannot be NULL.
476 FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
481 /******************************************************************
482 * __RTCastToVoid (MSVCRT.@)
484 void* MSVCRT___RTCastToVoid(type_info
*cppobj
)
486 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
487 TRACE("(%p)\n",cppobj
);
489 /* Casts to void* simply cast to the base object */
490 if (!IsBadReadPtr(cppobj
, sizeof(void *)) &&
491 !IsBadReadPtr(cppobj
->vtable
- 1,sizeof(void *)) &&
492 !IsBadReadPtr((void*)cppobj
->vtable
[-1], sizeof(rtti_object_locator
)))
494 rtti_object_locator
*obj_locator
= (rtti_object_locator
*)cppobj
->vtable
[-1];
495 int src_offset
= obj_locator
->base_class_offset
;
497 return (void*)((unsigned long)cppobj
- src_offset
);
503 /* INTERNAL: Set up vtables
504 * FIXME:should be static, cope with versions?
506 void msvcrt_init_vtables(void)
508 exception_vtable
[0] = MSVCRT_exception_dtor
;
509 exception_vtable
[1] = (void*)MSVCRT_what_exception
;
511 bad_typeid_vtable
[0] = MSVCRT_bad_typeid_dtor
;
512 bad_typeid_vtable
[1] = exception_vtable
[1];
513 bad_typeid_vtable
[2] = _purecall
; /* FIXME */
515 __non_rtti_object_vtable
[0] = MSVCRT___non_rtti_object_dtor
;
516 __non_rtti_object_vtable
[1] = bad_typeid_vtable
[1];
517 __non_rtti_object_vtable
[2] = bad_typeid_vtable
[2];
519 bad_cast_vtable
[0] = MSVCRT_bad_cast_dtor
;
520 bad_cast_vtable
[1] = exception_vtable
[1];
521 bad_cast_vtable
[2] = _purecall
; /* FIXME */
523 type_info_vtable
[0] = MSVCRT_type_info_dtor
;