Mercurial > hg > octave-lyh
annotate src/ov-usr-fcn.cc @ 9794:0d4613a736e9
convert build system to use automake and libtool
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 10 Nov 2009 15:02:25 -0500 |
parents | b6f5a59a66d7 |
children | 6918d86add73 |
rev | line source |
---|---|
2974 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
8920 | 4 2006, 2007, 2008, 2009 John W. Eaton |
2974 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2974 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2974 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "str-vec.h" | |
29 | |
30 #include <defaults.h> | |
3974 | 31 #include "Cell.h" |
2974 | 32 #include "defun.h" |
33 #include "error.h" | |
34 #include "input.h" | |
35 #include "oct-obj.h" | |
36 #include "ov-usr-fcn.h" | |
37 #include "ov.h" | |
38 #include "pager.h" | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
39 #include "pt-eval.h" |
2985 | 40 #include "pt-jump.h" |
2974 | 41 #include "pt-misc.h" |
42 #include "pt-pr-code.h" | |
2982 | 43 #include "pt-stmt.h" |
2974 | 44 #include "pt-walk.h" |
45 #include "symtab.h" | |
46 #include "toplev.h" | |
47 #include "unwind-prot.h" | |
48 #include "utils.h" | |
3489 | 49 #include "parse.h" |
2974 | 50 #include "variables.h" |
51 | |
3131 | 52 // Maximum nesting level for functions called recursively. |
5794 | 53 static int Vmax_recursion_depth = 256; |
3131 | 54 |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
55 // Whether to optimize subsasgn method calls. |
9529
8e5009334661
partially revert e79470be3ecb
Jaroslav Hajek <highegg@gmail.com>
parents:
9522
diff
changeset
|
56 static bool Voptimize_subsasgn_calls = true; |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
57 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
58 // User defined scripts. |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
59 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
60 DEFINE_OCTAVE_ALLOCATOR (octave_user_script); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
61 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
62 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_script, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
63 "user-defined script", |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
64 "user-defined script"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
65 |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
66 octave_user_script::octave_user_script (void) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
67 : octave_user_code (), cmd_list (0), file_name (), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
68 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
69 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
70 call_depth (-1) |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
71 { } |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
72 |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
73 octave_user_script::octave_user_script (const std::string& fnm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
74 const std::string& nm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
75 tree_statement_list *cmds, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
76 const std::string& ds) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
77 : octave_user_code (nm, ds), cmd_list (cmds), file_name (fnm), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
78 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
79 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
80 call_depth (-1) |
7736 | 81 { |
82 if (cmd_list) | |
83 cmd_list->mark_as_script_body (); | |
84 } | |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
85 |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
86 octave_user_script::octave_user_script (const std::string& fnm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
87 const std::string& nm, |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
88 const std::string& ds) |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
89 : octave_user_code (nm, ds), cmd_list (0), file_name (fnm), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
90 t_parsed (static_cast<time_t> (0)), |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
91 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
92 call_depth (-1) |
7731
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
93 { } |
2d2a969c731c
fix max_recursion_limit exceeded at startup with Intel C++
Jaroslav Hajek <highegg@gmail.com>
parents:
7719
diff
changeset
|
94 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
95 octave_user_script::~octave_user_script (void) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
96 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
97 delete cmd_list; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
98 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
99 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
100 octave_value_list |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
101 octave_user_script::subsref (const std::string&, |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
102 const std::list<octave_value_list>&, int) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
103 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
104 octave_value_list retval; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
105 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
106 ::error ("invalid use of script in index expression"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
107 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
108 return retval; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
109 } |
7336 | 110 |
111 octave_value_list | |
112 octave_user_script::do_multi_index_op (int nargout, | |
113 const octave_value_list& args) | |
114 { | |
115 octave_value_list retval; | |
116 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
117 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
118 |
7336 | 119 if (! error_state) |
120 { | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
121 if (args.length () == 0 && nargout == 0) |
7336 | 122 { |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
123 if (cmd_list) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
124 { |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
125 unwind_protect::protect_var (call_depth); |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
126 call_depth++; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
127 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
128 if (call_depth < Vmax_recursion_depth) |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
129 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
130 octave_call_stack::push (this); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
131 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
132 unwind_protect::add_fcn (octave_call_stack::pop); |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
133 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
134 unwind_protect::protect_var (tree_evaluator::in_fcn_or_script_body); |
8669
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
135 tree_evaluator::in_fcn_or_script_body = true; |
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
136 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
137 cmd_list->accept (*current_evaluator); |
7336 | 138 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
139 if (tree_return_command::returning) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
140 tree_return_command::returning = 0; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
141 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
142 if (tree_break_command::breaking) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
143 tree_break_command::breaking--; |
8013
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
144 |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
145 if (error_state) |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
146 octave_call_stack::backtrace_error_message (); |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
147 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
148 else |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
149 ::error ("max_recursion_limit exceeded"); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
150 } |
7336 | 151 } |
152 else | |
153 error ("invalid call to script"); | |
154 } | |
155 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
156 unwind_protect::run_frame (uwp_frame); |
8011
3100283874d7
improve backtrace error messages
John W. Eaton <jwe@octave.org>
parents:
7968
diff
changeset
|
157 |
7336 | 158 return retval; |
159 } | |
160 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
161 void |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
162 octave_user_script::accept (tree_walker& tw) |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
163 { |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
164 tw.visit_octave_user_script (*this); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
165 } |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7589
diff
changeset
|
166 |
2974 | 167 // User defined functions. |
168 | |
3219 | 169 DEFINE_OCTAVE_ALLOCATOR (octave_user_function); |
2974 | 170 |
3219 | 171 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_user_function, |
4612 | 172 "user-defined function", |
3219 | 173 "user-defined function"); |
2974 | 174 |
175 // Ugh. This really needs to be simplified (code/data? | |
176 // extrinsic/intrinsic state?). | |
177 | |
178 octave_user_function::octave_user_function | |
7336 | 179 (symbol_table::scope_id sid, tree_parameter_list *pl, |
180 tree_parameter_list *rl, tree_statement_list *cl) | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
181 : octave_user_code (std::string (), std::string ()), |
2974 | 182 param_list (pl), ret_list (rl), cmd_list (cl), |
7336 | 183 lead_comm (), trail_comm (), file_name (), |
6323 | 184 parent_name (), t_parsed (static_cast<time_t> (0)), |
3255 | 185 t_checked (static_cast<time_t> (0)), |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
186 system_fcn_file (false), call_depth (-1), |
7589
90fb25a5a3fa
minor cleanup in octave_user_function constructor
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
187 num_named_args (param_list ? param_list->length () : 0), |
7336 | 188 nested_function (false), inline_function (false), |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
189 class_constructor (false), class_method (false), |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
190 parent_scope (-1), local_scope (sid) |
7736 | 191 { |
192 if (cmd_list) | |
193 cmd_list->mark_as_function_body (); | |
9639
8d79f36ebdde
store scope->function pointer
Jaroslav Hajek <highegg@gmail.com>
parents:
9529
diff
changeset
|
194 |
8d79f36ebdde
store scope->function pointer
Jaroslav Hajek <highegg@gmail.com>
parents:
9529
diff
changeset
|
195 if (local_scope >= 0) |
8d79f36ebdde
store scope->function pointer
Jaroslav Hajek <highegg@gmail.com>
parents:
9529
diff
changeset
|
196 symbol_table::set_curr_fcn (this, local_scope); |
7736 | 197 } |
2974 | 198 |
199 octave_user_function::~octave_user_function (void) | |
200 { | |
201 delete param_list; | |
202 delete ret_list; | |
203 delete cmd_list; | |
3665 | 204 delete lead_comm; |
205 delete trail_comm; | |
7336 | 206 |
207 symbol_table::erase_scope (local_scope); | |
2974 | 208 } |
209 | |
210 octave_user_function * | |
211 octave_user_function::define_ret_list (tree_parameter_list *t) | |
212 { | |
213 ret_list = t; | |
214 | |
215 return this; | |
216 } | |
217 | |
218 void | |
4343 | 219 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974 | 220 { |
4343 | 221 file_name = nm; |
2974 | 222 } |
223 | |
224 void | |
225 octave_user_function::mark_as_system_fcn_file (void) | |
226 { | |
227 if (! file_name.empty ()) | |
228 { | |
229 // We really should stash the whole path to the file we found, | |
230 // when we looked it up, to avoid possible race conditions... | |
5775 | 231 // FIXME |
2974 | 232 // |
233 // We probably also don't need to get the library directory | |
234 // every time, but since this function is only called when the | |
235 // function file is parsed, it probably doesn't matter that | |
236 // much. | |
237 | |
3523 | 238 std::string ff_name = fcn_file_in_path (file_name); |
2974 | 239 |
3565 | 240 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974 | 241 system_fcn_file = 1; |
242 } | |
243 else | |
244 system_fcn_file = 0; | |
245 } | |
246 | |
247 bool | |
248 octave_user_function::takes_varargs (void) const | |
249 { | |
250 return (param_list && param_list->takes_varargs ()); | |
251 } | |
252 | |
5848 | 253 bool |
254 octave_user_function::takes_var_return (void) const | |
2974 | 255 { |
5848 | 256 return (ret_list && ret_list->takes_varargs ()); |
2974 | 257 } |
258 | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
259 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
260 octave_user_function::lock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
261 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
262 symbol_table::lock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
263 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
264 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
265 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
266 octave_user_function::unlock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
267 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
268 symbol_table::unlock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
269 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
270 |
2974 | 271 octave_value_list |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
272 octave_user_function::all_va_args (const octave_value_list& args) |
2974 | 273 { |
274 octave_value_list retval; | |
275 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
276 octave_idx_type n = args.length () - num_named_args; |
2974 | 277 |
3178 | 278 if (n > 0) |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
279 retval = args.slice (num_named_args, n); |
2974 | 280 |
281 return retval; | |
282 } | |
283 | |
284 octave_value_list | |
4247 | 285 octave_user_function::subsref (const std::string& type, |
4219 | 286 const std::list<octave_value_list>& idx, |
3933 | 287 int nargout) |
288 { | |
289 octave_value_list retval; | |
290 | |
291 switch (type[0]) | |
292 { | |
293 case '(': | |
5154 | 294 { |
5155 | 295 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154 | 296 |
297 retval = do_multi_index_op (tmp_nargout, idx.front ()); | |
298 } | |
3933 | 299 break; |
300 | |
301 case '{': | |
302 case '.': | |
303 { | |
304 std::string nm = type_name (); | |
305 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
306 } | |
307 break; | |
308 | |
309 default: | |
310 panic_impossible (); | |
311 } | |
312 | |
5775 | 313 // FIXME -- perhaps there should be an |
4059 | 314 // octave_value_list::next_subsref member function? See also |
315 // octave_builtin::subsref. | |
3933 | 316 |
4219 | 317 if (idx.size () > 1) |
4994 | 318 retval = retval(0).next_subsref (nargout, type, idx); |
4059 | 319 |
320 return retval; | |
3933 | 321 } |
322 | |
323 octave_value_list | |
3544 | 324 octave_user_function::do_multi_index_op (int nargout, |
325 const octave_value_list& args) | |
2974 | 326 { |
327 octave_value_list retval; | |
328 | |
329 if (error_state) | |
330 return retval; | |
331 | |
332 if (! cmd_list) | |
333 return retval; | |
334 | |
335 int nargin = args.length (); | |
336 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
337 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
2974 | 338 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
339 unwind_protect::protect_var (call_depth); |
2974 | 340 call_depth++; |
341 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
342 if (call_depth >= Vmax_recursion_depth) |
3131 | 343 { |
344 ::error ("max_recursion_limit exceeded"); | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
345 unwind_protect::run_frame (uwp_frame); |
3131 | 346 return retval; |
347 } | |
348 | |
7336 | 349 // Save old and set current symbol table context, for |
350 // eval_undefined_error(). | |
351 | |
7901 | 352 octave_call_stack::push (this, local_scope, call_depth); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
353 unwind_protect::add_fcn (octave_call_stack::pop); |
7901 | 354 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
355 if (call_depth > 0) |
2974 | 356 { |
7336 | 357 symbol_table::push_context (); |
2974 | 358 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
359 unwind_protect::add_fcn (symbol_table::pop_context); |
7336 | 360 } |
2974 | 361 |
362 string_vector arg_names = args.name_tags (); | |
363 | |
364 if (param_list && ! param_list->varargs_only ()) | |
365 { | |
366 param_list->define_from_arg_vector (args); | |
367 if (error_state) | |
368 goto abort; | |
369 } | |
370 | |
3239 | 371 // Force parameter list to be undefined when this function exits. |
372 // Doing so decrements the reference counts on the values of local | |
373 // variables that are also named function parameters. | |
374 | |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
375 if (param_list) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
376 unwind_protect::add_method (param_list, |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
377 &tree_parameter_list::undefine); |
3239 | 378 |
379 // Force return list to be undefined when this function exits. | |
380 // Doing so decrements the reference counts on the values of local | |
381 // variables that are also named values returned by this function. | |
382 | |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
383 if (ret_list) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
384 unwind_protect::add_method (ret_list, |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
385 &tree_parameter_list::undefine); |
3239 | 386 |
8142
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
387 if (call_depth == 0) |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
388 { |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
389 // Force symbols to be undefined again when this function |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
390 // exits. |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
391 // |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
392 // This cleanup function is added to the unwind_protect stack |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
393 // after the calls to clear the parameter lists so that local |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
394 // variables will be cleared before the parameter lists are |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
395 // cleared. That way, any function parameters that have been |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
396 // declared global will be unmarked as global before they are |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
397 // undefined by the clear_param_list cleanup function. |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
398 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
399 unwind_protect::add_fcn (symbol_table::clear_variables); |
8142
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
400 } |
54b41376e381
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add symbol_table::clear_variables cleanup function to the unwind_protect stack after the parameter list cleanup functions
John W. Eaton <jwe@octave.org>
parents:
8013
diff
changeset
|
401 |
2974 | 402 // The following code is in a separate scope to avoid warnings from |
403 // G++ about `goto abort' crossing the initialization of some | |
404 // variables. | |
405 | |
406 { | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
407 bind_automatic_vars (arg_names, nargin, nargout, all_va_args (args)); |
2974 | 408 |
409 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); | |
410 | |
411 if (echo_commands) | |
412 print_code_function_header (); | |
413 | |
414 // Evaluate the commands that make up the function. | |
415 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
416 unwind_protect::protect_var (tree_evaluator::in_fcn_or_script_body); |
8669
33783e94fb16
line number fixes and other evaluator tweaks
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
417 tree_evaluator::in_fcn_or_script_body = true; |
3489 | 418 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
419 bool special_expr = (is_inline_function () |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
420 || cmd_list->is_anon_function_body ()); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
421 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
422 if (special_expr) |
6657 | 423 { |
424 assert (cmd_list->length () == 1); | |
425 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
426 tree_statement *stmt = 0; |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
427 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
428 if ((stmt = cmd_list->front ()) |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
429 && stmt->is_expression ()) |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
430 { |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
431 tree_expression *expr = stmt->expression (); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
432 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
433 retval = expr->rvalue (nargout); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
434 } |
6657 | 435 } |
436 else | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
437 cmd_list->accept (*current_evaluator); |
2974 | 438 |
439 if (echo_commands) | |
440 print_code_function_trailer (); | |
441 | |
4207 | 442 if (tree_return_command::returning) |
443 tree_return_command::returning = 0; | |
2974 | 444 |
4207 | 445 if (tree_break_command::breaking) |
446 tree_break_command::breaking--; | |
2974 | 447 |
448 if (error_state) | |
8013
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
449 { |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
450 octave_call_stack::backtrace_error_message (); |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
451 goto abort; |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
452 } |
2974 | 453 |
454 // Copy return values out. | |
455 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
456 if (ret_list && ! special_expr) |
3871 | 457 { |
4748 | 458 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871 | 459 |
5848 | 460 Cell varargout; |
461 | |
6591 | 462 if (ret_list->takes_varargs ()) |
5848 | 463 { |
7336 | 464 octave_value varargout_varval = symbol_table::varval ("varargout"); |
6591 | 465 |
7336 | 466 if (varargout_varval.is_defined ()) |
6591 | 467 { |
7336 | 468 varargout = varargout_varval.cell_value (); |
5848 | 469 |
6591 | 470 if (error_state) |
471 error ("expecting varargout to be a cell array object"); | |
472 } | |
5848 | 473 } |
474 | |
475 if (! error_state) | |
9644
080e11f1b0c1
don't return undefined output values from user functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9639
diff
changeset
|
476 retval = ret_list->convert_to_const_vector (nargout, varargout); |
3871 | 477 } |
2974 | 478 } |
479 | |
480 abort: | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
481 unwind_protect::run_frame (uwp_frame); |
2974 | 482 |
483 return retval; | |
484 } | |
485 | |
486 void | |
487 octave_user_function::accept (tree_walker& tw) | |
488 { | |
489 tw.visit_octave_user_function (*this); | |
490 } | |
491 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
492 bool |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
493 octave_user_function::subsasgn_optimization_ok (void) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
494 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
495 bool retval = false; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
496 if (Voptimize_subsasgn_calls |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
497 && param_list->length () > 0 && ! param_list->varargs_only () |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
498 && ret_list->length () == 1 && ! ret_list->takes_varargs ()) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
499 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
500 tree_identifier *par1 = param_list->front ()->ident (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
501 tree_identifier *ret1 = ret_list->front ()->ident (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
502 retval = par1->name () == ret1->name (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
503 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
504 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
505 return retval; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
506 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
507 |
7336 | 508 #if 0 |
2974 | 509 void |
3933 | 510 octave_user_function::print_symtab_info (std::ostream& os) const |
511 { | |
7336 | 512 symbol_table::print_info (os, local_scope); |
3933 | 513 } |
7336 | 514 #endif |
3933 | 515 |
516 void | |
2974 | 517 octave_user_function::print_code_function_header (void) |
518 { | |
5794 | 519 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 520 |
521 tpc.visit_octave_user_function_header (*this); | |
522 } | |
523 | |
524 void | |
525 octave_user_function::print_code_function_trailer (void) | |
526 { | |
5794 | 527 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 528 |
529 tpc.visit_octave_user_function_trailer (*this); | |
530 } | |
531 | |
532 void | |
533 octave_user_function::bind_automatic_vars | |
3974 | 534 (const string_vector& arg_names, int nargin, int nargout, |
535 const octave_value_list& va_args) | |
2974 | 536 { |
537 if (! arg_names.empty ()) | |
7336 | 538 symbol_table::varref ("argn") = arg_names; |
2974 | 539 |
7336 | 540 symbol_table::varref (".nargin.") = nargin; |
541 symbol_table::varref (".nargout.") = nargout; | |
542 | |
543 symbol_table::mark_hidden (".nargin."); | |
544 symbol_table::mark_hidden (".nargout."); | |
3974 | 545 |
546 if (takes_varargs ()) | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8142
diff
changeset
|
547 symbol_table::varref ("varargin") = va_args.cell_value (); |
2974 | 548 } |
549 | |
4700 | 550 DEFUN (nargin, args, , |
551 "-*- texinfo -*-\n\ | |
552 @deftypefn {Built-in Function} {} nargin ()\n\ | |
553 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ | |
554 Within a function, return the number of arguments passed to the function.\n\ | |
555 At the top level, return the number of command line arguments passed to\n\ | |
556 Octave. If called with the optional argument @var{fcn_name}, return the\n\ | |
557 maximum number of arguments the named function can accept, or -1 if the\n\ | |
558 function accepts a variable number of arguments.\n\ | |
5642 | 559 @seealso{nargout, varargin, varargout}\n\ |
4700 | 560 @end deftypefn") |
561 { | |
562 octave_value retval; | |
563 | |
564 int nargin = args.length (); | |
565 | |
566 if (nargin == 1) | |
567 { | |
568 std::string fname = args(0).string_value (); | |
569 | |
570 if (! error_state) | |
571 { | |
7336 | 572 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 573 |
574 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 575 |
576 if (fcn) | |
577 { | |
578 if (fcn->takes_varargs ()) | |
579 retval = -1; | |
580 else | |
581 { | |
582 tree_parameter_list *param_list = fcn->parameter_list (); | |
583 | |
584 retval = param_list ? param_list->length () : 0; | |
585 } | |
586 } | |
587 else | |
588 error ("nargin: invalid function"); | |
589 } | |
590 else | |
591 error ("nargin: expecting string as first argument"); | |
592 } | |
593 else if (nargin == 0) | |
594 { | |
7336 | 595 retval = symbol_table::varval (".nargin."); |
4700 | 596 |
7336 | 597 if (retval.is_undefined ()) |
598 retval = 0; | |
4700 | 599 } |
600 else | |
5823 | 601 print_usage (); |
4700 | 602 |
603 return retval; | |
604 } | |
605 | |
606 DEFUN (nargout, args, , | |
607 "-*- texinfo -*-\n\ | |
608 @deftypefn {Built-in Function} {} nargout ()\n\ | |
609 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ | |
610 Within a function, return the number of values the caller expects to\n\ | |
611 receive. If called with the optional argument @var{fcn_name}, return the\n\ | |
612 maximum number of values the named function can produce, or -1 if the\n\ | |
613 function can produce a variable number of values.\n\ | |
614 \n\ | |
615 For example,\n\ | |
616 \n\ | |
617 @example\n\ | |
618 f ()\n\ | |
619 @end example\n\ | |
620 \n\ | |
621 @noindent\n\ | |
4704 | 622 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700 | 623 \n\ |
624 @example\n\ | |
625 [s, t] = f ()\n\ | |
626 @end example\n\ | |
627 \n\ | |
628 @noindent\n\ | |
629 will cause @code{nargout} to return 2 inside the function\n\ | |
630 @code{f}.\n\ | |
631 \n\ | |
632 At the top level, @code{nargout} is undefined.\n\ | |
5642 | 633 @seealso{nargin, varargin, varargout}\n\ |
4700 | 634 @end deftypefn") |
635 { | |
636 octave_value retval; | |
637 | |
638 int nargin = args.length (); | |
639 | |
640 if (nargin == 1) | |
641 { | |
642 std::string fname = args(0).string_value (); | |
643 | |
644 if (! error_state) | |
645 { | |
7336 | 646 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 647 |
648 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 649 |
650 if (fcn) | |
651 { | |
652 if (fcn->takes_var_return ()) | |
653 retval = -1; | |
654 else | |
655 { | |
656 tree_parameter_list *ret_list = fcn->return_list (); | |
657 | |
658 retval = ret_list ? ret_list->length () : 0; | |
659 } | |
660 } | |
661 else | |
662 error ("nargout: invalid function"); | |
663 } | |
664 else | |
665 error ("nargout: expecting string as first argument"); | |
666 } | |
667 else if (nargin == 0) | |
668 { | |
7336 | 669 if (! symbol_table::at_top_level ()) |
4700 | 670 { |
7336 | 671 retval = symbol_table::varval (".nargout."); |
4700 | 672 |
7336 | 673 if (retval.is_undefined ()) |
674 retval = 0; | |
4700 | 675 } |
676 else | |
677 error ("nargout: invalid call at top level"); | |
678 } | |
679 else | |
5823 | 680 print_usage (); |
4700 | 681 |
682 return retval; | |
683 } | |
684 | |
5794 | 685 DEFUN (max_recursion_depth, args, nargout, |
686 "-*- texinfo -*-\n\ | |
687 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ | |
688 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ | |
689 Query or set the internal limit on the number of times a function may\n\ | |
690 be called recursively. If the limit is exceeded, an error message is\n\ | |
691 printed and control returns to the top level.\n\ | |
692 @end deftypefn") | |
3131 | 693 { |
5794 | 694 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974 | 695 } |
696 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
697 DEFUN (optimize_subsasgn_calls, args, nargout, |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
698 "-*- texinfo -*-\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
699 @deftypefn {Built-in Function} {@var{val} =} optimize_subsasgn_calls ()\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
700 @deftypefnx {Built-in Function} {@var{old_val} =} optimize_subsasgn_calls (@var{new_val})\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
701 Query or set the internal flag for subsasgn method call optimizations.\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
702 If true, Octave will attempt to eliminate the redundant copying when calling\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
703 subsasgn method of a user-defined class.\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
704 @end deftypefn") |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
705 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
706 return SET_INTERNAL_VARIABLE (optimize_subsasgn_calls); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
707 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
708 |
2974 | 709 /* |
710 ;;; Local Variables: *** | |
711 ;;; mode: C++ *** | |
712 ;;; End: *** | |
713 */ |