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"); |
|
276 return retval; |
|
277 } |
|
278 |
2974
|
279 if (symtab_entry && ! symtab_entry->is_read_only ()) |
|
280 { |
|
281 symtab_entry->protect (); |
2985
|
282 unwind_protect::add (unprotect_function, symtab_entry); |
2974
|
283 } |
|
284 |
|
285 if (call_depth > 1) |
|
286 { |
|
287 sym_tab->push_context (); |
2985
|
288 unwind_protect::add (pop_symbol_table_context, sym_tab); |
2974
|
289 |
|
290 if (vr_list) |
|
291 { |
|
292 // Push new vr_list. |
|
293 |
|
294 unwind_protect_ptr (vr_list); |
|
295 vr_list = new tree_va_return_list; |
|
296 |
|
297 // Clear and delete the new one before restoring the old |
|
298 // one. |
|
299 |
2985
|
300 unwind_protect::add (delete_vr_list, vr_list); |
2974
|
301 } |
|
302 } |
|
303 |
|
304 if (vr_list) |
|
305 vr_list->clear (); |
|
306 |
|
307 // Force symbols to be undefined again when this function exits. |
|
308 |
2985
|
309 unwind_protect::add (clear_symbol_table, sym_tab); |
2974
|
310 |
|
311 // Save old and set current symbol table context, for |
|
312 // eval_undefined_error(). |
|
313 |
|
314 unwind_protect_ptr (curr_sym_tab); |
|
315 curr_sym_tab = sym_tab; |
|
316 |
|
317 unwind_protect_ptr (curr_function); |
|
318 curr_function = this; |
|
319 |
|
320 // XXX FIXME XXX -- ??? |
|
321 // unwind_protect_ptr (args_passed); |
|
322 |
|
323 args_passed = args; |
|
324 |
3239
|
325 // Force cache of arguments to be undefined when this function exits. |
|
326 // Doing so decrements the reference counts on the values of local |
|
327 // variables that are also named function parameters. |
|
328 |
|
329 unwind_protect::add (::clear_args_passed, this); |
|
330 |
2974
|
331 string_vector arg_names = args.name_tags (); |
|
332 |
|
333 unwind_protect_int (num_args_passed); |
|
334 num_args_passed = nargin; |
|
335 |
|
336 unwind_protect_int (num_named_args); |
|
337 unwind_protect_int (curr_va_arg_number); |
|
338 |
|
339 if (param_list && ! param_list->varargs_only ()) |
|
340 { |
|
341 param_list->define_from_arg_vector (args); |
|
342 if (error_state) |
|
343 goto abort; |
|
344 } |
|
345 |
3239
|
346 // Force parameter list to be undefined when this function exits. |
|
347 // Doing so decrements the reference counts on the values of local |
|
348 // variables that are also named function parameters. |
|
349 |
|
350 unwind_protect::add (clear_param_list, param_list); |
|
351 |
2974
|
352 if (ret_list && Vdefine_all_return_values) |
|
353 { |
|
354 octave_value tmp = builtin_any_variable ("default_return_value"); |
|
355 |
|
356 if (tmp.is_defined ()) |
|
357 ret_list->initialize_undefined_elements (tmp); |
|
358 } |
|
359 |
3239
|
360 // Force return list to be undefined when this function exits. |
|
361 // Doing so decrements the reference counts on the values of local |
|
362 // variables that are also named values returned by this function. |
|
363 |
|
364 unwind_protect::add (clear_param_list, ret_list); |
|
365 |
2974
|
366 // The following code is in a separate scope to avoid warnings from |
|
367 // G++ about `goto abort' crossing the initialization of some |
|
368 // variables. |
|
369 |
|
370 { |
|
371 bind_automatic_vars (arg_names, nargin, nargout); |
|
372 |
|
373 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); |
|
374 |
|
375 if (echo_commands) |
|
376 print_code_function_header (); |
|
377 |
|
378 // Evaluate the commands that make up the function. |
|
379 |
|
380 octave_value_list tmp = cmd_list->eval (); |
|
381 |
|
382 octave_value last_computed_value; |
|
383 |
|
384 if (! tmp.empty ()) |
|
385 last_computed_value = tmp(0); |
|
386 |
|
387 if (echo_commands) |
|
388 print_code_function_trailer (); |
|
389 |
2985
|
390 if (tree_return_command::returning) |
|
391 tree_return_command::returning = 0; |
2974
|
392 |
2985
|
393 if (tree_break_command::breaking) |
|
394 tree_break_command::breaking--; |
2974
|
395 |
|
396 if (error_state) |
|
397 { |
|
398 traceback_error (); |
|
399 goto abort; |
|
400 } |
|
401 |
|
402 // Copy return values out. |
|
403 |
|
404 if (ret_list) |
|
405 retval = ret_list->convert_to_const_vector (vr_list); |
|
406 else if (Vreturn_last_computed_value) |
|
407 retval(0) = last_computed_value; |
|
408 } |
|
409 |
|
410 abort: |
2985
|
411 unwind_protect::run_frame ("func_eval"); |
2974
|
412 |
|
413 return retval; |
|
414 } |
|
415 |
|
416 void |
|
417 octave_user_function::traceback_error (void) |
|
418 { |
|
419 if (error_state >= 0) |
|
420 error_state = -1; |
|
421 |
|
422 if (fcn_name.empty ()) |
|
423 { |
|
424 if (file_name.empty ()) |
|
425 ::error ("called from `?unknown?'"); |
|
426 else |
|
427 ::error ("called from file `%s'", file_name.c_str ()); |
|
428 } |
|
429 else |
|
430 { |
|
431 if (file_name.empty ()) |
|
432 ::error ("called from `%s'", fcn_name.c_str ()); |
|
433 else |
|
434 ::error ("called from `%s' in file `%s'", |
|
435 fcn_name.c_str (), file_name.c_str ()); |
|
436 } |
|
437 } |
|
438 |
|
439 void |
|
440 octave_user_function::accept (tree_walker& tw) |
|
441 { |
|
442 tw.visit_octave_user_function (*this); |
|
443 } |
|
444 |
|
445 void |
|
446 octave_user_function::print_code_function_header (void) |
|
447 { |
|
448 tree_print_code tpc (octave_stdout, Vps4); |
|
449 |
|
450 tpc.visit_octave_user_function_header (*this); |
|
451 } |
|
452 |
|
453 void |
|
454 octave_user_function::print_code_function_trailer (void) |
|
455 { |
|
456 tree_print_code tpc (octave_stdout, Vps4); |
|
457 |
|
458 tpc.visit_octave_user_function_trailer (*this); |
|
459 } |
|
460 |
|
461 void |
|
462 octave_user_function::install_automatic_vars (void) |
|
463 { |
|
464 argn_sr = sym_tab->lookup ("argn", true); |
|
465 nargin_sr = sym_tab->lookup ("nargin", true); |
|
466 nargout_sr = sym_tab->lookup ("nargout", true); |
|
467 } |
|
468 |
|
469 void |
|
470 octave_user_function::bind_automatic_vars |
|
471 (const string_vector& arg_names, int nargin, int nargout) |
|
472 { |
|
473 if (! arg_names.empty ()) |
|
474 argn_sr->define (arg_names); |
|
475 |
|
476 nargin_sr->define (static_cast<double> (nargin)); |
|
477 nargout_sr->define (static_cast<double> (nargout)); |
|
478 } |
|
479 |
|
480 DEFUN (va_arg, args, , |
|
481 "va_arg (): return next argument in a function that takes a\n\ |
|
482 variable number of parameters") |
|
483 { |
|
484 octave_value_list retval; |
|
485 |
|
486 int nargin = args.length (); |
|
487 |
|
488 if (nargin == 0) |
|
489 { |
|
490 if (curr_function) |
|
491 { |
|
492 if (curr_function->takes_varargs ()) |
|
493 retval = curr_function->octave_va_arg (); |
|
494 else |
|
495 { |
|
496 ::error ("va_arg only valid within function taking variable"); |
|
497 ::error ("number of arguments"); |
|
498 } |
|
499 } |
|
500 else |
|
501 ::error ("va_arg only valid within function body"); |
|
502 } |
|
503 else |
|
504 print_usage ("va_arg"); |
|
505 |
|
506 return retval; |
|
507 } |
|
508 |
|
509 DEFUN (va_start, args, , |
|
510 "va_start (): reset the pointer to the list of optional arguments\n\ |
|
511 to the beginning") |
|
512 { |
|
513 octave_value_list retval; |
|
514 |
|
515 int nargin = args.length (); |
|
516 |
|
517 if (nargin == 0) |
|
518 { |
|
519 if (curr_function) |
|
520 { |
|
521 if (curr_function->takes_varargs ()) |
|
522 curr_function->octave_va_start (); |
|
523 else |
|
524 { |
|
525 ::error ("va_start only valid within function taking variable"); |
|
526 ::error ("number of arguments"); |
|
527 } |
|
528 } |
|
529 else |
|
530 ::error ("va_start only valid within function body"); |
|
531 } |
|
532 else |
|
533 print_usage ("va_start"); |
|
534 |
|
535 return retval; |
|
536 } |
|
537 |
|
538 DEFUN (vr_val, args, , |
|
539 "vr_val (X): append X to the list of optional return values for a\n\ |
|
540 function that allows a variable number of return values") |
|
541 { |
|
542 octave_value_list retval; |
|
543 |
|
544 int nargin = args.length (); |
|
545 |
|
546 if (nargin == 1) |
|
547 { |
|
548 if (curr_function) |
|
549 { |
|
550 if (curr_function->takes_var_return ()) |
|
551 curr_function->octave_vr_val (args(0)); |
|
552 else |
|
553 { |
|
554 ::error ("vr_val only valid within function declared to"); |
|
555 ::error ("produce a variable number of values"); |
|
556 } |
|
557 } |
|
558 else |
|
559 ::error ("vr_val only valid within function body"); |
|
560 } |
|
561 else |
|
562 print_usage ("vr_val"); |
|
563 |
|
564 return retval; |
|
565 } |
|
566 |
|
567 static int |
|
568 define_all_return_values (void) |
|
569 { |
|
570 Vdefine_all_return_values = check_preference ("define_all_return_values"); |
|
571 |
|
572 return 0; |
|
573 } |
|
574 |
|
575 static int |
3131
|
576 max_recursion_depth (void) |
|
577 { |
|
578 Vmax_recursion_depth = check_preference ("max_recursion_depth"); |
|
579 |
|
580 return 0; |
|
581 } |
|
582 |
|
583 static int |
2974
|
584 return_last_computed_value (void) |
|
585 { |
|
586 Vreturn_last_computed_value |
|
587 = check_preference ("return_last_computed_value"); |
|
588 |
|
589 return 0; |
|
590 } |
|
591 |
|
592 void |
|
593 symbols_of_ov_usr_fcn (void) |
|
594 { |
3258
|
595 DEFVAR (default_return_value, Matrix (), 0, |
2974
|
596 "the default for value for unitialized variables returned from\n\ |
|
597 functions. Only used if the variable initialize_return_values is\n\ |
3156
|
598 nonzero."); |
2974
|
599 |
3258
|
600 DEFVAR (define_all_return_values, 0.0, define_all_return_values, |
2974
|
601 "control whether values returned from functions should have a\n\ |
|
602 value even if one has not been explicitly assigned. See also\n\ |
|
603 default_return_value"); |
|
604 |
3258
|
605 DEFVAR (max_recursion_depth, 256.0, max_recursion_depth, |
3131
|
606 "maximum nesting level for functions called recursively.\n\ |
|
607 The default value is 256."); |
|
608 |
3258
|
609 DEFVAR (return_last_computed_value, 0.0, return_last_computed_value, |
2974
|
610 "if a function does not return any values explicitly, return the\n\ |
|
611 last computed value"); |
|
612 } |
|
613 |
|
614 /* |
|
615 ;;; Local Variables: *** |
|
616 ;;; mode: C++ *** |
|
617 ;;; End: *** |
|
618 */ |