Java bindings improvements for CASCADE, minor cleanup.
[cvc5.git] / src / cvc4.i
1 %import "bindings/swig.h"
2
3 %include "stdint.i"
4 %include "stl.i"
5
6 %module CVC4
7 // nspace completely broken with Java packaging
8 //%nspace;
9
10 namespace std {
11 class istream;
12 class ostream;
13 template <class T> class set {};
14 template <class K, class V, class H> class hash_map {};
15 }
16
17 %{
18 // Perl's headers define "seed" to Perl_seed, which breaks
19 // gmpxx.h; undo the damage for our CVC4 module.
20 #ifdef SWIGPERL
21 # undef seed
22 #endif /* SWIGPERL */
23
24 // OCaml's headers define "invalid_argument" and "flush" to
25 // caml_invalid_argument and caml_flush, which breaks C++
26 // standard headers; undo this damage
27 //
28 // Unfortunately, this code isn't inserted early enough. swig puts
29 // an include <stdexcept> very early, which breaks linking due to a
30 // nonexistent std::caml_invalid_argument symbol.. ridiculous!
31 //
32 #ifdef SWIGOCAML
33 # if defined(flush) || defined(invalid_argument)
34 # error "flush" or "invalid_argument" (or both) is defined by the ocaml headers. You must #undef it above before inclusion of <stdexcept>.
35 # endif /* flush */
36 # undef flush
37 # undef invalid_argument
38 #endif /* SWIGOCAML */
39
40 namespace CVC4 {}
41 using namespace CVC4;
42
43 #include <iostream>
44 #include <vector>
45 #include <set>
46 #include <string>
47 #include <ext/hash_map>
48 #include <typeinfo>
49 #include <cassert>
50
51 #include "util/sexpr.h"
52 #include "util/exception.h"
53 #include "expr/type.h"
54 #include "expr/expr.h"
55 #include "util/datatype.h"
56 #include "expr/command.h"
57
58 #ifdef SWIGJAVA
59 #include "bindings/java_stream_adapters.h"
60 std::set<JavaInputStreamAdapter*> CVC4::JavaInputStreamAdapter::s_adapters;
61 #endif
62 %}
63
64 %template(vectorCommandPtr) std::vector< CVC4::Command* >;
65 %template(vectorType) std::vector< CVC4::Type >;
66 %template(vectorExpr) std::vector< CVC4::Expr >;
67 %template(vectorVectorExpr) std::vector< std::vector< CVC4::Expr > >;
68 %template(vectorDatatypeType) std::vector< CVC4::DatatypeType >;
69 %template(vectorSExpr) std::vector< CVC4::SExpr >;
70 %template(vectorString) std::vector< std::string >;
71 %template(vectorPairStringType) std::vector< std::pair< std::string, CVC4::Type > >;
72 %template(pairStringType) std::pair< std::string, CVC4::Type >;
73 %template(setType) std::set< CVC4::Type >;
74 %template(hashmapExpr) std::hash_map< CVC4::Expr, CVC4::Expr, CVC4::ExprHashFunction >;
75
76 // This is unfortunate, but seems to be necessary; if we leave NULL
77 // defined, swig will expand it to "(void*) 0", and some of swig's
78 // helper functions won't compile properly.
79 #undef NULL
80
81 #ifdef SWIGJAVA
82
83 #include "bindings/java_iterator_adapter.h"
84 #include "bindings/java_stream_adapters.h"
85
86 %exception %{
87 try {
88 $action
89 } catch(CVC4::Exception& e) {
90 std::stringstream ss;
91 ss << e.what() << ": " << e.getMessage();
92 std::string explanation = ss.str();
93 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
94 }
95 %}
96
97 // Create a mapping from C++ Exceptions to Java Exceptions.
98 // This is in a couple of throws typemaps, simply because it's sensitive to SWIG's concept of which namespace we're in.
99 %typemap(throws) Exception %{
100 std::string name = "edu/nyu/acsys/CVC4/$1_type";
101 /*
102 size_t i = name.find("::");
103 if(i != std::string::npos) {
104 size_t j = name.rfind("::");
105 assert(i <= j);
106 name.replace(i, j - i + 2, "/");
107 }
108 */
109 jclass clazz = jenv->FindClass(name.c_str());
110 assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
111 jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
112 assert(method != NULL && jenv->ExceptionOccurred() == NULL);
113 jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
114 assert(t != NULL && jenv->ExceptionOccurred() == NULL);
115 int status = jenv->Throw(t);
116 assert(status == 0);
117 %}
118 %typemap(throws) CVC4::Exception %{
119 std::string name = "edu/nyu/acsys/$1_type";
120 size_t i = name.find("::");
121 if(i != std::string::npos) {
122 size_t j = name.rfind("::");
123 assert(i <= j);
124 name.replace(i, j - i + 2, "/");
125 }
126 jclass clazz = jenv->FindClass(name.c_str());
127 assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
128 jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
129 assert(method != NULL && jenv->ExceptionOccurred() == NULL);
130 jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
131 assert(t != NULL && jenv->ExceptionOccurred() == NULL);
132 int status = jenv->Throw(t);
133 assert(status == 0);
134 %}
135
136 %typemap(throws) ModalException = Exception;
137 %typemap(throws) LogicException = Exception;
138 %typemap(throws) OptionException = Exception;
139 %typemap(throws) IllegalArgumentException = Exception;
140 %typemap(throws) AssertionException = Exception;
141
142 %typemap(throws) CVC4::TypeCheckingException = CVC4::Exception;
143 %typemap(throws) CVC4::ScopeException = CVC4::Exception;
144 %typemap(throws) CVC4::IllegalArgumentException = CVC4::Exception;
145 %typemap(throws) CVC4::AssertionException = CVC4::Exception;
146 %typemap(throws) CVC4::parser::InputStreamException = CVC4::Exception;
147 %typemap(throws) CVC4::parser::ParserException = CVC4::Exception;
148
149 // Generate an error if the mapping from C++ CVC4 Exception to Java CVC4 Exception doesn't exist above
150 %typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY] %{
151 #error "exception $1_type doesn't map to Java correctly---please edit src/cvc4.i and add it"
152 %}
153
154 %include "java/typemaps.i" // primitive pointers and references
155 %include "java/std_string.i" // map std::string to java.lang.String
156 %include "java/arrays_java.i" // C arrays to Java arrays
157 %include "java/various.i" // map char** to java.lang.String[]
158
159 // Functions on the C++ side taking std::ostream& should on the Java side
160 // take a java.io.OutputStream. A JavaOutputStreamAdapter is created in
161 // the wrapper which creates and passes on a std::stringstream to the C++
162 // function. Then on exit, the string from the stringstream is dumped to
163 // the Java-side OutputStream.
164 %typemap(jni) std::ostream& "jlong"
165 %typemap(jtype) std::ostream& "long"
166 %typemap(jstype) std::ostream& "java.io.OutputStream"
167 %typemap(javain,
168 pre=" edu.nyu.acsys.CVC4.JavaOutputStreamAdapter temp$javainput = new edu.nyu.acsys.CVC4.JavaOutputStreamAdapter();", pgcppname="temp$javainput",
169 post=" new java.io.PrintStream($javainput).print(temp$javainput.toString());")
170 std::ostream& "edu.nyu.acsys.CVC4.JavaOutputStreamAdapter.getCPtr(temp$javainput)"
171
172 %typemap(jni) std::istream& "jlong"
173 %typemap(jtype) std::istream& "long"
174 %typemap(jstype) std::istream& "java.io.InputStream"
175 %typemap(javain,
176 pre=" edu.nyu.acsys.CVC4.JavaInputStreamAdapter temp$javainput = edu.nyu.acsys.CVC4.JavaInputStreamAdapter.get($javainput);", pgcppname="temp$javainput",
177 post="")
178 std::istream& "edu.nyu.acsys.CVC4.JavaInputStreamAdapter.getCPtr(temp$javainput)"
179 %typemap(in) jobject inputStream %{
180 $1 = jenv->NewGlobalRef($input);
181 %}
182 %typemap(out) CVC4::JavaInputStreamAdapter* %{
183 $1->pull(jenv);
184 *(CVC4::JavaInputStreamAdapter **)&$result = $1;
185 %}
186 %typemap(javacode) CVC4::JavaInputStreamAdapter %{
187 private static java.util.HashMap<java.io.InputStream, JavaInputStreamAdapter> streams =
188 new java.util.HashMap<java.io.InputStream, JavaInputStreamAdapter>();
189 public static JavaInputStreamAdapter get(java.io.InputStream is) {
190 if(streams.containsKey(is)) {
191 return (JavaInputStreamAdapter) streams.get(is);
192 }
193 JavaInputStreamAdapter adapter = new JavaInputStreamAdapter(is);
194 streams.put(is, adapter);
195 return adapter;
196 }
197 %}
198 %typemap(javafinalize) CVC4::JavaInputStreamAdapter %{
199 protected void finalize() {
200 streams.remove(getInputStream());
201 delete();
202 }
203 %}
204 %ignore CVC4::JavaInputStreamAdapter::init(JNIEnv*);
205 %ignore CVC4::JavaInputStreamAdapter::pullAdapters(JNIEnv*);
206 %ignore CVC4::JavaInputStreamAdapter::pull(JNIEnv*);
207 %javamethodmodifiers CVC4::JavaInputStreamAdapter::getInputStream() const "private";
208 %javamethodmodifiers CVC4::JavaInputStreamAdapter::JavaInputStreamAdapter(jobject) "private";
209
210 %exception CVC4::parser::Parser::nextCommand() %{
211 try {
212 CVC4::JavaInputStreamAdapter::pullAdapters(jenv);
213 $action
214 } catch(CVC4::Exception& e) {
215 std::stringstream ss;
216 ss << e.what() << ": " << e.getMessage();
217 std::string explanation = ss.str();
218 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
219 }
220 %}
221 %exception CVC4::parser::Parser::nextExpression() %{
222 try {
223 CVC4::JavaInputStreamAdapter::pullAdapters(jenv);
224 $action
225 } catch(CVC4::Exception& e) {
226 std::stringstream ss;
227 ss << e.what() << ": " << e.getMessage();
228 std::string explanation = ss.str();
229 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
230 }
231 %}
232 %exception CVC4::JavaInputStreamAdapter::~JavaInputStreamAdapter() %{
233 try {
234 jenv->DeleteGlobalRef(arg1->getInputStream());
235 $action
236 } catch(CVC4::Exception& e) {
237 std::stringstream ss;
238 ss << e.what() << ": " << e.getMessage();
239 std::string explanation = ss.str();
240 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
241 }
242 %}
243
244 #endif /* SWIGJAVA */
245
246 %include "util/integer.i"
247 %include "util/rational.i"
248 %include "util/exception.i"
249 %include "util/language.i"
250 %include "options/options.i"
251 %include "util/cardinality.i"
252 %include "util/bool.i"
253 %include "util/sexpr.i"
254 %include "util/statistics.i"
255 %include "util/result.i"
256 %include "util/configuration.i"
257 %include "util/bitvector.i"
258 %include "util/subrange_bound.i"
259 %include "util/array.i"
260 %include "util/array_store_all.i"
261 %include "util/predicate.i"
262 %include "util/hash.i"
263
264 %include "expr/type.i"
265 %include "util/ascription_type.i"
266 %include "util/datatype.i"
267 %include "util/tuple.i"
268 %include "util/record.i"
269 %include "util/regexp.i"
270 %include "util/uninterpreted_constant.i"
271
272 %include "expr/kind.i"
273 %include "expr/expr.i"
274 %include "expr/command.i"
275 %include "expr/symbol_table.i"
276 %include "expr/expr_manager.i"
277 %include "expr/expr_stream.i"
278 %include "expr/variable_type_map.i"
279
280 %include "theory/logic_info.i"
281
282 %include "smt/smt_engine.i"
283 %include "smt/modal_exception.i"
284 %include "smt/logic_exception.i"
285
286 %include "options/options.i"
287 %include "options/option_exception.i"
288
289 %include "parser/cvc4parser.i"