summaryrefslogtreecommitdiff
path: root/Source/Modules/com.cxx
blob: 6cee6bf8bad30da084ceff87fc9357db72fe9359 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
/* -----------------------------------------------------------------------------
 * 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.
 *
 * com.cxx
 *
 * COM module for SWIG.
 * ----------------------------------------------------------------------------- */

char cvsroot_com_cxx[] = "$Id$";

#include "swigmod.h"
#include "cparse.h"

typedef struct {
  unsigned long Data1;
  unsigned short Data2;
  unsigned short Data3;
  unsigned char Data4[8];
} GUID, UUID;

class COM:public Language {
  static const char *usage;
  const String *empty_string;

  Hash *swig_types_hash;
  List *filenames_list;

  File *f_runtime;
  File *f_header;
  File *f_module;
  File *f_deffile;
  File *f_rcfile;
  File *f_wrappers;
  File *f_proxy;
  File *f_proxy_forward_defs;
  File *f_vtables;
  File *f_vtable_defs;
  File *f_factory;

  bool proxy_flag;		// Flag for generating proxy classes
  bool dllexports_flag;
  bool deffile_flag;
  bool rcfile_flag;
  bool enum_constant_flag;	// Flag for when wrapping an enum or constant
  bool static_flag;		// Flag for when wrapping a static functions or member variables
  bool variable_wrapper_flag;	// Flag for when wrapping a nonstatic member variable
  bool wrapping_member_flag;	// Flag for when wrapping a member variable/enum/const
  bool global_variable_flag;	// Flag for when wrapping a global variable
  bool generate_property_declaration_flag;	// Flag for generating properties
  bool member_func_flag;
  bool constructor_flag;

  String *proxy_class_def;
  String *proxy_class_forward_def;
  String *proxy_class_code;
  String *proxy_class_name;
  String *proxy_class_constants_code;
  String *clsid_list;
  List *proxy_class_member_functions;
  String *variable_name;	//Name of a variable being wrapped
  int guid_counter;
  GUID *proxy_iid;
  GUID *proxy_clsid;

  String *module_class_vtable_code;
  String *proxy_class_vtable_code;
  String *proxy_class_vtable_defs;

  String *module_class_name;	// module class name
  String *module_class_code;

public:

  /* -----------------------------------------------------------------------------
   * COM()
   * ----------------------------------------------------------------------------- */

  COM():empty_string(NewString("")),
      proxy_flag(true),
      deffile_flag(true),
      rcfile_flag(true),
      dllexports_flag(true),
      enum_constant_flag(false),
      proxy_class_vtable_code(NewString("")),
      proxy_class_vtable_defs(NewString("")),
      clsid_list(NewString("")),
      guid_counter(0) {
    /* Empty for now */
  }

  /* -----------------------------------------------------------------------------
   * getProxyName()
   *
   * Test to see if a type corresponds to something wrapped with a proxy class
   * Return NULL if not otherwise the proxy class name
   * ----------------------------------------------------------------------------- */
  
   String *getProxyName(SwigType *t) {
    if (proxy_flag) {
      Node *n = classLookup(t);
      if (n) {
	return Getattr(n, "sym:name");
      }
    }
    return NULL;
  }

  /* ------------------------------------------------------------
   * main()
   * ------------------------------------------------------------ */

  virtual void main(int argc, char *argv[]) {

    SWIG_library_directory("com");

    // Look for certain command line options
    for (int i = 1; i < argc; i++) {
      if (argv[i]) {
	if (strcmp(argv[i], "-help") == 0) {
	  Printf(stdout, "%s\n", usage);
	} else if ((strcmp(argv[i], "-nodllexports") == 0)) {
	  Swig_mark_arg(i);
          dllexports_flag = false;
          deffile_flag = false;
          rcfile_flag = false;
	} else if (strcmp(argv[i], "-nodeffile") == 0) {
	  Swig_mark_arg(i);
          deffile_flag = false;
	} else if (strcmp(argv[i], "-norcfile") == 0) {
	  Swig_mark_arg(i);
          rcfile_flag = false;
	}
      }
    }
    // Add a symbol to the parser for conditional compilation
    Preprocessor_define("SWIGCOM 1", 0);

    // Add typemap definitions
    SWIG_typemap_lang("com");
    SWIG_config_file("com.swg");

    allow_overloading(0);
  }

  /* ---------------------------------------------------------------------
   * top()
   * --------------------------------------------------------------------- */

  virtual int top(Node *n) {

    /* Initialize all of the output files */
    String *outfile = Getattr(n, "outfile");
    String *outfile_h = Getattr(n, "outfile_h");

    if (!outfile) {
      Printf(stderr, "Unable to determine outfile\n");
      SWIG_exit(EXIT_FAILURE);
    }

    f_runtime = NewFile(outfile, "w");
    if (!f_runtime) {
      FileErrorDisplay(outfile);
      SWIG_exit(EXIT_FAILURE);
    }

    f_header = NewString("");
    f_wrappers = NewString("");
    f_proxy = NewString("");
    f_proxy_forward_defs = NewString("");
    f_vtables = NewString("");
    f_vtable_defs = NewString("");
    f_factory = NewString("");

    /* Register file targets with the SWIG file handler */
    Swig_register_filebyname("header", f_header);
    Swig_register_filebyname("runtime", f_runtime);
    Swig_register_filebyname("factory", f_factory);

    swig_types_hash = NewHash();
    filenames_list = NewList();

    /* FIXME: do it as it is done in other targets */
    module_class_name = Copy(Getattr(n, "name"));

    module_class_code = NewString("");
    proxy_class_def = NewString("");
    proxy_class_forward_def = NewString("");
    proxy_class_code = NewString("");

    GUID typelib_guid;
    generateGUID(&typelib_guid);
    GUID module_iid;
    generateGUID(&module_iid);
    GUID module_clsid;
    generateGUID(&module_clsid);

    module_class_vtable_code = NewString("");

    Printf(module_class_vtable_code, "GUID IID_%s = ", module_class_name);
    formatGUID(module_class_vtable_code, &module_iid, true);
    Printf(module_class_vtable_code, ";\n\n");

    Printf(module_class_vtable_code, "GUID CLSID_%s = ", module_class_name);
    formatGUID(module_class_vtable_code, &module_clsid, true);
    Printf(module_class_vtable_code, ";\n\n");

    Printf(module_class_vtable_code, "HRESULT SWIGSTDCALL _wrap%sQueryInterface(void *that, GUID *iid, "
        "void ** ppvObject) {\n", module_class_name);

    Printf(module_class_vtable_code, "  if (SWIGIsEqual(iid, &IID_IUnknown) ||\n"
        "      SWIGIsEqual(iid, &IID_IDispatch) ||\n"
        "      SWIGIsEqual(iid, &IID_%s)", module_class_name);

    Printf(module_class_vtable_code, ") {\n"
        "    /* FIXME: This could be more elegant */\n"
        "    SWIGAddRef1(that);\n"
        "    *ppvObject = that;\n"
        "    return S_OK;\n"
        "  }\n\n");

    Printf(module_class_vtable_code, "  return E_NOINTERFACE;\n}\n\n");

    Printf(module_class_vtable_code, "extern SWIG_funcptr _wrap%s_vtable[];\n\n", module_class_name);

    Printf(module_class_vtable_code,
        "void * SWIGSTDCALL _wrap_new_%s(void *SWIG_ignored) {\n"
        "#ifdef __cplusplus\n"
        "  SWIGWrappedObject *res = new SWIGWrappedObject;\n"
        "#else\n"
        "  SWIGWrappedObject *res = (SWIGWrappedObject *) malloc(sizeof(SWIGWrappedObject));\n"
        "#endif\n"
        "  res->vtable = _wrap%s_vtable;\n"
        "  res->SWIGWrappedObject_vtable = NULL;\n"
        "  /* cPtr and cMemOwn make no sense for the module class */\n"
        "  res->cPtr = NULL;\n"
        "  res->cMemOwn = 0;\n"
        "  InterlockedIncrement(&globalRefCount);\n"
        "  res->refCount = 1;\n"
        "  res->deleteInstance = 0;\n"
        "  /* GetTypeInfoOfGuid */\n"
        "  ((HRESULT (SWIGSTDCALL *)(ITypeLib *, GUID *, ITypeInfo **)) (((SWIGIUnknown *) SWIG_typelib)->vtable[6]))(SWIG_typelib, &IID_%s, &res->typeInfo);\n"
        "  return (void *) res;\n"
        "};\n\n",
        module_class_name, module_class_name, module_class_name);

    Printf(module_class_vtable_code, "SWIG_funcptr _wrap%s_vtable[] = "
        "{\n  (SWIG_funcptr) _wrap%sQueryInterface,"
        "\n  (SWIG_funcptr) SWIGAddRef1,"
        "\n  (SWIG_funcptr) SWIGRelease1,"
        "\n  (SWIG_funcptr) SWIGGetTypeInfoCount,"
        "\n  (SWIG_funcptr) SWIGGetTypeInfo,"
        "\n  (SWIG_funcptr) SWIGGetIDsOfNames,"
        "\n  (SWIG_funcptr) SWIGInvoke",
        module_class_name, module_class_name);

    Printf(clsid_list, "static TCHAR * SWIG_tlb_guid_string = _T(\"{");
    formatGUID(clsid_list, &typelib_guid, false);
    Printf(clsid_list, "}\");\n\n");

    Printf(clsid_list, "static SWIGClassDescription_t SWIGClassDescription[] = {\n");
    Printf(clsid_list, "  { (SWIG_funcptr) _wrap_new_%s, &CLSID_%s, _T(\"{", module_class_name, module_class_name);
    formatGUID(clsid_list, &module_clsid, false);
    Printf(clsid_list,  "}\") },\n");

    /* Emit code */
    Language::top(n);

    /* Insert guard element */
    Printf(clsid_list, "  { NULL, NULL, NULL } /* guard element */\n");

    Printf(clsid_list, "};\n\n");

    Printv(module_class_vtable_code, "\n};\n\n", NIL);

    // Output a COM type wrapper class for each SWIG type
    for (Iterator swig_type = First(swig_types_hash); swig_type.key; swig_type = Next(swig_type)) {
      emitTypeWrapperClass(swig_type.key, swig_type.item);
    }

    /* Generate DEF file */
    if (deffile_flag) {
      String *filen = NewStringf("%s%s.def", SWIG_output_directory(), module_class_name);
      f_deffile = NewFile(filen, "w");
      if (!f_deffile) {
	FileErrorDisplay(filen);
	SWIG_exit(EXIT_FAILURE);
      }
      // Append(filenames_list, Copy(filen));
      Delete(filen);
      filen = NULL;

      Printf(f_deffile, "LIBRARY %s\n", module_class_name);

      Printf(f_deffile, "EXPORTS\n"
          "  DllGetClassObject PRIVATE\n"
          "  DllCanUnloadNow PRIVATE\n"
          "  DllRegisterServer PRIVATE\n"
          "  DllUnregisterServer PRIVATE\n"
          "  DllMain\n");
    }

    if (rcfile_flag) {
      String *filen = NewStringf("%s%s_rc.rc", SWIG_output_directory(), module_class_name);
      f_rcfile = NewFile(filen, "w");
      if (!f_rcfile) {
	FileErrorDisplay(filen);
	SWIG_exit(EXIT_FAILURE);
      }
      // Append(filenames_list, Copy(filen));
      Delete(filen);
      filen = NULL;

      Printf(f_rcfile, "1 typelib \"%s.tlb\"\n", module_class_name);
    }

    /* Generate the IDL file containing the module class and proxy classes */
    {
      String *filen = NewStringf("%s%s.idl", SWIG_output_directory(), module_class_name);
      f_module = NewFile(filen, "w");
      if (!f_module) {
	FileErrorDisplay(filen);
	SWIG_exit(EXIT_FAILURE);
      }
      // Append(filenames_list, Copy(filen));
      Delete(filen);
      filen = NULL;

      // Banner for the IDL file
      emitBanner(f_module);

      // Standard imports
      Printf(f_module, "[\n  uuid(");
      formatGUID(f_module, &typelib_guid, false);
      Printf(f_module, ")\n]\nlibrary %sTLB {\n\n", module_class_name);

      // Import IDispatch
      // FIXME: Printf(f_module, "  importlib(\"stdole32.tlb\");\n\n");
      Printf(f_module, "  importlib(\"stdole2.tlb\");\n\n");

      Printv(f_module, f_proxy_forward_defs, "\n", NIL);

      // Interface for module class
      Printf(f_module, "  [\n    object,\n    local,\n    uuid(");
      formatGUID(f_module, &module_iid, false);
      Printf(f_module, "),\n    dual\n  ]\n  interface %s : IDispatch {\n", module_class_name);
      // FIXME: a temporary workaround for a possible WIDL bug
      Printf(f_module, "#ifdef __WIDL__\n");
      Printf(f_module, "    import \"stdole2.idl\";\n");
      Printf(f_module, "#endif\n");

      // Add the wrapper methods
      Printv(f_module, module_class_code, NIL);

      Printf(f_module, "  };\n\n");

      Printv(f_module, "  [\n    uuid(", NIL);
      formatGUID(f_module, &module_clsid, false);
      Printv(f_module, ")\n  ]\n  coclass ", module_class_name, "Impl {\n"
          "    interface ", module_class_name, ";\n  };\n\n", NIL);

      // Add the proxy code
      Printv(f_module, f_proxy, NIL);

      Printf(f_module, "};\n");
    }

    /* Close all of the files */
    Dump(f_header, f_runtime);
    Delete(f_header);
    Printv(f_runtime, f_vtable_defs, "\n", NIL);
    Delete(f_vtable_defs);
    Dump(f_wrappers, f_runtime);
    Delete(f_wrappers);
    // Vtable for the module class
    Printv(f_runtime, module_class_vtable_code, NIL);
    Dump(f_vtables, f_runtime);
    Delete(f_vtables);
    Dump(clsid_list, f_runtime);
    Delete(clsid_list);
    Replaceall(f_factory, "$module", module_class_name);
    if (dllexports_flag)
      Dump(f_factory, f_runtime);
    Delete(f_factory);
    Close(f_runtime);
    Delete(f_runtime);
    Close(f_module);
    Delete(f_module);
    if (deffile_flag) {
      Close(f_deffile);
      Delete(f_deffile);
    }
    if (rcfile_flag) {
      Close(f_rcfile);
      Delete(f_rcfile);
    }
    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * emitBanner()
   * ----------------------------------------------------------------------------- */

  void emitBanner(File *f) {
    Printf(f, "/* ----------------------------------------------------------------------------\n");
    Printf(f, " * This file was automatically generated by SWIG (http://www.swig.org).\n");
    Printf(f, " * Version %s\n", Swig_package_version());
    Printf(f, " *\n");
    Printf(f, " * Do not make changes to this file unless you know what you are doing--modify\n");
    Printf(f, " * the SWIG interface file instead.\n");
    Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
  }

  /* -----------------------------------------------------------------------------
   * functionWrapper()
   * ----------------------------------------------------------------------------- */

  virtual int functionWrapper(Node *n) {
    String *symname = Getattr(n, "sym:name");
    SwigType *t = Getattr(n, "type");
    ParmList *l = Getattr(n, "parms");
    String *tm;
    Parm *p;
    int i;
    String *c_return_type = NewString("");
    bool is_void_return;
    int num_arguments = 0;
    int num_required = 0;

    /* FIXME */
    String *overloaded_name = Copy(symname);

    // A new wrapper function object
    Wrapper *f = NewWrapper();

    // Make a wrapper name for this function
    String *wname = Swig_name_wrapper(overloaded_name);

    /* Attach the non-standard typemaps to the parameter list. */
    Swig_typemap_attach_parms("ctype", l, f);

    /* Get return types */
    if ((tm = Swig_typemap_lookup("ctype", n, "", 0))) {
      String *ctypeout = Getattr(n, "tmap:ctype:out");	// the type in the ctype typemap's out attribute overrides the type in the typemap
      if (ctypeout)
	tm = ctypeout;
      Printf(c_return_type, "%s", tm);
    } else {
      Swig_warning(WARN_COM_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(t, 0));
    }

    is_void_return = (Cmp(c_return_type, "void") == 0);
    if (!is_void_return)
      Wrapper_add_localv(f, "jresult", c_return_type, "jresult", NIL);

    Printv(f->def, c_return_type, " SWIGSTDCALL ", wname, "(", NIL);

    // Emit all of the local variables for holding arguments.
    emit_parameter_variables(l, f);

    /* Attach the standard typemaps */
    emit_attach_parmmaps(l, f);

    // Parameter overloading
    Setattr(n, "wrap:parms", l);
    Setattr(n, "wrap:name", wname);

    /* Get number of required and total arguments */
    num_arguments = emit_num_arguments(l);
    num_required = emit_num_required(l);
    int gencomma = 0;

    /* There are no global or static member functions in COM - thus they need fake 'this' arguments */
    // FIXME: this should include static member functions too
    if ((!is_wrapping_class() || static_flag) && !enum_constant_flag) {
      Printv(f->def, "void *SWIG_ignored", NIL);
      gencomma = 1;
    }


    // Now walk the function parameter list and generate code to get arguments
    for (i = 0, p = l; i < num_arguments; i++) {

      while (checkAttribute(p, "tmap:in:numinputs", "0")) {
	p = Getattr(p, "tmap:in:next");
      }

      SwigType *pt = Getattr(p, "type");
      String *ln = Getattr(p, "lname");
      String *c_param_type = NewString("");
      String *arg = NewString("");

      Printf(arg, "j%s", ln);

      /* Get the ctype types of the parameter */
      if ((tm = Getattr(p, "tmap:ctype"))) {
	Printv(c_param_type, tm, NIL);
      } else {
	Swig_warning(WARN_COM_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(pt, 0));
      }

      // Add parameter to C function
      Printv(f->def, gencomma ? ", " : "", c_param_type, " ", arg, NIL);

      gencomma = 1;

      // Get typemap for this argument
      if ((tm = Getattr(p, "tmap:in"))) {
	// FIXME: canThrow(n, "in", p);
	Replaceall(tm, "$source", arg);	/* deprecated */
	Replaceall(tm, "$target", ln);	/* deprecated */
	Replaceall(tm, "$arg", arg);	/* deprecated? */
	Replaceall(tm, "$input", arg);
	Setattr(p, "emit:input", arg);
	Printf(f->code, "%s\n", tm);
	p = Getattr(p, "tmap:in:next");
      } else {
	Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
	p = nextSibling(p);
      }
      Delete(c_param_type);
      Delete(arg);
    }

    String *null_attribute = 0;
    // Now write code to make the function call
    /* FIXME: if (!native_function_flag) */ {
      if (Cmp(nodeType(n), "constant") == 0) {
        // Wrapping a constant hack
        Swig_save("functionWrapper", n, "wrap:action", NIL);

        // below based on Swig_VargetToFunction()
        SwigType *ty = Swig_wrapped_var_type(Getattr(n, "type"), use_naturalvar_mode(n));
        Setattr(n, "wrap:action", NewStringf("result = (%s) %s;", SwigType_lstr(ty, 0), Getattr(n, "value")));
      }

      // FIXME: Swig_director_emit_dynamic_cast(n, f);
      String *actioncode = emit_action(n);

      if (Cmp(nodeType(n), "constant") == 0)
        Swig_restore(n);

      /* Return value if necessary  */
      if ((tm = Swig_typemap_lookup_out("out", n, "result", f, actioncode))) {
	// FIXME: canThrow(n, "out", n);
	Replaceall(tm, "$source", "result");	/* deprecated */
	Replaceall(tm, "$target", "jresult");	/* deprecated */
	Replaceall(tm, "$result", "jresult");

        if (GetFlag(n, "feature:new"))
          Replaceall(tm, "$owner", "1");
        else
          Replaceall(tm, "$owner", "0");

        /* FIXME: see if this is needed and works as it should */
        substituteClassname(t, tm);

	Printf(f->code, "%s", tm);
	null_attribute = Getattr(n, "tmap:out:null");
	if (Len(tm))
	  Printf(f->code, "\n");
      } else {
	Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(t, 0), Getattr(n, "name"));
      }
      emit_return_variable(n, t, f);
    }

    Printf(f->def, ") {");

    if (!is_void_return)
      Printv(f->code, "    return jresult;\n", NIL);
    Printf(f->code, "}\n");

    Wrapper_print(f, f_wrappers);

    if (!(proxy_flag && is_wrapping_class() && !constructor_flag) && !enum_constant_flag) {
      moduleClassFunctionHandler(n);
      Printf(module_class_vtable_code, ",\n  (SWIG_funcptr) %s", wname);
    }

    /* 
     * Generate the proxy class properties for public member variables.
     * Not for enums and constants.
     */
    if (proxy_flag && wrapping_member_flag && !enum_constant_flag) {
      // Capitalize the first letter in the variable in the getter/setter function name
      bool getter_flag = Cmp(symname, Swig_name_set(Swig_name_member(proxy_class_name, variable_name))) != 0;

      String *getter_setter_name = NewString("");
#if 0
      if (!getter_flag)
	Printf(getter_setter_name, "set");
      else
	Printf(getter_setter_name, "get");
      Putc(toupper((int) *Char(variable_name)), getter_setter_name);
      Printf(getter_setter_name, "%s", Char(variable_name) + 1);
#endif

      Printf(getter_setter_name, "%s", variable_name);

      Setattr(n, "proxyfuncname", getter_setter_name);
      Setattr(n, "imfuncname", symname);

      proxyClassFunctionHandler(n);
      Delete(getter_setter_name);
    }

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------
   * globalvariableHandler()
   * ------------------------------------------------------------------------ */

  virtual int globalvariableHandler(Node *n) {

    generate_property_declaration_flag = true;
    variable_name = Getattr(n, "sym:name");
    global_variable_flag = true;
    int ret = Language::globalvariableHandler(n);
    global_variable_flag = false;
    generate_property_declaration_flag = false;

    return ret;
  }

  /* ----------------------------------------------------------------------
   * memberfunctionHandler()
   * ---------------------------------------------------------------------- */

  virtual int memberfunctionHandler(Node *n) {
    Language::memberfunctionHandler(n);

    if (proxy_flag) {
      // FIXME: String *overloaded_name = getOverloadedName(n);
      String *overloaded_name = Getattr(n, "sym:name");
      String *intermediary_function_name = Swig_name_member(proxy_class_name, overloaded_name);
      Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
      Setattr(n, "imfuncname", intermediary_function_name);
      proxyClassFunctionHandler(n);
      Delete(overloaded_name);
    }
    return SWIG_OK;
  }

  /* ----------------------------------------------------------------------
   * membervariableHandler()
   * ---------------------------------------------------------------------- */

  virtual int membervariableHandler(Node *n) {

    generate_property_declaration_flag = true;
    variable_name = Getattr(n, "sym:name");
    wrapping_member_flag = true;
    variable_wrapper_flag = true;
    Language::membervariableHandler(n);
    wrapping_member_flag = false;
    variable_wrapper_flag = false;
    generate_property_declaration_flag = false;

    return SWIG_OK;
  }

  /* ----------------------------------------------------------------------
   * staticmembervariableHandler()
   * ---------------------------------------------------------------------- */

  virtual int staticmembervariableHandler(Node *n) {

    bool static_const_member_flag = (Getattr(n, "value") == 0);

    generate_property_declaration_flag = true;
    variable_name = Getattr(n, "sym:name");
    wrapping_member_flag = true;
    static_flag = true;
    Language::staticmembervariableHandler(n);
    wrapping_member_flag = false;
    static_flag = false;
    generate_property_declaration_flag = false;

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * constructorHandler()
   * ----------------------------------------------------------------------------- */

  virtual int constructorHandler(Node *n) {
    static_flag = true;
    constructor_flag = true;
    Language::constructorHandler(n);
    constructor_flag = false;
    static_flag = false;

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * moduleClassFunctionHandler()
   * ----------------------------------------------------------------------------- */

  int moduleClassFunctionHandler(Node *n) {
    SwigType *t = Getattr(n, "type");
    ParmList *l = Getattr(n, "parms");
    String *tm;
    String *return_type = NewString("");
    String *function_code = NewString("");
    Parm *p;
    int i;
    String *func_name = NULL;
    bool setter_flag = false;

    if (l) {
      if (SwigType_type(Getattr(l, "type")) == T_VOID) {
	l = nextSibling(l);
      }
    }

    /* Attach the non-standard typemaps to the parameter list */
    Swig_typemap_attach_parms("comtype", l, NULL);
    Swig_typemap_attach_parms("comin", l, NULL);

    /* Get return types */
    if (!constructor_flag && (tm = Swig_typemap_lookup("comtype", n, "", 0))) {
      String *comtypeout = Getattr(n, "tmap:comtype:out");	// the type in the comtype typemap's out attribute overrides the type in the typemap
      if (comtypeout)
	tm = comtypeout;
      substituteClassname(t, tm);
      Printf(return_type, "%s", tm);
    } else if (constructor_flag) {
      Printf(return_type, "%s *", proxy_class_name);
    } else {
      Swig_warning(WARN_COM_TYPEMAP_COMTYPE_UNDEF, input_file, line_number, "No comtype typemap defined for %s\n", SwigType_str(t, 0));
    }

    if (proxy_flag && global_variable_flag) {
      func_name = NewString("");
      setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(variable_name)) == 0);
      Printf(func_name, "%s", variable_name);

      if (setter_flag) {
        Printf(function_code, "    [ propput ]\n");
      } else {
        Printf(function_code, "    [ propget ]\n");
      }
    } else {
      /* FIXME: ... */
      func_name = Getattr(n, "sym:name");
    }

    Printf(function_code, "    %s %s(", return_type, func_name);

    /* Get number of required and total arguments */
    int num_arguments = emit_num_arguments(l);
    int num_required = emit_num_required(l);

    int gencomma = 0;

    /* Output each parameter */
    for (i = 0, p = l; i < num_arguments; i++) {

      /* Ignored parameters */
      while (checkAttribute(p, "tmap:in:numinputs", "0")) {
	p = Getattr(p, "tmap:in:next");
      }

      SwigType *pt = Getattr(p, "type");
      String *param_type = NewString("");

      /* Get the COM parameter type */
      if ((tm = Getattr(p, "tmap:comtype"))) {
	substituteClassname(pt, tm);
	Printf(param_type, "%s", tm);
      } else {
	Swig_warning(WARN_COM_TYPEMAP_COMTYPE_UNDEF, input_file, line_number, "No comtype typemap defined for %s\n", SwigType_str(pt, 0));
      }

      /* FIXME: get the real argument name, it is important in the IDL */
      String *arg = NewStringf("arg%d", i);

      /* Add parameter to module class function */
      if (gencomma >= 2)
	Printf(function_code, ", ");
      gencomma = 2;
      Printf(function_code, "%s %s", param_type, arg);

      p = Getattr(p, "tmap:in:next");
      Delete(arg);
      Delete(param_type);
    }

    Printf(function_code, ");\n");

    Printv(module_class_code, function_code, NIL);
    Delete(function_code);
    Delete(return_type);

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * emitProxyClassDefAndCPPCasts()
   * ----------------------------------------------------------------------------- */

  void emitProxyClassDefAndCPPCasts(Node *n) {
    String *c_classname = SwigType_namestr(Getattr(n, "name"));
    String *c_baseclass = NULL;
    String *baseclass = NULL;
    String *c_baseclassname = NULL;
    String *typemap_lookup_type = Getattr(n, "classtypeobj");
    bool feature_director = Swig_directorclass(n) ? true : false;

    Node *attributes = NewHash();

    const String *pure_baseclass = NewString("");

    // C++ inheritance
    // FIXME: if (!purebase_replace) {
      List *baselist = Getattr(n, "bases");
      if (baselist) {
        Iterator base = First(baselist);
        while (base.item && GetFlag(base.item, "feature:ignore")) {
          base = Next(base);
        }
        if (base.item) {
          c_baseclassname = Getattr(base.item, "name");
          baseclass = Copy(getProxyName(c_baseclassname));
          if (baseclass)
            c_baseclass = SwigType_namestr(Getattr(base.item, "name"));
          base = Next(base);
          /* Warn about multiple inheritance for additional base class(es) */
          while (base.item) {
            if (GetFlag(base.item, "feature:ignore")) {
              base = Next(base);
              continue;
            }
            String *proxyclassname = SwigType_str(Getattr(n, "classtypeobj"), 0);
            String *baseclassname = SwigType_str(Getattr(base.item, "name"), 0);
            Swig_warning(WARN_COM_MULTIPLE_INHERITANCE, input_file, line_number,
                         "Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in C#.\n", proxyclassname, baseclassname);
            base = Next(base);
          }
        }
      }
    // FIXME: }

    const String *wanted_base = baseclass ? baseclass : pure_baseclass;
    bool derived = baseclass && getProxyName(c_baseclassname);

    if (!Getattr(n, "abstract")) {
      Printv(proxy_class_def, "  [\n    uuid(", NIL);
      formatGUID(proxy_class_def, proxy_clsid, false);
      Printv(proxy_class_def, ")\n  ]\n  coclass $comclassnameImpl {\n"
          "    interface $comclassname;\n  };\n\n", NIL);
    }

    Printv(proxy_class_forward_def, "  interface $comclassname;\n", NIL);
    Printv(proxy_class_def, "  [\n    object,\n    local,\n    uuid(", NIL);
    formatGUID(proxy_class_def, proxy_iid, false);
/*
    Printv(proxy_class_def, ")\n  ]\n  interface $comclassname",
        *Char(wanted_base) ? " : " : "",
        *Char(wanted_base) ? wanted_base : "", " {", NIL);
 */
    Printv(proxy_class_def, "),\n    dual\n  ]\n  interface $comclassname : ",
        *Char(wanted_base) ? wanted_base : "IDispatch", " {", NIL);

    Delete(attributes);

    // FIXME: temporary
    Printv(proxy_class_def, typemapLookup("combody", typemap_lookup_type, WARN_NONE),
	   NIL);

    // Emit extra user code
    Printv(proxy_class_def, typemapLookup("comcode", typemap_lookup_type, WARN_NONE),
	   NIL);

    // Substitute various strings into the above template
    Replaceall(proxy_class_code, "$comclassname", proxy_class_name);
    Replaceall(proxy_class_def, "$comclassname", proxy_class_name);
    Replaceall(proxy_class_forward_def, "$comclassname", proxy_class_name);

    Replaceall(proxy_class_def, "$module", module_class_name);
    Replaceall(proxy_class_code, "$module", module_class_name);

    Delete(baseclass);
  }

  /* ----------------------------------------------------------------------
   * generateGUID()
   * ---------------------------------------------------------------------- */

  void generateGUID(GUID *result) {
    GUID temp = { 0x4ec3e0ed, 0x3cb2, 0x4f1a, 0x81, 0x85, 0x73, 0xfa, 0xdc, 0xc5, 0x75, guid_counter++ };
    *result = temp;
  }

  /* ----------------------------------------------------------------------
   * formatGUID()
   * ---------------------------------------------------------------------- */

  void formatGUID(File *output, GUID *input, bool cFormat) {
    if (cFormat) {
      Printf(output,
          "{ 0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x}}",
          input->Data1, input->Data2, input->Data3, input->Data4[0], input->Data4[1],
          input->Data4[2], input->Data4[3], input->Data4[4], input->Data4[5], input->Data4[6], input->Data4[7]);
    } else {
      Printf(output,
          "%08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X",
          input->Data1, input->Data2, input->Data3, (((int) input->Data4[0]) << 8) | input->Data4[1],
          input->Data4[2], input->Data4[3], input->Data4[4], input->Data4[5], input->Data4[6], input->Data4[7]);
    }
  }

  /* ----------------------------------------------------------------------
   * classHandler()
   * ---------------------------------------------------------------------- */

  virtual int classHandler(Node *n) {
    if (proxy_flag) {
      proxy_class_name = NewString(Getattr(n, "sym:name"));
      List *bases = NULL;

      if (!addSymbol(proxy_class_name, n))
	return SWIG_ERROR;

/* FIXME */
#if 0
      if (Cmp(proxy_class_name, imclass_name) == 0) {
	Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name);
	SWIG_exit(EXIT_FAILURE);
      }
#endif

      if (Cmp(proxy_class_name, module_class_name) == 0) {
	Printf(stderr, "Class name cannot be equal to module class name: %s\n", proxy_class_name);
	SWIG_exit(EXIT_FAILURE);
      }

      Clear(proxy_class_def);
      Clear(proxy_class_code);
      Clear(proxy_class_forward_def);
      Clear(proxy_class_vtable_code);
      Clear(proxy_class_vtable_defs);

      proxy_iid = new GUID;
      generateGUID(proxy_iid);
      Setattr(n, "wrap:iid", proxy_iid);

      Printf(proxy_class_vtable_code, "GUID IID_%s = ", proxy_class_name);
      formatGUID(proxy_class_vtable_code, proxy_iid, true);
      Printf(proxy_class_vtable_code, ";\n\n");

      if (!Getattr(n, "abstract")) {
        /* Generate class object */
        proxy_clsid = new GUID;
        generateGUID(proxy_clsid);

        Printf(proxy_class_vtable_code, "GUID CLSID_%s = ", proxy_class_name);
        formatGUID(proxy_class_vtable_code, proxy_clsid, true);
        Printf(proxy_class_vtable_code, ";\n\n");

        Printf(clsid_list, "  { (SWIG_funcptr) _wrap_new_%s, &CLSID_%s, _T(\"{", proxy_class_name, proxy_class_name);
        formatGUID(clsid_list, proxy_clsid, false);
        Printf(clsid_list,  "}\") },\n");
      }

      Printf(proxy_class_vtable_code, "HRESULT SWIGSTDCALL _wrap%sQueryInterface1(void *that, GUID *iid, "
          "void ** ppvObject) {\n", proxy_class_name);

      /* Look if the requested interface is ISWIGWrappedObject */
      Printf(proxy_class_vtable_code,
          "  if (SWIGIsEqual(iid, &IID_ISWIGWrappedObject)) {\n"
          "    /* FIXME: This could be more elegant */\n"
          "    SWIGAddRef1(that);\n"
          /* Address of current object, incremented by the size of a pointer */
          "    *ppvObject = (void *) ((void **)that + 1);\n"
          "    return S_OK;\n"
          "  }\n\n");

      Printf(proxy_class_vtable_code, "  if (SWIGIsEqual(iid, &IID_IUnknown) ||\n"
        "      SWIGIsEqual(iid, &IID_IDispatch) ||\n"
        "      SWIGIsEqual(iid, &IID_%s)", proxy_class_name);

      bases = Getattr(n, "bases");

      /* Iterate through the ancestors */
      while (bases) {
        Iterator base = First(bases);

        Printf(proxy_class_vtable_code, " ||\n      SWIGIsEqual(iid, &IID_%s)", Getattr(base.item, "sym:name"));

        /* Get next base */
        bases = Getattr(base.item, "bases");
      }

      Printf(proxy_class_vtable_code, ") {\n"
          "    /* FIXME: This could be more elegant */\n"
          "    SWIGAddRef1(that);\n"
          "    *ppvObject = that;\n"
          "    return S_OK;\n"
          "  }\n\n");

      Printf(proxy_class_vtable_code, "  return E_NOINTERFACE;\n}\n\n");

      bases = NULL;

      Printf(proxy_class_vtable_code, "HRESULT SWIGSTDCALL _wrap%sQueryInterface2(void *that, GUID *iid, "
          "void ** ppvObject) {\n", proxy_class_name);

      Printf(proxy_class_vtable_code,
          "  return _wrap%sQueryInterface1((void *) ((void **) that - 1), iid, ppvObject);\n", proxy_class_name);

      Printf(proxy_class_vtable_code, "}\n\n");

      Printf(proxy_class_vtable_code, "SWIG_funcptr _wrap%sSWIGWrappedObject_vtable[] = "
          "{\n  (SWIG_funcptr) _wrap%sQueryInterface2,"
          "\n  (SWIG_funcptr) SWIGAddRef2,"
          "\n  (SWIG_funcptr) SWIGRelease2,"
          "\n  (SWIG_funcptr) SWIGGetCPtr"
          "\n};\n\n",
          proxy_class_name, proxy_class_name);

      Printf(proxy_class_vtable_code, "SWIG_funcptr _wrap%svtable[] = "
          "{\n  (SWIG_funcptr) _wrap%sQueryInterface1,"
          "\n  (SWIG_funcptr) SWIGAddRef1,"
          "\n  (SWIG_funcptr) SWIGRelease1,"
          "\n  (SWIG_funcptr) SWIGGetTypeInfoCount,"
          "\n  (SWIG_funcptr) SWIGGetTypeInfo,"
          "\n  (SWIG_funcptr) SWIGGetIDsOfNames,"
          "\n  (SWIG_funcptr) SWIGInvoke",
          proxy_class_name, proxy_class_name);

      bases = Getattr(n, "bases");

      if (!bases) {
        proxy_class_member_functions = NewList();
        Setattr(n, "wrap:member_functions", proxy_class_member_functions);
      } else {
        Iterator base = First(bases);

        List *base_member_functions = Getattr(base.item, "wrap:member_functions");
        Setattr(n, "wrap:member_functions", Copy(base_member_functions));

        for (Iterator func = First(base_member_functions); func.item; func = Next(func)) {
          Printf(proxy_class_vtable_code, ",\n  (SWIG_funcptr)%s", func.item);
        }
      }

      // FIXME: destructor_call = NewString("");
      proxy_class_constants_code = NewString("");
    }

    Language::classHandler(n);

    if (proxy_flag) {

      emitProxyClassDefAndCPPCasts(n);

      Replaceall(proxy_class_def, "$module", module_class_name);
      Replaceall(proxy_class_code, "$module", module_class_name);
      Replaceall(proxy_class_constants_code, "$module", module_class_name);
      // FIXME: Replaceall(proxy_class_def, "$imclassname", imclass_name);
      // FIXME: Replaceall(proxy_class_code, "$imclassname", imclass_name);
      // FIXME: Replaceall(proxy_class_constants_code, "$imclassname", imclass_name);
      // FIXME: Replaceall(proxy_class_def, "$dllimport", dllimport);
      // FIXME: Replaceall(proxy_class_code, "$dllimport", dllimport);
      // FIXME: Replaceall(proxy_class_constants_code, "$dllimport", dllimport);

      Printv(f_proxy_forward_defs, proxy_class_forward_def, NIL);
      Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);

      // Write out all the constants
      if (Len(proxy_class_constants_code) != 0)
	Printv(f_proxy, proxy_class_constants_code, NIL);

      Printf(f_proxy, "  };\n\n");

      Printv(proxy_class_vtable_code, "\n};\n\n", NIL);

      Printf(proxy_class_vtable_code, "void * SWIGSTDCALL SWIG_wrap%s(void *arg, int cMemOwn) {\n"
          "#ifdef __cplusplus\n"
          "  SWIGWrappedObject *res = new SWIGWrappedObject;\n"
          "#else\n"
          "  SWIGWrappedObject *res = (SWIGWrappedObject *) malloc(sizeof(SWIGWrappedObject));\n"
          "#endif\n"
          "  res->vtable = _wrap%svtable;\n"
          "  res->SWIGWrappedObject_vtable = _wrap%sSWIGWrappedObject_vtable;\n"
          "  res->cPtr = arg;\n"
          "  res->cMemOwn = cMemOwn;\n"
          "  res->refCount = 0;\n"
          "  res->deleteInstance = _wrap_delete_%s;\n"
          "  /* GetTypeInfoOfGuid */\n"
          "  ((HRESULT (SWIGSTDCALL *)(ITypeLib *, GUID *, ITypeInfo **)) (((SWIGIUnknown *) SWIG_typelib)->vtable[6]))(SWIG_typelib, &IID_%s, &res->typeInfo);\n"
          "  return (void *) res;\n"
          "}\n\n",
          proxy_class_name, proxy_class_name, proxy_class_name, proxy_class_name, proxy_class_name);

      Printf(proxy_class_vtable_defs, "void * SWIGSTDCALL SWIG_wrap%s(void *arg, int cMemOwn);\n", proxy_class_name);

      Printv(f_vtables, proxy_class_vtable_code, NIL);
      Printv(f_vtable_defs, proxy_class_vtable_defs, NIL);

      Delete(proxy_class_name);
      proxy_class_name = NULL;
      Delete(proxy_class_constants_code);
      proxy_class_constants_code = NULL;
      delete proxy_iid;
    }

    return SWIG_OK;
  }

  /* ----------------------------------------------------------------------
   * staticmemberfunctionHandler()
   * ---------------------------------------------------------------------- */

  virtual int staticmemberfunctionHandler(Node *n) {

    static_flag = true;
    member_func_flag = true;
    Language::staticmemberfunctionHandler(n);

    if (proxy_flag) {
      // FIXME: String *overloaded_name = getOverloadedName(n);
      String *overloaded_name = Getattr(n, "sym:name");
      String *intermediary_function_name = Swig_name_member(proxy_class_name, overloaded_name);
      Setattr(n, "proxyfuncname", Getattr(n, "sym:name"));
      Setattr(n, "imfuncname", intermediary_function_name);
      proxyClassFunctionHandler(n);
      Delete(overloaded_name);
    }
    static_flag = false;
    member_func_flag = false;

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * proxyClassFunctionHandler()
   *
   * Function called for creating a COM wrapper function around a c++ function in the 
   * proxy class. Used for both static and non-static C++ class functions.
   * C++ class static functions map to member functions of the objects and/or to
   * member functions of the module class (TODO).
   * ----------------------------------------------------------------------------- */

  void proxyClassFunctionHandler(Node *n) {
    SwigType *t = Getattr(n, "type");
    ParmList *l = Getattr(n, "parms");
    String *intermediary_function_name = Getattr(n, "imfuncname");
    String *proxy_function_name = Getattr(n, "proxyfuncname");
    String *tm;
    Parm *p;
    int i;
    String *return_type = NewString("");
    String *function_code = NewString("");
    bool setter_flag = false;
    String *pre_code = NewString("");
    String *post_code = NewString("");

    if (!proxy_flag)
      return;

    // Wrappers not wanted for some methods where the parameters cannot be overloaded in COM
    if (Getattr(n, "overload:ignore"))
      return;

    // Don't generate proxy method for additional explicitcall method used in directors
    if (GetFlag(n, "explicitcall"))
      return;

    if (!Getattr(n, "override")) {
      String *wname = Getattr(n, "wrap:name");
      Printf(proxy_class_vtable_code, ",\n  (SWIG_funcptr)%s", wname);
      Append(proxy_class_member_functions, wname);
    }

    if (l) {
      if (SwigType_type(Getattr(l, "type")) == T_VOID) {
	l = nextSibling(l);
      }
    }

    /* Attach the non-standard typemaps to the parameter list */
    Swig_typemap_attach_parms("in", l, NULL);
    Swig_typemap_attach_parms("comtype", l, NULL);
    // FIXME: Swig_typemap_attach_parms("javain", l, NULL);

    /* Get return types */
    if ((tm = Swig_typemap_lookup("comtype", n, "", 0))) {
      // Note that in the case of polymorphic (covariant) return types, the method's return type is changed to be the base of the C++ return type
      SwigType *covariant = Getattr(n, "covariant");
      substituteClassname(covariant ? covariant : t, tm);
      Printf(return_type, "%s", tm);
      if (covariant)
	Swig_warning(WARN_COM_COVARIANT_RET, input_file, line_number,
		     "Covariant return types not supported in COM. Proxy method will return %s.\n", SwigType_str(covariant, 0));
    } else {
      Swig_warning(WARN_COM_TYPEMAP_COMTYPE_UNDEF, input_file, line_number, "No comstype typemap defined for %s\n", SwigType_str(t, 0));
    }

    if (wrapping_member_flag && !enum_constant_flag) {
      // Properties
      setter_flag = (Cmp(Getattr(n, "sym:name"), Swig_name_set(Swig_name_member(proxy_class_name, variable_name))) == 0);
      
      if (setter_flag) {
        Printf(function_code, "    [ propput ]\n");
      } else {
        Printf(function_code, "    [ propget ]\n");
      }
    }


    /* Start generating the proxy function */
    Printf(function_code, "    %s %s(", return_type, proxy_function_name);

    emit_mark_varargs(l);

    // FIXME: int gencomma = !static_flag;
    int gencomma = 1;

    /* Output each parameter */
    for (i = 0, p = l; p; i++) {

      /* Ignored varargs */
      if (checkAttribute(p, "varargs:ignore", "1")) {
	p = nextSibling(p);
	continue;
      }

      /* Ignored parameters */
      if (checkAttribute(p, "tmap:in:numinputs", "0")) {
	p = Getattr(p, "tmap:in:next");
	continue;
      }

      /* Ignore the 'this' argument for variable wrappers */
      if (!(variable_wrapper_flag && i == 0) || static_flag) {
	SwigType *pt = Getattr(p, "type");
	String *param_type = NewString("");

	/* Get the COM parameter type */
	if ((tm = Getattr(p, "tmap:comtype"))) {
	  substituteClassname(pt, tm);
	  Printf(param_type, "%s", tm);
	} else {
	  Swig_warning(WARN_COM_TYPEMAP_COMTYPE_UNDEF, input_file, line_number, "No comtype typemap defined for %s\n", SwigType_str(pt, 0));
	}

	// FIXME: String *arg = makeParameterName(n, p, i, setter_flag);
        String *arg = NewStringf("arg%d", i);

	/* Add parameter to proxy function */
	if (gencomma >= 2)
	  Printf(function_code, ", ");
	gencomma = 2;
	Printf(function_code, "%s %s", param_type, arg);

	Delete(arg);
	Delete(param_type);
      }
      p = Getattr(p, "tmap:in:next");
    }

    Printf(function_code, ")");

    // FIXME: generateThrowsClause(n, function_code);
    Printv(function_code, ";\n", NIL);

    if (!Getattr(n, "override")) {
      Printv(proxy_class_code, function_code, NIL);
    }

    Delete(pre_code);
    Delete(post_code);
    Delete(function_code);
    Delete(return_type);
  }

  /* -----------------------------------------------------------------------------
   * emitTypeWrapperClass()
   * ----------------------------------------------------------------------------- */

  void emitTypeWrapperClass(String *classname, SwigType *type) {
    Clear(proxy_class_def);
    Clear(proxy_class_forward_def);

    proxy_iid = new GUID;
    generateGUID(proxy_iid);

    Printv(proxy_class_forward_def, "  interface $comclassname;\n", NIL);

    Printv(proxy_class_def, "  [\n    object,\n    local,\n    uuid(", NIL);
    formatGUID(proxy_class_def, proxy_iid, false);
    Printv(proxy_class_def, ")\n  ]\n  interface $comclassname {\n",
           typemapLookup("combody", type, WARN_COM_TYPEMAP_COMBODY_UNDEF),
	   typemapLookup("comcode", type, WARN_NONE),
           "  };\n\n", NIL);

    Replaceall(proxy_class_forward_def, "$comclassname", classname);
    Replaceall(proxy_class_def, "$comclassname", classname);
    Replaceall(proxy_class_forward_def, "$module", module_class_name);
    Replaceall(proxy_class_def, "$module", module_class_name);

    Printf(f_vtable_defs, "void * (SWIGSTDCALL * SWIG_wrap%s)(void *) = SWIG_wrap_opaque;\n", classname);

    Printv(f_proxy_forward_defs, proxy_class_forward_def, NIL);
    Printv(f_proxy, proxy_class_def, NIL);

    delete proxy_iid;
  }

  /* -----------------------------------------------------------------------------
   * typemapLookup()
   * ----------------------------------------------------------------------------- */

  const String *typemapLookup(const String *op, String *type, int warning, Node *typemap_attributes = NULL) {
    String *tm = NULL;
    const String *code = NULL;

    if ((tm = Swig_typemap_search(op, type, NULL, NULL))) {
      code = Getattr(tm, "code");
      if (typemap_attributes)
	Swig_typemap_attach_kwargs(tm, op, typemap_attributes);
    }

    if (!code) {
      code = empty_string;
      if (warning != WARN_NONE)
	Swig_warning(warning, input_file, line_number, "No %s typemap defined for %s\n", op, type);
    }

    return code ? code : empty_string;
  }

  /* -----------------------------------------------------------------------------
   * substituteClassname()
   *
   * Substitute $comclassname with the proxy class name for classes/structs/unions that SWIG knows about.
   * Also substitutes enums with enum name.
   * Otherwise use the $descriptor name for the COM class name. Note that the $&comclassname substitution
   * is the same as a $&descriptor substitution, ie one pointer added to descriptor name.
   * Inputs:
   *   pt - parameter type
   *   tm - comtype typemap
   * Outputs:
   *   tm - comtype typemap with $comclassname substitution
   * Return:
   *   substitution_performed - flag indicating if a substitution was performed
   * ----------------------------------------------------------------------------- */

  bool substituteClassname(SwigType *pt, String *tm) {
    bool substitution_performed = false;
    SwigType *type = Copy(SwigType_typedef_resolve_all(pt));
    SwigType *strippedtype = SwigType_strip_qualifiers(type);

    if (Strstr(tm, "$comclassname")) {
      SwigType *classnametype = Copy(strippedtype);
      substituteClassnameSpecialVariable(classnametype, tm, "$comclassname");
      substitution_performed = true;
      Delete(classnametype);
    }
    if (Strstr(tm, "$*comclassname")) {
      SwigType *classnametype = Copy(strippedtype);
      Delete(SwigType_pop(classnametype));
      substituteClassnameSpecialVariable(classnametype, tm, "$*comclassname");
      substitution_performed = true;
      Delete(classnametype);
    }
    if (Strstr(tm, "$&comclassname")) {
      SwigType *classnametype = Copy(strippedtype);
      SwigType_add_pointer(classnametype);
      substituteClassnameSpecialVariable(classnametype, tm, "$&comclassname");
      substitution_performed = true;
      Delete(classnametype);
    }

    Delete(strippedtype);
    Delete(type);

    return substitution_performed;
  }

  /* -----------------------------------------------------------------------------
   * substituteClassnameSpecialVariable()
   * ----------------------------------------------------------------------------- */

  void substituteClassnameSpecialVariable(SwigType *classnametype, String *tm, const char *classnamespecialvariable) {
    if (SwigType_isenum(classnametype)) {
      // FIXME: String *enumname = getEnumName(classnametype);
      String *enumname = classnametype;
      if (enumname)
	Replaceall(tm, classnamespecialvariable, enumname);
      else
	Replaceall(tm, classnamespecialvariable, NewStringf("int"));
    } else {
      String *classname = getProxyName(classnametype);
      if (classname) {
	Replaceall(tm, classnamespecialvariable, classname);	// getProxyName() works for pointers to classes too
      } else {			// use $descriptor if SWIG does not know anything about this type. Note that any typedefs are resolved.
	String *descriptor = NewStringf("SWIGTYPE%s", SwigType_manglestr(classnametype));
	Replaceall(tm, classnamespecialvariable, descriptor);

	// Add to hash table so that the type wrapper classes can be created later
	Setattr(swig_types_hash, descriptor, classnametype);
	Delete(descriptor);
      }
    }
  }


};				/* class COM */

/* -----------------------------------------------------------------------------
 * swig_com()    - Instantiate module
 * ----------------------------------------------------------------------------- */

static Language *new_swig_com() {
  return new COM();
}
extern "C" Language *swig_com(void) {
  return new_swig_com();
}

/* -----------------------------------------------------------------------------
 * Static member variables
 * ----------------------------------------------------------------------------- */

const char *COM::usage = (char *) "\
COM Options (available with -com)\n\
     -norcfile       - Do not generate RC (resource definition) file\n\
     -nodeffile      - Do not generate DEF file\n\
     -nodllexports   - Do not generate DllGetClassObject and DllCanUnloadNow\n\
                       (implicates -nodeffile)\n\
\n";