In .:
[mono.git] / mcs / class / System / Microsoft.VisualBasic / VBCodeGenerator.cs
1 //
2 // Microsoft.VisualBasic.VBCodeGenerator.cs
3 //
4 // Author:
5 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //   (partially based on CSharpCodeGenerator)
7 //   Jochen Wezel (jwezel@compumaster.de)
8 //
9 // (C) 2003 Andreas Nahr
10 // (C) 2003 Jochen Wezel (http://www.compumaster.de)
11 //
12 // Modifications:
13 // 2003-11-06 JW: some corrections regarding missing spaces in generated code (e. g. "Property ")
14 // 2003-11-06 JW: QuoteSnippetString implemented
15 // 2003-11-08 JW: automatically add Microsoft.VisualBasic
16 // 2003-11-12 JW: some corrections to allow correct compilation
17 // 2003-11-28 JW: implementing code differences into current build of this file
18 // 2003-12-10 JW: added "String." for the ChrW method because mbas doesn't support it without the String currently / TODO: remove it ASAP!
19
20 //
21 // Permission is hereby granted, free of charge, to any person obtaining
22 // a copy of this software and associated documentation files (the
23 // "Software"), to deal in the Software without restriction, including
24 // without limitation the rights to use, copy, modify, merge, publish,
25 // distribute, sublicense, and/or sell copies of the Software, and to
26 // permit persons to whom the Software is furnished to do so, subject to
27 // the following conditions:
28 // 
29 // The above copyright notice and this permission notice shall be
30 // included in all copies or substantial portions of the Software.
31 // 
32 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 //
40
41
42 using System;
43 using System.Globalization;
44 using System.Text;
45 using System.Text.RegularExpressions;
46 using System.CodeDom;
47 using System.CodeDom.Compiler;
48 using System.IO;
49 using System.Reflection;
50 using System.Collections;
51
52 namespace Microsoft.VisualBasic
53 {
54         internal class VBCodeGenerator : CodeGenerator
55         {
56                 private string[] Keywords = new string[] {
57                         "AddHandler", "AddressOf", "Alias", "And",
58                         "AndAlso", "Ansi", "As", "Assembly",
59                         "Auto", "Boolean", "ByRef", "Byte", 
60                         "ByVal", "Call", "Case", "Catch", 
61                         "CBool", "CByte", "CChar", "CDate", 
62                         "CDec", "CDbl", "Char", "CInt", 
63                         "Class", "CLng", "CObj", "Const", 
64                         "CShort", "CSng", "CStr", "CType", 
65                         "Date", "Decimal", "Declare", "Default", 
66                         "Delegate", "Dim", "DirectCast", "Do", 
67                         "Double", "Each", "Else", "ElseIf", 
68                         "End", "Enum", "Erase", "Error", 
69                         "Event", "Exit", "False", "Finally", 
70                         "For", "Friend", "Function", "Get", 
71                         "GetType", "GoSub", "GoTo", "Handles", 
72                         "If", "Implements", "Imports", "In", 
73                         "Inherits", "Integer", "Interface", "Is", 
74                         "Let", "Lib", "Like", "Long", 
75                         "Loop", "Me", "Mod", "Module", 
76                         "MustInherit", "MustOverride", "MyBase", "MyClass", 
77                         "Namespace", "New", "Next", "Not", 
78                         "Nothing", "NotInheritable", "NotOverridable", "Object", 
79                         "On", "Option", "Optional", "Or", 
80                         "OrElse", "Overloads", "Overridable", "Overrides", 
81                         "ParamArray", "Preserve", "Private", "Property", 
82                         "Protected", "Public", "RaiseEvent", "ReadOnly", 
83                         "ReDim", "REM", "RemoveHandler", "Resume", 
84                         "Return", "Select", "Set", "Shadows", 
85                         "Shared", "Short", "Single", "Static", 
86                         "Step", "Stop", "String", "Structure", 
87                         "Sub", "SyncLock", "Then", "Throw", 
88                         "To", "True", "Try", "TypeOf", 
89                         "Unicode", "Until", "Variant", "When", 
90                         "While", "With", "WithEvents", "WriteOnly", 
91                         "Xor" 
92                 };
93
94                 public VBCodeGenerator()
95                 {
96                 }
97
98                 protected override string NullToken {
99                         get {
100                                 return "Nothing";
101                         }
102                 }
103
104                 protected override void ContinueOnNewLine (string st)
105                 {
106                         Output.Write (st);
107                         Output.WriteLine (" _");
108                 }
109
110                 protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
111                 {
112                         TextWriter output = Output;
113
114                         output.Write ("New ");
115
116                         CodeExpressionCollection initializers = expression.Initializers;
117                         CodeTypeReference createType = expression.CreateType;
118
119                         if (initializers.Count > 0) {
120
121                                 OutputType (createType);
122                                 
123                                 output.WriteLine (" {");
124                                 ++Indent;
125                                 OutputExpressionList (initializers, true);
126                                 --Indent;
127                                 output.Write ("}");
128
129                         } 
130                         else {
131                                 CodeTypeReference arrayType = createType.ArrayElementType;
132                                 while (arrayType != null) 
133                                 {
134                                         createType = arrayType;
135                                         arrayType = arrayType.ArrayElementType;
136                                 }
137
138                                 OutputType (createType);
139
140                                 output.Write ('(');
141
142                                 CodeExpression size = expression.SizeExpression;
143                                 if (size != null)
144                                         GenerateExpression (size);
145                                 else
146                                         output.Write (expression.Size);
147
148                                 output.Write (')');
149                         }
150                 }
151
152                 protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)
153                 {
154                         Output.Write ("MyBase");
155                 }
156
157                 protected override void GenerateCastExpression (CodeCastExpression expression)
158                 {
159                         TextWriter output = Output;
160                         // ENHANCE: Use a DirectCast if it is known that expression.Expression is no Value-Type
161                         output.Write ("CType(");
162                         GenerateExpression (expression.Expression);
163                         output.Write (", ");
164                         OutputType (expression.TargetType);
165                         output.Write (")");
166                 }
167
168                 private bool AsBool(object datavalue)
169                 {
170                         return datavalue != null && datavalue is bool && (bool)datavalue;
171                 }
172                 
173                 private string OnOff(bool datavalue)
174                 {
175                         return datavalue?"On":"Off";
176                 }
177
178                 protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
179                 {
180                         GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
181                         GenerateComment (new CodeComment (" <autogenerated>"));
182                         GenerateComment (new CodeComment ("     This code was generated by a tool."));
183                         GenerateComment (new CodeComment ("     Mono Runtime Version: " + System.Environment.Version));
184                         GenerateComment (new CodeComment (""));
185                         GenerateComment (new CodeComment ("     Changes to this file may cause incorrect behavior and will be lost if "));
186                         GenerateComment (new CodeComment ("     the code is regenerated."));
187                         GenerateComment (new CodeComment (" </autogenerated>"));
188                         GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
189                         Output.WriteLine ();
190                         if (AsBool(compileUnit.UserData["AllowLateBound"])) {
191                                 Output.WriteLine("Option Explicit {0}",OnOff(AsBool(compileUnit.UserData["RequireVariableDeclaration"])));
192                                 Output.WriteLine("Option Strict Off");
193                         } else {
194                                 Output.WriteLine("Option Explicit On"); // Strict On implies Explicit On
195                                 Output.WriteLine("Option Strict On");
196                         }
197                         Output.WriteLine ();
198                 }
199
200                 protected override void GenerateCompileUnit (CodeCompileUnit compileUnit)
201                 {
202                         GenerateCompileUnitStart (compileUnit);
203
204                         OutputAttributes (compileUnit.AssemblyCustomAttributes,
205                                 "Assembly: ", LineHandling.NewLine);
206
207                         GenerateNamespaces (compileUnit);
208
209                         GenerateCompileUnitEnd (compileUnit);
210                 }
211
212                 protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
213                 {
214                         TextWriter output = Output;
215
216                         output.Write ("AddressOf ");
217
218                         CodeExpression targetObject = expression.TargetObject;
219                         if (targetObject != null) {
220                                 GenerateExpression (targetObject);
221                                 Output.Write ('.');
222                         }
223                         output.Write (expression.MethodName);
224                 }
225
226                 protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)
227                 {
228                         CodeExpression targetObject = expression.TargetObject;
229                         if (targetObject != null) {
230                                 GenerateExpression (targetObject);
231                                 Output.Write ('.');
232                         }
233                         Output.Write (expression.FieldName);
234                 }
235                 
236                 protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
237                 {
238                         Output.Write (expression.ParameterName);
239                 }
240
241                 protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
242                 {
243                         Output.Write (expression.VariableName);
244                 }
245                 
246                 protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
247                 {
248                         TextWriter output = Output;
249
250                         GenerateExpression (expression.TargetObject);
251                         output.Write ('(');
252                         OutputExpressionList (expression.Indices);
253                         output.Write (')');
254                 }
255                 
256                 protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)
257                 {
258                         TextWriter output = Output;
259
260                         GenerateExpression (expression.TargetObject);
261                         output.Write (".Item(");
262                         OutputExpressionList (expression.Indices);
263                         output.Write (')');
264                 }
265                 
266                 protected override void GenerateSnippetExpression (CodeSnippetExpression expression)
267                 {
268                         Output.Write (expression.Value);
269                 }
270                 
271                 protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)
272                 {
273                         TextWriter output = Output;
274
275                         GenerateMethodReferenceExpression (expression.Method);
276
277                         output.Write ('(');
278                         OutputExpressionList (expression.Parameters);
279                         output.Write (')');
280                 }
281
282                 protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
283                 {
284                         GenerateExpression (expression.TargetObject);
285                         Output.Write ('.');
286                         Output.Write (expression.MethodName);
287                 }
288
289                 protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
290                 {
291                         if (expression.TargetObject != null) {
292                                 GenerateExpression (expression.TargetObject);
293                                 Output.Write ('.');
294                         }
295                         Output.Write (CreateEscapedIdentifier(expression.EventName));
296                 }
297
298                 protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
299                 {
300                         Output.Write ("RaiseEvent ");
301                         GenerateExpression (expression.TargetObject);
302                         Output.Write ('(');
303                         OutputExpressionList (expression.Parameters);
304                         Output.WriteLine (')');
305                 }
306                 
307                 protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
308                 {
309                         Output.Write( "New " );
310                         OutputType (expression.CreateType);
311                         Output.Write ('(');
312                         OutputExpressionList (expression.Parameters);
313                         Output.Write (')');
314                 }
315
316                 protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
317                 {
318                         OutputAttributes (e.CustomAttributes, null, LineHandling.InLine);
319                         OutputDirection (e.Direction);
320                         OutputTypeNamePair (e.Type, e.Name);
321                 }
322
323                 protected override void GeneratePrimitiveExpression (CodePrimitiveExpression e)
324                 {
325                         if (e.Value is char) {
326                                 char c = (char) e.Value;
327                                 int ch = (int) c;
328 #if NET_2_0
329                                 Output.Write("Global.Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
330                         } else if (e.Value is ushort) {
331                                 ushort uc = (ushort) e.Value;
332                                 Output.Write (uc.ToString(CultureInfo.InvariantCulture));
333                                 Output.Write ("US");
334                         } else if (e.Value is uint) {
335                                 uint ui = (uint) e.Value;
336                                 Output.Write (ui.ToString(CultureInfo.InvariantCulture));
337                                 Output.Write ("UI");
338                         } else if (e.Value is ulong) {
339                                 ulong ul = (ulong) e.Value;
340                                 Output.Write (ul.ToString(CultureInfo.InvariantCulture));
341                                 Output.Write ("UL");
342                         } else if (e.Value is sbyte) {
343                                 sbyte sb = (sbyte) e.Value;
344                                 Output.Write ("CSByte(");
345                                 Output.Write (sb.ToString(CultureInfo.InvariantCulture));
346                                 Output.Write (')');
347 #else
348                                 Output.Write("Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
349 #endif
350                         } else {
351                                 base.GeneratePrimitiveExpression(e);
352                         }
353                 }
354
355                 protected override void GenerateSingleFloatValue (float s)
356                 {
357                         base.GenerateSingleFloatValue (s);
358                         base.Output.Write ('!');
359                 }
360
361                 protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
362                 {
363                         GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
364                 }
365
366                 protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
367                 {
368                         Output.Write ("Value"); 
369                 }
370
371                 protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)
372                 {
373                         Output.Write ("Me");
374                 }
375
376                 protected override void GenerateExpressionStatement (CodeExpressionStatement statement)
377                 {
378                         GenerateExpression (statement.Expression);
379                         Output.WriteLine (); //start new line
380                 }
381
382                 protected override void GenerateIterationStatement (CodeIterationStatement statement)
383                 {
384                         TextWriter output = Output;
385
386                         GenerateStatement (statement.InitStatement);
387                         output.Write ("Do While ");
388                         GenerateExpression (statement.TestExpression);
389                         output.WriteLine ();
390                         Indent++;
391                         GenerateStatements (statement.Statements);
392                         GenerateStatement (statement.IncrementStatement);
393                         Indent--;
394                         output.WriteLine ("Loop");
395                 }
396
397                 protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)
398                 {
399                         Output.Write ("Throw");
400                         if (statement.ToThrow != null) {
401                                 Output.Write (' ');
402                                 GenerateExpression (statement.ToThrow);
403                         }
404                         Output.WriteLine ();
405                 }
406
407                 protected override void GenerateComment (CodeComment comment)
408                 {
409                         TextWriter output = Output;
410                         string commentChars = null;
411
412                         if (comment.DocComment) {
413                                 commentChars = "'''";
414                         } else {
415                                 commentChars = "'";
416                         }
417         
418                         output.Write (commentChars);
419                         string text = comment.Text;
420
421                         for (int i = 0; i < text.Length; i++) {
422                                 output.Write (text[i]);
423                                 if (text[i] == '\r') {
424                                         if (i < (text.Length - 1) && text[i + 1] == '\n') {
425                                                 continue;
426                                         }
427                                         output.Write (commentChars);
428                                 } else if (text[i] == '\n') {
429                                         output.Write (commentChars);
430                                 }
431                         }
432
433                         output.WriteLine ();
434                 }
435
436                 protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)
437                 {
438                         TextWriter output = Output;
439
440                         if (statement.Expression != null) {
441                                 output.Write ("Return ");
442                                 GenerateExpression (statement.Expression);
443                                 output.WriteLine ();
444                         } else {
445                                 output.WriteLine ("Return");
446                         }
447                 }
448
449                 protected override void GenerateConditionStatement (CodeConditionStatement statement)
450                 {
451                         TextWriter output = Output;
452                         output.Write ("If ");
453
454                         GenerateExpression (statement.Condition);
455
456                         output.WriteLine (" Then");
457                         ++Indent;
458                         GenerateStatements (statement.TrueStatements);
459                         --Indent;
460
461                         CodeStatementCollection falses = statement.FalseStatements;
462                         if (falses.Count > 0) {
463                                 output.WriteLine ("Else");
464                                 ++Indent;
465                                 GenerateStatements (falses);
466                                 --Indent;
467                         }
468                         else {
469                                 if (Options.ElseOnClosing)
470                                         output.WriteLine ("Else");
471                         }
472                         output.WriteLine ("End If");
473                 }
474
475                 protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)
476                 {
477                         TextWriter output = Output;
478
479                         output.WriteLine ("Try ");
480                         ++Indent;
481                         GenerateStatements (statement.TryStatements);
482                         --Indent;
483                         
484                         foreach (CodeCatchClause clause in statement.CatchClauses) {
485                                 output.Write ("Catch ");
486                                 OutputTypeNamePair (clause.CatchExceptionType, clause.LocalName);
487                                 output.WriteLine ();
488                                 ++Indent;
489                                 GenerateStatements (clause.Statements);
490                                 --Indent;
491                         }
492
493                         CodeStatementCollection finallies = statement.FinallyStatements;
494                         if (finallies.Count > 0) {
495
496                                 output.WriteLine ("Finally");
497                                 ++Indent;
498                                 GenerateStatements (finallies);
499                                 --Indent;
500                         }
501
502                         output.WriteLine("End Try");
503                 }
504
505                 protected override void GenerateAssignStatement (CodeAssignStatement statement)
506                 {                       
507                         TextWriter output = Output;
508                         GenerateExpression (statement.Left);
509                         output.Write (" = ");
510                         GenerateExpression (statement.Right);
511                         output.WriteLine ();
512                 }
513
514                 protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)
515                 {
516                         TextWriter output = Output;
517
518                         Output.Write ("AddHandler ");
519                         GenerateEventReferenceExpression (statement.Event);
520                         Output.Write ( ", ");
521                         GenerateExpression (statement.Listener);
522                         output.WriteLine ();
523                 }
524
525                 protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)
526                 {
527                         TextWriter output = Output;
528
529                         Output.Write ("RemoveHandler ");
530                         GenerateEventReferenceExpression (statement.Event);
531                         Output.Write ( ", ");
532                         GenerateExpression (statement.Listener);
533                         output.WriteLine ();
534                 }
535
536                 protected override void GenerateGotoStatement (CodeGotoStatement statement)
537                 {
538                         TextWriter output = Output;
539
540                         output.Write ("goto ");
541                         output.Write (statement.Label);
542                         output.WriteLine ();
543                 }
544                 
545                 protected override void GenerateLabeledStatement (CodeLabeledStatement statement)
546                 {
547                         TextWriter output = Output;
548
549                         Indent--;
550                         output.WriteLine (statement.Label + ":");
551                         Indent++;
552                         if (statement.Statement != null) {
553                                 GenerateStatement (statement.Statement);
554                         }
555                 }
556
557                 protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
558                 {
559                         TextWriter output = Output;
560
561                         output.Write ("GetType(");
562                         OutputType (e.Type);
563                         output.Write (")");
564                 }
565
566                 protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )
567                 {
568                         TextWriter output = Output;
569
570                         output.Write ("Dim ");
571                         OutputTypeNamePair (statement.Type, statement.Name);
572
573                         CodeExpression initExpression = statement.InitExpression;
574                         if (initExpression != null) 
575                         {
576                                 output.Write (" = ");
577                                 GenerateExpression (initExpression);
578                         }
579
580                         output.WriteLine();
581                 }
582
583                 protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
584                 {
585                         Output.WriteLine ();
586                         Output.Write ("#ExternalSource(");
587                         Output.Write (linePragma.FileName);
588                         Output.Write (", ");
589                         Output.Write (linePragma.LineNumber);
590                         Output.WriteLine (")");
591                 }
592
593                 protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
594                 {
595                         Output.WriteLine ("#End ExternalSource");
596                 }
597
598                 protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
599                 {
600                         if (IsCurrentDelegate || IsCurrentEnum) {
601                                 return;
602                         }
603
604                         TextWriter output = Output;
605
606                         OutputAttributes (eventRef.CustomAttributes, null, 
607                                 LineHandling.ContinueLine);
608
609                         OutputMemberAccessModifier (eventRef.Attributes);
610
611                         output.Write ("Event ");
612                         OutputTypeNamePair (eventRef.Type, GetEventName(eventRef));
613
614 #if NET_2_0
615                         if (eventRef.ImplementationTypes.Count > 0) {
616                                 OutputImplementationTypes (eventRef.ImplementationTypes, eventRef.Name);
617                         } else if (eventRef.PrivateImplementationType != null) {
618                                 output.Write (" Implements ");
619                                 OutputType (eventRef.PrivateImplementationType);
620                                 output.Write ('.');
621                                 output.Write (eventRef.Name);
622                         }
623 #endif
624
625                         output.WriteLine ();
626                 }
627
628                 protected override void GenerateField (CodeMemberField field)
629                 {
630                         if (IsCurrentDelegate || IsCurrentInterface) {
631                                 return;
632                         }
633
634                         TextWriter output = Output;
635
636                         OutputAttributes (field.CustomAttributes, null, 
637                                 LineHandling.ContinueLine);
638
639                         if (IsCurrentEnum) {
640                                 output.Write (field.Name);
641                         } else {
642                                 MemberAttributes attributes = field.Attributes;
643                                 OutputMemberAccessModifier (attributes);
644                                 OutputVTableModifier (attributes);
645                                 OutputFieldScopeModifier (attributes);
646                                 OutputTypeNamePair (field.Type, field.Name);
647                         }
648
649                         CodeExpression initExpression = field.InitExpression;
650                         if (initExpression != null) {
651                                 output.Write (" = ");
652                                 GenerateExpression (initExpression);
653                         }
654
655                         output.WriteLine();
656                 }
657                 
658                 protected override void GenerateSnippetMember (CodeSnippetTypeMember member)
659                 {
660                         Output.Write (member.Text);
661                 }
662                 
663                 protected override void GenerateEntryPointMethod (CodeEntryPointMethod method, CodeTypeDeclaration declaration)
664                 {
665 #if NET_2_0
666                         OutputAttributes (method.CustomAttributes, null, 
667                                 LineHandling.ContinueLine);
668 #endif
669
670                         Output.WriteLine ("Public Shared Sub Main()");
671                         Indent++;
672                         GenerateStatements (method.Statements);
673                         Indent--;
674                         Output.WriteLine ("End Sub");
675                 }
676                 
677                 [MonoTODO ("partially implemented")]
678                 protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)
679                 {
680                         if (IsCurrentDelegate || IsCurrentEnum) {
681                                 return;
682                         }
683
684                         bool isSub = method.ReturnType.BaseType == typeof(void).FullName;
685
686                         TextWriter output = Output;
687
688                         OutputAttributes (method.CustomAttributes, null, 
689                                 LineHandling.ContinueLine);
690
691                         MemberAttributes attributes = method.Attributes;
692
693                         if (!IsCurrentInterface) {
694                                 if (method.PrivateImplementationType == null) {
695                                         OutputMemberAccessModifier (attributes);
696                                         if (IsOverloaded (method, declaration)) {
697                                                 output.Write ("Overloads ");
698                                         }
699                                 }
700                                 OutputVTableModifier (attributes);
701                                 OutputMemberScopeModifier (attributes);
702                         } else {
703                                 OutputVTableModifier (attributes);
704                         }
705
706                         if (isSub)
707                                 output.Write ("Sub ");
708                         else
709                                 output.Write ("Function ");
710
711                         output.Write (GetMethodName(method));
712                         output.Write ('(');
713                         OutputParameters (method.Parameters);
714                         output.Write (')');
715
716                         if (!isSub) {
717                                 output.Write (" As ");
718                                 OutputAttributes (method.ReturnTypeCustomAttributes, null,
719                                         LineHandling.InLine);
720                                 OutputType (method.ReturnType);
721                         }
722
723                         if (method.ImplementationTypes.Count > 0) {
724                                 OutputImplementationTypes (method.ImplementationTypes, method.Name);
725                         } else if (method.PrivateImplementationType != null) {
726                                 output.Write (" Implements ");
727                                 OutputType (method.PrivateImplementationType);
728                                 output.Write ('.');
729                                 output.Write (method.Name);
730                         }
731
732                         output.WriteLine ();
733                         if (!IsCurrentInterface) {
734                                 if ((attributes & MemberAttributes.ScopeMask) != MemberAttributes.Abstract) {
735                                         ++Indent;
736                                         GenerateStatements (method.Statements);
737                                         --Indent;
738                                         if (isSub)
739                                                 output.WriteLine ("End Sub");
740                                         else
741                                                 output.WriteLine ("End Function");
742                                 }
743                         }
744                 }
745
746                 protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)
747                 {
748                         if (IsCurrentDelegate || IsCurrentEnum) {
749                                 return;
750                         }
751
752                         TextWriter output = Output;
753
754                         OutputAttributes (property.CustomAttributes, null, 
755                                 LineHandling.ContinueLine);
756
757                         MemberAttributes attributes = property.Attributes;
758
759                         if (!IsCurrentInterface) {
760                                 if (property.PrivateImplementationType == null) {
761                                         OutputMemberAccessModifier (attributes);
762                                         if (IsOverloaded (property, declaration)) {
763                                                 output.Write ("Overloads ");
764                                         }
765                                 }
766                                 OutputVTableModifier (attributes);
767                                 OutputMemberScopeModifier (attributes);
768                         } else {
769                                 OutputVTableModifier (attributes);
770                         }
771
772                         // mark property as default property if we're dealing with an indexer
773                         if (string.Compare (GetPropertyName(property), "Item", true, CultureInfo.InvariantCulture) == 0 && property.Parameters.Count > 0) {
774                                 output.Write ("Default ");
775                         }
776
777                         if (property.HasGet && (!property.HasSet))
778                                 output.Write ("ReadOnly " );
779
780                         if (property.HasSet && (!property.HasGet))
781                                 output.Write ("WriteOnly " );
782
783                         output.Write ("Property ");
784                         Output.Write (GetPropertyName (property));
785 #if NET_2_0
786                         // in .NET 2.0, always output parantheses (whether or not there 
787                         // are any parameters to output
788                         Output.Write ('(');
789                         OutputParameters (property.Parameters);
790                         Output.Write (')');
791 #else
792                         if (property.Parameters.Count > 0) {
793                                 Output.Write ('(');
794                                 OutputParameters (property.Parameters);
795                                 Output.Write (')');
796                         }
797 #endif
798                         Output.Write (" As ");
799                         Output.Write (GetTypeOutput(property.Type));
800
801                         if (property.ImplementationTypes.Count > 0) {
802                                 OutputImplementationTypes (property.ImplementationTypes, property.Name);
803                         } else if (property.PrivateImplementationType != null) {
804                                 output.Write (" Implements ");
805                                 OutputType (property.PrivateImplementationType);
806                                 output.Write ('.');
807                                 output.Write (property.Name);
808                         }
809
810                         output.WriteLine ();
811
812                         if (!IsCurrentInterface) {
813                                 ++Indent;
814                                 if (property.HasGet) {
815                                         output.WriteLine ("Get");
816                                         ++Indent;
817                                         GenerateStatements (property.GetStatements);
818                                         --Indent;
819                                         output.WriteLine ("End Get");
820                                 }
821
822                                 if (property.HasSet) {
823                                         output.WriteLine ("Set");
824                                         ++Indent;
825                                         GenerateStatements (property.SetStatements);
826                                         --Indent;
827                                         output.WriteLine ("End Set");
828                                 }
829
830                                 --Indent;
831                                 output.WriteLine ("End Property");
832                         }
833                 }
834
835                 protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
836                 {
837                         if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
838                                 return;
839                         }
840
841                         OutputAttributes (constructor.CustomAttributes, null,
842                                 LineHandling.ContinueLine);
843                         OutputMemberAccessModifier (constructor.Attributes);
844                         Output.Write ("Sub New(");
845                         OutputParameters (constructor.Parameters);
846                         Output.WriteLine (")");
847                         Indent++;
848                         // check if ctor passes args on to other ctor in class
849                         CodeExpressionCollection ctorArgs = constructor.ChainedConstructorArgs;
850                         if (ctorArgs.Count > 0) {
851                                 Output.Write ("Me.New(");
852                                 OutputExpressionList (ctorArgs);
853                                 Output.WriteLine (")");
854                         } else {
855                                 // check if ctor passes args on to ctor in base class
856                                 ctorArgs = constructor.BaseConstructorArgs;
857                                 if (ctorArgs.Count > 0) {
858                                         Output.Write ("MyBase.New(");
859                                         OutputExpressionList (ctorArgs);
860                                         Output.WriteLine (")");
861 #if NET_2_0
862                                 } else if (IsCurrentClass) {
863 #else
864                                 } else {
865 #endif
866                                         // call default base ctor
867                                         Output.WriteLine ("MyBase.New()");
868                                 }
869                         }
870                         GenerateStatements (constructor.Statements);
871                         Indent--;
872                         Output.WriteLine ("End Sub");
873                 }
874                 
875                 protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
876                 {
877                         if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
878                                 return;
879                         }
880
881 #if NET_2_0
882                         OutputAttributes (constructor.CustomAttributes, null,
883                                 LineHandling.ContinueLine);
884 #endif
885
886                         Output.WriteLine ("Shared Sub New()");
887                         Indent++;
888                         GenerateStatements (constructor.Statements);
889                         Indent--;
890                         Output.WriteLine ("End Sub");
891                 }
892
893                 [MonoTODO ("not implemented")]
894                 protected override void GenerateTypeStart (CodeTypeDeclaration declaration)
895                 {
896                         TextWriter output = Output;
897
898                         OutputAttributes (declaration.CustomAttributes, null, 
899                                 LineHandling.ContinueLine);
900
901                         TypeAttributes attributes = declaration.TypeAttributes;
902
903                         if (IsCurrentDelegate) {
904                                 CodeTypeDelegate delegateDecl = (CodeTypeDelegate) declaration;
905
906                                 if ((attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
907                                         output.Write ("Public ");
908                                 }
909
910                                 bool isSub = delegateDecl.ReturnType.BaseType == typeof (void).FullName;
911                                 if (isSub) {
912                                         output.Write ("Delegate Sub ");
913                                 } else {
914                                         output.Write ("Delegate Function ");
915                                 }
916
917                                 output.Write (delegateDecl.Name);
918                                 output.Write ("(");
919                                 Output.Write (")");
920                                 if (!isSub) {
921                                         Output.Write (" As ");
922                                         OutputType (delegateDecl.ReturnType);
923                                 }
924                                 Output.WriteLine ("");
925                         } else {
926                                 OutputTypeAttributes (declaration);
927
928                                 output.Write (declaration.Name);
929
930                                 if (IsCurrentEnum) {
931                                         if (declaration.BaseTypes.Count > 0) {
932                                                 output.Write (" As ");
933                                                 OutputType (declaration.BaseTypes[0]);
934                                         }
935                                         output.WriteLine ();
936                                         ++Indent;
937                                 } else {
938                                         ++Indent;
939
940                                         bool firstInherits = true;
941                                         bool firstImplements = true;
942
943                                         for (int i = 0; i < declaration.BaseTypes.Count; i++) {
944                                                 // a struct can only implement interfaces
945                                                 // an interface can only inherit from other interface
946
947                                                 CodeTypeReference typeRef = declaration.BaseTypes[i];
948                                                 
949                                                 if (firstInherits && !declaration.IsStruct && !typeRef.IsInterface) {
950                                                         output.WriteLine ();
951                                                         output.Write ("Inherits ");
952                                                         firstInherits = false;
953                                                 } else if (!declaration.IsInterface && firstImplements) {
954                                                         output.WriteLine ();
955                                                         output.Write ("Implements ");
956                                                         firstImplements = false;
957                                                 } else {
958                                                         output.Write (", ");
959                                                 }
960                                                 OutputType (typeRef);
961                                         }
962                                         output.WriteLine ();
963                                 }
964                         }
965                 }
966
967                 protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)
968                 {
969                         if (IsCurrentDelegate) {
970                                 return;
971                         }
972                         string output = string.Empty;
973
974                         --Indent;
975                         if (declaration.IsStruct)
976                                 output = "End Structure";
977                         if (declaration.IsInterface)
978                                 output = "End Interface";
979                         if (declaration.IsEnum)
980                                 output = "End Enum";
981                         if (declaration.IsClass)
982                                 output = "End Class";
983
984                         Output.WriteLine (output);
985                 }
986
987                 protected override void GenerateNamespace(CodeNamespace ns)
988                 {
989                         GenerateCommentStatements (ns.Comments);
990                         
991                         // add regular imports
992                         GenerateNamespaceImports (ns);
993
994                         Output.WriteLine (); 
995                         GenerateNamespaceStart (ns); 
996                         GenerateTypes (ns);
997                         GenerateNamespaceEnd (ns);
998                 }
999
1000
1001                 protected override void GenerateNamespaceStart (CodeNamespace ns)
1002                 {
1003                         TextWriter output = Output;
1004                         
1005                         string name = ns.Name;
1006                         if (name != null && name != string.Empty) {
1007                                 output.Write ("Namespace ");
1008                                 output.WriteLine (name);
1009                                 ++Indent;
1010                         }
1011                 }
1012
1013                 protected override void GenerateNamespaceEnd (CodeNamespace ns)
1014                 {
1015                         string name = ns.Name;
1016                         if (name != null && name != string.Empty) {
1017                                 --Indent;
1018                                 Output.WriteLine ("End Namespace");
1019                         }
1020                 }
1021
1022                 protected override void GenerateNamespaceImport (CodeNamespaceImport import)
1023                 {
1024                         TextWriter output = Output;
1025
1026                         output.Write ("Imports ");
1027                         output.Write (import.Namespace);
1028                         output.WriteLine ();
1029                 }
1030                 
1031                 protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
1032                 {
1033                         Output.Write ('<');
1034                 }
1035                 
1036                 protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
1037                 {
1038                         Output.Write (">");
1039                 }
1040
1041                 private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, LineHandling lineHandling) {
1042                         if (attributes.Count == 0) {
1043                                 return;
1044                         }
1045
1046                         GenerateAttributeDeclarationsStart (attributes);
1047
1048                         IEnumerator enumerator = attributes.GetEnumerator ();
1049                         if (enumerator.MoveNext ()) {
1050                                 CodeAttributeDeclaration att = (CodeAttributeDeclaration) enumerator.Current;
1051                                 if (prefix != null) {
1052                                         Output.Write (prefix);
1053                                 }
1054                                 OutputAttributeDeclaration (att);
1055
1056                                 while (enumerator.MoveNext ()) {
1057                                         Output.Write (", ");
1058                                         if (lineHandling != LineHandling.InLine) {
1059                                                 ContinueOnNewLine ("");
1060                                                 Output.Write (" ");
1061                                         }
1062                                         att = (CodeAttributeDeclaration) enumerator.Current;
1063                                         if (prefix != null) {
1064                                                 Output.Write (prefix);
1065                                         }
1066                                         OutputAttributeDeclaration (att);
1067                                 }
1068                         }
1069                         GenerateAttributeDeclarationsEnd (attributes);
1070                         Output.Write (" ");
1071
1072                         switch (lineHandling) {
1073                                 case LineHandling.ContinueLine:
1074                                         ContinueOnNewLine ("");
1075                                         break;
1076                                 case LineHandling.NewLine:
1077                                         Output.WriteLine ();
1078                                         break;
1079                         }
1080                 }
1081
1082                 protected override void OutputAttributeArgument (CodeAttributeArgument argument)
1083                 {
1084                         string name = argument.Name;
1085                         if (name != null && name.Length > 0) {
1086                                 Output.Write (name);
1087                                 Output.Write (":=");
1088                         }
1089                         GenerateExpression (argument.Value);
1090                 }
1091
1092                 private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
1093                 {
1094                         Output.Write (attribute.Name.Replace ('+', '.'));
1095                         Output.Write ('(');
1096                         IEnumerator enumerator = attribute.Arguments.GetEnumerator ();
1097                         if (enumerator.MoveNext ()) {
1098                                 CodeAttributeArgument argument = (CodeAttributeArgument) enumerator.Current;
1099                                 OutputAttributeArgument (argument);
1100
1101                                 while (enumerator.MoveNext ()) {
1102                                         Output.Write (", ");
1103                                         argument = (CodeAttributeArgument) enumerator.Current;
1104                                         OutputAttributeArgument (argument);
1105                                 }
1106                         }
1107                         Output.Write (')');
1108                 }
1109
1110                 protected override void OutputDirection (FieldDirection direction)
1111                 {
1112                         switch (direction) {
1113                         case FieldDirection.In:
1114                                 Output.Write ("ByVal ");
1115                                 break;
1116                         case FieldDirection.Out:
1117                         case FieldDirection.Ref:
1118                                 Output.Write ("ByRef ");
1119                                 break;
1120                         }
1121                 }
1122
1123                 protected override void OutputFieldScopeModifier (MemberAttributes attributes)
1124                 {
1125                         switch (attributes & MemberAttributes.ScopeMask) {
1126                                 case MemberAttributes.Static:
1127                                         Output.Write ("Shared ");
1128                                         break;
1129                                 case MemberAttributes.Const:
1130                                         Output.Write ("Const ");
1131                                         break;
1132                         }
1133                 }
1134
1135                 private void OutputImplementationTypes (CodeTypeReferenceCollection implementationTypes, string member)
1136                 {
1137                         IEnumerator enumerator = implementationTypes.GetEnumerator ();
1138                         if (enumerator.MoveNext ()) {
1139                                 Output.Write (" Implements ");
1140
1141                                 CodeTypeReference typeReference = (CodeTypeReference) enumerator.Current;
1142                                 OutputType (typeReference);
1143                                 Output.Write ('.');
1144                                 OutputIdentifier (member);
1145
1146                                 while (enumerator.MoveNext ()) {
1147                                         Output.Write (" , ");
1148                                         typeReference = (CodeTypeReference) enumerator.Current;
1149                                         OutputType (typeReference);
1150                                         Output.Write ('.');
1151                                         OutputIdentifier (member);
1152                                 }
1153                         }
1154                 }
1155
1156                 protected override void OutputMemberAccessModifier (MemberAttributes attributes)
1157                 {
1158                         switch (attributes & MemberAttributes.AccessMask) {
1159                                 case MemberAttributes.Assembly:
1160                                 case MemberAttributes.FamilyAndAssembly:
1161                                         Output.Write ("Friend "); 
1162                                         break;
1163                                 case MemberAttributes.Family:
1164                                         Output.Write ("Protected ");
1165                                         break;
1166                                 case MemberAttributes.FamilyOrAssembly:
1167                                         Output.Write ("Protected Friend ");
1168                                         break;
1169                                 case MemberAttributes.Private:
1170                                         Output.Write ("Private ");
1171                                         break;
1172                                 case MemberAttributes.Public:
1173                                         Output.Write ("Public ");
1174                                         break;
1175                         }
1176                 }
1177
1178                 private void OutputVTableModifier (MemberAttributes attributes)
1179                 {
1180                         if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New) {
1181                                 Output.Write ("Shadows ");
1182                         }
1183                 }
1184
1185                 protected override void OutputMemberScopeModifier (MemberAttributes attributes)
1186                 {
1187                         switch (attributes & MemberAttributes.ScopeMask) {
1188                                 case MemberAttributes.Abstract:
1189                                         Output.Write ("MustOverride ");
1190                                         break;
1191                                 case MemberAttributes.Final:
1192                                         // do nothing
1193                                         break;
1194                                 case MemberAttributes.Static:
1195                                         Output.Write ("Shared ");
1196                                         break;
1197                                 case MemberAttributes.Override:
1198                                         Output.Write ("Overrides ");
1199                                         break;
1200                                 case MemberAttributes.Overloaded:
1201                                         // based on http://gendotnet.com/Code%20Gen%20Articles/codedom.htm
1202                                         Output.Write ("Overloads ");
1203
1204                                         MemberAttributes access_ovl = attributes & MemberAttributes.AccessMask;
1205                                         if (access_ovl == MemberAttributes.Public || access_ovl == MemberAttributes.Family) {
1206                                                 Output.Write ("Overridable ");
1207                                         }
1208                                         break;
1209                                 default:
1210                                         //
1211                                         // FUNNY! if the scope value is
1212                                         // rubbish (0 or >Const), and access
1213                                         // is public, protected make it
1214                                         // "virtual".
1215                                         //
1216                                         // i'm not sure whether this is 100%
1217                                         // correct, but it seems to be MS
1218                                         // behavior.
1219                                         //
1220                                         // On MS.NET 2.0, internal properties
1221                                         // are also marked "virtual".
1222                                         //
1223                                         MemberAttributes access = attributes & MemberAttributes.AccessMask;
1224                                         if (access == MemberAttributes.Public || 
1225 #if NET_2_0
1226                                                 access == MemberAttributes.Family || access == MemberAttributes.Assembly)
1227 #else
1228                                                 access == MemberAttributes.Family)
1229 #endif
1230                                                 Output.Write ("Overridable ");
1231                                         break;
1232                         }
1233                 }
1234
1235                 protected override void OutputOperator (CodeBinaryOperatorType op)
1236                 {
1237                         switch (op) {
1238                         case CodeBinaryOperatorType.Add:
1239                                 Output.Write ("+");
1240                                 break;
1241                         case CodeBinaryOperatorType.Subtract:
1242                                 Output.Write ("-");
1243                                 break;
1244                         case CodeBinaryOperatorType.Multiply:
1245                                 Output.Write ("*");
1246                                 break;
1247                         case CodeBinaryOperatorType.Divide:
1248                                 Output.Write ("/");
1249                                 break;
1250                         case CodeBinaryOperatorType.Modulus:
1251                                 Output.Write ("Mod");
1252                                 break;
1253                         case CodeBinaryOperatorType.Assign:
1254                                 Output.Write ("=");
1255                                 break;
1256                         case CodeBinaryOperatorType.IdentityInequality:
1257                                 Output.Write ("<>");
1258                                 break;
1259                         case CodeBinaryOperatorType.IdentityEquality:
1260                                 Output.Write ("Is");
1261                                 break;
1262                         case CodeBinaryOperatorType.ValueEquality:
1263                                 Output.Write ("=");
1264                                 break;
1265                         case CodeBinaryOperatorType.BitwiseOr:
1266                                 Output.Write ("Or");
1267                                 break;
1268                         case CodeBinaryOperatorType.BitwiseAnd:
1269                                 Output.Write ("And");
1270                                 break;
1271                         case CodeBinaryOperatorType.BooleanOr:
1272                                 Output.Write ("OrElse");
1273                                 break;
1274                         case CodeBinaryOperatorType.BooleanAnd:
1275                                 Output.Write ("AndAlso");
1276                                 break;
1277                         case CodeBinaryOperatorType.LessThan:
1278                                 Output.Write ("<");
1279                                 break;
1280                         case CodeBinaryOperatorType.LessThanOrEqual:
1281                                 Output.Write ("<=");
1282                                 break;
1283                         case CodeBinaryOperatorType.GreaterThan:
1284                                 Output.Write (">");
1285                                 break;
1286                         case CodeBinaryOperatorType.GreaterThanOrEqual:
1287                                 Output.Write (">=");
1288                                 break;
1289                         }
1290                 }
1291
1292                 private void OutputTypeAttributes (CodeTypeDeclaration declaration)
1293                 {
1294                         TextWriter output = Output;
1295                         TypeAttributes attributes = declaration.TypeAttributes;
1296
1297                         switch (attributes & TypeAttributes.VisibilityMask) {
1298                                 case TypeAttributes.Public:
1299                                 case TypeAttributes.NestedPublic:
1300                                         output.Write ("Public ");
1301                                         break;
1302                                 case TypeAttributes.NestedPrivate:
1303                                         output.Write ("Private ");
1304                                         break;
1305 #if NET_2_0
1306                                 case TypeAttributes.NotPublic:
1307                                 case TypeAttributes.NestedFamANDAssem:
1308                                 case TypeAttributes.NestedAssembly:
1309                                         output.Write ("Friend ");
1310                                         break; 
1311                                 case TypeAttributes.NestedFamily:
1312                                         output.Write ("Protected ");
1313                                         break;
1314                                 case TypeAttributes.NestedFamORAssem:
1315                                         output.Write ("Protected Friend ");
1316                                         break;
1317 #endif
1318                         }
1319
1320                         if (declaration.IsStruct) {
1321                                 output.Write ("Structure ");
1322                         } else if (declaration.IsEnum) {
1323                                 output.Write ("Enum ");
1324                         } else {
1325                                 if ((attributes & TypeAttributes.Interface) != 0) {
1326                                         output.Write ("Interface ");
1327                                 } else {
1328                                         if ((attributes & TypeAttributes.Sealed) != 0)
1329                                                 output.Write ("NotInheritable ");
1330
1331                                         if ((attributes & TypeAttributes.Abstract) != 0)
1332                                                 output.Write ("MustInherit ");
1333
1334                                         output.Write ("Class ");
1335                                 }
1336                         }
1337                 }
1338
1339                 protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)
1340                 {
1341 #if NET_2_0
1342                         if (name.Length == 0) {
1343                                 name = "__exception";
1344                         }
1345 #endif
1346                         Output.Write (CreateEscapedIdentifier(name) + " As " + GetTypeOutput (typeRef));
1347                 }
1348
1349                 protected override void OutputType (CodeTypeReference type)
1350                 {
1351                         Output.Write (GetTypeOutput (type));
1352                 }
1353
1354                 protected override string QuoteSnippetString (string value)
1355                 {
1356                         StringBuilder mySBuilder = new StringBuilder(value.Length);
1357                         mySBuilder.Append ("\"");
1358                         bool inQuotes = true;
1359                         for (int MyCounter = 0; MyCounter < value.Length; MyCounter++)
1360                         {
1361                                 if (value[MyCounter] == 34) //quotation mark
1362                                 {
1363                                         if (!inQuotes)
1364                                         {
1365                                                 mySBuilder.Append ("&\"");
1366                                                 inQuotes = true;
1367                                         }
1368                                         mySBuilder.Append (value[MyCounter]);
1369                                         mySBuilder.Append (value[MyCounter]);
1370                                 }
1371                                 else if (value[MyCounter] >= 32) //standard ansi/unicode characters
1372                                 {
1373                                         if (!inQuotes)
1374                                         {
1375                                                 mySBuilder.Append ("&\"");
1376                                                 inQuotes = true;
1377                                         }
1378                                         mySBuilder.Append (value[MyCounter]);
1379                                 }
1380                                 else //special chars, e.g. line break
1381                                 {
1382                                         if (inQuotes)
1383                                         { 
1384                                                 mySBuilder.Append ("\"");
1385                                                 inQuotes = false;
1386                                         }
1387                                         mySBuilder.Append ("&Microsoft.VisualBasic.ChrW(");
1388                                         mySBuilder.Append ((int)value[MyCounter]); 
1389                                         mySBuilder.Append (")");
1390                                 }                       
1391                         }
1392                         if (inQuotes)
1393                                 mySBuilder.Append ("\"");
1394                         return mySBuilder.ToString();
1395                 }
1396
1397                 private void GenerateMemberReferenceExpression (CodeExpression targetObject, string memberName)
1398                 {
1399                         GenerateExpression (targetObject);
1400                         Output.Write ('.');
1401                         Output.Write (memberName);
1402                 }
1403                         
1404                 /* 
1405                  * ICodeGenerator
1406                  */
1407
1408                 protected override string CreateEscapedIdentifier (string value)
1409                 {
1410                         for (int x = 0; x < Keywords.Length; x++)
1411                                 if (value.ToLower().Equals (Keywords[x].ToLower()))
1412                                         return "[" + value + "]";
1413                         return value;
1414                 }
1415
1416                 protected override string CreateValidIdentifier (string value)
1417                 {
1418                         for (int x = 0; x < Keywords.Length; x++)
1419                                 if (value.ToLower().Equals (Keywords[x].ToLower()))
1420                                         return "_" + value;
1421                         return value;
1422                 }
1423
1424                 protected override string GetTypeOutput (CodeTypeReference type)
1425                 {
1426                         string output;
1427                         CodeTypeReference arrayType;
1428
1429                         arrayType = type.ArrayElementType;
1430                         if (arrayType != null)
1431                                 output = GetTypeOutput (arrayType);
1432                         else { 
1433                                 switch (type.BaseType) {
1434                                 case "System.DateTime":
1435                                         output = "Date";
1436                                         break;
1437                                 case "System.Decimal":
1438                                         output = "Decimal";
1439                                         break;
1440                                 case "System.Double":
1441                                         output = "Double";
1442                                         break;
1443                                 case "System.Single":
1444                                         output = "Single";
1445                                         break;
1446                                 case "System.Byte":
1447                                         output = "Byte";
1448                                         break;
1449                                 case "System.Int32":
1450                                         output = "Integer";
1451                                         break;
1452                                 case "System.Int64":
1453                                         output = "Long";
1454                                         break;
1455                                 case "System.Int16":
1456                                         output = "Short";
1457                                         break;
1458                                 case "System.Boolean":
1459                                         output = "Boolean";
1460                                         break;
1461                                 case "System.Char":
1462                                         output = "Char";
1463                                         break;
1464                                 case "System.String":
1465                                         output = "String";
1466                                         break;
1467                                 case "System.Object":
1468                                         output = "Object";
1469                                         break;
1470 #if NET_2_0
1471                                 case "System.SByte":
1472                                         output = "SByte";
1473                                         break;
1474                                 case "System.UInt16":
1475                                         output = "UShort";
1476                                         break;
1477                                 case "System.UInt32":
1478                                         output = "UInteger";
1479                                         break;
1480                                 case "System.UInt64":
1481                                         output = "ULong";
1482                                         break;
1483 #endif
1484                                 default:
1485                                         output = type.BaseType.Replace('+', '.');
1486                                         break;
1487                                 }
1488                         }
1489
1490                         int rank = type.ArrayRank;
1491                         if (rank > 0) {
1492                                 output += "(";
1493                                 for (--rank; rank > 0; --rank)
1494                                         output += ",";
1495                                 output += ")";
1496                         }
1497
1498                         return output;
1499                 }
1500
1501                 protected override bool IsValidIdentifier (string identifier)
1502                 {
1503                         for (int x = 0; x < Keywords.Length; x++)
1504                                 if (identifier.ToLower().Equals (Keywords[x].ToLower()))
1505                                         return false;
1506                         return true;
1507                 }
1508
1509                 protected override bool Supports (GeneratorSupport supports)
1510                 {
1511                         return true;
1512                 }
1513
1514                 private bool IsOverloaded (CodeMemberProperty property, CodeTypeDeclaration type)
1515                 {
1516                         if ((property.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
1517                                 return true;
1518                         }
1519
1520                         foreach (CodeTypeMember member in type.Members) {
1521                                 CodeMemberProperty p = member as CodeMemberProperty;
1522                                 if (p == null) {
1523                                         // member is not a property
1524                                         continue;
1525                                 }
1526
1527                                 if (p != property && p.Name == property.Name && p.PrivateImplementationType == null) {
1528                                         return true;
1529                                 }
1530                         }
1531                         return false;
1532                 }
1533
1534                 private bool IsOverloaded (CodeMemberMethod method, CodeTypeDeclaration type)
1535                 {
1536                         if ((method.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
1537                                 return true;
1538                         }
1539
1540                         foreach (CodeTypeMember member in type.Members) {
1541                                 CodeMemberMethod m = member as CodeMemberMethod;
1542                                 if (m == null) {
1543                                         // member is not a method
1544                                         continue;
1545                                 }
1546
1547                                 if (!(m is CodeTypeConstructor) && !(m is CodeConstructor) && m != method && m.Name == method.Name && m.PrivateImplementationType == null) {
1548                                         return true;
1549                                 }
1550                         }
1551                         return false;
1552                 }
1553
1554                 private string GetEventName (CodeMemberEvent evt)
1555                 {
1556 #if NET_2_0
1557                         if (evt.PrivateImplementationType == null) {
1558                                 return evt.Name;
1559                         }
1560
1561                         string baseType = evt.PrivateImplementationType.BaseType.Replace ('.', '_');
1562                         return baseType + "_" + evt.Name;
1563 #else
1564                         return evt.Name;
1565 #endif
1566                 }
1567
1568                 private string GetMethodName (CodeMemberMethod method)
1569                 {
1570                         if (method.PrivateImplementationType == null) {
1571                                 return method.Name;
1572                         }
1573
1574                         string baseType = method.PrivateImplementationType.BaseType.Replace ('.', '_');
1575                         return baseType + "_" + method.Name;
1576                 }
1577
1578                 private string GetPropertyName (CodeMemberProperty property)
1579                 {
1580                         if (property.PrivateImplementationType == null) {
1581                                 return property.Name;
1582                         }
1583
1584                         string baseType = property.PrivateImplementationType.BaseType.Replace ('.', '_');
1585                         return baseType + "_" + property.Name;
1586                 }
1587
1588                 private enum LineHandling
1589                 {
1590                         InLine,
1591                         ContinueLine,
1592                         NewLine
1593                 }
1594         }
1595 }