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 // Nonzero means we're breaking out of a loop or function body. |
|
32 extern int breaking; |
|
33 |
|
34 // Nonzero means we're jumping to the end of a loop. |
|
35 extern int continuing; |
|
36 |
|
37 // Nonzero means we're returning from a function. Global because it |
|
38 // is also needed in tree-expr.cc. |
|
39 extern int returning; |
|
40 |
|
41 #include "error.h" |
|
42 #include "oct-lvalue.h" |
|
43 #include "ov.h" |
|
44 #include "pt-cmd.h" |
|
45 #include "pt-except.h" |
|
46 #include "pt-exp.h" |
|
47 #include "pt-stmt.h" |
|
48 #include "pt-walk.h" |
|
49 #include "unwind-prot.h" |
|
50 #include "variables.h" |
|
51 |
|
52 // Simple exception handling. |
|
53 |
|
54 tree_try_catch_command::~tree_try_catch_command (void) |
|
55 { |
|
56 delete try_code; |
|
57 delete catch_code; |
|
58 } |
|
59 |
|
60 static void |
|
61 do_catch_code (void *ptr) |
|
62 { |
|
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 |
|
68 buffer_error_messages = 0; |
|
69 bind_global_error_variable (); |
|
70 add_unwind_protect (clear_global_error_variable, 0); |
|
71 |
|
72 // Similarly, if we have seen a return or break statement, allow all |
|
73 // the catch code to run before returning or handling the break. |
|
74 // We don't have to worry about continue statements because they can |
|
75 // only occur in loops. |
|
76 |
|
77 unwind_protect_int (returning); |
|
78 returning = 0; |
|
79 |
|
80 unwind_protect_int (breaking); |
|
81 breaking = 0; |
|
82 |
|
83 if (list) |
|
84 list->eval (); |
|
85 |
|
86 // This is the one for breaking. (The unwind_protects are popped |
|
87 // off the stack in the reverse of the order they are pushed on). |
|
88 |
|
89 // XXX FIXME XXX -- inside a try-catch, should break work like |
|
90 // a return, or just jump to the end of the try_catch block? |
|
91 // The following code makes it just jump to the end of the block. |
|
92 |
|
93 run_unwind_protect (); |
|
94 if (breaking) |
|
95 breaking--; |
|
96 |
|
97 // This is the one for returning. |
|
98 |
|
99 if (returning) |
|
100 discard_unwind_protect (); |
|
101 else |
|
102 run_unwind_protect (); |
|
103 |
|
104 run_unwind_protect (); |
|
105 } |
|
106 |
|
107 void |
|
108 tree_try_catch_command::eval (void) |
|
109 { |
|
110 begin_unwind_frame ("tree_try_catch::eval"); |
|
111 |
|
112 add_unwind_protect (do_catch_code, catch_code); |
|
113 |
|
114 if (catch_code) |
|
115 { |
|
116 unwind_protect_int (buffer_error_messages); |
|
117 buffer_error_messages = 1; |
|
118 } |
|
119 |
|
120 if (try_code) |
|
121 try_code->eval (); |
|
122 |
|
123 if (catch_code && error_state) |
|
124 { |
|
125 error_state = 0; |
|
126 run_unwind_frame ("tree_try_catch::eval"); |
|
127 } |
|
128 else |
|
129 { |
|
130 error_state = 0; |
|
131 discard_unwind_frame ("tree_try_catch::eval"); |
|
132 } |
|
133 } |
|
134 |
|
135 void |
|
136 tree_try_catch_command::accept (tree_walker& tw) |
|
137 { |
|
138 tw.visit_try_catch_command (*this); |
|
139 } |
|
140 |
|
141 // Simple exception handling. |
|
142 |
|
143 tree_unwind_protect_command::~tree_unwind_protect_command (void) |
|
144 { |
|
145 delete unwind_protect_code; |
|
146 delete cleanup_code; |
|
147 } |
|
148 |
|
149 static void |
|
150 do_unwind_protect_cleanup_code (void *ptr) |
|
151 { |
|
152 tree_statement_list *list = static_cast<tree_statement_list *> (ptr); |
|
153 |
|
154 // We want to run the cleanup code without error_state being set, |
|
155 // but we need to restore its value, so that any errors encountered |
|
156 // in the first part of the unwind_protect are not completely |
|
157 // ignored. |
|
158 |
|
159 unwind_protect_int (error_state); |
|
160 error_state = 0; |
|
161 |
|
162 // Similarly, if we have seen a return or break statement, allow all |
|
163 // the cleanup code to run before returning or handling the break. |
|
164 // We don't have to worry about continue statements because they can |
|
165 // only occur in loops. |
|
166 |
|
167 unwind_protect_int (returning); |
|
168 returning = 0; |
|
169 |
|
170 unwind_protect_int (breaking); |
|
171 breaking = 0; |
|
172 |
|
173 if (list) |
|
174 list->eval (); |
|
175 |
|
176 // This is the one for breaking. (The unwind_protects are popped |
|
177 // off the stack in the reverse of the order they are pushed on). |
|
178 |
|
179 // XXX FIXME XXX -- inside an unwind_protect, should break work like |
|
180 // a return, or just jump to the end of the unwind_protect block? |
|
181 // The following code makes it just jump to the end of the block. |
|
182 |
|
183 run_unwind_protect (); |
|
184 if (breaking) |
|
185 breaking--; |
|
186 |
|
187 // This is the one for returning. |
|
188 |
|
189 if (returning) |
|
190 discard_unwind_protect (); |
|
191 else |
|
192 run_unwind_protect (); |
|
193 |
|
194 // We don't want to ignore errors that occur in the cleanup code, so |
|
195 // if an error is encountered there, leave error_state alone. |
|
196 // Otherwise, set it back to what it was before. |
|
197 |
|
198 if (error_state) |
|
199 discard_unwind_protect (); |
|
200 else |
|
201 run_unwind_protect (); |
|
202 } |
|
203 |
|
204 void |
|
205 tree_unwind_protect_command::eval (void) |
|
206 { |
|
207 add_unwind_protect (do_unwind_protect_cleanup_code, cleanup_code); |
|
208 |
|
209 if (unwind_protect_code) |
|
210 unwind_protect_code->eval (); |
|
211 |
|
212 run_unwind_protect (); |
|
213 } |
|
214 |
|
215 void |
|
216 tree_unwind_protect_command::accept (tree_walker& tw) |
|
217 { |
|
218 tw.visit_unwind_protect_command (*this); |
|
219 } |
|
220 |
|
221 /* |
|
222 ;;; Local Variables: *** |
|
223 ;;; mode: C++ *** |
|
224 ;;; End: *** |
|
225 */ |