コード例 #1
0
ファイル: CommonUtils.cs プロジェクト: Soothsilver/soothsharp
        /// <summary>
        /// Gets a silvernode that combines the silvernodes of all elements of <paramref name="results"/>. They are separated by
        /// newlines. Errors are combined as well.
        /// </summary>
        /// <param name="results">The results to merge, separated by newlines.</param>
        /// <param name="parent">The Roslyn node that represents this grouping of results.</param>
        public static TranslationResult GetHighlevelSequence(IEnumerable <TranslationResult> results, SyntaxNode parent = null)
        {
            TranslationResult           result   = new TranslationResult();
            HighlevelSequenceSilvernode sequence = new HighlevelSequenceSilvernode(parent);

            foreach (var incoming in results)
            {
                result.Errors.AddRange(incoming.Errors);
                if (incoming.Silvernode != null)
                {
                    sequence.List.Add(incoming.Silvernode);
                }
            }
            result.Silvernode = sequence;
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Performs all steps of the translation, as detailed in the thesis, except for verification.
        /// </summary>
        /// <exception cref="InvalidOperationException">The process was already executed once.</exception>
        public TranslationProcessResult Execute()
        {
            if (this.executed)
            {
                throw new InvalidOperationException("The process was already executed once.");
            }
            this.executed = true;
            VerboseLog("Loading mscorlib and Soothsharp.Contracts...");
            var mscorlib         = MetadataReference.CreateFromFile(typeof(Attribute).Assembly.Location);
            var contractsLibrary = MetadataReference.CreateFromFile(typeof(Contracts.Contract).Assembly.Location);
            var systemCore       = MetadataReference.CreateFromFile(typeof(ParallelQuery).Assembly.Location);

            VerboseLog("Initializing compilation...");
            CSharpCompilation compilation;

            try
            {
                compilation = CSharpCompilation.Create("translated_assembly", this.compilationUnits.Select(unit => unit.RoslynTree),
                                                       (new[] { mscorlib, contractsLibrary, systemCore }).Union(this.referencedAssemblies.Select(filename => MetadataReference.CreateFromFile(filename)))
                                                       );
            } catch (System.IO.IOException exception)
            {
                return(TranslationProcessResult.Error(null, Diagnostics.SSIL112_FileNotFound, exception.Message));
            }

            VerboseLog("Processing trees...");
            HighlevelSequenceSilvernode masterTree = new HighlevelSequenceSilvernode(null);

            foreach (CompilationUnit compilationUnit in this.compilationUnits)
            {
                VerboseLog("Processing " + compilationUnit.RoslynTree.FilePath + "...");
                VerboseLog("- Semantic analysis...");
                SemanticModel semanticModel = compilation.GetSemanticModel(compilationUnit.RoslynTree, false);

                VerboseLog("- CONVERSION PHASE begins...");
                Sharpnode cSharpTree;
#if GLOBAL_TRY_CATCH
                try
                {
#endif
                cSharpTree = new CompilationUnitSharpnode(compilationUnit.RoslynTree.GetRoot() as CompilationUnitSyntax);
#if GLOBAL_TRY_CATCH
            }
            catch (Exception ex)
            {
                this.masterErrorList.Add(new Error(Diagnostics.SSIL103_ExceptionConstructingCSharp, compilationUnit.RoslynTree.GetRoot(), ex.ToString()));
                continue;
            }
#endif

                VerboseLog("- COLLECTION PHASE begins...");
                cSharpTree.CollectTypesInto(this, semanticModel);

                VerboseLog("- MAIN PHASE begins...");
                TranslationResult translationResult;
                var diags = semanticModel.GetDiagnostics().ToList();
                bool skipTranslatingThisTree = false;
                foreach (var diag in diags)
                {
                    if (diag.Severity != Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
                    {
                        continue;
                    }
                    masterErrorList.Add(new Error(Diagnostics.SSIL123_ThereIsThisCSharpError, null, diag.ToString()));
                    skipTranslatingThisTree = true;
                }
                if (!skipTranslatingThisTree)
                {
#if GLOBAL_TRY_CATCH
                    try
                    {
#endif
                    translationResult =
                        cSharpTree.Translate(TranslationContext.StartNew(this, semanticModel,
                                                                         this.Configuration.VerifyUnmarkedItems, compilationUnit.Style));
#if GLOBAL_TRY_CATCH
                }
                catch (Exception ex)
                {
                    this.masterErrorList.Add(new Error(Diagnostics.SSIL104_ExceptionConstructingSilver, compilationUnit.RoslynTree.GetRoot(), ex.ToString()));
                    continue;
                }
#endif
                    masterTree.List.Add(translationResult.Silvernode);
                    this.masterErrorList.AddRange(translationResult.Errors);
                }
            }

            VerboseLog("GLOBAL ADDITION PHASE begins...");
            foreach (var collectedType in this.collectedTypes)
            {
                masterTree.List.Add(collectedType.GenerateGlobalSilvernode(this));
            }
            if (this.ArraysTranslator.ArraysWereUsed)
            {
                masterTree.List.Add(this.ArraysTranslator.GenerateGlobalSilvernode());
            }


            VerboseLog("OPTIMIZATION PHASE begins...");
            masterTree.OptimizeRecursively();

            VerboseLog("NAME ASSIGNMENT PHASE begins...");
            this.IdentifierTranslator.AssignTrueNames();

            VerboseLog("POSTPROCESSING PHASE begins...");
            masterTree.Postprocess();

            return(new TranslationProcessResult(masterTree, this.masterErrorList));
        }