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