Exemple #1
0
        protected virtual Expression CompileDereference(Compiler compiler, Frame frame, Expression left, Parse.BinaryExpression expression, System.Type typeHint)
        {
            left = compiler.MaterializeReference(left);

            var local = compiler.AddFrame(frame, expression);
            var memberType = left.Type.GenericTypeArguments[0];
            var parameters = new List<ParameterExpression>();

            var valueParam = compiler.CreateValueParam(expression, local, left, memberType);
            parameters.Add(valueParam);

            var indexParam = compiler.CreateIndexParam(expression, local);
            parameters.Add(indexParam);

            var right =
                compiler.MaterializeReference
                (
                    compiler.CompileExpression(local, expression.Right, typeHint)
                );

            var selection = Expression.Lambda(right, parameters);
            var select =
                typeof(Enumerable).GetMethodExt
                (
                    "Select",
                    new System.Type[] { typeof(IEnumerable<ReflectionUtility.T>), typeof(Func<ReflectionUtility.T, int, ReflectionUtility.T>) }
                );
            select = select.MakeGenericMethod(memberType, selection.ReturnType);
            return Expression.Call(select, left, selection);
        }
Exemple #2
0
        private Expression CompileDereference(Compiler compiler, Frame frame, Expression left, Parse.BinaryExpression expression, System.Type typeHint)
        {
            left = compiler.MaterializeReference(left);

            var local = compiler.AddFrame(frame, expression);
            foreach (var field in left.Type.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                local.Add(expression, Name.FromNative(field.Name), field);
                var fieldExpression = Expression.Field(left, field);
                compiler.ExpressionsBySymbol.Add(field, fieldExpression);
            }
            return compiler.CompileExpression(local, expression.Right, typeHint);
        }
Exemple #3
0
        // Restriction
        public override ExpressionContext CompileExtractExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.ExtractExpression expression, BaseType typeHint)
        {
            var local = compiler.AddFrame(frame, expression);
            var alreadyOptional = left.Type is OptionalType;
            var memberNative = left.Type.GetNative(compiler.Emitter);
            var resultType = alreadyOptional ? left.Type : new OptionalType(left.Type);
            var resultNative = resultType.GetNative(compiler.Emitter);

            // Register value symbol
            LocalBuilder valueLocal = null;
            var localSymbol = new Object();
            local.Add(expression.Condition, Name.FromComponents(Parse.ReservedWords.Value), localSymbol);
            compiler.ContextsBySymbol.Add
            (
                localSymbol,
                new ExpressionContext
                (
                    null,
                    left.Type,
                    left.Characteristics,
                    m => { m.IL.Emit(OpCodes.Ldloc, valueLocal); }
                )
            );

            var condition = compiler.CompileExpression(local, expression.Condition, SystemTypes.Boolean);
            if (!(condition.Type is BooleanType))
                throw new CompilerException(expression.Condition, CompilerException.Codes.IncorrectType, condition.Type, "Boolean");

            return
                new ExpressionContext
                (
                    expression,
                    resultType,
                    Compiler.MergeCharacteristics(left.Characteristics, condition.Characteristics),
                    m =>
                    {
                        var nullLabel = m.IL.DefineLabel();
                        var endLabel = m.IL.DefineLabel();

                        // Register value argument
                        valueLocal = m.DeclareLocal(expression, memberNative, Parse.ReservedWords.Value);
                        left.EmitGet(m);
                        m.IL.Emit(OpCodes.Stloc, valueLocal);

                        condition.EmitGet(m);
                        m.IL.Emit(OpCodes.Brfalse, nullLabel);

                        // Passed condition
                        if (!alreadyOptional && memberNative.IsValueType)
                        {
                            var optionalLocal = m.DeclareLocal(expression, resultNative, Parse.ReservedWords.Value);
                            m.IL.Emit(OpCodes.Ldloca, optionalLocal);
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                            m.IL.Emit(OpCodes.Call, resultNative.GetConstructor(new System.Type[] { left.Type.GetNative(compiler.Emitter) }));
                            m.IL.Emit(OpCodes.Ldloc, optionalLocal);
                        }
                        else
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                        m.IL.Emit(OpCodes.Br, endLabel);

                        // Failed condition
                        m.IL.MarkLabel(nullLabel);
                        if (!alreadyOptional && memberNative.IsValueType)
                        {
                            var optionalLocal = m.DeclareLocal(expression, resultNative, Parse.ReservedWords.Value);
                            m.IL.Emit(OpCodes.Ldloca, optionalLocal);
                            m.IL.Emit(OpCodes.Initobj, resultNative);
                            m.IL.Emit(OpCodes.Ldloc, optionalLocal);
                        }
                        else if (alreadyOptional && memberNative.IsValueType)
                        {
                            m.IL.Emit(OpCodes.Ldloca, valueLocal);
                            m.IL.Emit(OpCodes.Initobj, resultNative);
                            m.IL.Emit(OpCodes.Ldloc, valueLocal);
                        }
                        else
                            m.IL.Emit(OpCodes.Ldnull);

                        m.IL.MarkLabel(endLabel);
                    }
                );
        }
Exemple #4
0
        private ExpressionContext CompileDereference(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            var local = compiler.AddFrame(frame, expression);
            var native = left.ActualNative(compiler.Emitter);

            LocalBuilder valueVariable = null;

            // Create symbol for each tuple member
            foreach (var a in ((TupleType)left.Type).Attributes)
            {
                var field = native.GetField(a.Key.ToString(), BindingFlags.Public | BindingFlags.Instance);
                local.Add(expression, a.Key, field);
                compiler.ContextsBySymbol.Add
                (
                    field,
                    new ExpressionContext
                    (
                        null,
                        a.Value,
                        Characteristic.Default,
                        m =>
                        {
                            m.IL.Emit(OpCodes.Ldloc, valueVariable);
                            m.IL.Emit(OpCodes.Ldfld, field);
                        }
                    )
                );
            }

            var right = compiler.CompileExpression(local, expression.Right, typeHint);

            return
                new ExpressionContext
                (
                    expression,
                    right.Type,
                    Compiler.MergeCharacteristics(left.Characteristics, right.Characteristics),
                    m =>
                    {
                        m.IL.BeginScope();

                        left.EmitGet(m);
                        valueVariable = m.DeclareLocal(expression.Right, native, "value");
                        m.IL.Emit(OpCodes.Stloc, valueVariable);

                        right.EmitGet(m);

                        m.IL.EndScope();
                    }
                );
        }
Exemple #5
0
        // Restriction
        public override ExpressionContext CompileExtractExpression(Compiler compiler, Frame frame, ExpressionContext left, Parse.ExtractExpression expression, BaseType typeHint)
        {
            var memberType = ((NaryType)left.Type).Of;
            var memberNative = left.NativeType ?? memberType.GetNative(compiler.Emitter);

            var local = compiler.AddFrame(frame, expression);

            // Prepare index and value symbols
            var indexSymbol = PrepareValueIndexContext(compiler, left, expression.Condition, memberType, memberNative, local);

            // Compile condition
            var condition = compiler.CompileExpression(local, expression.Condition, SystemTypes.Boolean);
            if (!(condition.Type is BooleanType))
                throw new CompilerException(expression.Condition, CompilerException.Codes.IncorrectType, condition.Type, "Boolean");

            var indexReferenced = compiler.References.ContainsKey(indexSymbol);

            return
                new ExpressionContext
                (
                    expression,
                    left.Type,
                    Compiler.MergeCharacteristics(left.Characteristics, condition.Characteristics),
                    m =>
                    {
                        // Create a new private method for the condition
                        var typeBuilder = (TypeBuilder)m.Builder.DeclaringType;
                        var innerMethod =
                            new MethodContext
                            (
                                typeBuilder.DefineMethod
                                (
                                    "Where" + expression.GetHashCode(),
                                    MethodAttributes.Private | MethodAttributes.Static,
                                    typeof(bool),	// return type
                                    indexReferenced ? new System.Type[] { memberNative, typeof(int) } : new System.Type[] { memberNative }	// param types
                                )
                            );
                        condition.EmitGet(innerMethod);
                        innerMethod.IL.Emit(OpCodes.Ret);

                        left.EmitGet(m);

                        // TODO: Force ordering to left if Set and index is referenced

                        // Instantiate a delegate pointing to the new method
                        m.IL.Emit(OpCodes.Ldnull);				// instance
                        m.IL.Emit(OpCodes.Ldftn, innerMethod.Builder);	// method
                        var funcType =
                            indexReferenced
                                ? System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(int), typeof(bool))
                                : System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(bool));
                        m.IL.Emit
                        (
                            OpCodes.Newobj,
                            funcType.GetConstructor(new[] { typeof(object), typeof(IntPtr) })
                        );

                        funcType = indexReferenced ? typeof(Func<ReflectionUtility.T, int, bool>) : typeof(Func<ReflectionUtility.T, bool>);
                        var where = typeof(System.Linq.Enumerable).GetMethodExt("Where", new System.Type[] { typeof(IEnumerable<ReflectionUtility.T>), funcType });
                        where = where.MakeGenericMethod(memberNative);

                        m.IL.EmitCall(OpCodes.Call, where, null);
                    }
                );
        }
Exemple #6
0
        protected virtual ExpressionContext CompileDereference(Compiler compiler, Frame frame, ExpressionContext left, Parse.BinaryExpression expression, Type.BaseType typeHint)
        {
            var memberType = ((NaryType)left.Type).Of;
            var memberNative = left.NativeType ?? memberType.GetNative(compiler.Emitter);

            var local = compiler.AddFrame(frame, expression);

            // Prepare index and value symbols
            var indexSymbol = PrepareValueIndexContext(compiler, left, expression.Right, memberType, memberNative, local);

            // Compile selection
            var selection = compiler.CompileExpression(local, expression.Right);

            var indexReferenced = compiler.References.ContainsKey(indexSymbol);

            return
                new ExpressionContext
                (
                    expression,
                    new ListType(selection.Type),
                    Compiler.MergeCharacteristics(left.Characteristics, selection.Characteristics),
                    m =>
                    {
                        // Create a new private method for the condition
                        var typeBuilder = (TypeBuilder)m.Builder.DeclaringType;
                        var innerMethod =
                            new MethodContext
                            (
                                typeBuilder.DefineMethod
                                (
                                    "Select" + expression.GetHashCode(),
                                    MethodAttributes.Private | MethodAttributes.Static,
                                    selection.ActualNative(compiler.Emitter),	// temporary return type
                                    indexReferenced ? new System.Type[] { memberNative, typeof(int) } : new System.Type[] { memberNative }	// param types
                                )
                            );
                        selection.EmitGet(innerMethod);
                        innerMethod.IL.Emit(OpCodes.Ret);

                        left.EmitGet(m);

                        // TODO: Force ordering of Left if a set and index is referenced

                        // Instantiate a delegate pointing to the new method
                        m.IL.Emit(OpCodes.Ldnull);				// instance
                        m.IL.Emit(OpCodes.Ldftn, innerMethod.Builder);	// method
                        var funcType =
                            indexReferenced
                                ? System.Linq.Expressions.Expression.GetFuncType(memberNative, typeof(int), innerMethod.Builder.ReturnType)
                                : System.Linq.Expressions.Expression.GetFuncType(memberNative, innerMethod.Builder.ReturnType);
                        m.IL.Emit
                        (
                            OpCodes.Newobj,
                            funcType.GetConstructor(new[] { typeof(object), typeof(IntPtr) })
                        );

                        funcType = indexReferenced ? typeof(Func<ReflectionUtility.T, int, ReflectionUtility.T>) : typeof(Func<ReflectionUtility.T, ReflectionUtility.T>);
                        var select =
                            typeof(Enumerable).GetMethodExt
                            (
                                "Select",
                                new System.Type[] { typeof(IEnumerable<ReflectionUtility.T>), funcType }
                            );
                        select = select.MakeGenericMethod(memberNative, innerMethod.Builder.ReturnType);

                        m.IL.EmitCall(OpCodes.Call, select, null);
                    }
                );
        }