C_CodeGen.jrag 32.1 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 2013)
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
46
47
    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;

    private C_env(String qualid, String lcName, String rawPrefix, 
48
49
		  int indent, int depth, C_printer printer, int version) {
      this.version = version;
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
50
      this.verStr = (version == 2006 ? "2006" : ""); 
Anders Nilsson's avatar
Anders Nilsson committed
51
52
53
      this.qualid = qualid;
      this.lcName = lcName;
      this.rawPrefix = rawPrefix;
54
55
56
57
58
      if (rawPrefix.equals("")) {
        this.prefix = rawPrefix;
      } else {
        this.prefix = rawPrefix + "_";
      }
Anders Nilsson's avatar
Anders Nilsson committed
59
60
61
62
63
64
      this.indent = indent;
      this.depth = depth;
      this.printer = printer;
    }

    public C_env(String qualid, String lcName, String rawPrefix, 
65
66
		 PrintStream out, int version) {
      this(qualid, lcName, rawPrefix, 0, 0, new C_printer(out), version);
Anders Nilsson's avatar
Anders Nilsson committed
67
68
69
70
    }

    public C_env nestArray(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
71
		       indent, depth + 1, printer, version);
Anders Nilsson's avatar
Anders Nilsson committed
72
73
74
75
    }

    public C_env nestStruct(String suffix) {
      return new C_env(qualid + suffix, lcName, rawPrefix, 
76
		       indent, depth, printer, version);
Anders Nilsson's avatar
Anders Nilsson committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    }

    public void indent() {
      indent++;
    }

    public void unindent() {
      indent--;
    }

    public String prefix() {
      return rawPrefix;
    }

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

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

  }

  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) {
    String index = ".a";
    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) {
    return env.nestArray(".a[i_" + env.depth + "]");
  }


}

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, 
145
146
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
147
148
149
150
151
152
153
154
155
156
157
158

    // 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
159
    env.println("#include <stdint.h>");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
160
    env.println("#include \"labcomm"+env.verStr+".h\"");
Anders Nilsson's avatar
Anders Nilsson committed
161
162
163
164
165
166
167
168
169
170
171
    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, 
172
173
			     String lcName, String prefix, int version) {
    C_env env = new C_env("", lcName, prefix, out, version);
Anders Nilsson's avatar
Anders Nilsson committed
174
175

    // Include
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
176
177
    env.println("#include \"labcomm"+env.verStr+".h\"");
    env.println("#include \"labcomm"+env.verStr+"_private.h\"");
Anders Nilsson's avatar
Anders Nilsson committed
178
179
180
181
182
183
184
185
186
187
188
189
    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);
190
      getDecl(i).C_emitDecoderDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
191
192
193
194
      getDecl(i).C_emitEncoderDeclaration(env);
      getDecl(i).C_emitSizeofDeclaration(env);
      env.println("");
    }
Anders Blomdell's avatar
Anders Blomdell committed
195
    C_emitConstructorDeclaration(env);
Anders Nilsson's avatar
Anders Nilsson committed
196
197
198
199
200
    C_emitForAll(env);
  }

  public void Program.C_emitC(C_env env) {
    for (int i = 0; i < getNumDecl(); i++) {
201
      getDecl(i).C_emitSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
202
203
      getDecl(i).C_emitDecoder(env);
      getDecl(i).C_emitDecoderRegisterHandler(env);
204
      getDecl(i).C_emitDecoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
205
206
      getDecl(i).C_emitEncoder(env);
      getDecl(i).C_emitEncoderRegisterHandler(env);
207
      getDecl(i).C_emitEncoderIoctl(env);
Anders Nilsson's avatar
Anders Nilsson committed
208
209
      getDecl(i).C_emitSizeof(env);
    }
210
    C_emitConstructor(env);
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
  }
 
}

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");
  }

  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);
  }

  public void PrimType.C_emitType(C_env env, String name) {
    switch (getToken()) {
261
262
263
264
265
266
267
      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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
      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 {

335
  public void Decl.C_emitDecoderDeclaration(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
336
337
  }

338
  public void SampleDecl.C_emitDecoderDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
339
    env.println("int labcomm"+env.verStr+"_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
340
341
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
342
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
343
344
345
346
347
348
349
350
351
    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(");");
352

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
353
    env.println("int labcomm"+env.verStr+"_decoder_ioctl_" + env.prefix + getName() + "(");
354
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
355
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
356
357
358
359
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
360
361
362
363
364
365
  }
  
  public void Decl.C_emitEncoderDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitEncoderDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
366
    env.println("int labcomm"+env.verStr+"_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
367
368
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
369
    env.println("struct labcomm"+env.verStr+"_encoder *e);");
Anders Nilsson's avatar
Anders Nilsson committed
370
371
    env.unindent();

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
372
    env.println("int labcomm"+env.verStr+"_encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
373
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
374
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
Anders Nilsson's avatar
Anders Nilsson committed
375
376
377
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(");");
378

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
379
    env.println("int labcomm"+env.verStr+"_encoder_ioctl_" + env.prefix + getName() + "(");
380
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
381
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
382
383
384
385
    env.println("int ioctl_action,");
    env.println("...");
    env.unindent();
    env.println(");");
Anders Nilsson's avatar
Anders Nilsson committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  }

}

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) {
    return env.qualid + ".n_" + i;
  }
  
}

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");
441
    env.println("static void decode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
442
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
443
    env.println("struct labcomm"+env.verStr+"_reader *r,");
Anders Nilsson's avatar
Anders Nilsson committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
    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) {
  }

  public void PrimType.C_emitDecoder(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
479
    env.println(env.qualid + " = labcomm"+env.verStr+"_read_" + getName() + "(r);");
Anders Nilsson's avatar
Anders Nilsson committed
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
  }

  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
525
    env.println(env.qualid + ".n_" + i + " = labcomm"+env.verStr+"_read_packed32(r);");
Anders Nilsson's avatar
Anders Nilsson committed
526
527
528
529
530
531
532
533
534
535
536
537
  }

  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) {
538
    env.print(env.qualid + 
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
539
              ".a = labcomm"+env.verStr+"_memory_alloc(r->memory, 1, sizeof(" + 
540
	      env.qualid + ".a[0])");
Anders Nilsson's avatar
Anders Nilsson committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    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");
  }

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

  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
603
    env.println("labcomm"+env.verStr+"_memory_free(r->memory, 1, " + 
604
                env.qualid + ".a);");
Anders Nilsson's avatar
Anders Nilsson committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  }

  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
621
    env.println("int labcomm"+env.verStr+"_decoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
622
623
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
624
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
Anders Nilsson's avatar
Anders Nilsson committed
625
626
627
628
629
630
631
632
633
634
635
    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
636
    env.println("return labcomm"+env.verStr+"_internal_decoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
637
638
    env.indent();
    env.println("d,");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
639
640
641
    env.println("&labcomm"+env.verStr+"_signature_" + env.prefix + getName() + ",");
    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
642
643
644
645
646
647
648
649
650
    env.println("context");
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }

}

651
652
653
654
655
656
657
658
659
660
661
662
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
663
    env.println("int labcomm"+env.verStr+"_decoder_ioctl_" + env.prefix + getName() + "(");
664
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
665
    env.println("struct labcomm"+env.verStr+"_decoder *d,");
666
667
668
669
670
671
672
673
674
    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
675
    env.println("result = labcomm"+env.verStr+"_internal_decoder_ioctl(");
676
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
677
    env.println("d, &labcomm"+env.verStr+"_signature_" + env.prefix + getName() + ", ");
678
    env.println("ioctl_action, va);");
679
680
681
682
683
684
685
686
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
}

Anders Nilsson's avatar
Anders Nilsson committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700

aspect C_Encoder {

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

  public void TypeDecl.C_emitEncoder(C_env env) {
  }

  public void SampleDecl.C_emitEncoder(C_env env) {
    env = env.nestStruct("(*v)");
701
    env.println("static int encode_" + env.prefix + getName() + "(");
Anders Nilsson's avatar
Anders Nilsson committed
702
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
703
    env.println("struct labcomm"+env.verStr+"_writer *w,");
Anders Nilsson's avatar
Anders Nilsson committed
704
705
706
707
708
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
709
    env.println("int result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
710
    getType().C_emitEncoder(env);
711
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
712
713
714
715
    env.unindent();
    env.println("}");

    // Typesafe encode wrapper
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
716
717
    env.println("int labcomm"+env.verStr+"_encode_" + env.prefix + getName() + "(");
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
Anders Nilsson's avatar
Anders Nilsson committed
718
719
720
721
722
    env.println(env.prefix + getName() + " *v");
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
723
    env.println("return labcomm"+env.verStr+"_internal_encode(e, &labcomm"+env.verStr+"_signature_" + 
724
		env.prefix + getName() + 
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
725
		", (labcomm"+env.verStr+"_encoder_function)encode_" + env.prefix + getName() +
726
		", v);");
Anders Nilsson's avatar
Anders Nilsson committed
727
728
729
730
731
732
733
734
735
736
737
    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) {
738
    env.println("result = 0;");
Anders Nilsson's avatar
Anders Nilsson committed
739
740
741
  }

  public void PrimType.C_emitEncoder(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
742
    env.println("result = labcomm"+env.verStr+"_write_" + getName() + 
743
                "(w, " + env.qualid + ");");
744
    env.println("if (result != 0) { return result; }");
Anders Nilsson's avatar
Anders Nilsson committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  }

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

  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
789
    env.println("labcomm"+env.verStr+"_write_packed32(w, " + env.qualid + ".n_" + i + ");");
Anders Nilsson's avatar
Anders Nilsson committed
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  }

  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) {
    throw new Error(this.getClass().getName() + 
		    ".C_emitEncoderRegisterHandler(C_env env)" + 
		    " not declared");
  }

  public void TypeDecl.C_emitEncoderRegisterHandler(C_env env) {
  }

  public void SampleDecl.C_emitEncoderRegisterHandler(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
808
    env.println("int labcomm"+env.verStr+"_encoder_register_" + 
Anders Nilsson's avatar
Anders Nilsson committed
809
810
		env.prefix + getName() + "(");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
811
    env.println("struct labcomm"+env.verStr+"_encoder *e");
Anders Nilsson's avatar
Anders Nilsson committed
812
813
814
815
    env.unindent();
    env.println(")");
    env.println("{");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
816
    env.println("return labcomm"+env.verStr+"_internal_encoder_register(");
Anders Nilsson's avatar
Anders Nilsson committed
817
818
    env.indent();
    env.println("e,");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
819
820
    env.println("&labcomm"+env.verStr+"_signature_" + env.prefix + getName() + ",");
    env.println("(labcomm"+env.verStr+"_encoder_function)encode_" + env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
821
822
823
824
825
    env.unindent();
    env.println(");");
    env.unindent();
    env.println("}");
  }
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840

}

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
841
    env.println("int labcomm"+env.verStr+"_encoder_ioctl_" + env.prefix + getName() + "(");
842
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
843
    env.println("struct labcomm"+env.verStr+"_encoder *e,");
844
845
846
847
848
849
850
851
852
    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
853
    env.println("result = labcomm"+env.verStr+"_internal_encoder_ioctl(");
854
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
855
    env.println("e, &labcomm"+env.verStr+"_signature_" + env.prefix + getName() + ", ");
856
    env.println("ioctl_action, va);");
857
858
859
860
861
862
    env.unindent();
    env.println("va_end(va);");
    env.println("return result;");
    env.unindent();
    env.println("}");
  }
Anders Nilsson's avatar
Anders Nilsson committed
863
864
865
866
867

}

aspect C_Signature {

868
  public void ASTNode.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
869
    throw new Error(this.getClass().getName() + 
870
		    ".C_emitSignature(C_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
871
872
873
		    " not declared");
  }

874
  public void Decl.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
875
876
  }

877
  public void SampleDecl.C_emitSignature(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
878
    env.println("static unsigned char signature_bytes_" + 
879
		       env.prefix + getName() + "[] = {");
880
    SignatureList signature = signature(env.version);
Anders Nilsson's avatar
Anders Nilsson committed
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
    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("};");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
896
    env.println("struct labcomm"+env.verStr+"_signature labcomm"+env.verStr+"_signature_" + 
897
898
899
		env.prefix + getName() + " = {");
    env.indent();
    env.println("LABCOMM_SAMPLE, \"" + getName() + "\",");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
900
    env.println("(int (*)(struct labcomm"+env.verStr+"_signature *, void *))labcomm"+env.verStr+"_sizeof_" + 
901
902
903
904
905
906
		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(" };");
Anders Nilsson's avatar
Anders Nilsson committed
907
908
  }

909
  public void ASTNode.C_emitConstructor(C_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
910
    throw new Error(this.getClass().getName() + 
911
		    ".C_emitConstructor(C_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
912
913
914
		    " not declared");
  }

915
916
917
918
  public void Program.C_emitConstructor(C_env env) {
    env.println("LABCOMM_CONSTRUCTOR void init_" +
		env.prefix + "_signatures(void)");
    env.println("{");
919
    env.indent();
920
921
922
923
924
925
926
    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);
    }
927
    env.unindent();
928
929
930
931
932
933
934
935
936
    env.println("}"); 
    env.unindent();
    env.println("}"); 
  }

  public void Decl.C_emitConstructor(C_env env) {
  }

  public void SampleDecl.C_emitConstructor(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
937
    env.println("labcomm"+env.verStr+"_set_local_index(&labcomm"+env.verStr+"_signature_" + 
938
		env.prefix + getName() + ");");
Anders Nilsson's avatar
Anders Nilsson committed
939
940
941
  }


Anders Blomdell's avatar
Anders Blomdell committed
942
943
944
945
946
947
948
  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) {
949
    env.println("void init_" + env.prefix + "_signatures(void);");
Anders Blomdell's avatar
Anders Blomdell committed
950
951
  }

Anders Nilsson's avatar
Anders Nilsson committed
952
953
954
955
956
957
958
959
}

aspect C_Sizeof {

  public void Decl.C_emitSizeofDeclaration(C_env env) {
  }

  public void SampleDecl.C_emitSizeofDeclaration(C_env env) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
960
    env.println("extern int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
Anders Blomdell's avatar
Anders Blomdell committed
961
		"(" + env.prefix + getName() + " *v);");
Anders Nilsson's avatar
Anders Nilsson committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
  }

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

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

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

  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: { 
	throw new Error(this.getClass().getName() + 
			".C_fixedSizeof()" + 
			" unknown size (" + getName() + ")"); 
      } 
    }
  }

  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;
  }

  public void Decl.C_emitSizeof(C_env env) {
  }

  public void SampleDecl.C_emitSizeof(C_env env) {
    env = env.nestStruct("(*v)");
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1021
    env.println("int labcomm"+env.verStr+"_sizeof_" + env.prefix + getName() +
Anders Blomdell's avatar
Anders Blomdell committed
1022
		"(" + env.prefix + getName() + " *v)");
Anders Nilsson's avatar
Anders Nilsson committed
1023
1024
    env.println("{");
    env.indent();
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1025
    env.println("int result = labcomm"+env.verStr+"_size_packed32(labcomm"+env.verStr+"_signature_" + 
Anders Blomdell's avatar
Anders Blomdell committed
1026
                env.prefix + getName() +".index);");
Anders Nilsson's avatar
Anders Nilsson committed
1027
1028
1029
    if (C_isDynamic()) {
      getType().C_emitSizeof(env);
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1030
      env.println("result += " + C_fixedSizeof() + ";");
Anders Nilsson's avatar
Anders Nilsson committed
1031
    }    
Anders Blomdell's avatar
Anders Blomdell committed
1032
    env.println("return result;");
Anders Nilsson's avatar
Anders Nilsson committed
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
    env.unindent();
    env.println("}");
  }

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

  public void PrimType.C_emitSizeof(C_env env) {
    switch (getToken()) {
      case LABCOMM_STRING: { 
1046
	env.println("result += 0 + strlen(" + env.qualid + ");"); 
Anders Nilsson's avatar
Anders Nilsson committed
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
      } break;
      default: { 
	throw new Error(this.getClass().getName() + 
			".C_emitSizeof(C_env env)" + 
			" known size (" + getName() + ")"); 
      } 
    }
  }

  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()) {
	getField(i).getType().C_emitSizeof(
	  env.nestStruct("." + getField(i).getName()));
      } else {
	fixed += getField(i).getType().C_fixedSizeof();
      }
    }
    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++) {
	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_emitSizeof(C_Nest(env));
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.unindent();
	env.println("}");
      }
      env.unindent();
      env.println("}");
    } else {
Anders Blomdell's avatar
Anders Blomdell committed
1098
      for (int i = 0 ; i < getNumExp() ; i++) {
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
1099
      	env.println("result += labcomm"+env.verStr+"_size_packed32(" + 
Anders Blomdell's avatar
Anders Blomdell committed
1100
1101
	            getExp(i).C_getLimit(env, i) + ");");
      }
Anders Nilsson's avatar
Anders Nilsson committed
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
      env.print("result += " + getType().C_fixedSizeof());
      for (int i = 0 ; i < getNumExp() ; i++) {
	env.print(" * " + getExp(i).C_getLimit(env, i));
      }
      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 {

1144
1145
  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
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    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
1158
    env.println(",C,typedef," + env.prefix + getName() + "," + 
1159
                 env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
1160
1161
1162
  }

  public void SampleDecl.C_info(C_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
1163
    env.println(",C,sample," + env.prefix + getName() + "," + 
1164
                env.prefix + getName());
Anders Nilsson's avatar
Anders Nilsson committed
1165
1166
1167
  }

}