/* 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.Diagnostics; using dnlib.DotNet; using dnlib.DotNet.Emit; namespace dnSpy.Contracts.Decompiler { /// /// A local present in decompiled code /// public sealed class SourceLocal : ISourceVariable { /// /// The local or null /// public Local? Local { get; } IVariable? ISourceVariable.Variable => Local; bool ISourceVariable.IsLocal => true; bool ISourceVariable.IsParameter => false; /// /// Gets the name of the local the decompiler used. It could be different from the real name if the decompiler renamed it. /// public string Name { get; } /// /// Gets the type of the local /// public TypeSig Type { get; } /// /// Gets the hoisted field or null if it's not a hoisted local /// public FieldDef? HoistedField { get; } /// /// Gets the flags /// public SourceVariableFlags Flags { get; } /// /// true if this is a decompiler generated local /// public bool IsDecompilerGenerated => (Flags & SourceVariableFlags.DecompilerGenerated) != 0; /// /// Constructor /// /// Local or null /// Name used by the decompiler /// Type of local /// Flags public SourceLocal(Local? local, string name, TypeSig type, SourceVariableFlags flags) { Debug.Assert((flags & SourceVariableFlags.DecompilerGenerated) == 0); Local = local; Name = name ?? throw new ArgumentNullException(nameof(name)); Type = type ?? throw new ArgumentNullException(nameof(type)); // It's decompiler generated if Local is null && HoistedField is null if (local is null) flags |= SourceVariableFlags.DecompilerGenerated; else flags &= ~SourceVariableFlags.DecompilerGenerated; Flags = flags; } /// /// Constructor /// /// Local or null /// Name used by the decompiler /// Hoisted field /// Flags public SourceLocal(Local? local, string name, FieldDef hoistedField, SourceVariableFlags flags) { Debug.Assert((flags & SourceVariableFlags.DecompilerGenerated) == 0); Local = local; Name = name ?? throw new ArgumentNullException(nameof(name)); HoistedField = hoistedField ?? throw new ArgumentNullException(nameof(hoistedField)); Type = hoistedField.FieldType; // It's decompiler generated if Local is null && HoistedField is null Flags = flags & ~SourceVariableFlags.DecompilerGenerated; } } }