/NRefactory/ICSharpCode.NRefactory/Semantics/MemberResolveResult.cs

http://github.com/icsharpcode/ILSpy · C# · 148 lines · 102 code · 17 blank · 29 comment · 13 complexity · c932d6958abb11407e9de48c79a1f61c MD5 · raw file

  1. // Copyright (c) 2010-2013 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 System.Globalization;
  21. using System.Linq;
  22. using ICSharpCode.NRefactory.TypeSystem;
  23. namespace ICSharpCode.NRefactory.Semantics
  24. {
  25. /// <summary>
  26. /// Represents the result of a member invocation.
  27. /// Used for field/property/event access.
  28. /// Also, <see cref="InvocationResolveResult"/> derives from MemberResolveResult.
  29. /// </summary>
  30. public class MemberResolveResult : ResolveResult
  31. {
  32. readonly IMember member;
  33. readonly bool isConstant;
  34. readonly object constantValue;
  35. readonly ResolveResult targetResult;
  36. readonly bool isVirtualCall;
  37. public MemberResolveResult(ResolveResult targetResult, IMember member, IType returnTypeOverride = null)
  38. : base(returnTypeOverride ?? ComputeType(member))
  39. {
  40. this.targetResult = targetResult;
  41. this.member = member;
  42. var thisRR = targetResult as ThisResolveResult;
  43. this.isVirtualCall = member.IsOverridable && !(thisRR != null && thisRR.CausesNonVirtualInvocation);
  44. IField field = member as IField;
  45. if (field != null) {
  46. isConstant = field.IsConst;
  47. if (isConstant)
  48. constantValue = field.ConstantValue;
  49. }
  50. }
  51. public MemberResolveResult(ResolveResult targetResult, IMember member, bool isVirtualCall, IType returnTypeOverride = null)
  52. : base(returnTypeOverride ?? ComputeType(member))
  53. {
  54. this.targetResult = targetResult;
  55. this.member = member;
  56. this.isVirtualCall = isVirtualCall;
  57. IField field = member as IField;
  58. if (field != null) {
  59. isConstant = field.IsConst;
  60. if (isConstant)
  61. constantValue = field.ConstantValue;
  62. }
  63. }
  64. static IType ComputeType(IMember member)
  65. {
  66. switch (member.SymbolKind) {
  67. case SymbolKind.Constructor:
  68. return member.DeclaringType;
  69. case SymbolKind.Field:
  70. if (((IField)member).IsFixed)
  71. return new PointerType(member.ReturnType);
  72. break;
  73. }
  74. return member.ReturnType;
  75. }
  76. public MemberResolveResult(ResolveResult targetResult, IMember member, IType returnType, bool isConstant, object constantValue)
  77. : base(returnType)
  78. {
  79. this.targetResult = targetResult;
  80. this.member = member;
  81. this.isConstant = isConstant;
  82. this.constantValue = constantValue;
  83. }
  84. public MemberResolveResult(ResolveResult targetResult, IMember member, IType returnType, bool isConstant, object constantValue, bool isVirtualCall)
  85. : base(returnType)
  86. {
  87. this.targetResult = targetResult;
  88. this.member = member;
  89. this.isConstant = isConstant;
  90. this.constantValue = constantValue;
  91. this.isVirtualCall = isVirtualCall;
  92. }
  93. public ResolveResult TargetResult {
  94. get { return targetResult; }
  95. }
  96. /// <summary>
  97. /// Gets the member.
  98. /// This property never returns null.
  99. /// </summary>
  100. public IMember Member {
  101. get { return member; }
  102. }
  103. /// <summary>
  104. /// Gets whether this MemberResolveResult is a virtual call.
  105. /// </summary>
  106. public bool IsVirtualCall {
  107. get { return isVirtualCall; }
  108. }
  109. public override bool IsCompileTimeConstant {
  110. get { return isConstant; }
  111. }
  112. public override object ConstantValue {
  113. get { return constantValue; }
  114. }
  115. public override IEnumerable<ResolveResult> GetChildResults()
  116. {
  117. if (targetResult != null)
  118. return new[] { targetResult };
  119. else
  120. return Enumerable.Empty<ResolveResult>();
  121. }
  122. public override string ToString()
  123. {
  124. return string.Format(CultureInfo.InvariantCulture, "[{0} {1}]", GetType().Name, member);
  125. }
  126. public override DomRegion GetDefinitionRegion()
  127. {
  128. return member.Region;
  129. }
  130. }
  131. }