[jit] Rewrite some logging to work on embedded systems.
[mono.git] / mcs / class / System / System.CodeDom.Compiler / CodeDomProvider.cs
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)
11 //
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.
30 //
31
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;
39
40 #pragma warning disable 618
41
42 namespace System.CodeDom.Compiler {
43
44         [ComVisible (true)]
45         [ToolboxItem (false)]
46         public abstract class CodeDomProvider : Component
47         {
48                 //
49                 // Constructors
50                 //
51                 protected CodeDomProvider()
52                 {
53                 }
54
55                 //
56                 // Properties
57                 //
58                 public virtual string FileExtension {
59                         get {
60                                 return String.Empty;
61                         }
62                 }
63
64                 public virtual LanguageOptions LanguageOptions {
65                         get {
66                                 return LanguageOptions.None;
67                         }
68                 }
69
70                 //
71                 // Methods
72                 //
73                 [Obsolete ("ICodeCompiler is obsolete")]
74                 public abstract ICodeCompiler CreateCompiler();
75
76                 [Obsolete ("ICodeGenerator is obsolete")]
77                 public abstract ICodeGenerator CreateGenerator();
78                 
79                 public virtual ICodeGenerator CreateGenerator (string fileName)
80                 {
81                         return CreateGenerator();
82                 }
83
84                 public virtual ICodeGenerator CreateGenerator (TextWriter output)
85                 {
86                         return CreateGenerator();
87                 }
88
89                 [Obsolete ("ICodeParser is obsolete")]
90                 public virtual ICodeParser CreateParser()
91                 {
92                         return null;
93                 }
94
95                 public virtual TypeConverter GetConverter (Type type)
96                 {
97                         return TypeDescriptor.GetConverter (type);
98                 }
99
100                 public virtual CompilerResults CompileAssemblyFromDom (CompilerParameters options, params CodeCompileUnit[] compilationUnits)
101                 {
102                         ICodeCompiler cc = CreateCompiler ();
103                         if (cc == null)
104                                 throw GetNotImplemented ();
105                         return cc.CompileAssemblyFromDomBatch (options, compilationUnits);
106                 }
107
108                 public virtual CompilerResults CompileAssemblyFromFile (CompilerParameters options, params string[] fileNames)
109                 {
110                         ICodeCompiler cc = CreateCompiler ();
111                         if (cc == null)
112                                 throw GetNotImplemented ();
113                         return cc.CompileAssemblyFromFileBatch (options, fileNames);
114                 }
115
116                 public virtual CompilerResults CompileAssemblyFromSource (CompilerParameters options, params string[] sources)
117                 {
118                         ICodeCompiler cc = CreateCompiler ();
119                         if (cc == null)
120                                 throw GetNotImplemented ();
121                         return cc.CompileAssemblyFromSourceBatch (options, sources);
122                 }
123
124                 public virtual string CreateEscapedIdentifier (string value)
125                 {
126                         ICodeGenerator cg = CreateGenerator ();
127                         if (cg == null)
128                                 throw GetNotImplemented ();
129                         return cg.CreateEscapedIdentifier (value);
130                 }
131
132 #if CONFIGURATION_DEP
133                 [ComVisible (false)]
134                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
135                 public static CodeDomProvider CreateProvider (string language)
136                 {
137                         CompilerInfo ci = GetCompilerInfo (language);
138                         return (ci == null) ? null : ci.CreateProvider ();
139                 }
140                 [ComVisible (false)]
141                 public static CodeDomProvider CreateProvider (string language, IDictionary<string, string> providerOptions)
142                 {
143                         CompilerInfo ci = GetCompilerInfo (language);
144                         return ci == null ? null : ci.CreateProvider (providerOptions);
145                 }
146
147 #endif
148                 public virtual string CreateValidIdentifier (string value)
149                 {
150                         ICodeGenerator cg = CreateGenerator ();
151                         if (cg == null)
152                                 throw GetNotImplemented ();
153                         return cg.CreateValidIdentifier (value);
154                 }
155
156                 public virtual void GenerateCodeFromCompileUnit (CodeCompileUnit compileUnit, 
157                         TextWriter writer, CodeGeneratorOptions options)
158                 {
159                         ICodeGenerator cg = CreateGenerator ();
160                         if (cg == null)
161                                 throw GetNotImplemented ();
162                         cg.GenerateCodeFromCompileUnit (compileUnit, writer, options);
163                 }
164
165                 public virtual void GenerateCodeFromExpression (CodeExpression expression, TextWriter writer, CodeGeneratorOptions options)
166                 {
167                         ICodeGenerator cg = CreateGenerator ();
168                         if (cg == null)
169                                 throw GetNotImplemented ();
170                         cg.GenerateCodeFromExpression (expression, writer, options);
171                 }
172
173                 public virtual void GenerateCodeFromMember (CodeTypeMember member, TextWriter writer, CodeGeneratorOptions options)
174                 {
175                         // Documented to always throw an exception (if not overriden)
176                         throw GetNotImplemented ();
177                         // Note: the pattern is different from other GenerateCodeFrom* because 
178                         // ICodeGenerator doesn't have a GenerateCodeFromMember member
179                 }
180
181                 public virtual void GenerateCodeFromNamespace (CodeNamespace codeNamespace, TextWriter writer, CodeGeneratorOptions options)
182                 {
183                         ICodeGenerator cg = CreateGenerator ();
184                         if (cg == null)
185                                 throw GetNotImplemented ();
186                         cg.GenerateCodeFromNamespace (codeNamespace, writer, options);
187                 }
188
189                 public virtual void GenerateCodeFromStatement (CodeStatement statement, TextWriter writer, CodeGeneratorOptions options)
190                 {
191                         ICodeGenerator cg = CreateGenerator ();
192                         if (cg == null)
193                                 throw GetNotImplemented ();
194                         cg.GenerateCodeFromStatement (statement, writer, options);
195                 }
196
197                 public virtual void GenerateCodeFromType (CodeTypeDeclaration codeType, TextWriter writer, CodeGeneratorOptions options)
198                 {
199                         ICodeGenerator cg = CreateGenerator ();
200                         if (cg == null)
201                                 throw GetNotImplemented ();
202                         cg.GenerateCodeFromType (codeType, writer, options);
203                 }
204
205 #if CONFIGURATION_DEP
206                 [ComVisible (false)]
207                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
208                 public static CompilerInfo[] GetAllCompilerInfo ()
209                 {
210
211                         return (Config == null) ? null : Config.CompilerInfos;
212                 }
213
214
215                 [ComVisible (false)]
216                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
217                 public static CompilerInfo GetCompilerInfo (string language)
218                 {
219                         if (language == null)
220                                 throw new ArgumentNullException ("language");
221                         if (Config == null)
222                                 return null;
223                         CompilerCollection cc = Config.Compilers;
224                         return cc[language];
225                 }
226
227                 [ComVisible (false)]
228                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
229                 public static string GetLanguageFromExtension (string extension)
230                 {
231                         if (extension == null)
232                                 throw new ArgumentNullException ("extension");
233
234                         if (Config != null) 
235                                 return Config.Compilers.GetLanguageFromExtension (extension);
236                         return null;
237                 }
238 #else
239                 public static CompilerInfo[] GetAllCompilerInfo () { return null; }
240                 public static CompilerInfo GetCompilerInfo (string language) { return null; }
241                 public static string GetLanguageFromExtension (string extension) { return null; }
242 #endif
243
244                 public virtual string GetTypeOutput (CodeTypeReference type)
245                 {
246                         ICodeGenerator cg = CreateGenerator ();
247                         if (cg == null)
248                                 throw GetNotImplemented ();
249                         return cg.GetTypeOutput (type);
250                 }
251
252 #if CONFIGURATION_DEP
253                 [ComVisible (false)]
254                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
255                 public static bool IsDefinedExtension (string extension)
256                 {
257                         if (extension == null)
258                                 throw new ArgumentNullException ("extension");
259
260                         if (Config != null)
261                                 return (Config.Compilers.GetCompilerInfoForExtension (extension) != null);
262                         
263                         return false;
264                 }
265
266                 [ComVisible (false)]
267                 [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
268                 public static bool IsDefinedLanguage (string language)
269                 {
270                         if (language == null)
271                                 throw new ArgumentNullException ("language");
272
273                         if (Config != null)
274                                 return (Config.Compilers.GetCompilerInfoForLanguage (language) != null);
275
276                         return false;
277                 }
278 #endif
279
280                 public virtual bool IsValidIdentifier (string value)
281                 {
282                         ICodeGenerator cg = CreateGenerator ();
283                         if (cg == null)
284                                 throw GetNotImplemented ();
285                         return cg.IsValidIdentifier (value);
286                 }
287
288                 public virtual CodeCompileUnit Parse (TextReader codeStream)
289                 {
290                         ICodeParser cp = CreateParser ();
291                         if (cp == null)
292                                 throw GetNotImplemented ();
293                         return cp.Parse (codeStream);
294                 }
295
296                 public virtual bool Supports (GeneratorSupport supports)
297                 {
298                         ICodeGenerator cg = CreateGenerator ();
299                         if (cg == null)
300                                 throw GetNotImplemented ();
301                         return cg.Supports (supports);
302                 }
303
304 #if CONFIGURATION_DEP
305                 static CodeDomConfigurationHandler Config {
306                         get { return ConfigurationManager.GetSection ("system.codedom") as CodeDomConfigurationHandler; }
307                 }
308 #endif
309                 
310                 //
311                 // This is used to prevent confusing Moma about methods not implemented.
312                 //
313                 Exception GetNotImplemented ()
314                 {
315                         return new NotImplementedException ();
316                 }               
317         }
318 }
319
320 #pragma warning restore 618