cpp.c 32 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll C++ objects
 *
 * Copyright 2000 Jon Griffiths
5
 * Copyright 2003 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
#include "winternl.h"
#include "wine/exception.h"
#include "excpt.h"
#include "wine/debug.h"
#include "msvcrt/malloc.h"
35
#include "msvcrt/stdlib.h"
36

37 38 39
#include "msvcrt.h"
#include "cppexcept.h"
#include "mtdll.h"
40 41

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42

43 44 45 46 47 48 49 50
/*
 * exception object: base for exception, bad_cast, bad_typeid, __non_rtti_object
 */
typedef struct
{
  void* pfn_vector_dtor;
  void* pfn_what;
} exception_vtable;
51 52 53

typedef struct __exception
{
54 55 56
  const exception_vtable *vtable;
  char *name;    /* Name of this exception, always a new copy for each object */
  int   do_free; /* Whether to free 'name' in our dtor */
57 58
} exception;

59 60 61
typedef exception bad_cast;
typedef exception bad_typeid;
typedef exception __non_rtti_object;
62

63 64 65 66 67 68
typedef struct _rtti_base_descriptor
{
  type_info *type_descriptor;
  int num_base_classes;
  int base_class_offset;
  unsigned int flags;
69 70
  int unknown1;
  int unknown2;
71 72 73 74
} rtti_base_descriptor;

typedef struct _rtti_base_array
{
75
  const rtti_base_descriptor *bases[3]; /* First element is the class itself */
76 77 78 79 80 81 82
} rtti_base_array;

typedef struct _rtti_object_hierachy
{
  int unknown1;
  int unknown2;
  int array_len; /* Size of the array pointed to by 'base_classes' */
83
  const rtti_base_array *base_classes;
84 85 86 87 88 89 90 91
} rtti_object_hierachy;

typedef struct _rtti_object_locator
{
  int unknown1;
  int base_class_offset;
  unsigned int flags;
  type_info *type_descriptor;
92
  const rtti_object_hierachy *type_hierachy;
93 94
} rtti_object_locator;

95 96 97 98 99 100 101 102 103 104 105

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

#define DEFINE_THISCALL_WRAPPER(func) \
    extern void __thiscall_ ## func(); \
    __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
                      "popl %eax\n\t" \
                      "pushl %ecx\n\t" \
                      "pushl %eax\n\t" \
                      "jmp " __ASM_NAME(#func) )

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
const exception_vtable MSVCRT_exception_vtable;
const exception_vtable MSVCRT_bad_typeid_vtable;
const exception_vtable MSVCRT_bad_cast_vtable;
const exception_vtable MSVCRT___non_rtti_object_vtable;
static const exception_vtable MSVCRT_type_info_vtable;

/* 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;
  }
}
129

130
/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
131
 *		??0exception@@QAE@ABQBD@Z (MSVCRT.@)
132
 */
133 134
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor);
exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
135
{
136 137
  TRACE("(%p,%s)\n", _this, *name);
  EXCEPTION_ctor(_this, name);
138
  return _this;
139 140 141
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
142
 *		??0exception@@QAE@ABV0@@Z (MSVCRT.@)
143
 */
144 145
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor);
exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
146
{
147 148 149
  TRACE("(%p,%p)\n", _this, rhs);

  if (!rhs->do_free)
150
  {
151 152 153
    _this->vtable = &MSVCRT_exception_vtable;
    _this->name = rhs->name;
    _this->do_free = FALSE;
154
  }
155 156 157
  else
    EXCEPTION_ctor(_this, (const char**)&rhs->name);
  TRACE("name = %s\n", _this->name);
158
  return _this;
159 160 161
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
162
 *		??0exception@@QAE@XZ (MSVCRT.@)
163
 */
164 165
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor);
exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
166
{
167 168 169 170
  static const char* empty = NULL;

  TRACE("(%p)\n", _this);
  EXCEPTION_ctor(_this, &empty);
171
  return _this;
172 173 174
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
175
 *		??1exception@@UAE@XZ (MSVCRT.@)
176
 */
177 178
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor);
void __stdcall MSVCRT_exception_dtor(exception * _this)
179
{
180 181 182
  TRACE("(%p)\n", _this);
  _this->vtable = &MSVCRT_exception_vtable;
  if (_this->do_free) MSVCRT_free(_this->name);
183 184 185
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
186
 *		??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
187
 */
188 189
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals);
exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
190
{
191
  TRACE("(%p %p)\n", _this, rhs);
192 193 194 195 196
  if (_this != rhs)
  {
      MSVCRT_exception_dtor(_this);
      MSVCRT_exception_copy_ctor(_this, rhs);
  }
197
  TRACE("name = %s\n", _this->name);
198 199 200 201
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
202
 *		??_Eexception@@UAEPAXI@Z (MSVCRT.@)
203
 */
204 205
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor);
void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
206
{
207
    TRACE("(%p %x)\n", _this, flags);
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    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;
222 223 224
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
225
 *		??_Gexception@@UAEPAXI@Z (MSVCRT.@)
226
 */
227 228
DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor);
void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
229
{
230
    TRACE("(%p %x)\n", _this, flags);
231 232 233
    MSVCRT_exception_dtor(_this);
    if (flags & 1) MSVCRT_operator_delete(_this);
    return _this;
234 235 236
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
237
 *		?what@exception@@UBEPBDXZ (MSVCRT.@)
238
 */
239 240
DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception);
const char * __stdcall MSVCRT_what_exception(exception * _this)
241
{
242
  TRACE("(%p) returning %s\n", _this, _this->name);
243
  return _this->name ? _this->name : "Unknown exception";
244 245 246
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
247
 *		??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
248
 */
249 250
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor);
bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
251
{
252 253 254
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT_bad_typeid_vtable;
255
  return _this;
256 257 258
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
259
 *		??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
260
 */
261 262
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor);
bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
263
{
264 265 266
  TRACE("(%p %s)\n", _this, name);
  EXCEPTION_ctor(_this, &name);
  _this->vtable = &MSVCRT_bad_typeid_vtable;
267
  return _this;
268 269 270
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
271
 *		??1bad_typeid@@UAE@XZ (MSVCRT.@)
272
 */
273 274
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor);
void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
275
{
276 277
  TRACE("(%p)\n", _this);
  MSVCRT_exception_dtor(_this);
278 279 280
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
281
 *		??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
282
 */
283 284
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals);
bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
285
{
286 287
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_opequals(_this, rhs);
288 289 290
  return _this;
}

291 292 293 294 295 296
/******************************************************************
 *              ??_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)
{
297
    TRACE("(%p %x)\n", _this, flags);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    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)
{
320
    TRACE("(%p %x)\n", _this, flags);
321 322 323 324 325
    MSVCRT_bad_typeid_dtor(_this);
    if (flags & 1) MSVCRT_operator_delete(_this);
    return _this;
}

326
/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
327
 *		??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
328
 */
329 330 331
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)
332
{
333 334 335
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_bad_typeid_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT___non_rtti_object_vtable;
336
  return _this;
337 338 339
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
340
 *		??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
341
 */
342 343 344
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor);
__non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
                                                            const char * name)
345
{
346 347 348
  TRACE("(%p %s)\n", _this, name);
  EXCEPTION_ctor(_this, &name);
  _this->vtable = &MSVCRT___non_rtti_object_vtable;
349
  return _this;
350 351 352
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
353
 *		??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
354
 */
355 356
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor);
void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
357
{
358 359
  TRACE("(%p)\n", _this);
  MSVCRT_bad_typeid_dtor(_this);
360 361 362
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
363
 *		??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
364
 */
365 366 367
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)
368
{
369 370
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_bad_typeid_opequals(_this, rhs);
371 372 373 374
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
375
 *		??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
376
 */
377 378
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor);
void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
379
{
380
    TRACE("(%p %x)\n", _this, flags);
381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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;
395 396 397
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
398
 *		??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
399
 */
400 401
DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor);
void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
402
{
403
  TRACE("(%p %x)\n", _this, flags);
404 405 406
  MSVCRT___non_rtti_object_dtor(_this);
  if (flags & 1) MSVCRT_operator_delete(_this);
  return _this;
407 408 409
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
410
 *		??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
411
 */
412 413
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor);
bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
414
{
415 416 417
  TRACE("(%p %s)\n", _this, *name);
  EXCEPTION_ctor(_this, name);
  _this->vtable = &MSVCRT_bad_cast_vtable;
418
  return _this;
419 420 421
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
422
 *		??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
423
 */
424 425
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor);
bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
426
{
427 428 429
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_copy_ctor(_this, rhs);
  _this->vtable = &MSVCRT_bad_cast_vtable;
430
  return _this;
431 432 433
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
434
 *		??1bad_cast@@UAE@XZ (MSVCRT.@)
435
 */
436 437
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor);
void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
438
{
439 440
  TRACE("(%p)\n", _this);
  MSVCRT_exception_dtor(_this);
441 442 443
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
444
 *		??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
445
 */
446 447
DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals);
bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
448
{
449 450
  TRACE("(%p %p)\n", _this, rhs);
  MSVCRT_exception_opequals(_this, rhs);
451 452 453 454 455 456 457 458 459
  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)
{
460
    TRACE("(%p %x)\n", _this, flags);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    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)
{
483
  TRACE("(%p %x)\n", _this, flags);
484 485
  MSVCRT_bad_cast_dtor(_this);
  if (flags & 1) MSVCRT_operator_delete(_this);
486 487 488 489
  return _this;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
490
 *		??8type_info@@QBEHABV0@@Z (MSVCRT.@)
491
 */
492
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals);
493 494
int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
{
495 496
    int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
497
    return ret;
498 499 500
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
501
 *		??9type_info@@QBEHABV0@@Z (MSVCRT.@)
502
 */
503
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals);
504 505
int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
{
506 507
    int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
508 509 510 511 512 513 514 515 516
    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)
{
517 518
    int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
    TRACE("(%p %p) returning %d\n", _this, rhs, ret);
519
    return ret;
520 521 522
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
523
 *		??1type_info@@UAE@XZ (MSVCRT.@)
524
 */
525 526
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor);
void __stdcall MSVCRT_type_info_dtor(type_info * _this)
527
{
528 529 530
  TRACE("(%p)\n", _this);
  if (_this->name)
    MSVCRT_free(_this->name);
531 532 533
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
534
 *		?name@type_info@@QBEPBDXZ (MSVCRT.@)
535
 */
536
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name);
537 538
const char * __stdcall MSVCRT_type_info_name(type_info * _this)
{
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  if (!_this->name)
  {
    /* Create and set the demangled name */
    char* name = MSVCRT___unDName(0, _this->mangled, 0,
                                  (MSVCRT_malloc_func)MSVCRT_malloc,
                                  (MSVCRT_free_func)MSVCRT_free, 0x2800);

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

      /* It seems _unDName may leave blanks at the end of the demangled name */
      if (name[len] == ' ')
        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);
568 569 570 571
  return _this->name;
}

/******************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
572
 *		?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
573
 */
574
DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name);
575 576
const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
{
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  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;
600 601
}

602 603
/* vtables */

604
const exception_vtable MSVCRT_exception_vtable =
605 606 607 608 609
{
    __thiscall_MSVCRT_exception_vector_dtor,
    __thiscall_MSVCRT_what_exception
};

610
const exception_vtable MSVCRT_bad_typeid_vtable =
611 612 613 614 615
{
    __thiscall_MSVCRT_bad_typeid_vector_dtor,
    __thiscall_MSVCRT_what_exception
};

616
const exception_vtable MSVCRT_bad_cast_vtable =
617 618 619 620 621
{
    __thiscall_MSVCRT_bad_cast_vector_dtor,
    __thiscall_MSVCRT_what_exception
};

622
const exception_vtable MSVCRT___non_rtti_object_vtable =
623 624 625 626 627
{
    __thiscall_MSVCRT___non_rtti_object_vector_dtor,
    __thiscall_MSVCRT_what_exception
};

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
static const exception_vtable MSVCRT_type_info_vtable =
{
    __thiscall_MSVCRT_type_info_vector_dtor,
    NULL
};

/* Static RTTI for exported objects */

static type_info exception_type_info =
{
  (void*)&MSVCRT_type_info_vtable,
  NULL,
  ".?AVexception@@"
};

static const rtti_base_descriptor exception_rtti_base_descriptor =
{
  &exception_type_info,
  0,
  0,
  0,
  0,
  0
};

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

static const rtti_object_hierachy exception_type_hierachy =
{
  0,
  0,
  1,
  &exception_rtti_base_array
};

static const rtti_object_locator exception_rtti =
{
  0,
  0,
  0,
  &exception_type_info,
  &exception_type_hierachy
};

static const cxx_type_info exception_cxx_type_info =
{
  0,
  &exception_type_info,
  0,
  -1,
  0,
  sizeof(exception),
  (cxx_copy_ctor)__thiscall_MSVCRT_exception_copy_ctor
};

static type_info bad_typeid_type_info =
{
  (void*)&MSVCRT_type_info_vtable,
  NULL,
  ".?AVbad_typeid@@"
};

static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
{
  &bad_typeid_type_info,
  1,
  0,
  0xffffffff,
  0,
  0
};

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

static const rtti_object_hierachy bad_typeid_type_hierachy =
{
  0,
  0,
  2,
  &bad_typeid_rtti_base_array
};

static const rtti_object_locator bad_typeid_rtti =
{
  0,
  0,
  0,
  &bad_typeid_type_info,
  &bad_typeid_type_hierachy
};

static const cxx_type_info bad_typeid_cxx_type_info =
{
  0,
  &bad_typeid_type_info,
  0,
  -1,
  0,
  sizeof(exception),
  (cxx_copy_ctor)__thiscall_MSVCRT_bad_typeid_copy_ctor
};

static type_info bad_cast_type_info =
{
  (void*)&MSVCRT_type_info_vtable,
  NULL,
  ".?AVbad_cast@@"
};

static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
{
  &bad_cast_type_info,
  1,
  0,
  0xffffffff,
  0,
  0
};

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

static const rtti_object_hierachy bad_cast_type_hierachy =
{
  0,
  0,
  2,
  &bad_cast_rtti_base_array
};

static const rtti_object_locator bad_cast_rtti =
{
  0,
  0,
  0,
  &bad_cast_type_info,
  &bad_cast_type_hierachy
};

static const cxx_type_info bad_cast_cxx_type_info =
{
  0,
  &bad_cast_type_info,
  0,
  -1,
  0,
  sizeof(exception),
  (cxx_copy_ctor)__thiscall_MSVCRT_bad_cast_copy_ctor
};

static type_info __non_rtti_object_type_info =
{
  (void*)&MSVCRT_type_info_vtable,
  NULL,
  ".?AV__non_rtti_object@@"
};

static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
{
  &__non_rtti_object_type_info,
  2,
  0,
  0xffffffff,
  0,
  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
  }
};

static const rtti_object_hierachy __non_rtti_object_type_hierachy =
{
  0,
  0,
  3,
  &__non_rtti_object_rtti_base_array
};

static const rtti_object_locator __non_rtti_object_rtti =
{
  0,
  0,
  0,
  &__non_rtti_object_type_info,
  &__non_rtti_object_type_hierachy
};

static const cxx_type_info __non_rtti_object_cxx_type_info =
{
  0,
  &__non_rtti_object_type_info,
  0,
  -1,
  0,
  sizeof(exception),
  (cxx_copy_ctor)__thiscall_MSVCRT___non_rtti_object_copy_ctor
};

static type_info type_info_type_info =
{
  (void*)&MSVCRT_type_info_vtable,
  NULL,
  ".?AVtype_info@@"
};

static const rtti_base_descriptor type_info_rtti_base_descriptor =
{
  &type_info_type_info,
  0,
  0,
  0xffffffff,
  0,
  0
};

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

static const rtti_object_hierachy type_info_type_hierachy =
{
  0,
  0,
  1,
  &type_info_rtti_base_array
};

static const rtti_object_locator type_info_rtti =
{
  0,
  0,
  0,
  &type_info_type_info,
  &type_info_type_hierachy
};

/*
 * 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,
  (void*)__thiscall_MSVCRT_bad_cast_dtor,
  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,
  (void*)__thiscall_MSVCRT_bad_typeid_dtor,
  NULL,
  &bad_cast_type_info_table
};

static const cxx_exception_type __non_rtti_object_exception_type =
{
  0,
  (void*)__thiscall_MSVCRT___non_rtti_object_dtor,
  NULL,
  &bad_typeid_type_info_table
};

942 943 944 945 946
#endif  /* __i386__ */


/******************************************************************
 *		?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
947 948 949 950 951 952 953 954
 *
 * 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.
955 956 957 958 959 960 961 962 963 964 965 966
 */
terminate_function MSVCRT_set_terminate(terminate_function func)
{
    MSVCRT_thread_data *data = msvcrt_get_thread_data();
    terminate_function previous = data->terminate_handler;
    TRACE("(%p) returning %p\n",func,previous);
    data->terminate_handler = func;
    return previous;
}

/******************************************************************
 *		?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
967 968 969 970 971 972 973 974
 *
 * 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.
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
 */
unexpected_function MSVCRT_set_unexpected(unexpected_function func)
{
    MSVCRT_thread_data *data = msvcrt_get_thread_data();
    unexpected_function previous = data->unexpected_handler;
    TRACE("(%p) returning %p\n",func,previous);
    data->unexpected_handler = func;
    return previous;
}

/******************************************************************
 *              ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z  (MSVCRT.@)
 */
_se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
{
    MSVCRT_thread_data *data = msvcrt_get_thread_data();
    _se_translator_function previous = data->se_translator;
    TRACE("(%p) returning %p\n",func,previous);
    data->se_translator = func;
    return previous;
}

/******************************************************************
 *		?terminate@@YAXXZ (MSVCRT.@)
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
 *
 * 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.
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
 */
void MSVCRT_terminate(void)
{
    MSVCRT_thread_data *data = msvcrt_get_thread_data();
    if (data->terminate_handler) data->terminate_handler();
    MSVCRT_abort();
}

/******************************************************************
 *		?unexpected@@YAXXZ (MSVCRT.@)
 */
void MSVCRT_unexpected(void)
{
    MSVCRT_thread_data *data = msvcrt_get_thread_data();
    if (data->unexpected_handler) data->unexpected_handler();
    MSVCRT_terminate();
}
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
/* Get type info from an object (internal) */
static const rtti_object_locator* RTTI_GetObjectLocator(type_info *cppobj)
{
  const rtti_object_locator *obj_locator = NULL;

#ifdef __i386__
  const exception_vtable* vtable = (const exception_vtable*)cppobj->vtable;

  /* Perhaps this is one of classes we export? */
  if (vtable == &MSVCRT_exception_vtable)
  {
    TRACE("returning exception_rtti\n");
    return &exception_rtti;
  }
  else if (vtable == &MSVCRT_bad_typeid_vtable)
  {
    TRACE("returning bad_typeid_rtti\n");
    return &bad_typeid_rtti;
  }
  else if (vtable == &MSVCRT_bad_cast_vtable)
  {
    TRACE("returning bad_cast_rtti\n");
    return &bad_cast_rtti;
  }
  else if (vtable == &MSVCRT___non_rtti_object_vtable)
  {
    TRACE("returning __non_rtti_object_rtti\n");
    return &__non_rtti_object_rtti;
  }
  else if (vtable == &MSVCRT_type_info_vtable)
  {
    TRACE("returning type_info_rtti\n");
    return &type_info_rtti;
  }
#endif

  if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
      !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
      !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
  {
    obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
    TRACE("returning type_info from vtable (%p)\n", obj_locator);
  }

  return obj_locator;
}

1074 1075
/******************************************************************
 *		__RTtypeid (MSVCRT.@)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
 *
 * 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.
1091 1092 1093
 */
type_info* MSVCRT___RTtypeid(type_info *cppobj)
{
1094
  const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);
1095

1096 1097
#ifdef __i386__
  if (!obj_locator)
1098
  {
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    static const char* szNullPtr = "Attempted a typeid of NULL pointer!";
    static const char* szBadPtr = "Bad read pointer - no RTTI data!";
    const cxx_exception_type *e_type;
    exception e;

    /* Throw a bad_typeid or __non_rtti_object exception */
    if (!cppobj)
    {
      EXCEPTION_ctor(&e, &szNullPtr);
      e.vtable = &MSVCRT_bad_typeid_vtable;
      e_type = &bad_typeid_exception_type;
    }
    else
    {
      EXCEPTION_ctor(&e, &szBadPtr);
      e.vtable = &MSVCRT___non_rtti_object_vtable;
      e_type = &__non_rtti_object_exception_type;
    }

    _CxxThrowException(&e, e_type);
    DebugBreak();
1120
  }
1121 1122
  return obj_locator->type_descriptor;
#else
1123
  return NULL;
1124
#endif
1125 1126 1127 1128
}

/******************************************************************
 *		__RTDynamicCast (MSVCRT.@)
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
 *
 * 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.
1148 1149 1150 1151 1152
 */
void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
                             type_info *src, type_info *dst,
                             int do_throw)
{
1153 1154
  const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);

1155
  /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1156
  TRACE("(%p,%d,%p,%p,%d)\n", cppobj, unknown, src, dst, do_throw);
1157 1158

  if (unknown)
1159
    FIXME("Unknown parameter is non-zero: please report\n");
1160 1161 1162 1163 1164 1165 1166

  /* 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 heirachy
   * 3.If destination type is found, return base object address + dest offset
   *   Otherwise, fail the cast
   */
1167
  if (obj_locator)
1168 1169
  {
    int count = 0;
1170 1171
    const rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
    const rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
1172 1173 1174 1175
    int src_offset = obj_locator->base_class_offset, dst_offset = -1;

    while (count < obj_bases->array_len)
    {
1176
      const type_info *typ = (*base_desc)->type_descriptor;
1177

1178
      if (!strcmp(typ->mangled, dst->mangled))
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
      {
        dst_offset = (*base_desc)->base_class_offset;
        break;
      }
      base_desc++;
      count++;
    }
    if (dst_offset >= 0)
      return (void*)((unsigned long)cppobj - src_offset + dst_offset);
  }

1190
#ifdef __i386__
1191 1192 1193 1194
  /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
   * to a reference, since references cannot be NULL.
   */
  if (do_throw)
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
  {
    static const char* exception_text = "Bad dynamic_cast!";
    exception e;

    /* Throw a bad_cast exception */
    EXCEPTION_ctor(&e, &exception_text);
    e.vtable = &MSVCRT_bad_cast_vtable;
    _CxxThrowException(&e, &bad_cast_exception_type);
    DebugBreak();
  }
#endif
1206 1207 1208 1209 1210 1211
  return NULL;
}


/******************************************************************
 *		__RTCastToVoid (MSVCRT.@)
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
 *
 * 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.
1225 1226 1227
 */
void* MSVCRT___RTCastToVoid(type_info *cppobj)
{
1228 1229
  const rtti_object_locator *obj_locator = RTTI_GetObjectLocator(cppobj);

1230
  /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
1231
  TRACE("(%p)\n", cppobj);
1232 1233

  /* Casts to void* simply cast to the base object */
1234 1235
  if (obj_locator)
    return (void*)((unsigned long)cppobj - obj_locator->base_class_offset);
1236 1237
  return NULL;
}