fix ref count in symbol::Libraries 28/17128/4
Antoine ELIAS [Fri, 28 Aug 2015 10:46:32 +0000 (12:46 +0200)]
Change-Id: Ie9055491150d9e7e3ad289cb0f35b018a52ac053

scilab/modules/ast/Makefile.am
scilab/modules/ast/Makefile.in
scilab/modules/ast/ast.vcxproj
scilab/modules/ast/ast.vcxproj.filters
scilab/modules/ast/includes/symbol/libraries.hxx
scilab/modules/ast/includes/symbol/variables.hxx
scilab/modules/ast/src/cpp/symbol/libraries.cpp [new file with mode: 0644]
scilab/modules/ast/src/cpp/symbol/variables.cpp [new file with mode: 0644]
scilab/modules/ast/src/cpp/types/library.cpp

index a05231d..36659f2 100644 (file)
@@ -56,7 +56,9 @@ src/cpp/parse/parsescilab.cpp \
 src/cpp/parse/printerror.cpp \
 src/cpp/parse/scanscilab.cpp \
 src/cpp/symbol/context.cpp \
+src/cpp/symbol/libraries.cpp \
 src/cpp/symbol/symbol.cpp \
+src/cpp/symbol/variables.cpp \
 src/cpp/system_env/configvariable.cpp \
 src/cpp/system_env/configvariable_interface.cpp \
 src/cpp/system_env/context_get.cpp \
index 07ba894..a0054b5 100644 (file)
@@ -209,7 +209,8 @@ am__libsciast_la_SOURCES_DIST = src/c/operations/doublecomplex.c \
        src/cpp/operations/types_subtraction.cpp \
        src/cpp/parse/parser.cpp src/cpp/parse/parsescilab.cpp \
        src/cpp/parse/printerror.cpp src/cpp/parse/scanscilab.cpp \
-       src/cpp/symbol/context.cpp src/cpp/symbol/symbol.cpp \
+       src/cpp/symbol/context.cpp src/cpp/symbol/libraries.cpp \
+       src/cpp/symbol/symbol.cpp src/cpp/symbol/variables.cpp \
        src/cpp/system_env/configvariable.cpp \
        src/cpp/system_env/configvariable_interface.cpp \
        src/cpp/system_env/context_get.cpp \
@@ -286,7 +287,9 @@ am_libsciast_la_OBJECTS =  \
        src/cpp/parse/libsciast_la-printerror.lo \
        src/cpp/parse/libsciast_la-scanscilab.lo \
        src/cpp/symbol/libsciast_la-context.lo \
+       src/cpp/symbol/libsciast_la-libraries.lo \
        src/cpp/symbol/libsciast_la-symbol.lo \
+       src/cpp/symbol/libsciast_la-variables.lo \
        src/cpp/system_env/libsciast_la-configvariable.lo \
        src/cpp/system_env/libsciast_la-configvariable_interface.lo \
        src/cpp/system_env/libsciast_la-context_get.lo \
@@ -746,7 +749,8 @@ libsciast_la_SOURCES = src/c/operations/doublecomplex.c \
        src/cpp/operations/types_subtraction.cpp \
        src/cpp/parse/parser.cpp src/cpp/parse/parsescilab.cpp \
        src/cpp/parse/printerror.cpp src/cpp/parse/scanscilab.cpp \
-       src/cpp/symbol/context.cpp src/cpp/symbol/symbol.cpp \
+       src/cpp/symbol/context.cpp src/cpp/symbol/libraries.cpp \
+       src/cpp/symbol/symbol.cpp src/cpp/symbol/variables.cpp \
        src/cpp/system_env/configvariable.cpp \
        src/cpp/system_env/configvariable_interface.cpp \
        src/cpp/system_env/context_get.cpp \
@@ -1319,8 +1323,14 @@ src/cpp/symbol/$(DEPDIR)/$(am__dirstamp):
 src/cpp/symbol/libsciast_la-context.lo:  \
        src/cpp/symbol/$(am__dirstamp) \
        src/cpp/symbol/$(DEPDIR)/$(am__dirstamp)
+src/cpp/symbol/libsciast_la-libraries.lo:  \
+       src/cpp/symbol/$(am__dirstamp) \
+       src/cpp/symbol/$(DEPDIR)/$(am__dirstamp)
 src/cpp/symbol/libsciast_la-symbol.lo: src/cpp/symbol/$(am__dirstamp) \
        src/cpp/symbol/$(DEPDIR)/$(am__dirstamp)
+src/cpp/symbol/libsciast_la-variables.lo:  \
+       src/cpp/symbol/$(am__dirstamp) \
+       src/cpp/symbol/$(DEPDIR)/$(am__dirstamp)
 src/cpp/system_env/$(am__dirstamp):
        @$(MKDIR_P) src/cpp/system_env
        @: > src/cpp/system_env/$(am__dirstamp)
@@ -1522,7 +1532,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/parse/$(DEPDIR)/libsciast_la-printerror.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/parse/$(DEPDIR)/libsciast_la-scanscilab.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/symbol/$(DEPDIR)/libsciast_la-context.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@src/cpp/symbol/$(DEPDIR)/libsciast_la-libraries.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/symbol/$(DEPDIR)/libsciast_la-symbol.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@src/cpp/symbol/$(DEPDIR)/libsciast_la-variables.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/system_env/$(DEPDIR)/libsciast_la-configvariable.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/system_env/$(DEPDIR)/libsciast_la-configvariable_interface.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@src/cpp/system_env/$(DEPDIR)/libsciast_la-context_get.Plo@am__quote@
@@ -1914,6 +1926,13 @@ src/cpp/symbol/libsciast_la-context.lo: src/cpp/symbol/context.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o src/cpp/symbol/libsciast_la-context.lo `test -f 'src/cpp/symbol/context.cpp' || echo '$(srcdir)/'`src/cpp/symbol/context.cpp
 
+src/cpp/symbol/libsciast_la-libraries.lo: src/cpp/symbol/libraries.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT src/cpp/symbol/libsciast_la-libraries.lo -MD -MP -MF src/cpp/symbol/$(DEPDIR)/libsciast_la-libraries.Tpo -c -o src/cpp/symbol/libsciast_la-libraries.lo `test -f 'src/cpp/symbol/libraries.cpp' || echo '$(srcdir)/'`src/cpp/symbol/libraries.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) src/cpp/symbol/$(DEPDIR)/libsciast_la-libraries.Tpo src/cpp/symbol/$(DEPDIR)/libsciast_la-libraries.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='src/cpp/symbol/libraries.cpp' object='src/cpp/symbol/libsciast_la-libraries.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o src/cpp/symbol/libsciast_la-libraries.lo `test -f 'src/cpp/symbol/libraries.cpp' || echo '$(srcdir)/'`src/cpp/symbol/libraries.cpp
+
 src/cpp/symbol/libsciast_la-symbol.lo: src/cpp/symbol/symbol.cpp
 @am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT src/cpp/symbol/libsciast_la-symbol.lo -MD -MP -MF src/cpp/symbol/$(DEPDIR)/libsciast_la-symbol.Tpo -c -o src/cpp/symbol/libsciast_la-symbol.lo `test -f 'src/cpp/symbol/symbol.cpp' || echo '$(srcdir)/'`src/cpp/symbol/symbol.cpp
 @am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) src/cpp/symbol/$(DEPDIR)/libsciast_la-symbol.Tpo src/cpp/symbol/$(DEPDIR)/libsciast_la-symbol.Plo
@@ -1921,6 +1940,13 @@ src/cpp/symbol/libsciast_la-symbol.lo: src/cpp/symbol/symbol.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o src/cpp/symbol/libsciast_la-symbol.lo `test -f 'src/cpp/symbol/symbol.cpp' || echo '$(srcdir)/'`src/cpp/symbol/symbol.cpp
 
+src/cpp/symbol/libsciast_la-variables.lo: src/cpp/symbol/variables.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT src/cpp/symbol/libsciast_la-variables.lo -MD -MP -MF src/cpp/symbol/$(DEPDIR)/libsciast_la-variables.Tpo -c -o src/cpp/symbol/libsciast_la-variables.lo `test -f 'src/cpp/symbol/variables.cpp' || echo '$(srcdir)/'`src/cpp/symbol/variables.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) src/cpp/symbol/$(DEPDIR)/libsciast_la-variables.Tpo src/cpp/symbol/$(DEPDIR)/libsciast_la-variables.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='src/cpp/symbol/variables.cpp' object='src/cpp/symbol/libsciast_la-variables.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o src/cpp/symbol/libsciast_la-variables.lo `test -f 'src/cpp/symbol/variables.cpp' || echo '$(srcdir)/'`src/cpp/symbol/variables.cpp
+
 src/cpp/system_env/libsciast_la-configvariable.lo: src/cpp/system_env/configvariable.cpp
 @am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciast_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT src/cpp/system_env/libsciast_la-configvariable.lo -MD -MP -MF src/cpp/system_env/$(DEPDIR)/libsciast_la-configvariable.Tpo -c -o src/cpp/system_env/libsciast_la-configvariable.lo `test -f 'src/cpp/system_env/configvariable.cpp' || echo '$(srcdir)/'`src/cpp/system_env/configvariable.cpp
 @am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) src/cpp/system_env/$(DEPDIR)/libsciast_la-configvariable.Tpo src/cpp/system_env/$(DEPDIR)/libsciast_la-configvariable.Plo
index be92d27..0e5c54c 100644 (file)
@@ -465,7 +465,9 @@ lib /DEF:"$(ProjectDir)string_import.def" /SUBSYSTEM:WINDOWS /MACHINE:$(Platform
     <ClCompile Include="src\cpp\parse\printerror.cpp" />
     <ClCompile Include="src\cpp\parse\scanscilab.cpp" />
     <ClCompile Include="src\cpp\symbol\context.cpp" />
+    <ClCompile Include="src\cpp\symbol\libraries.cpp" />
     <ClCompile Include="src\cpp\symbol\symbol.cpp" />
+    <ClCompile Include="src\cpp\symbol\variables.cpp" />
     <ClCompile Include="src\cpp\system_env\configvariable.cpp" />
     <ClCompile Include="src\cpp\system_env\configvariable_interface.cpp" />
     <ClCompile Include="src\cpp\system_env\context_get.cpp" />
index c64bc2d..4207520 100644 (file)
     <ClCompile Include="src\cpp\system_env\numericconstants_interface.cpp">
       <Filter>Source Files\system_env</Filter>
     </ClCompile>
+    <ClCompile Include="src\cpp\symbol\libraries.cpp">
+      <Filter>Source Files\symbol</Filter>
+    </ClCompile>
+    <ClCompile Include="src\cpp\symbol\variables.cpp">
+      <Filter>Source Files\symbol</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>
\ No newline at end of file
index 5df413c..c3f4c11 100644 (file)
@@ -39,59 +39,21 @@ struct Library
 
     Library(const Symbol& _name) : name(_name), m_global(false) {};
 
-    void put(types::Library* _pLib, int _iLevel)
-    {
-        if (empty() || top()->m_iLevel < _iLevel)
-        {
-            //create a new level
-            stack.push(new ScopedLibrary(_iLevel, _pLib));
-            _pLib->IncreaseRef();
-        }
-        else
-        {
-            //update current level
-            types::Library* pLib = top()->m_pLib;
-            if (pLib != _pLib)
-            {
-                pLib->DecreaseRef();
-                pLib->killMe();
-                top()->m_pLib = _pLib;
-                _pLib->IncreaseRef();
-            }
-        }
-    }
+    void put(types::Library* _pLib, int _iLevel);
+    types::MacroFile* get(const Symbol& _keyMacro) const;
+    int getMacrosName(std::list<std::wstring>& lst);
 
-    void put(ScopedLibrary* pSL)
+    inline void put(ScopedLibrary* pSL)
     {
         stack.push(pSL);
     }
 
-    types::MacroFile* get(const Symbol& _keyMacro) const
-    {
-        if (empty() == false)
-        {
-            return top()->getMacroFile(_keyMacro);
-        }
-
-        return nullptr;
-    }
-
-    int getMacrosName(std::list<std::wstring>& lst)
-    {
-        if (empty() == false)
-        {
-            top()->m_pLib->getMacrosName(lst);
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    bool empty() const
+    inline bool empty() const
     {
         return stack.empty();
     }
 
-    ScopedLibrary* top() const
+    inline ScopedLibrary* top() const
     {
         return stack.top();
     }
@@ -116,241 +78,21 @@ struct Libraries
 {
     typedef std::map<Symbol, Library*> MapLibs;
 
-    Libraries() {};
-
-    Library* getOrCreate(const Symbol& _key)
-    {
-        MapLibs::const_iterator it = libs.find(_key);
-        if (it == libs.end())
-        {
-            //create an empty StackedValues
-            Library* lib = new Library(_key);
-            libs[_key] = lib;
-            return lib;
-        }
-
-        return it->second;
-    }
-
-    int getLevel(const Symbol& _key) const
-    {
-        MapLibs::const_iterator it = libs.find(_key);
-        if (it != libs.end())
-        {
-            if (!it->second->empty())
-            {
-                return it->second->top()->m_iLevel;
-            }
-        }
-        else
-        {
-            for (auto i = libs.rbegin(), end = libs.rend(); i != end; ++i)
-            {
-                Library * lib = i->second;
-                if (!lib->empty())
-                {
-                    types::MacroFile * pMF = lib->get(_key);
-                    if (pMF)
-                    {
-                        return lib->top()->m_iLevel;
-                    }
-                }
-            }
-        }
-
-        return SCOPE_ALL;
-    }
-
-    void put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
-    {
-        Library* lib = getOrCreate(_keyLib);
-        lib->put(_pLib, _iLevel);
-    }
-
-    bool putInPreviousScope(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
-    {
-        Library* lib = getOrCreate(_keyLib);
-
-        if (lib->empty())
-        {
-            lib->put(_pLib, _iLevel);
-        }
-        else if (lib->top()->m_iLevel > _iLevel)
-        {
-            ScopedLibrary* pLib = lib->top();
-            lib->pop();
-            putInPreviousScope(_keyLib, _pLib, _iLevel);
-            //decresef ref before, increase it in put
-            //pVar->m_pIT->DecreaseRef();
-            lib->put(pLib->m_pLib, pLib->m_iLevel);
-        }
-        else
-        {
-            lib->put(_pLib, _iLevel);
-        }
-
-        return true;
-    }
-
-    types::InternalType* get(const Symbol& _key, int _iLevel)
-    {
-        //does _key is a lib name
-        auto lib = libs.find(_key);
-        if (lib != libs.end())
-        {
-            if (lib->second->empty() == false)
-            {
-                if (_iLevel == SCOPE_ALL || lib->second->top()->m_iLevel == _iLevel)
-                {
-                    return lib->second->top()->m_pLib;
-                }
-            }
-        }
-
-        //does _key is a macro in a lib
-        auto it = libs.rbegin();
-        for (auto it = libs.rbegin(), itEnd = libs.rend(); it != itEnd ; ++it)
-        {
-            Library* lib = it->second;
-            if (it->second->empty() == false)
-            {
-                if (_iLevel == SCOPE_ALL || it->second->top()->m_iLevel == _iLevel)
-                {
-                    types::MacroFile* pMF = it->second->get(_key);
-                    if (pMF)
-                    {
-                        return (types::InternalType*)pMF;
-                    }
-                }
-            }
-        }
-
-        return NULL;
-    }
-
-    bool remove(const Symbol& _key, int _iLevel)
-    {
-        MapLibs::iterator it = libs.find(_key);
-        if (it != libs.end())
-        {
-            if (it->second->empty() == false)
-            {
-                if (it->second->top()->m_iLevel == _iLevel)
-                {
-                    ScopedLibrary * pSL = it->second->top();
-                    types::Library* pIT = pSL->m_pLib;
-                    pIT->DecreaseRef();
-                    pIT->killMe();
-                    it->second->pop();
-                    delete pSL;
-                    return true;
-                }
-            }
-        }
-
-        return false;
-    }
-
-    int getMacrosName(std::list<std::wstring>& lst)
-    {
-        MapLibs::iterator it = libs.begin();
-        MapLibs::iterator itEnd = libs.end();
-        for (auto it : libs)
-        {
-            it.second->getMacrosName(lst);
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    int getVarsName(std::list<std::wstring>& lst)
-    {
-        for (auto it : libs)
-        {
-            if (it.second->empty() == false)
-            {
-                lst.push_back(it.first.getName().c_str());
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    int getVarsToVariableBrowser(std::list<Library*>& lst)
-    {
-        for (auto lib : libs)
-        {
-            if (lib.second->empty() == false)
-            {
-                lst.push_back(lib.second);
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    void clearAll()
-    {
-        for (auto lib : libs)
-        {
-            while (!lib.second->empty())
-            {
-                ScopedLibrary * pSL = lib.second->top();
-                types::InternalType * pIT = pSL->m_pLib;
-                pIT->DecreaseRef();
-                pIT->killMe();
-                lib.second->pop();
-                delete pSL;
-            }
-
-            delete lib.second;
-        }
-    }
-
-    bool getVarsNameForWho(std::list<std::wstring>* lstVarName, int* iVarLenMax, bool bSorted = false) const
-    {
-        for (auto it = libs.begin(), itEnd = libs.end(); it != itEnd; ++it)
-        {
-            std::wstring wstrVarName(it->first.getName().c_str());
-            if (lstVarName && it->second->empty() == false)
-            {
-                lstVarName->push_back(wstrVarName);
-                *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
-            }
-        }
-
-        if (bSorted)
-        {
-            if (lstVarName)
-            {
-                lstVarName->sort();
-            }
-        }
-
-        return true;
-    }
-
-    int whereis(std::list<std::wstring>& lst, const Symbol& _key)
-    {
-        for (auto lib : libs)
-        {
-            if (lib.second->get(_key) != NULL)
-            {
-                lst.push_back(lib.first.getName());
-            }
-        }
-        return static_cast<int>(lst.size());
-    }
-
-    int librarieslist(std::list<std::wstring>& lst)
-    {
-        for (auto lib : libs)
-        {
-            lst.push_back(lib.first.getName());
-        }
-
-        return static_cast<int>(lst.size());
-    }
+    Libraries() {}
+
+    Library* getOrCreate(const Symbol& _key);
+    int getLevel(const Symbol& _key) const;
+    void put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel);
+    bool putInPreviousScope(const Symbol& _keyLib, types::Library* _pLib, int _iLevel);
+    types::InternalType* get(const Symbol& _key, int _iLevel);
+    bool remove(const Symbol& _key, int _iLevel);
+    int getMacrosName(std::list<std::wstring>& lst);
+    int getVarsName(std::list<std::wstring>& lst);
+    int getVarsToVariableBrowser(std::list<Library*>& lst);
+    void clearAll();
+    bool getVarsNameForWho(std::list<std::wstring>* lstVarName, int* iVarLenMax, bool bSorted = false) const;
+    int whereis(std::list<std::wstring>& lst, const Symbol& _key);
+    int librarieslist(std::list<std::wstring>& lst);
 
 private:
     MapLibs libs;
index 258d79a..53140cb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- *  Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
+ *  Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
  *
  *  This file must be used under the terms of the CeCILL.
  *  This source file is licensed as described in the file COPYING, which
@@ -18,6 +18,7 @@
 #include "symbol.hxx"
 #include "internal.hxx"
 #include "double.hxx"
+#include "dynlib_ast.h"
 
 namespace symbol
 {
@@ -32,67 +33,23 @@ struct ScopedVariable
     bool protect;
 };
 
-struct Variable
+struct EXTERN_AST Variable
 {
     typedef std::stack<ScopedVariable*> StackVar;
 
     Variable(const Symbol& _name) : name(_name), m_Global(false), m_GlobalValue(NULL), last(nullptr) {};
-    ~Variable()
-    {
-        while (!empty())
-        {
-            ScopedVariable * pSV = top();
-            types::InternalType * pIT = pSV->m_pIT;
-            pIT->DecreaseRef();
-            pIT->killMe();
-            pop();
-            delete pSV;
-        }
+    ~Variable();
 
-        if (m_GlobalValue)
-        {
-            m_GlobalValue->DecreaseRef();
-            m_GlobalValue->killMe();
-        }
-    }
+    void put(types::InternalType* _pIT, int _iLevel);
+    void setGlobalValue(types::InternalType* _pIT);
+    void setGlobalVisible(int _iLevel, bool _bVisible);
 
-    void put(ScopedVariable* pSV)
+    inline void put(ScopedVariable* pSV)
     {
         last = pSV;
         stack.push(last);
     }
 
-    void put(types::InternalType* _pIT, int _iLevel)
-    {
-        if (isGlobal() && isGlobalVisible(_iLevel))
-        {
-            setGlobalValue(_pIT);
-            return;
-        }
-
-        if (empty() || top()->m_iLevel < _iLevel)
-        {
-            //create a new level
-            last = new ScopedVariable(_iLevel, _pIT);
-            stack.push(last);
-            _pIT->IncreaseRef();
-        }
-        else
-        {
-            //update current level
-            types::InternalType* pIT = top()->m_pIT;
-            if (pIT != _pIT)
-            {
-                // _pIT may contained in pIT
-                // so increases ref of _pIT before kill pIT
-                top()->m_pIT = _pIT;
-                _pIT->IncreaseRef();
-                pIT->DecreaseRef();
-                pIT->killMe();
-            }
-        }
-    }
-
     inline types::InternalType* get() const
     {
         if (empty())
@@ -163,35 +120,6 @@ struct Variable
         return false;
     }
 
-    void setGlobalVisible(int _iLevel, bool _bVisible)
-    {
-        if (empty() || top()->m_iLevel != _iLevel)
-        {
-            last = new ScopedVariable(_iLevel, types::Double::Empty());
-            stack.push(last);
-        }
-
-        top()->m_globalVisible = _bVisible;
-    }
-
-    void setGlobalValue(types::InternalType* _pIT)
-    {
-        if (m_GlobalValue != _pIT)
-        {
-            if (m_GlobalValue)
-            {
-                m_GlobalValue->DecreaseRef();
-                m_GlobalValue->killMe();
-            }
-
-            m_GlobalValue = _pIT;
-            if (_pIT != NULL)
-            {
-                _pIT->IncreaseRef();
-            }
-        }
-    }
-
     inline types::InternalType* getGlobalValue()
     {
         return m_GlobalValue;
@@ -211,350 +139,33 @@ struct Variables
 
     Variables() {};
 
-    Variable* getOrCreate(const Symbol& _key)
-    {
-        MapVars::const_iterator it = vars.find(_key);
-        if (it == vars.end())
-        {
-            //create an empty StackedValues
-            Variable* var = new Variable(_key);
-            vars[_key] = var;
-            return var;
-        }
-
-        return it->second;
-    }
-
-    int getLevel(const Symbol& _key) const
-    {
-        MapVars::const_iterator it = vars.find(_key);
-        if (it != vars.end() && !it->second->empty())
-        {
-            return it->second->top()->m_iLevel;
-        }
-
-        return SCOPE_ALL;
-    }
-
-    void put(const Symbol& _key, types::InternalType* _pIT, int _iLevel)
-    {
-        Variable* var = getOrCreate(_key);
-        var->put(_pIT, _iLevel);
-    }
-
-    types::InternalType* get(const Symbol& _key, int _iLevel)
-    {
-        MapVars::const_iterator it = vars.find(_key);
-        if (it != vars.end() && it->second->empty() == false)
-        {
-            if (_iLevel == SCOPE_ALL || it->second->top()->m_iLevel == _iLevel)
-            {
-                return it->second->get();
-            }
-        }
-
-        return NULL;
-    }
-
-    types::InternalType* get(Variable* _var, int _iLevel)
-    {
-        if (_var != NULL && _var->empty() == false)
-        {
-            if (_iLevel == SCOPE_ALL || _var->top()->m_iLevel == _iLevel)
-            {
-                return _var->get();
-            }
-        }
-
-        return NULL;
-    }
-
-    types::InternalType* getAllButCurrentLevel(const Symbol& _key, int _iLevel)
-    {
-        MapVars::const_iterator it = vars.find(_key);
-        if (it != vars.end() && it->second->empty() == false)
-        {
-            if (it->second->top()->m_iLevel < _iLevel)
-            {
-                return it->second->get();
-            }
-            else
-            {
-                ScopedVariable* pSave = it->second->top();
-                it->second->pop();
-                types::InternalType* pIT = getAllButCurrentLevel(_key, _iLevel);
-                it->second->put(pSave);
-                return pIT;
-            }
-        }
-
-        return NULL;
-    }
-
-    bool remove(Variable* _var, int _iLevel)
-    {
-        if (_var->empty() == false)
-        {
-            if (_var->top()->m_iLevel == _iLevel)
-            {
-                ScopedVariable* pSave = _var->top();
-                types::InternalType* pIT = pSave->m_pIT;
-                pIT->DecreaseRef();
-                pIT->killMe();
-                _var->pop();
-                delete pSave;
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-    bool remove(const Symbol& _key, int _iLevel)
-    {
-        MapVars::iterator it = vars.find(_key);
-        if (it != vars.end())
-        {
-            Variable* pVar = it->second;
-            return remove(pVar, _iLevel);
-        }
-
-        return false;
-    }
-
-    int getMacrosName(std::list<std::wstring>& lst)
-    {
-        for (auto it : vars)
-        {
-            if (it.second->empty() == false)
-            {
-                types::InternalType* pIT = it.second->top()->m_pIT;
-                if (pIT && (pIT->isMacro() || pIT->isMacroFile()))
-                {
-                    lst.push_back(it.first.getName().c_str());
-                }
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    int getVarsName(std::list<std::wstring>& lst)
-    {
-        for (auto it : vars)
-        {
-            if (it.second->empty() == false)
-            {
-                types::InternalType* pIT = it.second->top()->m_pIT;
-                if (pIT &&
-                        pIT->isMacro() == false &&
-                        pIT->isMacroFile() == false &&
-                        pIT->isFunction() == false)
-                {
-                    lst.push_back(it.first.getName().c_str());
-                }
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    bool getVarsNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted = false) const
-    {
-        for (auto it : vars)
-        {
-            std::wstring wstrVarName(it.first.getName().c_str());
-            if (it.second->empty() == false)
-            {
-                types::InternalType* pIT = it.second->top()->m_pIT;
-                if (pIT && pIT->isFunction() == false)
-                {
-                    lstVarName.push_back(wstrVarName);
-                    *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
-                }
-            }
-        }
-
-        if (bSorted)
-        {
-            lstVarName.sort();
-        }
-
-        return true;
-    }
-
-    bool getGlobalNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted = false) const
-    {
-        for (auto it : vars)
-        {
-            if (it.second->empty() == false && it.second->isGlobal())
-            {
-                std::wstring wstrVarName(it.first.getName().c_str());
-                lstVarName.push_back(wstrVarName);
-                *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
-            }
-        }
-
-        if (bSorted)
-        {
-            lstVarName.sort();
-        }
-
-        return true;
-    }
-
-    int getProtectedVarsName(std::list<std::wstring>& lstVarName) const
-    {
-        for (auto it : vars)
-        {
-            if (it.second->empty() == false)
-            {
-                ScopedVariable* pSV = it.second->top();
-                if (pSV->protect && it.first.getName() != L"ans")
-                {
-                    lstVarName.push_back(it.first.getName());
-                }
-            }
-        }
-
-        return static_cast<int>(lstVarName.size());
-    }
-
-    int getFunctionsName(std::list<std::wstring>& lst)
-    {
-        for (auto it : vars)
-        {
-            if (it.second->empty() == false)
-            {
-                types::InternalType* pIT = it.second->top()->m_pIT;
-                if (pIT && pIT->isFunction())
-                {
-                    lst.push_back(it.first.getName().c_str());
-                }
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    int getFunctionList(std::list<Symbol>& lst, std::wstring _stModuleName, int _iLevel)
-    {
-        for (auto var : vars)
-        {
-            if (var.second->empty())
-            {
-                continue;
-            }
-
-            if ((var.second->top()->m_iLevel == _iLevel || _iLevel == 1) && var.second->top()->m_pIT->isCallable())
-            {
-                types::Callable* pCall = var.second->top()->m_pIT->getAs<types::Callable>();
-                if (_stModuleName == L"" || _stModuleName == pCall->getModule())
-                {
-                    lst.push_back(var.first);
-                }
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    int getVarsToVariableBrowser(std::list<Variable*>& lst)
-    {
-        for (auto var : vars)
-        {
-            if (var.second->empty() == false)
-            {
-                types::InternalType* pIT = var.second->top()->m_pIT;
-                if (pIT &&
-                        pIT->isMacro() == false &&
-                        pIT->isMacroFile() == false &&
-                        pIT->isFunction() == false)
-                {
-                    lst.push_back(var.second);
-                }
-            }
-        }
-
-        return static_cast<int>(lst.size());
-    }
-
-    bool putInPreviousScope(Variable* _var, types::InternalType* _pIT, int _iLevel)
-    {
-        if (_var->empty())
-        {
-            _var->put(_pIT, _iLevel);
-        }
-        else if (_var->top()->m_iLevel > _iLevel)
-        {
-            ScopedVariable* pVar = _var->top();
-            _var->pop();
-            putInPreviousScope(_var, _pIT, _iLevel);
-            //decresef ref before, increase it in put
-            //pVar->m_pIT->DecreaseRef();
-            _var->put(pVar);
-        }
-        else
-        {
-            _var->put(_pIT, _iLevel);
-        }
-
-        return true;
-    }
+    Variable* getOrCreate(const Symbol& _key);
+    int getLevel(const Symbol& _key) const;
+    void put(const Symbol& _key, types::InternalType* _pIT, int _iLevel);
+    types::InternalType* get(const Symbol& _key, int _iLevel);
+    types::InternalType* get(Variable* _var, int _iLevel);
+    types::InternalType* getAllButCurrentLevel(const Symbol& _key, int _iLevel);
+    bool remove(Variable* _var, int _iLevel);
+    bool remove(const Symbol& _key, int _iLevel);
+    int getMacrosName(std::list<std::wstring>& lst);
+    int getVarsName(std::list<std::wstring>& lst);
+    bool getVarsNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted = false) const;
+    bool getGlobalNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted = false) const;
+    int getProtectedVarsName(std::list<std::wstring>& lstVarName) const;
+    int getFunctionsName(std::list<std::wstring>& lst);
+    int getFunctionList(std::list<Symbol>& lst, std::wstring _stModuleName, int _iLevel);
+    int getVarsToVariableBrowser(std::list<Variable*>& lst);
+    bool putInPreviousScope(Variable* _var, types::InternalType* _pIT, int _iLevel);
 
     //globals
 
-    void setGlobal(const Symbol& _key)
-    {
-        getOrCreate(_key)->setGlobal(true);
-    }
-
-    void setGlobalVisible(const Symbol& _key, bool _bVisible, int _iLevel)
-    {
-        Variable* pVar = getOrCreate(_key);
-        pVar->setGlobalVisible(_iLevel, _bVisible);
-        if (_bVisible)
-        {
-            pVar->setGlobal(true);
-        }
-    }
-
-    bool isGlobalVisible(const Symbol& _key, int _iLevel)
-    {
-        return getOrCreate(_key)->isGlobalVisible(_iLevel);
-    }
-
-    bool isGlobal(const Symbol& _key, int /*_iLevel*/)
-    {
-        return getOrCreate(_key)->isGlobal();
-    }
-
-    types::InternalType* getGlobalValue(const Symbol& _key)
-    {
-        return getOrCreate(_key)->getGlobalValue();
-    }
-
-    void removeGlobal(const Symbol& _key, int _iLevel)
-    {
-        Variable* pVar = getOrCreate(_key);
-        if (pVar->isGlobal())
-        {
-            pVar->setGlobal(false);
-            pVar->setGlobalValue(NULL);
-        }
-
-        remove(pVar, _iLevel);
-    }
-
-    void clearAll()
-    {
-        for (auto var : vars)
-        {
-            delete var.second;
-        }
-
-        vars.clear();
-    }
+    void setGlobal(const Symbol& _key);
+    void setGlobalVisible(const Symbol& _key, bool _bVisible, int _iLevel);
+    bool isGlobalVisible(const Symbol& _key, int _iLevel);
+    bool isGlobal(const Symbol& _key, int /*_iLevel*/);
+    types::InternalType* getGlobalValue(const Symbol& _key);
+    void removeGlobal(const Symbol& _key, int _iLevel);
+    void clearAll();
 
 private:
     MapVars vars;
diff --git a/scilab/modules/ast/src/cpp/symbol/libraries.cpp b/scilab/modules/ast/src/cpp/symbol/libraries.cpp
new file mode 100644 (file)
index 0000000..d74e9ef
--- /dev/null
@@ -0,0 +1,287 @@
+/*
+*  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+*  Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
+*
+*  This file must be used under the terms of the CeCILL.
+*  This source file is licensed as described in the file COPYING, which
+*  you should have received as part of this distribution.  The terms
+*  are also available at
+*  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+*/
+
+#include "libraries.hxx"
+
+namespace symbol
+{
+
+void Library::put(types::Library* _pLib, int _iLevel)
+{
+    if (empty() || top()->m_iLevel < _iLevel)
+    {
+        //create a new level
+        stack.push(new ScopedLibrary(_iLevel, _pLib));
+    }
+    else
+    {
+        //update current level
+        types::Library* pLib = top()->m_pLib;
+        if (pLib != _pLib)
+        {
+            pLib->killMe();
+            top()->m_pLib = _pLib;
+        }
+    }
+}
+
+types::MacroFile* Library::get(const Symbol& _keyMacro) const
+{
+    if (empty() == false)
+    {
+        return top()->getMacroFile(_keyMacro);
+    }
+
+    return nullptr;
+}
+
+int Library::getMacrosName(std::list<std::wstring>& lst)
+{
+    if (empty() == false)
+    {
+        top()->m_pLib->getMacrosName(lst);
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+Library* Libraries::getOrCreate(const Symbol& _key)
+{
+    MapLibs::const_iterator it = libs.find(_key);
+    if (it == libs.end())
+    {
+        //create an empty StackedValues
+        Library* lib = new Library(_key);
+        libs[_key] = lib;
+        return lib;
+    }
+
+    return it->second;
+}
+
+int Libraries::getLevel(const Symbol& _key) const
+{
+    MapLibs::const_iterator it = libs.find(_key);
+    if (it != libs.end())
+    {
+        if (!it->second->empty())
+        {
+            return it->second->top()->m_iLevel;
+        }
+    }
+    else
+    {
+        for (auto i = libs.rbegin(), end = libs.rend(); i != end; ++i)
+        {
+            Library * lib = i->second;
+            if (!lib->empty())
+            {
+                types::MacroFile * pMF = lib->get(_key);
+                if (pMF)
+                {
+                    return lib->top()->m_iLevel;
+                }
+            }
+        }
+    }
+
+    return SCOPE_ALL;
+}
+
+void Libraries::put(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
+{
+    Library* lib = getOrCreate(_keyLib);
+    lib->put(_pLib, _iLevel);
+}
+
+bool Libraries::putInPreviousScope(const Symbol& _keyLib, types::Library* _pLib, int _iLevel)
+{
+    Library* lib = getOrCreate(_keyLib);
+
+    if (lib->empty())
+    {
+        lib->put(_pLib, _iLevel);
+    }
+    else if (lib->top()->m_iLevel > _iLevel)
+    {
+        ScopedLibrary* pLib = lib->top();
+        lib->pop();
+        putInPreviousScope(_keyLib, _pLib, _iLevel);
+        lib->put(pLib);
+    }
+    else
+    {
+        lib->put(_pLib, _iLevel);
+    }
+
+    return true;
+}
+
+types::InternalType* Libraries::get(const Symbol& _key, int _iLevel)
+{
+    //does _key is a lib name
+    auto lib = libs.find(_key);
+    if (lib != libs.end())
+    {
+        if (lib->second->empty() == false)
+        {
+            if (_iLevel == SCOPE_ALL || lib->second->top()->m_iLevel == _iLevel)
+            {
+                return lib->second->top()->m_pLib;
+            }
+        }
+    }
+
+    //does _key is a macro in a lib
+    auto it = libs.rbegin();
+    for (auto it = libs.rbegin(), itEnd = libs.rend(); it != itEnd; ++it)
+    {
+        Library* lib = it->second;
+        if (it->second->empty() == false)
+        {
+            if (_iLevel == SCOPE_ALL || it->second->top()->m_iLevel == _iLevel)
+            {
+                types::MacroFile* pMF = it->second->get(_key);
+                if (pMF)
+                {
+                    return (types::InternalType*)pMF;
+                }
+            }
+        }
+    }
+
+    return NULL;
+}
+
+bool Libraries::remove(const Symbol& _key, int _iLevel)
+{
+    MapLibs::iterator it = libs.find(_key);
+    if (it != libs.end())
+    {
+        if (it->second->empty() == false)
+        {
+            if (it->second->top()->m_iLevel == _iLevel)
+            {
+                ScopedLibrary * pSL = it->second->top();
+                types::Library* pIT = pSL->m_pLib;
+                pIT->killMe();
+                it->second->pop();
+                delete pSL;
+                return true;
+            }
+        }
+    }
+
+    return false;
+}
+
+int Libraries::getMacrosName(std::list<std::wstring>& lst)
+{
+    MapLibs::iterator it = libs.begin();
+    MapLibs::iterator itEnd = libs.end();
+    for (auto it : libs)
+    {
+        it.second->getMacrosName(lst);
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+int Libraries::getVarsName(std::list<std::wstring>& lst)
+{
+    for (auto it : libs)
+    {
+        if (it.second->empty() == false)
+        {
+            lst.push_back(it.first.getName().c_str());
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+int Libraries::getVarsToVariableBrowser(std::list<Library*>& lst)
+{
+    for (auto lib : libs)
+    {
+        if (lib.second->empty() == false)
+        {
+            lst.push_back(lib.second);
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+void Libraries::clearAll()
+{
+    for (auto lib : libs)
+    {
+        while (!lib.second->empty())
+        {
+            ScopedLibrary * pSL = lib.second->top();
+            types::InternalType * pIT = pSL->m_pLib;
+            pIT->killMe();
+            lib.second->pop();
+            delete pSL;
+        }
+
+        delete lib.second;
+    }
+}
+
+bool Libraries::getVarsNameForWho(std::list<std::wstring>* lstVarName, int* iVarLenMax, bool bSorted) const
+{
+    for (auto it = libs.begin(), itEnd = libs.end(); it != itEnd; ++it)
+    {
+        std::wstring wstrVarName(it->first.getName().c_str());
+        if (lstVarName && it->second->empty() == false)
+        {
+            lstVarName->push_back(wstrVarName);
+            *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
+        }
+    }
+
+    if (bSorted)
+    {
+        if (lstVarName)
+        {
+            lstVarName->sort();
+        }
+    }
+
+    return true;
+}
+
+int Libraries::whereis(std::list<std::wstring>& lst, const Symbol& _key)
+{
+    for (auto lib : libs)
+    {
+        if (lib.second->get(_key) != NULL)
+        {
+            lst.push_back(lib.first.getName());
+        }
+    }
+    return static_cast<int>(lst.size());
+}
+
+int Libraries::librarieslist(std::list<std::wstring>& lst)
+{
+    for (auto lib : libs)
+    {
+        lst.push_back(lib.first.getName());
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+}
diff --git a/scilab/modules/ast/src/cpp/symbol/variables.cpp b/scilab/modules/ast/src/cpp/symbol/variables.cpp
new file mode 100644 (file)
index 0000000..d83e269
--- /dev/null
@@ -0,0 +1,443 @@
+/*
+*  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+*  Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
+*
+*  This file must be used under the terms of the CeCILL.
+*  This source file is licensed as described in the file COPYING, which
+*  you should have received as part of this distribution.  The terms
+*  are also available at
+*  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+*/
+
+#include "variables.hxx"
+
+namespace symbol
+{
+Variable::~Variable()
+{
+    while (!empty())
+    {
+        ScopedVariable * pSV = top();
+        types::InternalType * pIT = pSV->m_pIT;
+        pIT->DecreaseRef();
+        pIT->killMe();
+        pop();
+        delete pSV;
+    }
+
+    if (m_GlobalValue)
+    {
+        m_GlobalValue->DecreaseRef();
+        m_GlobalValue->killMe();
+    }
+}
+
+void Variable::setGlobalValue(types::InternalType* _pIT)
+{
+    if (m_GlobalValue != _pIT)
+    {
+        if (m_GlobalValue)
+        {
+            m_GlobalValue->DecreaseRef();
+            m_GlobalValue->killMe();
+        }
+
+        m_GlobalValue = _pIT;
+        if (_pIT != NULL)
+        {
+            _pIT->IncreaseRef();
+        }
+    }
+}
+
+void Variable::setGlobalVisible(int _iLevel, bool _bVisible)
+{
+    if (empty() || top()->m_iLevel != _iLevel)
+    {
+        last = new ScopedVariable(_iLevel, types::Double::Empty());
+        stack.push(last);
+    }
+
+    top()->m_globalVisible = _bVisible;
+}
+
+
+void Variable::put(types::InternalType* _pIT, int _iLevel)
+{
+    if (isGlobal() && isGlobalVisible(_iLevel))
+    {
+        setGlobalValue(_pIT);
+        return;
+    }
+
+    if (empty() || top()->m_iLevel < _iLevel)
+    {
+        //create a new level
+        last = new ScopedVariable(_iLevel, _pIT);
+        stack.push(last);
+        _pIT->IncreaseRef();
+    }
+    else
+    {
+        //update current level
+        types::InternalType* pIT = top()->m_pIT;
+        if (pIT != _pIT)
+        {
+            // _pIT may contained in pIT
+            // so increases ref of _pIT before kill pIT
+            top()->m_pIT = _pIT;
+            _pIT->IncreaseRef();
+            pIT->DecreaseRef();
+            pIT->killMe();
+        }
+    }
+}
+
+Variable* Variables::getOrCreate(const Symbol& _key)
+{
+    MapVars::const_iterator it = vars.find(_key);
+    if (it == vars.end())
+    {
+        //create an empty StackedValues
+        Variable* var = new Variable(_key);
+        vars[_key] = var;
+        return var;
+    }
+
+    return it->second;
+}
+
+int Variables::getLevel(const Symbol& _key) const
+{
+    MapVars::const_iterator it = vars.find(_key);
+    if (it != vars.end() && !it->second->empty())
+    {
+        return it->second->top()->m_iLevel;
+    }
+
+    return SCOPE_ALL;
+}
+
+void Variables::put(const Symbol& _key, types::InternalType* _pIT, int _iLevel)
+{
+    Variable* var = getOrCreate(_key);
+    var->put(_pIT, _iLevel);
+}
+
+types::InternalType* Variables::get(const Symbol& _key, int _iLevel)
+{
+    MapVars::const_iterator it = vars.find(_key);
+    if (it != vars.end() && it->second->empty() == false)
+    {
+        if (_iLevel == SCOPE_ALL || it->second->top()->m_iLevel == _iLevel)
+        {
+            return it->second->get();
+        }
+    }
+
+    return NULL;
+}
+
+types::InternalType* Variables::get(Variable* _var, int _iLevel)
+{
+    if (_var != NULL && _var->empty() == false)
+    {
+        if (_iLevel == SCOPE_ALL || _var->top()->m_iLevel == _iLevel)
+        {
+            return _var->get();
+        }
+    }
+
+    return NULL;
+}
+
+types::InternalType* Variables::getAllButCurrentLevel(const Symbol& _key, int _iLevel)
+{
+    MapVars::const_iterator it = vars.find(_key);
+    if (it != vars.end() && it->second->empty() == false)
+    {
+        if (it->second->top()->m_iLevel < _iLevel)
+        {
+            return it->second->get();
+        }
+        else
+        {
+            ScopedVariable* pSave = it->second->top();
+            it->second->pop();
+            types::InternalType* pIT = getAllButCurrentLevel(_key, _iLevel);
+            it->second->put(pSave);
+            return pIT;
+        }
+    }
+
+    return NULL;
+}
+
+bool Variables::remove(Variable* _var, int _iLevel)
+{
+    if (_var->empty() == false)
+    {
+        if (_var->top()->m_iLevel == _iLevel)
+        {
+            ScopedVariable* pSave = _var->top();
+            types::InternalType* pIT = pSave->m_pIT;
+            pIT->DecreaseRef();
+            pIT->killMe();
+            _var->pop();
+            delete pSave;
+            return true;
+        }
+    }
+
+    return false;
+}
+
+bool Variables::remove(const Symbol& _key, int _iLevel)
+{
+    MapVars::iterator it = vars.find(_key);
+    if (it != vars.end())
+    {
+        Variable* pVar = it->second;
+        return remove(pVar, _iLevel);
+    }
+
+    return false;
+}
+
+int Variables::getMacrosName(std::list<std::wstring>& lst)
+{
+    for (auto it : vars)
+    {
+        if (it.second->empty() == false)
+        {
+            types::InternalType* pIT = it.second->top()->m_pIT;
+            if (pIT && (pIT->isMacro() || pIT->isMacroFile()))
+            {
+                lst.push_back(it.first.getName().c_str());
+            }
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+int Variables::getVarsName(std::list<std::wstring>& lst)
+{
+    for (auto it : vars)
+    {
+        if (it.second->empty() == false)
+        {
+            types::InternalType* pIT = it.second->top()->m_pIT;
+            if (pIT &&
+                    pIT->isMacro() == false &&
+                    pIT->isMacroFile() == false &&
+                    pIT->isFunction() == false)
+            {
+                lst.push_back(it.first.getName().c_str());
+            }
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+bool Variables::getVarsNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted) const
+{
+    for (auto it : vars)
+    {
+        std::wstring wstrVarName(it.first.getName().c_str());
+        if (it.second->empty() == false)
+        {
+            types::InternalType* pIT = it.second->top()->m_pIT;
+            if (pIT && pIT->isFunction() == false)
+            {
+                lstVarName.push_back(wstrVarName);
+                *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
+            }
+        }
+    }
+
+    if (bSorted)
+    {
+        lstVarName.sort();
+    }
+
+    return true;
+}
+
+bool Variables::getGlobalNameForWho(std::list<std::wstring>& lstVarName, int* iVarLenMax, bool bSorted) const
+{
+    for (auto it : vars)
+    {
+        if (it.second->empty() == false && it.second->isGlobal())
+        {
+            std::wstring wstrVarName(it.first.getName().c_str());
+            lstVarName.push_back(wstrVarName);
+            *iVarLenMax = std::max(*iVarLenMax, (int)wstrVarName.size());
+        }
+    }
+
+    if (bSorted)
+    {
+        lstVarName.sort();
+    }
+
+    return true;
+}
+
+int Variables::getProtectedVarsName(std::list<std::wstring>& lstVarName) const
+{
+    for (auto it : vars)
+    {
+        if (it.second->empty() == false)
+        {
+            ScopedVariable* pSV = it.second->top();
+            if (pSV->protect && it.first.getName() != L"ans")
+            {
+                lstVarName.push_back(it.first.getName());
+            }
+        }
+    }
+
+    return static_cast<int>(lstVarName.size());
+}
+
+int Variables::getFunctionsName(std::list<std::wstring>& lst)
+{
+    for (auto it : vars)
+    {
+        if (it.second->empty() == false)
+        {
+            types::InternalType* pIT = it.second->top()->m_pIT;
+            if (pIT && pIT->isFunction())
+            {
+                lst.push_back(it.first.getName().c_str());
+            }
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+int Variables::getFunctionList(std::list<Symbol>& lst, std::wstring _stModuleName, int _iLevel)
+{
+    for (auto var : vars)
+    {
+        if (var.second->empty())
+        {
+            continue;
+        }
+
+        if ((var.second->top()->m_iLevel == _iLevel || _iLevel == 1) && var.second->top()->m_pIT->isCallable())
+        {
+            types::Callable* pCall = var.second->top()->m_pIT->getAs<types::Callable>();
+            if (_stModuleName == L"" || _stModuleName == pCall->getModule())
+            {
+                lst.push_back(var.first);
+            }
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+int Variables::getVarsToVariableBrowser(std::list<Variable*>& lst)
+{
+    for (auto var : vars)
+    {
+        if (var.second->empty() == false)
+        {
+            types::InternalType* pIT = var.second->top()->m_pIT;
+            if (pIT &&
+                    pIT->isMacro() == false &&
+                    pIT->isMacroFile() == false &&
+                    pIT->isFunction() == false)
+            {
+                lst.push_back(var.second);
+            }
+        }
+    }
+
+    return static_cast<int>(lst.size());
+}
+
+bool Variables::putInPreviousScope(Variable* _var, types::InternalType* _pIT, int _iLevel)
+{
+    if (_var->empty())
+    {
+        _var->put(_pIT, _iLevel);
+    }
+    else if (_var->top()->m_iLevel > _iLevel)
+    {
+        ScopedVariable* pVar = _var->top();
+        _var->pop();
+        putInPreviousScope(_var, _pIT, _iLevel);
+        //decresef ref before, increase it in put
+        //pVar->m_pIT->DecreaseRef();
+        _var->put(pVar);
+    }
+    else
+    {
+        _var->put(_pIT, _iLevel);
+    }
+
+    return true;
+}
+
+//globals
+
+void Variables::setGlobal(const Symbol& _key)
+{
+    getOrCreate(_key)->setGlobal(true);
+}
+
+void Variables::setGlobalVisible(const Symbol& _key, bool _bVisible, int _iLevel)
+{
+    Variable* pVar = getOrCreate(_key);
+    pVar->setGlobalVisible(_iLevel, _bVisible);
+    if (_bVisible)
+    {
+        pVar->setGlobal(true);
+    }
+}
+
+bool Variables::isGlobalVisible(const Symbol& _key, int _iLevel)
+{
+    return getOrCreate(_key)->isGlobalVisible(_iLevel);
+}
+
+bool Variables::isGlobal(const Symbol& _key, int /*_iLevel*/)
+{
+    return getOrCreate(_key)->isGlobal();
+}
+
+types::InternalType* Variables::getGlobalValue(const Symbol& _key)
+{
+    return getOrCreate(_key)->getGlobalValue();
+}
+
+void Variables::removeGlobal(const Symbol& _key, int _iLevel)
+{
+    Variable* pVar = getOrCreate(_key);
+    if (pVar->isGlobal())
+    {
+        pVar->setGlobal(false);
+        pVar->setGlobalValue(NULL);
+    }
+
+    remove(pVar, _iLevel);
+}
+
+void Variables::clearAll()
+{
+    for (auto var : vars)
+    {
+        delete var.second;
+    }
+
+    vars.clear();
+}
+
+
+}
\ No newline at end of file
index 528493c..c3012a0 100644 (file)
@@ -28,6 +28,9 @@ namespace types
 Library::Library(const std::wstring& _wstPath) :
     m_wstPath(_wstPath)
 {
+#ifndef NDEBUG
+    Inspector::addItem(this);
+#endif
 }
 
 Library::~Library()
@@ -44,6 +47,9 @@ Library::~Library()
     }
 
     m_macros.clear();
+#ifndef NDEBUG
+    Inspector::removeItem(this);
+#endif
 }
 
 bool Library::toString(std::wostringstream& ostr)