Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cp / exception.cc
blob088b1e24e3b4cc30f519b2821341de60eef246a5
1 // Functions for Exception Support for -*- C++ -*-
2 // Copyright (C) 1994, 1995, 1996, 1998 Free Software Foundation
4 // This file is part of GNU CC.
6 // GNU CC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // GNU CC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GNU CC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 // As a special exception, if you link this library with other files,
22 // some of which are compiled with GCC, to produce an executable,
23 // this library does not by itself cause the resulting executable
24 // to be covered by the GNU General Public License.
25 // This exception does not however invalidate any other reasons why
26 // the executable file might be covered by the GNU General Public License.
28 #pragma implementation "exception"
30 #include "typeinfo"
31 #include "exception"
33 /* Define terminate, unexpected, set_terminate, set_unexpected as
34 well as the default terminate func and default unexpected func. */
36 extern terminate_handler __terminate_func __attribute__((__noreturn__));
38 void
39 terminate ()
41 __terminate_func ();
44 void
45 __default_unexpected ()
47 terminate ();
50 static unexpected_handler __unexpected_func = __default_unexpected;
52 terminate_handler
53 set_terminate (terminate_handler func)
55 terminate_handler old = __terminate_func;
57 __terminate_func = func;
58 return old;
61 unexpected_handler
62 set_unexpected (unexpected_handler func)
64 unexpected_handler old = __unexpected_func;
66 __unexpected_func = func;
67 return old;
70 void
71 unexpected ()
73 __unexpected_func ();
76 /* C++-specific state about the current exception.
77 This must match init_exception_processing().
79 Note that handlers and caught are not redundant; when rethrown, an
80 exception can have multiple active handlers and still be considered
81 uncaught. */
83 struct cp_eh_info
85 void *value;
86 void *type;
87 void (*cleanup)(void *, int);
88 bool caught;
89 cp_eh_info *next;
90 long handlers;
93 /* Language-specific EH info pointer, defined in libgcc2. */
95 extern cp_eh_info *__eh_info; // actually void*
97 /* Is P the type_info node for a pointer of some kind? */
99 extern bool __is_pointer (void *);
101 /* Compiler hook to return a pointer to the info for the current exception.
102 Used by get_eh_info (). */
104 extern "C" cp_eh_info *
105 __cp_exception_info (void)
107 return __eh_info;
110 /* Compiler hook to push a new exception onto the stack.
111 Used by expand_throw(). */
113 extern "C" void
114 __cp_push_exception (void *value, void *type, void (*cleanup)(void *, int))
116 cp_eh_info *p = new cp_eh_info;
117 p->value = value;
118 p->type = type;
119 p->cleanup = cleanup;
120 p->handlers = 0;
121 p->caught = false;
122 p->next = __eh_info;
123 __eh_info = p;
126 /* Compiler hook to pop an exception that has been finalized. Used by
127 push_eh_cleanup(). P is the info for the exception caught by the
128 current catch block. */
130 extern "C" void
131 __cp_pop_exception (cp_eh_info *p)
133 cp_eh_info **q = &__eh_info;
135 --p->handlers;
137 /* Don't really pop if there are still active handlers for our exception,
138 or if our exception is being rethrown (i.e. if the active exception is
139 our exception and it is uncaught). */
140 if (p->handlers != 0
141 || (p == *q && !p->caught))
142 return;
144 for (; *q; q = &((*q)->next))
145 if (*q == p)
146 break;
148 if (! *q)
149 terminate ();
151 *q = p->next;
153 if (p->cleanup)
154 /* 3 is a magic value for destructors; see build_delete(). */
155 p->cleanup (p->value, 3);
156 else if (__is_pointer (p->type))
157 /* do nothing; pointers are passed directly in p->value. */;
158 else
159 delete p->value;
161 delete p;
164 extern "C" void
165 __uncatch_exception (void)
167 cp_eh_info *p = __cp_exception_info ();
168 if (p)
169 p->caught = false;
170 /* otherwise __throw will call terminate(); don't crash here. */
173 extern "C" void
174 __throw_bad_cast (void)
176 throw bad_cast ();
179 extern "C" void
180 __throw_bad_typeid (void)
182 throw bad_typeid ();
185 /* Has the current exception been caught? */
187 bool
188 uncaught_exception ()
190 cp_eh_info *p = __cp_exception_info ();
191 return p && ! p->caught;
194 const char * exception::
195 what () const
197 return typeid (*this).name ();