C_CodeGen.jrag 49.7 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
import java.util.Vector;

aspect C_CodeGenEnv {

  // Environment wrapper for C-code generation
  // handles qualid nesting, indentation, file writing and
  // prefix propagation

  public class C_env {

11
    final private static class C_printer {
Anders Nilsson's avatar
Anders Nilsson committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
      
      private boolean newline = true;
      private PrintStream out;

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

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

36
    public final int version; //labcomm version (2006 or 2014)
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
37
    public final String verStr; // version suffix to append (currently _2006 and empty string)
38

Anders Nilsson's avatar
Anders Nilsson committed
39
40
41
42
43
44
45
    public final String qualid;
    public final String lcName;
    public final String rawPrefix;
    public final String prefix;
    private int indent;
    public final int depth;
    private C_printer printer;
46
47
48
    public final int nestedLevel;
    private boolean rootIsPointer;
    private int rootLevel;
Anders Nilsson's avatar
Anders Nilsson committed
49

50
51
52
53
    public boolean versionHasMetaData() {
      return version != 2006;
    }

Anders Nilsson's avatar
Anders Nilsson committed
54
    private C_env(String qualid, String lcName, String rawPrefix, 
Tommy Olofsson's avatar
Tommy Olofsson committed
55
56
57
		  int indent, int depth, C_printer printer,
                  int nestedLevel, int version)
    {
58
      this.version = version;
59
      this.verStr = LabCommVersion.versionString(version);
Anders Nilsson's avatar
Anders Nilsson committed
60
61
62
      this.qualid = qualid;
      this.lcName = lcName;
      this.rawPrefix = rawPrefix;
63
64
65
66
      if (rawPrefix == null) {
        System.err.println("WARNING: prefix==null");
        this.prefix = ""; 
      } else if (rawPrefix.equals("")) {
67
68
69
70
        this.prefix = rawPrefix;
      } else {
        this.prefix = rawPrefix + "_";
      }
Anders Nilsson's avatar
Anders Nilsson committed
71
72
73
      this.indent = indent;
      this.depth = depth;
      this.printer = printer;
74
      this.nestedLevel = nestedLevel;
Anders Nilsson's avatar
Anders Nilsson committed
75
76
    }

Tommy Olofsson's avatar
Tommy Olofsson committed
77
78
79
80
    public C_env(String qualid, String lcName, String rawPrefix,
		 PrintStream out, int version)
    {
      this(qualid, lcName, rawPrefix, 0, 0, new C_printer(out), 0, version);
81
82
83
    }

    public C_env(String qualid, String lcName, String rawPrefix,
Tommy Olofsson's avatar
Tommy Olofsson committed
84
85
		 int indent, int depth, C_printer printer, int version) {
            this(qualid, lcName, rawPrefix, indent, depth, printer, 0, version);
Anders Nilsson's avatar
Anders Nilsson committed
86
87
88
    }

    public C_env nestArray(String suffix) {
Tommy Olofsson's avatar
Tommy Olofsson committed
89
90
      return new C_env(qualid + suffix, lcName, rawPrefix,
		       indent, depth + 1, printer, nestedLevel + 1, version);
Anders Nilsson's avatar
Anders Nilsson committed
91
92
93
94
    }

    public C_env nestStruct(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
Tommy Olofsson's avatar
Tommy Olofsson committed
95
		       indent, depth, printer, nestedLevel + 1, 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
    }

    public void indent() {
      indent++;
    }

    public void unindent() {
      indent--;
    }

    public String prefix() {
      return rawPrefix;
    }

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

114
115
116
117
    public void println() {
      printer.println(this, "");
    }

Anders Nilsson's avatar
Anders Nilsson committed
118
119
120
121
    public void println(String s) {
      printer.println(this, s);
    }

122
123
124
125
126
127
    public C_env setPointer() {
      rootIsPointer = true;
      rootLevel = nestedLevel;
      return this;
    }

Tommy Olofsson's avatar
Tommy Olofsson committed
128
    public String memberAccessor() {
129
130
      return (rootIsPointer && (nestedLevel == rootLevel)) ? "->" : ".";
    }
Tommy Olofsson's avatar
Tommy Olofsson committed
131
132
133
134

    public String accessor() {
      return (rootIsPointer && (nestedLevel == rootLevel)) ? "*" : "";
    }
Anders Nilsson's avatar
Anders Nilsson committed
135
136
137
138
139
140
141
142
143
  }

  public C_env ArrayType.C_Nest(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_Nest(C_env env)" + 
		    " not declared");
  }

  public C_env FixedArrayType.C_Nest(C_env env) {
Tommy Olofsson's avatar
Tommy Olofsson committed
144
    String index = env.memberAccessor() + "a";
Anders Nilsson's avatar
Anders Nilsson committed
145
146
147
148
149
150
151
    for (int i = 0 ; i < getNumExp() ; i++) {
      index += "[i_" + env.depth + "_" + i + "]";
    }
    return env.nestArray(index);
  }

  public C_env VariableArrayType.C_Nest(C_env env) {
Tommy Olofsson's avatar
Tommy Olofsson committed
152
    return env.nestArray(env.memberAccessor() + "a[i_" + env.depth + "]");
Anders Nilsson's avatar
Anders Nilsson committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  }


}

aspect C_IsDynamic {
  
  // Determine if a type has dynamically allocated data
  syn boolean Decl.C_isDynamic() = getType().C_isDynamic();
  syn boolean Type.C_isDynamic() = false;
  syn boolean PrimType.C_isDynamic() = getToken() == LABCOMM_STRING;
  syn boolean UserType.C_isDynamic() = 
    lookupType(getName()).getType().C_isDynamic();
  syn boolean StructType.C_isDynamic() {
    for (int i = 0 ; i < getNumField() ; i++) {
      if (getField(i).getType().C_isDynamic()) {
	return true;
      }
    }
    return false;
  }
  syn boolean FixedArrayType.C_isDynamic() = getType().C_isDynamic();
  syn boolean VariableArrayType.C_isDynamic() = true;
}

aspect C_CodeGen {

  public void Program.C_genH(PrintStream out, Vector includes, 
181
182
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
183
184
185
186
187
188
189
190
191
192
193
194

    // Hackish prettyprint preamble
    out.println("/* LabComm declarations:");
    pp(out);
    out.println("*/");
    env.println("");
    env.println("");
    env.println("#ifndef __LABCOMM_" + env.lcName + "_H__"); 
    env.println("#define __LABCOMM_" + env.lcName + "_H__");
    env.println("");

    // Include
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
195
    env.println("#include \"labcomm"+env.verStr+".h\"");
Anders Nilsson's avatar
Anders Nilsson committed
196
197
198
199
200
201
202
203
204
205
206
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");

    C_emitH(env);

    env.println("#endif");
  }

  public void Program.C_genC(PrintStream out, Vector includes, 
207
208
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
209
210

    // Include
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
211
212
    env.println("#include \"labcomm"+env.verStr+".h\"");
    env.println("#include \"labcomm"+env.verStr+"_private.h\"");
Anders Nilsson's avatar
Anders Nilsson committed
213
214
215
216
217
218
219
220
221
222
223
224
    for (int i = 0 ; i < includes.size() ; i++) {
      env.println("#include \"" + includes.get(i) + "\"");
    }
    env.println("");
    
    // Method Implementations
    C_emitC(env);
  }

  public void Program.C_emitH(C_env env) {
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitType(env);
225
      getDecl(i).C_emitDecoderDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
226
227
      getDecl(i).C_emitEncoderDeclaration(env);
      getDecl(i).C_emitSizeofDeclaration(env);
Tommy Olofsson's avatar
Tommy Olofsson committed
228
      getDecl(i).C_emitCopyDeclaration(env);
229
      getDecl(i).C_emitCopyDeallocationDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
230
231
      env.println("");
    }
Anders Blomdell's avatar
Anders Blomdell committed
232
    C_emitConstructorDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
233
234
235
236
237
    C_emitForAll(env);
  }

  public void Program.C_emitC(C_env env) {
    for (int i = 0; i < getNumDecl(); i++) {
238
      getDecl(i).C_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
239
240
      getDecl(i).C_emitDecoder(env);
      getDecl(i).C_emitDecoderRegisterHandler(env);
241
      getDecl(i).C_emitDecoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
242
243
      getDecl(i).C_emitEncoder(env);
      getDecl(i).C_emitEncoderRegisterHandler(env);
244
      getDecl(i).C_emitEncoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
245
      getDecl(i).C_emitSizeof(env);
246
      getDecl(i).C_emitCopy(env);
247
      getDecl(i).C_emitCopyDeallocation(env);
Anders Nilsson's avatar
Anders Nilsson committed
248
    }
249
    C_emitConstructor(env);
Anders Nilsson's avatar
Anders Nilsson committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  }
 
}

aspect C_Common {

  public void ArrayType.C_emitLoopVariables(C_env env) {
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.println("int i_" + env.depth + "_" + i + ";");
    }
  }

}

aspect C_Type {

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

  public void TypeDecl.C_emitType(C_env env) {
    env.println("#ifndef PREDEFINED_" + env.prefix + getName());
    env.print("typedef ");
    getType().C_emitType(env, env.prefix + getName());
    env.println(";");
    env.println("#endif");
  }

  public void SampleDecl.C_emitType(C_env env) {
    env.println("#ifndef PREDEFINED_" + env.prefix + getName());
    env.print("typedef ");
    getType().C_emitType(env, env.prefix + getName());
    env.println(";");
    env.println("#endif");
286
287
288
    env.println("extern const struct labcomm"+env.verStr+"_signature " +
                "*labcomm"+env.verStr+"_signature_" + env.prefix + getName() + 
                ";");
Anders Nilsson's avatar
Anders Nilsson committed
289
290
291
292
293
294
295
296
297
298
299
300
  }

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

  public void VoidType.C_emitType(C_env env, String name) {
    env.print("char " + name);
  }

301
302
303
304
  public void SampleRefType.C_emitType(C_env env, String name) {
    env.print("const struct labcomm_signature *" + name);
  }

Anders Nilsson's avatar
Anders Nilsson committed
305
306
  public void PrimType.C_emitType(C_env env, String name) {
    switch (getToken()) {
307
308
309
310
311
312
313
      case LABCOMM_BOOLEAN: { env.print("uint8_t"); } break;
      case LABCOMM_BYTE: { env.print("uint8_t"); } break;
      case LABCOMM_SHORT: { env.print("int16_t"); } break;
      case LABCOMM_INT: { env.print("int32_t"); } break;
      case LABCOMM_LONG: { env.print("int64_t"); } break;
      case LABCOMM_FLOAT: { env.print("float"); } break;
      case LABCOMM_DOUBLE: { env.print("double"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
      case LABCOMM_STRING: { env.print("char*"); } break;
    }
    env.print(" " + name);
  }

  public void UserType.C_emitType(C_env env, String name) {
    env.print(env.prefix + getName() + " " + name);
  }

  public void StructType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitType(env);
      env.println(";");
    }
    env.unindent();
    env.print("} " + name);
  }

  public void Field.C_emitType(C_env env) {
    getType().C_emitType(env, getName());
  }

  public void FixedArrayType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    StringBuffer index = new StringBuffer("a");
    for (int i = 0 ; i < getNumExp() ; i++) {
      index.append("[" + getExp(i).C_getValue() + "]");
    }
    getType().C_emitType(env, index.toString());
    env.println(";");
    env.unindent();
    env.print("} " + name);
  }

  public void VariableArrayType.C_emitType(C_env env, String name) {
    env.println("struct {");
    env.indent();
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (getExp(i) instanceof VariableSize) {
	env.println("int n_" + i + ";");
      } else {
	env.println("// n_" + i + "=" + getExp(i).C_getValue());
      }
    }
    getType().C_emitType(env, "*a");
    env.println(";");
    env.unindent();
    env.print("} " + name);
  }

  public String Exp.C_getValue() {
   throw new Error(this.getClass().getName() + 
		    ".C_getValue()" + 
		    " not declared");
  }

  public String IntegerLiteral.C_getValue() {
    return getValue();
  }

}

aspect C_Declarations {

381
  public void Decl.C_emitDecoderDeclaration(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
382
383
  }

384
  public void SampleDecl.C_emitDecoderDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
385
    env.println("int labcomm"+env.verStr+"_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
386
387
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
388
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
389
390
391
392
393
394
395
396
397
    env.println("void (*handler)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(");");
398

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
399
    env.println("int labcomm"+env.verStr+"_decoder_ioctl_" + env.prefix + getName() + "(");
400
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
401
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
402
403
404
405
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
406
407
408
409
  }
  
  public void Decl.C_emitEncoderDeclaration(C_env env) {
  }
410
//
Anders Nilsson's avatar
Anders Nilsson committed
411
  public void SampleDecl.C_emitEncoderDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
412
    env.println("int labcomm"+env.verStr+"_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
413
414
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
415
    env.println("struct labcomm"+env.verStr+"_encoder *e);");
Anders Nilsson's avatar
Anders Nilsson committed
416
417
    env.unindent();

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
418
    env.println("int labcomm"+env.verStr+"_encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
419
    env.indent();
420
421
422
423
    env.println("struct labcomm"+env.verStr+"_encoder *e");
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
424
425
    env.unindent();
    env.println(");");
426

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
427
    env.println("int labcomm"+env.verStr+"_encoder_ioctl_" + env.prefix + getName() + "(");
428
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
429
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
430
431
432
433
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  }

}

aspect C_Limit {

  public String Exp.C_getLimit(C_env env, int i) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitDecoderLimit(C_env env, int i)" + 
		    " not declared");
  }

  public String IntegerLiteral.C_getLimit(C_env env, int i) {
    return getValue();
  }

  public String VariableSize.C_getLimit(C_env env, int i) {
Tommy Olofsson's avatar
Tommy Olofsson committed
451
    return env.qualid + env.memberAccessor() + "n_" + i;
Anders Nilsson's avatar
Anders Nilsson committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  }
  
}

aspect C_Index {

  public void ArrayType.C_emitCalcIndex(C_env env) {
  }

  public void VariableArrayType.C_emitCalcIndex(C_env env) {
    env.print("int i_" + env.depth + " = ");

    String i_prefix = "i_" + env.depth + "_";
    String expr = i_prefix + "0";
    for (int i = 1 ; i < getNumExp() ; i++) {
      expr = "(" + expr + ") * " +
	getExp(i).C_getLimit(env, i) + " + " + 
	i_prefix + i;
    }
    env.println(expr + ";");
  }

}

aspect C_Decoder {

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

  public void TypeDecl.C_emitDecoder(C_env env) {
  }

  public void SampleDecl.C_emitDecoder(C_env env) {
    env = env.nestStruct("v");
489
    env.println("static void decode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
490
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
491
    env.println("struct labcomm"+env.verStr+"_reader *r,");
Anders Nilsson's avatar
Anders Nilsson committed
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
    env.println("void (*handle)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println(env.prefix + getName() + " v;");
    getType().C_emitDecoder(env);
    env.println("handle(&v, context);");
    if (C_isDynamic()) {
      env.println("{");
      env.indent();
      getType().C_emitDecoderDeallocation(env);
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

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

  public void VoidType.C_emitDecoder(C_env env) {
  }

526
527
528
529
  public void SampleRefType.C_emitDecoder(C_env env) {
    env.println(env.qualid + " = labcomm_internal_decoder_index_to_signature(" +
                             "r->decoder, labcomm"+env.verStr+"_read_int(r));");
  }
Anders Nilsson's avatar
Anders Nilsson committed
530
  public void PrimType.C_emitDecoder(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
531
    env.println(env.qualid + " = labcomm"+env.verStr+"_read_" + getName() + "(r);");
Anders Nilsson's avatar
Anders Nilsson committed
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
  }

  public void UserType.C_emitDecoder(C_env env) {
    lookupType(getName()).getType().C_emitDecoder(env);
  }

  public void StructType.C_emitDecoder(C_env env) {
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitDecoder(env);
    }
  }

  public void ArrayType.C_emitDecoder(C_env env) {
    C_emitDecoderDecodeLimit(env);
    C_emitDecoderArrayAllocate(env);
    env.println("{");
    env.indent();
    C_emitLoopVariables(env);
    for (int i = 0 ; i < getNumExp() ; i++) {
      String iterator = "i_" + env.depth + "_" + i;
      env.println("for (" + iterator + " = 0" +
		  " ; " +
		  iterator + " < " + getExp(i).C_getLimit(env, i) +
		  " ; " +
		  iterator + "++) {");
      env.indent();
    }
    C_emitCalcIndex(env);
    getType().C_emitDecoder(C_Nest(env));
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

  public void Field.C_emitDecoder(C_env env) {
    getType().C_emitDecoder(env.nestStruct("." + getName()));
  }

  public void Exp.C_emitDecoderDecodeLimit(C_env env, int i) {
  }

  public void VariableSize.C_emitDecoderDecodeLimit(C_env env, int i) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
577
    env.println(env.qualid + ".n_" + i + " = labcomm"+env.verStr+"_read_packed32(r);");
Anders Nilsson's avatar
Anders Nilsson committed
578
579
580
581
582
583
584
585
586
587
588
589
  }

  public void ArrayType.C_emitDecoderDecodeLimit(C_env env) {
    for (int i = 0 ; i < getNumExp() ; i++) {
      getExp(i).C_emitDecoderDecodeLimit(env, i);
    }
  }

  public void ArrayType.C_emitDecoderArrayAllocate(C_env env) {
  }

  public void VariableArrayType.C_emitDecoderArrayAllocate(C_env env) {
590
    env.print(env.qualid + 
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
591
              ".a = labcomm"+env.verStr+"_memory_alloc(r->memory, 1, sizeof(" + 
592
	      env.qualid + ".a[0])");
Anders Nilsson's avatar
Anders Nilsson committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    for (int i = 0 ; i < getNumExp() ; i++) {
      env.print(" * " + getExp(i).C_getLimit(env, i));
    }
    env.println(");");
  }

  // Code for deallocation of dynamically allocated data 

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

607
608
609
  public void SampleRefType.C_emitDecoderDeallocation(C_env env) {
  }

Anders Nilsson's avatar
Anders Nilsson committed
610
611
  public void PrimType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
612
      env.println("labcomm"+env.verStr+"_memory_free(r->memory, 1, " + 
613
                  env.qualid + ");");
Anders Nilsson's avatar
Anders Nilsson committed
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
    }
  }

  public void UserType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
      lookupType(getName()).getType().C_emitDecoderDeallocation(env);
    }
  }

  public void StructType.C_emitDecoderDeallocation(C_env env) {
    if (C_isDynamic()) {
      for (int i = 0 ; i < getNumField() ; i++) {
	getField(i).C_emitDecoderDeallocation(env);
      }
    }
  }

  public void ArrayType.C_emitDecoderDeallocation(C_env env) {
    if (getType().C_isDynamic()) {
      env.println("{");
      env.indent();
      C_emitLoopVariables(env);
      for (int i = 0 ; i < getNumExp() ; i++) {
	String iterator = "i_" + env.depth + "_" + i;
	env.println("for (" + iterator + " = 0" +
		    " ; " +
		    iterator + " < " + getExp(i).C_getLimit(env, i) +
		    " ; " +
		    iterator + "++) {");
	env.indent();
      }
      C_emitCalcIndex(env);
      getType().C_emitDecoderDeallocation(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.unindent();
	env.println("}");
      }
      env.unindent();
      env.println("}");
    }
  }

  public void VariableArrayType.C_emitDecoderDeallocation(C_env env) {
    super.C_emitDecoderDeallocation(env);
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
658
    env.println("labcomm"+env.verStr+"_memory_free(r->memory, 1, " + 
659
                env.qualid + ".a);");
Anders Nilsson's avatar
Anders Nilsson committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
  }

  public void Field.C_emitDecoderDeallocation(C_env env) {
    getType().C_emitDecoderDeallocation(env.nestStruct("." + getName()));
  }

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

  public void TypeDecl.C_emitDecoderRegisterHandler(C_env env) {
  }

  public void SampleDecl.C_emitDecoderRegisterHandler(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
676
    env.println("int labcomm"+env.verStr+"_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
677
678
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
679
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
680
681
682
683
684
685
686
687
688
689
690
    env.println("void (*handler)(");
    env.indent();
    env.println(env.prefix + getName() + " *v,");
    env.println("void *context");
    env.unindent();
    env.println("),");
    env.println("void *context");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
691
    env.println("return labcomm"+env.verStr+"_internal_decoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
692
693
    env.indent();
    env.println("d,");
694
    env.println("&signature_" + env.prefix + getName() + ",");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
695
696
    env.println("(labcomm"+env.verStr+"_decoder_function)decode_" + env.prefix + getName() + ",");
    env.println("(labcomm"+env.verStr+"_handler_function)handler,");
Anders Nilsson's avatar
Anders Nilsson committed
697
698
699
700
701
702
703
704
705
    env.println("context");
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }

}

706
707
aspect C_copy {

Tommy Olofsson's avatar
Tommy Olofsson committed
708
  private void SampleDecl.C_emitCopyFunctionParam(C_env env, String src,
Tommy Olofsson's avatar
Tommy Olofsson committed
709
710
						  String dst)
  {
Tommy Olofsson's avatar
Tommy Olofsson committed
711
712
713
714
715
716
717
718
    env.println("void labcomm" + env.verStr + "_copy_" +
                env.prefix + getName() + "(");
    env.indent();
    env.println("struct labcomm" + env.verStr + "_memory *mem,");
    env.println(env.prefix + getName() + " *" + dst + ",");
    env.println(env.prefix + getName() + " *" + src);
    env.unindent();
    env.print(")");
Tommy Olofsson's avatar
Tommy Olofsson committed
719
720
721
722
723
724
725
726
727
728
  }

  public void Decl.C_emitCopyDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeclaration(C_env env) {
    C_emitCopyFunctionParam(env, "src", "dst");
    env.println(";");
  }

729
730
731
732
733
734
735
736
737
738
739
740
741
742
  public void Decl.C_emitCopy(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitCopy(C_env env)" + 
		    " not declared");
  }

  public void TypeDecl.C_emitCopy(C_env env) {
  }

  public void SampleDecl.C_emitCopy(C_env env) {
    final String dst = "dst";
    final String src = "src";
    C_env env_src = env.nestStruct(src).setPointer();
    C_env env_dst = env.nestStruct(dst).setPointer();
Tommy Olofsson's avatar
Tommy Olofsson committed
743
744
745

    C_emitCopyFunctionParam(env_src, src, dst);
    env_src.println("");
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
    env_src.println("{");
    env_src.indent();
    getType().C_emitCopy(env_src, env_dst);
    env_src.unindent();
    env_src.println("}");
  }

  public void Type.C_emitCopy(C_env env_src, C_env env_dst) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitCopy(C_env env)" + 
		    " not declared");
  }

  public void VoidType.C_emitCopy(C_env env_src, C_env env_dst) {
  }

762
763
764
765
766
767
  public void SampleRefType.C_emitCopy(C_env env_src, C_env env_dst) {
    env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
                    env_src.accessor() + env_src.qualid + ";");
  
  }

768
  public void PrimType.C_emitCopy(C_env env_src, C_env env_dst) {
769
770
    if (C_isDynamic()) {
      env_src.println(String.format(
Tommy Olofsson's avatar
Tommy Olofsson committed
771
	  "%s%s = labcomm%s_memory_alloc(mem, 1, strlen(%s%s)+1);",
772
	  env_dst.accessor(), env_dst.qualid,
Tommy Olofsson's avatar
Tommy Olofsson committed
773
          env_src.verStr,
774
775
776
777
778
779
780
781
782
783
	  env_src.accessor(), env_src.qualid));
      env_src.println(String.format(
	  "memcpy(%s%s, %s%s, strlen(%s%s)+1);",
	  env_dst.accessor(), env_dst.qualid,
	  env_src.accessor(), env_src.qualid,
	  env_src.accessor(), env_src.qualid));
    } else {
      env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
		      env_src.accessor() + env_src.qualid + ";");
    }
784
785
786
787
788
789
790
791
792
793
794
795
796
  }

  public void UserType.C_emitCopy(C_env env_src, C_env env_dst) {
    lookupType(getName()).getType().C_emitCopy(env_src, env_dst);
  }

  public void StructType.C_emitCopy(C_env env_src, C_env env_dst) {
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitCopy(env_src, env_dst);
    }
  }

  public void ArrayType.C_emitCopy(C_env env_src, C_env env_dst) {
Tommy Olofsson's avatar
Tommy Olofsson committed
797
    C_emitCopyDecodeLimit(env_src, env_dst);
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    C_emitCopyArrayAllocate(env_src, env_dst);
    env_src.println("{");
    env_src.indent();
    C_emitLoopVariables(env_src);
    for (int i = 0 ; i < getNumExp() ; i++) {
      String iterator = "i_" + env_src.depth + "_" + i;
      env_src.println("for (" + iterator + " = 0" +
		  " ; " +
		  iterator + " < " + getExp(i).C_getLimit(env_src, i) +
		  " ; " +
		  iterator + "++) {");
      env_src.indent();
    }
    C_emitCalcIndex(env_src);
    getType().C_emitCopy(C_Nest(env_src), C_Nest(env_dst));
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env_src.unindent();
      env_src.println("}");
    }
    env_src.unindent();
    env_src.println("}");
  }

  public void Field.C_emitCopy(C_env env_src, C_env env_dst) {
Tommy Olofsson's avatar
Tommy Olofsson committed
822
    String fnam = env_src.memberAccessor() + getName();
823
824
825
    getType().C_emitCopy(env_src.nestStruct(fnam), env_dst.nestStruct(fnam));
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
826
827
  public void Exp.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst, int i) {
    // Ordinary array has no length-member.
828
829
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
830
831
832
833
  public void VariableSize.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst, int i) {
    String src = env_src.qualid + env_src.memberAccessor() + "n_" + i;
    String dst = env_dst.qualid + env_dst.memberAccessor() + "n_" + i;
    env_src.println(dst + " = " + src + ";");
834
835
  }

Tommy Olofsson's avatar
Tommy Olofsson committed
836
  public void ArrayType.C_emitCopyDecodeLimit(C_env env_src, C_env env_dst) {
837
    for (int i = 0 ; i < getNumExp() ; i++) {
Tommy Olofsson's avatar
Tommy Olofsson committed
838
      getExp(i).C_emitCopyDecodeLimit(env_src, env_dst, i);
839
840
841
842
843
844
845
846
847
    }
  }

  public void ArrayType.C_emitCopyArrayAllocate(C_env env_src, C_env env_dst) {
  }

  public void VariableArrayType.C_emitCopyArrayAllocate(C_env env_src,
							C_env env_dst)
  {
Tommy Olofsson's avatar
Tommy Olofsson committed
848
    env_src.print(env_dst.qualid + env_dst.memberAccessor() +
Tommy Olofsson's avatar
Tommy Olofsson committed
849
850
                  "a = labcomm" + env_src.verStr +
                  "_memory_alloc(mem, 1, sizeof(" +
Tommy Olofsson's avatar
Tommy Olofsson committed
851
		  env_src.qualid + env_src.memberAccessor() + "a[0])");
852
853
854
855
856
857
    for (int i = 0 ; i < getNumExp() ; i++) {
      env_src.print(" * " + getExp(i).C_getLimit(env_src, i));
    }
    env_dst.println(");");
  }

858
859
860
861
862
  // Code for deallocation of dynamically allocated data in a copy.

  private void SampleDecl.C_emitCopyDeallocationFunctionParam(C_env env,
							      String par)
  {
Tommy Olofsson's avatar
Tommy Olofsson committed
863
864
    env.println("void labcomm" + env.verStr + "_copy_free_" +
                env.prefix + getName() + "(");
865
    env.indent();
Tommy Olofsson's avatar
Tommy Olofsson committed
866
    env.println("struct labcomm" + env.verStr + "_memory *mem,");
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
    env.println(env.prefix + getName() + " *" + par);
    env.unindent();
    env.print(")");
  }

  public void Decl.C_emitCopyDeallocationDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeallocationDeclaration(C_env env) {
    C_emitCopyDeallocationFunctionParam(env, "c");
    env.println(";");
  }

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

  public void TypeDecl.C_emitCopyDeallocation(C_env env) {
  }

  public void SampleDecl.C_emitCopyDeallocation(C_env env) {
    String par = "par";
    env = env.nestStruct(par).setPointer();

    C_emitCopyDeallocationFunctionParam(env, par);
    env.println("");
    env.println("{");
    env.indent();
    getType().C_emitCopyDeallocation(env);
    env.unindent();
    env.println("}");
  }
901
902

  public void Type.C_emitCopyDeallocation(C_env env) {
903
904
    throw new Error(this.getClass().getName() +
  		    ".C_emitCopyDeallocation(C_env env)" +
905
906
907
  		    " not declared");
  }

908
909
910
  public void VoidType.C_emitCopyDeallocation(C_env env) {
  }

911
912
913
  public void SampleRefType.C_emitCopyDeallocation(C_env env) {
  }

914
915
  public void PrimType.C_emitCopyDeallocation(C_env env) {
    if (C_isDynamic()) {
Tommy Olofsson's avatar
Tommy Olofsson committed
916
      env.println("labcomm" + env.verStr + "_memory_free(mem, 1, " +
917
                  env.accessor() + env.qualid + ");");
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
    }
  }

  public void UserType.C_emitCopyDeallocation(C_env env) {
    if (C_isDynamic()) {
      lookupType(getName()).getType().C_emitCopyDeallocation(env);
    }
  }

  public void StructType.C_emitCopyDeallocation(C_env env) {
    if (C_isDynamic()) {
      for (int i = 0 ; i < getNumField() ; i++) {
  	getField(i).C_emitCopyDeallocation(env);
      }
    }
  }

  public void ArrayType.C_emitCopyDeallocation(C_env env) {
    if (getType().C_isDynamic()) {
      env.println("{");
      env.indent();
      C_emitLoopVariables(env);
      for (int i = 0 ; i < getNumExp() ; i++) {
  	String iterator = "i_" + env.depth + "_" + i;
  	env.println("for (" + iterator + " = 0" +
  		    " ; " +
  		    iterator + " < " + getExp(i).C_getLimit(env, i) +
  		    " ; " +
  		    iterator + "++) {");
  	env.indent();
      }
      C_emitCalcIndex(env);
      getType().C_emitCopyDeallocation(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
  	env.unindent();
  	env.println("}");
      }
      env.unindent();
      env.println("}");
    }
  }

  public void VariableArrayType.C_emitCopyDeallocation(C_env env) {
    super.C_emitCopyDeallocation(env);
Tommy Olofsson's avatar
Tommy Olofsson committed
962
    env.println("labcomm" + env.verStr + "_memory_free(mem, 1, " +
963
                env.qualid + env.memberAccessor() + "a);");
964
965
966
  }

  public void Field.C_emitCopyDeallocation(C_env env) {
967
968
    getType().C_emitCopyDeallocation(env.nestStruct(env.memberAccessor()
						    + getName()));
969
970
971
  }
}

972
973
974
975
976
977
978
979
980
981
982
983
aspect C_DecoderIoctl {

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

  public void TypeDecl.C_emitDecoderIoctl(C_env env) {
  }

  public void SampleDecl.C_emitDecoderIoctl(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
984
    env.println("int labcomm"+env.verStr+"_decoder_ioctl_" + env.prefix + getName() + "(");
985
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
986
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
987
988
989
990
991
992
993
994
995
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("int result;");
    env.println("va_list va;");
    env.println("va_start(va, ioctl_action);");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
996
    env.println("result = labcomm"+env.verStr+"_internal_decoder_ioctl(");
997
    env.indent();
998
    env.println("d, &signature_" + env.prefix + getName() + ", ");
999
    env.println("ioctl_action, va);");
1000
1001
1002
1003
1004
1005
1006
1007
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
}

Anders Nilsson's avatar
Anders Nilsson committed
1008
1009
1010
1011

aspect C_Encoder {

  public void Decl.C_emitEncoder(C_env env) {
1012
1013
1014
    throw new Error(this.getClass().getName() + 
        ".C_emitEncoder()" + 
        " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1015
1016
  }

1017
1018
1019
  public void TypeDecl.C_emitEncoder(C_env env) {
    // do nothing for type decls
  }
Anders Nilsson's avatar
Anders Nilsson committed
1020
1021
1022

  public void SampleDecl.C_emitEncoder(C_env env) {
    env = env.nestStruct("(*v)");
1023
    env.println("static int encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
1024
    env.indent();
1025
1026
1027
1028
    env.println("struct labcomm"+env.verStr+"_writer *w");
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
1029
1030
1031
1032
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1033
    env.println("int result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1034
    getType().C_emitEncoder(env);
1035
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
1036
1037
1038
1039
    env.unindent();
    env.println("}");

    // Typesafe encode wrapper
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1040
    env.println("int labcomm"+env.verStr+"_encode_" + env.prefix + getName() + "(");
1041
1042
1043
1044
    env.println("struct labcomm"+env.verStr+"_encoder *e");
    if(!isVoid() ) {
        env.println(", "+env.prefix + getName() + " *v");
    }
Anders Nilsson's avatar
Anders Nilsson committed
1045
1046
1047
1048
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1049
    env.println("return labcomm"+env.verStr+"_internal_encode(e, &signature_" + 
1050
		env.prefix + getName() + 
1051
1052
1053
		", (labcomm"+env.verStr+"_encoder_function)encode_" + 
                env.prefix + getName() +
		(!isVoid()?", v":", NULL")+");");
Anders Nilsson's avatar
Anders Nilsson committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
    env.unindent();
    env.println("}");
  }

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

  public void VoidType.C_emitEncoder(C_env env) {
1065
    env.println("result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1066
1067
  }

1068
1069
1070
1071
1072
1073
1074
  public void SampleRefType.C_emitEncoder(C_env env) {
    env.println("result = labcomm"+env.verStr+"_write_int(w, " + 
                "labcomm_internal_encoder_signature_to_index(w->encoder, " +
                env.qualid + "));");
    env.println("if (result != 0) { return result; }");
  }

Anders Nilsson's avatar
Anders Nilsson committed
1075
  public void PrimType.C_emitEncoder(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1076
    env.println("result = labcomm"+env.verStr+"_write_" + getName() + 
1077
                "(w, " + env.qualid + ");");
1078
    env.println("if (result != 0) { return result; }");
Anders Nilsson's avatar
Anders Nilsson committed
1079
1080
1081
  }

  public void UserType.C_emitEncoder(C_env env) {
1082
    decl().getType().C_emitEncoder(env);
Anders Nilsson's avatar
Anders Nilsson committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
  }

  public void StructType.C_emitEncoder(C_env env) {
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).C_emitEncoder(env);
    }
  }

  public void ArrayType.C_emitEncoder(C_env env) {
    C_emitEncoderEncodeLimit(env);
    env.println("{");
    env.indent();
    C_emitLoopVariables(env);
    for (int i = 0 ; i < getNumExp() ; i++) {
      String iterator = "i_" + env.depth + "_" + i;
      env.println("for (" + iterator + " = 0" +
		  " ; " +
		  iterator + " < " + getExp(i).C_getLimit(env, i) +
		  " ; " +
		  iterator + "++) {");
      env.indent();
    }
    C_emitCalcIndex(env);
    getType().C_emitEncoder(C_Nest(env));
    for (int i = getNumExp() - 1 ; i >= 0 ; i--) {
      env.unindent();
      env.println("}");
    }
    env.unindent();
    env.println("}");
  }

  public void Field.C_emitEncoder(C_env env) {
    getType().C_emitEncoder(env.nestStruct("." + getName()));
  }

  public void Exp.C_emitEncoderEncodeLimit(C_env env, int i) {
  }

  public void VariableSize.C_emitEncoderEncodeLimit(C_env env, int i) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1123
    env.println("labcomm"+env.verStr+"_write_packed32(w, " + env.qualid + ".n_" + i + ");");
Anders Nilsson's avatar
Anders Nilsson committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
  }

  public void ArrayType.C_emitEncoderEncodeLimit(C_env env) {
    for (int i = 0 ; i < getNumExp() ; i++) {
      getExp(i).C_emitEncoderEncodeLimit(env, i);
    }
  }

  public void Decl.C_emitEncoderRegisterHandler(C_env env) {
1133
1134
1135
1136
1137
1138
1139
1140
1141
    throw new Error(this.getClass().getName() + 
  		    ".C_emitEncoderRegisterHandler(C_env env)" + 
  		    " not declared");
  }
  
  public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
    // do nothing for type decls
  }
  
Anders Nilsson's avatar
Anders Nilsson committed
1142
  public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1143
    env.println("int labcomm"+env.verStr+"_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
1144
1145
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1146
    env.println("struct labcomm"+env.verStr+"_encoder *e");
Anders Nilsson's avatar
Anders Nilsson committed
1147
1148
1149
1150
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
1151
    C_emitUserTypeDeps(env, null, false); //XXX HERE BE DRAGONS
1152
                                          //currently set to false to turn off
1153
                                          //outputting of code
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1154
    env.println("return labcomm"+env.verStr+"_internal_encoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
1155
1156
    env.indent();
    env.println("e,");
1157
    env.println("&signature_" + env.prefix + getName() + ",");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1158
    env.println("(labcomm"+env.verStr+"_encoder_function)encode_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
1159
1160
1161
1162
1163
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178

}

aspect C_EncoderIoctl {

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

  public void TypeDecl.C_emitEncoderIoctl(C_env env) {
  }

  public void SampleDecl.C_emitEncoderIoctl(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1179
    env.println("int labcomm"+env.verStr+"_encoder_ioctl_" + env.prefix + getName() + "(");
1180
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1181
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
1182
1183
1184
1185
1186
1187
1188
1189
1190
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
    env.println("int result;");
    env.println("va_list va;");
    env.println("va_start(va, ioctl_action);");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1191
    env.println("result = labcomm"+env.verStr+"_internal_encoder_ioctl(");
1192
    env.indent();
1193
    env.println("e, &signature_" + env.prefix + getName() + ", ");
1194
    env.println("ioctl_action, va);");
1195
1196
1197
1198
1199
1200
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
Anders Nilsson's avatar
Anders Nilsson committed
1201
1202
1203

}

1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
aspect C_TypeDependencies {
  public void Decl.C_emitUserTypeDeps(C_env env, String via, boolean outputCode) {
    if( hasDependencies() ) {
        Iterator<Decl> it = type_dependencies().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

            t.C_emitUserTypeDeps(env, t.getName(), outputCode);
            if(outputCode) {
               System.out.println("Decl.C_emitUserTypeDeps registering "+t.getName());
               env.println("labcomm"+env.verStr+"_encoder_register_"+env.prefix + t.getName()+"(e);");
            } else {  // Just output a comment
	        String refpath = (via == null) ? "directly" : "indirectly via "+via;
	       env.println(" //Depends ("+refpath+") on "+t.getName() );
            }
        }
    } 
  }
  public void Decl.C_emitUserTypeRefs(C_env env, String via, boolean outputCode) {
    if( isReferenced() ) {
        Iterator<Decl> it = type_references().iterator();
        while(it.hasNext()) {
            Decl t = it.next();

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

Anders Nilsson's avatar
Anders Nilsson committed
1240
1241
aspect C_Signature {

1242
  public void ASTNode.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1243
    throw new Error(this.getClass().getName() + 
1244
		    ".C_emitSignature(C_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
1245
1246
1247
		    " not declared");
  }

1248
1249
1250
1251
  syn String Decl.C_DeclTypeString();
  eq SampleDecl.C_DeclTypeString() = "LABCOMM_SAMPLE";
  eq TypeDecl.C_DeclTypeString() = "LABCOMM_TYPEDEF";

1252
  public void Decl.C_emitSignature(C_env env) {
1253
    C_emitFlatSignature(env);
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
//
//  if( false && (isReferenced() || isSampleDecl())){ 
//    Signature signature = getSignature();
//    signature.C_emitSignature(env, !isSampleDecl());
//  } else {
//    env.println("// not emitting signature for "+getName()+isReferenced()+isSampleDecl());  
//  }
//  if(env.versionHasMetaData()) { 
//    if(isReferenced() || isSampleDecl()){
//        env.println("(int (*)(void *))labcomm"+env.verStr+"_signature_" + 
//	 	env.prefix + getName() + "_emit_signature");
//    } else {
//        env.println("NULL");  // HERE BE DRAGONS! Is it worth the size saving to skip emitting the emit_signature function for unused types?
//                              //                  The code won't likely end up in a target system anyway?
//    }
//  }
//  env.unindent();
//  env.println(" };");
//
1273
1274
1275
1276
1277
1278
1279
1280
1281
  }

  public void ASTNode.C_emitFlatSignature(C_env env) {
    throw new Error(this.getClass().getName() + 
                    ".C_emitFlatSignature(C_env env)" + 
                    " not declared");
  }

  public void Decl.C_emitFlatSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1282
1283
  }

1284
  public void SampleDecl.C_emitFlatSignature(C_env env){
Anders Nilsson's avatar
Anders Nilsson committed
1285
    env.println("static unsigned char signature_bytes_" + 
1286
1287
  	        env.prefix + getName() + "[] = {");
    SignatureList signature = flatSignature(env.version);
Anders Nilsson's avatar
Anders Nilsson committed
1288
1289
1290
1291
1292
    for (int i = 0 ; i < signature.size() ; i++) {
      String comment = signature.getComment(i);
      if (comment != null) {
        env.println(signature.getIndent(i) + "// " + comment);
      }
1293
      byte[] data = signature.getData(i, env.version);
Anders Nilsson's avatar
Anders Nilsson committed
1294
1295
1296
1297
1298
1299
1300
1301
1302
      if (data != null) {
        env.print(signature.getIndent(i));
        for (int j = 0 ; j < data.length ; j++) {
          env.print(data[j] + ", ");
        }
        env.println("");
      }
    }
    env.println("};");
1303

1304
    C_emitSizeofValue(env);
1305
1306
    env.println("static struct labcomm"+env.verStr+"_signature " +
                "signature_" + env.prefix + getName() + " = {");
1307
    env.indent();
1308
1309
    env.println("\"" + getName() + "\",");
    env.println("sizeof_" + env.prefix + getName() + ",");
1310
1311
1312
1313
1314
    env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),");
    env.println("signature_bytes_" + env.prefix + getName() + ",");
    env.println("0");
    env.unindent();
    env.println(" };");
1315
1316
1317
    env.println("const struct labcomm"+env.verStr+"_signature " +
                "*labcomm"+env.verStr+"_signature_" + env.prefix + getName() + 
                " = &signature_" + env.prefix + getName() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1318
1319
  }

1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
    public void Signature.C_emitSignature(C_env env, boolean decl){
      getSignatureList().C_emitSignature(env, decl);
    }

    public abstract void SignatureLine.C_emitSignature(C_env env, boolean decl);

    public void TypeRefSignatureLine.C_emitSignature(C_env env, boolean isDecl){ 
      //env.print(getIndentString());
      //env.println("LABCOMM_SIGDEF_SIGNATURE(labcomm"+env.verStr+"_signature_" + env.prefix + decl.getName() +"),");
    }

    public void DataSignatureLine.C_emitSignature(C_env env, boolean decl){ 
       //   String comment = getComment();
       //   if (comment != null && comment.length() > 0) {
       //     env.println(getIndentString() + "// " + comment);
       //   }
       //   byte[] data = getData(env.version);
       //   if (data != null && data.length > 0) {
       //     env.print(getIndentString());
       //     env.print("LABCOMM_SIGDEF_BYTES("+data.length+", \"");
       //     for (int j = 0 ; j < data.length ; j++) {
       //       byte d = data[j];
       //       //if(d>='a'&&d<='z' || d>='A'&&d<='Z'|| d>='0'&&d<='9'  )
       //       //  env.print(""+(char)d);
       //       //else
       //       env.print("\\x"+Integer.toHexString(d));
       //     }
       //     env.println("\"),");
       // }
    }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
//
//
//      byte[] data = getData(env.version);
//        if (data != null) {
//            for (int j = 0 ; j < data.length ; j++) {
//                env.print(getIndentString());
//                //env.print("printf(\"labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +")\\n\"); ");
//                env.print("labcomm"+env.verStr+"_write_byte( w, (unsigned char)"+ String.format("0x%02X ", data[j]) +"); ");
//                env.println("if (result != 0) { return result; }");
//            }
//            env.println();
//        }
//
//}
1364
  public void SignatureList.C_emitSignature(C_env env, boolean decl) { 
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
//  env.println("static struct labcomm_signature_data signature_tree_" + 
//  	 env.prefix + parentDecl().getName() + "[] = {");
//  env.indent();
//  for (int i = 0 ; i < size() ; i++) {
//    SignatureLine l = getSignatureLine(i);
//    l.C_emitSignature(env, decl);
//  }
//  
//  env.println("LABCOMM_SIGDEF_END");
//  env.println("};");
//  env.unindent();
//  env.println();
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
  }



//  public void SampleDecl.C_emitSignature(C_env env) {
//    env.println("static unsigned char signature_bytes_" + 
//		       env.prefix + getName() + "[] = {");
//    SignatureList signature = signature(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);
//      if (data != null) {
//        env.print(signature.getIndent(i));
//        for (int j = 0 ; j < data.length ; j++) {
//          env.print(data[j] + ", ");
//        }
//        env.println("");
//      }
//    }
//    env.println("};");
//    env.println("struct labcomm"+env.verStr+"_signature labcomm"+env.verStr+"_signature_" + 
//		env.prefix + getName() + " = {");
//    env.indent();
//    env.println("LABCOMM_SAMPLE, \"" + getName() + "\",");
//    env.println("(int (*)(struct labcomm"+env.verStr+"_signature *, void *))labcomm"+env.verStr+"_sizeof_" + 
//		env.prefix + getName() + ",");
//    env.println("sizeof(signature_bytes_" + env.prefix + getName() + "),");
//    env.println("signature_bytes_" + env.prefix + getName() + ",");
//    env.println("0");
//    env.unindent();
//    env.println(" };");
//  }

}
aspect C_Constructor {
1415
  public void ASTNode.C_emitConstructor(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1416
    throw new Error(this.getClass().getName() + 
1417
		    ".C_emitConstructor(C_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
1418
1419
1420
		    " not declared");
  }

1421
1422
1423
1424
  public void Program.C_emitConstructor(C_env env) {
    env.println("LABCOMM_CONSTRUCTOR void init_" +
		env.prefix + "_signatures(void)");
    env.println("{");
1425
    env.indent();
1426
1427
1428
1429
1430
1431
1432
    env.println("static int initialized = 0;");
    env.println("if (initialized == 0) {");
    env.indent();
    env.println("initialized = 1;");
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_emitConstructor(env);
    }
1433
    env.unindent();
1434
1435
1436
1437
1438
1439
1440
    env.println("}"); 
    env.unindent();
    env.println("}"); 
  }

  public void Decl.C_emitConstructor(C_env env) {
  }
1441
//XXX
1442
  public void SampleDecl.C_emitConstructor(C_env env) {
1443
    env.println("labcomm"+env.verStr+"_set_local_index(&signature_" + 
1444
		env.prefix + getName() + ");");
Anders Nilsson's avatar
Anders Nilsson committed
1445
1446
1447
  }


Anders Blomdell's avatar
Anders Blomdell committed
1448
1449
1450
1451
1452
1453
1454
  public void ASTNode.C_emitConstructorDeclaration(C_env env) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitConstructorDeclaration(C_env env)" + 
		    " not declared");
  }

  public void Program.C_emitConstructorDeclaration(C_env env) {
1455
    env.println("void init_" + env.prefix + "_signatures(void);");
Anders Blomdell's avatar
Anders Blomdell committed
1456
1457
  }

Anders Nilsson's avatar
Anders Nilsson committed
1458
1459
1460
}

aspect C_Sizeof {
1461
 public void Decl.C_emitSizeofDeclaration(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1462
1463
1464
  }

  public void SampleDecl.C_emitSizeofDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1465
    env.println("extern int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
1466
        "(" + env.prefix + getName() + " *v);");
Anders Nilsson's avatar
Anders Nilsson committed
1467
1468
1469
1470
1471
1472
  }

  public int Decl.C_fixedSizeof() {
    return getType().C_fixedSizeof();
  }

1473
1474
1475
1476
1477
1478
  public void Decl.C_emitSizeof(C_env env) {
  }

  public void SampleDecl.C_emitSizeof(C_env env) {
    env = env.nestStruct("(*v)");
    env.println("int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
1479
        "(" + env.prefix + getName() + " *v)");
1480
1481
    env.println("{");
    env.indent();
Anders Blomdell's avatar
Anders Blomdell committed
1482
    env.println("return labcomm"+env.verStr+"_internal_sizeof(" +
1483
                "&signature_" + env.prefix + getName() +
1484
1485
1486
1487
1488
                ", v);");
    env.unindent();
    env.println("}");
  }

Anders Nilsson's avatar
Anders Nilsson committed
1489
1490
  public int Type.C_fixedSizeof() {
    throw new Error(this.getClass().getName() + 
1491
1492
            ".C_fixedSizeof()" + 
            " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1493
1494
1495
1496
1497
1498
  }

  public int VoidType.C_fixedSizeof() {
    return 0;
  }

1499
1500
1501
1502
  public int SampleRefType.C_fixedSizeof() {
    return 4;
  }

Anders Nilsson's avatar
Anders Nilsson committed
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
  public int PrimType.C_fixedSizeof() {
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { return 1; } 
      case LABCOMM_BYTE: { return 1; } 
      case LABCOMM_SHORT: { return 2; } 
      case LABCOMM_INT: { return 4; } 
      case LABCOMM_LONG: { return 8; }
      case LABCOMM_FLOAT: { return 4; }
      case LABCOMM_DOUBLE: { return 8; }
      default: { 
1513
1514
1515
    throw new Error(this.getClass().getName() + 
            ".C_fixedSizeof()" + 
            " unknown size (" + getName() + ")"); 
Anders Nilsson's avatar
Anders Nilsson committed
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
      } 
    }
  }

  public int UserType.C_fixedSizeof() {
    return lookupType(getName()).getType().C_fixedSizeof();
  }

  public int StructType.C_fixedSizeof() {
    int result = 0;
    for (int i = 0 ; i < getNumField() ; i++) {
      result += getField(i).getType().C_fixedSizeof();
    }
    return result;
  }

  public int ArrayType.C_fixedSizeof() {
    int elements = 1;
    for (int i = 0 ; i < getNumExp() ; i++) {
      int n = Integer.parseInt(((IntegerLiteral)getExp(i)).getValue());
      elements = elements * n;
    }
    return getType().C_fixedSizeof() * elements;
  }

1541
  public void Decl.C_emitSizeofValue(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1542
1543
  }

1544
  public void SampleDecl.C_emitSizeofValue(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
1545
    env = env.nestStruct("(*v)");
1546
    env.println("static int sizeof_" + env.prefix + getName() + "(void *vv)");
Anders Nilsson's avatar
Anders Nilsson committed
1547
1548
    env.println("{");
    env.indent();
1549
    env.println("int result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
1550
    if (C_isDynamic()) {
1551
      env.println(env.prefix + getName() + " *v = vv;");
Anders Nilsson's avatar
Anders Nilsson committed
1552
1553
      getType().C_emitSizeof(env);
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1554
      env.println("result += " + C_fixedSizeof() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1555
    }    
Anders Blomdell's avatar
Anders Blomdell committed
1556
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
1557
1558
1559
1560
1561
1562
    env.unindent();
    env.println("}");
  }

  public void Type.C_emitSizeof(C_env env) {
    throw new Error(this.getClass().getName() + 
1563
1564
            ".C_emitSizeof(C_env env)" + 
            " not declared");
Anders Nilsson's avatar
Anders Nilsson committed
1565
1566
1567
1568
1569
  }

  public void PrimType.C_emitSizeof(C_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { 
Anders Blomdell's avatar
Anders Blomdell committed
1570
        env.print("{ int l = strlen(" + env.qualid + "); ");
1571
    env.println("result += labcomm"+env.verStr+"_size_packed32(l) + l; }"); 
Anders Nilsson's avatar
Anders Nilsson committed
1572
1573
      } break;
      default: { 
1574
1575
1576
    throw new Error(this.getClass().getName() + 
            ".C_emitSizeof(C_env env)" + 
            " known size (" + getName() + ")"); 
Anders Nilsson's avatar
Anders Nilsson committed
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
      } 
    }
  }

  public void UserType.C_emitSizeof(C_env env) {
    lookupType(getName()).getType().C_emitSizeof(env);
  }

  public void StructType.C_emitSizeof(C_env env) {
    int fixed = 0;
    for (int i = 0 ; i < getNumField() ; i++) {
      if (getField(i).getType().C_isDynamic()) {
1589
1590
    getField(i).getType().C_emitSizeof(
      env.nestStruct("." + getField(i).getName()));
Anders Nilsson's avatar
Anders Nilsson committed
1591
      } else {
1592
    fixed += getField(i).getType().C_fixedSizeof();
Anders Nilsson's avatar
Anders Nilsson committed
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
      }
    }
    if (fixed > 0) {
      env.println("result += " + fixed + ";");
    }
  }

  public void ArrayType.C_emitSizeof(C_env env) {
    if (getType().C_isDynamic()) {
      env.println("{");
      env.indent();
      C_emitLoopVariables(env);
      for (int i = 0 ; i < getNumExp() ; i++) {
1606
1607
1608
1609
1610
1611
1612
    String iterator = "i_" + env.depth + "_" + i;
    env.println("for (" + iterator + " = 0" +
            " ; " +
            iterator + " < " + getExp(i).C_getLimit(env, i) +
            " ; " +
            iterator + "++) {");
    env.indent();
Anders Nilsson's avatar
Anders Nilsson committed
1613
1614
1615
1616
      }
      C_emitCalcIndex(env);
      getType().C_emitSizeof(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
1617
1618
    env.unindent();
    env.println("}");
Anders Nilsson's avatar
Anders Nilsson committed
1619
1620
1621
1622
      }
      env.unindent();
      env.println("}");
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1623
      for (int i = 0 ; i < getNumExp() ; i++) {
1624
1625
        env.println("result += labcomm"+env.verStr+"_size_packed32(" + 
                getExp(i).C_getLimit(env, i) + ");");
Anders Blomdell's avatar
Anders Blomdell committed
1626
      }
Anders Nilsson's avatar
Anders Nilsson committed
1627
1628
      env.print("result += " + getType().C_fixedSizeof());
      for (int i = 0 ; i < getNumExp() ; i++) {
1629
    env.print(" * " + getExp(i).C_getLimit(env, i));
Anders Nilsson's avatar
Anders Nilsson committed
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
      }
      env.println(";");      
    }
  }
}

aspect C_forAll {

  public void Program.C_emitForAll(C_env env) {
    env.print("#define LABCOMM_FORALL_SAMPLES_" + env.lcName + 
		"(func, sep)");
    env.indent();
    boolean needSeparator = false;
    for (int i = 0; i < getNumDecl(); i++) {
      String s = getDecl(i).C_forAll(env);
      if (s != null) {
	if (needSeparator) { env.print(" sep"); }
	env.println(" \\");
	env.print(s);
	needSeparator = true;
      }
    }
    env.println("");
    env.unindent();
  }
  
  public String Decl.C_forAll(C_env env) {
    return null;
  }

  public String SampleDecl.C_forAll(C_env env) {
    return "func(" + getName() + ", " + env.prefix + getName() + ")";
  }

}

aspect C_Info {

1668
1669
  public void Program.C_info(PrintStream out, String prefix, int version) {
    C_env env = new C_env("", "", prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
    for (int i = 0; i < getNumDecl(); i++) {
      getDecl(i).C_info(env);
    }
  }

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

  public void TypeDecl.C_info(C_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
1682
    env.println(",C,typedef," + env.prefix + getName() + "," + 
1683
1684
                 env.prefix + getName() + "," +
                 C_info_type_or_void(env.prefix));
Anders Nilsson's avatar
Anders Nilsson committed
1685
1686
1687
  }

  public void SampleDecl.C_info(C_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
1688
    env.println(",C,sample," + env.prefix + getName() + "," + 
1689
1690
                 env.prefix + getName() + "," +
                 C_info_type_or_void(env.prefix));
Anders Nilsson's avatar
Anders Nilsson committed
1691
1692
  }

1693
1694
1695
1696
1697
1698
1699
1700
1701
  // make void types explicitly as they require special treatment
  // in encoder/decoder calls
  protected String Decl.C_info_type_or_void(String prefix) {
    if(isVoid() ) {
      return "void";
    } else {
      return prefix + getName() ;
    }
  }
Anders Nilsson's avatar
Anders Nilsson committed
1702
}