[bug_14300] double free fixed
[scilab.git] / scilab / modules / ast / src / cpp / ast / runvisitor.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 //for Visual Leak Detector in debug compilation mode
14 //#define DEBUG_VLD
15 #if defined(DEBUG_VLD) && defined(_DEBUG)
16 #include <vld.h>
17 #endif
18
19 #include <string>
20
21 #include "execvisitor.hxx"
22 #include "stepvisitor.hxx"
23 #include "timedvisitor.hxx"
24 #include "shortcutvisitor.hxx"
25 #include "printvisitor.hxx"
26 //#include "AnalysisVisitor.hxx"
27 #include "debuggervisitor.hxx"
28 #include "debugmanager.hxx"
29
30 #include "visitor_common.hxx"
31
32 #include "context.hxx"
33 #include "generic_operations.hxx"
34 #include "types_or.hxx"
35 #include "types_and.hxx"
36 #include "localization.hxx"
37
38 #include "macrofile.hxx"
39 #include "macro.hxx"
40 #include "cell.hxx"
41 #include "filemanager_interface.h"
42
43 #include "runner.hxx"
44 #include "threadmanagement.hxx"
45
46 #include "coverage_instance.hxx"
47
48 extern "C"
49 {
50 #include "sciprint.h"
51 #include "os_string.h"
52 #include "elem_common.h"
53 #include "storeCommand.h"
54 #include "prompt.h"
55 #include "scilabRead.h"
56 }
57
58 namespace ast
59 {
60 template <class T>
61 void RunVisitorT<T>::visitprivate(const StringExp & e)
62 {
63     CoverageInstance::invokeAndStartChrono((void*)&e);
64     if (e.getConstant() == nullptr)
65     {
66         types::String *psz = new types::String(e.getValue().c_str());
67         (const_cast<StringExp *>(&e))->setConstant(psz);
68     }
69     setResult(e.getConstant());
70     CoverageInstance::stopChrono((void*)&e);
71 }
72
73 template <class T>
74 void RunVisitorT<T>::visitprivate(const DoubleExp & e)
75 {
76     CoverageInstance::invokeAndStartChrono((void*)&e);
77     if (e.getConstant() == nullptr)
78     {
79         types::Double *pdbl = new types::Double(e.getValue());
80         (const_cast<DoubleExp *>(&e))->setConstant(pdbl);
81     }
82     setResult(e.getConstant());
83     CoverageInstance::stopChrono((void*)&e);
84 }
85
86 template <class T>
87 void RunVisitorT<T>::visitprivate(const BoolExp & e)
88 {
89     CoverageInstance::invokeAndStartChrono((void*)&e);
90     if (e.getConstant() == nullptr)
91     {
92         types::Bool *pB = new types::Bool(e.getValue());
93         (const_cast<BoolExp *>(&e))->setConstant(pB);
94     }
95     setResult(e.getConstant());
96     CoverageInstance::stopChrono((void*)&e);
97 }
98
99 template <class T>
100 void RunVisitorT<T>::visitprivate(const NilExp & e)
101 {
102     CoverageInstance::invokeAndStartChrono((void*)&e);
103     setResult(new types::Void());
104     CoverageInstance::stopChrono((void*)&e);
105 }
106
107 template <class T>
108 void RunVisitorT<T>::visitprivate(const SimpleVar & e)
109 {
110     CoverageInstance::invokeAndStartChrono((void*)&e);
111     symbol::Context* ctx = symbol::Context::getInstance();
112     symbol::Variable* var = ((SimpleVar&)e).getStack();
113     types::InternalType *pI = ctx->get(var);
114     setResult(pI);
115     if (pI != nullptr)
116     {
117         if (e.isVerbose() && pI->isCallable() == false && ConfigVariable::isPromptShow())
118         {
119             std::wostringstream ostr;
120             ostr << e.getSymbol().getName() << L"  = ";
121 #ifndef NDEBUG
122             ostr << L"(" << pI->getRef() << L")";
123 #endif
124             ostr << std::endl;
125             ostr << std::endl;
126             scilabWriteW(ostr.str().c_str());
127             std::wostringstream ostrName;
128             ostrName << e.getSymbol().getName();
129             VariableToString(pI, ostrName.str().c_str());
130         }
131
132         //check if var is recalled in current scope like
133         //function f()
134         //  a; //<=> a=a;
135         //  a(2) = 18;
136         //endfunction
137         if (e.getParent()->isSeqExp())
138         {
139             if (ctx->getScopeLevel() > 1 && var->empty() == false && var->top()->m_iLevel != ctx->getScopeLevel())
140             {
141                 //put var in current scope
142                 ctx->put(var, pI);
143             }
144         }
145     }
146     else
147     {
148         char pstError[bsiz];
149         wchar_t* pwstError;
150
151         char* strErr = wide_string_to_UTF8(e.getSymbol().getName().c_str());
152
153         os_sprintf(pstError, _("Undefined variable: %s\n"), strErr);
154         pwstError = to_wide_string(pstError);
155         FREE(strErr);
156         std::wstring wstError(pwstError);
157         FREE(pwstError);
158         CoverageInstance::stopChrono((void*)&e);
159         throw InternalError(wstError, 999, e.getLocation());
160         //Err, SimpleVar doesn't exist in Scilab scopes.
161     }
162     CoverageInstance::stopChrono((void*)&e);
163 }
164
165 template <class T>
166 void RunVisitorT<T>::visitprivate(const ColonVar & e)
167 {
168     CoverageInstance::invokeAndStartChrono((void*)&e);
169     types::Colon *pC = new types::Colon();
170     setResult(pC);
171     CoverageInstance::stopChrono((void*)&e);
172 }
173
174 template <class T>
175 void RunVisitorT<T>::visitprivate(const DollarVar & e)
176 {
177     CoverageInstance::invokeAndStartChrono((void*)&e);
178     setResult(types::Polynom::Dollar());
179     CoverageInstance::stopChrono((void*)&e);
180 }
181
182 template <class T>
183 void RunVisitorT<T>::visitprivate(const BreakExp & e)
184 {
185     CoverageInstance::invokeAndStartChrono((void*)&e);
186     const_cast<BreakExp*>(&e)->setBreak();
187     CoverageInstance::stopChrono((void*)&e);
188 }
189
190 template <class T>
191 void RunVisitorT<T>::visitprivate(const ContinueExp &e)
192 {
193     CoverageInstance::invokeAndStartChrono((void*)&e);
194     const_cast<ContinueExp*>(&e)->setContinue();
195     CoverageInstance::stopChrono((void*)&e);
196 }
197
198 template <class T>
199 void RunVisitorT<T>::visitprivate(const ArrayListExp & e)
200 {
201     CoverageInstance::invokeAndStartChrono((void*)&e);
202     exps_t::const_iterator it;
203     int iNbExpSize = this->getExpectedSize();
204     this->setExpectedSize(1);
205
206     types::typed_list lstIT;
207     for (it = e.getExps().begin(); it != e.getExps().end(); it++)
208     {
209         (*it)->accept(*this);
210         for (int j = 0; j < getResultSize(); j++)
211         {
212             lstIT.push_back(getResult(j));
213         }
214     }
215
216     setResult(lstIT);
217
218     this->setExpectedSize(iNbExpSize);
219     CoverageInstance::stopChrono((void*)&e);
220 }
221
222 template <class T>
223 void RunVisitorT<T>::visitprivate(const VarDec & e)
224 {
225     CoverageInstance::invokeAndStartChrono((void*)&e);
226     try
227     {
228         /*getting what to assign*/
229         e.getInit().accept(*this);
230         getResult()->IncreaseRef();
231     }
232     catch (const InternalError& error)
233     {
234         CoverageInstance::stopChrono((void*)&e);
235         throw error;
236     }
237     CoverageInstance::stopChrono((void*)&e);
238 }
239
240 template <class T>
241 void RunVisitorT<T>::visitprivate(const CellExp & e)
242 {
243     CoverageInstance::invokeAndStartChrono((void*)&e);
244
245     exps_t::const_iterator row;
246     exps_t::const_iterator col;
247     int iColMax = 0;
248
249     exps_t lines = e.getLines();
250     //check dimmension
251     for (row = lines.begin(); row != lines.end(); ++row)
252     {
253         exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
254         if (iColMax == 0)
255         {
256             iColMax = static_cast<int>(cols.size());
257         }
258
259         if (iColMax != static_cast<int>(cols.size()))
260         {
261             std::wostringstream os;
262             os << _W("inconsistent row/column dimensions\n");
263             //os << ((Location)(*row)->getLocation()).getLocationString() << std::endl;
264             CoverageInstance::stopChrono((void*)&e);
265             throw InternalError(os.str(), 999, (*row)->getLocation());
266         }
267     }
268
269     //alloc result cell
270     types::Cell *pC = new types::Cell(static_cast<int>(lines.size()), iColMax);
271
272     int i = 0;
273     int j = 0;
274
275     //insert items in cell
276     for (i = 0, row = lines.begin(); row != lines.end(); ++row, ++i)
277     {
278         exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
279         for (j = 0, col = cols.begin(); col != cols.end(); ++col, ++j)
280         {
281             try
282             {
283                 (*col)->accept(*this);
284             }
285             catch (ScilabException &)
286             {
287                 CoverageInstance::stopChrono((void*)&e);
288                 throw;
289             }
290             types::InternalType *pIT = getResult();
291             if (pIT->isImplicitList())
292             {
293                 types::InternalType * _pIT = pIT->getAs<types::ImplicitList>()->extractFullMatrix();
294                 pC->set(i, j, _pIT);
295                 _pIT->killMe();
296             }
297             else
298             {
299                 pC->set(i, j, pIT);
300             }
301             clearResult();
302         }
303     }
304
305     //return new cell
306     setResult(pC);
307
308     CoverageInstance::stopChrono((void*)&e);
309 }
310
311 template <class T>
312 void RunVisitorT<T>::visitprivate(const FieldExp &e)
313 {
314     /*
315       a.b
316       */
317
318     CoverageInstance::invokeAndStartChrono((void*)&e);
319
320     if (!e.getTail()->isSimpleVar())
321     {
322         wchar_t szError[bsiz];
323         os_swprintf(szError, bsiz, _W("/!\\ Unmanaged FieldExp.\n").c_str());
324         CoverageInstance::stopChrono((void*)&e);
325         throw InternalError(szError, 999, e.getLocation());
326     }
327
328     try
329     {
330         e.getHead()->accept(*this);
331     }
332     catch (const InternalError& error)
333     {
334         CoverageInstance::stopChrono((void*)&e);
335         throw error;
336     }
337
338     if (getResult() == NULL)
339     {
340         wchar_t szError[bsiz];
341         os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
342         CoverageInstance::stopChrono((void*)&e);
343         throw InternalError(szError, 999, e.getLocation());
344     }
345
346     // TODO: handle case where getSize() > 1
347     // l=list(struct("toto","coucou"),struct("toto","hello"),1,2);[a,b]=l(1:2).toto
348     //
349     if (getResultSize() > 1)
350     {
351         clearResult();
352         wchar_t szError[bsiz];
353         os_swprintf(szError, bsiz, _W("Not yet implemented in Scilab.\n").c_str());
354         CoverageInstance::stopChrono((void*)&e);
355         throw InternalError(szError, 999, e.getLocation());
356     }
357
358     SimpleVar * psvRightMember = static_cast<SimpleVar *>(const_cast<Exp *>(e.getTail()));
359     std::wstring wstField = psvRightMember->getSymbol().getName();
360     types::InternalType * pValue = getResult();
361     types::InternalType * pReturn = NULL;
362     bool ok = false;
363
364     try
365     {
366         if (pValue->isGenericType() || pValue->isUserType())
367         {
368             ok = pValue->getAs<types::GenericType>()->extract(wstField, pReturn);
369         }
370     }
371     catch (std::wstring & err)
372     {
373         CoverageInstance::stopChrono((void*)&e);
374         throw InternalError(err.c_str(), 999, e.getTail()->getLocation());
375     }
376
377     if (ok)
378     {
379         if (pReturn == NULL)
380         {
381             std::wostringstream os;
382             os << _W("Invalid index.\n");
383             CoverageInstance::stopChrono((void*)&e);
384             throw InternalError(os.str(), 999, e.getLocation());
385         }
386
387         setResult(pReturn);
388         if (pValue->isDeletable())
389         {
390             if (pValue->isContainer())
391             {
392                 // prevent delete of pReturn in case where
393                 // extract not return a clone
394                 pReturn->IncreaseRef();
395                 pValue->killMe();
396                 pReturn->DecreaseRef();
397             }
398             else
399             {
400                 pValue->killMe();
401             }
402         }
403     }
404     else if (pValue->isFieldExtractionOverloadable())
405     {
406         types::typed_list in;
407         types::typed_list out;
408
409         types::String* pS = new types::String(wstField.c_str());
410
411         //TODO: in the case where overload is a macro there is no need to incref in
412         // because args will be put in context, removed and killed if required.
413         // But if the overload is a function... it is another story...
414
415         pS->IncreaseRef();
416         pValue->IncreaseRef();
417
418         in.push_back(pS);
419         in.push_back(pValue);
420         types::Callable::ReturnValue Ret = types::Callable::Error;
421         std::wstring stType = pValue->getShortTypeStr();
422
423         try
424         {
425             Ret = Overload::call(L"%" + stType + L"_e", in, 1, out, this);
426         }
427         catch (const InternalError& ie)
428         {
429             try
430             {
431                 //to compatibility with scilab 5 code.
432                 //tlist/mlist name are truncated to 8 first character
433                 if (stType.size() > 8)
434                 {
435                     Ret = Overload::call(L"%" + stType.substr(0, 8) + L"_e", in, 1, out, this);
436                 }
437                 else
438                 {
439                     CoverageInstance::stopChrono((void*)&e);
440                     throw ie;
441                 }
442             }
443             catch (const InternalError& ie)
444             {
445                 // TList or Mlist
446                 if (pValue->isList())
447                 {
448                     Ret = Overload::call(L"%l_e", in, 1, out, this);
449                 }
450                 else
451                 {
452                     CoverageInstance::stopChrono((void*)&e);
453                     throw ie;
454                 }
455             }
456         }
457
458         if (Ret != types::Callable::OK)
459         {
460             cleanInOut(in, out);
461             setResult(NULL);
462             CoverageInstance::stopChrono((void*)&e);
463             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
464         }
465
466         setResult(out);
467         cleanIn(in, out);
468     }
469     else
470     {
471         pValue->killMe();
472         wchar_t szError[bsiz];
473         os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
474         CoverageInstance::stopChrono((void*)&e);
475         throw InternalError(szError, 999, e.getLocation());
476     }
477
478     CoverageInstance::stopChrono((void*)&e);
479 }
480
481 template <class T>
482 void RunVisitorT<T>::visitprivate(const IfExp  &e)
483 {
484     CoverageInstance::invokeAndStartChrono((void*)&e);
485
486     //Create local exec visitor
487     ShortCutVisitor SCTest;
488     bool bTestStatus = false;
489
490     //condition
491     try
492     {
493         e.getTest().accept(SCTest);
494         e.getTest().accept(*this);
495     }
496     catch (ScilabException &)
497     {
498         CoverageInstance::stopChrono((void*)&e);
499         throw;
500     }
501
502     bTestStatus = getResult()->isTrue();
503     clearResult();
504     try
505     {
506         if (bTestStatus == true)
507         {
508             e.getThen().accept(*this);
509         }
510         else if (e.hasElse())
511         {
512             const ast::Exp & _else = e.getElse();
513             if (_else.isCommentExp())
514             {
515                 CoverageInstance::invoke(_else);
516             }
517             else
518             {
519                 e.getElse().accept(*this);
520             }
521         }
522     }
523     catch (ScilabException &)
524     {
525         CoverageInstance::stopChrono((void*)&e);
526         throw;
527     }
528
529     if (e.isBreakable()
530             && ((&e.getElse())->isBreak()
531                 || (&e.getThen())->isBreak()))
532     {
533         const_cast<IfExp*>(&e)->setBreak();
534         const_cast<Exp*>(&e.getElse())->resetBreak();
535         const_cast<Exp*>(&e.getThen())->resetBreak();
536     }
537
538     if (e.isContinuable()
539             && ((&e.getElse())->isContinue()
540                 || (&e.getThen())->isContinue()))
541     {
542         const_cast<IfExp*>(&e)->setContinue();
543         const_cast<Exp*>(&e.getElse())->resetContinue();
544         const_cast<Exp*>(&e.getThen())->resetContinue();
545     }
546
547     if (e.isReturnable()
548             && ((&e.getElse())->isReturn()
549                 || (&e.getThen())->isReturn()))
550     {
551         const_cast<IfExp*>(&e)->setReturn();
552         const_cast<Exp*>(&e.getElse())->resetReturn();
553         const_cast<Exp*>(&e.getThen())->resetReturn();
554     }
555
556     CoverageInstance::stopChrono((void*)&e);
557 }
558
559 template <class T>
560 void RunVisitorT<T>::visitprivate(const WhileExp  &e)
561 {
562     CoverageInstance::invokeAndStartChrono((void*)&e);
563
564     //Create local exec visitor
565     ShortCutVisitor SCTest;
566
567     try
568     {
569         //manage & and | like && and ||
570         e.getTest().accept(SCTest);
571         //condition
572         e.getTest().accept(*this);
573     }
574     catch (ScilabException &)
575     {
576         CoverageInstance::stopChrono((void*)&e);
577         throw;
578     }
579
580     types::InternalType* pIT = getResult();
581
582     while (pIT->isTrue())
583     {
584         pIT->killMe();
585         try
586         {
587             e.getBody().accept(*this);
588         }
589         catch (ScilabException &)
590         {
591             CoverageInstance::stopChrono((void*)&e);
592             throw;
593         }
594
595         //clear old result value before evaluate new one
596         if (getResult() != NULL)
597         {
598             getResult()->killMe();
599         }
600
601         if (e.getBody().isBreak())
602         {
603             const_cast<Exp*>(&(e.getBody()))->resetBreak();
604             break;
605         }
606
607         if (e.getBody().isReturn())
608         {
609             const_cast<WhileExp*>(&e)->setReturn();
610             const_cast<Exp*>(&(e.getBody()))->resetReturn();
611             break;
612         }
613
614         if (e.getBody().isContinue())
615         {
616             const_cast<Exp*>(&(e.getBody()))->resetContinue();
617         }
618
619         try
620         {
621             e.getTest().accept(*this);
622         }
623         catch (ScilabException &)
624         {
625             CoverageInstance::stopChrono((void*)&e);
626             throw;
627         }
628         pIT = getResult();
629     }
630
631     //pIT->killMe();
632     //clear result of condition or result of body
633     clearResult();
634     CoverageInstance::stopChrono((void*)&e);
635 }
636
637 template <class T>
638 void RunVisitorT<T>::visitprivate(const ForExp  &e)
639 {
640     CoverageInstance::invokeAndStartChrono((void*)&e);
641     symbol::Context* ctx = symbol::Context::getInstance();
642     //vardec visit increase its result reference
643     try
644     {
645         e.getVardec().accept(*this);
646     }
647     catch (ScilabException &)
648     {
649         CoverageInstance::stopChrono((void*)&e);
650         throw;
651     }
652     types::InternalType* pIT = getResult();
653
654     if (pIT->isImplicitList())
655     {
656         //get IL
657         types::ImplicitList* pVar = pIT->getAs<types::ImplicitList>();
658         //get IL initial Type
659         types::InternalType * pIL = pVar->getInitalType();
660         //std::cout << "for IL: " << pIL << std::endl;
661         //std::cout << "  for IV: " << pIT << std::endl;
662         //get index stack
663         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
664
665         if (ctx->isprotected(var))
666         {
667             std::wostringstream os;
668             os << _W("Redefining permanent variable.\n");
669             CoverageInstance::stopChrono((void*)&e);
670             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
671         }
672
673         ctx->put(var, pIL);
674         //use ref count to lock var against clear and detect any changes
675         pIL->IncreaseRef();
676
677         int size = static_cast<int>(pVar->getSize());
678         for (int i = 0; i < size; ++i)
679         {
680             //check if loop index has changed, deleted, copy ...
681             if (pIL->getRef() != 2)
682             {
683                 switch (pIL->getRef())
684                 {
685                     case 1:
686                         //someone clear me
687                         ctx->put(var, pIL);
688                         break;
689                     default:
690                         //someone assign me to another var
691                         //a = i;
692                         //unlock me
693                         pIL->DecreaseRef();
694
695                         //no need to destroy, it already assign to another var
696                         //pIL->killMe();
697
698                         //create a new me
699                         pIL = pVar->getInitalType();
700                         //lock loop index
701                         pIL->IncreaseRef();
702                         //update me ( must decrease ref of a )
703                         if (ctx->isprotected(var))
704                         {
705                             std::wostringstream os;
706                             os << _W("Redefining permanent variable.\n");
707                             CoverageInstance::stopChrono((void*)&e);
708                             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
709                         }
710
711                         ctx->put(var, pIL);
712                         break;
713                 }
714             }
715
716             pVar->extractValue(i, pIL);
717
718             try
719             {
720                 e.getBody().accept(*this);
721             }
722             catch (const InternalError& ie)
723             {
724                 //unlock loop index and implicit list
725                 pIL->DecreaseRef();
726                 pIL->killMe();
727                 pIT->DecreaseRef();
728                 pIT->killMe();
729
730                 setResult(NULL);
731                 CoverageInstance::stopChrono((void*)&e);
732                 throw ie;
733             }
734
735             if (e.getBody().isBreak())
736             {
737                 const_cast<Exp&>(e.getBody()).resetBreak();
738                 break;
739             }
740
741             if (e.getBody().isContinue())
742             {
743                 const_cast<Exp&>(e.getBody()).resetContinue();
744                 continue;
745             }
746
747             if (e.getBody().isReturn())
748             {
749                 const_cast<ForExp&>(e).setReturn();
750                 const_cast<Exp&>(e.getBody()).resetReturn();
751                 break;
752             }
753         }
754
755         //unlock loop index
756         pIL->DecreaseRef();
757         pIL->killMe();
758     }
759     else if (pIT->isList())
760     {
761         types::List* pL = pIT->getAs<types::List>();
762         const int size = pL->getSize();
763         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
764         for (int i = 0; i < size; ++i)
765         {
766             types::InternalType* pNew = pL->get(i);
767
768             if (ctx->isprotected(var))
769             {
770                 std::wostringstream os;
771                 os << _W("Redefining permanent variable.\n");
772                 CoverageInstance::stopChrono((void*)&e);
773                 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
774             }
775             ctx->put(var, pNew);
776
777             try
778             {
779                 e.getBody().accept(*this);
780             }
781             catch (const InternalError& ie)
782             {
783                 //implicit list
784                 pIT->DecreaseRef();
785                 pIT->killMe();
786                 setResult(NULL);
787                 CoverageInstance::stopChrono((void*)&e);
788                 throw ie;
789             }
790
791             if (e.getBody().isBreak())
792             {
793                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
794                 break;
795             }
796
797             if (e.getBody().isContinue())
798             {
799                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
800                 continue;
801             }
802
803             if (e.getBody().isReturn())
804             {
805                 const_cast<ForExp*>(&e)->setReturn();
806                 const_cast<Exp&>(e.getBody()).resetReturn();
807                 break;
808             }
809         }
810     }
811     else if (pIT->isGenericType())
812     {
813         //Matrix i = [1,3,2,6] or other type
814         types::GenericType* pVar = pIT->getAs<types::GenericType>();
815         if (pVar->getDims() > 2)
816         {
817             pIT->DecreaseRef();
818             pIT->killMe();
819             CoverageInstance::stopChrono((void*)&e);
820             throw InternalError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.getVardec().getLocation());
821         }
822
823         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
824         for (int i = 0; i < pVar->getCols(); i++)
825         {
826             types::GenericType* pNew = pVar->getColumnValues(i);
827             if (pNew == NULL)
828             {
829                 pIT->DecreaseRef();
830                 pIT->killMe();
831                 CoverageInstance::stopChrono((void*)&e);
832                 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
833             }
834
835             if (ctx->isprotected(var))
836             {
837                 std::wostringstream os;
838                 os << _W("Redefining permanent variable.\n");
839                 CoverageInstance::stopChrono((void*)&e);
840                 throw InternalError(os.str(), 999, e.getVardec().getLocation());
841             }
842             ctx->put(var, pNew);
843
844             try
845             {
846                 e.getBody().accept(*this);
847             }
848             catch (const InternalError& ie)
849             {
850                 //implicit list
851                 pIT->DecreaseRef();
852                 pIT->killMe();
853                 setResult(NULL);
854                 CoverageInstance::stopChrono((void*)&e);
855                 throw ie;
856             }
857
858             if (e.getBody().isBreak())
859             {
860                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
861                 break;
862             }
863
864             if (e.getBody().isContinue())
865             {
866                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
867                 continue;
868             }
869
870             if (e.getBody().isReturn())
871             {
872                 const_cast<ForExp*>(&e)->setReturn();
873                 const_cast<Exp&>(e.getBody()).resetReturn();
874                 break;
875             }
876         }
877     }
878     else
879     {
880         pIT->DecreaseRef();
881         pIT->killMe();
882         CoverageInstance::stopChrono((void*)&e);
883         throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
884     }
885
886     pIT->DecreaseRef();
887     pIT->killMe();
888
889     setResult(NULL);
890     CoverageInstance::stopChrono((void*)&e);
891 }
892
893 template <class T>
894 void RunVisitorT<T>::visitprivate(const ReturnExp &e)
895 {
896     CoverageInstance::invokeAndStartChrono((void*)&e);
897     if (e.isGlobal())
898     {
899         if (ConfigVariable::getPauseLevel() != 0 && symbol::Context::getInstance()->getScopeLevel() == ConfigVariable::getActivePauseLevel())
900         {
901             if (ConfigVariable::getEnableDebug() == true)
902             {
903                 sciprint(_("%s: function is disabled in debug mode.\n"), "resume");
904                 CoverageInstance::stopChrono((void*)&e);
905                 return;
906             }
907
908             //return or resume
909             ConfigVariable::DecreasePauseLevel();
910             CoverageInstance::stopChrono((void*)&e);
911             return;
912         }
913         else
914         {
915             const_cast<ReturnExp*>(&e)->setReturn();
916         }
917     }
918     else
919     {
920         //return(x)
921
922         if (e.getParent() == nullptr || e.getParent()->isAssignExp() == false)
923         {
924             CoverageInstance::stopChrono((void*)&e);
925             throw InternalError(_W("With input arguments, return / resume expects output arguments.\n"), 999, e.getLocation());
926         }
927         //in case of CallExp, we can return only one values
928         int iSaveExpectedSize = getExpectedSize();
929         setExpectedSize(1);
930         try
931         {
932             e.getExp().accept(*this);
933         }
934         catch (ScilabException &)
935         {
936             CoverageInstance::stopChrono((void*)&e);
937             throw;
938         }
939         setExpectedSize(iSaveExpectedSize);
940         const_cast<ReturnExp*>(&e)->setReturn();
941     }
942
943     CoverageInstance::stopChrono((void*)&e);
944 }
945
946 template <class T>
947 void RunVisitorT<T>::visitprivate(const IntSelectExp &e)
948 {
949     CoverageInstance::invokeAndStartChrono((void*)&e);
950     bool found = false;
951     //e.getSelect()->accept(*this);
952     //InternalType* pIT = getResult();
953     //setResult(nullptr);
954     //if (pIT && pIT->isDouble())
955     //{
956     //    Double * pDbl = static_cast<Double *>(pIT);
957     //    if (!pDbl->isComplex() && pDbl->getSize() == 1)
958     //    {
959     //        int64_t val;
960     //        if (analysis::tools::asInteger<int64_t>(pDbl->get(0), val))
961     //        {
962     //            Exp * exp = e.getExp(val);
963     //            found = true;
964     //            if (exp)
965     //            {
966     //                Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
967     //                if (e.isBreakable())
968     //                {
969     //                    const_cast<IntSelectExp*>(&e)->resetBreak();
970     //                    body->setBreakable();
971     //                }
972
973     //                if (e.isContinuable())
974     //                {
975     //                    const_cast<IntSelectExp*>(&e)->resetContinue();
976     //                    body->setContinuable();
977     //                }
978
979     //                if (e.isReturnable())
980     //                {
981     //                    const_cast<IntSelectExp*>(&e)->resetReturn();
982     //                    body->setReturnable();
983     //                }
984
985     //                try
986     //                {
987     //                    //the good one
988     //                    body->accept(*this);
989     //                }
990     //                catch (const InternalError& ie)
991     //                {
992     //                    pIT->killMe();
993     //                    throw ie;
994     //                }
995
996     //                if (e.isBreakable() && body->isBreak())
997     //                {
998     //                    const_cast<IntSelectExp*>(&e)->setBreak();
999     //                    body->resetBreak();
1000     //                }
1001
1002     //                if (e.isContinuable() && body->isContinue())
1003     //                {
1004     //                    const_cast<IntSelectExp*>(&e)->setContinue();
1005     //                    body->resetContinue();
1006     //                }
1007
1008     //                if (e.isReturnable() && body->isReturn())
1009     //                {
1010     //                    const_cast<IntSelectExp*>(&e)->setReturn();
1011     //                    body->resetReturn();
1012     //                }
1013     //            }
1014     //        }
1015     //    }
1016     //}
1017
1018     if (!found)
1019     {
1020         try
1021         {
1022             e.getOriginal()->accept(*this);
1023         }
1024         catch (ScilabException &)
1025         {
1026             CoverageInstance::stopChrono((void*)&e);
1027             throw;
1028         }
1029     }
1030     CoverageInstance::stopChrono((void*)&e);
1031 }
1032
1033 template <class T>
1034 void RunVisitorT<T>::visitprivate(const StringSelectExp &e)
1035 {
1036     CoverageInstance::invokeAndStartChrono((void*)&e);
1037     try
1038     {
1039         e.getSelect()->accept(*this);
1040     }
1041     catch (ScilabException &)
1042     {
1043         CoverageInstance::stopChrono((void*)&e);
1044         throw;
1045     }
1046     types::InternalType* pIT = getResult();
1047     setResult(nullptr);
1048     bool found = false;
1049     if (pIT && pIT->isString())
1050     {
1051         types::String * pStr = static_cast<types::String *>(pIT);
1052         if (pStr->getSize() == 1)
1053         {
1054             if (wchar_t * s = pStr->get(0))
1055             {
1056                 const std::wstring ws(s);
1057                 Exp * exp = e.getExp(ws);
1058                 found = true;
1059                 if (exp)
1060                 {
1061                     Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
1062                     if (e.isBreakable())
1063                     {
1064                         const_cast<StringSelectExp*>(&e)->resetBreak();
1065                         body->setBreakable();
1066                     }
1067
1068                     if (e.isContinuable())
1069                     {
1070                         const_cast<StringSelectExp*>(&e)->resetContinue();
1071                         body->setContinuable();
1072                     }
1073
1074                     if (e.isReturnable())
1075                     {
1076                         const_cast<StringSelectExp*>(&e)->resetReturn();
1077                         body->setReturnable();
1078                     }
1079
1080                     try
1081                     {
1082                         //the good one
1083                         body->accept(*this);
1084                     }
1085                     catch (const InternalError& ie)
1086                     {
1087                         pIT->killMe();
1088                         CoverageInstance::stopChrono((void*)&e);
1089                         throw ie;
1090                     }
1091
1092                     if (e.isBreakable() && body->isBreak())
1093                     {
1094                         const_cast<StringSelectExp*>(&e)->setBreak();
1095                         body->resetBreak();
1096                     }
1097
1098                     if (e.isContinuable() && body->isContinue())
1099                     {
1100                         const_cast<StringSelectExp*>(&e)->setContinue();
1101                         body->resetContinue();
1102                     }
1103
1104                     if (e.isReturnable() && body->isReturn())
1105                     {
1106                         const_cast<StringSelectExp*>(&e)->setReturn();
1107                         body->resetReturn();
1108                     }
1109                 }
1110             }
1111         }
1112     }
1113
1114     if (!found)
1115     {
1116         try
1117         {
1118             e.getOriginal()->accept(*this);
1119         }
1120         catch (ScilabException &)
1121         {
1122             CoverageInstance::stopChrono((void*)&e);
1123             throw;
1124         }
1125     }
1126     CoverageInstance::stopChrono((void*)&e);
1127 }
1128
1129 template <class T>
1130 void RunVisitorT<T>::visitprivate(const SelectExp &e)
1131 {
1132     // FIXME : exec select ... case ... else ... end
1133     CoverageInstance::invokeAndStartChrono((void*)&e);
1134     try
1135     {
1136         e.getSelect()->accept(*this);
1137     }
1138     catch (ScilabException &)
1139     {
1140         CoverageInstance::stopChrono((void*)&e);
1141         throw;
1142     }
1143
1144     bool bCase = false;
1145
1146     types::InternalType* pIT = getResult();
1147     setResult(NULL);
1148     if (pIT)
1149     {
1150         // protect pIT to avoid double free when
1151         // the variable in select is override in the case
1152         pIT->IncreaseRef();
1153
1154         //find good case
1155         exps_t cases = e.getCases();
1156         for (auto exp : cases)
1157         {
1158             CaseExp * pCase = exp->getAs<CaseExp>();
1159             try
1160             {
1161                 pCase->getTest()->accept(*this);
1162             }
1163             catch (ScilabException &)
1164             {
1165                 CoverageInstance::stopChrono((void*)&e);
1166                 throw;
1167             }
1168             types::InternalType *pITCase = getResult();
1169             setResult(NULL);
1170             if (pITCase)
1171             {
1172                 if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1173                 {
1174                     //check each item
1175                 }
1176                 else if (*pITCase == *pIT)
1177                 {
1178                     try
1179                     {
1180                         //the good one
1181                         pCase->getBody()->accept(*this);
1182                     }
1183                     catch (const InternalError& ie)
1184                     {
1185                         pIT->DecreaseRef();
1186                         pIT->killMe();
1187                         CoverageInstance::stopChrono((void*)&e);
1188                         throw ie;
1189                     }
1190
1191                     if (e.isBreakable() && pCase->getBody()->isBreak())
1192                     {
1193                         const_cast<SelectExp*>(&e)->setBreak();
1194                         pCase->getBody()->resetBreak();
1195                     }
1196
1197                     if (e.isContinuable() && pCase->getBody()->isContinue())
1198                     {
1199                         const_cast<SelectExp*>(&e)->setContinue();
1200                         pCase->getBody()->resetContinue();
1201                     }
1202
1203                     if (e.isReturnable() && pCase->getBody()->isReturn())
1204                     {
1205                         const_cast<SelectExp*>(&e)->setReturn();
1206                         pCase->getBody()->resetReturn();
1207                     }
1208
1209                     pITCase->killMe();
1210                     bCase = true;
1211                     break;
1212                 }
1213
1214                 pITCase->killMe();
1215             }
1216         }
1217     }
1218
1219     if (bCase == false && e.getDefaultCase() != NULL)
1220     {
1221         try
1222         {
1223             //default case
1224             e.getDefaultCase()->accept(*this);
1225         }
1226         catch (const InternalError& ie)
1227         {
1228             if (pIT)
1229             {
1230                 pIT->DecreaseRef();
1231                 pIT->killMe();
1232             }
1233             CoverageInstance::stopChrono((void*)&e);
1234             throw ie;
1235         }
1236
1237         if (e.isBreakable() && e.getDefaultCase()->isBreak())
1238         {
1239             const_cast<SelectExp*>(&e)->setBreak();
1240             e.getDefaultCase()->resetBreak();
1241         }
1242
1243         if (e.isContinuable() && e.getDefaultCase()->isContinue())
1244         {
1245             const_cast<SelectExp*>(&e)->setContinue();
1246             e.getDefaultCase()->resetContinue();
1247         }
1248
1249         if (e.isReturnable() && e.getDefaultCase()->isReturn())
1250         {
1251             const_cast<SelectExp*>(&e)->setReturn();
1252             e.getDefaultCase()->resetReturn();
1253         }
1254     }
1255
1256     clearResult();
1257
1258     if (pIT)
1259     {
1260         pIT->DecreaseRef();
1261         pIT->killMe();
1262     }
1263     CoverageInstance::stopChrono((void*)&e);
1264 }
1265
1266 template <class T>
1267 void RunVisitorT<T>::visitprivate(const SeqExp  &e)
1268 {
1269     CoverageInstance::invokeAndStartChrono((void*)&e);
1270     int lastLine = 0;
1271     for (auto exp : e.getExps())
1272     {
1273         if (exp->isCommentExp())
1274         {
1275             continue;
1276         }
1277
1278         if (ConfigVariable::isExecutionBreak())
1279         {
1280             ConfigVariable::resetExecutionBreak();
1281             if (ConfigVariable::getPromptMode() == 7)
1282             {
1283                 ClearTemporaryPrompt();
1284             }
1285
1286             StorePrioritaryCommand("pause");
1287             ThreadManagement::WaitForRunMeSignal();
1288         }
1289
1290         // interrupt me to execute a prioritary command
1291         while (StaticRunner_isInterruptibleCommand() == 1 && StaticRunner_isRunnerAvailable() == 1)
1292         {
1293             StaticRunner_launch();
1294             StaticRunner_setInterruptibleCommand(1);
1295         }
1296
1297         try
1298         {
1299             //reset default values
1300             setResult(NULL);
1301             int iExpectedSize = getExpectedSize();
1302             setExpectedSize(-1);
1303             exp->accept(*this);
1304             setExpectedSize(iExpectedSize);
1305             types::InternalType * pIT = getResult();
1306
1307             // In case of exec file, set the file name in the Macro to store where it is defined.
1308             int iFileID = ConfigVariable::getExecutedFileID();
1309             if (iFileID && exp->isFunctionDec())
1310             {
1311                 types::InternalType* pITMacro = symbol::Context::getInstance()->get(exp->getAs<FunctionDec>()->getSymbol());
1312                 if (pITMacro)
1313                 {
1314                     types::Macro* pMacro = pITMacro->getAs<types::Macro>();
1315                     const wchar_t* filename = getfile_filename(iFileID);
1316                     // scilab.quit is not open with mopen
1317                     // in this case filename is NULL because FileManager have not been filled.
1318                     if (filename)
1319                     {
1320                         pMacro->setFileName(filename);
1321                     }
1322                 }
1323             }
1324
1325             if (pIT != NULL)
1326             {
1327                 bool bImplicitCall = false;
1328                 if (pIT->isCallable()) //to manage call without ()
1329                 {
1330                     types::Callable *pCall = pIT->getAs<types::Callable>();
1331                     types::typed_list out;
1332                     types::typed_list in;
1333                     types::optional_list opt;
1334
1335                     try
1336                     {
1337                         //in this case of calling, we can return only one values
1338                         int iSaveExpectedSize = getExpectedSize();
1339                         setExpectedSize(1);
1340
1341                         pCall->invoke(in, opt, getExpectedSize(), out, e);
1342                         setExpectedSize(iSaveExpectedSize);
1343
1344                         if (out.size() == 0)
1345                         {
1346                             setResult(NULL);
1347                         }
1348                         else
1349                         {
1350                             setResult(out[0]);
1351                         }
1352
1353                         bImplicitCall = true;
1354                     }
1355                     catch (const InternalError& ie)
1356                     {
1357                         if (ConfigVariable::getLastErrorFunction() == L"")
1358                         {
1359                             ConfigVariable::setLastErrorFunction(pCall->getName());
1360                             ConfigVariable::setLastErrorLine(e.getLocation().first_line);
1361                         }
1362                         CoverageInstance::stopChrono((void*)&e);
1363                         throw ie;
1364                     }
1365                 }
1366                 else if (pIT->isImplicitList())
1367                 {
1368                     //expand implicit when possible
1369                     types::ImplicitList* pIL = pIT->getAs<types::ImplicitList>();
1370                     if (pIL->isComputable())
1371                     {
1372                         types::InternalType* p = pIL->extractFullMatrix();
1373                         if (p)
1374                         {
1375                             setResult(p);
1376                         }
1377                     }
1378                 }
1379
1380                 //don't output Simplevar and empty result
1381                 if (getResult() != NULL && (!exp->isSimpleVar() || bImplicitCall))
1382                 {
1383                     //symbol::Context::getInstance()->put(symbol::Symbol(L"ans"), *execMe.getResult());
1384                     types::InternalType* pITAns = getResult();
1385                     symbol::Context::getInstance()->put(m_pAns, pITAns);
1386                     if (exp->isVerbose() && ConfigVariable::isPromptShow())
1387                     {
1388                         //TODO manage multiple returns
1389                         scilabWriteW(L" ans  =\n\n");
1390                         std::wostringstream ostrName;
1391                         ostrName << L"ans";
1392                         VariableToString(pITAns, ostrName.str().c_str());
1393                     }
1394                 }
1395
1396                 pIT->killMe();
1397             }
1398
1399             if (ConfigVariable::getPromptMode() == 7)
1400             {
1401                 Location loc = exp->getLocation();
1402                 if (lastLine < loc.first_line)
1403                 {
1404                     //break execution
1405                     SetTemporaryPrompt(SCIPROMPT_PAUSE);
1406                     ConfigVariable::setScilabCommand(0);
1407                     char* pcConsoleReadStr = ConfigVariable::getConsoleReadStr();
1408                     if (pcConsoleReadStr) // exec is called from a callback
1409                     {
1410                         ThreadManagement::SendConsoleExecDoneSignal();
1411                     }
1412                     else // exec is called from the console
1413                     {
1414                         scilabRead();
1415                         pcConsoleReadStr = ConfigVariable::getConsoleReadStr();
1416                     }
1417
1418                     if (pcConsoleReadStr && pcConsoleReadStr[0] == 'p' && pcConsoleReadStr[1] == '\0')
1419                     {
1420                         //mode pause
1421                         ConfigVariable::setExecutionBreak();
1422                     }
1423                 }
1424
1425                 lastLine = loc.last_line;
1426             }
1427
1428             if ((&e)->isBreakable() && exp->isBreak())
1429             {
1430                 const_cast<SeqExp *>(&e)->setBreak();
1431                 exp->resetBreak();
1432                 break;
1433             }
1434
1435             if ((&e)->isContinuable() && exp->isContinue())
1436             {
1437                 const_cast<SeqExp *>(&e)->setContinue();
1438                 exp->resetContinue();
1439                 break;
1440             }
1441
1442             if ((&e)->isReturnable() && exp->isReturn())
1443             {
1444                 const_cast<SeqExp *>(&e)->setReturn();
1445                 exp->resetReturn();
1446                 break;
1447             }
1448         }
1449         catch (const InternalError& ie)
1450         {
1451             ConfigVariable::fillWhereError(ie.GetErrorLocation().first_line);
1452             CoverageInstance::stopChrono((void*)&e);
1453             throw ie;
1454         }
1455
1456         // If something other than NULL is given to setResult, then that would imply
1457         // to make a cleanup in visit(ForExp) for example (e.getBody().accept(*this);)
1458         setResult(NULL);
1459     }
1460
1461     CoverageInstance::stopChrono((void*)&e);
1462 }
1463
1464 template <class T>
1465 void RunVisitorT<T>::visitprivate(const NotExp &e)
1466 {
1467     CoverageInstance::invokeAndStartChrono((void*)&e);
1468     /*
1469       @ or ~ !
1470       */
1471     try
1472     {
1473         e.getExp().accept(*this);
1474     }
1475     catch (ScilabException &)
1476     {
1477         CoverageInstance::stopChrono((void*)&e);
1478         throw;
1479     }
1480
1481     types::InternalType * pValue = getResult();
1482     types::InternalType * pReturn = NULL;
1483     if (pValue->neg(pReturn))
1484     {
1485         if (pValue != pReturn)
1486         {
1487             pValue->killMe();
1488         }
1489
1490         setResult(pReturn);
1491     }
1492     else
1493     {
1494         // neg returned false so the negation is not possible so we call the overload (%foo_5)
1495         types::typed_list in;
1496         types::typed_list out;
1497
1498         pValue->IncreaseRef();
1499         in.push_back(pValue);
1500
1501         types::Callable::ReturnValue Ret = Overload::call(L"%" + pValue->getShortTypeStr() + L"_5", in, 1, out, this);
1502
1503         if (Ret != types::Callable::OK)
1504         {
1505             cleanInOut(in, out);
1506             CoverageInstance::stopChrono((void*)&e);
1507             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1508         }
1509
1510         setResult(out);
1511         cleanIn(in, out);
1512     }
1513     CoverageInstance::stopChrono((void*)&e);
1514 }
1515
1516 template <class T>
1517 void RunVisitorT<T>::visitprivate(const TransposeExp &e)
1518 {
1519     CoverageInstance::invokeAndStartChrono((void*)&e);
1520     try
1521     {
1522         e.getExp().accept(*this);
1523     }
1524     catch (ScilabException &)
1525     {
1526         CoverageInstance::stopChrono((void*)&e);
1527         throw;
1528     }
1529
1530     if (getResultSize() != 1)
1531     {
1532         clearResult();
1533         wchar_t szError[bsiz];
1534         os_swprintf(szError, bsiz, _W("%ls: Can not transpose multiple elements.\n").c_str(), L"Transpose");
1535         CoverageInstance::stopChrono((void*)&e);
1536         throw InternalError(szError, 999, e.getLocation());
1537     }
1538
1539     types::InternalType * pValue = getResult();
1540     types::InternalType * pReturn = NULL;
1541     const bool bConjug = e.getConjugate() == TransposeExp::_Conjugate_;
1542
1543     if ((bConjug && pValue->adjoint(pReturn)) || (!bConjug && pValue->transpose(pReturn)))
1544     {
1545         if (pValue != pReturn)
1546         {
1547             pValue->killMe();
1548         }
1549
1550         setResult(pReturn);
1551         CoverageInstance::stopChrono((void*)&e);
1552
1553         return;
1554     }
1555     else
1556     {
1557         // transpose returned false so the negation is not possible so we call the overload (%foo_t or %foo_0)
1558         types::typed_list in;
1559         types::typed_list out;
1560
1561         pValue->IncreaseRef();
1562         in.push_back(pValue);
1563
1564         types::Callable::ReturnValue Ret;
1565         if (bConjug)
1566         {
1567             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_t", in, 1, out, this);
1568         }
1569         else
1570         {
1571             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_0", in, 1, out, this);
1572         }
1573
1574         if (Ret != types::Callable::OK)
1575         {
1576             cleanInOut(in, out);
1577             CoverageInstance::stopChrono((void*)&e);
1578             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1579         }
1580
1581         setResult(out);
1582         cleanIn(in, out);
1583     }
1584
1585     CoverageInstance::stopChrono((void*)&e);
1586 }
1587
1588 template <class T>
1589 void RunVisitorT<T>::visitprivate(const FunctionDec & e)
1590 {
1591     CoverageInstance::invokeAndStartChrono((void*)&e);
1592     symbol::Context* ctx = symbol::Context::getInstance();
1593     /*
1594       function foo
1595       endfunction
1596       */
1597
1598     // funcprot(0) : do nothing
1599     // funcprot(1) && warning(on) : warning
1600     //get input parameters list
1601     std::list<symbol::Variable*> *pVarList = new std::list<symbol::Variable*>();
1602     const exps_t & vars = e.getArgs().getVars();
1603     for (const auto var : vars)
1604     {
1605         pVarList->push_back(var->getAs<SimpleVar>()->getStack());
1606     }
1607
1608     //get output parameters list
1609     std::list<symbol::Variable*> *pRetList = new std::list<symbol::Variable*>();
1610     const exps_t & rets = e.getReturns().getVars();
1611     for (const auto ret : rets)
1612     {
1613         pRetList->push_back(ret->getAs<SimpleVar>()->getStack());
1614     }
1615
1616     types::Macro *pMacro = new types::Macro(e.getSymbol().getName(), *pVarList, *pRetList,
1617                                             const_cast<SeqExp&>(static_cast<const SeqExp&>(e.getBody())), L"script");
1618     pMacro->setLines(e.getLocation().first_line, e.getLocation().last_line);
1619
1620     if (ctx->isprotected(symbol::Symbol(pMacro->getName())))
1621     {
1622         delete pMacro;
1623         std::wostringstream os;
1624         os << _W("Redefining permanent variable.\n");
1625         CoverageInstance::stopChrono((void*)&e);
1626         throw InternalError(os.str(), 999, e.getLocation());
1627     }
1628
1629     if (ctx->addMacro(pMacro) == false)
1630     {
1631         char pstError[1024];
1632         char* pstFuncName = wide_string_to_UTF8(e.getSymbol().getName().c_str());
1633         os_sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1634         wchar_t* pwstError = to_wide_string(pstError);
1635         std::wstring wstError(pwstError);
1636         FREE(pstFuncName);
1637         FREE(pwstError);
1638         pMacro->killMe();
1639         CoverageInstance::stopChrono((void*)&e);
1640         throw InternalError(wstError, 999, e.getLocation());
1641     }
1642
1643     CoverageInstance::stopChrono((void*)&e);
1644 }
1645
1646 template <class T>
1647 void RunVisitorT<T>::visitprivate(const ListExp &e)
1648 {
1649     CoverageInstance::invokeAndStartChrono((void*)&e);
1650     try
1651     {
1652         e.getStart().accept(*this);
1653     }
1654     catch (ScilabException &)
1655     {
1656         CoverageInstance::stopChrono((void*)&e);
1657         throw;
1658     }
1659     types::GenericType* pITStart = static_cast<types::GenericType*>(getResult());
1660     if ((pITStart->getSize() != 1 || (pITStart->isDouble() && pITStart->getAs<types::Double>()->isComplex())) &&
1661             pITStart->isList() == false) // list case => call overload
1662     {
1663         pITStart->killMe();
1664         setResult(NULL);
1665         wchar_t szError[bsiz];
1666         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 1);
1667         CoverageInstance::stopChrono((void*)&e);
1668         throw InternalError(szError, 999, e.getLocation());
1669     }
1670     types::InternalType * piStart = pITStart;
1671
1672     try
1673     {
1674         e.getStep().accept(*this);
1675     }
1676     catch (ScilabException &)
1677     {
1678         CoverageInstance::stopChrono((void*)&e);
1679         throw;
1680     }
1681     types::GenericType* pITStep = static_cast<types::GenericType*>(getResult());
1682     setResult(NULL);
1683     if ((pITStep->getSize() != 1 || (pITStep->isDouble() && pITStep->getAs<types::Double>()->isComplex())) &&
1684             pITStep->isList() == false) // list case => call overload
1685     {
1686         pITStart->killMe();
1687         pITStep->killMe();
1688         setResult(NULL);
1689         wchar_t szError[bsiz];
1690         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2);
1691         CoverageInstance::stopChrono((void*)&e);
1692         throw InternalError(szError, 999, e.getLocation());
1693     }
1694     types::InternalType* piStep = pITStep;
1695
1696     try
1697     {
1698         e.getEnd().accept(*this);
1699     }
1700     catch (ScilabException &)
1701     {
1702         CoverageInstance::stopChrono((void*)&e);
1703         throw;
1704     }
1705
1706     types::GenericType* pITEnd = static_cast<types::GenericType*>(getResult());
1707     setResult(NULL);
1708     if ((pITEnd->getSize() != 1 || (pITEnd->isDouble() && pITEnd->getAs<types::Double>()->isComplex())) &&
1709             pITEnd->isList() == false) // list case => call overload
1710     {
1711         pITStart->killMe();
1712         pITStep->killMe();
1713         pITEnd->killMe();
1714         setResult(NULL);
1715         wchar_t szError[bsiz];
1716         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 3);
1717         CoverageInstance::stopChrono((void*)&e);
1718         throw InternalError(szError, 999, e.getLocation());
1719     }
1720     types::InternalType* piEnd = pITEnd;
1721
1722     ////check if implicitlist is 1:$ to replace by ':'
1723     //if (piStart->isDouble() && piStep->isDouble() && piEnd->isPoly())
1724     //{
1725     //    if (piStart->getAs<Double>()->get()[0] == 1 && piStep->getAs<Double>()->get()[0] == 1)
1726     //    {
1727     //        SinglePoly* end = piEnd->getAs<Polynom>()->get()[0];
1728     //        if (end->getRank() == 1 && end->get()[0] == 0 && end->get()[1] == 1)
1729     //        {
1730     //            setResult(new Colon());
1731     //            return;
1732     //        }
1733     //    }
1734     //}
1735
1736     //check compatibility
1737     // double : double : double or poly : poly : poly and mix like double : double : poly
1738     if ((piStart->isPoly() || piStart->isDouble()) &&
1739             (piStep->isPoly() || piStep->isDouble()) &&
1740             (piEnd->isPoly() || piEnd->isDouble()))
1741     {
1742         // No need to kill piStart, ... because Implicit list ctor will incref them
1743         setResult(new types::ImplicitList(piStart, piStep, piEnd));
1744         CoverageInstance::stopChrono((void*)&e);
1745         return;
1746     }
1747
1748     // int : double or int : int
1749     if (piStart->isInt() &&
1750             (piStep->isDouble() || piStep->isInt()) &&
1751             piEnd->isInt())
1752     {
1753         // check for same int type int8, int 16 ...
1754         if (piStart->getType() == piEnd->getType() &&
1755                 (piStart->getType() == piStep->getType() ||
1756                  piStep->isDouble()))
1757         {
1758             // No need to kill piStart, ... because Implicit list ctor will incref them
1759             setResult(new types::ImplicitList(piStart, piStep, piEnd));
1760             CoverageInstance::stopChrono((void*)&e);
1761             return;
1762         }
1763     }
1764
1765     // Call Overload
1766     types::Callable::ReturnValue Ret;
1767     types::typed_list in;
1768     types::typed_list out;
1769
1770     piStart->IncreaseRef();
1771     in.push_back(piStart);
1772
1773     try
1774     {
1775         if (e.hasExplicitStep())
1776         {
1777             // 1:2:4
1778             //call overload %typeStart_b_typeStep
1779             piStep->IncreaseRef();
1780             in.push_back(piStep);
1781             piEnd->IncreaseRef();
1782             in.push_back(piEnd);
1783             Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piStep->getShortTypeStr(), in, 1, out, true);
1784         }
1785         else
1786         {
1787             // 1:2
1788             //call overload %typeStart_b_typeEnd
1789             piStep->killMe();
1790             piEnd->IncreaseRef();
1791             in.push_back(piEnd);
1792             Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piEnd->getShortTypeStr(), in, 1, out, true);
1793         }
1794     }
1795     catch (const InternalError& error)
1796     {
1797         setResult(NULL);
1798         cleanInOut(in, out);
1799         CoverageInstance::stopChrono((void*)&e);
1800         throw error;
1801     }
1802
1803     if (Ret != types::Callable::OK)
1804     {
1805         setResult(NULL);
1806         cleanInOut(in, out);
1807         CoverageInstance::stopChrono((void*)&e);
1808         throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1809     }
1810
1811     setResult(out);
1812     cleanIn(in, out);
1813     CoverageInstance::stopChrono((void*)&e);
1814 }
1815
1816 template <class T>
1817 void RunVisitorT<T>::visitprivate(const OptimizedExp &e)
1818 {
1819 }
1820
1821 template <class T>
1822 void RunVisitorT<T>::visitprivate(const MemfillExp &e)
1823 {
1824     CoverageInstance::invokeAndStartChrono((void*)&e);
1825     try
1826     {
1827         e.getOriginal()->accept(*this);
1828     }
1829     catch (ScilabException &)
1830     {
1831         CoverageInstance::stopChrono((void*)&e);
1832         throw;
1833     }
1834 }
1835
1836 template <class T>
1837 void RunVisitorT<T>::visitprivate(const DAXPYExp &e)
1838 {
1839     CoverageInstance::invokeAndStartChrono((void*)&e);
1840     types::InternalType* pIT = NULL;
1841     types::Double* ad = NULL;
1842     int ar = 0;
1843     int ac = 0;
1844
1845     types::Double* xd = NULL;
1846     int xr = 0;
1847     int xc = 0;
1848
1849     types::Double* yd = NULL;
1850     int yr = 0;
1851     int yc = 0;
1852
1853     //check types and dimensions
1854
1855     //y must be double
1856     const Exp &ye = e.getY();
1857     try
1858     {
1859         ye.accept(*this);
1860     }
1861     catch (ScilabException &)
1862     {
1863         CoverageInstance::stopChrono((void*)&e);
1864         throw;
1865     }
1866
1867     pIT = getResult();
1868     if (pIT->isDouble())
1869     {
1870         yd = pIT->getAs<types::Double>();
1871         if (yd->getDims() == 2 && yd->isComplex() == false)
1872         {
1873             yr = yd->getRows();
1874             yc = yd->getCols();
1875         }
1876         else
1877         {
1878             yd->killMe();
1879             try
1880             {
1881                 e.getOriginal()->accept(*this);
1882             }
1883             catch (ScilabException &)
1884             {
1885                 CoverageInstance::stopChrono((void*)&e);
1886                 throw;
1887             }
1888             CoverageInstance::stopChrono((void*)&e);
1889             return;
1890         }
1891     }
1892     else
1893     {
1894         pIT->killMe();
1895         try
1896         {
1897             e.getOriginal()->accept(*this);
1898         }
1899         catch (ScilabException &)
1900         {
1901             CoverageInstance::stopChrono((void*)&e);
1902             throw;
1903         }
1904         CoverageInstance::stopChrono((void*)&e);
1905         return;
1906     }
1907
1908     //x
1909     const Exp &xe = e.getX();
1910     try
1911     {
1912         xe.accept(*this);
1913     }
1914     catch (ScilabException &)
1915     {
1916         CoverageInstance::stopChrono((void*)&e);
1917         throw;
1918     }
1919     pIT = getResult();
1920
1921     if (pIT->isDouble())
1922     {
1923         xd = pIT->getAs<types::Double>();
1924         if (xd->isScalar() && xd->isComplex() == false)
1925         {
1926             // x become a
1927             ad = xd;
1928             ar = 1;
1929             ac = 1;
1930         }
1931         else if (xd->getDims() == 2 && xd->isComplex() == false)
1932         {
1933             xr = xd->getRows();
1934             xc = xd->getCols();
1935         }
1936         else
1937         {
1938             yd->killMe();
1939             xd->killMe();
1940             try
1941             {
1942                 e.getOriginal()->accept(*this);
1943             }
1944             catch (ScilabException &)
1945             {
1946                 CoverageInstance::stopChrono((void*)&e);
1947                 throw;
1948             }
1949             CoverageInstance::stopChrono((void*)&e);
1950             return;
1951         }
1952     }
1953     else
1954     {
1955         pIT->killMe();
1956         yd->killMe();
1957         try
1958         {
1959             e.getOriginal()->accept(*this);
1960         }
1961         catch (ScilabException &)
1962         {
1963             CoverageInstance::stopChrono((void*)&e);
1964             throw;
1965         }
1966         CoverageInstance::stopChrono((void*)&e);
1967         return;
1968     }
1969
1970     const Exp &ae = e.getA();
1971     try
1972     {
1973         ae.accept(*this);
1974     }
1975     catch (ScilabException &)
1976     {
1977         CoverageInstance::stopChrono((void*)&e);
1978         throw;
1979     }
1980     pIT = getResult();
1981
1982     if (pIT->isDouble())
1983     {
1984         if (ad)
1985         {
1986             xd = pIT->getAs<types::Double>();
1987             //X is scalar it become A
1988             //now use A as X
1989             if (xd->getDims() == 2 && xd->isComplex() == false)
1990             {
1991                 xr = xd->getRows();
1992                 xc = xd->getCols();
1993             }
1994             else
1995             {
1996                 yd->killMe();
1997                 xd->killMe();
1998                 ad->killMe();
1999                 try
2000                 {
2001                     e.getOriginal()->accept(*this);
2002                 }
2003                 catch (ScilabException &)
2004                 {
2005                     CoverageInstance::stopChrono((void*)&e);
2006                     throw;
2007                 }
2008                 CoverageInstance::stopChrono((void*)&e);
2009                 return;
2010             }
2011         }
2012         else
2013         {
2014             //a is a and it must be scalar
2015             ad = pIT->getAs<types::Double>();
2016             if (/*ad->isScalar() && */ad->isComplex() == false)
2017             {
2018                 ar = ad->getRows(); //1;
2019                 ac = ad->getCols();//1;
2020             }
2021             else
2022             {
2023                 yd->killMe();
2024                 xd->killMe();
2025                 ad->killMe();
2026                 try
2027                 {
2028                     e.getOriginal()->accept(*this);
2029                 }
2030                 catch (ScilabException &)
2031                 {
2032                     CoverageInstance::stopChrono((void*)&e);
2033                     throw;
2034                 }
2035                 throw;
2036                 return;
2037             }
2038         }
2039     }
2040     else
2041     {
2042         pIT->killMe();
2043         yd->killMe();
2044         xd->killMe();
2045         try
2046         {
2047             e.getOriginal()->accept(*this);
2048         }
2049         catch (ScilabException &)
2050         {
2051             CoverageInstance::stopChrono((void*)&e);
2052             throw;
2053         }
2054         CoverageInstance::stopChrono((void*)&e);
2055         return;
2056     }
2057
2058     if (ad && xd && yd)
2059     {
2060         if (ac == 1 &&
2061                 ar == 1 &&
2062                 xr == yr &&
2063                 xc == yc)
2064         {
2065             //go !
2066             int one = 1;
2067             int size = xc * xr;
2068             //Double* od = (Double*)yd->clone();
2069             C2F(daxpy)(&size, ad->get(), xd->get(), &one, yd->get(), &one);
2070             //setResult(od);
2071             //yd->killMe();
2072             xd->killMe();
2073             ad->killMe();
2074             CoverageInstance::stopChrono((void*)&e);
2075             return;
2076         }
2077         else if (ac == xr && ar == yr && xc == yc)
2078         {
2079             char n = 'n';
2080             double one = 1;
2081             C2F(dgemm)(&n, &n, &ar, &xc, &ac, &one, ad->get(), &ar, xd->get(), &ac, &one, yd->get(), &ar);
2082             xd->killMe();
2083             ad->killMe();
2084             CoverageInstance::stopChrono((void*)&e);
2085             return;
2086         }
2087     }
2088
2089     if (yd)
2090     {
2091         yd->killMe();
2092     }
2093
2094     if (xd)
2095     {
2096         xd->killMe();
2097     }
2098
2099     if (ad)
2100     {
2101         ad->killMe();
2102     }
2103
2104     try
2105     {
2106         e.getOriginal()->accept(*this);
2107     }
2108     catch (ScilabException &)
2109     {
2110         CoverageInstance::stopChrono((void*)&e);
2111         throw;
2112     }
2113     CoverageInstance::stopChrono((void*)&e);
2114
2115     return;
2116 }
2117
2118 template <class T>
2119 void RunVisitorT<T>::visitprivate(const TryCatchExp  &e)
2120 {
2121     CoverageInstance::invokeAndStartChrono((void*)&e);
2122     //save current prompt mode
2123     int oldVal = ConfigVariable::getSilentError();
2124     int oldMode = ConfigVariable::getPromptMode();
2125     //set mode silent for errors
2126     ConfigVariable::setSilentError(1);
2127
2128     symbol::Context* pCtx = symbol::Context::getInstance();
2129     try
2130     {
2131         int scope = pCtx->getScopeLevel();
2132         int level = ConfigVariable::getRecursionLevel();
2133         try
2134         {
2135             e.getTry().accept(*this);
2136             //restore previous prompt mode
2137             ConfigVariable::setSilentError(oldVal);
2138         }
2139         catch (const RecursionException& /* re */)
2140         {
2141             ConfigVariable::setPromptMode(oldMode);
2142
2143             //close opened scope during try
2144             while (pCtx->getScopeLevel() > scope)
2145             {
2146                 pCtx->scope_end();
2147             }
2148
2149             //decrease recursion to init value and close where
2150             while (ConfigVariable::getRecursionLevel() > level)
2151             {
2152                 ConfigVariable::where_end();
2153                 ConfigVariable::decreaseRecursion();
2154             }
2155
2156             //print msg about recursion limit and trigger an error
2157             wchar_t sz[1024];
2158             os_swprintf(sz, 1024, _W("Recursion limit reached (%d).\n").data(), ConfigVariable::getRecursionLimit());
2159             CoverageInstance::stopChrono((void*)&e);
2160             throw ast::InternalError(sz);
2161         }
2162
2163     }
2164     catch (const InternalError& /* ie */)
2165     {
2166         //restore previous prompt mode
2167         ConfigVariable::setSilentError(oldVal);
2168         //to lock lasterror
2169         ConfigVariable::setLastErrorCall();
2170         // reset call stack filled when error occured
2171         ConfigVariable::resetWhereError();
2172         try
2173         {
2174             e.getCatch().accept(*this);
2175         }
2176         catch (ScilabException &)
2177         {
2178             CoverageInstance::stopChrono((void*)&e);
2179             throw;
2180         }
2181     }
2182     CoverageInstance::stopChrono((void*)&e);
2183 }
2184
2185
2186 } /* namespace ast */
2187
2188 #include "run_CallExp.hpp"
2189 #include "run_MatrixExp.hpp"
2190 #include "run_OpExp.hpp"
2191 #include "run_AssignExp.hpp"
2192
2193 template EXTERN_AST class ast::RunVisitorT<ast::ExecVisitor>;
2194 template EXTERN_AST class ast::RunVisitorT<ast::StepVisitor>;
2195 template EXTERN_AST class ast::RunVisitorT<ast::TimedVisitor>;
2196 template EXTERN_AST class ast::RunVisitorT<ast::DebuggerVisitor>;