aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/games/gluegen/JavaEmitter.java
blob: 46d976b23ea9be0364d38ca0260f09e469c0112b (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
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package net.java.games.gluegen;

import java.io.*;
import java.util.*;
import java.text.MessageFormat;

import net.java.games.gluegen.cgram.types.*;

// PROBLEMS:
//  - what if something returns 'const int *'? Could we
//    return an IntBuffer that has read-only behavior? Or do we copy the array
//    (but we don't know its size!). What do we do if it returns a non-const
//    int*? Should the user be allowed to write back to the returned pointer?
//
//  - Non-const array types must be properly released with JNI_COMMIT
//    in order to see side effects if the array was copied.


public class JavaEmitter implements GlueEmitter {
  private StructLayout layout;
  private TypeDictionary typedefDictionary;
  private TypeDictionary structDictionary;
  private Map            canonMap;
  private JavaConfiguration cfg;

  /**
   * Style of code emission. Can emit everything into one class
   * (AllStatic), separate interface and implementing classes
   * (InterfaceAndImpl), only the interface (InterfaceOnly), or only
   * the implementation (ImplOnly).
   */
  static final int ALL_STATIC = 1;
  static final int INTERFACE_AND_IMPL = 2;
  static final int INTERFACE_ONLY = 3;
  static final int IMPL_ONLY = 4;

  private PrintWriter javaWriter; // Emits either interface or, in AllStatic mode, everything
  private PrintWriter javaImplWriter; // Only used in non-AllStatic modes for impl class
  private PrintWriter cWriter;
  private MachineDescription machDesc;
  
  public void readConfigurationFile(String filename) throws Exception {
    cfg = createConfig();
    cfg.read(filename);
  }

  public void setMachineDescription(MachineDescription md) {
    machDesc = md;
  }

  public void beginEmission(GlueEmitterControls controls) throws IOException
  {
    try
    {
      openWriters();
    }
    catch (Exception e)
    {
      throw new RuntimeException(
        "Unable to open files for writing", e);
    }
    
    emitAllFileHeaders();

    // Request emission of any structs requested
    for (Iterator iter = cfg.forcedStructs().iterator(); iter.hasNext(); ) {
      controls.forceStructEmission((String) iter.next());
    }
  }

  public void endEmission()
  {
    emitAllFileFooters();

    try
    {
      closeWriters();
    }
    catch (Exception e)
    {
      throw new RuntimeException(
        "Unable to close open files", e);
    }
  }

  public void beginDefines() throws Exception
  {
    if (cfg.allStatic() || cfg.emitInterface()) {
      javaWriter().println();
    }
  }

  public void emitDefine(String name, String value, String optionalComment) throws Exception
  {
    if (cfg.allStatic() || cfg.emitInterface()) {
      // TODO: Some defines (e.g., GL_DOUBLE_EXT in gl.h) are defined in terms
      // of other defines -- should we emit them as references to the original
      // define (not even sure if the lexer supports this)? Right now they're
      // emitted as the numeric value of the original definition. If we decide
      // emit them as references we'll also have to emit them in the correct
      // order. It's probably not an issue right now because the emitter
      // currently only emits only numeric defines -- if it handled #define'd
      // objects it would make a bigger difference.
 
      if (!cfg.shouldIgnore(name)) {
        String type = null;

        // FIXME: need to handle when type specifier is in last char (e.g.,
        // "1.0d or 2759L", because parseXXX() methods don't allow the type
        // specifier character in the string.
        //
        //char lastChar = value.charAt(value.length()-1);
        
        try {
          // see if it's a long or int
          int radix;
          String parseValue;
          // FIXME: are you allowed to specify hex/octal constants with
          // negation, e.g. "-0xFF" or "-056"? If so, need to modify the
          // following "if(..)" checks and parseValue computation
          if (value.startsWith("0x") || value.startsWith("0X")) {
            radix = 16;
            parseValue = value.substring(2);
          }
          else if (value.startsWith("0") && value.length() > 1) {
            // TODO: is "0" the prefix in C to indicate octal???
            radix = 8; 
            parseValue = value.substring(1);
          }
          else {
            radix = 10;
            parseValue = value;
          }
          //System.err.println("parsing " + value + " as long w/ radix " + radix);
          long longVal = Long.parseLong(parseValue, radix);
          type = "long";
          // if constant is small enough, store it as an int instead of a long
          if (longVal > Integer.MIN_VALUE && longVal < Integer.MAX_VALUE) {
            type = "int";
          }
          
        } catch (NumberFormatException e) {
          try {
            // see if it's a double or float
            double dVal = Double.parseDouble(value);
            type = "double";
            // if constant is small enough, store it as a float instead of a double
            if (dVal > Float.MIN_VALUE && dVal < Float.MAX_VALUE) {
              type = "float";
            }
            
          } catch (NumberFormatException e2) {            
            throw new RuntimeException(
              "Cannot emit define \""+name+"\": value \""+value+
              "\" cannot be assigned to a int, long, float, or double", e2);
          }
        }

        if (type == null) {
            throw new RuntimeException(
              "Cannot emit define (2) \""+name+"\": value \""+value+
              "\" cannot be assigned to a int, long, float, or double");
        }
        if (optionalComment != null && optionalComment.length() != 0) {
          javaWriter().println("  /** " + optionalComment + " */");
        }
        javaWriter().println("  public static final " + type + " " + name + " = " + value + ";");
      }
    }
  }

  public void endDefines() throws Exception
  {
  }

  public void beginFunctions(TypeDictionary typedefDictionary,
                             TypeDictionary structDictionary,
                             Map            canonMap) throws Exception {
    this.typedefDictionary = typedefDictionary;
    this.structDictionary  = structDictionary;
    this.canonMap          = canonMap;
    if (cfg.allStatic() || cfg.emitInterface()) {
      javaWriter().println();
    }
  }

  public Iterator emitFunctions(List/*<FunctionSymbol>*/ originalCFunctions)
    throws Exception {
    // Sometimes headers will have the same function prototype twice, once
    // with the argument names and once without. We'll remember the signatures
    // we've already processed we don't generate duplicate bindings.
    //
    // Note: this code assumes that on the equals() method in FunctionSymbol
    // only considers function name and argument types (i.e., it does not
    // consider argument *names*) when comparing FunctionSymbols for equality    
    Set funcsToBindSet = new HashSet(100);
    for (Iterator cIter = originalCFunctions.iterator(); cIter.hasNext(); ) {
      FunctionSymbol cFunc = (FunctionSymbol) cIter.next();
      if (!funcsToBindSet.contains(cFunc)) {
        funcsToBindSet.add(cFunc);
      }
    }

    ArrayList funcsToBind = new ArrayList(funcsToBindSet.size());
    funcsToBind.addAll(funcsToBindSet);
    // sort functions to make them easier to find in native code
    Collections.sort(
      funcsToBind,
      new Comparator() {
          public int compare(Object o1, Object o2) {
            return ((FunctionSymbol)o1).getName().compareTo(
              ((FunctionSymbol)o2).getName());
          }
          public boolean equals(Object obj) {
            return obj.getClass() == this.getClass();
          }
        });

    // Bind all the C funcs to Java methods
    ArrayList/*<FunctionEmitter>*/ methodBindingEmitters = new ArrayList(2*funcsToBind.size());
    for (Iterator iter = funcsToBind.iterator(); iter.hasNext(); ) {
      FunctionSymbol cFunc = (FunctionSymbol) iter.next();
      // Check to see whether this function should be ignored
      if (cfg.shouldIgnore(cFunc.getName())) {
        continue; // don't generate bindings for this symbol
      }
      
      Iterator allBindings = generateMethodBindingEmitters(cFunc);
      while (allBindings.hasNext()) {
        methodBindingEmitters.add(allBindings.next());
      }
    }

    // Emit all the methods
    for (int i = 0; i < methodBindingEmitters.size(); ++i) {
      FunctionEmitter emitter = (FunctionEmitter)methodBindingEmitters.get(i);      
      try {
        emitter.emit();
      } catch (Exception e) {
        throw new RuntimeException(
            "Error while emitting binding for \"" + emitter.getName() + "\"", e);
      }
      emitter.getDefaultOutput().println(); // put newline after method body
    }

    // Return the list of FunctionSymbols that we generated gluecode for
    return funcsToBind.iterator();
  }

  /**
   * Create the object that will read and store configuration information for
   * this JavaEmitter.
   */
  protected JavaConfiguration createConfig() {
    return new JavaConfiguration();
  }

  /**
   * Get the configuration information for this JavaEmitter.
   */
  protected JavaConfiguration getConfig() {
    return cfg;
  }

  /**
   * Generate all appropriate Java bindings for the specified C function
   * symbols.
   */
  protected Iterator generateMethodBindingEmitters(FunctionSymbol sym) throws Exception {

    ArrayList/*<FunctionEmitter>*/ allEmitters = new ArrayList(1);

    try {
      // Get Java binding for the function
      MethodBinding mb = bindFunction(sym, null, null);
      
      // Expand all void* arguments
      List bindings = expandMethodBinding(mb);
      boolean overloaded = (bindings.size() > 1);
      if (overloaded) {
        // resize ahead of time for speed
        allEmitters.ensureCapacity(bindings.size());
      }
      
      // List of the indices of the arguments in this function that should be
      // expanded to the same type when binding functions with multiple void*
      // arguments
      List mirrorIdxs = cfg.mirroredArgs(sym.getName());
      
      for (Iterator iter = bindings.iterator(); iter.hasNext(); ) {
        MethodBinding binding = (MethodBinding) iter.next();        

        // Honor the MirrorExpandedBindingArgs directive in .cfg files
        if (mirrorIdxs != null) {
          assert(mirrorIdxs.size() >= 2); // sanity check.
          boolean typesMatch = true;
          int argIndex = ((Integer)mirrorIdxs.get(0)).intValue();
          JavaType leftArgType = binding.getJavaArgumentType(argIndex);
          for (int i = 1; i < mirrorIdxs.size(); ++i) {
            argIndex = ((Integer)mirrorIdxs.get(i)).intValue();
            JavaType rightArgType = binding.getJavaArgumentType(argIndex);
            if (!(leftArgType.equals(rightArgType))) {
              typesMatch = false;
              break;
            }
            leftArgType = rightArgType;
          }
          // Don't emit the binding if the specified args aren't the same type
          if (!typesMatch) { continue; } // skip this binding
        }

        // Try to create an NIOBuffer variant for this expanded binding. If
        // it's the same as the original binding, then we'll be able to emit
        // the binding like any normal binding because no special binding
        // generation (wrapper methods, etc) will be necessary.
        MethodBinding specialBinding = binding.createNIOBufferVariant();     
        
        if (cfg.allStatic() && binding.hasContainingType()) {
          // This should not currently happen since structs are emitted using a different mechanism
          throw new IllegalArgumentException("Cannot create binding in AllStatic mode because method has containing type: \"" +
                                             binding + "\"");
        }

        boolean isUnimplemented = cfg.isUnimplemented(binding.getName());
        
        if (cfg.emitImpl()) {
          // Generate the emitter for the method which may do conversion
          // from type wrappers to NIO Buffers or which may call the
          // underlying function directly
          JavaMethodBindingImplEmitter entryPoint =
            new JavaMethodBindingImplEmitter(binding,
                                             (cfg.allStatic() ? javaWriter() : javaImplWriter()),
                                             cfg.runtimeExceptionType(),
                                             isUnimplemented);
          entryPoint.addModifier(JavaMethodBindingEmitter.PUBLIC);
          if (cfg.allStatic()) {
            entryPoint.addModifier(JavaMethodBindingEmitter.STATIC);
          }
          if (!isUnimplemented && !binding.needsBody()) {
            entryPoint.addModifier(JavaMethodBindingEmitter.NATIVE);
          }
          entryPoint.setReturnedArrayLengthExpression(cfg.returnedArrayLength(binding.getName()));
          allEmitters.add(entryPoint);
        }

        if (cfg.emitInterface()) {
          // Generate an emitter that will emit just the interface to the function
          JavaMethodBindingEmitter entryPointInterface =
            new JavaMethodBindingEmitter(binding, javaWriter(), cfg.runtimeExceptionType());
          entryPointInterface.addModifier(JavaMethodBindingEmitter.PUBLIC);
          entryPointInterface.setReturnedArrayLengthExpression(cfg.returnedArrayLength(binding.getName()));
          allEmitters.add(entryPointInterface);           
        }

        if (cfg.emitImpl()) {
          // If the user has stated that the function will be
          // manually implemented, then don't auto-generate a function body.
          if (!cfg.manuallyImplement(sym.getName()) && !isUnimplemented) {
            if (binding.needsBody()) {
              // Generate the method which calls the underlying function
              // after unboxing has occurred
              PrintWriter output = cfg.allStatic() ? javaWriter() : javaImplWriter();
              JavaMethodBindingEmitter wrappedEntryPoint =
                new JavaMethodBindingEmitter(specialBinding, output, cfg.runtimeExceptionType(), true);
              wrappedEntryPoint.addModifier(JavaMethodBindingEmitter.PRIVATE);
              wrappedEntryPoint.addModifier(JavaMethodBindingEmitter.STATIC); // Doesn't really matter
              wrappedEntryPoint.addModifier(JavaMethodBindingEmitter.NATIVE);
              allEmitters.add(wrappedEntryPoint); 
            }
        
            CMethodBindingEmitter cEmitter =
              makeCEmitter(specialBinding, 
                           overloaded,
                           (binding != specialBinding),
                           cfg.implPackageName(), cfg.implClassName(),
                           cWriter());
            allEmitters.add(cEmitter);
          }
        }
      } // end iteration over expanded bindings
    } catch (Exception e) {
      throw new RuntimeException(
        "Error while generating bindings for \"" + sym + "\"", e);
    }

    return allEmitters.iterator();
  }

    
  public void endFunctions() throws Exception
  {
    if (cfg.allStatic() || cfg.emitInterface()) {
      emitCustomJavaCode(javaWriter(), cfg.className());
    }
    if (!cfg.allStatic() && cfg.emitImpl()) {
      emitCustomJavaCode(javaImplWriter(), cfg.implClassName());
    }
  }

  public void beginStructLayout() throws Exception {}
  public void layoutStruct(CompoundType t) throws Exception {
    getLayout().layout(t);
  }
  public void endStructLayout() throws Exception {}

  public void beginStructs(TypeDictionary typedefDictionary,
                           TypeDictionary structDictionary,
                           Map            canonMap) throws Exception {
    this.typedefDictionary = typedefDictionary;
    this.structDictionary  = structDictionary;
    this.canonMap          = canonMap;
  }

  public void emitStruct(CompoundType structType, String alternateName) throws Exception {
    String name = structType.getName();
    if (name == null && alternateName != null) {
      name = alternateName;
    }

    if (name == null) {
      System.err.println("WARNING: skipping emission of unnamed struct \"" + structType + "\"");
      return;
    }

    if (cfg.shouldIgnore(name)) {
      return;
    }

    Type containingCType = canonicalize(new PointerType(machDesc.pointerSizeInBytes(), structType, 0));
    JavaType containingType = typeToJavaType(containingCType, false);
    if (!containingType.isCompoundTypeWrapper()) {
      return;
    }
    String containingTypeName = containingType.getName();

    boolean needsNativeCode = false;
    for (int i = 0; i < structType.getNumFields(); i++) {
      if (structType.getField(i).getType().isFunctionPointer()) {
        needsNativeCode = true;
        break;
      }
    }

    String structClassPkg = cfg.packageForStruct(name);
    PrintWriter writer = null;
    PrintWriter cWriter = null;
    try
    {
      writer = openFile(
        cfg.javaOutputDir() + File.separator +
        CodeGenUtils.packageAsPath(structClassPkg) +
        File.separator + containingTypeName + ".java");
      CodeGenUtils.emitAutogeneratedWarning(writer, this);
      if (needsNativeCode) {
        String nRoot = cfg.nativeOutputDir();
        if (cfg.nativeOutputUsesJavaHierarchy()) {
          nRoot +=
            File.separator +
            CodeGenUtils.packageAsPath(cfg.packageName());
        }
        cWriter = openFile(nRoot + File.separator + containingTypeName + "_JNI.c");
        CodeGenUtils.emitAutogeneratedWarning(cWriter, this);
        emitCHeader(cWriter, containingTypeName);
      }
    }
    catch(Exception e)
    {
      throw new RuntimeException(
        "Unable to open files for emission of struct class", e);
    }
    
    writer.println();
    writer.println("package " + structClassPkg + ";");
    writer.println();
    writer.println("import java.nio.*;");
    writer.println();
    writer.println("import net.java.games.gluegen.runtime.*;");
    writer.println();
    List/*<String>*/ imports = cfg.imports();
    for (Iterator iter = imports.iterator(); iter.hasNext(); ) {
      writer.print("import ");
      writer.print(iter.next());
      writer.println(";");
    }
    List/*<String>*/ javadoc = cfg.javadocForClass(containingTypeName);
    for (Iterator iter = javadoc.iterator(); iter.hasNext(); ) {
      writer.println((String) iter.next());
    }
    writer.println();
    writer.print("public class " + containingTypeName + " ");
    boolean firstIteration = true;
    List/*<String>*/ userSpecifiedInterfaces = cfg.implementedInterfaces(containingTypeName);
    for (Iterator iter = userSpecifiedInterfaces.iterator(); iter.hasNext(); ) {
      if (firstIteration) {
        writer.print("implements ");
      }
      firstIteration = false;
      writer.print(iter.next());
      writer.print(" ");
    }
    writer.println("{");
    writer.println("  private StructAccessor accessor;");
    writer.println();
    writer.println("  public static int size() {");
    writer.println("    return " + structType.getSize() + ";");
    writer.println("  }");
    writer.println();
    writer.println("  public " + containingTypeName + "() {");
    writer.println("    this(BufferFactory.newDirectByteBuffer(size()));");
    writer.println("  }");
    writer.println();
    writer.println("  public " + containingTypeName + "(ByteBuffer buf) {");
    writer.println("    accessor = new StructAccessor(buf);");
    writer.println("  }");
    writer.println();
    writer.println("  public ByteBuffer getBuffer() {");
    writer.println("    return accessor.getBuffer();");
    writer.println("  }");
    for (int i = 0; i < structType.getNumFields(); i++) {
      Field field = structType.getField(i);
      Type fieldType = field.getType();
      if (!cfg.shouldIgnore(name + " " + field.getName())) {
        if (fieldType.isFunctionPointer()) {
          try {
            // Emit method call and associated native code
            FunctionType   funcType     = fieldType.asPointer().getTargetType().asFunction();
            FunctionSymbol funcSym      = new FunctionSymbol(field.getName(), funcType);
            MethodBinding  binding      = bindFunction(funcSym, containingType, containingCType);
            binding.findThisPointer(); // FIXME: need to provide option to disable this on per-function basis
            MethodBinding  specialBinding    = binding.createNIOBufferVariant();
            writer.println();

            JavaMethodBindingEmitter entryPoint = new JavaMethodBindingImplEmitter(binding, writer, cfg.runtimeExceptionType());
            entryPoint.addModifier(JavaMethodBindingEmitter.PUBLIC);
            if (!binding.needsBody() && !binding.hasContainingType()) {
              entryPoint.addModifier(JavaMethodBindingEmitter.NATIVE);
            }
            entryPoint.emit();

            JavaMethodBindingEmitter wrappedEntryPoint = new JavaMethodBindingEmitter(specialBinding, writer, cfg.runtimeExceptionType(), true);
            wrappedEntryPoint.addModifier(JavaMethodBindingEmitter.PRIVATE);
            wrappedEntryPoint.addModifier(JavaMethodBindingEmitter.NATIVE);
            wrappedEntryPoint.emit();

            CMethodBindingEmitter cEmitter =
              makeCEmitter(specialBinding,
                           false, // overloaded
                           true, // doing impl routine?
                           structClassPkg,
                           containingTypeName,
                           cWriter);
            cEmitter.emit();
          } catch (Exception e) {
            System.err.println("While processing field " + field + " of type " + name + ":");
            throw(e);
          }
        } else if (fieldType.isCompound()) {
          // FIXME: will need to support this at least in order to
          // handle the union in jawt_Win32DrawingSurfaceInfo (fabricate
          // a name?)
          if (fieldType.getName() == null) {
            throw new RuntimeException("Anonymous structs as fields not supported yet (field \"" +
                                       field + "\" in type \"" + name + "\")");
          }
        
          writer.println();
          writer.println("  public " + fieldType.getName() + " " + field.getName() + "() {");
          writer.println("    return new " + fieldType.getName() + "(accessor.slice(" +
                         field.getOffset() + ", " + fieldType.getSize() + "));");
          writer.println("  }");

          // FIXME: add setter by autogenerating "copyTo" for all compound type wrappers
        } else if (fieldType.isArray()) {
          System.err.println("WARNING: Array fields (field \"" + field + "\" of type \"" + name +
                             "\") not implemented yet");
        } else {
          JavaType javaType = null;
          try {
            javaType = typeToJavaType(fieldType, false);
          } catch (Exception e) {
            System.err.println("Error occurred while creating accessor for field \"" +
                               field.getName() + "\" in type \"" + name + "\"");
            e.printStackTrace();
            throw(e);
          }
          if (javaType.isPrimitive()) {
            // Primitive type
            String externalJavaTypeName = javaType.getName();
            String internalJavaTypeName = externalJavaTypeName;
            if (isOpaque(fieldType)) {
              internalJavaTypeName = compatiblePrimitiveJavaTypeName(fieldType, javaType);
            }
            String capitalized =
              "" + Character.toUpperCase(internalJavaTypeName.charAt(0)) + internalJavaTypeName.substring(1);
            int slot = slot(fieldType, (int) field.getOffset());
            // Setter
            writer.println();
            writer.println("  public " + containingTypeName + " " + field.getName() + "(" + externalJavaTypeName + " val) {");
            writer.print  ("    accessor.set" + capitalized + "At(" + slot + ", ");
            if (!externalJavaTypeName.equals(internalJavaTypeName)) {
              writer.print("(" + internalJavaTypeName + ") ");
            }
            writer.println("val);");
            writer.println("    return this;");
            writer.println("  }");
            // Getter
            writer.println();
            writer.println("  public " + externalJavaTypeName + " " + field.getName() + "() {");
            writer.print  ("    return ");
            if (!externalJavaTypeName.equals(internalJavaTypeName)) {
              writer.print("(" + externalJavaTypeName + ") ");
            }
            writer.println("accessor.get" + capitalized + "At(" + slot + ");");
            writer.println("  }");
          } else {
            // FIXME
            System.err.println("WARNING: Complicated fields (field \"" + field + "\" of type \"" + name +
                               "\") not implemented yet");
            //          throw new RuntimeException("Complicated fields (field \"" + field + "\" of type \"" + t +
            //                                     "\") not implemented yet");
          }
        }
      }
    }
    emitCustomJavaCode(writer, containingTypeName);
    writer.println("}");
    writer.flush();
    writer.close();
    if (needsNativeCode) {
      cWriter.flush();
      cWriter.close();
    }
  }
  public void endStructs() throws Exception {}

  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private CMethodBindingEmitter makeCEmitter(MethodBinding binding,
                                             boolean overloaded,
                                             boolean doingImplRoutine,
                                             String bindingJavaPackageName,
                                             String bindingJavaClassName,
                                             PrintWriter output) {
    MessageFormat returnValueCapacityFormat = null;         
    MessageFormat returnValueLengthFormat = null;         
    JavaType javaReturnType = binding.getJavaReturnType();
    if (javaReturnType.isNIOBuffer()) {
      // See whether capacity has been specified
      String capacity = cfg.returnValueCapacity(binding.getName());
      if (capacity != null) {
        returnValueCapacityFormat = new MessageFormat(capacity);
      }
    } else if (javaReturnType.isArray()) {
      // See whether length has been specified
      String len = cfg.returnValueLength(binding.getName());
      if (len != null) {
        returnValueLengthFormat = new MessageFormat(len);
      }
    }
    CMethodBindingEmitter cEmitter;
    if (doingImplRoutine) {
      cEmitter = new CMethodBindingImplEmitter(binding, overloaded,
                                               bindingJavaPackageName,
                                               bindingJavaClassName,
                                               cfg.allStatic(), output);
    } else {
      cEmitter = new CMethodBindingEmitter(binding, overloaded,
                                           bindingJavaPackageName,
                                           bindingJavaClassName,
                                           cfg.allStatic(), output);
    }
    if (returnValueCapacityFormat != null) {
      cEmitter.setReturnValueCapacityExpression(returnValueCapacityFormat);
    }
    if (returnValueLengthFormat != null) {
      cEmitter.setReturnValueLengthExpression(returnValueLengthFormat);
    }
    cEmitter.setTemporaryCVariableDeclarations(cfg.temporaryCVariableDeclarations(binding.getName()));
    cEmitter.setTemporaryCVariableAssignments(cfg.temporaryCVariableAssignments(binding.getName()));
    return cEmitter;
  }

  private JavaType typeToJavaType(Type cType, boolean outgoingArgument) {
    // Recognize JNIEnv* case up front
    PointerType opt = cType.asPointer();
    if ((opt != null) &&
        (opt.getTargetType().getName() != null) &&
        (opt.getTargetType().getName().equals("JNIEnv"))) {
      return JavaType.createForJNIEnv();
    }

    // Opaque specifications override automatic conversions
    TypeInfo info = cfg.typeInfo(cType, typedefDictionary);
    if (info != null) {
      return info.javaType();
    }
    Type t = cType;
    if (t.isInt() || t.isEnum()) {
      switch (t.getSize()) {
       case 1:  return javaType(Byte.TYPE);
       case 2:  return javaType(Short.TYPE);
       case 4:  return javaType(Integer.TYPE);
       case 8:  return javaType(Long.TYPE);
       default: throw new RuntimeException("Unknown integer type of size " +
                                           t.getSize() + " and name " + t.getName());
      }
    } else if (t.isFloat()) {
      return javaType(Float.TYPE);
    } else if (t.isDouble()) {
      return javaType(Double.TYPE);
    } else if (t.isVoid()) {
      return javaType(Void.TYPE);
    } else {
      if (t.pointerDepth() > 0 || arrayDimension(t) > 0) {
        Type targetType; // target type 
        if (t.isPointer()) {
          // t is <type>*, we need to get <type>
          targetType = t.asPointer().getTargetType();
        } else {
          // t is <type>[], we need to get <type>
          targetType = t.asArray().getElementType(); 
        }

        // Handle Types of form pointer-to-type or array-of-type, like
        // char* or int[]; these are expanded out into Java primitive
        // arrays, NIO buffers, or both in expandMethodBinding
        if (t.pointerDepth() == 1 || arrayDimension(t) == 1) {
          if (targetType.isVoid()) {
            return JavaType.createForVoidPointer();
          } else if (targetType.isInt()) {
            switch (targetType.getSize()) {
              case 1:  return JavaType.createForCCharPointer();
              case 2:  return JavaType.createForCShortPointer();
              case 4:  return JavaType.createForCInt32Pointer();
              case 8:  return JavaType.createForCInt64Pointer();
              default: throw new RuntimeException("Unknown integer array type of size " +
                                                  t.getSize() + " and name " + t.getName());
            }
          } else if (targetType.isFloat()) {
            return JavaType.createForCFloatPointer();
          } else if (targetType.isDouble()) {
            return JavaType.createForCDoublePointer();
          } else if (targetType.isCompound()) {
            if (t.isArray()) {
              throw new RuntimeException("Arrays of compound types not handled yet");
            }
            // Special cases for known JNI types (in particular for converting jawt.h)
            if (t.getName() != null &&
                t.getName().equals("jobject")) {
              return javaType(java.lang.Object.class);
            }

            String name = targetType.getName();
            if (name == null) {
              // Try containing pointer type for any typedefs
              name = t.getName();
              if (name == null) {
                throw new RuntimeException("Couldn't find a proper type name for pointer type " + t);
              }
            }

            return JavaType.createForCStruct(cfg.renameJavaType(name));
          } else {
            throw new RuntimeException("Don't know how to convert pointer/array type \"" +
                                       t + "\"");
          }
        }
        // Handle Types of form pointer-to-pointer-to-type or
        // array-of-arrays-of-type, like char** or int[][]
        else if (t.pointerDepth() == 2 || arrayDimension(t) == 2) {
          // Get the target type of the target type (targetType was computer earlier
          // as to be a pointer to the target type, so now we need to get its
          // target type)
          Type bottomType;
          if (targetType.isPointer()) {
            // t is<type>**, targetType is <type>*, we need to get <type>
            bottomType = targetType.asPointer().getTargetType(); 
          } else {
            // t is<type>[][], targetType is <type>[], we need to get <type>
            bottomType = targetType.asArray().getElementType(); 
          }
          if (bottomType.isInt()) {
            switch (bottomType.getSize())
            {
              case 1:  return javaType(ArrayTypes.byteArrayArrayClass);
              // FIXME: handle 2,4,8-byte int types here
              default:
                throw new RuntimeException(
                  "Could not convert C type \"" + t + "\" to appropriate " +
                  "Java type; Currently, the only supported depth=2 " +
                  "pointer/array integer types are \"char**\" and \"char[][]\"");
            }
          } else if (targetType.isPointer() && (targetType.pointerDepth() == 1)) {
            // Array of pointers; convert as array of StructAccessors
            return JavaType.createForCArray(targetType);
          } else {
            throw new RuntimeException(
              "Could not convert C type \"" + t + "\" " +
              "to appropriate Java type; need to add more support for " +
              "depth=2 pointer/array types with non-integral target " +
              "types [debug info: targetType=\"" + targetType + "\"]");            
          }
                   
        } else {
          // can't handle this type of pointer/array argument
          throw new RuntimeException(
            "Could not convert C pointer/array \"" + t + "\" to " +
            "appropriate Java type; types with pointer/array depth " +
            "greater than 2 are not yet supported [debug info: " +
            "pointerDepth=" + t.pointerDepth() + " arrayDimension=" +
            arrayDimension(t) + " targetType=\"" + targetType + "\"]");
        }
        
      } else {
        throw new RuntimeException(
          "Could not convert C type \"" + t + "\" to appropriate Java type; ");
      }
    }    
  }

  private static boolean isIntegerType(Class c) {
    return ((c == Byte.TYPE) ||
            (c == Short.TYPE) ||
            (c == Character.TYPE) ||
            (c == Integer.TYPE) ||
            (c == Long.TYPE));
  }

  private int slot(Type t, int byteOffset) {
    if (t.isInt()) {
      switch (t.getSize()) {
       case 1:  
       case 2:  
       case 4:  
       case 8:  return byteOffset / t.getSize();
       default: throw new RuntimeException("Illegal type");
      }
    } else if (t.isFloat()) {
      return byteOffset / 4;
    } else if (t.isDouble()) {
      return byteOffset / 8;
    } else if (t.isPointer()) {
      return byteOffset / machDesc.pointerSizeInBytes();
    } else {
      throw new RuntimeException("Illegal type " + t);
    }
  }

  private StructLayout getLayout() {
    if (layout == null) {
      layout = StructLayout.createForCurrentPlatform();
    }
    return layout;
  }

  protected PrintWriter openFile(String filename) throws IOException {
    //System.out.println("Trying to open: " + filename);
    File file = new File(filename);
    String parentDir = file.getParent();
    if (parentDir != null)
    {
      File pDirFile = new File(parentDir);
      pDirFile.mkdirs();
    }
    return new PrintWriter(new BufferedWriter(new FileWriter(file)));
  }

  private int arrayDimension(Type type) {
    ArrayType arrayType = type.asArray();
    if (arrayType == null) {
      return 0;
    }
    return 1 + arrayDimension(arrayType.getElementType());
  }

  private boolean isOpaque(Type type) {
    return (cfg.typeInfo(type, typedefDictionary) != null);
  }

  private String compatiblePrimitiveJavaTypeName(Type fieldType,
                                                 JavaType javaType) {
    Class c = javaType.getJavaClass();
    if (!isIntegerType(c)) {
      // FIXME
      throw new RuntimeException("Can't yet handle opaque definitions of structs' fields to non-integer types (byte, short, int, long, etc.)");
    }
    switch (fieldType.getSize()) {
      case 1:  return "byte";
      case 2:  return "short";
      case 4:  return "int";
      case 8:  return "long";
      default: throw new RuntimeException("Can't handle opaque definitions if the starting type isn't compatible with integral types");
    }
  }

  private void openWriters() throws IOException {
    String jRoot =
      cfg.javaOutputDir() + File.separator +
      CodeGenUtils.packageAsPath(cfg.packageName());
    String jImplRoot = null;
    if (!cfg.allStatic()) {
      jImplRoot =
        cfg.javaOutputDir() + File.separator +
        CodeGenUtils.packageAsPath(cfg.implPackageName());
    }
    String nRoot = cfg.nativeOutputDir();
    if (cfg.nativeOutputUsesJavaHierarchy())
    {
      nRoot +=
        File.separator + CodeGenUtils.packageAsPath(cfg.packageName());
    }
    
    if (cfg.allStatic() || cfg.emitInterface()) {
      javaWriter = openFile(jRoot + File.separator + cfg.className() + ".java");
    }
    if (!cfg.allStatic() && cfg.emitImpl()) {
      javaImplWriter = openFile(jImplRoot + File.separator + cfg.implClassName() + ".java");
    }
    if (cfg.emitImpl()) {
      cWriter = openFile(nRoot + File.separator + cfg.implClassName() + "_JNI.c");
    }

    if (javaWriter != null) {
      CodeGenUtils.emitAutogeneratedWarning(javaWriter, this);
    }
    if (javaImplWriter != null) {
      CodeGenUtils.emitAutogeneratedWarning(javaImplWriter, this);
    }
    if (cWriter != null) {
      CodeGenUtils.emitAutogeneratedWarning(cWriter, this);
    }
  }

  protected PrintWriter javaWriter() {
    if (!cfg.allStatic() && !cfg.emitInterface()) {
      throw new InternalError("Should not call this");
    }
    return javaWriter;
  }

  protected PrintWriter javaImplWriter() {
    if (cfg.allStatic() || !cfg.emitImpl()) {
      throw new InternalError("Should not call this");
    }
    return javaImplWriter;
  }
  
  protected PrintWriter cWriter() {
    if (!cfg.emitImpl()) {
      throw new InternalError("Should not call this");
    }
    return cWriter;
  }

  private void closeWriter(PrintWriter writer) throws IOException {
    writer.flush();
    writer.close();
  }

  private void closeWriters() throws IOException {
    if (javaWriter != null) {
      closeWriter(javaWriter);
    }
    if (javaImplWriter != null) {
      closeWriter(javaImplWriter);
    }
    if (cWriter != null) {
      closeWriter(cWriter);
    }
    javaWriter = null;
    javaImplWriter = null;
    cWriter = null;
  }

  /**
   * Returns the value that was specified by the configuration directive
   * "JavaOutputDir", or the default if none was specified.
   */
  protected String getJavaOutputDir() {
    return cfg.javaOutputDir();
  }

  /**
   * Returns the value that was specified by the configuration directive
   * "Package", or the default if none was specified.
   */
  protected String getJavaPackageName() {
    return cfg.packageName();
  }

  /**
   * Returns the value that was specified by the configuration directive
   * "ImplPackage", or the default if none was specified.
   */
  protected String getImplPackageName() {
    return cfg.implPackageName();
  }

  /**
   * Emit all the strings specified in the "CustomJavaCode" parameters of
   * the configuration file.
   */
  protected void emitCustomJavaCode(PrintWriter writer, String className) throws Exception
  {
    List code = cfg.customJavaCodeForClass(className);
    if (code.size() == 0)
      return;

    writer.println();
    writer.println("  // --- Begin CustomJavaCode .cfg declarations"); 
    for (Iterator iter = code.iterator(); iter.hasNext(); ) {
      writer.println((String) iter.next());
    }
    writer.println("  // ---- End CustomJavaCode .cfg declarations"); 
  }
  
  /**
   * Write out any header information for the output files (class declaration
   * and opening brace, import statements, etc).
   */
  protected void emitAllFileHeaders() throws IOException {    
    try {    
      if (cfg.allStatic() || cfg.emitInterface()) {
        String[] interfaces;
        List userSpecifiedInterfaces = null;
        if (cfg.emitInterface()) {
          userSpecifiedInterfaces = cfg.extendedInterfaces(cfg.className());
        } else {
          userSpecifiedInterfaces = cfg.implementedInterfaces(cfg.className());
        }
        interfaces = new String[userSpecifiedInterfaces.size()];
        userSpecifiedInterfaces.toArray(interfaces);
        
        final List/*<String>*/ intfDocs = cfg.javadocForClass(cfg.className());
        CodeGenUtils.EmissionCallback docEmitter =
          new CodeGenUtils.EmissionCallback() {
            public void emit(PrintWriter w) {
              for (Iterator iter = intfDocs.iterator(); iter.hasNext(); ) {
                w.println((String) iter.next());
              }
            }
          };

        CodeGenUtils.emitJavaHeaders(
          javaWriter,
          cfg.packageName(),
          cfg.className(),
          cfg.allStatic() ? true : false, 
          (String[]) cfg.imports().toArray(new String[] {}),
          new String[] { "public" },
          interfaces,
          null,
          docEmitter);               
      }
    
      if (!cfg.allStatic() && cfg.emitImpl()) {
        final List/*<String>*/ implDocs = cfg.javadocForClass(cfg.className());
        CodeGenUtils.EmissionCallback docEmitter =
          new CodeGenUtils.EmissionCallback() {
            public void emit(PrintWriter w) {
              for (Iterator iter = implDocs.iterator(); iter.hasNext(); ) {
                w.println((String) iter.next());
              }
            }
          };

        String[] interfaces;
        List userSpecifiedInterfaces = null;
        userSpecifiedInterfaces = cfg.implementedInterfaces(cfg.implClassName());
        interfaces = new String[1 + userSpecifiedInterfaces.size()];
        userSpecifiedInterfaces.toArray(interfaces);
        interfaces[userSpecifiedInterfaces.size()] = cfg.className();

        CodeGenUtils.emitJavaHeaders(
          javaImplWriter,
          cfg.implPackageName(),
          cfg.implClassName(),
          true,
          (String[]) cfg.imports().toArray(new String[] {}),
          new String[] { "public" },
          interfaces,
          null,
          docEmitter);                      
      }
          
      if (cfg.emitImpl()) {
        PrintWriter cWriter = cWriter();
        emitCHeader(cWriter, cfg.implClassName());
      }
    } catch (Exception e) {
      throw new RuntimeException(
        "Error emitting all file headers: cfg.allStatic()=" + cfg.allStatic() +
        " cfg.emitImpl()=" + cfg.emitImpl() + " cfg.emitInterface()=" + cfg.emitInterface(),
        e);       
    }
    
  }
  
  protected void emitCHeader(PrintWriter cWriter, String className) {
    cWriter.println("#include <jni.h>");
    cWriter.println();

    if (getConfig().emitImpl()) {
      cWriter.println("#include <assert.h>"); 
      cWriter.println();
    }

    for (Iterator iter = cfg.customCCode().iterator(); iter.hasNext(); ) {
      cWriter.println((String) iter.next());
    }
    cWriter.println();
  }
  
  /**
   * Write out any footer information for the output files (closing brace of
   * class definition, etc).
   */
  protected void emitAllFileFooters(){
    if (cfg.allStatic() || cfg.emitInterface()) {
      javaWriter().println();
      javaWriter().println("} // end of class " + cfg.className());
    }
    if (!cfg.allStatic() && cfg.emitImpl())
    {
      javaImplWriter().println();
      javaImplWriter().println("} // end of class " + cfg.implClassName());
    }
  }

  private JavaType javaType(Class c) {
    return JavaType.createForClass(c);
  }

  private MethodBinding bindFunction(FunctionSymbol sym,
                                     JavaType containingType,
                                     Type containingCType) {

    MethodBinding binding = new MethodBinding(sym, containingType, containingCType);
    
    if (cfg.returnsString(binding.getName())) {
      PointerType prt = sym.getReturnType().asPointer();
      if (prt == null ||
          prt.getTargetType().asInt() == null ||
          prt.getTargetType().getSize() != 1) {
        throw new RuntimeException(
          "Cannot apply ReturnsString configuration directive to \"" + sym +
          "\". ReturnsString requires native method to have return type \"char *\"");
      }
      binding.setJavaReturnType(JavaType.createForClass(java.lang.String.class));
    } else {
      binding.setJavaReturnType(typeToJavaType(sym.getReturnType(), false));
    }

    // List of the indices of the arguments in this function that should be
    // converted from byte[] to String
    List stringArgIndices = cfg.stringArguments(binding.getName());

    for (int i = 0; i < sym.getNumArguments(); i++) {
      Type cArgType = sym.getArgumentType(i);
      JavaType mappedType = typeToJavaType(cArgType, true);
      //System.out.println("C arg type -> \"" + cArgType + "\"" );
      //System.out.println("      Java -> \"" + mappedType + "\"" );
     
      // Take into account any ArgumentIsString configuration directives that apply
      if (stringArgIndices != null && stringArgIndices.contains(new Integer(i))) {   
        //System.out.println("Forcing conversion of " + binding.getName() + " arg #" + i + " from byte[] to String ");
        if (mappedType.isCVoidPointerType() ||
            mappedType.isCCharPointerType() ||
            (mappedType.isArray() && mappedType.getJavaClass() == ArrayTypes.byteArrayArrayClass)) {
          // convert mapped type from void* and byte[] to String, or byte[][] to String[]
          if (mappedType.getJavaClass() == ArrayTypes.byteArrayArrayClass) {
            mappedType = javaType(ArrayTypes.stringArrayClass);
          } else {         
            mappedType = javaType(String.class);
          }
        }
        else {
        throw new RuntimeException(
          "Cannot apply ArgumentIsString configuration directive to " +
          "argument " + i + " of \"" + sym + "\": argument type is not " +
          "a \"void*\", \"char *\", or \"char**\" equivalent");
        }
      }
      binding.addJavaArgumentType(mappedType);
      //System.out.println("During binding of [" + sym + "], added mapping from C type: " + cArgType + " to Java type: " + mappedType);
    }

    //System.err.println("---> " + binding);
    //System.err.println("    ---> " + binding.getCSymbol());
    return binding;
  }
  
  // Expands a MethodBinding containing C primitive pointer types into
  // multiple variants taking Java primitive arrays and NIO buffers, subject
  // to the per-function "NIO only" rule in the configuration file
  private List/*<MethodBinding>*/ expandMethodBinding(MethodBinding binding) {
    List result = new ArrayList();
    result.add(binding);
    int i = 0;
    while (i < result.size()) {
      MethodBinding mb = (MethodBinding) result.get(i);
      boolean shouldRemoveCurrent = false;
      for (int j = 0; j < mb.getNumArguments(); j++) {
        JavaType t = mb.getJavaArgumentType(j);
        if (t.isCPrimitivePointerType()) {
          // Remove original from list
          shouldRemoveCurrent = true;
          MethodBinding variant = null;

          // Non-NIO variants for void* and other C primitive pointer types
          if (!cfg.nioOnly(mb.getCSymbol().getName())) {
            if (t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.booleanArrayClass));
              if (! result.contains(variant)) result.add(variant);
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.charArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCCharPointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.byteArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCShortPointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.shortArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCInt32PointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.intArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCInt64PointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.longArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCFloatPointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.floatArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
            if (t.isCDoublePointerType() || t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, javaType(ArrayTypes.doubleArrayClass));
              if (! result.contains(variant)) result.add(variant);
            }
          }

          // NIO variants for void* and other C primitive pointer types
          if (!cfg.noNio(mb.getCSymbol().getName())) {
            if (t.isCVoidPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }
          }

          if ((cfg.nioMode() == JavaConfiguration.NIO_MODE_ALL_POINTERS && !cfg.noNio(mb.getCSymbol().getName())) ||
              (cfg.nioMode() == JavaConfiguration.NIO_MODE_VOID_ONLY    && cfg.forcedNio(mb.getCSymbol().getName()))) {
            if (t.isCCharPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOByteBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }

            if (t.isCShortPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOShortBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }

            if (t.isCInt32PointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOIntBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }

            if (t.isCInt64PointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOLongBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }

            if (t.isCFloatPointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIOFloatBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }

            if (t.isCDoublePointerType()) {
              variant = mb.createCPrimitivePointerVariant(j, JavaType.forNIODoubleBufferClass());
              if (! result.contains(variant)) result.add(variant);
            }
          }
        }
      }
      if (mb.getJavaReturnType().isCPrimitivePointerType()) {
        MethodBinding variant = null;
        if (mb.getJavaReturnType().isCVoidPointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.forNIOByteBufferClass());
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCCharPointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.byteArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCShortPointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.shortArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCInt32PointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.intArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCInt64PointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.longArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCFloatPointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.floatArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        } else if (mb.getJavaReturnType().isCDoublePointerType()) {
          variant = mb.createCPrimitivePointerVariant(-1, JavaType.createForClass(ArrayTypes.doubleArrayClass));
          if (! result.contains(variant)) result.add(variant); 
        }
        shouldRemoveCurrent = true;
      }
      if (shouldRemoveCurrent) {
        result.remove(i);
        --i;
      }
      ++i;
    }

    // Honor the flattenNIOVariants directive in the configuration file
    if (cfg.flattenNIOVariants()) {
      i = 0;
      while (i < result.size()) {
        boolean shouldRemoveCurrent = false;
        MethodBinding mb = (MethodBinding) result.get(i);
        for (int j = 0; j < binding.getNumArguments() && !shouldRemoveCurrent; j++) {
          JavaType t1 = binding.getJavaArgumentType(j);
          if (t1.isCPrimitivePointerType() && !t1.isCVoidPointerType()) {
            for (int k = j + 1; k < binding.getNumArguments() && !shouldRemoveCurrent; k++) {
              JavaType t2 = binding.getJavaArgumentType(k);
              if (t2.isCPrimitivePointerType() && !t2.isCVoidPointerType()) {
                // The "NIO-ness" of the converted arguments in the
                // new binding must match
                JavaType nt1 = mb.getJavaArgumentType(j);
                JavaType nt2 = mb.getJavaArgumentType(k);
                if (nt1.isNIOBuffer() != nt2.isNIOBuffer()) {
                  shouldRemoveCurrent = true;
                }
              }
            }
          }
        }
        if (shouldRemoveCurrent) {
          result.remove(i);
          --i;
        }

        ++i;
      }
    }

    return result;
  }

  private String resultName() {
    return "_res";
  }

  private Type canonicalize(Type t) {
    Type res = (Type) canonMap.get(t);
    if (res != null) {
      return res;
    }
    canonMap.put(t, t);
    return t;
  }
}