ILLanguage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using ICSharpCode.Decompiler;
  21. using ICSharpCode.Decompiler.Disassembler;
  22. using Mono.Cecil;
  23. namespace ICSharpCode.ILSpy
  24. {
  25. /// <summary>
  26. /// IL language support.
  27. /// </summary>
  28. /// <remarks>
  29. /// Currently comes in two versions:
  30. /// flat IL (detectControlStructure=false) and structured IL (detectControlStructure=true).
  31. /// </remarks>
  32. public class ILLanguage : Language
  33. {
  34. private readonly bool detectControlStructure;
  35. public ILLanguage(bool detectControlStructure)
  36. {
  37. this.detectControlStructure = detectControlStructure;
  38. }
  39. public override string Name {
  40. get { return "IL"; }
  41. }
  42. public override string FileExtension {
  43. get { return ".il"; }
  44. }
  45. public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
  46. {
  47. var dis = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  48. dis.DisassembleMethod(method);
  49. }
  50. public override void DecompileField(FieldDefinition field, ITextOutput output, DecompilationOptions options)
  51. {
  52. var dis = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  53. dis.DisassembleField(field);
  54. }
  55. public override void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options)
  56. {
  57. ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  58. rd.DisassembleProperty(property);
  59. if (property.GetMethod != null) {
  60. output.WriteLine();
  61. rd.DisassembleMethod(property.GetMethod);
  62. }
  63. if (property.SetMethod != null) {
  64. output.WriteLine();
  65. rd.DisassembleMethod(property.SetMethod);
  66. }
  67. foreach (var m in property.OtherMethods) {
  68. output.WriteLine();
  69. rd.DisassembleMethod(m);
  70. }
  71. }
  72. public override void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options)
  73. {
  74. ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  75. rd.DisassembleEvent(ev);
  76. if (ev.AddMethod != null) {
  77. output.WriteLine();
  78. rd.DisassembleMethod(ev.AddMethod);
  79. }
  80. if (ev.RemoveMethod != null) {
  81. output.WriteLine();
  82. rd.DisassembleMethod(ev.RemoveMethod);
  83. }
  84. foreach (var m in ev.OtherMethods) {
  85. output.WriteLine();
  86. rd.DisassembleMethod(m);
  87. }
  88. }
  89. public override void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options)
  90. {
  91. var dis = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  92. dis.DisassembleType(type);
  93. }
  94. public override void DecompileNamespace(string nameSpace, IEnumerable<TypeDefinition> types, ITextOutput output, DecompilationOptions options)
  95. {
  96. new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken).DisassembleNamespace(nameSpace, types);
  97. }
  98. public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options)
  99. {
  100. output.WriteLine("// " + assembly.FileName);
  101. output.WriteLine();
  102. ReflectionDisassembler rd = new ReflectionDisassembler(output, detectControlStructure, options.CancellationToken);
  103. if (options.FullDecompilation)
  104. rd.WriteAssemblyReferences(assembly.ModuleDefinition);
  105. if (assembly.AssemblyDefinition != null)
  106. rd.WriteAssemblyHeader(assembly.AssemblyDefinition);
  107. output.WriteLine();
  108. rd.WriteModuleHeader(assembly.ModuleDefinition);
  109. if (options.FullDecompilation) {
  110. output.WriteLine();
  111. output.WriteLine();
  112. rd.WriteModuleContents(assembly.ModuleDefinition);
  113. }
  114. }
  115. public override string TypeToString(TypeReference t, bool includeNamespace, ICustomAttributeProvider attributeProvider = null)
  116. {
  117. PlainTextOutput output = new PlainTextOutput();
  118. t.WriteTo(output, includeNamespace ? ILNameSyntax.TypeName : ILNameSyntax.ShortTypeName);
  119. return output.ToString();
  120. }
  121. }
  122. }