* Bugs 16012, 16014, 16085: fixed regression of insertion in structs
[scilab.git] / scilab / modules / ast / src / cpp / ast / visitor_common.cpp
index 3824223..6ba5e0c 100644 (file)
@@ -616,7 +616,7 @@ types::InternalType* callOverload(const ast::Exp& e, const std::wstring& _strTyp
 
     types::InternalType* pFunc = symbol::Context::getInstance()->get(symbol::Symbol(function_name));
     if (pFunc == NULL &&
-        (_source->getShortTypeStr().size() > 8 || _dest && _dest->getShortTypeStr().size() > 8))
+            (_source->getShortTypeStr().size() > 8 || _dest && _dest->getShortTypeStr().size() > 8))
     {
         if (_source->getShortTypeStr().size() > 8)
         {
@@ -756,9 +756,9 @@ bool getFieldsFromExp(ast::Exp* _pExp, std::list<ExpHistory*>& fields)
         // used to manage insertion with list in argument
         // a(list("field", 2)) = 2 as a.field(2)
         if (pCurrentArgs &&  pCurrentArgs->size() > 0 &&
-            (*pCurrentArgs)[0]->isList() &&
-            (*pCurrentArgs)[0]->isTList() == false &&
-            (*pCurrentArgs)[0]->isMList() == false)
+                (*pCurrentArgs)[0]->isList() &&
+                (*pCurrentArgs)[0]->isTList() == false &&
+                (*pCurrentArgs)[0]->isMList() == false)
         {
             bArgList = true;
             pList = (*pCurrentArgs)[0]->getAs<types::List>();
@@ -772,9 +772,9 @@ bool getFieldsFromExp(ast::Exp* _pExp, std::list<ExpHistory*>& fields)
         do
         {
             if (pCurrentArgs &&
-                pCurrentArgs->size() == 1 &&
-                (*pCurrentArgs)[0]->isString() &&
-                (*pCurrentArgs)[0]->getAs<types::String>()->getSize() == 1)
+                    pCurrentArgs->size() == 1 &&
+                    (*pCurrentArgs)[0]->isString() &&
+                    (*pCurrentArgs)[0]->getAs<types::String>()->getSize() == 1)
             {
                 // a("b") => a.b or a(x)("b") => a(x).b
                 ExpHistory * pEHParent = fields.back();
@@ -815,7 +815,8 @@ bool getFieldsFromExp(ast::Exp* _pExp, std::list<ExpHistory*>& fields)
                     pCurrentArgs->push_back(pList->get(iListIncr)->clone());
                 }
             }
-        } while (iListIncr < iListSize);
+        }
+        while (iListIncr < iListSize);
 
         if (bArgList)
         {
@@ -910,11 +911,11 @@ types::InternalType* evaluateFields(const ast::Exp* _pExp, std::list<ExpHistory*
         iterFields++;
 
         workFields.push_back(new ExpHistory(NULL,
-            pFirstField->getExp(),
-            pFirstField->getArgs(),
-            pFirstField->getLevel(),
-            pFirstField->isCellExp(),
-            pITMain));
+                                            pFirstField->getExp(),
+                                            pFirstField->getArgs(),
+                                            pFirstField->getLevel(),
+                                            pFirstField->isCellExp(),
+                                            pITMain));
 
         //*** evaluate fields ***//
         while (iterFields != fields.end())
@@ -1018,11 +1019,11 @@ types::InternalType* evaluateFields(const ast::Exp* _pExp, std::list<ExpHistory*
                         }
 
                         ExpHistory* pEHChield = new ExpHistory(pEH,
-                            (*iterFields)->getExp(),
-                            (*iterFields)->getArgs(),
-                            (*iterFields)->getLevel(),
-                            (*iterFields)->isCellExp(),
-                            pIT);
+                                                               (*iterFields)->getExp(),
+                                                               (*iterFields)->getArgs(),
+                                                               (*iterFields)->getLevel(),
+                                                               (*iterFields)->isCellExp(),
+                                                               pIT);
 
                         pEHChield->setWhereReinsert(0);
                         workFields.push_back(pEHChield);
@@ -2038,7 +2039,7 @@ types::InternalType* insertionCall(const ast::Exp& e, types::typed_list* _pArgs,
             }
             else // insert something in a struct
             {
-                if (_pInsert->isStruct())
+                if (_pInsert->isStruct() && _pInsert->getAs<types::Struct>()->isEmpty() == false)
                 {
                     types::String* pStrFieldsName = pStruct->getFieldNames();
                     types::Struct* pStructInsert = _pInsert->clone()->getAs<types::Struct>();
@@ -2082,6 +2083,14 @@ types::InternalType* insertionCall(const ast::Exp& e, types::typed_list* _pArgs,
 
                         pStrFieldsName->killMe();
                     }
+                    else if (pStrInsertFieldsName)
+                    {
+                        //insertion of non-empty struct in empty struct
+                        for (int i = pStrInsertFieldsName->getSize(); i > 0; i--)
+                        {
+                            pStruct->addFieldFront(pStrInsertFieldsName->get(i - 1));
+                        }
+                    }
 
                     // insert elements in following pArgs
                     pRet = pStruct->insert(_pArgs, pStructInsert);
@@ -2089,16 +2098,29 @@ types::InternalType* insertionCall(const ast::Exp& e, types::typed_list* _pArgs,
 
                     pStructInsert->killMe();
 
-                    // insert fields of pStructInsert in pRet
-                    for (int i = 0; i < pStrInsertFieldsName->getSize(); i++)
+                    // if not an empty struct
+                    if (pStrFieldsName)
                     {
-                        if (pStructRet->exists(pStrInsertFieldsName->get(i)) == false)
+                        // insert fields of pStructInsert in pRet
+                        for (int i = 0; i < pStrInsertFieldsName->getSize(); i++)
                         {
-                            pStructRet->addField(pStrInsertFieldsName->get(i));
+                            if (pStructRet->exists(pStrInsertFieldsName->get(i)) == false)
+                            {
+                                pStructRet->addField(pStrInsertFieldsName->get(i));
+                            }
                         }
-                    }
 
-                    pStrInsertFieldsName->killMe();
+                        pStrInsertFieldsName->killMe();
+                    }
+                }
+                else if (_pInsert->isStruct())
+                {
+                    // insertion of empty struct in a struct
+                    pRet = pStruct;
+                }
+                else if (_pInsert->isDouble() && _pInsert->getAs<types::Double>()->isEmpty())
+                {
+                    pRet = pStruct->remove(_pArgs);
                 }
                 else
                 {