LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/expr - term_context.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 66 78 84.6 %
Date: 2026-02-03 13:01:51 Functions: 16 22 72.7 %
Branches: 48 68 70.6 %

           Branch data     Line data    Source code
       1                 :            : /******************************************************************************
       2                 :            :  * Top contributors (to current version):
       3                 :            :  *   Andrew Reynolds, Aina Niemetz, Mathias Preiner
       4                 :            :  *
       5                 :            :  * This file is part of the cvc5 project.
       6                 :            :  *
       7                 :            :  * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
       8                 :            :  * in the top-level source directory and their institutional affiliations.
       9                 :            :  * All rights reserved.  See the file COPYING in the top-level source
      10                 :            :  * directory for licensing information.
      11                 :            :  * ****************************************************************************
      12                 :            :  *
      13                 :            :  * Implementation of term context utilities.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include "expr/term_context.h"
      17                 :            : 
      18                 :            : #include "expr/node_algorithm.h"
      19                 :            : #include "theory/theory.h"
      20                 :            : 
      21                 :            : namespace cvc5::internal {
      22                 :            : 
      23                 :        935 : uint32_t TermContext::computeValueOp(TNode t, uint32_t tval) const
      24                 :            : {
      25                 :            :   // default is no change
      26                 :        935 :   return tval;
      27                 :            : }
      28                 :            : 
      29                 :    1865020 : uint32_t RtfTermContext::initialValue() const
      30                 :            : {
      31                 :            :   // by default, not in a term context or a quantifier
      32                 :    1865020 :   return 0;
      33                 :            : }
      34                 :            : 
      35                 :   25813900 : uint32_t RtfTermContext::computeValue(TNode t,
      36                 :            :                                       uint32_t tval,
      37                 :            :                                       size_t child) const
      38                 :            : {
      39         [ +  + ]:   25813900 :   if (t.isClosure())
      40                 :            :   {
      41         [ +  + ]:       5672 :     if (tval % 2 == 0)
      42                 :            :     {
      43                 :       5172 :       return tval + 1;
      44                 :            :     }
      45                 :            :   }
      46         [ +  + ]:   25808300 :   else if (hasNestedTermChildren(t))
      47                 :            :   {
      48         [ +  + ]:    6288400 :     if (tval < 2)
      49                 :            :     {
      50                 :    3577300 :       return tval + 2;
      51                 :            :     }
      52                 :            :   }
      53                 :   22231500 :   return tval;
      54                 :            : }
      55                 :            : 
      56                 :          0 : uint32_t RtfTermContext::getValue(bool inQuant, bool inTerm)
      57                 :            : {
      58 [ -  - ][ -  - ]:          0 :   return (inQuant ? 1 : 0) + 2 * (inTerm ? 1 : 0);
      59                 :            : }
      60                 :            : 
      61                 :    5453810 : void RtfTermContext::getFlags(uint32_t val, bool& inQuant, bool& inTerm)
      62                 :            : {
      63                 :    5453810 :   inQuant = val % 2 == 1;
      64                 :    5453810 :   inTerm = val >= 2;
      65                 :    5453810 : }
      66                 :            : 
      67                 :   25808300 : bool RtfTermContext::hasNestedTermChildren(TNode t)
      68                 :            : {
      69                 :   25808300 :   Kind k = t.getKind();
      70                 :            :   // dont' worry about FORALL or EXISTS, these are part of inQuant.
      71         [ +  + ]:   35305900 :   return theory::kindToTheoryId(k) != theory::THEORY_BOOL && k != Kind::EQUAL
      72 [ +  + ][ +  + ]:    6297730 :          && k != Kind::SEP_STAR && k != Kind::SEP_WAND && k != Kind::SEP_LABEL
                 [ +  + ]
      73 [ +  + ][ +  + ]:   35305900 :          && k != Kind::BITVECTOR_EAGER_ATOM;
      74                 :            : }
      75                 :            : 
      76                 :          0 : uint32_t InQuantTermContext::initialValue() const { return 0; }
      77                 :            : 
      78                 :          0 : uint32_t InQuantTermContext::computeValue(TNode t,
      79                 :            :                                           uint32_t tval,
      80                 :            :                                           size_t index) const
      81                 :            : {
      82         [ -  - ]:          0 :   return t.isClosure() ? 1 : tval;
      83                 :            : }
      84                 :            : 
      85         [ -  - ]:          0 : uint32_t InQuantTermContext::getValue(bool inQuant) { return inQuant ? 1 : 0; }
      86                 :            : 
      87                 :          0 : bool InQuantTermContext::inQuant(uint32_t val, bool& inQuant)
      88                 :            : {
      89                 :          0 :   return val == 1;
      90                 :            : }
      91                 :            : 
      92                 :      17972 : uint32_t PolarityTermContext::initialValue() const
      93                 :            : {
      94                 :            :   // by default, we have true polarity
      95                 :      17972 :   return 2;
      96                 :            : }
      97                 :            : 
      98                 :     198918 : uint32_t PolarityTermContext::computeValue(TNode t,
      99                 :            :                                            uint32_t tval,
     100                 :            :                                            size_t index) const
     101                 :            : {
     102 [ +  + ][ +  + ]:     198918 :   switch (t.getKind())
                 [ -  + ]
     103                 :            :   {
     104                 :      19296 :     case Kind::AND:
     105                 :            :     case Kind::OR:
     106                 :            :     case Kind::SEP_STAR:
     107                 :            :       // polarity preserved
     108                 :      19296 :       return tval;
     109                 :         72 :     case Kind::IMPLIES:
     110                 :            :       // first child reverses, otherwise we preserve
     111 [ +  + ][ +  - ]:         72 :       return index == 0 ? (tval == 0 ? 0 : (3 - tval)) : tval;
     112                 :      17881 :     case Kind::NOT:
     113                 :            :       // polarity reversed
     114         [ +  + ]:      17881 :       return tval == 0 ? 0 : (3 - tval);
     115                 :       1867 :     case Kind::ITE:
     116                 :            :       // head has no polarity, branches preserve
     117         [ +  + ]:       1867 :       return index == 0 ? 0 : tval;
     118                 :          0 :     case Kind::FORALL:
     119                 :            :       // body preserves, others have no polarity.
     120         [ -  - ]:          0 :       return index == 1 ? tval : 0;
     121                 :     159802 :     default:
     122                 :            :       // no polarity
     123                 :     159802 :       break;
     124                 :            :   }
     125                 :     159802 :   return 0;
     126                 :            : }
     127                 :            : 
     128                 :          0 : uint32_t PolarityTermContext::getValue(bool hasPol, bool pol)
     129                 :            : {
     130 [ -  - ][ -  - ]:          0 :   return hasPol ? (pol ? 2 : 1) : 0;
     131                 :            : }
     132                 :            : 
     133                 :      66706 : void PolarityTermContext::getFlags(uint32_t val, bool& hasPol, bool& pol)
     134                 :            : {
     135                 :      66706 :   hasPol = val != 0;
     136                 :      66706 :   pol = val == 2;
     137                 :      66706 : }
     138                 :            : 
     139                 :      15884 : uint32_t TheoryLeafTermContext::initialValue() const { return 0; }
     140                 :            : 
     141                 :     123492 : uint32_t TheoryLeafTermContext::computeValue(TNode t,
     142                 :            :                                              uint32_t tval,
     143                 :            :                                              size_t index) const
     144                 :            : {
     145         [ +  + ]:     123492 :   return theory::Theory::isLeafOf(t, d_theoryId) ? 1 : tval;
     146                 :            : }
     147                 :      64884 : uint32_t BoolSkeletonTermContext::initialValue() const { return 0; }
     148                 :            : 
     149                 :     953662 : uint32_t BoolSkeletonTermContext::computeValue(TNode t,
     150                 :            :                                                uint32_t tval,
     151                 :            :                                                size_t child) const
     152                 :            : {
     153         [ +  + ]:     953662 :   if (tval == 0)
     154                 :            :   {
     155         [ +  + ]:     472822 :     if (!expr::isBooleanConnective(t))
     156                 :            :     {
     157                 :     213730 :       return 1;
     158                 :            :     }
     159                 :     259092 :     return 0;
     160                 :            :   }
     161                 :     480840 :   return 1;
     162                 :            : }
     163                 :            : 
     164                 :       8566 : uint32_t WithinKindTermContext::initialValue() const { return 0; }
     165                 :            : 
     166                 :     447102 : uint32_t WithinKindTermContext::computeValue(TNode t,
     167                 :            :                                              uint32_t tval,
     168                 :            :                                              size_t index) const
     169                 :            : {
     170         [ +  + ]:     447102 :   if (tval == 0)
     171                 :            :   {
     172         [ +  + ]:     446990 :     if (t.getKind() == d_kind)
     173                 :            :     {
     174                 :         46 :       return 1;
     175                 :            :     }
     176                 :     446944 :     return 0;
     177                 :            :   }
     178                 :        112 :   return 1;
     179                 :            : }
     180                 :            : 
     181                 :       1648 : uint32_t WithinPathTermContext::initialValue() const { return 1; }
     182                 :            : 
     183                 :     599868 : uint32_t WithinPathTermContext::computeValue(TNode t,
     184                 :            :                                              uint32_t tval,
     185                 :            :                                              size_t index) const
     186                 :            : {
     187         [ +  + ]:     599868 :   if (tval == 0)
     188                 :            :   {
     189                 :     568548 :     return 0;
     190                 :            :   }
     191 [ -  + ][ -  + ]:      31320 :   Assert(!d_path.empty());
                 [ -  - ]
     192                 :      31320 :   size_t cc = (tval - 1) % d_path.size();
     193         [ +  + ]:      31320 :   if (index == d_path[cc])
     194                 :            :   {
     195                 :      10684 :     return tval + 1;
     196                 :            :   }
     197                 :      20636 :   return 0;
     198                 :            : }
     199                 :            : }  // namespace cvc5::internal

Generated by: LCOV version 1.14