[System] Removal of the NET_2_0 in the source code
[mono.git] / mcs / class / System / Microsoft.VisualBasic / VBCodeGenerator.cs
index 0d40e472c7425e1818d6637a4b9713c4939d5b6b..db1e087e442ee16d0f4ba66ffb3203bfa4a3e0cc 100644 (file)
@@ -5,6 +5,8 @@
 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
 //   (partially based on CSharpCodeGenerator)
 //   Jochen Wezel (jwezel@compumaster.de)
+//   Frederik Carlier (frederik.carlier@carlier-online.be)
+//   Rolf Bjarne Kvinge (RKvinge@novell.com)
 //
 // (C) 2003 Andreas Nahr
 // (C) 2003 Jochen Wezel (http://www.compumaster.de)
@@ -16,6 +18,7 @@
 // 2003-11-12 JW: some corrections to allow correct compilation
 // 2003-11-28 JW: implementing code differences into current build of this file
 // 2003-12-10 JW: added "String." for the ChrW method because mbas doesn't support it without the String currently / TODO: remove it ASAP!
+// 2007-04-13 FC: Added support for the IdentityInequality operator when comparing against Nothing
 
 //
 // Permission is hereby granted, free of charge, to any person obtaining
@@ -53,7 +56,7 @@ namespace Microsoft.VisualBasic
 {
        internal class VBCodeGenerator : CodeGenerator
        {
-               private string[] Keywords = new string[] {
+               private string [] Keywords = new string [] {
                        "AddHandler", "AddressOf", "Alias", "And",
                        "AndAlso", "Ansi", "As", "Assembly",
                        "Auto", "Boolean", "ByRef", "Byte", 
@@ -68,7 +71,7 @@ namespace Microsoft.VisualBasic
                        "End", "Enum", "Erase", "Error", 
                        "Event", "Exit", "False", "Finally", 
                        "For", "Friend", "Function", "Get", 
-                       "GetType", "GoSub", "GoTo", "Handles", 
+                       "GetType", "Global", "GoSub", "GoTo", "Handles", 
                        "If", "Implements", "Imports", "In", 
                        "Inherits", "Integer", "Interface", "Is", 
                        "Let", "Lib", "Like", "Long", 
@@ -78,7 +81,7 @@ namespace Microsoft.VisualBasic
                        "Nothing", "NotInheritable", "NotOverridable", "Object", 
                        "On", "Option", "Optional", "Or", 
                        "OrElse", "Overloads", "Overridable", "Overrides", 
-                       "ParamArray", "Preserve", "Private", "Property", 
+                       "ParamArray", "Partial", "Preserve", "Private", "Property", 
                        "Protected", "Public", "RaiseEvent", "ReadOnly", 
                        "ReDim", "REM", "RemoveHandler", "Resume", 
                        "Return", "Select", "Set", "Shadows", 
@@ -90,7 +93,6 @@ namespace Microsoft.VisualBasic
                        "While", "With", "WithEvents", "WriteOnly", 
                        "Xor" 
                };
-
                public VBCodeGenerator()
                {
                }
@@ -107,6 +109,37 @@ namespace Microsoft.VisualBasic
                        Output.WriteLine (" _");
                }
 
+               protected override void GenerateBinaryOperatorExpression (CodeBinaryOperatorExpression e)
+               {
+                       // We need to special case for comparisons against null;
+                       // in Visual Basic the "Not (Expr) Is Nothing" construct is used
+                       
+                       bool null_comparison = false;
+                       bool reverse = false;
+                       if (e.Operator == CodeBinaryOperatorType.IdentityInequality) {
+                               CodePrimitiveExpression nothing;
+                               nothing = e.Left as CodePrimitiveExpression;
+                               if (nothing == null) {
+                                       nothing = e.Right as CodePrimitiveExpression;
+                               } else {
+                                       reverse = true;
+                               }
+                               null_comparison = nothing != null && nothing.Value == null;
+                       }
+
+                       if (null_comparison) {
+                               TextWriter output = Output;
+
+                               output.Write ("(Not (");
+                               GenerateExpression (reverse ? e.Right : e.Left);
+                               output.Write (") Is ");
+                               GenerateExpression (reverse ? e.Left : e.Right);
+                               output.Write (')');
+                       } else {
+                               base.GenerateBinaryOperatorExpression (e);
+                       }
+               }
+
                protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
                {
                        TextWriter output = Output;
@@ -120,24 +153,21 @@ namespace Microsoft.VisualBasic
 
                                OutputType (createType);
                                
-                               output.WriteLine (" {");
+                               output.Write ("() {");
                                ++Indent;
-                               OutputExpressionList (initializers, true);
+                               OutputExpressionList (initializers);
                                --Indent;
                                output.Write ("}");
-
-                       } 
-                       else {
+                       } else {
                                CodeTypeReference arrayType = createType.ArrayElementType;
-                               while (arrayType != null) 
-                               {
+                               while (arrayType != null) {
                                        createType = arrayType;
                                        arrayType = arrayType.ArrayElementType;
                                }
 
                                OutputType (createType);
 
-                               output.Write ('(');
+                               output.Write ("((");
 
                                CodeExpression size = expression.SizeExpression;
                                if (size != null)
@@ -145,7 +175,7 @@ namespace Microsoft.VisualBasic
                                else
                                        output.Write (expression.Size);
 
-                               output.Write (')');
+                               output.Write (") - 1) {}");
                        }
                }
 
@@ -165,14 +195,14 @@ namespace Microsoft.VisualBasic
                        output.Write (")");
                }
 
-               private bool AsBool(object datavalue)
+               private bool AsBool (object datavalue)
                {
-                       return datavalue != null && datavalue is bool && (bool)datavalue;
+                       return datavalue != null && datavalue is bool && (bool) datavalue;
                }
                
-               private string OnOff(bool datavalue)
+               private string OnOff (bool datavalue)
                {
-                       return datavalue?"On":"Off";
+                       return datavalue ? "On" : "Off";
                }
 
                protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
@@ -187,8 +217,8 @@ namespace Microsoft.VisualBasic
                        GenerateComment (new CodeComment (" </autogenerated>"));
                        GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
                        Output.WriteLine ();
-                       if (AsBool(compileUnit.UserData["AllowLateBound"])) {
-                               Output.WriteLine("Option Explicit {0}",OnOff(AsBool(compileUnit.UserData["RequireVariableDeclaration"])));
+                       if (AsBool (compileUnit.UserData ["AllowLateBound"])) {
+                               Output.WriteLine("Option Explicit {0}", OnOff (AsBool (compileUnit.UserData ["RequireVariableDeclaration"])));
                                Output.WriteLine("Option Strict Off");
                        } else {
                                Output.WriteLine("Option Explicit On"); // Strict On implies Explicit On
@@ -230,17 +260,17 @@ namespace Microsoft.VisualBasic
                                GenerateExpression (targetObject);
                                Output.Write ('.');
                        }
-                       Output.Write (expression.FieldName);
+                       Output.Write (CreateEscapedIdentifier (expression.FieldName));
                }
                
                protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
                {
-                       Output.Write (expression.ParameterName);
+                       Output.Write (CreateEscapedIdentifier (expression.ParameterName));
                }
 
                protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
                {
-                       Output.Write (expression.VariableName);
+                       Output.Write (CreateEscapedIdentifier (expression.VariableName));
                }
                
                protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
@@ -258,7 +288,7 @@ namespace Microsoft.VisualBasic
                        TextWriter output = Output;
 
                        GenerateExpression (expression.TargetObject);
-                       output.Write (".Item(");
+                       output.Write ("(");
                        OutputExpressionList (expression.Indices);
                        output.Write (')');
                }
@@ -281,9 +311,11 @@ namespace Microsoft.VisualBasic
 
                protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
                {
-                       GenerateExpression (expression.TargetObject);
-                       Output.Write ('.');
-                       Output.Write (expression.MethodName);
+                       if (expression.TargetObject != null) {
+                               GenerateExpression (expression.TargetObject);
+                               Output.Write ('.');
+                       }
+                       Output.Write (CreateEscapedIdentifier (expression.MethodName));
                }
 
                protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
@@ -291,17 +323,34 @@ namespace Microsoft.VisualBasic
                        if (expression.TargetObject != null) {
                                GenerateExpression (expression.TargetObject);
                                Output.Write ('.');
+                               if (expression.TargetObject is CodeThisReferenceExpression) {
+                                       // We're actually creating a reference to a compiler-generated field here...
+                                       Output.Write (expression.EventName + "Event");
+                               } else {
+                                       Output.Write (CreateEscapedIdentifier (expression.EventName));
+                               }
+                       } else {
+                               Output.Write (CreateEscapedIdentifier (expression.EventName + "Event"));
                        }
-                       Output.Write (CreateEscapedIdentifier(expression.EventName));
                }
 
                protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
                {
-                       Output.Write ("RaiseEvent ");
-                       GenerateExpression (expression.TargetObject);
+                       CodeEventReferenceExpression ev = expression.TargetObject as CodeEventReferenceExpression;
+                       
+                       if (ev != null) {
+                               Output.Write ("RaiseEvent ");
+                               if (ev.TargetObject != null && !(ev.TargetObject is CodeThisReferenceExpression)) {
+                                       GenerateExpression (ev.TargetObject);
+                                       Output.Write (".");
+                               }
+                               Output.Write (ev.EventName);
+                       } else if (expression.TargetObject != null) {
+                               GenerateExpression (expression.TargetObject);
+                       }
                        Output.Write ('(');
                        OutputExpressionList (expression.Parameters);
-                       Output.WriteLine (')');
+                       Output.Write (')');
                }
                
                protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
@@ -325,7 +374,6 @@ namespace Microsoft.VisualBasic
                        if (e.Value is char) {
                                char c = (char) e.Value;
                                int ch = (int) c;
-#if NET_2_0
                                Output.Write("Global.Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
                        } else if (e.Value is ushort) {
                                ushort uc = (ushort) e.Value;
@@ -344,9 +392,6 @@ namespace Microsoft.VisualBasic
                                Output.Write ("CSByte(");
                                Output.Write (sb.ToString(CultureInfo.InvariantCulture));
                                Output.Write (')');
-#else
-                               Output.Write("Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
-#endif
                        } else {
                                base.GeneratePrimitiveExpression(e);
                        }
@@ -360,7 +405,11 @@ namespace Microsoft.VisualBasic
 
                protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
                {
-                       GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
+                       if (expression.TargetObject != null) {
+                               GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
+                       } else {
+                               Output.Write (CreateEscapedIdentifier (expression.PropertyName));
+                       }
                }
 
                protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
@@ -419,13 +468,13 @@ namespace Microsoft.VisualBasic
                        string text = comment.Text;
 
                        for (int i = 0; i < text.Length; i++) {
-                               output.Write (text[i]);
+                               output.Write (text [i]);
                                if (text[i] == '\r') {
-                                       if (i < (text.Length - 1) && text[i + 1] == '\n') {
+                                       if (i < (text.Length - 1) && text [i + 1] == '\n') {
                                                continue;
                                        }
                                        output.Write (commentChars);
-                               } else if (text[i] == '\n') {
+                               } else if (text [i] == '\n') {
                                        output.Write (commentChars);
                                }
                        }
@@ -492,7 +541,6 @@ namespace Microsoft.VisualBasic
 
                        CodeStatementCollection finallies = statement.FinallyStatements;
                        if (finallies.Count > 0) {
-
                                output.WriteLine ("Finally");
                                ++Indent;
                                GenerateStatements (finallies);
@@ -503,7 +551,7 @@ namespace Microsoft.VisualBasic
                }
 
                protected override void GenerateAssignStatement (CodeAssignStatement statement)
-               {                       
+               {
                        TextWriter output = Output;
                        GenerateExpression (statement.Left);
                        output.Write (" = ");
@@ -516,7 +564,11 @@ namespace Microsoft.VisualBasic
                        TextWriter output = Output;
 
                        Output.Write ("AddHandler ");
-                       GenerateEventReferenceExpression (statement.Event);
+                       if (statement.Event.TargetObject != null) {
+                               GenerateEventReferenceExpression (statement.Event);
+                       } else {
+                               Output.Write (CreateEscapedIdentifier (statement.Event.EventName));
+                       }
                        Output.Write ( ", ");
                        GenerateExpression (statement.Listener);
                        output.WriteLine ();
@@ -527,7 +579,11 @@ namespace Microsoft.VisualBasic
                        TextWriter output = Output;
 
                        Output.Write ("RemoveHandler ");
-                       GenerateEventReferenceExpression (statement.Event);
+                       if (statement.Event.TargetObject != null) {
+                               GenerateEventReferenceExpression (statement.Event);
+                       } else {
+                               Output.Write (CreateEscapedIdentifier (statement.Event.EventName));
+                       }
                        Output.Write ( ", ");
                        GenerateExpression (statement.Listener);
                        output.WriteLine ();
@@ -571,8 +627,7 @@ namespace Microsoft.VisualBasic
                        OutputTypeNamePair (statement.Type, statement.Name);
 
                        CodeExpression initExpression = statement.InitExpression;
-                       if (initExpression != null) 
-                       {
+                       if (initExpression != null) {
                                output.Write (" = ");
                                GenerateExpression (initExpression);
                        }
@@ -583,11 +638,12 @@ namespace Microsoft.VisualBasic
                protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
                {
                        Output.WriteLine ();
-                       Output.Write ("#ExternalSource(");
+                       Output.Write ("#ExternalSource(\"");
                        Output.Write (linePragma.FileName);
-                       Output.Write ("");
+                       Output.Write ("\",");
                        Output.Write (linePragma.LineNumber);
                        Output.WriteLine (")");
+                       Output.WriteLine ("");
                }
 
                protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
@@ -597,13 +653,12 @@ namespace Microsoft.VisualBasic
 
                protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
                {
-                       if (IsCurrentDelegate || IsCurrentEnum) {
+                       if (IsCurrentDelegate || IsCurrentEnum)
                                return;
-                       }
 
                        TextWriter output = Output;
 
-                       OutputAttributes (eventRef.CustomAttributes, null, 
+                       OutputAttributes (eventRef.CustomAttributes, null,
                                LineHandling.ContinueLine);
 
                        OutputMemberAccessModifier (eventRef.Attributes);
@@ -611,7 +666,6 @@ namespace Microsoft.VisualBasic
                        output.Write ("Event ");
                        OutputTypeNamePair (eventRef.Type, GetEventName(eventRef));
 
-#if NET_2_0
                        if (eventRef.ImplementationTypes.Count > 0) {
                                OutputImplementationTypes (eventRef.ImplementationTypes, eventRef.Name);
                        } else if (eventRef.PrivateImplementationType != null) {
@@ -620,16 +674,14 @@ namespace Microsoft.VisualBasic
                                output.Write ('.');
                                output.Write (eventRef.Name);
                        }
-#endif
 
                        output.WriteLine ();
                }
 
                protected override void GenerateField (CodeMemberField field)
                {
-                       if (IsCurrentDelegate || IsCurrentInterface) {
+                       if (IsCurrentDelegate || IsCurrentInterface)
                                return;
-                       }
 
                        TextWriter output = Output;
 
@@ -662,10 +714,8 @@ namespace Microsoft.VisualBasic
                
                protected override void GenerateEntryPointMethod (CodeEntryPointMethod method, CodeTypeDeclaration declaration)
                {
-#if NET_2_0
-                       OutputAttributes (method.CustomAttributes, null, 
+                       OutputAttributes (method.CustomAttributes, null,
                                LineHandling.ContinueLine);
-#endif
 
                        Output.WriteLine ("Public Shared Sub Main()");
                        Indent++;
@@ -677,9 +727,8 @@ namespace Microsoft.VisualBasic
                [MonoTODO ("partially implemented")]
                protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)
                {
-                       if (IsCurrentDelegate || IsCurrentEnum) {
+                       if (IsCurrentDelegate || IsCurrentEnum)
                                return;
-                       }
 
                        bool isSub = method.ReturnType.BaseType == typeof(void).FullName;
 
@@ -709,6 +758,7 @@ namespace Microsoft.VisualBasic
                                output.Write ("Function ");
 
                        output.Write (GetMethodName(method));
+                       OutputTypeParameters (method.TypeParameters);
                        output.Write ('(');
                        OutputParameters (method.Parameters);
                        output.Write (')');
@@ -745,9 +795,8 @@ namespace Microsoft.VisualBasic
 
                protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)
                {
-                       if (IsCurrentDelegate || IsCurrentEnum) {
+                       if (IsCurrentDelegate || IsCurrentEnum)
                                return;
-                       }
 
                        TextWriter output = Output;
 
@@ -782,19 +831,11 @@ namespace Microsoft.VisualBasic
 
                        output.Write ("Property ");
                        Output.Write (GetPropertyName (property));
-#if NET_2_0
                        // in .NET 2.0, always output parantheses (whether or not there 
                        // are any parameters to output
                        Output.Write ('(');
                        OutputParameters (property.Parameters);
                        Output.Write (')');
-#else
-                       if (property.Parameters.Count > 0) {
-                               Output.Write ('(');
-                               OutputParameters (property.Parameters);
-                               Output.Write (')');
-                       }
-#endif
                        Output.Write (" As ");
                        Output.Write (GetTypeOutput(property.Type));
 
@@ -813,18 +854,22 @@ namespace Microsoft.VisualBasic
                                ++Indent;
                                if (property.HasGet) {
                                        output.WriteLine ("Get");
-                                       ++Indent;
-                                       GenerateStatements (property.GetStatements);
-                                       --Indent;
-                                       output.WriteLine ("End Get");
+                                       if (!IsAbstract (property.Attributes)) {
+                                               ++Indent;
+                                               GenerateStatements (property.GetStatements);
+                                               --Indent;
+                                               output.WriteLine ("End Get");
+                                       }
                                }
 
                                if (property.HasSet) {
                                        output.WriteLine ("Set");
-                                       ++Indent;
-                                       GenerateStatements (property.SetStatements);
-                                       --Indent;
-                                       output.WriteLine ("End Set");
+                                       if (!IsAbstract (property.Attributes)) {
+                                               ++Indent;
+                                               GenerateStatements (property.SetStatements);
+                                               --Indent;
+                                               output.WriteLine ("End Set");
+                                       }
                                }
 
                                --Indent;
@@ -834,9 +879,8 @@ namespace Microsoft.VisualBasic
 
                protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
                {
-                       if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
+                       if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface)
                                return;
-                       }
 
                        OutputAttributes (constructor.CustomAttributes, null,
                                LineHandling.ContinueLine);
@@ -858,13 +902,9 @@ namespace Microsoft.VisualBasic
                                        Output.Write ("MyBase.New(");
                                        OutputExpressionList (ctorArgs);
                                        Output.WriteLine (")");
-#if NET_2_0
                                } else if (IsCurrentClass) {
-#else
-                               } else {
-#endif
                                        // call default base ctor
-                                       Output.WriteLine ("MyBase.New()");
+                                       Output.WriteLine ("MyBase.New");
                                }
                        }
                        GenerateStatements (constructor.Statements);
@@ -874,14 +914,11 @@ namespace Microsoft.VisualBasic
                
                protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
                {
-                       if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
+                       if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface)
                                return;
-                       }
 
-#if NET_2_0
                        OutputAttributes (constructor.CustomAttributes, null,
                                LineHandling.ContinueLine);
-#endif
 
                        Output.WriteLine ("Shared Sub New()");
                        Indent++;
@@ -890,7 +927,7 @@ namespace Microsoft.VisualBasic
                        Output.WriteLine ("End Sub");
                }
 
-               [MonoTODO ("not implemented")]
+               [MonoTODO ("partially implemented")]
                protected override void GenerateTypeStart (CodeTypeDeclaration declaration)
                {
                        TextWriter output = Output;
@@ -914,8 +951,10 @@ namespace Microsoft.VisualBasic
                                        output.Write ("Delegate Function ");
                                }
 
-                               output.Write (delegateDecl.Name);
+                               output.Write (CreateEscapedIdentifier (delegateDecl.Name));
+                               OutputTypeParameters (delegateDecl.TypeParameters);
                                output.Write ("(");
+                               OutputParameters (delegateDecl.Parameters);
                                Output.Write (")");
                                if (!isSub) {
                                        Output.Write (" As ");
@@ -924,8 +963,8 @@ namespace Microsoft.VisualBasic
                                Output.WriteLine ("");
                        } else {
                                OutputTypeAttributes (declaration);
-
-                               output.Write (declaration.Name);
+                               output.Write (CreateEscapedIdentifier (declaration.Name));
+                               OutputTypeParameters (declaration.TypeParameters);
 
                                if (IsCurrentEnum) {
                                        if (declaration.BaseTypes.Count > 0) {
@@ -1120,12 +1159,12 @@ namespace Microsoft.VisualBasic
                protected override void OutputFieldScopeModifier (MemberAttributes attributes)
                {
                        switch (attributes & MemberAttributes.ScopeMask) {
-                               case MemberAttributes.Static:
-                                       Output.Write ("Shared ");
-                                       break;
-                               case MemberAttributes.Const:
-                                       Output.Write ("Const ");
-                                       break;
+                       case MemberAttributes.Static:
+                               Output.Write ("Shared ");
+                               break;
+                       case MemberAttributes.Const:
+                               Output.Write ("Const ");
+                               break;
                        }
                }
 
@@ -1153,79 +1192,73 @@ namespace Microsoft.VisualBasic
                protected override void OutputMemberAccessModifier (MemberAttributes attributes)
                {
                        switch (attributes & MemberAttributes.AccessMask) {
-                               case MemberAttributes.Assembly:
-                               case MemberAttributes.FamilyAndAssembly:
-                                       Output.Write ("Friend "); 
-                                       break;
-                               case MemberAttributes.Family:
-                                       Output.Write ("Protected ");
-                                       break;
-                               case MemberAttributes.FamilyOrAssembly:
-                                       Output.Write ("Protected Friend ");
-                                       break;
-                               case MemberAttributes.Private:
-                                       Output.Write ("Private ");
-                                       break;
-                               case MemberAttributes.Public:
-                                       Output.Write ("Public ");
-                                       break;
+                       case MemberAttributes.Assembly:
+                       case MemberAttributes.FamilyAndAssembly:
+                               Output.Write ("Friend "); 
+                               break;
+                       case MemberAttributes.Family:
+                               Output.Write ("Protected ");
+                               break;
+                       case MemberAttributes.FamilyOrAssembly:
+                               Output.Write ("Protected Friend ");
+                               break;
+                       case MemberAttributes.Private:
+                               Output.Write ("Private ");
+                               break;
+                       case MemberAttributes.Public:
+                               Output.Write ("Public ");
+                               break;
                        }
                }
 
                private void OutputVTableModifier (MemberAttributes attributes)
                {
-                       if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New) {
+                       if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
                                Output.Write ("Shadows ");
-                       }
                }
 
                protected override void OutputMemberScopeModifier (MemberAttributes attributes)
                {
                        switch (attributes & MemberAttributes.ScopeMask) {
-                               case MemberAttributes.Abstract:
-                                       Output.Write ("MustOverride ");
-                                       break;
-                               case MemberAttributes.Final:
-                                       // do nothing
-                                       break;
-                               case MemberAttributes.Static:
-                                       Output.Write ("Shared ");
-                                       break;
-                               case MemberAttributes.Override:
-                                       Output.Write ("Overrides ");
-                                       break;
-                               case MemberAttributes.Overloaded:
-                                       // based on http://gendotnet.com/Code%20Gen%20Articles/codedom.htm
-                                       Output.Write ("Overloads ");
+                       case MemberAttributes.Abstract:
+                               Output.Write ("MustOverride ");
+                               break;
+                       case MemberAttributes.Final:
+                               // do nothing
+                               break;
+                       case MemberAttributes.Static:
+                               Output.Write ("Shared ");
+                               break;
+                       case MemberAttributes.Override:
+                               Output.Write ("Overrides ");
+                               break;
+                       case MemberAttributes.Overloaded:
+                               // based on http://gendotnet.com/Code%20Gen%20Articles/codedom.htm
+                               Output.Write ("Overloads ");
 
-                                       MemberAttributes access_ovl = attributes & MemberAttributes.AccessMask;
-                                       if (access_ovl == MemberAttributes.Public || access_ovl == MemberAttributes.Family) {
-                                               Output.Write ("Overridable ");
-                                       }
-                                       break;
-                               default:
-                                       //
-                                       // FUNNY! if the scope value is
-                                       // rubbish (0 or >Const), and access
-                                       // is public, protected make it
-                                       // "virtual".
-                                       //
-                                       // i'm not sure whether this is 100%
-                                       // correct, but it seems to be MS
-                                       // behavior.
-                                       //
-                                       // On MS.NET 2.0, internal properties
-                                       // are also marked "virtual".
-                                       //
-                                       MemberAttributes access = attributes & MemberAttributes.AccessMask;
-                                       if (access == MemberAttributes.Public || 
-#if NET_2_0
-                                               access == MemberAttributes.Family || access == MemberAttributes.Assembly)
-#else
-                                               access == MemberAttributes.Family)
-#endif
-                                               Output.Write ("Overridable ");
-                                       break;
+                               MemberAttributes access_ovl = attributes & MemberAttributes.AccessMask;
+                               if (access_ovl == MemberAttributes.Public || access_ovl == MemberAttributes.Family)
+                                       Output.Write ("Overridable ");
+                               break;
+                       default:
+                               //
+                               // FUNNY! if the scope value is
+                               // rubbish (0 or >Const), and access
+                               // is public, protected make it
+                               // "virtual".
+                               //
+                               // i'm not sure whether this is 100%
+                               // correct, but it seems to be MS
+                               // behavior.
+                               //
+                               // On MS.NET 2.0, internal properties
+                               // are also marked "virtual".
+                               //
+                               MemberAttributes access = attributes & MemberAttributes.AccessMask;
+                               if (access == MemberAttributes.Public || 
+                                       access == MemberAttributes.Family || access == MemberAttributes.Assembly)
+                                       Output.Write ("Overridable ");
+                               break;
                        }
                }
 
@@ -1291,27 +1324,28 @@ namespace Microsoft.VisualBasic
                        TextWriter output = Output;
                        TypeAttributes attributes = declaration.TypeAttributes;
 
+                       if (declaration.IsPartial)
+                               output.Write ("Partial ");
+                       
                        switch (attributes & TypeAttributes.VisibilityMask) {
-                               case TypeAttributes.Public:
-                               case TypeAttributes.NestedPublic:
-                                       output.Write ("Public ");
-                                       break;
-                               case TypeAttributes.NestedPrivate:
-                                       output.Write ("Private ");
-                                       break;
-#if NET_2_0
-                               case TypeAttributes.NotPublic:
-                               case TypeAttributes.NestedFamANDAssem:
-                               case TypeAttributes.NestedAssembly:
-                                       output.Write ("Friend ");
-                                       break; 
-                               case TypeAttributes.NestedFamily:
-                                       output.Write ("Protected ");
-                                       break;
-                               case TypeAttributes.NestedFamORAssem:
-                                       output.Write ("Protected Friend ");
-                                       break;
-#endif
+                       case TypeAttributes.Public:
+                       case TypeAttributes.NestedPublic:
+                               output.Write ("Public ");
+                               break;
+                       case TypeAttributes.NestedPrivate:
+                               output.Write ("Private ");
+                               break;
+                       case TypeAttributes.NotPublic:
+                       case TypeAttributes.NestedFamANDAssem:
+                       case TypeAttributes.NestedAssembly:
+                               output.Write ("Friend ");
+                               break; 
+                       case TypeAttributes.NestedFamily:
+                               output.Write ("Protected ");
+                               break;
+                       case TypeAttributes.NestedFamORAssem:
+                               output.Write ("Protected Friend ");
+                               break;
                        }
 
                        if (declaration.IsStruct) {
@@ -1333,13 +1367,56 @@ namespace Microsoft.VisualBasic
                        }
                }
 
+               void OutputTypeParameters (CodeTypeParameterCollection parameters)
+               {
+                       int count = parameters.Count;
+                       if (count == 0)
+                               return;
+
+                       Output.Write ("(Of ");
+                       for (int i = 0; i < count; ++i) {
+                               if (i > 0)
+                                       Output.Write (", ");
+                               CodeTypeParameter p = parameters [i];
+                               Output.Write (p.Name);
+                               OutputTypeParameterConstraints (p);
+                       }
+                       Output.Write (')');
+               }
+
+               void OutputTypeParameterConstraints (CodeTypeParameter parameter)
+               {
+                       int constraint_count = parameter.Constraints.Count +
+                               (parameter.HasConstructorConstraint ? 1 : 0);
+
+                       if (constraint_count == 0)
+                               return;
+
+                       Output.Write (" As ");
+
+                       if (constraint_count > 1)
+                               Output.Write (" {");
+
+                       for (int i = 0; i < parameter.Constraints.Count; i++) {
+                               if (i > 0)
+                                       Output.Write (", ");
+                               OutputType (parameter.Constraints [i]);
+                       }
+
+                       if (parameter.HasConstructorConstraint) {
+                               if (constraint_count > 1)
+                                       Output.Write (", ");
+                               Output.Write ("New");
+                       }
+
+                       if (constraint_count > 1)
+                               Output.Write ("}");
+               }
+
                protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)
                {
-#if NET_2_0
-                       if (name.Length == 0) {
+                       if (name.Length == 0)
                                name = "__exception";
-                       }
-#endif
                        Output.Write (CreateEscapedIdentifier(name) + " As " + GetTypeOutput (typeRef));
                }
 
@@ -1353,12 +1430,10 @@ namespace Microsoft.VisualBasic
                        StringBuilder mySBuilder = new StringBuilder(value.Length);
                        mySBuilder.Append ("\"");
                        bool inQuotes = true;
-                       for (int MyCounter = 0; MyCounter < value.Length; MyCounter++)
-                       {
+                       for (int MyCounter = 0; MyCounter < value.Length; MyCounter++) {
                                if (value[MyCounter] == 34) //quotation mark
                                {
-                                       if (!inQuotes)
-                                       {
+                                       if (!inQuotes) {
                                                mySBuilder.Append ("&\"");
                                                inQuotes = true;
                                        }
@@ -1367,8 +1442,7 @@ namespace Microsoft.VisualBasic
                                }
                                else if (value[MyCounter] >= 32) //standard ansi/unicode characters
                                {
-                                       if (!inQuotes)
-                                       {
+                                       if (!inQuotes) {
                                                mySBuilder.Append ("&\"");
                                                inQuotes = true;
                                        }
@@ -1376,15 +1450,14 @@ namespace Microsoft.VisualBasic
                                }
                                else //special chars, e.g. line break
                                {
-                                       if (inQuotes)
-                                       { 
+                                       if (inQuotes) {
                                                mySBuilder.Append ("\"");
                                                inQuotes = false;
                                        }
                                        mySBuilder.Append ("&Microsoft.VisualBasic.ChrW(");
                                        mySBuilder.Append ((int)value[MyCounter]); 
                                        mySBuilder.Append (")");
-                               }                       
+                               }
                        }
                        if (inQuotes)
                                mySBuilder.Append ("\"");
@@ -1426,7 +1499,7 @@ namespace Microsoft.VisualBasic
                        arrayType = type.ArrayElementType;
                        if (arrayType != null)
                                output = GetTypeOutput (arrayType);
-                       else { 
+                       else {
                                switch (type.BaseType) {
                                case "System.DateTime":
                                        output = "Date";
@@ -1464,7 +1537,6 @@ namespace Microsoft.VisualBasic
                                case "System.Object":
                                        output = "Object";
                                        break;
-#if NET_2_0
                                case "System.SByte":
                                        output = "SByte";
                                        break;
@@ -1477,9 +1549,9 @@ namespace Microsoft.VisualBasic
                                case "System.UInt64":
                                        output = "ULong";
                                        break;
-#endif
                                default:
                                        output = type.BaseType.Replace('+', '.');
+                                       output = CreateEscapedIdentifier (output);
                                        break;
                                }
                        }
@@ -1521,9 +1593,8 @@ namespace Microsoft.VisualBasic
                                        continue;
                                }
 
-                               if (p != property && p.Name == property.Name && p.PrivateImplementationType == null) {
+                               if (p != property && p.Name == property.Name && p.PrivateImplementationType == null)
                                        return true;
-                               }
                        }
                        return false;
                }
@@ -1541,32 +1612,25 @@ namespace Microsoft.VisualBasic
                                        continue;
                                }
 
-                               if (!(m is CodeTypeConstructor) && !(m is CodeConstructor) && m != method && m.Name == method.Name && m.PrivateImplementationType == null) {
+                               if (!(m is CodeTypeConstructor) && !(m is CodeConstructor) && m != method && m.Name == method.Name && m.PrivateImplementationType == null)
                                        return true;
-                               }
                        }
                        return false;
                }
 
                private string GetEventName (CodeMemberEvent evt)
                {
-#if NET_2_0
-                       if (evt.PrivateImplementationType == null) {
+                       if (evt.PrivateImplementationType == null)
                                return evt.Name;
-                       }
 
                        string baseType = evt.PrivateImplementationType.BaseType.Replace ('.', '_');
                        return baseType + "_" + evt.Name;
-#else
-                       return evt.Name;
-#endif
                }
 
                private string GetMethodName (CodeMemberMethod method)
                {
-                       if (method.PrivateImplementationType == null) {
+                       if (method.PrivateImplementationType == null)
                                return method.Name;
-                       }
 
                        string baseType = method.PrivateImplementationType.BaseType.Replace ('.', '_');
                        return baseType + "_" + method.Name;
@@ -1574,14 +1638,18 @@ namespace Microsoft.VisualBasic
 
                private string GetPropertyName (CodeMemberProperty property)
                {
-                       if (property.PrivateImplementationType == null) {
+                       if (property.PrivateImplementationType == null)
                                return property.Name;
-                       }
 
                        string baseType = property.PrivateImplementationType.BaseType.Replace ('.', '_');
                        return baseType + "_" + property.Name;
                }
 
+               static bool IsAbstract (MemberAttributes attributes)
+               {
+                       return (attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract;
+               }
+
                private enum LineHandling
                {
                        InLine,