mirror of https://github.com/icsharpcode/ILSpy.git
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							157 lines
						
					
					
						
							5.8 KiB
						
					
					
				
			
		
		
	
	
							157 lines
						
					
					
						
							5.8 KiB
						
					
					
				// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team | 
						|
//  | 
						|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this | 
						|
// software and associated documentation files (the "Software"), to deal in the Software | 
						|
// without restriction, including without limitation the rights to use, copy, modify, merge, | 
						|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | 
						|
// to whom the Software is furnished to do so, subject to the following conditions: | 
						|
//  | 
						|
// The above copyright notice and this permission notice shall be included in all copies or | 
						|
// substantial portions of the Software. | 
						|
//  | 
						|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | 
						|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | 
						|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | 
						|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | 
						|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
						|
// DEALINGS IN THE SOFTWARE. | 
						|
 | 
						|
using System; | 
						|
using System.Collections.Generic; | 
						|
using System.Collections.Immutable; | 
						|
using System.Linq; | 
						|
 | 
						|
using ICSharpCode.Decompiler; | 
						|
using ICSharpCode.Decompiler.CSharp; | 
						|
using ICSharpCode.Decompiler.Disassembler; | 
						|
using ICSharpCode.Decompiler.Metadata; | 
						|
using ICSharpCode.Decompiler.IL; | 
						|
using ICSharpCode.Decompiler.IL.Transforms; | 
						|
using ICSharpCode.Decompiler.TypeSystem; | 
						|
 | 
						|
using SRM = System.Reflection.Metadata; | 
						|
using static System.Reflection.Metadata.PEReaderExtensions; | 
						|
 | 
						|
namespace ICSharpCode.ILSpy | 
						|
{ | 
						|
#if DEBUG | 
						|
	/// <summary> | 
						|
	/// Represents the ILAst "language" used for debugging purposes. | 
						|
	/// </summary> | 
						|
	abstract class ILAstLanguage : Language | 
						|
	{ | 
						|
		public event EventHandler StepperUpdated; | 
						|
 | 
						|
		protected virtual void OnStepperUpdated(EventArgs e = null) | 
						|
		{ | 
						|
			StepperUpdated?.Invoke(this, e ?? new EventArgs()); | 
						|
		} | 
						|
 | 
						|
		public Stepper Stepper { get; set; } = new Stepper(); | 
						|
 | 
						|
		readonly string name; | 
						|
		 | 
						|
		protected ILAstLanguage(string name) | 
						|
		{ | 
						|
			this.name = name; | 
						|
		} | 
						|
		 | 
						|
		public override string Name { get { return name; } } | 
						|
 | 
						|
		internal static IEnumerable<ILAstLanguage> GetDebugLanguages() | 
						|
		{ | 
						|
			yield return new TypedIL(); | 
						|
			yield return new BlockIL(CSharpDecompiler.GetILTransforms()); | 
						|
		} | 
						|
		 | 
						|
		public override string FileExtension { | 
						|
			get { | 
						|
				return ".il"; | 
						|
			} | 
						|
		} | 
						|
 | 
						|
		public override string TypeDefinitionToString(TypeDefinition type, bool includeNamespace) | 
						|
		{ | 
						|
			PlainTextOutput output = new PlainTextOutput(); | 
						|
			((SRM.EntityHandle)type.Handle).WriteTo(type.Module, output, GenericContext.Empty, includeNamespace ? ILNameSyntax.TypeName : ILNameSyntax.ShortTypeName); | 
						|
			return output.ToString(); | 
						|
		} | 
						|
 | 
						|
		public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) | 
						|
		{ | 
						|
			base.DecompileMethod(method, output, options); | 
						|
			var module = method.Module; | 
						|
			new ReflectionDisassembler(output, options.CancellationToken).DisassembleMethodHeader(module, method.Handle); | 
						|
			output.WriteLine(); | 
						|
			output.WriteLine(); | 
						|
		} | 
						|
 | 
						|
		class TypedIL : ILAstLanguage | 
						|
		{ | 
						|
			public TypedIL() : base("Typed IL") {} | 
						|
			 | 
						|
			public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) | 
						|
			{ | 
						|
				base.DecompileMethod(method, output, options); | 
						|
				var metadata = method.Module.Metadata; | 
						|
				if (!method.Handle.HasBody(metadata)) | 
						|
					return; | 
						|
				var methodDef = metadata.GetMethodDefinition(method.Handle); | 
						|
				var typeSystem = new DecompilerTypeSystem(method.Module); | 
						|
				ILReader reader = new ILReader(typeSystem); | 
						|
				var methodBody = method.Module.Reader.GetMethodBody(methodDef.RelativeVirtualAddress); | 
						|
				reader.WriteTypedIL(method.Module, method.Handle, methodBody, output, options.CancellationToken); | 
						|
			} | 
						|
		} | 
						|
 | 
						|
		class BlockIL : ILAstLanguage | 
						|
		{ | 
						|
			readonly IReadOnlyList<IILTransform> transforms; | 
						|
 | 
						|
			public BlockIL(IReadOnlyList<IILTransform> transforms) : base("ILAst") | 
						|
			{ | 
						|
				this.transforms = transforms; | 
						|
			} | 
						|
 | 
						|
			public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options) | 
						|
			{ | 
						|
				base.DecompileMethod(method, output, options); | 
						|
				var metadata = method.Module.Metadata; | 
						|
				if (!method.Handle.HasBody(metadata)) | 
						|
					return; | 
						|
				var methodDef = metadata.GetMethodDefinition(method.Handle); | 
						|
				var typeSystem = new DecompilerTypeSystem(method.Module); | 
						|
				var specializingTypeSystem = typeSystem.GetSpecializingTypeSystem(new SimpleTypeResolveContext(typeSystem.ResolveAsMethod(method.Handle))); | 
						|
				var reader = new ILReader(specializingTypeSystem); | 
						|
				reader.UseDebugSymbols = options.DecompilerSettings.UseDebugSymbols; | 
						|
				var methodBody = method.Module.Reader.GetMethodBody(methodDef.RelativeVirtualAddress); | 
						|
				ILFunction il = reader.ReadIL(method.Module, method.Handle, methodBody, options.CancellationToken); | 
						|
				var namespaces = new HashSet<string>(); | 
						|
				var decompiler = new CSharpDecompiler(typeSystem, options.DecompilerSettings) { CancellationToken = options.CancellationToken }; | 
						|
				ILTransformContext context = decompiler.CreateILTransformContext(il); | 
						|
				context.Stepper.StepLimit = options.StepLimit; | 
						|
				context.Stepper.IsDebug = options.IsDebug; | 
						|
				try { | 
						|
					il.RunTransforms(transforms, context); | 
						|
				} catch (StepLimitReachedException) { | 
						|
				} catch (Exception ex) { | 
						|
					output.WriteLine(ex.ToString()); | 
						|
					output.WriteLine(); | 
						|
					output.WriteLine("ILAst after the crash:"); | 
						|
				} finally { | 
						|
					// update stepper even if a transform crashed unexpectedly | 
						|
					if (options.StepLimit == int.MaxValue) { | 
						|
						Stepper = context.Stepper; | 
						|
						OnStepperUpdated(new EventArgs()); | 
						|
					} | 
						|
				} | 
						|
				(output as ISmartTextOutput)?.AddButton(Images.ViewCode, "Show Steps", delegate { | 
						|
					DebugSteps.Show(); | 
						|
				}); | 
						|
				output.WriteLine(); | 
						|
				il.WriteTo(output, DebugSteps.Options); | 
						|
			} | 
						|
		} | 
						|
	} | 
						|
	#endif | 
						|
}
 | 
						|
 |