GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-fcn.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_fcn_h)
27 #define octave_ov_fcn_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 #include "oct-time.h"
34 #include "str-vec.h"
35 
36 #include "ovl.h"
37 #include "ov-base.h"
38 #include "ov-typeinfo.h"
39 #include "symscope.h"
40 
42 
43 class stack_frame;
44 class tree_evaluator;
45 class tree_walker;
46 
47 OCTAVE_END_NAMESPACE(octave)
48 
49 // Functions.
50 
51 class
52 OCTINTERP_API
54 {
55 public:
56 
58  : m_relative (false), m_locked (false), m_private (false),
59  m_dispatch_class (), m_package_name (), m_name (), m_dir_name (),
60  m_doc () { }
61 
62  OCTAVE_DISABLE_COPY_MOVE (octave_function)
63 
64  ~octave_function () = default;
65 
66  octave_base_value * clone () const;
67  octave_base_value * empty_clone () const;
68 
69  bool is_defined () const { return true; }
70 
71  bool is_function () const { return true; }
72 
73  virtual bool is_system_fcn_file () const { return false; }
74 
75  virtual std::string fcn_file_name () const { return ""; }
76 
77  virtual std::string src_file_name () const { return ""; }
78 
79  // The name to show in the profiler (also used as map-key).
80  virtual std::string profiler_name () const { return name (); }
81 
82  virtual std::string parent_fcn_name () const { return ""; }
83 
84  virtual octave::symbol_scope parent_fcn_scope () const
85  { return octave::symbol_scope::invalid (); }
86 
87  virtual std::list<std::string> parent_fcn_names () const
88  { return std::list<std::string> (); }
89 
90  virtual void mark_fcn_file_up_to_date (const octave::sys::time&) { }
91 
92  virtual octave::symbol_scope scope () { return octave::symbol_scope::invalid (); }
93 
94  virtual octave::sys::time time_parsed () const
95  { return octave::sys::time (static_cast<OCTAVE_TIME_T> (0)); }
96 
97  virtual octave::sys::time time_checked () const
98  { return octave::sys::time (static_cast<OCTAVE_TIME_T> (0)); }
99 
100  virtual int call_depth () const { return 0; }
101 
102  virtual bool is_nested_function () const { return false; }
103 
104  virtual bool is_parent_function () const { return false; }
105 
106  virtual bool is_subfunction () const { return false; }
107 
108  virtual bool is_compiled () const { return false; }
109 
110  bool is_class_constructor (const std::string& cname = "") const
111  {
112  return (is_classdef_constructor (cname) || is_legacy_constructor (cname));
113  }
114 
115  bool is_class_method (const std::string& cname = "") const
116  {
117  return (is_classdef_method (cname) || is_legacy_method (cname));
118  }
119 
120  virtual bool
121  is_legacy_constructor (const std::string& = "") const
122  { return false; }
123 
124  virtual bool
125  is_classdef_constructor (const std::string& = "") const
126  { return false; }
127 
128  virtual bool is_legacy_method (const std::string& = "") const
129  { return false; }
130 
131  virtual bool is_classdef_method (const std::string& = "") const
132  { return false; }
133 
134  virtual bool takes_varargs () const { return false; }
135 
136  virtual bool takes_var_return () const { return false; }
137 
138  // The next two functions are for dispatching to built-in
139  // functions given built-in classes.
140 
141  virtual void push_dispatch_class (const std::string&) { }
142 
143  virtual bool handles_dispatch_class (const std::string&) const
144  { return false; }
145 
146  void stash_dispatch_class (const std::string& nm) { m_dispatch_class = nm; }
147 
148  std::string dispatch_class () const { return m_dispatch_class; }
149 
150  void stash_package_name (const std::string& pack) { m_package_name = pack; }
151 
152  std::string package_name () const { return m_package_name; }
153 
154  virtual void
155  mark_as_private_function (const std::string& cname = "")
156  {
157  m_private = true;
158  m_dispatch_class = cname;
159  }
160 
161  bool is_private_function () const { return m_private; }
162 
163  bool is_private_function_of_class (const std::string& nm) const
164  { return m_private && m_dispatch_class == nm; }
165 
166  virtual bool
167  is_anonymous_function_of_class (const std::string& = "") const
168  { return false; }
169 
170  std::string dir_name () const { return m_dir_name; }
171 
172  void stash_dir_name (const std::string& dir) { m_dir_name = dir; }
173 
174  void lock ()
175  {
176  this->lock_subfunctions ();
177  m_locked = true;
178  }
179 
180  void unlock ()
181  {
182  this->unlock_subfunctions ();
183  m_locked = false;
184  }
185 
186  bool islocked () const { return m_locked; }
187 
188  virtual void lock_subfunctions () { }
189 
190  virtual void unlock_subfunctions () { }
191 
192  virtual void maybe_relocate_end () { }
193 
194  // Not valid until after the function is completely parsed.
195  virtual bool has_subfunctions () const { return false; }
196 
197  virtual void stash_subfunction_names (const std::list<std::string>&) { }
198 
199  virtual std::list<std::string> subfunction_names () const
200  { return std::list<std::string> (); }
201 
202  void mark_relative () { m_relative = true; }
203 
204  bool is_relative () const { return m_relative; }
205 
206  std::string name () const { return m_name; }
207 
208  std::string canonical_name () const
209  {
210  if (m_package_name.empty ())
211  return m_name;
212  else
213  return m_package_name + '.' + m_name;
214  }
215 
216  void document (const std::string& ds) { m_doc = ds; }
217 
218  virtual std::string
219  doc_string (const std::string& /*meth_name*/ = "") const { return m_doc; }
220 
221  virtual void unload () { }
222 
223  virtual void accept (octave::tree_walker&) { }
224 
225  virtual bool accepts_postfix_index (char type) const
226  { return (type == '('); }
227 
228  // Push new stack frame (if necessary) and execute function.
229  virtual octave_value_list
230  call (octave::tree_evaluator& tw, int nargout = 0,
231  const octave_value_list& args = octave_value_list ());
232 
233  // Execute function without pushing new stack frame (assumes that has
234  // already been done).
235  virtual octave_value_list
236  execute (octave::tree_evaluator& tw, int nargout = 0,
237  const octave_value_list& args = octave_value_list ()) = 0;
238 
239 protected:
240 
241  octave_function (const std::string& nm,
242  const std::string& ds = "")
243  : m_relative (false), m_locked (false), m_private (false),
244  m_dispatch_class (), m_name (nm), m_dir_name (), m_doc (ds) { }
245 
246  // TRUE if this function was found from a m_relative path element.
248 
249  // TRUE if this function is tagged so that it can't be cleared.
250  bool m_locked;
251 
252  // TRUE means this is a private function.
253  bool m_private;
254 
255  // If this object is a class method or constructor, or a private
256  // function inside a class directory, this is the name of the class
257  // to which the method belongs.
258  std::string m_dispatch_class;
259 
260  // If this function is part of a package, this is the full name
261  // of the package to which the function belongs.
262  std::string m_package_name;
263 
264  // The name of this function.
265  std::string m_name;
266 
267  // The name of the directory in the path where we found this
268  // function. May be m_relative.
269  std::string m_dir_name;
270 
271  // The help text for this function.
272  std::string m_doc;
273 };
274 
275 #endif
virtual bool handles_dispatch_class(const std::string &) const
Definition: ov-fcn.h:143
bool m_private
Definition: ov-fcn.h:253
virtual bool is_anonymous_function_of_class(const std::string &="") const
Definition: ov-fcn.h:167
virtual octave::symbol_scope scope()
Definition: ov-fcn.h:92
virtual void unlock_subfunctions()
Definition: ov-fcn.h:190
octave_function(const std::string &nm, const std::string &ds="")
Definition: ov-fcn.h:241
virtual std::list< std::string > parent_fcn_names() const
Definition: ov-fcn.h:87
virtual bool is_nested_function() const
Definition: ov-fcn.h:102
virtual bool is_legacy_constructor(const std::string &="") const
Definition: ov-fcn.h:121
virtual bool is_subfunction() const
Definition: ov-fcn.h:106
std::string dispatch_class() const
Definition: ov-fcn.h:148
virtual octave_value_list execute(octave::tree_evaluator &tw, int nargout=0, const octave_value_list &args=octave_value_list())=0
bool islocked() const
Definition: ov-fcn.h:186
virtual std::string profiler_name() const
Definition: ov-fcn.h:80
std::string canonical_name() const
Definition: ov-fcn.h:208
std::string package_name() const
Definition: ov-fcn.h:152
virtual octave::symbol_scope parent_fcn_scope() const
Definition: ov-fcn.h:84
bool m_locked
Definition: ov-fcn.h:250
bool is_class_method(const std::string &cname="") const
Definition: ov-fcn.h:115
virtual bool takes_var_return() const
Definition: ov-fcn.h:136
virtual bool takes_varargs() const
Definition: ov-fcn.h:134
bool is_class_constructor(const std::string &cname="") const
Definition: ov-fcn.h:110
virtual std::string doc_string(const std::string &="") const
Definition: ov-fcn.h:219
bool is_private_function() const
Definition: ov-fcn.h:161
bool is_defined() const
Definition: ov-fcn.h:69
virtual octave::sys::time time_checked() const
Definition: ov-fcn.h:97
std::string m_dir_name
Definition: ov-fcn.h:269
virtual std::string parent_fcn_name() const
Definition: ov-fcn.h:82
void unlock()
Definition: ov-fcn.h:180
virtual void push_dispatch_class(const std::string &)
Definition: ov-fcn.h:141
virtual bool is_classdef_constructor(const std::string &="") const
Definition: ov-fcn.h:125
virtual void maybe_relocate_end()
Definition: ov-fcn.h:192
virtual bool accepts_postfix_index(char type) const
Definition: ov-fcn.h:225
virtual std::string src_file_name() const
Definition: ov-fcn.h:77
~octave_function()=default
void document(const std::string &ds)
Definition: ov-fcn.h:216
std::string m_dispatch_class
Definition: ov-fcn.h:258
void stash_dir_name(const std::string &dir)
Definition: ov-fcn.h:172
virtual bool is_legacy_method(const std::string &="") const
Definition: ov-fcn.h:128
virtual bool is_classdef_method(const std::string &="") const
Definition: ov-fcn.h:131
virtual void lock_subfunctions()
Definition: ov-fcn.h:188
virtual bool is_parent_function() const
Definition: ov-fcn.h:104
void lock()
Definition: ov-fcn.h:174
virtual int call_depth() const
Definition: ov-fcn.h:100
bool m_relative
Definition: ov-fcn.h:247
virtual bool has_subfunctions() const
Definition: ov-fcn.h:195
std::string m_name
Definition: ov-fcn.h:265
std::string m_package_name
Definition: ov-fcn.h:262
virtual void mark_fcn_file_up_to_date(const octave::sys::time &)
Definition: ov-fcn.h:90
bool is_relative() const
Definition: ov-fcn.h:204
virtual octave::sys::time time_parsed() const
Definition: ov-fcn.h:94
virtual bool is_compiled() const
Definition: ov-fcn.h:108
void stash_dispatch_class(const std::string &nm)
Definition: ov-fcn.h:146
bool is_private_function_of_class(const std::string &nm) const
Definition: ov-fcn.h:163
octave_function()
Definition: ov-fcn.h:57
std::string m_doc
Definition: ov-fcn.h:272
bool is_function() const
Definition: ov-fcn.h:71
virtual std::string fcn_file_name() const
Definition: ov-fcn.h:75
virtual void mark_as_private_function(const std::string &cname="")
Definition: ov-fcn.h:155
virtual void unload()
Definition: ov-fcn.h:221
virtual bool is_system_fcn_file() const
Definition: ov-fcn.h:73
std::string name() const
Definition: ov-fcn.h:206
virtual std::list< std::string > subfunction_names() const
Definition: ov-fcn.h:199
std::string dir_name() const
Definition: ov-fcn.h:170
void stash_package_name(const std::string &pack)
Definition: ov-fcn.h:150
virtual void accept(octave::tree_walker &)
Definition: ov-fcn.h:223
void mark_relative()
Definition: ov-fcn.h:202
virtual void stash_subfunction_names(const std::list< std::string > &)
Definition: ov-fcn.h:197
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn