-//\r
-// Microsoft.VisualBasic.VBCodeGenerator.cs\r
-//\r
-// Author:\r
-// Andreas Nahr (ClassDevelopment@A-SoftTech.com)\r
-// (partially based on CSharpCodeGenerator)\r
-// Jochen Wezel (jwezel@compumaster.de)\r
-//\r
-// (C) 2003 Andreas Nahr\r
-// (C) 2003 Jochen Wezel (http://www.compumaster.de)\r
-//\r
-// Modifications:\r
-// 2003-11-06 JW: some corrections regarding missing spaces in generated code (e. g. "Property ")\r
-// 2003-11-06 JW: QuoteSnippetString implemented\r
-// 2003-11-08 JW: automatically add Microsoft.VisualBasic\r
-// 2003-11-12 JW: some corrections to allow correct compilation\r
-// 2003-11-28 JW: implementing code differences into current build of this file\r
-// 2003-12-10 JW: added "String." for the ChrW method because mbas doesn't support it without the String currently / TODO: remove it ASAP!\r
-\r
-//\r
-// Permission is hereby granted, free of charge, to any person obtaining\r
-// a copy of this software and associated documentation files (the\r
-// "Software"), to deal in the Software without restriction, including\r
-// without limitation the rights to use, copy, modify, merge, publish,\r
-// distribute, sublicense, and/or sell copies of the Software, and to\r
-// permit persons to whom the Software is furnished to do so, subject to\r
-// the following conditions:\r
-// \r
-// The above copyright notice and this permission notice shall be\r
-// included in all copies or substantial portions of the Software.\r
-// \r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-\r
-\r
-using System;\r
-using System.Globalization;\r
-using System.Text;\r
-using System.Text.RegularExpressions;\r
-using System.CodeDom;\r
-using System.CodeDom.Compiler;\r
-using System.IO;\r
-using System.Reflection;\r
-using System.Collections;\r
-\r
-namespace Microsoft.VisualBasic\r
-{\r
- internal class VBCodeGenerator : CodeGenerator\r
- {\r
- private string[] Keywords = new string[] {\r
- "AddHandler", "AddressOf", "Alias", "And",\r
- "AndAlso", "Ansi", "As", "Assembly",\r
- "Auto", "Boolean", "ByRef", "Byte", \r
- "ByVal", "Call", "Case", "Catch", \r
- "CBool", "CByte", "CChar", "CDate", \r
- "CDec", "CDbl", "Char", "CInt", \r
- "Class", "CLng", "CObj", "Const", \r
- "CShort", "CSng", "CStr", "CType", \r
- "Date", "Decimal", "Declare", "Default", \r
- "Delegate", "Dim", "DirectCast", "Do", \r
- "Double", "Each", "Else", "ElseIf", \r
- "End", "Enum", "Erase", "Error", \r
- "Event", "Exit", "False", "Finally", \r
- "For", "Friend", "Function", "Get", \r
- "GetType", "GoSub", "GoTo", "Handles", \r
- "If", "Implements", "Imports", "In", \r
- "Inherits", "Integer", "Interface", "Is", \r
- "Let", "Lib", "Like", "Long", \r
- "Loop", "Me", "Mod", "Module", \r
- "MustInherit", "MustOverride", "MyBase", "MyClass", \r
- "Namespace", "New", "Next", "Not", \r
- "Nothing", "NotInheritable", "NotOverridable", "Object", \r
- "On", "Option", "Optional", "Or", \r
- "OrElse", "Overloads", "Overridable", "Overrides", \r
- "ParamArray", "Preserve", "Private", "Property", \r
- "Protected", "Public", "RaiseEvent", "ReadOnly", \r
- "ReDim", "REM", "RemoveHandler", "Resume", \r
- "Return", "Select", "Set", "Shadows", \r
- "Shared", "Short", "Single", "Static", \r
- "Step", "Stop", "String", "Structure", \r
- "Sub", "SyncLock", "Then", "Throw", \r
- "To", "True", "Try", "TypeOf", \r
- "Unicode", "Until", "Variant", "When", \r
- "While", "With", "WithEvents", "WriteOnly", \r
- "Xor" \r
- };\r
-\r
- public VBCodeGenerator()\r
- {\r
- }\r
-\r
- protected override string NullToken {\r
- get {\r
- return "Nothing";\r
- }\r
- }\r
-\r
- protected override void ContinueOnNewLine (string st)\r
- {\r
- Output.Write (st);\r
- Output.WriteLine (" _");\r
- }\r
-\r
- protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("New ");\r
-\r
- CodeExpressionCollection initializers = expression.Initializers;\r
- CodeTypeReference createType = expression.CreateType;\r
-\r
- if (initializers.Count > 0) {\r
-\r
- OutputType (createType);\r
- \r
- output.WriteLine (" {");\r
- ++Indent;\r
- OutputExpressionList (initializers, true);\r
- --Indent;\r
- output.Write ("}");\r
-\r
- } \r
- else {\r
- CodeTypeReference arrayType = createType.ArrayElementType;\r
- while (arrayType != null) \r
- {\r
- createType = arrayType;\r
- arrayType = arrayType.ArrayElementType;\r
- }\r
-\r
- OutputType (createType);\r
-\r
- output.Write ('(');\r
-\r
- CodeExpression size = expression.SizeExpression;\r
- if (size != null)\r
- GenerateExpression (size);\r
- else\r
- output.Write (expression.Size);\r
-\r
- output.Write (')');\r
- }\r
- }\r
-\r
- protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)\r
- {\r
- Output.Write ("MyBase");\r
- }\r
-\r
- protected override void GenerateCastExpression (CodeCastExpression expression)\r
- {\r
- TextWriter output = Output;\r
- // ENHANCE: Use a DirectCast if it is known that expression.Expression is no Value-Type\r
- output.Write ("CType(");\r
- GenerateExpression (expression.Expression);\r
- output.Write (", ");\r
- OutputType (expression.TargetType);\r
- output.Write (")");\r
- }\r
-\r
- private bool AsBool(object datavalue)\r
- {\r
- return datavalue != null && datavalue is bool && (bool)datavalue;\r
- }\r
- \r
- private string OnOff(bool datavalue)\r
- {\r
- return datavalue?"On":"Off";\r
- }\r
-\r
- protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)\r
- {\r
- GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));\r
- GenerateComment (new CodeComment (" <autogenerated>"));\r
- GenerateComment (new CodeComment (" This code was generated by a tool."));\r
- GenerateComment (new CodeComment (" Mono Runtime Version: " + System.Environment.Version));\r
- GenerateComment (new CodeComment (""));\r
- GenerateComment (new CodeComment (" Changes to this file may cause incorrect behavior and will be lost if "));\r
- GenerateComment (new CodeComment (" the code is regenerated."));\r
- GenerateComment (new CodeComment (" </autogenerated>"));\r
- GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));\r
- Output.WriteLine ();\r
- if (AsBool(compileUnit.UserData["AllowLateBound"])) {\r
- Output.WriteLine("Option Explicit {0}",OnOff(AsBool(compileUnit.UserData["RequireVariableDeclaration"])));\r
- Output.WriteLine("Option Strict Off");\r
- } else {\r
- Output.WriteLine("Option Explicit On"); // Strict On implies Explicit On\r
- Output.WriteLine("Option Strict On");\r
- }\r
- Output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateCompileUnit (CodeCompileUnit compileUnit)\r
- {\r
- GenerateCompileUnitStart (compileUnit);\r
-\r
- OutputAttributes (compileUnit.AssemblyCustomAttributes, \r
- "Assembly: ", false);\r
-\r
- GenerateNamespaces (compileUnit);\r
-\r
- GenerateCompileUnitEnd (compileUnit);\r
- }\r
-\r
- protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("AddressOf ");\r
-\r
- CodeExpression targetObject = expression.TargetObject;\r
- if (targetObject != null) {\r
- GenerateExpression (targetObject);\r
- Output.Write ('.');\r
- }\r
- output.Write (expression.MethodName);\r
- }\r
-\r
- protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)\r
- {\r
- CodeExpression targetObject = expression.TargetObject;\r
- if (targetObject != null) {\r
- GenerateExpression (targetObject);\r
- Output.Write ('.');\r
- }\r
- Output.Write (expression.FieldName);\r
- }\r
- \r
- protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)\r
- {\r
- Output.Write (expression.ParameterName);\r
- }\r
-\r
- protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)\r
- {\r
- Output.Write (expression.VariableName);\r
- }\r
- \r
- protected override void GenerateIndexerExpression (CodeIndexerExpression expression)\r
- {\r
- TextWriter output = Output;\r
-\r
- GenerateExpression (expression.TargetObject);\r
- output.Write ('(');\r
- OutputExpressionList (expression.Indices);\r
- output.Write (')');\r
- }\r
- \r
- protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)\r
- {\r
- TextWriter output = Output;\r
-\r
- GenerateExpression (expression.TargetObject);\r
- output.Write (".Item(");\r
- OutputExpressionList (expression.Indices);\r
- output.Write (')');\r
- }\r
- \r
- protected override void GenerateSnippetExpression (CodeSnippetExpression expression)\r
- {\r
- Output.Write (expression.Value);\r
- }\r
- \r
- protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)\r
- {\r
- TextWriter output = Output;\r
-\r
- GenerateMethodReferenceExpression (expression.Method);\r
-\r
- output.Write ('(');\r
- OutputExpressionList (expression.Parameters);\r
- output.Write (')');\r
- }\r
-\r
- protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)\r
- {\r
- GenerateExpression (expression.TargetObject);\r
- Output.Write ('.');\r
- Output.Write (expression.MethodName);\r
- }\r
-\r
- protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)\r
- {\r
- GenerateExpression (expression.TargetObject);\r
- Output.Write ('.');\r
- Output.Write (expression.EventName);\r
- }\r
-\r
- protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)\r
- {\r
- Output.Write ("RaiseEvent ");\r
- GenerateExpression (expression.TargetObject);\r
- Output.Write ('(');\r
- OutputExpressionList (expression.Parameters);\r
- Output.WriteLine (')');\r
- }\r
- \r
- protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)\r
- {\r
- Output.Write( "New " );\r
- OutputType (expression.CreateType);\r
- Output.Write ('(');\r
- OutputExpressionList (expression.Parameters);\r
- Output.Write (')');\r
- }\r
-\r
- protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)\r
- {\r
- if (e.CustomAttributes != null && e.CustomAttributes.Count > 0)\r
- OutputAttributeDeclarations (e.CustomAttributes);\r
- OutputDirection (e.Direction);\r
- OutputTypeNamePair (e.Type, e.Name);\r
- }\r
-\r
- protected override void GeneratePrimitiveExpression (CodePrimitiveExpression e)\r
- {\r
- TextWriter output = Output;\r
-\r
- if (e.Value == null) {\r
- output.Write (NullToken);\r
- return;\r
- }\r
-\r
- Type type = e.Value.GetType ();\r
- if (type == typeof (bool)) {\r
- output.Write (e.Value.ToString ().ToLower (CultureInfo.InvariantCulture));\r
- } \r
- else if (type == typeof (char)) {\r
- output.Write ("\"" + e.Value.ToString () + "\"c");\r
- } \r
- else if (type == typeof (string)) {\r
- output.Write (QuoteSnippetString ((string) e.Value));\r
- } \r
- else if (type == typeof (byte) || type == typeof (sbyte) || type == typeof (short) ||\r
- type == typeof (int) || type == typeof (long) || type == typeof (float) ||\r
- type == typeof (double) || type == typeof (decimal)) {\r
- output.Write (e.Value.ToString ());\r
- } \r
- else {\r
- throw new ArgumentException ("Value type (" + type + ") is not a primitive type");\r
- }\r
- }\r
-\r
- protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)\r
- {\r
- GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);\r
- }\r
-\r
- protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)\r
- {\r
- Output.Write ("Value"); \r
- }\r
-\r
- protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)\r
- {\r
- Output.Write ("Me");\r
- }\r
-\r
- protected override void GenerateExpressionStatement (CodeExpressionStatement statement)\r
- {\r
- GenerateExpression (statement.Expression);\r
- Output.WriteLine (); //start new line\r
- }\r
-\r
- protected override void GenerateIterationStatement (CodeIterationStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- GenerateStatement (statement.InitStatement);\r
- output.Write ("Do While ");\r
- GenerateExpression (statement.TestExpression);\r
- output.WriteLine ();\r
- GenerateStatements (statement.Statements);\r
- GenerateStatement (statement.IncrementStatement);\r
- output.WriteLine ("Loop");\r
- }\r
-\r
- protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)\r
- {\r
- Output.Write ("Throw ");\r
- GenerateExpression (statement.ToThrow);\r
- }\r
-\r
- protected override void GenerateComment (CodeComment comment)\r
- {\r
- TextWriter output = Output;\r
-\r
- if (comment.DocComment)\r
- output.Write ("''' ");\r
- else\r
- output.Write ("' ");\r
-\r
- output.WriteLine (comment.Text);\r
- }\r
-\r
- protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("Return ");\r
- GenerateExpression (statement.Expression);\r
- output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateConditionStatement (CodeConditionStatement statement)\r
- {\r
- TextWriter output = Output;\r
- output.Write ("If (");\r
-\r
- GenerateExpression (statement.Condition);\r
-\r
- output.WriteLine (") Then");\r
- ++Indent;\r
- GenerateStatements (statement.TrueStatements);\r
- --Indent;\r
-\r
- CodeStatementCollection falses = statement.FalseStatements;\r
- if (falses.Count > 0) {\r
- output.WriteLine ("Else");\r
- ++Indent;\r
- GenerateStatements (falses);\r
- --Indent;\r
- }\r
- else {\r
- if (Options.ElseOnClosing)\r
- output.WriteLine ("Else");\r
- }\r
- output.WriteLine ("End If");\r
- }\r
-\r
- protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.WriteLine ("Try");\r
- ++Indent;\r
- GenerateStatements (statement.TryStatements);\r
- --Indent;\r
- output.WriteLine ();\r
- \r
- foreach (CodeCatchClause clause in statement.CatchClauses) {\r
- output.Write ("Catch ");\r
- OutputTypeNamePair (clause.CatchExceptionType, clause.LocalName);\r
- output.WriteLine ();\r
- ++Indent;\r
- GenerateStatements (clause.Statements);\r
- --Indent;\r
- output.WriteLine ();\r
- }\r
-\r
- CodeStatementCollection finallies = statement.FinallyStatements;\r
- if (finallies.Count > 0) {\r
-\r
- output.WriteLine ("Finally");\r
- ++Indent;\r
- GenerateStatements (finallies);\r
- --Indent;\r
- output.WriteLine ();\r
- }\r
-\r
- if (Options.ElseOnClosing) {\r
- if (statement.CatchClauses.Count == 0)\r
- output.WriteLine ("Catch");\r
- if (statement.FinallyStatements.Count == 0)\r
- output.WriteLine ("Finally");\r
- }\r
-\r
- output.WriteLine("End Try");\r
- }\r
-\r
- protected override void GenerateAssignStatement (CodeAssignStatement statement)\r
- { \r
- TextWriter output = Output;\r
- GenerateExpression (statement.Left);\r
- output.Write (" = ");\r
- GenerateExpression (statement.Right);\r
- output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- Output.Write ("AddHandler ");\r
- GenerateEventReferenceExpression (statement.Event);\r
- Output.Write ( ", ");\r
- GenerateExpression (statement.Listener);\r
- output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- Output.Write ("RemoveHandler ");\r
- GenerateEventReferenceExpression (statement.Event);\r
- Output.Write ( ", ");\r
- GenerateExpression (statement.Listener);\r
- output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateGotoStatement (CodeGotoStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("Goto ");\r
- output.Write (statement.Label);\r
- output.WriteLine ();\r
- }\r
- \r
- protected override void GenerateLabeledStatement (CodeLabeledStatement statement)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write (statement.Label + ":");\r
- GenerateStatement (statement.Statement);\r
- }\r
-\r
- protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("GetType(");\r
- OutputType (e.Type);\r
- output.Write (")");\r
- }\r
-\r
- protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("Dim ");\r
- OutputTypeNamePair (statement.Type, statement.Name);\r
-\r
- CodeExpression initExpression = statement.InitExpression;\r
- if (initExpression != null) \r
- {\r
- output.Write (" = ");\r
- GenerateExpression (initExpression);\r
- }\r
-\r
- output.WriteLine();\r
- }\r
-\r
- protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)\r
- {\r
- Output.WriteLine ();\r
- Output.Write ("#ExternalSource(");\r
- Output.Write (linePragma.FileName);\r
- Output.Write (", ");\r
- Output.Write (linePragma.LineNumber);\r
- Output.WriteLine (")");\r
- }\r
-\r
- protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)\r
- {\r
- Output.WriteLine ("#End ExternalSource");\r
- }\r
-\r
- protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)\r
- {\r
- TextWriter output = Output;\r
-\r
- if (eventRef.CustomAttributes.Count > 0)\r
- OutputAttributes (eventRef.CustomAttributes, null, true);\r
-\r
- MemberAttributes attributes = eventRef.Attributes;\r
-\r
- OutputMemberAccessModifier (attributes);\r
- OutputMemberScopeModifier (attributes | MemberAttributes.Final); // Don't output "Overridable"\r
-\r
- output.Write ("Event ");\r
- OutputTypeNamePair (eventRef.Type, eventRef.Name);\r
- output.WriteLine ();\r
- }\r
-\r
- protected override void GenerateField (CodeMemberField field)\r
- {\r
- TextWriter output = Output;\r
-\r
- if (field.CustomAttributes.Count > 0)\r
- OutputAttributes (field.CustomAttributes, null, true);\r
-\r
- MemberAttributes attributes = field.Attributes;\r
- OutputMemberAccessModifier (attributes);\r
- OutputFieldScopeModifier (attributes);\r
-\r
- OutputTypeNamePair (field.Type, field.Name);\r
-\r
- CodeExpression initExpression = field.InitExpression;\r
- if (initExpression != null) {\r
- output.Write (" = ");\r
- GenerateExpression (initExpression);\r
- }\r
-\r
- output.WriteLine();\r
- }\r
- \r
- protected override void GenerateSnippetMember (CodeSnippetTypeMember member)\r
- {\r
- Output.Write (member.Text);\r
- }\r
- \r
- protected override void GenerateEntryPointMethod( CodeEntryPointMethod method, CodeTypeDeclaration declaration )\r
- {\r
- method.Name = "Main";\r
- GenerateMethod (method, declaration);\r
- }\r
- \r
- [MonoTODO ("partially implemented")]\r
- protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)\r
- {\r
- bool isSub = method.ReturnType == null || method.ReturnType.BaseType == "System.Void";\r
-\r
- TextWriter output = Output;\r
-\r
- if (method.CustomAttributes.Count > 0)\r
- OutputAttributeDeclarations (method.CustomAttributes);\r
-\r
- MemberAttributes attributes = method.Attributes;\r
-\r
- OutputMemberAccessModifier (attributes);\r
- OutputMemberScopeModifier (attributes);\r
-\r
- if (isSub)\r
- output.Write ("Sub ");\r
- else\r
- output.Write ("Function ");\r
-\r
- output.Write (method.Name);\r
- output.Write ('(');\r
- OutputParameters (method.Parameters);\r
- output.Write (')');\r
-\r
- if (!isSub) {\r
- output.Write (" As ");\r
- OutputType (method.ReturnType);\r
- }\r
-\r
- if (method.ImplementationTypes.Count > 0) {\r
- output.Write (" Implements ");\r
- foreach (CodeTypeReference type in method.ImplementationTypes)\r
- {\r
- OutputType (type);\r
- output.Write ('.');\r
- // TODO implementation incomplete\r
-\r
- }\r
- }\r
-\r
- // TODO private implementations\r
-\r
- if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract)\r
- output.WriteLine ();\r
- else {\r
- output.WriteLine ();\r
- ++Indent;\r
- GenerateStatements (method.Statements);\r
- --Indent;\r
- if (isSub)\r
- output.WriteLine ("End Sub");\r
- else\r
- output.WriteLine ("End Function");\r
- }\r
- }\r
-\r
- protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)\r
- {\r
- TextWriter output = Output;\r
-\r
- if (property.CustomAttributes.Count > 0)\r
- OutputAttributeDeclarations (property.CustomAttributes);\r
-\r
- MemberAttributes attributes = property.Attributes;\r
- OutputMemberAccessModifier (attributes);\r
- OutputMemberScopeModifier (attributes);\r
-\r
- if (property.HasGet && (!property.HasSet))\r
- output.Write ("ReadOnly " );\r
-\r
- if (property.HasSet && (!property.HasGet))\r
- output.Write ("WriteOnly " );\r
-\r
- output.Write ("Property " );\r
- \r
- OutputTypeNamePair (property.Type, property.Name);\r
- output.WriteLine ();\r
- ++Indent;\r
-\r
- if (property.HasGet) {\r
- output.WriteLine ("Get");\r
- ++Indent;\r
-\r
- GenerateStatements (property.GetStatements);\r
-\r
- --Indent;\r
- output.WriteLine ("End Get");\r
- }\r
- \r
- if (property.HasSet) {\r
- output.Write ("Set (");\r
- OutputTypeNamePair (property.Type, "Value");\r
- output.WriteLine (")");\r
- ++Indent;\r
-\r
- GenerateStatements (property.SetStatements);\r
-\r
- --Indent;\r
- output.WriteLine ("End Set");\r
- }\r
-\r
- --Indent;\r
- output.WriteLine ("End Property");\r
- }\r
-\r
- [MonoTODO ("not implemented")]\r
- protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)\r
- {\r
- if (constructor.CustomAttributes.Count > 0)\r
- OutputAttributeDeclarations (constructor.CustomAttributes);\r
- OutputMemberAccessModifier (constructor.Attributes);\r
- Output.Write ("Sub New(");\r
- OutputParameters (constructor.Parameters);\r
- Output.WriteLine (")");\r
- // Handle BaseConstructorArgs, ChainedConstructorArgs, ImplementationTypes\r
- Indent++;\r
- GenerateStatements (constructor.Statements);\r
- Indent--;\r
- Output.WriteLine ("End Sub");\r
- }\r
- \r
- protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)\r
- {\r
- Output.WriteLine ("Shared Sub New()");\r
- Indent++;\r
- GenerateStatements (constructor.Statements);\r
- Indent--;\r
- Output.WriteLine ("End Sub");\r
- }\r
-\r
- [MonoTODO ("not implemented")]\r
- protected override void GenerateTypeStart (CodeTypeDeclaration declaration)\r
- {\r
- TextWriter output = Output;\r
-\r
- OutputAttributes (declaration.CustomAttributes, null, true);\r
- TypeAttributes attributes = declaration.TypeAttributes;\r
- OutputTypeAttributes (attributes,\r
- declaration.IsStruct,\r
- declaration.IsEnum);\r
-\r
- output.WriteLine (declaration.Name);\r
-\r
- ++Indent;\r
- \r
- IEnumerator enumerator = declaration.BaseTypes.GetEnumerator();\r
- if (enumerator.MoveNext()) \r
- {\r
- CodeTypeReference type = (CodeTypeReference)enumerator.Current;\r
- \r
- if (type != null)\r
- {\r
- output.Write ("Inherits ");\r
- OutputType (type);\r
- output.WriteLine ();\r
- }\r
- \r
- while (enumerator.MoveNext()) \r
- {\r
- type = (CodeTypeReference)enumerator.Current;\r
- \r
- if (type != null)\r
- {\r
- output.Write ("Implements ");\r
- OutputType (type);\r
- output.WriteLine ();\r
- }\r
- }\r
- }\r
- }\r
-\r
- protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)\r
- {\r
- string output = string.Empty;\r
-\r
- --Indent;\r
- if (declaration.IsStruct)\r
- output = "End Structure";\r
- if (declaration.IsInterface)\r
- output = "End Interface";\r
- if (declaration.IsEnum)\r
- output = "End Enum";\r
- if (declaration.IsClass)\r
- output = "End Class";\r
-\r
- Output.WriteLine (output);\r
- }\r
-\r
- protected override void GenerateNamespace(CodeNamespace ns)\r
- {\r
- GenerateCommentStatements (ns.Comments);\r
- \r
- // add regular imports\r
- GenerateNamespaceImports (ns);\r
-\r
- Output.WriteLine (); \r
- GenerateNamespaceStart (ns); \r
- GenerateTypes (ns);\r
- GenerateNamespaceEnd (ns);\r
- }\r
-\r
-\r
- protected override void GenerateNamespaceStart (CodeNamespace ns)\r
- {\r
- TextWriter output = Output;\r
- \r
- string name = ns.Name;\r
- if (name != null && name != string.Empty) {\r
- output.Write ("Namespace ");\r
- output.WriteLine (name);\r
- ++Indent;\r
- }\r
- }\r
-\r
- protected override void GenerateNamespaceEnd (CodeNamespace ns)\r
- {\r
- string name = ns.Name;\r
- if (name != null && name != string.Empty) {\r
- --Indent;\r
- Output.WriteLine ("End Namespace");\r
- }\r
- }\r
-\r
- protected override void GenerateNamespaceImport (CodeNamespaceImport import)\r
- {\r
- TextWriter output = Output;\r
-\r
- output.Write ("Imports ");\r
- output.Write (import.Namespace);\r
- output.WriteLine ();\r
- }\r
- \r
- protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)\r
- {\r
- Output.Write ('<');\r
- }\r
- \r
- protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)\r
- {\r
- Output.Write (">");\r
- }\r
-\r
- private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, bool continueLine) {\r
- if (attributes.Count != 0) {\r
- GenerateAttributeDeclarationsStart (attributes);\r
-\r
- IEnumerator enumerator = attributes.GetEnumerator ();\r
- if (enumerator.MoveNext ()) {\r
- CodeAttributeDeclaration att = (CodeAttributeDeclaration) enumerator.Current;\r
- if (prefix != null) {\r
- Output.Write (prefix);\r
- }\r
- OutputAttributeDeclaration (att);\r
-\r
- while (enumerator.MoveNext ()) {\r
- ContinueOnNewLine (", ");\r
- Output.Write (" ");\r
- att = (CodeAttributeDeclaration) enumerator.Current;\r
- if (prefix != null) {\r
- Output.Write (prefix);\r
- }\r
- OutputAttributeDeclaration (att);\r
- }\r
- }\r
- GenerateAttributeDeclarationsEnd (attributes);\r
- Output.Write (" ");\r
- if (continueLine) {\r
- ContinueOnNewLine ("");\r
- } else {\r
- Output.WriteLine ();\r
- }\r
- }\r
- }\r
-\r
- protected override void OutputAttributeArgument (CodeAttributeArgument argument)\r
- {\r
- string name = argument.Name;\r
- if (name != null) {\r
- Output.Write (name);\r
- Output.Write (":=");\r
- }\r
- GenerateExpression (argument.Value);\r
- }\r
-\r
- private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)\r
- {\r
- Output.Write (attribute.Name.Replace ('+', '.'));\r
- Output.Write ('(');\r
- IEnumerator enumerator = attribute.Arguments.GetEnumerator ();\r
- if (enumerator.MoveNext ()) {\r
- CodeAttributeArgument argument = (CodeAttributeArgument) enumerator.Current;\r
- OutputAttributeArgument (argument);\r
-\r
- while (enumerator.MoveNext ()) {\r
- Output.Write (", ");\r
- argument = (CodeAttributeArgument) enumerator.Current;\r
- OutputAttributeArgument (argument);\r
- }\r
- }\r
- Output.Write (')');\r
- }\r
-\r
- protected override void OutputDirection (FieldDirection direction)\r
- {\r
- switch (direction) {\r
- case FieldDirection.In:\r
- //there is no "In"\r
- break;\r
- case FieldDirection.Out:\r
- Output.Write ("ByVal ");\r
- break;\r
- case FieldDirection.Ref:\r
- Output.Write ("ByRef ");\r
- break;\r
- }\r
- }\r
-\r
- protected override void OutputFieldScopeModifier (MemberAttributes attributes)\r
- {\r
- if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)\r
- Output.Write ("New ");\r
-\r
- switch (attributes & MemberAttributes.ScopeMask) {\r
- case MemberAttributes.Static:\r
- Output.Write ("Shared ");\r
- break;\r
- case MemberAttributes.Const:\r
- Output.Write ("Const ");\r
- break;\r
- }\r
- }\r
-\r
- protected override void OutputMemberAccessModifier (MemberAttributes attributes)\r
- {\r
- switch (attributes & MemberAttributes.AccessMask) {\r
- case MemberAttributes.Assembly:\r
- Output.Write ("Friend ");\r
- break;\r
- case MemberAttributes.FamilyAndAssembly:\r
- Output.Write ("Friend "); \r
- break;\r
- case MemberAttributes.Family:\r
- Output.Write ("Protected ");\r
- break;\r
- case MemberAttributes.FamilyOrAssembly:\r
- Output.Write ("Protected Friend ");\r
- break;\r
- case MemberAttributes.Private:\r
- Output.Write ("Private ");\r
- break;\r
- case MemberAttributes.Public:\r
- Output.Write ("Public ");\r
- break;\r
- }\r
- }\r
-\r
- protected override void OutputMemberScopeModifier (MemberAttributes attributes)\r
- {\r
- if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)\r
- Output.Write ("New ");\r
-\r
- switch (attributes & MemberAttributes.ScopeMask) {\r
- case MemberAttributes.Abstract:\r
- Output.Write ("MustOverride ");\r
- break;\r
- case MemberAttributes.Final:\r
- //JW 2004-06-03: seems to be the "sealed" keyword in C# and the "NotOverridable" keyword in VB, but conflicts with ASP.NET generation\r
- //Output.Write ("NotOverridable ");\r
- break;\r
- case MemberAttributes.Static:\r
- Output.Write ("Shared ");\r
- break;\r
- case MemberAttributes.Override:\r
- Output.Write ("Overrides ");\r
- break;\r
- case MemberAttributes.Overloaded:\r
- // based on http://gendotnet.com/Code%20Gen%20Articles/codedom.htm\r
- Output.Write ("Overloads ");\r
- MemberAttributes access_ovl = attributes & MemberAttributes.AccessMask;\r
- if ( access_ovl == MemberAttributes.Public || \r
- access_ovl == MemberAttributes.Family )\r
- Output.Write ("Overridable ");\r
- break;\r
- default:\r
- //\r
- // FUNNY! if the scope value is\r
- // rubbish (0 or >Const), and access\r
- // is public or protected, make it\r
- // "virtual".\r
- //\r
- // i'm not sure whether this is 100%\r
- // correct, but it seems to be MS\r
- // behavior. \r
- //\r
- MemberAttributes access = attributes & MemberAttributes.AccessMask;\r
- if ( access == MemberAttributes.Public || \r
- access == MemberAttributes.Family )\r
- Output.Write ("Overridable ");\r
- break;\r
- }\r
- }\r
-\r
- protected override void OutputOperator (CodeBinaryOperatorType op)\r
- {\r
- switch (op) {\r
- case CodeBinaryOperatorType.Add:\r
- Output.Write ("+");\r
- break;\r
- case CodeBinaryOperatorType.Subtract:\r
- Output.Write ("-");\r
- break;\r
- case CodeBinaryOperatorType.Multiply:\r
- Output.Write ("*");\r
- break;\r
- case CodeBinaryOperatorType.Divide:\r
- Output.Write ("/");\r
- break;\r
- case CodeBinaryOperatorType.Modulus:\r
- Output.Write ("Mod");\r
- break;\r
- case CodeBinaryOperatorType.Assign:\r
- Output.Write ("=");\r
- break;\r
- case CodeBinaryOperatorType.IdentityInequality:\r
- Output.Write ("<>");\r
- break;\r
- case CodeBinaryOperatorType.IdentityEquality:\r
- Output.Write ("Is");\r
- break;\r
- case CodeBinaryOperatorType.ValueEquality:\r
- Output.Write ("=");\r
- break;\r
- case CodeBinaryOperatorType.BitwiseOr:\r
- Output.Write ("Or");\r
- break;\r
- case CodeBinaryOperatorType.BitwiseAnd:\r
- Output.Write ("And");\r
- break;\r
- case CodeBinaryOperatorType.BooleanOr:\r
- Output.Write ("OrElse");\r
- break;\r
- case CodeBinaryOperatorType.BooleanAnd:\r
- Output.Write ("AndAlso");\r
- break;\r
- case CodeBinaryOperatorType.LessThan:\r
- Output.Write ("<");\r
- break;\r
- case CodeBinaryOperatorType.LessThanOrEqual:\r
- Output.Write ("<=");\r
- break;\r
- case CodeBinaryOperatorType.GreaterThan:\r
- Output.Write (">");\r
- break;\r
- case CodeBinaryOperatorType.GreaterThanOrEqual:\r
- Output.Write (">=");\r
- break;\r
- }\r
- }\r
-\r
- protected override void OutputTypeAttributes (TypeAttributes attributes, bool isStruct, bool isEnum)\r
- {\r
- TextWriter output = Output;\r
-\r
- switch (attributes & TypeAttributes.VisibilityMask) {\r
- case TypeAttributes.NotPublic:\r
- // Does this mean friend access?\r
- output.Write ("Friend ");\r
- break; \r
-\r
- case TypeAttributes.Public:\r
- case TypeAttributes.NestedPublic:\r
- output.Write ("Public ");\r
- break;\r
-\r
- case TypeAttributes.NestedPrivate:\r
- output.Write ("Private ");\r
- break;\r
- case TypeAttributes.NestedAssembly:\r
- output.Write ("Friend ");\r
- break;\r
- case TypeAttributes.NestedFamily:\r
- output.Write ("Protected ");\r
- break;\r
- case TypeAttributes.NestedFamORAssem:\r
- output.Write ("Protected Friend ");\r
- break;\r
- case TypeAttributes.NestedFamANDAssem:\r
- output.Write ("Friend ");\r
- break;\r
- }\r
-\r
- if (isStruct)\r
- output.Write ("Structure ");\r
-\r
- else if (isEnum)\r
- output.Write ("Enumeration ");\r
-\r
- else {\r
- if ((attributes & TypeAttributes.Interface) != 0) \r
- output.Write ("Interface ");\r
-\r
- else {\r
- if ((attributes & TypeAttributes.Sealed) != 0)\r
- output.Write ("NotInheritable ");\r
-\r
- if ((attributes & TypeAttributes.Abstract) != 0)\r
- output.Write ("MustInherit ");\r
- \r
- output.Write ("Class ");\r
- }\r
- }\r
- }\r
-\r
- protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)\r
- {\r
- Output.Write (name + " As " + GetTypeOutput (typeRef));\r
- }\r
-\r
- protected override void OutputType (CodeTypeReference type)\r
- {\r
- Output.Write (GetTypeOutput (type));\r
- }\r
-\r
- protected override string QuoteSnippetString (string value)\r
- {\r
- StringBuilder mySBuilder = new StringBuilder(value.Length);\r
- mySBuilder.Append ("\"");\r
- bool inQuotes = true;\r
- for (int MyCounter = 0; MyCounter < value.Length; MyCounter++)\r
- {\r
- if (value[MyCounter] == 34) //quotation mark\r
- {\r
- if (!inQuotes)\r
- {\r
- mySBuilder.Append ("&\"");\r
- inQuotes = true;\r
- }\r
- mySBuilder.Append (value[MyCounter]);\r
- mySBuilder.Append (value[MyCounter]);\r
- }\r
- else if (value[MyCounter] >= 32) //standard ansi/unicode characters\r
- {\r
- if (!inQuotes)\r
- {\r
- mySBuilder.Append ("&\"");\r
- inQuotes = true;\r
- }\r
- mySBuilder.Append (value[MyCounter]);\r
- }\r
- else //special chars, e.g. line break\r
- {\r
- if (inQuotes)\r
- { \r
- mySBuilder.Append ("\"");\r
- inQuotes = false;\r
- }\r
- mySBuilder.Append ("&Microsoft.VisualBasic.ChrW(");\r
- mySBuilder.Append ((int)value[MyCounter]); \r
- mySBuilder.Append (")");\r
- } \r
- }\r
- if (inQuotes)\r
- mySBuilder.Append ("\"");\r
- return mySBuilder.ToString();\r
- }\r
-\r
- private void GenerateMemberReferenceExpression (CodeExpression targetObject, string memberName)\r
- {\r
- GenerateExpression (targetObject);\r
- Output.Write ('.');\r
- Output.Write (memberName);\r
- }\r
- \r
- /* \r
- * ICodeGenerator\r
- */\r
-\r
- protected override string CreateEscapedIdentifier (string value)\r
- {\r
- for (int x = 0; x < Keywords.Length; x++)\r
- if (value.ToLower().Equals (Keywords[x].ToLower()))\r
- return "[" + value + "]";\r
- return value;\r
- }\r
-\r
- protected override string CreateValidIdentifier (string value)\r
- {\r
- for (int x = 0; x < Keywords.Length; x++)\r
- if (value.ToLower().Equals (Keywords[x].ToLower()))\r
- return "_" + value;\r
- return value;\r
- }\r
-\r
- protected override string GetTypeOutput (CodeTypeReference type)\r
- {\r
- string output;\r
- CodeTypeReference arrayType;\r
-\r
- arrayType = type.ArrayElementType;\r
- if (arrayType != null)\r
- output = GetTypeOutput (arrayType);\r
- else { \r
- switch (type.BaseType) {\r
- case "System.Decimal":\r
- output = "Decimal";\r
- break;\r
- case "System.Double":\r
- output = "Double";\r
- break;\r
- case "System.Single":\r
- output = "Single";\r
- break;\r
- case "System.Byte":\r
- output = "Byte";\r
- break;\r
- case "System.Int32":\r
- output = "Integer";\r
- break;\r
- case "System.Int64":\r
- output = "Long";\r
- break;\r
- case "System.Int16":\r
- output = "Short";\r
- break;\r
- case "System.Boolean":\r
- output = "Boolean";\r
- break;\r
- case "System.Char":\r
- output = "Char";\r
- break;\r
- case "System.String":\r
- output = "String";\r
- break;\r
- case "System.Object":\r
- output = "Object";\r
- break;\r
- default:\r
- output = type.BaseType;\r
- break;\r
- }\r
- }\r
-\r
- int rank = type.ArrayRank;\r
- if (rank > 0) {\r
- output += "(";\r
- for (--rank; rank > 0; --rank)\r
- output += ",";\r
- output += ")";\r
- }\r
-\r
- return output;\r
- }\r
-\r
- protected override bool IsValidIdentifier (string identifier)\r
- {\r
- for (int x = 0; x < Keywords.Length; x++)\r
- if (identifier.ToLower().Equals (Keywords[x].ToLower()))\r
- return false;\r
- return true;\r
- }\r
-\r
- protected override bool Supports (GeneratorSupport supports)\r
- {\r
- return true;\r
- }\r
- }\r
-}\r
+//
+// Microsoft.VisualBasic.VBCodeGenerator.cs
+//
+// Author:
+// Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+// (partially based on CSharpCodeGenerator)
+// Jochen Wezel (jwezel@compumaster.de)
+//
+// (C) 2003 Andreas Nahr
+// (C) 2003 Jochen Wezel (http://www.compumaster.de)
+//
+// Modifications:
+// 2003-11-06 JW: some corrections regarding missing spaces in generated code (e. g. "Property ")
+// 2003-11-06 JW: QuoteSnippetString implemented
+// 2003-11-08 JW: automatically add Microsoft.VisualBasic
+// 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!
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+using System;
+using System.Globalization;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.IO;
+using System.Reflection;
+using System.Collections;
+
+namespace Microsoft.VisualBasic
+{
+ internal class VBCodeGenerator : CodeGenerator
+ {
+ private string[] Keywords = new string[] {
+ "AddHandler", "AddressOf", "Alias", "And",
+ "AndAlso", "Ansi", "As", "Assembly",
+ "Auto", "Boolean", "ByRef", "Byte",
+ "ByVal", "Call", "Case", "Catch",
+ "CBool", "CByte", "CChar", "CDate",
+ "CDec", "CDbl", "Char", "CInt",
+ "Class", "CLng", "CObj", "Const",
+ "CShort", "CSng", "CStr", "CType",
+ "Date", "Decimal", "Declare", "Default",
+ "Delegate", "Dim", "DirectCast", "Do",
+ "Double", "Each", "Else", "ElseIf",
+ "End", "Enum", "Erase", "Error",
+ "Event", "Exit", "False", "Finally",
+ "For", "Friend", "Function", "Get",
+ "GetType", "GoSub", "GoTo", "Handles",
+ "If", "Implements", "Imports", "In",
+ "Inherits", "Integer", "Interface", "Is",
+ "Let", "Lib", "Like", "Long",
+ "Loop", "Me", "Mod", "Module",
+ "MustInherit", "MustOverride", "MyBase", "MyClass",
+ "Namespace", "New", "Next", "Not",
+ "Nothing", "NotInheritable", "NotOverridable", "Object",
+ "On", "Option", "Optional", "Or",
+ "OrElse", "Overloads", "Overridable", "Overrides",
+ "ParamArray", "Preserve", "Private", "Property",
+ "Protected", "Public", "RaiseEvent", "ReadOnly",
+ "ReDim", "REM", "RemoveHandler", "Resume",
+ "Return", "Select", "Set", "Shadows",
+ "Shared", "Short", "Single", "Static",
+ "Step", "Stop", "String", "Structure",
+ "Sub", "SyncLock", "Then", "Throw",
+ "To", "True", "Try", "TypeOf",
+ "Unicode", "Until", "Variant", "When",
+ "While", "With", "WithEvents", "WriteOnly",
+ "Xor"
+ };
+
+ public VBCodeGenerator()
+ {
+ }
+
+ protected override string NullToken {
+ get {
+ return "Nothing";
+ }
+ }
+
+ protected override void ContinueOnNewLine (string st)
+ {
+ Output.Write (st);
+ Output.WriteLine (" _");
+ }
+
+ protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
+ {
+ TextWriter output = Output;
+
+ output.Write ("New ");
+
+ CodeExpressionCollection initializers = expression.Initializers;
+ CodeTypeReference createType = expression.CreateType;
+
+ if (initializers.Count > 0) {
+
+ OutputType (createType);
+
+ output.WriteLine (" {");
+ ++Indent;
+ OutputExpressionList (initializers, true);
+ --Indent;
+ output.Write ("}");
+
+ }
+ else {
+ CodeTypeReference arrayType = createType.ArrayElementType;
+ while (arrayType != null)
+ {
+ createType = arrayType;
+ arrayType = arrayType.ArrayElementType;
+ }
+
+ OutputType (createType);
+
+ output.Write ('(');
+
+ CodeExpression size = expression.SizeExpression;
+ if (size != null)
+ GenerateExpression (size);
+ else
+ output.Write (expression.Size);
+
+ output.Write (')');
+ }
+ }
+
+ protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)
+ {
+ Output.Write ("MyBase");
+ }
+
+ protected override void GenerateCastExpression (CodeCastExpression expression)
+ {
+ TextWriter output = Output;
+ // ENHANCE: Use a DirectCast if it is known that expression.Expression is no Value-Type
+ output.Write ("CType(");
+ GenerateExpression (expression.Expression);
+ output.Write (", ");
+ OutputType (expression.TargetType);
+ output.Write (")");
+ }
+
+ private bool AsBool(object datavalue)
+ {
+ return datavalue != null && datavalue is bool && (bool)datavalue;
+ }
+
+ private string OnOff(bool datavalue)
+ {
+ return datavalue?"On":"Off";
+ }
+
+ protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
+ {
+ GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
+ GenerateComment (new CodeComment (" <autogenerated>"));
+ GenerateComment (new CodeComment (" This code was generated by a tool."));
+ GenerateComment (new CodeComment (" Mono Runtime Version: " + System.Environment.Version));
+ GenerateComment (new CodeComment (""));
+ GenerateComment (new CodeComment (" Changes to this file may cause incorrect behavior and will be lost if "));
+ GenerateComment (new CodeComment (" the code is regenerated."));
+ GenerateComment (new CodeComment (" </autogenerated>"));
+ GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
+ Output.WriteLine ();
+ 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
+ Output.WriteLine("Option Strict On");
+ }
+ Output.WriteLine ();
+ }
+
+ protected override void GenerateCompileUnit (CodeCompileUnit compileUnit)
+ {
+ GenerateCompileUnitStart (compileUnit);
+
+ OutputAttributes (compileUnit.AssemblyCustomAttributes,
+ "Assembly: ", LineHandling.NewLine);
+
+ GenerateNamespaces (compileUnit);
+
+ GenerateCompileUnitEnd (compileUnit);
+ }
+
+ protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
+ {
+ TextWriter output = Output;
+
+ output.Write ("AddressOf ");
+
+ CodeExpression targetObject = expression.TargetObject;
+ if (targetObject != null) {
+ GenerateExpression (targetObject);
+ Output.Write ('.');
+ }
+ output.Write (expression.MethodName);
+ }
+
+ protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)
+ {
+ CodeExpression targetObject = expression.TargetObject;
+ if (targetObject != null) {
+ GenerateExpression (targetObject);
+ Output.Write ('.');
+ }
+ Output.Write (expression.FieldName);
+ }
+
+ protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
+ {
+ Output.Write (expression.ParameterName);
+ }
+
+ protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
+ {
+ Output.Write (expression.VariableName);
+ }
+
+ protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
+ {
+ TextWriter output = Output;
+
+ GenerateExpression (expression.TargetObject);
+ output.Write ('(');
+ OutputExpressionList (expression.Indices);
+ output.Write (')');
+ }
+
+ protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)
+ {
+ TextWriter output = Output;
+
+ GenerateExpression (expression.TargetObject);
+ output.Write (".Item(");
+ OutputExpressionList (expression.Indices);
+ output.Write (')');
+ }
+
+ protected override void GenerateSnippetExpression (CodeSnippetExpression expression)
+ {
+ Output.Write (expression.Value);
+ }
+
+ protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)
+ {
+ TextWriter output = Output;
+
+ GenerateMethodReferenceExpression (expression.Method);
+
+ output.Write ('(');
+ OutputExpressionList (expression.Parameters);
+ output.Write (')');
+ }
+
+ protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
+ {
+ GenerateExpression (expression.TargetObject);
+ Output.Write ('.');
+ Output.Write (expression.MethodName);
+ }
+
+ protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
+ {
+ if (expression.TargetObject != null) {
+ GenerateExpression (expression.TargetObject);
+ Output.Write ('.');
+ }
+ Output.Write (CreateEscapedIdentifier(expression.EventName));
+ }
+
+ protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
+ {
+ Output.Write ("RaiseEvent ");
+ GenerateExpression (expression.TargetObject);
+ Output.Write ('(');
+ OutputExpressionList (expression.Parameters);
+ Output.WriteLine (')');
+ }
+
+ protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
+ {
+ Output.Write( "New " );
+ OutputType (expression.CreateType);
+ Output.Write ('(');
+ OutputExpressionList (expression.Parameters);
+ Output.Write (')');
+ }
+
+ protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
+ {
+ OutputAttributes (e.CustomAttributes, null, LineHandling.InLine);
+ OutputDirection (e.Direction);
+ OutputTypeNamePair (e.Type, e.Name);
+ }
+
+ protected override void GeneratePrimitiveExpression (CodePrimitiveExpression e)
+ {
+ 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;
+ Output.Write (uc.ToString(CultureInfo.InvariantCulture));
+ Output.Write ("US");
+ } else if (e.Value is uint) {
+ uint ui = (uint) e.Value;
+ Output.Write (ui.ToString(CultureInfo.InvariantCulture));
+ Output.Write ("UI");
+ } else if (e.Value is ulong) {
+ ulong ul = (ulong) e.Value;
+ Output.Write (ul.ToString(CultureInfo.InvariantCulture));
+ Output.Write ("UL");
+ } else if (e.Value is sbyte) {
+ sbyte sb = (sbyte) e.Value;
+ 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);
+ }
+ }
+
+ protected override void GenerateSingleFloatValue (float s)
+ {
+ base.GenerateSingleFloatValue (s);
+ base.Output.Write ('!');
+ }
+
+ protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
+ {
+ GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
+ }
+
+ protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
+ {
+ Output.Write ("Value");
+ }
+
+ protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)
+ {
+ Output.Write ("Me");
+ }
+
+ protected override void GenerateExpressionStatement (CodeExpressionStatement statement)
+ {
+ GenerateExpression (statement.Expression);
+ Output.WriteLine (); //start new line
+ }
+
+ protected override void GenerateIterationStatement (CodeIterationStatement statement)
+ {
+ TextWriter output = Output;
+
+ GenerateStatement (statement.InitStatement);
+ output.Write ("Do While ");
+ GenerateExpression (statement.TestExpression);
+ output.WriteLine ();
+ Indent++;
+ GenerateStatements (statement.Statements);
+ GenerateStatement (statement.IncrementStatement);
+ Indent--;
+ output.WriteLine ("Loop");
+ }
+
+ protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)
+ {
+ Output.Write ("Throw");
+ if (statement.ToThrow != null) {
+ Output.Write (' ');
+ GenerateExpression (statement.ToThrow);
+ }
+ Output.WriteLine ();
+ }
+
+ protected override void GenerateComment (CodeComment comment)
+ {
+ TextWriter output = Output;
+ string commentChars = null;
+
+ if (comment.DocComment) {
+ commentChars = "'''";
+ } else {
+ commentChars = "'";
+ }
+
+ output.Write (commentChars);
+ string text = comment.Text;
+
+ for (int i = 0; i < text.Length; i++) {
+ output.Write (text[i]);
+ if (text[i] == '\r') {
+ if (i < (text.Length - 1) && text[i + 1] == '\n') {
+ continue;
+ }
+ output.Write (commentChars);
+ } else if (text[i] == '\n') {
+ output.Write (commentChars);
+ }
+ }
+
+ output.WriteLine ();
+ }
+
+ protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)
+ {
+ TextWriter output = Output;
+
+ if (statement.Expression != null) {
+ output.Write ("Return ");
+ GenerateExpression (statement.Expression);
+ output.WriteLine ();
+ } else {
+ output.WriteLine ("Return");
+ }
+ }
+
+ protected override void GenerateConditionStatement (CodeConditionStatement statement)
+ {
+ TextWriter output = Output;
+ output.Write ("If ");
+
+ GenerateExpression (statement.Condition);
+
+ output.WriteLine (" Then");
+ ++Indent;
+ GenerateStatements (statement.TrueStatements);
+ --Indent;
+
+ CodeStatementCollection falses = statement.FalseStatements;
+ if (falses.Count > 0) {
+ output.WriteLine ("Else");
+ ++Indent;
+ GenerateStatements (falses);
+ --Indent;
+ }
+ else {
+ if (Options.ElseOnClosing)
+ output.WriteLine ("Else");
+ }
+ output.WriteLine ("End If");
+ }
+
+ protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)
+ {
+ TextWriter output = Output;
+
+ output.WriteLine ("Try ");
+ ++Indent;
+ GenerateStatements (statement.TryStatements);
+ --Indent;
+
+ foreach (CodeCatchClause clause in statement.CatchClauses) {
+ output.Write ("Catch ");
+ OutputTypeNamePair (clause.CatchExceptionType, clause.LocalName);
+ output.WriteLine ();
+ ++Indent;
+ GenerateStatements (clause.Statements);
+ --Indent;
+ }
+
+ CodeStatementCollection finallies = statement.FinallyStatements;
+ if (finallies.Count > 0) {
+
+ output.WriteLine ("Finally");
+ ++Indent;
+ GenerateStatements (finallies);
+ --Indent;
+ }
+
+ output.WriteLine("End Try");
+ }
+
+ protected override void GenerateAssignStatement (CodeAssignStatement statement)
+ {
+ TextWriter output = Output;
+ GenerateExpression (statement.Left);
+ output.Write (" = ");
+ GenerateExpression (statement.Right);
+ output.WriteLine ();
+ }
+
+ protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)
+ {
+ TextWriter output = Output;
+
+ Output.Write ("AddHandler ");
+ GenerateEventReferenceExpression (statement.Event);
+ Output.Write ( ", ");
+ GenerateExpression (statement.Listener);
+ output.WriteLine ();
+ }
+
+ protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)
+ {
+ TextWriter output = Output;
+
+ Output.Write ("RemoveHandler ");
+ GenerateEventReferenceExpression (statement.Event);
+ Output.Write ( ", ");
+ GenerateExpression (statement.Listener);
+ output.WriteLine ();
+ }
+
+ protected override void GenerateGotoStatement (CodeGotoStatement statement)
+ {
+ TextWriter output = Output;
+
+ output.Write ("goto ");
+ output.Write (statement.Label);
+ output.WriteLine ();
+ }
+
+ protected override void GenerateLabeledStatement (CodeLabeledStatement statement)
+ {
+ TextWriter output = Output;
+
+ Indent--;
+ output.WriteLine (statement.Label + ":");
+ Indent++;
+ if (statement.Statement != null) {
+ GenerateStatement (statement.Statement);
+ }
+ }
+
+ protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
+ {
+ TextWriter output = Output;
+
+ output.Write ("GetType(");
+ OutputType (e.Type);
+ output.Write (")");
+ }
+
+ protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )
+ {
+ TextWriter output = Output;
+
+ output.Write ("Dim ");
+ OutputTypeNamePair (statement.Type, statement.Name);
+
+ CodeExpression initExpression = statement.InitExpression;
+ if (initExpression != null)
+ {
+ output.Write (" = ");
+ GenerateExpression (initExpression);
+ }
+
+ output.WriteLine();
+ }
+
+ protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
+ {
+ Output.WriteLine ();
+ Output.Write ("#ExternalSource(");
+ Output.Write (linePragma.FileName);
+ Output.Write (", ");
+ Output.Write (linePragma.LineNumber);
+ Output.WriteLine (")");
+ }
+
+ protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
+ {
+ Output.WriteLine ("#End ExternalSource");
+ }
+
+ protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
+ {
+ if (IsCurrentDelegate || IsCurrentEnum) {
+ return;
+ }
+
+ TextWriter output = Output;
+
+ OutputAttributes (eventRef.CustomAttributes, null,
+ LineHandling.ContinueLine);
+
+ OutputMemberAccessModifier (eventRef.Attributes);
+
+ 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) {
+ output.Write (" Implements ");
+ OutputType (eventRef.PrivateImplementationType);
+ output.Write ('.');
+ output.Write (eventRef.Name);
+ }
+#endif
+
+ output.WriteLine ();
+ }
+
+ protected override void GenerateField (CodeMemberField field)
+ {
+ if (IsCurrentDelegate || IsCurrentInterface) {
+ return;
+ }
+
+ TextWriter output = Output;
+
+ OutputAttributes (field.CustomAttributes, null,
+ LineHandling.ContinueLine);
+
+ if (IsCurrentEnum) {
+ output.Write (field.Name);
+ } else {
+ MemberAttributes attributes = field.Attributes;
+ OutputMemberAccessModifier (attributes);
+ OutputVTableModifier (attributes);
+ OutputFieldScopeModifier (attributes);
+ OutputTypeNamePair (field.Type, field.Name);
+ }
+
+ CodeExpression initExpression = field.InitExpression;
+ if (initExpression != null) {
+ output.Write (" = ");
+ GenerateExpression (initExpression);
+ }
+
+ output.WriteLine();
+ }
+
+ protected override void GenerateSnippetMember (CodeSnippetTypeMember member)
+ {
+ Output.Write (member.Text);
+ }
+
+ protected override void GenerateEntryPointMethod (CodeEntryPointMethod method, CodeTypeDeclaration declaration)
+ {
+#if NET_2_0
+ OutputAttributes (method.CustomAttributes, null,
+ LineHandling.ContinueLine);
+#endif
+
+ Output.WriteLine ("Public Shared Sub Main()");
+ Indent++;
+ GenerateStatements (method.Statements);
+ Indent--;
+ Output.WriteLine ("End Sub");
+ }
+
+ [MonoTODO ("partially implemented")]
+ protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)
+ {
+ if (IsCurrentDelegate || IsCurrentEnum) {
+ return;
+ }
+
+ bool isSub = method.ReturnType.BaseType == typeof(void).FullName;
+
+ TextWriter output = Output;
+
+ OutputAttributes (method.CustomAttributes, null,
+ LineHandling.ContinueLine);
+
+ MemberAttributes attributes = method.Attributes;
+
+ if (!IsCurrentInterface) {
+ if (method.PrivateImplementationType == null) {
+ OutputMemberAccessModifier (attributes);
+ if (IsOverloaded (method, declaration)) {
+ output.Write ("Overloads ");
+ }
+ }
+ OutputVTableModifier (attributes);
+ OutputMemberScopeModifier (attributes);
+ } else {
+ OutputVTableModifier (attributes);
+ }
+
+ if (isSub)
+ output.Write ("Sub ");
+ else
+ output.Write ("Function ");
+
+ output.Write (GetMethodName(method));
+ output.Write ('(');
+ OutputParameters (method.Parameters);
+ output.Write (')');
+
+ if (!isSub) {
+ output.Write (" As ");
+ OutputAttributes (method.ReturnTypeCustomAttributes, null,
+ LineHandling.InLine);
+ OutputType (method.ReturnType);
+ }
+
+ if (method.ImplementationTypes.Count > 0) {
+ OutputImplementationTypes (method.ImplementationTypes, method.Name);
+ } else if (method.PrivateImplementationType != null) {
+ output.Write (" Implements ");
+ OutputType (method.PrivateImplementationType);
+ output.Write ('.');
+ output.Write (method.Name);
+ }
+
+ output.WriteLine ();
+ if (!IsCurrentInterface) {
+ if ((attributes & MemberAttributes.ScopeMask) != MemberAttributes.Abstract) {
+ ++Indent;
+ GenerateStatements (method.Statements);
+ --Indent;
+ if (isSub)
+ output.WriteLine ("End Sub");
+ else
+ output.WriteLine ("End Function");
+ }
+ }
+ }
+
+ protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)
+ {
+ if (IsCurrentDelegate || IsCurrentEnum) {
+ return;
+ }
+
+ TextWriter output = Output;
+
+ OutputAttributes (property.CustomAttributes, null,
+ LineHandling.ContinueLine);
+
+ MemberAttributes attributes = property.Attributes;
+
+ if (!IsCurrentInterface) {
+ if (property.PrivateImplementationType == null) {
+ OutputMemberAccessModifier (attributes);
+ if (IsOverloaded (property, declaration)) {
+ output.Write ("Overloads ");
+ }
+ }
+ OutputVTableModifier (attributes);
+ OutputMemberScopeModifier (attributes);
+ } else {
+ OutputVTableModifier (attributes);
+ }
+
+ // mark property as default property if we're dealing with an indexer
+ if (string.Compare (GetPropertyName(property), "Item", true, CultureInfo.InvariantCulture) == 0 && property.Parameters.Count > 0) {
+ output.Write ("Default ");
+ }
+
+ if (property.HasGet && (!property.HasSet))
+ output.Write ("ReadOnly " );
+
+ if (property.HasSet && (!property.HasGet))
+ output.Write ("WriteOnly " );
+
+ 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));
+
+ if (property.ImplementationTypes.Count > 0) {
+ OutputImplementationTypes (property.ImplementationTypes, property.Name);
+ } else if (property.PrivateImplementationType != null) {
+ output.Write (" Implements ");
+ OutputType (property.PrivateImplementationType);
+ output.Write ('.');
+ output.Write (property.Name);
+ }
+
+ output.WriteLine ();
+
+ if (!IsCurrentInterface) {
+ ++Indent;
+ if (property.HasGet) {
+ output.WriteLine ("Get");
+ ++Indent;
+ GenerateStatements (property.GetStatements);
+ --Indent;
+ output.WriteLine ("End Get");
+ }
+
+ if (property.HasSet) {
+ output.WriteLine ("Set");
+ ++Indent;
+ GenerateStatements (property.SetStatements);
+ --Indent;
+ output.WriteLine ("End Set");
+ }
+
+ --Indent;
+ output.WriteLine ("End Property");
+ }
+ }
+
+ protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
+ {
+ if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
+ return;
+ }
+
+ OutputAttributes (constructor.CustomAttributes, null,
+ LineHandling.ContinueLine);
+ OutputMemberAccessModifier (constructor.Attributes);
+ Output.Write ("Sub New(");
+ OutputParameters (constructor.Parameters);
+ Output.WriteLine (")");
+ Indent++;
+ // check if ctor passes args on to other ctor in class
+ CodeExpressionCollection ctorArgs = constructor.ChainedConstructorArgs;
+ if (ctorArgs.Count > 0) {
+ Output.Write ("Me.New(");
+ OutputExpressionList (ctorArgs);
+ Output.WriteLine (")");
+ } else {
+ // check if ctor passes args on to ctor in base class
+ ctorArgs = constructor.BaseConstructorArgs;
+ if (ctorArgs.Count > 0) {
+ 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()");
+ }
+ }
+ GenerateStatements (constructor.Statements);
+ Indent--;
+ Output.WriteLine ("End Sub");
+ }
+
+ protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
+ {
+ if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface) {
+ return;
+ }
+
+#if NET_2_0
+ OutputAttributes (constructor.CustomAttributes, null,
+ LineHandling.ContinueLine);
+#endif
+
+ Output.WriteLine ("Shared Sub New()");
+ Indent++;
+ GenerateStatements (constructor.Statements);
+ Indent--;
+ Output.WriteLine ("End Sub");
+ }
+
+ [MonoTODO ("not implemented")]
+ protected override void GenerateTypeStart (CodeTypeDeclaration declaration)
+ {
+ TextWriter output = Output;
+
+ OutputAttributes (declaration.CustomAttributes, null,
+ LineHandling.ContinueLine);
+
+ TypeAttributes attributes = declaration.TypeAttributes;
+
+ if (IsCurrentDelegate) {
+ CodeTypeDelegate delegateDecl = (CodeTypeDelegate) declaration;
+
+ if ((attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
+ output.Write ("Public ");
+ }
+
+ bool isSub = delegateDecl.ReturnType.BaseType == typeof (void).FullName;
+ if (isSub) {
+ output.Write ("Delegate Sub ");
+ } else {
+ output.Write ("Delegate Function ");
+ }
+
+ output.Write (delegateDecl.Name);
+ output.Write ("(");
+ Output.Write (")");
+ if (!isSub) {
+ Output.Write (" As ");
+ OutputType (delegateDecl.ReturnType);
+ }
+ Output.WriteLine ("");
+ } else {
+ OutputTypeAttributes (declaration);
+
+ output.Write (declaration.Name);
+
+ if (IsCurrentEnum) {
+ if (declaration.BaseTypes.Count > 0) {
+ output.Write (" As ");
+ OutputType (declaration.BaseTypes[0]);
+ }
+ output.WriteLine ();
+ ++Indent;
+ } else {
+ ++Indent;
+
+ bool firstInherits = true;
+ bool firstImplements = true;
+
+ for (int i = 0; i < declaration.BaseTypes.Count; i++) {
+ // a struct can only implement interfaces
+ // an interface can only inherit from other interface
+
+ CodeTypeReference typeRef = declaration.BaseTypes[i];
+
+ if (firstInherits && !declaration.IsStruct && !typeRef.IsInterface) {
+ output.WriteLine ();
+ output.Write ("Inherits ");
+ firstInherits = false;
+ } else if (!declaration.IsInterface && firstImplements) {
+ output.WriteLine ();
+ output.Write ("Implements ");
+ firstImplements = false;
+ } else {
+ output.Write (", ");
+ }
+ OutputType (typeRef);
+ }
+ output.WriteLine ();
+ }
+ }
+ }
+
+ protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)
+ {
+ if (IsCurrentDelegate) {
+ return;
+ }
+ string output = string.Empty;
+
+ --Indent;
+ if (declaration.IsStruct)
+ output = "End Structure";
+ if (declaration.IsInterface)
+ output = "End Interface";
+ if (declaration.IsEnum)
+ output = "End Enum";
+ if (declaration.IsClass)
+ output = "End Class";
+
+ Output.WriteLine (output);
+ }
+
+ protected override void GenerateNamespace(CodeNamespace ns)
+ {
+ GenerateNamespaceImports (ns);
+ Output.WriteLine ();
+ GenerateCommentStatements (ns.Comments);
+ GenerateNamespaceStart (ns);
+ GenerateTypes (ns);
+ GenerateNamespaceEnd (ns);
+ }
+
+
+ protected override void GenerateNamespaceStart (CodeNamespace ns)
+ {
+ TextWriter output = Output;
+
+ string name = ns.Name;
+ if (name != null && name != string.Empty) {
+ output.Write ("Namespace ");
+ output.WriteLine (name);
+ ++Indent;
+ }
+ }
+
+ protected override void GenerateNamespaceEnd (CodeNamespace ns)
+ {
+ string name = ns.Name;
+ if (name != null && name != string.Empty) {
+ --Indent;
+ Output.WriteLine ("End Namespace");
+ }
+ }
+
+ protected override void GenerateNamespaceImport (CodeNamespaceImport import)
+ {
+ TextWriter output = Output;
+
+ output.Write ("Imports ");
+ output.Write (import.Namespace);
+ output.WriteLine ();
+ }
+
+ protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
+ {
+ Output.Write ('<');
+ }
+
+ protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
+ {
+ Output.Write (">");
+ }
+
+ private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, LineHandling lineHandling) {
+ if (attributes.Count == 0) {
+ return;
+ }
+
+ GenerateAttributeDeclarationsStart (attributes);
+
+ IEnumerator enumerator = attributes.GetEnumerator ();
+ if (enumerator.MoveNext ()) {
+ CodeAttributeDeclaration att = (CodeAttributeDeclaration) enumerator.Current;
+ if (prefix != null) {
+ Output.Write (prefix);
+ }
+ OutputAttributeDeclaration (att);
+
+ while (enumerator.MoveNext ()) {
+ Output.Write (", ");
+ if (lineHandling != LineHandling.InLine) {
+ ContinueOnNewLine ("");
+ Output.Write (" ");
+ }
+ att = (CodeAttributeDeclaration) enumerator.Current;
+ if (prefix != null) {
+ Output.Write (prefix);
+ }
+ OutputAttributeDeclaration (att);
+ }
+ }
+ GenerateAttributeDeclarationsEnd (attributes);
+ Output.Write (" ");
+
+ switch (lineHandling) {
+ case LineHandling.ContinueLine:
+ ContinueOnNewLine ("");
+ break;
+ case LineHandling.NewLine:
+ Output.WriteLine ();
+ break;
+ }
+ }
+
+ protected override void OutputAttributeArgument (CodeAttributeArgument argument)
+ {
+ string name = argument.Name;
+ if (name != null && name.Length > 0) {
+ Output.Write (name);
+ Output.Write (":=");
+ }
+ GenerateExpression (argument.Value);
+ }
+
+ private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
+ {
+ Output.Write (attribute.Name.Replace ('+', '.'));
+ Output.Write ('(');
+ IEnumerator enumerator = attribute.Arguments.GetEnumerator ();
+ if (enumerator.MoveNext ()) {
+ CodeAttributeArgument argument = (CodeAttributeArgument) enumerator.Current;
+ OutputAttributeArgument (argument);
+
+ while (enumerator.MoveNext ()) {
+ Output.Write (", ");
+ argument = (CodeAttributeArgument) enumerator.Current;
+ OutputAttributeArgument (argument);
+ }
+ }
+ Output.Write (')');
+ }
+
+ protected override void OutputDirection (FieldDirection direction)
+ {
+ switch (direction) {
+ case FieldDirection.In:
+ Output.Write ("ByVal ");
+ break;
+ case FieldDirection.Out:
+ case FieldDirection.Ref:
+ Output.Write ("ByRef ");
+ break;
+ }
+ }
+
+ protected override void OutputFieldScopeModifier (MemberAttributes attributes)
+ {
+ switch (attributes & MemberAttributes.ScopeMask) {
+ case MemberAttributes.Static:
+ Output.Write ("Shared ");
+ break;
+ case MemberAttributes.Const:
+ Output.Write ("Const ");
+ break;
+ }
+ }
+
+ private void OutputImplementationTypes (CodeTypeReferenceCollection implementationTypes, string member)
+ {
+ IEnumerator enumerator = implementationTypes.GetEnumerator ();
+ if (enumerator.MoveNext ()) {
+ Output.Write (" Implements ");
+
+ CodeTypeReference typeReference = (CodeTypeReference) enumerator.Current;
+ OutputType (typeReference);
+ Output.Write ('.');
+ OutputIdentifier (member);
+
+ while (enumerator.MoveNext ()) {
+ Output.Write (" , ");
+ typeReference = (CodeTypeReference) enumerator.Current;
+ OutputType (typeReference);
+ Output.Write ('.');
+ OutputIdentifier (member);
+ }
+ }
+ }
+
+ 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;
+ }
+ }
+
+ private void OutputVTableModifier (MemberAttributes attributes)
+ {
+ 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 ");
+
+ 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;
+ }
+ }
+
+ protected override void OutputOperator (CodeBinaryOperatorType op)
+ {
+ switch (op) {
+ case CodeBinaryOperatorType.Add:
+ Output.Write ("+");
+ break;
+ case CodeBinaryOperatorType.Subtract:
+ Output.Write ("-");
+ break;
+ case CodeBinaryOperatorType.Multiply:
+ Output.Write ("*");
+ break;
+ case CodeBinaryOperatorType.Divide:
+ Output.Write ("/");
+ break;
+ case CodeBinaryOperatorType.Modulus:
+ Output.Write ("Mod");
+ break;
+ case CodeBinaryOperatorType.Assign:
+ Output.Write ("=");
+ break;
+ case CodeBinaryOperatorType.IdentityInequality:
+ Output.Write ("<>");
+ break;
+ case CodeBinaryOperatorType.IdentityEquality:
+ Output.Write ("Is");
+ break;
+ case CodeBinaryOperatorType.ValueEquality:
+ Output.Write ("=");
+ break;
+ case CodeBinaryOperatorType.BitwiseOr:
+ Output.Write ("Or");
+ break;
+ case CodeBinaryOperatorType.BitwiseAnd:
+ Output.Write ("And");
+ break;
+ case CodeBinaryOperatorType.BooleanOr:
+ Output.Write ("OrElse");
+ break;
+ case CodeBinaryOperatorType.BooleanAnd:
+ Output.Write ("AndAlso");
+ break;
+ case CodeBinaryOperatorType.LessThan:
+ Output.Write ("<");
+ break;
+ case CodeBinaryOperatorType.LessThanOrEqual:
+ Output.Write ("<=");
+ break;
+ case CodeBinaryOperatorType.GreaterThan:
+ Output.Write (">");
+ break;
+ case CodeBinaryOperatorType.GreaterThanOrEqual:
+ Output.Write (">=");
+ break;
+ }
+ }
+
+ private void OutputTypeAttributes (CodeTypeDeclaration declaration)
+ {
+ TextWriter output = Output;
+ TypeAttributes attributes = declaration.TypeAttributes;
+
+ 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
+ }
+
+ if (declaration.IsStruct) {
+ output.Write ("Structure ");
+ } else if (declaration.IsEnum) {
+ output.Write ("Enum ");
+ } else {
+ if ((attributes & TypeAttributes.Interface) != 0) {
+ output.Write ("Interface ");
+ } else {
+ if ((attributes & TypeAttributes.Sealed) != 0)
+ output.Write ("NotInheritable ");
+
+ if ((attributes & TypeAttributes.Abstract) != 0)
+ output.Write ("MustInherit ");
+
+ output.Write ("Class ");
+ }
+ }
+ }
+
+ protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)
+ {
+#if NET_2_0
+ if (name.Length == 0) {
+ name = "__exception";
+ }
+#endif
+ Output.Write (CreateEscapedIdentifier(name) + " As " + GetTypeOutput (typeRef));
+ }
+
+ protected override void OutputType (CodeTypeReference type)
+ {
+ Output.Write (GetTypeOutput (type));
+ }
+
+ protected override string QuoteSnippetString (string value)
+ {
+ StringBuilder mySBuilder = new StringBuilder(value.Length);
+ mySBuilder.Append ("\"");
+ bool inQuotes = true;
+ for (int MyCounter = 0; MyCounter < value.Length; MyCounter++)
+ {
+ if (value[MyCounter] == 34) //quotation mark
+ {
+ if (!inQuotes)
+ {
+ mySBuilder.Append ("&\"");
+ inQuotes = true;
+ }
+ mySBuilder.Append (value[MyCounter]);
+ mySBuilder.Append (value[MyCounter]);
+ }
+ else if (value[MyCounter] >= 32) //standard ansi/unicode characters
+ {
+ if (!inQuotes)
+ {
+ mySBuilder.Append ("&\"");
+ inQuotes = true;
+ }
+ mySBuilder.Append (value[MyCounter]);
+ }
+ else //special chars, e.g. line break
+ {
+ if (inQuotes)
+ {
+ mySBuilder.Append ("\"");
+ inQuotes = false;
+ }
+ mySBuilder.Append ("&Microsoft.VisualBasic.ChrW(");
+ mySBuilder.Append ((int)value[MyCounter]);
+ mySBuilder.Append (")");
+ }
+ }
+ if (inQuotes)
+ mySBuilder.Append ("\"");
+ return mySBuilder.ToString();
+ }
+
+ private void GenerateMemberReferenceExpression (CodeExpression targetObject, string memberName)
+ {
+ GenerateExpression (targetObject);
+ Output.Write ('.');
+ Output.Write (memberName);
+ }
+
+ /*
+ * ICodeGenerator
+ */
+
+ protected override string CreateEscapedIdentifier (string value)
+ {
+ for (int x = 0; x < Keywords.Length; x++)
+ if (value.ToLower().Equals (Keywords[x].ToLower()))
+ return "[" + value + "]";
+ return value;
+ }
+
+ protected override string CreateValidIdentifier (string value)
+ {
+ for (int x = 0; x < Keywords.Length; x++)
+ if (value.ToLower().Equals (Keywords[x].ToLower()))
+ return "_" + value;
+ return value;
+ }
+
+ protected override string GetTypeOutput (CodeTypeReference type)
+ {
+ string output;
+ CodeTypeReference arrayType;
+
+ arrayType = type.ArrayElementType;
+ if (arrayType != null)
+ output = GetTypeOutput (arrayType);
+ else {
+ switch (type.BaseType) {
+ case "System.DateTime":
+ output = "Date";
+ break;
+ case "System.Decimal":
+ output = "Decimal";
+ break;
+ case "System.Double":
+ output = "Double";
+ break;
+ case "System.Single":
+ output = "Single";
+ break;
+ case "System.Byte":
+ output = "Byte";
+ break;
+ case "System.Int32":
+ output = "Integer";
+ break;
+ case "System.Int64":
+ output = "Long";
+ break;
+ case "System.Int16":
+ output = "Short";
+ break;
+ case "System.Boolean":
+ output = "Boolean";
+ break;
+ case "System.Char":
+ output = "Char";
+ break;
+ case "System.String":
+ output = "String";
+ break;
+ case "System.Object":
+ output = "Object";
+ break;
+#if NET_2_0
+ case "System.SByte":
+ output = "SByte";
+ break;
+ case "System.UInt16":
+ output = "UShort";
+ break;
+ case "System.UInt32":
+ output = "UInteger";
+ break;
+ case "System.UInt64":
+ output = "ULong";
+ break;
+#endif
+ default:
+ output = type.BaseType.Replace('+', '.');
+ break;
+ }
+ }
+
+ int rank = type.ArrayRank;
+ if (rank > 0) {
+ output += "(";
+ for (--rank; rank > 0; --rank)
+ output += ",";
+ output += ")";
+ }
+
+ return output;
+ }
+
+ protected override bool IsValidIdentifier (string identifier)
+ {
+ for (int x = 0; x < Keywords.Length; x++)
+ if (identifier.ToLower().Equals (Keywords[x].ToLower()))
+ return false;
+ return true;
+ }
+
+ protected override bool Supports (GeneratorSupport supports)
+ {
+ return true;
+ }
+
+ private bool IsOverloaded (CodeMemberProperty property, CodeTypeDeclaration type)
+ {
+ if ((property.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
+ return true;
+ }
+
+ foreach (CodeTypeMember member in type.Members) {
+ CodeMemberProperty p = member as CodeMemberProperty;
+ if (p == null) {
+ // member is not a property
+ continue;
+ }
+
+ if (p != property && p.Name == property.Name && p.PrivateImplementationType == null) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private bool IsOverloaded (CodeMemberMethod method, CodeTypeDeclaration type)
+ {
+ if ((method.Attributes & MemberAttributes.Overloaded) == MemberAttributes.Overloaded) {
+ return true;
+ }
+
+ foreach (CodeTypeMember member in type.Members) {
+ CodeMemberMethod m = member as CodeMemberMethod;
+ if (m == null) {
+ // member is not a method
+ continue;
+ }
+
+ 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) {
+ 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) {
+ return method.Name;
+ }
+
+ string baseType = method.PrivateImplementationType.BaseType.Replace ('.', '_');
+ return baseType + "_" + method.Name;
+ }
+
+ private string GetPropertyName (CodeMemberProperty property)
+ {
+ if (property.PrivateImplementationType == null) {
+ return property.Name;
+ }
+
+ string baseType = property.PrivateImplementationType.BaseType.Replace ('.', '_');
+ return baseType + "_" + property.Name;
+ }
+
+ private enum LineHandling
+ {
+ InLine,
+ ContinueLine,
+ NewLine
+ }
+ }
+}