Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System / compmod / system / codedom / compiler / CompilerInfo.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CompilerInfo.cs" company="Microsoft">
3 // 
4 // <OWNER>Microsoft</OWNER>
5 //     Copyright (c) Microsoft Corporation.  All rights reserved.
6 // </copyright>                                                                
7 //------------------------------------------------------------------------------
8
9 namespace System.CodeDom.Compiler {
10     using System;
11     using System.Reflection;
12     using System.Security.Permissions;
13     using System.CodeDom.Compiler;
14     using System.Configuration;
15     using System.Collections.Generic;
16     using System.Diagnostics;
17
18     [PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
19     public sealed class CompilerInfo {
20         internal String _codeDomProviderTypeName; // This can never by null
21         internal CompilerParameters _compilerParams; // This can never by null
22         internal String[] _compilerLanguages; // This can never by null
23         internal String[] _compilerExtensions; // This can never by null
24         internal String configFileName;
25         internal IDictionary<string, string> _providerOptions;  // This can never be null
26         internal int configFileLineNumber;
27         internal Boolean _mapped;
28         
29         private Type type;
30         
31         private CompilerInfo() {} // Not createable
32
33         public String[] GetLanguages() { 
34             return CloneCompilerLanguages();
35         }
36
37         public String[] GetExtensions() { 
38             return CloneCompilerExtensions();
39         }
40
41         public Type CodeDomProviderType { 
42             get {
43                 if( type == null) {
44                     lock(this) {
45                         if( type == null) {
46                             type = Type.GetType(_codeDomProviderTypeName);
47                             if (type == null) {                    
48                                 if( configFileName == null) {
49                                     throw new ConfigurationErrorsException(SR.GetString(SR.Unable_To_Locate_Type, 
50                                                                       _codeDomProviderTypeName, string.Empty, 0));
51                                 }
52                                 else {
53                                     throw new ConfigurationErrorsException(SR.GetString(SR.Unable_To_Locate_Type,
54                                                                       _codeDomProviderTypeName), configFileName, configFileLineNumber);
55                                 }
56                             }
57                         }                                                        
58                     }
59                 }
60                     
61                 return type;
62             }
63         }
64
65         public bool IsCodeDomProviderTypeValid {
66             get {
67                 Type type = Type.GetType(_codeDomProviderTypeName);
68                 return (type != null);
69             }
70         }
71
72         public CodeDomProvider CreateProvider() {
73             // if the provider defines an IDictionary<string, string> ctor and
74             // provider options have been provided then call that and give it the 
75             // provider options dictionary.  Otherwise call the normal one.
76
77             Debug.Assert(_providerOptions != null, "Created CompilerInfo w/ null _providerOptions");
78
79             if (_providerOptions.Count > 0) {
80                 ConstructorInfo ci = CodeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) });
81                 if (ci != null) {
82                     return (CodeDomProvider)ci.Invoke(new object[] { _providerOptions });
83                 }
84             }
85
86             return (CodeDomProvider)Activator.CreateInstance(CodeDomProviderType);
87         }
88
89         public CodeDomProvider CreateProvider(IDictionary<String, String> providerOptions)
90         {
91             if (providerOptions == null)
92                 throw new ArgumentNullException("providerOptions");
93
94             ConstructorInfo constructor = CodeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) });
95             if (constructor != null) {
96                 return (CodeDomProvider)constructor.Invoke(new object[] { providerOptions });
97             }
98             else
99                 throw new InvalidOperationException(SR.GetString(SR.Provider_does_not_support_options, CodeDomProviderType.ToString()));
100
101         }
102
103         public CompilerParameters CreateDefaultCompilerParameters() {
104             return CloneCompilerParameters();
105         }
106
107
108         internal CompilerInfo(CompilerParameters compilerParams, String codeDomProviderTypeName, String[] compilerLanguages, String[] compilerExtensions) {
109             _compilerLanguages = compilerLanguages;
110             _compilerExtensions = compilerExtensions;
111             _codeDomProviderTypeName = codeDomProviderTypeName;
112             if (compilerParams == null)
113                 compilerParams = new CompilerParameters();
114
115             _compilerParams = compilerParams;
116         }
117
118         internal CompilerInfo(CompilerParameters compilerParams, String codeDomProviderTypeName) {
119             _codeDomProviderTypeName = codeDomProviderTypeName;
120             if (compilerParams == null)
121                 compilerParams = new CompilerParameters();
122
123             _compilerParams = compilerParams;
124         }
125
126
127         public override int GetHashCode() {
128             return _codeDomProviderTypeName.GetHashCode();
129         }
130
131         public override bool Equals(Object o) {
132             CompilerInfo other = o as CompilerInfo;
133             if (o == null)
134                 return false;
135
136             return CodeDomProviderType == other.CodeDomProviderType &&
137                 CompilerParams.WarningLevel == other.CompilerParams.WarningLevel &&
138                 CompilerParams.IncludeDebugInformation == other.CompilerParams.IncludeDebugInformation &&
139                 CompilerParams.CompilerOptions == other.CompilerParams.CompilerOptions;
140         }
141
142         private CompilerParameters CloneCompilerParameters() {
143             CompilerParameters copy = new CompilerParameters();
144             copy.IncludeDebugInformation = _compilerParams.IncludeDebugInformation;
145             copy.TreatWarningsAsErrors = _compilerParams.TreatWarningsAsErrors;
146             copy.WarningLevel = _compilerParams.WarningLevel;
147             copy.CompilerOptions = _compilerParams.CompilerOptions;
148             return copy;
149         }
150
151         private String[] CloneCompilerLanguages() {
152             String[] compilerLanguages = new String[_compilerLanguages.Length]; 
153             Array.Copy(_compilerLanguages, compilerLanguages, _compilerLanguages.Length);
154             return compilerLanguages;
155         }
156
157         private String[] CloneCompilerExtensions() {
158             String[] compilerExtensions = new String[_compilerExtensions.Length]; 
159             Array.Copy(_compilerExtensions, compilerExtensions, _compilerExtensions.Length);
160             return compilerExtensions;
161         }
162
163         internal CompilerParameters CompilerParams {
164             get {
165                 return _compilerParams;
166             }
167         }
168
169         // @
170         internal IDictionary<string, string> ProviderOptions {
171             get {
172                 return _providerOptions;
173             }
174         }
175     }
176 }
177