示例#1
0
        public override bool Define()
        {
            if (!base.Define())
            {
                return(false);
            }

            if (!BuiltinTypeSpec.IsPrimitiveType(MemberType))
            {
                Report.Error(1663, Location,
                             "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
                             GetSignatureForError());
            }
            else if (declarators != null)
            {
                foreach (var d in declarators)
                {
                    var f = new FixedField(Parent, d.GetFieldTypeExpression(this), ModFlags, new MemberName(d.Name.Value, d.Name.Location), OptAttributes);
                    f.initializer = d.Initializer;
                    ((ConstInitializer)f.initializer).Name = d.Name.Value;
                    f.Define();
                    Parent.PartialContainer.Members.Add(f);
                }
            }

            // Create nested fixed buffer container
            string name = String.Format("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);

            fixed_buffer_type = Parent.TypeBuilder.DefineNestedType(name,
                                                                    TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                                                                    Compiler.BuiltinTypes.ValueType.GetMetaInfo());

            var ffield = fixed_buffer_type.DefineField(FixedElementName, MemberType.GetMetaInfo(), FieldAttributes.Public);

            FieldBuilder = Parent.TypeBuilder.DefineField(Name, fixed_buffer_type, ModifiersExtensions.FieldAttr(ModFlags));

            var element_spec = new FieldSpec(null, this, MemberType, ffield, ModFlags);

            spec = new FixedFieldSpec(Module, Parent.Definition, this, FieldBuilder, element_spec, ModFlags);

            Parent.MemberCache.AddMember(spec);
            return(true);
        }
示例#2
0
            public FieldSpec DefineInitializedData(byte[] data, Location loc)
            {
                Struct size_type;

                if (!size_types.TryGetValue(data.Length, out size_type))
                {
                    //
                    // Build common type for this data length. We cannot use
                    // DefineInitializedData because it creates public type,
                    // and its name is not unique among modules
                    //
                    size_type = new Struct(this, new MemberName("$ArrayType=" + data.Length, loc), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
                    size_type.CreateContainer();
                    size_type.DefineContainer();

                    size_types.Add(data.Length, size_type);

                    // It has to work even if StructLayoutAttribute does not exist
                    size_type.TypeBuilder.__SetLayout(1, data.Length);
                }

                var name = "$field-" + fields.ToString("X");

                ++fields;
                const Modifiers fmod     = Modifiers.STATIC | Modifiers.INTERNAL;
                var             fbuilder = TypeBuilder.DefineField(name, size_type.CurrentType.GetMetaInfo(), ModifiersExtensions.FieldAttr(fmod) | FieldAttributes.HasFieldRVA);

                fbuilder.__SetDataAndRVA(data);

                return(new FieldSpec(CurrentType, null, size_type.CurrentType, fbuilder, fmod));
            }
示例#3
0
            public FieldSpec DefineInitializedData(byte[] data, Location loc)
            {
                Struct size_type;

                if (!size_types.TryGetValue(data.Length, out size_type))
                {
                    //
                    // Build common type for this data length. We cannot use
                    // DefineInitializedData because it creates public type,
                    // and its name is not unique among modules
                    //
                    size_type = new Struct(null, this, new MemberName("$ArrayType=" + data.Length, Location), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
                    size_type.CreateType();
                    size_type.DefineType();

                    size_types.Add(data.Length, size_type);
                    var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve(Location);
                    if (ctor != null)
                    {
                        var argsEncoded = new AttributeEncoder();
                        argsEncoded.Encode((short)LayoutKind.Explicit);

                        var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve(Location);
                        var pack       = Module.PredefinedMembers.StructLayoutPack.Resolve(Location);
                        if (field_size != null && pack != null)
                        {
                            argsEncoded.EncodeNamedArguments(
                                new[] { field_size, pack },
                                new[] { new IntConstant(Compiler.BuiltinTypes, (int)data.Length, Location), new IntConstant(Compiler.BuiltinTypes, 1, Location) }
                                );

                            size_type.TypeBuilder.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), argsEncoded.ToArray());
                        }
                    }
                }

                var name = "$field-" + fields.ToString("X");

                ++fields;
                const Modifiers fmod     = Modifiers.STATIC | Modifiers.INTERNAL;
                var             fbuilder = TypeBuilder.DefineField(name, size_type.CurrentType.GetMetaInfo(), ModifiersExtensions.FieldAttr(fmod) | FieldAttributes.HasFieldRVA);

                fbuilder.__SetDataAndRVA(data);

                return(new FieldSpec(CurrentType, null, size_type.CurrentType, fbuilder, fmod));
            }
示例#4
0
            public FieldSpec DefineInitializedData(byte[] data, Location loc)
            {
                Struct size_type;

                if (!size_types.TryGetValue(data.Length, out size_type))
                {
                    //
                    // Build common type for this data length. We cannot use
                    // DefineInitializedData because it creates public type,
                    // and its name is not unique among modules
                    //
                    size_type = new Struct(null, this, new MemberName("$ArrayType=" + data.Length, Location), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
                    size_type.CreateType();
                    size_type.DefineType();

                    size_types.Add(data.Length, size_type);

                    var pa = Module.PredefinedAttributes.StructLayout;
                    if (pa.Constructor != null || pa.ResolveConstructor(Location, TypeManager.short_type))
                    {
                        var argsEncoded = new AttributeEncoder();
                        argsEncoded.Encode((short)LayoutKind.Explicit);

                        var field_size = pa.GetField("Size", TypeManager.int32_type, Location);
                        var pack       = pa.GetField("Pack", TypeManager.int32_type, Location);
                        if (field_size != null)
                        {
                            argsEncoded.EncodeNamedArguments(
                                new[] { field_size, pack },
                                new[] { new IntConstant((int)data.Length, Location), new IntConstant(1, Location) }
                                );
                        }

                        pa.EmitAttribute(size_type.TypeBuilder, argsEncoded);
                    }
                }

                var name = "$field-" + fields.ToString("X");

                ++fields;
                const Modifiers fmod     = Modifiers.STATIC | Modifiers.INTERNAL;
                var             fbuilder = TypeBuilder.DefineField(name, size_type.CurrentType.GetMetaInfo(), ModifiersExtensions.FieldAttr(fmod) | FieldAttributes.HasFieldRVA);

#if STATIC
                fbuilder.__SetDataAndRVA(data);
#else
                if (set_data == null)
                {
                    set_data = typeof(FieldBuilder).GetMethod("SetRVAData", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                }

                try
                {
                    set_data.Invoke(fbuilder, new object[] { data });
                }
                catch
                {
                    Report.RuntimeMissingSupport(loc, "SetRVAData");
                }
#endif

                return(new FieldSpec(CurrentType, null, size_type.CurrentType, fbuilder, fmod));
            }