Merge pull request #3769 from evincarofautumn/fix-verify-before-allocs
[mono.git] / mcs / class / System / System.CodeDom.Compiler / CodeDomProvider.cs
index a0bbf60354bcbd1a0fdd6fb68e41117dc4556359..192f1c070739da293c929c92976b268f2e2f7a8c 100644 (file)
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.ComponentModel;
 using System.Configuration;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Security.Permissions;
 
+#pragma warning disable 618
+
 namespace System.CodeDom.Compiler {
 
-#if NET_2_0
        [ComVisible (true)]
-#endif
        [ToolboxItem (false)]
        public abstract class CodeDomProvider : Component
        {
@@ -69,14 +70,10 @@ namespace System.CodeDom.Compiler {
                //
                // Methods
                //
-#if NET_2_0
                [Obsolete ("ICodeCompiler is obsolete")]
-#endif
                public abstract ICodeCompiler CreateCompiler();
 
-#if NET_2_0
                [Obsolete ("ICodeGenerator is obsolete")]
-#endif
                public abstract ICodeGenerator CreateGenerator();
                
                public virtual ICodeGenerator CreateGenerator (string fileName)
@@ -89,9 +86,7 @@ namespace System.CodeDom.Compiler {
                        return CreateGenerator();
                }
 
-#if NET_2_0
                [Obsolete ("ICodeParser is obsolete")]
-#endif
                public virtual ICodeParser CreateParser()
                {
                        return null;
@@ -102,12 +97,11 @@ namespace System.CodeDom.Compiler {
                        return TypeDescriptor.GetConverter (type);
                }
 
-#if NET_2_0
                public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
                {
                        ICodeCompiler cc = CreateCompiler ();
                        if (cc == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cc.CompileAssemblyFromDomBatch (options, compilationUnits);
                }
 
@@ -115,26 +109,27 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeCompiler cc = CreateCompiler ();
                        if (cc == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cc.CompileAssemblyFromFileBatch (options, fileNames);
                }
 
-               public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] fileNames)
+               public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] sources)
                {
                        ICodeCompiler cc = CreateCompiler ();
                        if (cc == null)
-                               throw new NotImplementedException ();
-                       return cc.CompileAssemblyFromSourceBatch (options, fileNames);
+                               throw GetNotImplemented ();
+                       return cc.CompileAssemblyFromSourceBatch (options, sources);
                }
 
                public virtual string CreateEscapedIdentifier (string value)
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cg.CreateEscapedIdentifier (value);
                }
 
+#if CONFIGURATION_DEP
                [ComVisible (false)]
                [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
                public static CodeDomProvider CreateProvider (string language)
@@ -142,12 +137,19 @@ namespace System.CodeDom.Compiler {
                        CompilerInfo ci = GetCompilerInfo (language);
                        return (ci == null) ? null : ci.CreateProvider ();
                }
+               [ComVisible (false)]
+               public static CodeDomProvider CreateProvider (string language, IDictionary<string, string> providerOptions)
+               {
+                       CompilerInfo ci = GetCompilerInfo (language);
+                       return ci == null ? null : ci.CreateProvider (providerOptions);
+               }
 
+#endif
                public virtual string CreateValidIdentifier (string value)
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cg.CreateValidIdentifier (value);
                }
 
@@ -156,7 +158,7 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        cg.GenerateCodeFromCompileUnit (compileUnit, writer, options);
                }
 
@@ -164,14 +166,14 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        cg.GenerateCodeFromExpression (expression, writer, options);
                }
 
                public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
                {
                        // Documented to always throw an exception (if not overriden)
-                       throw new NotImplementedException ();
+                       throw GetNotImplemented ();
                        // Note: the pattern is different from other GenerateCodeFrom* because 
                        // ICodeGenerator doesn't have a GenerateCodeFromMember member
                }
@@ -180,7 +182,7 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        cg.GenerateCodeFromNamespace (codeNamespace, writer, options);
                }
 
@@ -188,7 +190,7 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        cg.GenerateCodeFromStatement (statement, writer, options);
                }
 
@@ -196,31 +198,30 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        cg.GenerateCodeFromType (codeType, writer, options);
                }
 
+#if CONFIGURATION_DEP
                [ComVisible (false)]
                [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
                public static CompilerInfo[] GetAllCompilerInfo ()
                {
-                       int n = 0;
-                       if ((Config != null) && (Config.Compilers != null)) 
-                               n = Config.Compilers.Hash.Count;
-                       CompilerInfo[] ci = new CompilerInfo [n];
-                       if (n > 0)
-                               Config.Compilers.Hash.Values.CopyTo (ci, 0);
-                       return ci;
+
+                       return (Config == null) ? null : Config.CompilerInfos;
                }
 
+
                [ComVisible (false)]
                [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
                public static CompilerInfo GetCompilerInfo (string language)
                {
                        if (language == null)
                                throw new ArgumentNullException ("language");
-
-                       return (Config == null) ? null : Config.GetCompilerInfo (language);
+                       if (Config == null)
+                               return null;
+                       CompilerCollection cc = Config.Compilers;
+                       return cc[language];
                }
 
                [ComVisible (false)]
@@ -230,24 +231,25 @@ namespace System.CodeDom.Compiler {
                        if (extension == null)
                                throw new ArgumentNullException ("extension");
 
-                       if (Config != null) {
-                               foreach (DictionaryEntry de in Config.Compilers.Hash) {
-                                       CompilerInfo c = (CompilerInfo) de.Value;
-                                       if (Array.IndexOf (c.GetExtensions (), extension) != -1)
-                                               return (string) de.Key;
-                               }
-                       }
+                       if (Config != null) 
+                               return Config.Compilers.GetLanguageFromExtension (extension);
                        return null;
                }
+#else
+               public static CompilerInfo[] GetAllCompilerInfo () { return null; }
+               public static CompilerInfo GetCompilerInfo (string language) { return null; }
+               public static string GetLanguageFromExtension (string extension) { return null; }
+#endif
 
                public virtual string GetTypeOutput (CodeTypeReference type)
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cg.GetTypeOutput (type);
                }
 
+#if CONFIGURATION_DEP
                [ComVisible (false)]
                [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
                public static bool IsDefinedExtension (string extension)
@@ -255,12 +257,9 @@ namespace System.CodeDom.Compiler {
                        if (extension == null)
                                throw new ArgumentNullException ("extension");
 
-                       if (Config != null) {
-                               foreach (CompilerInfo c in Config.Compilers.Hash.Values) {
-                                       if (Array.IndexOf (c.GetExtensions (), extension) != -1)
-                                               return true;
-                               }
-                       }
+                       if (Config != null)
+                               return (Config.Compilers.GetCompilerInfoForExtension (extension) != null);
+                       
                        return false;
                }
 
@@ -271,16 +270,18 @@ namespace System.CodeDom.Compiler {
                        if (language == null)
                                throw new ArgumentNullException ("language");
 
-                       if (Config == null)
-                               return false;
-                       return (Config.GetCompilerInfo (language) == null);
+                       if (Config != null)
+                               return (Config.Compilers.GetCompilerInfoForLanguage (language) != null);
+
+                       return false;
                }
+#endif
 
                public virtual bool IsValidIdentifier (string value)
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cg.IsValidIdentifier (value);
                }
 
@@ -288,7 +289,7 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeParser cp = CreateParser ();
                        if (cp == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cp.Parse (codeStream);
                }
 
@@ -296,13 +297,24 @@ namespace System.CodeDom.Compiler {
                {
                        ICodeGenerator cg = CreateGenerator ();
                        if (cg == null)
-                               throw new NotImplementedException ();
+                               throw GetNotImplemented ();
                        return cg.Supports (supports);
                }
 
-               static CompilationConfiguration Config {
-                       get { return ConfigurationSettings.GetConfig ("system.codedom") as CompilationConfiguration; }
+#if CONFIGURATION_DEP
+               static CodeDomConfigurationHandler Config {
+                       get { return ConfigurationManager.GetSection ("system.codedom") as CodeDomConfigurationHandler; }
                }
 #endif
+               
+               //
+               // This is used to prevent confusing Moma about methods not implemented.
+               //
+               Exception GetNotImplemented ()
+               {
+                       return new NotImplementedException ();
+               }               
        }
 }
+
+#pragma warning restore 618
\ No newline at end of file