2 * msvcrt.dll C++ objects
4 * Copyright 2000 Jon Griffiths
9 #include "msvcrt/malloc.h"
11 #include "wine/debug.h"
13 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
16 typedef void (*v_table_ptr)();
18 static v_table_ptr exception_vtable[2];
19 static v_table_ptr bad_typeid_vtable[3];
20 static v_table_ptr __non_rtti_object_vtable[3];
21 static v_table_ptr bad_cast_vtable[3];
22 static v_table_ptr type_info_vtable[1];
24 typedef struct __exception
28 int do_free; /* FIXME: take string copy with char* ctor? */
31 typedef struct __bad_typeid
36 typedef struct ____non_rtti_object
41 typedef struct __bad_cast
46 typedef struct __type_info
53 typedef struct _rtti_base_descriptor
55 type_info *type_descriptor;
57 int base_class_offset;
59 } rtti_base_descriptor;
61 typedef struct _rtti_base_array
63 rtti_base_descriptor *bases[1]; /* First element is the class itself */
66 typedef struct _rtti_object_hierachy
70 int array_len; /* Size of the array pointed to by 'base_classes' */
71 rtti_base_array *base_classes;
72 } rtti_object_hierachy;
74 typedef struct _rtti_object_locator
77 int base_class_offset;
79 type_info *type_descriptor;
80 rtti_object_hierachy *type_hierachy;
81 } rtti_object_locator;
83 /******************************************************************
84 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
86 void MSVCRT_exception_ctor(exception * _this, const char ** name)
88 TRACE("(%p %s)\n",_this,*name);
89 _this->vtable = exception_vtable;
91 TRACE("name = %s\n",_this->name);
92 _this->do_free = 0; /* FIXME */
95 /******************************************************************
96 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
98 void MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
100 TRACE("(%p %p)\n",_this,rhs);
102 memcpy (_this, rhs, sizeof (*_this));
103 TRACE("name = %s\n",_this->name);
106 /******************************************************************
107 * ??0exception@@QAE@XZ (MSVCRT.@)
109 void MSVCRT_exception_default_ctor(exception * _this)
111 TRACE("(%p)\n",_this);
112 _this->vtable = exception_vtable;
114 _this->do_free = 0; /* FIXME */
117 /******************************************************************
118 * ??1exception@@UAE@XZ (MSVCRT.@)
120 void MSVCRT_exception_dtor(exception * _this)
122 TRACE("(%p)\n",_this);
125 /******************************************************************
126 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
128 exception * MSVCRT_exception_opequals(exception * _this, const exception * rhs)
130 TRACE("(%p %p)\n",_this,rhs);
131 memcpy (_this, rhs, sizeof (*_this));
132 TRACE("name = %s\n",_this->name);
136 /******************************************************************
137 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
139 void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
141 TRACE("(%p %d)\n",_this,arg1);
146 /******************************************************************
147 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
149 void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
151 TRACE("(%p %d)\n",_this,arg1);
156 /******************************************************************
157 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
159 const char * MSVCRT_what_exception(exception * _this)
161 TRACE("(%p) returning %s\n",_this,_this->name);
166 static terminate_function func_terminate=NULL;
167 static unexpected_function func_unexpected=NULL;
169 /******************************************************************
170 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
172 terminate_function MSVCRT_set_terminate(terminate_function func)
174 terminate_function previous=func_terminate;
175 TRACE("(%p) returning %p\n",func,previous);
180 /******************************************************************
181 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
183 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
185 unexpected_function previous=func_unexpected;
186 TRACE("(%p) returning %p\n",func,previous);
187 func_unexpected=func;
191 /******************************************************************
192 * ?terminate@@YAXXZ (MSVCRT.@)
194 void MSVCRT_terminate()
199 /******************************************************************
200 * ?unexpected@@YAXXZ (MSVCRT.@)
202 void MSVCRT_unexpected()
204 (*func_unexpected)();
208 /******************************************************************
209 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
211 void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
213 TRACE("(%p %p)\n",_this,rhs);
214 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
217 /******************************************************************
218 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
220 void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
222 TRACE("(%p %s)\n",_this,name);
223 MSVCRT_exception_ctor(&_this->base, &name);
224 _this->base.vtable = bad_typeid_vtable;
227 /******************************************************************
228 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
230 void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
232 TRACE("(%p)\n",_this);
233 MSVCRT_exception_dtor(&_this->base);
236 /******************************************************************
237 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
239 bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
241 TRACE("(%p %p)\n",_this,rhs);
242 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
246 /******************************************************************
247 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
249 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
250 const __non_rtti_object * rhs)
252 TRACE("(%p %p)\n",_this,rhs);
253 MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
256 /******************************************************************
257 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
259 void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
262 TRACE("(%p %s)\n",_this,name);
263 MSVCRT_bad_typeid_ctor(&_this->base,name);
264 _this->base.base.vtable = __non_rtti_object_vtable;
267 /******************************************************************
268 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
270 void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
272 TRACE("(%p)\n",_this);
273 MSVCRT_bad_typeid_dtor(&_this->base);
276 /******************************************************************
277 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
279 __non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
280 const __non_rtti_object *rhs)
282 TRACE("(%p %p)\n",_this,rhs);
283 memcpy (_this, rhs, sizeof (*_this));
284 TRACE("name = %s\n",_this->base.base.name);
288 /******************************************************************
289 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
291 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
293 TRACE("(%p %d)\n",_this,arg1);
298 /******************************************************************
299 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
301 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
303 TRACE("(%p %d)\n",_this,arg1);
308 /******************************************************************
309 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
311 void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
313 TRACE("(%p %s)\n",_this,*name);
314 MSVCRT_exception_ctor(&_this->base, name);
315 _this->base.vtable = bad_cast_vtable;
318 /******************************************************************
319 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
321 void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
323 TRACE("(%p %p)\n",_this,rhs);
324 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
327 /******************************************************************
328 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
330 void MSVCRT_bad_cast_dtor(bad_cast * _this)
332 TRACE("(%p)\n",_this);
333 MSVCRT_exception_dtor(&_this->base);
336 /******************************************************************
337 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
339 bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
341 TRACE("(%p %p)\n",_this,rhs);
342 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
346 /******************************************************************
347 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
349 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
351 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
352 return _this->name == rhs->name;
355 /******************************************************************
356 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
358 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
360 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
361 return _this->name != rhs->name;
364 /******************************************************************
365 * ??1type_info@@UAE@XZ (MSVCRT.@)
367 void MSVCRT_type_info_dtor(type_info * _this)
369 TRACE("(%p)\n",_this);
371 MSVCRT_free(_this->data);
374 /******************************************************************
375 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
377 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
379 TRACE("(%p) returning %s\n",_this,_this->name);
383 /******************************************************************
384 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
386 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
388 TRACE("(%p) returning %s\n",_this,_this->name);
393 /******************************************************************
394 * __RTtypeid (MSVCRT.@)
396 type_info* MSVCRT___RTtypeid(type_info *cppobj)
398 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
399 TRACE("(%p)\n",cppobj);
401 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
402 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
403 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
405 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
406 return obj_locator->type_descriptor;
408 /* FIXME: throw a C++ exception */
409 FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
413 /******************************************************************
414 * __RTDynamicCast (MSVCRT.@)
416 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
417 type_info *src, type_info *dst,
420 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
421 TRACE("(%p,%d,%p,%p,%d)\n",cppobj, unknown, src, dst, do_throw);
424 FIXME("Unknown prameter is non-zero: please report\n");
426 /* To cast an object at runtime:
427 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
428 * 2.Search for the destination type in the class heirachy
429 * 3.If destination type is found, return base object address + dest offset
430 * Otherwise, fail the cast
432 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
433 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
434 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
437 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
438 rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
439 rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
440 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
442 while (count < obj_bases->array_len)
444 type_info *typ = (*base_desc)->type_descriptor;
446 if (!strcmp(typ->name, dst->name))
448 dst_offset = (*base_desc)->base_class_offset;
455 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
458 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
459 * to a reference, since references cannot be NULL.
462 FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
467 /******************************************************************
468 * __RTCastToVoid (MSVCRT.@)
470 void* MSVCRT___RTCastToVoid(type_info *cppobj)
472 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
473 TRACE("(%p)\n",cppobj);
475 /* Casts to void* simply cast to the base object */
476 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
477 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
478 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
480 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
481 int src_offset = obj_locator->base_class_offset;
483 return (void*)((unsigned long)cppobj - src_offset);
489 /* INTERNAL: Set up vtables
490 * FIXME:should be static, cope with versions?
492 void msvcrt_init_vtables(void)
494 exception_vtable[0] = MSVCRT_exception_dtor;
495 exception_vtable[1] = (void*)MSVCRT_what_exception;
497 bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
498 bad_typeid_vtable[1] = exception_vtable[1];
499 bad_typeid_vtable[2] = _purecall; /* FIXME */
501 __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
502 __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
503 __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
505 bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
506 bad_cast_vtable[1] = exception_vtable[1];
507 bad_cast_vtable[2] = _purecall; /* FIXME */
509 type_info_vtable[0] = MSVCRT_type_info_dtor;