private void ValidateMethod(MethodDeclaration method, List<string> list, CompilationResult result)
        {
            var name = method.Name;

            if (String.IsNullOrWhiteSpace(name))
            {
                result.AddError(new CompilationMessage
                {
                    FilePath = method.OriginatingTree != null ? method.OriginatingTree.FilePath : null,
                    Location = DocumentLocation.FromTreeNode(method.OriginatingTree, method.OriginatingNode),
                    Message = "All generated method calls must have a valid name."
                });

                return;
            }

            if (list.Contains(name))
            {
                result.AddError(new CompilationMessage
                {
                    FilePath = method.OriginatingTree != null ? method.OriginatingTree.FilePath : null,
                    Location = DocumentLocation.FromTreeNode(method.OriginatingTree, method.OriginatingNode),
                    Message = "Method overloading is not supported. Consider using optional parameters, " +
                        "or providing an alternate name using the ScriptName attribute."
                });

                return;
            }

            list.Add(name);
        }
Пример #2
0
 /// <summary>
 /// Creates a new error message associated with the current node.
 /// </summary>
 /// <param name="message">The message text.</param>
 protected void AddError(string message)
 {
     if (_currentResult != null)
     {
         _currentResult.AddError(new CompilationMessage
         {
             FilePath = _currentTree != null ? _currentTree.FilePath : null,
             Location = DocumentLocation.FromTreeNode(_currentTree, _currentNode),
             Message  = message
         });
     }
 }
Пример #3
0
        private void ValidateMethod(MethodDeclaration method, List <string> list, CompilationResult result)
        {
            if (!String.IsNullOrWhiteSpace(method.Name))
            {
                if (list.Contains(method.Name))
                {
                    result.AddError(new CompilationMessage
                    {
                        FilePath = method.OriginatingTree != null ? method.OriginatingTree.FilePath : null,
                        Location = DocumentLocation.FromTreeNode(method.OriginatingTree, method.OriginatingNode),
                        Message  = "Method overloading is not supported. Consider using optional parameters, " +
                                   "or providing an alternate name using the ScriptName attribute."
                    });

                    return;
                }
            }

            list.Add(method.Name);
        }
Пример #4
0
        public CompilationResult TranslateAndCompile(VSGraphModel graphModel, AssemblyType assemblyType, CompilationOptions compilationOptions)
        {
            string graphModelSourceFilePath = graphModel.SourceFilePath;

            CompilationResult compilationResult = new CompilationResult();

            compilationResult.sourceCode = new string[Enum.GetNames(typeof(SourceCodePhases)).Length];

            long translationTime = 0;
            long analysisTime    = 0;
            long compilationTime = 0;

            Stopwatch sw = Stopwatch.StartNew();

            Profiler.BeginSample("Validation");
            OnValidate(graphModel, assemblyType, compilationOptions, ref compilationResult);
            long validationTime = sw.ElapsedMilliseconds;

            Profiler.EndSample();

            compilationResult.errors.AddRange(m_Errors);

            if (compilationResult.status == CompilationStatus.Succeeded)
            {
                sw.Restart();
                Profiler.BeginSample("Translation");

                var syntaxTree = OnTranslate(graphModel, assemblyType, compilationOptions, ref compilationResult);

                compilationResult.errors.AddRange(m_Errors);

                translationTime = sw.ElapsedMilliseconds;
                Profiler.EndSample();

                sw.Restart();
                ApplyPluginsToAst(ref syntaxTree, compilationOptions, graphModel.Stencil, ref compilationResult);
                analysisTime = sw.ElapsedMilliseconds;
                sw.Restart();
                Profiler.BeginSample("Compilation");

                if (compilationResult.status == CompilationStatus.Succeeded)
                {
                    int syntaxTreeHashCode = syntaxTree.ToString().GetHashCode();
                    if (s_LastSyntaxTreeHashCode == syntaxTreeHashCode)
                    {
                        compilationResult = s_LastCompilationResult;
                        if (logCompileTimeStats)
                        {
                            Debug.Log("Reused cached compilation result");
                        }
                    }
                    else
                    {
                        try
                        {
                            if (logCompileTimeStats)
                            {
                                Debug.Log("Compute new compilation result");
                            }
                            compilationResult = CheckSemanticModel(syntaxTree, graphModelSourceFilePath, assemblyType, compilationResult);
                        }
                        catch (LoopDetectedException e)
                        {
                            compilationResult.AddError(e.Message);
                            compilationResult.status = CompilationStatus.Failed;
                        }
                        finally
                        {
                            s_LastSyntaxTreeHashCode = syntaxTreeHashCode;
                            s_LastCompilationResult  = compilationResult;
                        }
                    }

                    Profiler.BeginSample("WriteSource");
                    if (assemblyType == AssemblyType.Source)
                    {
                        string directoryName = Path.GetDirectoryName(graphModelSourceFilePath);
                        Assert.IsNotNull(directoryName, nameof(directoryName) + " != null");
                        Directory.CreateDirectory(directoryName);

                        string sourceCode = compilationResult.sourceCode[(int)SourceCodePhases.Final];

                        if (compilationResult.status == CompilationStatus.Succeeded)
                        {
                            File.WriteAllText(graphModelSourceFilePath, sourceCode);
                        }
                    }
                    Profiler.EndSample();
                }
                compilationTime = sw.ElapsedMilliseconds;
                Profiler.EndSample();
            }

            // needs to be done on the main thread
            foreach (CompilerError error in compilationResult.errors)
            {
                if (error.sourceNodeGuid == default && !error.sourceNode.Destroyed)
                {
                    error.sourceNodeGuid = error.sourceNode.Guid;
                }
                else
                {
                    graphModel.NodesByGuid.TryGetValue(error.sourceNodeGuid, out error.sourceNode);
                }
            }

            if (logCompileTimeStats)
            {
                Debug.Log($"Validation: {validationTime}ms Translation: {translationTime}ms Code Analysis: {analysisTime}ms Compilation: {compilationTime}ms");
            }

            return(compilationResult);
        }