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 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2974
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <ctime> |
|
31 |
|
32 #include <string> |
4214
|
33 #include <stack> |
2974
|
34 |
3665
|
35 #include "comment-list.h" |
2974
|
36 #include "oct-obj.h" |
|
37 #include "ov-fcn.h" |
|
38 #include "ov-typeinfo.h" |
|
39 |
|
40 class string_vector; |
|
41 |
|
42 class octave_value; |
|
43 class tree_parameter_list; |
|
44 class tree_statement_list; |
|
45 class tree_va_return_list; |
|
46 class tree_walker; |
|
47 class symbol_table; |
|
48 class symbol_record; |
|
49 |
|
50 // Builtin functions. |
|
51 |
|
52 class |
|
53 octave_user_function : public octave_function |
|
54 { |
|
55 public: |
|
56 |
|
57 octave_user_function (tree_parameter_list *pl = 0, |
|
58 tree_parameter_list *rl = 0, |
|
59 tree_statement_list *cl = 0, |
|
60 symbol_table *st = 0); |
|
61 |
|
62 ~octave_user_function (void); |
|
63 |
|
64 octave_function *function_value (bool) { return this; } |
|
65 |
|
66 octave_user_function *define_param_list (tree_parameter_list *t); |
|
67 |
|
68 octave_user_function *define_ret_list (tree_parameter_list *t); |
|
69 |
4343
|
70 void stash_fcn_file_name (const std::string& nm); |
2974
|
71 |
3665
|
72 void stash_leading_comment (octave_comment_list *lc) { lead_comm = lc; } |
|
73 |
|
74 void stash_trailing_comment (octave_comment_list *tc) { trail_comm = tc; } |
|
75 |
3325
|
76 void mark_fcn_file_up_to_date (const octave_time& t) { t_checked = t; } |
3165
|
77 |
3255
|
78 void stash_fcn_file_time (const octave_time& t) |
3165
|
79 { |
|
80 t_parsed = t; |
|
81 mark_fcn_file_up_to_date (t); |
|
82 } |
2974
|
83 |
4346
|
84 void stash_symtab_ptr (symbol_record *sr) { symtab_entry = sr; } |
2974
|
85 |
4346
|
86 std::string fcn_file_name (void) const { return file_name; } |
2974
|
87 |
4346
|
88 octave_time time_parsed (void) const { return t_parsed; } |
2974
|
89 |
4346
|
90 octave_time time_checked (void) const { return t_checked; } |
3165
|
91 |
2974
|
92 void mark_as_system_fcn_file (void); |
|
93 |
4346
|
94 bool is_system_fcn_file (void) const { return system_fcn_file; } |
2974
|
95 |
|
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 |
3523
|
112 void stash_function_name (const std::string& s); |
2974
|
113 |
4346
|
114 std::string function_name (void) const { return fcn_name; } |
2974
|
115 |
4238
|
116 void mark_as_nested_function (void) { nested_function = true; } |
|
117 |
|
118 bool is_nested_function (void) const { return nested_function; } |
|
119 |
3875
|
120 void save_args_passed (const octave_value_list& args) |
|
121 { |
|
122 if (call_depth > 1) |
|
123 saved_args.push (args_passed); |
|
124 |
|
125 args_passed = args; |
|
126 } |
|
127 |
|
128 void restore_args_passed (void) |
|
129 { |
3933
|
130 if (saved_args.empty ()) |
|
131 args_passed = octave_value_list (); |
|
132 else |
4214
|
133 { |
|
134 args_passed = saved_args.top (); |
|
135 saved_args.pop (); |
|
136 } |
3875
|
137 } |
3239
|
138 |
4271
|
139 octave_value subsref (const std::string& type, |
|
140 const std::list<octave_value_list>& idx) |
|
141 { |
|
142 panic_impossible (); |
|
143 return octave_value (); |
|
144 } |
|
145 |
4247
|
146 octave_value_list subsref (const std::string& type, |
4219
|
147 const std::list<octave_value_list>& idx, |
3933
|
148 int nargout); |
|
149 |
3544
|
150 octave_value_list |
|
151 do_multi_index_op (int nargout, const octave_value_list& args); |
2974
|
152 |
4346
|
153 void traceback_error (void) const; |
2974
|
154 |
|
155 tree_parameter_list *parameter_list (void) { return param_list; } |
|
156 |
|
157 tree_parameter_list *return_list (void) { return ret_list; } |
|
158 |
|
159 tree_statement_list *body (void) { return cmd_list; } |
|
160 |
3665
|
161 octave_comment_list *leading_comment (void) { return lead_comm; } |
|
162 |
|
163 octave_comment_list *trailing_comment (void) { return trail_comm; } |
|
164 |
2974
|
165 void accept (tree_walker& tw); |
|
166 |
3933
|
167 void print_symtab_info (std::ostream& os) const; |
|
168 |
2974
|
169 private: |
|
170 |
|
171 octave_user_function (void); |
|
172 |
|
173 octave_user_function (const octave_user_function& m); |
|
174 |
|
175 // List of arguments for this function. These are local variables. |
|
176 tree_parameter_list *param_list; |
|
177 |
|
178 // List of parameters we return. These are also local variables in |
|
179 // this function. |
|
180 tree_parameter_list *ret_list; |
|
181 |
|
182 // The list of commands that make up the body of this function. |
|
183 tree_statement_list *cmd_list; |
|
184 |
|
185 // The local symbol table for this function. |
|
186 symbol_table *sym_tab; |
|
187 |
3665
|
188 // The comments preceding the FUNCTION token. |
|
189 octave_comment_list *lead_comm; |
|
190 |
|
191 // The comments preceding the ENDFUNCTION token. |
|
192 octave_comment_list *trail_comm; |
|
193 |
2974
|
194 // The name of the file we parsed |
3523
|
195 std::string file_name; |
2974
|
196 |
|
197 // The name of the function. |
3523
|
198 std::string fcn_name; |
2974
|
199 |
|
200 // The time the file was parsed. |
3255
|
201 octave_time t_parsed; |
2974
|
202 |
3165
|
203 // The time the file was last checked to see if it needs to be |
|
204 // parsed again. |
3255
|
205 octave_time t_checked; |
3165
|
206 |
2974
|
207 // True if this function came from a file that is considered to be a |
|
208 // system function. This affects whether we check the time stamp |
|
209 // on the file to see if it has changed. |
|
210 bool system_fcn_file; |
|
211 |
|
212 // Used to keep track of recursion depth. |
|
213 int call_depth; |
|
214 |
|
215 // The number of arguments that have names. |
|
216 int num_named_args; |
|
217 |
4238
|
218 // TRUE means this is a nested function. |
|
219 bool nested_function; |
|
220 |
2974
|
221 // The values that were passed as arguments. |
|
222 octave_value_list args_passed; |
|
223 |
3875
|
224 // A place to store the passed args for recursive calls. |
4214
|
225 std::stack<octave_value_list> saved_args; |
3875
|
226 |
2974
|
227 // The number of arguments passed in. |
|
228 int num_args_passed; |
|
229 |
|
230 // Used to keep track of the current offset into the list of va_args. |
|
231 int curr_va_arg_number; |
|
232 |
|
233 // The list of return values when an unspecified number can be |
|
234 // returned. |
|
235 tree_va_return_list *vr_list; |
|
236 |
|
237 // The symbol record for this function. |
|
238 symbol_record *symtab_entry; |
|
239 |
|
240 // The symbol record for argn in the local symbol table. |
|
241 symbol_record *argn_sr; |
|
242 |
|
243 // The symbol record for nargin in the local symbol table. |
|
244 symbol_record *nargin_sr; |
|
245 |
|
246 // The symbol record for nargout in the local symbol table. |
|
247 symbol_record *nargout_sr; |
|
248 |
3974
|
249 // The symbol record for varargin in the local symbol table. |
|
250 symbol_record *varargin_sr; |
|
251 |
2974
|
252 void print_code_function_header (void); |
|
253 |
|
254 void print_code_function_trailer (void); |
|
255 |
|
256 void install_automatic_vars (void); |
|
257 |
|
258 void bind_automatic_vars (const string_vector& arg_names, int nargin, |
3974
|
259 int nargout, const octave_value_list& va_args); |
3219
|
260 |
|
261 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
262 |
|
263 DECLARE_OCTAVE_ALLOCATOR |
2974
|
264 }; |
|
265 |
|
266 #endif |
|
267 |
|
268 /* |
|
269 ;;; Local Variables: *** |
|
270 ;;; mode: C++ *** |
|
271 ;;; End: *** |
|
272 */ |