Ejemplo n.º 1
0
        /// <summary>
        /// Resolves metadata references stored in command line arguments and reports errors for those that can't be resolved.
        /// </summary>
        internal List <MetadataReference> ResolveMetadataReferences(
            MetadataReferenceResolver externalReferenceResolver,
            MetadataReferenceProvider metadataProvider,
            List <DiagnosticInfo> diagnostics,
            AssemblyIdentityComparer assemblyIdentityComparer,
            TouchedFileLogger touchedFiles,
            out MetadataReferenceResolver referenceDirectiveResolver)
        {
            using (Logger.LogBlock(FunctionId.Common_CommandLineCompiler_ResolveMetadataReferences))
            {
                List <MetadataReference> resolved = new List <MetadataReference>();
                ResolveMetadataReferencesFromArguments(externalReferenceResolver, metadataProvider, diagnostics, resolved);

                if (Arguments.IsInteractive)
                {
                    referenceDirectiveResolver = externalReferenceResolver;
                }
                else
                {
                    // when compiling into an assembly (csc/vbc) we only allow #r that match references given on command line:
                    referenceDirectiveResolver = new ExistingReferencesResolver(
                        resolved.Where(r => r.Properties.Kind == MetadataImageKind.Assembly).OfType <MetadataFileReference>().AsImmutable(),
                        Arguments.ReferencePaths,
                        Arguments.BaseDirectory,
                        assemblyIdentityComparer,
                        touchedFiles);
                }

                return(resolved);
            }
        }
        /// <summary>
        /// Resolves a command line metadata reference with the given <paramref name="resolvedFullPath"/> to a <see cref="MetadataReference"/>.
        /// </summary>
        private static MetadataReference ResolveMetadataFile(
            string resolvedFullPath,
            MetadataReferenceProperties properties,
            MetadataReferenceProvider metadataProvider,
            string display,
            List <DiagnosticInfo> diagnosticsOpt,
            CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(resolvedFullPath != null);
            Debug.Assert(PathUtilities.IsAbsolute(resolvedFullPath));
            Debug.Assert(display != null);

            if (diagnosticsOpt == null)
            {
                return(metadataProvider.GetReference(resolvedFullPath, properties));
            }

            MetadataReference reference;
            Diagnostic        diagnostic;

            if (PortableExecutableReference.TryGetMetadata(
                    properties.Kind == MetadataImageKind.Assembly,
                    messageProviderOpt,
                    Location.None,
                    display,
                    () => metadataProvider.GetReference(resolvedFullPath, properties),
                    out reference,
                    out diagnostic))
            {
                return(reference);
            }

            diagnosticsOpt.Add(((DiagnosticWithInfo)diagnostic).Info);
            return(new UnresolvedMetadataReference(resolvedFullPath, properties));
        }
Ejemplo n.º 3
0
 // Expects correct arguments.
 internal CompilationOptions(
     OutputKind outputKind,
     string moduleName,
     string mainTypeName,
     string scriptClassName,
     string cryptoKeyContainer,
     string cryptoKeyFile,
     bool?delaySign,
     bool optimize,
     bool checkOverflow,
     int fileAlignment,
     ulong baseAddress,
     Platform platform,
     ReportDiagnostic generalDiagnosticOption,
     int warningLevel,
     IEnumerable <KeyValuePair <string, ReportDiagnostic> > specificDiagnosticOptions,
     bool highEntropyVirtualAddressSpace,
     DebugInformationKind debugInformationKind,
     SubsystemVersion subsystemVersion,
     bool concurrentBuild,
     XmlReferenceResolver xmlReferenceResolver,
     SourceReferenceResolver sourceReferenceResolver,
     MetadataReferenceResolver metadataReferenceResolver,
     MetadataReferenceProvider metadataReferenceProvider,
     AssemblyIdentityComparer assemblyIdentityComparer,
     StrongNameProvider strongNameProvider,
     MetadataImportOptions metadataImportOptions)
 {
     Initialize(
         outputKind, moduleName, mainTypeName, scriptClassName, cryptoKeyContainer, cryptoKeyFile, delaySign, optimize, checkOverflow, fileAlignment,
         baseAddress, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, highEntropyVirtualAddressSpace, debugInformationKind,
         subsystemVersion, concurrentBuild, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, metadataReferenceProvider, assemblyIdentityComparer, strongNameProvider,
         metadataImportOptions);
 }
Ejemplo n.º 4
0
        internal void Initialize(
            OutputKind outputKind,
            string moduleName,
            string mainTypeName,
            string scriptClassName,
            string cryptoKeyContainer,
            string cryptoKeyFile,
            bool?delaySign,
            bool optimize,
            bool checkOverflow,
            int fileAlignment,
            ulong baseAddress,
            Platform platform,
            ReportDiagnostic generalDiagnosticOption,
            int warningLevel,
            IEnumerable <KeyValuePair <string, ReportDiagnostic> > specificDiagnosticOptions,
            bool highEntropyVirtualAddressSpace,
            DebugInformationKind debugInformationKind,
            SubsystemVersion subsystemVersion,
            bool concurrentBuild,
            FileResolver fileResolver,
            MetadataReferenceProvider metadataReferenceProvider,
            AssemblyIdentityComparer assemblyIdentityComparer,
            StrongNameProvider strongNameProvider,
            MetadataImportOptions metadataImportOptions)
        {
            this.OutputKind                     = outputKind;
            this.ModuleName                     = moduleName;
            this.MainTypeName                   = mainTypeName;
            this.ScriptClassName                = scriptClassName;
            this.CryptoKeyContainer             = cryptoKeyContainer;
            this.CryptoKeyFile                  = cryptoKeyFile;
            this.DelaySign                      = delaySign;
            this.CheckOverflow                  = checkOverflow;
            this.FileAlignment                  = fileAlignment;
            this.BaseAddress                    = baseAddress;
            this.Platform                       = platform;
            this.GeneralDiagnosticOption        = generalDiagnosticOption;
            this.WarningLevel                   = warningLevel;
            this.SpecificDiagnosticOptions      = specificDiagnosticOptions.ToImmutableDictionaryOrEmpty();
            this.HighEntropyVirtualAddressSpace = highEntropyVirtualAddressSpace;
            this.DebugInformationKind           = debugInformationKind;
            this.Optimize                       = optimize;
            this.ConcurrentBuild                = concurrentBuild;
            this.SubsystemVersion               = subsystemVersion;
            this.FileResolver                   = fileResolver ?? FileResolver.Default;
            this.MetadataReferenceProvider      = metadataReferenceProvider ?? MetadataReferenceProvider.Default;
            this.StrongNameProvider             = strongNameProvider;
            this.AssemblyIdentityComparer       = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default;
            this.MetadataImportOptions          = metadataImportOptions;

            this.lazyErrors = new Lazy <ImmutableArray <Diagnostic> >(() =>
            {
                var builder = ArrayBuilder <Diagnostic> .GetInstance();
                ValidateOptions(builder);
                return(builder.ToImmutableAndFree());
            });
        }
Ejemplo n.º 5
0
 public SolutionServices(Workspace workspace, IWorkspaceServiceProvider workspaceServices)
 {
     this.Workspace                 = workspace;
     this.WorkspaceServices         = workspaceServices;
     this.LanguageServicesFactory   = WorkspaceServices.GetService <ILanguageServiceProviderFactory>();
     this.TemporaryStorage          = WorkspaceServices.GetService <ITemporaryStorageService>();
     this.TextFactory               = WorkspaceServices.GetService <ITextFactoryService>();
     this.TextCache                 = WorkspaceServices.GetService <ITextCacheService>();
     this.CompilationCacheService   = WorkspaceServices.GetService <ICompilationCacheService>();
     this.MetadataReferenceProvider = WorkspaceServices.GetService <IMetadataReferenceProviderService>().GetProvider();
 }
Ejemplo n.º 6
0
 public SolutionServices(Workspace workspace, IWorkspaceServiceProvider workspaceServices)
 {
     this.Workspace = workspace;
     this.WorkspaceServices = workspaceServices;
     this.LanguageServicesFactory = WorkspaceServices.GetService<ILanguageServiceProviderFactory>();
     this.TemporaryStorage = WorkspaceServices.GetService<ITemporaryStorageService>();
     this.TextFactory = WorkspaceServices.GetService<ITextFactoryService>();
     this.TextCache = WorkspaceServices.GetService<ITextCacheService>();
     this.CompilationCacheService = WorkspaceServices.GetService<ICompilationCacheService>();
     this.MetadataReferenceProvider = WorkspaceServices.GetService<IMetadataReferenceProviderService>().GetProvider();
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns false if there were unresolved references in arguments, true otherwise.
        /// </summary>
        protected virtual bool ResolveMetadataReferencesFromArguments(MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnostics, FileResolver externalReferenceResolver, List <MetadataReference> resolved)
        {
            bool result = true;

            foreach (var reference in Arguments.ResolveMetadataReferences(externalReferenceResolver, metadataProvider, diagnostics, MessageProvider))
            {
                if (!reference.IsUnresolved)
                {
                    resolved.Add(reference);
                }
                else
                {
                    result = false;
                    Debug.Assert(diagnostics.Any());
                }
            }

            return(result);
        }
        /// <summary>
        /// Resolves this command line metadata reference to a <see cref="MetadataReference"/> using given file resolver and metadata provider.
        /// </summary>
        /// <exception cref="BadImageFormatException">If the PE image format is invalid and <paramref name="diagnosticsOpt"/> is null.</exception>
        /// <exception cref="FileNotFoundException">If the Metadata file could not be found and <paramref name="diagnosticsOpt"/> is null.</exception>
        /// <exception cref="IOException">If error reading the file from disk and <paramref name="diagnosticsOpt"/> is null.</exception>
        /// <remarks>
        /// NOTE: Other exceptions, apart from those mentioned above, may be generated by the fileResolver or the metadata provider. These are not handled by this method.
        /// </remarks>
        /// <param name="fileResolver">The file resolver to use for assembly name and relative path resolution.</param>
        /// <param name="metadataProvider">Uses to create metadata references from resolved full paths.</param>
        /// <param name="diagnosticsOpt">Optional diagnostics list for storing diagnostics.</param>
        /// <param name="messageProviderOpt">Optional <see cref="CommonMessageProvider"/> for generating diagnostics.</param>
        /// <returns>Returns resolved metadata reference or <see cref="UnresolvedMetadataReference"/>.</returns>
        internal MetadataReference Resolve(FileResolver fileResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(fileResolver != null);
            Debug.Assert(metadataProvider != null);

            string path;

            if (this.IsAssemblyName)
            {
                path = fileResolver.ResolveAssemblyName(this.reference);
                if (path == null)
                {
                    if (diagnosticsOpt != null)
                    {
                        diagnosticsOpt.Add(new DiagnosticInfo(messageProviderOpt, messageProviderOpt.ERR_MetadataFileNotFound, this.reference));
                    }

                    return(new UnresolvedMetadataReference(this.reference, this.properties));
                }
            }
            else
            {
                path = this.reference;
            }

            // use search paths and base path of the resolver - usually these are the same as the paths stored on the arguments:
            string fullPath = fileResolver.ResolveMetadataFileChecked(path, baseFilePath: null);

            if (fullPath == null)
            {
                if (diagnosticsOpt != null)
                {
                    diagnosticsOpt.Add(new DiagnosticInfo(messageProviderOpt, messageProviderOpt.ERR_MetadataFileNotFound, this.reference));
                }

                return(new UnresolvedMetadataReference(this.reference, this.properties));
            }
            else
            {
                return(ResolveMetadataFile(fullPath, this.properties, metadataProvider, this.reference, diagnosticsOpt, messageProviderOpt));
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        internal IEnumerable <MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);

            foreach (CommandLineReference cmdReference in MetadataReferences)
            {
                yield return(ResolveMetadataReference(cmdReference, metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt) ??
                             new UnresolvedMetadataReference(cmdReference.Reference, cmdReference.Properties));
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        internal IEnumerable<MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List<DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);

            foreach (CommandLineReference cmdReference in MetadataReferences)
            {
                yield return ResolveMetadataReference(cmdReference, metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt) ?? 
                    new UnresolvedMetadataReference(cmdReference.Reference, cmdReference.Properties);
            }
        }
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// </summary>
        /// <param name="metadataResolver"><see cref="MetadataReferenceResolver"/> to use for assembly name and relative path resolution.</param>
        /// <param name="metadataProvider"><see cref="MetadataReferenceProvider"/> to read metadata from resolved paths.</param>
        /// <returns>Yields resolved metadata references or <see cref="UnresolvedMetadataReference"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="metadataResolver"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="metadataProvider"/> is null.</exception>
        public IEnumerable<MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider)
        {
            if (metadataResolver == null)
            {
                throw new ArgumentNullException("metadataResolver");
            }

            if (metadataProvider == null)
            {
                throw new ArgumentNullException("metadataProvider");
            }

            return ResolveMetadataReferences(metadataResolver, metadataProvider, diagnosticsOpt: null, messageProviderOpt: null);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Resolves metadata references stored in command line arguments and reports errors for those that can't be resolved.
        /// </summary>
        internal List<MetadataReference> ResolveMetadataReferences(
            MetadataReferenceResolver externalReferenceResolver,
            MetadataReferenceProvider metadataProvider,
            List<DiagnosticInfo> diagnostics,
            AssemblyIdentityComparer assemblyIdentityComparer,
            TouchedFileLogger touchedFiles,
            out MetadataReferenceResolver referenceDirectiveResolver)
        {
            using (Logger.LogBlock(FunctionId.Common_CommandLineCompiler_ResolveMetadataReferences))
            {
                List<MetadataReference> resolved = new List<MetadataReference>();
                ResolveMetadataReferencesFromArguments(externalReferenceResolver, metadataProvider, diagnostics, resolved);

                if (Arguments.IsInteractive)
                {
                    referenceDirectiveResolver = externalReferenceResolver;
                }
                else
                {
                    // when compiling into an assembly (csc/vbc) we only allow #r that match references given on command line:
                    referenceDirectiveResolver = new ExistingReferencesResolver(
                        resolved.Where(r => r.Properties.Kind == MetadataImageKind.Assembly).OfType<MetadataFileReference>().AsImmutable(),
                        Arguments.ReferencePaths,
                        Arguments.BaseDirectory,
                        assemblyIdentityComparer,
                        touchedFiles);
                }

                return resolved;
            }
        }
        internal virtual bool ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List<DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt, List<MetadataReference> resolved)
        {
            bool result = true;

            foreach (CommandLineReference cmdReference in MetadataReferences)
            {
                var r = ResolveMetadataReference(cmdReference, metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt);
                if (r != null)
                {
                    resolved.Add(r);
                }
                else
                {
                    result = false;
                    if (diagnosticsOpt == null)
                    {
                        // no diagnostic, so leaved unresolved reference in list
                        resolved.Add(new UnresolvedMetadataReference(cmdReference.Reference, cmdReference.Properties));
                    }
                }
            }

            return result;
        }
 protected abstract CompilationOptions CommonWithMetadataReferenceProvider(MetadataReferenceProvider provider);
Ejemplo n.º 15
0
 // Expects correct arguments.
 internal CompilationOptions(
     OutputKind outputKind,
     string moduleName,
     string mainTypeName,
     string scriptClassName,
     string cryptoKeyContainer,
     string cryptoKeyFile,
     bool? delaySign,
     bool optimize,
     bool checkOverflow,
     int fileAlignment,
     ulong baseAddress,
     Platform platform,
     ReportDiagnostic generalDiagnosticOption,
     int warningLevel,
     IEnumerable<KeyValuePair<string, ReportDiagnostic>> specificDiagnosticOptions,
     bool highEntropyVirtualAddressSpace,
     DebugInformationKind debugInformationKind,
     SubsystemVersion subsystemVersion,
     bool concurrentBuild,
     XmlReferenceResolver xmlReferenceResolver,
     SourceReferenceResolver sourceReferenceResolver,
     MetadataReferenceResolver metadataReferenceResolver,
     MetadataReferenceProvider metadataReferenceProvider,
     AssemblyIdentityComparer assemblyIdentityComparer,
     StrongNameProvider strongNameProvider,
     MetadataImportOptions metadataImportOptions)
 {
     Initialize(
         outputKind, moduleName, mainTypeName, scriptClassName, cryptoKeyContainer, cryptoKeyFile, delaySign, optimize, checkOverflow, fileAlignment, 
         baseAddress, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, highEntropyVirtualAddressSpace, debugInformationKind, 
         subsystemVersion, concurrentBuild, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, metadataReferenceProvider, assemblyIdentityComparer, strongNameProvider, 
         metadataImportOptions);
 }
Ejemplo n.º 16
0
 public CompilationOptions WithMetadataReferenceProvider(MetadataReferenceProvider provider)
 {
     return(CommonWithMetadataReferenceProvider(provider));
 }
Ejemplo n.º 17
0
        // TODO: change to private protected when available
        internal static MetadataReference ResolveMetadataReference(CommandLineReference cmdReference, MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);
            Debug.Assert((diagnosticsOpt == null) == (messageProviderOpt == null));

            string resolvedPath = metadataResolver.ResolveReference(cmdReference.Reference, baseFilePath: null);

            if (resolvedPath == null)
            {
                if (diagnosticsOpt != null)
                {
                    diagnosticsOpt.Add(new DiagnosticInfo(messageProviderOpt, messageProviderOpt.ERR_MetadataFileNotFound, cmdReference.Reference));
                }

                return(null);
            }

            try
            {
                return(metadataProvider.GetReference(resolvedPath, cmdReference.Properties));
            }
            catch (Exception e) if (diagnosticsOpt != null && (e is BadImageFormatException || e is IOException))
                {
                    var diagnostic = PortableExecutableReference.ExceptionToDiagnostic(e, messageProviderOpt, Location.None, cmdReference.Reference, cmdReference.Properties.Kind);
                    diagnosticsOpt.Add(((DiagnosticWithInfo)diagnostic).Info);
                    return(null);
                }
        }
Ejemplo n.º 18
0
        internal virtual bool ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt, List <MetadataReference> resolved)
        {
            bool result = true;

            foreach (CommandLineReference cmdReference in MetadataReferences)
            {
                var r = ResolveMetadataReference(cmdReference, metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt);
                if (r != null)
                {
                    resolved.Add(r);
                }
                else
                {
                    result = false;
                    if (diagnosticsOpt == null)
                    {
                        // no diagnostic, so leaved unresolved reference in list
                        resolved.Add(new UnresolvedMetadataReference(cmdReference.Reference, cmdReference.Properties));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        /// <remarks>
        /// called by CommonCompiler with diagnostics and message provider
        /// </remarks>
        internal IEnumerable <MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);

            var resolved = new List <MetadataReference>();

            this.ResolveMetadataReferences(metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt, resolved);

            return(resolved);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// </summary>
        /// <param name="metadataResolver"><see cref="MetadataReferenceResolver"/> to use for assembly name and relative path resolution.</param>
        /// <param name="metadataProvider"><see cref="MetadataReferenceProvider"/> to read metadata from resolved paths.</param>
        /// <returns>Yields resolved metadata references or <see cref="UnresolvedMetadataReference"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="metadataResolver"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="metadataProvider"/> is null.</exception>
        public IEnumerable <MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider)
        {
            if (metadataResolver == null)
            {
                throw new ArgumentNullException("metadataResolver");
            }

            if (metadataProvider == null)
            {
                throw new ArgumentNullException("metadataProvider");
            }

            return(ResolveMetadataReferences(metadataResolver, metadataProvider, diagnosticsOpt: null, messageProviderOpt: null));
        }
        // TODO: change to private protected when available
        internal static MetadataReference ResolveMetadataReference(CommandLineReference cmdReference, MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List<DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);
            Debug.Assert((diagnosticsOpt == null) == (messageProviderOpt == null));

            string resolvedPath = metadataResolver.ResolveReference(cmdReference.Reference, baseFilePath: null);
            if (resolvedPath == null)
            {
                if (diagnosticsOpt != null)
                {
                    diagnosticsOpt.Add(new DiagnosticInfo(messageProviderOpt, messageProviderOpt.ERR_MetadataFileNotFound, cmdReference.Reference));
                }

                return null;
            }

            try
            {
                return metadataProvider.GetReference(resolvedPath, cmdReference.Properties);
            }
            catch (Exception e) if (diagnosticsOpt != null && (e is BadImageFormatException || e is IOException))
            {
                var diagnostic = PortableExecutableReference.ExceptionToDiagnostic(e, messageProviderOpt, Location.None, cmdReference.Reference, cmdReference.Properties.Kind);
                diagnosticsOpt.Add(((DiagnosticWithInfo)diagnostic).Info);
                return null;
            }
        }

        #endregion

        #region Analyzer References

        /// <summary>
        /// Resolves analyzer references stored in <see cref="AnalyzerReferences"/> using given file resolver.
        /// </summary>
        /// <returns>Yields resolved <see cref="AnalyzerFileReference"/> or <see cref="UnresolvedAnalyzerReference"/>.</returns>
        public IEnumerable<AnalyzerReference> ResolveAnalyzerReferences()
        {
            foreach (CommandLineAnalyzerReference cmdLineReference in AnalyzerReferences)
            {
                yield return ResolveAnalyzerReference(cmdLineReference) 
                    ?? (AnalyzerReference)new UnresolvedAnalyzerReference(cmdLineReference.FilePath);
            }
        }
        // Expects correct arguments.
        internal CompilationOptions(
            OutputKind outputKind,
            string moduleName,
            string mainTypeName,
            string scriptClassName,
            string cryptoKeyContainer,
            string cryptoKeyFile,
            bool? delaySign,
            OptimizationLevel optimizationLevel,
            bool checkOverflow,
            int fileAlignment,
            ulong baseAddress,
            Platform platform,
            ReportDiagnostic generalDiagnosticOption,
            int warningLevel,
            ImmutableDictionary<string, ReportDiagnostic> specificDiagnosticOptions,
            bool highEntropyVirtualAddressSpace,
            SubsystemVersion subsystemVersion,
            bool concurrentBuild,
            XmlReferenceResolver xmlReferenceResolver,
            SourceReferenceResolver sourceReferenceResolver,
            MetadataReferenceResolver metadataReferenceResolver,
            MetadataReferenceProvider metadataReferenceProvider,
            AssemblyIdentityComparer assemblyIdentityComparer,
            StrongNameProvider strongNameProvider,
            MetadataImportOptions metadataImportOptions,
            ImmutableArray<string> features)
        {
            this.OutputKind = outputKind;
            this.ModuleName = moduleName;
            this.MainTypeName = mainTypeName;
            this.ScriptClassName = scriptClassName;
            this.CryptoKeyContainer = cryptoKeyContainer;
            this.CryptoKeyFile = cryptoKeyFile;
            this.DelaySign = delaySign;
            this.CheckOverflow = checkOverflow;
            this.FileAlignment = fileAlignment;
            this.BaseAddress = baseAddress;
            this.Platform = platform;
            this.GeneralDiagnosticOption = generalDiagnosticOption;
            this.WarningLevel = warningLevel;
            this.SpecificDiagnosticOptions = specificDiagnosticOptions;
            this.HighEntropyVirtualAddressSpace = highEntropyVirtualAddressSpace;
            this.OptimizationLevel = optimizationLevel;
            this.ConcurrentBuild = concurrentBuild;
            this.SubsystemVersion = subsystemVersion;
            this.XmlReferenceResolver = xmlReferenceResolver;
            this.SourceReferenceResolver = sourceReferenceResolver;
            this.MetadataReferenceResolver = metadataReferenceResolver;
            this.MetadataReferenceProvider = metadataReferenceProvider;
            this.StrongNameProvider = strongNameProvider;
            this.AssemblyIdentityComparer = assemblyIdentityComparer ?? AssemblyIdentityComparer.Default;
            this.MetadataImportOptions = metadataImportOptions;
            this.Features = features;

            this.lazyErrors = new Lazy<ImmutableArray<Diagnostic>>(() =>
            {
                var builder = ArrayBuilder<Diagnostic>.GetInstance();
                ValidateOptions(builder);
                return builder.ToImmutableAndFree();
            });
        }
 public CompilationOptions WithMetadataReferenceProvider(MetadataReferenceProvider provider)
 {
     return CommonWithMetadataReferenceProvider(provider);
 }
        /// <summary>
        /// Resolves metadata references stored in <see cref="P:MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        internal IEnumerable <MetadataReference> ResolveMetadataReferences(FileResolver fileResolver, MetadataReferenceProvider metadataProvider, List <DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            if (fileResolver == null)
            {
                fileResolver = new FileResolver(ReferencePaths, BaseDirectory);
            }

            if (metadataProvider == null)
            {
                metadataProvider = MetadataReferenceProvider.Default;
            }

            foreach (CommandLineReference cmdLineReference in MetadataReferences)
            {
                yield return(cmdLineReference.Resolve(fileResolver, metadataProvider, diagnosticsOpt, messageProviderOpt));
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Returns false if there were unresolved references in arguments, true otherwise.
        /// </summary>
        protected virtual bool ResolveMetadataReferencesFromArguments(MetadataReferenceResolver externalReferenceResolver, MetadataReferenceProvider metadataProvider, List<DiagnosticInfo> diagnostics, List<MetadataReference> resolved)
        {
            bool result = true;

            foreach (var reference in Arguments.ResolveMetadataReferences(externalReferenceResolver, metadataProvider, diagnostics, MessageProvider))
            {
                if (!reference.IsUnresolved)
                {
                    resolved.Add(reference);
                }
                else
                {
                    result = false;
                    Debug.Assert(diagnostics.Any());
                }
            }

            return result;
        }
        /// <summary>
        /// Resolves metadata references stored in <see cref="MetadataReferences"/> using given file resolver and metadata provider.
        /// If a non-null diagnostic bag <paramref name="diagnosticsOpt"/> is provided, it catches exceptions that may be generated while reading the metadata file and
        /// reports appropriate diagnostics.
        /// Otherwise, if <paramref name="diagnosticsOpt"/> is null, the exceptions are unhandled.
        /// </summary>
        /// <remarks>
        /// called by CommonCompiler with diagnostics and message provider
        /// </remarks>
        internal IEnumerable<MetadataReference> ResolveMetadataReferences(MetadataReferenceResolver metadataResolver, MetadataReferenceProvider metadataProvider, List<DiagnosticInfo> diagnosticsOpt, CommonMessageProvider messageProviderOpt)
        {
            Debug.Assert(metadataResolver != null);
            Debug.Assert(metadataProvider != null);

            var resolved = new List<MetadataReference>();
            this.ResolveMetadataReferences(metadataResolver, metadataProvider, diagnosticsOpt, messageProviderOpt, resolved);

            return resolved;
        }
Ejemplo n.º 27
0
 protected abstract CompilationOptions CommonWithMetadataReferenceProvider(MetadataReferenceProvider provider);
 /// <summary>
 /// Resolves metadata references stored in <see cref="P:MetadataReferences"/> using given file resolver and metadata provider.
 /// </summary>
 /// <param name="fileResolver">The file resolver to use for assembly name and relative path resolution, or null to use a default.</param>
 /// <param name="metadataProvider">Uses to create metadata references from resolved full paths, or null to use a default.</param>
 /// <returns>Yields resolved metadata references or <see cref="UnresolvedMetadataReference"/>.</returns>
 public IEnumerable <MetadataReference> ResolveMetadataReferences(FileResolver fileResolver = null, MetadataReferenceProvider metadataProvider = null)
 {
     return(ResolveMetadataReferences(fileResolver, metadataProvider, diagnosticsOpt: null, messageProviderOpt: null));
 }