Example #1
0
        TypeBuilder CreateTypeBuilder(DataType dt, TypeBuilder parent, TypeAttributes typeAttrs, Type typeBase)
        {
            var result = parent?.DefineNestedType(dt.CilTypeName(), dt.CilTypeAttributes(true) | typeAttrs, typeBase) ?? _module.DefineType(dt.CilTypeName(), dt.CilTypeAttributes(false) | typeAttrs, typeBase);
            var data   = new CilType(_backend, _essentials, _linker, result, dt);

            if (!dt.IsEnum && dt.IsFlattenedDefinition)
            {
                var flatParams     = dt.FlattenedParameters;
                var flatParamNames = new string[flatParams.Length];

                for (int i = 0, l = flatParamNames.Length; i < l; i++)
                {
                    flatParamNames[i] = flatParams[i].Name;
                }

                var resultParams = result.DefineGenericParameters(flatParamNames);

                for (int i = 0; i < resultParams.Length; i++)
                {
                    data.GenericParameters.Add(new CilMember <GenericTypeParameterBuilder, GenericParameterType>(resultParams[i], flatParams[i]));
                }

                if (dt.IsGenericDefinition)
                {
                    var dtParams = dt.GenericParameters;

                    for (int i = 0, l = dtParams.Length; i < l; i++)
                    {
                        _linker.AddType(dtParams[i], resultParams[resultParams.Length - dtParams.Length + i]);
                    }
                }
            }

            _linker.AddType(dt, result);
            Add(data);
            return(result);
        }
Example #2
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]));
            }
        }