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.

66 lines
2.8 KiB

  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.Linq;
  20. using ICSharpCode.NRefactory.CSharp;
  21. using Mono.Cecil;
  22. namespace ICSharpCode.Decompiler.Ast.Transforms
  23. {
  24. /// <summary>
  25. /// Converts extension method calls into infix syntax.
  26. /// </summary>
  27. public class IntroduceExtensionMethods : IAstTransform
  28. {
  29. readonly DecompilerContext context;
  30. public IntroduceExtensionMethods(DecompilerContext context)
  31. {
  32. this.context = context;
  33. }
  34. public void Run(AstNode compilationUnit)
  35. {
  36. foreach (InvocationExpression invocation in compilationUnit.Descendants.OfType<InvocationExpression>()) {
  37. MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;
  38. MethodReference methodReference = invocation.Annotation<MethodReference>();
  39. if (mre != null && mre.Target is TypeReferenceExpression && methodReference != null && invocation.Arguments.Any()) {
  40. MethodDefinition d = methodReference.Resolve();
  41. if (d != null) {
  42. foreach (var ca in d.CustomAttributes) {
  43. if (ca.AttributeType.Name == "ExtensionAttribute" && ca.AttributeType.Namespace == "System.Runtime.CompilerServices") {
  44. var firstArgument = invocation.Arguments.First();
  45. if (firstArgument is NullReferenceExpression)
  46. firstArgument = firstArgument.ReplaceWith(expr => expr.CastTo(AstBuilder.ConvertType(d.Parameters.First().ParameterType)));
  47. else
  48. mre.Target = firstArgument.Detach();
  49. if (invocation.Arguments.Any()) {
  50. // HACK: removing type arguments should be done indepently from whether a method is an extension method,
  51. // just by testing whether the arguments can be inferred
  52. mre.TypeArguments.Clear();
  53. }
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }