2974
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_user_function_h) |
|
24 #define octave_user_function_h 1 |
|
25 |
|
26 #include <ctime> |
|
27 |
|
28 #include <string> |
4214
|
29 #include <stack> |
2974
|
30 |
3665
|
31 #include "comment-list.h" |
2974
|
32 #include "oct-obj.h" |
|
33 #include "ov-fcn.h" |
|
34 #include "ov-typeinfo.h" |
|
35 |
|
36 class string_vector; |
|
37 |
|
38 class octave_value; |
|
39 class tree_parameter_list; |
|
40 class tree_statement_list; |
|
41 class tree_va_return_list; |
|
42 class tree_walker; |
|
43 class symbol_table; |
|
44 class symbol_record; |
|
45 |
|
46 // Builtin functions. |
|
47 |
|
48 class |
|
49 octave_user_function : public octave_function |
|
50 { |
|
51 public: |
|
52 |
|
53 octave_user_function (tree_parameter_list *pl = 0, |
|
54 tree_parameter_list *rl = 0, |
|
55 tree_statement_list *cl = 0, |
|
56 symbol_table *st = 0); |
|
57 |
|
58 ~octave_user_function (void); |
|
59 |
4654
|
60 octave_function *function_value (bool = false) { return this; } |
2974
|
61 |
4700
|
62 octave_user_function *user_function_value (bool = false) { return this; } |
|
63 |
2974
|
64 octave_user_function *define_param_list (tree_parameter_list *t); |
|
65 |
|
66 octave_user_function *define_ret_list (tree_parameter_list *t); |
|
67 |
4343
|
68 void stash_fcn_file_name (const std::string& nm); |
2974
|
69 |
3665
|
70 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
71 |
|
72 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
73 |
3325
|
74 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
75 |
3255
|
76 void stash_fcn_file_time (const octave_time& t) |
3165
|
77 { |
|
78 t_parsed = t; |
|
79 mark_fcn_file_up_to_date (t); |
|
80 } |
2974
|
81 |
4346
|
82 void stash_symtab_ptr (symbol_record *sr) { symtab_entry = sr; } |
2974
|
83 |
4346
|
84 std::string fcn_file_name (void) const { return file_name; } |
2974
|
85 |
4346
|
86 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
87 |
4346
|
88 octave_time time_checked (void) const { return t_checked; } |
3165
|
89 |
2974
|
90 void mark_as_system_fcn_file (void); |
|
91 |
4346
|
92 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
93 |
4748
|
94 bool is_user_function (void) const { return true; } |
|
95 |
2974
|
96 bool takes_varargs (void) const; |
|
97 |
4346
|
98 void octave_va_start (void) { curr_va_arg_number = num_named_args; } |
2974
|
99 |
|
100 octave_value octave_va_arg (void); |
|
101 |
|
102 octave_value_list octave_all_va_args (void); |
|
103 |
|
104 bool takes_var_return (void) const; |
|
105 |
|
106 void octave_vr_val (const octave_value& val); |
|
107 |
3974
|
108 void varargout_to_vr_val (void); |
|
109 |
|
110 bool has_varargout (void) const; |
|
111 |
4748
|
112 void stash_function_name (const std::string& s) { my_name = s; } |
2974
|
113 |
4238
|
114 void mark_as_nested_function (void) { nested_function = true; } |
|
115 |
|
116 bool is_nested_function (void) const { return nested_function; } |
|
117 |
3875
|
118 void save_args_passed (const octave_value_list& args) |
|
119 { |
|
120 if (call_depth > 1) |
|
121 saved_args.push (args_passed); |
|
122 |
|
123 args_passed = args; |
|
124 } |
|
125 |
|
126 void restore_args_passed (void) |
|
127 { |
3933
|
128 if (saved_args.empty ()) |
|
129 args_passed = octave_value_list (); |
|
130 else |
4214
|
131 { |
|
132 args_passed = saved_args.top (); |
|
133 saved_args.pop (); |
|
134 } |
3875
|
135 } |
3239
|
136 |
4661
|
137 octave_value subsref (const std::string&, |
|
138 const std::list<octave_value_list>&) |
4271
|
139 { |
|
140 panic_impossible (); |
|
141 return octave_value (); |
|
142 } |
|
143 |
4247
|
144 octave_value_list subsref (const std::string& type, |
4219
|
145 const std::list<octave_value_list>& idx, |
3933
|
146 int nargout); |
|
147 |
3544
|
148 octave_value_list |
|
149 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
150 |
4346
|
151 void traceback_error (void) const; |
2974
|
152 |
|
153 tree_parameter_list *parameter_list (void) { return param_list; } |
|
154 |
|
155 tree_parameter_list *return_list (void) { return ret_list; } |
|
156 |
|
157 tree_statement_list *body (void) { return cmd_list; } |
|
158 |
3665
|
159 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
160 |
|
161 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
162 |
2974
|
163 void accept (tree_walker& tw); |
|
164 |
3933
|
165 void print_symtab_info (std::ostream& os) const; |
|
166 |
2974
|
167 private: |
|
168 |
|
169 // List of arguments for this function. These are local variables. |
|
170 tree_parameter_list *param_list; |
|
171 |
|
172 // List of parameters we return. These are also local variables in |
|
173 // this function. |
|
174 tree_parameter_list *ret_list; |
|
175 |
|
176 // The list of commands that make up the body of this function. |
|
177 tree_statement_list *cmd_list; |
|
178 |
|
179 // The local symbol table for this function. |
|
180 symbol_table *sym_tab; |
|
181 |
3665
|
182 // The comments preceding the FUNCTION token. |
|
183 octave_comment_list *lead_comm; |
|
184 |
|
185 // The comments preceding the ENDFUNCTION token. |
|
186 octave_comment_list *trail_comm; |
|
187 |
2974
|
188 // The name of the file we parsed |
3523
|
189 std::string file_name; |
2974
|
190 |
|
191 // The time the file was parsed. |
3255
|
192 octave_time t_parsed; |
2974
|
193 |
3165
|
194 // The time the file was last checked to see if it needs to be |
|
195 // parsed again. |
3255
|
196 octave_time t_checked; |
3165
|
197 |
2974
|
198 // True if this function came from a file that is considered to be a |
|
199 // system function. This affects whether we check the time stamp |
|
200 // on the file to see if it has changed. |
|
201 bool system_fcn_file; |
|
202 |
|
203 // Used to keep track of recursion depth. |
|
204 int call_depth; |
|
205 |
|
206 // The number of arguments that have names. |
|
207 int num_named_args; |
|
208 |
4238
|
209 // TRUE means this is a nested function. |
|
210 bool nested_function; |
|
211 |
2974
|
212 // The values that were passed as arguments. |
|
213 octave_value_list args_passed; |
|
214 |
3875
|
215 // A place to store the passed args for recursive calls. |
4214
|
216 std::stack<octave_value_list> saved_args; |
3875
|
217 |
2974
|
218 // The number of arguments passed in. |
|
219 int num_args_passed; |
|
220 |
|
221 // Used to keep track of the current offset into the list of va_args. |
|
222 int curr_va_arg_number; |
|
223 |
|
224 // The list of return values when an unspecified number can be |
|
225 // returned. |
|
226 tree_va_return_list *vr_list; |
|
227 |
|
228 // The symbol record for this function. |
|
229 symbol_record *symtab_entry; |
|
230 |
|
231 // The symbol record for argn in the local symbol table. |
|
232 symbol_record *argn_sr; |
|
233 |
|
234 // The symbol record for nargin in the local symbol table. |
|
235 symbol_record *nargin_sr; |
|
236 |
|
237 // The symbol record for nargout in the local symbol table. |
|
238 symbol_record *nargout_sr; |
|
239 |
3974
|
240 // The symbol record for varargin in the local symbol table. |
|
241 symbol_record *varargin_sr; |
|
242 |
2974
|
243 void print_code_function_header (void); |
|
244 |
|
245 void print_code_function_trailer (void); |
|
246 |
|
247 void install_automatic_vars (void); |
|
248 |
|
249 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
250 int nargout, const octave_value_list& va_args); |
3219
|
251 |
4645
|
252 |
|
253 // No copying! |
|
254 |
|
255 octave_user_function (const octave_user_function& fn); |
|
256 |
|
257 octave_user_function& operator = (const octave_user_function& fn); |
|
258 |
4612
|
259 DECLARE_OCTAVE_ALLOCATOR |
3219
|
260 |
4612
|
261 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2974
|
262 }; |
|
263 |
|
264 #endif |
|
265 |
|
266 /* |
|
267 ;;; Local Variables: *** |
|
268 ;;; mode: C++ *** |
|
269 ;;; End: *** |
|
270 */ |