/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.Collections.ObjectModel; using dnSpy.Contracts.Debugger.DotNet.Text; using dnSpy.Contracts.Debugger.Engine.Evaluation; using dnSpy.Contracts.Debugger.Evaluation; namespace dnSpy.Contracts.Debugger.DotNet.Evaluation.ExpressionCompiler { /// /// Contains the compiled assembly and info on which method to evaluate to get the result of an expression /// public readonly struct DbgDotNetCompilationResult { /// /// true if it has an error message () /// public bool IsError => ErrorMessage is not null; /// /// Gets the error message or null if there was no error /// public string? ErrorMessage { get; } /// /// Gets the .NET assembly bytes or null if there was an error (). It's /// empty if is empty. /// public byte[]? Assembly { get; } /// /// Gets the result of all compiled expressions or null if there was an error () /// public DbgDotNetCompiledExpressionResult[]? CompiledExpressions { get; } /// /// Constructor /// /// Error message public DbgDotNetCompilationResult(string errorMessage) { ErrorMessage = errorMessage ?? throw new ArgumentNullException(nameof(errorMessage)); Assembly = null; CompiledExpressions = null; } /// /// Constructor /// /// .NET assembly bytes /// Compiled expressions info public DbgDotNetCompilationResult(byte[] assembly, DbgDotNetCompiledExpressionResult[] compiledExpressions) { ErrorMessage = null; Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly)); CompiledExpressions = compiledExpressions ?? throw new ArgumentNullException(nameof(compiledExpressions)); } } /// /// Compiled expression result flags /// [Flags] public enum DbgDotNetCompiledExpressionResultFlags { /// /// No bit is set /// None = 0, /// /// Compiler generated variable /// CompilerGenerated = 0x00000001, } /// /// Compiled expression result /// public struct DbgDotNetCompiledExpressionResult { /// /// Error message or null if no error. See also /// public string? ErrorMessage; /// /// Name of the type that contains the method () that should be evaluated /// public string TypeName; /// /// Name of the method that should be evaluated. The declaring type is /// public string MethodName; /// /// Gets the expression that was compiled. This is eg. a C# or Visual Basic expression. /// public string Expression; /// /// Display name shown in the UI /// public DbgDotNetText Name; /// /// Gets the evaluation result flags /// public DbgEvaluationResultFlags Flags; /// /// Gets the image, see /// public string ImageName; /// /// Gets extra custom type info or null if none /// public DbgDotNetCustomTypeInfo? CustomTypeInfo; /// /// Gets the format specifiers or null /// public ReadOnlyCollection? FormatSpecifiers; /// /// Parameter/local index or -1 if unknown /// public int Index; /// /// Gets the compiled expression flags /// public DbgDotNetCompiledExpressionResultFlags ResultFlags; /// /// Creates a successful compiled expression with no error /// /// Name of type that contains the method to evaluate /// Name of the method to evaluate /// Original expression /// Display name shown in the UI /// Evaluation result flags /// Image, see /// Optional custom type info known by the language expression compiler and the language value formatter /// Format specifiers /// Result flags /// Parameter/local index or -1 if unknown /// public static DbgDotNetCompiledExpressionResult Create(string typeName, string methodName, string expression, DbgDotNetText name, DbgEvaluationResultFlags flags, string imageName, DbgDotNetCustomTypeInfo? customTypeInfo = null, ReadOnlyCollection? formatSpecifiers = null, DbgDotNetCompiledExpressionResultFlags resultFlags = DbgDotNetCompiledExpressionResultFlags.None, int index = -1) { if (name.Parts is null) throw new ArgumentException(); return new DbgDotNetCompiledExpressionResult { TypeName = typeName ?? throw new ArgumentNullException(nameof(typeName)), MethodName = methodName ?? throw new ArgumentNullException(nameof(methodName)), Expression = expression ?? throw new ArgumentNullException(nameof(expression)), Name = name, Flags = flags, ImageName = imageName ?? throw new ArgumentNullException(nameof(imageName)), CustomTypeInfo = customTypeInfo, FormatSpecifiers = formatSpecifiers, Index = index, ResultFlags = resultFlags, }; } /// /// Creates an error /// /// Expression /// Display name shown in the UI /// Error message, see also /// public static DbgDotNetCompiledExpressionResult CreateError(string expression, DbgDotNetText name, string errorMessage) { if (name.Parts is null) throw new ArgumentException(); return new DbgDotNetCompiledExpressionResult { ErrorMessage = errorMessage ?? throw new ArgumentNullException(nameof(errorMessage)), Expression = expression ?? throw new ArgumentNullException(nameof(expression)), Name = name, Flags = DbgEvaluationResultFlags.ReadOnly, ImageName = PredefinedDbgValueNodeImageNames.Error, }; } } }