Release 20030408.
[wine/gsoc-2012-control.git] / dlls / msvcrt / cpp.c
blob6bb322c6f88639f4779f8b266ad55a9cd6a839ad
1 /*
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
21 #include "msvcrt.h"
22 #include "msvcrt/eh.h"
23 #include "msvcrt/stdlib.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
40 v_table_ptr *vtable;
41 const char *name;
42 int do_free; /* FIXME: take string copy with char* ctor? */
43 } exception;
45 typedef struct __bad_typeid
47 exception base;
48 } bad_typeid;
50 typedef struct ____non_rtti_object
52 bad_typeid base;
53 } __non_rtti_object;
55 typedef struct __bad_cast
57 exception base;
58 } bad_cast;
60 typedef struct __type_info
62 v_table_ptr *vtable;
63 void *data;
64 char name[1];
65 } type_info;
67 typedef struct _rtti_base_descriptor
69 type_info *type_descriptor;
70 int num_base_classes;
71 int base_class_offset;
72 unsigned int flags;
73 } rtti_base_descriptor;
75 typedef struct _rtti_base_array
77 rtti_base_descriptor *bases[1]; /* First element is the class itself */
78 } rtti_base_array;
80 typedef struct _rtti_object_hierachy
82 int unknown1;
83 int unknown2;
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
90 int unknown1;
91 int base_class_offset;
92 unsigned int flags;
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;
104 _this->name = *name;
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);
115 if (_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;
127 _this->name = "";
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);
147 return _this;
150 /******************************************************************
151 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
153 void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
155 TRACE("(%p %d)\n",_this,arg1);
156 _purecall();
157 return NULL;
160 /******************************************************************
161 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
163 void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
165 TRACE("(%p %d)\n",_this,arg1);
166 _purecall();
167 return NULL;
170 /******************************************************************
171 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
173 const char * MSVCRT_what_exception(exception * _this)
175 TRACE("(%p) returning %s\n",_this,_this->name);
176 return _this->name;
180 /******************************************************************
181 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
183 terminate_function MSVCRT_set_terminate(terminate_function func)
185 MSVCRT_thread_data *data = msvcrt_get_thread_data();
186 terminate_function previous = data->terminate_handler;
187 TRACE("(%p) returning %p\n",func,previous);
188 data->terminate_handler = func;
189 return previous;
192 /******************************************************************
193 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
195 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
197 MSVCRT_thread_data *data = msvcrt_get_thread_data();
198 unexpected_function previous = data->unexpected_handler;
199 TRACE("(%p) returning %p\n",func,previous);
200 data->unexpected_handler = func;
201 return previous;
204 /******************************************************************
205 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
207 _se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
209 MSVCRT_thread_data *data = msvcrt_get_thread_data();
210 _se_translator_function previous = data->se_translator;
211 TRACE("(%p) returning %p\n",func,previous);
212 data->se_translator = func;
213 return previous;
216 /******************************************************************
217 * ?terminate@@YAXXZ (MSVCRT.@)
219 void MSVCRT_terminate(void)
221 MSVCRT_thread_data *data = msvcrt_get_thread_data();
222 if (data->terminate_handler) data->terminate_handler();
223 MSVCRT_abort();
226 /******************************************************************
227 * ?unexpected@@YAXXZ (MSVCRT.@)
229 void MSVCRT_unexpected(void)
231 MSVCRT_thread_data *data = msvcrt_get_thread_data();
232 if (data->unexpected_handler) data->unexpected_handler();
233 MSVCRT_terminate();
237 /******************************************************************
238 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
240 void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
242 TRACE("(%p %p)\n",_this,rhs);
243 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
246 /******************************************************************
247 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
249 void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
251 TRACE("(%p %s)\n",_this,name);
252 MSVCRT_exception_ctor(&_this->base, &name);
253 _this->base.vtable = bad_typeid_vtable;
256 /******************************************************************
257 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
259 void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
261 TRACE("(%p)\n",_this);
262 MSVCRT_exception_dtor(&_this->base);
265 /******************************************************************
266 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
268 bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
270 TRACE("(%p %p)\n",_this,rhs);
271 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
272 return _this;
275 /******************************************************************
276 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
278 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
279 const __non_rtti_object * rhs)
281 TRACE("(%p %p)\n",_this,rhs);
282 MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
285 /******************************************************************
286 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
288 void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
289 const char * name)
291 TRACE("(%p %s)\n",_this,name);
292 MSVCRT_bad_typeid_ctor(&_this->base,name);
293 _this->base.base.vtable = __non_rtti_object_vtable;
296 /******************************************************************
297 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
299 void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
301 TRACE("(%p)\n",_this);
302 MSVCRT_bad_typeid_dtor(&_this->base);
305 /******************************************************************
306 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
308 __non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
309 const __non_rtti_object *rhs)
311 TRACE("(%p %p)\n",_this,rhs);
312 memcpy (_this, rhs, sizeof (*_this));
313 TRACE("name = %s\n",_this->base.base.name);
314 return _this;
317 /******************************************************************
318 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
320 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
322 TRACE("(%p %d)\n",_this,arg1);
323 _purecall();
324 return NULL;
327 /******************************************************************
328 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
330 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
332 TRACE("(%p %d)\n",_this,arg1);
333 _purecall();
334 return NULL;
337 /******************************************************************
338 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
340 void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
342 TRACE("(%p %s)\n",_this,*name);
343 MSVCRT_exception_ctor(&_this->base, name);
344 _this->base.vtable = bad_cast_vtable;
347 /******************************************************************
348 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
350 void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
352 TRACE("(%p %p)\n",_this,rhs);
353 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
356 /******************************************************************
357 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
359 void MSVCRT_bad_cast_dtor(bad_cast * _this)
361 TRACE("(%p)\n",_this);
362 MSVCRT_exception_dtor(&_this->base);
365 /******************************************************************
366 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
368 bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
370 TRACE("(%p %p)\n",_this,rhs);
371 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
372 return _this;
375 /******************************************************************
376 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
378 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
380 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
381 return _this->name == rhs->name;
384 /******************************************************************
385 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
387 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
389 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
390 return _this->name != rhs->name;
393 /******************************************************************
394 * ??1type_info@@UAE@XZ (MSVCRT.@)
396 void MSVCRT_type_info_dtor(type_info * _this)
398 TRACE("(%p)\n",_this);
399 if (_this->data)
400 MSVCRT_free(_this->data);
403 /******************************************************************
404 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
406 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
408 TRACE("(%p) returning %s\n",_this,_this->name);
409 return _this->name;
412 /******************************************************************
413 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
415 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
417 TRACE("(%p) returning %s\n",_this,_this->name);
418 return _this->name;
422 /******************************************************************
423 * __RTtypeid (MSVCRT.@)
425 type_info* MSVCRT___RTtypeid(type_info *cppobj)
427 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
428 TRACE("(%p)\n",cppobj);
430 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
431 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
432 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
434 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
435 return obj_locator->type_descriptor;
437 /* FIXME: throw a C++ exception */
438 FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
439 return NULL;
442 /******************************************************************
443 * __RTDynamicCast (MSVCRT.@)
445 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
446 type_info *src, type_info *dst,
447 int do_throw)
449 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
450 TRACE("(%p,%d,%p,%p,%d)\n",cppobj, unknown, src, dst, do_throw);
452 if (unknown)
453 FIXME("Unknown prameter is non-zero: please report\n");
455 /* To cast an object at runtime:
456 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
457 * 2.Search for the destination type in the class heirachy
458 * 3.If destination type is found, return base object address + dest offset
459 * Otherwise, fail the cast
461 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
462 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
463 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
465 int count = 0;
466 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
467 rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
468 rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
469 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
471 while (count < obj_bases->array_len)
473 type_info *typ = (*base_desc)->type_descriptor;
475 if (!strcmp(typ->name, dst->name))
477 dst_offset = (*base_desc)->base_class_offset;
478 break;
480 base_desc++;
481 count++;
483 if (dst_offset >= 0)
484 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
487 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
488 * to a reference, since references cannot be NULL.
490 if (do_throw)
491 FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
492 return NULL;
496 /******************************************************************
497 * __RTCastToVoid (MSVCRT.@)
499 void* MSVCRT___RTCastToVoid(type_info *cppobj)
501 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
502 TRACE("(%p)\n",cppobj);
504 /* Casts to void* simply cast to the base object */
505 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
506 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
507 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
509 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
510 int src_offset = obj_locator->base_class_offset;
512 return (void*)((unsigned long)cppobj - src_offset);
514 return NULL;
518 /* INTERNAL: Set up vtables
519 * FIXME:should be static, cope with versions?
521 void msvcrt_init_vtables(void)
523 exception_vtable[0] = MSVCRT_exception_dtor;
524 exception_vtable[1] = (void*)MSVCRT_what_exception;
526 bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
527 bad_typeid_vtable[1] = exception_vtable[1];
528 bad_typeid_vtable[2] = _purecall; /* FIXME */
530 __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
531 __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
532 __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
534 bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
535 bad_cast_vtable[1] = exception_vtable[1];
536 bad_cast_vtable[2] = _purecall; /* FIXME */
538 type_info_vtable[0] = MSVCRT_type_info_dtor;