Exemple #1
0
		void EmitFieldSize (int buffer_size)
		{
			int type_size = BuiltinTypeSpec.GetSize (MemberType);

			if (buffer_size > int.MaxValue / type_size) {
				Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
					GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
				return;
			}

			AttributeEncoder encoder;

			var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve (Location);
			if (ctor == null)
				return;

			var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve (Location);
			var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve (Location);
			if (field_size == null || field_charset == null)
				return;

			var char_set = CharSet ?? Module.DefaultCharSet ?? 0;

			encoder = new AttributeEncoder ();
			encoder.Encode ((short)LayoutKind.Sequential);
			encoder.EncodeNamedArguments (
				new [] { field_size, field_charset },
				new Constant [] { 
					new IntConstant (Compiler.BuiltinTypes, buffer_size * type_size, Location),
					new IntConstant (Compiler.BuiltinTypes, (int) char_set, Location)
				}
			);

			fixed_buffer_type.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());

			//
			// Don't emit FixedBufferAttribute attribute for private types
			//
			if ((ModFlags & Modifiers.PRIVATE) != 0)
				return;

			ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve (Location);
			if (ctor == null)
				return;

			encoder = new AttributeEncoder ();
			encoder.EncodeTypeName (MemberType);
			encoder.Encode (buffer_size);
			encoder.EncodeEmptyNamedArguments ();

			FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
		}
Exemple #2
0
        void EmitFieldSize(int buffer_size)
        {
            int type_size = BuiltinTypeSpec.GetSize(MemberType);

            if (buffer_size > int.MaxValue / type_size)
            {
                Report.Error(1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
                             GetSignatureForError(), buffer_size.ToString(), MemberType.GetSignatureForError());
                return;
            }

            AttributeEncoder encoder;
            MethodSpec       ctor;

            var char_set = CharSetValue ?? Module.DefaultCharSet ?? 0;

#if STATIC
            //
            // Set struct layout without resolving StructLayoutAttribute which is not always available
            //

            TypeAttributes attribs = TypeAttributes.SequentialLayout;
            switch (char_set)
            {
            case CharSet.None:
            case CharSet.Ansi:
                attribs |= TypeAttributes.AnsiClass;
                break;

            case CharSet.Auto:
                attribs |= TypeAttributes.AutoClass;
                break;

            case CharSet.Unicode:
                attribs |= TypeAttributes.UnicodeClass;
                break;
            }

            fixed_buffer_type.__SetAttributes(fixed_buffer_type.Attributes | attribs);
            fixed_buffer_type.__SetLayout(0, buffer_size * type_size);
#else
            ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve(Location);
            if (ctor == null)
            {
                return;
            }

            var field_size    = Module.PredefinedMembers.StructLayoutSize.Resolve(Location);
            var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve(Location);
            if (field_size == null || field_charset == null)
            {
                return;
            }

            encoder = new AttributeEncoder();
            encoder.Encode((short)LayoutKind.Sequential);
            encoder.EncodeNamedArguments(
                new [] { field_size, field_charset },
                new Constant [] {
                new IntConstant(Compiler.BuiltinTypes, buffer_size * type_size, Location),
                new IntConstant(Compiler.BuiltinTypes, (int)char_set, Location)
            }
                );

            fixed_buffer_type.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), encoder.ToArray(out _));
#endif
            //
            // Don't emit FixedBufferAttribute attribute for private types
            //
            if ((ModFlags & Modifiers.PRIVATE) != 0)
            {
                return;
            }

            ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve(Location);
            if (ctor == null)
            {
                return;
            }

            encoder = new AttributeEncoder();
            encoder.EncodeTypeName(MemberType);
            encoder.Encode(buffer_size);
            encoder.EncodeEmptyNamedArguments();

            FieldBuilder.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), encoder.ToArray(out var references));
            Module.AddAssemblyReferences(references);
        }