2008-08-07 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System / Microsoft.CSharp / CSharpCodeGenerator.cs
index 628f79da6b2e9502c84359050dc4e8b742d9f34b..b91480a1fa30872ad9b7ad12933ea67de8c13527 100644 (file)
@@ -40,9 +40,17 @@ namespace Mono.CSharp
        using System.Collections;
        using System.Text;
 
+#if NET_2_0
+       using System.Collections.Generic;
+#endif
+       
        internal class CSharpCodeGenerator
                : CodeGenerator
        {
+#if NET_2_0
+               IDictionary <string, string> providerOptions;
+#endif
+               
                // It is used for beautiful "for" syntax
                bool dont_write_semicolon;
 
@@ -54,6 +62,17 @@ namespace Mono.CSharp
                        dont_write_semicolon = false;
                }
 
+#if NET_2_0
+               public CSharpCodeGenerator (IDictionary <string, string> providerOptions)
+               {
+                       this.providerOptions = providerOptions;
+               }
+
+               protected IDictionary <string, string> ProviderOptions {
+                       get { return providerOptions; }
+               }
+#endif
+               
                //
                // Properties
                //
@@ -270,7 +289,8 @@ namespace Mono.CSharp
 
                protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression expression)
                {
-                       GenerateExpression (expression.TargetObject);
+                       if (expression.TargetObject != null)
+                               GenerateExpression (expression.TargetObject);
                        Output.Write ('(');
                        OutputExpressionList (expression.Parameters);
                        Output.Write (')');
@@ -541,6 +561,9 @@ namespace Mono.CSharp
                {
                        Output.WriteLine ();
                        Output.WriteLine ("#line default");
+#if NET_2_0
+                       Output.WriteLine ("#line hidden");
+#endif
                }
 
                protected override void GenerateEvent (CodeMemberEvent eventRef, CodeTypeDeclaration declaration)
@@ -674,7 +697,7 @@ namespace Mono.CSharp
                        GenerateGenericsConstraints (method.TypeParameters);
 #endif
 
-                       if ((attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract || declaration.IsInterface)
+                       if (IsAbstract (attributes) || declaration.IsInterface)
                                output.WriteLine (';');
                        else {
                                OutputStartBrace ();
@@ -685,6 +708,11 @@ namespace Mono.CSharp
                        }
                }
 
+               static bool IsAbstract (MemberAttributes attributes)
+               {
+                       return (attributes & MemberAttributes.ScopeMask) == MemberAttributes.Abstract;
+               }
+
                protected override void GenerateProperty (CodeMemberProperty property,
                                                          CodeTypeDeclaration declaration)
                {
@@ -723,12 +751,12 @@ namespace Mono.CSharp
                                OutputParameters(property.Parameters);
                                output.Write(']');
                        } else {
-                               output.Write (property.Name);
+                               output.Write (GetSafeName (property.Name));
                        }
                        OutputStartBrace ();
                        ++Indent;
 
-                       if (declaration.IsInterface)
+                       if (declaration.IsInterface || IsAbstract (property.Attributes))
                        {
                                if (property.HasGet) output.WriteLine("get;");
                                if (property.HasSet) output.WriteLine("set;");
@@ -929,7 +957,18 @@ namespace Mono.CSharp
 
                private void OutputAttributes (CodeAttributeDeclarationCollection attributes, string prefix, bool inline)
                {
+#if NET_2_0
+                       bool params_set = false;
+#endif
+
                        foreach (CodeAttributeDeclaration att in attributes) {
+#if NET_2_0
+                               if (att.Name == "System.ParamArrayAttribute") {
+                                       params_set = true;
+                                       continue;
+                               }
+#endif
+
                                GenerateAttributeDeclarationsStart (attributes);
                                if (prefix != null) {
                                        Output.Write (prefix);
@@ -942,6 +981,18 @@ namespace Mono.CSharp
                                        Output.WriteLine ();
                                }
                        }
+
+#if NET_2_0
+                       if (params_set) {
+                               if (prefix != null)
+                                       Output.Write (prefix);
+                               Output.Write ("params");
+                               if (inline)
+                                       Output.Write (" ");
+                               else
+                                       Output.WriteLine ();
+                       }
+#endif
                }
 
                private void OutputAttributeDeclaration (CodeAttributeDeclaration attribute)
@@ -1342,11 +1393,14 @@ namespace Mono.CSharp
                                                                // skip ` character
                                                                i++;
                                                                // determine number of type arguments to output
-                                                               int typeArgCount = baseType[i] - '0';
+                                                               int end = i;
+                                                               while (end < baseType.Length && Char.IsDigit (baseType [end]))
+                                                                       end++;
+                                                               int typeArgCount = Int32.Parse (baseType.Substring (i, end - i));
                                                                // output type arguments
                                                                OutputTypeArguments (type.TypeArguments, sb, typeArgCount);
                                                                // skip type argument indicator
-                                                               i++;
+                                                               i = end;
                                                                // if next character is . or +, then append .
                                                                if ((i < baseType.Length) && ((baseType[i] == '+') || (baseType[i] == '.'))) {
                                                                        sb.Append ('.');
@@ -1484,31 +1538,42 @@ namespace Mono.CSharp
                        if (count == 0)
                                return;
 
-                       ++Indent;
-                       foreach (CodeTypeParameter p in parameters) {
-                               if (p.Constraints.Count == 0)
-                                       continue;
+                       bool indented = false;
+                       
+                       for (int i = 0; i < count; i++) {
+                               CodeTypeParameter p = parameters [i];
+                               bool hasConstraints = (p.Constraints.Count != 0);
                                Output.WriteLine ();
+                               if (!hasConstraints && !p.HasConstructorConstraint)
+                                       continue;
+
+                               if (!indented) {
+                                       ++Indent;
+                                       indented = true;
+                               }
+
                                Output.Write ("where ");
                                Output.Write (p.Name);
                                Output.Write (" : ");
 
-                               bool is_first = true;
-                               foreach (CodeTypeReference r in p.Constraints) {
-                                       if (is_first)
-                                               is_first = false;
-                                       else
+                               for (int j = 0; j < p.Constraints.Count; j++) {
+                                       if (j > 0)
                                                Output.Write (", ");
-                                       OutputType (r);
+                                       OutputType (p.Constraints [j]);
                                }
+
                                if (p.HasConstructorConstraint) {
-                                       if (!is_first)
+                                       if (hasConstraints)
                                                Output.Write (", ");
-                                       Output.Write ("new ()");
+                                       Output.Write ("new");
+                                       if (hasConstraints)
+                                               Output.Write (" ");
+                                       Output.Write ("()");
                                }
-
                        }
-                       --Indent;
+
+                       if (indented)
+                               --Indent;
                }
 
                string GetTypeArguments (CodeTypeReferenceCollection collection)