Java_CodeGen.jrag 23.6 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
import java.io.*;
import java.util.*;

aspect Java_CodeGenEnv {

  // Environment wrapper for Java-code generation
  // handles indentation, file writing, 

  public class Java_env {

11
    public final int version;  //labcomm version to generate code for
12
    public final String verStr;
Anders Nilsson's avatar
Anders Nilsson committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    private int indent;
    private int depth;
    private Java_printer printer;
    private HashMap unique = new HashMap();

    final private class Java_printer {
      
      private boolean newline = true;
      private File file;
      private PrintStream out;
      private IOException exception;
      

      public Java_printer(File f) {
  	file = f;
28
29
30
31
        File parentFile = f.getParentFile();
        if(parentFile != null) {
            parentFile.mkdirs();
        }
Anders Nilsson's avatar
Anders Nilsson committed
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
      }

     public Java_printer(PrintStream out) {
        this.out = out;
      }

      public void close() throws IOException {
	if (out != null) {
  	  out.close();
        }
	if (exception != null) {
	  throw exception;
        }
      }

      public PrintStream getPrintStream() {
	return(out);
      }

      public void checkOpen() {
	if (out == null && exception == null) {
          try {
    	    out = new PrintStream(new FileOutputStream(file));
          } catch (IOException e) {
	    exception = e;
          }
        }
      }

      public void print(Java_env env, String s) {
	checkOpen();
        if (newline) {
          newline = false;
          for (int i = 0 ; i < env.indent ; i++) {
            out.print("  ");
          }
        }
        out.print(s);
      }

      public void println(Java_env env, String s) {
	checkOpen();
        print(env, s);
        out.println();
        newline = true;
      }
    }

80
81
    private Java_env(int version, int indent) {
      this.version = version; 
82
      this.verStr = (version == 2006) ? "2006" : "";
Anders Nilsson's avatar
Anders Nilsson committed
83
      this.indent = indent;
84
85
86
87
    }

    private Java_env(int version, Java_printer printer) {
      this(version, 0);
Anders Nilsson's avatar
Anders Nilsson committed
88
89
90
      this.printer = printer;
    }

91
92
    public Java_env(int version, File f) {
      this(version, 0);
Anders Nilsson's avatar
Anders Nilsson committed
93
94
95
      this.printer = new Java_printer(f);
    }

96
97
    public Java_env(int version, PrintStream out) {
      this(version, 0);
Anders Nilsson's avatar
Anders Nilsson committed
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
      this.printer = new Java_printer(out);
    }

    public void close() throws IOException {
      printer.close();
    }

    public PrintStream getPrintStream() {
      return printer.getPrintStream();
    }
    public void indent(int amount) {
      indent += amount;
    }

    public void indent() {
      indent(1);
    }

    public void unindent(int amount) {
      indent -= amount;
    }

    public void unindent() {
      unindent(1);
    }

    public void print(String s) {
      printer.print(this, s);
    }

    public void println(String s) {
      printer.println(this, s);
    }

    public void println() {
      printer.println(this, "");
    }

    public int getDepth() {
      return depth;
    }

    public String print_for_begin(String limit) {
      print("for (int i_" + depth + " = 0 ; ");
      print("i_" + depth + " < " + limit + " ; ");
      println("i_" + depth + "++) {");
      indent();
      depth++;
      return "[i_" + (depth - 1) + "]";
    }

    public void print_for_end() {
      depth--;
      unindent();
      println("}");
    }

155
156
157
158
159
160
161
162
163
164
    public void print_block_begin() {
      println("{");
      indent();
    }

    public void print_block_end() {
      unindent();
      println("}");
    }

Anders Nilsson's avatar
Anders Nilsson committed
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
    public String getUnique(Object o) {
      String result = (String)unique.get(o);
      if (result == null) {
   	result = "_" + (unique.size() + 1) + "_";
      }
      unique.put(o, result);
      return result;
    }

  }

}

aspect Java_StructName {

  inh int Decl.Java_Depth();
  inh int Type.Java_Depth();
  eq Program.getDecl(int i).Java_Depth() = 0;
  eq StructType.getField(int i).Java_Depth() = Java_Depth() + 1;

  inh String Type.Java_structName();
  eq Program.getDecl(int i).Java_structName() = getDecl(i).getName();
  eq StructType.getField(int i).Java_structName() {
    if (Java_Depth() == 0) {
      return "struct_" + getField(i).getName();
    } else {
      return Java_structName() + "_" + getField(i).getName();
    }
  }
}

aspect Java_Void {

  syn boolean Decl.isVoid() = getType().isVoid();
199
  syn boolean UserType.isVoid() = decl().isVoid();
Anders Nilsson's avatar
Anders Nilsson committed
200
201
202
203
204
205
206
  syn boolean Type.isVoid() = false;
  syn boolean VoidType.isVoid() = true;

}

aspect Java_CodeGen {

207
  public void Program.J_gen(PrintStream ps, String pack, int version) throws IOException {
208
209
210
    Java_env env;
/*
    // Registration class
211
    env = new Java_env(version, ps);
212
213
214
215
216
217
218
219
220
221
222
223
    if (pack != null && pack.length() > 0) {
      env.println("package " + pack + ";");
    }
    env.println("public class LabCommRegister {");
    env.println();
    env.indent();
    Java_emitTypeRegister(env);
    env.unindent();
    env.println();
    env.println("}");
//    env.close();
*/
224
    env = new Java_env(version, ps);
225
226
227
228
229
230
231
232
233
234
235
236
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
        d.Java_emitClass(env, pack);
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
    env.close();
  }

237
  public void Program.J_gen(String dir, String pack, int version) throws IOException {
Anders Nilsson's avatar
Anders Nilsson committed
238
239
240
    Java_env env;
/*
    // Registration class
241
    env = new Java_env(version, new File(dir, "LabCommRegister.java"));
Anders Nilsson's avatar
Anders Nilsson committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    if (pack != null && pack.length() > 0) {
      env.println("package " + pack + ";");
    }
    env.println("public class LabCommRegister {");
    env.println();
    env.indent();
    Java_emitTypeRegister(env);
    env.unindent();
    env.println();
    env.println("}");
    env.close();
*/
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
257
        env = new Java_env(version, new File(dir, d.getName() + ".java"));
Anders Nilsson's avatar
Anders Nilsson committed
258
259
260
261
262
263
264
265
266
        d.Java_emitClass(env, pack);
        env.close();
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
  }

267
268
  /** Experimental method for generating code to a map <classname, source> 
    */
269
  public void Program.J_gen(Map<String,String> src, String pack, int version) throws IOException {
270
271
272
273
274
275
276
277
278
    Java_env env;
/*
    // Registration class was commented out, so got removed in this copy
*/
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(bs);
279
        env = new Java_env(version, out);
280
281
282
283
284
285
286
287
288
289
        d.Java_emitClass(env, pack);
        env.close();
        src.put(d.getName(), bs.toString());
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
  }

Anders Nilsson's avatar
Anders Nilsson committed
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
}

aspect Java_Register {

  public void Program.Java_emitTypeRegister(Java_env env) {
    /*
    env.println("static void register(LabCommChannel c) {");
    env.indent();
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).Java_emitTypeRegister(env);
    }
    env.unindent();
    env.println("}");
*/

  }

  public void Decl.Java_emitTypeRegister(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitTypeRegister(Java_env env)" + 
		    " not declared");
  }

  public void SampleDecl.Java_emitTypeRegister(Java_env env) {
    env.println(getName() + ".register(c);");
  }

  public void TypeDecl.Java_emitTypeRegister(Java_env env) {
    // TODO
  }

}

aspect Java_Class {

  public void Decl.Java_emitClass(Java_env env, String pack) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitClass(Java_env env, String pack)" + 
		    " not declared");
  }

  public void TypeDecl.Java_emitClass(Java_env env, String pack) {
    if (getType().Java_needInstance()) {
      // Hackish prettyprint preamble
      env.println("/* ");
      pp(env.getPrintStream());
      env.println("*/");

      if (pack != null && pack.length() > 0) {
        env.println("package " + pack + ";");
      }

      env.println("import java.io.IOException;");
343
344
345
      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommType;");
      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
      env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
Anders Nilsson's avatar
Anders Nilsson committed
346
347
348
349
350
351
352
353
354
355
356
357
      env.println();
      env.println("public class " + getName() + " implements LabCommType {");
      env.println();
      env.indent();
      getType().Java_emitInstance(env);
      Java_emitEncoder(env);
      Java_emitDecoder(env);
      env.unindent();
      env.println("}");
    }
  }

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//  TODO: ?
//  Code snippet for making samples of user types extend their type 
//  currently commented out. Is this a good idea?
//
//  syn String Type.getTypeName();
//  eq Type.getTypeName() = getClass().getName();
//  eq PrimType.getTypeName() = getName();
//  eq UserType.getTypeName() = getName();

//  syn boolean Type.isUserType();
//  eq Type.isUserType() = false;
//  eq UserType.isUserType() = true;
//
// plus use some lines down

Anders Nilsson's avatar
Anders Nilsson committed
373
374
375
376
377
378
379
380
381
382
  public void SampleDecl.Java_emitClass(Java_env env, String pack) {
    env.println("/* ");
    pp(env.getPrintStream());
    env.println("*/");

    if (pack != null && pack.length() > 0) {
      env.println("package " + pack + ";");
    }

    env.println("import java.io.IOException;");
383
384
385
386
387
    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDecoder;");
    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommDispatcher;");
    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommEncoder;");
    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommHandler;");
    env.println("import se.lth.control.labcomm"+env.verStr+".LabCommSample;");
Anders Nilsson's avatar
Anders Nilsson committed
388
    env.println();
389
390
391
392
393
    env.print("public class " + getName());
//    if(getType().isUserType()) {
//        env.print(" extends "+getType().getTypeName());
//    }
    env.println(" implements LabCommSample {");
Anders Nilsson's avatar
Anders Nilsson committed
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
    env.println();
    env.indent();
    getType().Java_emitInstance(env);
    env.println("public interface Handler extends LabCommHandler {");
    env.print("  public void handle_" + getName() + "(");
    if (!isVoid()) {
      getType().Java_emitType(env);
      env.print(" value");
    }
    env.println(") throws Exception;");
    env.println("}");
    env.println();
    env.println("public static void register(LabCommDecoder d, Handler h) throws IOException {");
    env.indent();
    env.println("d.register(new Dispatcher(), h);");
    env.unindent();
    env.println("}");
    env.println();

    env.println("public static void register(LabCommEncoder e) throws IOException {");
    env.indent();
    env.println("e.register(new Dispatcher());");
    env.unindent();
    env.println("}");
    env.println(); 

    env.println("private static class Dispatcher implements LabCommDispatcher {");
    env.indent();
    env.println(); 
    env.println("public Class getSampleClass() {");
    env.indent();
    env.println("return " + getName() + ".class;");
    env.unindent();
    env.println("}");
    env.println(); 
    env.println("public String getName() {");
    env.indent();
    env.println("return \"" + getName() + "\";");
    env.unindent();
    env.println("}");
    env.println(); 
    env.println("public byte[] getSignature() {");
    env.indent();
    env.println("return signature;");
    env.unindent();
    env.println("}");
    env.println(); 
    env.println("public void decodeAndHandle(LabCommDecoder d,");
    env.println("                            LabCommHandler h) throws Exception {");
    env.indent();
    if (isVoid()) {
      env.println(getName() + ".decode(d);");
      env.println("((Handler)h).handle_" + getName() + "();"); 
    } else {
      env.println("((Handler)h).handle_" + getName() + "(" + getName() + ".decode(d));"); 
    }
    env.unindent();
    env.println("}");
    env.println("");
    env.unindent();
    env.println("}");
    env.println("");

    Java_emitEncoder(env);
    Java_emitDecoder(env);
    env.println("private static byte[] signature = new byte[] {");
    env.indent();
461
    SignatureList signature = signature(env.version);
Anders Nilsson's avatar
Anders Nilsson committed
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
    for (int i = 0 ; i < signature.size() ; i++) {
      String comment = signature.getComment(i);
      if (comment != null) {
        env.println(signature.getIndent(i) + "// " + comment);
      }
      byte[] data = signature.getData(i);
      if (data != null) {
        env.print(signature.getIndent(i));
        for (int j = 0 ; j < data.length ; j++) {
	  env.print(data[j] + ", ");
        }
        env.println();
      }
    }
    env.unindent();
    env.println("};");
    env.unindent();
    env.println();
    env.println("}");
  }

  public void TypeDecl.Java_emitEncoder(Java_env env) {
    env.print("public static void encode(LabCommEncoder e");
    if (!isVoid()) {
      env.print(", ");
      getType().Java_emitType(env);
      env.print(" value");
    }
    env.println(") throws IOException {");
    env.indent();
    getType().Java_emitEncoder(env, "value");
    env.unindent();
    env.println("}");
    env.println();
  }

  public void SampleDecl.Java_emitEncoder(Java_env env) {
    env.print("public static void encode(LabCommEncoder e");
    if (!isVoid()) {
      env.print(", ");
      getType().Java_emitType(env);
      env.print(" value");
    }
    env.println(") throws IOException {");
    env.indent();
    env.println("e.begin(" + getName() + ".class);");
    getType().Java_emitEncoder(env, "value");
    env.println("e.end(" + getName() + ".class);");
    env.unindent();
    env.println("}");
    env.println();
  }

  public void Type.Java_emitEncoder(Java_env env, String name) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitEncoder(Java_env env, String name)" + 
		    " not declared");
  }

  public void VoidType.Java_emitEncoder(Java_env env, String name) {
  }

  public void PrimType.Java_emitEncoder(Java_env env, String name) {
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { env.print("e.encodeBoolean"); } break;
      case LABCOMM_BYTE: { env.print("e.encodeByte"); } break;
      case LABCOMM_SHORT: { env.print("e.encodeShort"); } break;
      case LABCOMM_INT: { env.print("e.encodeInt"); } break;
      case LABCOMM_LONG: { env.print("e.encodeLong"); } break;
      case LABCOMM_FLOAT: { env.print("e.encodeFloat"); } break;
      case LABCOMM_DOUBLE: { env.print("e.encodeDouble"); } break;
      case LABCOMM_STRING: { env.print("e.encodeString"); } break;
    }
    env.println("(" + name + ");");
  }

  public void ArrayType.Java_emitEncoder(Java_env env, String name) {
    int baseDepth = env.getDepth();
    String prefix = "";
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = getExp(i).Java_emitEncoder(env, name + prefix);
543
      env.print_block_begin();
Anders Nilsson's avatar
Anders Nilsson committed
544
545
546
547
548
549
550
551
552
553
      env.println("int i_" + (baseDepth + i) + "_max = " + limit + ";");
      prefix = prefix + "[0]";
    }
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = "i_" + (baseDepth + i) + "_max";
      name = name + env.print_for_begin(limit);
    }
    getType().Java_emitEncoder(env, name);
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
554
      env.print_block_end();
Anders Nilsson's avatar
Anders Nilsson committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    }
  }
  
  public String Exp.Java_emitEncoder(Java_env env, String name) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitEncoder(Java_env env, String name)" + 
		    " not declared");
  }

  public String IntegerLiteral.Java_emitEncoder(Java_env env, String name) {
    return getValue();
  }

  public String VariableSize.Java_emitEncoder(Java_env env, String name) {
569
    env.println("e.encodePacked32(" + name + ".length);");
Anders Nilsson's avatar
Anders Nilsson committed
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
    return name + ".length";
  }

  public void StructType.Java_emitEncoder(Java_env env, String name) {
    for (int i = 0 ; i < getNumField() ; i++) {
      Field f = getField(i);
      f.getType().Java_emitEncoder(env, name + "." + f.getName());
    }
  }

  public void UserType.Java_emitEncoder(Java_env env, String name) {
    if (Java_needInstance()) {
      env.println(getName() + ".encode(e, " + name + ");");
    } else {
      decl().getType().Java_emitEncoder(env, name);
    }
  }

  public void Decl.Java_emitDecoder(Java_env env) {
    env.print("public static ");
    getType().Java_emitType(env);
    env.println(" decode(LabCommDecoder d) throws IOException {");
    env.indent();
    if (!isVoid()) {
      getType().Java_emitType(env);
      env.println(" result;");
      getType().Java_emitDecoder(env, "result");
      env.println("return result;");
    }
    env.unindent();
    env.println("}");
    env.println();
  }

  public void Type.Java_emitDecoder(Java_env env, String name) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitDecoder(Java_env env, String name)" + 
		    " not declared");
  }

  public void VoidType.Java_emitDecoder(Java_env env, String name) {
  }

  public void PrimType.Java_emitDecoder(Java_env env, String name) {
    env.print(name + " = ");
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { env.println("d.decodeBoolean();"); } break;
      case LABCOMM_BYTE: { env.println("d.decodeByte();"); } break;
      case LABCOMM_SHORT: { env.println("d.decodeShort();"); } break;
      case LABCOMM_INT: { env.println("d.decodeInt();"); } break;
      case LABCOMM_LONG: { env.println("d.decodeLong();"); } break;
      case LABCOMM_FLOAT: { env.println("d.decodeFloat();"); } break;
      case LABCOMM_DOUBLE: { env.println("d.decodeDouble();"); } break;
      case LABCOMM_STRING: { env.println("d.decodeString();"); } break;
    }
  }

  public void ArrayType.Java_emitDecoder(Java_env env, String name) {
    env.println("{");
    env.indent();
    int baseDepth = env.getDepth();
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print("int i_" + (baseDepth + i) + "_max = ");
      getExp(i).Java_emitDecoder(env);
      env.println(";");
    }
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = "i_" + (baseDepth + i) + "_max";
      env.print(name + " = "); 
      Java_emitNew(env, limit, getNumExp() - i);
      env.println(";");
      name = name + env.print_for_begin(limit);
    }
    getType().Java_emitDecoder(env, name);
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
    }
    env.unindent();
    env.println("}");
  }

  public void Exp.Java_emitDecoder(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitDecoder(Java_env env)" + 
		    " not declared");
  }

  public void IntegerLiteral.Java_emitDecoder(Java_env env) {
    env.print(getValue());
  }

  public void VariableSize.Java_emitDecoder(Java_env env) {
662
    env.print("d.decodePacked32()");
Anders Nilsson's avatar
Anders Nilsson committed
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
  }

  public void StructType.Java_emitDecoder(Java_env env, String name) {
    env.print(name + " = new ");
    Java_emitType(env);
    env.println("();");
    for (int i = 0 ; i < getNumField() ; i++) {
      Field f = getField(i);
      f.getType().Java_emitDecoder(env, name + "." + f.getName());
    }
  }

  public void UserType.Java_emitDecoder(Java_env env, String name) {
    if (Java_needInstance()) {
      env.println(name + " = " + getName() + ".decode(d);");
    } else {
      decl().getType().Java_emitDecoder(env, name);
    }
  }

  public void Type.Java_emitNew(Java_env env, String size) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitNew(Java_env env, String size)" + 
		    " not declared");
  }

  public void ArrayType.Java_emitNew(Java_env env, String size, int depth) {
    env.print("new ");
    getType().Java_emitTypePrefix(env);
    env.print("[" + size + "]");
    getType().Java_emitTypeSuffix(env);
    for (int i = 1 ; i < depth ; i++) {
      env.print("[]");
    }
  }

  public void Type.Java_emitTypePrefix(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitTypePrefix(Java_env env)" + 
		    " not declared");
  }

  public void PrimType.Java_emitTypePrefix(Java_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { env.print("String"); } break;
      default: { env.print(getName()); } break;
    }
  }

  public void UserType.Java_emitTypePrefix(Java_env env) {
    if (Java_needInstance()) {
      env.print(getName());
    } else {
      decl().getType().Java_emitTypePrefix(env);
    } 
  }

  public void ArrayType.Java_emitTypePrefix(Java_env env){
    getType().Java_emitTypePrefix(env);
  }

  public void StructType.Java_emitTypePrefix(Java_env env){
    env.print(Java_structName());
  }

  public void Type.Java_emitTypeSuffix(Java_env env) {
  }

  public void UserType.Java_emitTypeSuffix(Java_env env) {
    if (! Java_needInstance()) {
      decl().getType().Java_emitTypeSuffix(env);
    } 
  }

  public void ArrayType.Java_emitTypeSuffix(Java_env env){
    getType().Java_emitTypeSuffix(env);
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print("[]");
    }
  }

  public boolean Type.Java_needInstance() {
    throw new Error(this.getClass().getName() + 
		    ".Java_needInstance()" + 
		    " not declared");
  }

750
751
752
753
  public boolean VoidType.Java_needInstance() {
    return false;
  }

Anders Nilsson's avatar
Anders Nilsson committed
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
  public boolean PrimType.Java_needInstance() {
    return false;
  }

  public boolean UserType.Java_needInstance() {
    return decl().getType().Java_needInstance();
  }

  public boolean StructType.Java_needInstance() {
    return true;
  }

  public boolean ArrayType.Java_needInstance() {
    return getType().Java_needInstance();
  }

  public boolean Type.Java_isPrimitive() {
    return false;
  }

  public boolean PrimType.Java_isPrimitive() {
    return true;
  }

  public void Type.Java_emitInstance(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitInstance(Java_env env)" + 
		    " not declared");
  }

  public void VoidType.Java_emitInstance(Java_env env) {
  }

  public void PrimType.Java_emitInstance(Java_env env) {
  }

  public void ArrayType.Java_emitInstance(Java_env env) {
    getType().Java_emitInstance(env);
  }

  public void StructType.Java_emitInstance(Java_env env) {
    if (Java_Depth() > 0) {
      env.println("public static class " + Java_structName() + " {");
      env.indent();
    }
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).getType().Java_emitInstance(env);
    }
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).Java_emitField(env);
    }
    if (Java_Depth() > 0) {
      env.unindent();
      env.println("}");
    }
    env.println();
  }

  public void UserType.Java_emitInstance(Java_env env) {
  }

  public void Field.Java_emitField(Java_env env) {
    env.print("public ");
    getType().Java_emitType(env);
    env.println(" " + getName() + ";");    
  }

  public void Type.Java_emitType(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_emitType(Java_env env)" + 
		    " not declared");
  }

  public void VoidType.Java_emitType(Java_env env) {
    env.print("void");
  }

  public void PrimType.Java_emitType(Java_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { env.print("String"); } break;
      default: { env.print(getName()); } break;
    }
  }

  public void UserType.Java_emitType(Java_env env) {
    decl().getType().Java_emitType(env);
  }

  public void ArrayType.Java_emitType(Java_env env){
    getType().Java_emitType(env);
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print("[]");
    }
  }

  public void StructType.Java_emitType(Java_env env){
    env.print(Java_structName());
  }

}

aspect Java_Info {

857
858
  public void Program.Java_info(PrintStream out, int version) {
    Java_env env = new Java_env(version, out);
Anders Nilsson's avatar
Anders Nilsson committed
859
860
861
862
863
864
865
866
867
868
869
870
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).Java_info(env);
    }
  }

  public void Decl.Java_info(Java_env env) {
    throw new Error(this.getClass().getName() + 
		    ".Java_info(Java_env env)" + 
		    " not declared");
  }

  public void TypeDecl.Java_info(Java_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
871
    env.print(",Java,typedef," + getName() + ",");
872
873
    getType().Java_emitType(env); 
    env.print(",not_applicable_for_Java");
Anders Nilsson's avatar
Anders Nilsson committed
874
875
876
877
    env.println();
  }

  public void SampleDecl.Java_info(Java_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
878
    env.print(",Java,sample," + getName() + ",");
Anders Nilsson's avatar
Anders Nilsson committed
879
    getType().Java_emitType(env);
880
    env.print(",not_applicable_for_Java");
Anders Nilsson's avatar
Anders Nilsson committed
881
882
883
884
    env.println();
  }

}