Exemplo n.º 1
0
        OutputKind GetDefaultOutputKind(CompilationKind kind)
        {
            if (!SupportsNetModule)
            {
                return(OutputKind.DynamicallyLinkedLibrary);
            }

            switch (kind)
            {
            case CompilationKind.EditAssembly:
                // We can't use netmodule when editing assembly attributes since the compiler won't add an assembly for obvious reasons
                return(OutputKind.DynamicallyLinkedLibrary);

            case CompilationKind.EditMethod:
            case CompilationKind.AddClass:
            case CompilationKind.EditClass:
            case CompilationKind.AddMembers:
                // Use a netmodule to prevent the compiler from adding assembly attributes. Sometimes the compiler must
                // add assembly attributes but the attributes have missing members and the compiler can't compile the code.
                //	error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.CompilationRelaxationsAttribute..ctor'
                //	error CS0656: Missing compiler required member 'System.Runtime.CompilerServices.RuntimeCompatibilityAttribute..ctor'
                // If unsafe code is enabled, it will try to add even more attributes.
                return(OutputKind.NetModule);

            default:
                Debug.Fail($"Unknown {nameof(CompilationKind)}: {kind}");
                goto case CompilationKind.EditMethod;
            }
        }
Exemplo n.º 2
0
        bool IsSupportedLanguage(IDecompiler decompiler, CompilationKind kind)
        {
            if (decompiler == null)
            {
                return(false);
            }

            switch (kind)
            {
            case CompilationKind.Assembly:
                if (!decompiler.CanDecompile(DecompilationType.AssemblyInfo))
                {
                    return(false);
                }
                break;

            case CompilationKind.Method:
            case CompilationKind.EditClass:
                if (!decompiler.CanDecompile(DecompilationType.TypeMethods))
                {
                    return(false);
                }
                break;

            case CompilationKind.AddClass:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(kind));
            }

            return(languageCompilerProviders.Any(a => a.Language == decompiler.GenericGuid));
        }
Exemplo n.º 3
0
		bool IsSupportedLanguage(IDecompiler decompiler, CompilationKind kind) {
			if (decompiler == null)
				return false;

			switch (kind) {
			case CompilationKind.Assembly:
				if (!decompiler.CanDecompile(DecompilationType.AssemblyInfo))
					return false;
				break;

			case CompilationKind.Method:
			case CompilationKind.EditClass:
				if (!decompiler.CanDecompile(DecompilationType.TypeMethods))
					return false;
				break;

			case CompilationKind.AddClass:
				break;

			default:
				throw new ArgumentOutOfRangeException(nameof(kind));
			}

			return languageCompilerProviders.Any(a => a.Language == decompiler.GenericGuid);
		}
Exemplo n.º 4
0
        /// <summary>
        /// Used to log a message that should go into both the compiler server log as well as the MSBuild logs
        ///
        /// These are intended to be processed by automation in the binlog hence do not change the structure of
        /// the messages here.
        /// </summary>
        private void LogCompilationMessage(
            ICompilerServerLogger logger,
            Guid requestId,
            CompilationKind kind,
            string diagnostic
            )
        {
            var category = kind switch
            {
                CompilationKind.Server => "server",
                CompilationKind.Tool => "tool",
                CompilationKind.ToolFallback => "server failed",
                CompilationKind.FatalError => "fatal error",
                _ => throw new Exception($"Unexpected value {kind}"),
            };

            var message = $"CompilerServer: {category} - {diagnostic} - {requestId}";

            if (kind == CompilationKind.FatalError)
            {
                logger.LogError(message);
                Log.LogError(message);
            }
            else
            {
                logger.Log(message);
                Log.LogMessage(message);
            }
        }
Exemplo n.º 5
0
 protected RoslynLanguageCompiler(CompilationKind kind, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider)
 {
     this.codeEditorProvider           = codeEditorProvider ?? throw new ArgumentNullException(nameof(codeEditorProvider));
     this.docFactory                   = docFactory ?? throw new ArgumentNullException(nameof(docFactory));
     this.roslynDocumentChangedService = roslynDocumentChangedService ?? throw new ArgumentNullException(nameof(roslynDocumentChangedService));
     this.textViewUndoManagerProvider  = textViewUndoManagerProvider ?? throw new ArgumentNullException(nameof(textViewUndoManagerProvider));
     documents = new List <RoslynCodeDocument>();
 }
Exemplo n.º 6
0
        IDecompiler TryGetUsedLanguage(CompilationKind kind)
        {
            var defaultDecompiler = decompilerService.Decompiler;

            if (IsSupportedLanguage(defaultDecompiler, kind))
            {
                return(defaultDecompiler);
            }
            return(decompilerService.AllDecompilers.FirstOrDefault(a => a.GenericGuid == defaultDecompiler.GenericGuid && IsSupportedLanguage(a, kind)) ??
                   decompilerService.AllDecompilers.FirstOrDefault(a => IsSupportedLanguage(a, kind)));
        }
Exemplo n.º 7
0
 protected RoslynLanguageCompiler(CompilationKind kind, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider)
 {
     this.codeEditorProvider           = codeEditorProvider ?? throw new ArgumentNullException(nameof(codeEditorProvider));
     this.docFactory                   = docFactory ?? throw new ArgumentNullException(nameof(docFactory));
     this.roslynDocumentChangedService = roslynDocumentChangedService ?? throw new ArgumentNullException(nameof(roslynDocumentChangedService));
     this.textViewUndoManagerProvider  = textViewUndoManagerProvider ?? throw new ArgumentNullException(nameof(textViewUndoManagerProvider));
     DefaultOutputKind                 = GetDefaultOutputKind(kind);
     documents       = new List <RoslynCodeDocument>();
     projectId       = ProjectId.CreateNewId();
     loadedDocuments = new HashSet <DocumentId>();
 }
Exemplo n.º 8
0
		public bool CanCompile(CompilationKind kind) {
			switch (kind) {
			case CompilationKind.Assembly:
			case CompilationKind.Method:
			case CompilationKind.AddClass:
			case CompilationKind.EditClass:
				return true;
			default:
				Debug.Fail($"Unknown kind: {kind}");
				return false;
			}
		}
Exemplo n.º 9
0
		public string GetHeader(CompilationKind kind) {
			var info = GetLanguageCompilerProvider(kind);
			if (info == null)
				return null;
			switch (kind) {
			case CompilationKind.Assembly:		return string.Format(dnSpy_AsmEditor_Resources.EditAssemblyCode2, info.Value.Key.GenericNameUI);
			case CompilationKind.Method:		return string.Format(dnSpy_AsmEditor_Resources.EditMethodBodyCode, info.Value.Key.GenericNameUI);
			case CompilationKind.AddClass:		return string.Format(dnSpy_AsmEditor_Resources.EditCodeAddClass2, info.Value.Key.GenericNameUI);
			case CompilationKind.EditClass:		return string.Format(dnSpy_AsmEditor_Resources.EditCodeEditClass2, info.Value.Key.GenericNameUI);
			default: throw new ArgumentOutOfRangeException(nameof(kind));
			}
		}
Exemplo n.º 10
0
		KeyValuePair<IDecompiler, ILanguageCompilerProvider>? GetLanguageCompilerProvider(CompilationKind kind) {
			var language = TryGetUsedLanguage(kind);
			if (language == null)
				return null;

			var serviceCreator = languageCompilerProviders.FirstOrDefault(a => a.Language == language.GenericGuid);
			if (serviceCreator == null)
				return null;
			if (!serviceCreator.CanCompile(kind))
				return null;

			return new KeyValuePair<IDecompiler, ILanguageCompilerProvider>(language, serviceCreator);
		}
Exemplo n.º 11
0
        public bool CanCompile(CompilationKind kind)
        {
            switch (kind)
            {
            case CompilationKind.Assembly:
            case CompilationKind.Method:
            case CompilationKind.AddClass:
                return(true);

            default:
                Debug.Fail($"Unknown kind: {kind}");
                return(false);
            }
        }
Exemplo n.º 12
0
		protected RoslynLanguageCompiler(CompilationKind kind, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider) {
			if (codeEditorProvider == null)
				throw new ArgumentNullException(nameof(codeEditorProvider));
			if (docFactory == null)
				throw new ArgumentNullException(nameof(docFactory));
			if (roslynDocumentChangedService == null)
				throw new ArgumentNullException(nameof(roslynDocumentChangedService));
			if (textViewUndoManagerProvider == null)
				throw new ArgumentNullException(nameof(textViewUndoManagerProvider));
			this.codeEditorProvider = codeEditorProvider;
			this.docFactory = docFactory;
			this.roslynDocumentChangedService = roslynDocumentChangedService;
			this.textViewUndoManagerProvider = textViewUndoManagerProvider;
			documents = new List<RoslynCodeDocument>();
		}
Exemplo n.º 13
0
        public string GetHeader(CompilationKind kind)
        {
            var info = GetLanguageCompilerProvider(kind);

            if (info == null)
            {
                return(null);
            }
            switch (kind)
            {
            case CompilationKind.Assembly:          return($"{dnSpy_AsmEditor_Resources.EditAssemblyCode} ({info.Value.Key.GenericNameUI})");

            case CompilationKind.Method:            return(string.Format(dnSpy_AsmEditor_Resources.EditMethodBodyCode, info.Value.Key.GenericNameUI));

            case CompilationKind.AddClass:          return($"{dnSpy_AsmEditor_Resources.EditCodeAddClass} ({info.Value.Key.GenericNameUI})");

            default: throw new ArgumentOutOfRangeException(nameof(kind));
            }
        }
Exemplo n.º 14
0
 public override ILanguageCompiler Create(CompilationKind kind) => new CSharpLanguageCompiler(kind, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider);
Exemplo n.º 15
0
 public CSharpLanguageCompiler(CompilationKind kind, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider)
     : base(kind, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider)
 {
 }
Exemplo n.º 16
0
		public bool CanCreate(CompilationKind kind) => GetLanguageCompilerProvider(kind) != null;
Exemplo n.º 17
0
 public abstract ILanguageCompiler Create(CompilationKind kind);
Exemplo n.º 18
0
 public bool CanCreate(CompilationKind kind) => GetLanguageCompilerProvider(kind) != null;
Exemplo n.º 19
0
		public ImageReference? GetIcon(CompilationKind kind) {
			var info = GetLanguageCompilerProvider(kind);
			return info?.Value.Icon;
		}
Exemplo n.º 20
0
 public override ILanguageCompiler Create(CompilationKind kind) => new VisualBasicLanguageCompiler(kind, visualBasicCompilerSettings, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider);
Exemplo n.º 21
0
 public CSharpLanguageCompiler(CompilationKind kind, CSharpCompilerSettings csharpCompilerSettings, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider)
     : base(kind, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider) =>
     this.csharpCompilerSettings = csharpCompilerSettings ?? throw new ArgumentNullException(nameof(csharpCompilerSettings));
Exemplo n.º 22
0
        KeyValuePair <IDecompiler, ILanguageCompilerProvider>?GetLanguageCompilerProvider(CompilationKind kind)
        {
            var language = TryGetUsedLanguage(kind);

            if (language == null)
            {
                return(null);
            }

            var serviceCreator = languageCompilerProviders.FirstOrDefault(a => a.Language == language.GenericGuid);

            if (serviceCreator == null)
            {
                return(null);
            }
            if (!serviceCreator.CanCompile(kind))
            {
                return(null);
            }

            return(new KeyValuePair <IDecompiler, ILanguageCompilerProvider>(language, serviceCreator));
        }
Exemplo n.º 23
0
        public ImageReference?GetIcon(CompilationKind kind)
        {
            var info = GetLanguageCompilerProvider(kind);

            return(info?.Value.Icon);
        }
Exemplo n.º 24
0
 public bool CanCreate(CompilationKind kind) => !(GetLanguageCompilerProvider(kind) is null);
Exemplo n.º 25
0
		public abstract ILanguageCompiler Create(CompilationKind kind);
		public override ILanguageCompiler Create(CompilationKind kind) => new VisualBasicLanguageCompiler(kind, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider);
Exemplo n.º 27
0
		IDecompiler TryGetUsedLanguage(CompilationKind kind) {
			var defaultDecompiler = decompilerService.Decompiler;
			if (IsSupportedLanguage(defaultDecompiler, kind))
				return defaultDecompiler;
			return decompilerService.AllDecompilers.FirstOrDefault(a => a.GenericGuid == defaultDecompiler.GenericGuid && IsSupportedLanguage(a, kind)) ??
					decompilerService.AllDecompilers.FirstOrDefault(a => IsSupportedLanguage(a, kind));
		}
		public VisualBasicLanguageCompiler(CompilationKind kind, ICodeEditorProvider codeEditorProvider, IRoslynDocumentationProviderFactory docFactory, IRoslynDocumentChangedService roslynDocumentChangedService, ITextViewUndoManagerProvider textViewUndoManagerProvider)
			: base(kind, codeEditorProvider, docFactory, roslynDocumentChangedService, textViewUndoManagerProvider) {
		}