Mercurial > hg > octave-lyh
annotate src/ov-usr-fcn.cc @ 9529:8e5009334661
partially revert e79470be3ecb
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 17 Aug 2009 13:09:12 +0200 |
parents | e79470be3ecb |
children | 8d79f36ebdde |
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 (); | |
194 } | |
2974 | 195 |
196 octave_user_function::~octave_user_function (void) | |
197 { | |
198 delete param_list; | |
199 delete ret_list; | |
200 delete cmd_list; | |
3665 | 201 delete lead_comm; |
202 delete trail_comm; | |
7336 | 203 |
204 symbol_table::erase_scope (local_scope); | |
2974 | 205 } |
206 | |
207 octave_user_function * | |
208 octave_user_function::define_ret_list (tree_parameter_list *t) | |
209 { | |
210 ret_list = t; | |
211 | |
212 return this; | |
213 } | |
214 | |
215 void | |
4343 | 216 octave_user_function::stash_fcn_file_name (const std::string& nm) |
2974 | 217 { |
4343 | 218 file_name = nm; |
2974 | 219 } |
220 | |
221 void | |
222 octave_user_function::mark_as_system_fcn_file (void) | |
223 { | |
224 if (! file_name.empty ()) | |
225 { | |
226 // We really should stash the whole path to the file we found, | |
227 // when we looked it up, to avoid possible race conditions... | |
5775 | 228 // FIXME |
2974 | 229 // |
230 // We probably also don't need to get the library directory | |
231 // every time, but since this function is only called when the | |
232 // function file is parsed, it probably doesn't matter that | |
233 // much. | |
234 | |
3523 | 235 std::string ff_name = fcn_file_in_path (file_name); |
2974 | 236 |
3565 | 237 if (Vfcn_file_dir == ff_name.substr (0, Vfcn_file_dir.length ())) |
2974 | 238 system_fcn_file = 1; |
239 } | |
240 else | |
241 system_fcn_file = 0; | |
242 } | |
243 | |
244 bool | |
245 octave_user_function::takes_varargs (void) const | |
246 { | |
247 return (param_list && param_list->takes_varargs ()); | |
248 } | |
249 | |
5848 | 250 bool |
251 octave_user_function::takes_var_return (void) const | |
2974 | 252 { |
5848 | 253 return (ret_list && ret_list->takes_varargs ()); |
2974 | 254 } |
255 | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
256 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
257 octave_user_function::lock_subfunctions (void) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
258 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
259 symbol_table::lock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
260 } |
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 void |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
263 octave_user_function::unlock_subfunctions (void) |
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 symbol_table::unlock_subfunctions (local_scope); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
266 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
267 |
2974 | 268 octave_value_list |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
269 octave_user_function::all_va_args (const octave_value_list& args) |
2974 | 270 { |
271 octave_value_list retval; | |
272 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
273 octave_idx_type n = args.length () - num_named_args; |
2974 | 274 |
3178 | 275 if (n > 0) |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
276 retval = args.slice (num_named_args, n); |
2974 | 277 |
278 return retval; | |
279 } | |
280 | |
281 octave_value_list | |
4247 | 282 octave_user_function::subsref (const std::string& type, |
4219 | 283 const std::list<octave_value_list>& idx, |
3933 | 284 int nargout) |
285 { | |
286 octave_value_list retval; | |
287 | |
288 switch (type[0]) | |
289 { | |
290 case '(': | |
5154 | 291 { |
5155 | 292 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5154 | 293 |
294 retval = do_multi_index_op (tmp_nargout, idx.front ()); | |
295 } | |
3933 | 296 break; |
297 | |
298 case '{': | |
299 case '.': | |
300 { | |
301 std::string nm = type_name (); | |
302 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
303 } | |
304 break; | |
305 | |
306 default: | |
307 panic_impossible (); | |
308 } | |
309 | |
5775 | 310 // FIXME -- perhaps there should be an |
4059 | 311 // octave_value_list::next_subsref member function? See also |
312 // octave_builtin::subsref. | |
3933 | 313 |
4219 | 314 if (idx.size () > 1) |
4994 | 315 retval = retval(0).next_subsref (nargout, type, idx); |
4059 | 316 |
317 return retval; | |
3933 | 318 } |
319 | |
320 octave_value_list | |
3544 | 321 octave_user_function::do_multi_index_op (int nargout, |
322 const octave_value_list& args) | |
2974 | 323 { |
324 octave_value_list retval; | |
325 | |
326 if (error_state) | |
327 return retval; | |
328 | |
329 if (! cmd_list) | |
330 return retval; | |
331 | |
332 int nargin = args.length (); | |
333 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
334 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
2974 | 335 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
336 unwind_protect::protect_var (call_depth); |
2974 | 337 call_depth++; |
338 | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
339 if (call_depth >= Vmax_recursion_depth) |
3131 | 340 { |
341 ::error ("max_recursion_limit exceeded"); | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
342 unwind_protect::run_frame (uwp_frame); |
3131 | 343 return retval; |
344 } | |
345 | |
7336 | 346 // Save old and set current symbol table context, for |
347 // eval_undefined_error(). | |
348 | |
7901 | 349 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
|
350 unwind_protect::add_fcn (octave_call_stack::pop); |
7901 | 351 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7736
diff
changeset
|
352 if (call_depth > 0) |
2974 | 353 { |
7336 | 354 symbol_table::push_context (); |
2974 | 355 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
356 unwind_protect::add_fcn (symbol_table::pop_context); |
7336 | 357 } |
2974 | 358 |
359 string_vector arg_names = args.name_tags (); | |
360 | |
361 if (param_list && ! param_list->varargs_only ()) | |
362 { | |
363 param_list->define_from_arg_vector (args); | |
364 if (error_state) | |
365 goto abort; | |
366 } | |
367 | |
3239 | 368 // Force parameter list to be undefined when this function exits. |
369 // Doing so decrements the reference counts on the values of local | |
370 // variables that are also named function parameters. | |
371 | |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
372 if (param_list) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
373 unwind_protect::add_method (param_list, |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
374 &tree_parameter_list::undefine); |
3239 | 375 |
376 // Force return list to be undefined when this function exits. | |
377 // Doing so decrements the reference counts on the values of local | |
378 // variables that are also named values returned by this function. | |
379 | |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
380 if (ret_list) |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
381 unwind_protect::add_method (ret_list, |
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
382 &tree_parameter_list::undefine); |
3239 | 383 |
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
|
384 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
|
385 { |
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
|
386 // 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
|
387 // 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
|
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 // 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
|
390 // 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
|
391 // 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
|
392 // 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
|
393 // 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
|
394 // 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
|
395 |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
396 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
|
397 } |
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 |
2974 | 399 // The following code is in a separate scope to avoid warnings from |
400 // G++ about `goto abort' crossing the initialization of some | |
401 // variables. | |
402 | |
403 { | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
404 bind_automatic_vars (arg_names, nargin, nargout, all_va_args (args)); |
2974 | 405 |
406 bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS); | |
407 | |
408 if (echo_commands) | |
409 print_code_function_header (); | |
410 | |
411 // Evaluate the commands that make up the function. | |
412 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
413 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
|
414 tree_evaluator::in_fcn_or_script_body = true; |
3489 | 415 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
416 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
|
417 || 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
|
418 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
419 if (special_expr) |
6657 | 420 { |
421 assert (cmd_list->length () == 1); | |
422 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
423 tree_statement *stmt = 0; |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
424 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
425 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
|
426 && stmt->is_expression ()) |
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 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
|
429 |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
430 retval = expr->rvalue (nargout); |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
431 } |
6657 | 432 } |
433 else | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
434 cmd_list->accept (*current_evaluator); |
2974 | 435 |
436 if (echo_commands) | |
437 print_code_function_trailer (); | |
438 | |
4207 | 439 if (tree_return_command::returning) |
440 tree_return_command::returning = 0; | |
2974 | 441 |
4207 | 442 if (tree_break_command::breaking) |
443 tree_break_command::breaking--; | |
2974 | 444 |
445 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
|
446 { |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
447 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
|
448 goto abort; |
b3e667f1ab4c
call backtrace_error_message in eval functions, not when popping stack
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
449 } |
2974 | 450 |
451 // Copy return values out. | |
452 | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8580
diff
changeset
|
453 if (ret_list && ! special_expr) |
3871 | 454 { |
4748 | 455 ret_list->initialize_undefined_elements (my_name, nargout, Matrix ()); |
3871 | 456 |
5848 | 457 Cell varargout; |
458 | |
6591 | 459 if (ret_list->takes_varargs ()) |
5848 | 460 { |
7336 | 461 octave_value varargout_varval = symbol_table::varval ("varargout"); |
6591 | 462 |
7336 | 463 if (varargout_varval.is_defined ()) |
6591 | 464 { |
7336 | 465 varargout = varargout_varval.cell_value (); |
5848 | 466 |
6591 | 467 if (error_state) |
468 error ("expecting varargout to be a cell array object"); | |
469 } | |
5848 | 470 } |
471 | |
472 if (! error_state) | |
473 retval = ret_list->convert_to_const_vector (varargout); | |
3871 | 474 } |
2974 | 475 } |
476 | |
477 abort: | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9144
diff
changeset
|
478 unwind_protect::run_frame (uwp_frame); |
2974 | 479 |
480 return retval; | |
481 } | |
482 | |
483 void | |
484 octave_user_function::accept (tree_walker& tw) | |
485 { | |
486 tw.visit_octave_user_function (*this); | |
487 } | |
488 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
489 bool |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
490 octave_user_function::subsasgn_optimization_ok (void) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
491 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
492 bool retval = false; |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
493 if (Voptimize_subsasgn_calls |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
494 && param_list->length () > 0 && ! param_list->varargs_only () |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
495 && ret_list->length () == 1 && ! ret_list->takes_varargs ()) |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
496 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
497 tree_identifier *par1 = param_list->front ()->ident (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
498 tree_identifier *ret1 = ret_list->front ()->ident (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
499 retval = par1->name () == ret1->name (); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
500 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
501 |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
502 return retval; |
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 |
7336 | 505 #if 0 |
2974 | 506 void |
3933 | 507 octave_user_function::print_symtab_info (std::ostream& os) const |
508 { | |
7336 | 509 symbol_table::print_info (os, local_scope); |
3933 | 510 } |
7336 | 511 #endif |
3933 | 512 |
513 void | |
2974 | 514 octave_user_function::print_code_function_header (void) |
515 { | |
5794 | 516 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 517 |
518 tpc.visit_octave_user_function_header (*this); | |
519 } | |
520 | |
521 void | |
522 octave_user_function::print_code_function_trailer (void) | |
523 { | |
5794 | 524 tree_print_code tpc (octave_stdout, VPS4); |
2974 | 525 |
526 tpc.visit_octave_user_function_trailer (*this); | |
527 } | |
528 | |
529 void | |
530 octave_user_function::bind_automatic_vars | |
3974 | 531 (const string_vector& arg_names, int nargin, int nargout, |
532 const octave_value_list& va_args) | |
2974 | 533 { |
534 if (! arg_names.empty ()) | |
7336 | 535 symbol_table::varref ("argn") = arg_names; |
2974 | 536 |
7336 | 537 symbol_table::varref (".nargin.") = nargin; |
538 symbol_table::varref (".nargout.") = nargout; | |
539 | |
540 symbol_table::mark_hidden (".nargin."); | |
541 symbol_table::mark_hidden (".nargout."); | |
3974 | 542 |
543 if (takes_varargs ()) | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8142
diff
changeset
|
544 symbol_table::varref ("varargin") = va_args.cell_value (); |
2974 | 545 } |
546 | |
4700 | 547 DEFUN (nargin, args, , |
548 "-*- texinfo -*-\n\ | |
549 @deftypefn {Built-in Function} {} nargin ()\n\ | |
550 @deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ | |
551 Within a function, return the number of arguments passed to the function.\n\ | |
552 At the top level, return the number of command line arguments passed to\n\ | |
553 Octave. If called with the optional argument @var{fcn_name}, return the\n\ | |
554 maximum number of arguments the named function can accept, or -1 if the\n\ | |
555 function accepts a variable number of arguments.\n\ | |
5642 | 556 @seealso{nargout, varargin, varargout}\n\ |
4700 | 557 @end deftypefn") |
558 { | |
559 octave_value retval; | |
560 | |
561 int nargin = args.length (); | |
562 | |
563 if (nargin == 1) | |
564 { | |
565 std::string fname = args(0).string_value (); | |
566 | |
567 if (! error_state) | |
568 { | |
7336 | 569 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 570 |
571 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 572 |
573 if (fcn) | |
574 { | |
575 if (fcn->takes_varargs ()) | |
576 retval = -1; | |
577 else | |
578 { | |
579 tree_parameter_list *param_list = fcn->parameter_list (); | |
580 | |
581 retval = param_list ? param_list->length () : 0; | |
582 } | |
583 } | |
584 else | |
585 error ("nargin: invalid function"); | |
586 } | |
587 else | |
588 error ("nargin: expecting string as first argument"); | |
589 } | |
590 else if (nargin == 0) | |
591 { | |
7336 | 592 retval = symbol_table::varval (".nargin."); |
4700 | 593 |
7336 | 594 if (retval.is_undefined ()) |
595 retval = 0; | |
4700 | 596 } |
597 else | |
5823 | 598 print_usage (); |
4700 | 599 |
600 return retval; | |
601 } | |
602 | |
603 DEFUN (nargout, args, , | |
604 "-*- texinfo -*-\n\ | |
605 @deftypefn {Built-in Function} {} nargout ()\n\ | |
606 @deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ | |
607 Within a function, return the number of values the caller expects to\n\ | |
608 receive. If called with the optional argument @var{fcn_name}, return the\n\ | |
609 maximum number of values the named function can produce, or -1 if the\n\ | |
610 function can produce a variable number of values.\n\ | |
611 \n\ | |
612 For example,\n\ | |
613 \n\ | |
614 @example\n\ | |
615 f ()\n\ | |
616 @end example\n\ | |
617 \n\ | |
618 @noindent\n\ | |
4704 | 619 will cause @code{nargout} to return 0 inside the function @code{f} and\n\ |
4700 | 620 \n\ |
621 @example\n\ | |
622 [s, t] = f ()\n\ | |
623 @end example\n\ | |
624 \n\ | |
625 @noindent\n\ | |
626 will cause @code{nargout} to return 2 inside the function\n\ | |
627 @code{f}.\n\ | |
628 \n\ | |
629 At the top level, @code{nargout} is undefined.\n\ | |
5642 | 630 @seealso{nargin, varargin, varargout}\n\ |
4700 | 631 @end deftypefn") |
632 { | |
633 octave_value retval; | |
634 | |
635 int nargin = args.length (); | |
636 | |
637 if (nargin == 1) | |
638 { | |
639 std::string fname = args(0).string_value (); | |
640 | |
641 if (! error_state) | |
642 { | |
7336 | 643 octave_value fcn_val = symbol_table::find_user_function (fname); |
4930 | 644 |
645 octave_user_function *fcn = fcn_val.user_function_value (true); | |
4700 | 646 |
647 if (fcn) | |
648 { | |
649 if (fcn->takes_var_return ()) | |
650 retval = -1; | |
651 else | |
652 { | |
653 tree_parameter_list *ret_list = fcn->return_list (); | |
654 | |
655 retval = ret_list ? ret_list->length () : 0; | |
656 } | |
657 } | |
658 else | |
659 error ("nargout: invalid function"); | |
660 } | |
661 else | |
662 error ("nargout: expecting string as first argument"); | |
663 } | |
664 else if (nargin == 0) | |
665 { | |
7336 | 666 if (! symbol_table::at_top_level ()) |
4700 | 667 { |
7336 | 668 retval = symbol_table::varval (".nargout."); |
4700 | 669 |
7336 | 670 if (retval.is_undefined ()) |
671 retval = 0; | |
4700 | 672 } |
673 else | |
674 error ("nargout: invalid call at top level"); | |
675 } | |
676 else | |
5823 | 677 print_usage (); |
4700 | 678 |
679 return retval; | |
680 } | |
681 | |
5794 | 682 DEFUN (max_recursion_depth, args, nargout, |
683 "-*- texinfo -*-\n\ | |
684 @deftypefn {Built-in Function} {@var{val} =} max_recursion_depth ()\n\ | |
685 @deftypefnx {Built-in Function} {@var{old_val} =} max_recursion_depth (@var{new_val})\n\ | |
686 Query or set the internal limit on the number of times a function may\n\ | |
687 be called recursively. If the limit is exceeded, an error message is\n\ | |
688 printed and control returns to the top level.\n\ | |
689 @end deftypefn") | |
3131 | 690 { |
5794 | 691 return SET_INTERNAL_VARIABLE (max_recursion_depth); |
2974 | 692 } |
693 | |
9522
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
694 DEFUN (optimize_subsasgn_calls, args, nargout, |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
695 "-*- texinfo -*-\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
696 @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
|
697 @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
|
698 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
|
699 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
|
700 subsasgn method of a user-defined class.\n\ |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
701 @end deftypefn") |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
702 { |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
703 return SET_INTERNAL_VARIABLE (optimize_subsasgn_calls); |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
704 } |
e79470be3ecb
implement subsasgn this-arg optimization
Jaroslav Hajek <highegg@gmail.com>
parents:
9396
diff
changeset
|
705 |
2974 | 706 /* |
707 ;;; Local Variables: *** | |
708 ;;; mode: C++ *** | |
709 ;;; End: *** | |
710 */ |