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