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