GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cdef-utils.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "cdef-class.h"
31 #include "cdef-manager.h"
32 #include "cdef-method.h"
33 #include "cdef-package.h"
34 #include "cdef-property.h"
35 #include "cdef-utils.h"
36 #include "interpreter-private.h"
37 #include "ov-classdef.h"
38 #include "ov-usr-fcn.h"
39 #include "pt-eval.h"
40 
42 
43 std::string
44 get_base_name (const std::string& nm)
45 {
46  std::string::size_type pos = nm.find_last_of ('.');
47 
48  if (pos != std::string::npos)
49  return nm.substr (pos + 1);
50 
51  return nm;
52 }
53 
54 void
55 make_function_of_class (const std::string& class_name,
56  const octave_value& fcn)
57 {
58  octave_function *of = fcn.function_value ();
59 
60  of->stash_dispatch_class (class_name);
61 
63 
64  if (uf)
65  {
66  if (get_base_name (class_name) == uf->name ())
68  else
70  }
71 }
72 
73 void
75 {
76  make_function_of_class (cls.get_name (), fcn);
77 }
78 
80 lookup_class (const std::string& name, bool error_if_not_found,
81  bool load_if_not_found)
82 {
84 
85  return cdm.find_class (name, error_if_not_found, load_if_not_found);
86 }
87 
90 {
91  // FIXME: placeholder for the time being, the purpose
92  // is to centralized any class update activity here.
93 
94  return cls;
95 }
96 
99 {
100  if (ov.is_string())
101  return lookup_class (ov.string_value ());
102  else
103  {
104  cdef_class cls (to_cdef (ov));
105 
106  return lookup_class (cls);
107  }
108 
109  return cdef_class ();
110 }
111 
112 std::list<cdef_class>
113 lookup_classes (const Cell& cls_list)
114 {
115  std::list<cdef_class> retval;
116 
117  for (int i = 0; i < cls_list.numel (); i++)
118  {
119  cdef_class c = lookup_class (cls_list(i));
120 
121  retval.push_back (c);
122  }
123 
124  return retval;
125 }
126 
128 to_ov (const cdef_object& obj)
129 {
130  if (obj.ok ())
131  return octave_value (new octave_classdef (obj));
132  else
133  return octave_value (Matrix ());
134 }
135 
137 to_ov (const octave_value& ov)
138 {
139  return ov;
140 }
141 
143 to_cdef (const octave_value& val)
144 {
145  if (val.type_name () != "object")
146  error ("cannot convert '%s' into 'object'", val.type_name().c_str ());
147 
148  return dynamic_cast<octave_classdef *> (val.internal_rep ())->get_object ();
149 }
150 
153 {
154  if (val.type_name () != "object")
155  error ("cannot convert '%s' into 'object'", val.type_name().c_str ());
156 
157  return dynamic_cast<octave_classdef *> (val.internal_rep ())->get_object_ref ();
158 }
159 
161 to_cdef (const cdef_object& obj)
162 {
163  return obj;
164 }
165 
167 to_ov (const std::list<cdef_class>& class_list)
168 {
169  Cell cls (class_list.size (), 1);
170  int i = 0;
171 
172  for (const auto& cdef_cls : class_list)
173  cls(i++) = to_ov (cdef_cls);
174 
175  return octave_value (cls);
176 }
177 
178 bool
180 {
181  bool retval = false;
182 
183  if (fcn.is_defined ())
184  {
185  if (fcn.is_user_function ())
186  {
187  octave_user_function *uf = fcn.user_function_value (true);
188 
189  if (! uf || ! uf->body ())
190  retval = true;
191  }
192  }
193  else
194  retval = true;
195 
196  return retval;
197 }
198 
199 bool
200 is_superclass (const cdef_class& clsa, const cdef_class& clsb,
201  bool allow_equal, int max_depth)
202 {
203  bool retval = false;
204 
205  if (allow_equal && clsa == clsb)
206  retval = true;
207  else if (max_depth != 0)
208  {
209  Cell c = clsb.get ("SuperClasses").cell_value ();
210 
211  for (int i = 0; ! retval && i < c.numel (); i++)
212  {
213  octave_classdef *metacls = c(i).classdef_object_value ();
214  std::string clsname = metacls->get_property (0, "Name").string_value ();
215  cdef_class cls = lookup_class (clsname);
216 
217  retval = is_superclass (clsa, cls, true,
218  max_depth < 0 ? max_depth : max_depth-1);
219  }
220  }
221 
222  return retval;
223 }
224 
225 bool
226 is_strict_superclass (const cdef_class& clsa, const cdef_class& clsb)
227 {
228  return is_superclass (clsa, clsb, false);
229 }
230 
231 bool
232 is_direct_superclass (const cdef_class& clsa, const cdef_class& clsb)
233 {
234  return is_superclass (clsa, clsb, false, 1);
235 }
236 
238 lookup_package (const std::string& name, bool error_if_not_found,
239  bool load_if_not_found)
240 {
242 
243  return cdm.find_package (name, error_if_not_found, load_if_not_found);
244 }
245 
247 get_class_context (std::string& name, bool& in_constructor)
248 {
249  name = "";
250  in_constructor = false;
251 
252  cdef_class cls;
253 
254  // If the dispatch class is set in the current stack frame it
255  // overrides whatever dispatch class there is for the currently
256  // executing function so that function handles returned from class
257  // methods will use the dispatch class of the class in which they
258  // are defined instead of the class in which they are executing.
259 
261 
262  std::string dispatch_class = tw.get_dispatch_class ();
263 
264  if (! dispatch_class.empty ())
265  return lookup_class (dispatch_class);
266 
267  octave_function *fcn = tw.current_function ();
268 
269  if (fcn && (fcn->is_class_method ()
270  || fcn->is_classdef_constructor ()
272  || (fcn->is_private_function ()
273  && ! fcn->dispatch_class ().empty ())))
274  {
275  cls = lookup_class (fcn->dispatch_class ());
276 
277  name = fcn->name ();
278  in_constructor = fcn->is_classdef_constructor ();
279  }
280 
281  return cls;
282 }
283 
286 {
287  std::string dummy_string;
288  bool dummy_bool;
289 
290  return get_class_context (dummy_string, dummy_bool);
291 }
292 
293 bool
294 check_access (const cdef_class& cls, const octave_value& acc,
295  const std::string& meth_name, const std::string& prop_name,
296  bool is_prop_set)
297 {
298  if (acc.is_string ())
299  {
300  std::string acc_s = acc.string_value ();
301 
302  if (acc_s == "public")
303  return true;
304 
305  cdef_class ctx = get_class_context ();
306 
307  // The access is private or protected, this requires a
308  // valid class context.
309 
310  if (ctx.ok ())
311  {
312  if (acc_s == "private")
313  return (ctx == cls);
314  else if (acc_s == "protected")
315  {
316  if (is_superclass (cls, ctx))
317  // Calling a protected method in a superclass.
318  return true;
319  else if (is_strict_superclass (ctx, cls))
320  {
321  // Calling a protected method or property in a derived
322  // class. This is only allowed if the context class knows
323  // about it and has access to it.
324 
325  if (! meth_name.empty ())
326  {
327  cdef_method m = ctx.find_method (meth_name);
328 
329  if (m.ok ())
330  return check_access (ctx,
331  m.get ("Access"), meth_name);
332 
333  return false;
334  }
335  else if (! prop_name.empty ())
336  {
337  cdef_property p = ctx.find_property (prop_name);
338 
339  if (p.ok ())
340  {
341  octave_value p_access = p.get (is_prop_set ?
342  "SetAccess" :
343  "GetAccess");
344 
345  return check_access (ctx, p_access, meth_name,
346  prop_name, is_prop_set);
347  }
348 
349  return false;
350  }
351  else
352  panic_impossible ();
353  }
354 
355  return false;
356  }
357  else
358  panic_impossible ();
359  }
360  }
361  else if (acc.isobject ())
362  {
363  cdef_class ctx = get_class_context ();
364 
365  // At this point, a class context is always required.
366  if (ctx.ok ())
367  {
368  if (ctx == cls)
369  return true;
370 
371  cdef_class acc_cls (to_cdef (acc));
372 
373  if (is_superclass (acc_cls, ctx))
374  return true;
375  }
376  }
377  else if (acc.iscell ())
378  {
379  Cell acc_c = acc.cell_value ();
380 
381  cdef_class ctx = get_class_context ();
382 
383  // At this point, a class context is always required.
384 
385  if (ctx.ok ())
386  {
387  if (ctx == cls)
388  return true;
389 
390  for (int i = 0; i < acc.numel (); i++)
391  {
392  cdef_class acc_cls (to_cdef (acc_c(i)));
393 
394  if (is_superclass (acc_cls, ctx))
395  return true;
396  }
397  }
398  }
399  else
400  error ("invalid property/method access in class '%s'",
401  cls.get_name ().c_str ());
402 
403  return false;
404 }
405 
406 OCTAVE_END_NAMESPACE(octave)
bool is_superclass(const cdef_class &clsa, const cdef_class &clsb, bool allow_equal, int max_depth)
Definition: cdef-utils.cc:200
bool check_access(const cdef_class &cls, const octave_value &acc, const std::string &meth_name, const std::string &prop_name, bool is_prop_set)
Definition: cdef-utils.cc:294
bool is_strict_superclass(const cdef_class &clsa, const cdef_class &clsb)
Definition: cdef-utils.cc:226
bool is_dummy_method(const octave_value &fcn)
Definition: cdef-utils.cc:179
cdef_object to_cdef(const octave_value &val)
Definition: cdef-utils.cc:143
std::list< cdef_class > lookup_classes(const Cell &cls_list)
Definition: cdef-utils.cc:113
cdef_object & to_cdef_ref(const octave_value &val)
Definition: cdef-utils.cc:152
cdef_class get_class_context(std::string &name, bool &in_constructor)
Definition: cdef-utils.cc:247
cdef_class lookup_class(const std::string &name, bool error_if_not_found, bool load_if_not_found)
Definition: cdef-utils.cc:80
std::string get_base_name(const std::string &nm)
Definition: cdef-utils.cc:44
octave_value to_ov(const cdef_object &obj)
Definition: cdef-utils.cc:128
cdef_package lookup_package(const std::string &name, bool error_if_not_found, bool load_if_not_found)
Definition: cdef-utils.cc:238
void make_function_of_class(const std::string &class_name, const octave_value &fcn)
Definition: cdef-utils.cc:55
bool is_direct_superclass(const cdef_class &clsa, const cdef_class &clsb)
Definition: cdef-utils.cc:232
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
Definition: Cell.h:43
Definition: dMatrix.h:42
cdef_method find_method(const std::string &nm, bool local=false)
Definition: cdef-class.h:457
std::string get_name() const
Definition: cdef-class.h:318
cdef_property find_property(const std::string &nm)
Definition: cdef-class.h:463
cdef_package find_package(const std::string &name, bool error_if_not_found=true, bool load_if_not_found=true)
cdef_class find_class(const std::string &name, bool error_if_not_found=true, bool load_if_not_found=true)
bool ok() const
Definition: cdef-object.h:312
octave_value get(const std::string &pname) const
Definition: cdef-object.h:268
virtual octave_user_function * user_function_value(bool silent=false)
Definition: ov-base.cc:949
octave_value get_property(octave_idx_type idx, const std::string &name) const
Definition: ov-classdef.h:138
virtual bool is_anonymous_function_of_class(const std::string &="") const
Definition: ov-fcn.h:167
std::string dispatch_class() const
Definition: ov-fcn.h:148
bool is_class_method(const std::string &cname="") const
Definition: ov-fcn.h:115
bool is_private_function() const
Definition: ov-fcn.h:161
virtual bool is_classdef_constructor(const std::string &="") const
Definition: ov-fcn.h:125
void stash_dispatch_class(const std::string &nm)
Definition: ov-fcn.h:146
std::string name() const
Definition: ov-fcn.h:206
octave::tree_statement_list * body()
Definition: ov-usr-fcn.h:118
void mark_as_classdef_method()
Definition: ov-usr-fcn.h:363
void mark_as_classdef_constructor()
Definition: ov-usr-fcn.h:347
octave_base_value * internal_rep() const
Definition: ov.h:1387
Cell cell_value() const
octave_user_function * user_function_value(bool silent=false) const
bool is_string() const
Definition: ov.h:637
bool is_defined() const
Definition: ov.h:592
octave_function * function_value(bool silent=false) const
bool iscell() const
Definition: ov.h:604
octave_idx_type numel() const
Definition: ov.h:559
std::string string_value(bool force=false) const
Definition: ov.h:974
bool is_user_function() const
Definition: ov.h:783
std::string type_name() const
Definition: ov.h:1345
bool isobject() const
Definition: ov.h:664
std::string get_dispatch_class() const
Definition: pt-eval.cc:2597
octave_function * current_function(bool skip_first=false) const
Definition: pt-eval.cc:2561
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
#define panic_impossible()
Definition: error.h:503
cdef_manager & __get_cdef_manager__()
tree_evaluator & __get_evaluator__()
T octave_idx_type m
Definition: mx-inlines.cc:781
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))