예제 #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 ());
		}
예제 #2
0
파일: class.cs 프로젝트: 0xd4d/NRefactory
		void EmitIndexerName ()
		{
			if (!has_normal_indexers)
				return;

			var ctor = Module.PredefinedMembers.DefaultMemberAttributeCtor.Get ();
			if (ctor == null)
				return;

			var encoder = new AttributeEncoder ();
			encoder.Encode (GetAttributeDefaultMember ());
			encoder.EncodeEmptyNamedArguments ();

			TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
		}
예제 #3
0
파일: assembly.cs 프로젝트: jj-jabb/mono
        public virtual void Emit()
        {
            if (Compiler.Settings.Target == Target.Module)
            {
                module_target_attrs = new AssemblyAttributesPlaceholder(module, name);
                module_target_attrs.CreateContainer();
                module_target_attrs.DefineContainer();
                module_target_attrs.Define();
                module.AddCompilerGeneratedClass(module_target_attrs);
            }
            else if (added_modules != null)
            {
                ReadModulesAssemblyAttributes();
            }

            if (Compiler.Settings.GenerateDebugInfo)
            {
                symbol_writer = new MonoSymbolFile();
            }

            module.EmitContainer();

            if (module.HasExtensionMethod)
            {
                var pa = module.PredefinedAttributes.Extension;
                if (pa.IsDefined)
                {
                    SetCustomAttribute(pa.Constructor, AttributeEncoder.Empty);
                }
            }

            if (!IsSatelliteAssembly)
            {
                if (!wrap_non_exception_throws_custom)
                {
                    PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
                    if (pa.IsDefined && pa.ResolveBuilder())
                    {
                        var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get();
                        if (prop != null)
                        {
                            AttributeEncoder encoder = new AttributeEncoder();
                            encoder.EncodeNamedPropertyArgument(prop, new BoolLiteral(Compiler.BuiltinTypes, true, Location.Null));
                            SetCustomAttribute(pa.Constructor, encoder.ToArray());
                        }
                    }
                }

                if (declarative_security != null)
                {
#if STATIC
                    foreach (var entry in declarative_security)
                    {
                        Builder.__AddDeclarativeSecurity(entry);
                    }
#else
                    throw new NotSupportedException("Assembly-level security");
#endif
                }
            }

            CheckReferencesPublicToken();

            SetEntryPoint();
        }
예제 #4
0
파일: assembly.cs 프로젝트: tris2481/ILSpy
        public virtual void Emit()
        {
            if (Compiler.Settings.Target == Target.Module)
            {
                module_target_attrs = new AssemblyAttributesPlaceholder(module, name);
                module_target_attrs.CreateType();
                module_target_attrs.DefineType();
                module_target_attrs.Define();
                module.AddCompilerGeneratedClass(module_target_attrs);
            }
            else if (added_modules != null)
            {
                ReadModulesAssemblyAttributes();
            }

            if (Compiler.Settings.GenerateDebugInfo)
            {
                symbol_writer = new MonoSymbolWriter(file_name);

                // Register all source files with symbol writer
                foreach (var source in Compiler.SourceFiles)
                {
                    source.DefineSymbolInfo(symbol_writer);
                }

                // TODO: global variables
                SymbolWriter.symwriter = symbol_writer;
            }

            module.Emit();

            if (module.HasExtensionMethod)
            {
                var pa = module.PredefinedAttributes.Extension;
                if (pa.IsDefined)
                {
                    SetCustomAttribute(pa.Constructor, AttributeEncoder.Empty);
                }
            }

            if (!wrap_non_exception_throws_custom)
            {
                PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
                if (pa.IsDefined && pa.ResolveBuilder())
                {
                    var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get();
                    if (prop != null)
                    {
                        AttributeEncoder encoder = new AttributeEncoder();
                        encoder.EncodeNamedPropertyArgument(prop, new BoolLiteral(Compiler.BuiltinTypes, true, Location.Null));
                        SetCustomAttribute(pa.Constructor, encoder.ToArray());
                    }
                }
            }

            if (declarative_security != null)
            {
#if STATIC
                foreach (var entry in declarative_security)
                {
                    Builder.__AddDeclarativeSecurity(entry);
                }
#else
                var args = new PermissionSet[3];
                declarative_security.TryGetValue(SecurityAction.RequestMinimum, out args[0]);
                declarative_security.TryGetValue(SecurityAction.RequestOptional, out args[1]);
                declarative_security.TryGetValue(SecurityAction.RequestRefuse, out args[2]);
                builder_extra.AddPermissionRequests(args);
#endif
            }

            CheckReferencesPublicToken();

            SetEntryPoint();
        }
예제 #5
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));
            }
예제 #6
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;

			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 ());
		}
예제 #7
0
파일: field.cs 프로젝트: allisterb/Escher
        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);
        }