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