/* 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.Globalization; using dnSpy.Contracts.Debugger.Text; namespace dnSpy.Contracts.Debugger.Evaluation { /// /// A value shown in a treeview (eg. in locals window) /// public abstract class DbgValueNode : DbgObject { /// /// Gets the language /// public abstract DbgLanguage Language { get; } /// /// Gets the process /// public DbgProcess Process => Runtime.Process; /// /// Gets the runtime /// public abstract DbgRuntime Runtime { get; } /// /// true if this is an error value node /// public bool HasError => ErrorMessage is not null; /// /// Gets the error message or null /// public abstract string? ErrorMessage { get; } /// /// true if is not null /// public bool HasValue => Value is not null; /// /// Gets the value or null if there's none /// public abstract DbgValue? Value { get; } /// /// true if it's a node that has an can be evaluated, false if it's a non-value node, eg. 'Type variables', 'Raw View', etc. /// public abstract bool CanEvaluateExpression { get; } /// /// Gets the expression that is used when adding an expression to the watch window or /// when assigning a new value to the source. See also . /// public abstract string Expression { get; } /// /// Image name, see /// public abstract string ImageName { get; } /// /// true if this is a read-only value /// public abstract bool IsReadOnly { get; } /// /// true if the expression causes side effects /// public abstract bool CausesSideEffects { get; } /// /// Returns true if it has children, false if it has no children and null if it's unknown (eg. it's too expensive to calculate it now). /// UI code can use this property to decide if it shows the treeview node expander ("|>"). /// public abstract bool? HasChildren { get; } /// /// Number of children. This property is called as late as possible and can be lazily initialized. /// It's assumed to be 0 if is false. /// /// Evaluation info /// public abstract ulong GetChildCount(DbgEvaluationInfo evalInfo); /// /// Creates new children. The returned s are automatically closed when their runtime continues /// /// Evaluation info /// Index of first child /// Max number of children to return /// Options /// public abstract DbgValueNode[] GetChildren(DbgEvaluationInfo evalInfo, ulong index, int count, DbgValueNodeEvaluationOptions options); /// /// Formats the name /// /// Evaluation info /// Output /// Formatter options /// Culture or null to use invariant culture public void FormatName(DbgEvaluationInfo evalInfo, IDbgTextWriter output, DbgValueFormatterOptions options, CultureInfo? cultureInfo = null) => Format(evalInfo, new DbgValueNodeFormatParameters { NameOutput = output ?? throw new ArgumentNullException(nameof(output)), NameFormatterOptions = options, }, cultureInfo); /// /// Formats the value /// /// Evaluation info /// Output /// Formatter options /// Culture or null to use invariant culture public void FormatValue(DbgEvaluationInfo evalInfo, IDbgTextWriter output, DbgValueFormatterOptions options, CultureInfo? cultureInfo) => Format(evalInfo, new DbgValueNodeFormatParameters { ValueOutput = output ?? throw new ArgumentNullException(nameof(output)), ValueFormatterOptions = options, }, cultureInfo); /// /// Formats the expected type ("field" type) /// /// Evaluation info /// Output /// Formatter options /// Value options /// Culture or null to use invariant culture public void FormatExpectedType(DbgEvaluationInfo evalInfo, IDbgTextWriter output, DbgValueFormatterTypeOptions options, DbgValueFormatterOptions valueOptions, CultureInfo? cultureInfo) => Format(evalInfo, new DbgValueNodeFormatParameters { ExpectedTypeOutput = output ?? throw new ArgumentNullException(nameof(output)), ExpectedTypeFormatterOptions = options, TypeFormatterOptions = valueOptions, }, cultureInfo); /// /// Formats the actual type (value type) /// /// Evaluation info /// Output /// Formatter options /// Value options /// Culture or null to use invariant culture public void FormatActualType(DbgEvaluationInfo evalInfo, IDbgTextWriter output, DbgValueFormatterTypeOptions options, DbgValueFormatterOptions valueOptions, CultureInfo? cultureInfo) => Format(evalInfo, new DbgValueNodeFormatParameters { ActualTypeOutput = output ?? throw new ArgumentNullException(nameof(output)), ActualTypeFormatterOptions = options, TypeFormatterOptions = valueOptions, }, cultureInfo); /// /// Formats the name, value, and type /// /// Evaluation info /// Options /// Culture or null to use invariant culture public abstract void Format(DbgEvaluationInfo evalInfo, IDbgValueNodeFormatParameters options, CultureInfo? cultureInfo); /// /// Writes a new value /// /// Evaluation info /// Source expression (rhs) /// Options /// public abstract DbgValueNodeAssignmentResult Assign(DbgEvaluationInfo evalInfo, string expression, DbgEvaluationOptions options); } /// /// Assignment result /// public readonly struct DbgValueNodeAssignmentResult { /// /// Gets the error message or null if none /// public string? Error { get; } /// /// Gets the flags /// public DbgEEAssignmentResultFlags Flags { get; } /// /// Constructor /// /// Result flags /// Error message or null if none public DbgValueNodeAssignmentResult(DbgEEAssignmentResultFlags flags, string? error) { Flags = flags; Error = error; } } /// /// Parameters when formatting a /// public interface IDbgValueNodeFormatParameters { /// /// Used when writing the name or null if it's not written /// IDbgTextWriter? NameOutput { get; } /// /// Used when writing the value or null if it's not written /// IDbgTextWriter? ValueOutput { get; } /// /// Used when writing the expected type ("field" type) or null if it's not written /// IDbgTextWriter? ExpectedTypeOutput { get; } /// /// Used when writing the actual type (value type) or null if it's not written /// IDbgTextWriter? ActualTypeOutput { get; } /// /// Name formatter options /// DbgValueFormatterOptions NameFormatterOptions { get; } /// /// Value formatter options /// DbgValueFormatterOptions ValueFormatterOptions { get; } /// /// Type formatter options /// DbgValueFormatterOptions TypeFormatterOptions { get; } /// /// Expected type formatter options /// DbgValueFormatterTypeOptions ExpectedTypeFormatterOptions { get; } /// /// Actual type formatter options /// DbgValueFormatterTypeOptions ActualTypeFormatterOptions { get; } } sealed class DbgValueNodeFormatParameters : IDbgValueNodeFormatParameters { public IDbgTextWriter? NameOutput { get; set; } public IDbgTextWriter? ValueOutput { get; set; } public IDbgTextWriter? ExpectedTypeOutput { get; set; } public IDbgTextWriter? ActualTypeOutput { get; set; } public DbgValueFormatterOptions NameFormatterOptions { get; set; } public DbgValueFormatterOptions ValueFormatterOptions { get; set; } public DbgValueFormatterOptions TypeFormatterOptions { get; set; } public DbgValueFormatterTypeOptions ExpectedTypeFormatterOptions { get; set; } public DbgValueFormatterTypeOptions ActualTypeFormatterOptions { get; set; } } }