GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-usr-fcn.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_ov_usr_fcn_h)
24 #define octave_ov_usr_fcn_h 1
25 
26 #include "octave-config.h"
27 
28 #include <ctime>
29 
30 #include <string>
31 #include <stack>
32 
33 #include "comment-list.h"
34 #include "ovl.h"
35 #include "ov-fcn.h"
36 #include "ov-typeinfo.h"
37 #include "symscope.h"
38 #include "unwind-prot.h"
39 
40 class string_vector;
41 
42 class octave_value;
43 
44 namespace octave
45 {
46  class file_info;
47  class tree_parameter_list;
48  class tree_statement_list;
49  class tree_evaluator;
50  class tree_expression;
51  class tree_walker;
52 #if defined (HAVE_LLVM)
53  class jit_function_info;
54 #endif
55 
56 }
57 
58 class
60 {
61 protected:
62 
65  const std::string& ds = "")
66  : octave_function (nm, ds), m_scope (scope), m_file_info (nullptr),
67  curr_unwind_protect_frame (nullptr)
68  { }
69 
70 public:
71 
73  : octave_function (), m_scope (), m_file_info (nullptr),
74  curr_unwind_protect_frame (nullptr)
75  { }
76 
77  // No copying!
78 
79  octave_user_code (const octave_user_code& f) = delete;
80 
81  octave_user_code& operator = (const octave_user_code& f) = delete;
82 
83  ~octave_user_code (void);
84 
85  bool is_user_code (void) const { return true; }
86 
89  {
90  return curr_unwind_protect_frame;
91  }
92 
93  std::string get_code_line (size_t line);
94 
95  std::deque<std::string> get_code_lines (size_t line, size_t num_lines);
96 
97  void cache_function_text (const std::string& text,
98  const octave::sys::time& timestamp);
99 
100  octave::symbol_scope scope (void) { return m_scope; }
101 
102  virtual std::map<std::string, octave_value> subfunctions (void) const;
103 
104  virtual octave::tree_statement_list * body (void) = 0;
105 
106 protected:
107 
108  void get_file_info (void);
109 
110  // Our symbol table scope.
112 
113  // Cached text of function or script code with line offsets
114  // calculated.
116 
117  // pointer to the current unwind_protect frame of this function.
119 };
120 
121 // Scripts.
122 
123 class
125 {
126 public:
127 
128  octave_user_script (void);
129 
130  octave_user_script (const std::string& fnm, const std::string& nm,
131  const octave::symbol_scope& scope = octave::symbol_scope (),
132  octave::tree_statement_list *cmds = nullptr,
133  const std::string& ds = "");
134 
135  octave_user_script (const std::string& fnm, const std::string& nm,
136  const octave::symbol_scope& scope = octave::symbol_scope (),
137  const std::string& ds = "");
138 
139  // No copying!
140 
141  octave_user_script (const octave_user_script& f) = delete;
142 
143  octave_user_script& operator = (const octave_user_script& f) = delete;
144 
145  ~octave_user_script (void);
146 
147  octave_function * function_value (bool = false) { return this; }
148 
149  octave_user_script * user_script_value (bool = false) { return this; }
150 
151  octave_user_code * user_code_value (bool = false) { return this; }
152 
153  // Scripts and user functions are both considered "scripts" because
154  // they are written in Octave's scripting language.
155 
156  bool is_user_script (void) const { return true; }
157 
158  void stash_fcn_file_name (const std::string& nm) { file_name = nm; }
159 
160  void mark_fcn_file_up_to_date (const octave::sys::time& t) { t_checked = t; }
161 
163  {
164  t_parsed = t;
165  mark_fcn_file_up_to_date (t);
166  }
167 
168  std::string fcn_file_name (void) const { return file_name; }
169 
170  octave::sys::time time_parsed (void) const { return t_parsed; }
171 
172  octave::sys::time time_checked (void) const { return t_checked; }
173 
175  call (octave::tree_evaluator& tw, int nargout = 0,
176  const octave_value_list& args = octave_value_list ());
177 
178  octave::tree_statement_list * body (void) { return cmd_list; }
179 
180  void accept (octave::tree_walker& tw);
181 
182 private:
183 
184  // The list of commands that make up the body of this function.
186 
187  // The name of the file we parsed.
189 
190  // The time the file was parsed.
192 
193  // The time the file was last checked to see if it needs to be
194  // parsed again.
196 
197  // Used to keep track of recursion depth.
199 
201 };
202 
203 // User-defined functions.
204 
205 class
207 {
208 public:
209 
211  octave::tree_parameter_list *pl = nullptr,
212  octave::tree_parameter_list *rl = nullptr,
213  octave::tree_statement_list *cl = nullptr);
214 
215  // No copying!
216 
217  octave_user_function (const octave_user_function& fn) = delete;
218 
219  octave_user_function& operator = (const octave_user_function& fn) = delete;
220 
221  ~octave_user_function (void);
222 
224  {
225  return is_anonymous_function ()
226  ? 0 : static_cast<octave::symbol_record::context_id>(call_depth);
227  }
228 
229  octave_function * function_value (bool = false) { return this; }
230 
231  octave_user_function * user_function_value (bool = false) { return this; }
232 
233  octave_user_code * user_code_value (bool = false) { return this; }
234 
235  octave_user_function * define_param_list (octave::tree_parameter_list *t);
236 
238 
239  void stash_fcn_file_name (const std::string& nm);
240 
241  void stash_fcn_location (int line, int col)
242  {
243  location_line = line;
244  location_column = col;
245  }
246 
247  int beginning_line (void) const { return location_line; }
248  int beginning_column (void) const { return location_column; }
249 
250  void stash_fcn_end_location (int line, int col)
251  {
252  end_location_line = line;
253  end_location_column = col;
254  }
255 
256  int ending_line (void) const { return end_location_line; }
257  int ending_column (void) const { return end_location_column; }
258 
259  void maybe_relocate_end (void);
260 
261  void stash_parent_fcn_name (const std::string& p) { parent_name = p; }
262 
263  void stash_parent_fcn_scope (const octave::symbol_scope& ps);
264 
265  void stash_leading_comment (octave::comment_list *lc) { lead_comm = lc; }
266 
267  void stash_trailing_comment (octave::comment_list *tc) { trail_comm = tc; }
268 
269  void mark_fcn_file_up_to_date (const octave::sys::time& t) { t_checked = t; }
270 
272  {
273  t_parsed = t;
274  mark_fcn_file_up_to_date (t);
275  }
276 
277  std::string fcn_file_name (void) const { return file_name; }
278 
279  std::string profiler_name (void) const;
280 
281  std::string parent_fcn_name (void) const { return parent_name; }
282 
284  {
285  return m_scope.parent_scope ();
286  }
287 
288  octave::sys::time time_parsed (void) const { return t_parsed; }
289 
290  octave::sys::time time_checked (void) const { return t_checked; }
291 
292  void mark_as_system_fcn_file (void);
293 
294  bool is_system_fcn_file (void) const { return system_fcn_file; }
295 
296  bool is_user_function (void) const { return true; }
297 
298  void erase_subfunctions (void);
299 
300  bool takes_varargs (void) const;
301 
302  bool takes_var_return (void) const;
303 
304  void mark_as_private_function (const std::string& cname = "");
305 
306  void lock_subfunctions (void);
307 
308  void unlock_subfunctions (void);
309 
310  std::map<std::string, octave_value> subfunctions (void) const;
311 
312  bool has_subfunctions (void) const;
313 
314  void stash_subfunction_names (const std::list<std::string>& names);
315 
316  std::list<std::string> subfunction_names (void) const;
317 
318  octave_value_list all_va_args (const octave_value_list& args);
319 
320  void stash_function_name (const std::string& s) { my_name = s; }
321 
322  void mark_as_subfunction (void) { subfunction = true; }
323 
324  bool is_subfunction (void) const { return subfunction; }
325 
326  void mark_as_inline_function (void) { inline_function = true; }
327 
328  bool is_inline_function (void) const { return inline_function; }
329 
330  void mark_as_anonymous_function (void) { anonymous_function = true; }
331 
332  bool is_anonymous_function (void) const { return anonymous_function; }
333 
334  bool is_anonymous_function_of_class
335  (const std::string& cname = "") const
336  {
337  return anonymous_function
338  ? (cname.empty ()
339  ? (! dispatch_class ().empty ())
340  : cname == dispatch_class ())
341  : false;
342  }
343 
344  // If we are a special expression, then the function body consists of exactly
345  // one expression. The expression's result is the return value of the
346  // function.
347  bool is_special_expr (void) const
348  {
349  return is_inline_function () || is_anonymous_function ();
350  }
351 
352  bool is_nested_function (void) const { return nested_function; }
353 
354  void mark_as_nested_function (void) { nested_function = true; }
355 
356  void mark_as_class_constructor (void) { class_constructor = legacy; }
357 
358  void mark_as_classdef_constructor (void) { class_constructor = classdef; }
359 
360  bool is_class_constructor (const std::string& cname = "") const
361  {
362  return class_constructor == legacy
363  ? (cname.empty () ? true : cname == dispatch_class ()) : false;
364  }
365 
366  bool is_classdef_constructor (const std::string& cname = "") const
367  {
368  return class_constructor == classdef
369  ? (cname.empty () ? true : cname == dispatch_class ()) : false;
370  }
371 
372  void mark_as_class_method (void) { class_method = true; }
373 
374  bool is_class_method (const std::string& cname = "") const
375  {
376  return class_method
377  ? (cname.empty () ? true : cname == dispatch_class ()) : false;
378  }
379 
381  call (octave::tree_evaluator& tw, int nargout = 0,
382  const octave_value_list& args = octave_value_list ());
383 
384  octave::tree_parameter_list * parameter_list (void) { return param_list; }
385 
386  octave::tree_parameter_list * return_list (void) { return ret_list; }
387 
388  octave::tree_statement_list * body (void) { return cmd_list; }
389 
390  octave::comment_list * leading_comment (void) { return lead_comm; }
391 
392  octave::comment_list * trailing_comment (void) { return trail_comm; }
393 
394  // If is_special_expr is true, retrieve the sigular expression that forms the
395  // body. May be null (even if is_special_expr is true).
396  octave::tree_expression * special_expr (void);
397 
398  bool subsasgn_optimization_ok (void);
399 
400  void accept (octave::tree_walker& tw);
401 
402 #if defined (HAVE_LLVM)
403  octave::jit_function_info * get_info (void) { return jit_info; }
404 
405  void stash_info (octave::jit_function_info *info) { jit_info = info; }
406 #endif
407 
408  octave_value dump (void) const;
409 
410 private:
411 
412  enum class_ctor_type
413  {
416  classdef
417  };
418 
419  std::string ctor_type_str (void) const;
420 
421  // List of arguments for this function. These are local variables.
423 
424  // List of parameters we return. These are also local variables in
425  // this function.
427 
428  // The list of commands that make up the body of this function.
430 
431  // The comments preceding the FUNCTION token.
433 
434  // The comments preceding the ENDFUNCTION token.
436 
437  // The name of the file we parsed.
439 
440  // Location where this function was defined.
445 
446  // The name of the parent function, if any.
448 
449  // The time the file was parsed.
451 
452  // The time the file was last checked to see if it needs to be
453  // parsed again.
455 
456  // True if this function came from a file that is considered to be a
457  // system function. This affects whether we check the time stamp
458  // on the file to see if it has changed.
460 
461  // Used to keep track of recursion depth.
463 
464  // The number of arguments that have names.
466 
467  // TRUE means this is a subfunction of a primary function.
469 
470  // TRUE means this is an inline function.
472 
473  // TRUE means this is an anonymous function.
475 
476  // TRUE means this is a nested function. (either a child or parent)
478 
479  // Enum describing whether this function is the constructor for class object.
481 
482  // TRUE means this function is a method for a class.
484 
485 #if defined (HAVE_LLVM)
487 #endif
488 
489  void maybe_relocate_end_internal (void);
490 
491  void print_code_function_header (const std::string& prefix);
492 
493  void print_code_function_trailer (const std::string& prefix);
494 
495  void bind_automatic_vars (octave::tree_evaluator& tw,
496  const string_vector& arg_names,
497  int nargin, int nargout,
498  const octave_value_list& va_args);
499 
500  void restore_warning_states (void);
501 
503 };
504 
505 #endif
void mark_fcn_file_up_to_date(const octave::sys::time &t)
Definition: ov-usr-fcn.h:160
void mark_as_inline_function(void)
Definition: ov-usr-fcn.h:326
void stash_info(octave::jit_function_info *info)
Definition: ov-usr-fcn.h:405
octave_user_function * user_function_value(bool=false)
Definition: ov-usr-fcn.h:231
void stash_trailing_comment(octave::comment_list *tc)
Definition: ov-usr-fcn.h:267
void stash_fcn_file_time(const octave::sys::time &t)
Definition: ov-usr-fcn.h:162
bool is_user_function(void) const
Definition: ov-usr-fcn.h:296
int ending_line(void) const
Definition: ov-usr-fcn.h:256
void stash_leading_comment(octave::comment_list *lc)
Definition: ov-usr-fcn.h:265
octave::sys::time time_checked(void) const
Definition: ov-usr-fcn.h:172
octave::comment_list * lead_comm
Definition: ov-usr-fcn.h:432
octave::sys::time time_parsed(void) const
Definition: ov-usr-fcn.h:288
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
octave::sys::time t_parsed
Definition: ov-usr-fcn.h:450
octave::symbol_scope parent_fcn_scope(void) const
Definition: ov-usr-fcn.h:283
octave::tree_statement_list * body(void)
Definition: ov-usr-fcn.h:178
octave_user_code(void)
Definition: ov-usr-fcn.h:72
bool is_special_expr(void) const
Definition: ov-usr-fcn.h:347
octave::file_info * m_file_info
Definition: ov-usr-fcn.h:115
octave_user_code(const std::string &nm, const octave::symbol_scope &scope=octave::symbol_scope(), const std::string &ds="")
Definition: ov-usr-fcn.h:63
octave::sys::time t_checked
Definition: ov-usr-fcn.h:454
int beginning_column(void) const
Definition: ov-usr-fcn.h:248
octave::tree_statement_list * body(void)
Definition: ov-usr-fcn.h:388
void stash_fcn_end_location(int line, int col)
Definition: ov-usr-fcn.h:250
bool is_class_method(const std::string &cname="") const
Definition: ov-usr-fcn.h:374
octave::sys::time time_parsed(void) const
Definition: ov-usr-fcn.h:170
octave_user_code * user_code_value(bool=false)
Definition: ov-usr-fcn.h:151
octave::sys::time time_checked(void) const
Definition: ov-usr-fcn.h:290
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
bool is_user_script(void) const
Definition: ov-usr-fcn.h:156
octave::jit_function_info * jit_info
Definition: ov-usr-fcn.h:486
s
Definition: file-io.cc:2729
octave_function * function_value(bool=false)
Definition: ov-usr-fcn.h:147
octave::sys::time t_parsed
Definition: ov-usr-fcn.h:191
void mark_fcn_file_up_to_date(const octave::sys::time &t)
Definition: ov-usr-fcn.h:269
void stash_fcn_location(int line, int col)
Definition: ov-usr-fcn.h:241
int beginning_line(void) const
Definition: ov-usr-fcn.h:247
octave::tree_parameter_list * parameter_list(void)
Definition: ov-usr-fcn.h:384
octave::symbol_scope scope(void)
Definition: ov-usr-fcn.h:100
bool is_subfunction(void) const
Definition: ov-usr-fcn.h:324
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
std::string parent_fcn_name(void) const
Definition: ov-usr-fcn.h:281
std::shared_ptr< symbol_scope_rep > parent_scope(void) const
Definition: symscope.h:653
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
octave::comment_list * trailing_comment(void)
Definition: ov-usr-fcn.h:392
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:997
octave::tree_parameter_list * ret_list
Definition: ov-usr-fcn.h:426
void stash_function_name(const std::string &s)
Definition: ov-usr-fcn.h:320
octave::jit_function_info * get_info(void)
Definition: ov-usr-fcn.h:403
void mark_as_nested_function(void)
Definition: ov-usr-fcn.h:354
void mark_as_class_method(void)
Definition: ov-usr-fcn.h:372
int ending_column(void) const
Definition: ov-usr-fcn.h:257
octave::sys::time t_checked
Definition: ov-usr-fcn.h:195
void stash_fcn_file_time(const octave::sys::time &t)
Definition: ov-usr-fcn.h:271
octave::comment_list * leading_comment(void)
Definition: ov-usr-fcn.h:390
octave::tree_parameter_list * return_list(void)
Definition: ov-usr-fcn.h:386
std::string file_name
Definition: ov-usr-fcn.h:188
bool is_anonymous_function(void) const
Definition: ov-usr-fcn.h:332
octave::tree_statement_list * cmd_list
Definition: ov-usr-fcn.h:429
std::string file_name
Definition: ov-usr-fcn.h:438
octave::tree_parameter_list * param_list
Definition: ov-usr-fcn.h:422
void mark_as_classdef_constructor(void)
Definition: ov-usr-fcn.h:358
octave_user_code * user_code_value(bool=false)
Definition: ov-usr-fcn.h:233
std::string fcn_file_name(void) const
Definition: ov-usr-fcn.h:277
octave_user_script * user_script_value(bool=false)
Definition: ov-usr-fcn.h:149
octave::unwind_protect * unwind_protect_frame(void)
Definition: ov-usr-fcn.h:88
bool is_inline_function(void) const
Definition: ov-usr-fcn.h:328
octave::unwind_protect * curr_unwind_protect_frame
Definition: ov-usr-fcn.h:118
p
Definition: lu.cc:138
void stash_fcn_file_name(const std::string &nm)
Definition: ov-usr-fcn.h:158
std::string parent_name
Definition: ov-usr-fcn.h:447
bool is_user_code(void) const
Definition: ov-usr-fcn.h:85
octave::tree_statement_list * cmd_list
Definition: ov-usr-fcn.h:185
class_ctor_type class_constructor
Definition: ov-usr-fcn.h:480
octave_function * function_value(bool=false)
Definition: ov-usr-fcn.h:229
args.length() nargin
Definition: file-io.cc:589
void mark_as_anonymous_function(void)
Definition: ov-usr-fcn.h:330
octave::comment_list * trail_comm
Definition: ov-usr-fcn.h:435
bool is_class_constructor(const std::string &cname="") const
Definition: ov-usr-fcn.h:360
bool is_classdef_constructor(const std::string &cname="") const
Definition: ov-usr-fcn.h:366
octave::symbol_record::context_id active_context() const
Definition: ov-usr-fcn.h:223
void mark_as_subfunction(void)
Definition: ov-usr-fcn.h:322
bool is_nested_function(void) const
Definition: ov-usr-fcn.h:352
void stash_parent_fcn_name(const std::string &p)
Definition: ov-usr-fcn.h:261
static int call_depth
Definition: daspk.cc:59
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
bool is_system_fcn_file(void) const
Definition: ov-usr-fcn.h:294
void mark_as_class_constructor(void)
Definition: ov-usr-fcn.h:356
octave::symbol_scope m_scope
Definition: ov-usr-fcn.h:111
std::string fcn_file_name(void) const
Definition: ov-usr-fcn.h:168