changeset 9639:8d79f36ebdde

store scope->function pointer
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 14 Sep 2009 11:23:54 +0200
parents 5d49ff601e9b
children 110863cd3371
files src/ChangeLog src/ov-usr-fcn.cc src/symtab.cc src/symtab.h
diffstat 4 files changed, 39 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Sat Sep 12 10:15:38 2009 -0400
+++ b/src/ChangeLog	Mon Sep 14 11:23:54 2009 +0200
@@ -1,3 +1,14 @@
+2009-09-14  Jaroslav Hajek  <highegg@gmail.com>
+
+	* symtab.h (symbol_table::curr_fcn): New member field.
+	(symbol_table::get_curr_fcn, symbol_table::set_curr_fcn): New member
+	funcs.
+	* symtab.cc (symbol_table::fcn_info::fcn_info_rep::xfind): Use
+	symbol_table::get_curr_fcn.
+	(symbol_table::fcn_info::fcn_info_rep::x_builtin_find): Ditto.
+	* ov-usr-fcn.cc (octave_user_function::octave_user_function): Call
+	symbol_table::set_curr_fcn.
+
 2009-09-12  John W. Eaton  <jwe@octave.org>
 
 	* version.h (OCTAVE_VERSION): Now 3.3.50+.
--- a/src/ov-usr-fcn.cc	Sat Sep 12 10:15:38 2009 -0400
+++ b/src/ov-usr-fcn.cc	Mon Sep 14 11:23:54 2009 +0200
@@ -191,6 +191,9 @@
 {
   if (cmd_list)
     cmd_list->mark_as_function_body ();
+
+  if (local_scope >= 0)
+    symbol_table::set_curr_fcn (this, local_scope);
 }
 
 octave_user_function::~octave_user_function (void)
--- a/src/symtab.cc	Sat Sep 12 10:15:38 2009 -0400
+++ b/src/symtab.cc	Mon Sep 14 11:23:54 2009 +0200
@@ -41,7 +41,6 @@
 #include "pager.h"
 #include "parse.h"
 #include "pt-arg-list.h"
-#include "toplev.h"
 #include "unwind-prot.h"
 #include "utils.h"
 #include "debug.h"
@@ -535,7 +534,7 @@
 
       scope_val_iterator r = subfunctions.find (xcurrent_scope);
 
-      octave_function *curr_fcn = 0;
+      octave_user_function *curr_fcn = symbol_table::get_curr_fcn ();
 
       if (r != subfunctions.end ())
         {
@@ -545,8 +544,6 @@
         }
       else
         {
-          curr_fcn = octave_call_stack::current ();
-
           if (curr_fcn)
             {
               scope_id pscope = curr_fcn->parent_fcn_scope ();
@@ -567,9 +564,6 @@
 
       // Private function.
 
-      if (! curr_fcn)
-        curr_fcn = octave_call_stack::current ();
-
       if (curr_fcn)
         {
           std::string dir_name = curr_fcn->dir_name ();
@@ -765,7 +759,7 @@
 
   // Private function.
 
-  octave_function *curr_fcn = octave_call_stack::current ();
+  octave_user_function *curr_fcn = symbol_table::get_curr_fcn ();
 
   if (curr_fcn)
     {
--- a/src/symtab.h	Sat Sep 12 10:15:38 2009 -0400
+++ b/src/symtab.h	Mon Sep 14 11:23:54 2009 +0200
@@ -34,6 +34,7 @@
 #include "regex-match.h"
 
 class tree_argument_list;
+class octave_user_function;
 
 #include "oct-obj.h"
 #include "ov.h"
@@ -1793,6 +1794,24 @@
     parent_map[classname] = parent_list;
   }
 
+  static octave_user_function *get_curr_fcn (scope_id scope = xcurrent_scope)
+    {
+      symbol_table *inst = get_instance (scope);
+      return inst->curr_fcn;
+    }
+
+  static void set_curr_fcn (octave_user_function *curr_fcn,
+                            scope_id scope = xcurrent_scope)
+    {
+      assert (scope != xtop_scope && scope != xglobal_scope);
+      symbol_table *inst = get_instance (scope);
+      // FIXME: normally, functions should not usurp each other's scope.
+      // If for any incredible reason this is needed, call 
+      // set_user_function (0, scope) first.
+      assert (inst->curr_fcn == 0 || curr_fcn == 0);
+      inst->curr_fcn = curr_fcn;
+    }
+
 private:
 
   typedef std::map<std::string, symbol_record>::const_iterator table_const_iterator;
@@ -1817,6 +1836,9 @@
   // Map from symbol names to symbol info.
   std::map<std::string, symbol_record> table;
 
+  // The associated user code (may be null).
+  octave_user_function *curr_fcn;
+
   // Map from names of global variables to values.
   static std::map<std::string, octave_value> global_table;
 
@@ -1854,7 +1876,7 @@
   static context_id xcurrent_context;
 
   symbol_table (void)
-    : table_name (), table () { }
+    : table_name (), table (), curr_fcn (0) { }
 
   ~symbol_table (void) { }