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