PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting/Actions/DefaultBinder.Invoke.cs

#
C# | 293 lines | 183 code | 42 blank | 68 comment | 23 complexity | 10845403a719d53328b58c35520f06c6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System; using Microsoft;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using Microsoft.Linq.Expressions;
  19. using System.Reflection;
  20. using Microsoft.Scripting;
  21. using Microsoft.Scripting.Actions;
  22. using Microsoft.Scripting.Generation;
  23. using Microsoft.Scripting.Runtime;
  24. using Microsoft.Scripting.Utils;
  25. using Microsoft.Scripting.Actions.Calls;
  26. namespace Microsoft.Scripting.Actions {
  27. using Ast = Microsoft.Linq.Expressions.Expression;
  28. public partial class DefaultBinder : ActionBinder {
  29. /// <summary>
  30. /// Provides default binding for performing a call on the specified meta objects.
  31. /// </summary>
  32. /// <param name="signature">The signature describing the call</param>
  33. /// <param name="target">The object to be called</param>
  34. /// <param name="args">
  35. /// Additional meta objects are the parameters for the call as specified by the CallSignature in the CallAction.
  36. /// </param>
  37. /// <returns>A MetaObject representing the call or the error.</returns>
  38. public MetaObject Call(CallSignature signature, MetaObject target, params MetaObject[] args) {
  39. return Call(signature, new ParameterBinder(this), target, args);
  40. }
  41. /// <summary>
  42. /// Provides default binding for performing a call on the specified meta objects.
  43. /// </summary>
  44. /// <param name="signature">The signature describing the call</param>
  45. /// <param name="target">The meta object to be called.</param>
  46. /// <param name="args">
  47. /// Additional meta objects are the parameters for the call as specified by the CallSignature in the CallAction.
  48. /// </param>
  49. /// <param name="parameterBinder">ParameterBinder used to map arguments to parameters.</param>
  50. /// <returns>A MetaObject representing the call or the error.</returns>
  51. public MetaObject Call(CallSignature signature, ParameterBinder parameterBinder, MetaObject target, params MetaObject[] args) {
  52. ContractUtils.RequiresNotNullItems(args, "args");
  53. ContractUtils.RequiresNotNull(parameterBinder, "parameterBinder");
  54. TargetInfo targetInfo = GetTargetInfo(signature, target, args);
  55. if (targetInfo != null) {
  56. // we're calling a well-known MethodBase
  57. return MakeMetaMethodCall(signature, parameterBinder, targetInfo);
  58. } else {
  59. // we can't call this object
  60. return MakeCannotCallRule(target, target.LimitType);
  61. }
  62. }
  63. #region Method Call Rule
  64. private MetaObject MakeMetaMethodCall(CallSignature signature, ParameterBinder parameterBinder, TargetInfo targetInfo) {
  65. Restrictions restrictions = Restrictions.Combine(targetInfo.Arguments).Merge(targetInfo.Restrictions);
  66. if (targetInfo.Instance != null) {
  67. restrictions = targetInfo.Instance.Restrictions.Merge(restrictions);
  68. }
  69. if (targetInfo.Instance != null) {
  70. return CallInstanceMethod(
  71. parameterBinder,
  72. targetInfo.Targets,
  73. targetInfo.Instance,
  74. targetInfo.Arguments,
  75. signature,
  76. restrictions
  77. );
  78. }
  79. return CallMethod(
  80. parameterBinder,
  81. targetInfo.Targets,
  82. targetInfo.Arguments,
  83. signature,
  84. restrictions);
  85. }
  86. #endregion
  87. #region Target acquisition
  88. /// <summary>
  89. /// Gets a TargetInfo object for performing a call on this object.
  90. ///
  91. /// If this object is a delegate we bind to the Invoke method.
  92. /// If this object is a MemberGroup or MethodGroup we bind to the methods in the member group.
  93. /// If this object is a BoundMemberTracker we bind to the methods with the bound instance.
  94. /// If the underlying type has defined an operator Call method we'll bind to that method.
  95. /// </summary>
  96. private TargetInfo GetTargetInfo(CallSignature signature, MetaObject target, MetaObject[] args) {
  97. Debug.Assert(target.HasValue);
  98. object objTarget = target.Value;
  99. return
  100. TryGetDelegateTargets(target, args, objTarget as Delegate) ??
  101. TryGetMemberGroupTargets(target, args, objTarget as MemberGroup) ??
  102. TryGetMethodGroupTargets(target, args, objTarget as MethodGroup) ??
  103. TryGetBoundMemberTargets(target, args, objTarget as BoundMemberTracker) ??
  104. TryGetOperatorTargets(target, args, target, signature);
  105. }
  106. /// <summary>
  107. /// Binds to the methods in a method group.
  108. /// </summary>
  109. private static TargetInfo TryGetMethodGroupTargets(MetaObject target, MetaObject[] args, MethodGroup mthgrp) {
  110. if (mthgrp != null) {
  111. List<MethodBase> foundTargets = new List<MethodBase>();
  112. foreach (MethodTracker mt in mthgrp.Methods) {
  113. foundTargets.Add(mt.Method);
  114. }
  115. return new TargetInfo(null, ArrayUtils.Insert(target, args), Restrictions.InstanceRestriction(target.Expression, mthgrp), foundTargets.ToArray());
  116. }
  117. return null;
  118. }
  119. /// <summary>
  120. /// Binds to the methods in a member group.
  121. ///
  122. /// TODO: We should really only have either MemberGroup or MethodGroup, not both.
  123. /// </summary>
  124. private static TargetInfo TryGetMemberGroupTargets(MetaObject target, MetaObject[] args, MemberGroup mg) {
  125. if (mg != null) {
  126. MethodBase[] targets;
  127. List<MethodInfo> foundTargets = new List<MethodInfo>();
  128. foreach (MemberTracker mt in mg) {
  129. if (mt.MemberType == TrackerTypes.Method) {
  130. foundTargets.Add(((MethodTracker)mt).Method);
  131. }
  132. }
  133. targets = foundTargets.ToArray();
  134. return new TargetInfo(null, ArrayUtils.Insert(target, args), targets);
  135. }
  136. return null;
  137. }
  138. /// <summary>
  139. /// Binds to the BoundMemberTracker and uses the instance in the tracker and restricts
  140. /// based upon the object instance type.
  141. /// </summary>
  142. private TargetInfo TryGetBoundMemberTargets(MetaObject self, MetaObject[] args, BoundMemberTracker bmt) {
  143. if (bmt != null) {
  144. Debug.Assert(bmt.Instance == null); // should be null for trackers that leak to user code
  145. MethodBase[] targets;
  146. // instance is pulled from the BoundMemberTracker and restricted to the correct
  147. // type.
  148. MetaObject instance = new MetaObject(
  149. Ast.Convert(
  150. Ast.Property(
  151. Ast.Convert(self.Expression, typeof(BoundMemberTracker)),
  152. typeof(BoundMemberTracker).GetProperty("ObjectInstance")
  153. ),
  154. bmt.BoundTo.DeclaringType
  155. ),
  156. self.Restrictions
  157. ).Restrict(CompilerHelpers.GetType(bmt.ObjectInstance));
  158. // we also add a restriction to make sure we're going to the same BoundMemberTracker
  159. Restrictions restrictions = Restrictions.ExpressionRestriction(
  160. Ast.Equal(
  161. Ast.Property(
  162. Ast.Convert(self.Expression, typeof(BoundMemberTracker)),
  163. typeof(BoundMemberTracker).GetProperty("BoundTo")
  164. ),
  165. Ast.Constant(bmt.BoundTo)
  166. )
  167. );
  168. switch (bmt.BoundTo.MemberType) {
  169. case TrackerTypes.MethodGroup:
  170. targets = ((MethodGroup)bmt.BoundTo).GetMethodBases();
  171. break;
  172. case TrackerTypes.Method:
  173. targets = new MethodBase[] { ((MethodTracker)bmt.BoundTo).Method };
  174. break;
  175. default:
  176. throw new InvalidOperationException(); // nothing else binds yet
  177. }
  178. return new TargetInfo(instance, args, restrictions, targets);
  179. }
  180. return null;
  181. }
  182. /// <summary>
  183. /// Binds to the Invoke method on a delegate if this is a delegate type.
  184. /// </summary>
  185. private static TargetInfo TryGetDelegateTargets(MetaObject target, MetaObject[] args, Delegate d) {
  186. if (d != null) {
  187. return new TargetInfo(target, args, d.GetType().GetMethod("Invoke"));
  188. }
  189. return null;
  190. }
  191. /// <summary>
  192. /// Attempts to bind to an operator Call method.
  193. /// </summary>
  194. private TargetInfo TryGetOperatorTargets(MetaObject self, MetaObject[] args, object target, CallSignature signature) {
  195. MethodBase[] targets;
  196. Type targetType = CompilerHelpers.GetType(target);
  197. MemberGroup callMembers = GetMember(OldCallAction.Make(this, signature), targetType, "Call");
  198. List<MethodBase> callTargets = new List<MethodBase>();
  199. foreach (MemberTracker mi in callMembers) {
  200. if (mi.MemberType == TrackerTypes.Method) {
  201. MethodInfo method = ((MethodTracker)mi).Method;
  202. if (method.IsSpecialName) {
  203. callTargets.Add(method);
  204. }
  205. }
  206. }
  207. Expression instance = null;
  208. if (callTargets.Count > 0) {
  209. targets = callTargets.ToArray();
  210. instance = Ast.Convert(self.Expression, CompilerHelpers.GetType(target));
  211. return new TargetInfo(null, ArrayUtils.Insert(self, args), targets);
  212. }
  213. return null;
  214. }
  215. #endregion
  216. #region Error support
  217. private MetaObject MakeCannotCallRule(MetaObject self, Type type) {
  218. return MakeError(
  219. ErrorInfo.FromException(
  220. Ast.New(
  221. typeof(ArgumentTypeException).GetConstructor(new Type[] { typeof(string) }),
  222. Ast.Constant(type.Name + " is not callable")
  223. )
  224. ),
  225. self.Restrictions.Merge(Restrictions.TypeRestriction(self.Expression, type))
  226. );
  227. }
  228. #endregion
  229. /// <summary>
  230. /// Encapsulates information about the target of the call. This includes an implicit instance for the call,
  231. /// the methods that we'll be calling as well as any restrictions required to perform the call.
  232. /// </summary>
  233. class TargetInfo {
  234. public readonly MetaObject Instance;
  235. public readonly MetaObject[] Arguments;
  236. public readonly MethodBase[] Targets;
  237. public readonly Restrictions Restrictions;
  238. public TargetInfo(MetaObject instance, MetaObject[] arguments, params MethodBase[] args) :
  239. this(instance, arguments, Restrictions.Empty, args) {
  240. }
  241. public TargetInfo(MetaObject instance, MetaObject[] arguments, Restrictions restrictions, params MethodBase[] targets) {
  242. Assert.NotNullItems(targets);
  243. Assert.NotNull(restrictions);
  244. Instance = instance;
  245. Arguments = arguments;
  246. Targets = targets;
  247. Restrictions = restrictions;
  248. }
  249. }
  250. }
  251. }