Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CodeGenerator.cs « System.CodeDom.Compiler « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 36ea5d13ef37edba4645bd1b5113583ba3a98294 (plain)
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
//
// System.CodeDom.Compiler CodeGenerator class
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Daniel Stodden (stodden@in.tum.de)
//
// (C) 2001 Ximian, Inc.
//

using System.CodeDom;
using System.Reflection;
using System.IO;
using System.Collections;
	
namespace System.CodeDom.Compiler {

	public abstract class CodeGenerator
		: ICodeGenerator
	{
		private IndentedTextWriter output;
		private CodeGeneratorOptions options;
		private CodeTypeMember currentMember;
		private CodeTypeDeclaration currentType;

		//
		// Constructors
		//
		protected CodeGenerator()
		{
		}

		//
		// Properties
		//
		protected CodeTypeMember CurrentMember {
			get {
				return currentMember;
			}
		}
		
		protected string CurrentMemberName {
			get {
				if ( currentType == null )
					return null;
				return currentMember.Name;
			}
		}

		protected string CurrentTypeName {
			get {
				if ( currentType == null )
					return null;
				return currentType.Name;
			}
		}
		
		protected int Indent {
			get {
				return output.Indent;
			}
			set {
				output.Indent = value;
			}
		}
		
		protected bool IsCurrentClass {
			get {
				if ( currentType == null )
					return false;
				return currentType.IsClass;
			}
		}

		protected bool IsCurrentDelegate {
			get {
				return currentType is CodeTypeDelegate;
			}
		}

		protected bool IsCurrentEnum {
			get {
				if ( currentType == null )
					return false;
				return currentType.IsEnum;
			}
		}

		protected bool IsCurrentInterface {
			get {
				if ( currentType == null )
					return false;
				return currentType.IsInterface;
			}
		}

		protected bool IsCurrentStruct {
			get {
				if ( currentType == null )
					return false;
				return currentType.IsStruct;
			}
		}

		protected abstract string NullToken {
			get;
		}
		
		
		protected CodeGeneratorOptions Options {
			get {
				return options;
			}
		}
			
		protected TextWriter Output {
			get {
				return output;
			}
		}

		//
		// Methods
		//
		protected virtual void ContinueOnNewLine( string st )
		{
		}

		/*
		 * Code Generation methods
		 */
		protected abstract void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e);
		protected abstract void GenerateArrayCreateExpression (CodeArrayCreateExpression e);
		protected abstract void GenerateArrayIndexerExpression( CodeArrayIndexerExpression e );
		protected abstract void GenerateAssignStatement (CodeAssignStatement s);
		protected abstract void GenerateAttachEventStatement (CodeAttachEventStatement s);
		protected abstract void GenerateAttributeDeclarationsStart( CodeAttributeDeclarationCollection attributes );
		protected abstract void GenerateAttributeDeclarationsEnd( CodeAttributeDeclarationCollection attributes );
		protected abstract void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e);

		protected virtual void GenerateBinaryOperatorExpression (CodeBinaryOperatorExpression e)
		{
			GenerateExpression( e.Left );
			
			switch ( e.Operator ) {
			case CodeBinaryOperatorType.Add:
				output.Write( " + " );
				break;
			case CodeBinaryOperatorType.Assign:
				output.Write( " = " );
				break;
			case CodeBinaryOperatorType.BitwiseAnd:
				output.Write( " & " );
				break;
			case CodeBinaryOperatorType.BitwiseOr:
				output.Write( " | " );
				break;
			case CodeBinaryOperatorType.BooleanAnd:
				output.Write( " && " );
				break;
			case CodeBinaryOperatorType.BooleanOr:
				output.Write( " || " );
				break;
			case CodeBinaryOperatorType.Divide:
				output.Write( " / " );
				break;
			case CodeBinaryOperatorType.GreaterThan:
				output.Write( " > " );
				break;
			case CodeBinaryOperatorType.GreaterThanOrEqual:
				output.Write( " >= " );
				break;
			case CodeBinaryOperatorType.IdentityEquality:
				output.Write( " == " );
				break;
			case CodeBinaryOperatorType.IdentityInequality:
				output.Write( " != " );
				break;
			case CodeBinaryOperatorType.LessThan:
				output.Write( " < " );
				break;
			case CodeBinaryOperatorType.LessThanOrEqual:
				output.Write( " <= " );
				break;
			case CodeBinaryOperatorType.Modulus:
				output.Write( " % " );
				break;
			case CodeBinaryOperatorType.Multiply:
				output.Write( " * " );
				break;
			case CodeBinaryOperatorType.Subtract:
				output.Write( " - " );
				break;
			case CodeBinaryOperatorType.ValueEquality:
				output.Write( " == " );
				break;
			}

			GenerateExpression( e.Right );
		}

		protected abstract void GenerateCastExpression (CodeCastExpression e);
		protected abstract void GenerateComment( CodeComment comment );

		protected virtual void GenerateCommentStatement( CodeCommentStatement statement )
		{
			GenerateComment( statement.Comment );
		}

		protected virtual void GenerateCommentStatements (CodeCommentStatementCollection statements)
		{
			foreach ( CodeCommentStatement comment in statements )
				GenerateCommentStatement( comment );
		}

		protected virtual void GenerateCompileUnit( CodeCompileUnit compileUnit )
		{
			GenerateCompileUnitStart( compileUnit );

			CodeAttributeDeclarationCollection attributes = compileUnit.AssemblyCustomAttributes;
			if ( attributes.Count != 0 ) {
				GenerateAttributeDeclarationsStart( attributes );
				output.Write( "assembly: " );
				OutputAttributeDeclarations( compileUnit.AssemblyCustomAttributes );
				GenerateAttributeDeclarationsEnd( attributes );
			}

			foreach ( CodeNamespace ns in compileUnit.Namespaces )
				GenerateNamespace( ns );

			GenerateCompileUnitEnd( compileUnit );
		}

		protected virtual void GenerateCompileUnitEnd( CodeCompileUnit compileUnit )
		{
			output.WriteLine( "<compileUnitEnd>" );
		}

		protected virtual void GenerateCompileUnitStart( CodeCompileUnit compileUnit )
		{
			output.WriteLine( "<compileUnitStart>" );
		}

		protected abstract void GenerateConditionStatement( CodeConditionStatement s );
		protected abstract void GenerateConstructor (CodeConstructor x, CodeTypeDeclaration d);
		protected abstract void GenerateDelegateCreateExpression( CodeDelegateCreateExpression e );
		protected abstract void GenerateDelegateInvokeExpression( CodeDelegateInvokeExpression e );

		protected virtual void GenerateDirectionExpression( CodeDirectionExpression e )
		{
			OutputDirection( e.Direction );
			output.Write( ' ' );
			GenerateExpression( e.Expression );
		}

		protected abstract void GenerateEntryPointMethod( CodeEntryPointMethod m, CodeTypeDeclaration d );
		protected abstract void GenerateEvent( CodeMemberEvent ev, CodeTypeDeclaration d );
		protected abstract void GenerateEventReferenceExpression( CodeEventReferenceExpression e );

		protected void GenerateExpression( CodeExpression e )
		{
			CodeArgumentReferenceExpression argref = e as CodeArgumentReferenceExpression;
			if ( argref != null ) {
				GenerateArgumentReferenceExpression( argref );
				return;
			}
			CodeArrayCreateExpression mkarray = e as CodeArrayCreateExpression;
			if ( mkarray != null ) {
				GenerateArrayCreateExpression( mkarray );
				return;
			}
			CodeArrayIndexerExpression arrayidx = e as CodeArrayIndexerExpression;
			if ( arrayidx != null ) {
				GenerateArrayIndexerExpression( arrayidx );
				return;
			}
			CodeBaseReferenceExpression baseref = e as CodeBaseReferenceExpression;
			if ( baseref != null ) {
				GenerateBaseReferenceExpression( baseref );
				return;
			}
			CodeBinaryOperatorExpression binary = e as CodeBinaryOperatorExpression;
			if ( binary != null ) {
				GenerateBinaryOperatorExpression( binary );
				return;
			}
			CodeCastExpression cast = e as CodeCastExpression;
			if ( cast != null ) {
				GenerateCastExpression( cast );
				return;
			}
			CodeDelegateCreateExpression mkdel = e as CodeDelegateCreateExpression;
			if ( mkdel != null ) {
				GenerateDelegateCreateExpression( mkdel );
				return;
			}
			CodeDelegateInvokeExpression delinvoke = e as CodeDelegateInvokeExpression;
			if ( delinvoke != null ) {
				GenerateDelegateInvokeExpression( delinvoke );
				return;
			}
			CodeDirectionExpression direction = e as CodeDirectionExpression;
			if ( direction != null ) {
				GenerateDirectionExpression( direction );
				return;
			}
			CodeEventReferenceExpression eventref = e as CodeEventReferenceExpression;
			if ( eventref != null ) {
				GenerateEventReferenceExpression( eventref );
				return;
			}
			CodeFieldReferenceExpression fieldref = e as CodeFieldReferenceExpression;
			if ( fieldref != null ) {
				GenerateFieldReferenceExpression( fieldref );
				return;
			}
			CodeIndexerExpression idx = e as CodeIndexerExpression;
			if ( idx != null ) {
				GenerateIndexerExpression( idx );
				return;
			}
			CodeMethodInvokeExpression methodinv = e as CodeMethodInvokeExpression;
			if ( methodinv != null ) {
				GenerateMethodInvokeExpression( methodinv );
				return;
			}
			CodeMethodReferenceExpression methodref = e as CodeMethodReferenceExpression;
			if ( methodref != null ) {
				GenerateMethodReferenceExpression( methodref );
				return;
			}
			CodeObjectCreateExpression objref = e as CodeObjectCreateExpression;
			if ( objref != null ) {
				GenerateObjectCreateExpression( objref );
				return;
			}
			CodeParameterDeclarationExpression param = e as CodeParameterDeclarationExpression;
			if ( param != null ) {
				GenerateParameterDeclarationExpression( param );
				return;
			}
			CodePrimitiveExpression primitive = e as CodePrimitiveExpression;
			if ( primitive != null ) {
				GeneratePrimitiveExpression( primitive );
				return;
			}
			CodePropertyReferenceExpression propref = e as CodePropertyReferenceExpression;
			if ( propref != null ) {
				GeneratePropertyReferenceExpression( propref );
				return;
			}
			CodePropertySetValueReferenceExpression propset = e as CodePropertySetValueReferenceExpression;
			if ( propset != null ) {
				GeneratePropertySetValueReferenceExpression( propset );
				return;
			}
			CodeSnippetExpression snippet = e as CodeSnippetExpression;
			if ( snippet != null ) {
				GenerateSnippetExpression( snippet );
				return;
			}
			CodeThisReferenceExpression thisref = e as CodeThisReferenceExpression;
			if ( thisref != null ) {
				GenerateThisReferenceExpression( thisref );
				return;
			}
			CodeTypeOfExpression typeOf = e as CodeTypeOfExpression;
			if ( typeOf != null ) {
				GenerateTypeOfExpression( typeOf );
				return;
			}
			CodeTypeReferenceExpression typeref = e as CodeTypeReferenceExpression;
			if ( typeref != null ) {
				GenerateTypeReferenceExpression( typeref );
				return;
			}
			CodeVariableReferenceExpression varref = e as CodeVariableReferenceExpression;
			if ( varref != null ) {
				GenerateVariableReferenceExpression( varref );
				return;
			}
		}

		protected abstract void GenerateExpressionStatement( CodeExpressionStatement statement );
		protected abstract void GenerateField (CodeMemberField f);
		protected abstract void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e);
		protected abstract void GenerateGotoStatement( CodeGotoStatement statement );
		protected abstract void GenerateIndexerExpression (CodeIndexerExpression e);
		protected abstract void GenerateIterationStatement( CodeIterationStatement s );
		protected abstract void GenerateLabeledStatement( CodeLabeledStatement statement );
		protected abstract void GenerateLinePragmaStart (CodeLinePragma p);
		protected abstract void GenerateLinePragmaEnd (CodeLinePragma p);
		protected abstract void GenerateMethod (CodeMemberMethod m, CodeTypeDeclaration d);
		protected abstract void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e);
		protected abstract void GenerateMethodReferenceExpression( CodeMethodReferenceExpression e );
		protected abstract void GenerateMethodReturnStatement (CodeMethodReturnStatement e);

		protected virtual void GenerateNamespace (CodeNamespace ns)
		{
			foreach ( CodeCommentStatement statement in ns.Comments )
				GenerateCommentStatement( statement );

			GenerateNamespaceStart( ns );

			foreach ( CodeNamespaceImport import in ns.Imports )
				GenerateNamespaceImport( import );

			output.WriteLine();

			foreach ( CodeTypeDeclaration type in ns.Types )
				GenerateCodeFromType( type, output, options );

			GenerateNamespaceEnd( ns );
		}

		protected abstract void GenerateNamespaceStart (CodeNamespace ns);
		protected abstract void GenerateNamespaceEnd (CodeNamespace ns);
		protected abstract void GenerateNamespaceImport (CodeNamespaceImport i);
		protected abstract void GenerateObjectCreateExpression (CodeObjectCreateExpression e);

		protected virtual void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
		{
			OutputAttributeDeclarations( e.CustomAttributes );
			OutputDirection( e.Direction );
			OutputType( e.Type );
			output.Write( ' ' );
			output.Write( e.Name );
		}

		protected virtual void GeneratePrimitiveExpression (CodePrimitiveExpression e)
		{
			output.Write( e.Value );
		}

		protected abstract void GenerateProperty (CodeMemberProperty p, CodeTypeDeclaration d);
		protected abstract void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e);
		protected abstract void GeneratePropertySetValueReferenceExpression( CodePropertySetValueReferenceExpression e );
		protected abstract void GenerateRemoveEventStatement( CodeRemoveEventStatement statement );
		protected abstract void GenerateSnippetExpression( CodeSnippetExpression e );
		protected abstract void GenerateSnippetMember( CodeSnippetTypeMember m );
		protected virtual void GenerateSnippetStatement( CodeSnippetStatement s )
		{
			output.Write( s.Value );
		}

		protected void GenerateStatement( CodeStatement s )
		{
			CodeAssignStatement assign = s as CodeAssignStatement;
			if ( assign != null ) {
				GenerateAssignStatement( assign );
				return;
			}
			CodeAttachEventStatement attach = s as CodeAttachEventStatement;
			if ( attach != null ) {
				GenerateAttachEventStatement( attach );
				return;
			}
			CodeCommentStatement comment = s as CodeCommentStatement;
			if ( comment != null ) {
				GenerateCommentStatement( comment );
				return;
			}
			CodeConditionStatement condition = s as CodeConditionStatement;
			if ( condition != null ) {
				GenerateConditionStatement( condition );
				return;
			}
			CodeExpressionStatement expression = s as CodeExpressionStatement;
			if ( expression != null ) {
				GenerateExpressionStatement( expression );
				return;
			}
			CodeGotoStatement gotostmt = s as CodeGotoStatement;
			if ( gotostmt != null ) {
				GenerateGotoStatement( gotostmt );
				return;
			}
			CodeIterationStatement iteration = s as CodeIterationStatement;
			if ( iteration != null ) {
				GenerateIterationStatement( iteration );
				return;
			}
			CodeLabeledStatement label = s as CodeLabeledStatement;
			if ( label != null ) {
				GenerateLabeledStatement( label );
				return;
			}
			CodeMethodReturnStatement returnstmt = s as CodeMethodReturnStatement;
			if ( returnstmt != null ) {
				GenerateMethodReturnStatement( returnstmt );
				return;
			}
			CodeRemoveEventStatement remove = s as CodeRemoveEventStatement;
			if ( remove != null ) {
				GenerateRemoveEventStatement( remove );
				return;
			}
			CodeSnippetStatement snippet = s as CodeSnippetStatement;
			if ( snippet != null ) {
				GenerateSnippetStatement( snippet );
				return;
			}
			CodeThrowExceptionStatement exception = s as CodeThrowExceptionStatement;
			if ( exception != null ) {
				GenerateThrowExceptionStatement( exception );
				return;
			}
			CodeTryCatchFinallyStatement trycatch = s as CodeTryCatchFinallyStatement;
			if ( trycatch != null ) {
				GenerateTryCatchFinallyStatement( trycatch );
				return;
			}
			CodeVariableDeclarationStatement declaration = s as CodeVariableDeclarationStatement;
			if ( declaration != null ) {
				GenerateVariableDeclarationStatement( declaration );
				return;
			}
		}

		protected void GenerateStatements( CodeStatementCollection c )
		{
			foreach ( CodeStatement statement in c )
				GenerateStatement( statement );
		}

		protected abstract void GenerateThisReferenceExpression (CodeThisReferenceExpression e);
		protected abstract void GenerateThrowExceptionStatement (CodeThrowExceptionStatement s);
		protected abstract void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement s);
		protected abstract void GenerateTypeEnd( CodeTypeDeclaration declaration );
		protected abstract void GenerateTypeConstructor( CodeTypeConstructor constructor );

		protected virtual void GenerateTypeOfExpression (CodeTypeOfExpression e)
		{
			output.Write( "typeof(" );
			OutputType( e.Type );
			output.Write( ")" );
		}

		protected virtual void GenerateTypeReferenceExpression (CodeTypeReferenceExpression e)
		{
			OutputType( e.Type );
		}

		protected abstract void GenerateTypeStart( CodeTypeDeclaration declaration );
		protected abstract void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement e);
		protected abstract void GenerateVariableReferenceExpression( CodeVariableReferenceExpression e );

		//
		// Other members
		//
		
		/*
		 * Output Methods
		 */
		protected virtual void OutputAttributeArgument( CodeAttributeArgument argument )
		{
			string name = argument.Name;
			if ( name != null ) {
				output.Write( name );
				output.Write( '=' );
			}
			GenerateExpression( argument.Value );
		}

		private void OutputAttributeDeclaration( CodeAttributeDeclaration attribute )
		{
			output.Write( attribute.Name );
			output.Write( '(' );
			IEnumerator enumerator = attribute.Arguments.GetEnumerator();
			if ( enumerator.MoveNext() ) {
				CodeAttributeArgument argument = (CodeAttributeArgument)enumerator.Current;
				OutputAttributeArgument( argument );
				
				while ( enumerator.MoveNext() ) {
					output.Write( ',' );
					argument = (CodeAttributeArgument)enumerator.Current;
					OutputAttributeArgument( argument );
				}
			}
			output.Write( ')' );
		}

		protected virtual void OutputAttributeDeclarations( CodeAttributeDeclarationCollection attributes )
		{
			GenerateAttributeDeclarationsStart( attributes );
			
			IEnumerator enumerator = attributes.GetEnumerator();
			if ( enumerator.MoveNext() ) {
				CodeAttributeDeclaration attribute = (CodeAttributeDeclaration)enumerator.Current;

				OutputAttributeDeclaration( attribute );
				
				while ( enumerator.MoveNext() ) {
					attribute = (CodeAttributeDeclaration)enumerator.Current;

					output.WriteLine( ',' );
					OutputAttributeDeclaration( attribute );
				}
			}

			GenerateAttributeDeclarationsEnd( attributes );
		}

		protected virtual void OutputDirection( FieldDirection direction )
		{
			switch ( direction ) {
			case FieldDirection.In:
				output.Write( "in " );
				break;
			case FieldDirection.Out:
				output.Write( "out " );
				break;
			case FieldDirection.Ref:
				output.Write( "ref " );
				break;
			}
		}

		protected virtual void OutputExpressionList( CodeExpressionCollection expressions )
		{
			OutputExpressionList( expressions, false );
		}

		protected virtual void OutputExpressionList( CodeExpressionCollection expressions,
							     bool newLineBetweenItems )
		{
			IEnumerator enumerator = expressions.GetEnumerator();
			if ( enumerator.MoveNext() ) {
				CodeExpression expression = (CodeExpression)enumerator.Current;

				GenerateExpression( expression );
				
				while ( enumerator.MoveNext() ) {
					expression = (CodeExpression)enumerator.Current;
					
					output.Write( ',' );
					if ( newLineBetweenItems )
						output.WriteLine();
					else
						output.Write( ' ' );
					
					GenerateExpression( expression );
				}
			}
		}

		protected virtual void OutputFieldScopeModifier( MemberAttributes attributes )
		{
			if ( (attributes & MemberAttributes.VTableMask) == MemberAttributes.New )
				output.Write( "new " );

			switch ( attributes & MemberAttributes.ScopeMask ) {
			case MemberAttributes.Static:
				output.Write( "static " );
				break;
			case MemberAttributes.Const:
				output.Write( "const " );
				break;
			}
		}

		protected virtual void OutputIdentifier( string ident )
		{
		}

		protected virtual void OutputMemberAccessModifier( MemberAttributes attributes )
		{
			switch ( attributes & MemberAttributes.AccessMask ) {
			case MemberAttributes.Assembly:
				output.Write( "internal " );
				break;
			case MemberAttributes.FamilyAndAssembly:
				output.Write( "/* FamAndAssem */ internal " ); 
				break;
			case MemberAttributes.Family:
				output.Write( "protected " );
				break;
			case MemberAttributes.FamilyOrAssembly:
				output.Write( "protected internal " );
				break;
			case MemberAttributes.Private:
				output.Write( "private " );
				break;
			case MemberAttributes.Public:
				output.Write( "public " );
				break;
			}
		}

		protected virtual void OutputMemberScopeModifier( MemberAttributes attributes )
		{
			if ( (attributes & MemberAttributes.VTableMask) == MemberAttributes.New )
				output.Write( "new " );

			switch ( attributes & MemberAttributes.ScopeMask ) {
			case MemberAttributes.Abstract:
				output.Write( "abstract " );
				break;
			case MemberAttributes.Final:
				output.Write( "sealed " );
				break;
			case MemberAttributes.Static:
				output.Write( "static " );
				break;
			case MemberAttributes.Override:
				output.Write( "override " );
				break;
			default:
				//
				// FUNNY! if the scope value is
				// rubbish (0 or >Const), and access
				// is public or protected, make it
				// "virtual".
				//
				// i'm not sure whether this is 100%
				// correct, but it seems to be MS
				// behavior. 
				//
				MemberAttributes access = attributes & MemberAttributes.AccessMask;
				if ( access == MemberAttributes.Public || 
				     access == MemberAttributes.Family )
					output.Write( "virtual " );
				break;
			}
		}
				
		protected virtual void OutputOperator( CodeBinaryOperatorType op )
		{
		}

		protected virtual void OutputParameters( CodeParameterDeclarationExpressionCollection parameters )
		{
		}

		protected abstract void OutputType( CodeTypeReference t );

		protected virtual void OutputTypeAttributes( TypeAttributes attributes,
							     bool isStruct,
							     bool isEnum )
		{
			switch ( attributes & TypeAttributes.VisibilityMask ) {
			case TypeAttributes.NotPublic:
				// private by default
				break; 

			case TypeAttributes.Public:
			case TypeAttributes.NestedPublic:
				output.Write( "public " );
				break;

			case TypeAttributes.NestedPrivate:
				output.Write( "private " );
				break;
			}

			if ( isStruct )

				output.Write( "struct " );

			else if ( isEnum )

				output.Write( "enum " );

			else {
				if ( (attributes & TypeAttributes.Interface) != 0 ) 

					output.Write( "interface " );

				else {

					if ( (attributes & TypeAttributes.Sealed) != 0 )
						output.Write( "sealed " );

					if ( (attributes & TypeAttributes.Abstract) != 0 )
						output.Write( "abstract " );
					
					output.Write( "class " );
				}
			}
		}
		
		protected virtual void OutputTypeNamePair( CodeTypeReference type,
							   string name )
		{
			OutputType( type );
			output.Write( ' ' );
			output.Write( name );
		}

		protected abstract string QuoteSnippetString( string value );

		/*
		 * ICodeGenerator
		 */
		protected abstract string CreateEscapedIdentifier( string value );
		string ICodeGenerator.CreateEscapedIdentifier( string value )
		{
			return CreateEscapedIdentifier( value );
		}

		protected abstract string CreateValidIdentifier( string value );
		string ICodeGenerator.CreateValidIdentifier( string value )
		{
			return CreateValidIdentifier( value );
		}

		private void InitOutput( TextWriter output, CodeGeneratorOptions options )
		{
			if ( options == null )
				options = new CodeGeneratorOptions();
				
			this.output = new IndentedTextWriter( output, options.IndentString );
			this.options = options;
		}

		public virtual void GenerateCodeFromCompileUnit( CodeCompileUnit compileUnit,
								 TextWriter output,
								 CodeGeneratorOptions options )
		{
			InitOutput( output, options );
			GenerateCompileUnit( compileUnit );
		}

		[MonoTODO]
		public virtual void GenerateCodeFromExpression( CodeExpression expression,
								TextWriter output,
								CodeGeneratorOptions options )
		{
			InitOutput( output, options );
		}

		public virtual void GenerateCodeFromNamespace( CodeNamespace ns,
							       TextWriter output, 
							       CodeGeneratorOptions options )
		{
			InitOutput( output, options );
			GenerateNamespace( ns );
		}

		public virtual void GenerateCodeFromStatement( CodeStatement statement,
							       TextWriter output, 
							       CodeGeneratorOptions options )
		{
			InitOutput( output, options );
		}

		public virtual void GenerateCodeFromType( CodeTypeDeclaration type,
							  TextWriter output,
							  CodeGeneratorOptions options )
		{
			CodeTypeDeclaration prevType = this.currentType;
			this.currentType = type;

			InitOutput( output, options );

			GenerateTypeStart( type );

			foreach ( CodeTypeMember member in type.Members ) {

				CodeTypeMember prevMember = this.currentMember;
				this.currentMember = member;

				if ( options.BlankLinesBetweenMembers )
					output.WriteLine();

				CodeMemberEvent eventm = member as CodeMemberEvent;
				if ( eventm != null ) {
					GenerateEvent( eventm, type );
					continue;
				}
				CodeMemberField field = member as CodeMemberField;
				if ( field != null ) {
					GenerateField( field );
					continue;
				}
				CodeMemberMethod method = member as CodeMemberMethod;
				if ( method != null ) {
					GenerateMethod( method, type );
					continue;
				}
				CodeMemberProperty property = member as CodeMemberProperty;
				if ( property != null ) {
					GenerateProperty( property, type );
					continue;
				}
				CodeSnippetTypeMember snippet = member as CodeSnippetTypeMember;
				if ( snippet != null ) {
					GenerateSnippetMember( snippet );
					continue;
				}
				CodeTypeDeclaration subtype = member as CodeTypeDeclaration;
				if ( subtype != null ) {
					GenerateCodeFromType( subtype, output, options );
					continue;
				}
				
				this.currentMember = prevMember;
			}
				
			GenerateTypeEnd( type );
			this.currentType = prevType;
		}

		protected abstract string GetTypeOutput( CodeTypeReference type );

		string ICodeGenerator.GetTypeOutput( CodeTypeReference type )
		{
			return GetTypeOutput( type );
		}

		protected abstract bool IsValidIdentifier( string value );

		bool ICodeGenerator.IsValidIdentifier( string value )
		{
			return IsValidIdentifier( value );
		}

		protected abstract bool Supports( GeneratorSupport supports );

		bool ICodeGenerator.Supports( GeneratorSupport value )
		{
			return Supports( value );
		}

		[MonoTODO]
		protected virtual void ValidateIdentifier( string value )
		{
			throw new NotImplementedException();
		}

		void ICodeGenerator.ValidateIdentifier( string value )
		{
			ValidateIdentifier( value );
		}
	}
}