26 #if defined (HAVE_CONFIG_H)
66 make_fcn_handle (
const octave_value& fcn,
const std::string& meth_name,
67 const std::string& class_name)
83 cdef_class::cdef_class_rep::cdef_class_rep (
const std::list<cdef_class>& superclasses)
87 put (
"SuperClasses",
to_ov (superclasses));
88 m_implicit_ctor_list = superclasses;
92 cdef_class::cdef_class_rep::find_method (
const std::string& nm,
bool local)
94 auto it = m_method_map.find (nm);
96 if (it == m_method_map.end ())
114 Cell super_classes = get (
"SuperClasses").cell_value ();
116 for (
int i = 0; i < super_classes.
numel (); i++)
134 ctor_analyzer () =
delete;
136 ctor_analyzer (
const std::string& ctor,
const std::string& obj)
137 :
tree_walker (), m_who (ctor), m_obj_name (obj) { }
139 OCTAVE_DISABLE_COPY_MOVE (ctor_analyzer)
141 ~ctor_analyzer () =
default;
164 std::list<cdef_class> get_constructor_list ()
const
165 {
return m_ctor_list; }
213 m_ctor_list.push_back (cls);
223 std::string m_obj_name;
226 std::list<cdef_class> m_ctor_list;
230 cdef_class::cdef_class_rep::install_method (
const cdef_method& meth)
232 m_method_map[meth.
get_name ()] = meth;
252 if (! ret_list || ret_list->
size () != 1)
253 error (
"%s: invalid constructor output arguments",
256 std::string m_obj_name = ret_list->
front ()->
name ();
257 ctor_analyzer a (meth.
get_name (), m_obj_name);
261 std::list<cdef_class> explicit_ctor_list
262 = a.get_constructor_list ();
264 for (
const auto& cdef_cls : explicit_ctor_list)
267 std::cerr <<
"explicit superclass constructor: "
268 << cdef_cls.get_name () << std::endl;
271 m_implicit_ctor_list.remove (cdef_cls);
279 cdef_class::cdef_class_rep::load_all_methods ()
285 cdef_class::cdef_class_rep::get_methods (
bool include_ctor)
287 std::map<std::string, cdef_method> meths;
289 find_methods (meths,
false, include_ctor);
291 Cell c (meths.size (), 1);
295 for (
const auto& nm_mthd : meths)
296 c(idx++, 0) =
to_ov (nm_mthd.second);
301 std::map<std::string, cdef_method>
302 cdef_class::cdef_class_rep::get_method_map (
bool only_inherited,
305 std::map<std::string, cdef_method> methods;
307 find_methods (methods, only_inherited, include_ctor);
313 cdef_class::cdef_class_rep::find_methods (std::map<std::string,
320 for (
const auto& it : m_method_map)
322 if (include_ctor || ! it.second.is_constructor ())
324 std::string nm = it.second.get_name ();
326 if (meths.find (nm) == meths.end ())
337 meths[nm] = it.second;
344 Cell super_classes = get (
"SuperClasses").cell_value ();
346 for (
int i = 0; i < super_classes.
numel (); i++)
350 cls.get_rep ()->find_methods (meths,
true,
false);
355 cdef_class::cdef_class_rep::find_property (
const std::string& nm)
357 auto it = m_property_map.find (nm);
359 if (it != m_property_map.end ())
369 Cell super_classes = get (
"SuperClasses").cell_value ();
371 for (
int i = 0; i < super_classes.
numel (); i++)
385 cdef_class::cdef_class_rep::install_property (
const cdef_property& prop)
387 m_property_map[prop.
get_name ()] = prop;
393 cdef_class::cdef_class_rep::get_properties (
int mode)
395 std::map<std::string, cdef_property> props;
397 props = get_property_map (mode);
399 Cell c (props.size (), 1);
403 for (
const auto& pname_prop : props)
404 c(idx++, 0) =
to_ov (pname_prop.second);
409 std::map<std::string, cdef_property>
410 cdef_class::cdef_class_rep::get_property_map (
int mode)
412 std::map<std::string, cdef_property> props;
414 find_properties (props, mode);
420 cdef_class::cdef_class_rep::find_properties (std::map<std::string,
424 for (
const auto& it : m_property_map)
426 std::string nm = it.second.get_name ();
428 if (props.find (nm) == props.end ())
430 if (mode == property_inherited)
439 props[nm] = it.second;
445 Cell super_classes = get (
"SuperClasses").cell_value ();
447 for (
int i = 0; i < super_classes.
numel (); i++)
451 cls.get_rep ()->find_properties (props,
452 (mode == property_all
454 : property_inherited));
459 cdef_class::cdef_class_rep::find_names (std::set<std::string>& names,
464 for (
const auto& cls_fnmap : m_method_map)
466 if (! cls_fnmap.second.is_constructor ())
468 std::string nm = cls_fnmap.second.get_name ();
483 for (
const auto& pname_prop : m_property_map)
485 std::string nm = pname_prop.second.get_name ();
501 Cell super_classes = get (
"SuperClasses").cell_value ();
503 for (
int i = 0; i < super_classes.
numel (); i++)
507 cls.get_rep ()->find_names (names, all);
512 cdef_class::cdef_class_rep::get_names ()
514 std::set<std::string> names;
516 find_names (names,
false);
520 return v.sort (
true);
524 cdef_class::cdef_class_rep::delete_object (
const cdef_object& obj)
538 Cell super_classes = get (
"SuperClasses").cell_value ();
540 for (
int i = 0; i < super_classes.
numel (); i++)
550 cdef_class::cdef_class_rep::meta_subsref (
const std::string& type,
551 const std::list<octave_value_list>& idx,
554 std::size_t skip = 1;
564 std::cerr <<
"constructor" << std::endl;
567 retval(0) = construct (idx.front ());
575 std::cerr <<
"static method/property" << std::endl;
578 if (idx.front ().length () != 1)
579 error (
"invalid meta.class indexing");
581 std::string nm = idx.front ()(0).xstring_value (
"invalid meta.class indexing, expected a method or property name");
588 error (
"method '%s' is not static", nm.c_str ());
592 if (type.length () > 1 && idx.size () > 1 && type[1] ==
'(')
594 args = *(++(idx.begin ()));
598 retval = meth.
execute (args, (type.length () > skip
599 ? 1 : nargout),
true,
607 error (
"no such method or property '%s'", nm.c_str ());
610 error (
"property '%s' is not constant", nm.c_str ());
612 retval(0) = prop.
get_value (
true,
"meta.class");
618 error (
"invalid meta.class indexing");
622 if (type.length () > skip && idx.size () > skip && ! retval.
empty ())
623 retval = retval(0).next_subsref (nargout, type, idx, skip);
629 cdef_class::cdef_class_rep::meta_release ()
637 cdef_class::cdef_class_rep::initialize_object (
cdef_object& obj)
641 std::list<cdef_class> super_classes
644 for (
auto& cls : super_classes)
647 for (
const auto& pname_prop : m_property_map)
649 if (! pname_prop.second.get (
"Dependent").bool_value ())
651 octave_value pvalue = pname_prop.second.get (
"DefaultValue");
654 obj.
put (pname_prop.first, pvalue);
665 cdef_class::cdef_class_rep::run_constructor (
cdef_object& obj,
670 for (
const auto& cls : m_implicit_ctor_list)
677 std::string cls_name = get_name ();
688 ctor_retval = ctor.
execute (ctor_args, 1,
true,
"constructor");
690 if (ctor_retval.
length () != 1)
691 error (
"%s: invalid number of output arguments for classdef constructor",
694 obj =
to_cdef (ctor_retval(0));
701 cdef_class::cdef_class_rep::get_method (
const std::string& name)
const
703 auto p = m_method_map.find (name);
705 if (p == m_method_map.end ())
708 return p->second.get_function ();
712 cdef_class::cdef_class_rep::get_method (
int line)
const
719 for (
auto i = m_method_map.cbegin (); i != m_method_map.cend (); ++i)
724 if (user_code ==
nullptr)
734 if (
line <= e && e <= closest_match_end_line && pfcn->is_defined ()
738 closest_match_end_line = e;
742 return closest_match;
760 error (
"cannot instantiate object for abstract class '%s'",
761 get_name ().c_str ());
765 if (is_meta_class ())
778 if (! empty_class.
ok ())
779 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
786 if (! empty_class.
ok ())
787 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
788 if (! empty_property.
ok ())
790 obj = empty_property;
796 if (! empty_class.
ok ())
797 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
798 if (! empty_method.
ok ())
806 if (! empty_package.
ok ())
817 if (is_handle_class ())
823 initialize_object (obj);
825 run_constructor (obj, args);
843 std::string s = expr->
name ();
846 return std::string (
"public");
847 else if (s ==
"protected")
848 return std::string (
"protected");
849 else if (s ==
"private")
850 return std::string (
"private");
859 template <
typename T>
865 else if (t->expression ())
866 return t->expression ()->original_text ();
882 full_class_name = t->
package_name () +
'.' + full_class_name;
885 std::cerr <<
"class: " << full_class_name << std::endl;
898 std::list<cdef_class> slist;
904 std::string sclass_name = (scls)->
class_name ();
907 std::cerr <<
"superclass: " << sclass_name << std::endl;
913 error (
"'%s' cannot inherit from '%s', because it is sealed",
914 full_class_name.c_str (), sclass_name.c_str ());
916 slist.push_back (sclass);
922 retval = cdm.
make_class (full_class_name, slist);
934 retval.
put (
"ContainingPackage",
to_ov (pack));
948 std::string aname = attr->ident ()->name ();
949 octave_value avalue = compute_attribute_value (tw, attr);
952 std::cerr <<
"class attribute: " << aname <<
" = "
953 << attribute_value_to_string (attr, avalue) << std::endl;
956 retval.
put (aname, avalue);
968 std::map<std::string, octave_value> set_methods;
972 std::list<tree_classdef_methods_block *> mb_list = b->
methods_list ();
976 for (
auto& mb_p : mb_list)
978 std::map<std::string, octave_value> amap;
981 std::cerr <<
"method block" << std::endl;
986 if (mb_p->attribute_list ())
988 for (
auto& attr_p : *mb_p->attribute_list ())
990 std::string aname = attr_p->ident ()->name ();
991 octave_value avalue = compute_attribute_value (tw, attr_p);
994 std::cerr <<
"method attribute: " << aname <<
" = "
995 << attribute_value_to_string (attr_p, avalue)
999 amap[aname] = avalue;
1005 if (mb_p->element_list ())
1007 for (
auto& mtd : *mb_p->element_list ())
1010 std::string mprefix = mname.substr (0, 4);
1012 if (mprefix ==
"get.")
1014 = make_fcn_handle (mtd, mname, full_class_name);
1015 else if (mprefix ==
"set.")
1016 set_methods[mname.substr (4)]
1017 = make_fcn_handle (mtd, mname, full_class_name);
1023 std::cerr << (mname ==
class_name ?
"constructor"
1025 <<
": " << mname << std::endl;
1035 for (
auto& attrnm_val : amap)
1036 meth.
put (attrnm_val.first, attrnm_val.second);
1053 std::list<std::string> external_methods
1054 = lp.
methods (full_class_name);
1056 for (
const auto& mtdnm : external_methods)
1085 std::list<tree_classdef_properties_block *> pb_list
1088 for (
auto& pb_p : pb_list)
1090 std::map<std::string, octave_value> amap;
1093 std::cerr <<
"property block" << std::endl;
1098 if (pb_p->attribute_list ())
1100 for (
auto& attr_p : *pb_p->attribute_list ())
1102 std::string aname = attr_p->ident ()->name ();
1103 octave_value avalue = compute_attribute_value (tw, attr_p);
1106 std::cerr <<
"property attribute: " << aname <<
" = "
1107 << attribute_value_to_string (attr_p, avalue)
1111 if (aname ==
"Access")
1113 amap[
"GetAccess"] = avalue;
1114 amap[
"SetAccess"] = avalue;
1117 amap[aname] = avalue;
1123 if (pb_p->element_list ())
1125 for (
auto& prop_p : *pb_p->element_list ())
1127 std::string prop_name = prop_p->ident ()->name ();
1134 std::cerr <<
"property: " << prop_p->ident ()->name ()
1144 std::cerr <<
"property default: "
1145 << attribute_value_to_string (prop_p, pvalue)
1149 prop.
put (
"DefaultValue", pvalue);
1162 for (
auto& attrnm_val : amap)
1163 prop.
put (attrnm_val.first, attrnm_val.second);
1175 prop.
put (
"GetMethod", git->second);
1179 auto sit = set_methods.
find (prop_name);
1181 if (sit != set_methods.end ())
1184 prop.
put (
"SetMethod", sit->second);
1185 set_methods.erase (sit);
1203 OCTAVE_END_NAMESPACE(
octave)
cdef_object to_cdef(const octave_value &val)
std::list< cdef_class > lookup_classes(const Cell &cls_list)
cdef_class lookup_class(const std::string &name, bool error_if_not_found, bool load_if_not_found)
std::string get_base_name(const std::string &nm)
octave_value to_ov(const cdef_object &obj)
void make_function_of_class(const std::string &class_name, const octave_value &fcn)
charNDArray max(char d, const charNDArray &m)
Array< octave_idx_type > find(octave_idx_type n=-1, bool backward=false) const
Find indices of (at most n) nonzero elements.
octave_idx_type numel() const
Number of elements in the array.
cdef_method find_method(const std::string &nm, bool local=false)
void install_property(const cdef_property &prop)
void install_method(const cdef_method &meth)
void run_constructor(cdef_object &obj, const octave_value_list &args)
std::string get_name() const
void file_name(const std::string &nm)
void initialize_object(cdef_object &obj)
void delete_object(const cdef_object &obj)
octave_value get_method_function(const std::string &nm)
cdef_property find_property(const std::string &nm)
Cell get_methods(bool include_ctor=false)
static cdef_class make_meta_class(interpreter &interp, tree_classdef *t, bool is_at_folder=false)
Analyze the tree_classdef tree and transform it to a cdef_class.
const cdef_class & meta_method() const
cdef_class make_class(const std::string &name, const std::list< cdef_class > &super_list=std::list< cdef_class >())
cdef_property make_property(const cdef_class &cls, const std::string &name, const octave_value &get_method=Matrix(), const std::string &get_access="public", const octave_value &set_method=Matrix(), const std::string &set_access="public")
const cdef_class & meta_property() const
cdef_package make_package(const std::string &nm, const std::string &parent="")
cdef_package find_package(const std::string &name, bool error_if_not_found=true, bool load_if_not_found=true)
void unregister_class(const cdef_class &cls)
const cdef_class & meta_class() const
cdef_method make_method(const cdef_class &cls, const std::string &name, const octave_value &fcn, const std::string &m_access="public", bool is_static=false)
const cdef_class & meta_package() const
bool is_constructor() const
octave_value_list execute(const octave_value_list &args, int nargout, bool do_check_access=true, const std::string &who="")
bool is_defined_in_class(const std::string &cname) const
octave_value get_function() const
std::string get_name() const
void mark_for_construction(const cdef_class &cls)
void put(const std::string &pname, const octave_value &val)
void mark_as_constructed()
void set_class(const cdef_class &cls)
std::string class_name() const
octave_value get(const std::string &pname) const
std::string get_name() const
octave_value get_value(const cdef_object &obj, bool do_check_access=true, const std::string &who="") const
tree_evaluator & get_evaluator()
load_path & get_load_path()
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
bool is_user_code() const
octave::tree_statement_list * body()
void stash_function_name(const std::string &s)
octave::tree_parameter_list * return_list()
octave_idx_type length() const
octave_value_list & prepend(const octave_value &val)
bool bool_value(bool warn=false) const
octave_user_function * user_function_value(bool silent=false) const
octave_user_code * user_code_value(bool silent=false) const
octave_function * function_value(bool silent=false) const
std::string string_value(bool force=false) const
tree_expression * expression()
std::list< tree_classdef_properties_block * > properties_list()
std::list< tree_classdef_methods_block * > methods_list()
tree_identifier * ident()
std::string package_name() const
tree_classdef_body * body()
tree_classdef_superclass_list * superclass_list()
tree_classdef_attribute_list * attribute_list()
std::string doc_string() const
std::string file_name() const
void push_dummy_scope(const std::string &name)
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual bool is_identifier() const
virtual std::string name() const
tree_expression * expression()
tree_expression * right_hand_side()
tree_expression * right_hand_side()
void accept(tree_walker &tw)
bool is_expression() const
tree_expression * expression()
std::string method_name() const
std::string class_name() const
virtual void visit_unwind_protect_command(tree_unwind_protect_command &)
virtual void visit_try_catch_command(tree_try_catch_command &)
virtual void visit_if_command_list(tree_if_command_list &)
virtual void visit_multi_assignment(tree_multi_assignment &)
virtual void visit_return_command(tree_return_command &)
virtual void visit_continue_command(tree_continue_command &)
virtual void visit_switch_case_list(tree_switch_case_list &)
virtual void visit_complex_for_command(tree_complex_for_command &)
virtual void visit_break_command(tree_break_command &)
virtual void visit_matrix(tree_matrix &)
virtual void visit_fcn_handle(tree_fcn_handle &)
virtual void visit_colon_expression(tree_colon_expression &)
virtual void visit_do_until_command(tree_do_until_command &)
virtual void visit_no_op_command(tree_no_op_command &)
virtual void visit_binary_expression(tree_binary_expression &)
virtual void visit_simple_for_command(tree_simple_for_command &)
virtual void visit_postfix_expression(tree_postfix_expression &)
virtual void visit_prefix_expression(tree_prefix_expression &)
virtual void visit_anon_fcn_handle(tree_anon_fcn_handle &)
virtual void visit_parameter_list(tree_parameter_list &)
virtual void visit_statement(tree_statement &)
virtual void visit_index_expression(tree_index_expression &)
virtual void visit_decl_command(tree_decl_command &)
virtual void visit_switch_case(tree_switch_case &)
virtual void visit_cell(tree_cell &)
virtual void visit_identifier(tree_identifier &)
virtual void visit_decl_init_list(tree_decl_init_list &)
virtual void visit_if_clause(tree_if_clause &)
virtual void visit_if_command(tree_if_command &)
virtual void visit_argument_list(tree_argument_list &)
virtual void visit_decl_elt(tree_decl_elt &)
virtual void visit_constant(tree_constant &)
virtual void visit_simple_assignment(tree_simple_assignment &)
virtual void visit_superclass_ref(tree_superclass_ref &)
virtual void visit_octave_user_script(octave_user_script &)
virtual void visit_octave_user_function(octave_user_function &)
virtual void visit_function_def(tree_function_def &)
virtual void visit_while_command(tree_while_command &)
virtual void visit_switch_command(tree_switch_command &)
virtual void accept(tree_walker &tw)=0
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
#define panic_impossible()
cdef_manager & __get_cdef_manager__()
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))