LCOV - code coverage report
Current view: top level - data/include/mcrl2/data/detail - variable_context.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 27 29 93.1 %
Date: 2020-02-21 00:44:40 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Wieger Wesselink
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file mcrl2/data/detail/variable_context.h
      10             : /// \brief add your file description here.
      11             : 
      12             : #ifndef MCRL2_DATA_DETAIL_VARIABLE_CONTEXT_H
      13             : #define MCRL2_DATA_DETAIL_VARIABLE_CONTEXT_H
      14             : 
      15             : #include "mcrl2/data/variable.h"
      16             : 
      17             : namespace mcrl2 {
      18             : 
      19             : namespace data {
      20             : 
      21             : class data_type_checker;
      22             : 
      23             : namespace detail {
      24             : 
      25             : // Throws an exception if the names of the variables are not unique
      26             : inline
      27        3152 : void check_duplicate_variable_names(const data::variable_list& x, const std::string& msg)
      28             : {
      29        6304 :   std::set<core::identifier_string> names;
      30        8781 :   for (const data::variable& v: x)
      31             :   {
      32        5629 :     auto p = names.insert(v.name());
      33        5629 :     if (!p.second)
      34             :     {
      35           0 :       throw mcrl2::runtime_error("Duplicate " + msg + " " + std::string(v.name()) + " encountered");
      36             :     }
      37             :   }
      38        3152 : }
      39             : 
      40       28554 : class variable_context
      41             : {
      42             :   private:
      43             :     std::map<core::identifier_string, sort_expression> m_variables;
      44             : 
      45             :   protected:
      46             :     void typecheck_variable(const data_type_checker& typechecker, const variable& v) const;
      47             : 
      48             :   public:
      49        4668 :     variable_context()
      50        4668 :     { }
      51             : 
      52             :     explicit variable_context(const std::map<core::identifier_string, sort_expression>& variables)
      53             :       : m_variables(variables)
      54             :     { }
      55             : 
      56      114134 :     const std::map<core::identifier_string, sort_expression>& context() const
      57             :     {
      58      114134 :       return m_variables;
      59             :     }
      60             : 
      61             :     // Adds the elements of variables to variable_map without checking.
      62             :     template <typename VariableContainer>
      63        1342 :     void add_context_variables(const VariableContainer& variables)
      64             :     {
      65        7771 :       for (const data::variable& v: variables)
      66             :       {
      67        6429 :           m_variables[v.name()] = v.sort();
      68             :       }
      69        1342 :     }
      70             : 
      71             : 
      72             :     // Adds the elements of variables to variable_map
      73             :     // Throws an exception if a typecheck error is encountered
      74             :     template <typename VariableContainer>
      75        7667 :     void add_context_variables(const VariableContainer& variables, const data_type_checker& typechecker)
      76             :     {
      77             :       // first remove the existing entries
      78       17522 :       for (const data::variable& v: variables)
      79             :       {
      80        9855 :         m_variables.erase(v.name());
      81             :       }
      82             : 
      83       25551 :       for (const data::variable& v: variables)
      84             :       {
      85        9855 :         typecheck_variable(typechecker, v);
      86        9855 :         auto i = m_variables.find(v.name());
      87        9855 :         if (i == m_variables.end())
      88             :         {
      89        9855 :           m_variables[v.name()] = v.sort();
      90             :         }
      91             :         else
      92             :         {
      93           0 :           throw mcrl2::runtime_error("attempt to overload global variable " + core::pp(v.name()));
      94             :         }
      95             :       }
      96        7667 :     }
      97             : 
      98        1422 :     void clear()
      99             :     {
     100        1422 :       m_variables.clear();
     101        1422 :     }
     102             : };
     103             : 
     104             : } // namespace detail
     105             : 
     106             : } // namespace data
     107             : 
     108             : } // namespace mcrl2
     109             : 
     110             : #endif // MCRL2_DATA_DETAIL_VARIABLE_CONTEXT_H

Generated by: LCOV version 1.13