Analysis: just use one 'safe' tag 50/16750/2
Calixte DENIZET [Wed, 1 Jul 2015 08:22:31 +0000 (10:22 +0200)]
Change-Id: Idf9416ae2788ce6845c2da3005befcfb419a3072

scilab/modules/ast/includes/analysis/Decorator.hxx
scilab/modules/ast/src/cpp/analysis/IndexAnalyzer.cpp
scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp
scilab/modules/ast/src/cpp/ast/debugvisitor.cpp

index ed0359d..cff367d 100644 (file)
@@ -28,11 +28,9 @@ struct Decorator
     bool cloneData;
     bool deleteData;
     bool hasRefCount;
-    bool safeIndex;
-    bool safeInsertion;
     bool safe;
 
-    Decorator() : res(), call(nullptr), cloneData(false), deleteData(false), hasRefCount(false), safeIndex(false), safeInsertion(false), safe(false) { }
+    Decorator() : res(), call(nullptr), cloneData(false), deleteData(false), hasRefCount(false), safe(false) { }
 
     ~Decorator()
     {
@@ -46,28 +44,28 @@ struct Decorator
 
     inline Call & setCall(Call * _call)
     {
-       delete call;
+        delete call;
         call = _call;
         return *call;
     }
 
     inline Call & setCall(const std::wstring & name)
     {
-       delete call;
+        delete call;
         call = new Call(name);
         return *call;
     }
 
     inline Call & setCall(const std::wstring & name, const std::vector<TIType> & args)
     {
-       delete call;
+        delete call;
         call = new Call(name, args);
         return *call;
     }
-    
+
     inline Call & setCall(const std::wstring & name, const TIType & arg)
     {
-       delete call;
+        delete call;
         call = new Call(name, arg);
         return *call;
     }
@@ -98,9 +96,7 @@ struct Decorator
         out << L", Cl:" << (deco.cloneData ? L"T" : L"F")
             << L", Del:" << (deco.deleteData ? L"T" : L"F")
             << L", RefC:" << (deco.hasRefCount ? L"T" : L"F")
-            << L", SafeIndex:" << (deco.safeIndex ? L"T" : L"F")
-            << L", SafeInsertion:" << (deco.safeInsertion ? L"T" : L"F")
-           << L", Safe:" << (deco.safe ? L"T" : L"F");
+            << L", Safe:" << (deco.safe ? L"T" : L"F");
 
         return out;
     }
index f2266f4..bd8ecc9 100644 (file)
 
 namespace analysis
 {
-    
-    bool AnalysisVisitor::analyzeIndices(TIType & type, ast::CallExp & ce)
+
+bool AnalysisVisitor::analyzeIndices(TIType & type, ast::CallExp & ce)
+{
+    const ast::exps_t args = ce.getArgs();
+    const unsigned int size = args.size();
+
+    if (size >= 3)
+    {
+        // Not handle yet...
+        // TODO
+        return false;
+    }
+
+    if (size == 0)
+    {
+        Result & res = ce.getDecorator().setResult(type);
+        setResult(res);
+        return true;
+    }
+
+    SymbolicDimension first, second;
+    bool safe, ret;
+
+    if (size == 1)
     {
-        const ast::exps_t args = ce.getArgs();
-        const unsigned int size = args.size();
+        // when there is one argument, a(?) is equivalent to A(?,1)
+        // where A = matrix(a, r_a * c_a, 1)
 
-        if (size >= 3)
+        SymbolicDimension rows(type.rows);
+        second = SymbolicDimension(getGVN(), 1.);
+        if (type.cols != 1)
         {
-            // Not handle yet...
-            // TODO
-            return false;
+            rows *= type.cols;
         }
 
-        if (size == 0)
+        ret = getDimension(rows, *args.front(), safe, first);
+    }
+    else
+    {
+        bool _safe;
+        ret = getDimension(type.rows, *args.front(), _safe, first);
+        if (ret)
         {
-            Result & res = ce.getDecorator().setResult(type);
-            setResult(res);
-            return true;
+            ret = getDimension(type.cols, *args.back(), safe, second);
+            safe = safe && _safe;
         }
-
-        SymbolicDimension first, second;
-       bool safe, ret;
-
-        if (size == 1)
+        else
         {
-            // when there is one argument, a(?) is equivalent to A(?,1)
-            // where A = matrix(a, r_a * c_a, 1)
-
-           SymbolicDimension rows(type.rows);
-           second = SymbolicDimension(getGVN(), 1.);
-            if (type.cols != 1)
-            {
-                rows *= type.cols;
-            }
-
-           ret = getDimension(rows, *args.front(), safe, first);
+            safe = _safe;
         }
-       else
-       {
-           bool _safe;
-           ret = getDimension(type.rows, *args.front(), _safe, first);
-           if (ret)
-           {
-               ret = getDimension(type.cols, *args.back(), safe, second);
-               safe = safe && _safe;
-           }
-           else
-           {
-               safe = _safe;
-           }
-       }
-
-       if (ret)
-       {
-           TIType typ(getGVN(), type.type, first, second);
-           Result & _res = ce.getDecorator().setResult(typ);
-            setResult(_res);
-           ce.getDecorator().safeIndex = safe;
-       }
+    }
 
-       return ret;
+    if (ret)
+    {
+        TIType typ(getGVN(), type.type, first, second);
+        Result & _res = ce.getDecorator().setResult(typ);
+        setResult(_res);
+        ce.getDecorator().safe = safe;
     }
 
+    return ret;
+}
+
 
-    bool AnalysisVisitor::getDimension(SymbolicDimension & dim, ast::Exp & arg, bool & safe, SymbolicDimension & out)
+bool AnalysisVisitor::getDimension(SymbolicDimension & dim, ast::Exp & arg, bool & safe, SymbolicDimension & out)
+{
+    switch (arg.getType())
     {
-        switch (arg.getType())
-        {
         case ast::Exp::COLONVAR :
         {
-           out = dim;
-           safe = true;
+            out = dim;
+            safe = true;
             return true;
         }
         case ast::Exp::DOLLARVAR : // a($)
         {
-           out = SymbolicDimension(getGVN(), 1.);
-           safe = true;
+            out = SymbolicDimension(getGVN(), 1.);
+            safe = true;
             return true;
         }
         case ast::Exp::DOUBLEEXP : // a(12) or a([1 2])
@@ -104,83 +104,83 @@ namespace analysis
                     types::Double * const pDbl = static_cast<types::Double *>(pIT);
                     if (pDbl->isEmpty())
                     {
-                       out = SymbolicDimension(getGVN(), 0.);
-                       safe = true;
+                        out = SymbolicDimension(getGVN(), 0.);
+                        safe = true;
                         return true;
                     }
 
-                   const double * real = pDbl->getReal();
-                   const int size = pDbl->getSize();
-                   int64_t max;
-                   if (tools::asInteger(real[0], max))
-                   {
-                       int64_t min = max;
-                       if (!pDbl->isComplex())
-                       {
-                           for (int i = 0; i < size; ++i)
-                           {
-                               int64_t _real;
-                               if (tools::asInteger(real[i], _real))
-                               {
-                                   if (_real < min)
-                                   {
-                                       min = _real;
-                                   }
-                                   else if (_real > max)
-                                   {
-                                       max = _real;
-                                   }
-                               }
-                               else
-                               {
-                                   return false;
-                               }
-                           }
-                           
-                           out = SymbolicDimension(getGVN(), size);
-                           safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
-                           return true;
-                       }
-                       else
-                       {
-                           const double * imag = pDbl->getImg();
-                           int i;
-                           for (i = 0; i < size; ++i)
-                           {
-                               if (imag[i])
-                               {
-                                   break;
-                               }
-                               int64_t _real;
-                               if (tools::asInteger(real[i], _real))
-                               {
-                                   if (_real < min)
-                                   {
-                                       min = _real;
-                                   }
-                                   else if (_real > max)
-                                   {
-                                       max = _real;
-                                   }
-                               }
-                           }
+                    const double * real = pDbl->getReal();
+                    const int size = pDbl->getSize();
+                    int64_t max;
+                    if (tools::asInteger(real[0], max))
+                    {
+                        int64_t min = max;
+                        if (!pDbl->isComplex())
+                        {
+                            for (int i = 0; i < size; ++i)
+                            {
+                                int64_t _real;
+                                if (tools::asInteger(real[i], _real))
+                                {
+                                    if (_real < min)
+                                    {
+                                        min = _real;
+                                    }
+                                    else if (_real > max)
+                                    {
+                                        max = _real;
+                                    }
+                                }
+                                else
+                                {
+                                    return false;
+                                }
+                            }
+
+                            out = SymbolicDimension(getGVN(), size);
+                            safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
+                            return true;
+                        }
+                        else
+                        {
+                            const double * imag = pDbl->getImg();
+                            int i;
+                            for (i = 0; i < size; ++i)
+                            {
+                                if (imag[i])
+                                {
+                                    break;
+                                }
+                                int64_t _real;
+                                if (tools::asInteger(real[i], _real))
+                                {
+                                    if (_real < min)
+                                    {
+                                        min = _real;
+                                    }
+                                    else if (_real > max)
+                                    {
+                                        max = _real;
+                                    }
+                                }
+                            }
 
-                           if (i == size)
-                           {
-                               out = SymbolicDimension(getGVN(), size);
-                               safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
-                               return true;
-                           }
-                           else
-                           {
-                               return false;
-                           }
-                       }
-                   }
-                   else
-                   {
-                       return false;
-                   }
+                            if (i == size)
+                            {
+                                out = SymbolicDimension(getGVN(), size);
+                                safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
+                                return true;
+                            }
+                            else
+                            {
+                                return false;
+                            }
+                        }
+                    }
+                    else
+                    {
+                        return false;
+                    }
                 }
                 else if (pIT->isImplicitList())
                 {
@@ -193,47 +193,47 @@ namespace analysis
 
                         switch (type)
                         {
-                        case 0 :
-                        {
-                           out = SymbolicDimension(getGVN(), 0.);
-                           safe = true;
-                           return true;
-                        }
-                        case 1 :
-                        {
-                           out = SymbolicDimension(getGVN(), 1.);
-                           safe = false;
-                           return true;
-                        }
-                        case 2 :
-                        {
-                            const uint64_t N = ForList64::size(start, end, step);
-                            uint64_t max, min;
-                            if (step > 0)
+                            case 0 :
                             {
-                                min = start;
-                                max = (uint64_t)(start + (N - 1) * step);
+                                out = SymbolicDimension(getGVN(), 0.);
+                                safe = true;
+                                return true;
                             }
-                            else
+                            case 1 :
                             {
-                                max = start;
-                                min = (uint64_t)(start + (N - 1) * step);
+                                out = SymbolicDimension(getGVN(), 1.);
+                                safe = false;
+                                return true;
                             }
+                            case 2 :
+                            {
+                                const uint64_t N = ForList64::size(start, end, step);
+                                uint64_t max, min;
+                                if (step > 0)
+                                {
+                                    min = start;
+                                    max = (uint64_t)(start + (N - 1) * step);
+                                }
+                                else
+                                {
+                                    max = start;
+                                    min = (uint64_t)(start + (N - 1) * step);
+                                }
 
-                           out = SymbolicDimension(getGVN(), N);
-                           safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue((int64_t)max));
-                           return true;
-                       }
+                                out = SymbolicDimension(getGVN(), N);
+                                safe = (min >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue((int64_t)max));
+                                return true;
+                            }
                         }
                     }
                 }
             }
-           else
-           {
-               out = SymbolicDimension(getGVN(), 1.);
-               safe = (de.getValue() >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(de.getValue()));
-               return true;    
-           }
+            else
+            {
+                out = SymbolicDimension(getGVN(), 1.);
+                safe = (de.getValue() >= 1) && getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(de.getValue()));
+                return true;
+            }
             return false;
         }
         case ast::Exp::BOOLEXP : // a(a > 1) => a([%f %t %t]) => a([2 3])
@@ -257,24 +257,24 @@ namespace analysis
                         }
                     }
 
-                   out = SymbolicDimension(getGVN(), count);
-                   safe = getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
-                   return true;
+                    out = SymbolicDimension(getGVN(), count);
+                    safe = getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
+                    return true;
                 }
             }
-           else
-           {
-               if (be.getValue())
-               {
-                   out = SymbolicDimension(getGVN(), 1.);
-               }
-               else
-               {
-                   out = SymbolicDimension(getGVN(), 0.);
-               }
-               safe = true;
-               return true;    
-           }
+            else
+            {
+                if (be.getValue())
+                {
+                    out = SymbolicDimension(getGVN(), 1.);
+                }
+                else
+                {
+                    out = SymbolicDimension(getGVN(), 0.);
+                }
+                safe = true;
+                return true;
+            }
             return false;
         }
         case ast::Exp::LISTEXP :
@@ -290,9 +290,9 @@ namespace analysis
                 TIType typ;
                 if (sl.getType(getGVN(), typ))
                 {
-                   out = SymbolicDimension(getGVN(), typ.cols.getValue());
-                   safe = false;//getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
-                   return true;
+                    out = SymbolicDimension(getGVN(), typ.cols.getValue());
+                    safe = false;//getCM().check(ConstraintManager::GREATER, dim.getValue(), getGVN().getValue(max));
+                    return true;
                 }
             }
             return false;
@@ -301,36 +301,36 @@ namespace analysis
         {
             arg.accept(*this);
             Result & _res = getResult();
-           SymbolicRange & range = _res.getRange(); 
-           if (range.isValid())
-           {
-               //std::wcerr << *range.getStart()->poly << ":" << *range.getEnd()->poly << ",," << *dim.getValue()->poly << std::endl;
-               safe = getCM().check(ConstraintManager::VALID_RANGE, range.getStart(), range.getEnd(), getGVN().getValue(1), dim.getValue());
-               out = _res.getType().rows * _res.getType().cols;
+            SymbolicRange & range = _res.getRange();
+            if (range.isValid())
+            {
+                //std::wcerr << *range.getStart()->poly << ":" << *range.getEnd()->poly << ",," << *dim.getValue()->poly << std::endl;
+                safe = getCM().check(ConstraintManager::VALID_RANGE, range.getStart(), range.getEnd(), getGVN().getValue(1), dim.getValue());
+                out = _res.getType().rows * _res.getType().cols;
 
-               return true;
-           }
+                return true;
+            }
 
-           // To use with find
-           // e.g. a(find(a > 0)): find(a > 0) return a matrix where the max index is rc(a) so the extraction is safe
+            // To use with find
+            // e.g. a(find(a > 0)): find(a > 0) return a matrix where the max index is rc(a) so the extraction is safe
             if (_res.getType().ismatrix() && _res.getType().type != TIType::BOOLEAN)
             {
-               out = _res.getType().rows * _res.getType().cols;
-               SymbolicDimension & maxIndex = _res.getMaxIndex();
-               if (maxIndex.isValid())
-               {
-                   safe = getCM().check(ConstraintManager::GREATER, dim.getValue(), maxIndex.getValue());
-               }
-               else
-               {
-                   safe = false;
-               }
+                out = _res.getType().rows * _res.getType().cols;
+                SymbolicDimension & maxIndex = _res.getMaxIndex();
+                if (maxIndex.isValid())
+                {
+                    safe = getCM().check(ConstraintManager::GREATER, dim.getValue(), maxIndex.getValue());
+                }
+                else
+                {
+                    safe = false;
+                }
                 return true;
             }
             return false;
         }
-        }
     }
+}
 
 
 }
index ded4dea..0afcba1 100644 (file)
 namespace analysis
 {
 
-    void AnalysisVisitor::visit(ast::AssignExp & e)
+void AnalysisVisitor::visit(ast::AssignExp & e)
+{
+    logger.log(L"AssignExp", e.getLocation());
+    if (e.getLeftExp().isSimpleVar()) // A = ...
     {
-        logger.log(L"AssignExp", e.getLocation());
-        if (e.getLeftExp().isSimpleVar()) // A = ...
-        {
-            ast::SimpleVar & var = static_cast<ast::SimpleVar &>(e.getLeftExp());
-            const symbol::Symbol & sym = var.getSymbol();
+        ast::SimpleVar & var = static_cast<ast::SimpleVar &>(e.getLeftExp());
+        const symbol::Symbol & sym = var.getSymbol();
 
-            if (e.getRightExp().isSimpleVar())
-            {
-                // We have A = B (so the data associated to b is shared with a)
-                const symbol::Symbol & symR = static_cast<ast::SimpleVar &>(e.getRightExp()).getSymbol();
-                getDM().share(sym, symR, getSymInfo(symR).getType(), &e);
-            }
-            else
+        if (e.getRightExp().isSimpleVar())
+        {
+            // We have A = B (so the data associated to b is shared with a)
+            const symbol::Symbol & symR = static_cast<ast::SimpleVar &>(e.getRightExp()).getSymbol();
+            getDM().share(sym, symR, getSymInfo(symR).getType(), &e);
+        }
+        else
+        {
+            if (e.getRightExp().isCallExp()) // A = foo(...)
             {
-                if (e.getRightExp().isCallExp()) // A = foo(...)
+                // apply the ConstantVisitor
+                e.getRightExp().accept(cv);
+                if (e.getRightExp().isCallExp())
                 {
-                    // apply the ConstantVisitor
-                    e.getRightExp().accept(cv);
-                    if (e.getRightExp().isCallExp())
-                    {
-                        visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ 1);
-                    }
-                    else
-                    {
-                        e.getRightExp().accept(*this);
-                    }
+                    visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ 1);
                 }
-                else // A = 1 + 2
+                else
                 {
-                    cv.setLHS(1);
-                    e.getRightExp().accept(cv);
                     e.getRightExp().accept(*this);
                 }
+            }
+            else // A = 1 + 2
+            {
+                cv.setLHS(1);
+                e.getRightExp().accept(cv);
+                e.getRightExp().accept(*this);
+            }
 
-                Result & RR = getResult();
-                // Don't remove tmp... temp.remove(RR); WHY THIS COMMENT ?????
+            Result & RR = getResult();
+            // Don't remove tmp... temp.remove(RR); WHY THIS COMMENT ?????
 
-                var.getDecorator().res = RR;
-                Info & info = getDM().define(sym, RR.getType(), RR.isAnInt(), &e);
-                info.getConstant() = RR.getConstant();
-               getDM().releaseTmp(RR.getTempId());
-            }
+            var.getDecorator().res = RR;
+            Info & info = getDM().define(sym, RR.getType(), RR.isAnInt(), &e);
+            info.getConstant() = RR.getConstant();
+            getDM().releaseTmp(RR.getTempId());
         }
-        else if (e.getLeftExp().isCallExp()) // A(12) = ...
+    }
+    else if (e.getLeftExp().isCallExp()) // A(12) = ...
+    {
+        ast::CallExp & ce = static_cast<ast::CallExp &>(e.getLeftExp());
+        if (ce.getName().isSimpleVar())
         {
-            ast::CallExp & ce = static_cast<ast::CallExp &>(e.getLeftExp());
-            if (ce.getName().isSimpleVar())
+            const symbol::Symbol & symL = static_cast<ast::SimpleVar &>(ce.getName()).getSymbol();
+            e.getRightExp().accept(*this);
+            Result & RR = e.getRightExp().getDecorator().getResult();
+            ce.getDecorator().res = RR;
+            Info & info = getDM().write(symL, RR.getType(), &e);
+            ce.getName().getDecorator().setResult(info.type);
+            if (analyzeIndices(info.type, ce))
             {
-                const symbol::Symbol & symL = static_cast<ast::SimpleVar &>(ce.getName()).getSymbol();
-                e.getRightExp().accept(*this);
-                Result RR = getResult();
-                ce.getDecorator().res = RR;
-                Info & info = getDM().write(symL, RR.getType(), &e);
-                if (analyzeIndices(info.type, ce))
-               {
-                   e.getDecorator().safeInsertion = (RR.getType() == getResult().getType());
-               }
-               getDM().releaseTmp(RR.getTempId());
+                e.getDecorator().safe = (RR.getType() == getResult().getType());
             }
+            getDM().releaseTmp(RR.getTempId());
         }
-        else if (e.getLeftExp().isAssignListExp()) // [A, B] = ...
+    }
+    else if (e.getLeftExp().isAssignListExp()) // [A, B] = ...
+    {
+        ast::AssignListExp & ale = static_cast<ast::AssignListExp &>(e.getLeftExp());
+        if (e.getRightExp().isCallExp())
         {
-            ast::AssignListExp & ale = static_cast<ast::AssignListExp &>(e.getLeftExp());
-            if (e.getRightExp().isCallExp())
+            const ast::exps_t & exps = ale.getExps();
+            visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ exps.size());
+            std::vector<Result>::iterator j = multipleLHS.begin();
+            for (const auto exp : exps)
             {
-                const ast::exps_t & exps = ale.getExps();
-                visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ exps.size());
-                std::vector<Result>::iterator j = multipleLHS.begin();
-                for (const auto exp : exps)
+                // TODO: handle fields...
+                if (exp->isSimpleVar() && j != multipleLHS.end())
                 {
-                    // TODO: handle fields...
-                    if (exp->isSimpleVar() && j != multipleLHS.end())
-                    {
-                        ast::SimpleVar & var = *static_cast<ast::SimpleVar *>(exp);
-                        const symbol::Symbol & sym = var.getSymbol();
-                        Info & info = getDM().define(sym, j->getType(), j->isAnInt(), exp);
-                        info.setConstant(j->getConstant());
-                       var.getDecorator().res = *j;
-                        ++j;
-                    }
+                    ast::SimpleVar & var = *static_cast<ast::SimpleVar *>(exp);
+                    const symbol::Symbol & sym = var.getSymbol();
+                    Info & info = getDM().define(sym, j->getType(), j->isAnInt(), exp);
+                    info.setConstant(j->getConstant());
+                    var.getDecorator().res = *j;
+                    ++j;
                 }
             }
         }
     }
 }
+}
index ec0061d..4c26516 100644 (file)
@@ -175,14 +175,14 @@ void DebugVisitor::visit (const SimpleVar &e)
     std::wstring ty;
     if (type.type != analysis::TIType::UNKNOWN)
     {
-       if (type.isscalar())
-       {
-           ty = L" (" + analysis::TIType::toString(type.type) + L")";
-       }
-       else
-       {
-           ty = L" (" + analysis::TIType::toString(type.type) + L"*)";
-       }
+        if (type.isscalar())
+        {
+            ty = L" (" + analysis::TIType::toString(type.type) + L")";
+        }
+        else
+        {
+            ty = L" (" + analysis::TIType::toString(type.type) + L"*)";
+        }
     }
 
     DEBUG_START_NODE(e);
@@ -270,10 +270,6 @@ void DebugVisitor::visit(const CellCallExp &e)
 void DebugVisitor::visit(const CallExp &e)
 {
     std::wstring str;
-    if (e.getDecorator().safeIndex)
-    {
-       str = L" (Ins/Ext is safe)";
-    }
 
     DEBUG_START_NODE(e);
     DEBUG(L"Exec CallExp" + str, e);