Removed some unnecessary includes.
[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
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
13
14
15 void _purecall(void);
16
17 typedef void (*v_table_ptr)();
18
19 static v_table_ptr exception_vtable[2];
20 static v_table_ptr bad_typeid_vtable[3];
21 static v_table_ptr __non_rtti_object_vtable[3];
22 static v_table_ptr bad_cast_vtable[3];
23 static v_table_ptr type_info_vtable[1];
24
25 typedef struct __exception
26 {
27   v_table_ptr *vtable;
28   const char *name;
29   int do_free; /* FIXME: take string copy with char* ctor? */
30 } exception;
31
32 typedef struct __bad_typeid
33 {
34   exception base;
35 } bad_typeid;
36
37 typedef struct ____non_rtti_object
38 {
39   bad_typeid base;
40 } __non_rtti_object;
41
42 typedef struct __bad_cast
43 {
44   exception base;
45 } bad_cast;
46
47 typedef struct __type_info
48 {
49   v_table_ptr *vtable;
50   void *data;
51   char name[1];
52 } type_info;
53
54 /******************************************************************
55  *              ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
56  */
57 void MSVCRT_exception_ctor(exception * _this, const char ** name)
58 {
59   TRACE("(%p %s)\n",_this,*name);
60   _this->vtable = exception_vtable;
61   _this->name = *name;
62   TRACE("name = %s\n",_this->name);
63   _this->do_free = 0; /* FIXME */
64 }
65
66 /******************************************************************
67  *              ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
68  */
69 void MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
70 {
71   TRACE("(%p %p)\n",_this,rhs);
72   if (_this != rhs)
73     memcpy (_this, rhs, sizeof (*_this));
74   TRACE("name = %s\n",_this->name);
75 }
76
77 /******************************************************************
78  *              ??0exception@@QAE@XZ (MSVCRT.@)
79  */
80 void MSVCRT_exception_default_ctor(exception * _this)
81 {
82   TRACE("(%p)\n",_this);
83   _this->vtable = exception_vtable;
84   _this->name = "";
85   _this->do_free = 0; /* FIXME */
86 }
87
88 /******************************************************************
89  *              ??1exception@@UAE@XZ (MSVCRT.@)
90  */
91 void MSVCRT_exception_dtor(exception * _this)
92 {
93   TRACE("(%p)\n",_this);
94 }
95
96 /******************************************************************
97  *              ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
98  */
99 exception * MSVCRT_exception_opequals(exception * _this, const exception * rhs)
100 {
101   TRACE("(%p %p)\n",_this,rhs);
102   memcpy (_this, rhs, sizeof (*_this));
103   TRACE("name = %s\n",_this->name);
104   return _this;
105 }
106
107 /******************************************************************
108  *              ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
109  */
110 void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
111 {
112   TRACE("(%p %d)\n",_this,arg1);
113   _purecall();
114   return NULL;
115 }
116
117 /******************************************************************
118  *              ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
119  */
120 void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
121 {
122   TRACE("(%p %d)\n",_this,arg1);
123   _purecall();
124   return NULL;
125 }
126
127 /******************************************************************
128  *              ?what@exception@@UBEPBDXZ (MSVCRT.@)
129  */
130 const char * __stdcall MSVCRT_exception_what(exception * _this)
131 {
132   TRACE("(%p) returning %s\n",_this,_this->name);
133   return _this->name;
134 }
135
136
137 static terminate_function func_terminate=NULL;
138 static unexpected_function func_unexpected=NULL;
139
140 /******************************************************************
141  *              ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
142  */
143 terminate_function MSVCRT_set_terminate(terminate_function func)
144 {
145   terminate_function previous=func_terminate;
146   TRACE("(%p) returning %p\n",func,previous);
147   func_terminate=func;
148   return previous;
149 }
150
151 /******************************************************************
152  *              ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
153  */
154 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
155 {
156   unexpected_function previous=func_unexpected;
157   TRACE("(%p) returning %p\n",func,previous);
158   func_unexpected=func;
159   return previous;
160 }
161
162 /******************************************************************
163  *              ?terminate@@YAXXZ (MSVCRT.@)
164  */
165 void MSVCRT_terminate()
166 {
167   (*func_terminate)();
168 }
169
170 /******************************************************************
171  *              ?unexpected@@YAXXZ (MSVCRT.@)
172  */
173 void MSVCRT_unexpected()
174 {
175   (*func_unexpected)();
176 }
177
178
179 /******************************************************************
180  *              ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
181  */
182 void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
183 {
184   TRACE("(%p %p)\n",_this,rhs);
185   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
186 }
187
188 /******************************************************************
189  *              ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
190  */
191 void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
192 {
193   TRACE("(%p %s)\n",_this,name);
194   MSVCRT_exception_ctor(&_this->base, &name);
195   _this->base.vtable = bad_typeid_vtable;
196 }
197
198 /******************************************************************
199  *              ??1bad_typeid@@UAE@XZ (MSVCRT.@)
200  */
201 void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
202 {
203   TRACE("(%p)\n",_this);
204   MSVCRT_exception_dtor(&_this->base);
205 }
206
207 /******************************************************************
208  *              ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
209  */
210 bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
211 {
212   TRACE("(%p %p)\n",_this,rhs);
213   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
214   return _this;
215 }
216
217 /******************************************************************
218  *              ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
219  */
220 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
221                                                 const __non_rtti_object * rhs)
222 {
223   TRACE("(%p %p)\n",_this,rhs);
224   MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
225 }
226
227 /******************************************************************
228  *              ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
229  */
230 void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
231                                            const char * name)
232 {
233   TRACE("(%p %s)\n",_this,name);
234   MSVCRT_bad_typeid_ctor(&_this->base,name);
235   _this->base.base.vtable = __non_rtti_object_vtable;
236 }
237
238 /******************************************************************
239  *              ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
240  */
241 void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
242 {
243   TRACE("(%p)\n",_this);
244   MSVCRT_bad_typeid_dtor(&_this->base);
245 }
246
247 /******************************************************************
248  *              ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
249  */
250 __non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
251                                                               const __non_rtti_object *rhs)
252 {
253   TRACE("(%p %p)\n",_this,rhs);
254   memcpy (_this, rhs, sizeof (*_this));
255   TRACE("name = %s\n",_this->base.base.name);
256   return _this;
257 }
258
259 /******************************************************************
260  *              ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
261  */
262 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
263 {
264   TRACE("(%p %d)\n",_this,arg1);
265   _purecall();
266   return NULL;
267 }
268
269 /******************************************************************
270  *              ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
271  */
272 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
273 {
274   TRACE("(%p %d)\n",_this,arg1);
275   _purecall();
276   return NULL;
277 }
278
279 /******************************************************************
280  *              ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
281  */
282 void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
283 {
284   TRACE("(%p %s)\n",_this,*name);
285   MSVCRT_exception_ctor(&_this->base, name);
286   _this->base.vtable = bad_cast_vtable;
287 }
288
289 /******************************************************************
290  *              ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
291  */
292 void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
293 {
294   TRACE("(%p %p)\n",_this,rhs);
295   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
296 }
297
298 /******************************************************************
299  *              ??1bad_cast@@UAE@XZ (MSVCRT.@)
300  */
301 void MSVCRT_bad_cast_dtor(bad_cast * _this)
302 {
303   TRACE("(%p)\n",_this);
304   MSVCRT_exception_dtor(&_this->base);
305 }
306
307 /******************************************************************
308  *              ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
309  */
310 bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
311 {
312   TRACE("(%p %p)\n",_this,rhs);
313   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
314   return _this;
315 }
316
317 /******************************************************************
318  *              ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
319  */
320 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
321 {
322   TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
323   return _this->name == rhs->name;
324 }
325
326 /******************************************************************
327  *              ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
328  */
329 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
330 {
331   TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
332   return _this->name != rhs->name;
333 }
334
335 /******************************************************************
336  *              ??1type_info@@UAE@XZ (MSVCRT.@)
337  */
338 void MSVCRT_type_info_dtor(type_info * _this)
339 {
340   TRACE("(%p)\n",_this);
341   if (_this->data)
342     MSVCRT_free(_this->data);
343 }
344
345 /******************************************************************
346  *              ?name@type_info@@QBEPBDXZ (MSVCRT.@)
347  */
348 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
349 {
350   TRACE("(%p) returning %s\n",_this,_this->name);
351   return _this->name;
352 }
353
354 /******************************************************************
355  *              ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
356  */
357 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
358 {
359   TRACE("(%p) returning %s\n",_this,_this->name);
360   return _this->name;
361 }
362
363
364 /* INTERNAL: Set up vtables
365  * FIXME:should be static, cope with versions?
366  */
367 void msvcrt_init_vtables(void)
368 {
369   exception_vtable[0] = MSVCRT_exception_dtor;
370   exception_vtable[1] = (void*)MSVCRT_exception_what;
371
372   bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
373   bad_typeid_vtable[1] = exception_vtable[1];
374   bad_typeid_vtable[2] = _purecall; /* FIXME */
375
376   __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
377   __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
378   __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
379
380   bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
381   bad_cast_vtable[1] = exception_vtable[1];
382   bad_cast_vtable[2] = _purecall; /* FIXME */
383
384   type_info_vtable[0] = MSVCRT_type_info_dtor;
385
386 }
387