private bool isICollectionImport(JoinPoint import, out InstanceRef iCollectionToSet, out MethodID addMethod) { addMethod = null; iCollectionToSet = null; //Detect if import type is of ICollection var imp = import.Point as Import; var itemType = imp.ImportTypeInfo.ItemType; var collectionTypeName = string.Format("System.Collections.Generic.ICollection<{0}>", itemType.TypeName); var collectionType = TypeDescriptor.Create(collectionTypeName); var isICollection = _context.IsOfType(imp.ImportTypeInfo.ImportType, collectionType); if (!isICollection) { //cannot resolve type as ICollection return(false); } var collectionAddMethod = _context.GetMethod(collectionType, "Add").MethodID; addMethod = collectionAddMethod; iCollectionToSet = getImportInstance(import); return(true); }
/// <summary> /// Calls the direct with return. /// </summary> /// <param name="method">The method.</param> /// <param name="argumentInstances">The argument instances.</param> /// <returns>InstanceRef.</returns> internal InstanceRef CallDirectWithReturn(DirectCompositionMethod method, params InstanceRef[] argumentInstances) { var resultStorage = getFreeStorage("ret"); //TODO determine result type var resultInstance = new InstanceRef(this, null, true); _instanceStorages.Add(resultInstance, resultStorage); var argumentStorages = from argumentInstance in argumentInstances select new VariableName(getStorage(argumentInstance)); //emitting routine emit((e) => { //wrap method into direct invoke instruction e.DirectInvoke((c) => { //fetch arguments var argumentValues = new List <Instance>(); foreach (var argumentStorage in argumentStorages) { var argumentValue = c.GetValue(argumentStorage); argumentValues.Add(argumentValue); } var result = method(c, argumentValues.ToArray()); c.SetValue(new VariableName(resultStorage), result); }); }); return(resultInstance); }
/// <summary> /// Creates the array that will contains given instances. /// </summary> /// <param name="itemType">Type of the array item.</param> /// <param name="instances">The instances.</param> /// <returns>Instance referene with created array.</returns> internal InstanceRef CreateArray(TypeDescriptor itemType, IEnumerable <InstanceRef> instances) { var instArray = instances.ToArray(); var arrayInfo = TypeDescriptor.Create(string.Format("Array<{0},1>", itemType.TypeName)); var intParam = ParameterTypeInfo.Create("p", TypeDescriptor.Create <int>()); var ctorID = Naming.Method(arrayInfo, Naming.CtorName, false, intParam); var setID = Naming.Method(arrayInfo, "set_Item", false, intParam, ParameterTypeInfo.Create("p2", itemType)); var arrayStorage = getFreeStorage("arr"); emit((e) => { //array construction e.AssignNewObject(arrayStorage, arrayInfo); var lengthVar = e.GetTemporaryVariable("len"); e.AssignLiteral(lengthVar, instArray.Length); e.Call(ctorID, arrayStorage, Arguments.Values(lengthVar)); //set instances to appropriate indexes var arrIndex = e.GetTemporaryVariable("set"); for (int i = 0; i < instArray.Length; ++i) { var instStorage = getStorage(instArray[i]); e.AssignLiteral(arrIndex, i); e.Call(setID, arrayStorage, Arguments.Values(arrIndex, instStorage)); } }); var array = new InstanceRef(this, arrayInfo, true); _instanceStorages[array] = arrayStorage; return(array); }
/// <summary> /// Calls the specified called instance. /// </summary> /// <param name="calledInstance">The called instance.</param> /// <param name="methodID">The method identifier.</param> /// <param name="arguments">The arguments.</param> internal void Call(InstanceRef calledInstance, MethodID methodID, InstanceRef[] arguments) { checkNull(methodID); var inst = getStorage(calledInstance); var args = getArgumentStorages(arguments); emit((e) => e.Call(methodID, inst, args)); }
/// <summary> /// Calls the with return. /// </summary> /// <param name="calledInstance">The called instance.</param> /// <param name="methodID">The method identifier.</param> /// <param name="arguments">The arguments.</param> /// <returns>InstanceRef.</returns> internal InstanceRef CallWithReturn(InstanceRef calledInstance, MethodID methodID, InstanceRef[] arguments) { checkNull(methodID); var inst = getStorage(calledInstance); var args = getArgumentStorages(arguments); var resultStorage = getFreeStorage("ret"); //TODO determine result type var resultInstance = new InstanceRef(this, null, true); _instanceStorages.Add(resultInstance, resultStorage); emit((e) => { e.Call(methodID, inst, args); e.AssignReturnValue(resultStorage, resultInstance.Type); }); return(resultInstance); }
/// <summary> /// Gets the storage. /// </summary> /// <param name="instance">The instance.</param> /// <returns>System.String.</returns> private string getStorage(InstanceRef instance) { return(_instanceStorages[instance]); }