private Stream OpenFile(string filePath, TextWriter consoleOutput, FileMode mode = FileMode.Open, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.None)
        {
            try
            {
                return(FileOpen(filePath, mode, access, share));
            }
            catch (Exception e)
            {
                if (consoleOutput != null)
                {
                    // TODO: distinct error message?
                    DiagnosticInfo diagnosticInfo = ReflDiagnosticInfo.ctor(MessageProvider, (int)MessageProvider.GetFieldOrProperty <int>("ERR_OutputWriteFailed"), new[] { filePath, e.Message });
                    consoleOutput.WriteLine(diagnosticInfo.InvokeFunction("ToString", Culture));
                }

                return(null);
            }
        }
        private int RunCore(TextWriter consoleOutput, ErrorLogger2 errorLogger, CancellationToken cancellationToken)
        {
            Debug.Assert(!Arguments.GetFieldOrProperty <bool>("IsScriptRunner"));

            cancellationToken.ThrowIfCancellationRequested();

            if (Arguments.DisplayLogo)
            {
                PrintLogo(consoleOutput);
            }

            if (Arguments.DisplayHelp)
            {
                PrintHelp(consoleOutput);
                return(Succeeded);
            }

            if (ReportErrors(Arguments.Errors, consoleOutput, errorLogger))
            {
                return(Failed);
            }

            var touchedFilesLogger = (Arguments.TouchedFilesPath != null) ? new TouchedFileLogger() : null;

            Compilation compilation = CreateCompilation(consoleOutput, touchedFilesLogger, errorLogger);

            if (compilation == null)
            {
                return(Failed);
            }


            var diagnostics         = typeof(List <>).MakeGenericTypeFast(Refl.Type_DiagnosticInfo).InvokeFunction(".ctor");
            var analyzers           = ResolveAnalyzersAndGeneratorsFromArguments(diagnostics, MessageProvider, touchedFilesLogger);
            var additionalTextFiles = ResolveAdditionalFilesFromArguments(diagnostics, MessageProvider, touchedFilesLogger);

            if (ReportErrors((IEnumerable <DiagnosticInfo>)diagnostics, consoleOutput, errorLogger))
            {
                return(Failed);
            }

            cancellationToken.ThrowIfCancellationRequested();

            CancellationTokenSource analyzerCts     = null;
            AnalyzerManager         analyzerManager = null;
            AnalyzerDriver          analyzerDriver  = null;

            try
            {
                Func <ImmutableArray <Diagnostic> > getAnalyzerDiagnostics       = null;
                ConcurrentSet <Diagnostic>          analyzerExceptionDiagnostics = null;
                if (!analyzers.IsDefaultOrEmpty)
                {
                    analyzerCts     = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                    analyzerManager = new AnalyzerManager();
                    analyzerExceptionDiagnostics = new ConcurrentSet <Diagnostic>();
                    Action <Diagnostic> addExceptionDiagnostic = diagnostic => analyzerExceptionDiagnostics.Add(diagnostic);
                    var analyzerOptions = new AnalyzerOptions(ImmutableArray.CreateRange(additionalTextFiles.Select(x => (AdditionalText)x)));

                    analyzerDriver = ReflAnalyzerDriver.CreateAndAttachToCompilation(
                        compilation,
                        analyzers,
                        analyzerOptions,
                        analyzerManager,
                        addExceptionDiagnostic,
                        Arguments.ReportAnalyzer,
                        out compilation,
                        analyzerCts.Token);
                    getAnalyzerDiagnostics = () => ((Task <ImmutableArray <Diagnostic> >)analyzerDriver.InvokeFunction("GetDiagnosticsAsync", compilation)).Result;
                }

                // Print the diagnostics produced during the parsing stage and exit if there were any errors.
                if (ReportErrors(compilation.GetParseDiagnostics(), consoleOutput, errorLogger))
                {
                    return(Failed);
                }

                if (ReportErrors(compilation.GetDeclarationDiagnostics(), consoleOutput, errorLogger))
                {
                    return(Failed);
                }
                consoleOutput.WriteLine("Rewriting LINQ to procedural code...");
                var originalCompilation = compilation;

                var rewrittenLinqInvocations = 0;
                var rewrittenMethods         = 0;
                foreach (var syntaxTree in compilation.SyntaxTrees)
                {
                    var        rewriter = new LinqRewriter(originalCompilation.GetSemanticModel(syntaxTree));
                    var        root     = syntaxTree.GetRoot();
                    SyntaxNode rewritten;
                    try
                    {
                        rewritten = rewriter.Visit(root);
                    }
                    catch (Exception ex)
                    {
                        ReportErrors(new[] { rewriter.CreateDiagnosticForException(ex, syntaxTree.FilePath) }, consoleOutput, errorLogger);

                        var m = ex;
                        while (m != null)
                        {
                            Console.WriteLine(m);
                            Console.WriteLine(m.StackTrace);
                            m = m.InnerException;
                        }


                        return(Failed);
                    }

                    if (rewritten != root)
                    {
                        var originalOptions = (CSharp.CSharpParseOptions)root.SyntaxTree.Options;
                        var parseOptions    = ((CSharp.CSharpParseOptions)syntaxTree.Options).WithLanguageVersion(originalOptions.LanguageVersion);
                        var syntaxTree2     = syntaxTree.WithRootAndOptions(rewritten, parseOptions);

                        OriginalPaths[syntaxTree] = syntaxTree.FilePath;

                        //rewritten = rewritten.SyntaxTree.WithRootAndOptions
                        compilation = compilation.ReplaceSyntaxTree(syntaxTree, syntaxTree2);


                        rewrittenLinqInvocations += rewriter.RewrittenLinqQueries;
                        rewrittenMethods         += rewriter.RewrittenMethods;
                    }
                }

                var infoDescriptor = new DiagnosticDescriptor("REWRITE1001", "RoslynLinqRewrite statistics", "Rewritten {0} LINQ queries in {1} methods as procedural code.", "Compiler", DiagnosticSeverity.Info, true);
                var info           = Diagnostic.Create(infoDescriptor, Location.None, rewrittenLinqInvocations, rewrittenMethods);

                consoleOutput.WriteLine(DiagnosticFormatter.Format(info));

                var rewriteto = Environment.GetEnvironmentVariable("ROSLYN_LINQ_REWRITE_OUT_STATISTICS_TO");
                if (rewriteto != null)
                {
                    File.WriteAllText(rewriteto, string.Format("Rewritten {0} LINQ queries in {1} methods as procedural code.", rewrittenLinqInvocations, rewrittenMethods));
                }

                //consoleOutput.WriteLine(string.Format, rewrittenLinqInvocations, rewrittenMethods));

                var k = compilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).ToList();
                if (k.Count != 0)
                {
                    foreach (var err in k)
                    {
                        var errordescr = new DiagnosticDescriptor("REWRITE1002", "Could not compile rewritten method", "Could not compile rewritten method. Consider applying a [Shaman.Runtime.NoLinqRewrite] attribute. File: {0}. Code: {1}", "Compiler", DiagnosticSeverity.Error, true);
                        var m          = err.Location.SourceTree.GetRoot().FindNode(err.Location.SourceSpan);
                        var err2       = Diagnostic.Create(infoDescriptor, err.Location, err.Location.SourceTree.FilePath, m.ToString());

                        consoleOutput.WriteLine(DiagnosticFormatter.Format(info));

                        //System.Console.WriteLine("Could not compile rewritten method. Consider applying a [Shaman.Runtime.NoLinqRewrite] attribute.");

                        //System.Console.WriteLine(err.Location.SourceTree.FilePath);
                        //var z = m.FirstAncestorOrSelf<CSharp.Syntax.BaseMethodDeclarationSyntax>(x => x.Kind() == CSharp.SyntaxKind.MethodDeclaration);
                        //compilation.GetSemanticModel().GetEnclosingSymbol(err.Location.SourceSpan.Start)
                    }
                }
                if (ReportErrors(compilation.GetParseDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error), consoleOutput, errorLogger))
                {
                    return(Failed);
                }

                if (ReportErrors(compilation.GetDeclarationDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error), consoleOutput, errorLogger))
                {
                    return(Failed);
                }



                EmitResult emitResult;

                // NOTE: as native compiler does, we generate the documentation file
                // NOTE: 'in place', replacing the contents of the file if it exists

                string finalPeFilePath;
                string finalPdbFilePath;
                string finalXmlFilePath;

                Stream xmlStreamOpt = null;

                cancellationToken.ThrowIfCancellationRequested();

                finalXmlFilePath = Arguments.DocumentationPath;
                if (finalXmlFilePath != null)
                {
                    xmlStreamOpt = OpenFile(finalXmlFilePath, consoleOutput, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete);
                    if (xmlStreamOpt == null)
                    {
                        return(Failed);
                    }

                    xmlStreamOpt.SetLength(0);
                }

                cancellationToken.ThrowIfCancellationRequested();
                ImmutableArray <Diagnostic>  emitDiagnostics;
                IEnumerable <DiagnosticInfo> errors;
                using (var win32ResourceStreamOpt = GetWin32Resources(Arguments, compilation, out errors))
                    using (xmlStreamOpt)
                    {
                        if (ReportErrors(errors, consoleOutput, errorLogger))
                        {
                            return(Failed);
                        }

                        cancellationToken.ThrowIfCancellationRequested();

                        string outputName = GetOutputFileName(compilation, cancellationToken);

                        finalPeFilePath  = Path.Combine(Arguments.OutputDirectory, outputName);
                        finalPdbFilePath = Arguments.PdbPath ?? Path.ChangeExtension(finalPeFilePath, ".pdb");

                        // NOTE: Unlike the PDB path, the XML doc path is not embedded in the assembly, so we don't need to pass it to emit.
                        var emitOptions = Arguments.EmitOptions.
                                          WithOutputNameOverride(outputName).
                                          WithPdbFilePath(finalPdbFilePath);

                        // The PDB path is emitted in it's entirety into the PE.  This makes it impossible to have deterministic
                        // builds that occur in different source directories.  To enable this we shave all path information from
                        // the PDB when specified by the user.
                        //
                        // This is a temporary work around to allow us to make progress with determinism.  The following issue
                        // tracks getting an official solution here.
                        //
                        // https://github.com/dotnet/roslyn/issues/9813
                        if (Arguments.ParseOptions.Features.ContainsKey("pdb-path-determinism") && !string.IsNullOrEmpty(emitOptions.PdbFilePath))
                        {
                            emitOptions = emitOptions.WithPdbFilePath(Path.GetFileName(emitOptions.PdbFilePath));
                        }

                        //var dummyCompiler = Refl.Type_Csc.InvokeFunction(".ctor", backup_responseFile, (object)null, backup_args, backup_analyzerLoader);

                        var constr             = Refl.Type_Csc.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).First();
                        var backup_buildPaths2 = Refl.Type_BuildPaths.InvokeFunction(".ctor", backup_buildPaths.ClientDirectory, backup_buildPaths.WorkingDirectory, backup_buildPaths.SdkDirectory, backup_buildPaths.TempDirectory);
                        var dummyCompiler      = constr.Invoke(new object[] { backup_responseFile, backup_buildPaths2, backup_args, backup_analyzerLoader });
                        //var dummyCompiler = Activator.CreateInstance(Refl.Type_Csc, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.CreateInstance|BindingFlags.Instance, null, new object[]{ backup_responseFile, (object)null, backup_args, backup_analyzerLoader}, null);
                        var peStreamProvider     = Refl.Type_CompilerEmitStreamProvider.InvokeFunction(".ctor", dummyCompiler, finalPeFilePath);
                        var pdbStreamProviderOpt = Arguments.EmitPdb ? Refl.Type_CompilerEmitStreamProvider.InvokeFunction(".ctor", dummyCompiler, finalPdbFilePath) : null;

                        /*
                         * internal EmitResult Emit(
                         *  Compilation.EmitStreamProvider peStreamProvider,
                         *  Compilation.EmitStreamProvider pdbStreamProvider,
                         *  Compilation.EmitStreamProvider xmlDocumentationStreamProvider,
                         *  Compilation.EmitStreamProvider win32ResourcesStreamProvider,
                         *  IEnumerable<ResourceDescription> manifestResources,
                         *  EmitOptions options,
                         *  IMethodSymbol debugEntryPoint,
                         *  CompilationTestData testData,
                         *  Func<ImmutableArray<Diagnostic>> getHostDiagnostics,
                         *  CancellationToken cancellationToken)
                         */
                        var diagnosticBag = Refl.Type_DiagnosticBag.InvokeFunction(".ctor");

                        emitResult = null;
                        try
                        {
                            var peStream  = ReflEmitStreamProvider.CreateStream(peStreamProvider, diagnosticBag);
                            var pdbStream = pdbStreamProviderOpt != null?ReflEmitStreamProvider.CreateStream(pdbStreamProviderOpt, diagnosticBag) : null;

                            emitResult = compilation.Emit(
                                peStream,
                                pdbStream,
                                xmlStreamOpt,
                                win32ResourceStreamOpt,
                                Arguments.ManifestResources,
                                emitOptions,
                                null,
                                cancellationToken);
                        }
                        catch
                        {
                            emitDiagnostics = GetEmitDiagnostic(diagnosticBag);
                            if (emitDiagnostics.Length == 0)
                            {
                                throw;
                            }
                        }
                        emitDiagnostics = GetEmitDiagnostic(diagnosticBag);

                        if (emitResult != null && emitResult.Success && touchedFilesLogger != null)
                        {
                            if (pdbStreamProviderOpt != null)
                            {
                                touchedFilesLogger.InvokeAction("AddWritten", finalPdbFilePath);
                            }

                            touchedFilesLogger.InvokeAction("AddWritten", finalPeFilePath);
                        }
                        peStreamProvider.InvokeAction("Close", diagnosticBag);
                        pdbStreamProviderOpt?.InvokeAction("Close", diagnosticBag);
                    }



                if (ReportErrors((emitResult != null ? emitResult.Diagnostics : Enumerable.Empty <Diagnostic>()).Union(emitDiagnostics), consoleOutput, errorLogger))
                {
                    return(Failed);
                }

                cancellationToken.ThrowIfCancellationRequested();

                if (analyzerExceptionDiagnostics != null && ReportErrors(analyzerExceptionDiagnostics, consoleOutput, errorLogger))
                {
                    return(Failed);
                }

                bool errorsReadingAdditionalFiles = false;
                foreach (var additionalFile in additionalTextFiles)
                {
                    if (ReportErrors(additionalFile.GetFieldOrProperty <IEnumerable <Diagnostic> >("Diagnostics"), consoleOutput, errorLogger))
                    {
                        errorsReadingAdditionalFiles = true;
                    }
                }

                if (errorsReadingAdditionalFiles)
                {
                    return(Failed);
                }

                cancellationToken.ThrowIfCancellationRequested();

                if (Arguments.TouchedFilesPath != null)
                {
                    Debug.Assert(touchedFilesLogger != null);

                    if (finalXmlFilePath != null)
                    {
                        touchedFilesLogger.InvokeAction("AddWritten", finalXmlFilePath);
                    }

                    var readStream = OpenFile(Arguments.TouchedFilesPath + ".read", consoleOutput, mode: FileMode.OpenOrCreate);
                    if (readStream == null)
                    {
                        return(Failed);
                    }

                    using (var writer = new StreamWriter(readStream))
                    {
                        touchedFilesLogger.InvokeAction("WriteReadPaths", writer);
                    }

                    var writtenStream = OpenFile(Arguments.TouchedFilesPath + ".write", consoleOutput, mode: FileMode.OpenOrCreate);
                    if (writtenStream == null)
                    {
                        return(Failed);
                    }

                    using (var writer = new StreamWriter(writtenStream))
                    {
                        touchedFilesLogger.InvokeAction("WriteWrittenPaths", writer);
                    }
                }
            }
            finally
            {
                // At this point analyzers are already complete in which case this is a no-op.  Or they are
                // still running because the compilation failed before all of the compilation events were
                // raised.  In the latter case the driver, and all its associated state, will be waiting around
                // for events that are never coming.  Cancel now and let the clean up process begin.
                if (analyzerCts != null)
                {
                    analyzerCts.Cancel();

                    if (analyzerManager != null)
                    {
                        // Clear cached analyzer descriptors and unregister exception handlers hooked up to the LocalizableString fields of the associated descriptors.
                        analyzerManager.InvokeAction("ClearAnalyzerState", analyzers);
                    }

                    if (Arguments.ReportAnalyzer && analyzerDriver != null && compilation != null)
                    {
                        ReportAnalyzerExecutionTime(consoleOutput, analyzerDriver, Culture, compilation.Options.ConcurrentBuild);
                    }
                }
            }

            return(Succeeded);
        }