Changes needed to compile at Google, plus some bug fixes from Google.
[cvc5.git] / src / printer / printer.h
1 /********************* */
2 /*! \file printer.h
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: none
6 ** Minor contributors (to current version): Andrew Reynolds
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief Base of the pretty-printer interface
13 **
14 ** Base of the pretty-printer interface.
15 **/
16
17 #include "cvc4_private.h"
18
19 #ifndef __CVC4__PRINTER__PRINTER_H
20 #define __CVC4__PRINTER__PRINTER_H
21
22 #include <map>
23 #include <string>
24
25 #include "util/language.h"
26 #include "util/sexpr.h"
27 #include "util/model.h"
28 #include "expr/node.h"
29 #include "expr/command.h"
30
31 namespace CVC4 {
32
33 class Printer {
34 /** Printers for each OutputLanguage */
35 static Printer* d_printers[language::output::LANG_MAX];
36
37 /** Make a Printer for a given OutputLanguage */
38 static Printer* makePrinter(OutputLanguage lang) throw();
39
40 // disallow copy, assignment
41 Printer(const Printer&) CVC4_UNUSED;
42 Printer& operator=(const Printer&) CVC4_UNUSED;
43
44 protected:
45 // derived classes can construct, but no one else.
46 Printer() throw() {}
47 virtual ~Printer() {}
48
49 /** write model response to command */
50 virtual void toStream(std::ostream& out, const Model& m, const Command* c) const throw() = 0;
51
52 /** write model response to command using another language printer */
53 void toStreamUsing(OutputLanguage lang, std::ostream& out, const Model& m, const Command* c) const throw() {
54 getPrinter(lang)->toStream(out, m, c);
55 }
56
57 public:
58 /** Get the Printer for a given OutputLanguage */
59 static Printer* getPrinter(OutputLanguage lang) throw() {
60 if(lang == language::output::LANG_AUTO) {
61 // Infer the language to use for output.
62 //
63 // Options can be null in certain circumstances (e.g., when printing
64 // the singleton "null" expr. So we guard against segfault
65 if(&Options::current() != NULL) {
66 if(options::outputLanguage.wasSetByUser()) {
67 lang = options::outputLanguage();
68 }
69 if(lang == language::output::LANG_AUTO && options::inputLanguage.wasSetByUser()) {
70 lang = language::toOutputLanguage(options::inputLanguage());
71 }
72 }
73 if(lang == language::output::LANG_AUTO) {
74 lang = language::output::LANG_CVC4; // default
75 }
76 }
77 if(d_printers[lang] == NULL) {
78 d_printers[lang] = makePrinter(lang);
79 }
80 return d_printers[lang];
81 }
82
83 /** Write a Node out to a stream with this Printer. */
84 virtual void toStream(std::ostream& out, TNode n,
85 int toDepth, bool types, size_t dag) const throw() = 0;
86
87 /** Write a Command out to a stream with this Printer. */
88 virtual void toStream(std::ostream& out, const Command* c,
89 int toDepth, bool types, size_t dag) const throw() = 0;
90
91 /** Write a CommandStatus out to a stream with this Printer. */
92 virtual void toStream(std::ostream& out, const CommandStatus* s) const throw() = 0;
93
94 /** Write an SExpr out to a stream with this Printer. */
95 virtual void toStream(std::ostream& out, const SExpr& sexpr) const throw();
96
97 /**
98 * Write a Result out to a stream with this Printer.
99 *
100 * The default implementation writes a reasonable string in lowercase
101 * for sat, unsat, valid, invalid, or unknown results. This behavior
102 * is overridable by each Printer, since sometimes an output language
103 * has a particular preference for how results should appear.
104 */
105 virtual void toStream(std::ostream& out, const Result& r) const throw();
106
107 /** Write a Model out to a stream with this Printer. */
108 virtual void toStream(std::ostream& out, const Model& m) const throw();
109
110 /** Write an UnsatCore out to a stream with this Printer. */
111 virtual void toStream(std::ostream& out, const UnsatCore& core) const throw();
112
113 /** Write an UnsatCore out to a stream with this Printer. */
114 virtual void toStream(std::ostream& out, const UnsatCore& core, const std::map<Expr, std::string>& names) const throw();
115
116 };/* class Printer */
117
118 /**
119 * IOStream manipulator to pretty-print SExprs.
120 */
121 class PrettySExprs {
122 /**
123 * The allocated index in ios_base for our setting.
124 */
125 static const int s_iosIndex;
126
127 /**
128 * When this manipulator is used, the setting is stored here.
129 */
130 bool d_prettySExprs;
131
132 public:
133 /**
134 * Construct a PrettySExprs with the given setting.
135 */
136 PrettySExprs(bool prettySExprs) : d_prettySExprs(prettySExprs) {}
137
138 inline void applyPrettySExprs(std::ostream& out) {
139 out.iword(s_iosIndex) = d_prettySExprs;
140 }
141
142 static inline bool getPrettySExprs(std::ostream& out) {
143 return out.iword(s_iosIndex);
144 }
145
146 static inline void setPrettySExprs(std::ostream& out, bool prettySExprs) {
147 out.iword(s_iosIndex) = prettySExprs;
148 }
149
150 /**
151 * Set the pretty-sexprs state on the output stream for the current
152 * stack scope. This makes sure the old state is reset on the
153 * stream after normal OR exceptional exit from the scope, using the
154 * RAII C++ idiom.
155 */
156 class Scope {
157 std::ostream& d_out;
158 bool d_oldPrettySExprs;
159
160 public:
161
162 inline Scope(std::ostream& out, bool prettySExprs) :
163 d_out(out),
164 d_oldPrettySExprs(PrettySExprs::getPrettySExprs(out)) {
165 PrettySExprs::setPrettySExprs(out, prettySExprs);
166 }
167
168 inline ~Scope() {
169 PrettySExprs::setPrettySExprs(d_out, d_oldPrettySExprs);
170 }
171
172 };/* class PrettySExprs::Scope */
173
174 };/* class PrettySExprs */
175
176 /**
177 * Sets the default pretty-sexprs setting for an ostream. Use like this:
178 *
179 * // let out be an ostream, s an SExpr
180 * out << PrettySExprs(true) << s << endl;
181 *
182 * The setting stays permanently (until set again) with the stream.
183 */
184 inline std::ostream& operator<<(std::ostream& out, PrettySExprs ps) {
185 ps.applyPrettySExprs(out);
186 return out;
187 }
188
189 }/* CVC4 namespace */
190
191 #endif /* __CVC4__PRINTER__PRINTER_H */
192