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) |
3523
|
77 : octave_function (std::string (), std::string ()), |
2974
|
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 |
3523
|
139 std::string ff_name = fcn_file_in_path (file_name); |
2974
|
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 |
3523
|
202 octave_user_function::stash_function_name (const std::string& s) |
2974
|
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 |
3544
|
257 octave_user_function::do_multi_index_op (int nargout, |
|
258 const octave_value_list& args) |
2974
|
259 { |
|
260 octave_value_list retval; |
|
261 |
|
262 if (error_state) |
|
263 return retval; |
|
264 |
|
265 if (! cmd_list) |
|
266 return retval; |
|
267 |
|
268 int nargin = args.length (); |
|
269 |
2985
|
270 unwind_protect::begin_frame ("func_eval"); |
2974
|
271 |
|
272 unwind_protect_int (call_depth); |
|
273 call_depth++; |
|
274 |
3131
|
275 if (call_depth > Vmax_recursion_depth) |
|
276 { |
|
277 ::error ("max_recursion_limit exceeded"); |
3321
|
278 unwind_protect::run_frame ("func_eval"); |
3131
|
279 return retval; |
|
280 } |
|
281 |
2974
|
282 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
283 { |
|
284 symtab_entry->protect (); |
2985
|
285 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
286 } |
|
287 |
|
288 if (call_depth > 1) |
|
289 { |
|
290 sym_tab->push_context (); |
2985
|
291 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
292 |
|
293 if (vr_list) |
|
294 { |
|
295 // Push new vr_list. |
|
296 |
|
297 unwind_protect_ptr (vr_list); |
|
298 vr_list = new tree_va_return_list; |
|
299 |
|
300 // Clear and delete the new one before restoring the old |
|
301 // one. |
|
302 |
2985
|
303 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
304 } |
|
305 } |
|
306 |
|
307 if (vr_list) |
|
308 vr_list->clear (); |
|
309 |
|
310 // Force symbols to be undefined again when this function exits. |
|
311 |
2985
|
312 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
313 |
|
314 // Save old and set current symbol table context, for |
|
315 // eval_undefined_error(). |
|
316 |
|
317 unwind_protect_ptr (curr_sym_tab); |
|
318 curr_sym_tab = sym_tab; |
|
319 |
|
320 unwind_protect_ptr (curr_function); |
|
321 curr_function = this; |
|
322 |
|
323 // XXX FIXME XXX -- ??? |
|
324 // unwind_protect_ptr (args_passed); |
|
325 |
|
326 args_passed = args; |
|
327 |
3239
|
328 // Force cache of arguments to be undefined when this function exits. |
|
329 // Doing so decrements the reference counts on the values of local |
|
330 // variables that are also named function parameters. |
|
331 |
|
332 unwind_protect::add (::clear_args_passed, this); |
|
333 |
2974
|
334 string_vector arg_names = args.name_tags (); |
|
335 |
|
336 unwind_protect_int (num_args_passed); |
|
337 num_args_passed = nargin; |
|
338 |
|
339 unwind_protect_int (num_named_args); |
|
340 unwind_protect_int (curr_va_arg_number); |
|
341 |
|
342 if (param_list && ! param_list->varargs_only ()) |
|
343 { |
|
344 param_list->define_from_arg_vector (args); |
|
345 if (error_state) |
|
346 goto abort; |
|
347 } |
|
348 |
3239
|
349 // Force parameter list to be undefined when this function exits. |
|
350 // Doing so decrements the reference counts on the values of local |
|
351 // variables that are also named function parameters. |
|
352 |
|
353 unwind_protect::add (clear_param_list, param_list); |
|
354 |
2974
|
355 if (ret_list && Vdefine_all_return_values) |
|
356 { |
|
357 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
358 |
|
359 if (tmp.is_defined ()) |
|
360 ret_list->initialize_undefined_elements (tmp); |
|
361 } |
|
362 |
3239
|
363 // Force return list to be undefined when this function exits. |
|
364 // Doing so decrements the reference counts on the values of local |
|
365 // variables that are also named values returned by this function. |
|
366 |
|
367 unwind_protect::add (clear_param_list, ret_list); |
|
368 |
2974
|
369 // The following code is in a separate scope to avoid warnings from |
|
370 // G++ about `goto abort' crossing the initialization of some |
|
371 // variables. |
|
372 |
|
373 { |
|
374 bind_automatic_vars (arg_names, nargin, nargout); |
|
375 |
|
376 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
377 |
|
378 if (echo_commands) |
|
379 print_code_function_header (); |
|
380 |
|
381 // Evaluate the commands that make up the function. |
|
382 |
3489
|
383 unwind_protect_bool (evaluating_function_body); |
|
384 evaluating_function_body = true; |
|
385 |
2974
|
386 octave_value_list tmp = cmd_list->eval (); |
|
387 |
|
388 octave_value last_computed_value; |
|
389 |
|
390 if (! tmp.empty ()) |
|
391 last_computed_value = tmp(0); |
|
392 |
|
393 if (echo_commands) |
|
394 print_code_function_trailer (); |
|
395 |
2985
|
396 if (tree_return_command::returning) |
|
397 tree_return_command::returning = 0; |
2974
|
398 |
2985
|
399 if (tree_break_command::breaking) |
|
400 tree_break_command::breaking--; |
2974
|
401 |
|
402 if (error_state) |
|
403 { |
|
404 traceback_error (); |
|
405 goto abort; |
|
406 } |
|
407 |
|
408 // Copy return values out. |
|
409 |
|
410 if (ret_list) |
|
411 retval = ret_list->convert_to_const_vector (vr_list); |
|
412 else if (Vreturn_last_computed_value) |
|
413 retval(0) = last_computed_value; |
|
414 } |
|
415 |
|
416 abort: |
2985
|
417 unwind_protect::run_frame ("func_eval"); |
2974
|
418 |
|
419 return retval; |
|
420 } |
|
421 |
|
422 void |
|
423 octave_user_function::traceback_error (void) |
|
424 { |
|
425 if (error_state >= 0) |
|
426 error_state = -1; |
|
427 |
|
428 if (fcn_name.empty ()) |
|
429 { |
|
430 if (file_name.empty ()) |
|
431 ::error ("called from `?unknown?'"); |
|
432 else |
|
433 ::error ("called from file `%s'", file_name.c_str ()); |
|
434 } |
|
435 else |
|
436 { |
|
437 if (file_name.empty ()) |
|
438 ::error ("called from `%s'", fcn_name.c_str ()); |
|
439 else |
|
440 ::error ("called from `%s' in file `%s'", |
|
441 fcn_name.c_str (), file_name.c_str ()); |
|
442 } |
|
443 } |
|
444 |
|
445 void |
|
446 octave_user_function::accept (tree_walker& tw) |
|
447 { |
|
448 tw.visit_octave_user_function (*this); |
|
449 } |
|
450 |
|
451 void |
|
452 octave_user_function::print_code_function_header (void) |
|
453 { |
|
454 tree_print_code tpc (octave_stdout, Vps4); |
|
455 |
|
456 tpc.visit_octave_user_function_header (*this); |
|
457 } |
|
458 |
|
459 void |
|
460 octave_user_function::print_code_function_trailer (void) |
|
461 { |
|
462 tree_print_code tpc (octave_stdout, Vps4); |
|
463 |
|
464 tpc.visit_octave_user_function_trailer (*this); |
|
465 } |
|
466 |
|
467 void |
|
468 octave_user_function::install_automatic_vars (void) |
|
469 { |
|
470 argn_sr = sym_tab->lookup ("argn", true); |
|
471 nargin_sr = sym_tab->lookup ("nargin", true); |
|
472 nargout_sr = sym_tab->lookup ("nargout", true); |
|
473 } |
|
474 |
|
475 void |
|
476 octave_user_function::bind_automatic_vars |
|
477 (const string_vector& arg_names, int nargin, int nargout) |
|
478 { |
|
479 if (! arg_names.empty ()) |
|
480 argn_sr->define (arg_names); |
|
481 |
|
482 nargin_sr->define (static_cast<double> (nargin)); |
|
483 nargout_sr->define (static_cast<double> (nargout)); |
|
484 } |
|
485 |
|
486 DEFUN (va_arg, args, , |
3445
|
487 "-*- texinfo -*-\n\ |
|
488 @deftypefn {Built-in Function} {} va_arg ()\n\ |
|
489 Return the value of hte next available argument and move the internal\n\ |
|
490 pointer to the next argument. It is an error to call @code{va_arg}\n\ |
|
491 when ther eare no more arguments available, or in a function that\n\ |
|
492 has not been declared to take a variable number of parameters.\n\ |
|
493 @end deftypefn") |
2974
|
494 { |
|
495 octave_value_list retval; |
|
496 |
|
497 int nargin = args.length (); |
|
498 |
|
499 if (nargin == 0) |
|
500 { |
|
501 if (curr_function) |
|
502 { |
|
503 if (curr_function->takes_varargs ()) |
|
504 retval = curr_function->octave_va_arg (); |
|
505 else |
|
506 { |
|
507 ::error ("va_arg only valid within function taking variable"); |
|
508 ::error ("number of arguments"); |
|
509 } |
|
510 } |
|
511 else |
|
512 ::error ("va_arg only valid within function body"); |
|
513 } |
|
514 else |
|
515 print_usage ("va_arg"); |
|
516 |
|
517 return retval; |
|
518 } |
|
519 |
|
520 DEFUN (va_start, args, , |
3445
|
521 "-*- texinfo -*-\n\ |
|
522 @deftypefn {Built-in Function} {} va_start ()\n\ |
|
523 Position an internal pointer to the first unnamed argument in\n\ |
|
524 functions that have been declared to accept a variable number of\n\ |
|
525 arguments. It is an error to call @code{va_start} in a function\n\ |
|
526 that has not been declared to take a variable number of parameters.\n\ |
|
527 @end deftypefn") |
2974
|
528 { |
|
529 octave_value_list retval; |
|
530 |
|
531 int nargin = args.length (); |
|
532 |
|
533 if (nargin == 0) |
|
534 { |
|
535 if (curr_function) |
|
536 { |
|
537 if (curr_function->takes_varargs ()) |
|
538 curr_function->octave_va_start (); |
|
539 else |
|
540 { |
|
541 ::error ("va_start only valid within function taking variable"); |
|
542 ::error ("number of arguments"); |
|
543 } |
|
544 } |
|
545 else |
|
546 ::error ("va_start only valid within function body"); |
|
547 } |
|
548 else |
|
549 print_usage ("va_start"); |
|
550 |
|
551 return retval; |
|
552 } |
|
553 |
|
554 DEFUN (vr_val, args, , |
3445
|
555 "-*- texinfo -*-\n\ |
|
556 @deftypefn {Built-in Function} {} vr_val (@var{x})\n\ |
|
557 Each time this function is called, it places the value of its argument\n\ |
|
558 at the end of the list of values to return from the current\n\ |
|
559 function. Once @code{vr_val} has been called, there is no way to go\n\ |
|
560 back to the beginning of the list and rewrite any of the return\n\ |
|
561 values. This function may only be called within functions that have\n\ |
|
562 been declared to return an unspecified number of output arguments.\n\ |
|
563 @end deftypefn") |
2974
|
564 { |
|
565 octave_value_list retval; |
|
566 |
|
567 int nargin = args.length (); |
|
568 |
|
569 if (nargin == 1) |
|
570 { |
|
571 if (curr_function) |
|
572 { |
|
573 if (curr_function->takes_var_return ()) |
|
574 curr_function->octave_vr_val (args(0)); |
|
575 else |
|
576 { |
|
577 ::error ("vr_val only valid within function declared to"); |
|
578 ::error ("produce a variable number of values"); |
|
579 } |
|
580 } |
|
581 else |
|
582 ::error ("vr_val only valid within function body"); |
|
583 } |
|
584 else |
|
585 print_usage ("vr_val"); |
|
586 |
|
587 return retval; |
|
588 } |
|
589 |
|
590 static int |
|
591 define_all_return_values (void) |
|
592 { |
|
593 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
594 |
|
595 return 0; |
|
596 } |
|
597 |
|
598 static int |
3131
|
599 max_recursion_depth (void) |
|
600 { |
|
601 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
602 |
|
603 return 0; |
|
604 } |
|
605 |
|
606 static int |
2974
|
607 return_last_computed_value (void) |
|
608 { |
|
609 Vreturn_last_computed_value |
|
610 = check_preference ("return_last_computed_value"); |
|
611 |
|
612 return 0; |
|
613 } |
|
614 |
|
615 void |
|
616 symbols_of_ov_usr_fcn (void) |
|
617 { |
3258
|
618 DEFVAR (default_return_value, Matrix (), 0, |
3371
|
619 "-*- texinfo -*-\n\ |
|
620 @defvr {Built-in Variable} default_return_value\n\ |
|
621 The value given to otherwise uninitialized return values if\n\ |
|
622 @code{define_all_return_values} is nonzero. The default value is\n\ |
|
623 @code{[]}.\n\ |
|
624 @end defvr"); |
2974
|
625 |
3258
|
626 DEFVAR (define_all_return_values, 0.0, define_all_return_values, |
3371
|
627 "-*- texinfo -*-\n\ |
|
628 @defvr {Built-in Variable} define_all_return_values\n\ |
|
629 If the value of @code{define_all_return_values} is nonzero, Octave\n\ |
|
630 will substitute the value specified by @code{default_return_value} for\n\ |
|
631 any return values that remain undefined when a function returns. The\n\ |
|
632 default value is 0.\n\ |
|
633 @end defvr"); |
2974
|
634 |
3258
|
635 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3371
|
636 "-*- texinfo -*-\n\ |
|
637 @defvr {Built-in Variable} max_recursion_depth\n\ |
|
638 Limit the number of times a function may be called recursively.\n\ |
|
639 If the limit is exceeded, an error message is printed and control\n\ |
|
640 returns to the top level.\n\ |
|
641 \n\ |
|
642 The default value is 256.\n\ |
|
643 @end defvr"); |
3131
|
644 |
3258
|
645 DEFVAR (return_last_computed_value, 0.0, return_last_computed_value, |
3371
|
646 "-*- texinfo -*-\n\ |
|
647 @defvr {Built-in Variable} return_last_computed_value\n\ |
|
648 If the value of @code{return_last_computed_value} is true, and a\n\ |
|
649 function is defined without explicitly specifying a return value, the\n\ |
|
650 function will return the value of the last expression. Otherwise, no\n\ |
|
651 value will be returned. The default value is 0.\n\ |
|
652 \n\ |
|
653 For example, the function\n\ |
|
654 \n\ |
|
655 @example\n\ |
|
656 function f ()\n\ |
|
657 2 + 2;\n\ |
|
658 endfunction\n\ |
|
659 @end example\n\ |
|
660 \n\ |
|
661 @noindent\n\ |
|
662 will either return nothing, if the value of\n\ |
|
663 @code{return_last_computed_value} is 0, or 4, if the value of\n\ |
|
664 @code{return_last_computed_value} is nonzero.\n\ |
|
665 @end defvr"); |
2974
|
666 } |
|
667 |
|
668 /* |
|
669 ;;; Local Variables: *** |
|
670 ;;; mode: C++ *** |
|
671 ;;; End: *** |
|
672 */ |