Merge tag 'smtcomp2014-resubmission'
[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 #ifdef SWIGPYTHON
65 %pythonappend CVC4::SmtEngine::SmtEngine %{
66 self.thisown = 0
67 %}
68 %pythonappend CVC4::ExprManager::ExprManager %{
69 self.thisown = 0
70 %}
71 #endif /* SWIGPYTHON */
72
73 %template(vectorCommandPtr) std::vector< CVC4::Command* >;
74 %template(vectorType) std::vector< CVC4::Type >;
75 %template(vectorExpr) std::vector< CVC4::Expr >;
76 %template(vectorUnsignedInt) std::vector< unsigned int >;
77 %template(vectorVectorExpr) std::vector< std::vector< CVC4::Expr > >;
78 %template(vectorDatatypeType) std::vector< CVC4::DatatypeType >;
79 %template(vectorSExpr) std::vector< CVC4::SExpr >;
80 %template(vectorString) std::vector< std::string >;
81 %template(vectorPairStringType) std::vector< std::pair< std::string, CVC4::Type > >;
82 %template(pairStringType) std::pair< std::string, CVC4::Type >;
83 %template(setOfType) std::set< CVC4::Type >;
84 %template(hashmapExpr) std::hash_map< CVC4::Expr, CVC4::Expr, CVC4::ExprHashFunction >;
85
86 // This is unfortunate, but seems to be necessary; if we leave NULL
87 // defined, swig will expand it to "(void*) 0", and some of swig's
88 // helper functions won't compile properly.
89 #undef NULL
90
91 #ifdef SWIGJAVA
92
93 #include "bindings/java_iterator_adapter.h"
94 #include "bindings/java_stream_adapters.h"
95
96 %exception %{
97 try {
98 $action
99 } catch(CVC4::Exception& e) {
100 std::stringstream ss;
101 ss << e.what() << ": " << e.getMessage();
102 std::string explanation = ss.str();
103 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
104 }
105 %}
106
107 // Create a mapping from C++ Exceptions to Java Exceptions.
108 // This is in a couple of throws typemaps, simply because it's sensitive to SWIG's concept of which namespace we're in.
109 %typemap(throws) Exception %{
110 std::string name = "edu/nyu/acsys/CVC4/$1_type";
111 /*
112 size_t i = name.find("::");
113 if(i != std::string::npos) {
114 size_t j = name.rfind("::");
115 assert(i <= j);
116 name.replace(i, j - i + 2, "/");
117 }
118 */
119 jclass clazz = jenv->FindClass(name.c_str());
120 assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
121 jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
122 assert(method != NULL && jenv->ExceptionOccurred() == NULL);
123 jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
124 assert(t != NULL && jenv->ExceptionOccurred() == NULL);
125 int status = jenv->Throw(t);
126 assert(status == 0);
127 %}
128 %typemap(throws) CVC4::Exception %{
129 std::string name = "edu/nyu/acsys/$1_type";
130 size_t i = name.find("::");
131 if(i != std::string::npos) {
132 size_t j = name.rfind("::");
133 assert(i <= j);
134 name.replace(i, j - i + 2, "/");
135 }
136 jclass clazz = jenv->FindClass(name.c_str());
137 assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
138 jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
139 assert(method != NULL && jenv->ExceptionOccurred() == NULL);
140 jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
141 assert(t != NULL && jenv->ExceptionOccurred() == NULL);
142 int status = jenv->Throw(t);
143 assert(status == 0);
144 %}
145
146 %typemap(throws) ModalException = Exception;
147 %typemap(throws) LogicException = Exception;
148 %typemap(throws) OptionException = Exception;
149 %typemap(throws) IllegalArgumentException = Exception;
150 %typemap(throws) AssertionException = Exception;
151
152 %typemap(throws) CVC4::TypeCheckingException = CVC4::Exception;
153 %typemap(throws) CVC4::ScopeException = CVC4::Exception;
154 %typemap(throws) CVC4::IllegalArgumentException = CVC4::Exception;
155 %typemap(throws) CVC4::AssertionException = CVC4::Exception;
156 %typemap(throws) CVC4::parser::InputStreamException = CVC4::Exception;
157 %typemap(throws) CVC4::parser::ParserException = CVC4::Exception;
158
159 %typemap(throws) CVC4::RationalFromDoubleException = Exception;
160
161 // Generate an error if the mapping from C++ CVC4 Exception to Java CVC4 Exception doesn't exist above
162 %typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY] %{
163 #error "exception $1_type doesn't map to Java correctly---please edit src/cvc4.i and add it"
164 %}
165
166 %include "java/typemaps.i" // primitive pointers and references
167 %include "java/std_string.i" // map std::string to java.lang.String
168 %include "java/arrays_java.i" // C arrays to Java arrays
169 %include "java/various.i" // map char** to java.lang.String[]
170
171 // Functions on the C++ side taking std::ostream& should on the Java side
172 // take a java.io.OutputStream. A JavaOutputStreamAdapter is created in
173 // the wrapper which creates and passes on a std::stringstream to the C++
174 // function. Then on exit, the string from the stringstream is dumped to
175 // the Java-side OutputStream.
176 %typemap(jni) std::ostream& "jlong"
177 %typemap(jtype) std::ostream& "long"
178 %typemap(jstype) std::ostream& "java.io.OutputStream"
179 %typemap(javain,
180 pre=" edu.nyu.acsys.CVC4.JavaOutputStreamAdapter temp$javainput = new edu.nyu.acsys.CVC4.JavaOutputStreamAdapter();", pgcppname="temp$javainput",
181 post=" new java.io.PrintStream($javainput).print(temp$javainput.toString());")
182 std::ostream& "edu.nyu.acsys.CVC4.JavaOutputStreamAdapter.getCPtr(temp$javainput)"
183
184 %typemap(jni) std::istream& "jlong"
185 %typemap(jtype) std::istream& "long"
186 %typemap(jstype) std::istream& "java.io.InputStream"
187 %typemap(javain,
188 pre=" edu.nyu.acsys.CVC4.JavaInputStreamAdapter temp$javainput = edu.nyu.acsys.CVC4.JavaInputStreamAdapter.get($javainput);", pgcppname="temp$javainput",
189 post="")
190 std::istream& "edu.nyu.acsys.CVC4.JavaInputStreamAdapter.getCPtr(temp$javainput)"
191 %typemap(in) jobject inputStream %{
192 $1 = jenv->NewGlobalRef($input);
193 %}
194 %typemap(out) CVC4::JavaInputStreamAdapter* %{
195 $1->pull(jenv);
196 *(CVC4::JavaInputStreamAdapter **)&$result = $1;
197 %}
198 %typemap(javacode) CVC4::JavaInputStreamAdapter %{
199 private static java.util.HashMap<java.io.InputStream, JavaInputStreamAdapter> streams =
200 new java.util.HashMap<java.io.InputStream, JavaInputStreamAdapter>();
201 public static JavaInputStreamAdapter get(java.io.InputStream is) {
202 if(streams.containsKey(is)) {
203 return (JavaInputStreamAdapter) streams.get(is);
204 }
205 JavaInputStreamAdapter adapter = new JavaInputStreamAdapter(is);
206 streams.put(is, adapter);
207 return adapter;
208 }
209 %}
210 %typemap(javafinalize) CVC4::JavaInputStreamAdapter %{
211 protected void finalize() {
212 streams.remove(getInputStream());
213 delete();
214 }
215 %}
216 %ignore CVC4::JavaInputStreamAdapter::init(JNIEnv*);
217 %ignore CVC4::JavaInputStreamAdapter::pullAdapters(JNIEnv*);
218 %ignore CVC4::JavaInputStreamAdapter::pull(JNIEnv*);
219 %javamethodmodifiers CVC4::JavaInputStreamAdapter::getInputStream() const "private";
220 %javamethodmodifiers CVC4::JavaInputStreamAdapter::JavaInputStreamAdapter(jobject) "private";
221
222 %exception CVC4::parser::Parser::nextCommand() %{
223 try {
224 CVC4::JavaInputStreamAdapter::pullAdapters(jenv);
225 $action
226 } catch(CVC4::Exception& e) {
227 std::stringstream ss;
228 ss << e.what() << ": " << e.getMessage();
229 std::string explanation = ss.str();
230 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
231 }
232 %}
233 %exception CVC4::parser::Parser::nextExpression() %{
234 try {
235 CVC4::JavaInputStreamAdapter::pullAdapters(jenv);
236 $action
237 } catch(CVC4::Exception& e) {
238 std::stringstream ss;
239 ss << e.what() << ": " << e.getMessage();
240 std::string explanation = ss.str();
241 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
242 }
243 %}
244 %exception CVC4::JavaInputStreamAdapter::~JavaInputStreamAdapter() %{
245 try {
246 jenv->DeleteGlobalRef(arg1->getInputStream());
247 $action
248 } catch(CVC4::Exception& e) {
249 std::stringstream ss;
250 ss << e.what() << ": " << e.getMessage();
251 std::string explanation = ss.str();
252 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
253 }
254 %}
255
256 /* Copied (and modified) from java.swg; the standard swig version causes
257 * negative BigInteger to be interpreted unsigned. Here we throw an
258 * exception. */
259 %typemap(in) unsigned long long {
260 jclass clazz;
261 jmethodID mid;
262 jbyteArray ba;
263 jbyte* bae;
264 jsize sz;
265 int i;
266
267 if (!$input) {
268 SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "BigInteger null");
269 return $null;
270 }
271 clazz = JCALL1(GetObjectClass, jenv, $input);
272 mid = JCALL3(GetMethodID, jenv, clazz, "toByteArray", "()[B");
273 ba = (jbyteArray)JCALL2(CallObjectMethod, jenv, $input, mid);
274 bae = JCALL2(GetByteArrayElements, jenv, ba, 0);
275 sz = JCALL1(GetArrayLength, jenv, ba);
276 if((bae[0] & 0x80) != 0) {
277 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "BigInteger argument must be nonnegative.");
278 }
279 jsize test_sz = sz;
280 if(sz > 1 && bae[0] == 0) {
281 --test_sz;
282 }
283 if(test_sz > sizeof(unsigned long long)) {
284 SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "BigInteger argument out of bounds.");
285 }
286 $1 = 0;
287 for(i=0; i<sz; i++) {
288 $1 = ($1 << 8) | ($1_type)(unsigned char)bae[i];
289 }
290 JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0);
291 }
292
293 #endif /* SWIGJAVA */
294
295 %include "util/exception.i"
296 %include "util/integer.i"
297 %include "util/rational.i"
298 %include "util/language.i"
299 %include "util/cardinality.i"
300 %include "util/bool.i"
301 %include "util/sexpr.i"
302 %include "options/options.i"
303 %include "util/statistics.i"
304 %include "util/result.i"
305 %include "util/configuration.i"
306 %include "util/bitvector.i"
307 %include "util/subrange_bound.i"
308 %include "util/array.i"
309 %include "util/array_store_all.i"
310 %include "util/predicate.i"
311 %include "util/hash.i"
312
313 %include "expr/type.i"
314 %include "util/ascription_type.i"
315 %include "util/emptyset.i"
316 %include "util/datatype.i"
317 %include "util/tuple.i"
318 %include "util/record.i"
319 %include "util/regexp.i"
320 %include "util/uninterpreted_constant.i"
321 %include "util/proof.i"
322
323 %include "expr/kind.i"
324 %include "expr/expr.i"
325 %include "expr/command.i"
326 %include "expr/symbol_table.i"
327 %include "expr/expr_manager.i"
328 %include "expr/expr_stream.i"
329 %include "expr/variable_type_map.i"
330
331 %include "theory/logic_info.i"
332
333 %include "smt/smt_engine.i"
334 %include "smt/modal_exception.i"
335 %include "smt/logic_exception.i"
336
337 %include "options/options.i"
338 %include "options/option_exception.i"
339
340 %include "parser/cvc4parser.i"