changeset 16684:edbb123cbe3a classdef

Correct handling of package context in symbol table. This change partially revert the previous changes on the symbol table, because the function table is not a member of the symbol table, but a static field. * libinterp/interpfcn/load-path.h (load_path::do_find_method, load_path::do_methods): Remove methods. (load_path::find_method, load_path::methods): Reimplement with class load_path::loader. * libinterp/interpfcn/load-path.cc (load_path::do_find_method, load_path::do_methods): Remove methods. * libinterp/interpfcn/symtab.h (symbol_table::package_name): Remove field, moved to symbol_table::fcn_info::fcn_info_rep. Suppress all usage. (symbol_table::find): Remove scope argument. (symbol_table::alloc_package_scope): Remove method. (symbol_table::fcn_info::find_function, symbol_table::fcn_info::find_user_function, symbol_table::fcn_info::find): Remove package_name argument. (symbol_table::fcn_info::fcn_info_rep::find, symbol_table::fcn_info::fcn_info_rep::xfind, symbol_table::fcn_info::fcn_info_rep::find_function, symbol_table::fcn_info::fcn_info_rep::find_user_function, symbol_table::fcn_info::fcn_info_rep::find_package, symbol_table::fcn_info::fcn_info_rep::load_class_constructor): Likewise. (symbol_table::fcn_info::fcn_info_rep::package_name): New member. (symbol_table::fcn_info::fcn_info_rep::fcn_info_rep): Initialize it. (symbol_table::fcn_info::fcn_info_rep::full_name): New method. * libinterp/interpfcn/symtab.cc (symbol_table::fcn_info::fcn_info_rep::load_class_constructor, symbol_table::fcn_info::fcn_info_rep::find, symbol_table::fcn_info::fcn_info_rep::xfind, symbol_table::fcn_info::fcn_info_rep::find_package, symbol_table::fcn_info::fcn_info_rep::find_user_function): Remove package_name argument. Use package_name member instead. (symbol_table::fcn_info::fcn_info_rep::load_class_method): Simplify, using full_name method and package_name member. (symbol_table::fcn_info::fcn_info_rep::dump): Use full_name. (symbol_table::find): Remove scope argument. (symbol_table::do_find): Do not use (removed) package_name member. * libinterp/octave-value/ov-classdef.h (cdef_package::cdef_package_rep::scope): Remove member. Suppress all usages. * libinterp/octave-value/ov-classdef.cc (cdef_package::cdef_package_rep::find): Do not use (removed) scope member. Pass fully-qualified name to the symbol table.
author Michael Goffioul <michael.goffioul@gmail.com>
date Sat, 18 May 2013 23:12:56 -0400
parents e1c6ad54259f
children 04e110438873
files libinterp/interpfcn/load-path.cc libinterp/interpfcn/load-path.h libinterp/interpfcn/symtab.cc libinterp/interpfcn/symtab.h libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov-classdef.h
diffstat 6 files changed, 81 insertions(+), 153 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/interpfcn/load-path.cc	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/interpfcn/load-path.cc	Sat May 18 23:12:56 2013 -0400
@@ -1148,25 +1148,6 @@
 }
 
 std::string
-load_path::do_find_method (const std::string& class_name,
-                           const std::string& meth,
-                           std::string& dir_name) const
-{
-  size_t pos = class_name.rfind ('.');
-
-  if (pos == std::string::npos)
-    return default_loader.find_method (class_name, meth, dir_name);
-  else
-    {
-      std::string pname = class_name.substr (0, pos);
-
-      std::string cname = class_name.substr (pos+1);
-
-      return get_loader(pname).find_method (cname, meth, dir_name);
-    }
-}
-
-std::string
 load_path::loader::find_method (const std::string& class_name,
                                 const std::string& meth,
                                 std::string& dir_name, int type) const
@@ -1215,23 +1196,6 @@
 }
 
 std::list<std::string>
-load_path::do_methods (const std::string& class_name) const
-{
-  size_t pos = class_name.rfind ('.');
-
-  if (pos == std::string::npos)
-    return default_loader.methods (class_name);
-  else
-    {
-      std::string pname = class_name.substr (0, pos);
-
-      std::string cname = class_name.substr (pos+1);
-
-      return get_loader (pname).methods (cname);
-    }
-}
-
-std::list<std::string>
 load_path::loader::methods (const std::string& class_name) const
 {
   std::list<std::string> retval;
--- a/libinterp/interpfcn/load-path.h	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/interpfcn/load-path.h	Sat May 18 23:12:56 2013 -0400
@@ -95,24 +95,29 @@
 
   static std::string find_method (const std::string& class_name,
                                   const std::string& meth,
-                                  std::string& dir_name)
+                                  std::string& dir_name,
+                                  const std::string& pack_name = std::string ())
   {
     return instance_ok ()
-      ? instance->do_find_method (class_name, meth, dir_name)
+      ? instance->get_loader (pack_name).find_method (class_name, meth,
+                                                      dir_name)
       : std::string ();
   }
 
   static std::string find_method (const std::string& class_name,
-                                  const std::string& meth)
+                                  const std::string& meth,
+                                  const std::string& pack_name = std::string ())
   {
     std::string dir_name;
-    return find_method (class_name, meth, dir_name);
+    return find_method (class_name, meth, dir_name, pack_name);
   }
 
-  static std::list<std::string> methods (const std::string& class_name)
+  static std::list<std::string> methods (const std::string& class_name,
+                                         const std::string& pack_name = std::string ())
   {
     return instance_ok ()
-      ? instance->do_methods (class_name) : std::list<std::string> ();
+      ? instance->get_loader(pack_name).methods (class_name)
+      : std::list<std::string> ();
   }
 
   static std::list<std::string> overloads (const std::string& meth)
@@ -659,12 +664,6 @@
     return default_loader;
   }
 
-  std::string do_find_method (const std::string& class_name,
-                              const std::string& meth,
-                              std::string& dir_name) const;
-
-  std::list<std::string> do_methods (const std::string& class_name) const;
-
   std::list<std::string> do_overloads (const std::string& meth) const;
 
   bool do_find_package (const std::string& package_name) const
--- a/libinterp/interpfcn/symtab.cc	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/interpfcn/symtab.cc	Sat May 18 23:12:56 2013 -0400
@@ -374,24 +374,19 @@
 }
 
 octave_value
-symbol_table::fcn_info::fcn_info_rep::load_class_constructor
-  (const std::string& pname)
+symbol_table::fcn_info::fcn_info_rep::load_class_constructor (void)
 {
   octave_value retval;
 
   std::string dir_name;
 
-  std::string full_name = name;
-
-  if (! pname.empty ())
-    full_name = pname + "." + full_name;
-
-  std::string file_name = load_path::find_method (full_name, name, dir_name);
+  std::string file_name = load_path::find_method (name, name, dir_name,
+                                                  package_name);
 
   if (! file_name.empty ())
     {
       octave_function *fcn = load_fcn_from_file (file_name, dir_name, name,
-                                                 pname);
+                                                 package_name);
 
       if (fcn)
         {
@@ -409,7 +404,7 @@
 
       octave_value old_function_on_path = function_on_path;
 
-      octave_value maybe_cdef_ctor = find_user_function (pname);
+      octave_value maybe_cdef_ctor = find_user_function ();
 
       if (maybe_cdef_ctor.is_defined ())
         {
@@ -435,18 +430,8 @@
 {
   octave_value retval;
 
-  if (name == dispatch_type)
-    retval = load_class_constructor (std::string ());
-  else if (dispatch_type.length () > name.length ()
-           && dispatch_type.substr (dispatch_type.length () - name.length ()
-                                    - 1) == ("." + name))
-    {
-      std::string pname =
-        dispatch_type.substr (0,
-                              dispatch_type.length () - name.length () - 1);
-
-      retval = load_class_constructor (pname);
-    }
+  if (full_name () == dispatch_type)
+    retval = load_class_constructor ();
   else
     {
       octave_function *cm = cdef_manager::find_method_symbol (name,
@@ -664,10 +649,9 @@
 
 octave_value
 symbol_table::fcn_info::fcn_info_rep::find (const octave_value_list& args,
-                                            bool local_funcs,
-                                            const std::string& pname)
+                                            bool local_funcs)
 {
-  octave_value retval = xfind (args, local_funcs, pname);
+  octave_value retval = xfind (args, local_funcs);
 
   if (! (error_state || retval.is_defined ()))
     {
@@ -677,7 +661,7 @@
 
       load_path::update ();
 
-      retval = xfind (args, local_funcs, pname);
+      retval = xfind (args, local_funcs);
     }
 
   return retval;
@@ -685,8 +669,7 @@
 
 octave_value
 symbol_table::fcn_info::fcn_info_rep::xfind (const octave_value_list& args,
-                                             bool local_funcs,
-                                             const std::string& pname)
+                                             bool local_funcs)
 {
   if (local_funcs)
     {
@@ -769,7 +752,7 @@
 
   if (q == class_constructors.end ())
     {
-      octave_value val = load_class_constructor (pname);
+      octave_value val = load_class_constructor ();
 
       if (val.is_defined ())
         return val;
@@ -785,7 +768,7 @@
         return fval;
       else
         {
-          octave_value val = load_class_constructor (pname);
+          octave_value val = load_class_constructor ();
 
           if (val.is_defined ())
             return val;
@@ -831,14 +814,14 @@
 
   // Function on the path.
 
-  fcn = find_user_function (pname);
+  fcn = find_user_function ();
 
   if (fcn.is_defined ())
     return fcn;
 
   // Package
 
-  fcn = find_package (pname);
+  fcn = find_package ();
 
   if (fcn.is_defined ())
     return fcn;
@@ -895,7 +878,7 @@
 
   // Function on the path.
 
-  octave_value fcn = find_user_function (std::string ());
+  octave_value fcn = find_user_function ();
 
   if (fcn.is_defined ())
     return fcn;
@@ -1042,7 +1025,7 @@
 }
 
 octave_value
-symbol_table::fcn_info::fcn_info_rep::find_user_function (const std::string& pname)
+symbol_table::fcn_info::fcn_info_rep::find_user_function (void)
 {
   // Function on the path.
 
@@ -1053,12 +1036,13 @@
     {
       std::string dir_name;
 
-      std::string file_name = load_path::find_fcn (name, dir_name, pname);
+      std::string file_name = load_path::find_fcn (name, dir_name,
+                                                   package_name);
 
       if (! file_name.empty ())
         {
           octave_function *fcn = load_fcn_from_file (file_name, dir_name, "",
-                                                     pname);
+                                                     package_name);
 
           if (fcn)
             function_on_path = octave_value (fcn);
@@ -1069,7 +1053,7 @@
 }
 
 octave_value
-symbol_table::fcn_info::fcn_info_rep::find_package (const std::string& pname)
+symbol_table::fcn_info::fcn_info_rep::find_package (void)
 {
   // FIXME: implement correct way to check out of date package
   //if (package.is_defined ())
@@ -1077,12 +1061,8 @@
 
   if (! (error_state || package.is_defined ()))
     {
-      std::string full_name = name;
-
-      if (! pname.empty ())
-        full_name = pname + "." + full_name;
-
-      octave_function * fcn = cdef_manager::find_package_symbol (full_name);
+      octave_function * fcn =
+        cdef_manager::find_package_symbol (full_name ());
 
       if (fcn)
         package = octave_value (fcn);
@@ -1148,7 +1128,7 @@
 symbol_table::fcn_info::fcn_info_rep::dump
   (std::ostream& os, const std::string& prefix) const
 {
-  os << prefix << name
+  os << prefix << full_name ()
      << " ["
      << (cmdline_function.is_defined () ? "c" : "")
      << (built_in_function.is_defined () ? "b" : "")
@@ -1227,10 +1207,9 @@
 symbol_table::find (const std::string& name,
                     const octave_value_list& args,
                     bool skip_variables,
-                    bool local_funcs,
-                    scope_id scope)
+                    bool local_funcs)
 {
-  symbol_table *inst = get_instance (scope);
+  symbol_table *inst = get_instance (xcurrent_scope);
 
   return inst
     ? inst->do_find (name, args, skip_variables, local_funcs)
@@ -1428,12 +1407,12 @@
   fcn_table_iterator p = fcn_table.find (name);
 
   if (p != fcn_table.end ())
-    return p->second.find (args, local_funcs, package_name);
+    return p->second.find (args, local_funcs);
   else
     {
       fcn_info finfo (name);
 
-      octave_value fcn = finfo.find (args, local_funcs, package_name);
+      octave_value fcn = finfo.find (args, local_funcs);
 
       if (fcn.is_defined ())
         fcn_table[name] = finfo;
--- a/libinterp/interpfcn/symtab.h	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/interpfcn/symtab.h	Sat May 18 23:12:56 2013 -0400
@@ -754,19 +754,27 @@
     public:
 
       fcn_info_rep (const std::string& nm)
-        : name (nm), subfunctions (), private_functions (),
+        : name (nm), package_name (), subfunctions (), private_functions (),
           class_constructors (), class_methods (), dispatch_map (),
           cmdline_function (), autoload_function (), function_on_path (),
-          built_in_function (), count (1) { }
+          built_in_function (), count (1)
+      {
+        size_t pos = name.rfind ('.');
+
+        if (pos != std::string::npos)
+          {
+            package_name = name.substr (0, pos);
+            name = name.substr (pos+1);
+          }
+      }
 
       octave_value load_private_function (const std::string& dir_name);
 
-      octave_value load_class_constructor (const std::string& pname);
+      octave_value load_class_constructor (void);
 
       octave_value load_class_method (const std::string& dispatch_type);
 
-      octave_value find (const octave_value_list& args, bool local_funcs,
-                         const std::string& package_name);
+      octave_value find (const octave_value_list& args, bool local_funcs);
 
       octave_value builtin_find (void);
 
@@ -774,19 +782,18 @@
 
       octave_value find_autoload (void);
 
-      octave_value find_package (const std::string& package_name);
-
-      octave_value find_user_function (const std::string& package_name);
+      octave_value find_package (void);
+
+      octave_value find_user_function (void);
 
       bool is_user_function_defined (void) const
       {
         return function_on_path.is_defined ();
       }
 
-      octave_value find_function (const octave_value_list& args, bool local_funcs,
-                                  const std::string& package_name)
+      octave_value find_function (const octave_value_list& args, bool local_funcs)
       {
-        return find (args, local_funcs, package_name);
+        return find (args, local_funcs);
       }
 
       void lock_subfunction (scope_id scope)
@@ -922,8 +929,18 @@
 
       void dump (std::ostream& os, const std::string& prefix) const;
 
+      std::string full_name (void) const
+      {
+        if (package_name.empty ())
+          return name;
+        else
+          return package_name + "." + name;
+      }
+
       std::string name;
 
+      std::string package_name;
+
       // Scope id to function object.
       std::map<scope_id, octave_value> subfunctions;
 
@@ -953,8 +970,7 @@
 
     private:
 
-      octave_value xfind (const octave_value_list& args, bool local_funcs,
-                          const std::string& package_name);
+      octave_value xfind (const octave_value_list& args, bool local_funcs);
 
       octave_value x_builtin_find (void);
 
@@ -996,10 +1012,9 @@
     }
 
     octave_value find (const octave_value_list& args = octave_value_list (),
-                       bool local_funcs = true,
-                       const std::string& package_name = std::string ())
+                       bool local_funcs = true)
     {
-      return rep->find (args, local_funcs, package_name);
+      return rep->find (args, local_funcs);
     }
 
     octave_value builtin_find (void)
@@ -1027,9 +1042,9 @@
       return rep->find_autoload ();
     }
 
-    octave_value find_user_function (const std::string& pname = std::string ())
+    octave_value find_user_function (void)
     {
-      return rep->find_user_function (pname);
+      return rep->find_user_function ();
     }
 
     bool is_user_function_defined (void) const
@@ -1038,10 +1053,9 @@
     }
 
     octave_value find_function (const octave_value_list& args = octave_value_list (),
-                                bool local_funcs = true,
-                                const std::string& package_name = std::string ())
+                                bool local_funcs = true)
     {
-      return rep->find_function (args, local_funcs, package_name);
+      return rep->find_function (args, local_funcs);
     }
 
     void lock_subfunction (scope_id scope)
@@ -1147,21 +1161,6 @@
 
   static scope_id alloc_scope (void) { return scope_id_cache::alloc (); }
 
-  static scope_id alloc_package_scope (const std::string& name)
-  {
-    scope_id retval = alloc_scope ();
-
-    if (retval != -1)
-      {
-        symbol_table* inst = get_instance (retval, true);
-
-        inst->do_cache_name (name);
-        inst->package_name = name;
-      }
-
-    return retval;
-  }
-
   static void set_scope (scope_id scope)
   {
     if (scope == xglobal_scope)
@@ -1305,8 +1304,7 @@
   find (const std::string& name,
         const octave_value_list& args = octave_value_list (),
         bool skip_variables = false,
-        bool local_funcs = true,
-        scope_id scope = xcurrent_scope);
+        bool local_funcs = true);
 
   static octave_value builtin_find (const std::string& name);
 
@@ -2343,10 +2341,6 @@
   // If true then no variables can be added.
   bool static_workspace;
 
-  // The name of the package context associated with this table. This is
-  // only used by classdef packages.
-  std::string package_name;
-
   // Map from names of global variables to values.
   static std::map<std::string, octave_value> global_table;
 
@@ -2387,7 +2381,7 @@
 
   symbol_table (scope_id scope)
     : my_scope (scope), table_name (), table (), nest_children (), nest_parent (0),
-    curr_fcn (0), static_workspace (false), package_name (), persistent_table () { }
+    curr_fcn (0), static_workspace (false), persistent_table () { }
 
   ~symbol_table (void) { }
 
--- a/libinterp/octave-value/ov-classdef.cc	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/octave-value/ov-classdef.cc	Sat May 18 23:12:56 2013 -0400
@@ -2787,10 +2787,9 @@
 octave_value
 cdef_package::cdef_package_rep::find (const std::string& nm)
 {
-  if (scope == -1)
-    scope = symbol_table::alloc_package_scope (get_name ());
-
-  return symbol_table::find (nm, octave_value_list (), true, false, scope);
+  std::string symbol_name = get_name () + "." + nm;
+
+  return symbol_table::find (symbol_name, octave_value_list (), true, false);
 }
 
 octave_value_list
--- a/libinterp/octave-value/ov-classdef.h	Sat May 18 22:43:10 2013 -0400
+++ b/libinterp/octave-value/ov-classdef.h	Sat May 18 23:12:56 2013 -0400
@@ -1212,13 +1212,9 @@
   {
   public:
     cdef_package_rep (void)
-      : cdef_meta_object_rep (), member_count (0), scope (-1) { }
+      : cdef_meta_object_rep (), member_count (0) { }
 
-    ~cdef_package_rep (void)
-      {
-        if (scope != -1)
-          symbol_table::erase_scope (scope);
-      }
+    ~cdef_package_rep (void) { }
 
     cdef_object_rep* copy (void) const { return new cdef_package_rep (*this); }
 
@@ -1285,9 +1281,6 @@
     typedef std::map<std::string, cdef_package>::iterator package_iterator;
     typedef std::map<std::string, cdef_package>::const_iterator package_const_iterator;
 
-    // The symbol_table scope corresponding to this package.
-    symbol_table::scope_id scope;
-
   private:
     cdef_package_rep (const cdef_package_rep& p)
       : cdef_meta_object_rep (p), full_name (p.full_name),