示例#1
0
        /// <summary>
        /// Constructs a <c>FunctionCall</c> representation of creating a new array.
        /// </summary>
        /// <param name="elementType">type of array element</param>
        /// <param name="numElements">expression representing the array length</param>
        /// <param name="sample">sample instance of the created array</param>
        /// <returns></returns>
        public static FunctionCall NewArray(Type elementType, Expression numElements, Array sample)
        {
            ArrayParams    aparams;
            TypeDescriptor arrayType;

            if (sample != null)
            {
                long numElementsLong = sample.LongLength;
                aparams   = new ArrayParams(elementType, numElementsLong);
                arrayType = TypeDescriptor.GetTypeOf(sample);
            }
            else
            {
                aparams   = new ArrayParams(elementType);
                arrayType = elementType.MakeArrayType();
            }

            return(new FunctionCall()
            {
                Callee = MakeFun(
                    new IntrinsicFunction(IntrinsicFunction.EAction.NewArray, aparams),
                    arrayType),
                Arguments = new Expression[] { numElements },
                ResultType = arrayType,
                SetResultTypeClass = EResultTypeClass.ObjectReference
            });
        }
示例#2
0
        /// <summary>
        /// Constructs a <c>FunctionCall</c> representation of reading from a port.
        /// </summary>
        /// <param name="port">underlying accessed signal instance</param>
        public static FunctionCall ReadPort(SignalBase port)
        {
            TypeDescriptor type = TypeDescriptor.GetTypeOf(port.InitialValueObject);

            return(new FunctionCall()
            {
                Callee = MakeFun(
                    new IntrinsicFunction(IntrinsicFunction.EAction.ReadPort,
                                          new PortParams(port)), type),
                Arguments = new Expression[0],
                ResultType = type,
                SetResultTypeClass = EResultTypeClass.ObjectReference
            });
        }
示例#3
0
        private TypeDescriptor GetElement0Type()
        {
            if (_sample == null)
            {
                var etype = CILType.GetElementType();
                return(etype == null ? null : new TypeDescriptor(etype));
            }

            if (CILType.IsArray)
            {
                Array array = (Array)_sample;
                if (array.Length == 0)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }

                object sample = array.GetValue(new int[array.Rank]);
                if (sample == null)
                {
                    return(new TypeDescriptor(CILType.GetElementType()));
                }
                else
                {
                    return(new TypeDescriptor(sample));
                }
            }
            else if (CILType.IsByRef)
            {
                return(new TypeDescriptor(_sample));
            }
            else if (Constraints.Length > 0)
            {
                var indexers = CILType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                               .Where(p => p.GetIndexParameters().Length == Constraints.Length &&
                                      p.GetIndexParameters().All(ip => ip.ParameterType == typeof(int)));
                if (indexers.Any())
                {
                    var indexer = indexers.First();
                    if (Constraints.All(c => c.Size > 0))
                    {
                        object[] index = Constraints.Select(r => (object)r.FirstBound).ToArray();
                        try
                        {
                            var indexSample = indexer.GetValue(_sample, index);
                            return(TypeDescriptor.GetTypeOf(indexSample));
                        }
                        catch (Exception)
                        {
                            return(indexer.PropertyType);
                        }
                    }
                    else
                    {
                        return(indexer.PropertyType);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }