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