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