-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.cxx
6916 lines (5864 loc) · 198 KB
/
go.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html.
*
* go.cxx
*
* Go language module for SWIG.
* ----------------------------------------------------------------------------- */
#include "swigmod.h"
#include "cparse.h"
#include <ctype.h>
/* ----------------------------------------------------------------------
* siphash()
*
* 64-bit SipHash-2-4 to generate unique id for each module
* ---------------------------------------------------------------------- */
// An unsigned 64-bit integer that works on a 32-bit host.
typedef struct {
// Assume unsigned long is at least 32 bits.
unsigned long hi;
unsigned long lo;
} swig_uint64;
// Rotate v left by bits, which must be <= 32.
static inline void _rotl(swig_uint64 *v, int bits) {
assert(bits <= 32);
unsigned long tmp = v->hi;
if (bits == 32) {
v->hi = v->lo;
v->lo = tmp;
} else {
v->hi = (tmp << bits) | ((0xfffffffful & v->lo) >> (32 - bits));
v->lo = (v->lo << bits) | ((0xfffffffful & tmp) >> (32 - bits));
}
}
// dst ^= src
static inline void _xor(swig_uint64 *dst, swig_uint64 *src) {
dst->lo ^= src->lo;
dst->hi ^= src->hi;
}
// dst += src
static inline void _add(swig_uint64 *dst, swig_uint64 *src) {
dst->lo += src->lo;
dst->hi += src->hi + ((dst->lo & 0xfffffffful) < (src->lo&0xfffffffful) ? 1 : 0);
}
#define SIPROUND \
do { \
_add(&v0, &v1); _rotl(&v1, 13); _xor(&v1, &v0); _rotl(&v0, 32); \
_add(&v2, &v3); _rotl(&v3, 16); _xor(&v3, &v2); \
_add(&v0, &v3); _rotl(&v3, 21); _xor(&v3, &v0); \
_add(&v2, &v1); _rotl(&v1, 17); _xor(&v1, &v2); _rotl(&v2, 32); \
} while(0)
// Set out to the hash of inc/inlen.
static void siphash(swig_uint64 *out, const char *inc, unsigned long inlen) {
/* "somepseudorandomlygeneratedbytes" */
swig_uint64 v0 = {0x736f6d65UL, 0x70736575UL};
swig_uint64 v1 = {0x646f7261UL, 0x6e646f6dUL};
swig_uint64 v2 = {0x6c796765UL, 0x6e657261UL};
swig_uint64 v3 = {0x74656462UL, 0x79746573UL};
swig_uint64 b;
/* hard-coded k. */
swig_uint64 k0 = {0x07060504UL, 0x03020100UL};
swig_uint64 k1 = {0x0F0E0D0CUL, 0x0B0A0908UL};
int i;
const int cROUNDS = 2, dROUNDS = 4;
const unsigned char *in = (const unsigned char *)inc;
const unsigned char *end = in + inlen - (inlen % 8);
int left = inlen & 7;
_xor(&v3, &k1); _xor(&v2, &k0); _xor(&v1, &k1); _xor(&v0, &k0);
for (; in != end; in += 8) {
b.hi = 0; b.lo = 0;
for (i = 0; i < 4; i++) {
b.lo |= ((unsigned long)in[i]) << (8*i);
}
for (i = 0; i < 4; i++) {
b.hi |= ((unsigned long)in[i+4]) << (8*i);
}
_xor(&v3, &b);
for (i = 0; i < cROUNDS; i++) {
SIPROUND;
}
_xor(&v0, &b);
}
b.hi = (inlen & 0xff)<<24; b.lo = 0;
for (; left; left--) {
if (left > 4) {
b.hi |= ((unsigned long)in[left-1]) << (8*left-8-32);
} else {
b.lo |= ((unsigned long)in[left-1]) << (8*left-8);
}
}
_xor(&v3, &b);
for(i=0; i<cROUNDS; i++) {
SIPROUND;
}
_xor(&v0, &b); v2.lo ^= 0xff;
for(i=0; i<dROUNDS; i++) {
SIPROUND;
}
out->lo = 0; out->hi = 0;
_xor(out, &v0); _xor(out, &v1); _xor(out, &v2); _xor(out, &v3);
}
#undef SIPROUND
class GO:public Language {
static const char *const usage;
// Go package name.
String *package;
// SWIG module name.
String *module;
// Flag for generating cgo input files.
bool cgo_flag;
// Flag for generating gccgo output.
bool gccgo_flag;
// Prefix to use with gccgo.
String *go_prefix;
// -fgo-prefix option.
String *prefix_option;
// -fgo-pkgpath option.
String *pkgpath_option;
// Whether to use a shared library.
bool use_shlib;
// Name of shared library to import.
String *soname;
// Size in bits of the Go type "int". 0 if not specified.
int intgo_type_size;
/* Output files */
File *f_c_begin;
File *f_go_begin;
File *f_gc_begin;
/* Output fragments */
File *f_c_runtime;
File *f_c_header;
File *f_c_wrappers;
File *f_c_init;
File *f_c_directors;
File *f_c_directors_h;
File *f_go_imports;
File *f_go_runtime;
File *f_go_header;
File *f_go_wrappers;
File *f_go_directors;
File *f_gc_runtime;
File *f_gc_header;
File *f_gc_wrappers;
File *f_cgo_comment;
File *f_cgo_comment_typedefs;
// True if we imported a module.
bool saw_import;
// If not NULL, name of import package being processed.
String *imported_package;
// Build interface methods while handling a class. This is only
// non-NULL when we are handling methods.
String *interfaces;
// The class node while handling a class. This is only non-NULL
// when we are handling methods.
Node *class_node;
// The class name while handling a class. This is only non-NULL
// when we are handling methods. This is the name of the class as
// SWIG sees it.
String *class_name;
// The receiver name while handling a class. This is only non-NULL
// when we are handling methods. This is the name of the class
// as run through goCPointerType.
String *class_receiver;
// A hash table of method names that we have seen when processing a
// class. This lets us detect base class methods that we don't want
// to use.
Hash *class_methods;
// True when we are generating the wrapper functions for a variable.
bool making_variable_wrappers;
// True when working with a static member function.
bool is_static_member_function;
// A hash table of enum types that we have seen but which may not have
// been defined. The index is a SwigType.
Hash *undefined_enum_types;
// A hash table of types that we have seen but which may not have
// been defined. The index is a SwigType.
Hash *undefined_types;
// A hash table of classes which were defined. The index is a Go
// type name.
Hash *defined_types;
// A hash table of all the go_imports already imported. The index is a full
// import name e.g. '"runtime"' or '_ "runtime/cgo"' or 'sc "syscall"'.
Hash *go_imports;
// A unique ID used to make public symbols unique.
String *unique_id;
public:
GO():package(NULL),
module(NULL),
cgo_flag(false),
gccgo_flag(false),
go_prefix(NULL),
prefix_option(NULL),
pkgpath_option(NULL),
use_shlib(false),
soname(NULL),
intgo_type_size(0),
f_c_begin(NULL),
f_go_begin(NULL),
f_gc_begin(NULL),
f_c_runtime(NULL),
f_c_header(NULL),
f_c_wrappers(NULL),
f_c_init(NULL),
f_c_directors(NULL),
f_c_directors_h(NULL),
f_go_imports(NULL),
f_go_runtime(NULL),
f_go_header(NULL),
f_go_wrappers(NULL),
f_go_directors(NULL),
f_gc_runtime(NULL),
f_gc_header(NULL),
f_gc_wrappers(NULL),
f_cgo_comment(NULL),
f_cgo_comment_typedefs(NULL),
saw_import(false),
imported_package(NULL),
interfaces(NULL),
class_node(NULL),
class_name(NULL),
class_receiver(NULL),
class_methods(NULL),
making_variable_wrappers(false),
is_static_member_function(false),
undefined_enum_types(NULL),
undefined_types(NULL),
defined_types(NULL),
go_imports(NULL),
unique_id(NULL) {
director_multiple_inheritance = 1;
director_language = 1;
director_prot_ctor_code = NewString("_swig_gopanic(\"accessing abstract class or protected constructor\");");
}
private:
/* ------------------------------------------------------------
* main()
* ------------------------------------------------------------ */
virtual void main(int argc, char *argv[]) {
SWIG_library_directory("go");
bool display_help = false;
// Process command line options.
for (int i = 1; i < argc; i++) {
if (argv[i]) {
if (strcmp(argv[i], "-package") == 0) {
if (argv[i + 1]) {
package = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-cgo") == 0) {
Swig_mark_arg(i);
cgo_flag = true;
} else if (strcmp(argv[i], "-gccgo") == 0) {
Swig_mark_arg(i);
gccgo_flag = true;
} else if (strcmp(argv[i], "-go-prefix") == 0) {
if (argv[i + 1]) {
prefix_option = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-go-pkgpath") == 0) {
if (argv[i + 1]) {
pkgpath_option = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-use-shlib") == 0) {
Swig_mark_arg(i);
use_shlib = true;
} else if (strcmp(argv[i], "-soname") == 0) {
if (argv[i + 1]) {
soname = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-longsize") == 0) {
// Ignore for backward compatibility.
if (argv[i + 1]) {
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
++i;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-intgosize") == 0) {
if (argv[i + 1]) {
intgo_type_size = atoi(argv[i + 1]);
if (intgo_type_size != 32 && intgo_type_size != 64) {
Printf(stderr, "-intgosize not 32 or 64\n");
Swig_arg_error();
}
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
++i;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-help") == 0) {
display_help = true;
Printf(stdout, "%s\n", usage);
}
}
}
if (gccgo_flag && !pkgpath_option && !prefix_option) {
prefix_option = NewString("go");
}
// Add preprocessor symbol to parser.
Preprocessor_define("SWIGGO 1", 0);
if (cgo_flag) {
Preprocessor_define("SWIGGO_CGO 1", 0);
}
if (gccgo_flag) {
Preprocessor_define("SWIGGO_GCCGO 1", 0);
}
// This test may be removed in the future, when we can assume that
// everybody has upgraded to Go 1.1. The code below is prepared
// for this test to simply be taken out.
if (intgo_type_size == 0 && !display_help) {
Printf(stderr, "SWIG -go: -intgosize option required but not specified\n");
SWIG_exit(EXIT_FAILURE);
}
if (intgo_type_size == 32) {
Preprocessor_define("SWIGGO_INTGO_SIZE 32", 0);
} else if (intgo_type_size == 64) {
Preprocessor_define("SWIGGO_INTGO_SIZE 64", 0);
} else {
Preprocessor_define("SWIGGO_INTGO_SIZE 0", 0);
}
// Add typemap definitions.
SWIG_typemap_lang("go");
SWIG_config_file("go.swg");
allow_overloading();
}
/* ---------------------------------------------------------------------
* top()
*
* For 6g/8g, we are going to create the following files:
*
* 1) A .c or .cxx file compiled with gcc. This file will contain
* function wrappers. Each wrapper will take a pointer to a
* struct holding the arguments, unpack them, and call the real
* function.
*
* 2) A .go file which defines the Go form of all types, and which
* defines Go function wrappers. Each wrapper will call the C
* function wrapper in the second file.
*
* 3) A .c file compiled with 6c/8c. This file will define
* Go-callable C function wrappers. Each wrapper will use
* cgocall to call the function wrappers in the first file.
*
* When generating code for gccgo, we don't need the third file, and
* the function wrappers in the first file have a different form.
*
* --------------------------------------------------------------------- */
virtual int top(Node *n) {
Node *optionsnode = Getattr(Getattr(n, "module"), "options");
if (optionsnode) {
if (Getattr(optionsnode, "directors")) {
allow_directors();
}
if (Getattr(optionsnode, "dirprot")) {
allow_dirprot();
}
allow_allprotected(GetFlag(optionsnode, "allprotected"));
}
module = Getattr(n, "name");
if (!package) {
package = Copy(module);
}
if (!soname && use_shlib) {
soname = Copy(package);
Append(soname, ".so");
}
if (gccgo_flag) {
String *pref;
if (pkgpath_option) {
pref = pkgpath_option;
} else {
pref = prefix_option;
}
go_prefix = NewString("");
for (char *p = Char(pref); *p != '\0'; p++) {
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '$') {
Putc(*p, go_prefix);
} else {
Putc('_', go_prefix);
}
}
if (!pkgpath_option) {
Append(go_prefix, ".");
Append(go_prefix, getModuleName(package));
}
}
// Get filenames.
String *swig_filename = Getattr(n, "infile");
String *c_filename = Getattr(n, "outfile");
String *c_filename_h = Getattr(n, "outfile_h");
String *go_filename = NewString("");
Printf(go_filename, "%s%s.go", SWIG_output_directory(), module);
String *gc_filename = NULL;
if (!gccgo_flag) {
gc_filename = NewString("");
Printf(gc_filename, "%s%s_gc.c", SWIG_output_directory(), module);
}
// Generate a unique ID based on a hash of the SWIG input.
swig_uint64 hash = {0, 0};
FILE *swig_input = Swig_open(swig_filename);
if (swig_input == NULL) {
FileErrorDisplay(swig_filename);
SWIG_exit(EXIT_FAILURE);
}
String *swig_input_content = Swig_read_file(swig_input);
siphash(&hash, Char(swig_input_content), Len(swig_input_content));
Delete(swig_input_content);
fclose(swig_input);
unique_id = NewString("");
Printf(unique_id, "_%s_%08x%08x", getModuleName(package), hash.hi, hash.lo);
Printf(stdout, "%s\n\n", unique_id);
// Open files.
f_c_begin = NewFile(c_filename, "w", SWIG_output_files());
if (!f_c_begin) {
FileErrorDisplay(c_filename);
SWIG_exit(EXIT_FAILURE);
}
if (directorsEnabled()) {
if (!c_filename_h) {
Printf(stderr, "Unable to determine outfile_h\n");
SWIG_exit(EXIT_FAILURE);
}
f_c_directors_h = NewFile(c_filename_h, "w", SWIG_output_files());
if (!f_c_directors_h) {
FileErrorDisplay(c_filename_h);
SWIG_exit(EXIT_FAILURE);
}
}
f_go_begin = NewFile(go_filename, "w", SWIG_output_files());
if (!f_go_begin) {
FileErrorDisplay(go_filename);
SWIG_exit(EXIT_FAILURE);
}
if (!gccgo_flag && !cgo_flag) {
f_gc_begin = NewFile(gc_filename, "w", SWIG_output_files());
if (!f_gc_begin) {
FileErrorDisplay(gc_filename);
SWIG_exit(EXIT_FAILURE);
}
}
f_c_runtime = NewString("");
f_c_header = NewString("");
f_c_wrappers = NewString("");
f_c_init = NewString("");
f_c_directors = NewString("");
f_go_imports = NewString("");
f_go_runtime = NewString("");
f_go_header = NewString("");
f_go_wrappers = NewString("");
f_go_directors = NewString("");
if (!gccgo_flag && !cgo_flag) {
f_gc_runtime = NewString("");
f_gc_header = NewString("");
f_gc_wrappers = NewString("");
}
if (cgo_flag) {
f_cgo_comment = NewString("");
f_cgo_comment_typedefs = NewString("");
}
Swig_register_filebyname("begin", f_c_begin);
Swig_register_filebyname("runtime", f_c_runtime);
Swig_register_filebyname("header", f_c_header);
Swig_register_filebyname("wrapper", f_c_wrappers);
Swig_register_filebyname("init", f_c_init);
Swig_register_filebyname("director", f_c_directors);
Swig_register_filebyname("director_h", f_c_directors_h);
Swig_register_filebyname("go_begin", f_go_begin);
Swig_register_filebyname("go_imports", f_go_imports);
Swig_register_filebyname("go_runtime", f_go_runtime);
Swig_register_filebyname("go_header", f_go_header);
Swig_register_filebyname("go_wrapper", f_go_wrappers);
Swig_register_filebyname("go_director", f_go_directors);
if (!gccgo_flag && !cgo_flag) {
Swig_register_filebyname("gc_begin", f_gc_begin);
Swig_register_filebyname("gc_runtime", f_gc_runtime);
Swig_register_filebyname("gc_header", f_gc_header);
Swig_register_filebyname("gc_wrapper", f_gc_wrappers);
}
if (cgo_flag) {
Swig_register_filebyname("cgo_comment", f_cgo_comment);
Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs);
}
Swig_banner(f_c_begin);
if (CPlusPlus) {
Printf(f_c_begin, "\n// source: %s\n\n", swig_filename);
} else {
Printf(f_c_begin, "\n/* source: %s */\n\n", swig_filename);
}
Printf(f_c_runtime, "#define SWIGMODULE %s\n", module);
if (gccgo_flag) {
Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix);
}
if (directorsEnabled()) {
Printf(f_c_runtime, "#define SWIG_DIRECTORS\n");
Swig_banner(f_c_directors_h);
Printf(f_c_directors_h, "\n// source: %s\n\n", swig_filename);
Printf(f_c_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module);
Printf(f_c_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module);
Printf(f_c_directors_h, "class Swig_memory;\n\n");
Printf(f_c_directors, "\n// C++ director class methods.\n");
String *filename = Swig_file_filename(c_filename_h);
Printf(f_c_directors, "#include \"%s\"\n\n", filename);
Delete(filename);
}
Swig_banner(f_go_begin);
Printf(f_go_begin, "\n// source: %s\n", swig_filename);
if (!gccgo_flag && !cgo_flag && soname) {
Swig_banner(f_gc_begin);
Printf(f_gc_begin, "\n/* source: %s */\n\n", swig_filename);
Printf(f_gc_begin, "\n/* This file should be compiled with 6c/8c. */\n");
Printf(f_gc_begin, "#pragma dynimport _ _ \"%s\"\n", soname);
}
if (cgo_flag) {
Printv(f_cgo_comment_typedefs, "/*\n", NULL);
// The cgo program defines the intgo type after our function
// definitions, but we want those definitions to be able to use
// intgo also.
Printv(f_cgo_comment_typedefs, "#define intgo swig_intgo\n", NULL);
Printv(f_cgo_comment_typedefs, "typedef void *swig_voidp;\n", NULL);
}
// Output module initialization code.
Printf(f_go_begin, "\npackage %s\n\n", getModuleName(package));
if (gccgo_flag && !cgo_flag) {
Printf(f_go_runtime, "func SwigCgocall()\n");
Printf(f_go_runtime, "func SwigCgocallDone()\n");
Printf(f_go_runtime, "func SwigCgocallBack()\n");
Printf(f_go_runtime, "func SwigCgocallBackDone()\n\n");
}
// All the C++ wrappers should be extern "C".
Printv(f_c_wrappers, "#ifdef __cplusplus\n", "extern \"C\" {\n", "#endif\n\n", NULL);
// Set up the hash table for types not defined by SWIG.
undefined_enum_types = NewHash();
undefined_types = NewHash();
defined_types = NewHash();
go_imports = NewHash();
// Emit code.
Language::top(n);
Delete(go_imports);
// Write out definitions for the types not defined by SWIG.
if (Len(undefined_enum_types) > 0)
Printv(f_go_wrappers, "\n", NULL);
for (Iterator p = First(undefined_enum_types); p.key; p = Next(p)) {
String *name = p.item;
Printv(f_go_wrappers, "type ", name, " int\n", NULL);
}
Printv(f_go_wrappers, "\n", NULL);
for (Iterator p = First(undefined_types); p.key; p = Next(p)) {
String *ty = goType(NULL, p.key);
if (!Getattr(defined_types, ty)) {
String *cp = goCPointerType(p.key, false);
if (!Getattr(defined_types, cp)) {
Printv(f_go_wrappers, "type ", cp, " uintptr\n", NULL);
Printv(f_go_wrappers, "type ", ty, " interface {\n", NULL);
Printv(f_go_wrappers, "\tSwigcptr() uintptr;\n", NULL);
Printv(f_go_wrappers, "}\n", NULL);
Printv(f_go_wrappers, "func (p ", cp, ") Swigcptr() uintptr {\n", NULL);
Printv(f_go_wrappers, "\treturn uintptr(p)\n", NULL);
Printv(f_go_wrappers, "}\n\n", NULL);
}
Delete(cp);
}
Delete(ty);
}
Delete(undefined_enum_types);
Delete(undefined_types);
Delete(defined_types);
/* Write and cleanup */
Dump(f_c_header, f_c_runtime);
if (directorsEnabled()) {
Printf(f_c_directors_h, "#endif\n");
Delete(f_c_directors_h);
f_c_directors_h = NULL;
Dump(f_c_directors, f_c_runtime);
Delete(f_c_directors);
f_c_directors = NULL;
}
// End the extern "C".
Printv(f_c_wrappers, "#ifdef __cplusplus\n", "}\n", "#endif\n\n", NULL);
if (cgo_flag) {
// End the cgo comment.
Printv(f_cgo_comment, "#undef intgo\n", NULL);
Printv(f_cgo_comment, "*/\n", NULL);
Printv(f_cgo_comment, "import \"C\"\n", NULL);
Printv(f_cgo_comment, "\n", NULL);
}
Printf(stdout, "%s", f_c_wrappers);
Dump(f_c_runtime, f_c_begin);
Dump(f_c_wrappers, f_c_begin);
Dump(f_c_init, f_c_begin);
if (cgo_flag) {
Dump(f_cgo_comment_typedefs, f_go_begin);
Dump(f_cgo_comment, f_go_begin);
}
Dump(f_go_imports, f_go_begin);
Dump(f_go_header, f_go_begin);
Dump(f_go_runtime, f_go_begin);
Dump(f_go_wrappers, f_go_begin);
if (directorsEnabled()) {
Dump(f_go_directors, f_go_begin);
}
if (!gccgo_flag && !cgo_flag) {
Dump(f_gc_header, f_gc_begin);
Dump(f_gc_runtime, f_gc_begin);
Dump(f_gc_wrappers, f_gc_begin);
}
Delete(f_c_runtime);
Delete(f_c_header);
Delete(f_c_wrappers);
Delete(f_c_init);
Delete(f_go_imports);
Delete(f_go_runtime);
Delete(f_go_header);
Delete(f_go_wrappers);
Delete(f_go_directors);
if (!gccgo_flag && !cgo_flag) {
Delete(f_gc_runtime);
Delete(f_gc_header);
Delete(f_gc_wrappers);
}
if (cgo_flag) {
Delete(f_cgo_comment);
Delete(f_cgo_comment_typedefs);
}
Delete(f_c_begin);
Delete(f_go_begin);
if (!gccgo_flag && !cgo_flag) {
Delete(f_gc_begin);
}
return SWIG_OK;
}
/* ------------------------------------------------------------
* importDirective()
*
* Handle a SWIG import statement by generating a Go import
* statement.
* ------------------------------------------------------------ */
virtual int importDirective(Node *n) {
String *hold_import = imported_package;
String *modname = Getattr(n, "module");
if (modname) {
if (!Getattr(go_imports, modname)) {
Setattr(go_imports, modname, modname);
Printv(f_go_imports, "import \"", modname, "\"\n", NULL);
}
imported_package = modname;
saw_import = true;
}
int r = Language::importDirective(n);
imported_package = hold_import;
return r;
}
/* ----------------------------------------------------------------------
* Language::insertDirective()
*
* If the section is go_imports, store them for later.
* ---------------------------------------------------------------------- */
virtual int insertDirective(Node *n) {
char *section = Char(Getattr(n, "section"));
if ((ImportMode && !Getattr(n, "generated")) ||
!section || (strcmp(section, "go_imports") != 0)) {
return Language::insertDirective(n);
}
char *code = Char(Getattr(n, "code"));
char *pch = strtok(code, ",");
while (pch != NULL) {
// Do not import same thing more than once.
if (!Getattr(go_imports, pch)) {
Setattr(go_imports, pch, pch);
Printv(f_go_imports, "import ", pch, "\n", NULL);
}
pch = strtok(NULL, ",");
}
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* functionWrapper()
*
* Implement a function.
* ---------------------------------------------------------------------- */
virtual int functionWrapper(Node *n) {
if (GetFlag(n, "feature:ignore")) {
return SWIG_OK;
}
// We don't need explicit calls.
if (GetFlag(n, "explicitcall")) {
return SWIG_OK;
}
String *name = Getattr(n, "sym:name");
String *nodetype = Getattr(n, "nodeType");
bool is_static = is_static_member_function || isStatic(n);
bool is_friend = isFriend(n);
bool is_ctor_dtor = false;
SwigType *result = Getattr(n, "type");
// For some reason SWIG changs the "type" value during the call to
// functionWrapper. We need to remember the type for possible
// overload processing.
Setattr(n, "go:type", Copy(result));
String *go_name;
String *r1 = NULL;
if (making_variable_wrappers) {
// Change the name of the variable setter and getter functions
// to be more Go like.
bool is_set = Strcmp(Char(name) + Len(name) - 4, "_set") == 0;
assert(is_set || Strcmp(Char(name) + Len(name) - 4, "_get") == 0);
// Start with Set or Get.
go_name = NewString(is_set ? "Set" : "Get");
// If this is a static variable, put in the class name,
// capitalized.
if (is_static && class_name) {
String *ccn = exportedName(class_name);
Append(go_name, ccn);
Delete(ccn);
}
// Add the rest of the name, capitalized, dropping the _set or
// _get.
String *c1 = removeClassname(name);
String *c2 = exportedName(c1);
char *p = Char(c2);
int len = Len(p);
for (int i = 0; i < len - 4; ++i) {
Putc(p[i], go_name);
}
Delete(c2);
Delete(c1);
if (!checkIgnoredParameters(n, go_name)) {
Delete(go_name);
return SWIG_NOWRAP;
}
} else if (Cmp(nodetype, "constructor") == 0) {
is_ctor_dtor = true;
// Change the name of a constructor to be more Go like. Change
// new_ to New, and capitalize the class name.
assert(Strncmp(name, "new_", 4) == 0);
String *c1 = NewString(Char(name) + 4);
String *c2 = exportedName(c1);
go_name = NewString("New");
Append(go_name, c2);
Delete(c2);
Delete(c1);
if (Swig_methodclass(n) && Swig_directorclass(n)
&& Strcmp(Char(Getattr(n, "wrap:action")), director_prot_ctor_code) != 0) {
// The core SWIG code skips the first parameter when
// generating the $nondirector_new string. Recreate the
// action in this case. But don't it if we are using the
// special code for an abstract class.
String *call = Swig_cppconstructor_call(getClassType(),
Getattr(n, "parms"));
SwigType *type = Copy(getClassType());
SwigType_add_pointer(type);
String *cres = Swig_cresult(type, Swig_cresult_name(), call);
Setattr(n, "wrap:action", cres);
}
} else if (Cmp(nodetype, "destructor") == 0) {
// No need to emit protected destructors.
if (!is_public(n)) {
return SWIG_OK;
}
is_ctor_dtor = true;
// Change the name of a destructor to be more Go like. Change
// delete_ to Delete and capitalize the class name.
assert(Strncmp(name, "delete_", 7) == 0);
String *c1 = NewString(Char(name) + 7);
String *c2 = exportedName(c1);
go_name = NewString("Delete");
Append(go_name, c2);
Delete(c2);
Delete(c1);
result = NewString("void");
r1 = result;
} else {
if (!checkFunctionVisibility(n, NULL)) {
return SWIG_OK;
}
go_name = buildGoName(name, is_static, is_friend);
if (!checkIgnoredParameters(n, go_name)) {
Delete(go_name);
return SWIG_NOWRAP;
}
}
String *overname = NULL;
if (Getattr(n, "sym:overloaded")) {
overname = Getattr(n, "sym:overname");
} else {
String *scope;
if (!class_name || is_static || is_ctor_dtor) {
scope = NULL;
} else {
scope = NewString("swiggoscope.");
Append(scope, class_name);
}
if (!checkNameConflict(go_name, n, scope)) {
Delete(go_name);
return SWIG_NOWRAP;
}
}
String *wname = Swig_name_wrapper(name);
if (overname) {
Append(wname, overname);
}
Append(wname, unique_id);
Setattr(n, "wrap:name", wname);
ParmList *parms = Getattr(n, "parms");
Setattr(n, "wrap:parms", parms);
int r = makeWrappers(n, name, go_name, overname, wname, NULL, parms, result, is_static);
if (r != SWIG_OK) {
return r;
}
if (Getattr(n, "sym:overloaded") && !Getattr(n, "sym:nextSibling")) {
String *scope ;
if (!class_name || is_static || is_ctor_dtor) {
scope = NULL;
} else {
scope = NewString("swiggoscope.");
Append(scope, class_name);
}
if (!checkNameConflict(go_name, n, scope)) {
Delete(go_name);
return SWIG_NOWRAP;
}
String *receiver = class_receiver;
if (is_static || is_ctor_dtor) {
receiver = NULL;
}
r = makeDispatchFunction(n, go_name, receiver, is_static, NULL, false);
if (r != SWIG_OK) {
return r;
}
}
Delete(wname);
Delete(go_name);
Delete(r1);
return SWIG_OK;
}
/* ----------------------------------------------------------------------
* staticmemberfunctionHandler()
*
* For some reason the language code removes the "storage" attribute
* for a static function before calling functionWrapper, which means
* that we have no way of knowing whether a function is static or
* not. That makes no sense in the Go context. Here we note that a
* function is static.
* ---------------------------------------------------------------------- */
int staticmemberfunctionHandler(Node *n) {
assert(!is_static_member_function);
is_static_member_function = true;
int r = Language::staticmemberfunctionHandler(n);
is_static_member_function = false;
return r;
}
/* ----------------------------------------------------------------------
* makeWrappers()
*
* Write out the various function wrappers.
* n: The function we are emitting.
* name: The function name.
* go_name: The name of the function in Go.
* overname: The overload string for overloaded function.
* wname: The SWIG wrapped name--the name of the C function.
* base: A list of the names of base classes, in the case where this
* is is a vritual method not defined in the current class.
* parms: The parameters.
* result: The result type.
* is_static: Whether this is a static method or member.
* ---------------------------------------------------------------------- */
int makeWrappers(Node *n, String *name, String *go_name, String *overname, String *wname, List *base, ParmList *parms, SwigType *result, bool is_static) {
assert(result);
int ret = SWIG_OK;