OSDN Git Service

e78f551ed45c9962018f7059682ec65e5df1153f
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / cxxabi.h
1 // ABI Support -*- C++ -*-
2
3 // Copyright (C) 2000, 2002, 2003, 2004, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of GCC.
7 //
8 // GCC is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // GCC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
28
29 /* This file declares the new abi entry points into the runtime. It is not
30    normally necessary for user programs to include this header, or use the
31    entry points directly. However, this header is available should that be
32    needed.
33
34    Some of the entry points are intended for both C and C++, thus this header
35    is includable from both C and C++. Though the C++ specific parts are not
36    available in C, naturally enough.  */
37
38 /** @file cxxabi.h
39  *  The header provides an interface to the C++ ABI.
40  */
41
42 #ifndef _CXXABI_H
43 #define _CXXABI_H 1
44
45 #pragma GCC system_header
46
47 #pragma GCC visibility push(default)
48
49 #include <stddef.h>
50 #include <bits/c++config.h>
51 #include <bits/cxxabi_tweaks.h>
52 #include <bits/cxxabi_forced.h>
53
54 #ifdef __cplusplus
55 namespace __cxxabiv1
56 {
57   extern "C"
58   {
59 #endif
60
61   typedef __cxa_cdtor_return_type (*__cxa_cdtor_type)(void *);
62
63   // Allocate array.
64   void*
65   __cxa_vec_new(size_t __element_count, size_t __element_size,
66                 size_t __padding_size, __cxa_cdtor_type __constructor,
67                 __cxa_cdtor_type __destructor);
68
69   void*
70   __cxa_vec_new2(size_t __element_count, size_t __element_size,
71                  size_t __padding_size, __cxa_cdtor_type __constructor,
72                  __cxa_cdtor_type __destructor, void *(*__alloc) (size_t),
73                  void (*__dealloc) (void*));
74
75   void*
76   __cxa_vec_new3(size_t __element_count, size_t __element_size,
77                  size_t __padding_size, __cxa_cdtor_type __constructor,
78                  __cxa_cdtor_type __destructor, void *(*__alloc) (size_t),
79                  void (*__dealloc) (void*, size_t));
80
81   // Construct array.
82   __cxa_vec_ctor_return_type
83   __cxa_vec_ctor(void* __array_address, size_t __element_count,
84                  size_t __element_size, __cxa_cdtor_type __constructor,
85                  __cxa_cdtor_type __destructor);
86
87   __cxa_vec_ctor_return_type
88   __cxa_vec_cctor(void* __dest_array, void* __src_array,
89                   size_t __element_count, size_t __element_size,
90                   __cxa_cdtor_return_type (*__constructor) (void*, void*),
91                   __cxa_cdtor_type __destructor);
92
93   // Destruct array.
94   void
95   __cxa_vec_dtor(void* __array_address, size_t __element_count,
96                  size_t __element_size, __cxa_cdtor_type __destructor);
97
98   void
99   __cxa_vec_cleanup(void* __array_address, size_t __element_count, size_t __s,
100                     __cxa_cdtor_type __destructor) _GLIBCXX_NOTHROW;
101
102   // Destruct and release array.
103   void
104   __cxa_vec_delete(void* __array_address, size_t __element_size,
105                    size_t __padding_size, __cxa_cdtor_type __destructor);
106
107   void
108   __cxa_vec_delete2(void* __array_address, size_t __element_size,
109                     size_t __padding_size, __cxa_cdtor_type __destructor,
110                     void (*__dealloc) (void*));
111
112   void
113   __cxa_vec_delete3(void* __array_address, size_t __element_size,
114                     size_t __padding_size, __cxa_cdtor_type __destructor,
115                     void (*__dealloc) (void*, size_t));
116
117   int
118   __cxa_guard_acquire(__guard*);
119
120   void
121   __cxa_guard_release(__guard*) _GLIBCXX_NOTHROW;
122
123   void
124   __cxa_guard_abort(__guard*) _GLIBCXX_NOTHROW;
125
126   // DSO destruction.
127   int
128   __cxa_atexit(void (*)(void*), void*, void*) _GLIBCXX_NOTHROW;
129
130   int
131   __cxa_finalize(void*);
132
133   // Pure virtual functions.
134   void
135   __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
136
137   void
138   __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
139
140   // Exception handling auxillary.
141   void 
142   __cxa_bad_cast() __attribute__((__noreturn__));
143
144   void 
145   __cxa_bad_typeid() __attribute__((__noreturn__));
146
147
148   /**
149    *  @brief Demangling routine.
150    *  ABI-mandated entry point in the C++ runtime library for demangling.
151    *
152    *  @param __mangled_name A NUL-terminated character string
153    *  containing the name to be demangled.
154    *
155    *  @param __output_buffer A region of memory, allocated with
156    *  malloc, of @a *__length bytes, into which the demangled name is
157    *  stored.  If @a __output_buffer is not long enough, it is
158    *  expanded using realloc.  @a __output_buffer may instead be NULL;
159    *  in that case, the demangled name is placed in a region of memory
160    *  allocated with malloc.
161    *
162    *  @param __length If @a __length is non-NULL, the length of the
163    *  buffer containing the demangled name is placed in @a *__length.
164    *
165    *  @param __status @a *__status is set to one of the following values:
166    *   0: The demangling operation succeeded.
167    *  -1: A memory allocation failure occurred.
168    *  -2: @a mangled_name is not a valid name under the C++ ABI mangling rules.
169    *  -3: One of the arguments is invalid.
170    *
171    *  @return A pointer to the start of the NUL-terminated demangled
172    *  name, or NULL if the demangling fails.  The caller is
173    *  responsible for deallocating this memory using @c free.
174    *
175    *  The demangling is performed using the C++ ABI mangling rules,
176    *  with GNU extensions. For example, this function is used in
177    *  __gnu_cxx::__verbose_terminate_handler.
178    *
179    *  See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch39.html
180    *  for other examples of use.
181    *
182    *  @note The same demangling functionality is available via
183    *  libiberty (@c <libiberty/demangle.h> and @c libiberty.a) in GCC
184    *  3.1 and later, but that requires explicit installation (@c
185    *  --enable-install-libiberty) and uses a different API, although
186    *  the ABI is unchanged.
187    */
188   char*
189   __cxa_demangle(const char* __mangled_name, char* __output_buffer,
190                  size_t* __length, int* __status);
191
192 #ifdef __cplusplus
193   }
194 } // namespace __cxxabiv1
195 #endif
196
197 #ifdef __cplusplus
198
199 #include <typeinfo>
200
201 namespace __cxxabiv1
202 {
203   // Type information for int, float etc.
204   class __fundamental_type_info : public std::type_info
205   {
206   public:
207     explicit
208     __fundamental_type_info(const char* __n) : std::type_info(__n) { }
209
210     virtual
211     ~__fundamental_type_info();
212   };
213
214   // Type information for array objects.
215   class __array_type_info : public std::type_info
216   {
217   public:
218     explicit
219     __array_type_info(const char* __n) : std::type_info(__n) { }
220
221     virtual
222     ~__array_type_info();
223   };
224
225   // Type information for functions (both member and non-member).
226   class __function_type_info : public std::type_info
227   {
228   public:
229     explicit
230     __function_type_info(const char* __n) : std::type_info(__n) { }
231
232     virtual
233     ~__function_type_info();
234
235   protected:
236     // Implementation defined member function.
237     virtual bool
238     __is_function_p() const;
239   };
240
241   // Type information for enumerations.
242   class __enum_type_info : public std::type_info
243   {
244   public:
245     explicit
246     __enum_type_info(const char* __n) : std::type_info(__n) { }
247
248     virtual
249     ~__enum_type_info();
250   };
251
252   // Common type information for simple pointers and pointers to member.
253   class __pbase_type_info : public std::type_info
254   {
255   public:
256     unsigned int                __flags; // Qualification of the target object.
257     const std::type_info*       __pointee; // Type of pointed to object.
258
259     explicit
260     __pbase_type_info(const char* __n, int __quals,
261                       const std::type_info* __type)
262     : std::type_info(__n), __flags(__quals), __pointee(__type)
263     { }
264
265     virtual
266     ~__pbase_type_info();
267
268     // Implementation defined type.
269     enum __masks
270       {
271         __const_mask = 0x1,
272         __volatile_mask = 0x2,
273         __restrict_mask = 0x4,
274         __incomplete_mask = 0x8,
275         __incomplete_class_mask = 0x10
276       };
277
278   protected:
279     __pbase_type_info(const __pbase_type_info&);
280
281     __pbase_type_info&
282     operator=(const __pbase_type_info&);
283
284     // Implementation defined member functions.
285     virtual bool
286     __do_catch(const std::type_info* __thr_type, void** __thr_obj,
287                unsigned int __outer) const;
288
289     inline virtual bool
290     __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
291                     unsigned __outer) const;
292   };
293
294   // Type information for simple pointers.
295   class __pointer_type_info : public __pbase_type_info
296   {
297   public:
298     explicit
299     __pointer_type_info(const char* __n, int __quals,
300                         const std::type_info* __type)
301     : __pbase_type_info (__n, __quals, __type) { }
302
303
304     virtual
305     ~__pointer_type_info();
306
307   protected:
308     // Implementation defined member functions.
309     virtual bool
310     __is_pointer_p() const;
311
312     virtual bool
313     __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
314                     unsigned __outer) const;
315   };
316
317   class __class_type_info;
318
319   // Type information for a pointer to member variable.
320   class __pointer_to_member_type_info : public __pbase_type_info
321   {
322   public:
323     __class_type_info* __context;   // Class of the member.
324
325     explicit
326     __pointer_to_member_type_info(const char* __n, int __quals,
327                                   const std::type_info* __type,
328                                   __class_type_info* __klass)
329     : __pbase_type_info(__n, __quals, __type), __context(__klass) { }
330
331     virtual
332     ~__pointer_to_member_type_info();
333
334   protected:
335     __pointer_to_member_type_info(const __pointer_to_member_type_info&);
336
337     __pointer_to_member_type_info&
338     operator=(const __pointer_to_member_type_info&);
339
340     // Implementation defined member function.
341     virtual bool
342     __pointer_catch(const __pbase_type_info* __thr_type, void** __thr_obj,
343                     unsigned __outer) const;
344   };
345
346   // Helper class for __vmi_class_type.
347   class __base_class_type_info
348   {
349   public:
350     const __class_type_info*    __base_type;  // Base class type.
351     long                        __offset_flags;  // Offset and info.
352
353     enum __offset_flags_masks
354       {
355         __virtual_mask = 0x1,
356         __public_mask = 0x2,
357         __hwm_bit = 2,
358         __offset_shift = 8          // Bits to shift offset.
359       };
360
361     // Implementation defined member functions.
362     bool
363     __is_virtual_p() const
364     { return __offset_flags & __virtual_mask; }
365
366     bool
367     __is_public_p() const
368     { return __offset_flags & __public_mask; }
369
370     ptrdiff_t
371     __offset() const
372     {
373       // This shift, being of a signed type, is implementation
374       // defined. GCC implements such shifts as arithmetic, which is
375       // what we want.
376       return static_cast<ptrdiff_t>(__offset_flags) >> __offset_shift;
377     }
378   };
379
380   // Type information for a class.
381   class __class_type_info : public std::type_info
382   {
383   public:
384     explicit
385     __class_type_info (const char *__n) : type_info(__n) { }
386
387     virtual
388     ~__class_type_info ();
389
390     // Implementation defined types.
391     // The type sub_kind tells us about how a base object is contained
392     // within a derived object. We often do this lazily, hence the
393     // UNKNOWN value. At other times we may use NOT_CONTAINED to mean
394     // not publicly contained.
395     enum __sub_kind
396       {
397         // We have no idea.
398         __unknown = 0,
399
400         // Not contained within us (in some circumstances this might
401         // mean not contained publicly)
402         __not_contained,
403
404         // Contained ambiguously.
405         __contained_ambig,
406
407         // Via a virtual path.
408         __contained_virtual_mask = __base_class_type_info::__virtual_mask,
409
410         // Via a public path.
411         __contained_public_mask = __base_class_type_info::__public_mask,
412
413         // Contained within us.
414         __contained_mask = 1 << __base_class_type_info::__hwm_bit,
415
416         __contained_private = __contained_mask,
417         __contained_public = __contained_mask | __contained_public_mask
418       };
419
420     struct __upcast_result;
421     struct __dyncast_result;
422
423   protected:
424     // Implementation defined member functions.
425     virtual bool
426     __do_upcast(const __class_type_info* __dst_type, void**__obj_ptr) const;
427
428     virtual bool
429     __do_catch(const type_info* __thr_type, void** __thr_obj,
430                unsigned __outer) const;
431
432   public:
433     // Helper for upcast. See if DST is us, or one of our bases.
434     // Return false if not found, true if found.
435     virtual bool
436     __do_upcast(const __class_type_info* __dst, const void* __obj,
437                 __upcast_result& __restrict __result) const;
438
439     // Indicate whether SRC_PTR of type SRC_TYPE is contained publicly
440     // within OBJ_PTR. OBJ_PTR points to a base object of our type,
441     // which is the destination type. SRC2DST indicates how SRC
442     // objects might be contained within this type.  If SRC_PTR is one
443     // of our SRC_TYPE bases, indicate the virtuality. Returns
444     // not_contained for non containment or private containment.
445     inline __sub_kind
446     __find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
447                       const __class_type_info* __src_type,
448                       const void* __src_ptr) const;
449
450     // Helper for dynamic cast. ACCESS_PATH gives the access from the
451     // most derived object to this base. DST_TYPE indicates the
452     // desired type we want. OBJ_PTR points to a base of our type
453     // within the complete object. SRC_TYPE indicates the static type
454     // started from and SRC_PTR points to that base within the most
455     // derived object. Fill in RESULT with what we find. Return true
456     // if we have located an ambiguous match.
457     virtual bool
458     __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
459                  const __class_type_info* __dst_type, const void* __obj_ptr,
460                  const __class_type_info* __src_type, const void* __src_ptr,
461                  __dyncast_result& __result) const;
462
463     // Helper for find_public_subobj. SRC2DST indicates how SRC_TYPE
464     // bases are inherited by the type started from -- which is not
465     // necessarily the current type. The current type will be a base
466     // of the destination type.  OBJ_PTR points to the current base.
467     virtual __sub_kind
468     __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
469                          const __class_type_info* __src_type,
470                          const void* __src_ptr) const;
471   };
472
473   // Type information for a class with a single non-virtual base.
474   class __si_class_type_info : public __class_type_info
475   {
476   public:
477     const __class_type_info* __base_type;
478
479     explicit
480     __si_class_type_info(const char *__n, const __class_type_info *__base)
481     : __class_type_info(__n), __base_type(__base) { }
482
483     virtual
484     ~__si_class_type_info();
485
486   protected:
487     __si_class_type_info(const __si_class_type_info&);
488
489     __si_class_type_info&
490     operator=(const __si_class_type_info&);
491
492     // Implementation defined member functions.
493     virtual bool
494     __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
495                  const __class_type_info* __dst_type, const void* __obj_ptr,
496                  const __class_type_info* __src_type, const void* __src_ptr,
497                  __dyncast_result& __result) const;
498
499     virtual __sub_kind
500     __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
501                          const __class_type_info* __src_type,
502                          const void* __sub_ptr) const;
503
504     virtual bool
505     __do_upcast(const __class_type_info*__dst, const void*__obj,
506                 __upcast_result& __restrict __result) const;
507   };
508
509   // Type information for a class with multiple and/or virtual bases.
510   class __vmi_class_type_info : public __class_type_info
511   {
512   public:
513     unsigned int                __flags;  // Details about the class hierarchy.
514     unsigned int                __base_count;  // Number of direct bases.
515
516     // The array of bases uses the trailing array struct hack so this
517     // class is not constructable with a normal constructor. It is
518     // internally generated by the compiler.
519     __base_class_type_info      __base_info[1];  // Array of bases.
520
521     explicit
522     __vmi_class_type_info(const char* __n, int ___flags)
523     : __class_type_info(__n), __flags(___flags), __base_count(0) { }
524
525     virtual
526     ~__vmi_class_type_info();
527
528     // Implementation defined types.
529     enum __flags_masks
530       {
531         __non_diamond_repeat_mask = 0x1, // Distinct instance of repeated base.
532         __diamond_shaped_mask = 0x2, // Diamond shaped multiple inheritance.
533         __flags_unknown_mask = 0x10
534       };
535
536   protected:
537     // Implementation defined member functions.
538     virtual bool
539     __do_dyncast(ptrdiff_t __src2dst, __sub_kind __access_path,
540                  const __class_type_info* __dst_type, const void* __obj_ptr,
541                  const __class_type_info* __src_type, const void* __src_ptr,
542                  __dyncast_result& __result) const;
543
544     virtual __sub_kind
545     __do_find_public_src(ptrdiff_t __src2dst, const void* __obj_ptr,
546                          const __class_type_info* __src_type,
547                          const void* __src_ptr) const;
548
549     virtual bool
550     __do_upcast(const __class_type_info* __dst, const void* __obj,
551                 __upcast_result& __restrict __result) const;
552   };
553
554   // Exception handling forward declarations.
555   struct __cxa_exception;
556   struct __cxa_refcounted_exception;
557   struct __cxa_dependent_exception;
558   struct __cxa_eh_globals;
559
560   extern "C"
561   {
562   // Dynamic cast runtime.
563
564   // src2dst has the following possible values
565   //  >-1: src_type is a unique public non-virtual base of dst_type
566   //       dst_ptr + src2dst == src_ptr
567   //   -1: unspecified relationship
568   //   -2: src_type is not a public base of dst_type
569   //   -3: src_type is a multiple public non-virtual base of dst_type
570   void*
571   __dynamic_cast(const void* __src_ptr, // Starting object.
572                  const __class_type_info* __src_type, // Static type of object.
573                  const __class_type_info* __dst_type, // Desired target type.
574                  ptrdiff_t __src2dst); // How src and dst are related.
575
576
577   // Exception handling runtime.
578
579   // The __cxa_eh_globals for the current thread can be obtained by using
580   // either of the following functions.  The "fast" version assumes at least
581   // one prior call of __cxa_get_globals has been made from the current
582   // thread, so no initialization is necessary.
583   __cxa_eh_globals*
584   __cxa_get_globals() _GLIBCXX_NOTHROW __attribute__ ((__const__));
585
586   __cxa_eh_globals*
587   __cxa_get_globals_fast() _GLIBCXX_NOTHROW __attribute__ ((__const__));
588
589   // Allocate memory for the primary exception plus the thrown object.
590   void*
591   __cxa_allocate_exception(size_t) _GLIBCXX_NOTHROW;
592
593   // Free the space allocated for the primary exception.
594   void 
595   __cxa_free_exception(void*) _GLIBCXX_NOTHROW;
596
597   // Throw the exception.
598   void
599   __cxa_throw(void*, std::type_info*, void (*) (void *)) 
600   __attribute__((__noreturn__));
601
602   // Used to implement exception handlers.
603   void*
604   __cxa_get_exception_ptr(void*) _GLIBCXX_NOTHROW __attribute__ ((__pure__));
605
606   void*
607   __cxa_begin_catch(void*) _GLIBCXX_NOTHROW;
608
609   void 
610   __cxa_end_catch();
611
612   void 
613   __cxa_rethrow() __attribute__((__noreturn__));
614
615   // Returns the type_info for the currently handled exception [15.3/8], or
616   // null if there is none.
617   std::type_info*
618   __cxa_current_exception_type() _GLIBCXX_NOTHROW __attribute__ ((__pure__));
619
620   // GNU Extensions.
621
622   // Allocate memory for a dependent exception.
623   __cxa_dependent_exception*
624   __cxa_allocate_dependent_exception() _GLIBCXX_NOTHROW;
625
626   // Free the space allocated for the dependent exception.
627   void
628   __cxa_free_dependent_exception(__cxa_dependent_exception*) _GLIBCXX_NOTHROW;
629
630   } // extern "C"
631
632   // A magic placeholder class that can be caught by reference
633   // to recognize foreign exceptions.
634   class __foreign_exception
635   {
636     virtual ~__foreign_exception() throw();
637     virtual void __pure_dummy() = 0; // prevent catch by value
638   };
639
640 } // namespace __cxxabiv1
641
642 /** @namespace abi
643  *  @brief The cross-vendor C++ Application Binary Interface. A
644  *  namespace alias to __cxxabiv1, but user programs should use the
645  *  alias 'abi'.
646  *
647  *  A brief overview of an ABI is given in the libstdc++ FAQ, question
648  *  5.8 (you may have a copy of the FAQ locally, or you can view the online
649  *  version at http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#5_8 ).
650  *
651  *  GCC subscribes to a cross-vendor ABI for C++, sometimes
652  *  called the IA64 ABI because it happens to be the native ABI for that
653  *  platform.  It is summarized at http://www.codesourcery.com/cxx-abi/
654  *  along with the current specification.
655  *
656  *  For users of GCC greater than or equal to 3.x, entry points are
657  *  available in <cxxabi.h>, which notes, <em>'It is not normally
658  *  necessary for user programs to include this header, or use the
659  *  entry points directly.  However, this header is available should
660  *  that be needed.'</em>
661 */
662 namespace abi = __cxxabiv1;
663
664 namespace __gnu_cxx
665 {
666   /**
667    *  @brief Exception thrown by __cxa_guard_acquire.
668    *  @ingroup exceptions
669    *
670    *  6.7[stmt.dcl]/4: If control re-enters the declaration (recursively)
671    *  while the object is being initialized, the behavior is undefined.
672    *
673    *  Since we already have a library function to handle locking, we might
674    *  as well check for this situation and throw an exception.
675    *  We use the second byte of the guard variable to remember that we're
676    *  in the middle of an initialization.
677    */
678   class recursive_init_error: public std::exception
679   {
680   public:
681     recursive_init_error() throw() { }
682     virtual ~recursive_init_error() throw ();
683   };
684 }
685 #endif // __cplusplus
686
687 #pragma GCC visibility pop
688
689 #endif // __CXXABI_H