changeset 27504:7a31b25e3252

use shared_ptr for storing classdef and statement_list objects in parser * parse.h, oct-parse.yy (base_parser::m_stmt_list, base_parser::m_classdef_object): Use shared_ptr object. (base_parser::statement_list, base_parser::classdef_object): New accessor functions. Change all uses.
author John W. Eaton <jwe@octave.org>
date Thu, 10 Oct 2019 13:33:33 -0400
parents 1bc237447e56
children c409d16b7190
files libinterp/corefcn/interpreter.cc libinterp/parse-tree/bp-table.cc libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/parse.h libinterp/parse-tree/pt-eval.cc
diffstat 5 files changed, 59 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter.cc	Wed Oct 16 10:56:10 2019 -0400
+++ b/libinterp/corefcn/interpreter.cc	Thu Oct 10 13:33:33 2019 -0400
@@ -1483,7 +1483,7 @@
         if (status == 0)
           {
             if (parser.m_lexer.m_reading_classdef_file
-                && parser.m_classdef_object)
+                && parser.classdef_object ())
               {
                 // Convert parse tree for classdef object to
                 // meta.class info (and stash it in the symbol
@@ -1494,22 +1494,13 @@
 
                 bool is_at_folder = ! dispatch_type.empty ();
 
-                try
-                  {
-                    fcn_ptr = parser.m_classdef_object->make_meta_class (*this, is_at_folder);
-                  }
-                catch (const execution_exception&)
-                  {
-                    delete parser.m_classdef_object;
-                    throw;
-                  }
+                std::shared_ptr<tree_classdef> cdef_obj
+                  = parser.classdef_object();
+
+                fcn_ptr = cdef_obj->make_meta_class (*this, is_at_folder);
 
                 if (fcn_ptr)
                   retval = octave_value (fcn_ptr);
-
-                delete parser.m_classdef_object;
-
-                parser.m_classdef_object = nullptr;
               }
             else if (fcn_ptr)
               {
--- a/libinterp/parse-tree/bp-table.cc	Wed Oct 16 10:56:10 2019 -0400
+++ b/libinterp/parse-tree/bp-table.cc	Thu Oct 10 13:33:33 2019 -0400
@@ -230,13 +230,17 @@
         else
           {
             tree_statement *stmt = nullptr;
-            if (! parser.m_stmt_list)
+
+            std::shared_ptr<tree_statement_list> stmt_list
+              = parser.statement_list ();
+
+            if (! stmt_list)
               error ("dbstop: "
                      "condition is not empty, but has nothing to evaluate");
             else
               {
-                if (parser.m_stmt_list->length () == 1
-                    && (stmt = parser.m_stmt_list->front ())
+                if (stmt_list->length () == 1
+                    && (stmt = stmt_list->front ())
                     && stmt->is_expression ())
                   {
                     tree_expression *expr = stmt->expression ();
--- a/libinterp/parse-tree/oct-parse.yy	Wed Oct 16 10:56:10 2019 -0400
+++ b/libinterp/parse-tree/oct-parse.yy	Thu Oct 10 13:33:33 2019 -0400
@@ -382,14 +382,14 @@
 input           : simple_list '\n'
                   {
                     $$ = nullptr;
-                    parser.m_stmt_list = $1;
+                    parser.statement_list (std::shared_ptr<octave::tree_statement_list> ($1));
                     YYACCEPT;
                   }
                 | simple_list END_OF_INPUT
                   {
                     $$ = nullptr;
                     lexer.m_end_of_input = true;
-                    parser.m_stmt_list = $1;
+                    parser.statement_list (std::shared_ptr<octave::tree_statement_list> ($1));
                     YYACCEPT;
                   }
                 | parse_error
@@ -2185,14 +2185,12 @@
       m_max_fcn_depth (-1), m_curr_fcn_depth (-1), m_primary_fcn_scope (),
       m_curr_class_name (), m_curr_package_name (), m_function_scopes (),
       m_primary_fcn_ptr (nullptr), m_subfunction_names (),
-      m_classdef_object (nullptr), m_stmt_list (nullptr), m_lexer (lxr),
+      m_classdef_object (), m_stmt_list (), m_lexer (lxr),
       m_parser_state (yypstate_new ())
   { }
 
   base_parser::~base_parser (void)
   {
-    delete m_stmt_list;
-
     delete &m_lexer;
 
     // FIXME: Deleting the internal Bison parser state structure does
@@ -2222,10 +2220,8 @@
     m_function_scopes.clear ();
     m_primary_fcn_ptr  = nullptr;
     m_subfunction_names.clear ();
-    m_classdef_object = nullptr;
-
-    delete m_stmt_list;
-    m_stmt_list = nullptr;
+    m_classdef_object.reset ();
+    m_stmt_list.reset ();
 
     m_lexer.reset ();
 
@@ -3837,7 +3833,7 @@
                                      tree_statement_list *local_fcns)
   {
     if (m_lexer.m_reading_classdef_file)
-      m_classdef_object = cls;
+      m_classdef_object = std::shared_ptr<tree_classdef> (cls);
 
     if (local_fcns)
       {
--- a/libinterp/parse-tree/parse.h	Wed Oct 16 10:56:10 2019 -0400
+++ b/libinterp/parse-tree/parse.h	Thu Oct 10 13:33:33 2019 -0400
@@ -27,11 +27,11 @@
 
 #include <cstdio>
 
-#include <string>
-
 #include <deque>
 #include <map>
+#include <memory>
 #include <set>
+#include <string>
 
 #include "lex.h"
 #include "pt-misc.h"
@@ -154,6 +154,26 @@
 
     void reset (void);
 
+    void classdef_object (const std::shared_ptr<tree_classdef>& obj)
+    {
+      m_classdef_object = obj;
+    }
+
+    std::shared_ptr<tree_classdef> classdef_object (void) const
+    {
+      return m_classdef_object;
+    }
+
+    void statement_list (const std::shared_ptr<tree_statement_list>& lst)
+    {
+      m_stmt_list = lst;
+    }
+
+    std::shared_ptr<tree_statement_list> statement_list (void) const
+    {
+      return m_stmt_list;
+    }
+
     // Error mesages for mismatched end tokens.
     void end_token_error (token *tok, token::end_tok_type expected);
 
@@ -467,10 +487,10 @@
     std::list<std::string> m_subfunction_names;
 
     // Pointer to the classdef object we just parsed, if any.
-    tree_classdef *m_classdef_object;
+    std::shared_ptr<tree_classdef> m_classdef_object;
 
     // Result of parsing input.
-    tree_statement_list *m_stmt_list;
+    std::shared_ptr <tree_statement_list> m_stmt_list;
 
     // State of the lexer.
     base_lexer& m_lexer;
--- a/libinterp/parse-tree/pt-eval.cc	Wed Oct 16 10:56:10 2019 -0400
+++ b/libinterp/parse-tree/pt-eval.cc	Thu Oct 10 13:33:33 2019 -0400
@@ -280,9 +280,12 @@
               }
             else
               {
-                if (retval == 0 && curr_parser.m_stmt_list)
+                if (retval == 0)
                   {
-                    curr_parser.m_stmt_list->accept (tw);
+                    std::shared_ptr<tree_statement_list> stmt_list
+                      = curr_parser.statement_list ();
+
+                    stmt_list->accept (tw);
 
                     if (octave_completion_matches_called)
                       octave_completion_matches_called = false;
@@ -387,9 +390,12 @@
 
             if (retval == 0)
               {
-                if (repl_parser.m_stmt_list)
+                std::shared_ptr<tree_statement_list> stmt_list
+                  = repl_parser.statement_list ();
+
+                if (stmt_list)
                   {
-                    repl_parser.m_stmt_list->accept (*this);
+                    stmt_list->accept (*this);
 
                     octave_quit ();
 
@@ -543,12 +549,15 @@
 
         if (parse_status == 0)
           {
-            if (eval_parser.m_stmt_list)
+            std::shared_ptr<tree_statement_list> stmt_list
+              = eval_parser.statement_list ();
+
+            if (stmt_list)
               {
                 tree_statement *stmt = nullptr;
 
-                if (eval_parser.m_stmt_list->length () == 1
-                    && (stmt = eval_parser.m_stmt_list->front ())
+                if (stmt_list->length () == 1
+                    && (stmt = stmt_list->front ())
                     && stmt->is_expression ())
                   {
                     tree_expression *expr = stmt->expression ();
@@ -572,7 +581,7 @@
                       retval = octave_value_list ();
                   }
                 else if (nargout == 0)
-                  eval_parser.m_stmt_list->accept (*this);
+                  stmt_list->accept (*this);
                 else
                   error ("eval: invalid use of statement list");