private IAstNode ProcessReadWriteSimple(ReadWriteSimple readWriteSimple, int operationId)
        {
            IAstRefOrValue sourceValue = ReadSrcMappingValue(readWriteSimple, operationId);

            IAstRefOrValue convertedValue;

            if (readWriteSimple.NullSubstitutor != null && (ReflectionUtils.IsNullable(readWriteSimple.Source.MemberType) || !readWriteSimple.Source.MemberType.IsValueType))
            {
                convertedValue = new AstIfTernar(
                    ReflectionUtils.IsNullable(readWriteSimple.Source.MemberType)
                        ? (IAstValue) new AstExprNot(AstBuildHelper.ReadPropertyRV(new AstValueToAddr((IAstValue)sourceValue), readWriteSimple.Source.MemberType.GetProperty("HasValue")))
                        : new AstExprIsNull(sourceValue),
                    GetNullValue(readWriteSimple.NullSubstitutor),     // source is null
                    AstBuildHelper.CastClass(
                        ConvertMappingValue(
                            readWriteSimple,
                            operationId,
                            sourceValue
                            ),
                        readWriteSimple.Destination.MemberType
                        )
                    );
            }
            else
            {
                convertedValue =
                    ConvertMappingValue(
                        readWriteSimple,
                        operationId,
                        sourceValue
                        );
            }

            return(WriteMappingValue(readWriteSimple, operationId, convertedValue));
        }
Exemplo n.º 2
0
        private IAstNode ProcessDestSrcReadOperation(
            DestSrcReadOperation operation,
            int operationId)
        {
            IAstRefOrValue src =
                AstBuildHelper.ReadMembersChain(
                    AstBuildHelper.ReadLocalRA(locFrom),
                    operation.Source.MembersChain
                    );

            IAstRefOrValue dst =
                AstBuildHelper.ReadMembersChain(
                    AstBuildHelper.ReadLocalRA(locFrom),
                    operation.Destination.MembersChain
                    );

            return(AstBuildHelper.CallMethod(
                       typeof(ValueProcessor).GetMethod("Invoke"),
                       new AstCastclassRef(
                           (IAstRef)AstBuildHelper.ReadMemberRV(
                               GetStoredObject(operationId, typeof(DestSrcReadOperation)),
                               typeof(DestWriteOperation).GetProperty("ValueProcessor")
                               ),
                           operation.ValueProcessor.GetType()
                           ),
                       new List <IAstStackItem> {
                src, dst, AstBuildHelper.ReadLocalRV(locState)
            }
                       ));
        }
Exemplo n.º 3
0
 public AstIfNull(IAstRef value, IAstRefOrValue ifNullValue)
 {
     _value = value;
     _ifNullValue = ifNullValue;
     if (!_value.itemType.IsAssignableFrom(_ifNullValue.itemType))
     {
         throw new EmitMapperException("Incorrect ifnull expression");
     }
 }
Exemplo n.º 4
0
 public AstIfNull(IAstRef value, IAstRefOrValue ifNullValue)
 {
     _value       = value;
     _ifNullValue = ifNullValue;
     if (!_value.itemType.IsAssignableFrom(_ifNullValue.itemType))
     {
         throw new EmitMapperException("Incorrect ifnull expression");
     }
 }
Exemplo n.º 5
0
    /// <summary>
    ///   Casts the class.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <param name="targetType">The target type.</param>
    /// <returns>An IAstRefOrValue.</returns>
    public static IAstRefOrValue CastClass(IAstRefOrValue value, Type targetType)
    {
        if (targetType.IsValueType)
        {
            return(new AstCastclassValue(value, targetType));
        }

        return(new AstCastclassRef(value, targetType));
    }
Exemplo n.º 6
0
        public AstIfTernar(IAstRefOrValue condition, IAstRefOrValue trueBranch, IAstRefOrValue falseBranch)
        {
            if (trueBranch.itemType != falseBranch.itemType)
            {
                throw new EmitMapperException("Types mismatch");
            }

            this.condition = condition;
            this.trueBranch = trueBranch;
            this.falseBranch = falseBranch;
        }
Exemplo n.º 7
0
        public AstIfTernar(IAstRefOrValue condition, IAstRefOrValue trueBranch, IAstRefOrValue falseBranch)
        {
            if (trueBranch.itemType != falseBranch.itemType)
            {
                throw new EmitMapperException("Types mismatch");
            }

            this.condition   = condition;
            this.trueBranch  = trueBranch;
            this.falseBranch = falseBranch;
        }
Exemplo n.º 8
0
        private IAstRefOrValue ConvertMappingValue(
            ReadWriteSimple rwMapOp,
            int operationId,
            IAstRefOrValue sourceValue)
        {
            IAstRefOrValue convertedValue = sourceValue;

            if (rwMapOp.Converter != null)
            {
                convertedValue = AstBuildHelper.CallMethod(
                    rwMapOp.Converter.GetType().GetMethod("Invoke"),
                    new AstCastclassRef(
                        (IAstRef)AstBuildHelper.ReadMemberRV(
                            GetStoredObject(operationId, typeof(ReadWriteSimple)),
                            typeof(ReadWriteSimple).GetProperty("Converter")
                            ),
                        rwMapOp.Converter.GetType()
                        ),
                    new List <IAstStackItem>()
                {
                    sourceValue,
                    AstBuildHelper.ReadLocalRV(locState),
                }
                    );
            }
            else
            {
                if (rwMapOp.ShallowCopy && rwMapOp.Destination.MemberType == rwMapOp.Source.MemberType)
                {
                    convertedValue = sourceValue;
                }
                else
                {
                    var mi = staticConvertersManager.GetStaticConverter(rwMapOp.Source.MemberType, rwMapOp.Destination.MemberType);
                    if (mi != null)
                    {
                        convertedValue = AstBuildHelper.CallMethod(
                            mi,
                            null,
                            new List <IAstStackItem> {
                            sourceValue
                        }
                            );
                    }
                    else
                    {
                        convertedValue = ConvertByMapper(rwMapOp);
                    }
                }
            }

            return(convertedValue);
        }
Exemplo n.º 9
0
        public static IAstRefOrValue ReadMembersChain(IAstRefOrAddr sourceObject, MemberInfo[] membersChain)
        {
            IAstRefOrAddr  src    = sourceObject;
            IAstRefOrValue result = null;

            for (int i = 0; i < membersChain.Length - 1; ++i)
            {
                src = ReadMemberRA(src, membersChain[i]);
            }
            result = ReadMemberRV(src, membersChain[membersChain.Length - 1]);
            return(result);
        }
Exemplo n.º 10
0
 public AstWriteProperty(IAstRefOrAddr targetObject, IAstRefOrValue value, PropertyInfo propertyInfo)
 {
     _targetObject = targetObject;
     _value        = value;
     _propertyInfo = propertyInfo;
     _setMethod    = propertyInfo.GetSetMethod();
     if (_setMethod == null)
     {
         throw new Exception("Property " + propertyInfo.Name + " doesn't have set accessor");
     }
     if (_setMethod.GetParameters().Length != 1)
     {
         throw new EmitMapperException("Property " + propertyInfo.Name + " has invalid arguments");
     }
 }
Exemplo n.º 11
0
		public AstWriteProperty(IAstRefOrAddr targetObject, IAstRefOrValue value, PropertyInfo propertyInfo)
		{
			_targetObject = targetObject;
			_value = value;
			_propertyInfo = propertyInfo;
			_setMethod = propertyInfo.GetSetMethod();
			if (_setMethod == null)
			{
				throw new Exception("Property " + propertyInfo.Name + " doesn't have set accessor");
			}
			if (_setMethod.GetParameters().Length != 1)
			{
				throw new EmitMapperException("Property " + propertyInfo.Name + " has invalid arguments");
			}
		}
Exemplo n.º 12
0
 private IAstNode Process_ValuesFilter(
     IReadWriteOperation op,
     int operationId,
     IAstNode result,
     IAstRefOrValue value,
     string fieldName,
     Delegate filterDelegate
     )
 {
     result = new AstComplexNode
     {
         nodes = new List <IAstNode>
         {
             new AstIf
             {
                 condition = (IAstValue)AstBuildHelper.CallMethod(
                     filterDelegate.GetType().GetMethod("Invoke"),
                     new AstCastclassRef(
                         (IAstRef)AstBuildHelper.ReadMemberRV(
                             GetStoredObject(operationId, typeof(IReadWriteOperation)),
                             typeof(IReadWriteOperation).GetProperty(fieldName)
                             ),
                         filterDelegate.GetType()
                         ),
                     new List <IAstStackItem>()
                 {
                     value,
                     AstBuildHelper.ReadLocalRV(locState),
                 }
                     ),
                 trueBranch = new AstComplexNode {
                     nodes = new List <IAstNode> {
                         result
                     }
                 }
             }
         }
     };
     return(result);
 }
Exemplo n.º 13
0
    /// <summary>
    ///   Writes the members chain.
    /// </summary>
    /// <param name="membersChain">The members chain.</param>
    /// <param name="targetObject">The target object.</param>
    /// <param name="value">The value.</param>
    /// <returns>An IAstNode.</returns>
    public static IAstNode WriteMembersChain(
        IEnumerable <MemberInfo> membersChain,
        IAstRefOrAddr targetObject,
        IAstRefOrValue value)
    {
        var        readTarget = targetObject;
        var        enumerator = membersChain.GetEnumerator();
        MemberInfo cur        = null;

        if (enumerator.MoveNext())
        {
            cur = enumerator.Current;
        }

        while (enumerator.MoveNext())
        {
            readTarget = ReadMemberRA(readTarget, cur);
            cur        = enumerator.Current;
        }

        return(WriteMember(cur, readTarget, value));
    }
Exemplo n.º 14
0
        private IAstNode WriteMappingValue(
            IMappingOperation mappingOperation,
            int mappingItemId,
            IAstRefOrValue value)
        {
            IAstNode writeValue;

            if (mappingOperation is SrcReadOperation)
            {
                writeValue = AstBuildHelper.CallMethod(
                    typeof(ValueSetter).GetMethod("Invoke"),
                    new AstCastclassRef(
                        (IAstRef)AstBuildHelper.ReadMemberRV(
                            GetStoredObject(mappingItemId, typeof(SrcReadOperation)),
                            typeof(SrcReadOperation).GetProperty("Setter")
                            ),
                        (mappingOperation as SrcReadOperation).Setter.GetType()
                        ),
                    new List <IAstStackItem>()
                {
                    AstBuildHelper.ReadLocalRV(locTo),
                    value,
                    AstBuildHelper.ReadLocalRV(locState),
                }
                    );
            }
            else
            {
                writeValue = AstBuildHelper.WriteMembersChain(
                    (mappingOperation as IDestOperation).Destination.MembersChain,
                    AstBuildHelper.ReadLocalRA(locTo),
                    value
                    );
            }
            return(writeValue);
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref="AstWriteLocal" /> class.
 /// </summary>
 /// <param name="loc">The loc.</param>
 /// <param name="value">The value.</param>
 public AstWriteLocal(LocalVariableInfo loc, IAstRefOrValue value)
 {
     LocalIndex = loc.LocalIndex;
     LocalType  = loc.LocalType;
     Value      = value;
 }
Exemplo n.º 16
0
        public AstExprNot(IAstRefOrValue value)
		{
			_value = value;
		}
Exemplo n.º 17
0
		public AstExprIsNull(IAstRefOrValue value)
        {
            this.value = value;
        }
Exemplo n.º 18
0
		public static IAstRefOrValue Convert(
			Type destinationType, 
			Type sourceType, 
			IAstRefOrValue sourceValue
			)
		{
			if (destinationType == sourceValue.itemType)
			{
				return sourceValue;
			}

			if (destinationType == typeof(string))
			{
				return 
					new AstCallMethodRef(
						typeof(NativeConverter).GetMethod(
						"ObjectToString",
						BindingFlags.NonPublic | BindingFlags.Static
					),
					null,
                    new List<IAstStackItem>()
					{
						sourceValue
					}
				);
			}

			foreach (var m in typeof(Convert).GetMethods(BindingFlags.Static | BindingFlags.Public))
			{
				if (m.ReturnType == destinationType)
				{
					var parameters = m.GetParameters();
					if (parameters.Length == 1 && parameters[0].ParameterType == sourceType)
					{
						return
							AstBuildHelper.CallMethod(
								m,
								null,
                                new List<IAstStackItem> { sourceValue }
							);
					}
				}
			}

			return AstBuildHelper.CallMethod(
				typeof(EMConvert).GetMethod(
					"ChangeType", 
					new[] 
					{ 
						typeof(object), 
						typeof(Type), 
						typeof(Type)
					}
				),
				null,
				new List<IAstStackItem>{
					sourceValue,
					new AstTypeof(){type = sourceType},
					new AstTypeof(){type = destinationType}
				}
			);
		}
Exemplo n.º 19
0
 public AstCastclass(IAstRefOrValue value, Type targetType)
 {
     Value      = value;
     TargetType = targetType;
 }
Exemplo n.º 20
0
 public AstExprNot(IAstRefOrValue value)
 {
     _value = value;
 }
Exemplo n.º 21
0
 public static IAstNode WriteMember(MemberInfo memberInfo, IAstRefOrAddr targetObject, IAstRefOrValue value)
 {
     if (memberInfo.MemberType == MemberTypes.Field)
     {
         return(new AstWriteField()
         {
             fieldInfo = (FieldInfo)memberInfo,
             targetObject = targetObject,
             value = value
         });
     }
     else
     {
         return(new AstWriteProperty(targetObject, value, (PropertyInfo)memberInfo));
     }
 }
Exemplo n.º 22
0
 public static IAstRefOrValue CastClass(IAstRefOrValue value, Type targetType)
 {
     if (targetType.IsValueType)
     {
         return new AstCastclassValue(value, targetType);
     }
     else
     {
         return new AstCastclassRef(value, targetType);
     }
 }
Exemplo n.º 23
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="AstCastclassRef" /> class.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <param name="targetType">The target type.</param>
 public AstCastclassRef(IAstRefOrValue value, Type targetType)
     : base(value, targetType)
 {
 }
		private IAstNode WriteMappingValue(
			IMappingOperation mappingOperation,
			int mappingItemId,
			IAstRefOrValue value)
		{
			IAstNode writeValue;

			if (mappingOperation is SrcReadOperation)
			{
				writeValue = AstBuildHelper.CallMethod(
					typeof(ValueSetter).GetMethod("Invoke"),
                    new AstCastclassRef(
					    (IAstRef)AstBuildHelper.ReadMemberRV(
					         GetStoredObject(mappingItemId, typeof(SrcReadOperation)),
					         typeof(SrcReadOperation).GetProperty("Setter")
					     ),
                         (mappingOperation as SrcReadOperation).Setter.GetType()
                    ),
					new List<IAstStackItem>()
                        {
							AstBuildHelper.ReadLocalRV(locTo),
                            value,
							AstBuildHelper.ReadLocalRV(locState),
                        }
					);
			}
			else
			{
				writeValue = AstBuildHelper.WriteMembersChain(
					(mappingOperation as IDestOperation).Destination.MembersChain,
					AstBuildHelper.ReadLocalRA(locTo),
					value
				);
			}
			return writeValue;
		}
		private IAstNode Process_ValuesFilter(
			IReadWriteOperation op, 
			int operationId, 
			IAstNode result, 
			IAstRefOrValue value, 
			string fieldName,
			Delegate filterDelegate
		)
		{
			result = new AstComplexNode
			{
				nodes = new List<IAstNode>
					{
						new AstIf
						{
							condition = (IAstValue)AstBuildHelper.CallMethod(
								filterDelegate.GetType().GetMethod("Invoke"),
								new AstCastclassRef(
									(IAstRef)AstBuildHelper.ReadMemberRV(
										 GetStoredObject(operationId, typeof(IReadWriteOperation)),
										 typeof(IReadWriteOperation).GetProperty(fieldName)
									),
									filterDelegate.GetType()
								),
								new List<IAstStackItem>()
								{
									value,
									AstBuildHelper.ReadLocalRV(locState),
								}
							),
							trueBranch = new AstComplexNode{ nodes = new List<IAstNode> { result } }
						}
					}
			};
			return result;
		}
Exemplo n.º 26
0
        public static IAstNode WriteMembersChain(MemberInfo[] membersChain, IAstRefOrAddr targetObject, IAstRefOrValue value)
        {
            if (membersChain.Length == 1)
            {
                return WriteMember(membersChain[0], targetObject, value);
            }

            IAstRefOrAddr readTarget = targetObject;

            for (int i = 0; i < membersChain.Length - 1; ++i)
            {
                readTarget = ReadMemberRA(readTarget, membersChain[i]);
            }
            return WriteMember(membersChain[membersChain.Length - 1], readTarget, value);
        }
Exemplo n.º 27
0
 public AstWriteLocal(LocalBuilder loc, IAstRefOrValue value)
 {
     localIndex = loc.LocalIndex;
     localType  = loc.LocalType;
     this.value = value;
 }
Exemplo n.º 28
0
        public static IAstRefOrValue Convert(
            Type destinationType,
            Type sourceType,
            IAstRefOrValue sourceValue
            )
        {
            if (destinationType == sourceValue.itemType)
            {
                return(sourceValue);
            }

            if (destinationType == typeof(string))
            {
                return
                    (new AstCallMethodRef(
                         typeof(NativeConverter).GetMethod(
                             "ObjectToString",
                             BindingFlags.NonPublic | BindingFlags.Static
                             ),
                         null,
                         new List <IAstStackItem>()
                {
                    sourceValue
                }
                         ));
            }

            foreach (var m in typeof(Convert).GetMethods(BindingFlags.Static | BindingFlags.Public))
            {
                if (m.ReturnType == destinationType)
                {
                    var parameters = m.GetParameters();
                    if (parameters.Length == 1 && parameters[0].ParameterType == sourceType)
                    {
                        return
                            (AstBuildHelper.CallMethod(
                                 m,
                                 null,
                                 new List <IAstStackItem> {
                            sourceValue
                        }
                                 ));
                    }
                }
            }

            return(AstBuildHelper.CallMethod(
                       typeof(EMConvert).GetMethod(
                           "ChangeType",
                           new[]
            {
                typeof(object),
                typeof(Type),
                typeof(Type)
            }
                           ),
                       null,
                       new List <IAstStackItem> {
                sourceValue,
                new AstTypeof()
                {
                    type = sourceType
                },
                new AstTypeof()
                {
                    type = destinationType
                }
            }
                       ));
        }
Exemplo n.º 29
0
        public static IAstNode WriteMembersChain(MemberInfo[] membersChain, IAstRefOrAddr targetObject, IAstRefOrValue value)
        {
            if (membersChain.Length == 1)
            {
                return(WriteMember(membersChain[0], targetObject, value));
            }

            IAstRefOrAddr readTarget = targetObject;

            for (int i = 0; i < membersChain.Length - 1; ++i)
            {
                readTarget = ReadMemberRA(readTarget, membersChain[i]);
            }
            return(WriteMember(membersChain[membersChain.Length - 1], readTarget, value));
        }
Exemplo n.º 30
0
        public AstCastclass(IAstRefOrValue value, Type targetType)
		{
			_value = value;
			_targetType = targetType;
		}
Exemplo n.º 31
0
        public AstCastclassValue(IAstRefOrValue value, Type targetType): base(value, targetType)
		{
		}
Exemplo n.º 32
0
 public AstWriteArgument(int argumentIndex, Type argumentType, IAstRefOrValue value)
 {
     this.argumentIndex = argumentIndex;
     this.argumentType  = argumentType;
     this.value         = value;
 }
		private IAstRefOrValue ConvertMappingValue(
			ReadWriteSimple rwMapOp,
			int operationId,
			IAstRefOrValue sourceValue)
		{
			IAstRefOrValue convertedValue = sourceValue;
			if (rwMapOp.Converter != null)
			{
				convertedValue = AstBuildHelper.CallMethod(
					rwMapOp.Converter.GetType().GetMethod("Invoke"),
                    new AstCastclassRef(
					    (IAstRef)AstBuildHelper.ReadMemberRV(
					         GetStoredObject(operationId, typeof(ReadWriteSimple)),
					         typeof(ReadWriteSimple).GetProperty("Converter")
					    ),
                        rwMapOp.Converter.GetType()
                    ),
					new List<IAstStackItem>()
                    {
                        sourceValue,
						AstBuildHelper.ReadLocalRV(locState),
                    }
				);
			}
			else
			{
				if (rwMapOp.ShallowCopy && rwMapOp.Destination.MemberType == rwMapOp.Source.MemberType)
				{
					convertedValue = sourceValue;
				}
				else
				{
					var mi = staticConvertersManager.GetStaticConverter(rwMapOp.Source.MemberType, rwMapOp.Destination.MemberType);
					if (mi != null)
					{
						convertedValue = AstBuildHelper.CallMethod(
							mi,
							null,
							new List<IAstStackItem> { sourceValue }
						);
					}
					else
					{
						convertedValue = ConvertByMapper(rwMapOp);
					}
				}
			}

			return convertedValue;
		}
Exemplo n.º 34
0
        public static IAstNode WriteMember(MemberInfo memberInfo, IAstRefOrAddr targetObject, IAstRefOrValue value)
        {
            if (memberInfo.MemberType == MemberTypes.Field)
            {
                return new AstWriteField()
                           {
                               fieldInfo = (FieldInfo)memberInfo,
                               targetObject = targetObject,
                               value = value
                           };
            }
            else
            {
				return new AstWriteProperty(targetObject, value, (PropertyInfo)memberInfo);
            }
        }
Exemplo n.º 35
0
 public AstExprIsNull(IAstRefOrValue value)
 {
     this.value = value;
 }
Exemplo n.º 36
0
 public AstWriteLocal(LocalBuilder loc, IAstRefOrValue value)
 {
     localIndex = loc.LocalIndex;
     localType = loc.LocalType;
     this.value = value;
 }