2 // Microsoft.VisualBasic.VBCodeGenerator.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 // (partially based on CSharpCodeGenerator)
8 // (C) 2003 Andreas Nahr
13 using System.Text.RegularExpressions;
15 using System.CodeDom.Compiler;
17 using System.Reflection;
18 using System.Collections;
20 namespace Microsoft.VisualBasic
22 internal class VBCodeGenerator : CodeCompiler
24 private string[] Keywords = new string[] {
25 "AddHandler", "AddressOf", "Alias", "And",
26 "AndAlso", "Ansi", "As", "Assembly",
27 "Auto", "Boolean", "ByRef", "Byte",
28 "ByVal", "Call", "Case", "Catch",
29 "CBool", "CByte", "CChar", "CDate",
30 "CDec", "CDbl", "Char", "CInt",
31 "Class", "CLng", "CObj", "Const",
32 "CShort", "CSng", "CStr", "CType",
33 "Date", "Decimal", "Declare", "Default",
34 "Delegate", "Dim", "DirectCast", "Do",
35 "Double", "Each", "Else", "ElseIf",
36 "End", "Enum", "Erase", "Error",
37 "Event", "Exit", "False", "Finally",
38 "For", "Friend", "Function", "Get",
39 "GetType", "GoSub", "GoTo", "Handles",
40 "If", "Implements", "Imports", "In",
41 "Inherits", "Integer", "Interface", "Is",
42 "Let", "Lib", "Like", "Long",
43 "Loop", "Me", "Mod", "Module",
44 "MustInherit", "MustOverride", "MyBase", "MyClass",
45 "Namespace", "New", "Next", "Not",
46 "Nothing", "NotInheritable", "NotOverridable", "Object",
47 "On", "Option", "Optional", "Or",
48 "OrElse", "Overloads", "Overridable", "Overrides",
49 "ParamArray", "Preserve", "Private", "Property",
50 "Protected", "Public", "RaiseEvent", "ReadOnly",
51 "ReDim", "REM", "RemoveHandler", "Resume",
52 "Return", "Select", "Set", "Shadows",
53 "Shared", "Short", "Single", "Static",
54 "Step", "Stop", "String", "Structure",
55 "Sub", "SyncLock", "Then", "Throw",
56 "To", "True", "Try", "TypeOf",
57 "Unicode", "Until", "Variant", "When",
58 "While", "With", "WithEvents", "WriteOnly",
62 public VBCodeGenerator()
66 protected override string NullToken {
72 protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression expression)
74 TextWriter output = Output;
76 output.Write ("New ");
78 CodeExpressionCollection initializers = expression.Initializers;
79 CodeTypeReference createType = expression.CreateType;
81 if (initializers.Count > 0) {
83 OutputType (createType);
85 output.WriteLine (" {");
87 OutputExpressionList (initializers, true);
93 CodeTypeReference arrayType = createType.ArrayElementType;
94 while (arrayType != null)
96 createType = arrayType;
97 arrayType = arrayType.ArrayElementType;
100 OutputType (createType);
104 CodeExpression size = expression.SizeExpression;
106 GenerateExpression (size);
108 output.Write (expression.Size);
114 protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression expression)
116 Output.Write ("MyBase");
119 protected override void GenerateCastExpression (CodeCastExpression expression)
121 TextWriter output = Output;
122 // ENHANCE: Use a DirectCast if it is known that expression.Expression is no Value-Type
123 output.Write ("CType(");
124 GenerateExpression (expression.Expression);
126 OutputType (expression.TargetType);
130 protected override void GenerateCompileUnitStart (CodeCompileUnit compileUnit)
132 GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
133 GenerateComment (new CodeComment (" <autogenerated>"));
134 GenerateComment (new CodeComment (" This code was generated by a tool."));
135 GenerateComment (new CodeComment (" Mono Runtime Version: " + System.Environment.Version));
136 GenerateComment (new CodeComment (""));
137 GenerateComment (new CodeComment (" Changes to this file may cause incorrect behavior and will be lost if "));
138 GenerateComment (new CodeComment (" the code is regenerated."));
139 GenerateComment (new CodeComment (" </autogenerated>"));
140 GenerateComment (new CodeComment ("------------------------------------------------------------------------------"));
144 protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
146 TextWriter output = Output;
148 output.Write ("AddressOf ");
150 CodeExpression targetObject = expression.TargetObject;
151 if (targetObject != null) {
152 GenerateExpression (targetObject);
155 output.Write (expression.MethodName);
158 protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression expression)
160 CodeExpression targetObject = expression.TargetObject;
161 if (targetObject != null) {
162 GenerateExpression (targetObject);
165 Output.Write (expression.FieldName);
168 protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression expression)
170 Output.Write (expression.ParameterName);
173 protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression expression)
175 Output.Write (expression.VariableName);
178 protected override void GenerateIndexerExpression (CodeIndexerExpression expression)
180 TextWriter output = Output;
182 GenerateExpression (expression.TargetObject);
184 OutputExpressionList (expression.Indices);
188 protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression expression)
190 TextWriter output = Output;
192 GenerateExpression (expression.TargetObject);
194 OutputExpressionList (expression.Indices);
198 protected override void GenerateSnippetExpression (CodeSnippetExpression expression)
200 Output.Write (expression.Value);
203 protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression expression)
205 TextWriter output = Output;
207 GenerateMethodReferenceExpression (expression.Method);
210 OutputExpressionList (expression.Parameters);
214 protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression expression)
216 GenerateExpression (expression.TargetObject);
218 Output.Write (expression.MethodName);
221 protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression expression)
223 GenerateExpression (expression.TargetObject);
225 Output.Write (expression.EventName);
228 protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
230 Output.Write ("RaiseEvent ");
231 GenerateExpression (expression.TargetObject);
233 OutputExpressionList (expression.Parameters);
234 Output.WriteLine (')');
237 protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression expression)
239 Output.Write( "New " );
240 OutputType (expression.CreateType);
242 OutputExpressionList (expression.Parameters);
246 protected override void GenerateParameterDeclarationExpression (CodeParameterDeclarationExpression e)
248 if (e.CustomAttributes != null && e.CustomAttributes.Count > 0)
249 OutputAttributeDeclarations (e.CustomAttributes);
250 OutputDirection (e.Direction);
251 OutputTypeNamePair (e.Type, e.Name);
254 protected override void GeneratePrimitiveExpression (CodePrimitiveExpression e)
256 TextWriter output = Output;
258 if (e.Value == null) {
259 output.Write (NullToken);
263 Type type = e.Value.GetType ();
264 if (type == typeof (bool)) {
266 output.Write ("True");
268 output.Write ("False");
270 else if (type == typeof (char)) {
271 output.Write ("\"" + e.Value.ToString () + "\"c");
273 else if (type == typeof (string)) {
274 output.Write (QuoteSnippetString ((string) e.Value));
276 else if (type == typeof (byte) || type == typeof (sbyte) || type == typeof (short) ||
277 type == typeof (int) || type == typeof (long) || type == typeof (float) ||
278 type == typeof (double) || type == typeof (decimal)) {
279 output.Write (e.Value.ToString ());
282 throw new ArgumentException ("Value type (" + type + ") is not a primitive type");
286 protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression expression)
288 GenerateMemberReferenceExpression (expression.TargetObject, expression.PropertyName);
291 protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression expression)
293 Output.Write ("Value");
296 protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression expression)
301 protected override void GenerateExpressionStatement (CodeExpressionStatement statement)
303 GenerateExpression (statement.Expression);
304 Output.WriteLine (); //start new line
307 protected override void GenerateIterationStatement (CodeIterationStatement statement)
309 TextWriter output = Output;
311 GenerateStatement (statement.InitStatement);
312 output.Write ("Do While ");
313 GenerateExpression (statement.TestExpression);
315 GenerateStatements (statement.Statements);
316 GenerateStatement (statement.IncrementStatement);
317 output.WriteLine ("Loop");
320 protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement statement)
322 Output.Write ("Throw ");
323 GenerateExpression (statement.ToThrow);
326 protected override void GenerateComment (CodeComment comment)
328 TextWriter output = Output;
330 if (comment.DocComment)
331 output.Write ("''' ");
335 output.WriteLine (comment.Text);
338 protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement statement)
340 TextWriter output = Output;
342 output.Write ("Return ");
343 GenerateExpression (statement.Expression);
347 protected override void GenerateConditionStatement (CodeConditionStatement statement)
349 TextWriter output = Output;
350 output.Write ("If (");
352 GenerateExpression (statement.Condition);
354 output.WriteLine (") Then");
356 GenerateStatements (statement.TrueStatements);
359 CodeStatementCollection falses = statement.FalseStatements;
360 if (falses.Count > 0) {
361 output.WriteLine ("Else");
363 GenerateStatements (falses);
367 if (Options.ElseOnClosing)
368 output.WriteLine ("Else");
370 output.WriteLine ("End If");
373 protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement statement)
375 TextWriter output = Output;
376 CodeGeneratorOptions options = Options;
378 output.WriteLine ("Try");
380 GenerateStatements (statement.TryStatements);
384 foreach (CodeCatchClause clause in statement.CatchClauses) {
385 output.Write ("Catch ");
386 OutputTypeNamePair (clause.CatchExceptionType, clause.LocalName);
389 GenerateStatements (clause.Statements);
394 CodeStatementCollection finallies = statement.FinallyStatements;
395 if (finallies.Count > 0) {
397 output.WriteLine ("Finally");
399 GenerateStatements (finallies);
404 if (Options.ElseOnClosing) {
405 if (statement.CatchClauses.Count == 0)
406 output.WriteLine ("Catch");
407 if (statement.FinallyStatements.Count == 0)
408 output.WriteLine ("Finally");
411 output.WriteLine("End Try");
414 protected override void GenerateAssignStatement (CodeAssignStatement statement)
416 TextWriter output = Output;
417 GenerateExpression (statement.Left);
418 output.Write (" = ");
419 GenerateExpression (statement.Right);
423 protected override void GenerateAttachEventStatement (CodeAttachEventStatement statement)
425 TextWriter output = Output;
427 Output.Write ("AddHandler ");
428 GenerateEventReferenceExpression (statement.Event);
429 Output.Write ( ", ");
430 GenerateExpression (statement.Listener);
434 protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement statement)
436 TextWriter output = Output;
438 Output.Write ("RemoveHandler ");
439 GenerateEventReferenceExpression (statement.Event);
440 Output.Write ( ", ");
441 GenerateExpression (statement.Listener);
445 protected override void GenerateGotoStatement (CodeGotoStatement statement)
447 TextWriter output = Output;
449 output.Write ("Goto ");
450 output.Write (statement.Label);
454 protected override void GenerateLabeledStatement (CodeLabeledStatement statement)
456 TextWriter output = Output;
458 output.Write (statement.Label + ":");
459 GenerateStatement (statement.Statement);
462 protected override void GenerateTypeOfExpression (CodeTypeOfExpression e)
464 TextWriter output = Output;
466 output.Write ("GetType(");
471 protected override void GenerateVariableDeclarationStatement( CodeVariableDeclarationStatement statement )
473 TextWriter output = Output;
475 output.Write ("Dim ");
476 OutputTypeNamePair (statement.Type, statement.Name);
478 CodeExpression initExpression = statement.InitExpression;
479 if (initExpression != null)
481 output.Write (" = ");
482 GenerateExpression (initExpression);
488 protected override void GenerateLinePragmaStart (CodeLinePragma linePragma)
491 Output.Write ("#ExternalSource(");
492 Output.Write (linePragma.FileName);
494 Output.Write (linePragma.LineNumber);
495 Output.WriteLine (")");
498 protected override void GenerateLinePragmaEnd (CodeLinePragma linePragma)
500 Output.WriteLine ("#End ExternalSource");
503 protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
505 TextWriter output = Output;
507 if (eventRef.CustomAttributes.Count > 0)
508 OutputAttributeDeclarations (eventRef.CustomAttributes);
510 MemberAttributes attributes = eventRef.Attributes;
512 OutputMemberAccessModifier (attributes);
513 OutputMemberScopeModifier (attributes);
515 output.Write ("Event ");
516 OutputTypeNamePair (eventRef.Type, eventRef.Name);
520 protected override void GenerateField (CodeMemberField field)
522 TextWriter output = Output;
524 if (field.CustomAttributes.Count > 0)
525 OutputAttributeDeclarations (field.CustomAttributes);
527 MemberAttributes attributes = field.Attributes;
528 OutputMemberAccessModifier (attributes);
529 OutputFieldScopeModifier (attributes);
531 OutputTypeNamePair (field.Type, field.Name);
533 CodeExpression initExpression = field.InitExpression;
534 if (initExpression != null) {
535 output.Write (" = ");
536 GenerateExpression (initExpression);
542 protected override void GenerateSnippetMember (CodeSnippetTypeMember member)
544 Output.Write (member.Text);
547 protected override void GenerateEntryPointMethod( CodeEntryPointMethod method, CodeTypeDeclaration declaration )
549 method.Name = "Main";
550 GenerateMethod (method, declaration);
553 [MonoTODO ("partially implemented")]
554 protected override void GenerateMethod (CodeMemberMethod method, CodeTypeDeclaration declaration)
556 TextWriter output = Output;
558 if (method.CustomAttributes.Count > 0)
559 OutputAttributeDeclarations (method.CustomAttributes);
561 MemberAttributes attributes = method.Attributes;
563 OutputMemberAccessModifier (attributes);
564 OutputMemberScopeModifier (attributes);
566 if (method.ReturnType == null)
567 output.Write ("Sub ");
569 output.Write ("Function ");
571 output.Write (method.Name);
573 OutputParameters (method.Parameters);
576 if (method.ReturnType != null) {
577 output.Write (" As ");
578 OutputType (method.ReturnType);
581 if (method.ImplementationTypes.Count > 0) {
582 output.Write (" Implements ");
583 foreach (CodeTypeReference type in method.ImplementationTypes)
587 // TODO implementation incomplete
592 // TODO private implementations
594 if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract)
599 GenerateStatements (method.Statements);
601 if (method.ReturnType == null)
602 output.WriteLine ("End Sub");
604 output.WriteLine ("End Function");
608 protected override void GenerateProperty (CodeMemberProperty property, CodeTypeDeclaration declaration)
610 TextWriter output = Output;
612 if (property.CustomAttributes.Count > 0)
613 OutputAttributeDeclarations (property.CustomAttributes);
615 MemberAttributes attributes = property.Attributes;
616 OutputMemberAccessModifier (attributes);
617 OutputMemberScopeModifier (attributes);
619 if (property.HasGet && (property.HasSet = false))
620 output.Write ("ReadOnly" );
622 if (property.HasSet && (property.HasGet = false))
623 output.Write ("WriteOnly" );
625 output.Write ("Property" );
627 OutputTypeNamePair (property.Type, property.Name);
631 if (property.HasGet) {
632 output.WriteLine ("Get");
635 GenerateStatements (property.GetStatements);
638 output.WriteLine ("End Get");
641 if (property.HasSet) {
642 output.WriteLine ("Set");
645 GenerateStatements (property.SetStatements);
648 output.WriteLine ("End Set");
652 output.WriteLine ("End Property");
655 [MonoTODO ("not implemented")]
656 protected override void GenerateConstructor (CodeConstructor constructor, CodeTypeDeclaration declaration)
658 if (constructor.CustomAttributes.Count > 0)
659 OutputAttributeDeclarations (constructor.CustomAttributes);
660 OutputMemberAccessModifier (constructor.Attributes);
661 Output.Write ("Sub New(");
662 OutputParameters (constructor.Parameters);
663 Output.WriteLine (")");
664 // Handle BaseConstructorArgs, ChainedConstructorArgs, ImplementationTypes
666 GenerateStatements (constructor.Statements);
668 Output.WriteLine ("End Sub");
671 protected override void GenerateTypeConstructor (CodeTypeConstructor constructor)
673 Output.WriteLine ("Shared Sub New()");
675 GenerateStatements (constructor.Statements);
677 Output.WriteLine ("End Sub");
680 [MonoTODO ("not implemented")]
681 protected override void GenerateTypeStart (CodeTypeDeclaration declaration)
683 TextWriter output = Output;
685 if (declaration.CustomAttributes.Count > 0)
686 OutputAttributeDeclarations (declaration.CustomAttributes);
687 TypeAttributes attributes = declaration.TypeAttributes;
688 OutputTypeAttributes (attributes,
689 declaration.IsStruct,
692 output.WriteLine (declaration.Name);
696 IEnumerator enumerator = declaration.BaseTypes.GetEnumerator();
697 if (enumerator.MoveNext())
699 CodeTypeReference type = (CodeTypeReference)enumerator.Current;
703 output.Write ("Inherits ");
708 while (enumerator.MoveNext())
710 type = (CodeTypeReference)enumerator.Current;
714 output.Write ("Implements ");
722 protected override void GenerateTypeEnd (CodeTypeDeclaration declaration)
724 string output = string.Empty;
727 if (declaration.IsStruct)
728 output = "End Structure";
729 if (declaration.IsInterface)
730 output = "End Interface";
731 if (declaration.IsEnum)
733 if (declaration.IsClass)
734 output = "End Class";
736 Output.WriteLine (output);
739 protected override void GenerateNamespaceStart (CodeNamespace ns)
741 TextWriter output = Output;
743 string name = ns.Name;
744 if (name != null && name != string.Empty) {
745 output.Write ("Namespace ");
746 output.WriteLine (name);
751 protected override void GenerateNamespaceEnd (CodeNamespace ns)
753 string name = ns.Name;
754 if (name != null && name != string.Empty) {
756 Output.WriteLine ("End Namespace");
760 protected override void GenerateNamespaceImport (CodeNamespaceImport import)
762 TextWriter output = Output;
764 output.Write ("Imports ");
765 output.Write (import.Namespace);
769 protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
774 protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
776 Output.WriteLine ('>');
779 protected override void OutputDirection (FieldDirection direction)
782 case FieldDirection.In:
785 case FieldDirection.Out:
786 Output.Write ("ByVal ");
788 case FieldDirection.Ref:
789 Output.Write ("ByRef ");
794 protected override void OutputFieldScopeModifier (MemberAttributes attributes)
796 if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
797 Output.Write ("New ");
799 switch (attributes & MemberAttributes.ScopeMask) {
800 case MemberAttributes.Static:
801 Output.Write ("Shared ");
803 case MemberAttributes.Const:
804 Output.Write ("Const ");
809 protected override void OutputMemberAccessModifier (MemberAttributes attributes)
811 switch (attributes & MemberAttributes.AccessMask) {
812 case MemberAttributes.Assembly:
813 Output.Write ("Friend ");
815 case MemberAttributes.FamilyAndAssembly:
816 Output.Write ("Friend ");
818 case MemberAttributes.Family:
819 Output.Write ("Protected ");
821 case MemberAttributes.FamilyOrAssembly:
822 Output.Write ("Protected Friend ");
824 case MemberAttributes.Private:
825 Output.Write ("Private ");
827 case MemberAttributes.Public:
828 Output.Write ("Public ");
833 protected override void OutputMemberScopeModifier (MemberAttributes attributes)
835 if ((attributes & MemberAttributes.VTableMask) == MemberAttributes.New)
836 Output.Write ("New ");
838 switch (attributes & MemberAttributes.ScopeMask) {
839 case MemberAttributes.Abstract:
840 Output.Write ("MustOverride ");
842 case MemberAttributes.Final:
843 Output.Write ("NotOverridable ");
845 case MemberAttributes.Static:
846 Output.Write ("Shared ");
848 case MemberAttributes.Override:
849 Output.Write ("Overrides ");
853 // FUNNY! if the scope value is
854 // rubbish (0 or >Const), and access
855 // is public or protected, make it
858 // i'm not sure whether this is 100%
859 // correct, but it seems to be MS
862 MemberAttributes access = attributes & MemberAttributes.AccessMask;
863 if ( access == MemberAttributes.Public ||
864 access == MemberAttributes.Family )
865 Output.Write ("Overridable ");
870 protected override void OutputOperator (CodeBinaryOperatorType op)
873 case CodeBinaryOperatorType.Add:
876 case CodeBinaryOperatorType.Subtract:
879 case CodeBinaryOperatorType.Multiply:
882 case CodeBinaryOperatorType.Divide:
885 case CodeBinaryOperatorType.Modulus:
886 Output.Write ("Mod");
888 case CodeBinaryOperatorType.Assign:
891 case CodeBinaryOperatorType.IdentityInequality:
894 case CodeBinaryOperatorType.IdentityEquality:
897 case CodeBinaryOperatorType.ValueEquality:
900 case CodeBinaryOperatorType.BitwiseOr:
903 case CodeBinaryOperatorType.BitwiseAnd:
904 Output.Write ("And");
906 case CodeBinaryOperatorType.BooleanOr:
907 Output.Write ("OrElse");
909 case CodeBinaryOperatorType.BooleanAnd:
910 Output.Write ("AndAlso");
912 case CodeBinaryOperatorType.LessThan:
915 case CodeBinaryOperatorType.LessThanOrEqual:
918 case CodeBinaryOperatorType.GreaterThan:
921 case CodeBinaryOperatorType.GreaterThanOrEqual:
927 protected override void OutputTypeAttributes (TypeAttributes attributes, bool isStruct, bool isEnum)
929 TextWriter output = Output;
931 switch (attributes & TypeAttributes.VisibilityMask) {
932 case TypeAttributes.NotPublic:
933 // Does this mean friend access?
934 output.Write ("Friend ");
937 case TypeAttributes.Public:
938 case TypeAttributes.NestedPublic:
939 output.Write ("Public ");
942 case TypeAttributes.NestedPrivate:
943 output.Write ("Private ");
945 case TypeAttributes.NestedAssembly:
946 output.Write ("Friend ");
948 case TypeAttributes.NestedFamily:
949 output.Write ("Protected ");
951 case TypeAttributes.NestedFamORAssem:
952 output.Write ("Protected Friend");
954 case TypeAttributes.NestedFamANDAssem:
955 output.Write ("Friend ");
960 output.Write ("Structure ");
963 output.Write ("Enumeration ");
966 if ((attributes & TypeAttributes.Interface) != 0)
967 output.Write ("Interface ");
970 if ((attributes & TypeAttributes.Sealed) != 0)
971 output.Write ("NotInheritable ");
973 if ((attributes & TypeAttributes.Abstract) != 0)
974 output.Write ("MustInherit ");
976 output.Write ("Class ");
981 protected override void OutputTypeNamePair (CodeTypeReference typeRef, String name)
983 Output.Write (name + " As " + GetTypeOutput (typeRef));
986 protected override void OutputType (CodeTypeReference type)
988 Output.Write (GetTypeOutput (type));
991 [MonoTODO ("not implemented")]
992 protected override string QuoteSnippetString (string value)
994 // FIXME: escape ASCII chars that are not code compatible (e.g. vbcrlf)
998 private void GenerateDeclaration (CodeTypeReference type, string name, CodeExpression initExpression)
1000 TextWriter output = Output;
1002 OutputTypeNamePair (type, name);
1004 if (initExpression != null) {
1005 output.Write (" = ");
1006 GenerateExpression (initExpression);
1009 output.WriteLine ();
1012 private void GenerateMemberReferenceExpression (CodeExpression targetObject, string memberName)
1014 GenerateExpression (targetObject);
1016 Output.Write (memberName);
1023 protected override string CreateEscapedIdentifier (string value)
1025 for (int x = 0; x < Keywords.Length; x++)
1026 if (value.ToLower().Equals (Keywords[x].ToLower()))
1027 return "[" + value + "]";
1031 protected override string CreateValidIdentifier (string value)
1033 for (int x = 0; x < Keywords.Length; x++)
1034 if (value.ToLower().Equals (Keywords[x].ToLower()))
1039 protected override string GetTypeOutput (CodeTypeReference type)
1042 CodeTypeReference arrayType;
1044 arrayType = type.ArrayElementType;
1045 if (arrayType != null)
1046 output = GetTypeOutput (arrayType);
1048 switch (type.BaseType) {
1050 case "System.Decimal":
1053 case "System.Double":
1056 case "System.Single":
1063 case "System.SByte":
1066 case "System.Int32":
1069 case "System.UInt32":
1072 case "System.Int64":
1075 case "System.UInt64":
1078 case "System.Int16":
1081 case "System.UInt16":
1085 case "System.Boolean":
1093 case "System.String":
1096 case "System.Object":
1105 output = type.BaseType;
1110 int rank = type.ArrayRank;
1113 for (--rank; rank > 0; --rank)
1121 protected override bool IsValidIdentifier (string identifier)
1123 for (int x = 0; x < Keywords.Length; x++)
1124 if (identifier.ToLower().Equals (Keywords[x].ToLower()))
1129 protected override bool Supports (GeneratorSupport supports)
1134 protected override string CompilerName {
1140 protected override string FileExtension {
1146 [MonoTODO ("check")]
1147 protected override string CmdArgsFromParameters (CompilerParameters options)
1149 //FIXME correct this agains mbas parameters if parameters are complete
1150 StringBuilder args = new StringBuilder();
1151 if (options.GenerateExecutable)
1152 args.Append ("/target:exe ");
1154 args.Append ("/target:library ");
1156 if (options.IncludeDebugInformation)
1157 args.Append ("/debug ");
1159 if (options.TreatWarningsAsErrors)
1160 args.Append ("/warnaserror ");
1161 args.AppendFormat ("/warn:{0} ", options.WarningLevel);
1162 if (options.OutputAssembly == null)
1163 options.OutputAssembly = Path.ChangeExtension (Path.GetTempFileName(), "dll");
1165 args.AppendFormat ("/out:'{0}' ", options.OutputAssembly);
1167 if (null != options.ReferencedAssemblies) {
1168 foreach (string import in options.ReferencedAssemblies)
1169 args.AppendFormat ("/r:'{0}' ", import);
1172 if (options.Win32Resource != null)
1173 args.AppendFormat ("/Win32Resource:{0} ", options.Win32Resource);
1175 foreach (string source in options.TempFiles)
1176 args.AppendFormat ("'{0}' ", source);
1178 return args.ToString();
1181 [MonoTODO ("check")]
1182 protected override void ProcessCompilerOutputLine (CompilerResults results, string line)
1184 CompilerError error = new CompilerError();
1185 //FIXME check this Regex against mbas compiler if the output there is complete
1186 Regex reg = new Regex (@"^(\s*(?<file>.*)\((?<line>\d*)(,(?<column>\d*))?\)\s+)*(?<level>\w+)\s*(?<number>.*):\s(?<message>.*)",
1187 RegexOptions.Compiled | RegexOptions.ExplicitCapture);
1188 Match match = reg.Match (line);
1189 if (!match.Success) {
1192 if (String.Empty != match.Result ("${file}"))
1193 error.FileName = match.Result ("${file}");
1194 if (String.Empty != match.Result ("${line}"))
1195 error.Line = Int32.Parse (match.Result ("${line}"));
1196 if (String.Empty != match.Result ("${column}"))
1197 error.Column = Int32.Parse (match.Result ("${column}"));
1198 if (match.Result ("${level}") == "warning")
1199 error.IsWarning=true;
1200 error.ErrorNumber = match.Result ("${number}");
1201 error.ErrorText = match.Result ("${message}");
1203 results.Errors.Add (error);