Language.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 Mono.Cecil;
  22. namespace ICSharpCode.ILSpy
  23. {
  24. /// <summary>
  25. /// Base class for language-specific decompiler implementations.
  26. /// </summary>
  27. public abstract class Language
  28. {
  29. /// <summary>
  30. /// Gets the name of the language (as shown in the UI)
  31. /// </summary>
  32. public abstract string Name { get; }
  33. /// <summary>
  34. /// Gets the file extension used by source code files in this language.
  35. /// </summary>
  36. public abstract string FileExtension { get; }
  37. public virtual string ProjectFileExtension
  38. {
  39. get { return null; }
  40. }
  41. // /// <summary>
  42. // /// Gets the syntax highlighting used for this language.
  43. // /// </summary>
  44. // public virtual ICSharpCode.AvalonEdit.Highlighting.IHighlightingDefinition SyntaxHighlighting
  45. // {
  46. // get
  47. // {
  48. // return ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinitionByExtension(this.FileExtension);
  49. // }
  50. // }
  51. public virtual void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
  52. {
  53. WriteCommentLine(output, TypeToString(method.DeclaringType, true) + "." + method.Name);
  54. }
  55. public virtual void DecompileProperty(PropertyDefinition property, ITextOutput output, DecompilationOptions options)
  56. {
  57. WriteCommentLine(output, TypeToString(property.DeclaringType, true) + "." + property.Name);
  58. }
  59. public virtual void DecompileField(FieldDefinition field, ITextOutput output, DecompilationOptions options)
  60. {
  61. WriteCommentLine(output, TypeToString(field.DeclaringType, true) + "." + field.Name);
  62. }
  63. public virtual void DecompileEvent(EventDefinition ev, ITextOutput output, DecompilationOptions options)
  64. {
  65. WriteCommentLine(output, TypeToString(ev.DeclaringType, true) + "." + ev.Name);
  66. }
  67. public virtual void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options)
  68. {
  69. WriteCommentLine(output, TypeToString(type, true));
  70. }
  71. public virtual void DecompileNamespace(string nameSpace, IEnumerable<TypeDefinition> types, ITextOutput output, DecompilationOptions options)
  72. {
  73. WriteCommentLine(output, nameSpace);
  74. }
  75. public virtual void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options)
  76. {
  77. WriteCommentLine(output, assembly.FileName);
  78. if (assembly.AssemblyDefinition != null) {
  79. var name = assembly.AssemblyDefinition.Name;
  80. if (name.IsWindowsRuntime) {
  81. WriteCommentLine(output, name.Name + " [WinRT]");
  82. } else {
  83. WriteCommentLine(output, name.FullName);
  84. }
  85. } else {
  86. WriteCommentLine(output, assembly.ModuleDefinition.Name);
  87. }
  88. }
  89. public virtual void WriteCommentLine(ITextOutput output, string comment)
  90. {
  91. output.WriteLine("// " + comment);
  92. }
  93. /// <summary>
  94. /// Converts a type reference into a string. This method is used by the member tree node for parameter and return types.
  95. /// </summary>
  96. public virtual string TypeToString(TypeReference type, bool includeNamespace, ICustomAttributeProvider typeAttributes = null)
  97. {
  98. if (includeNamespace)
  99. return type.FullName;
  100. else
  101. return type.Name;
  102. }
  103. /// <summary>
  104. /// Converts a member signature to a string.
  105. /// This is used for displaying the tooltip on a member reference.
  106. /// </summary>
  107. public virtual string GetTooltip(MemberReference member)
  108. {
  109. if (member is TypeReference)
  110. return TypeToString((TypeReference)member, true);
  111. else
  112. return member.ToString();
  113. }
  114. public virtual string FormatPropertyName(PropertyDefinition property, bool? isIndexer = null)
  115. {
  116. if (property == null)
  117. throw new ArgumentNullException("property");
  118. return property.Name;
  119. }
  120. public virtual string FormatTypeName(TypeDefinition type)
  121. {
  122. if (type == null)
  123. throw new ArgumentNullException("type");
  124. return type.Name;
  125. }
  126. /// <summary>
  127. /// Used for WPF keyboard navigation.
  128. /// </summary>
  129. public override string ToString()
  130. {
  131. return Name;
  132. }
  133. public virtual bool ShowMember(MemberReference member)
  134. {
  135. return true;
  136. }
  137. /// <summary>
  138. /// Used by the analyzer to map compiler generated code back to the original code's location
  139. /// </summary>
  140. public virtual MemberReference GetOriginalCodeLocation(MemberReference member)
  141. {
  142. return member;
  143. }
  144. }
  145. }