26#if defined (HAVE_CONFIG_H)
66make_fcn_handle (
const octave_value& fcn,
const std::string& meth_name,
67 const std::string& class_name)
83cdef_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;
92cdef_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;
230cdef_class::cdef_class_rep::install_method (
const cdef_method& meth)
232 std::string method_name = meth.
get_name ();
233 auto it = m_method_map.find (method_name);
236 if (it != m_method_map.end ())
238 std::string class_file = file_name ();
239 error (
"duplicate method '%s' in class '%s' in file '%s'",
240 method_name.c_str (), get_name ().c_str (), class_file.c_str ());
244 m_method_map[method_name] = meth;
264 if (! ret_list || ret_list->size () != 1)
265 error (
"%s: invalid constructor output arguments",
268 std::string m_obj_name = ret_list->front ()->name ();
269 ctor_analyzer a (meth.
get_name (), m_obj_name);
273 std::list<cdef_class> explicit_ctor_list
274 = a.get_constructor_list ();
276 for (
const auto& cdef_cls : explicit_ctor_list)
279 std::cerr <<
"explicit superclass constructor: "
280 << cdef_cls.get_name () << std::endl;
283 m_implicit_ctor_list.remove (cdef_cls);
291cdef_class::cdef_class_rep::load_all_methods ()
297cdef_class::cdef_class_rep::get_methods (
bool include_ctor)
299 std::map<std::string, cdef_method> meths;
301 find_methods (meths,
false, include_ctor);
303 Cell c (meths.size (), 1);
307 for (
const auto& nm_mthd : meths)
308 c(idx++, 0) =
to_ov (nm_mthd.second);
313std::map<std::string, cdef_method>
314cdef_class::cdef_class_rep::get_method_map (
bool only_inherited,
317 std::map<std::string, cdef_method> methods;
319 find_methods (methods, only_inherited, include_ctor);
325cdef_class::cdef_class_rep::find_methods (std::map<std::string,
332 for (
const auto& it : m_method_map)
334 if (include_ctor || ! it.second.is_constructor ())
336 std::string nm = it.second.get_name ();
338 if (meths.find (nm) == meths.end ())
349 meths[nm] = it.second;
356 Cell super_classes = get (
"SuperClasses").cell_value ();
358 for (
int i = 0; i < super_classes.
numel (); i++)
362 cls.get_rep ()->find_methods (meths,
true,
false);
367cdef_class::cdef_class_rep::find_property (
const std::string& nm)
369 auto it = m_property_map.find (nm);
371 if (it != m_property_map.end ())
381 Cell super_classes = get (
"SuperClasses").cell_value ();
383 for (
int i = 0; i < super_classes.
numel (); i++)
397cdef_class::cdef_class_rep::install_property (
const cdef_property& prop)
399 std::string prop_name = prop.
get_name ();
400 auto it = m_property_map.find (prop_name);
403 if (it != m_property_map.end ())
405 std::string class_file = file_name ();
406 error (
"duplicate property '%s' in class '%s' in file '%s'",
407 prop_name.c_str (), get_name ().c_str (), class_file.c_str ());
410 m_property_map[prop_name] = prop;
413 m_property_rank_map[prop.
get_name ()] = m_member_count;
419cdef_class::cdef_class_rep::get_properties (
int mode)
421 std::map<property_key, cdef_property> props;
423 props = get_property_map (mode);
425 Cell c (props.size (), 1);
429 for (
const auto& pname_prop : props)
430 c(idx++, 0) =
to_ov (pname_prop.second);
435std::map<property_key, cdef_property>
436cdef_class::cdef_class_rep::get_property_map (
int mode)
438 std::map<property_key, cdef_property> props;
440 find_properties (props, mode);
446cdef_class::cdef_class_rep::find_properties
447 (std::map<property_key, cdef_property>& props,
int mode)
449 std::set<std::string> prop_names;
455 find_properties_aux (props, prop_names, mode);
459cdef_class::cdef_class_rep::find_properties_aux
460 (std::map<property_key, cdef_property>& props,
461 std::set<std::string>& prop_names,
int mode)
465 static unsigned int property_offset {0};
467 for (
const auto& it : m_property_map)
469 std::string nm = it.second.get_name ();
471 if (prop_names.find (nm) == prop_names.end ())
473 if (mode == property_inherited)
483 std::make_pair (property_offset + m_property_rank_map[nm], nm);
484 props[pk] = it.second;
485 prop_names.insert (nm);
489 property_offset += m_member_count;
493 Cell super_classes = get (
"SuperClasses").cell_value ();
495 for (
int i = 0; i < super_classes.
numel (); i++)
499 cls.get_rep ()->find_properties_aux (props, prop_names,
500 (mode == property_all
502 : property_inherited));
509cdef_class::cdef_class_rep::find_names (std::set<std::string>& names,
514 for (
const auto& cls_fnmap : m_method_map)
516 if (! cls_fnmap.second.is_constructor ())
518 std::string nm = cls_fnmap.second.get_name ();
533 for (
const auto& pname_prop : m_property_map)
535 std::string nm = pname_prop.second.get_name ();
551 Cell super_classes = get (
"SuperClasses").cell_value ();
553 for (
int i = 0; i < super_classes.
numel (); i++)
557 cls.get_rep ()->find_names (names, all);
562cdef_class::cdef_class_rep::get_names ()
564 std::set<std::string> names;
566 find_names (names,
false);
570 return v.sort (
true);
574cdef_class::cdef_class_rep::delete_object (
const cdef_object& obj)
588 Cell super_classes = get (
"SuperClasses").cell_value ();
590 for (
int i = 0; i < super_classes.
numel (); i++)
600cdef_class::cdef_class_rep::meta_subsref (
const std::string& type,
601 const std::list<octave_value_list>& idx,
604 std::size_t skip = 1;
614 std::cerr <<
"constructor" << std::endl;
617 retval(0) = construct (idx.front ());
625 std::cerr <<
"static method/property" << std::endl;
628 if (idx.front ().length () != 1)
629 error (
"invalid meta.class indexing");
631 std::string nm = idx.front ()(0).xstring_value (
"invalid meta.class indexing, expected a method or property name");
638 error (
"method '%s' is not static", nm.c_str ());
642 if (type.length () > 1 && idx.size () > 1 && type[1] ==
'(')
644 args = *(++(idx.begin ()));
648 retval = meth.
execute (args, (type.length () > skip
649 ? 1 : nargout),
true,
657 error (
"no such method or property '%s'", nm.c_str ());
660 error (
"property '%s' is not constant", nm.c_str ());
662 retval(0) = prop.
get_value (
true,
"meta.class");
668 error (
"invalid meta.class indexing");
672 if (type.length () > skip && idx.size () > skip && ! retval.
empty ())
673 retval = retval(0).next_subsref (nargout, type, idx, skip);
679cdef_class::cdef_class_rep::meta_release ()
687cdef_class::cdef_class_rep::initialize_object (
cdef_object& obj)
691 std::list<cdef_class> super_classes
694 for (
auto& cls : super_classes)
695 cls.initialize_object (obj);
697 for (
const auto& pname_prop : m_property_map)
699 if (! pname_prop.second.get (
"Dependent").bool_value ())
701 octave_value pvalue = pname_prop.second.get (
"DefaultValue");
704 obj.
put (pname_prop.first, pvalue);
715cdef_class::cdef_class_rep::run_constructor (
cdef_object& obj,
720 for (
const auto& cls : m_implicit_ctor_list)
727 std::string cls_name = get_name ();
738 ctor_retval = ctor.
execute (ctor_args, 1,
true,
"constructor");
740 if (ctor_retval.
length () != 1)
741 error (
"%s: invalid number of output arguments for classdef constructor",
744 obj =
to_cdef (ctor_retval(0));
751cdef_class::cdef_class_rep::get_method (
const std::string& name)
const
753 auto p = m_method_map.find (name);
755 if (p == m_method_map.end ())
758 return p->second.get_function ();
762cdef_class::cdef_class_rep::get_method (
int line)
const
765 int closest_match_end_line = std::numeric_limits<int>::max ();
769 for (
auto i = m_method_map.cbegin (); i != m_method_map.cend (); ++i)
783 octave::filepos end_pos = pfcn->
end_pos ();
785 int end_line = end_pos.line ();
787 if (line <= end_line && end_line <= closest_match_end_line
791 closest_match_end_line = end_line;
796 for (
auto i = m_property_map.cbegin (); i != m_property_map.cend (); ++i)
798 const octave_value& get_meth = i->second.get (
"GetMethod");
807 octave::filepos end_pos = pfcn->
end_pos ();
809 int end_line = end_pos.line ();
811 if (line <= end_line && end_line <= closest_match_end_line
814 closest_match = get_meth;
815 closest_match_end_line = end_line;
819 const octave_value& set_meth = i->second.get (
"SetMethod");
828 octave::filepos end_pos = pfcn->
end_pos ();
830 int end_line = end_pos.line ();
832 if (line <= end_line && end_line <= closest_match_end_line
835 closest_match = set_meth;
836 closest_match_end_line = end_line;
841 return closest_match;
859 error (
"cannot instantiate object for abstract class '%s'",
860 get_name ().c_str ());
864 if (is_meta_class ())
877 if (! empty_class.
ok ())
878 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
885 if (! empty_class.
ok ())
886 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
887 if (! empty_property.
ok ())
889 obj = empty_property;
895 if (! empty_class.
ok ())
896 empty_class = cdm.
make_class (
"", std::list<cdef_class> ());
897 if (! empty_method.
ok ())
905 if (! empty_package.
ok ())
910 error (
"expecting meta class, property, method, or package in cdef_class::cdef_class_rep::construct_object - please report this bug");
916 if (is_handle_class ())
922 initialize_object (obj);
924 run_constructor (obj, args);
942 std::string s = expr->
name ();
945 return std::string (
"public");
946 else if (s ==
"protected")
947 return std::string (
"protected");
948 else if (s ==
"private")
949 return std::string (
"private");
964 else if (t->expression ())
965 return t->expression ()->original_text ();
981 full_class_name = t->
package_name () +
'.' + full_class_name;
984 std::cerr <<
"class: " << full_class_name << std::endl;
997 std::list<cdef_class> slist;
1003 std::string sclass_name = (scls)->
class_name ();
1006 std::cerr <<
"superclass: " << sclass_name << std::endl;
1012 error (
"'%s' cannot inherit from '%s', because it is sealed",
1013 full_class_name.c_str (), sclass_name.c_str ());
1015 slist.push_back (sclass);
1021 retval = cdm.
make_class (full_class_name, slist);
1033 retval.
put (
"ContainingPackage",
to_ov (pack));
1047 std::string aname = attr->ident ()->name ();
1048 octave_value avalue = compute_attribute_value (tw, attr);
1051 std::cerr <<
"class attribute: " << aname <<
" = "
1052 << attribute_value_to_string (attr, avalue) << std::endl;
1055 retval.
put (aname, avalue);
1067 std::map<std::string, octave_value> set_methods;
1071 std::list<tree_classdef_methods_block *> mb_list = b->
method_list ();
1075 for (
auto& mb_p : mb_list)
1077 std::map<std::string, octave_value> amap;
1080 std::cerr <<
"method block" << std::endl;
1085 if (mb_p->attribute_list ())
1087 for (
auto& attr_p : *mb_p->attribute_list ())
1089 std::string aname = attr_p->ident ()->name ();
1090 octave_value avalue = compute_attribute_value (tw, attr_p);
1093 std::cerr <<
"method attribute: " << aname <<
" = "
1094 << attribute_value_to_string (attr_p, avalue)
1098 amap[aname] = avalue;
1104 if (mb_p->element_list ())
1106 for (
auto& mtd : *mb_p->element_list ())
1109 std::string mprefix = mname.substr (0, 4);
1111 if (mprefix ==
"get.")
1113 = make_fcn_handle (mtd, mname, full_class_name);
1114 else if (mprefix ==
"set.")
1115 set_methods[mname.substr (4)]
1116 = make_fcn_handle (mtd, mname, full_class_name);
1122 std::cerr << (mname ==
class_name ?
"constructor"
1124 <<
": " << mname << std::endl;
1134 for (
auto& attrnm_val : amap)
1135 meth.
put (attrnm_val.first, attrnm_val.second);
1152 std::list<std::string> external_methods
1153 = lp.
methods (full_class_name);
1155 for (
const auto& mtdnm : external_methods)
1184 std::list<tree_classdef_properties_block *> pb_list
1187 for (
auto& pb_p : pb_list)
1189 std::map<std::string, octave_value> amap;
1192 std::cerr <<
"property block" << std::endl;
1197 if (pb_p->attribute_list ())
1199 for (
auto& attr_p : *pb_p->attribute_list ())
1201 std::string aname = attr_p->ident ()->name ();
1202 octave_value avalue = compute_attribute_value (tw, attr_p);
1205 std::cerr <<
"property attribute: " << aname <<
" = "
1206 << attribute_value_to_string (attr_p, avalue)
1210 if (aname ==
"Access")
1212 amap[
"GetAccess"] = avalue;
1213 amap[
"SetAccess"] = avalue;
1216 amap[aname] = avalue;
1222 if (pb_p->element_list ())
1224 for (
auto& prop_p : *pb_p->element_list ())
1226 std::string prop_name = prop_p->ident ()->name ();
1233 std::cerr <<
"property: " << prop_p->ident ()->name ()
1243 std::cerr <<
"property default: "
1244 << attribute_value_to_string (prop_p, pvalue)
1248 prop.
put (
"DefaultValue", pvalue);
1261 for (
auto& attrnm_val : amap)
1262 prop.
put (attrnm_val.first, attrnm_val.second);
1274 prop.
put (
"GetMethod", git->second);
1278 auto sit = set_methods.
find (prop_name);
1280 if (sit != set_methods.end ())
1283 prop.
put (
"SetMethod", sit->second);
1284 set_methods.erase (sit);
1302OCTAVE_END_NAMESPACE(octave)
std::pair< unsigned int, std::string > property_key
cdef_object to_cdef(const octave_value &val)
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)
std::list< cdef_class > lookup_classes(const Cell &cls_list)
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 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.
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_package() const
cdef_package make_package(const std::string &nm, const std::string &parent="")
const cdef_class & meta_property() const
cdef_package find_package(const std::string &name, bool error_if_not_found=true, bool load_if_not_found=true)
const cdef_class & meta_method() const
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)
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
load_path & get_load_path()
tree_evaluator & get_evaluator()
std::list< std::string > methods(const std::string &class_name, const std::string &pack_name="")
octave::filepos end_pos() const
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 is_function_handle() const
bool bool_value(bool warn=false) const
octave_function * function_value(bool silent=false) const
std::string string_value(bool force=false) const
octave_user_function * user_function_value(bool silent=false) const
octave_user_code * user_code_value(bool silent=false) const
tree_expression * expression()
std::list< tree_classdef_properties_block * > property_list()
std::list< tree_classdef_methods_block * > method_list()
std::string package_name() const
tree_classdef_body * body()
tree_identifier * ident()
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)
tree_expression * expression()
bool is_expression() const
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,...)
cdef_manager & __get_cdef_manager__()