2008-08-07 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System / Microsoft.CSharp / CSharpCodeGenerator.cs
index a638077aeb9fe802d30f00f94855232fe99376bc..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)
@@ -1244,6 +1295,11 @@ namespace Mono.CSharp
     
                protected override string GetTypeOutput (CodeTypeReference type)
                {
+#if NET_2_0
+                       if ((type.Options & CodeTypeReferenceOptions.GenericTypeParameter) != 0)
+                               return type.BaseType;
+#endif
+
                        string typeOutput = null;
 
                        if (type.ArrayElementType != null) {
@@ -1260,6 +1316,7 @@ namespace Mono.CSharp
                                }
                                typeOutput += ']';
                        }
+
                        return typeOutput;
                }
 
@@ -1336,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 ('.');
@@ -1376,12 +1436,36 @@ namespace Mono.CSharp
                        return typeOutput;
                }
 
+               static bool is_identifier_start_character (char c)
+                {
+                        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '@' || Char.IsLetter (c);
+                }
+
+                static bool is_identifier_part_character (char c)
+                {
+                        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c)
+;
+                }
+               
                protected override bool IsValidIdentifier (string identifier)
                {
+                       if (identifier == null || identifier.Length == 0)
+                               return false;
+                       
                        if (keywordsTable == null)
                                FillKeywordTable ();
 
-                       return !keywordsTable.Contains (identifier);
+                       if (keywordsTable.Contains (identifier))
+                               return false;
+
+                       if (!is_identifier_start_character (identifier [0]))
+                                return false;
+                        
+                        for (int i = 1; i < identifier.Length; i ++)
+                                if (! is_identifier_part_character (identifier [i]))
+                                        return false;
+                        
+                        return true;
                }
 
                protected override bool Supports (GeneratorSupport supports)
@@ -1454,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)
@@ -1497,6 +1592,10 @@ namespace Mono.CSharp
                {
                        if (count == 0) {
                                return;
+                       } else if (typeArguments.Count == 0) {
+                               // generic type definition
+                               sb.Append ("<>");
+                               return;
                        }
 
                        sb.Append ('<');