Esempio n. 1
0
        protected override void compile(Context ctx, bool mustReturn)
        {
            resolve(ctx);

            var gen = ctx.CurrentILGenerator;

            var destType = m_Field != null ? m_Field.FieldType : m_Property.PropertyType;
            var valType = Value.GetExpressionType(ctx);

            ctx.CheckTypedExpression(Value, valType, true);

            if(!destType.IsExtendablyAssignableFrom(valType))
                Error(CompilerMessages.ImplicitCastImpossible, valType, destType);

            if (!m_IsStatic)
            {
                var exprType = Expression.GetExpressionType(ctx);
                if (Expression is IPointerProvider && exprType.IsStruct())
                    (Expression as IPointerProvider).PointerRequired = true;

                Expression.Compile(ctx, true);
            }

            Expr.Cast(Value, destType).Compile(ctx, true);

            if(m_Field != null)
                gen.EmitSaveField(m_Field.FieldInfo);
            else
                gen.EmitCall(m_Property.Setter, true);
        }
Esempio n. 2
0
        protected override Type resolve(Context ctx, bool mustReturn)
        {
            ctx.CheckTypedExpression(Expression, allowNull: true);

            var stmtTypes = new List<Type>(MatchStatements.Count);
            Type commonType = null;
            foreach (var stmt in MatchStatements)
            {
                stmt.ParentNode = this;
                stmtTypes.Add(stmt.Resolve(ctx, mustReturn));

                foreach (var rule in stmt.MatchRules)
                {
                    var nameRule = rule as MatchNameRule;
                    // detect catch-all expression for a type
                    if (nameRule != null && stmt.Condition == null)
                    {
                        var nameType = nameRule.Type == null ? typeof (object) : ctx.ResolveType(nameRule.Type);
                        if (commonType != null && commonType.IsExtendablyAssignableFrom(nameType))
                            error(CompilerMessages.PatternUnreachable);

                        commonType = nameType;
                    }
                }
            }

            return stmtTypes.Any(x => x.IsVoid())
                ? typeof(UnitType)
                : stmtTypes.ToArray().GetMostCommonType();
        }
Esempio n. 3
0
        protected override Type resolve(Context ctx, bool mustReturn)
        {
            if (Identifier == "_")
                error(CompilerMessages.UnderscoreNameUsed);

            var nameInfo = Local ?? ctx.Scope.FindLocal(Identifier);
            if (nameInfo != null)
            {
                if (nameInfo.IsImmutable && !IsInitialization)
                    error(CompilerMessages.IdentifierIsConstant, Identifier);
            }
            else
            {
                try
                {
                    _Property = ctx.ResolveGlobalProperty(Identifier);

                    if (!_Property.HasSetter)
                        error(CompilerMessages.GlobalPropertyNoSetter, Identifier);
                }
                catch (KeyNotFoundException)
                {
                    error(CompilerMessages.VariableNotFound, Identifier);
                }
            }

            var destType = nameInfo != null ? nameInfo.Type : _Property.PropertyType;
            ensureLambdaInferred(ctx, Value, destType);

            var exprType = Value.Resolve(ctx);
            ctx.CheckTypedExpression(Value, exprType, true);

            if (!destType.IsExtendablyAssignableFrom(exprType))
            {
                error(
                    nameInfo != null ? CompilerMessages.IdentifierTypeMismatch : CompilerMessages.GlobalPropertyTypeMismatch,
                    exprType,
                    destType
                );
            }

            return base.resolve(ctx, mustReturn);
        }
Esempio n. 4
0
        public override void ProcessClosures(Context ctx)
        {
            base.ProcessClosures(ctx);

            var type = Value != null
                ? Value.GetExpressionType(ctx)
                : ctx.ResolveType(Type);
            ctx.CheckTypedExpression(Value, type);

            if(LocalName != null)
                return;

            if(Name == "_")
                Error(CompilerMessages.UnderscoreName);

            try
            {
                var name = ctx.CurrentScope.DeclareName(Name, type, IsImmutable);
                if (Value != null && Value.IsConstant && ctx.Options.UnrollConstants)
                {
                    name.IsConstant = true;
                    name.ConstantValue = Value.ConstantValue;
                }
            }
            catch (LensCompilerException ex)
            {
                ex.BindToLocation(this);
                throw;
            }
        }
Esempio n. 5
0
        protected override void compile(Context ctx, bool mustReturn)
        {
            var gen = ctx.CurrentILGenerator;

            var exprType = Value.GetExpressionType(ctx);
            ctx.CheckTypedExpression(Value, exprType, true);

            var nameInfo = LocalName ?? ctx.CurrentScope.FindName(Identifier);
            if (nameInfo != null)
            {
                if (nameInfo.IsImmutable && !IsInitialization)
                    Error(CompilerMessages.IdentifierIsConstant, Identifier);

                if (!nameInfo.Type.IsExtendablyAssignableFrom(exprType))
                    Error(CompilerMessages.IdentifierTypeMismatch, exprType, nameInfo.Type);

                if (nameInfo.IsClosured)
                {
                    if (nameInfo.ClosureDistance == 0)
                        assignClosuredLocal(ctx, nameInfo);
                    else
                        assignClosuredRemote(ctx, nameInfo);
                }
                else
                {
                    assignLocal(ctx, nameInfo);
                }

                return;
            }

            try
            {
                var pty = ctx.ResolveGlobalProperty(Identifier);

                if(!pty.HasSetter)
                    Error(CompilerMessages.GlobalPropertyNoSetter, Identifier);

                var type = pty.PropertyType;
                if(!type.IsExtendablyAssignableFrom(exprType))
                    Error(CompilerMessages.GlobalPropertyTypeMismatch, exprType, type);

                var cast = Expr.Cast(Value, type);
                if (pty.SetterMethod != null)
                {
                    cast.Compile(ctx, true);
                    gen.EmitCall(pty.SetterMethod.MethodInfo);
                }
                else
                {
                    var method = typeof (GlobalPropertyHelper).GetMethod("Set").MakeGenericMethod(type);

                    gen.EmitConstant(ctx.ContextId);
                    gen.EmitConstant(pty.PropertyId);
                    Expr.Cast(Value, type).Compile(ctx, true);
                    gen.EmitCall(method);
                }
            }
            catch (KeyNotFoundException)
            {
                Error(CompilerMessages.VariableNotFound, Identifier);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Attempts to resolve member reference to a field or a property.
        /// </summary>
        private void resolveSelf(Context ctx)
        {
            var type = StaticType != null
                ? ctx.ResolveType(StaticType)
                : Expression.Resolve(ctx);

            checkTypeInSafeMode(ctx, type);

            // check for field
            try
            {
                _Field = ctx.ResolveField(type, MemberName);
                _IsStatic = _Field.IsStatic;
                if (Expression == null && !_IsStatic)
                    error(CompilerMessages.DynamicMemberFromStaticContext, type, MemberName);
            }
            catch (KeyNotFoundException)
            {
                try
                {
                    _Property = ctx.ResolveProperty(type, MemberName);
                    if (!_Property.CanSet)
                        error(CompilerMessages.PropertyNoSetter, MemberName, type);

                    _IsStatic = _Property.IsStatic;
                    if (Expression == null && !_IsStatic)
                        error(CompilerMessages.DynamicMemberFromStaticContext, type, MemberName);
                }
                catch (KeyNotFoundException)
                {
                    error(CompilerMessages.TypeSettableIdentifierNotFound, type, MemberName);
                }
            }

            var destType = _Field != null ? _Field.FieldType : _Property.PropertyType;
            ensureLambdaInferred(ctx, Value, destType);

            var valType = Value.Resolve(ctx);
            ctx.CheckTypedExpression(Value, valType, true);

            if (!destType.IsExtendablyAssignableFrom(valType))
                error(CompilerMessages.ImplicitCastImpossible, valType, destType);
        }
Esempio n. 7
0
        protected override Type resolve(Context ctx, bool mustReturn)
        {
            var type = Value != null
                ? Value.Resolve(ctx)
                : ctx.ResolveType(Type);

            ctx.CheckTypedExpression(Value, type);

            if (Local == null)
            {
                if (Name == "_")
                    error(CompilerMessages.UnderscoreName);

                try
                {
                    var name = ctx.Scope.DeclareLocal(Name, type, IsImmutable);
                    if (Value != null && Value.IsConstant && ctx.Options.UnrollConstants)
                    {
                        name.IsConstant = true;
                        name.ConstantValue = Value.ConstantValue;
                    }
                }
                catch (LensCompilerException ex)
                {
                    ex.BindToLocation(this);
                    throw;
                }
            }

            return base.resolve(ctx, mustReturn);
        }