Skip to content

Commit 1548399

Browse files
committed
Add smt_set_option_commandt
1 parent dc29af3 commit 1548399

File tree

6 files changed

+161
-1
lines changed

6 files changed

+161
-1
lines changed

src/solvers/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ SRC = $(BOOLEFORCE_SRC) \
193193
smt2/smt2_tokenizer.cpp \
194194
smt2/smt2irep.cpp \
195195
smt2_incremental/smt_commands.cpp \
196+
smt2_incremental/smt_options.cpp \
196197
smt2_incremental/smt_sorts.cpp \
197198
smt2_incremental/smt_terms.cpp \
198199
smt2_incremental/smt_to_string.cpp \

src/solvers/smt2_incremental/smt_commands.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ size_t smt_pop_commandt::levels() const
144144
return get_size_t(ID_value);
145145
}
146146

147+
smt_set_option_commandt::smt_set_option_commandt(smt_optiont option)
148+
: smt_commandt(ID_smt_set_option_command)
149+
{
150+
set(ID_value, upcast(std::move(option)));
151+
}
152+
153+
const smt_optiont &smt_set_option_commandt::option() const
154+
{
155+
return downcast(find(ID_value));
156+
}
157+
147158
template <typename visitort>
148159
void accept(const smt_commandt &command, const irep_idt &id, visitort &&visitor)
149160
{

src/solvers/smt2_incremental/smt_commands.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_COMMANDS_H
55

66
#include <solvers/smt2_incremental/smt_terms.h>
7+
#include <solvers/smt2_incremental/smt_options.h>
78
#include <util/irep.h>
89

910
class smt_command_const_downcast_visitort;
@@ -103,9 +104,13 @@ class smt_set_logic_commandt : public smt_commandt
103104
public:
104105
};
105106

106-
class smt_set_option_commandt : public smt_commandt
107+
class smt_set_option_commandt :
108+
public smt_commandt,
109+
private smt_optiont::storert<smt_set_option_commandt>
107110
{
108111
public:
112+
explicit smt_set_option_commandt(smt_optiont option);
113+
const smt_optiont &option() const;
109114
};
110115

111116
class smt_command_const_downcast_visitort
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Author: Diffblue Ltd.
2+
3+
#include <solvers/smt2_incremental/smt_options.h>
4+
5+
// Define the irep_idts for options.
6+
#define OPTION_ID(the_id) \
7+
const irep_idt ID_smt_option_##the_id{"smt_option_" #the_id};
8+
#include <solvers/smt2_incremental/smt_options.def>
9+
#undef OPTION_ID
10+
11+
smt_optiont::smt_optiont(const irep_idt id) : irept(id)
12+
{
13+
}
14+
15+
bool smt_optiont::operator==(const smt_optiont &other) const {
16+
return irept::operator==(other);
17+
}
18+
19+
bool smt_optiont::operator!=(const smt_optiont &other) const {
20+
return !(*this == other);
21+
}
22+
23+
smt_option_produce_modelst::smt_option_produce_modelst(bool setting)
24+
: smt_optiont{ID_smt_option_produce_models}
25+
{
26+
set(ID_value, setting);
27+
}
28+
29+
bool smt_option_produce_modelst::setting() const
30+
{
31+
return get_bool(ID_value);
32+
}
33+
34+
template <typename visitort>
35+
void accept(const smt_optiont &option, const irep_idt &id, visitort &&visitor)
36+
{
37+
#define OPTION_ID(the_id) \
38+
if(id == ID_smt_option_##the_id) \
39+
return visitor.visit(static_cast<const smt_option_##the_id##t &>(option));
40+
// The include below is marked as nolint because including the same file
41+
// multiple times is required as part of the x macro pattern.
42+
#include <solvers/smt2_incremental/smt_options.def> // NOLINT(build/include)
43+
#undef OPTION_ID
44+
UNREACHABLE;
45+
}
46+
47+
void smt_optiont::accept(smt_option_const_downcast_visitort &visitor) const
48+
{
49+
::accept(*this, id(), visitor);
50+
}
51+
52+
void smt_optiont::accept(smt_option_const_downcast_visitort &&visitor) const
53+
{
54+
::accept(*this, id(), std::move(visitor));
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// \file
2+
/// This set of definitions is used as part of the
3+
/// [X-macro](https://en.wikipedia.org/wiki/X_Macro) pattern. These define the
4+
/// set of options which are implemented and it is used to automate repetitive
5+
/// parts of the implementation. These include -
6+
/// * The constant `irep_idt`s used to identify each of the option classes.
7+
/// * The member functions of the `smt_option_const_downcast_visitort` class.
8+
/// * The type of option checks required to implement `smt_optiont::accept`.
9+
OPTION_ID(produce_models)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Author: Diffblue Ltd.
2+
3+
#ifndef CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_OPTIONS_H
4+
#define CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_OPTIONS_H
5+
6+
#include <util/irep.h>
7+
8+
class smt_option_const_downcast_visitort;
9+
10+
class smt_optiont : protected irept
11+
{
12+
public:
13+
// smt_optiont does not support the notion of an empty / null state. Use
14+
// optionalt<smt_optiont> instead if an empty option is required.
15+
smt_optiont() = delete;
16+
17+
using irept::pretty;
18+
19+
bool operator==(const smt_optiont &) const;
20+
bool operator!=(const smt_optiont &) const;
21+
22+
void accept(smt_option_const_downcast_visitort &) const;
23+
void accept(smt_option_const_downcast_visitort &&) const;
24+
25+
/// \brief Class for adding the ability to up and down cast smt_optiont to and
26+
/// from irept. These casts are required by other irept derived classes in
27+
/// order to store instances of smt_optiont inside them.
28+
/// \tparam derivedt The type of class which derives from this class and from
29+
/// irept.
30+
template <typename derivedt>
31+
class storert
32+
{
33+
protected:
34+
storert();
35+
static irept upcast(smt_optiont option);
36+
static const smt_optiont &downcast(const irept &);
37+
};
38+
39+
protected:
40+
explicit smt_optiont(irep_idt id);
41+
};
42+
43+
template <typename derivedt>
44+
smt_optiont::storert<derivedt>::storert()
45+
{
46+
static_assert(
47+
std::is_base_of<irept, derivedt>::value &&
48+
std::is_base_of<storert<derivedt>, derivedt>::value,
49+
"Only irept based classes need to upcast smt_termt to store it.");
50+
}
51+
52+
template <typename derivedt>
53+
irept smt_optiont::storert<derivedt>::upcast(smt_optiont option)
54+
{
55+
return static_cast<irept &&>(std::move(option));
56+
}
57+
58+
template <typename derivedt>
59+
const smt_optiont &smt_optiont::storert<derivedt>::downcast(const irept &irep)
60+
{
61+
return static_cast<const smt_optiont &>(irep);
62+
}
63+
64+
class smt_option_produce_modelst : public smt_optiont
65+
{
66+
public:
67+
explicit smt_option_produce_modelst(bool setting);
68+
bool setting() const;
69+
};
70+
71+
class smt_option_const_downcast_visitort
72+
{
73+
public:
74+
#define OPTION_ID(the_id) virtual void visit(const smt_option_##the_id##t &) = 0;
75+
#include "smt_options.def"
76+
#undef OPTION_ID
77+
};
78+
79+
#endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_OPTIONS_H

0 commit comments

Comments
 (0)