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