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