Beispiel #1
0
 private void adjustParamType()
 {
     checkVariable();
     if (_var != null)
     {
         bool bOK = false;
         _valType.SetDataType(new DataTypePointer(new TypePointer(typeof(Type))));
         bool isString = (typeof(JsString).Equals(_var.BaseClassType) || typeof(PhpString).Equals(_var.BaseClassType));
         _valType.SetConstructorTypeScope(_var.BaseClassType);
         DataTypePointer dp = _valType.ConstantValue.Value as DataTypePointer;
         if (dp != null)
         {
             bOK = _var.ClassType.IsAssignableFrom(dp);
         }
         if (!bOK)
         {
             _valType.SetValue(_var.ClassType);
             dp = _var.ClassType;
         }
         if (isString)
         {
             DataTypePointer st = new DataTypePointer(new TypePointer(typeof(string)));
             if (!st.IsAssignableFrom(_val.DataType))
             {
                 _val.SetDataType(st);
             }
         }
         else
         {
             if (!_var.ClassType.IsAssignableFrom(_val.DataType))
             {
                 _val.SetDataType(dp);
             }
         }
     }
 }
        public override CodeExpression ExportCode(IMethodCompile method)
        {
            MathNode.Trace("{0}.ExportCode", this.GetType().Name);
            if (_methodPointer == null)
            {
                MathNode.Trace("Warning: method pointer is null");
                return(null);
            }
            bool isWeb = false;

            if (_methodPointer.Owner != null && _methodPointer.Owner.RootPointer != null)
            {
                isWeb = _methodPointer.Owner.RootPointer.IsWebPage;
            }
            if (isWeb && _methodPointer.RunAt == EnumWebRunAt.Client)
            {
                CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("clientRequest"), "GetStringValue", new CodePrimitiveExpression(DataPassingCodeName));
                return(cmie);
            }
            if (method == null)
            {
                throw new DesignerException("Calling {0}.ExportCode with null method", this.GetType().Name);
            }
            if (method.MethodCode == null)
            {
                throw new DesignerException("Calling {0}.ExportCode with null method.MethodCode", this.GetType().Name);
            }
            CodeStatementCollection supprtStatements = method.MethodCode.Statements;
            int n = ChildNodeCount;

            CodeExpression[] ps;
            if (n > 0)
            {
                IList <IParameter> pList = _methodPointer.MethodPointed.MethodParameterTypes;;

                //_methodPointer.MethodPointed.
                ps = new CodeExpression[n];
                for (int i = 0; i < n; i++)
                {
                    this[i].CompileDataType = new RaisDataType(pList[i].ParameterLibType);
                    if (typeof(string).Equals(pList[i].ParameterLibType))
                    {
                        if (this[i].Parent != null)
                        {
                            MathNodeMethodPointer mmp = this[i].Parent as MathNodeMethodPointer;
                            if (mmp != null && mmp.Method != null && mmp.Method.Owner != null)
                            {
                                if (typeof(DateTime).Equals(mmp.Method.Owner.ObjectType))
                                {
                                    if (this[i].ActualCompiledType != null && typeof(CultureInfo).Equals(this[i].ActualCompiledType.LibType))
                                    {
                                        this[i].CompileDataType = this[i].ActualCompiledType;
                                    }
                                }
                            }
                        }
                    }
                    ps[i] = this[i].ExportCode(method);
                }
            }
            else
            {
                ps = new CodeExpression[] { };
            }
            _methodPointer.SetParameterExpressions(ps);
            CodeExpression ce = _methodPointer.GetReferenceCode(method, supprtStatements, true);

            if (_methodPointer.ReturnBaseType != null && this.CompileDataType != null && this.CompileDataType.Type != null)
            {
                DataTypePointer     targetType = new DataTypePointer(this.CompileDataType.Type);
                DataTypePointer     sourceType = new DataTypePointer(_methodPointer.ReturnBaseType);
                IGenericTypePointer igp        = _methodPointer as IGenericTypePointer;
                if (igp != null)
                {
                    if (targetType.IsGenericType || targetType.IsGenericParameter)
                    {
                        DataTypePointer dtp = igp.GetConcreteType(targetType.BaseClassType);
                        if (dtp != null)
                        {
                            targetType = dtp;
                        }
                    }
                    if (sourceType.IsGenericType || sourceType.IsGenericParameter)
                    {
                        DataTypePointer dtp = igp.GetConcreteType(sourceType.BaseClassType);
                        if (dtp != null)
                        {
                            sourceType = dtp;
                        }
                    }
                }
                if (!targetType.IsAssignableFrom(sourceType))
                {
                    ce = CompilerUtil.GetTypeConversion(targetType, ce, sourceType, supprtStatements);
                }
            }
            return(ce);
        }
        public static CodeExpression GetTypeConversion(DataTypePointer targetType, CodeExpression data, DataTypePointer dataType, CodeStatementCollection statements)
        {
            if (targetType.IsLibType && typeof(Type).Equals(targetType.BaseClassType))
            {
                if (data is CodeTypeOfExpression)
                {
                    return(data);
                }
            }
            if (targetType.IsAssignableFrom(dataType))
            {
                return(data);
            }
            if (typeof(string).Equals(targetType.BaseClassType))
            {
                if (typeof(JsString).Equals(dataType.BaseClassType))
                {
                    return(data);
                }
                if (typeof(PhpString).Equals(dataType.BaseClassType))
                {
                    return(data);
                }
                if (dataType.IsValueType)
                {
                    return(new CodeMethodInvokeExpression(data, "ToString"));
                }
                else
                {
                    string         vn = "l" + Guid.NewGuid().GetHashCode().ToString("x");
                    string         vs = "l" + Guid.NewGuid().GetHashCode().ToString("x");
                    CodeExpression val;
                    if (data is CodePropertyReferenceExpression || data is CodeFieldReferenceExpression || data is CodeVariableReferenceExpression)
                    {
                        val = data;
                    }
                    else
                    {
                        CodeVariableDeclarationStatement vds = new CodeVariableDeclarationStatement(dataType.GetCodeTypeReference(), vn, data);
                        statements.Add(vds);
                        val = new CodeVariableReferenceExpression(vn);
                    }
                    CodeVariableDeclarationStatement vds2 = new CodeVariableDeclarationStatement(typeof(string), vs);
                    statements.Add(vds2);
                    CodeVariableReferenceExpression re  = new CodeVariableReferenceExpression(vs);
                    CodeConditionStatement          ccs = new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                            val, CodeBinaryOperatorType.IdentityInequality,
                            new CodePrimitiveExpression(null)),
                        new CodeStatement[] { new CodeAssignStatement(re, new CodeMethodInvokeExpression(val, "ToString")) },
                        new CodeStatement[] { new CodeAssignStatement(re, new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(typeof(string)), "Empty")) });
                    statements.Add(ccs);
                    return(re);
                }
            }
            if (typeof(bool).Equals(targetType.BaseClassType))
            {
                return(ConvertToBool(dataType.BaseClassType, data));
            }
            if ((targetType.BaseClassType.IsPrimitive || targetType.BaseClassType.GetInterface("IConvertible") != null) &&
                (dataType.BaseClassType.IsPrimitive || dataType.BaseClassType.GetInterface("IConvertible") != null))
            {
                CodeExpression ex = VPLUtil.ConvertByType(targetType.BaseClassType, data);
                if (ex != null)
                {
                    return(ex);
                }
            }
            if (targetType.BaseClassType.IsArray)
            {
                if (!dataType.BaseClassType.IsArray && !typeof(Array).Equals(dataType.BaseClassType))
                {
                    Type elementType;
                    if (targetType.BaseClassType.HasElementType)
                    {
                        elementType = targetType.BaseClassType.GetElementType();
                    }
                    else
                    {
                        elementType = typeof(object);
                    }
                    CodeArrayCreateExpression ae = new CodeArrayCreateExpression(elementType, new CodeExpression[] { GetTypeConversion(new DataTypePointer(elementType), data, dataType, statements) });
                    return(ae);
                }
            }
            TypeConverter converter = TypeDescriptor.GetConverter(targetType);

            if (converter != null)
            {
                if (converter.CanConvertFrom(dataType.BaseClassType))
                {
                    string vn = "c" + Guid.NewGuid().GetHashCode().ToString("x");
                    CodeVariableDeclarationStatement vds = new CodeVariableDeclarationStatement(typeof(TypeConverter), vn,
                                                                                                new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter", new CodeTypeOfExpression(targetType.GetCodeTypeReference())));
                    statements.Add(vds);
                    string vn2 = "c" + Guid.NewGuid().GetHashCode().ToString("x");
                    CodeVariableDeclarationStatement vds2 = new CodeVariableDeclarationStatement(typeof(TypeConverterNullable), vn2,
                                                                                                 new CodeObjectCreateExpression(typeof(TypeConverterNullable), new CodeVariableReferenceExpression(vn)));
                    statements.Add(vds2);
                    return(new CodeCastExpression(targetType.GetCodeTypeReference(),
                                                  new CodeMethodInvokeExpression(
                                                      new CodeVariableReferenceExpression(vn2), "ConvertFrom", data)));
                }
            }
            converter = TypeDescriptor.GetConverter(dataType);
            if (converter != null)
            {
                if (converter.CanConvertTo(targetType.BaseClassType))
                {
                    string vn = "c" + Guid.NewGuid().GetHashCode().ToString("x");
                    CodeVariableDeclarationStatement vds = new CodeVariableDeclarationStatement(typeof(TypeConverter), vn,
                                                                                                new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter", new CodeTypeOfExpression(dataType.GetCodeTypeReference())));
                    statements.Add(vds);
                    return(new CodeMethodInvokeExpression(
                               new CodeVariableReferenceExpression(vn), "CanConvertTo", data, new CodeTypeOfExpression(targetType.GetCodeTypeReference())));
                }
            }
            TypeCode tc = Type.GetTypeCode(targetType.BaseClassType);

            switch (tc)
            {
            case TypeCode.Boolean:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Byte:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToByte", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Char:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToChar", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.DateTime:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToDateTime", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Decimal:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToDecimal", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Double:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToDouble", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Int16:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToInt16", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Int32:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToInt32", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Int64:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToInt64", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.SByte:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToSByte", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.Single:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToSingle", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.String:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToString", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.UInt16:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToUInt16", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.UInt32:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToUInt32", VPLUtil.GetCoreExpressionFromCast(data)));

            case TypeCode.UInt64:
                return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(Convert)), "ToUInt64", VPLUtil.GetCoreExpressionFromCast(data)));
            }
            ConstructorInfo cif = targetType.BaseClassType.GetConstructor(new Type[] { dataType.BaseClassType });

            if (cif != null)
            {
                return(new CodeObjectCreateExpression(targetType.BaseClassType, data));
            }
            return(new CodeCastExpression(targetType.GetCodeTypeReference(), VPLUtil.GetCoreExpressionFromCast(data)));
        }
Beispiel #4
0
        public override CodeExpression ExportCode(IMethodCompile method)
        {
            CodeExpression ce;

            if (_valuePointer != null)
            {
                bool isWeb = false;
                if (_valuePointer.RootPointer != null)
                {
                    isWeb = _valuePointer.RootPointer.IsWebPage;
                }
                if (!IsField() && isWeb && this.IsWebClientValue())
                {
                    //this is a client property, use clientRequest.GetStringValue(DataPassingCodeName)
                    CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
                        new CodeVariableReferenceExpression("clientRequest"), "GetStringValue", new CodePrimitiveExpression(DataPassingCodeName));
                    ce = cmie;
                }
                else
                {
                    ce = _valuePointer.GetReferenceCode(method, method.MethodCode.Statements, _forValue);
                }
                ActualCompiledType = new MathExp.RaisTypes.RaisDataType(_valuePointer.ObjectType);
                if (CompileDataType != null)
                {
                    if (CompileDataType.Type != null && _valuePointer.ObjectType != null)
                    {
                        DataTypePointer     targetType = new DataTypePointer(CompileDataType.Type);
                        DataTypePointer     sourceType = new DataTypePointer(_valuePointer.ObjectType);
                        IGenericTypePointer igp        = _valuePointer as IGenericTypePointer;
                        if (igp != null)
                        {
                            if (targetType.IsGenericType || targetType.IsGenericParameter)
                            {
                                DataTypePointer dtp = igp.GetConcreteType(targetType.BaseClassType);
                                if (dtp != null)
                                {
                                    targetType = dtp;
                                }
                            }
                            if (sourceType.IsGenericType || sourceType.IsGenericParameter)
                            {
                                DataTypePointer dtp = igp.GetConcreteType(sourceType.BaseClassType);
                                if (dtp != null)
                                {
                                    sourceType = dtp;
                                }
                            }
                        }
                        if (!targetType.IsAssignableFrom(sourceType))
                        {
                            ce = CompilerUtil.GetTypeConversion(targetType, ce, sourceType, method.MethodCode.Statements);
                            ActualCompiledType = new MathExp.RaisTypes.RaisDataType(targetType.BaseClassType);
                        }
                    }
                }
            }
            else
            {
                ce = new CodeArgumentReferenceExpression(CodeName);
            }
            return(ce);
        }