2 // System.CodeDom.Compiler.CodeDomProvider.cs
5 // Daniel Stodden (stodden@in.tum.de)
6 // Marek Safar (marek.safar@seznam.cz)
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Sebastien Pouliot <sebastien@ximian.com>
10 // Copyright (C) 2002,2003,2004,2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Configuration;
36 using System.Runtime.InteropServices;
37 using System.Security.Permissions;
39 namespace System.CodeDom.Compiler {
45 public abstract class CodeDomProvider : Component
50 protected CodeDomProvider()
57 public virtual string FileExtension {
63 public virtual LanguageOptions LanguageOptions {
65 return LanguageOptions.None;
73 [Obsolete ("ICodeCompiler is obsolete")]
75 public abstract ICodeCompiler CreateCompiler();
78 [Obsolete ("ICodeGenerator is obsolete")]
80 public abstract ICodeGenerator CreateGenerator();
82 public virtual ICodeGenerator CreateGenerator (string fileName)
84 return CreateGenerator();
87 public virtual ICodeGenerator CreateGenerator (TextWriter output)
89 return CreateGenerator();
93 [Obsolete ("ICodeParser is obsolete")]
95 public virtual ICodeParser CreateParser()
100 public virtual TypeConverter GetConverter (Type type)
102 return TypeDescriptor.GetConverter (type);
106 public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
108 ICodeCompiler cc = CreateCompiler ();
110 throw new NotImplementedException ();
111 return cc.CompileAssemblyFromDomBatch (options, compilationUnits);
114 public virtual CompilerResults CompileAssemblyFromFile (CompilerParameters options, params string[] fileNames)
116 ICodeCompiler cc = CreateCompiler ();
118 throw new NotImplementedException ();
119 return cc.CompileAssemblyFromFileBatch (options, fileNames);
122 public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] fileNames)
124 ICodeCompiler cc = CreateCompiler ();
126 throw new NotImplementedException ();
127 return cc.CompileAssemblyFromSourceBatch (options, fileNames);
130 public virtual string CreateEscapedIdentifier (string value)
132 ICodeGenerator cg = CreateGenerator ();
134 throw new NotImplementedException ();
135 return cg.CreateEscapedIdentifier (value);
139 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
140 public static CodeDomProvider CreateProvider (string language)
142 CompilerInfo ci = GetCompilerInfo (language);
143 return (ci == null) ? null : ci.CreateProvider ();
146 public virtual string CreateValidIdentifier (string value)
148 ICodeGenerator cg = CreateGenerator ();
150 throw new NotImplementedException ();
151 return cg.CreateValidIdentifier (value);
154 public virtual void GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
155 TextWriter writer, CodeGeneratorOptions options)
157 ICodeGenerator cg = CreateGenerator ();
159 throw new NotImplementedException ();
160 cg.GenerateCodeFromCompileUnit (compileUnit, writer, options);
163 public virtual void GenerateCodeFromExpression (CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
165 ICodeGenerator cg = CreateGenerator ();
167 throw new NotImplementedException ();
168 cg.GenerateCodeFromExpression (expression, writer, options);
171 public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
173 // Documented to always throw an exception (if not overriden)
174 throw new NotImplementedException ();
175 // Note: the pattern is different from other GenerateCodeFrom* because
176 // ICodeGenerator doesn't have a GenerateCodeFromMember member
179 public virtual void GenerateCodeFromNamespace (CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
181 ICodeGenerator cg = CreateGenerator ();
183 throw new NotImplementedException ();
184 cg.GenerateCodeFromNamespace (codeNamespace, writer, options);
187 public virtual void GenerateCodeFromStatement (CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
189 ICodeGenerator cg = CreateGenerator ();
191 throw new NotImplementedException ();
192 cg.GenerateCodeFromStatement (statement, writer, options);
195 public virtual void GenerateCodeFromType (CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
197 ICodeGenerator cg = CreateGenerator ();
199 throw new NotImplementedException ();
200 cg.GenerateCodeFromType (codeType, writer, options);
204 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
205 public static CompilerInfo[] GetAllCompilerInfo ()
208 if ((Config != null) && (Config.Compilers != null))
209 n = Config.Compilers.Hash.Count;
210 CompilerInfo[] ci = new CompilerInfo [n];
212 Config.Compilers.Hash.Values.CopyTo (ci, 0);
217 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
218 public static CompilerInfo GetCompilerInfo (string language)
220 if (language == null)
221 throw new ArgumentNullException ("language");
223 return (Config == null) ? null : Config.GetCompilerInfo (language);
227 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
228 public static string GetLanguageFromExtension (string extension)
230 if (extension == null)
231 throw new ArgumentNullException ("extension");
233 if (Config != null) {
234 foreach (DictionaryEntry de in Config.Compilers.Hash) {
235 CompilerInfo c = (CompilerInfo) de.Value;
236 if (Array.IndexOf (c.GetExtensions (), extension) != -1)
237 return (string) de.Key;
243 public virtual string GetTypeOutput (CodeTypeReference type)
245 ICodeGenerator cg = CreateGenerator ();
247 throw new NotImplementedException ();
248 return cg.GetTypeOutput (type);
252 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
253 public static bool IsDefinedExtension (string extension)
255 if (extension == null)
256 throw new ArgumentNullException ("extension");
258 if (Config != null) {
259 foreach (CompilerInfo c in Config.Compilers.Hash.Values) {
260 if (Array.IndexOf (c.GetExtensions (), extension) != -1)
268 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
269 public static bool IsDefinedLanguage (string language)
271 if (language == null)
272 throw new ArgumentNullException ("language");
276 return (Config.GetCompilerInfo (language) == null);
279 public virtual bool IsValidIdentifier (string value)
281 ICodeGenerator cg = CreateGenerator ();
283 throw new NotImplementedException ();
284 return cg.IsValidIdentifier (value);
287 public virtual CodeCompileUnit Parse (TextReader codeStream)
289 ICodeParser cp = CreateParser ();
291 throw new NotImplementedException ();
292 return cp.Parse (codeStream);
295 public virtual bool Supports (GeneratorSupport supports)
297 ICodeGenerator cg = CreateGenerator ();
299 throw new NotImplementedException ();
300 return cg.Supports (supports);
303 static CompilationConfiguration Config {
304 get { return ConfigurationSettings.GetConfig ("system.codedom") as CompilationConfiguration; }