cxxabi.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // new abi support -*- C++ -*-
  2. // Copyright (C) 2000, 2002, 2003, 2004, 2006, 2007, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of GCC.
  6. //
  7. // GCC is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // GCC is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // Under Section 7 of GPL version 3, you are granted additional
  18. // permissions described in the GCC Runtime Library Exception, version
  19. // 3.1, as published by the Free Software Foundation.
  20. // You should have received a copy of the GNU General Public License and
  21. // a copy of the GCC Runtime Library Exception along with this program;
  22. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  23. // <http://www.gnu.org/licenses/>.
  24. // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
  25. /* This file declares the new abi entry points into the runtime. It is not
  26. normally necessary for user programs to include this header, or use the
  27. entry points directly. However, this header is available should that be
  28. needed.
  29. Some of the entry points are intended for both C and C++, thus this header
  30. is includable from both C and C++. Though the C++ specific parts are not
  31. available in C, naturally enough. */
  32. /** @file cxxabi.h
  33. * The header provides an interface to the C++ ABI.
  34. */
  35. #ifndef _CXXABI_H
  36. #define _CXXABI_H 1
  37. #pragma GCC visibility push(default)
  38. #include <stddef.h>
  39. #include <bits/cxxabi_tweaks.h>
  40. #include <cxxabi-forced.h>
  41. #ifdef __cplusplus
  42. #define _GLIBCXX_NOTHROW throw()
  43. #else
  44. #define _GLIBCXX_NOTHROW __attribute__((nothrow))
  45. #endif
  46. #ifdef __cplusplus
  47. namespace __cxxabiv1
  48. {
  49. extern "C"
  50. {
  51. #endif
  52. typedef __cxa_cdtor_return_type (*__cxa_cdtor_type)(void *);
  53. // Allocate array.
  54. void*
  55. __cxa_vec_new(size_t __element_count, size_t __element_size,
  56. size_t __padding_size, __cxa_cdtor_type constructor,
  57. __cxa_cdtor_type destructor);
  58. void*
  59. __cxa_vec_new2(size_t __element_count, size_t __element_size,
  60. size_t __padding_size, __cxa_cdtor_type constructor,
  61. __cxa_cdtor_type destructor, void *(*__alloc) (size_t),
  62. void (*__dealloc) (void*));
  63. void*
  64. __cxa_vec_new3(size_t __element_count, size_t __element_size,
  65. size_t __padding_size, __cxa_cdtor_type constructor,
  66. __cxa_cdtor_type destructor, void *(*__alloc) (size_t),
  67. void (*__dealloc) (void*, size_t));
  68. // Construct array.
  69. __cxa_vec_ctor_return_type
  70. __cxa_vec_ctor(void* __array_address, size_t __element_count,
  71. size_t __element_size, __cxa_cdtor_type constructor,
  72. __cxa_cdtor_type destructor);
  73. __cxa_vec_ctor_return_type
  74. __cxa_vec_cctor(void* dest_array, void* src_array, size_t element_count,
  75. size_t element_size,
  76. __cxa_cdtor_return_type (*constructor) (void*, void*),
  77. __cxa_cdtor_type destructor);
  78. // Destruct array.
  79. void
  80. __cxa_vec_dtor(void* __array_address, size_t __element_count,
  81. size_t __element_size, __cxa_cdtor_type destructor);
  82. void
  83. __cxa_vec_cleanup(void* __array_address, size_t __element_count,
  84. size_t __element_size, __cxa_cdtor_type destructor);
  85. // Destruct and release array.
  86. void
  87. __cxa_vec_delete(void* __array_address, size_t __element_size,
  88. size_t __padding_size, __cxa_cdtor_type destructor);
  89. void
  90. __cxa_vec_delete2(void* __array_address, size_t __element_size,
  91. size_t __padding_size, __cxa_cdtor_type destructor,
  92. void (*__dealloc) (void*));
  93. void
  94. __cxa_vec_delete3(void* __array_address, size_t __element_size,
  95. size_t __padding_size, __cxa_cdtor_type destructor,
  96. void (*__dealloc) (void*, size_t));
  97. int
  98. __cxa_guard_acquire(__guard*);
  99. void
  100. __cxa_guard_release(__guard*);
  101. void
  102. __cxa_guard_abort(__guard*);
  103. // Pure virtual functions.
  104. void
  105. __cxa_pure_virtual(void);
  106. // Exception handling.
  107. void
  108. __cxa_bad_cast();
  109. void
  110. __cxa_bad_typeid();
  111. // DSO destruction.
  112. int
  113. __cxa_atexit(void (*)(void*), void*, void*) _GLIBCXX_NOTHROW;
  114. int
  115. __cxa_finalize(void*);
  116. /**
  117. * @brief Demangling routine.
  118. * ABI-mandated entry point in the C++ runtime library for demangling.
  119. *
  120. * @param __mangled_name A NUL-terminated character string
  121. * containing the name to be demangled.
  122. *
  123. * @param __output_buffer A region of memory, allocated with
  124. * malloc, of @a *__length bytes, into which the demangled name is
  125. * stored. If @a __output_buffer is not long enough, it is
  126. * expanded using realloc. @a __output_buffer may instead be NULL;
  127. * in that case, the demangled name is placed in a region of memory
  128. * allocated with malloc.
  129. *
  130. * @param __length If @a __length is non-NULL, the length of the
  131. * buffer containing the demangled name is placed in @a *__length.
  132. *
  133. * @param __status @a *__status is set to one of the following values:
  134. * 0: The demangling operation succeeded.
  135. * -1: A memory allocation failiure occurred.
  136. * -2: @a mangled_name is not a valid name under the C++ ABI mangling rules.
  137. * -3: One of the arguments is invalid.
  138. *
  139. * @return A pointer to the start of the NUL-terminated demangled
  140. * name, or NULL if the demangling fails. The caller is
  141. * responsible for deallocating this memory using @c free.
  142. *
  143. * The demangling is performed using the C++ ABI mangling rules,
  144. * with GNU extensions. For example, this function is used in
  145. * __gnu_cxx::__verbose_terminate_handler.
  146. *
  147. * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch39.html
  148. * for other examples of use.
  149. *
  150. * @note The same demangling functionality is available via
  151. * libiberty (@c <libiberty/demangle.h> and @c libiberty.a) in GCC
  152. * 3.1 and later, but that requires explicit installation (@c
  153. * --enable-install-libiberty) and uses a different API, although
  154. * the ABI is unchanged.
  155. */
  156. char*
  157. __cxa_demangle(const char* __mangled_name, char* __output_buffer,
  158. size_t* __length, int* __status);
  159. #ifdef __cplusplus
  160. }
  161. } // namespace __cxxabiv1
  162. #endif
  163. #ifdef __cplusplus
  164. #include <typeinfo>
  165. namespace __cxxabiv1
  166. {
  167. // Type information for int, float etc.
  168. class __fundamental_type_info : public std::type_info
  169. {
  170. public:
  171. explicit
  172. __fundamental_type_info(const char* __n) : std::type_info(__n) { }
  173. virtual
  174. ~__fundamental_type_info();
  175. };
  176. // Type information for array objects.
  177. class __array_type_info : public std::type_info
  178. {
  179. public:
  180. explicit
  181. __array_type_info(const char* __n) : std::type_info(__n) { }
  182. virtual
  183. ~__array_type_info();
  184. };
  185. // Type information for functions (both member and non-member).
  186. class __function_type_info : public std::type_info
  187. {
  188. public:
  189. explicit
  190. __function_type_info(const char* __n) : std::type_info(__n) { }
  191. virtual
  192. ~__function_type_info();
  193. protected:
  194. // Implementation defined member function.
  195. virtual bool
  196. __is_function_p() const;
  197. };
  198. // Type information for enumerations.
  199. class __enum_type_info : public std::type_info
  200. {
  201. public:
  202. explicit
  203. __enum_type_info(const char* __n) : std::type_info(__n) { }
  204. virtual
  205. ~__enum_type_info();
  206. };
  207. // Common type information for simple pointers and pointers to member.
  208. class __pbase_type_info : public std::type_info
  209. {
  210. public:
  211. unsigned int __flags; // Qualification of the target object.
  212. const std::type_info* __pointee; // Type of pointed to object.
  213. explicit
  214. __pbase_type_info(const char* __n, int __quals,
  215. const std::type_info* __type)
  216. : std::type_info(__n), __flags(__quals), __pointee(__type)
  217. { }
  218. virtual
  219. ~__pbase_type_info();
  220. // Implementation defined type.
  221. enum __masks
  222. {
  223. __const_mask = 0x1,
  224. __volatile_mask = 0x2,
  225. __restrict_mask = 0x4,
  226. __incomplete_mask = 0x8,
  227. __incomplete_class_mask = 0x10
  228. };
  229. protected:
  230. __pbase_type_info(const __pbase_type_info&);
  231. __pbase_type_info&
  232. operator=(const __pbase_type_info&);
  233. // Implementation defined member functions.
  234. virtual bool
  235. __do_catch(const std::type_info* __thr_type, void** __thr_obj,
  236. unsigned int __outer) const;
  237. inline virtual bool
  238. __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
  239. unsigned __outer) const;
  240. };
  241. // Type information for simple pointers.
  242. class __pointer_type_info : public __pbase_type_info
  243. {
  244. public:
  245. explicit
  246. __pointer_type_info(const char* __n, int __quals,
  247. const std::type_info* __type)
  248. : __pbase_type_info (__n, __quals, __type) { }
  249. virtual
  250. ~__pointer_type_info();
  251. protected:
  252. // Implementation defined member functions.
  253. virtual bool
  254. __is_pointer_p() const;
  255. virtual bool
  256. __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
  257. unsigned __outer) const;
  258. };
  259. class __class_type_info;
  260. // Type information for a pointer to member variable.
  261. class __pointer_to_member_type_info : public __pbase_type_info
  262. {
  263. public:
  264. __class_type_info* __context; // Class of the member.
  265. explicit
  266. __pointer_to_member_type_info(const char* __n, int __quals,
  267. const std::type_info* __type,
  268. __class_type_info* __klass)
  269. : __pbase_type_info(__n, __quals, __type), __context(__klass) { }
  270. virtual
  271. ~__pointer_to_member_type_info();
  272. protected:
  273. __pointer_to_member_type_info(const __pointer_to_member_type_info&);
  274. __pointer_to_member_type_info&
  275. operator=(const __pointer_to_member_type_info&);
  276. // Implementation defined member function.
  277. virtual bool
  278. __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
  279. unsigned __outer) const;
  280. };
  281. // Helper class for __vmi_class_type.
  282. class __base_class_type_info
  283. {
  284. public:
  285. const __class_type_info* __base_type; // Base class type.
  286. long __offset_flags; // Offset and info.
  287. enum __offset_flags_masks
  288. {
  289. __virtual_mask = 0x1,
  290. __public_mask = 0x2,
  291. __hwm_bit = 2,
  292. __offset_shift = 8 // Bits to shift offset.
  293. };
  294. // Implementation defined member functions.
  295. bool
  296. __is_virtual_p() const
  297. { return __offset_flags & __virtual_mask; }
  298. bool
  299. __is_public_p() const
  300. { return __offset_flags & __public_mask; }
  301. ptrdiff_t
  302. __offset() const
  303. {
  304. // This shift, being of a signed type, is implementation
  305. // defined. GCC implements such shifts as arithmetic, which is
  306. // what we want.
  307. return static_cast<ptrdiff_t>(__offset_flags) >> __offset_shift;
  308. }
  309. };
  310. // Type information for a class.
  311. class __class_type_info : public std::type_info
  312. {
  313. public:
  314. explicit
  315. __class_type_info (const char *__n) : type_info(__n) { }
  316. virtual
  317. ~__class_type_info ();
  318. // Implementation defined types.
  319. // The type sub_kind tells us about how a base object is contained
  320. // within a derived object. We often do this lazily, hence the
  321. // UNKNOWN value. At other times we may use NOT_CONTAINED to mean
  322. // not publicly contained.
  323. enum __sub_kind
  324. {
  325. // We have no idea.
  326. __unknown = 0,
  327. // Not contained within us (in some circumstances this might
  328. // mean not contained publicly)
  329. __not_contained,
  330. // Contained ambiguously.
  331. __contained_ambig,
  332. // Via a virtual path.
  333. __contained_virtual_mask = __base_class_type_info::__virtual_mask,
  334. // Via a public path.
  335. __contained_public_mask = __base_class_type_info::__public_mask,
  336. // Contained within us.
  337. __contained_mask = 1 << __base_class_type_info::__hwm_bit,
  338. __contained_private = __contained_mask,
  339. __contained_public = __contained_mask | __contained_public_mask
  340. };
  341. struct __upcast_result;
  342. struct __dyncast_result;
  343. protected:
  344. // Implementation defined member functions.
  345. virtual bool
  346. __do_upcast(const __class_type_info* __dst_type, void**__obj_ptr) const;
  347. virtual bool
  348. __do_catch(const type_info* __thr_type, void** __thr_obj,
  349. unsigned __outer) const;
  350. public:
  351. // Helper for upcast. See if DST is us, or one of our bases.
  352. // Return false if not found, true if found.
  353. virtual bool
  354. __do_upcast(const __class_type_info* __dst, const void* __obj,
  355. __upcast_result& __restrict __result) const;
  356. // Indicate whether SRC_PTR of type SRC_TYPE is contained publicly
  357. // within OBJ_PTR. OBJ_PTR points to a base object of our type,
  358. // which is the destination type. SRC2DST indicates how SRC
  359. // objects might be contained within this type. If SRC_PTR is one
  360. // of our SRC_TYPE bases, indicate the virtuality. Returns
  361. // not_contained for non containment or private containment.
  362. inline __sub_kind
  363. __find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
  364. const __class_type_info* __src_type,
  365. const void* __src_ptr) const;
  366. // Helper for dynamic cast. ACCESS_PATH gives the access from the
  367. // most derived object to this base. DST_TYPE indicates the
  368. // desired type we want. OBJ_PTR points to a base of our type
  369. // within the complete object. SRC_TYPE indicates the static type
  370. // started from and SRC_PTR points to that base within the most
  371. // derived object. Fill in RESULT with what we find. Return true
  372. // if we have located an ambiguous match.
  373. virtual bool
  374. __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
  375. const __class_type_info* __dst_type, const void* __obj_ptr,
  376. const __class_type_info* __src_type, const void* __src_ptr,
  377. __dyncast_result& __result) const;
  378. // Helper for find_public_subobj. SRC2DST indicates how SRC_TYPE
  379. // bases are inherited by the type started from -- which is not
  380. // necessarily the current type. The current type will be a base
  381. // of the destination type. OBJ_PTR points to the current base.
  382. virtual __sub_kind
  383. __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
  384. const __class_type_info* __src_type,
  385. const void* __src_ptr) const;
  386. };
  387. // Type information for a class with a single non-virtual base.
  388. class __si_class_type_info : public __class_type_info
  389. {
  390. public:
  391. const __class_type_info* __base_type;
  392. explicit
  393. __si_class_type_info(const char *__n, const __class_type_info *__base)
  394. : __class_type_info(__n), __base_type(__base) { }
  395. virtual
  396. ~__si_class_type_info();
  397. protected:
  398. __si_class_type_info(const __si_class_type_info&);
  399. __si_class_type_info&
  400. operator=(const __si_class_type_info&);
  401. // Implementation defined member functions.
  402. virtual bool
  403. __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
  404. const __class_type_info* __dst_type, const void* __obj_ptr,
  405. const __class_type_info* __src_type, const void* __src_ptr,
  406. __dyncast_result& __result) const;
  407. virtual __sub_kind
  408. __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
  409. const __class_type_info* __src_type,
  410. const void* __sub_ptr) const;
  411. virtual bool
  412. __do_upcast(const __class_type_info*__dst, const void*__obj,
  413. __upcast_result& __restrict __result) const;
  414. };
  415. // Type information for a class with multiple and/or virtual bases.
  416. class __vmi_class_type_info : public __class_type_info
  417. {
  418. public:
  419. unsigned int __flags; // Details about the class hierarchy.
  420. unsigned int __base_count; // Number of direct bases.
  421. // The array of bases uses the trailing array struct hack so this
  422. // class is not constructable with a normal constructor. It is
  423. // internally generated by the compiler.
  424. __base_class_type_info __base_info[1]; // Array of bases.
  425. explicit
  426. __vmi_class_type_info(const char* __n, int ___flags)
  427. : __class_type_info(__n), __flags(___flags), __base_count(0) { }
  428. virtual
  429. ~__vmi_class_type_info();
  430. // Implementation defined types.
  431. enum __flags_masks
  432. {
  433. __non_diamond_repeat_mask = 0x1, // Distinct instance of repeated base.
  434. __diamond_shaped_mask = 0x2, // Diamond shaped multiple inheritance.
  435. __flags_unknown_mask = 0x10
  436. };
  437. protected:
  438. // Implementation defined member functions.
  439. virtual bool
  440. __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
  441. const __class_type_info* __dst_type, const void* __obj_ptr,
  442. const __class_type_info* __src_type, const void* __src_ptr,
  443. __dyncast_result& __result) const;
  444. virtual __sub_kind
  445. __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
  446. const __class_type_info* __src_type,
  447. const void* __src_ptr) const;
  448. virtual bool
  449. __do_upcast(const __class_type_info* __dst, const void* __obj,
  450. __upcast_result& __restrict __result) const;
  451. };
  452. // Dynamic cast runtime.
  453. // src2dst has the following possible values
  454. // >-1: src_type is a unique public non-virtual base of dst_type
  455. // dst_ptr + src2dst == src_ptr
  456. // -1: unspecified relationship
  457. // -2: src_type is not a public base of dst_type
  458. // -3: src_type is a multiple public non-virtual base of dst_type
  459. extern "C" void*
  460. __dynamic_cast(const void* __src_ptr, // Starting object.
  461. const __class_type_info* __src_type, // Static type of object.
  462. const __class_type_info* __dst_type, // Desired target type.
  463. ptrdiff_t __src2dst); // How src and dst are related.
  464. // Returns the type_info for the currently handled exception [15.3/8], or
  465. // null if there is none.
  466. extern "C" std::type_info*
  467. __cxa_current_exception_type();
  468. // A magic placeholder class that can be caught by reference
  469. // to recognize foreign exceptions.
  470. class __foreign_exception
  471. {
  472. virtual ~__foreign_exception() throw();
  473. virtual void __pure_dummy() = 0; // prevent catch by value
  474. };
  475. } // namespace __cxxabiv1
  476. /** @namespace abi
  477. * @brief The cross-vendor C++ Application Binary Interface. A
  478. * namespace alias to __cxxabiv1, but user programs should use the
  479. * alias `abi'.
  480. *
  481. * A brief overview of an ABI is given in the libstdc++ FAQ, question
  482. * 5.8 (you may have a copy of the FAQ locally, or you can view the online
  483. * version at http://gcc.gnu.org/onlinedocs/libstdc++/faq/index.html#5_8).
  484. *
  485. * GCC subscribes to a cross-vendor ABI for C++, sometimes
  486. * called the IA64 ABI because it happens to be the native ABI for that
  487. * platform. It is summarized at http://www.codesourcery.com/cxx-abi/
  488. * along with the current specification.
  489. *
  490. * For users of GCC greater than or equal to 3.x, entry points are
  491. * available in <cxxabi.h>, which notes, <em>"It is not normally
  492. * necessary for user programs to include this header, or use the
  493. * entry points directly. However, this header is available should
  494. * that be needed."</em>
  495. */
  496. namespace abi = __cxxabiv1;
  497. #endif // __cplusplus
  498. #pragma GCC visibility pop
  499. #endif // __CXXABI_H