2974
|
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 "str-vec.h" |
|
32 |
|
33 #include <defaults.h> |
|
34 #include "defun.h" |
|
35 #include "error.h" |
|
36 #include "input.h" |
|
37 #include "oct-obj.h" |
|
38 #include "ov-usr-fcn.h" |
|
39 #include "ov.h" |
|
40 #include "pager.h" |
2985
|
41 #include "pt-jump.h" |
2974
|
42 #include "pt-misc.h" |
|
43 #include "pt-pr-code.h" |
2982
|
44 #include "pt-stmt.h" |
2974
|
45 #include "pt-walk.h" |
|
46 #include "symtab.h" |
|
47 #include "toplev.h" |
|
48 #include "unwind-prot.h" |
|
49 #include "utils.h" |
|
50 #include "variables.h" |
|
51 |
|
52 // If TRUE, variables returned from functions have default values even |
|
53 // if they are not explicitly initialized. |
|
54 static bool Vdefine_all_return_values; |
|
55 |
|
56 // If TRUE, the last computed value is returned from functions that |
|
57 // don't actually define any return variables. |
|
58 static bool Vreturn_last_computed_value; |
|
59 |
|
60 // User defined functions. |
|
61 |
|
62 octave_allocator |
|
63 octave_user_function::allocator (sizeof (octave_user_function)); |
|
64 |
|
65 int |
|
66 octave_user_function::t_id (-1); |
|
67 |
|
68 const string |
|
69 octave_user_function::t_name ("user-defined function"); |
|
70 |
|
71 // Ugh. This really needs to be simplified (code/data? |
|
72 // extrinsic/intrinsic state?). |
|
73 |
|
74 octave_user_function::octave_user_function |
|
75 (tree_parameter_list *pl, tree_parameter_list *rl, |
|
76 tree_statement_list *cl, symbol_table *st) |
|
77 : octave_function (string (), string ()), |
|
78 param_list (pl), ret_list (rl), cmd_list (cl), |
|
79 sym_tab (st), file_name (), fcn_name (), t_parsed (0), |
|
80 system_fcn_file (false), call_depth (0), num_named_args (0), |
|
81 args_passed (), num_args_passed (0), curr_va_arg_number (0), |
|
82 vr_list (0), symtab_entry (0), argn_sr (0), nargin_sr (0), |
|
83 nargout_sr (0) |
|
84 { |
|
85 install_automatic_vars (); |
|
86 |
|
87 if (param_list) |
|
88 { |
|
89 num_named_args = param_list->length (); |
|
90 curr_va_arg_number = num_named_args; |
|
91 } |
|
92 } |
|
93 |
|
94 octave_user_function::~octave_user_function (void) |
|
95 { |
|
96 delete param_list; |
|
97 delete ret_list; |
|
98 delete sym_tab; |
|
99 delete cmd_list; |
|
100 delete vr_list; |
|
101 } |
|
102 |
|
103 octave_user_function * |
|
104 octave_user_function::define_ret_list (tree_parameter_list *t) |
|
105 { |
|
106 ret_list = t; |
|
107 |
|
108 if (ret_list && ret_list->takes_varargs ()) |
|
109 vr_list = new tree_va_return_list; |
|
110 |
|
111 return this; |
|
112 } |
|
113 |
|
114 void |
|
115 octave_user_function::stash_fcn_file_name (void) |
|
116 { |
|
117 if (fcn_name.empty ()) |
|
118 file_name = ""; |
|
119 else |
|
120 file_name = fcn_file_in_path (fcn_name); |
|
121 } |
|
122 |
|
123 void |
|
124 octave_user_function::mark_as_system_fcn_file (void) |
|
125 { |
|
126 if (! file_name.empty ()) |
|
127 { |
|
128 // We really should stash the whole path to the file we found, |
|
129 // when we looked it up, to avoid possible race conditions... |
|
130 // XXX FIXME XXX |
|
131 // |
|
132 // We probably also don't need to get the library directory |
|
133 // every time, but since this function is only called when the |
|
134 // function file is parsed, it probably doesn't matter that |
|
135 // much. |
|
136 |
|
137 string ff_name = fcn_file_in_path (file_name); |
|
138 |
|
139 if (Vfcn_file_dir.compare (ff_name, 0, Vfcn_file_dir.length ()) == 0) |
|
140 system_fcn_file = 1; |
|
141 } |
|
142 else |
|
143 system_fcn_file = 0; |
|
144 } |
|
145 |
|
146 bool |
|
147 octave_user_function::takes_varargs (void) const |
|
148 { |
|
149 return (param_list && param_list->takes_varargs ()); |
|
150 } |
|
151 |
|
152 octave_value |
|
153 octave_user_function::octave_va_arg (void) |
|
154 { |
|
155 octave_value retval; |
|
156 |
|
157 if (curr_va_arg_number < num_args_passed) |
|
158 retval = args_passed (curr_va_arg_number++); |
|
159 else |
|
160 ::error ("va_arg: error getting arg number %d -- only %d provided", |
|
161 curr_va_arg_number + 1, num_args_passed); |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 octave_value_list |
|
167 octave_user_function::octave_all_va_args (void) |
|
168 { |
|
169 octave_value_list retval; |
|
170 |
|
171 retval.resize (num_args_passed - num_named_args); |
|
172 |
|
173 int k = 0; |
|
174 for (int i = num_named_args; i < num_args_passed; i++) |
|
175 retval(k++) = args_passed(i); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 bool |
|
181 octave_user_function::takes_var_return (void) const |
|
182 { |
|
183 return (ret_list && ret_list->takes_varargs ()); |
|
184 } |
|
185 |
|
186 void |
|
187 octave_user_function::octave_vr_val (const octave_value& val) |
|
188 { |
|
189 assert (vr_list); |
|
190 |
|
191 vr_list->append (val); |
|
192 } |
|
193 |
|
194 void |
|
195 octave_user_function::stash_function_name (const string& s) |
|
196 { |
|
197 fcn_name = s; |
|
198 } |
|
199 |
|
200 // For unwind protect. |
|
201 |
|
202 static void |
|
203 pop_symbol_table_context (void *table) |
|
204 { |
|
205 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
206 tmp->pop_context (); |
|
207 } |
|
208 |
|
209 static void |
|
210 delete_vr_list (void *list) |
|
211 { |
|
212 tree_va_return_list *tmp = static_cast<tree_va_return_list *> (list); |
|
213 tmp->clear (); |
|
214 delete tmp; |
|
215 } |
|
216 |
|
217 static void |
|
218 clear_symbol_table (void *table) |
|
219 { |
|
220 symbol_table *tmp = static_cast<symbol_table *> (table); |
|
221 tmp->clear (); |
|
222 } |
|
223 |
|
224 static void |
|
225 unprotect_function (void *sr_arg) |
|
226 { |
|
227 symbol_record *sr = static_cast<symbol_record *> (sr_arg); |
|
228 sr->unprotect (); |
|
229 } |
|
230 |
|
231 octave_value_list |
|
232 octave_user_function::do_index_op (int nargout, const octave_value_list& args) |
|
233 { |
|
234 octave_value_list retval; |
|
235 |
|
236 if (error_state) |
|
237 return retval; |
|
238 |
|
239 if (! cmd_list) |
|
240 return retval; |
|
241 |
|
242 int nargin = args.length (); |
|
243 |
2985
|
244 unwind_protect::begin_frame ("func_eval"); |
2974
|
245 |
|
246 unwind_protect_int (call_depth); |
|
247 call_depth++; |
|
248 |
|
249 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
250 { |
|
251 symtab_entry->protect (); |
2985
|
252 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
253 } |
|
254 |
|
255 if (call_depth > 1) |
|
256 { |
|
257 sym_tab->push_context (); |
2985
|
258 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
259 |
|
260 if (vr_list) |
|
261 { |
|
262 // Push new vr_list. |
|
263 |
|
264 unwind_protect_ptr (vr_list); |
|
265 vr_list = new tree_va_return_list; |
|
266 |
|
267 // Clear and delete the new one before restoring the old |
|
268 // one. |
|
269 |
2985
|
270 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
271 } |
|
272 } |
|
273 |
|
274 if (vr_list) |
|
275 vr_list->clear (); |
|
276 |
|
277 // Force symbols to be undefined again when this function exits. |
|
278 |
2985
|
279 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
280 |
|
281 // Save old and set current symbol table context, for |
|
282 // eval_undefined_error(). |
|
283 |
|
284 unwind_protect_ptr (curr_sym_tab); |
|
285 curr_sym_tab = sym_tab; |
|
286 |
|
287 unwind_protect_ptr (curr_function); |
|
288 curr_function = this; |
|
289 |
|
290 // XXX FIXME XXX -- ??? |
|
291 // unwind_protect_ptr (args_passed); |
|
292 |
|
293 args_passed = args; |
|
294 |
|
295 string_vector arg_names = args.name_tags (); |
|
296 |
|
297 unwind_protect_int (num_args_passed); |
|
298 num_args_passed = nargin; |
|
299 |
|
300 unwind_protect_int (num_named_args); |
|
301 unwind_protect_int (curr_va_arg_number); |
|
302 |
|
303 if (param_list && ! param_list->varargs_only ()) |
|
304 { |
|
305 param_list->define_from_arg_vector (args); |
|
306 if (error_state) |
|
307 goto abort; |
|
308 } |
|
309 |
|
310 if (ret_list && Vdefine_all_return_values) |
|
311 { |
|
312 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
313 |
|
314 if (tmp.is_defined ()) |
|
315 ret_list->initialize_undefined_elements (tmp); |
|
316 } |
|
317 |
|
318 // The following code is in a separate scope to avoid warnings from |
|
319 // G++ about `goto abort' crossing the initialization of some |
|
320 // variables. |
|
321 |
|
322 { |
|
323 bind_automatic_vars (arg_names, nargin, nargout); |
|
324 |
|
325 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
326 |
|
327 if (echo_commands) |
|
328 print_code_function_header (); |
|
329 |
|
330 // Evaluate the commands that make up the function. |
|
331 |
|
332 octave_value_list tmp = cmd_list->eval (); |
|
333 |
|
334 octave_value last_computed_value; |
|
335 |
|
336 if (! tmp.empty ()) |
|
337 last_computed_value = tmp(0); |
|
338 |
|
339 if (echo_commands) |
|
340 print_code_function_trailer (); |
|
341 |
2985
|
342 if (tree_return_command::returning) |
|
343 tree_return_command::returning = 0; |
2974
|
344 |
2985
|
345 if (tree_break_command::breaking) |
|
346 tree_break_command::breaking--; |
2974
|
347 |
|
348 if (error_state) |
|
349 { |
|
350 traceback_error (); |
|
351 goto abort; |
|
352 } |
|
353 |
|
354 // Copy return values out. |
|
355 |
|
356 if (ret_list) |
|
357 retval = ret_list->convert_to_const_vector (vr_list); |
|
358 else if (Vreturn_last_computed_value) |
|
359 retval(0) = last_computed_value; |
|
360 } |
|
361 |
|
362 abort: |
2985
|
363 unwind_protect::run_frame ("func_eval"); |
2974
|
364 |
|
365 return retval; |
|
366 } |
|
367 |
|
368 void |
|
369 octave_user_function::traceback_error (void) |
|
370 { |
|
371 if (error_state >= 0) |
|
372 error_state = -1; |
|
373 |
|
374 if (fcn_name.empty ()) |
|
375 { |
|
376 if (file_name.empty ()) |
|
377 ::error ("called from `?unknown?'"); |
|
378 else |
|
379 ::error ("called from file `%s'", file_name.c_str ()); |
|
380 } |
|
381 else |
|
382 { |
|
383 if (file_name.empty ()) |
|
384 ::error ("called from `%s'", fcn_name.c_str ()); |
|
385 else |
|
386 ::error ("called from `%s' in file `%s'", |
|
387 fcn_name.c_str (), file_name.c_str ()); |
|
388 } |
|
389 } |
|
390 |
|
391 void |
|
392 octave_user_function::accept (tree_walker& tw) |
|
393 { |
|
394 tw.visit_octave_user_function (*this); |
|
395 } |
|
396 |
|
397 void |
|
398 octave_user_function::print_code_function_header (void) |
|
399 { |
|
400 tree_print_code tpc (octave_stdout, Vps4); |
|
401 |
|
402 tpc.visit_octave_user_function_header (*this); |
|
403 } |
|
404 |
|
405 void |
|
406 octave_user_function::print_code_function_trailer (void) |
|
407 { |
|
408 tree_print_code tpc (octave_stdout, Vps4); |
|
409 |
|
410 tpc.visit_octave_user_function_trailer (*this); |
|
411 } |
|
412 |
|
413 void |
|
414 octave_user_function::install_automatic_vars (void) |
|
415 { |
|
416 argn_sr = sym_tab->lookup ("argn", true); |
|
417 nargin_sr = sym_tab->lookup ("nargin", true); |
|
418 nargout_sr = sym_tab->lookup ("nargout", true); |
|
419 } |
|
420 |
|
421 void |
|
422 octave_user_function::bind_automatic_vars |
|
423 (const string_vector& arg_names, int nargin, int nargout) |
|
424 { |
|
425 if (! arg_names.empty ()) |
|
426 argn_sr->define (arg_names); |
|
427 |
|
428 nargin_sr->define (static_cast<double> (nargin)); |
|
429 nargout_sr->define (static_cast<double> (nargout)); |
|
430 } |
|
431 |
|
432 DEFUN (va_arg, args, , |
|
433 "va_arg (): return next argument in a function that takes a\n\ |
|
434 variable number of parameters") |
|
435 { |
|
436 octave_value_list retval; |
|
437 |
|
438 int nargin = args.length (); |
|
439 |
|
440 if (nargin == 0) |
|
441 { |
|
442 if (curr_function) |
|
443 { |
|
444 if (curr_function->takes_varargs ()) |
|
445 retval = curr_function->octave_va_arg (); |
|
446 else |
|
447 { |
|
448 ::error ("va_arg only valid within function taking variable"); |
|
449 ::error ("number of arguments"); |
|
450 } |
|
451 } |
|
452 else |
|
453 ::error ("va_arg only valid within function body"); |
|
454 } |
|
455 else |
|
456 print_usage ("va_arg"); |
|
457 |
|
458 return retval; |
|
459 } |
|
460 |
|
461 DEFUN (va_start, args, , |
|
462 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
463 to the beginning") |
|
464 { |
|
465 octave_value_list retval; |
|
466 |
|
467 int nargin = args.length (); |
|
468 |
|
469 if (nargin == 0) |
|
470 { |
|
471 if (curr_function) |
|
472 { |
|
473 if (curr_function->takes_varargs ()) |
|
474 curr_function->octave_va_start (); |
|
475 else |
|
476 { |
|
477 ::error ("va_start only valid within function taking variable"); |
|
478 ::error ("number of arguments"); |
|
479 } |
|
480 } |
|
481 else |
|
482 ::error ("va_start only valid within function body"); |
|
483 } |
|
484 else |
|
485 print_usage ("va_start"); |
|
486 |
|
487 return retval; |
|
488 } |
|
489 |
|
490 DEFUN (vr_val, args, , |
|
491 "vr_val (X): append X to the list of optional return values for a\n\ |
|
492 function that allows a variable number of return values") |
|
493 { |
|
494 octave_value_list retval; |
|
495 |
|
496 int nargin = args.length (); |
|
497 |
|
498 if (nargin == 1) |
|
499 { |
|
500 if (curr_function) |
|
501 { |
|
502 if (curr_function->takes_var_return ()) |
|
503 curr_function->octave_vr_val (args(0)); |
|
504 else |
|
505 { |
|
506 ::error ("vr_val only valid within function declared to"); |
|
507 ::error ("produce a variable number of values"); |
|
508 } |
|
509 } |
|
510 else |
|
511 ::error ("vr_val only valid within function body"); |
|
512 } |
|
513 else |
|
514 print_usage ("vr_val"); |
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
|
519 static int |
|
520 define_all_return_values (void) |
|
521 { |
|
522 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
523 |
|
524 return 0; |
|
525 } |
|
526 |
|
527 static int |
|
528 return_last_computed_value (void) |
|
529 { |
|
530 Vreturn_last_computed_value |
|
531 = check_preference ("return_last_computed_value"); |
|
532 |
|
533 return 0; |
|
534 } |
|
535 |
|
536 void |
|
537 symbols_of_ov_usr_fcn (void) |
|
538 { |
|
539 DEFVAR (default_return_value, Matrix (), 0, 0, |
|
540 "the default for value for unitialized variables returned from\n\ |
|
541 functions. Only used if the variable initialize_return_values is\n\ |
|
542 set to \"true\"."); |
|
543 |
|
544 DEFVAR (define_all_return_values, 0.0, 0, define_all_return_values, |
|
545 "control whether values returned from functions should have a\n\ |
|
546 value even if one has not been explicitly assigned. See also\n\ |
|
547 default_return_value"); |
|
548 |
|
549 DEFVAR (return_last_computed_value, 0.0, 0, return_last_computed_value, |
|
550 "if a function does not return any values explicitly, return the\n\ |
|
551 last computed value"); |
|
552 } |
|
553 |
|
554 /* |
|
555 ;;; Local Variables: *** |
|
556 ;;; mode: C++ *** |
|
557 ;;; End: *** |
|
558 */ |