{
internal class VBCodeGenerator : CodeGenerator
{
-#if NET_2_0
private string [] Keywords = new string [] {
"AddHandler", "AddressOf", "Alias", "And",
"AndAlso", "Ansi", "As", "Assembly",
"While", "With", "WithEvents", "WriteOnly",
"Xor"
};
-#else
- 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"
- };
-#endif
+
+ private CodeAttributeDeclarationCollection assemblyCustomAttributes;
public VBCodeGenerator()
{
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);
}
{
CodeEventReferenceExpression ev = expression.TargetObject as CodeEventReferenceExpression;
-#if ONLY_1_1
- Output.Write ("RaiseEvent ");
-#endif
if (ev != null) {
-#if NET_2_0
Output.Write ("RaiseEvent ");
-#endif
if (ev.TargetObject != null && !(ev.TargetObject is CodeThisReferenceExpression)) {
GenerateExpression (ev.TargetObject);
Output.Write (".");
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 ("CSByte(");
Output.Write (sb.ToString(CultureInfo.InvariantCulture));
Output.Write (')');
-#else
- Output.Write("Microsoft.VisualBasic.ChrW(" + ch.ToString(CultureInfo.InvariantCulture) + ")");
-#endif
} else {
base.GeneratePrimitiveExpression(e);
}
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 ('.');
output.Write (eventRef.Name);
}
-#endif
output.WriteLine ();
}
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++;
output.Write ("Function ");
output.Write (GetMethodName(method));
-
-#if NET_2_0
OutputTypeParameters (method.TypeParameters);
-#endif
-
output.Write ('(');
OutputParameters (method.Parameters);
output.Write (')');
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));
++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;
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");
}
if (IsCurrentDelegate || IsCurrentEnum || IsCurrentInterface)
return;
-#if NET_2_0
OutputAttributes (constructor.CustomAttributes, null,
LineHandling.ContinueLine);
-#endif
Output.WriteLine ("Shared Sub New()");
Indent++;
}
output.Write (CreateEscapedIdentifier (delegateDecl.Name));
-#if NET_2_0
OutputTypeParameters (delegateDecl.TypeParameters);
-#endif
output.Write ("(");
OutputParameters (delegateDecl.Parameters);
Output.Write (")");
} else {
OutputTypeAttributes (declaration);
output.Write (CreateEscapedIdentifier (declaration.Name));
-#if NET_2_0
OutputTypeParameters (declaration.TypeParameters);
-#endif
if (IsCurrentEnum) {
if (declaration.BaseTypes.Count > 0) {
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);
//
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;
}
TextWriter output = Output;
TypeAttributes attributes = declaration.TypeAttributes;
-#if NET_2_0
if (declaration.IsPartial)
output.Write ("Partial ");
-#endif
switch (attributes & TypeAttributes.VisibilityMask) {
case TypeAttributes.Public:
case TypeAttributes.NestedPrivate:
output.Write ("Private ");
break;
-#if NET_2_0
case TypeAttributes.NotPublic:
case TypeAttributes.NestedFamANDAssem:
case TypeAttributes.NestedAssembly:
case TypeAttributes.NestedFamORAssem:
output.Write ("Protected Friend ");
break;
-#endif
}
if (declaration.IsStruct) {
}
}
-#if NET_2_0
void OutputTypeParameters (CodeTypeParameterCollection parameters)
{
int count = parameters.Count;
if (constraint_count > 1)
Output.Write ("}");
}
-#endif
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));
}
case "System.Object":
output = "Object";
break;
-#if NET_2_0
case "System.SByte":
output = "SByte";
break;
case "System.UInt64":
output = "ULong";
break;
-#endif
default:
output = type.BaseType.Replace('+', '.');
output = CreateEscapedIdentifier (output);
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)
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,