2974
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
|
4 2005, 2006, 2007 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 #if !defined (octave_user_function_h) |
|
25 #define octave_user_function_h 1 |
|
26 |
|
27 #include <ctime> |
|
28 |
|
29 #include <string> |
4214
|
30 #include <stack> |
2974
|
31 |
3665
|
32 #include "comment-list.h" |
2974
|
33 #include "oct-obj.h" |
|
34 #include "ov-fcn.h" |
|
35 #include "ov-typeinfo.h" |
7336
|
36 #include "symtab.h" |
2974
|
37 |
|
38 class string_vector; |
|
39 |
|
40 class octave_value; |
|
41 class tree_parameter_list; |
|
42 class tree_statement_list; |
|
43 class tree_va_return_list; |
|
44 class tree_walker; |
|
45 |
5744
|
46 // Scripts. |
|
47 |
|
48 class |
|
49 octave_user_script : public octave_function |
|
50 { |
|
51 public: |
|
52 |
|
53 octave_user_script (void) { } |
|
54 |
|
55 octave_user_script (const std::string& fnm, const std::string& nm, |
7336
|
56 const std::string& ds = std::string ()) |
5744
|
57 : octave_function (nm, ds), file_name (fnm) { } |
|
58 |
|
59 ~octave_user_script (void) { } |
|
60 |
|
61 // Scripts and user functions are both considered "scripts" because |
|
62 // they are written in Octave's scripting language. |
|
63 |
|
64 bool is_user_script (void) const { return true; } |
|
65 |
|
66 void stash_fcn_file_name (const std::string& nm) { file_name = nm; } |
|
67 |
|
68 std::string fcn_file_name (void) const { return file_name; } |
|
69 |
7336
|
70 octave_value_list |
|
71 do_multi_index_op (int nargout, const octave_value_list& args); |
|
72 |
5744
|
73 private: |
|
74 |
7336
|
75 // The name of the file to parse. |
5744
|
76 std::string file_name; |
|
77 |
|
78 // No copying! |
|
79 |
|
80 octave_user_script (const octave_user_script& f); |
|
81 |
|
82 octave_user_script& operator = (const octave_user_script& f); |
|
83 |
|
84 DECLARE_OCTAVE_ALLOCATOR |
|
85 |
|
86 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
87 }; |
|
88 |
|
89 // User-defined functions. |
2974
|
90 |
|
91 class |
|
92 octave_user_function : public octave_function |
|
93 { |
|
94 public: |
|
95 |
7336
|
96 octave_user_function (symbol_table::scope_id sid = -1, |
|
97 tree_parameter_list *pl = 0, |
2974
|
98 tree_parameter_list *rl = 0, |
7336
|
99 tree_statement_list *cl = 0); |
2974
|
100 |
|
101 ~octave_user_function (void); |
|
102 |
4654
|
103 octave_function *function_value (bool = false) { return this; } |
2974
|
104 |
4700
|
105 octave_user_function *user_function_value (bool = false) { return this; } |
|
106 |
2974
|
107 octave_user_function *define_param_list (tree_parameter_list *t); |
|
108 |
|
109 octave_user_function *define_ret_list (tree_parameter_list *t); |
|
110 |
4343
|
111 void stash_fcn_file_name (const std::string& nm); |
2974
|
112 |
6323
|
113 void stash_parent_fcn_name (const std::string& p) { parent_name = p; } |
|
114 |
3665
|
115 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
116 |
|
117 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
118 |
3325
|
119 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
120 |
3255
|
121 void stash_fcn_file_time (const octave_time& t) |
3165
|
122 { |
|
123 t_parsed = t; |
|
124 mark_fcn_file_up_to_date (t); |
|
125 } |
2974
|
126 |
4346
|
127 std::string fcn_file_name (void) const { return file_name; } |
2974
|
128 |
6323
|
129 std::string parent_fcn_name (void) const { return parent_name; } |
|
130 |
7336
|
131 symbol_table::scope_id scope (void) { return local_scope; } |
|
132 |
4346
|
133 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
134 |
4346
|
135 octave_time time_checked (void) const { return t_checked; } |
3165
|
136 |
2974
|
137 void mark_as_system_fcn_file (void); |
|
138 |
4346
|
139 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
140 |
4748
|
141 bool is_user_function (void) const { return true; } |
|
142 |
2974
|
143 bool takes_varargs (void) const; |
|
144 |
5848
|
145 bool takes_var_return (void) const; |
2974
|
146 |
|
147 octave_value_list octave_all_va_args (void); |
|
148 |
4748
|
149 void stash_function_name (const std::string& s) { my_name = s; } |
2974
|
150 |
4238
|
151 void mark_as_nested_function (void) { nested_function = true; } |
|
152 |
|
153 bool is_nested_function (void) const { return nested_function; } |
|
154 |
6149
|
155 void mark_as_inline_function (void) { inline_function = true; } |
|
156 |
|
157 bool is_inline_function (void) const { return inline_function; } |
|
158 |
7336
|
159 void mark_as_class_constructor (void) { class_constructor = true; } |
|
160 |
|
161 bool is_class_constructor (void) const { return class_constructor; } |
|
162 |
|
163 void mark_as_class_method (void) { class_method = true; } |
|
164 |
|
165 bool is_class_method (void) const { return class_method; } |
|
166 |
|
167 void stash_dispatch_class (const std::string& nm) { xdispatch_class = nm; } |
|
168 |
|
169 std::string dispatch_class (void) const { return xdispatch_class; } |
|
170 |
3875
|
171 void save_args_passed (const octave_value_list& args) |
|
172 { |
|
173 if (call_depth > 1) |
|
174 saved_args.push (args_passed); |
|
175 |
|
176 args_passed = args; |
|
177 } |
|
178 |
|
179 void restore_args_passed (void) |
|
180 { |
3933
|
181 if (saved_args.empty ()) |
|
182 args_passed = octave_value_list (); |
|
183 else |
4214
|
184 { |
|
185 args_passed = saved_args.top (); |
|
186 saved_args.pop (); |
|
187 } |
3875
|
188 } |
3239
|
189 |
4661
|
190 octave_value subsref (const std::string&, |
|
191 const std::list<octave_value_list>&) |
4271
|
192 { |
|
193 panic_impossible (); |
|
194 return octave_value (); |
|
195 } |
|
196 |
4247
|
197 octave_value_list subsref (const std::string& type, |
4219
|
198 const std::list<octave_value_list>& idx, |
3933
|
199 int nargout); |
|
200 |
3544
|
201 octave_value_list |
|
202 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
203 |
4346
|
204 void traceback_error (void) const; |
2974
|
205 |
|
206 tree_parameter_list *parameter_list (void) { return param_list; } |
|
207 |
|
208 tree_parameter_list *return_list (void) { return ret_list; } |
|
209 |
|
210 tree_statement_list *body (void) { return cmd_list; } |
|
211 |
3665
|
212 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
213 |
|
214 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
215 |
2974
|
216 void accept (tree_walker& tw); |
|
217 |
7336
|
218 #if 0 |
3933
|
219 void print_symtab_info (std::ostream& os) const; |
7336
|
220 #endif |
3933
|
221 |
2974
|
222 private: |
|
223 |
|
224 // List of arguments for this function. These are local variables. |
|
225 tree_parameter_list *param_list; |
|
226 |
|
227 // List of parameters we return. These are also local variables in |
|
228 // this function. |
|
229 tree_parameter_list *ret_list; |
|
230 |
|
231 // The list of commands that make up the body of this function. |
|
232 tree_statement_list *cmd_list; |
|
233 |
3665
|
234 // The comments preceding the FUNCTION token. |
|
235 octave_comment_list *lead_comm; |
|
236 |
|
237 // The comments preceding the ENDFUNCTION token. |
|
238 octave_comment_list *trail_comm; |
|
239 |
6323
|
240 // The name of the file we parsed. |
3523
|
241 std::string file_name; |
2974
|
242 |
6323
|
243 // The name of the parent function, if any. |
|
244 std::string parent_name; |
|
245 |
2974
|
246 // The time the file was parsed. |
3255
|
247 octave_time t_parsed; |
2974
|
248 |
3165
|
249 // The time the file was last checked to see if it needs to be |
|
250 // parsed again. |
3255
|
251 octave_time t_checked; |
3165
|
252 |
2974
|
253 // True if this function came from a file that is considered to be a |
|
254 // system function. This affects whether we check the time stamp |
|
255 // on the file to see if it has changed. |
|
256 bool system_fcn_file; |
|
257 |
|
258 // Used to keep track of recursion depth. |
|
259 int call_depth; |
|
260 |
|
261 // The number of arguments that have names. |
|
262 int num_named_args; |
|
263 |
4238
|
264 // TRUE means this is a nested function. |
|
265 bool nested_function; |
|
266 |
6149
|
267 // TRUE means this is an inline function. |
|
268 bool inline_function; |
|
269 |
7336
|
270 // TRUE means this function is the constructor for class object. |
|
271 bool class_constructor; |
|
272 |
|
273 // TRUE means this function is a method for a class. |
|
274 bool class_method; |
|
275 |
|
276 // If this object is a class method or constructor, this is the name |
|
277 // of the class to which the method belongs. |
|
278 std::string xdispatch_class; |
|
279 |
2974
|
280 // The values that were passed as arguments. |
|
281 octave_value_list args_passed; |
|
282 |
3875
|
283 // A place to store the passed args for recursive calls. |
4214
|
284 std::stack<octave_value_list> saved_args; |
3875
|
285 |
2974
|
286 // The number of arguments passed in. |
|
287 int num_args_passed; |
|
288 |
7336
|
289 symbol_table::scope_id local_scope; |
2974
|
290 |
7336
|
291 #if 0 |
2974
|
292 // The symbol record for argn in the local symbol table. |
7336
|
293 octave_value& argn_varref; |
2974
|
294 |
|
295 // The symbol record for nargin in the local symbol table. |
7336
|
296 octave_value& nargin_varref; |
2974
|
297 |
|
298 // The symbol record for nargout in the local symbol table. |
7336
|
299 octave_value& nargout_varref; |
2974
|
300 |
3974
|
301 // The symbol record for varargin in the local symbol table. |
7336
|
302 octave_value& varargin_varref; |
|
303 #endif |
3974
|
304 |
2974
|
305 void print_code_function_header (void); |
|
306 |
|
307 void print_code_function_trailer (void); |
|
308 |
|
309 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
310 int nargout, const octave_value_list& va_args); |
3219
|
311 |
4645
|
312 // No copying! |
|
313 |
|
314 octave_user_function (const octave_user_function& fn); |
|
315 |
|
316 octave_user_function& operator = (const octave_user_function& fn); |
|
317 |
4612
|
318 DECLARE_OCTAVE_ALLOCATOR |
3219
|
319 |
4612
|
320 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
321 }; |
|
322 |
|
323 #endif |
|
324 |
|
325 /* |
|
326 ;;; Local Variables: *** |
|
327 ;;; mode: C++ *** |
|
328 ;;; End: *** |
|
329 */ |