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

aspect Java_CodeGenEnv {

  // Environment wrapper for Java-code generation
7
  // handles indentation, file writing,
Anders Nilsson's avatar
Anders Nilsson committed
8
9
10

  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
    private int indent;
    private int depth;
    private Java_printer printer;
    private HashMap unique = new HashMap();

    final private class Java_printer {
19

Anders Nilsson's avatar
Anders Nilsson committed
20
21
22
23
      private boolean newline = true;
      private File file;
      private PrintStream out;
      private IOException exception;
24

Anders Nilsson's avatar
Anders Nilsson committed
25
26
27

      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
    private Java_env(int version, int indent) {
81
      this.version = version;
Anders Blomdell's avatar
Anders Blomdell committed
82
      this.verStr = "2014";
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
    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();
181
  inh int DataType.Java_Depth();
182
  eq Specification.getDecl(int i).Java_Depth() = 0;
Anders Nilsson's avatar
Anders Nilsson committed
183
184
  eq StructType.getField(int i).Java_Depth() = Java_Depth() + 1;

185
  inh String DataType.Java_structName();
186
  eq Specification.getDecl(int i).Java_structName() = getDecl(i).getName();
Anders Nilsson's avatar
Anders Nilsson committed
187
188
189
190
191
192
193
194
195
196
197
  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 {

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

}

aspect Java_CodeGen {

207
  public void Specification.J_gen(PrintStream ps, String pack, int version) throws IOException {
208
    Java_env env;
209
    env = new Java_env(version, ps);
210
211
212
213
214
215
216
217
218
219
220
221
    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();
  }

222
  public void Specification.J_gen(String dir, String pack, int version) throws IOException {
Anders Nilsson's avatar
Anders Nilsson committed
223
224
225
226
    Java_env env;
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
227
        env = new Java_env(version, new File(dir, d.getName() + ".java"));
Anders Nilsson's avatar
Anders Nilsson committed
228
229
230
231
232
233
234
235
236
        d.Java_emitClass(env, pack);
        env.close();
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
  }

237
  /** Experimental method for generating code to a map <classname, source>
238
    */
239
  public void Specification.J_gen(Map<String,String> src, String pack, int version) throws IOException {
240
241
242
243
244
245
    Java_env env;
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(bs);
246
        env = new Java_env(version, out);
247
248
249
250
251
252
253
254
255
        d.Java_emitClass(env, pack);
        env.close();
        src.put(d.getName(), bs.toString());
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
  }
256

Anders Nilsson's avatar
Anders Nilsson committed
257
258
259
260
261
}

aspect Java_Class {

  public void Decl.Java_emitClass(Java_env env, String pack) {
262
263
    throw new Error(this.getClass().getName() +
		    ".Java_emitClass(Java_env env, String pack)" +
Anders Nilsson's avatar
Anders Nilsson committed
264
265
266
		    " not declared");
  }

267
  public void Decl.Java_emitDeclPP(Java_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
268
269
270
      // Hackish prettyprint preamble
      env.println("/* ");
      pp(env.getPrintStream());
271
272
273

      Java_emitUserTypeDeps(env, null, false);
      Java_emitUserTypeRefs(env, null, false);
Anders Nilsson's avatar
Anders Nilsson committed
274
275
      env.println("*/");

276
277
278
279
280
281
282
283
284
285
286
287
  }

  public void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
  // XXX TODO will generate unnecessary recursion for types. fix this per commented out code
  // XXX      but ensure that types with references actually register themselves.. (i.e., add "nested" argument)
  //public abstract void Decl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode);

  //public void TypeDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
  //  // do nothing for type decls; sampledecl iterates over all dependencies and outputs
  //  // all type decls
  //}
  //public void SampleDecl.Java_emitUserTypeDeps(Java_env env, String via, boolean outputCode) {
288
289
290
291
292
//  if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) {
//      if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
//         env.println("if(sendMetaData){");
//         env.indent();
//      }
293
294
295
296
297
        Iterator<Decl> it = type_dependencies().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.Java_emitUserTypeDeps(env, t.getName(), outputCode);
298
            if( outputCode){// && t.getDataType().isUserType() ) {
299
300
301
               env.println(t.getName()+".register(e);");
            } else {  // Just output a comment
	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
302
	       env.println(" //Depends ("+refpath+") on "+t.getName() );
303
304
            }
        }
305
306
307
308
309
//      if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
//         env.unindent();
//         env.println("}");
//      }
//  }
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
  }
  public void Decl.Java_emitUserTypeRefs(Java_env env, String via, boolean outputCode) {
    if( isReferenced() ) {
        Iterator<Decl> it = type_references().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.Java_emitUserTypeRefs(env, t.getName(), outputCode);
            if(outputCode) {
               env.println(t.getName()+".register(e);");
            } else {  // Just output a comment
	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
	       env.println(" //Is referenced ("+refpath+")  by "+t.getName() );
            }
        }
    }
 }


  public void Decl.Java_emitRegisterEncoder(Java_env env) {
    env.println("public static void register(Encoder e) throws IOException {");
    env.indent();
    env.println("register(e, true);");
    env.unindent();
    env.println("}");
    env.println();
    env.println("public static void register(Encoder e, boolean sendMetaData) throws IOException {");
    env.indent();
    Java_emitUserTypeDeps(env, null, true);
339
    env.println("e.register(dispatcher);");
340
341
    env.unindent();
    env.println("}");
Anders Blomdell's avatar
Anders Blomdell committed
342
343
344
345
346
    env.println("public static void registerSampleRef(Encoder e) throws IOException{");
    env.indent();
    env.println("e.registerSampleRef(dispatcher);");
    env.unindent();
    env.println("}");
347
348
349
350
351
    env.println();
  }

  public void TypeDecl.Java_emitClass(Java_env env, String pack) {
      Java_emitDeclPP(env);
Anders Nilsson's avatar
Anders Nilsson committed
352
      if (pack != null && pack.length() > 0) {
353
          env.println("package " + pack + ";");
Anders Nilsson's avatar
Anders Nilsson committed
354
355
      }

356
      env.println("import se.lth.control.labcomm"+env.verStr+".Constant;");
Anders Blomdell's avatar
Anders Blomdell committed
357
      env.println("import se.lth.control.labcomm"+env.verStr+".SampleType;");
358

359
      if (getDataType().Java_needInstance() || hasDependencies() || isReferenced()) {
360
361
362
363
364
365
          env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;");
          env.println("import se.lth.control.labcomm"+env.verStr+".SampleDispatcher;");
          env.println("import se.lth.control.labcomm"+env.verStr+".SampleHandler;");
//          env.println();
//      }
//
366
//      if (getDataType().Java_needInstance()) {
367
368
369
370
371
372
          env.println("import java.io.IOException;");
          env.println("import se.lth.control.labcomm"+env.verStr+".Decoder;");
      }
      // For types without type_dependencies and not needing an instance,
      // currently just an empty class is generated

Anders Blomdell's avatar
Anders Blomdell committed
373
      env.println("public class " + getName() + " implements SampleType {");
Anders Nilsson's avatar
Anders Nilsson committed
374
      env.println();
375

Anders Nilsson's avatar
Anders Nilsson committed
376
      env.indent();
377
378
      if (getDataType().Java_needInstance()) {
        getDataType().Java_emitInstance(env);
379
380
381
382
383
        Java_emitEncoder(env);
        Java_emitDecoder(env);
      }

      //if(hasDependencies() || isReferenced()) {
384
      //if( getDataType().isUserType() && isReferenced()) {
385
386
387
388
389
390
      if( isReferenced()) {
        Java_emitRegisterEncoder(env);
        Java_emitDispatcher(env, false);
      }
      Java_emitSignature(env);

Anders Nilsson's avatar
Anders Nilsson committed
391
      env.println("}");
392
393
      env.unindent();
      env.println();
Anders Nilsson's avatar
Anders Nilsson committed
394
395
  }

396

Anders Nilsson's avatar
Anders Nilsson committed
397
  public void SampleDecl.Java_emitClass(Java_env env, String pack) {
398
    Java_emitDeclPP(env);
Anders Nilsson's avatar
Anders Nilsson committed
399
400
401
402
403
404

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

    env.println("import java.io.IOException;");
405
    env.println("import se.lth.control.labcomm"+env.verStr+".Constant;");
Anders Blomdell's avatar
Anders Blomdell committed
406
407
408
409
410
    env.println("import se.lth.control.labcomm"+env.verStr+".Decoder;");
    env.println("import se.lth.control.labcomm"+env.verStr+".SampleDispatcher;");
    env.println("import se.lth.control.labcomm"+env.verStr+".Encoder;");
    env.println("import se.lth.control.labcomm"+env.verStr+".SampleHandler;");
    env.println("import se.lth.control.labcomm"+env.verStr+".Sample;");
Anders Nilsson's avatar
Anders Nilsson committed
411
    env.println();
412
    env.print("public class " + getName());
413
414
415
416
//  TODO: ?
//  Code for making samples of user types extend their type
//  currently commented out. Is this a good idea or not?
//
417
418
//    if(getDataType().isUserType()) {
//        env.print(" extends "+getDataType().getTypeName());
419
//    }
Anders Blomdell's avatar
Anders Blomdell committed
420
    env.println(" implements Sample {");
Anders Nilsson's avatar
Anders Nilsson committed
421
422
    env.println();
    env.indent();
423
    getDataType().Java_emitInstance(env);
Anders Blomdell's avatar
Anders Blomdell committed
424
    env.println("public interface Handler extends SampleHandler {");
Anders Nilsson's avatar
Anders Nilsson committed
425
426
    env.print("  public void handle_" + getName() + "(");
    if (!isVoid()) {
427
      getDataType().Java_emitType(env);
Anders Nilsson's avatar
Anders Nilsson committed
428
429
430
431
432
      env.print(" value");
    }
    env.println(") throws Exception;");
    env.println("}");
    env.println();
433
    env.println("public static void register(Decoder d, Handler h) throws IOException {");
Anders Nilsson's avatar
Anders Nilsson committed
434
    env.indent();
435
    env.println("d.register(dispatcher, h);");
Anders Nilsson's avatar
Anders Nilsson committed
436
437
    env.unindent();
    env.println("}");
Anders Blomdell's avatar
Anders Blomdell committed
438
439
440
441
442
    env.println("public static void registerSampleRef(Decoder d) throws IOException {");
    env.indent();
    env.println("d.registerSampleRef(dispatcher);");
    env.unindent();
    env.println("}");
Anders Nilsson's avatar
Anders Nilsson committed
443
444
    env.println();

445
446
447
448
449
450
451

    Java_emitRegisterEncoder(env);
    Java_emitDispatcher(env, true);
    Java_emitEncoder(env);
    Java_emitDecoder(env);

    Java_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
452
453
    env.unindent();
    env.println("}");
454
455
456
    env.println();
  }

457
458
459
460
  public void TypeDecl.Java_emitSignature(Java_env env) {
    Signature signature = getSignature();
    signature.Java_emitSignature(env, true);
  }
461
462
463
464
465
466

  public void Decl.Java_emitSignature(Java_env env) {
    //always emit the flat signature, as it is needed
    //for matching at the decoder side (which cannot know
    //the type_ids of dependent types. Therefore, flat sigs
    //are used for matching
467
    Java_emitIntentions(env);
468
    Java_emitFlatSignature(env);
469
    if(isReferenced() || (isSampleDecl() && hasDependencies() )){
470
471
472
      Signature signature = getSignature();
      signature.Java_emitSignature(env, !isSampleDecl());
    }
473
474
  }

475
  public void Decl.Java_emitIntentions(Java_env env){
476
    env.println("private static int numIntentions = " +getNumIntentions() +";");
477
478
479
480
481
482
483
484
485
486
487
488
489
    env.println("private static byte[] intentions = new byte[] {");
    env.indent();
    byte[] data = getIntentionBytes();
    if (data != null) {
        for (int j = 0 ; j < data.length ; j++) {
            env.print(data[j] + ", ");
        }
    }
    env.unindent();
    env.println("};");
    env.println();
  }

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  public void Decl.Java_emitFlatSignature(Java_env env){
    env.println("private static byte[] signature = new byte[] {");
      env.indent();
      SignatureList signature = flatSignature(env.version);
      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, env.version);
        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.println();
  }
Anders Nilsson's avatar
Anders Nilsson committed
512

513
514
515
516
517
518
  public void TypeInstance.Java_emitAnnotationComment(Java_env env) {
      if(hasAnnotations()) {
          env.println("// "+getAnnotationString());
      }
  }

519
520
  //XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
  public void Decl.Java_emitDispatcher(Java_env env, boolean isSample) {
521
522
    // String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":""; 
    String genericStr = "<"+getName()+">"; 
523
    env.println("private static Dispatcher dispatcher = new Dispatcher();");
524
    env.println();
525
    getTypeInstance().Java_emitAnnotationComment(env);
526
    env.println("public SampleDispatcher getDispatcher() {");
Anders Nilsson's avatar
Anders Nilsson committed
527
    env.indent();
528
    env.println("return dispatcher;");
529
530
531
    env.unindent();
    env.println("}");
    env.println();
532
533
534
    env.println("private static class Dispatcher implements SampleDispatcher "+genericStr+"{");
    env.indent();
    env.println();
535
    env.println("public Class"+genericStr+" getSampleClass() {");
Anders Nilsson's avatar
Anders Nilsson committed
536
537
538
539
    env.indent();
    env.println("return " + getName() + ".class;");
    env.unindent();
    env.println("}");
540
    env.println();
Anders Nilsson's avatar
Anders Nilsson committed
541
542
543
544
545
    env.println("public String getName() {");
    env.indent();
    env.println("return \"" + getName() + "\";");
    env.unindent();
    env.println("}");
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
    env.println();
    env.println("public byte getTypeDeclTag() {");
    env.indent();
    if(env.version == 2006) {
      if(isSample) {
        env.println("return Constant.SAMPLE;");
      } else {
        env.println("return Constant.TYPEDEF;");
      }
    } else {
      if(isSample) {
        env.println("return Constant.SAMPLE_DEF;");
      } else {
        env.println("return Constant.TYPE_DEF;");
      }
    }
    env.unindent();
    env.println("}");
    env.println();
    env.println("public boolean isSample() {");
    env.indent();
    env.println("return "+isSample+";");
    env.unindent();
    env.println("}");
570
571
572
573
574
    env.println("public boolean hasDependencies() {");
    env.indent();
    env.println("return "+hasDependencies()+";");
    env.unindent();
    env.println("}");
575
    env.println();
576
    env.println("/** return the flat signature. */");
Anders Nilsson's avatar
Anders Nilsson committed
577
578
    env.println("public byte[] getSignature() {");
    env.indent();
579
580
581
582
583
    if(isSample) {
        env.println("return signature;");
    } else {
        env.println("throw new Error(\"a TYPE_DEF has no flat signature\");");
    }
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
    env.unindent();
    env.println("}");
    env.println();
    env.println("/** return the outermost intentions. */");
    env.println("public byte[] getIntentionBytes() {");
    env.indent();
    if(isSample) {
        env.println("return intentions;");
    } else {
        env.println("return new byte[0];");
    }
    env.unindent();
    env.println("}");
    env.println();
    env.println("/** return the number of intentions. */");
    env.println("public int getNumIntentions() {");
    env.indent();
    if(isSample) {
        env.println("return numIntentions;");
    } else {
        env.println("return 0;");
    }
Anders Nilsson's avatar
Anders Nilsson committed
606
607
    env.unindent();
    env.println("}");
608
609
610
611
612
613
614
    env.println();
//    env.println("public void encodeSignature(Encoder e) throws IOException{");
//    env.indent();
//    env.println("emitSignature(e);");
//    env.unindent();
//    env.println("}");
//    env.println();
615
616
    env.println("public void encodeTypeDef(Encoder e, int index) throws IOException{");
    env.indent();
617
618
619
620
621
    if(!isSample || hasDependencies()) {
      env.println("emitSignature(e);");
    } else {
      env.println("// the type has no dependencies, do nothing");
    }
622
623
624
    env.unindent();
    env.println("}");
    env.println();
625
626
627
628
629
630
    env.println("public boolean canDecodeAndHandle() {");
    env.indent();
    env.println("return "+isSample+";");
    env.unindent();
    env.println("}");
    env.println();
Anders Blomdell's avatar
Anders Blomdell committed
631
632
    env.println("public void decodeAndHandle(Decoder d,");
    env.println("                            SampleHandler h) throws Exception {");
Anders Nilsson's avatar
Anders Nilsson committed
633
    env.indent();
634
635
636
637
638
639
640
    if( isSample) {
        if (isVoid()) {
          env.println(getName() + ".decode(d);");
          env.println("((Handler)h).handle_" + getName() + "();");
        } else {
          env.println("((Handler)h).handle_" + getName() + "(" + getName() + ".decode(d));");
        }
Anders Nilsson's avatar
Anders Nilsson committed
641
    } else {
642
        env.println("throw new Exception(\"A typedef has no handler, the corresponding method on the sample class should be called.\");");
Anders Nilsson's avatar
Anders Nilsson committed
643
644
645
646
647
648
649
650
    }
    env.unindent();
    env.println("}");
    env.println("");
    env.unindent();
    env.println("}");
    env.println("");

651
652
 }

Anders Nilsson's avatar
Anders Nilsson committed
653
654

  public void TypeDecl.Java_emitEncoder(Java_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
655
    env.print("public static void encode(Encoder e");
Anders Nilsson's avatar
Anders Nilsson committed
656
657
    if (!isVoid()) {
      env.print(", ");
658
      getDataType().Java_emitType(env);
Anders Nilsson's avatar
Anders Nilsson committed
659
660
661
662
      env.print(" value");
    }
    env.println(") throws IOException {");
    env.indent();
663
    getDataType().Java_emitEncoder(env, "value");
Anders Nilsson's avatar
Anders Nilsson committed
664
665
666
667
668
669
    env.unindent();
    env.println("}");
    env.println();
  }

  public void SampleDecl.Java_emitEncoder(Java_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
670
    env.print("public static void encode(Encoder e");
Anders Nilsson's avatar
Anders Nilsson committed
671
672
    if (!isVoid()) {
      env.print(", ");
673
      getDataType().Java_emitType(env);
Anders Nilsson's avatar
Anders Nilsson committed
674
675
676
677
678
      env.print(" value");
    }
    env.println(") throws IOException {");
    env.indent();
    env.println("e.begin(" + getName() + ".class);");
679
    getDataType().Java_emitEncoder(env, "value");
Anders Nilsson's avatar
Anders Nilsson committed
680
681
682
683
684
685
    env.println("e.end(" + getName() + ".class);");
    env.unindent();
    env.println("}");
    env.println();
  }

686
  public void DataType.Java_emitEncoder(Java_env env, String name) {
687
688
    throw new Error(this.getClass().getName() +
		    ".Java_emitEncoder(Java_env env, String name)" +
Anders Nilsson's avatar
Anders Nilsson committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
		    " 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;
705
      case LABCOMM_SAMPLE: { env.print("e.encodeSampleRef"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
706
707
708
709
710
711
712
713
714
    }
    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);
715
      env.print_block_begin();
Anders Nilsson's avatar
Anders Nilsson committed
716
717
718
719
720
721
722
      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);
    }
723
    getDataType().Java_emitEncoder(env, name);
Anders Nilsson's avatar
Anders Nilsson committed
724
725
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
726
      env.print_block_end();
Anders Nilsson's avatar
Anders Nilsson committed
727
728
    }
  }
729

Anders Nilsson's avatar
Anders Nilsson committed
730
  public String Exp.Java_emitEncoder(Java_env env, String name) {
731
732
    throw new Error(this.getClass().getName() +
		    ".Java_emitEncoder(Java_env env, String name)" +
Anders Nilsson's avatar
Anders Nilsson committed
733
734
735
736
737
738
739
740
		    " not declared");
  }

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

  public String VariableSize.Java_emitEncoder(Java_env env, String name) {
741
    env.println("e.encodePacked32(" + name + ".length);");
Anders Nilsson's avatar
Anders Nilsson committed
742
743
744
745
746
747
    return name + ".length";
  }

  public void StructType.Java_emitEncoder(Java_env env, String name) {
    for (int i = 0 ; i < getNumField() ; i++) {
      Field f = getField(i);
748
      f.getDataType().Java_emitEncoder(env, name + "." + f.getName());
Anders Nilsson's avatar
Anders Nilsson committed
749
750
751
752
753
754
755
    }
  }

  public void UserType.Java_emitEncoder(Java_env env, String name) {
    if (Java_needInstance()) {
      env.println(getName() + ".encode(e, " + name + ");");
    } else {
756
      decl().getDataType().Java_emitEncoder(env, name);
Anders Nilsson's avatar
Anders Nilsson committed
757
758
759
760
761
    }
  }

  public void Decl.Java_emitDecoder(Java_env env) {
    env.print("public static ");
762
    getDataType().Java_emitType(env);
Anders Blomdell's avatar
Anders Blomdell committed
763
    env.println(" decode(Decoder d) throws IOException {");
Anders Nilsson's avatar
Anders Nilsson committed
764
765
    env.indent();
    if (!isVoid()) {
766
      getDataType().Java_emitType(env);
Anders Nilsson's avatar
Anders Nilsson committed
767
      env.println(" result;");
768
      getDataType().Java_emitDecoder(env, "result");
Anders Nilsson's avatar
Anders Nilsson committed
769
770
771
772
773
774
775
      env.println("return result;");
    }
    env.unindent();
    env.println("}");
    env.println();
  }

776
  public void DataType.Java_emitDecoder(Java_env env, String name) {
777
778
    throw new Error(this.getClass().getName() +
		    ".Java_emitDecoder(Java_env env, String name)" +
Anders Nilsson's avatar
Anders Nilsson committed
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
		    " 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;
796
      case LABCOMM_SAMPLE: { env.println("d.decodeSampleRef();"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
797
798
799
800
801
802
803
804
805
806
807
808
809
810
    }
  }

  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";
811
      env.print(name + " = ");
Anders Nilsson's avatar
Anders Nilsson committed
812
813
814
815
      Java_emitNew(env, limit, getNumExp() - i);
      env.println(";");
      name = name + env.print_for_begin(limit);
    }
816
    getDataType().Java_emitDecoder(env, name);
Anders Nilsson's avatar
Anders Nilsson committed
817
818
819
820
821
822
823
824
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
    }
    env.unindent();
    env.println("}");
  }

  public void Exp.Java_emitDecoder(Java_env env) {
825
826
    throw new Error(this.getClass().getName() +
		    ".Java_emitDecoder(Java_env env)" +
Anders Nilsson's avatar
Anders Nilsson committed
827
828
829
830
831
832
833
834
		    " not declared");
  }

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

  public void VariableSize.Java_emitDecoder(Java_env env) {
835
    env.print("d.decodePacked32()");
Anders Nilsson's avatar
Anders Nilsson committed
836
837
838
839
840
841
842
843
  }

  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);
844
      f.getDataType().Java_emitDecoder(env, name + "." + f.getName());
Anders Nilsson's avatar
Anders Nilsson committed
845
846
847
848
849
850
851
    }
  }

  public void UserType.Java_emitDecoder(Java_env env, String name) {
    if (Java_needInstance()) {
      env.println(name + " = " + getName() + ".decode(d);");
    } else {
852
      decl().getDataType().Java_emitDecoder(env, name);
Anders Nilsson's avatar
Anders Nilsson committed
853
854
855
    }
  }

856
  public void DataType.Java_emitNew(Java_env env, String size) {
857
858
    throw new Error(this.getClass().getName() +
		    ".Java_emitNew(Java_env env, String size)" +
Anders Nilsson's avatar
Anders Nilsson committed
859
860
861
862
863
		    " not declared");
  }

  public void ArrayType.Java_emitNew(Java_env env, String size, int depth) {
    env.print("new ");
864
    getDataType().Java_emitTypePrefix(env);
Anders Nilsson's avatar
Anders Nilsson committed
865
    env.print("[" + size + "]");
866
    getDataType().Java_emitTypeSuffix(env);
Anders Nilsson's avatar
Anders Nilsson committed
867
868
869
870
871
    for (int i = 1 ; i < depth ; i++) {
      env.print("[]");
    }
  }

872
  public void DataType.Java_emitTypePrefix(Java_env env) {
873
874
    throw new Error(this.getClass().getName() +
		    ".Java_emitTypePrefix(Java_env env)" +
Anders Nilsson's avatar
Anders Nilsson committed
875
876
877
878
879
880
		    " not declared");
  }

  public void PrimType.Java_emitTypePrefix(Java_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { env.print("String"); } break;
Anders Blomdell's avatar
Anders Blomdell committed
881
      case LABCOMM_SAMPLE: { env.print("Class"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
882
883
884
885
886
887
888
889
      default: { env.print(getName()); } break;
    }
  }

  public void UserType.Java_emitTypePrefix(Java_env env) {
    if (Java_needInstance()) {
      env.print(getName());
    } else {
890
      decl().getDataType().Java_emitTypePrefix(env);
891
    }
Anders Nilsson's avatar
Anders Nilsson committed
892
893
894
  }

  public void ArrayType.Java_emitTypePrefix(Java_env env){
895
    getDataType().Java_emitTypePrefix(env);
Anders Nilsson's avatar
Anders Nilsson committed
896
897
898
899
900
901
  }

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

902
  public void DataType.Java_emitTypeSuffix(Java_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
903
904
905
906
  }

  public void UserType.Java_emitTypeSuffix(Java_env env) {
    if (! Java_needInstance()) {
907
      decl().getDataType().Java_emitTypeSuffix(env);
908
    }
Anders Nilsson's avatar
Anders Nilsson committed
909
910
911
  }

  public void ArrayType.Java_emitTypeSuffix(Java_env env){
912
    getDataType().Java_emitTypeSuffix(env);
Anders Nilsson's avatar
Anders Nilsson committed
913
914
915
916
917
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print("[]");
    }
  }

918
  public boolean DataType.Java_needInstance() {
919
920
    throw new Error(this.getClass().getName() +
		    ".Java_needInstance()" +
Anders Nilsson's avatar
Anders Nilsson committed
921
922
923
		    " not declared");
  }

924
925
926
927
  public boolean VoidType.Java_needInstance() {
    return false;
  }

Anders Nilsson's avatar
Anders Nilsson committed
928
929
930
931
932
  public boolean PrimType.Java_needInstance() {
    return false;
  }

  public boolean UserType.Java_needInstance() {
933
    return decl().getDataType().Java_needInstance();
Anders Nilsson's avatar
Anders Nilsson committed
934
935
936
937
938
939
940
  }

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

  public boolean ArrayType.Java_needInstance() {
941
    return getDataType().Java_needInstance();
Anders Nilsson's avatar
Anders Nilsson committed
942
943
  }

944
  public boolean DataType.Java_isPrimitive() {
Anders Nilsson's avatar
Anders Nilsson committed
945
946
947
948
949
950
951
    return false;
  }

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

952
  public void DataType.Java_emitInstance(Java_env env) {
953
954
    throw new Error(this.getClass().getName() +
		    ".Java_emitInstance(Java_env env)" +
Anders Nilsson's avatar
Anders Nilsson committed
955
956
957
958
959
960
961
962
963
964
		    " 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) {
965
    getDataType().Java_emitInstance(env);
Anders Nilsson's avatar
Anders Nilsson committed
966
967
968
969
970
971
972
973
  }

  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++) {
974
      getField(i).getDataType().Java_emitInstance(env);
Anders Nilsson's avatar
Anders Nilsson committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
    }
    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 ");
991
    getDataType().Java_emitType(env);
992
    env.println(" " + getName() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
993
994
  }

995
  public void DataType.Java_emitType(Java_env env) {
996
997
    throw new Error(this.getClass().getName() +
		    ".Java_emitType(Java_env env)" +
Anders Nilsson's avatar
Anders Nilsson committed
998
999
1000
		    " not declared");
  }

For faster browsing, not all history is shown. View entire blame