source: trunk/src/msvcrt/cpp.c

Last change on this file was 10005, checked in by sandervl, 22 years ago

PF: MSVCRT update

File size: 15.7 KB
Line 
1/*
2 * msvcrt.dll C++ objects
3 *
4 * Copyright 2000 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "msvcrt.h"
22#include "msvcrt/eh.h"
23#include "msvcrt/stdlib.h"
24#include <string.h>
25#include "wine/debug.h"
26
27WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28
29typedef void (*v_table_ptr)();
30
31static v_table_ptr exception_vtable[2];
32static v_table_ptr bad_typeid_vtable[3];
33static v_table_ptr __non_rtti_object_vtable[3];
34static v_table_ptr bad_cast_vtable[3];
35static v_table_ptr type_info_vtable[1];
36
37typedef struct __exception
38{
39 v_table_ptr *vtable;
40 const char *name;
41 int do_free; /* FIXME: take string copy with char* ctor? */
42} exception;
43
44typedef struct __bad_typeid
45{
46 exception base;
47} bad_typeid;
48
49typedef struct ____non_rtti_object
50{
51 bad_typeid base;
52} __non_rtti_object;
53
54typedef struct __bad_cast
55{
56 exception base;
57} bad_cast;
58
59typedef struct __type_info
60{
61 v_table_ptr *vtable;
62 void *data;
63 char name[1];
64} type_info;
65
66typedef struct _rtti_base_descriptor
67{
68 type_info *type_descriptor;
69 int num_base_classes;
70 int base_class_offset;
71 unsigned int flags;
72} rtti_base_descriptor;
73
74typedef struct _rtti_base_array
75{
76 rtti_base_descriptor *bases[1]; /* First element is the class itself */
77} rtti_base_array;
78
79typedef struct _rtti_object_hierachy
80{
81 int unknown1;
82 int unknown2;
83 int array_len; /* Size of the array pointed to by 'base_classes' */
84 rtti_base_array *base_classes;
85} rtti_object_hierachy;
86
87typedef struct _rtti_object_locator
88{
89 int unknown1;
90 int base_class_offset;
91 unsigned int flags;
92 type_info *type_descriptor;
93 rtti_object_hierachy *type_hierachy;
94} rtti_object_locator;
95
96/******************************************************************
97 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
98 */
99void MSVCRT_exception_ctor(exception * _this, const char ** name)
100{
101 TRACE("MSVCRT: exception_ctor (%p %s)\n",_this,*name);
102 _this->vtable = exception_vtable;
103 _this->name = *name;
104 TRACE("name = %s\n",_this->name);
105 _this->do_free = 0; /* FIXME */
106}
107
108/******************************************************************
109 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
110 */
111void MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
112{
113 TRACE("MSVCRT: exception_copy_ctor (%p %p)\n",_this,rhs);
114 if (_this != rhs)
115 memcpy (_this, rhs, sizeof (*_this));
116 TRACE("name = %s\n",_this->name);
117}
118
119/******************************************************************
120 * ??0exception@@QAE@XZ (MSVCRT.@)
121 */
122void MSVCRT_exception_default_ctor(exception * _this)
123{
124 TRACE("MSVCRT: exception_default_ctor (%p)\n",_this);
125 _this->vtable = exception_vtable;
126 _this->name = "";
127 _this->do_free = 0; /* FIXME */
128}
129
130/******************************************************************
131 * ??1exception@@UAE@XZ (MSVCRT.@)
132 */
133void MSVCRT_exception_dtor(exception * _this)
134{
135 TRACE("MSVCRT: exception_dtor(%p)\n",_this);
136}
137
138/******************************************************************
139 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
140 */
141exception * MSVCRT_exception_opequals(exception * _this, const exception * rhs)
142{
143 TRACE("(%p %p)\n",_this,rhs);
144 memcpy (_this, rhs, sizeof (*_this));
145 TRACE("name = %s\n",_this->name);
146 return _this;
147}
148
149/******************************************************************
150 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
151 */
152void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
153{
154 TRACE("(%p %d)\n",_this,arg1);
155 _purecall();
156 return NULL;
157}
158
159/******************************************************************
160 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
161 */
162void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
163{
164 TRACE("(%p %d)\n",_this,arg1);
165 _purecall();
166 return NULL;
167}
168
169/******************************************************************
170 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
171 */
172const char * MSVCRT_what_exception(exception * _this)
173{
174 TRACE("(%p) returning %s\n",_this,_this->name);
175 return _this->name;
176}
177
178
179/******************************************************************
180 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
181 */
182terminate_function MSVCRT_set_terminate(terminate_function func)
183{
184 MSVCRT_thread_data *data = msvcrt_get_thread_data();
185 terminate_function previous = data->terminate_handler;
186 TRACE("(%p) returning %p\n",func,previous);
187 data->terminate_handler = func;
188 return previous;
189}
190
191/******************************************************************
192 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
193 */
194unexpected_function MSVCRT_set_unexpected(unexpected_function func)
195{
196 MSVCRT_thread_data *data = msvcrt_get_thread_data();
197 unexpected_function previous = data->unexpected_handler;
198 TRACE("(%p) returning %p\n",func,previous);
199 data->unexpected_handler = func;
200 return previous;
201}
202
203/******************************************************************
204 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
205 */
206_se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
207{
208 MSVCRT_thread_data *data = msvcrt_get_thread_data();
209 _se_translator_function previous = data->se_translator;
210 TRACE("(%p) returning %p\n",func,previous);
211 data->se_translator = func;
212 return previous;
213}
214
215/******************************************************************
216 * ?terminate@@YAXXZ (MSVCRT.@)
217 */
218void MSVCRT_terminate(void)
219{
220 MSVCRT_thread_data *data = msvcrt_get_thread_data();
221 if (data->terminate_handler) data->terminate_handler();
222 MSVCRT_abort();
223}
224
225/******************************************************************
226 * ?unexpected@@YAXXZ (MSVCRT.@)
227 */
228void MSVCRT_unexpected(void)
229{
230 MSVCRT_thread_data *data = msvcrt_get_thread_data();
231 if (data->unexpected_handler) data->unexpected_handler();
232 MSVCRT_terminate();
233}
234
235
236/******************************************************************
237 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
238 */
239void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
240{
241 TRACE("(%p %p)\n",_this,rhs);
242 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
243}
244
245/******************************************************************
246 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
247 */
248void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
249{
250 TRACE("(%p %s)\n",_this,name);
251 MSVCRT_exception_ctor(&_this->base, &name);
252 _this->base.vtable = bad_typeid_vtable;
253}
254
255/******************************************************************
256 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
257 */
258void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
259{
260 TRACE("(%p)\n",_this);
261 MSVCRT_exception_dtor(&_this->base);
262}
263
264/******************************************************************
265 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
266 */
267bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
268{
269 TRACE("(%p %p)\n",_this,rhs);
270 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
271 return _this;
272}
273
274/******************************************************************
275 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
276 */
277void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
278 const __non_rtti_object * rhs)
279{
280 TRACE("(%p %p)\n",_this,rhs);
281 MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
282}
283
284/******************************************************************
285 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
286 */
287void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
288 const char * name)
289{
290 TRACE("(%p %s)\n",_this,name);
291 MSVCRT_bad_typeid_ctor(&_this->base,name);
292 _this->base.base.vtable = __non_rtti_object_vtable;
293}
294
295/******************************************************************
296 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
297 */
298void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
299{
300 TRACE("(%p)\n",_this);
301 MSVCRT_bad_typeid_dtor(&_this->base);
302}
303
304/******************************************************************
305 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
306 */
307__non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
308 const __non_rtti_object *rhs)
309{
310 TRACE("(%p %p)\n",_this,rhs);
311 memcpy (_this, rhs, sizeof (*_this));
312 TRACE("name = %s\n",_this->base.base.name);
313 return _this;
314}
315
316/******************************************************************
317 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
318 */
319void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
320{
321 TRACE("(%p %d)\n",_this,arg1);
322 _purecall();
323 return NULL;
324}
325
326/******************************************************************
327 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
328 */
329void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
330{
331 TRACE("(%p %d)\n",_this,arg1);
332 _purecall();
333 return NULL;
334}
335
336/******************************************************************
337 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
338 */
339void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
340{
341 TRACE("(%p %s)\n",_this,*name);
342 MSVCRT_exception_ctor(&_this->base, name);
343 _this->base.vtable = bad_cast_vtable;
344}
345
346/******************************************************************
347 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
348 */
349void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
350{
351 TRACE("(%p %p)\n",_this,rhs);
352 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
353}
354
355/******************************************************************
356 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
357 */
358void MSVCRT_bad_cast_dtor(bad_cast * _this)
359{
360 TRACE("(%p)\n",_this);
361 MSVCRT_exception_dtor(&_this->base);
362}
363
364/******************************************************************
365 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
366 */
367bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
368{
369 TRACE("(%p %p)\n",_this,rhs);
370 MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
371 return _this;
372}
373
374/******************************************************************
375 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
376 */
377int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
378{
379 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
380 return _this->name == rhs->name;
381}
382
383/******************************************************************
384 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
385 */
386int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
387{
388 TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
389 return _this->name != rhs->name;
390}
391
392/******************************************************************
393 * ??1type_info@@UAE@XZ (MSVCRT.@)
394 */
395void MSVCRT_type_info_dtor(type_info * _this)
396{
397 TRACE("(%p)\n",_this);
398 if (_this->data)
399 MSVCRT_free(_this->data);
400}
401
402/******************************************************************
403 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
404 */
405const char * __stdcall MSVCRT_type_info_name(type_info * _this)
406{
407 TRACE("(%p) returning %s\n",_this,_this->name);
408 return _this->name;
409}
410
411/******************************************************************
412 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
413 */
414const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
415{
416 TRACE("(%p) returning %s\n",_this,_this->name);
417 return _this->name;
418}
419
420
421/******************************************************************
422 * __RTtypeid (MSVCRT.@)
423 */
424type_info* MSVCRT___RTtypeid(type_info *cppobj)
425{
426 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
427 TRACE("(%p)\n",cppobj);
428
429 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
430 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
431 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
432 {
433 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
434 return obj_locator->type_descriptor;
435 }
436 /* FIXME: throw a C++ exception */
437 FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
438 return NULL;
439}
440
441/******************************************************************
442 * __RTDynamicCast (MSVCRT.@)
443 */
444void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
445 type_info *src, type_info *dst,
446 int do_throw)
447{
448 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
449 TRACE("(%p,%d,%p,%p,%d)\n",cppobj, unknown, src, dst, do_throw);
450
451 if (unknown)
452 FIXME("Unknown prameter is non-zero: please report\n");
453
454 /* To cast an object at runtime:
455 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
456 * 2.Search for the destination type in the class heirachy
457 * 3.If destination type is found, return base object address + dest offset
458 * Otherwise, fail the cast
459 */
460 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
461 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
462 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
463 {
464 int count = 0;
465 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
466 rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
467 rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
468 int src_offset = obj_locator->base_class_offset, dst_offset = -1;
469
470 while (count < obj_bases->array_len)
471 {
472 type_info *typ = (*base_desc)->type_descriptor;
473
474 if (!strcmp(typ->name, dst->name))
475 {
476 dst_offset = (*base_desc)->base_class_offset;
477 break;
478 }
479 base_desc++;
480 count++;
481 }
482 if (dst_offset >= 0)
483 return (void*)((unsigned long)cppobj - src_offset + dst_offset);
484 }
485
486 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
487 * to a reference, since references cannot be NULL.
488 */
489 if (do_throw)
490 FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
491 return NULL;
492}
493
494
495/******************************************************************
496 * __RTCastToVoid (MSVCRT.@)
497 */
498void* MSVCRT___RTCastToVoid(type_info *cppobj)
499{
500 /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
501 TRACE("MSVCRT: ___RTCastToVoid (%p)\n",cppobj);
502
503 /* Casts to void* simply cast to the base object */
504 if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
505 !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
506 !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
507 {
508 rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
509 int src_offset = obj_locator->base_class_offset;
510
511 return (void*)((unsigned long)cppobj - src_offset);
512 }
513 return NULL;
514}
515
516
517/* INTERNAL: Set up vtables
518 * FIXME:should be static, cope with versions?
519 */
520void msvcrt_init_vtables(void)
521{
522 exception_vtable[0] = MSVCRT_exception_dtor;
523 exception_vtable[1] = (void*)MSVCRT_what_exception;
524
525 bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
526 bad_typeid_vtable[1] = exception_vtable[1];
527 bad_typeid_vtable[2] = _purecall; /* FIXME */
528
529 __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
530 __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
531 __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
532
533 bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
534 bad_cast_vtable[1] = exception_vtable[1];
535 bad_cast_vtable[2] = _purecall; /* FIXME */
536
537 type_info_vtable[0] = MSVCRT_type_info_dtor;
538
539}
Note: See TracBrowser for help on using the repository browser.