Merge pull request #3997 from lateralusX/jlorenss/win-api-family-support-safearray
[mono.git] / mcs / class / System / Microsoft.CSharp / CSharpCodeGenerator.cs
index 01212b6ba76fe465bfd7ebf0b208c097af5c96fc..ecc7b1a4645f400223e493bd48916a0e9819c5a9 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   Daniel Stodden (stodden@in.tum.de)
 //   Marek Safar (marek.safar@seznam.cz)
+//   Ilker Cetinkaya (mail@ilker.de)
 //
 // (C) 2002 Ximian, Inc.
 //
@@ -171,18 +172,62 @@ namespace Mono.CSharp
                {
                        GenerateCompileUnitStart (compileUnit);
 
+                       List<CodeNamespaceImport> imports = null;
+                       foreach (CodeNamespace codeNamespace in compileUnit.Namespaces) {
+                               if (!string.IsNullOrEmpty (codeNamespace.Name))
+                                       continue;
+
+                               if (codeNamespace.Imports.Count == 0)
+                                       continue;
+
+                               if (imports == null)
+                                       imports = new List<CodeNamespaceImport> ();
+
+                               foreach (CodeNamespaceImport i in codeNamespace.Imports)
+                                       imports.Add (i);
+                       }
+
+                       if (imports != null) {
+                               imports.Sort ((a, b) => a.Namespace.CompareTo (b.Namespace));
+                               foreach (var import in imports)
+                                       GenerateNamespaceImport (import);
+
+                               Output.WriteLine ();
+                       }
+
                        if (compileUnit.AssemblyCustomAttributes.Count > 0) {
                                OutputAttributes (compileUnit.AssemblyCustomAttributes, 
                                        "assembly: ", false);
                                Output.WriteLine ("");
                        }
 
-                       foreach (CodeNamespace ns in compileUnit.Namespaces)
-                               GenerateNamespace (ns);
+                       CodeNamespaceImportCollection global_imports = null;
+                       foreach (CodeNamespace codeNamespace in compileUnit.Namespaces) {
+                               if (string.IsNullOrEmpty (codeNamespace.Name)) {
+                                       global_imports = codeNamespace.Imports;
+                                       codeNamespace.Imports.Clear ();
+                               }
+
+                               GenerateNamespace (codeNamespace);
+
+                               if (global_imports != null) {
+                                       codeNamespace.Imports.Clear ();
+                                       foreach (CodeNamespaceImport ns in global_imports)
+                                       codeNamespace.Imports.Add (ns);
+                                       global_imports = null;
+                               }
+                       }
 
                        GenerateCompileUnitEnd (compileUnit);
                }
 
+               protected override void GenerateDefaultValueExpression (CodeDefaultValueExpression e)
+               {
+                       Output.Write ("default(");
+                       OutputType (e.Type);
+                       Output.Write (')');
+               }
+
                protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression expression)
                {
                        TextWriter output = Output;
@@ -1083,6 +1128,9 @@ namespace Mono.CSharp
                                        break;
                        }
 
+                       if ((declaration.Attributes & MemberAttributes.New) != 0)
+                               output.Write ("new ");
+
                        if (declaration.IsStruct) {
                                if (declaration.IsPartial) {
                                        output.Write ("partial ");
@@ -1321,7 +1369,7 @@ namespace Mono.CSharp
                                        break;
                                default:
                                        StringBuilder sb = new StringBuilder (baseType.Length);
-                                       if (type.Options == CodeTypeReferenceOptions.GlobalReference) {
+                                       if ((type.Options & CodeTypeReferenceOptions.GlobalReference) != 0) {
                                                sb.Append ("global::");
                                        }
 
@@ -1375,14 +1423,14 @@ namespace Mono.CSharp
                }
 
                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);
-                }
+               {
+                       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)
                {
@@ -1396,13 +1444,13 @@ namespace Mono.CSharp
                                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;
+                               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)
@@ -1568,9 +1616,13 @@ namespace Mono.CSharp
 
                static void FillKeywordTable ()
                {
-                       keywordsTable = new Hashtable ();
-                       foreach (string keyword in keywords) {
-                               keywordsTable.Add (keyword, keyword);
+                       lock (keywords) {
+                               if (keywordsTable == null) {
+                                       keywordsTable = new Hashtable ();
+                                       foreach (string keyword in keywords) {
+                                               keywordsTable.Add (keyword, keyword);
+                                       }
+                               }
                        }
                }