-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_cs.c
830 lines (784 loc) · 19.2 KB
/
gen_cs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
145
146
147
148
149
150
151
152
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
381
382
383
384
385
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
441
442
443
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
* gen_cs.c - Generate C# source code from "treecc" input files.
*
* Copyright (C) 2001 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "system.h"
#include "input.h"
#include "info.h"
#include "gen.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Declare the fields for a node type.
*/
static void DeclareFields(TreeCCContext *context,
TreeCCStream *stream, TreeCCNode *node)
{
TreeCCField *field;
field = node->fields;
while(field != 0)
{
TreeCCStreamPrint(stream, "\tpublic %s %s;\n",
field->type, field->name);
field = field->next;
}
}
/*
* Declare the type definitions for a node type.
*/
static void DeclareTypeDefs(TreeCCContext *context,
TreeCCNode *node)
{
if((node->flags & TREECC_NODE_ENUM) != 0)
{
/* Define an enumerated type */
TreeCCStream *stream = node->source;
TreeCCNode *child;
if(context->internal_access)
{
TreeCCStreamPrint(stream, "internal enum %s\n", node->name);
}
else
{
TreeCCStreamPrint(stream, "public enum %s\n", node->name);
}
TreeCCStreamPrint(stream, "{\n");
child = node->firstChild;
while(child != 0)
{
if((child->flags & TREECC_NODE_ENUM_VALUE) != 0)
{
TreeCCStreamPrint(stream, "\t%s,\n", child->name);
}
child = child->nextSibling;
}
TreeCCStreamPrint(stream, "}\n\n");
}
}
/*
* Output the parameters for a node creation function.
*/
static int CreateParams(TreeCCContext *context, TreeCCStream *stream,
TreeCCNode *node, int needComma)
{
TreeCCField *field;
if(node->parent)
{
needComma = CreateParams(context, stream, node->parent, needComma);
}
field = node->fields;
while(field != 0)
{
if((field->flags & TREECC_FIELD_NOCREATE) == 0)
{
if(needComma)
{
TreeCCStreamPrint(stream, ", ");
}
TreeCCStreamPrint(stream, "%s %s", field->type, field->name);
needComma = 1;
}
field = field->next;
}
return needComma;
}
/*
* Output the parameters to call an inherited constructor.
*/
static int InheritParamsSource(TreeCCContext *context, TreeCCStream *stream,
TreeCCNode *node, int needComma)
{
TreeCCField *field;
if(node->parent)
{
needComma = InheritParamsSource(context, stream,
node->parent, needComma);
}
field = node->fields;
while(field != 0)
{
if((field->flags & TREECC_FIELD_NOCREATE) == 0)
{
if(needComma)
{
TreeCCStreamPrint(stream, ", ");
}
TreeCCStreamPrint(stream, "%s", field->name);
needComma = 1;
}
field = field->next;
}
return needComma;
}
/*
* Implement the virtual methods that have implementations in a node type.
*/
static void ImplementVirtuals(TreeCCContext *context, TreeCCStream *stream,
TreeCCNode *node, TreeCCNode *actualNode)
{
TreeCCVirtual *virt;
TreeCCParam *param;
TreeCCOperationCase *operCase;
int declareCase, abstractCase;
TreeCCNode *tempNode;
int num, first;
int needComma;
if(node->parent)
{
ImplementVirtuals(context, stream, node->parent, actualNode);
}
virt = node->virtuals;
while(virt != 0)
{
/* Determine if we need a definition for this virtual,
and whether the definition is real or abstract */
operCase = TreeCCOperationFindCase(context, actualNode, virt->name);
if(!operCase)
{
tempNode = actualNode->parent;
abstractCase = 1;
while(tempNode != 0)
{
operCase = TreeCCOperationFindCase
(context, tempNode, virt->name);
if(operCase != 0)
{
abstractCase = 0;
break;
}
tempNode = tempNode->parent;
}
declareCase = abstractCase;
}
else
{
declareCase = 1;
abstractCase = 0;
}
if(declareCase)
{
if(abstractCase)
{
if(node == actualNode)
{
TreeCCStreamPrint(stream,
"\tpublic abstract %s %s(",
virt->returnType, virt->name);
}
else
{
/* Inherit the "abstract" definition from the parent */
virt = virt->next;
continue;
}
}
else
{
if(node == actualNode)
{
TreeCCStreamPrint(stream, "\tpublic virtual %s %s(",
virt->returnType, virt->name);
}
else
{
TreeCCStreamPrint(stream, "\tpublic override %s %s(",
virt->returnType, virt->name);
}
}
param = virt->oper->params;
needComma = 0;
num = 1;
first = 1;
while(param != 0)
{
if(needComma)
{
TreeCCStreamPrint(stream, ", ");
}
if(first)
{
/* Skip the first argument, which corresponds to "this" */
if(!(param->name))
{
++num;
}
first = 0;
}
else
{
if(param->name)
{
TreeCCStreamPrint(stream, "%s %s",
param->type, param->name);
}
else
{
TreeCCStreamPrint(stream, "%s P%d__",
param->type, num);
++num;
}
needComma = 1;
}
param = param->next;
}
if(!abstractCase)
{
TreeCCStreamPrint(stream, ")\n");
TreeCCStreamLine(stream, operCase->codeLinenum,
operCase->codeFilename);
TreeCCStreamPrint(stream, "\t{");
if(!(virt->oper->params->name) ||
!strcmp(virt->oper->params->name, "this"))
{
/* The first parameter is called "this", so we don't
need to declare it at the head of the function */
TreeCCStreamCodeIndent(stream, operCase->code, 1);
}
else
{
/* The first parameter is called something else,
so create a temporary variable to hold "this" */
TreeCCStreamPrint(stream, "\n\t\t%s %s = this;\n\t",
actualNode->name,
virt->oper->params->name);
TreeCCStreamCodeIndent(stream, operCase->code, 1);
}
TreeCCStreamPrint(stream, "}\n");
TreeCCStreamFixLine(stream);
TreeCCStreamPrint(stream, "\n");
}
else
{
TreeCCStreamPrint(stream, ");\n\n");
}
}
virt = virt->next;
}
}
/*
* Build the type declarations for a node type.
*/
static void BuildTypeDecls(TreeCCContext *context,
TreeCCNode *node)
{
TreeCCStream *stream;
int needComma;
const char *constructorAccess;
TreeCCField *field;
int isAbstract;
const char *accessMode;
/* Ignore if this is an enumerated type node */
if((node->flags & (TREECC_NODE_ENUM | TREECC_NODE_ENUM_VALUE)) != 0)
{
return;
}
/* Determine if this class has abstract virtuals */
isAbstract = TreeCCNodeHasAbstracts(context, node);
/* Determine the access mode for the class */
if(context->internal_access)
{
accessMode = "internal";
}
else
{
accessMode = "public";
}
/* Output the class header */
stream = node->source;
if(node->parent)
{
/* Inherit from a specified parent type */
if(isAbstract)
{
TreeCCStreamPrint(stream, "%s abstract class %s : %s\n{\n",
accessMode, node->name, node->parent->name);
}
else
{
TreeCCStreamPrint(stream, "%s class %s : %s\n{\n",
accessMode, node->name, node->parent->name);
}
}
else
{
/* This type is the base of a class hierarchy */
if(isAbstract)
{
if(context->baseType)
{
TreeCCStreamPrint(stream, "%s abstract class %s : %s\n{\n",
accessMode, node->name, context->baseType);
}
else
{
TreeCCStreamPrint(stream, "%s abstract class %s\n{\n",
accessMode, node->name);
}
}
else
{
if(context->baseType)
{
TreeCCStreamPrint(stream, "%s class %s : %s\n{\n",
accessMode, node->name, context->baseType);
}
else
{
TreeCCStreamPrint(stream, "%s class %s\n{\n",
accessMode, node->name);
}
}
/* Declare the node kind member variable */
TreeCCStreamPrint(stream, "\tprotected int kind__;\n");
/* Declare the filename and linenum fields if we are tracking lines */
if(context->track_lines)
{
TreeCCStreamPrint(stream, "\tprotected String filename__;\n");
TreeCCStreamPrint(stream, "\tprotected long linenum__;\n");
}
TreeCCStreamPrint(stream, "\n");
/* Declare the public methods for access to the above fields */
TreeCCStreamPrint(stream,
"\tpublic int getKind() { return kind__; }\n");
if(context->track_lines)
{
TreeCCStreamPrint(stream,
"\tpublic String getFilename() { return filename__; }\n");
TreeCCStreamPrint(stream,
"\tpublic long getLinenum() { return linenum__; }\n");
TreeCCStreamPrint(stream,
"\tpublic void setFilename(String filename) "
"{ filename__ = filename; }\n");
TreeCCStreamPrint(stream,
"\tpublic void setLinenum(long linenum) "
"{ linenum__ = linenum; }\n");
}
TreeCCStreamPrint(stream, "\n");
}
/* Declare the kind value */
if(node->parent)
{
TreeCCStreamPrint(stream, "\tpublic new const int KIND = %d;\n\n",
node->number);
}
else
{
TreeCCStreamPrint(stream, "\tpublic const int KIND = %d;\n\n",
node->number);
}
/* Declare the fields */
if(node->fields)
{
DeclareFields(context, stream, node);
TreeCCStreamPrint(stream, "\n");
}
/* Declare the constructor for the node type */
if(context->reentrant)
{
/* Re-entrant systems use a factory to create the nodes.
C# doesn't have a notion of "access by members of
the namespace only". The closest is "internal", but
even that isn't quite right, so we always use "public" */
constructorAccess = "public ";
}
else
{
/* Non-reentrant systems can construct nodes directly,
unless the node happens to be abstract, in which
case we force the constructor to be protected */
if((node->flags & TREECC_NODE_ABSTRACT) != 0)
{
constructorAccess = "protected ";
}
else
{
constructorAccess = "public ";
}
}
TreeCCStreamPrint(stream, "\t%s%s(", constructorAccess, node->name);
if(context->reentrant)
{
TreeCCStreamPrint(stream, "%s state__", context->state_type);
needComma = 1;
}
else
{
needComma = 0;
}
CreateParams(context, stream, node, needComma);
TreeCCStreamPrint(stream, ")\n");
/* Call the parent class constructor */
if(node->parent)
{
TreeCCStreamPrint(stream, "\t\t: base(");
if(context->reentrant)
{
TreeCCStreamPrint(stream, "state__");
needComma = 1;
}
else
{
needComma = 0;
}
InheritParamsSource(context, stream, node->parent, needComma);
TreeCCStreamPrint(stream, ")\n");
}
/* Set the node kind */
TreeCCStreamPrint(stream, "\t{\n");
TreeCCStreamPrint(stream, "\t\tthis.kind__ = KIND;\n");
/* Track the filename and line number if necessary */
if(context->track_lines && !(node->parent))
{
if(context->reentrant)
{
TreeCCStreamPrint(stream,
"\t\tthis.filename__ = state__.currFilename();\n");
TreeCCStreamPrint(stream,
"\t\tthis.linenum__ = state__.currLinenum();\n");
}
else
{
TreeCCStreamPrint(stream,
"\t\tthis.filename__ = %s.getState().currFilename();\n",
context->state_type);
TreeCCStreamPrint(stream,
"\t\tthis.linenum__ = %s.getState().currLinenum();\n",
context->state_type);
}
}
/* Initialize the fields that are specific to this node type */
field = node->fields;
while(field != 0)
{
if((field->flags & TREECC_FIELD_NOCREATE) == 0)
{
TreeCCStreamPrint(stream, "\t\tthis.%s = %s;\n",
field->name, field->name);
}
else if(field->value)
{
TreeCCStreamPrint(stream, "\t\tthis.%s = %s;\n",
field->name, field->value);
}
field = field->next;
}
TreeCCStreamPrint(stream, "\t}\n\n");
/* Implement the virtual functions */
ImplementVirtuals(context, stream, node, node);
/* Declare the "isA" and "getKindName" helper methods */
if(node->parent)
{
TreeCCStreamPrint(stream, "\tpublic override int isA(int kind)\n");
}
else
{
TreeCCStreamPrint(stream, "\tpublic virtual int isA(int kind)\n");
}
TreeCCStreamPrint(stream, "\t{\n");
TreeCCStreamPrint(stream, "\t\tif(kind == KIND)\n");
TreeCCStreamPrint(stream, "\t\t\treturn 1;\n");
TreeCCStreamPrint(stream, "\t\telse\n");
if(node->parent)
{
TreeCCStreamPrint(stream, "\t\t\treturn base.isA(kind);\n");
}
else
{
TreeCCStreamPrint(stream, "\t\t\treturn 0;\n");
}
TreeCCStreamPrint(stream, "\t}\n\n");
if(node->parent)
{
TreeCCStreamPrint(stream, "\tpublic override String getKindName()\n");
}
else
{
TreeCCStreamPrint(stream, "\tpublic virtual String getKindName()\n");
}
TreeCCStreamPrint(stream, "\t{\n");
TreeCCStreamPrint(stream, "\t\treturn \"%s\";\n", node->name);
TreeCCStreamPrint(stream, "\t}\n");
/* Output the class footer */
TreeCCStreamPrint(stream, "}\n\n");
}
/*
* Declare the parameters for a factory method in the state type.
*/
static int FactoryCreateParams(TreeCCContext *context, TreeCCStream *stream,
TreeCCNode *node, int needComma)
{
TreeCCField *field;
if(node->parent)
{
needComma = FactoryCreateParams(context, stream,
node->parent, needComma);
}
field = node->fields;
while(field != 0)
{
if((field->flags & TREECC_FIELD_NOCREATE) == 0)
{
if(needComma)
{
TreeCCStreamPrint(stream, ", ");
}
TreeCCStreamPrint(stream, "%s %s",
field->type, field->name);
needComma = 1;
}
field = field->next;
}
return needComma;
}
/*
* Output invocation parameters for a call to a constructor
* from within a factory method.
*/
static int FactoryInvokeParams(TreeCCContext *context, TreeCCStream *stream,
TreeCCNode *node, int needComma)
{
TreeCCField *field;
if(node->parent)
{
needComma = FactoryInvokeParams(context, stream,
node->parent, needComma);
}
field = node->fields;
while(field != 0)
{
if((field->flags & TREECC_FIELD_NOCREATE) == 0)
{
if(needComma)
{
TreeCCStreamPrint(stream, ", ");
}
TreeCCStreamPrint(stream, "%s", field->name);
needComma = 1;
}
field = field->next;
}
return needComma;
}
/*
* Implement the create function for a node type.
*/
static void ImplementCreateFuncs(TreeCCContext *context, TreeCCNode *node)
{
TreeCCStream *stream;
/* Ignore if this is an enumerated type node */
if((node->flags & (TREECC_NODE_ENUM | TREECC_NODE_ENUM_VALUE)) != 0)
{
return;
}
/* Ignore this if it is an abstract node */
if((node->flags & TREECC_NODE_ABSTRACT) != 0)
{
return;
}
/* Determine which stream to write to */
if(context->commonSource)
{
stream = context->commonSource;
}
else
{
stream = context->sourceStream;
}
/* Output the start of the function definition */
if(context->abstract_factory)
{
TreeCCStreamPrint(stream, "\tpublic virtual abstract %s %sCreate(",
node->name, node->name);
}
else if(context->virtual_factory)
{
TreeCCStreamPrint(stream, "\tpublic virtual %s %sCreate(",
node->name, node->name);
}
else
{
TreeCCStreamPrint(stream, "\tpublic %s %sCreate(",
node->name, node->name);
}
/* Output the parameters for the create function */
FactoryCreateParams(context, stream, node, 0);
/* Output the body of the creation function */
if(context->abstract_factory)
{
TreeCCStreamPrint(stream, ");\n");
}
else
{
TreeCCStreamPrint(stream, ")\n");
TreeCCStreamPrint(stream, "\t{\n");
TreeCCStreamPrint(stream, "\t\treturn new %s(this", node->name);
FactoryInvokeParams(context, stream, node, 1);
TreeCCStreamPrint(stream, ");\n");
TreeCCStreamPrint(stream, "\t}\n\n");
}
}
/*
* Implement the state type in the source stream.
*/
static void ImplementStateType(TreeCCContext *context, TreeCCStream *stream)
{
/* Declare the class header */
if(context->reentrant && context->abstract_factory)
{
if(context->internal_access)
{
TreeCCStreamPrint(stream, "internal abstract class %s\n{\n\n",
context->state_type);
}
else
{
TreeCCStreamPrint(stream, "public abstract class %s\n{\n\n",
context->state_type);
}
}
else
{
if(context->internal_access)
{
TreeCCStreamPrint(stream, "internal class %s\n{\n\n",
context->state_type);
}
else
{
TreeCCStreamPrint(stream, "public class %s\n{\n\n",
context->state_type);
}
}
/* Singleton handling for non-reentrant systems */
if(!(context->reentrant))
{
TreeCCStreamPrint(stream, "\tprivate static %s state__;\n\n",
context->state_type);
TreeCCStreamPrint(stream, "\tpublic static %s getState()\n",
context->state_type);
TreeCCStreamPrint(stream, "\t{\n");
TreeCCStreamPrint(stream, "\t\tif(state__ != null) return state__;\n");
TreeCCStreamPrint(stream, "\t\tstate__ = new %s();\n",
context->state_type);
TreeCCStreamPrint(stream, "\t\treturn state__;\n");
TreeCCStreamPrint(stream, "\t}\n\n");
}
/* Implement the constructor */
if(context->reentrant)
{
TreeCCStreamPrint(stream, "\tpublic %s() {}\n\n", context->state_type);
}
else
{
TreeCCStreamPrint(stream, "\tpublic %s() { state__ = this; }\n\n",
context->state_type);
}
/* Implement the create functions for all of the node types */
if(context->reentrant)
{
TreeCCNodeVisitAll(context, ImplementCreateFuncs);
}
/* Implement the line number tracking methods */
if(context->track_lines)
{
TreeCCStreamPrint(stream,
"\tpublic virtual String currFilename() { return null; }\n");
TreeCCStreamPrint(stream,
"\tpublic virtual long currLinenum() { return 0; }\n\n");
}
/* Declare the end of the state type */
TreeCCStreamPrint(stream, "}\n\n");
}
/*
* Write out header information for all streams.
*/
static void WriteCSharpHeaders(TreeCCContext *context)
{
TreeCCStream *stream = context->streamList;
while(stream != 0)
{
if(!(stream->isHeader))
{
TreeCCStreamPrint(stream,
"/* %s. Generated automatically by treecc */\n\n",
stream->embedName);
if(context->namespace)
{
TreeCCStreamPrint(stream, "namespace %s\n{\n\n",
context->namespace);
}
TreeCCStreamPrint(stream, "using System;\n\n");
TreeCCStreamSourceTopCS(stream);
}
if(stream->defaultFile)
{
/* Reset the dirty flag if this is a default stream,
because we don't want to write out the final file
if it isn't actually written to in practice */
stream->dirty = 0;
}
stream = stream->nextStream;
}
}
/*
* Write out footer information for all streams.
*/
static void WriteCSharpFooters(TreeCCContext *context)
{
TreeCCStream *stream = context->streamList;
while(stream != 0)
{
if(stream->defaultFile && !(stream->dirty))
{
/* Clear the default file's contents, which we don't need */
TreeCCStreamClear(stream);
}
else if(!(stream->isHeader))
{
TreeCCStreamSourceBottom(stream);
if(context->namespace)
{
TreeCCStreamPrint(stream, "}\n");
}
}
stream = stream->nextStream;
}
}
void TreeCCGenerateCSharp(TreeCCContext *context)
{
/* Write all stream headers */
WriteCSharpHeaders(context);
/* Generate the contents of the source stream */
TreeCCNodeVisitAll(context, DeclareTypeDefs);
if(context->commonSource)
{
ImplementStateType(context, context->commonSource);
}
else
{
ImplementStateType(context, context->sourceStream);
}
TreeCCNodeVisitAll(context, BuildTypeDecls);
TreeCCGenerateNonVirtuals(context, &TreeCCNonVirtualFuncsJava);
/* Write all stream footers */
WriteCSharpFooters(context);
}
#ifdef __cplusplus
};
#endif