cpp.c 32.4 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll C++ objects
 *
 * Copyright 2000 Jon Griffiths
5
 * Copyright 2003, 2004 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23 24
#include "config.h"
#include "wine/port.h"

25 26 27 28 29
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
30 31 32 33 34 35 36
#include "winternl.h"
#include "wine/exception.h"
#include "excpt.h"
#include "wine/debug.h"
#include "msvcrt.h"
#include "cppexcept.h"
#include "mtdll.h"
37 38

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39

40 41 42
typedef exception bad_cast;
typedef exception bad_typeid;
typedef exception __non_rtti_object;
43

44 45
typedef struct _rtti_base_descriptor
{
46
  const type_info *type_descriptor;
47
  int num_base_classes;
48 49
  this_ptr_offsets offsets;    /* offsets for computing the this pointer */
  unsigned int attributes;
50 51 52 53
} rtti_base_descriptor;

typedef struct _rtti_base_array
{
54
  const rtti_base_descriptor *bases[3]; /* First element is the class itself */
55 56
} rtti_base_array;

57
typedef struct _rtti_object_hierarchy
58
{
59 60
  unsigned int signature;
  unsigned int attributes;
61
  int array_len; /* Size of the array pointed to by 'base_classes' */
62
  const rtti_base_array *base_classes;
63
} rtti_object_hierarchy;
64 65 66

typedef struct _rtti_object_locator
{
67
  unsigned int signature;
68 69
  int base_class_offset;
  unsigned int flags;
70
  const type_info *type_descriptor;
71
  const rtti_object_hierarchy *type_hierarchy;
72 73
} rtti_object_locator;

74 75 76

#ifdef __i386__  /* thiscall functions are i386-specific */

77 78
#define THISCALL(func) __thiscall_ ## func
#define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
79
#define DEFINE_THISCALL_WRAPPER(func) \
80
    extern void THISCALL(func)(); \
81 82 83 84 85
    __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
                      "popl %eax\n\t" \
                      "pushl %ecx\n\t" \
                      "pushl %eax\n\t" \
                      "jmp " __ASM_NAME(#func) )
86 87 88 89 90 91 92
#else /* __i386__ */

#define THISCALL(func) func
#define THISCALL_NAME(func) __ASM_NAME(#func)
#define DEFINE_THISCALL_WRAPPER(func) /* nothing */

#endif /* __i386__ */
93

94 95 96 97 98 99 100 101 102 103 104
extern const vtable_ptr MSVCRT_exception_vtable;
extern const vtable_ptr MSVCRT_bad_typeid_vtable;
extern const vtable_ptr MSVCRT_bad_cast_vtable;
extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
extern const vtable_ptr MSVCRT_type_info_vtable;

static inline const rtti_object_locator *get_obj_locator( void *cppobj )
{
    const vtable_ptr *vtable = get_vtable( cppobj );
    return (const rtti_object_locator *)vtable[-1];
}
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static void dump_obj_locator( const rtti_object_locator *ptr )
{
    int i;
    const rtti_object_hierarchy *h = ptr->type_hierarchy;

    TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
           ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
           ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
    TRACE( "  hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
           h->signature, h->attributes, h->array_len, h->base_classes );
    for (i = 0; i < h->array_len; i++)
    {
        TRACE( "    base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
               h->base_classes->bases[i],
               h->base_classes->bases[i]->num_base_classes,
               h->base_classes->bases[i]->offsets.this_offset,
               h->base_classes->bases[i]->offsets.vbase_descr,
               h->base_classes->bases[i]->offsets.vbase_offset,
               h->base_classes->bases[i]->attributes,
               h->base_classes->bases[i]->type_descriptor,
               dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
    }
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* Internal common ctor for exception */
static void WINAPI EXCEPTION_ctor(exception *_this, const char** name)
{
  _this->vtable = &MSVCRT_exception_vtable;
  if (*name)
  {
    size_t name_len = strlen(*name) + 1;
    _this->name = MSVCRT_malloc(name_len);
    memcpy(_this->name, *name, name_len);
    _this->do_free = TRUE;
  }
  else
  {
    _this->name = NULL;
    _this->do_free = FALSE;
  }
}
147

148
/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
149
 *		??0exception@@QAE@ABQBD@Z (MSVCRT.@)
150
 */
151 152
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
153
{
154 155
  TRACE("(%p,%s)\n", _this, *name);
  EXCEPTION_ctor(_this, name);
156
  return _this;
157 158 159
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
160
 *		??0exception@@QAE@ABV0@@Z (MSVCRT.@)
161
 */
162 163
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
164
{
165 166 167
  TRACE("(%p,%p)\n", _this, rhs);

  if (!rhs->do_free)
168
  {
169 170 171
    _this->vtable = &MSVCRT_exception_vtable;
    _this->name = rhs->name;
    _this->do_free = FALSE;
172
  }
173 174 175
  else
    EXCEPTION_ctor(_this, (const char**)&rhs->name);
  TRACE("name = %s\n", _this->name);
176
  return _this;
177 178 179
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
180
 *		??0exception@@QAE@XZ (MSVCRT.@)
181
 */
182 183
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
184
{
185 186 187 188
  static const char* empty = NULL;

  TRACE("(%p)\n", _this);
  EXCEPTION_ctor(_this, &empty);
189
  return _this;
190 191 192
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
193
 *		??1exception@@UAE@XZ (MSVCRT.@)
194
 */
195 196
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
void __stdcall MSVCRT_exception_dtor(exception * _this)
197
{
198 199 200
  TRACE("(%p)\n", _this);
  _this->vtable = &MSVCRT_exception_vtable;
  if (_this->do_free) MSVCRT_free(_this->name);
201 202 203
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
204
 *		??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
205
 */
206 207
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
208
{
209
  TRACE("(%p %p)\n", _this, rhs);
210 211 212 213 214
  if (_this != rhs)
  {
      MSVCRT_exception_dtor(_this);
      MSVCRT_exception_copy_ctor(_this, rhs);
  }
215
  TRACE("name = %s\n", _this->name);
216 217 218 219
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
220
 *		??_Eexception@@UAEPAXI@Z (MSVCRT.@)
221
 */
222 223
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
224
{
225
    TRACE("(%p %x)\n", _this, flags);
226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if (flags & 2)
    {
        /* we have an array, with the number of elements stored before the first object */
        int i, *ptr = (int *)_this - 1;

        for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
        MSVCRT_operator_delete(ptr);
    }
    else
    {
        MSVCRT_exception_dtor(_this);
        if (flags & 1) MSVCRT_operator_delete(_this);
    }
    return _this;
240 241 242
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
243
 *		??_Gexception@@UAEPAXI@Z (MSVCRT.@)
244
 */
245 246
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
247
{
248
    TRACE("(%p %x)\n", _this, flags);
249 250 251
    MSVCRT_exception_dtor(_this);
    if (flags & 1) MSVCRT_operator_delete(_this);
    return _this;
252 253 254
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
255
 *		?what@exception@@UBEPBDXZ (MSVCRT.@)
256
 */
257 258
DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
const char * __stdcall MSVCRT_what_exception(exception * _this)
259
{
260
  TRACE("(%p) returning %s\n", _this, _this->name);
261
  return _this->name ? _this->name : "Unknown exception";
262 263 264
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
265
 *		??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
266
 */
267 268
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
269
{
270 271 272
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT_bad_typeid_vtable;
273
  return _this;
274 275 276
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
277
 *		??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
278
 */
279 280
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
281
{
282 283 284
  TRACE("(%p %s)\n", _this, name);
  EXCEPTION_ctor(_this, &name);
  _this->vtable = &MSVCRT_bad_typeid_vtable;
285
  return _this;
286 287 288
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
289
 *		??1bad_typeid@@UAE@XZ (MSVCRT.@)
290
 */
291 292
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
293
{
294 295
  TRACE("(%p)\n", _this);
  MSVCRT_exception_dtor(_this);
296 297 298
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
299
 *		??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
300
 */
301 302
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
303
{
304 305
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_opequals(_this, rhs);
306 307 308
  return _this;
}

309 310 311 312 313 314
/******************************************************************
 *              ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
 */
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor);
void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
{
315
    TRACE("(%p %x)\n", _this, flags);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    if (flags & 2)
    {
        /* we have an array, with the number of elements stored before the first object */
        int i, *ptr = (int *)_this - 1;

        for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
        MSVCRT_operator_delete(ptr);
    }
    else
    {
        MSVCRT_bad_typeid_dtor(_this);
        if (flags & 1) MSVCRT_operator_delete(_this);
    }
    return _this;
}

/******************************************************************
 *		??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
 */
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor);
void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
{
338
    TRACE("(%p %x)\n", _this, flags);
339 340 341 342 343
    MSVCRT_bad_typeid_dtor(_this);
    if (flags & 1) MSVCRT_operator_delete(_this);
    return _this;
}

344
/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
345
 *		??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
346
 */
347 348 349
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor);
__non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
                                                                 const __non_rtti_object * rhs)
350
{
351 352 353
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_bad_typeid_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT___non_rtti_object_vtable;
354
  return _this;
355 356 357
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
358
 *		??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
359
 */
360 361 362
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
__non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
                                                            const char * name)
363
{
364 365 366
  TRACE("(%p %s)\n", _this, name);
  EXCEPTION_ctor(_this, &name);
  _this->vtable = &MSVCRT___non_rtti_object_vtable;
367
  return _this;
368 369 370
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
371
 *		??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
372
 */
373 374
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
375
{
376 377
  TRACE("(%p)\n", _this);
  MSVCRT_bad_typeid_dtor(_this);
378 379 380
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
381
 *		??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
382
 */
383 384 385
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals);
__non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
                                                                const __non_rtti_object *rhs)
386
{
387 388
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_bad_typeid_opequals(_this, rhs);
389 390 391 392
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
393
 *		??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
394
 */
395 396
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
397
{
398
    TRACE("(%p %x)\n", _this, flags);
399 400 401 402 403 404 405 406 407 408 409 410 411 412
    if (flags & 2)
    {
        /* we have an array, with the number of elements stored before the first object */
        int i, *ptr = (int *)_this - 1;

        for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
        MSVCRT_operator_delete(ptr);
    }
    else
    {
        MSVCRT___non_rtti_object_dtor(_this);
        if (flags & 1) MSVCRT_operator_delete(_this);
    }
    return _this;
413 414 415
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
416
 *		??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
417
 */
418 419
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
420
{
421
  TRACE("(%p %x)\n", _this, flags);
422 423 424
  MSVCRT___non_rtti_object_dtor(_this);
  if (flags & 1) MSVCRT_operator_delete(_this);
  return _this;
425 426 427
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
428
 *		??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
429
 */
430 431
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
432
{
433 434 435
  TRACE("(%p %s)\n", _this, *name);
  EXCEPTION_ctor(_this, name);
  _this->vtable = &MSVCRT_bad_cast_vtable;
436
  return _this;
437 438 439
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
440
 *		??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
441
 */
442 443
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
444
{
445 446 447
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT_bad_cast_vtable;
448
  return _this;
449 450 451
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
452
 *		??1bad_cast@@UAE@XZ (MSVCRT.@)
453
 */
454 455
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
456
{
457 458
  TRACE("(%p)\n", _this);
  MSVCRT_exception_dtor(_this);
459 460 461
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
462
 *		??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
463
 */
464 465
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
466
{
467 468
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_opequals(_this, rhs);
469 470 471 472 473 474 475 476 477
  return _this;
}

/******************************************************************
 *              ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
 */
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor);
void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
{
478
    TRACE("(%p %x)\n", _this, flags);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    if (flags & 2)
    {
        /* we have an array, with the number of elements stored before the first object */
        int i, *ptr = (int *)_this - 1;

        for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
        MSVCRT_operator_delete(ptr);
    }
    else
    {
        MSVCRT_bad_cast_dtor(_this);
        if (flags & 1) MSVCRT_operator_delete(_this);
    }
    return _this;
}

/******************************************************************
 *		??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
 */
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor);
void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
{
501
  TRACE("(%p %x)\n", _this, flags);
502 503
  MSVCRT_bad_cast_dtor(_this);
  if (flags & 1) MSVCRT_operator_delete(_this);
504 505 506 507
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
508
 *		??8type_info@@QBEHABV0@@Z (MSVCRT.@)
509
 */
510
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
511 512
int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
{
513 514
    int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
515
    return ret;
516 517 518
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
519
 *		??9type_info@@QBEHABV0@@Z (MSVCRT.@)
520
 */
521
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
522 523
int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
{
524 525
    int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
526 527 528 529 530 531 532 533 534
    return ret;
}

/******************************************************************
 *		?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
 */
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before);
int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
{
535 536
    int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
537
    return ret;
538 539 540
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
541
 *		??1type_info@@UAE@XZ (MSVCRT.@)
542
 */
543 544
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
void __stdcall MSVCRT_type_info_dtor(type_info * _this)
545
{
546
  TRACE("(%p)\n", _this);
547
  MSVCRT_free(_this->name);
548 549 550
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
551
 *		?name@type_info@@QBEPBDXZ (MSVCRT.@)
552
 */
553
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
554 555
const char * __stdcall MSVCRT_type_info_name(type_info * _this)
{
556 557 558
  if (!_this->name)
  {
    /* Create and set the demangled name */
559 560 561 562 563 564
    /* Nota: mangled name in type_info struct always start with a '.', while
     * it isn't valid for mangled name.
     * Is this '.' really part of the mangled name, or has it some other meaning ?
     */
    char* name = __unDName(0, _this->mangled + 1, 0,
                           MSVCRT_malloc, MSVCRT_free, 0x2800);
565 566 567 568 569 570

    if (name)
    {
      unsigned int len = strlen(name);

      /* It seems _unDName may leave blanks at the end of the demangled name */
571
      while (len && name[--len] == ' ')
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
        name[len] = '\0';

      _mlock(_EXIT_LOCK2);

      if (_this->name)
      {
        /* Another thread set this member since we checked above - use it */
        MSVCRT_free(name);
      }
      else
        _this->name = name;

      _munlock(_EXIT_LOCK2);
    }
  }
  TRACE("(%p) returning %s\n", _this, _this->name);
588 589 590 591
  return _this->name;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
592
 *		?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
593
 */
594
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
595 596
const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
{
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  TRACE("(%p) returning %s\n", _this, _this->mangled);
  return _this->mangled;
}

/* Unexported */
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor);
void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
{
    TRACE("(%p %x)\n", _this, flags);
    if (flags & 2)
    {
        /* we have an array, with the number of elements stored before the first object */
        int i, *ptr = (int *)_this - 1;

        for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
        MSVCRT_operator_delete(ptr);
    }
    else
    {
        MSVCRT_type_info_dtor(_this);
        if (flags & 1) MSVCRT_operator_delete(_this);
    }
    return _this;
620 621
}

622 623
/* vtables */

624
#define __ASM_VTABLE(name,funcs) \
625 626
    __asm__(".data\n" \
            "\t.align 4\n" \
627 628 629 630
            "\t.long " __ASM_NAME(#name "_rtti") "\n" \
            "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
            __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
            "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
631
            funcs "\n\t.text");
632

633 634
#define __ASM_EXCEPTION_VTABLE(name) \
    __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) );
635

636 637 638
#ifndef __GNUC__
void __asm_dummy_vtables(void) {
#endif
639

640 641 642 643 644
__ASM_VTABLE(type_info,"");
__ASM_EXCEPTION_VTABLE(exception);
__ASM_EXCEPTION_VTABLE(bad_typeid);
__ASM_EXCEPTION_VTABLE(bad_cast);
__ASM_EXCEPTION_VTABLE(__non_rtti_object);
645

646 647 648
#ifndef __GNUC__
}
#endif
649 650 651

/* Static RTTI for exported objects */

652
static const type_info exception_type_info =
653
{
654
  &MSVCRT_type_info_vtable,
655 656 657 658 659 660 661 662
  NULL,
  ".?AVexception@@"
};

static const rtti_base_descriptor exception_rtti_base_descriptor =
{
  &exception_type_info,
  0,
663
  { 0, -1, 0 },
664 665 666 667 668 669 670 671 672 673 674 675
  0
};

static const rtti_base_array exception_rtti_base_array =
{
  {
    &exception_rtti_base_descriptor,
    NULL,
    NULL
  }
};

676
static const rtti_object_hierarchy exception_type_hierarchy =
677 678 679 680 681 682 683
{
  0,
  0,
  1,
  &exception_rtti_base_array
};

684
const rtti_object_locator exception_rtti =
685 686 687 688 689
{
  0,
  0,
  0,
  &exception_type_info,
690
  &exception_type_hierarchy
691 692 693 694 695 696
};

static const cxx_type_info exception_cxx_type_info =
{
  0,
  &exception_type_info,
697
  { 0, -1, 0 },
698
  sizeof(exception),
699
  (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
700 701
};

702
static const type_info bad_typeid_type_info =
703
{
704
  &MSVCRT_type_info_vtable,
705 706 707 708 709 710 711 712
  NULL,
  ".?AVbad_typeid@@"
};

static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
{
  &bad_typeid_type_info,
  1,
713
  { 0, -1, 0 },
714 715 716 717 718 719 720 721 722 723 724 725
  0
};

static const rtti_base_array bad_typeid_rtti_base_array =
{
  {
    &bad_typeid_rtti_base_descriptor,
    &exception_rtti_base_descriptor,
    NULL
  }
};

726
static const rtti_object_hierarchy bad_typeid_type_hierarchy =
727 728 729 730 731 732 733
{
  0,
  0,
  2,
  &bad_typeid_rtti_base_array
};

734
const rtti_object_locator bad_typeid_rtti =
735 736 737 738 739
{
  0,
  0,
  0,
  &bad_typeid_type_info,
740
  &bad_typeid_type_hierarchy
741 742 743 744 745 746
};

static const cxx_type_info bad_typeid_cxx_type_info =
{
  0,
  &bad_typeid_type_info,
747
  { 0, -1, 0 },
748
  sizeof(exception),
749
  (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
750 751
};

752
static const type_info bad_cast_type_info =
753
{
754
  &MSVCRT_type_info_vtable,
755 756 757 758 759 760 761 762
  NULL,
  ".?AVbad_cast@@"
};

static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
{
  &bad_cast_type_info,
  1,
763
  { 0, -1, 0 },
764 765 766 767 768 769 770 771 772 773 774 775
  0
};

static const rtti_base_array bad_cast_rtti_base_array =
{
  {
    &bad_cast_rtti_base_descriptor,
    &exception_rtti_base_descriptor,
    NULL
  }
};

776
static const rtti_object_hierarchy bad_cast_type_hierarchy =
777 778 779 780 781 782 783
{
  0,
  0,
  2,
  &bad_cast_rtti_base_array
};

784
const rtti_object_locator bad_cast_rtti =
785 786 787 788 789
{
  0,
  0,
  0,
  &bad_cast_type_info,
790
  &bad_cast_type_hierarchy
791 792 793 794 795 796
};

static const cxx_type_info bad_cast_cxx_type_info =
{
  0,
  &bad_cast_type_info,
797
  { 0, -1, 0 },
798
  sizeof(exception),
799
  (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
800 801
};

802
static const type_info __non_rtti_object_type_info =
803
{
804
  &MSVCRT_type_info_vtable,
805 806 807 808 809 810 811 812
  NULL,
  ".?AV__non_rtti_object@@"
};

static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
{
  &__non_rtti_object_type_info,
  2,
813
  { 0, -1, 0 },
814 815 816 817 818 819 820 821 822 823 824 825
  0
};

static const rtti_base_array __non_rtti_object_rtti_base_array =
{
  {
    &__non_rtti_object_rtti_base_descriptor,
    &bad_typeid_rtti_base_descriptor,
    &exception_rtti_base_descriptor
  }
};

826
static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
827 828 829 830 831 832 833
{
  0,
  0,
  3,
  &__non_rtti_object_rtti_base_array
};

834
const rtti_object_locator __non_rtti_object_rtti =
835 836 837 838 839
{
  0,
  0,
  0,
  &__non_rtti_object_type_info,
840
  &__non_rtti_object_type_hierarchy
841 842 843 844 845 846
};

static const cxx_type_info __non_rtti_object_cxx_type_info =
{
  0,
  &__non_rtti_object_type_info,
847
  { 0, -1, 0 },
848
  sizeof(exception),
849
  (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
850 851
};

852
static const type_info type_info_type_info =
853
{
854
  &MSVCRT_type_info_vtable,
855 856 857 858 859 860 861 862
  NULL,
  ".?AVtype_info@@"
};

static const rtti_base_descriptor type_info_rtti_base_descriptor =
{
  &type_info_type_info,
  0,
863
  { 0, -1, 0 },
864 865 866 867 868 869 870 871 872 873 874 875
  0
};

static const rtti_base_array type_info_rtti_base_array =
{
  {
    &type_info_rtti_base_descriptor,
    NULL,
    NULL
  }
};

876
static const rtti_object_hierarchy type_info_type_hierarchy =
877 878 879 880 881 882 883
{
  0,
  0,
  1,
  &type_info_rtti_base_array
};

884
const rtti_object_locator type_info_rtti =
885 886 887 888 889
{
  0,
  0,
  0,
  &type_info_type_info,
890
  &type_info_type_hierarchy
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
};

/*
 * Exception RTTI for cpp objects
 */
static const cxx_type_info_table bad_cast_type_info_table =
{
  3,
  {
   &__non_rtti_object_cxx_type_info,
   &bad_typeid_cxx_type_info,
   &exception_cxx_type_info
  }
};

static const cxx_exception_type bad_cast_exception_type =
{
  0,
909
  (void*)THISCALL(MSVCRT_bad_cast_dtor),
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  NULL,
  &bad_cast_type_info_table
};

static const cxx_type_info_table bad_typeid_type_info_table =
{
  2,
  {
   &bad_cast_cxx_type_info,
   &exception_cxx_type_info,
   NULL
  }
};

static const cxx_exception_type bad_typeid_exception_type =
{
  0,
927
  (void*)THISCALL(MSVCRT_bad_typeid_dtor),
928 929 930 931 932 933 934
  NULL,
  &bad_cast_type_info_table
};

static const cxx_exception_type __non_rtti_object_exception_type =
{
  0,
935
  (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
936 937 938 939
  NULL,
  &bad_typeid_type_info_table
};

940 941 942

/******************************************************************
 *		?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
943 944 945 946 947 948 949 950
 *
 * Install a handler to be called when terminate() is called.
 *
 * PARAMS
 *  func [I] Handler function to install
 *
 * RETURNS
 *  The previously installed handler function, if any.
951
 */
952
MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
953
{
954 955
    thread_data_t *data = msvcrt_get_thread_data();
    MSVCRT_terminate_function previous = data->terminate_handler;
956 957 958 959 960 961 962
    TRACE("(%p) returning %p\n",func,previous);
    data->terminate_handler = func;
    return previous;
}

/******************************************************************
 *		?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
963 964 965 966 967 968 969 970
 *
 * Install a handler to be called when unexpected() is called.
 *
 * PARAMS
 *  func [I] Handler function to install
 *
 * RETURNS
 *  The previously installed handler function, if any.
971
 */
972
MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
973
{
974 975
    thread_data_t *data = msvcrt_get_thread_data();
    MSVCRT_unexpected_function previous = data->unexpected_handler;
976 977 978 979 980 981 982 983
    TRACE("(%p) returning %p\n",func,previous);
    data->unexpected_handler = func;
    return previous;
}

/******************************************************************
 *              ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z  (MSVCRT.@)
 */
984
MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
985
{
986 987
    thread_data_t *data = msvcrt_get_thread_data();
    MSVCRT__se_translator_function previous = data->se_translator;
988 989 990 991 992 993 994
    TRACE("(%p) returning %p\n",func,previous);
    data->se_translator = func;
    return previous;
}

/******************************************************************
 *		?terminate@@YAXXZ (MSVCRT.@)
995 996 997 998 999 1000 1001 1002 1003 1004
 *
 * Default handler for an unhandled exception.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  This function does not return. Either control resumes from any
 *  handler installed by calling set_terminate(), or (by default) abort()
 *  is called.
1005
 */
1006
void CDECL MSVCRT_terminate(void)
1007
{
1008
    thread_data_t *data = msvcrt_get_thread_data();
1009 1010 1011 1012 1013 1014 1015
    if (data->terminate_handler) data->terminate_handler();
    MSVCRT_abort();
}

/******************************************************************
 *		?unexpected@@YAXXZ (MSVCRT.@)
 */
1016
void CDECL MSVCRT_unexpected(void)
1017
{
1018
    thread_data_t *data = msvcrt_get_thread_data();
1019 1020 1021
    if (data->unexpected_handler) data->unexpected_handler();
    MSVCRT_terminate();
}
1022

1023

1024 1025
/******************************************************************
 *		__RTtypeid (MSVCRT.@)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
 *
 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
 *
 * PARAMS
 *  cppobj [I] C++ object to get type information for.
 *
 * RETURNS
 *  Success: A type_info object describing cppobj.
 *  Failure: If the object to be cast has no RTTI, a __non_rtti_object
 *           exception is thrown. If cppobj is NULL, a bad_typeid exception
 *           is thrown. In either case, this function does not return.
 *
 * NOTES
 *  This function is usually called by compiler generated code as a result
 *  of using one of the C++ dynamic cast statements.
1041
 */
1042
const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1043
{
1044
    const type_info *ret;
1045 1046 1047

    if (!cppobj)
    {
1048 1049 1050 1051
        bad_typeid e;
        MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
        _CxxThrowException( &e, &bad_typeid_exception_type );
        return NULL;
1052
    }
1053 1054

    __TRY
1055
    {
1056 1057
        const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
        ret = obj_locator->type_descriptor;
1058
    }
1059
    __EXCEPT_PAGE_FAULT
1060 1061 1062 1063 1064 1065 1066 1067
    {
        __non_rtti_object e;
        MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
        _CxxThrowException( &e, &bad_typeid_exception_type );
        return NULL;
    }
    __ENDTRY
    return ret;
1068 1069 1070 1071
}

/******************************************************************
 *		__RTDynamicCast (MSVCRT.@)
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
 *
 * Dynamically cast a C++ object to one of its base classes.
 *
 * PARAMS
 *  cppobj   [I] Any C++ object to cast
 *  unknown  [I] Reserved, set to 0
 *  src      [I] type_info object describing cppobj
 *  dst      [I] type_info object describing the base class to cast to
 *  do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
 *
 * RETURNS
 *  Success: The address of cppobj, cast to the object described by dst.
 *  Failure: NULL, If the object to be cast has no RTTI, or dst is not a
 *           valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
 *           is thrown and this function does not return.
 *
 * NOTES
 *  This function is usually called by compiler generated code as a result
 *  of using one of the C++ dynamic cast statements.
1091
 */
1092 1093 1094
void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
                                   type_info *src, type_info *dst,
                                   int do_throw)
1095
{
1096
    void *ret;
1097

1098
    if (!cppobj) return NULL;
1099

1100 1101
    TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
          cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1102

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    if (unknown) FIXME("Unknown parameter is non-zero: please report\n");

    /* To cast an object at runtime:
     * 1.Find out the true type of the object from the typeinfo at vtable[-1]
     * 2.Search for the destination type in the class hierarchy
     * 3.If destination type is found, return base object address + dest offset
     *   Otherwise, fail the cast
     */
    __TRY
    {
        int i;
        const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
        const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1116
        const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

        if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);

        ret = NULL;
        for (i = 0; i < obj_bases->array_len; i++)
        {
            const type_info *typ = base_desc[i]->type_descriptor;

            if (!strcmp(typ->mangled, dst->mangled))
            {
                /* compute the correct this pointer for that base class */
                void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
                ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
                break;
            }
        }
        /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
         * to a reference, since references cannot be NULL.
         */
        if (!ret && do_throw)
        {
            const char *msg = "Bad dynamic_cast!";
            bad_cast e;
            MSVCRT_bad_cast_ctor( &e, &msg );
            _CxxThrowException( &e, &bad_cast_exception_type );
        }
    }
1144
    __EXCEPT_PAGE_FAULT
1145 1146 1147 1148 1149 1150 1151 1152
    {
        __non_rtti_object e;
        MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
        _CxxThrowException( &e, &bad_typeid_exception_type );
        return NULL;
    }
    __ENDTRY
    return ret;
1153 1154 1155 1156 1157
}


/******************************************************************
 *		__RTCastToVoid (MSVCRT.@)
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
 *
 * Dynamically cast a C++ object to a void*.
 *
 * PARAMS
 *  cppobj [I] The C++ object to cast
 *
 * RETURNS
 *  Success: The base address of the object as a void*.
 *  Failure: NULL, if cppobj is NULL or has no RTTI.
 *
 * NOTES
 *  This function is usually called by compiler generated code as a result
 *  of using one of the C++ dynamic cast statements.
1171
 */
1172
void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1173
{
1174
    void *ret;
1175

1176
    if (!cppobj) return NULL;
1177

1178 1179 1180 1181 1182
    __TRY
    {
        const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
        ret = (char *)cppobj - obj_locator->base_class_offset;
    }
1183
    __EXCEPT_PAGE_FAULT
1184 1185 1186 1187 1188 1189 1190 1191
    {
        __non_rtti_object e;
        MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
        _CxxThrowException( &e, &bad_typeid_exception_type );
        return NULL;
    }
    __ENDTRY
    return ret;
1192
}