go.swg 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* ------------------------------------------------------------
  2. * go.swg
  3. *
  4. * Go configuration module.
  5. * ------------------------------------------------------------ */
  6. %include <gostring.swg>
  7. /* Code insertion directives */
  8. #define %go_import(...) %insert(go_imports) %{__VA_ARGS__%}
  9. /* Basic types */
  10. %typemap(gotype) bool, const bool & "bool"
  11. %typemap(gotype) char, const char & "byte"
  12. %typemap(gotype) signed char, const signed char & "int8"
  13. %typemap(gotype) unsigned char, const unsigned char & "byte"
  14. %typemap(gotype) short, const short & "int16"
  15. %typemap(gotype) unsigned short, const unsigned short & "uint16"
  16. %typemap(gotype) int, const int & "int"
  17. %typemap(gotype) unsigned int, const unsigned int & "uint"
  18. %typemap(gotype) long, const long & "int64"
  19. %typemap(gotype) unsigned long, const unsigned long & "uint64"
  20. %typemap(gotype) long long, const long long & "int64"
  21. %typemap(gotype) unsigned long long, const unsigned long long & "uint64"
  22. %typemap(gotype) float, const float & "float32"
  23. %typemap(gotype) double, const double & "float64"
  24. %typemap(in) bool,
  25. char,
  26. signed char,
  27. unsigned char,
  28. short,
  29. unsigned short,
  30. int,
  31. unsigned int,
  32. long,
  33. unsigned long,
  34. long long,
  35. unsigned long long,
  36. float,
  37. double
  38. %{ $1 = ($1_ltype)$input; %}
  39. %typemap(in) const bool &,
  40. const char &,
  41. const signed char &,
  42. const unsigned char &,
  43. const short &,
  44. const unsigned short &,
  45. const int &,
  46. const unsigned int &,
  47. const long long &,
  48. const unsigned long long &,
  49. const float &,
  50. const double &
  51. %{ $1 = ($1_ltype)&$input; %}
  52. %typemap(in) const long & ($*1_ltype temp),
  53. const unsigned long & ($*1_ltype temp)
  54. %{ temp = ($*1_ltype)$input;
  55. $1 = ($1_ltype)&temp; %}
  56. %typemap(out) bool,
  57. char,
  58. signed char,
  59. unsigned char,
  60. short,
  61. unsigned short,
  62. int,
  63. unsigned int,
  64. long,
  65. unsigned long,
  66. long long,
  67. unsigned long long,
  68. float,
  69. double
  70. %{ $result = $1; %}
  71. %typemap(goout) bool,
  72. char,
  73. signed char,
  74. unsigned char,
  75. short,
  76. unsigned short,
  77. int,
  78. unsigned int,
  79. long,
  80. unsigned long,
  81. long long,
  82. unsigned long long,
  83. float,
  84. double
  85. ""
  86. %typemap(out) const bool &,
  87. const char &,
  88. const signed char &,
  89. const unsigned char &,
  90. const short &,
  91. const unsigned short &,
  92. const int &,
  93. const unsigned int &,
  94. const long &,
  95. const unsigned long &,
  96. const long long &,
  97. const unsigned long long &,
  98. const float &,
  99. const double &
  100. %{ $result = ($*1_ltype)*$1; %}
  101. %typemap(goout) const bool &,
  102. const char &,
  103. const signed char &,
  104. const unsigned char &,
  105. const short &,
  106. const unsigned short &,
  107. const int &,
  108. const unsigned int &,
  109. const long &,
  110. const unsigned long &,
  111. const long long &,
  112. const unsigned long long &,
  113. const float &,
  114. const double &
  115. ""
  116. %typemap(out) void ""
  117. %typemap(goout) void ""
  118. %typemap(directorin) bool,
  119. char,
  120. signed char,
  121. unsigned char,
  122. short,
  123. unsigned short,
  124. int,
  125. unsigned int,
  126. long,
  127. unsigned long,
  128. long long,
  129. unsigned long long,
  130. float,
  131. double
  132. %{ $input = ($1_ltype)$1; %}
  133. %typemap(godirectorin) bool,
  134. char,
  135. signed char,
  136. unsigned char,
  137. short,
  138. unsigned short,
  139. int,
  140. unsigned int,
  141. long,
  142. unsigned long,
  143. long long,
  144. unsigned long long,
  145. float,
  146. double
  147. ""
  148. %typemap(directorin) const bool &,
  149. const char &,
  150. const signed char &,
  151. const unsigned char &,
  152. const short &,
  153. const unsigned short &,
  154. const int &,
  155. const unsigned int &,
  156. const long &,
  157. const unsigned long &,
  158. const long long &,
  159. const unsigned long long &,
  160. const float &,
  161. const double &
  162. %{ $input = ($*1_ltype)$1; %}
  163. %typemap(godirectorin) const bool &,
  164. const char &,
  165. const signed char &,
  166. const unsigned char &,
  167. const short &,
  168. const unsigned short &,
  169. const int &,
  170. const unsigned int &,
  171. const long &,
  172. const unsigned long &,
  173. const long long &,
  174. const unsigned long long &,
  175. const float &,
  176. const double &
  177. ""
  178. %typemap(directorout) bool,
  179. char,
  180. signed char,
  181. unsigned char,
  182. short,
  183. unsigned short,
  184. int,
  185. unsigned int,
  186. long,
  187. unsigned long,
  188. long long,
  189. unsigned long long,
  190. float,
  191. double
  192. %{ $result = ($1_ltype)$input; %}
  193. %typemap(directorout,warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) const bool &,
  194. const char &,
  195. const signed char &,
  196. const unsigned char &,
  197. const short &,
  198. const unsigned short &,
  199. const int &,
  200. const unsigned int &,
  201. const long &,
  202. const unsigned long &,
  203. const long long &,
  204. const unsigned long long &,
  205. const float &,
  206. const double &
  207. %{
  208. $result = new $*1_ltype($input);
  209. swig_acquire_pointer(&swig_mem, $result);
  210. %}
  211. /* The size_t type. */
  212. %typemap(gotype) size_t, const size_t & %{int64%}
  213. %typemap(in) size_t
  214. %{ $1 = (size_t)$input; %}
  215. %typemap(in) const size_t &
  216. %{ $1 = ($1_ltype)&$input; %}
  217. %typemap(out) size_t
  218. %{ $result = $1; %}
  219. %typemap(goout) size_t ""
  220. %typemap(out) const size_t &
  221. %{ $result = ($*1_ltype)*$1; %}
  222. %typemap(goout) const size_t & ""
  223. %typemap(directorin) size_t
  224. %{ $input = (size_t)$1; %}
  225. %typemap(godirectorin) size_t ""
  226. %typemap(directorin) const size_t &
  227. %{ $input = ($*1_ltype)$1; %}
  228. %typemap(godirectorin) const size_t & ""
  229. %typemap(directorout) size_t
  230. %{ $result = ($1_ltype)$input; %}
  231. %typemap(directorout,warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) const size_t &
  232. %{
  233. $result = new $*1_ltype($input);
  234. swig_acquire_pointer(&swig_mem, $result);
  235. %}
  236. /* Member pointers. */
  237. %typemap(gotype) SWIGTYPE (CLASS::*)
  238. %{$gotypename%}
  239. %typemap(in) SWIGTYPE (CLASS::*)
  240. %{ $1 = *($&1_ltype)$input; %}
  241. %typemap(out) SWIGTYPE (CLASS::*)
  242. %{
  243. struct swig_out_type { intgo size; void* val; } *swig_out;
  244. swig_out = (struct swig_out_type*)malloc(sizeof(*swig_out));
  245. if (swig_out) {
  246. swig_out->size = sizeof($1_ltype);
  247. swig_out->val = malloc(swig_out->size);
  248. if (swig_out->val) {
  249. *($&1_ltype)(swig_out->val) = $1;
  250. }
  251. }
  252. $result = swig_out;
  253. %}
  254. %typemap(goout) SWIGTYPE (CLASS::*)
  255. %{
  256. {
  257. type swig_out_type struct { size int; val uintptr }
  258. p := (*swig_out_type)(unsafe.Pointer($1))
  259. if p == nil || p.val == 0 {
  260. $result = nil
  261. } else {
  262. m := make([]byte, p.size)
  263. a := (*[1024]byte)(unsafe.Pointer(p.val))[:p.size]
  264. copy(m, a)
  265. Swig_free(p.val)
  266. Swig_free(uintptr(unsafe.Pointer(p)))
  267. $result = &m[0]
  268. }
  269. }
  270. %}
  271. %typemap(directorin) SWIGTYPE (CLASS::*)
  272. %{ $input = *($&1_ltype)$1; %}
  273. %typemap(godirectorin) SWIGTYPE (CLASS::*) ""
  274. %typemap(directorout) SWIGTYPE (CLASS::*)
  275. %{
  276. $result = new $1_ltype($input);
  277. swig_acquire_pointer(&swig_mem, $result);
  278. %}
  279. /* Pointers. */
  280. /* We can't translate pointers using a typemap, so that is handled in
  281. the C++ code. */
  282. %typemap(gotype) SWIGTYPE *
  283. %{$gotypename%}
  284. %typemap(in) SWIGTYPE *
  285. %{ $1 = *($&1_ltype)&$input; %}
  286. %typemap(out) SWIGTYPE *
  287. %{ *($&1_ltype)&$result = ($1_ltype)$1; %}
  288. %typemap(goout) SWIGTYPE * ""
  289. %typemap(directorin) SWIGTYPE *
  290. %{ *($&1_ltype)&$input = ($1_ltype)$1; %}
  291. %typemap(godirectorin) SWIGTYPE * ""
  292. %typemap(directorout) SWIGTYPE *
  293. %{ $result = *($&1_ltype)&$input; %}
  294. %apply SWIGTYPE * { SWIGTYPE *const }
  295. /* Pointer references. */
  296. %typemap(gotype) SWIGTYPE *const&
  297. %{$gotypename%}
  298. %typemap(in) SWIGTYPE *const& ($*1_ltype temp = 0)
  299. %{
  300. temp = *($1_ltype)&$input;
  301. $1 = ($1_ltype)&temp;
  302. %}
  303. %typemap(out) SWIGTYPE *const&
  304. %{ *($1_ltype)&$result = *$1; %}
  305. %typemap(goout) SWIGTYPE *const& ""
  306. /* References. */
  307. /* Converting a C++ reference to Go has to be handled in the C++
  308. code. */
  309. %typemap(gotype) SWIGTYPE &
  310. %{$gotypename%}
  311. %typemap(in) SWIGTYPE &
  312. %{ $1 = *($&1_ltype)&$input; %}
  313. %typemap(out) SWIGTYPE &
  314. %{ *($&1_ltype)&$result = $1; %}
  315. %typemap(goout) SWIGTYPE & ""
  316. %typemap(directorin) SWIGTYPE &
  317. %{ $input = ($1_ltype)&$1; %}
  318. %typemap(godirectorin) SWIGTYPE & ""
  319. %typemap(directorout) SWIGTYPE &
  320. %{ *($&1_ltype)&$result = $input; %}
  321. %typemap(gotype) SWIGTYPE &&
  322. %{$gotypename%}
  323. %typemap(in) SWIGTYPE &&
  324. %{ $1 = *($&1_ltype)&$input; %}
  325. %typemap(out) SWIGTYPE &&
  326. %{ *($&1_ltype)&$result = $1; %}
  327. %typemap(goout) SWIGTYPE && ""
  328. %typemap(directorin) SWIGTYPE &&
  329. %{ $input = ($1_ltype)&$1_name; %}
  330. %typemap(godirectorin) SWIGTYPE && ""
  331. %typemap(directorout) SWIGTYPE &&
  332. %{ *($&1_ltype)&$result = $input; %}
  333. /* C arrays turn into Go pointers. If we know the length we can use a
  334. slice. */
  335. %typemap(gotype) SWIGTYPE []
  336. %{$gotypename%}
  337. %typemap(in) SWIGTYPE []
  338. %{ $1 = *($&1_ltype)&$input; %}
  339. %typemap(out) SWIGTYPE []
  340. %{ *($&1_ltype)&$result = $1; %}
  341. %typemap(goout) SWIGTYPE [] ""
  342. %typemap(directorin) SWIGTYPE []
  343. %{ $input = *($1_ltype)&$1; %}
  344. %typemap(godirectorin) SWIGTYPE [] ""
  345. %typemap(directorout) SWIGTYPE []
  346. %{ *($&1_ltype)&$result = $input; %}
  347. /* Strings. */
  348. %typemap(gotype)
  349. char *, char *&, char[ANY], char[] "string"
  350. /* Needed to avoid confusion with the way the go module handles
  351. references. */
  352. %typemap(gotype) char&, unsigned char& "*byte"
  353. %typemap(gotype) signed char& "*int8"
  354. %typemap(in)
  355. char *, char[ANY], char[]
  356. %{ $1 = ($1_ltype)$input.p; %}
  357. %typemap(in) char *&
  358. %{ $1 = ($1_ltype)$input.p; %}
  359. %typemap(out,fragment="AllocateString")
  360. char *, char *&, char[ANY], char[]
  361. %{ $result = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0); %}
  362. %typemap(goout,fragment="CopyString")
  363. char *, char *&, char[ANY], char[]
  364. %{ $result = swigCopyString($1) %}
  365. %typemap(directorin,fragment="AllocateString")
  366. char *, char *&, char[ANY], char[]
  367. %{
  368. $input = Swig_AllocateString((char*)$1, $1 ? strlen((char*)$1) : 0);
  369. %}
  370. %typemap(godirectorin,fragment="CopyString")
  371. char *, char *&, char[ANY], char[]
  372. %{
  373. $result = swigCopyString($input)
  374. %}
  375. %typemap(directorout)
  376. char *, char *&, char[ANY], char[]
  377. %{ $result = ($1_ltype)$input.p; %}
  378. /* String & length */
  379. %typemap(gotype) (char *STRING, size_t LENGTH) "string"
  380. %typemap(in) (char *STRING, size_t LENGTH)
  381. %{
  382. $1 = ($1_ltype)$input.p;
  383. $2 = ($2_ltype)$input.n;
  384. %}
  385. %typemap(out,fragment="AllocateString") (char *STRING, size_t LENGTH)
  386. %{ $result = Swig_AllocateString((char*)$1, (size_t)$2); %}
  387. %typemap(goout,fragment="CopyString") (char *STRING, size_t LENGTH)
  388. %{ $result = swigCopyString($1) %}
  389. %typemap(directorin,fragment="AllocateString") (char *STRING, size_t LENGTH)
  390. %{ $input = Swig_AllocateString((char*)$1, $2); %}
  391. %typemap(godirectorin,fragment="CopyString") (char *STRING, size_t LENGTH)
  392. %{ $result = swigCopyString($input) %}
  393. %typemap(directorout) (char *STRING, size_t LENGTH)
  394. %{
  395. $1 = ($1_ltype)$input.p;
  396. $2 = ($2_ltype)$input.n;
  397. %}
  398. /* Enums. We can't do the right thing for enums in typemap(gotype) so
  399. we deliberately don't define them. The right thing would be to
  400. capitalize the name. This is instead done in go.cxx. */
  401. %typemap(gotype) enum SWIGTYPE
  402. %{$gotypename%}
  403. %typemap(in) enum SWIGTYPE
  404. %{ $1 = ($1_ltype)$input; %}
  405. %typemap(out) enum SWIGTYPE
  406. %{ $result = (intgo)$1; %}
  407. %typemap(goout) enum SWIGTYPE ""
  408. %typemap(directorin) enum SWIGTYPE
  409. %{ $input = (intgo)$1; %}
  410. %typemap(godirectorin) enum SWIGTYPE ""
  411. %typemap(directorout) enum SWIGTYPE
  412. %{ $result = ($1_ltype)$input; %}
  413. %typemap(directorin) enum SWIGTYPE & (intgo e)
  414. %{
  415. e = (intgo)$1;
  416. $input = ($1_ltype)&e;
  417. %}
  418. %typemap(godirectorin) enum SWIGTYPE & ""
  419. %typemap(directorout) enum SWIGTYPE &
  420. %{
  421. $*1_ltype f = ($*1_ltype)*$input;
  422. $result = ($1_ltype)&f;
  423. %}
  424. /* Arbitrary type. This is a type passed by value in the C/C++ code.
  425. We convert it to a pointer for the Go code. Note that all basic
  426. types are explicitly handled above. */
  427. %typemap(gotype) SWIGTYPE
  428. %{$gotypename%}
  429. %typemap(in) SWIGTYPE ($&1_type argp)
  430. %{
  431. argp = ($&1_ltype)$input;
  432. if (argp == NULL) {
  433. _swig_gopanic("Attempt to dereference null $1_type");
  434. }
  435. $1 = ($1_ltype)*argp;
  436. %}
  437. %typemap(out) SWIGTYPE
  438. #ifdef __cplusplus
  439. %{ *($&1_ltype*)&$result = new $1_ltype($1); %}
  440. #else
  441. {
  442. $&1_ltype $1ptr = ($&1_ltype)malloc(sizeof($1_ltype));
  443. memmove($1ptr, &$1, sizeof($1_type));
  444. *($&1_ltype*)&$result = $1ptr;
  445. }
  446. #endif
  447. %typemap(goout) SWIGTYPE ""
  448. %typemap(directorin) SWIGTYPE
  449. %{ $input = ($&1_ltype)&$1; %}
  450. %typemap(godirectorin) SWIGTYPE ""
  451. %typemap(directorout) SWIGTYPE
  452. %{ $result = *($&1_ltype)$input; %}
  453. /* Exception handling */
  454. %typemap(throws) char *
  455. %{ _swig_gopanic($1); %}
  456. %typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY]
  457. %{
  458. (void)$1;
  459. _swig_gopanic("C++ $1_type exception thrown");
  460. %}
  461. /* Typecheck typemaps. The purpose of these is merely to issue a
  462. warning for overloaded C++ functions that cannot be overloaded in
  463. Go as more than one C++ type maps to a single Go type. */
  464. %typecheck(SWIG_TYPECHECK_BOOL) /* Go bool */
  465. bool,
  466. const bool &
  467. ""
  468. %typecheck(SWIG_TYPECHECK_CHAR) /* Go byte */
  469. char,
  470. const char &,
  471. unsigned char,
  472. const unsigned char &
  473. ""
  474. %typecheck(SWIG_TYPECHECK_INT8) /* Go int8 */
  475. signed char,
  476. const signed char &
  477. ""
  478. %typecheck(SWIG_TYPECHECK_INT16) /* Go int16 */
  479. short,
  480. const short &
  481. ""
  482. %typecheck(SWIG_TYPECHECK_INT16) /* Go uint16 */
  483. unsigned short,
  484. const unsigned short &
  485. ""
  486. %typecheck(SWIG_TYPECHECK_INT32) /* Go int */
  487. int,
  488. const int &
  489. ""
  490. %typecheck(SWIG_TYPECHECK_INT32) /* Go uint */
  491. unsigned int,
  492. const unsigned int &
  493. ""
  494. %typecheck(SWIG_TYPECHECK_INT64) /* Go int64 */
  495. long,
  496. const long &,
  497. long long,
  498. const long long &
  499. ""
  500. %typecheck(SWIG_TYPECHECK_INT64) /* Go uint64 */
  501. unsigned long,
  502. const unsigned long &,
  503. unsigned long long,
  504. const unsigned long long &
  505. ""
  506. %typecheck(SWIG_TYPECHECK_FLOAT) /* Go float32 */
  507. float,
  508. const float &
  509. ""
  510. %typecheck(SWIG_TYPECHECK_DOUBLE) /* Go float64 */
  511. double,
  512. const double &
  513. ""
  514. %typecheck(SWIG_TYPECHECK_STRING) /* Go string */
  515. char *,
  516. char *&,
  517. char[ANY],
  518. char [],
  519. signed char *,
  520. signed char *&,
  521. signed char[ANY],
  522. signed char [],
  523. unsigned char *,
  524. unsigned char *&,
  525. unsigned char[ANY],
  526. unsigned char []
  527. ""
  528. %typecheck(SWIG_TYPECHECK_POINTER)
  529. SWIGTYPE,
  530. SWIGTYPE *,
  531. SWIGTYPE &,
  532. SWIGTYPE &&,
  533. SWIGTYPE *const&,
  534. SWIGTYPE [],
  535. SWIGTYPE (CLASS::*)
  536. ""
  537. /* Go keywords. */
  538. %include <gokw.swg>
  539. %include <goruntime.swg>