X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem%2FMicrosoft.VisualBasic%2FVBCodeGenerator.cs;h=eed54ad4b63bac3dda99d1e3fc5f22b9d879ccb6;hb=b03e60628764d66654147bfc7a7e1a3242559888;hp=0d40e472c7425e1818d6637a4b9713c4939d5b6b;hpb=2007998771b1c4d9e762943676d7959daaf74385;p=mono.git diff --git a/mcs/class/System/Microsoft.VisualBasic/VBCodeGenerator.cs b/mcs/class/System/Microsoft.VisualBasic/VBCodeGenerator.cs index 0d40e472c74..eed54ad4b63 100644 --- a/mcs/class/System/Microsoft.VisualBasic/VBCodeGenerator.cs +++ b/mcs/class/System/Microsoft.VisualBasic/VBCodeGenerator.cs @@ -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", @@ -91,6 +94,8 @@ namespace Microsoft.VisualBasic "Xor" }; + private CodeAttributeDeclarationCollection assemblyCustomAttributes; + public VBCodeGenerator() { } @@ -107,6 +112,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 +156,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 +178,7 @@ namespace Microsoft.VisualBasic else output.Write (expression.Size); - output.Write (')'); + output.Write (") - 1) {}"); } } @@ -165,14 +198,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 +220,8 @@ namespace Microsoft.VisualBasic GenerateComment (new CodeComment (" ")); 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 @@ -199,13 +232,17 @@ namespace Microsoft.VisualBasic protected override void GenerateCompileUnit (CodeCompileUnit compileUnit) { - GenerateCompileUnitStart (compileUnit); + assemblyCustomAttributes = compileUnit.AssemblyCustomAttributes; - OutputAttributes (compileUnit.AssemblyCustomAttributes, - "Assembly: ", LineHandling.NewLine); + GenerateCompileUnitStart (compileUnit); GenerateNamespaces (compileUnit); + // In the case there are no namespaces, and hence no Import statements, now + // is the time to emit assembly level attributes. + if (assemblyCustomAttributes != null) + GenerateAssemblyAttributes (); + GenerateCompileUnitEnd (compileUnit); } @@ -230,17 +267,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 +295,7 @@ namespace Microsoft.VisualBasic TextWriter output = Output; GenerateExpression (expression.TargetObject); - output.Write (".Item("); + output.Write ("("); OutputExpressionList (expression.Indices); output.Write (')'); } @@ -281,9 +318,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 +330,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 +381,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 +399,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 +412,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 +475,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 +548,6 @@ namespace Microsoft.VisualBasic CodeStatementCollection finallies = statement.FinallyStatements; if (finallies.Count > 0) { - output.WriteLine ("Finally"); ++Indent; GenerateStatements (finallies); @@ -503,7 +558,7 @@ namespace Microsoft.VisualBasic } protected override void GenerateAssignStatement (CodeAssignStatement statement) - { + { TextWriter output = Output; GenerateExpression (statement.Left); output.Write (" = "); @@ -516,7 +571,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 +586,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 +634,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 +645,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 +660,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 +673,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 +681,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 +721,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 +734,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 +765,7 @@ namespace Microsoft.VisualBasic output.Write ("Function "); output.Write (GetMethodName(method)); + OutputTypeParameters (method.TypeParameters); output.Write ('('); OutputParameters (method.Parameters); output.Write (')'); @@ -745,9 +802,8 @@ namespace Microsoft.VisualBasic protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration) { - if (IsCurrentDelegate || IsCurrentEnum) { + if (IsCurrentDelegate || IsCurrentEnum) return; - } TextWriter output = Output; @@ -782,19 +838,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 +861,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 +886,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 +909,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 +921,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 +934,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 +958,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 +970,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) { @@ -987,7 +1033,14 @@ namespace Microsoft.VisualBasic protected override void GenerateNamespace(CodeNamespace ns) { GenerateNamespaceImports (ns); + + // Assembly level attributes can't be emitted before the Import statements + // in the file. + if (assemblyCustomAttributes != null) + GenerateAssemblyAttributes (); + Output.WriteLine (); + GenerateCommentStatements (ns.Comments); GenerateNamespaceStart (ns); GenerateTypes (ns); @@ -1120,12 +1173,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 +1206,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 +1338,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 +1381,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 +1444,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 +1456,7 @@ namespace Microsoft.VisualBasic } else if (value[MyCounter] >= 32) //standard ansi/unicode characters { - if (!inQuotes) - { + if (!inQuotes) { mySBuilder.Append ("&\""); inQuotes = true; } @@ -1376,15 +1464,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 +1513,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 +1551,6 @@ namespace Microsoft.VisualBasic case "System.Object": output = "Object"; break; -#if NET_2_0 case "System.SByte": output = "SByte"; break; @@ -1477,9 +1563,9 @@ namespace Microsoft.VisualBasic case "System.UInt64": output = "ULong"; break; -#endif default: output = type.BaseType.Replace('+', '.'); + output = CreateEscapedIdentifier (output); break; } } @@ -1521,9 +1607,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 +1626,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 +1652,25 @@ 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 void GenerateAssemblyAttributes () + { + OutputAttributes (assemblyCustomAttributes, + "Assembly: ", LineHandling.NewLine); + assemblyCustomAttributes = null; + } + private enum LineHandling { InLine,