2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / System.CodeDom.Compiler / CodeDomProvider.cs
blob417c3c67eeeb0be2227e6789d2058564f1f7e856
1 //
2 // System.CodeDom.Compiler.CodeDomProvider.cs
3 //
4 // Authors:
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>
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.Collections.Generic;
34 using System.ComponentModel;
35 using System.Configuration;
36 using System.IO;
37 using System.Runtime.InteropServices;
38 using System.Security.Permissions;
40 namespace System.CodeDom.Compiler {
42 #if NET_2_0
43 [ComVisible (true)]
44 #endif
45 [ToolboxItem (false)]
46 public abstract class CodeDomProvider : Component
49 // Constructors
51 protected CodeDomProvider()
56 // Properties
58 public virtual string FileExtension {
59 get {
60 return String.Empty;
64 public virtual LanguageOptions LanguageOptions {
65 get {
66 return LanguageOptions.None;
71 // Methods
73 #if NET_2_0
74 [Obsolete ("ICodeCompiler is obsolete")]
75 #endif
76 public abstract ICodeCompiler CreateCompiler();
78 #if NET_2_0
79 [Obsolete ("ICodeGenerator is obsolete")]
80 #endif
81 public abstract ICodeGenerator CreateGenerator();
83 public virtual ICodeGenerator CreateGenerator (string fileName)
85 return CreateGenerator();
88 public virtual ICodeGenerator CreateGenerator (TextWriter output)
90 return CreateGenerator();
93 #if NET_2_0
94 [Obsolete ("ICodeParser is obsolete")]
95 #endif
96 public virtual ICodeParser CreateParser()
98 return null;
101 public virtual TypeConverter GetConverter (Type type)
103 return TypeDescriptor.GetConverter (type);
106 #if NET_2_0
107 public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
109 ICodeCompiler cc = CreateCompiler ();
110 if (cc == null)
111 throw GetNotImplemented ();
112 return cc.CompileAssemblyFromDomBatch (options, compilationUnits);
115 public virtual CompilerResults CompileAssemblyFromFile (CompilerParameters options, params string[] fileNames)
117 ICodeCompiler cc = CreateCompiler ();
118 if (cc == null)
119 throw GetNotImplemented ();
120 return cc.CompileAssemblyFromFileBatch (options, fileNames);
123 public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] fileNames)
125 ICodeCompiler cc = CreateCompiler ();
126 if (cc == null)
127 throw GetNotImplemented ();
128 return cc.CompileAssemblyFromSourceBatch (options, fileNames);
131 public virtual string CreateEscapedIdentifier (string value)
133 ICodeGenerator cg = CreateGenerator ();
134 if (cg == null)
135 throw GetNotImplemented ();
136 return cg.CreateEscapedIdentifier (value);
139 #if CONFIGURATION_DEP
140 [ComVisible (false)]
141 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
142 public static CodeDomProvider CreateProvider (string language)
144 CompilerInfo ci = GetCompilerInfo (language);
145 return (ci == null) ? null : ci.CreateProvider ();
147 #if NET_4_0
148 [ComVisible (false)]
149 public static CodeDomProvider CreateProvider (string language, IDictionary<string, string> providerOptions)
151 CompilerInfo ci = GetCompilerInfo (language);
152 return ci == null ? null : ci.CreateProvider (providerOptions);
154 #endif
156 #endif
157 public virtual string CreateValidIdentifier (string value)
159 ICodeGenerator cg = CreateGenerator ();
160 if (cg == null)
161 throw GetNotImplemented ();
162 return cg.CreateValidIdentifier (value);
165 public virtual void GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit,
166 TextWriter writer, CodeGeneratorOptions options)
168 ICodeGenerator cg = CreateGenerator ();
169 if (cg == null)
170 throw GetNotImplemented ();
171 cg.GenerateCodeFromCompileUnit (compileUnit, writer, options);
174 public virtual void GenerateCodeFromExpression (CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
176 ICodeGenerator cg = CreateGenerator ();
177 if (cg == null)
178 throw GetNotImplemented ();
179 cg.GenerateCodeFromExpression (expression, writer, options);
182 public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
184 // Documented to always throw an exception (if not overriden)
185 throw GetNotImplemented ();
186 // Note: the pattern is different from other GenerateCodeFrom* because
187 // ICodeGenerator doesn't have a GenerateCodeFromMember member
190 public virtual void GenerateCodeFromNamespace (CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
192 ICodeGenerator cg = CreateGenerator ();
193 if (cg == null)
194 throw GetNotImplemented ();
195 cg.GenerateCodeFromNamespace (codeNamespace, writer, options);
198 public virtual void GenerateCodeFromStatement (CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
200 ICodeGenerator cg = CreateGenerator ();
201 if (cg == null)
202 throw GetNotImplemented ();
203 cg.GenerateCodeFromStatement (statement, writer, options);
206 public virtual void GenerateCodeFromType (CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
208 ICodeGenerator cg = CreateGenerator ();
209 if (cg == null)
210 throw GetNotImplemented ();
211 cg.GenerateCodeFromType (codeType, writer, options);
214 #if CONFIGURATION_DEP
215 [ComVisible (false)]
216 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
217 public static CompilerInfo[] GetAllCompilerInfo ()
220 return (Config == null) ? null : Config.CompilerInfos;
224 [ComVisible (false)]
225 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
226 public static CompilerInfo GetCompilerInfo (string language)
228 if (language == null)
229 throw new ArgumentNullException ("language");
230 if (Config == null)
231 return null;
232 CompilerCollection cc = Config.Compilers;
233 return cc[language];
236 [ComVisible (false)]
237 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
238 public static string GetLanguageFromExtension (string extension)
240 if (extension == null)
241 throw new ArgumentNullException ("extension");
243 if (Config != null)
244 return Config.Compilers.GetLanguageFromExtension (extension);
245 return null;
247 #endif
249 public virtual string GetTypeOutput (CodeTypeReference type)
251 ICodeGenerator cg = CreateGenerator ();
252 if (cg == null)
253 throw GetNotImplemented ();
254 return cg.GetTypeOutput (type);
257 #if CONFIGURATION_DEP
258 [ComVisible (false)]
259 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
260 public static bool IsDefinedExtension (string extension)
262 if (extension == null)
263 throw new ArgumentNullException ("extension");
265 if (Config != null)
266 return (Config.Compilers.GetCompilerInfoForExtension (extension) != null);
268 return false;
271 [ComVisible (false)]
272 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
273 public static bool IsDefinedLanguage (string language)
275 if (language == null)
276 throw new ArgumentNullException ("language");
278 if (Config != null)
279 return (Config.Compilers.GetCompilerInfoForLanguage (language) != null);
281 return false;
283 #endif
285 public virtual bool IsValidIdentifier (string value)
287 ICodeGenerator cg = CreateGenerator ();
288 if (cg == null)
289 throw GetNotImplemented ();
290 return cg.IsValidIdentifier (value);
293 public virtual CodeCompileUnit Parse (TextReader codeStream)
295 ICodeParser cp = CreateParser ();
296 if (cp == null)
297 throw GetNotImplemented ();
298 return cp.Parse (codeStream);
301 public virtual bool Supports (GeneratorSupport supports)
303 ICodeGenerator cg = CreateGenerator ();
304 if (cg == null)
305 throw GetNotImplemented ();
306 return cg.Supports (supports);
309 #if CONFIGURATION_DEP
310 static CodeDomConfigurationHandler Config {
311 get { return ConfigurationManager.GetSection ("system.codedom") as CodeDomConfigurationHandler; }
313 #endif
316 // This is used to prevent confusing Moma about methods not implemented.
318 Exception GetNotImplemented ()
320 return new NotImplementedException ();
322 #endif