예제 #1
0
        protected override Type ResolveInternal(Context ctx, bool mustReturn)
        {
            ResolveSelf(ctx);

            if (_type != null)
            {
                CheckTypeInSafeMode(ctx, _type);
            }

            if (Expression != null && Expression.Resolve(ctx).IsArray&& MemberName == "Length")
            {
                return(typeof(int));
            }

            if (_field != null)
            {
                return(_field.FieldType);
            }

            if (_property != null)
            {
                return(_property.PropertyType);
            }

            return(_method.ReturnType.IsVoid()
                ? FunctionalHelper.CreateActionType(_method.ArgumentTypes)
                : FunctionalHelper.CreateFuncType(_method.ReturnType, _method.ArgumentTypes));
        }
예제 #2
0
        public void CreateFuncTypeTest()
        {
            Assert.AreEqual(
                typeof(Func <int, string, TimeSpan>),
                FunctionalHelper.CreateFuncType(typeof(TimeSpan), typeof(int), typeof(string))
                );

            Assert.AreEqual(typeof(Func <bool>), FunctionalHelper.CreateFuncType(typeof(bool)));

            Assert.Throws <LensCompilerException>(() => FunctionalHelper.CreateFuncType(typeof(int), new Type[20]));
        }
예제 #3
0
        /// <summary>
        /// Emits code for getting the method as a delegate instance.
        /// </summary>
        private void EmitMethod(Context ctx, ILGenerator gen)
        {
            if (RefArgumentRequired)
            {
                Error(CompilerMessages.MethodRef);
            }

            if (_isStatic)
            {
                gen.EmitNull();
            }

            var retType = _method.ReturnType;
            var type    = retType.IsVoid()
                ? FunctionalHelper.CreateActionType(_method.ArgumentTypes)
                : FunctionalHelper.CreateFuncType(retType, _method.ArgumentTypes);

            var ctor = ctx.ResolveConstructor(type, new[] { typeof(object), typeof(IntPtr) });

            gen.EmitLoadFunctionPointer(_method.MethodInfo);
            gen.EmitCreateObject(ctor.ConstructorInfo);
        }
예제 #4
0
        protected override Type ResolveInternal(Context ctx, bool mustReturn)
        {
            if (Identifier == "_")
            {
                Error(CompilerMessages.UnderscoreNameUsed);
            }

            // local variable
            var local = Local ?? ctx.Scope.FindLocal(Identifier);

            if (local != null)
            {
                // only local constants are cached
                // because mutable variables could be closured later on
                if (local.IsConstant && local.IsImmutable && ctx.Options.UnrollConstants)
                {
                    _localConstant = local;
                }

                return(local.Type);
            }

            // static function declared in the script
            try
            {
                var methods = ctx.MainType.ResolveMethodGroup(Identifier);
                if (methods.Length > 1)
                {
                    Error(CompilerMessages.FunctionInvocationAmbiguous, Identifier);
                }

                _method = methods[0];
                return(FunctionalHelper.CreateFuncType(_method.ReturnType, _method.GetArgumentTypes(ctx)));
            }
            catch (KeyNotFoundException)
            {
            }

            // algebraic type without a constructor
            var type = ctx.FindType(Identifier);

            if (type != null && type.Kind == TypeEntityKind.TypeLabel)
            {
                try
                {
                    type.ResolveConstructor(new Type[0]);
                    _type = type;
                    return(_type.TypeInfo);
                }
                catch (KeyNotFoundException)
                {
                }
            }

            // global property
            try
            {
                _property = ctx.ResolveGlobalProperty(Identifier);
                return(_property.PropertyType);
            }
            catch (KeyNotFoundException)
            {
                Error(CompilerMessages.IdentifierNotFound, Identifier);
            }

            return(typeof(UnitType));
        }