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