1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2012-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <algorithm>
#include <iomanip>
#include "cdef-class.h"
#include "cdef-manager.h"
#include "cdef-method.h"
#include "cdef-package.h"
#include "cdef-property.h"
#include "cdef-utils.h"
#include "errwarn.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "load-path.h"
#include "ov-builtin.h"
#include "ov-classdef.h"
#include "ov-fcn-handle.h"
#include "ov-usr-fcn.h"
#include "parse.h"
#include "pt-assign.h"
#include "pt-classdef.h"
#include "pt-eval.h"
#include "pt-idx.h"
#include "pt-misc.h"
#include "pt-stmt.h"
#include "pt-walk.h"
#include "unwind-prot.h"
// Define to 1 to enable debugging statements.
#define DEBUG_TRACE 0
#if DEBUG_TRACE
# include <iostream>
#endif
namespace octave
{
static octave_value
make_fcn_handle (const octave_value& fcn, const std::string& meth_name,
const std::string& class_name)
{
octave_value retval;
if (fcn.is_defined ())
{
// FCN_HANDLE: METHOD
octave_fcn_handle *fh
= new octave_fcn_handle (fcn, class_name, meth_name);
retval = octave_value (fh);
}
return retval;
}
cdef_class::cdef_class_rep::cdef_class_rep (const std::list<cdef_class>& superclasses)
: cdef_meta_object_rep (), member_count (0), handle_class (false),
meta (false)
{
put ("SuperClasses", to_ov (superclasses));
implicit_ctor_list = superclasses;
}
cdef_method
cdef_class::cdef_class_rep::find_method (const std::string& nm, bool local)
{
auto it = method_map.find (nm);
if (it == method_map.end ())
{
// FIXME: look into class directory
}
else
{
cdef_method& meth = it->second;
// FIXME: check if method reload needed
if (meth.ok ())
return meth;
}
if (! local)
{
// Look into superclasses
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
cdef_method meth = cls.find_method (nm);
if (meth.ok ())
return meth;
}
}
return cdef_method ();
}
class ctor_analyzer : public tree_walker
{
public:
ctor_analyzer (void) = delete;
ctor_analyzer (const std::string& ctor, const std::string& obj)
: tree_walker (), who (ctor), obj_name (obj) { }
ctor_analyzer (const ctor_analyzer&) = delete;
ctor_analyzer& operator = (const ctor_analyzer&) = delete;
~ctor_analyzer (void) = default;
void visit_statement (tree_statement& t)
{
if (t.is_expression ())
t.expression ()->accept (*this);
}
void visit_simple_assignment (tree_simple_assignment& t)
{
t.right_hand_side ()->accept (*this);
}
void visit_multi_assignment (tree_multi_assignment& t)
{
t.right_hand_side ()->accept (*this);
}
void visit_index_expression (tree_index_expression& t)
{
t.expression ()->accept (*this);
}
std::list<cdef_class> get_constructor_list (void) const
{ return ctor_list; }
// NO-OP
void visit_anon_fcn_handle (tree_anon_fcn_handle&) { }
void visit_argument_list (tree_argument_list&) { }
void visit_binary_expression (tree_binary_expression&) { }
void visit_break_command (tree_break_command&) { }
void visit_colon_expression (tree_colon_expression&) { }
void visit_continue_command (tree_continue_command&) { }
void visit_decl_command (tree_decl_command&) { }
void visit_decl_init_list (tree_decl_init_list&) { }
void visit_decl_elt (tree_decl_elt&) { }
void visit_simple_for_command (tree_simple_for_command&) { }
void visit_complex_for_command (tree_complex_for_command&) { }
void visit_octave_user_script (octave_user_script&) { }
void visit_octave_user_function (octave_user_function&) { }
void visit_function_def (tree_function_def&) { }
void visit_identifier (tree_identifier&) { }
void visit_if_clause (tree_if_clause&) { }
void visit_if_command (tree_if_command&) { }
void visit_if_command_list (tree_if_command_list&) { }
void visit_switch_case (tree_switch_case&) { }
void visit_switch_case_list (tree_switch_case_list&) { }
void visit_switch_command (tree_switch_command&) { }
void visit_matrix (tree_matrix&) { }
void visit_cell (tree_cell&) { }
void visit_no_op_command (tree_no_op_command&) { }
void visit_constant (tree_constant&) { }
void visit_fcn_handle (tree_fcn_handle&) { }
void visit_parameter_list (tree_parameter_list&) { }
void visit_postfix_expression (tree_postfix_expression&) { }
void visit_prefix_expression (tree_prefix_expression&) { }
void visit_return_command (tree_return_command&) { }
void visit_try_catch_command (tree_try_catch_command&) { }
void visit_unwind_protect_command (tree_unwind_protect_command&) { }
void visit_while_command (tree_while_command&) { }
void visit_do_until_command (tree_do_until_command&) { }
void visit_superclass_ref (tree_superclass_ref& t)
{
if (t.method_name () == obj_name)
{
std::string class_name = t.class_name ();
cdef_class cls = lookup_class (class_name, false);
if (cls.ok ())
ctor_list.push_back (cls);
}
}
private:
// The name of the constructor being analyzed.
std::string who;
// The name of the first output argument of the constructor.
std::string obj_name;
// The list of superclass constructors that are explicitly called.
std::list<cdef_class> ctor_list;
};
void
cdef_class::cdef_class_rep::install_method (const cdef_method& meth)
{
method_map[meth.get_name ()] = meth;
member_count++;
if (meth.is_constructor ())
{
// Analyze the constructor code to determine what superclass
// constructors are called explicitly.
octave_value ov_fcn = meth.get_function ();
if (ov_fcn.is_defined ())
{
octave_user_function *uf = ov_fcn.user_function_value (true);
if (uf)
{
tree_parameter_list *ret_list = uf->return_list ();
tree_statement_list *body = uf->body ();
if (! ret_list || ret_list->size () != 1)
error ("%s: invalid constructor output arguments",
meth.get_name ().c_str ());
std::string obj_name = ret_list->front ()->name ();
ctor_analyzer a (meth.get_name (), obj_name);
body->accept (a);
std::list<cdef_class> explicit_ctor_list
= a.get_constructor_list ();
for (const auto& cdef_cls : explicit_ctor_list)
{
#if DEBUG_TRACE
std::cerr << "explicit superclass constructor: "
<< cdef_cls.get_name () << std::endl;
#endif
implicit_ctor_list.remove (cdef_cls);
}
}
}
}
}
void
cdef_class::cdef_class_rep::load_all_methods (void)
{
// FIXME: re-scan class directory
}
Cell
cdef_class::cdef_class_rep::get_methods (bool include_ctor)
{
std::map<std::string,cdef_method> meths;
find_methods (meths, false, include_ctor);
Cell c (meths.size (), 1);
int idx = 0;
for (const auto& nm_mthd : meths)
c(idx++, 0) = to_ov (nm_mthd.second);
return c;
}
std::map<std::string, cdef_method>
cdef_class::cdef_class_rep::get_method_map (bool only_inherited,
bool include_ctor)
{
std::map<std::string, cdef_method> methods;
find_methods (methods, only_inherited, include_ctor);
return methods;
}
void
cdef_class::cdef_class_rep::find_methods (std::map<std::string, cdef_method>& meths,
bool only_inherited,
bool include_ctor)
{
load_all_methods ();
method_const_iterator it;
for (it = method_map.begin (); it != method_map.end (); ++it)
{
if (include_ctor || ! it->second.is_constructor ())
{
std::string nm = it->second.get_name ();
if (meths.find (nm) == meths.end ())
{
if (only_inherited)
{
octave_value acc = it->second.get ("Access");
if (! acc.is_string ()
|| acc.string_value () == "private")
continue;
}
meths[nm] = it->second;
}
}
}
// Look into superclasses
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
cls.get_rep ()->find_methods (meths, true, false);
}
}
cdef_property
cdef_class::cdef_class_rep::find_property (const std::string& nm)
{
auto it = property_map.find (nm);
if (it != property_map.end ())
{
cdef_property& prop = it->second;
if (prop.ok ())
return prop;
}
// Look into superclasses
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
cdef_property prop = cls.find_property (nm);
if (prop.ok ())
return prop;
}
return cdef_property ();
}
void
cdef_class::cdef_class_rep::install_property (const cdef_property& prop)
{
property_map[prop.get_name ()] = prop;
member_count++;
}
Cell
cdef_class::cdef_class_rep::get_properties (int mode)
{
std::map<std::string,cdef_property> props;
props = get_property_map (mode);
Cell c (props.size (), 1);
int idx = 0;
for (const auto& pname_prop : props)
c(idx++, 0) = to_ov (pname_prop.second);
return c;
}
std::map<std::string, cdef_property>
cdef_class::cdef_class_rep::get_property_map (int mode)
{
std::map<std::string,cdef_property> props;
find_properties (props, mode);
return props;
}
void
cdef_class::cdef_class_rep::find_properties (std::map<std::string,
cdef_property>& props,
int mode)
{
property_const_iterator it;
for (it = property_map.begin (); it != property_map.end (); ++it)
{
std::string nm = it->second.get_name ();
if (props.find (nm) == props.end ())
{
if (mode == property_inherited)
{
octave_value acc = it->second.get ("GetAccess");
if (! acc.is_string ()
|| acc.string_value () == "private")
continue;
}
props[nm] = it->second;
}
}
// Look into superclasses
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
cls.get_rep ()->find_properties (props,
(mode == property_all
? property_all
: property_inherited));
}
}
void
cdef_class::cdef_class_rep::find_names (std::set<std::string>& names,
bool all)
{
load_all_methods ();
for (const auto& cls_fnmap : method_map)
{
if (! cls_fnmap.second.is_constructor ())
{
std::string nm = cls_fnmap.second.get_name ();
if (! all)
{
octave_value acc = cls_fnmap.second.get ("Access");
if (! acc.is_string()
|| acc.string_value () != "public")
continue;
}
names.insert (nm);
}
}
for (const auto& pname_prop : property_map)
{
std::string nm = pname_prop.second.get_name ();
if (! all)
{
octave_value acc = pname_prop.second.get ("GetAccess");
if (! acc.is_string()
|| acc.string_value () != "public")
continue;
}
names.insert (nm);
}
// Look into superclasses
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
cls.get_rep ()->find_names (names, all);
}
}
string_vector
cdef_class::cdef_class_rep::get_names (void)
{
std::set<std::string> names;
find_names (names, false);
string_vector v (names);
return v.sort (true);
}
void
cdef_class::cdef_class_rep::delete_object (const cdef_object& obj)
{
cdef_method dtor = find_method ("delete");
// FIXME: would it be better to tell find_method above to not find
// overloaded functions?
if (dtor.ok () && dtor.is_defined_in_class (get_name ()))
dtor.execute (obj, octave_value_list (), 0, true, "destructor");
// FIXME: should we destroy corresponding properties here?
// Call "delete" in super classes
Cell super_classes = get ("SuperClasses").cell_value ();
for (int i = 0; i < super_classes.numel (); i++)
{
cdef_class cls = lookup_class (super_classes(i));
if (cls.get_name () != "handle")
cls.delete_object (obj);
}
}
octave_value_list
cdef_class::cdef_class_rep::meta_subsref (const std::string& type,
const std::list<octave_value_list>& idx,
int nargout)
{
size_t skip = 1;
octave_value_list retval;
switch (type[0])
{
case '(':
// Constructor call
#if DEBUG_TRACE
std::cerr << "constructor" << std::endl;
#endif
retval(0) = construct (idx.front ());
break;
case '.':
{
// Static method, constant (or property?)
#if DEBUG_TRACE
std::cerr << "static method/property" << std::endl;
#endif
if (idx.front ().length () != 1)
error ("invalid meta.class indexing");
std::string nm = idx.front ()(0).xstring_value ("invalid meta.class indexing, expected a method or property name");
cdef_method meth = find_method (nm);
if (meth.ok ())
{
if (! meth.is_static ())
error ("method '%s' is not static", nm.c_str ());
octave_value_list args;
if (type.length () > 1 && idx.size () > 1 && type[1] == '(')
{
args = *(++(idx.begin ()));
skip++;
}
retval = meth.execute (args, (type.length () > skip
? 1 : nargout), true,
"meta.class");
}
else
{
cdef_property prop = find_property (nm);
if (! prop.ok ())
error ("no such method or property '%s'", nm.c_str ());
if (! prop.is_constant ())
error ("property '%s' is not constant", nm.c_str ());
retval(0) = prop.get_value (true, "meta.class");
}
}
break;
default:
error ("invalid meta.class indexing");
break;
}
if (type.length () > skip && idx.size () > skip && ! retval.empty ())
retval = retval(0).next_subsref (nargout, type, idx, skip);
return retval;
}
void
cdef_class::cdef_class_rep::meta_release (void)
{
cdef_manager& cdm
= __get_cdef_manager__ ("cdef_class::cdef_class_rep::meta_release");
cdm.unregister_class (wrap ());
}
void
cdef_class::cdef_class_rep::initialize_object (cdef_object& obj)
{
// Populate the object with default property values
std::list<cdef_class> super_classes
= lookup_classes (get ("SuperClasses").cell_value ());
for (auto& cls : super_classes)
cls.initialize_object (obj);
for (const auto& pname_prop : property_map)
{
if (! pname_prop.second.get ("Dependent").bool_value ())
{
octave_value pvalue = pname_prop.second.get ("DefaultValue");
if (pvalue.is_defined ())
obj.put (pname_prop.first, pvalue);
else
obj.put (pname_prop.first, octave_value (Matrix ()));
}
}
m_count++;
obj.mark_for_construction (cdef_class (this));
}
void
cdef_class::cdef_class_rep::run_constructor (cdef_object& obj,
const octave_value_list& args)
{
octave_value_list empty_args;
for (const auto& cls : implicit_ctor_list)
{
cdef_class supcls = lookup_class (cls);
supcls.run_constructor (obj, empty_args);
}
std::string cls_name = get_name ();
std::string ctor_name = get_base_name (cls_name);
cdef_method ctor = find_method (ctor_name);
if (ctor.ok ())
{
octave_value_list ctor_args (args);
octave_value_list ctor_retval;
ctor_args.prepend (to_ov (obj));
ctor_retval = ctor.execute (ctor_args, 1, true, "constructor");
if (ctor_retval.length () != 1)
error ("%s: invalid number of output arguments for classdef constructor",
ctor_name.c_str ());
obj = to_cdef (ctor_retval(0));
}
obj.mark_as_constructed (wrap ());
}
octave_value
cdef_class::cdef_class_rep::get_method (const std::string& name) const
{
auto p = method_map.find (name);
if (p == method_map.end ())
return octave_value ();
return p->second.get_function ();
}
octave_value
cdef_class::cdef_class_rep::construct (const octave_value_list& args)
{
cdef_object obj = construct_object (args);
if (obj.ok ())
return to_ov (obj);
return octave_value ();
}
cdef_object
cdef_class::cdef_class_rep::construct_object (const octave_value_list& args)
{
if (is_abstract ())
error ("cannot instantiate object for abstract class '%s'",
get_name ().c_str ());
cdef_object obj;
if (is_meta_class ())
{
// This code path is only used to create empty meta objects
// as filler for the empty values within a meta object array.
cdef_class this_cls = wrap ();
static cdef_object empty_class;
cdef_manager& cdm
= __get_cdef_manager__ ("cdef_class::cdef_class_rep::construct_object");
if (this_cls == cdm.meta_class ())
{
if (! empty_class.ok ())
empty_class = cdm.make_class ("", std::list<cdef_class> ());
obj = empty_class;
}
else if (this_cls == cdm.meta_property ())
{
static cdef_property empty_property;
if (! empty_class.ok ())
empty_class = cdm.make_class ("", std::list<cdef_class> ());
if (! empty_property.ok ())
empty_property = cdm.make_property (empty_class, "");
obj = empty_property;
}
else if (this_cls == cdm.meta_method ())
{
static cdef_method empty_method;
if (! empty_class.ok ())
empty_class = cdm.make_class ("", std::list<cdef_class> ());
if (! empty_method.ok ())
empty_method = cdm.make_method (empty_class, "", octave_value ());
obj = empty_method;
}
else if (this_cls == cdm.meta_package ())
{
static cdef_package empty_package;
if (! empty_package.ok ())
empty_package = cdm.make_package ("");
obj = empty_package;
}
else
panic_impossible ();
return obj;
}
else
{
if (is_handle_class ())
obj = cdef_object (new handle_cdef_object ());
else
obj = cdef_object (new value_cdef_object ());
obj.set_class (wrap ());
initialize_object (obj);
run_constructor (obj, args);
return obj;
}
return cdef_object ();
}
static octave_value
compute_attribute_value (tree_evaluator& tw,
tree_classdef_attribute *t)
{
tree_expression *expr = t->expression ();
if (expr)
{
if (expr->is_identifier ())
{
std::string s = expr->name ();
if (s == "public")
return std::string ("public");
else if (s == "protected")
return std::string ("protected");
else if (s == "private")
return std::string ("private");
}
return expr->evaluate (tw);
}
else
return octave_value (true);
}
template <typename T>
static std::string
attribute_value_to_string (T *t, octave_value v)
{
if (v.is_string ())
return v.string_value ();
else if (t->expression ())
return t->expression ()->original_text ();
else
return "true";
}
cdef_class
cdef_class::make_meta_class (interpreter& interp,
tree_classdef *t, bool is_at_folder)
{
cdef_class retval;
std::string class_name, full_class_name;
// Class creation
class_name = full_class_name = t->ident ()->name ();
if (! t->package_name ().empty ())
full_class_name = t->package_name () + '.' + full_class_name;
#if DEBUG_TRACE
std::cerr << "class: " << full_class_name << std::endl;
#endif
// Push a dummy scope frame on the call stack that corresponds to
// the scope that was used when parsing classdef object. Without
// this, we may pick up stray values from the current scope when
// evaluating expressions found in things like attribute lists.
unwind_protect frame;
tree_evaluator& tw = interp.get_evaluator ();
tw.push_dummy_scope (full_class_name);
frame.add_method (tw, &octave::tree_evaluator::pop_scope);
std::list<cdef_class> slist;
if (t->superclass_list ())
{
for (auto& scls : (*t->superclass_list ()))
{
std::string sclass_name = (scls)->class_name ();
#if DEBUG_TRACE
std::cerr << "superclass: " << sclass_name << std::endl;
#endif
cdef_class sclass = lookup_class (sclass_name);
if (sclass.get ("Sealed").bool_value ())
error ("'%s' cannot inherit from '%s', because it is sealed",
full_class_name.c_str (), sclass_name.c_str ());
slist.push_back (sclass);
}
}
cdef_manager& cdm = __get_cdef_manager__ ("cdef_class::make_meta_class");
retval = cdm.make_class (full_class_name, slist);
retval.doc_string (t->doc_string ());
// Package owning this class
if (! t->package_name ().empty ())
{
cdef_package pack = cdm.find_package (t->package_name ());
if (pack.ok ())
retval.put ("ContainingPackage", to_ov (pack));
}
// Class attributes
if (t->attribute_list ())
{
for (const auto& attr : (*t->attribute_list ()))
{
std::string aname = attr->ident ()->name ();
octave_value avalue = compute_attribute_value (tw, attr);
#if DEBUG_TRACE
std::cerr << "class attribute: " << aname << " = "
<< attribute_value_to_string (attr, avalue) << std::endl;
#endif
retval.put (aname, avalue);
}
}
tree_classdef_body *b = t->body ();
if (b)
{
// Keep track of the get/set accessor methods. They will be used
// later on when creating properties.
std::map<std::string, octave_value> get_methods;
std::map<std::string, octave_value> set_methods;
// Method blocks
std::list<tree_classdef_methods_block *> mb_list = b->methods_list ();
load_path& lp = interp.get_load_path ();
for (auto& mb_p : mb_list)
{
std::map<std::string, octave_value> amap;
#if DEBUG_TRACE
std::cerr << "method block" << std::endl;
#endif
// Method attributes
if (mb_p->attribute_list ())
{
for (auto& attr_p : *mb_p->attribute_list ())
{
std::string aname = attr_p->ident ()->name ();
octave_value avalue = compute_attribute_value (tw, attr_p);
#if DEBUG_TRACE
std::cerr << "method attribute: " << aname << " = "
<< attribute_value_to_string (attr_p, avalue)
<< std::endl;
#endif
amap[aname] = avalue;
}
}
// Methods
if (mb_p->element_list ())
{
for (auto& mtd : *mb_p->element_list ())
{
std::string mname = mtd.function_value ()->name ();
std::string mprefix = mname.substr (0, 4);
if (mprefix == "get.")
get_methods[mname.substr (4)]
= make_fcn_handle (mtd, mname, full_class_name);
else if (mprefix == "set.")
set_methods[mname.substr (4)]
= make_fcn_handle (mtd, mname, full_class_name);
else
{
cdef_method meth = cdm.make_method (retval, mname, mtd);
#if DEBUG_TRACE
std::cerr << (mname == class_name ? "constructor"
: "method")
<< ": " << mname << std::endl;
#endif
for (auto& attrnm_val : amap)
meth.put (attrnm_val.first, attrnm_val.second);
retval.install_method (meth);
}
}
}
}
if (is_at_folder)
{
// Look for all external methods visible on octave path at the
// time of loading of the class.
//
// FIXME: This is an "extension" to Matlab behavior, which only looks
// in the @-folder containing the original classdef file. However,
// this is easier to implement it that way at the moment.
std::list<std::string> external_methods
= lp.methods (full_class_name);
for (const auto& mtdnm : external_methods)
{
// FIXME: should we issue a warning if the method is already
// defined in the classdef file?
if (mtdnm != class_name
&& ! retval.find_method (mtdnm, true).ok ())
{
// Create a dummy method that is used until the actual
// method is loaded.
octave_user_function *fcn = new octave_user_function ();
fcn->stash_function_name (mtdnm);
cdef_method meth
= cdm.make_method (retval, mtdnm, octave_value (fcn));
retval.install_method (meth);
}
}
}
// Property blocks
// FIXME: default property expression should be able to call static
// methods of the class being constructed. A restricted CLASSNAME
// symbol should be added to the scope before evaluating default
// value expressions.
std::list<tree_classdef_properties_block *> pb_list
= b->properties_list ();
for (auto& pb_p : pb_list)
{
std::map<std::string, octave_value> amap;
#if DEBUG_TRACE
std::cerr << "property block" << std::endl;
#endif
// Property attributes
if (pb_p->attribute_list ())
{
for (auto& attr_p : *pb_p->attribute_list ())
{
std::string aname = attr_p->ident ()->name ();
octave_value avalue = compute_attribute_value (tw, attr_p);
#if DEBUG_TRACE
std::cerr << "property attribute: " << aname << " = "
<< attribute_value_to_string (attr_p, avalue)
<< std::endl;
#endif
if (aname == "Access")
{
amap["GetAccess"] = avalue;
amap["SetAccess"] = avalue;
}
else
amap[aname] = avalue;
}
}
// Properties
if (pb_p->element_list ())
{
for (auto& prop_p : *pb_p->element_list ())
{
std::string prop_name = prop_p->ident ()->name ();
cdef_property prop = cdm.make_property (retval, prop_name);
#if DEBUG_TRACE
std::cerr << "property: " << prop_p->ident ()->name ()
<< std::endl;
#endif
tree_expression *expr = prop_p->expression ();
if (expr)
{
octave_value pvalue = expr->evaluate (tw);
#if DEBUG_TRACE
std::cerr << "property default: "
<< attribute_value_to_string (prop_p, pvalue)
<< std::endl;
#endif
prop.put ("DefaultValue", pvalue);
}
// Install property attributes. This is done before assigning
// the property accessors so we can do validation by using
// cdef_property methods.
for (auto& attrnm_val : amap)
prop.put (attrnm_val.first, attrnm_val.second);
// Install property access methods, if any. Remove the
// accessor methods from the temporary storage map, so we can
// detect which ones are invalid and do not correspond to a
// defined property.
auto git = get_methods.find (prop_name);
if (git != get_methods.end ())
{
make_function_of_class (retval, git->second);
prop.put ("GetMethod", git->second);
get_methods.erase (git);
}
auto sit = set_methods.find (prop_name);
if (sit != set_methods.end ())
{
make_function_of_class (retval, sit->second);
prop.put ("SetMethod", sit->second);
set_methods.erase (sit);
}
retval.install_property (prop);
}
}
}
}
return retval;
}
octave_value
cdef_class::get_method_function (const std::string& /* nm */)
{
return octave_value (new octave_classdef_meta (*this));
}
}
|