Mercurial > hg > octave-nkf
annotate src/pt-except.cc @ 8184:bb3bdcdaa063
oct-type-conv.h (octave_type_conv_body): avoid shadow warning from GCC
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Oct 2008 14:12:09 -0400 |
parents | 71f068b22fcc |
children | 73c4516fae10 |
rev | line source |
---|---|
2982 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, |
7353 | 4 2006, 2007, 2008 John W. Eaton |
2982 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2982 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2982 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
4429 | 28 #include "quit.h" |
29 | |
2982 | 30 #include "error.h" |
31 #include "oct-lvalue.h" | |
32 #include "ov.h" | |
3770 | 33 #include "pt-bp.h" |
2982 | 34 #include "pt-cmd.h" |
35 #include "pt-except.h" | |
36 #include "pt-exp.h" | |
2985 | 37 #include "pt-jump.h" |
2982 | 38 #include "pt-stmt.h" |
39 #include "pt-walk.h" | |
40 #include "unwind-prot.h" | |
41 #include "variables.h" | |
42 | |
43 // Simple exception handling. | |
44 | |
45 tree_try_catch_command::~tree_try_catch_command (void) | |
46 { | |
47 delete try_code; | |
48 delete catch_code; | |
3665 | 49 delete lead_comm; |
50 delete mid_comm; | |
51 delete trail_comm; | |
2982 | 52 } |
53 | |
54 static void | |
55 do_catch_code (void *ptr) | |
56 { | |
4793 | 57 // Is it safe to call OCTAVE_QUIT here? We are already running |
58 // something on the unwind_protect stack, but the element for this | |
59 // action would have already been popped from the top of the stack, | |
60 // so we should not be attempting to run it again. | |
61 | |
62 OCTAVE_QUIT; | |
63 | |
64 // If we are interrupting immediately, or if an interrupt is in | |
65 // progress (octave_interrupt_state < 0), then we don't want to run | |
66 // the catch code (it should only run on errors, not interrupts). | |
67 | |
68 // If octave_interrupt_state is positive, an interrupt is pending. | |
69 // The only way that could happen would be for the interrupt to | |
70 // come in after the OCTAVE_QUIT above and before the if statement | |
71 // below -- it's possible, but unlikely. In any case, we should | |
72 // probably let the catch code throw the exception because we don't | |
73 // want to skip that and potentially run some other code. For | |
74 // example, an error may have originally brought us here for some | |
75 // cleanup operation and we shouldn't skip that. | |
76 | |
77 if (octave_interrupt_immediately || octave_interrupt_state < 0) | |
4429 | 78 return; |
79 | |
2982 | 80 tree_statement_list *list = static_cast<tree_statement_list *> (ptr); |
81 | |
82 // Set up for letting the user print any messages from errors that | |
83 // occurred in the body of the try_catch statement. | |
84 | |
4699 | 85 buffer_error_messages--; |
2982 | 86 |
87 if (list) | |
88 list->eval (); | |
89 } | |
90 | |
91 void | |
92 tree_try_catch_command::eval (void) | |
93 { | |
2985 | 94 unwind_protect::begin_frame ("tree_try_catch::eval"); |
3770 | 95 |
96 MAYBE_DO_BREAKPOINT; | |
2982 | 97 |
5344 | 98 unwind_protect_int (buffer_error_messages); |
7353 | 99 unwind_protect_bool (Vdebug_on_error); |
100 unwind_protect_bool (Vdebug_on_warning); | |
101 | |
5344 | 102 buffer_error_messages++; |
7353 | 103 Vdebug_on_error = false; |
104 Vdebug_on_warning = false; | |
2982 | 105 |
3490 | 106 unwind_protect::add (do_catch_code, catch_code); |
107 | |
2982 | 108 if (try_code) |
109 try_code->eval (); | |
110 | |
111 if (catch_code && error_state) | |
112 { | |
113 error_state = 0; | |
2985 | 114 unwind_protect::run_frame ("tree_try_catch::eval"); |
2982 | 115 } |
116 else | |
117 { | |
118 error_state = 0; | |
3490 | 119 |
4699 | 120 // Unwind stack elements must be cleared or run in the reverse |
121 // order in which they were added to the stack. | |
122 | |
3490 | 123 // For clearing the do_catch_code cleanup function. |
124 unwind_protect::discard (); | |
125 | |
7353 | 126 // For restoring Vdebug_on_warning, Vdebug_on_error, and |
127 // buffer_error_messages. | |
128 unwind_protect::run (); | |
129 unwind_protect::run (); | |
5344 | 130 unwind_protect::run (); |
3585 | 131 |
132 // Also clear the frame marker. | |
133 unwind_protect::discard (); | |
2982 | 134 } |
135 } | |
136 | |
5861 | 137 tree_command * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
138 tree_try_catch_command::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
139 symbol_table::context_id context) |
5861 | 140 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
141 return new |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
142 tree_try_catch_command (try_code ? try_code->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
143 catch_code ? catch_code->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
144 lead_comm ? lead_comm->dup () : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
145 mid_comm ? mid_comm->dup () : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
146 trail_comm ? trail_comm->dup () : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
147 line (), column ()); |
5861 | 148 } |
149 | |
2982 | 150 void |
151 tree_try_catch_command::accept (tree_walker& tw) | |
152 { | |
153 tw.visit_try_catch_command (*this); | |
154 } | |
155 | |
156 // Simple exception handling. | |
157 | |
158 tree_unwind_protect_command::~tree_unwind_protect_command (void) | |
159 { | |
160 delete unwind_protect_code; | |
161 delete cleanup_code; | |
3665 | 162 delete lead_comm; |
163 delete mid_comm; | |
164 delete trail_comm; | |
2982 | 165 } |
166 | |
167 static void | |
168 do_unwind_protect_cleanup_code (void *ptr) | |
169 { | |
170 tree_statement_list *list = static_cast<tree_statement_list *> (ptr); | |
171 | |
172 // We want to run the cleanup code without error_state being set, | |
173 // but we need to restore its value, so that any errors encountered | |
174 // in the first part of the unwind_protect are not completely | |
175 // ignored. | |
176 | |
177 unwind_protect_int (error_state); | |
178 error_state = 0; | |
179 | |
180 // Similarly, if we have seen a return or break statement, allow all | |
181 // the cleanup code to run before returning or handling the break. | |
182 // We don't have to worry about continue statements because they can | |
183 // only occur in loops. | |
184 | |
4207 | 185 unwind_protect_int (tree_return_command::returning); |
186 tree_return_command::returning = 0; | |
2982 | 187 |
4207 | 188 unwind_protect_int (tree_break_command::breaking); |
189 tree_break_command::breaking = 0; | |
2982 | 190 |
191 if (list) | |
192 list->eval (); | |
193 | |
3491 | 194 // The unwind_protects are popped off the stack in the reverse of |
195 // the order they are pushed on. | |
2982 | 196 |
5775 | 197 // FIXME -- these statements say that if we see a break or |
3491 | 198 // return statement in the cleanup block, that we want to use the |
199 // new value of the breaking or returning flag instead of restoring | |
200 // the previous value. Is that the right thing to do? I think so. | |
201 // Consider the case of | |
202 // | |
203 // function foo () | |
204 // unwind_protect | |
205 // stderr << "1: this should always be executed\n"; | |
206 // break; | |
207 // stderr << "1: this should never be executed\n"; | |
208 // unwind_protect_cleanup | |
209 // stderr << "2: this should always be executed\n"; | |
210 // return; | |
211 // stderr << "2: this should never be executed\n"; | |
212 // end_unwind_protect | |
213 // endfunction | |
214 // | |
215 // If we reset the value of the breaking flag, both the returning | |
216 // flag and the breaking flag will be set, and we shouldn't have | |
217 // both. So, use the most recent one. If there is no return or | |
218 // break in the cleanup block, the values should be reset to | |
219 // whatever they were when the cleanup block was entered. | |
2982 | 220 |
4207 | 221 if (tree_break_command::breaking || tree_return_command::returning) |
3491 | 222 { |
223 unwind_protect::discard (); | |
224 unwind_protect::discard (); | |
225 } | |
2982 | 226 else |
3491 | 227 { |
228 unwind_protect::run (); | |
229 unwind_protect::run (); | |
230 } | |
2982 | 231 |
232 // We don't want to ignore errors that occur in the cleanup code, so | |
233 // if an error is encountered there, leave error_state alone. | |
234 // Otherwise, set it back to what it was before. | |
235 | |
236 if (error_state) | |
2985 | 237 unwind_protect::discard (); |
2982 | 238 else |
2985 | 239 unwind_protect::run (); |
2982 | 240 } |
241 | |
242 void | |
243 tree_unwind_protect_command::eval (void) | |
244 { | |
2985 | 245 unwind_protect::add (do_unwind_protect_cleanup_code, cleanup_code); |
2982 | 246 |
3770 | 247 MAYBE_DO_BREAKPOINT; |
248 | |
2982 | 249 if (unwind_protect_code) |
250 unwind_protect_code->eval (); | |
251 | |
2985 | 252 unwind_protect::run (); |
2982 | 253 } |
254 | |
5861 | 255 tree_command * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
256 tree_unwind_protect_command::dup (symbol_table::scope_id scope, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
257 symbol_table::context_id context) |
5861 | 258 { |
259 return new tree_unwind_protect_command | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
260 (unwind_protect_code ? unwind_protect_code->dup (scope, context) : 0, |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7353
diff
changeset
|
261 cleanup_code ? cleanup_code->dup (scope, context) : 0, |
5861 | 262 lead_comm ? lead_comm->dup () : 0, |
263 mid_comm ? mid_comm->dup () : 0, | |
264 trail_comm ? trail_comm->dup () : 0, | |
265 line (), column ()); | |
266 } | |
267 | |
2982 | 268 void |
269 tree_unwind_protect_command::accept (tree_walker& tw) | |
270 { | |
271 tw.visit_unwind_protect_command (*this); | |
272 } | |
273 | |
274 /* | |
275 ;;; Local Variables: *** | |
276 ;;; mode: C++ *** | |
277 ;;; End: *** | |
278 */ |