示例#1
0
        // -------------------------------------------------------------------------------------------------------------
        // CIL implementation
        public static MethodBuilder CreateCilPInvokeMethod(
            Universe universe,
            IEssentials essentials,
            TypeBuilder builder,
            Method m,
            MethodAttributes methodAttributes,
            Type resolvedReturnType,
            Type[] resolvedParameterTypes)
        {
            var mb = builder.DefinePInvokeMethod(
                m.Name,
                m.Source.Package.Name + "-PInvoke.dll",
                PInvokeBackend.CName(m),
                methodAttributes | MethodAttributes.PinvokeImpl,
                CallingConventions.Standard,
                resolvedReturnType,
                null,
                null,
                resolvedParameterTypes,
                GetRequiredModifiers(universe, m.Parameters),
                null,
                CallingConvention.Cdecl,
                CharSet.Unicode);

            mb.SetImplementationFlags(MethodImplAttributes.PreserveSig);

            var rb = mb.DefineParameter(0, ParameterAttributes.None, null);
            var customReturnAttribute = GetCustomAttribute(universe, essentials, m.ReturnType);

            if (customReturnAttribute != null)
            {
                rb.SetCustomAttribute(customReturnAttribute);
            }

            int i = 1;

            foreach (var param in m.Parameters)
            {
                var pb = mb.DefineParameter(i, GetParameterTypeAttributes(param.Type), param.Name);
                var customAttribute = GetCustomAttribute(universe, essentials, param.Type);
                if (customAttribute != null)
                {
                    pb.SetCustomAttribute(customAttribute);
                }
                ++i;
            }

            return(mb);
        }
示例#2
0
        void PopulateDelegate()
        {
            var dt = (DelegateType)Definition;
            var cb = Builder.DefineConstructor(
                MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public,
                CallingConventions.Standard, new[] { _linker.System_Object, _linker.System_IntPtr });

            cb.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            var mb = Builder.DefineMethod("Invoke",
                                          MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual,
                                          _linker.GetType(dt.ReturnType), _linker.GetParameterTypes(dt.Parameters));

            mb.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            if (_backend.IsPInvokable(_essentials, dt))
            {
                for (int i = 0; i < dt.Parameters.Length; i++)
                {
                    var p  = dt.Parameters[i];
                    var pb = PInvokeBackend.DefineCilDelegateParameter(_linker.Universe, _essentials, mb, p, i);
                    PopulateParameter(p, pb);
                }
            }
            else
            {
                for (int i = 0; i < dt.Parameters.Length; i++)
                {
                    var p = dt.Parameters[i];
                    PopulateParameter(p, mb.DefineParameter(i + 1, p.CilParameterAttributes(), p.Name));
                }
            }

            _linker.AddConstructor(Definition, cb);
            _linker.AddMethod(Definition, mb);
        }
示例#3
0
 public PInvokeDecompiler(PInvokeBackend backend)
 {
     _backend = backend;
 }
示例#4
0
        void PopulateClassStructOrInterface()
        {
            if (Definition is ClassType && Definition.Base != null)
            {
                Builder.SetParent(_linker.GetType(Definition.Base));
            }

            foreach (var it in Definition.Interfaces)
            {
                Builder.AddInterfaceImplementation(_linker.GetType(it));
            }

            foreach (var m in Definition.Fields)
            {
                _linker.AddField(m, DefineField(m, _linker.GetType(m.ReturnType)));
            }

            foreach (var m in Definition.Events)
            {
                var eb = DefineEvent(m, _linker.GetType(m.ReturnType));

                if (m.ImplicitField != null)
                {
                    _linker.AddField(m.ImplicitField,
                                     DefineField(m.ImplicitField, _linker.GetType(m.ImplicitField.ReturnType)));
                }

                if (m.AddMethod != null)
                {
                    var mb = DefineMethod(m.AddMethod, MethodAttributes.SpecialName, _linker.GetType(m.AddMethod.ReturnType), _linker.GetParameterTypes(m.AddMethod.Parameters));
                    _linker.AddMethod(m.AddMethod, mb);
                    eb.SetAddOnMethod(mb);
                }

                if (m.RemoveMethod != null)
                {
                    var mb = DefineMethod(m.RemoveMethod, MethodAttributes.SpecialName, _linker.GetType(m.RemoveMethod.ReturnType), _linker.GetParameterTypes(m.RemoveMethod.Parameters));
                    _linker.AddMethod(m.RemoveMethod, mb);
                    eb.SetRemoveOnMethod(mb);
                }
            }

            foreach (var m in Definition.Properties)
            {
                var pb = DefineProperty(m, m.Parameters.Length > 0 ? PropertyAttributes.SpecialName : 0, _linker.GetType(m.ReturnType), _linker.GetParameterTypes(m.Parameters));

                if (m.ImplicitField != null)
                {
                    _linker.AddField(m.ImplicitField,
                                     DefineField(m.ImplicitField, _linker.GetType(m.ImplicitField.ReturnType)));
                }

                if (m.GetMethod != null)
                {
                    var mb = DefineMethod(m.GetMethod, MethodAttributes.SpecialName, _linker.GetType(m.GetMethod.ReturnType), _linker.GetParameterTypes(m.GetMethod.Parameters));
                    _linker.AddMethod(m.GetMethod, mb);
                    pb.SetGetMethod(mb);
                }

                if (m.SetMethod != null)
                {
                    var mb = DefineMethod(m.SetMethod, MethodAttributes.SpecialName, _linker.GetType(m.SetMethod.ReturnType), _linker.GetParameterTypes(m.SetMethod.Parameters));
                    _linker.AddMethod(m.SetMethod, mb);
                    pb.SetSetMethod(mb);
                }
            }

            foreach (var m in Definition.Methods)
            {
                if (_backend.IsPInvokable(_essentials, m))
                {
                    var mb = PInvokeBackend.CreateCilPInvokeMethod(
                        _linker.Universe,
                        _essentials,
                        Builder,
                        m,
                        m.CilMethodAttributes(),
                        _linker.GetType(m.ReturnType),
                        _linker.GetParameterTypes(m.Parameters));
                    m.SetBody(null);
                    Methods.Add(new CilMember <MethodBuilder, Function>(mb, m));
                    _linker.AddMethod(m, mb);
                }
                else
                {
                    var mb = DefineMethod(m);

                    if (m.IsGenericDefinition)
                    {
                        var paramNames = new string[m.GenericParameters.Length];
                        for (int i = 0; i < paramNames.Length; i++)
                        {
                            paramNames[i] = m.GenericParameters[i].UnoName;
                        }

                        var paramTypes = mb.DefineGenericParameters(paramNames);
                        for (int i = 0; i < paramTypes.Length; i++)
                        {
                            _linker.AddType(m.GenericParameters[i], paramTypes[i]);
                        }
                        for (int i = 0; i < paramTypes.Length; i++)
                        {
                            SetConstraints(paramTypes[i], m.GenericParameters[i]);
                        }
                    }

                    mb.SetReturnType(_linker.GetType(m.ReturnType));
                    mb.SetParameters(_linker.GetParameterTypes(m.Parameters));
                    _linker.AddMethod(m, mb);

                    if (m.Prototype != null && m.Prototype != m &&
                        m.Prototype.HasAttribute(_essentials.DotNetOverrideAttribute))
                    {
                        _linker.AddMethod(m.Prototype, mb);
                    }
                }
            }

            foreach (var m in Definition.Constructors)
            {
                var cb = DefineConstructor(m, _linker.GetParameterTypes(m.Parameters));
                _linker.AddConstructor(m, cb);
            }

            foreach (var m in Definition.Casts)
            {
                var mb = DefineMethod(m, MethodAttributes.SpecialName, _linker.GetType(m.ReturnType), _linker.GetParameterTypes(m.Parameters));
                _linker.AddMethod(m, mb);
            }

            foreach (var m in Definition.Operators)
            {
                var mb = DefineMethod(m, MethodAttributes.SpecialName, _linker.GetType(m.ReturnType), _linker.GetParameterTypes(m.Parameters));
                _linker.AddMethod(m, mb);
            }

            if (Definition.Initializer != null)
            {
                DefineTypeInitializer(Definition.Initializer);
            }

            if (Definition.Finalizer != null)
            {
                DefineTypeFinalizer(Definition.Finalizer);
            }

            // Add dummy field if [TargetSpecificType]
            if (Definition.HasAttribute(_essentials.TargetSpecificTypeAttribute))
            {
                Builder.DefineField("__ptr", _linker.System_IntPtr, FieldAttributes.Private);
            }

            if (Definition.IsStatic)
            {
                Builder.SetCustomAttribute(
                    new CustomAttributeBuilder(_linker.System_Runtime_CompilerServices_ExtensionAttribute_ctor, new object[0]));
            }
        }
示例#5
0
 public PInvokeWriter(PInvokeBackend backend, StringBuilder sb, Function context)
     : base(backend, sb, context)
 {
     _backend = backend;
 }
示例#6
0
 public PInvokeWriter(PInvokeBackend backend, string fileName)
     : base(backend, fileName)
 {
     _backend = backend;
 }