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