CS_CodeGen.jrag 29 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 CS_CodeGenEnv {

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

  public class CS_env {

11
    public final int version;
12
    public final String verStr;
Anders Nilsson's avatar
Anders Nilsson committed
13
14
15
16
17
    private int indent;
    private int depth;
    private CS_printer printer;
    private HashMap unique = new HashMap();

18
19
20
//    public boolean versionHasMetaData() {
//      return version != 2006;
//    }
21
    final private static class CS_printer {
Anders Nilsson's avatar
Anders Nilsson committed
22
23
24
25
26
27
28
29
30
      
      private boolean newline = true;
      private File file;
      private PrintStream out;
      private IOException exception;
      

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

     public CS_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(CS_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(CS_env env, String s) {
	checkOpen();
        print(env, s);
        out.println();
        newline = true;
      }
    }

83
84
    private CS_env(int indent, CS_printer printer, int version) {
      this.version = version;
Anders Nilsson's avatar
Anders Nilsson committed
85
86
      this.indent = indent;
      this.printer = printer;
87
      this.verStr = (version == 2006) ? "2006" : "";
Anders Nilsson's avatar
Anders Nilsson committed
88
89
    }

90
91
    public CS_env(File f, int version) {
      this(0, new CS_printer(f), version);
Anders Nilsson's avatar
Anders Nilsson committed
92
93
    }

94
95
    public CS_env(PrintStream out, int version) {
      this(0, new CS_printer(out), version);
Anders Nilsson's avatar
Anders Nilsson committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    }

    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;
115
116
117
      if (indent < 0) {
        throw new Error("Negative indent level");
      }
Anders Nilsson's avatar
Anders Nilsson committed
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
    }

    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 CS_StructName {

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

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

aspect CS_Void {

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

}

aspect CS_CodeGen {

  public void Program.CS_gen(String file, 
208
			     String namespace, int version) throws IOException {
Anders Nilsson's avatar
Anders Nilsson committed
209
    // Registration class
210
    CS_env env = new CS_env(new File(file), version);
Anders Nilsson's avatar
Anders Nilsson committed
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
    if (namespace != null && namespace.length() > 0) {
      env.println("namespace " + namespace + "{");
      env.indent();
    }
    env.println("using System;");
    env.println("using se.lth.control.labcomm;");
    for (int i = 0; i < getNumDecl(); i++) {
      Decl d = getDecl(i);
      try {
        d.CS_emitClass(env);
      } catch (Error e) {
	System.err.println(d.getName());
	throw e;
      }
    }
    if (namespace != null && namespace.length() > 0) {
      env.unindent();
      env.println("}");
    }
  }

}

aspect CS_Register {

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

  }

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

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

  public void TypeDecl.CS_emitTypeRegister(CS_env env) {
    // TODO
  }

}

aspect CS_Class {

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

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
  public void Decl.CS_emitDeclPP(CS_env env){
	env.println("/* ");
	pp(env.getPrintStream());
	
	CS_emitUserTypeDeps(env,null,false);
	CS_emitUserTypeRefs(env,null,false);
	env.println("*/");
  
  }
  
  public void Decl.CS_emitUserTypeDeps(CS_env env, String via, boolean outputCode){
//	if(env.versionHasMetaData() && hasDependencies() || isReferenced() ) {
//	if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
//	   env.println("if(sendMetaData){");
//	   env.indent();
//	}
//	Iterator<Decl> it = type_dependencies().iterator();
//	while(it.hasNext()) {
//	    Decl t = it.next();
//
//	    t.CS_emitUserTypeDeps(env, t.getName(), outputCode);
//	    if( outputCode && t.getType().isUserType() ) {
//               //env.println("/* FIXME: " + t.getName() + " does not exist");
//	       env.println(t.getName()+".register(e, sendMetaData);");
//               //env.println("*/");
//	    } else {  // Just output a comment
//		String refpath = (via == null) ? "directly" : "indirectly via "+via;
//	       //env.println(" //Depends ("+refpath+") on "+t.getName() + " (" + t +") " );
//	       //env.println(" //Depends ("+refpath+") on "+t.getName() );
//	       env.println(" //Depends ("+refpath+") on "+t.getName()+" -- "+t.getType() );
//	    }
//	}
//	if(env.versionHasMetaData() && isSampleDecl() && outputCode) {
//	   env.unindent();
//	   env.println("}");
//	}
//    }
  }
  
  public void Decl.CS_emitUserTypeRefs(CS_env env, String via, boolean outputCode){
     if( isReferenced() ) {
        Iterator<Decl> it = type_references().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.CS_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.CS_emitRegisterEncoder(CS_env env) {
    env.println("public static void register(Encoder e){");
    env.indent();
    env.println("e.register(Dispatcher.singleton());");
    env.unindent();
    env.println("}");

//    env.println();
//    env.println("public static void register(Encoder e, bool sendMetaData){");
//    env.indent();
//
//    CS_emitUserTypeDeps(env, null, true);
//    if(env.versionHasMetaData()) {
//        env.println("e.register(Dispatcher.singleton(), sendMetaData);");
//    } else {
//        env.println("e.register(Dispatcher.singleton());");
//    }
//    env.unindent();
//    env.println("}");
//    env.println();
  }
  
Anders Nilsson's avatar
Anders Nilsson committed
351
352
353
354
355
356
357
  public void TypeDecl.CS_emitClass(CS_env env) {
    if (getType().CS_needInstance()) {
      // Hackish prettyprint preamble
      env.println("/* ");
      pp(env.getPrintStream());
      env.println("*/");
      env.println();
Anders Blomdell's avatar
Anders Blomdell committed
358
      env.println("public class " + getName() + " : SampleType {");
Anders Nilsson's avatar
Anders Nilsson committed
359
360
361
      env.println();
      env.indent();
      getType().CS_emitInstance(env);
362
363
364
365
366
      if( isReferenced()) {
        CS_emitRegisterEncoder(env);
	CS_emitDispatcher(env,false);
      }
      CS_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
367
368
369
370
371
372
373
374
375
376
377
378
      CS_emitEncoder(env);
      CS_emitDecoder(env);
      env.unindent();
      env.println("}");
    }
  }

  public void SampleDecl.CS_emitClass(CS_env env) {
    env.println("/* ");
    pp(env.getPrintStream());
    env.println("*/");
    env.println();
Anders Blomdell's avatar
Anders Blomdell committed
379
    env.println("public class " + getName() + " : Sample {");
Anders Nilsson's avatar
Anders Nilsson committed
380
381
382
    env.println();
    env.indent();
    getType().CS_emitInstance(env);
Anders Blomdell's avatar
Anders Blomdell committed
383
    env.println("public interface Handler : SampleHandler {");
Anders Nilsson's avatar
Anders Nilsson committed
384
385
386
387
388
389
390
391
    env.print("  void handle(");
    if (!isVoid()) {
      getType().CS_emitType(env);
      env.print(" value");
    }
    env.println(");");
    env.println("}");
    env.println();
Anders Blomdell's avatar
Anders Blomdell committed
392
    env.println("public static void register(Decoder d, Handler h) {");
Anders Nilsson's avatar
Anders Nilsson committed
393
394
395
396
397
    env.indent();
    env.println("d.register(new Dispatcher(), h);");
    env.unindent();
    env.println("}");
    env.println();
398
/*
Anders Blomdell's avatar
Anders Blomdell committed
399
    env.println("public static void register(Encoder e) {");
Anders Nilsson's avatar
Anders Nilsson committed
400
401
402
403
    env.indent();
    env.println("e.register(new Dispatcher());");
    env.unindent();
    env.println("}");
404
405
406
    env.println(); */
    
    CS_emitRegisterEncoder(env);
Anders Nilsson's avatar
Anders Nilsson committed
407

408
    /*env.println("private class Dispatcher : LabCommDispatcher {");
Anders Nilsson's avatar
Anders Nilsson committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    env.indent();
    env.println(); 
    env.println("public Type getSampleClass() {");
    env.indent();
    env.println("return typeof(" + getName() + ");");
    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(); 
Anders Blomdell's avatar
Anders Blomdell committed
429
    env.println("public void decodeAndHandle(Decoder d, SampleHandler h) {");
Anders Nilsson's avatar
Anders Nilsson committed
430
431
432
433
434
435
436
437
438
439
440
441
    env.indent();
    if (isVoid()) {
      env.println(getName() + ".decode(d);");
      env.println("((Handler)h).handle();"); 
    } else {
      env.println("((Handler)h).handle(" + getName() + ".decode(d));"); 
    }
    env.unindent();
    env.println("}");
    env.println("");
    env.unindent();
    env.println("}");
442
443
    env.println("");*/
    CS_emitDispatcher(env,true);
Anders Nilsson's avatar
Anders Nilsson committed
444
445
446

    CS_emitEncoder(env);
    CS_emitDecoder(env);
447
    /*env.println("private static byte[] signature = new byte[] {");
Anders Nilsson's avatar
Anders Nilsson committed
448
    env.indent();
449
    SignatureList signature = flatSignature(env.version);
Anders Nilsson's avatar
Anders Nilsson committed
450
451
452
453
454
    for (int i = 0 ; i < signature.size() ; i++) {
      String comment = signature.getComment(i);
      if (comment != null) {
        env.println(signature.getIndent(i) + "// " + comment);
      }
455
      byte[] data = signature.getData(i, env.version);
Anders Nilsson's avatar
Anders Nilsson committed
456
457
458
459
460
461
462
463
464
465
466
      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();
467
468
    env.println();*/
    CS_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
469
470
471
    env.println("}");
  }

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
  public void Decl.CS_emitSignature(CS_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
    CS_emitFlatSignature(env);
//    if(isReferenced() || isSampleDecl()){
//      Signature signature = getSignature();
//      signature.CS_emitSignature(env, !isSampleDecl());
//    }
  }

  public void Decl.CS_emitFlatSignature(CS_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();
  }

  //XXX TODO: refactor: split into a static class ("TypeDefSingleton"?)and a (smaller) dispatcher
  public void Decl.CS_emitDispatcher(CS_env env, boolean isSample) {
    String genericStr = ""; //env.versionHasMetaData()?"<"+getName()+">":""; 
    env.println("private class Dispatcher : SampleDispatcher{");
    env.indent();
    env.println();
    env.println("private static Dispatcher single;");
    env.println();
    env.println("public static Dispatcher singleton() {");
    env.indent();
    env.println("lock(typeof(Dispatcher)) {");
    env.indent();
    env.println("if( single == null ) single = new Dispatcher();");
    env.println("return single;");
    env.unindent();
    env.println("}");
    env.unindent();
    env.println("}");
    env.println();
    env.println("public Type getSampleClass() {");
    env.indent();
    env.println("return typeof(" + getName() + ");");
    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 getTypeDeclTag() {");
    env.indent();
    if(isSample) {
      env.println("return Constant.SAMPLE_DEF;");
    } else {
      env.println("return Constant.TYPE_DEF;");
    }
    env.unindent();
    env.println("}");
    env.println();
    env.println("public bool isSample() {");
    env.indent();
    env.println("return "+isSample+";");
    env.unindent();
    env.println("}");
    env.println("public bool hasStaticSignature() {");
    env.indent();
    env.println("return "+!hasDependencies()+";");
    env.unindent();
    env.println("}");
    env.println();
    env.println("/** return the flat signature. Intended use is on decoder side */");
    env.println("public byte[] getSignature() {");
    env.indent();
    env.println("return signature;");
    env.unindent();
    env.println("}");
    env.println();
//    env.println("public void encodeSignature(Encoder e) throws IOException{");
//    env.indent();
//    env.println("emitSignature(e);");
//    env.unindent();
//    env.println("}");
//    env.println();
//  env.println("public void encodeSignatureMetadata(Encoder e, int index){");
//  env.indent();
//  env.println("e.encodePacked32(Constant.TYPE_DEF);");
//  env.println("e.encodePacked32(index);");
//  env.println("e.encodeString(getName());");
//  env.println("emitSignature(e);");
//  env.unindent();
//  env.println("}");
//  env.println();
    env.println("public bool canDecodeAndHandle() {");
    env.indent();
    env.println("return "+isSample+";");
    env.unindent();
    env.println("}");
    env.println();
    env.println("public void decodeAndHandle(Decoder d,");
    env.println("                            SampleHandler h) {");
    env.indent();
    if( isSample) {
        if (isVoid()) {
          env.println(getName() + ".decode(d);");
          env.println("((Handler)h).handle();");
        } else {
          env.println("((Handler)h).handle(" + getName() + ".decode(d));");
        }
    } else {
        env.println("throw new Exception(\"A typedef has no handler, the corresponding method on the sample class should be called.\");");
    }
    env.unindent();
    env.println("}");
    env.println("");
    env.unindent();
    env.println("}");
    env.println("");

 } //TODO, fix above method
  
Anders Nilsson's avatar
Anders Nilsson committed
609
  public void TypeDecl.CS_emitEncoder(CS_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
610
    env.print("public static void encode(Encoder e");
Anders Nilsson's avatar
Anders Nilsson committed
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    if (!isVoid()) {
      env.print(", ");
      getType().CS_emitType(env);
      env.print(" value");
    }
    env.println(") {");
    env.indent();
    getType().CS_emitEncoder(env, "value");
    env.unindent();
    env.println("}");
    env.println();
  }

  public void SampleDecl.CS_emitEncoder(CS_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
625
    env.print("public static void encode(Encoder e");
Anders Nilsson's avatar
Anders Nilsson committed
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
    if (!isVoid()) {
      env.print(", ");
      getType().CS_emitType(env);
      env.print(" value");
    }
    env.println(") {");
    env.indent();
    env.println("e.begin(typeof(" + getName() + "));");
    getType().CS_emitEncoder(env, "value");
    env.println("e.end(typeof(" + getName() + "));");
    env.unindent();
    env.println("}");
    env.println();
  }

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

  public void VoidType.CS_emitEncoder(CS_env env, String name) {
  }

  public void PrimType.CS_emitEncoder(CS_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.CS_emitEncoder(CS_env env, String name) {
665
    env.print_block_begin();
Anders Nilsson's avatar
Anders Nilsson committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    int baseDepth = env.getDepth();
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = getExp(i).CS_emitEncoder(env, 
					      name + ".GetLength(" + i + ")");
      env.println("int i_" + (baseDepth + i) + "_max = " + limit + ";");
    }
    String index = null;
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = "i_" + (baseDepth + i) + "_max";
      if (i == 0) {
        index = env.print_for_begin(limit);
      } else {
        index = index + ", " + env.print_for_begin(limit);
      }
    }
    getType().CS_emitEncoder(env, name + "[" + index + "]");
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
    }
685
    env.print_block_end();
Anders Nilsson's avatar
Anders Nilsson committed
686
687
688
689
690
691
692
693
694
695
696
697
698
  }
  
  public String Exp.CS_emitEncoder(CS_env env, String name) {
    throw new Error(this.getClass().getName() + 
		    ".CS_emitEncoder(CS_env env, String name)" + 
		    " not declared");
  }

  public String IntegerLiteral.CS_emitEncoder(CS_env env, String name) {
    return getValue();
  }

  public String VariableSize.CS_emitEncoder(CS_env env, String name) {
Anders Blomdell's avatar
Anders Blomdell committed
699
    env.println("e.encodePacked32(" + name + ");");
Anders Nilsson's avatar
Anders Nilsson committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
    return name;
  }

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

  public void UserType.CS_emitEncoder(CS_env env, String name) {
    if (CS_needInstance()) {
      env.println(getName() + ".encode(e, " + name + ");");
    } else {
      decl().getType().CS_emitEncoder(env, name);
    }
  }

  public void Decl.CS_emitDecoder(CS_env env) {
    env.print("public static ");
    getType().CS_emitType(env);
Anders Blomdell's avatar
Anders Blomdell committed
721
    env.println(" decode(Decoder d) {");
Anders Nilsson's avatar
Anders Nilsson committed
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
    env.indent();
    if (!isVoid()) {
      getType().CS_emitType(env);
      env.println(" result;");
      getType().CS_emitDecoder(env, "result");
      env.println("return result;");
    }
    env.unindent();
    env.println("}");
    env.println();
  }

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

  public void VoidType.CS_emitDecoder(CS_env env, String name) {
  }

  public void PrimType.CS_emitDecoder(CS_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.CS_emitDecoder(CS_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).CS_emitDecoder(env);
      env.println(";");
    }
    env.print(name + " = new "); 
    getType().CS_emitTypePrefix(env);
    env.print("[");
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (i > 0) {
	env.print(", ");
      }
      env.print("i_" + (baseDepth + i) + "_max");
    }
775
    env.print("]");
Anders Nilsson's avatar
Anders Nilsson committed
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
    getType().CS_emitTypeSuffix(env);
    env.println(";");
    
    String index = null;
    for (int i = 0 ; i < getNumExp() ; i++) {
      String limit = "i_" + (baseDepth + i) + "_max";
      if (i == 0) {
        index = env.print_for_begin(limit);
      } else {
        index = index + ", " + env.print_for_begin(limit);
      }
    }
    getType().CS_emitDecoder(env, name + "[" + index + "]");
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print_for_end();
    }
    env.unindent();
    env.println("}");
  }

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

  public void IntegerLiteral.CS_emitDecoder(CS_env env) {
    env.print(getValue());
  }

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

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

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

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

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

  public void UserType.CS_emitTypePrefix(CS_env env) {
    if (CS_needInstance()) {
      env.print(getName());
    } else {
      decl().getType().CS_emitTypePrefix(env);
    } 
  }

  public void ArrayType.CS_emitTypePrefix(CS_env env){
    getType().CS_emitTypePrefix(env);
  }

  public void StructType.CS_emitTypePrefix(CS_env env){
    env.print(CS_structName());
  }

  public void Type.CS_emitTypeSuffix(CS_env env) {
  }

  public void UserType.CS_emitTypeSuffix(CS_env env) {
    if (! CS_needInstance()) {
      decl().getType().CS_emitTypeSuffix(env);
    } 
  }

  public void ArrayType.CS_emitTypeSuffix(CS_env env){
    env.print("[");
    for (int i = 1 ; i < getNumExp() ; i++) {
      env.print(",");
    }
    env.print("]");
872
    getType().CS_emitTypeSuffix(env);
Anders Nilsson's avatar
Anders Nilsson committed
873
874
875
876
877
878
879
880
  }

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

881
882
883
884
  public boolean VoidType.CS_needInstance() {
    return false;
  }

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

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

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

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

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

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

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

  public void VoidType.CS_emitInstance(CS_env env) {
  }

  public void PrimType.CS_emitInstance(CS_env env) {
  }

  public void ArrayType.CS_emitInstance(CS_env env) {
    getType().CS_emitInstance(env);
  }

  public void StructType.CS_emitInstance(CS_env env) {
    if (CS_Depth() > 0) {
927
      env.println("public class " + CS_structName() + " {");
Anders Nilsson's avatar
Anders Nilsson committed
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
      env.indent();
    }
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).getType().CS_emitInstance(env);
    }
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).CS_emitField(env);
    }
    if (CS_Depth() > 0) {
      env.unindent();
      env.println("}");
    }
    env.println();
  }

  public void UserType.CS_emitInstance(CS_env env) {
  }

  public void Field.CS_emitField(CS_env env) {
    env.print("public ");
    getType().CS_emitType(env);
    env.println(" " + getName() + ";");    
  }

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

  public void VoidType.CS_emitType(CS_env env) {
    env.print("void");
  }

  public void PrimType.CS_emitType(CS_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { env.print("String"); } break;
      case LABCOMM_BOOLEAN: { env.print("bool"); } break;
      default: { env.print(getName()); } break;
    }
  }

  public void UserType.CS_emitType(CS_env env) {
    decl().getType().CS_emitType(env);
  }

  public void ArrayType.CS_emitType(CS_env env){
975
    getType().CS_emitTypePrefix(env);
Anders Nilsson's avatar
Anders Nilsson committed
976
977
978
979
980
    env.print("[");
    for (int i = 1 ; i < getNumExp() ; i++) {
      env.print(",");
    }
    env.print("]");
981
    getType().CS_emitTypeSuffix(env);
Anders Nilsson's avatar
Anders Nilsson committed
982
983
984
985
986
987
988
989
  }

  public void StructType.CS_emitType(CS_env env){
    env.print(CS_structName());
  }

}

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
aspect CS_Signature {
    public void Signature.CS_emitSignature(CS_env env, boolean decl){
 
      SignatureList sl = getSignatureList();
      sl.CS_emitSignature(env, decl);
    }

    public abstract void SignatureLine.CS_emitSignature(CS_env env, boolean decl);

    public void TypeRefSignatureLine.CS_emitSignature(CS_env env, boolean isDecl){
      env.print(getIndentString());
      env.println("e.encodePacked32(e.getTypeId( typeof("+decl.getName()+")));");
    }

    public void DataSignatureLine.CS_emitSignature(CS_env env, boolean decl){
        byte[] data = getData(env.version);
          if (data != null) {
              env.print(getIndentString());
              for (int j = 0 ; j < data.length ; j++) {
                  //env.print("e.encodeByte((byte)"+data[j]+");");
                  env.print("e.encodeByte((byte)"+ String.format("0x%02X ", data[j]) +"); ");
              }
              env.println();
          }
  }
    public void SignatureList.CS_emitSignature(CS_env env, boolean decl) {
      env.println("private static void emitSignature(Encoder e){");
      env.indent();
      for (int i = 0 ; i < size() ; i++) {
        String comment = getComment(i);
        if (comment != null && comment.length() > 0) {
            env.println(getIndent(i) + "// " + comment);
        }
        SignatureLine l = getSignatureLine(i);
        l.CS_emitSignature(env, decl);
      }
      env.println("}");
      env.unindent();
  }

}

Anders Nilsson's avatar
Anders Nilsson committed
1032
1033
aspect CS_Info {

1034
1035
  public void Program.CS_info(PrintStream out, String namespace, int version) {
    CS_env env = new CS_env(out, version);
Anders Nilsson's avatar
Anders Nilsson committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
    if (namespace == null) {
      namespace = ""; 
    } else {
      namespace = namespace + "."; 
    }
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).CS_info(env, namespace);
    }
  }

  public void Decl.CS_info(CS_env env, String namespace) {
    throw new Error(this.getClass().getName() + 
		    ".CS_info(CS_env env)" + 
		    " not declared");
  }

  public void TypeDecl.CS_info(CS_env env, String namespace) {
Anders Blomdell's avatar
Anders Blomdell committed
1053
    env.print(";C#;typedef;" + namespace + getName() + ";");
1054
    getType().CS_emitType(env) ;
1055
    env.print(";not_applicable_for_C#");
Anders Nilsson's avatar
Anders Nilsson committed
1056
1057
1058
1059
    env.println();
  }

  public void SampleDecl.CS_info(CS_env env, String namespace) {
Anders Blomdell's avatar
Anders Blomdell committed
1060
    env.print(";C#;sample;" + namespace + getName() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1061
    getType().CS_emitType(env);
1062
    env.print(";not_applicable_for_C#");
Anders Nilsson's avatar
Anders Nilsson committed
1063
1064
1065
1066
    env.println();
  }

}
1067