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