예제 #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}", TypeDefinition.FilterNestedName(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));
            }