diff libinterp/parse-tree/parse.h @ 23054:564e959a0e89

avoid invalid nested function and subfunctions definitions (bug #50014) * parse.h, oct-parse.in.yy (base_parser::parent_scope_info): New class. (base_parser::function_scopes): Use new parent_scope_info class for this data member. * oct-parse.in.yy: Change all uses of function_scopes for new type. (fcn_name): Issue error message and abort parse if duplicate nestted function or subfunction name is detect.
author John W. Eaton <jwe@octave.org>
date Sat, 14 Jan 2017 10:33:28 -0500
parents f75d289645ec
children 4e3d47dc7e25
line wrap: on
line diff
--- a/libinterp/parse-tree/parse.h	Mon Jan 16 16:00:18 2017 -0500
+++ b/libinterp/parse-tree/parse.h	Sat Jan 14 10:33:28 2017 -0500
@@ -29,9 +29,9 @@
 
 #include <string>
 
-#include <stack>
-#include <vector>
+#include <deque>
 #include <map>
+#include <set>
 
 #include "lex.h"
 #include "symtab.h"
@@ -147,6 +147,52 @@
   class
   base_parser
   {
+  private:
+
+    class parent_scope_info
+    {
+    public:
+
+      typedef std::pair<symbol_table::scope_id, std::string> value_type;
+
+      typedef std::deque<value_type>::iterator iterator;
+      typedef std::deque<value_type>::const_iterator const_iterator;
+
+      typedef std::deque<value_type>::reverse_iterator reverse_iterator;
+      typedef std::deque<value_type>::const_reverse_iterator const_reverse_iterator;
+
+      parent_scope_info (void) = default;
+
+      parent_scope_info (const parent_scope_info&) = default;
+
+      parent_scope_info& operator = (const parent_scope_info&) = default;
+
+      ~parent_scope_info (void) = default;
+
+      size_t size (void) const;
+
+      void push (const value_type& elt);
+
+      void push (symbol_table::scope_id id);
+
+      void pop (void);
+
+      bool name_ok (const std::string& name);
+
+      bool name_current_scope (const std::string& name);
+
+      symbol_table::scope_id parent_scope (void) const;
+
+      std::string parent_name (void) const;
+
+      void clear (void);
+
+    private:
+
+      std::deque<value_type> info;
+      std::set<std::string> all_names;
+    };
+
   public:
 
     base_parser (base_lexer& lxr);
@@ -442,10 +488,8 @@
     // in a package directory (+-directory).
     std::string curr_package_name;
 
-    // A stack holding the nested function scopes being parsed.
-    // We don't use std::stack, because we want the clear method.  Also, we
-    // must access one from the top
-    std::vector<symbol_table::scope_id> function_scopes;
+    // Nested function scopes and names currently being parsed.
+    parent_scope_info function_scopes;
 
     // Pointer to the primary user function or user script function.
     octave_function *primary_fcn_ptr;