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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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); |
|
99 buffer_error_messages++; |
2982
|
100 |
3490
|
101 unwind_protect::add (do_catch_code, catch_code); |
|
102 |
2982
|
103 if (try_code) |
|
104 try_code->eval (); |
|
105 |
|
106 if (catch_code && error_state) |
|
107 { |
|
108 error_state = 0; |
2985
|
109 unwind_protect::run_frame ("tree_try_catch::eval"); |
2982
|
110 } |
|
111 else |
|
112 { |
|
113 error_state = 0; |
3490
|
114 |
4699
|
115 // Unwind stack elements must be cleared or run in the reverse |
|
116 // order in which they were added to the stack. |
|
117 |
3490
|
118 // For clearing the do_catch_code cleanup function. |
|
119 unwind_protect::discard (); |
|
120 |
|
121 // For restoring buffer_error_messages. |
5344
|
122 unwind_protect::run (); |
3585
|
123 |
|
124 // Also clear the frame marker. |
|
125 unwind_protect::discard (); |
2982
|
126 } |
|
127 } |
|
128 |
|
129 void |
|
130 tree_try_catch_command::accept (tree_walker& tw) |
|
131 { |
|
132 tw.visit_try_catch_command (*this); |
|
133 } |
|
134 |
|
135 // Simple exception handling. |
|
136 |
|
137 tree_unwind_protect_command::~tree_unwind_protect_command (void) |
|
138 { |
|
139 delete unwind_protect_code; |
|
140 delete cleanup_code; |
3665
|
141 delete lead_comm; |
|
142 delete mid_comm; |
|
143 delete trail_comm; |
2982
|
144 } |
|
145 |
|
146 static void |
|
147 do_unwind_protect_cleanup_code (void *ptr) |
|
148 { |
|
149 tree_statement_list *list = static_cast<tree_statement_list *> (ptr); |
|
150 |
|
151 // We want to run the cleanup code without error_state being set, |
|
152 // but we need to restore its value, so that any errors encountered |
|
153 // in the first part of the unwind_protect are not completely |
|
154 // ignored. |
|
155 |
|
156 unwind_protect_int (error_state); |
|
157 error_state = 0; |
|
158 |
|
159 // Similarly, if we have seen a return or break statement, allow all |
|
160 // the cleanup code to run before returning or handling the break. |
|
161 // We don't have to worry about continue statements because they can |
|
162 // only occur in loops. |
|
163 |
4207
|
164 unwind_protect_int (tree_return_command::returning); |
|
165 tree_return_command::returning = 0; |
2982
|
166 |
4207
|
167 unwind_protect_int (tree_break_command::breaking); |
|
168 tree_break_command::breaking = 0; |
2982
|
169 |
|
170 if (list) |
|
171 list->eval (); |
|
172 |
3491
|
173 // The unwind_protects are popped off the stack in the reverse of |
|
174 // the order they are pushed on. |
2982
|
175 |
3491
|
176 // XXX FIXME XXX -- these statements say that if we see a break or |
|
177 // return statement in the cleanup block, that we want to use the |
|
178 // new value of the breaking or returning flag instead of restoring |
|
179 // the previous value. Is that the right thing to do? I think so. |
|
180 // Consider the case of |
|
181 // |
|
182 // function foo () |
|
183 // unwind_protect |
|
184 // stderr << "1: this should always be executed\n"; |
|
185 // break; |
|
186 // stderr << "1: this should never be executed\n"; |
|
187 // unwind_protect_cleanup |
|
188 // stderr << "2: this should always be executed\n"; |
|
189 // return; |
|
190 // stderr << "2: this should never be executed\n"; |
|
191 // end_unwind_protect |
|
192 // endfunction |
|
193 // |
|
194 // If we reset the value of the breaking flag, both the returning |
|
195 // flag and the breaking flag will be set, and we shouldn't have |
|
196 // both. So, use the most recent one. If there is no return or |
|
197 // break in the cleanup block, the values should be reset to |
|
198 // whatever they were when the cleanup block was entered. |
2982
|
199 |
4207
|
200 if (tree_break_command::breaking || tree_return_command::returning) |
3491
|
201 { |
|
202 unwind_protect::discard (); |
|
203 unwind_protect::discard (); |
|
204 } |
2982
|
205 else |
3491
|
206 { |
|
207 unwind_protect::run (); |
|
208 unwind_protect::run (); |
|
209 } |
2982
|
210 |
|
211 // We don't want to ignore errors that occur in the cleanup code, so |
|
212 // if an error is encountered there, leave error_state alone. |
|
213 // Otherwise, set it back to what it was before. |
|
214 |
|
215 if (error_state) |
2985
|
216 unwind_protect::discard (); |
2982
|
217 else |
2985
|
218 unwind_protect::run (); |
2982
|
219 } |
|
220 |
|
221 void |
|
222 tree_unwind_protect_command::eval (void) |
|
223 { |
2985
|
224 unwind_protect::add (do_unwind_protect_cleanup_code, cleanup_code); |
2982
|
225 |
3770
|
226 MAYBE_DO_BREAKPOINT; |
|
227 |
2982
|
228 if (unwind_protect_code) |
|
229 unwind_protect_code->eval (); |
|
230 |
2985
|
231 unwind_protect::run (); |
2982
|
232 } |
|
233 |
|
234 void |
|
235 tree_unwind_protect_command::accept (tree_walker& tw) |
|
236 { |
|
237 tw.visit_unwind_protect_command (*this); |
|
238 } |
|
239 |
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; End: *** |
|
244 */ |