LCOV - code coverage report
Current view: top level - src/visitors/typeAnalysis - typeAnalysisVector.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 71 0
Test Date: 2025-04-18 15:53:49 Functions: 0.0 % 2 0

            Line data    Source code
       1              : #include "../../../inc/parsingAnalysis/ast/vector/ast_index.h"
       2              : #include "../../../inc/parsingAnalysis/ast/vector/ast_vector.h"
       3              : #include "../../../inc/visitors/typeAnalysis/typeAnalysis.h"
       4              : #include <cstddef>
       5              : #include <memory>
       6              : 
       7              : namespace nicole {
       8              : 
       9              : /*
      10              : - si esta vacio retorna un vector<null>
      11              : - si no esta vacio, comprueba que todos los elementos sean de un mismo tipo
      12              : - si un elemento es un puntero userType debe comprobar que todos parten de una
      13              : misma base
      14              : */
      15              : std::expected<std::shared_ptr<Type>, Error>
      16            0 : TypeAnalysis::visit(const AST_VECTOR *node) const noexcept {
      17            0 :   if (!node) {
      18            0 :     return createError(ERROR_TYPE::NULL_NODE, "invalid AST_VECTOR");
      19            0 :   }
      20            0 :   const auto &values = node->values();
      21            0 :   if (values.empty()) {
      22            0 :     const auto emptyVecType{std::make_shared<VectorType>(typeTable_->null())};
      23            0 :     node->setReturnedFromAnalysis(emptyVecType);
      24            0 :     return emptyVecType;
      25            0 :   }
      26            0 :   auto firstResult = values[0]->accept(*this);
      27            0 :   if (!firstResult)
      28            0 :     return createError(firstResult.error());
      29            0 :   auto commonType = firstResult.value();
      30              :   /*
      31              :   if (insideDeclWithGenerics &&
      32              :       typeTable_->isGenericType(commonType, currentGenericList_))
      33              :     commonType = std::make_shared<PlaceHolder>(commonType);
      34              :   */
      35            0 :   for (size_t i = 1; i < values.size(); ++i) {
      36            0 :     auto result = values[i]->accept(*this);
      37            0 :     if (!result)
      38            0 :       return createError(result.error());
      39            0 :     auto elemType = result.value();
      40              : 
      41            0 :     if (auto constElem = std::dynamic_pointer_cast<ConstType>(elemType))
      42            0 :       elemType = constElem->baseType();
      43              :     /*
      44              :     if (insideDeclWithGenerics &&
      45              :         typeTable_->isGenericType(elemType, currentGenericList_))
      46              :       elemType = std::make_shared<PlaceHolder>(elemType);
      47              :     */
      48            0 :     if (!typeTable_->areSameType(commonType, elemType)) {
      49            0 :       if (!typeTable_->haveCommonAncestor(commonType, elemType))
      50            0 :         return createError(ERROR_TYPE::TYPE,
      51            0 :                            "vector elements do not have a common type");
      52            0 :     }
      53            0 :   }
      54            0 :   const auto vecType{std::make_shared<VectorType>(commonType)};
      55            0 :   node->setReturnedFromAnalysis(vecType);
      56            0 :   return vecType;
      57            0 : }
      58              : 
      59              : /*
      60              : - el value debe ser de tipo int
      61              : - se apoya en currentType para deducir el tipo del elemento al que accede
      62              :  */
      63              : std::expected<std::shared_ptr<Type>, Error>
      64            0 : TypeAnalysis::visit(const AST_INDEX *node) const noexcept {
      65            0 :   if (!node) {
      66            0 :     return createError(ERROR_TYPE::NULL_NODE, "invalid AST_INDEX");
      67            0 :   }
      68            0 :   const auto result{node->index()->accept(*this)};
      69            0 :   if (!result) {
      70            0 :     return createError(result.error());
      71            0 :   }
      72              : 
      73            0 :   bool isGeneric{false};
      74            0 :   if (insideDeclWithGenerics and
      75            0 :       typeTable_->isGenericType(*result, currentGenericList_)) {
      76            0 :     isGeneric = true;
      77              :     // return *result;
      78            0 :   }
      79              : 
      80            0 :   if (!isGeneric) {
      81            0 :     if (!typeTable_->areSameType(*result, typeTable_->intType())) {
      82            0 :       return createError(ERROR_TYPE::TYPE, "index must be type int");
      83            0 :     }
      84            0 :   }
      85              : 
      86            0 :   const auto vectorType{std::dynamic_pointer_cast<VectorType>(currentType_)};
      87            0 :   const auto basicType{std::dynamic_pointer_cast<BasicType>(currentType_)};
      88            0 :   if (!vectorType and !basicType) {
      89            0 :     return createError(ERROR_TYPE::TYPE,
      90            0 :                        "can only access to vectors or strings");
      91            0 :   }
      92            0 :   std::shared_ptr<Type> indexType{nullptr};
      93            0 :   if (vectorType) {
      94            0 :     indexType = vectorType->elementType();
      95            0 :     node->setReturnedFromAnalysis(indexType);
      96            0 :   }
      97            0 :   if (basicType) {
      98            0 :     if (basicType->baseKind() != BasicKind::Str) {
      99            0 :       return createError(ERROR_TYPE::TYPE,
     100            0 :                          "can only access to vectors or strings");
     101            0 :     }
     102            0 :     indexType = typeTable_->charType();
     103            0 :     node->setReturnedFromAnalysis(indexType);
     104            0 :   }
     105            0 :   currentType_ = indexType;
     106            0 :   return indexType;
     107            0 : }
     108              : 
     109              : } // namespace nicole
        

Generated by: LCOV version 2.0-1