mCRL2
Loading...
Searching...
No Matches
variable_context.h
Go to the documentation of this file.
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//
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
17namespace mcrl2 {
18
19namespace data {
20
21class data_type_checker;
22
23namespace detail {
24
25// Throws an exception if the names of the variables are not unique
26inline
27void check_duplicate_variable_names(const data::variable_list& x, const std::string& msg)
28{
29 std::set<core::identifier_string> names;
30 for (const data::variable& v: x)
31 {
32 auto p = names.insert(v.name());
33 if (!p.second)
34 {
35 throw mcrl2::runtime_error("Duplicate " + msg + " " + std::string(v.name()) + " encountered");
36 }
37 }
38}
39
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:
50 { }
51
52 explicit variable_context(const std::map<core::identifier_string, sort_expression>& variables)
53 : m_variables(variables)
54 { }
55
56 const std::map<core::identifier_string, sort_expression>& context() const
57 {
58 return m_variables;
59 }
60
61 // Adds the elements of variables to variable_map without checking.
62 template <typename VariableContainer>
63 void add_context_variables(const VariableContainer& variables)
64 {
65 for (const data::variable& v: variables)
66 {
67 m_variables[v.name()] = v.sort();
68 }
69 }
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 void add_context_variables(const VariableContainer& variables, const data_type_checker& typechecker)
76 {
77 // first remove the existing entries
78 for (const data::variable& v: variables)
79 {
80 m_variables.erase(v.name());
81 }
82
83 for (const data::variable& v: variables)
84 {
85 typecheck_variable(typechecker, v);
86 auto i = m_variables.find(v.name());
87 if (i == m_variables.end())
88 {
89 m_variables[v.name()] = v.sort();
90 }
91 else
92 {
93 throw mcrl2::runtime_error("attempt to overload global variable " + core::pp(v.name()));
94 }
95 }
96 }
97
98 void clear()
99 {
100 m_variables.clear();
101 }
102};
103
104} // namespace detail
105
106} // namespace data
107
108} // namespace mcrl2
109
110#endif // MCRL2_DATA_DETAIL_VARIABLE_CONTEXT_H
void typecheck_variable(const data_type_checker &typechecker, const variable &v) const
Definition typecheck.cpp:25
void add_context_variables(const VariableContainer &variables)
void add_context_variables(const VariableContainer &variables, const data_type_checker &typechecker)
std::map< core::identifier_string, sort_expression > m_variables
const std::map< core::identifier_string, sort_expression > & context() const
variable_context(const std::map< core::identifier_string, sort_expression > &variables)
\brief A data variable
Definition variable.h:28
Standard exception class for reporting runtime errors.
Definition exception.h:27
std::string pp(const identifier_string &x)
Definition core.cpp:26
void check_duplicate_variable_names(const data::variable_list &x, const std::string &msg)
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
The class variable.