File: CompilationUtil.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (670 lines) | stat: -rw-r--r-- 30,677 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//------------------------------------------------------------------------------
// <copyright file="CompilationConfiguration.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

/*
 * Code related to the <assemblies> config section
 *
 * Copyright (c) 1999 Microsoft Corporation
 */
namespace System.Web.Compilation {

    using System;
    using System.Web;
    using System.Configuration;
    using System.Web.UI;
    using System.Web.Configuration;
    using System.Web.Hosting;
    using System.Web.Util;
    using System.Globalization;
    using System.Collections;
    using System.Collections.Generic;
    using System.CodeDom.Compiler;
    using System.Linq;
    using System.Security;
    using System.Security.Permissions;
    using System.Reflection;
    using System.Runtime.ExceptionServices;
    using System.Threading;
    using System.Threading.Tasks;
    
    internal static class CompilationUtil {

        internal const string CodeDomProviderOptionPath = "system.codedom/compilers/compiler/ProviderOption/";
        private const string CompilerDirectoryPath = "CompilerDirectoryPath";
        private static int _maxConcurrentCompilations;

        internal static bool IsDebuggingEnabled(HttpContext context) {
            CompilationSection compConfig = MTConfigUtil.GetCompilationConfig(context);
            return compConfig.Debug;
        }

        internal static bool IsBatchingEnabled(string configPath) {
            CompilationSection config = MTConfigUtil.GetCompilationConfig(configPath);
            return config.Batch;
        }

        internal static int GetRecompilationsBeforeAppRestarts() {
            CompilationSection config = MTConfigUtil.GetCompilationAppConfig();
            return config.NumRecompilesBeforeAppRestart;
        }

        internal static CompilerType GetCodeDefaultLanguageCompilerInfo() {
            return new CompilerType(typeof(Microsoft.VisualBasic.VBCodeProvider), null);
        }

        internal static CompilerType GetDefaultLanguageCompilerInfo(CompilationSection compConfig, VirtualPath configPath) {
            if (compConfig == null) {
                // Get the <compilation> config object
                compConfig = MTConfigUtil.GetCompilationConfig(configPath);
            }

            // If no default language was specified in config, use VB
            if (compConfig.DefaultLanguage == null) {
                return GetCodeDefaultLanguageCompilerInfo();
            }
            else {
                return compConfig.GetCompilerInfoFromLanguage(compConfig.DefaultLanguage);
            }
        }

        /*
         * Return a CompilerType that a file name's extension maps to.
         */
        internal static CompilerType GetCompilerInfoFromVirtualPath(VirtualPath virtualPath) {

            // Get the extension of the source file to compile
            string extension = virtualPath.Extension;

            // Make sure there is an extension
            if (extension.Length == 0) {
                throw new HttpException(
                    SR.GetString(SR.Empty_extension, virtualPath));
            }

            return GetCompilerInfoFromExtension(virtualPath, extension);
        }

        /*
         * Return a CompilerType that a extension maps to.
         */
        private static CompilerType GetCompilerInfoFromExtension(VirtualPath configPath, string extension) {
            // Get the <compilation> config object
            CompilationSection config = MTConfigUtil.GetCompilationConfig(configPath);

            return config.GetCompilerInfoFromExtension(extension, true /*throwOnFail*/);
        }

        /*
         * Return a CompilerType that a language maps to.
         */
        internal static CompilerType GetCompilerInfoFromLanguage(VirtualPath configPath, string language) {
            // Get the <compilation> config object
            CompilationSection config = MTConfigUtil.GetCompilationConfig(configPath);

            return config.GetCompilerInfoFromLanguage(language);
        }

        internal static CompilerType GetCSharpCompilerInfo(
            CompilationSection compConfig, VirtualPath configPath) {

            if (compConfig == null) {
                // Get the <compilation> config object
                compConfig = MTConfigUtil.GetCompilationConfig(configPath);
            }

            if (compConfig.DefaultLanguage == null)
                return new CompilerType(typeof(Microsoft.CSharp.CSharpCodeProvider), null);

            return compConfig.GetCompilerInfoFromLanguage("c#");
        }

        internal static CodeSubDirectoriesCollection GetCodeSubDirectories() {
            // Get the <compilation> config object
            CompilationSection config = MTConfigUtil.GetCompilationAppConfig();

            CodeSubDirectoriesCollection codeSubDirectories = config.CodeSubDirectories;

            // Make sure the config data is valid
            if (codeSubDirectories != null) {
                codeSubDirectories.EnsureRuntimeValidation();
            }

            return codeSubDirectories;
        }

        internal static long GetRecompilationHash(CompilationSection ps) {
            HashCodeCombiner recompilationHash = new HashCodeCombiner();
            AssemblyCollection assemblies;
            BuildProviderCollection builders;
            FolderLevelBuildProviderCollection buildProviders;
            CodeSubDirectoriesCollection codeSubDirs;
                
            // Combine items from Compilation section
            recompilationHash.AddObject(ps.Debug);
            recompilationHash.AddObject(ps.TargetFramework);
            recompilationHash.AddObject(ps.Strict);
            recompilationHash.AddObject(ps.Explicit);
            recompilationHash.AddObject(ps.Batch);
            recompilationHash.AddObject(ps.OptimizeCompilations);
            recompilationHash.AddObject(ps.BatchTimeout);
            recompilationHash.AddObject(ps.MaxBatchGeneratedFileSize);
            recompilationHash.AddObject(ps.MaxBatchSize);
            recompilationHash.AddObject(ps.NumRecompilesBeforeAppRestart);
            recompilationHash.AddObject(ps.DefaultLanguage);
            recompilationHash.AddObject(ps.UrlLinePragmas);
            recompilationHash.AddObject(ps.DisableObsoleteWarnings);
            if (ps.AssemblyPostProcessorTypeInternal != null) {
                recompilationHash.AddObject(ps.AssemblyPostProcessorTypeInternal.FullName);
            }
            if (!String.IsNullOrWhiteSpace(ps.ControlBuilderInterceptorType)) {
                recompilationHash.AddObject(ps.ControlBuilderInterceptorType);
            }

            // Combine items from Compilers collection
            foreach (Compiler compiler in ps.Compilers) {
                recompilationHash.AddObject(compiler.Language);
                recompilationHash.AddObject(compiler.Extension);
                recompilationHash.AddObject(compiler.Type);
                recompilationHash.AddObject(compiler.WarningLevel);
                recompilationHash.AddObject(compiler.CompilerOptions);
            }

            // Combine items from <expressionBuilders> section
            foreach (System.Web.Configuration.ExpressionBuilder eb in ps.ExpressionBuilders) {
                recompilationHash.AddObject(eb.ExpressionPrefix);
                recompilationHash.AddObject(eb.Type);
            }

            // Combine items from the Assembly collection
            assemblies = ps.Assemblies;

            if (assemblies.Count == 0) {
                recompilationHash.AddObject("__clearassemblies");
            }
            else {
                foreach (AssemblyInfo ai in assemblies) {
                    recompilationHash.AddObject(ai.Assembly);
                }
            }

            // Combine items from the Builders Collection
            builders = ps.BuildProviders;

            if (builders.Count == 0) {
                recompilationHash.AddObject("__clearbuildproviders");
            }
            else {
                foreach (System.Web.Configuration.BuildProvider bp in builders) {
                    recompilationHash.AddObject(bp.Type);
                    recompilationHash.AddObject(bp.Extension);
                }
            }

            // Combine items from the FolderLevelBuildProviderCollection
            buildProviders = ps.FolderLevelBuildProviders;

            if (buildProviders.Count == 0) {
                recompilationHash.AddObject("__clearfolderlevelbuildproviders");
            }
            else {
                foreach (System.Web.Configuration.FolderLevelBuildProvider bp in buildProviders) {
                    recompilationHash.AddObject(bp.Type);
                    recompilationHash.AddObject(bp.Name);
                }
            }

            codeSubDirs = ps.CodeSubDirectories;
            if (codeSubDirs.Count == 0) {
                recompilationHash.AddObject("__clearcodesubdirs");
            }
            else {
                foreach (CodeSubDirectory csd in codeSubDirs) {
                    recompilationHash.AddObject(csd.DirectoryName);
                }
            }

            // Make sure the <system.CodeDom> section is hashed properly.
            CompilerInfo[] compilerInfoArray = CodeDomProvider.GetAllCompilerInfo();
            if (compilerInfoArray != null) {
                CompilerInfo cppCodeProvider = CodeDomProvider.GetCompilerInfo("cpp");
                foreach (CompilerInfo info in compilerInfoArray) {
                    // Skip cpp code provider (Dev11 193323).
                    if (info == cppCodeProvider) {
                        continue;
                    }

                    // Ignore it if the type is not valid.
                    if (!info.IsCodeDomProviderTypeValid) {
                        continue;
                    }

                    CompilerParameters parameters = info.CreateDefaultCompilerParameters();
                    string option = parameters.CompilerOptions;
                    if (!String.IsNullOrEmpty(option)) {
                        Type type = info.CodeDomProviderType;
                        if (type != null) {
                            recompilationHash.AddObject(type.FullName);
                        }
                        // compilerOptions need to be hashed.
                        recompilationHash.AddObject(option);
                    }

                    // DevDiv 62998
                    // The tag providerOption needs to be added to the hash,
                    // as the user could switch between v2 and v3.5.
                    if (info.CodeDomProviderType == null)
                        continue;

                    // Add a hash for each providerOption added, specific for each codeDomProvider, so that
                    // if some codedom setting has changed, we know we have to recompile.
                    IDictionary<string, string> providerOptions = GetProviderOptions(info);
                    if (providerOptions != null && providerOptions.Count > 0) {
                        string codeDomProviderType = info.CodeDomProviderType.FullName;
                        foreach (string key in providerOptions.Keys) {
                            string value = providerOptions[key];
                            recompilationHash.AddObject(codeDomProviderType + ":" +  key + "=" + value);
                        }
                    }
                }
            }

            return recompilationHash.CombinedHash;
        }


        /*
         * Return a file provider Type that an extension maps to.
         */
        internal static Type GetBuildProviderTypeFromExtension(VirtualPath configPath, string extension,
            BuildProviderAppliesTo neededFor, bool failIfUnknown) {

            // Get the <compilation> config object
            CompilationSection config = MTConfigUtil.GetCompilationConfig(configPath);

            return GetBuildProviderTypeFromExtension(config, extension, neededFor, failIfUnknown);
        }

        internal static Type GetBuildProviderTypeFromExtension(CompilationSection config, string extension,
            BuildProviderAppliesTo neededFor, bool failIfUnknown) {

            BuildProviderInfo providerInfo = BuildProvider.GetBuildProviderInfo(config, extension);

            Type buildProviderType = null;
            // Never return an IgnoreFileBuildProvider/ForceCopyBuildProvider, since it's just a marker
            if (providerInfo != null &&
                providerInfo.Type != typeof(IgnoreFileBuildProvider) &&
                providerInfo.Type != typeof(ForceCopyBuildProvider)) {
                buildProviderType = providerInfo.Type;
            }

            // In updatable precomp mode, only aspx/ascx/master web files need processing.  Ignore the rest.
            if (neededFor == BuildProviderAppliesTo.Web &&
                BuildManager.PrecompilingForUpdatableDeployment &&
                !typeof(BaseTemplateBuildProvider).IsAssignableFrom(buildProviderType)) {
                buildProviderType = null;
            }

            if (buildProviderType != null) {
                // Only return it if it applies to what it's needed for
                if ((neededFor & providerInfo.AppliesTo) != 0)
                    return buildProviderType;
            }
            // If the extension is registered as a compiler extension, use
            // a SourceFileBuildProvider to handle it (not supported in Resources directory)
            else if (neededFor != BuildProviderAppliesTo.Resources &&
                config.GetCompilerInfoFromExtension(extension, false /*throwOnFail*/) != null) {
                return typeof(SourceFileBuildProvider);
            }

            if (failIfUnknown) {
                throw new HttpException( SR.GetString(SR.Unknown_buildprovider_extension, extension, neededFor.ToString()));
            }

            return null;
        }

        // Returns the list of buildProvider types associated to the specified appliesTo
        internal static List<Type> GetFolderLevelBuildProviderTypes(CompilationSection config,
            FolderLevelBuildProviderAppliesTo appliesTo) {
            FolderLevelBuildProviderCollection buildProviders = config.FolderLevelBuildProviders;
            return buildProviders.GetBuildProviderTypes(appliesTo);
        }

        // In partial trust, do not allow the CompilerDirectoryPath provider option in codedom settings (Dev10 bug 462348)
        internal static void CheckCompilerDirectoryPathAllowed(IDictionary<string, string> providerOptions) {
            if (providerOptions == null) {
                return;
            }
            if (!providerOptions.ContainsKey(CompilerDirectoryPath)) {
                return;
            }

            if (!HttpRuntime.HasUnmanagedPermission()) {
                string errorString = SR.GetString(SR.Insufficient_trust_for_attribute, CompilerDirectoryPath);
                throw new HttpException(errorString);
            }
        }

        internal static void CheckCompilerOptionsAllowed(string compilerOptions, bool config, string file, int line) {
            
            // If it's empty, we never block it
            if (String.IsNullOrEmpty(compilerOptions))
                return;

            // Only allow the use of compilerOptions when we have UnmanagedCode access (ASURT 73678)
            if (!HttpRuntime.HasUnmanagedPermission()) {
                string errorString = SR.GetString(SR.Insufficient_trust_for_attribute, "compilerOptions");

                if (config)
                    throw new ConfigurationErrorsException(errorString, file, line);
                else
                    throw new HttpException(errorString);
            }
        }

        // This is used to determine what files need to be copied, and what stub files
        // need to be created during deployment precompilation.
        // Note: createStub only applies if the method returns false.
        internal static bool NeedToCopyFile(VirtualPath virtualPath, bool updatable, out bool createStub) {

            createStub = false;

            // Get the <compilation> config object
            CompilationSection config = MTConfigUtil.GetCompilationConfig(virtualPath);

            string extension = virtualPath.Extension;

            BuildProviderInfo providerInfo = BuildProvider.GetBuildProviderInfo(config, extension);

            if (providerInfo != null) {
                // We only care about 'web' providers.  Everything else we treat as static
                if ((BuildProviderAppliesTo.Web & providerInfo.AppliesTo) == 0)
                    return true;

                // If the provider is a ForceCopyBuildProvider, treat as static
                if (providerInfo.Type == typeof(ForceCopyBuildProvider))
                    return true;

                // During updatable precomp, everything needs to be copied over.  However,
                // aspx files that use code beside will later be overwritten by modified
                // versions (see TemplateParser.CreateModifiedMainDirectiveFileIfNeeded)
                if (providerInfo.Type != typeof(IgnoreFileBuildProvider) &&
                    BuildManager.PrecompilingForUpdatableDeployment) {
                    return true;
                }

                // There is a real provider, so don't copy the file.  We also need to determine whether
                // a stub file needs to be created.

                createStub = true;

                // Skip the stub file for some non-requestable types
                if (providerInfo.Type == typeof(UserControlBuildProvider) ||
                    providerInfo.Type == typeof(MasterPageBuildProvider) ||
                    providerInfo.Type == typeof(IgnoreFileBuildProvider)) {
                    createStub = false;
                }

                return false;
            }

            // If the extension is registered as a compiler extension, don't copy
            if (config.GetCompilerInfoFromExtension(extension, false /*throwOnFail*/) != null) {
                return false;
            }

            // Skip the copying for asax and skin files, which are not static even though they
            // don't have a registered BuildProvider (but don't skip .skin files during
            // updatable precomp).
            // 
            if (StringUtil.EqualsIgnoreCase(extension, ".asax"))
                return false;
            if (!updatable && StringUtil.EqualsIgnoreCase(extension, ThemeDirectoryCompiler.skinExtension))
                return false;

            //
            // If there is no BuildProvider registered, it's a static file, and should be copied
            //

            return true;
        }

        internal static Type LoadTypeWithChecks(string typeName, Type requiredBaseType, Type requiredBaseType2, ConfigurationElement elem, string propertyName) {
            Type t = ConfigUtil.GetType(typeName, propertyName, elem);

            if (requiredBaseType2 == null) {
                ConfigUtil.CheckAssignableType(requiredBaseType, t, elem, propertyName);
            }
            else {
                ConfigUtil.CheckAssignableType(requiredBaseType, requiredBaseType2, t, elem, propertyName);
            }

            return t;
        }

        // Devdiv Bug 57600
        // We need to use the constructor with ProviderOptions to get the v3.5/v4.0 compiler that was possibly set in config.
        // We first check if there is any providerOptions and invoke the constructor if so.
        // Otherwise, we fall back to the default constructor.
        internal static CodeDomProvider CreateCodeDomProvider(Type codeDomProviderType) {
            CodeDomProvider codeDomProvider = CreateCodeDomProviderWithPropertyOptions(codeDomProviderType);
            if (codeDomProvider != null) {
                return codeDomProvider;
            }
            return (CodeDomProvider)Activator.CreateInstance(codeDomProviderType);
        }

        internal static CodeDomProvider CreateCodeDomProviderNonPublic(Type codeDomProviderType) {
            CodeDomProvider codeDomProvider = CreateCodeDomProviderWithPropertyOptions(codeDomProviderType);
            if (codeDomProvider != null) {
                return codeDomProvider;
            }
            return (CodeDomProvider)HttpRuntime.CreateNonPublicInstance(codeDomProviderType);
        }

        [ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]
        private static CodeDomProvider CreateCodeDomProviderWithPropertyOptions(Type codeDomProviderType) {
            // The following resembles the code in System.CodeDom.CompilerInfo.CreateProvider

            // Make a copy to avoid modifying the original.
            var originalProviderOptions = GetProviderOptions(codeDomProviderType);
            IDictionary<string, string> providerOptions = null;
            if (originalProviderOptions != null) {
                providerOptions = new Dictionary<string, string>(originalProviderOptions);
            } else {
                providerOptions = new Dictionary<string, string>();
            }
            
            // Block CompilerDirectoryPath if we are in partial trust
            CheckCompilerDirectoryPathAllowed(providerOptions);

            // Check whether the user supplied the compilerDirectoryPath or was it added by us
            bool addedCompilerDirectoryPath = false;

            if (MultiTargetingUtil.IsTargetFramework20) {
                // If the target framework is v2.0, there won't be any codedom settings, so we need
                // to explicitly set the compiler to be the v2.0 compiler using compilerVersion=v2.0.
                providerOptions["CompilerVersion"] = "v2.0";
            }
            else if (MultiTargetingUtil.IsTargetFramework35) {
                // We need to explicitly set to v3.5, as it is possible for the
                // user to only have specified it for one compiler but not 
                // the other.
                // Dev10 bug 809212
                providerOptions["CompilerVersion"] = "v3.5";
            }
            else {
                // If we are targeting 4.0 but the compiler version is less than 4.0, set it to 4.0.
                // This can happen if a user tries to run a 2.0/3.5 web site in a 4.0 application pool without
                // upgrading it, and the codedom section still has 3.5 as the compilerVersion,
                // so we have to set the compilerVersion to 4.0 explicitly.
                string version = GetCompilerVersion(codeDomProviderType);
                Version v = GetVersionFromVString(version);
                if (v != null && v < MultiTargetingUtil.Version40) {
                    providerOptions["CompilerVersion"] = "v4.0";
                }
            }

            if (providerOptions != null && providerOptions.Count > 0) {
                Debug.Assert(codeDomProviderType != null, "codeDomProviderType should not be null");
                // Check whether the codedom provider supports a constructor that takes in providerOptions.
                // Currently only VB and C# support providerOptions for sure, while others such as JScript might not.
                ConstructorInfo ci = codeDomProviderType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) });
                CodeDomProvider provider = null;
                if (ci != null) {
                    // First, obtain the language for the given codedom provider type.
                    CodeDomProvider defaultProvider = (CodeDomProvider)Activator.CreateInstance(codeDomProviderType);
                    string extension = defaultProvider.FileExtension;
                    var language = CodeDomProvider.GetLanguageFromExtension(extension);
                    // Then, use the new createProvider API to create an instance.
                    provider = CodeDomProvider.CreateProvider(language, providerOptions);
                }
                // Restore the provider options if we previously manually added the compilerDirectoryPath.
                // Otherwise, we might incorrectly invalidate the compilerDirectoryPath in medium trust (Dev10 bug 550299).
                if (addedCompilerDirectoryPath) {
                    providerOptions.Remove(CompilerDirectoryPath);
                }
                return provider;
            }

            return null;
        }

        [ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]
        internal static IDictionary<string, string> GetProviderOptions(Type codeDomProviderType) {
            // Using reflection to get the property for the time being.
            // This could simply return CompilerInfo.PropertyOptions if it goes public in future.
            CodeDomProvider provider = (CodeDomProvider)Activator.CreateInstance(codeDomProviderType);
            string extension = provider.FileExtension;
            if (CodeDomProvider.IsDefinedExtension(extension)) {
                CompilerInfo ci = CodeDomProvider.GetCompilerInfo(CodeDomProvider.GetLanguageFromExtension(extension));
                return GetProviderOptions(ci);
            }
            return null;
        }

        [ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]
        private static IDictionary<string, string> GetProviderOptions(CompilerInfo ci) {
            Debug.Assert(ci != null, "CompilerInfo ci should not be null");
            PropertyInfo pi = ci.GetType().GetProperty("ProviderOptions",
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if (pi != null)
                return (IDictionary<string, string>)pi.GetValue(ci, null);
            return null;
        }

        /// <summary>
        /// Returns the compilerVersion providerOption specified for the codedom provider type.
        /// Returns null if the providerOption is not found.
        /// </summary>
        internal static string GetCompilerVersion(Type codeDomProviderType) {
            return GetProviderOption(codeDomProviderType, "CompilerVersion");
        }

        /// <summary>
        /// Returns the value of the providerOption specified for the codedom provider type.
        /// Returns null if the providerOption is not found.
        /// </summary>
        internal static string GetProviderOption(Type codeDomProviderType, string providerOption) {
            IDictionary<string, string> providerOptions = CompilationUtil.GetProviderOptions(codeDomProviderType);
            if (providerOptions != null) {
                string version;
                if (providerOptions.TryGetValue(providerOption, out version)) {
                    return version;
                }
            }
            return null;
        }        

        /// <summary>
        /// Returns true if the string matches "v3.5" exactly.
        /// </summary>
        internal static bool IsCompilerVersion35(string compilerVersion) {
            if (compilerVersion == "v3.5") {
                return true;
            }
            return false;
        }

        /// <summary>
        /// This returns true only if the codedom CompilverVersion provider option is exactly v3.5.
        /// </summary>
        internal static bool IsCompilerVersion35(Type codeDomProviderType) {
            string compilerVersion = GetCompilerVersion(codeDomProviderType);
            bool result = IsCompilerVersion35(compilerVersion);
            return result;
        }

        /// <summary>
        /// Returns true if the codedom CompilerVersion provider option is at least v3.5.
        /// </summary>
        /// <param name="codeDomProviderType"></param>
        /// <returns></returns>
        internal static bool IsCompilerVersion35OrAbove(Type codeDomProviderType) {
            string compilerVersion = GetCompilerVersion(codeDomProviderType);
            if (IsCompilerVersion35(compilerVersion)) {
                return true;
            }
            // The compilerVersion provider option is known to exist only for v3.5.
            // If it does not exist, then we need to rely on the target framework version to
            // determine whether we need to use the 2.0 or 4.0 compiler.
            if (MultiTargetingUtil.IsTargetFramework20) {
                return false;
            }

            // If it isn't 2.0 or 3.5, assume it is 4.0 and above.
            return true;
        }

        /// <summary>
        /// Returns true if the codedom provider has warnAsError set to true
        /// </summary>
        internal static bool WarnAsError(Type codeDomProviderType) {
            string value = GetProviderOption(codeDomProviderType, "WarnAsError");
            bool result;
            if (value != null && bool.TryParse(value, out result)) {
                return result;
            }

            // Assume false if the value wasn't set 
            return false;
        }

        // Returns the version when given string of form "v4.0"
        internal static Version GetVersionFromVString(string version) {
            if (string.IsNullOrEmpty(version)) {
                return null;
            }
            Debug.Assert(version.Length > 1, "Version has invalid length");
            return new Version(version.Substring(1));
        }

        // Returns maximum number of concurrent compilations
        internal static int MaxConcurrentCompilations {
            get {
                if (_maxConcurrentCompilations == 0) {
                    int maxConcurrentCompilations;

                    if (AppSettings.MaxConcurrentCompilations.HasValue && AppSettings.MaxConcurrentCompilations.Value >= 0) {
                        maxConcurrentCompilations = AppSettings.MaxConcurrentCompilations.Value;
                    }
                    else {
                        CompilationSection config = MTConfigUtil.GetCompilationAppConfig();
                        maxConcurrentCompilations = config.MaxConcurrentCompilations;
                    }

                    if (maxConcurrentCompilations <= 0) {
                        maxConcurrentCompilations = Environment.ProcessorCount;
                    }

                    Interlocked.CompareExchange(ref _maxConcurrentCompilations, maxConcurrentCompilations, 0);
                }

                return _maxConcurrentCompilations;
            }
        }
    }
}