Esempio n. 1
0
        public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
        {
            string name = binder.Name;

            if (name == _info.Name)
            {
                name = "TypeLibDesc";
            }
            else if (name != "Guid" &&
                     name != "Name" &&
                     name != "VersionMajor" &&
                     name != "VersionMinor")
            {
                return(binder.FallbackGetMember(this));
            }

            return(new DynamicMetaObject(
                       Expression.Convert(
                           Expression.Property(
                               AstUtils.Convert(Expression, typeof(ComTypeLibInfo)),
                               typeof(ComTypeLibInfo).GetProperty(name)
                               ),
                           typeof(object)
                           ),
                       ComTypeLibInfoRestrictions(this)
                       ));
        }
        private DynamicMetaObject TryBindGetMember(string name)
        {
            if (_lib.HasMember(name))
            {
                BindingRestrictions restrictions =
                    BindingRestrictions.GetTypeRestriction(
                        Expression, typeof(ComTypeLibDesc)
                        ).Merge(
                        BindingRestrictions.GetExpressionRestriction(
                            Expression.Equal(
                                Expression.Property(
                                    AstUtils.Convert(
                                        Expression, typeof(ComTypeLibDesc)
                                        ),
                                    typeof(ComTypeLibDesc).GetProperty(nameof(ComTypeLibDesc.Guid))
                                    ),
                                Expression.Constant(_lib.Guid)
                                )
                            )
                        );

                return(new DynamicMetaObject(
                           Expression.Constant(
                               ((ComTypeLibDesc)Value).GetTypeLibObjectDesc(name)
                               ),
                           restrictions
                           ));
            }

            return(null);
        }
 private BindingRestrictions EnumRestrictions()
 {
     return(BindingRestrictions.GetTypeRestriction(
                Expression, typeof(ComTypeEnumDesc)
                ).Merge(
                // ((ComTypeEnumDesc)<arg>).TypeLib.Guid == <guid>
                BindingRestrictions.GetExpressionRestriction(
                    Expression.Equal(
                        Expression.Property(
                            Expression.Property(
                                AstUtils.Convert(Expression, typeof(ComTypeEnumDesc)),
                                typeof(ComTypeDesc).GetProperty("TypeLib")),
                            typeof(ComTypeLibDesc).GetProperty("Guid")),
                        AstUtils.Constant(_desc.TypeLib.Guid)
                        )
                    )
                ).Merge(
                BindingRestrictions.GetExpressionRestriction(
                    Expression.Equal(
                        Expression.Property(
                            AstUtils.Convert(Expression, typeof(ComTypeEnumDesc)),
                            typeof(ComTypeEnumDesc).GetProperty("TypeName")
                            ),
                        AstUtils.Constant(_desc.TypeName)
                        )
                    )
                ));
 }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            Expression closureItem = GetClosureItem(node, true);

            if (closureItem is null)
            {
                return(node);
            }
            // Convert can go away if we switch to strongly typed StrongBox
            return(AstUtils.Convert(closureItem, node.Type));
        }
Esempio n. 5
0
 public DynamicMetaObject GetMetaObject(Expression parameter, int index)
 {
     return(DynamicMetaObject.Create(
                GetArgument(index),
                Expression.Call(
                    s_getArgMethod,
                    AstUtils.Convert(parameter, typeof(ArgumentArray)),
                    AstUtils.Constant(index)
                    )
                ));
 }
 public override DynamicMetaObject BindCreateInstance(CreateInstanceBinder binder, DynamicMetaObject[] args)
 {
     return(new DynamicMetaObject(
                Expression.Call(
                    AstUtils.Convert(Expression, typeof(ComTypeClassDesc)),
                    typeof(ComTypeClassDesc).GetMethod("CreateInstance")
                    ),
                BindingRestrictions.Combine(args).Merge(
                    BindingRestrictions.GetTypeRestriction(Expression, typeof(ComTypeClassDesc))
                    )
                ));
 }
        protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression node)
        {
            int count   = node.Variables.Count;
            var boxes   = new List <Expression>();
            var vars    = new List <ParameterExpression>();
            var indexes = new int[count];

            for (int i = 0; i < count; i++)
            {
                Expression box = GetClosureItem(node.Variables[i], false);
                if (box is null)
                {
                    indexes[i] = vars.Count;
                    vars.Add(node.Variables[i]);
                }
                else
                {
                    indexes[i] = -1 - boxes.Count;
                    boxes.Add(box);
                }
            }

            // No variables were rewritten. Just return the original node.
            if (boxes.Count == 0)
            {
                return(node);
            }

            var boxesArray = Expression.NewArrayInit(typeof(IStrongBox), boxes);

            // All of them were rewritten. Just return the array, wrapped in a
            // read-only collection.
            if (vars.Count == 0)
            {
                return(Expression.Invoke(
                           Expression.Constant((Func <IStrongBox[], IRuntimeVariables>)RuntimeVariables.Create),
                           boxesArray
                           ));
            }

            // Otherwise, we need to return an object that merges them
            Func <IRuntimeVariables, IRuntimeVariables, int[], IRuntimeVariables> helper = MergedRuntimeVariables.Create;

            return(Expression.Invoke(AstUtils.Constant(helper), Expression.RuntimeVariables(vars), boxesArray, AstUtils.Constant(indexes)));
        }
 protected override Expression VisitBinary(BinaryExpression node)
 {
     if (node.NodeType == ExpressionType.Assign &&
         node.Left.NodeType == ExpressionType.Parameter)
     {
         var        variable    = (ParameterExpression)node.Left;
         Expression closureItem = GetClosureItem(variable, true);
         if (closureItem != null)
         {
             // We need to convert to object to store the value in the box.
             return(Expression.Block(
                        new[] { variable },
                        Expression.Assign(variable, Visit(node.Right)),
                        Expression.Assign(closureItem, AstUtils.Convert(variable, typeof(object))),
                        variable
                        ));
         }
     }
     return(base.VisitBinary(node));
 }
Esempio n. 9
0
        //TODO enable sharing of these custom delegates
        private Delegate CreateCustomDelegate(Type delegateType)
        {
            //PerfTrack.NoteEvent(PerfTrack.Categories.Compiler, "Synchronously compiling a custom delegate");

            var method             = delegateType.GetMethod("Invoke");
            var paramInfos         = method.GetParameters();
            var parameters         = new ParameterExpression[paramInfos.Length];
            var parametersAsObject = new Expression[paramInfos.Length];

            for (int i = 0; i < paramInfos.Length; i++)
            {
                ParameterExpression parameter = Expression.Parameter(paramInfos[i].ParameterType, paramInfos[i].Name);
                parameters[i]         = parameter;
                parametersAsObject[i] = Expression.Convert(parameter, typeof(object));
            }

            var data      = Expression.NewArrayInit(typeof(object), parametersAsObject);
            var self      = AstUtils.Constant(this);
            var runMethod = typeof(LightLambda).GetMethod("Run");
            var body      = Expression.Convert(Expression.Call(self, runMethod, data), method.ReturnType);
            var lambda    = Expression.Lambda(delegateType, body, parameters);

            return(lambda.Compile());
        }