2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
9 #include "msvcrt/malloc.h"
11 DEFAULT_DEBUG_CHANNEL(msvcrt);
14 typedef void (*v_table_ptr)();
16 static v_table_ptr exception_vtable[2];
17 static v_table_ptr bad_typeid_vtable[3];
18 static v_table_ptr __non_rtti_object_vtable[3];
19 static v_table_ptr bad_cast_vtable[3];
20 static v_table_ptr type_info_vtable[1];
22 typedef struct __exception
26 int do_free; /* FIXME: take string copy with char* ctor? */
29 typedef struct __bad_typeid
34 typedef struct ____non_rtti_object
39 typedef struct __bad_cast
44 typedef struct __type_info
51 typedef struct _rtti_base_descriptor
53 type_info *type_descriptor;
55 int base_class_offset;
57 } rtti_base_descriptor;
59 typedef struct _rtti_base_array
61 rtti_base_descriptor *bases[1]; /* First element is the class itself */
64 typedef struct _rtti_object_hierachy
68 int array_len; /* Size of the array pointed to by 'base_classes' */
69 rtti_base_array *base_classes;
70 } rtti_object_hierachy;
72 typedef struct _rtti_object_locator
75 int base_class_offset;
77 type_info *type_descriptor;
78 rtti_object_hierachy *type_hierachy;
79 } rtti_object_locator;
81 /******************************************************************
82 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
84 void MSVCRT_exception_ctor(exception * _this, const char ** name)
86 TRACE("(%p %s)\n",_this,*name);
87 _this->vtable = exception_vtable;
89 TRACE("name = %s\n",_this->name);
90 _this->do_free = 0; /* FIXME */
93 /******************************************************************
94 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
96 void MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
98 TRACE("(%p %p)\n",_this,rhs);
100 memcpy (_this, rhs, sizeof (*_this));
101 TRACE("name = %s\n",_this->name);
104 /******************************************************************
105 * ??0exception@@QAE@XZ (MSVCRT.@)
107 void MSVCRT_exception_default_ctor(exception * _this)
109 TRACE("(%p)\n",_this);
110 _this->vtable = exception_vtable;
112 _this->do_free = 0; /* FIXME */
115 /******************************************************************
116 * ??1exception@@UAE@XZ (MSVCRT.@)
118 void MSVCRT_exception_dtor(exception * _this)
120 TRACE("(%p)\n",_this);
123 /******************************************************************
124 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
126 exception * MSVCRT_exception_opequals(exception * _this, const exception * rhs)
128 TRACE("(%p %p)\n",_this,rhs);
129 memcpy (_this, rhs, sizeof (*_this));
130 TRACE("name = %s\n",_this->name);
134 /******************************************************************
135 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
137 void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
139 TRACE("(%p %d)\n",_this,arg1);
144 /******************************************************************
145 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
147 void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
149 TRACE("(%p %d)\n",_this,arg1);
154 /******************************************************************
155 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
157 const char * MSVCRT_what_exception(exception * _this)
159 TRACE("(%p) returning %s\n",_this,_this->name);
164 static terminate_function func_terminate=NULL;
165 static unexpected_function func_unexpected=NULL;
167 /******************************************************************
168 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
170 terminate_function MSVCRT_set_terminate(terminate_function func)
172 terminate_function previous=func_terminate;
173 TRACE("(%p) returning %p\n",func,previous);
178 /******************************************************************
179 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
181 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
183 unexpected_function previous=func_unexpected;
184 TRACE("(%p) returning %p\n",func,previous);
185 func_unexpected=func;
189 /******************************************************************
190 * ?terminate@@YAXXZ (MSVCRT.@)
192 void MSVCRT_terminate()
197 /******************************************************************
198 * ?unexpected@@YAXXZ (MSVCRT.@)
200 void MSVCRT_unexpected()
202 (*func_unexpected)();
206 /******************************************************************
207 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
209 void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
211 TRACE("(%p %p)\n",_this,rhs);
212 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
215 /******************************************************************
216 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
218 void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
220 TRACE("(%p %s)\n",_this,name);
221 MSVCRT_exception_ctor(&_this->base, &name);
222 _this->base.vtable = bad_typeid_vtable;
225 /******************************************************************
226 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
228 void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
230 TRACE("(%p)\n",_this);
231 MSVCRT_exception_dtor(&_this->base);
234 /******************************************************************
235 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
237 bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
239 TRACE("(%p %p)\n",_this,rhs);
240 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
244 /******************************************************************
245 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
247 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
248 const __non_rtti_object * rhs)
250 TRACE("(%p %p)\n",_this,rhs);
251 MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
254 /******************************************************************
255 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
257 void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
260 TRACE("(%p %s)\n",_this,name);
261 MSVCRT_bad_typeid_ctor(&_this->base,name);
262 _this->base.base.vtable = __non_rtti_object_vtable;
265 /******************************************************************
266 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
268 void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
270 TRACE("(%p)\n",_this);
271 MSVCRT_bad_typeid_dtor(&_this->base);
274 /******************************************************************
275 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
277 __non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
278 const __non_rtti_object *rhs)
280 TRACE("(%p %p)\n",_this,rhs);
281 memcpy (_this, rhs, sizeof (*_this));
282 TRACE("name = %s\n",_this->base.base.name);
286 /******************************************************************
287 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
289 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
291 TRACE("(%p %d)\n",_this,arg1);
296 /******************************************************************
297 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
299 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
301 TRACE("(%p %d)\n",_this,arg1);
306 /******************************************************************
307 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
309 void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
311 TRACE("(%p %s)\n",_this,*name);
312 MSVCRT_exception_ctor(&_this->base, name);
313 _this->base.vtable = bad_cast_vtable;
316 /******************************************************************
317 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
319 void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
321 TRACE("(%p %p)\n",_this,rhs);
322 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
325 /******************************************************************
326 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
328 void MSVCRT_bad_cast_dtor(bad_cast * _this)
330 TRACE("(%p)\n",_this);
331 MSVCRT_exception_dtor(&_this->base);
334 /******************************************************************
335 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
337 bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
339 TRACE("(%p %p)\n",_this,rhs);
340 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
344 /******************************************************************
345 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
347 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
349 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
350 return _this->name == rhs->name;
353 /******************************************************************
354 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
356 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
358 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
359 return _this->name != rhs->name;
362 /******************************************************************
363 * ??1type_info@@UAE@XZ (MSVCRT.@)
365 void MSVCRT_type_info_dtor(type_info * _this)
367 TRACE("(%p)\n",_this);
369 MSVCRT_free(_this->data);
372 /******************************************************************
373 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
375 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
377 TRACE("(%p) returning %s\n",_this,_this->name);
381 /******************************************************************
382 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
384 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
386 TRACE("(%p) returning %s\n",_this,_this->name);
391 /******************************************************************
392 * __RTtypeid (MSVCRT.@)
394 type_info* MSVCRT___RTtypeid(type_info *cppobj)
396 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
397 TRACE("(%p)\n",cppobj);
399 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
400 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
401 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
403 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
404 return obj_locator->type_descriptor;
406 /* FIXME: throw a C++ exception */
407 FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
411 /******************************************************************
412 * __RTDynamicCast (MSVCRT.@)
414 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
415 type_info *src, type_info *dst,
418 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
419 TRACE("(%p,%d,%p,%p,%d)\n",cppobj, unknown, src, dst, do_throw);
422 FIXME("Unknown prameter is non-zero: please report\n");
424 /* To cast an object at runtime:
425 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
426 * 2.Search for the destination type in the class heirachy
427 * 3.If destination type is found, return base object address + dest offset
428 * Otherwise, fail the cast
430 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
431 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
432 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
435 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
436 rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
437 rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
438 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
440 while (count < obj_bases->array_len)
442 type_info *typ = (*base_desc)->type_descriptor;
444 if (!strcmp(typ->name, dst->name))
446 dst_offset = (*base_desc)->base_class_offset;
453 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
456 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
457 * to a reference, since references cannot be NULL.
460 FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
465 /******************************************************************
466 * __RTCastToVoid (MSVCRT.@)
468 void* MSVCRT___RTCastToVoid(type_info *cppobj)
470 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
471 TRACE("(%p)\n",cppobj);
473 /* Casts to void* simply cast to the base object */
474 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
475 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
476 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
478 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
479 int src_offset = obj_locator->base_class_offset;
481 return (void*)((unsigned long)cppobj - src_offset);
487 /* INTERNAL: Set up vtables
488 * FIXME:should be static, cope with versions?
490 void msvcrt_init_vtables(void)
492 exception_vtable[0] = MSVCRT_exception_dtor;
493 exception_vtable[1] = (void*)MSVCRT_what_exception;
495 bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
496 bad_typeid_vtable[1] = exception_vtable[1];
497 bad_typeid_vtable[2] = _purecall; /* FIXME */
499 __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
500 __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
501 __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
503 bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
504 bad_cast_vtable[1] = exception_vtable[1];
505 bad_cast_vtable[2] = _purecall; /* FIXME */
507 type_info_vtable[0] = MSVCRT_type_info_dtor;