예제 #1
0
        public override CodeExpression ExportCode(IMethodCompile method)        //)
        {
            CodeStatementCollection supprtStatements = method.MethodCode.Statements;

            if (this.UseDefaultValue)
            {
                if (_default == null)
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: Use default case 0:null");
                    return(ValueTypeUtil.GetDefaultCodeByType(this.DataType.Type));
                }
                else
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: Use default case 1:{0}", _default);
                    return(ObjectCreationCodeGen.ObjectCreationCode(_default));
                }
            }
            else
            {
                if (this.InPort != null && this.InPort.LinkedPortID != 0)
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: call linked item");
                    IMathExpression rootContainer = this.root.RootContainer;
                    if (rootContainer == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("Parameter {0} not associated with a root container", this.TraceInfo));
                    }
                    MathExpItem LinkedItem = rootContainer.GetItemByID(this.InPort.LinkedPortID);
                    if (LinkedItem == null)
                    {
                        throw new MathException(string.Format("Linked Port ID {0} from ({1}) does not match an item", InPort.LinkedPortID, this.TraceInfo));
                    }
                    CodeExpression ce = LinkedItem.ReturnCodeExpression(method);
                    return(RaisDataType.GetConversionCode(LinkedItem.MathExpression.DataType, ce, this.DataType, supprtStatements));
                }
                //
                MathNode.Trace("MathNodeParameter.ExportCode: call MathNodeVariable.ExportCode");
                return(base.ExportCode(method));
            }
        }
 public override CodeExpression ExportCode(IMethodCompile method)
 {
     MathNode.Trace("{0}.ExportCode: reference to [{1}]", this.GetType().Name, _value);
     return(ObjectCreationCodeGen.ObjectCreationCode(_value));
 }
예제 #3
0
        public static CodeExpression GetDefaultCodeByType(Type t)
        {
            if (t.Equals(typeof(void)))
            {
                return(new CodePrimitiveExpression(null));
            }

            TypeCode tc = Type.GetTypeCode(t);

            switch (tc)
            {
            case TypeCode.Boolean:
                return(new CodePrimitiveExpression(false));

            case TypeCode.Byte:
                return(new CodePrimitiveExpression(default(byte)));

            case TypeCode.Char:
                return(new CodePrimitiveExpression(default(char)));

            case TypeCode.DateTime:
                return(ObjectCreationCodeGen.ObjectCreationCode(default(DateTime)));

            case TypeCode.Decimal:
                return(new CodePrimitiveExpression((decimal)0));

            case TypeCode.Double:
                return(new CodePrimitiveExpression(default(double)));

            case TypeCode.Int16:
                return(new CodePrimitiveExpression(default(Int16)));

            case TypeCode.Int32:
                if (t.IsEnum)
                {
                    Array a = Enum.GetValues(t);
                    return(new CodeFieldReferenceExpression(
                               new CodeTypeReferenceExpression(t), a.GetValue(0).ToString()));
                }
                return(new CodePrimitiveExpression(default(Int32)));

            case TypeCode.Int64:
                return(new CodePrimitiveExpression(default(Int64)));

            case TypeCode.SByte:
                return(new CodePrimitiveExpression(default(sbyte)));

            case TypeCode.Single:
                return(new CodePrimitiveExpression(default(Single)));

            case TypeCode.String:
                return(new CodePrimitiveExpression(""));

            case TypeCode.UInt16:
                return(new CodePrimitiveExpression(default(UInt16)));

            case TypeCode.UInt32:
                return(new CodePrimitiveExpression(default(UInt32)));

            case TypeCode.UInt64:
                return(new CodePrimitiveExpression(default(UInt64)));

            case TypeCode.Object:
                return(ObjectCreationCodeGen.ObjectCreationCode(VPLUtil.GetDefaultValue(t)));

            case TypeCode.DBNull:
                return(new CodePrimitiveExpression(null));

            case TypeCode.Empty:
                return(new CodePrimitiveExpression(null));

            default:
                return(new CodeDefaultValueExpression(new CodeTypeReference(t)));
            }
        }
예제 #4
0
        /// <summary>Generates a property setter or event setter statement</summary>
        /// <param name="cx"><see cref="CodeExpression"/> for which property has to be set</param>
        /// <param name="objType"><see cref="Type"/> of <see cref="object"/>, <paramref name="cx"/> is
        /// representing</param>
        /// <param name="x">Property Name</param>
        /// <param name="y">Property Value</param>
        private static CodeStatement setPropEvent(CodeExpression cx, Type objType, string x, string y)
        {
            CodeStatement as1    = null;
            MemberTypes   result = ReflectionHelper.GetMember(x, objType);

            if (result == MemberTypes.Property)
            {
                // A Property Encountered.
                CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression(cx, x);
                Type t = ReflectionHelper.GetPropertyType(x, objType);
                if (t.IsPrimitive || t.Equals(typeof(System.String)))
                {
                    // A Primitive or string Property is Encountered
                    object o = ReflectionHelper.ConvertFrom(y, t);
                    if (t.Equals(typeof(IntPtr)))
                    {
                        IntPtr val = (IntPtr)o;
                        if (IntPtr.Size == 4)
                        {
                            as1 = new CodeAssignStatement(prop, new CodeObjectCreateExpression(typeof(IntPtr), new CodePrimitiveExpression(val.ToInt32())));
                        }
                        else
                        {
                            as1 = new CodeAssignStatement(prop, new CodeObjectCreateExpression(typeof(IntPtr), new CodePrimitiveExpression(val.ToInt64())));
                        }
                    }
                    else if (t.Equals(typeof(UIntPtr)))
                    {
                        UIntPtr val = (UIntPtr)o;
                        if (IntPtr.Size == 4)
                        {
                            as1 = new CodeAssignStatement(prop, new CodeObjectCreateExpression(typeof(UIntPtr), new CodePrimitiveExpression(val.ToUInt32())));
                        }
                        else
                        {
                            as1 = new CodeAssignStatement(prop, new CodeObjectCreateExpression(typeof(UIntPtr), new CodePrimitiveExpression(val.ToUInt64())));
                        }
                    }
                    else
                    {
                        as1 = new CodeAssignStatement(prop, new CodePrimitiveExpression(o));
                    }
                }
                else if (t.IsEnum)
                {
                    // An Enum Property Encountered
                    CodeTypeReferenceExpression  xpType  = new CodeTypeReferenceExpression(t);
                    CodeFieldReferenceExpression xpField = new CodeFieldReferenceExpression(xpType, y);
                    as1 = new CodeAssignStatement(prop, xpField);
                }
                else
                {
                    InstanceDescriptor idc    = ReflectionHelper.GetInstance(y, t);
                    CodeExpression     xpRite = null;
                    if (idc.Arguments == null || idc.Arguments.Count <= 0)
                    {
                        xpRite = new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(t), idc.MemberInfo.Name);
                    }
                    else
                    {
                        CodeExpression[] cpargs = new CodeExpression[idc.Arguments.Count];
                        int i = 0;
                        foreach (object o in idc.Arguments)
                        {
                            cpargs[i++] = ObjectCreationCodeGen.ObjectCreationCode(o);
                        }
                        xpRite = new CodeObjectCreateExpression(t, cpargs);
                    }
                    as1 = new CodeAssignStatement(prop, xpRite);
                }
            }
            else if (result == MemberTypes.Event)
            {
                Type typeEvent = ReflectionHelper.GetEventType(x, objType);
                CodeDelegateCreateExpression xpDele = new CodeDelegateCreateExpression(
                    new CodeTypeReference(typeEvent), new CodeThisReferenceExpression(), y);
                as1 = new CodeAttachEventStatement(cx, x, xpDele);
            }
            return(as1);
        }
예제 #5
0
        /// <summary>
        /// multi-thread support:
        ///
        /// thread_{branch id}(object data)
        /// {
        ///     object[] ps = (object[])data;
        ///     type1 var1 = ps[0];
        ///     type2 var2 = ps[1];
        ///     ...
        ///     {branch code}
        /// }
        /// </summary>
        /// <param name="compiler"></param>
        /// <param name="method"></param>
        /// <returns>true:all barnches of the main thread have method return or goto; false: at least one branch of the main thread does not have method return or goto</returns>
        public bool ExportCode(ILimnorCodeCompiler compiler, CodeMemberMethod method, CodeStatementCollection statements)
        {
            bool bRet = false;

            MathNode.Trace("BranchList.ExportCode. Method {0}, action blocks {1}================", method.Name, this.Count);
            //create code threads
            List <ActionBranch> independentThreads;

            if (_independentThreads == null)
            {
                independentThreads = FindoutActionThreads(true);
            }
            else
            {
                independentThreads = _independentThreads;
            }
            IsMultiThreads = (independentThreads.Count > 1);
            //the case of Count == 0 (empty method) is handled by MethodClass.ExportCode
            if (independentThreads.Count > 0)
            {
                int k0 = 0;                // main thread index
                this.ActionThreads.Clear();
                foreach (ActionBranch a in independentThreads)
                {
                    _threads.Add(a.BranchId, a);
                }
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (independentThreads[k].IsMainThread)
                    {
                        k0 = k;
                        break;
                    }
                }
                if (k0 == 0)
                {
                    independentThreads[0].IsMainThread = true;
                }
                this.MainThreadId = independentThreads[k0].BranchId;
                List <UInt32> usedBranches = new List <uint>();
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    independentThreads[k].IsMainThread = (k == k0);
                    independentThreads[k].SetIsMainThreadForSubBranches(usedBranches);
                }
                //generate additional threads
                CodeExpression[] obs = null;
                CodeExpression[] ps  = null;
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (k != k0)
                    {
                        ActionBranch ab = independentThreads[k];
                        ab.IsMainThread = false;
                        //method for this additional thread
                        CodeMemberMethod mt = new CodeMemberMethod();
                        mt.Name = "thread_" + ab.BranchId.ToString("x");
                        compiler.TypeDeclaration.Members.Add(mt);
                        mt.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data"));
                        //use an object[] to hold the method parameters which is passed to the thread method via the data parameter
                        string pas = "******" + Guid.NewGuid().GetHashCode().ToString("x");
                        mt.Statements.Add(new CodeVariableDeclarationStatement(typeof(object[]), pas,
                                                                               new CodeCastExpression(typeof(object[]), new CodeArgumentReferenceExpression("data"))));
                        //create variables named after the method parameters and assign values to them
                        //so that the actions within the method can use them as if they are method parameters.
                        for (int i = 0; i < _method.ParameterCount; i++)
                        {
                            mt.Statements.Add(new CodeVariableDeclarationStatement(_method.Parameters[i].TypeString,
                                                                                   _method.Parameters[i].Name,
                                                                                   new CodeCastExpression(_method.Parameters[i].TypeString,
                                                                                                          new CodeArrayIndexerExpression(new CodeVariableReferenceExpression(pas),
                                                                                                                                         new CodePrimitiveExpression(i)))));
                        }
                        //method contents: a single thread
                        ab.ExportThreadCode(compiler, mt, mt.Statements);
                        //
                        if (compiler.Debug)
                        {
                            mt.Statements.Add(new CodeExpressionStatement(
                                                  new CodeMethodInvokeExpression(
                                                      new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), LimnorDebugger.Debugger), "ThreadFinished"
                                                      //,new CodePrimitiveExpression(ab.BranchId)
                                                      )
                                                  )
                                              );
                        }
                        //code to launch this thread
                        if (obs == null)
                        {
                            //create an object[] to hold all method parameters to be passed into the thread method
                            obs = new CodeExpression[_method.ParameterCount];
                            for (int i = 0; i < _method.ParameterCount; i++)
                            {
                                obs[i] = new CodeArgumentReferenceExpression(_method.Parameters[i].Name);
                            }
                        }
                        //parameters for launch the thread
                        ps  = new CodeExpression[2];
                        pas = "******" + Guid.NewGuid().GetHashCode().ToString("x");
                        statements.Add(
                            new CodeVariableDeclarationStatement(typeof(object[]), pas, new CodeArrayCreateExpression(typeof(object), obs)));
                        //delegate to the thread method
                        ps[0] = new CodeDelegateCreateExpression(new CodeTypeReference(typeof(System.Threading.WaitCallback)),
                                                                 new CodeThisReferenceExpression(), mt.Name);
                        //parameter to the thread method
                        ps[1] = new CodeVariableReferenceExpression(pas);
                        //queue the thread
                        statements.Add(new CodeMethodInvokeExpression(
                                           new CodeTypeReferenceExpression(typeof(System.Threading.ThreadPool)),
                                           "QueueUserWorkItem", ps));
                    }
                }
                //main thread code
                bRet = independentThreads[k0].ExportThreadCode(compiler, method, statements);
                //
                if (_method == null)
                {
                    MathNode.LogError("method is null");
                }
                else                 //
                {
                    if (_method.ReturnValue != null)
                    {
                        if (!typeof(void).Equals(_method.ReturnValue.ObjectType))
                        {
                            //check whether all branches ends with a method return statement
                            if (!independentThreads[k0].AllBranchesEndWithMethodReturnStatement())
                            {
                                CodeExpression pe;
                                if (_method.ReturnValue.ObjectType.IsArray)
                                {
                                    pe = new CodePrimitiveExpression(null);
                                }
                                else
                                {
                                    pe = ObjectCreationCodeGen.ObjectCreationCode(VPLUtil.GetDefaultValue(_method.ReturnValue.ObjectType));
                                }
                                statements.Add(new CodeMethodReturnStatement(pe));
                            }
                        }
                    }
                }
            }
            MathNode.Trace("End of BranchList.ExportCode. Method {0}================", method.Name);
            return(bRet);
        }
예제 #6
0
        /// <summary>
        /// generate reference code
        /// </summary>
        /// <param name="methodOwnerXPath"></param>
        /// <returns></returns>
        public CodeExpression ExportCode(string methodOwnerXPath)
        {
            MathNode.Trace("ObjectRef.ExportCode for {0}", this);
            switch (Type)
            {
            case ObjectRefType.Argv:
            case ObjectRefType.EventSender:
                return(new CodeArgumentReferenceExpression(_name));

            case ObjectRefType.This:
                return(new CodeThisReferenceExpression());

            case ObjectRefType.Type:
                return(new CodeTypeReferenceExpression(DataType));

            case ObjectRefType.Field:
            case ObjectRefType.Property:
                if (Owner == null)
                {
                    throw new MathException("property reference does not have an owner");
                }
                return(new CodePropertyReferenceExpression(Owner.ExportCode(methodOwnerXPath), _name));

            case ObjectRefType.Const:
                if (_value == null)
                {
                    return(new CodePrimitiveExpression(null));
                }
                else
                {
                    return(ObjectCreationCodeGen.ObjectCreationCode(_value.DataValue));
                }

            case ObjectRefType.XPath:
                if (_xpathNode == null)
                {
                    throw new MathException("ObjectRef.ExportCode is called for XPath with null xpath Node");
                }
                if (string.IsNullOrEmpty(_name))
                {
                    throw new MathException("ObjectRef.ExportCode is called for XPath with null xpath value");
                }
                CodeExpression codeExp = new CodeThisReferenceExpression();
                if (methodOwnerXPath != _name)
                {
                    //object for the reference
                    //_name is the xpath
                    XmlNode thisNode = _xpathNode.OwnerDocument.DocumentElement.SelectSingleNode(_name);
                    if (thisNode == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("ObjectRef.ExportCode is called for XPath with invalid xpath value: {0}", _name));
                    }
                    //
                    if (thisNode.Name == XmlSerialization.RAIS_R)
                    {
                        if (XmlSerialization.IsStaticComponent(thisNode))
                        {
                            codeExp = new CodeTypeReferenceExpression(TypeString);
                        }
                        else
                        {
                            //find all names for each part R|A[@ID='?']R|A[@ID='?']...
                            string xpath0 = _name;
                            string name   = XmlSerialization.GetName(thisNode);
                            while (xpath0.Length > 0 && xpath0 != methodOwnerXPath)
                            {
                                int pos = xpath0.LastIndexOf('/');
                                if (pos > 0)
                                {
                                    xpath0 = xpath0.Substring(0, pos);
                                    if (xpath0 != methodOwnerXPath)
                                    {
                                        XmlNode n0 = XmlSerialization.GetXmlNodeByPath(thisNode.OwnerDocument, xpath0);
                                        if (n0 == null)
                                        {
                                            throw new MathException(XmlSerialization.FormatString("Invalid path {0}", xpath0));
                                        }
                                        name = XmlSerialization.GetName(n0) + "." + name;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            codeExp = new CodeFieldReferenceExpression(codeExp, name);
                        }
                    }
                }
                else
                {
                    XmlNode thisNode = _xpathNode.OwnerDocument.DocumentElement.SelectSingleNode(_name);
                    if (thisNode == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("ObjectRef.ExportCode is called for XPath with invalid xpath value: {0}", _name));
                    }
                    //use the parent node to generate code
                    if (thisNode.Name == XmlSerialization.RAIS_R)
                    {
                        if (XmlSerialization.IsStaticComponent(thisNode))
                        {
                            codeExp = new CodeTypeReferenceExpression(TypeString);
                        }
                    }
                }
                return(codeExp);
            }
            return(new CodeThisReferenceExpression());
        }