Exemple #1
0
        private void InitializeFieldDefinition(Cts.FieldDesc entity, Field record)
        {
            record.Name      = HandleString(entity.Name);
            record.Signature = new FieldSignature
            {
                Type = HandleType(entity.FieldType),
                // TODO: CustomModifiers
            };
            record.Flags = GetFieldAttributes(entity);

            var ecmaField = entity as Cts.Ecma.EcmaField;

            if (ecmaField != null)
            {
                Ecma.MetadataReader  reader             = ecmaField.MetadataReader;
                Ecma.FieldDefinition fieldDef           = reader.GetFieldDefinition(ecmaField.Handle);
                Ecma.ConstantHandle  defaultValueHandle = fieldDef.GetDefaultValue();
                if (!defaultValueHandle.IsNil)
                {
                    record.DefaultValue = HandleConstant(ecmaField.Module, defaultValueHandle);
                }

                Ecma.CustomAttributeHandleCollection customAttributes = fieldDef.GetCustomAttributes();
                if (customAttributes.Count > 0)
                {
                    record.CustomAttributes = HandleCustomAttributes(ecmaField.Module, customAttributes);
                }
            }

            // TODO: Offset
        }
Exemple #2
0
 private void InitializeFieldReference(Cts.FieldDesc entity, MemberReference record)
 {
     record.Name      = HandleString(entity.Name);
     record.Parent    = HandleType(entity.OwningType);
     record.Signature = new FieldSignature
     {
         Type = HandleType(entity.GetTypicalFieldDefinition().FieldType),
         // TODO: CustomModifiers
     };
 }
        /// <summary>
        /// Attempts to retrieve a <see cref="Field"/> record corresponding to the specified
        /// <paramref name="field"/>. Returns null if not found.
        /// </summary>
        public Field GetTransformedFieldDefinition(Cts.FieldDesc field)
        {
            Debug.Assert(field.OwningType.IsTypeDefinition);

            MetadataRecord rec;

            if (!_transform._fields.TryGet(field, out rec))
            {
                return(null);
            }

            return(rec as Field);
        }
Exemple #4
0
        private void InitializeFieldDefinition(Cts.FieldDesc entity, Field record)
        {
            record.Name      = HandleString(entity.Name);
            record.Signature = new FieldSignature
            {
                Type = HandleType(entity.FieldType),
                // TODO: CustomModifiers
            };
            record.Flags = GetFieldAttributes(entity);

            // TODO: CustomAttributes
            // TODO: DefaultValue
            // TODO: Offset
        }
Exemple #5
0
 public override MetadataRecord HandleQualifiedField(Cts.FieldDesc field)
 {
     if (_policy.GeneratesMetadata(field) && field.GetTypicalFieldDefinition() == field)
     {
         QualifiedField record = new QualifiedField();
         record.Field         = HandleFieldDefinition(field);
         record.EnclosingType = (TypeDefinition)HandleType(field.OwningType);
         return(record);
     }
     else
     {
         return(HandleFieldReference(field));
     }
 }
Exemple #6
0
 /// <summary>
 /// Gets an object representing the static data for RVA mapped fields from the PE image.
 /// </summary>
 public ObjectNode GetFieldRvaData(FieldDesc field)
 {
     if (field.GetType() == typeof(Internal.IL.Stubs.PInvokeLazyFixupField))
     {
         var pInvokeFixup = (Internal.IL.Stubs.PInvokeLazyFixupField)field;
         PInvokeMetadata metadata = pInvokeFixup.PInvokeMetadata;
         return NodeFactory.PInvokeMethodFixup(metadata.Module, metadata.Name);
     }
     else
     {
         // Use the typical field definition in case this is an instantiated generic type
         field = field.GetTypicalFieldDefinition();
         return NodeFactory.ReadOnlyDataBlob(NameMangler.GetMangledFieldName(field),
             ((EcmaField)field).GetFieldRvaData(), NodeFactory.Target.PointerSize);
     }
 }
Exemple #7
0
        private MetadataRecord HandleField(Cts.FieldDesc field)
        {
            MetadataRecord rec;

            if (_policy.GeneratesMetadata(field))
            {
                rec = HandleFieldDefinition(field);
            }
            else
            {
                rec = _fields.GetOrCreate(field, _initFieldRef ?? (_initFieldRef = InitializeFieldReference));
            }

            Debug.Assert(rec is Field || rec is MemberReference);

            return(rec);
        }
Exemple #8
0
        private FieldAttributes GetFieldAttributes(Cts.FieldDesc field)
        {
            FieldAttributes result;

            var ecmaField = field as Cts.Ecma.EcmaField;

            if (ecmaField != null)
            {
                var fieldDefinition = ecmaField.MetadataReader.GetFieldDefinition(ecmaField.Handle);
                result = fieldDefinition.Attributes;
            }
            else
            {
                result = 0;

                if (field.IsStatic)
                {
                    result |= FieldAttributes.Static;
                }
                if (field.IsInitOnly)
                {
                    result |= FieldAttributes.InitOnly;
                }
                if (field.IsLiteral)
                {
                    result |= FieldAttributes.Literal;
                }
                if (field.HasRva)
                {
                    result |= FieldAttributes.HasFieldRVA;
                }

                // Not set: Visibility, NotSerialized, SpecialName, RTSpecialName, HasFieldMarshal, HasDefault
            }

            return(result);
        }
Exemple #9
0
        private string ComputeMangledFieldName(FieldDesc field)
        {
            string prependTypeName = null;
            if (!_compilation.IsCppCodeGen)
                prependTypeName = GetMangledTypeName(field.OwningType);

            if (field is EcmaField)
            {
                var deduplicator = new HashSet<string>();

                // Add consistent names for all fields of the type, independent on the order in which
                // they are compiled
                lock (this)
                {
                    foreach (var f in field.OwningType.GetFields())
                    {
                        string name = SanitizeName(f.Name);

                        if (deduplicator.Contains(name))
                        {
                            string nameWithIndex;
                            for (int index = 1; ; index++)
                            {
                                nameWithIndex = name + "_" + index.ToString(CultureInfo.InvariantCulture);
                                if (!deduplicator.Contains(nameWithIndex))
                                    break;
                            }
                            name = nameWithIndex;
                        }
                        deduplicator.Add(name);

                        if (prependTypeName != null)
                            name = prependTypeName + "__" + name;

                        _mangledFieldNames = _mangledFieldNames.Add(f, name);
                    }
                }

                return _mangledFieldNames[field];
            }


            string mangledName = SanitizeName(field.Name);

            if (prependTypeName != null)
                mangledName = prependTypeName + "__" + mangledName;

            lock (this)
            {
                _mangledFieldNames = _mangledFieldNames.Add(field, mangledName);
            }

            return mangledName;
        }
Exemple #10
0
 internal FieldForInstantiatedType(FieldDesc fieldDef, InstantiatedType instantiatedType)
 {
     Debug.Assert(fieldDef.GetTypicalFieldDefinition() == fieldDef);
     _fieldDef         = fieldDef;
     _instantiatedType = instantiatedType;
 }
Exemple #11
0
 public FieldAndOffset(FieldDesc field, int offset)
 {
     Field = field;
     Offset = offset;
 }
Exemple #12
0
 private TypeDesc GetFieldTypeOrPlaceholder(FieldDesc field)
 {
     try
     {
         return field.FieldType;
     }
     catch
     {
         // TODO: For now, catch errors due to missing dependencies
         return _compilation.TypeSystemContext.GetWellKnownType(WellKnownType.Boolean);
     }
 }
Exemple #13
0
        public string GetMangledFieldName(FieldDesc field)
        {
            string mangledName;
            if (_mangledFieldNames.TryGetValue(field, out mangledName))
                return mangledName;

            return ComputeMangledFieldName(field);
        }
Exemple #14
0
 /// <summary>
 /// Gets an object representing the static data for RVA mapped fields from the PE image.
 /// </summary>
 public object GetFieldRvaData(FieldDesc field)
 {
     return _nodeFactory.ReadOnlyDataBlob(NameMangler.GetMangledFieldName(field),
         ((EcmaField)field).GetFieldRvaData(), _typeSystemContext.Target.PointerSize);
 }
Exemple #15
0
 public FieldAndOffset(FieldDesc field, int offset)
 {
     Field  = field;
     Offset = offset;
 }
Exemple #16
0
 private Field HandleFieldDefinition(Cts.FieldDesc field)
 {
     Debug.Assert(field.GetTypicalFieldDefinition() == field);
     Debug.Assert(_policy.GeneratesMetadata(field));
     return((Field)_fields.GetOrCreate(field, _initFieldDef ?? (_initFieldDef = InitializeFieldDefinition)));
 }
Exemple #17
0
 public FieldForInstantiatedTypeKey(FieldDesc fieldDef, InstantiatedType instantiatedType)
 {
     _fieldDef         = fieldDef;
     _instantiatedType = instantiatedType;
 }
Exemple #18
0
 // Abstraction to allow different runtimes to have different policy about which fields are
 // in the GC static region, and which are not
 protected internal virtual bool ComputeHasGCStaticBase(FieldDesc field)
 {
     // Type system contexts that support this need to override this.
     throw new NotSupportedException();
 }
Exemple #19
0
 public FieldDesc GetFieldForInstantiatedType(FieldDesc fieldDef, InstantiatedType instantiatedType)
 {
     return(_fieldForInstantiatedTypes.GetOrCreateValue(new FieldForInstantiatedTypeKey(fieldDef, instantiatedType)));
 }
        protected override bool ComputeHasGCStaticBase(FieldDesc field)
        {
            Debug.Assert(field.IsStatic);

            TypeDesc fieldType = field.FieldType;
            if (fieldType.IsValueType)
                return ((DefType)fieldType).ContainsGCPointers;
            else
                return fieldType.IsGCPointer;

        }
Exemple #21
0
 public FieldAndOffset(FieldDesc field, LayoutInt offset)
 {
     Field  = field;
     Offset = offset;
 }
 internal FieldForInstantiatedType(FieldDesc fieldDef, InstantiatedType instantiatedType)
 {
     _fieldDef = fieldDef;
     _instantiatedType = instantiatedType;
 }
 public bool GeneratesMetadata(FieldDesc fieldDef)
 {
     return true;
 }
Exemple #24
0
        private void AddFieldReference(FieldDesc field)
        {
            if (field.IsStatic)
            {
                var owningType = (MetadataType)field.OwningType;

                Object node;
                if (field.IsThreadStatic)
                {
                    node = _nodeFactory.TypeThreadStaticsSymbol(owningType);
                }
                else
                {
                    if (field.HasGCStaticBase)
                        node = _nodeFactory.TypeGCStaticsSymbol(owningType);
                    else
                        node = _nodeFactory.TypeNonGCStaticsSymbol(owningType);
                }

                // TODO: Remove once the depedencies for static fields are tracked properly
                _writer.GetCppSignatureTypeName(owningType);

                _dependencies.Add(node);
            }
        }
Exemple #25
0
 private void AppendFieldSignature(StringBuilder sb, FieldDesc field)
 {
     this.TypeNameFormatter.AppendNameWithValueClassPrefix(sb, field.FieldType);
     sb.Append(' ');
     AppendOwningType(sb, field.OwningType);
     sb.Append("::");
     sb.Append(field.Name);
 }
Exemple #26
0
 public string GetCppFieldName(FieldDesc field)
 {
     return _compilation.NameMangler.GetMangledFieldName(field).ToString();
 }
Exemple #27
0
 private MemberReference HandleFieldReference(Cts.FieldDesc field)
 {
     return((MemberReference)_fields.GetOrCreate(field, _initFieldRef ?? (_initFieldRef = InitializeFieldReference)));
 }
Exemple #28
0
        public override ComputedInstanceFieldLayout ComputeInstanceLayout(DefType defType, InstanceLayoutKind layoutKind)
        {
            MetadataType type = (MetadataType)defType;

            if (type.IsGenericDefinition)
            {
                ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
            }

            // CLI - Partition 1, section 9.5 - Generic types shall not be marked explicitlayout.
            if (type.HasInstantiation && type.IsExplicitLayout)
            {
                ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadExplicitGeneric, type.GetTypeDefinition());
            }

            // Count the number of instance fields in advance for convenience
            int numInstanceFields = 0;

            foreach (var field in type.GetFields())
            {
                if (field.IsStatic)
                {
                    continue;
                }

                TypeDesc fieldType = field.FieldType;

                // ByRef instance fields are not allowed.
                if (fieldType.IsByRef)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
                }

                // ByRef-like instance fields on non-byref-like types are not allowed.
                if (fieldType.IsByRefLike && !type.IsByRefLike)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
                }

                numInstanceFields++;
            }

            if (type.IsModuleType)
            {
                // This is a global type, it must not have instance fields.
                if (numInstanceFields > 0)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
                }

                // Global types do not do the rest of instance field layout.
                ComputedInstanceFieldLayout result = new ComputedInstanceFieldLayout();
                result.Offsets = Array.Empty <FieldAndOffset>();
                return(result);
            }

            // CLI - Partition 2, section 22.8
            // A type has layout if it is marked SequentialLayout or ExplicitLayout.  If any type within an inheritance chain has layout,
            // then so shall all its base classes, up to the one that descends immediately from System.ValueType (if it exists in the type's
            // hierarchy); otherwise, from System.Object
            // Note: While the CLI isn't clearly worded, the layout needs to be the same for the entire chain.
            // If the current type isn't ValueType or System.Object and has a layout and the parent type isn't
            // ValueType or System.Object then the layout type attributes need to match
            if ((!type.IsValueType && !type.IsObject) &&
                (type.IsSequentialLayout || type.IsExplicitLayout) &&
                (!type.BaseType.IsValueType && !type.BaseType.IsObject))
            {
                MetadataType baseType = type.MetadataBaseType;

                if (type.IsSequentialLayout != baseType.IsSequentialLayout ||
                    type.IsExplicitLayout != baseType.IsExplicitLayout)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadBadFormat, type);
                }
            }

            // Enum types must have a single instance field
            if (type.IsEnum && numInstanceFields != 1)
            {
                ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
            }

            if (type.IsPrimitive)
            {
                // Primitive types are special - they may have a single field of the same type
                // as the type itself. They do not do the rest of instance field layout.
                if (numInstanceFields > 1)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, type);
                }

                SizeAndAlignment instanceByteSizeAndAlignment;
                var sizeAndAlignment = ComputeInstanceSize(
                    type,
                    type.Context.Target.GetWellKnownTypeSize(type),
                    type.Context.Target.GetWellKnownTypeAlignment(type),
                    0,
                    out instanceByteSizeAndAlignment
                    );

                ComputedInstanceFieldLayout result = new ComputedInstanceFieldLayout
                {
                    ByteCountUnaligned = instanceByteSizeAndAlignment.Size,
                    ByteCountAlignment = instanceByteSizeAndAlignment.Alignment,
                    FieldAlignment     = sizeAndAlignment.Alignment,
                    FieldSize          = sizeAndAlignment.Size,
                    LayoutAbiStable    = true
                };

                if (numInstanceFields > 0)
                {
                    FieldDesc instanceField = null;
                    foreach (FieldDesc field in type.GetFields())
                    {
                        if (!field.IsStatic)
                        {
                            Debug.Assert(instanceField == null, "Unexpected extra instance field");
                            instanceField = field;
                        }
                    }

                    Debug.Assert(instanceField != null, "Null instance field");

                    result.Offsets = new FieldAndOffset[] {
                        new FieldAndOffset(instanceField, LayoutInt.Zero)
                    };
                }
                else
                {
                    result.Offsets = Array.Empty <FieldAndOffset>();
                }

                return(result);
            }

            // If the type has layout, read its packing and size info
            // If the type has explicit layout, also read the field offset info
            if (type.IsExplicitLayout || type.IsSequentialLayout)
            {
                if (type.IsEnum)
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadBadFormat, type);
                }

                var layoutMetadata = type.GetClassLayout();

                // If packing is out of range or not a power of two, throw that the size is invalid
                int packing = layoutMetadata.PackingSize;
                if (packing < 0 || packing > 128 || ((packing & (packing - 1)) != 0))
                {
                    ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadBadFormat, type);
                }

                Debug.Assert(layoutMetadata.Offsets == null || layoutMetadata.Offsets.Length == numInstanceFields);
            }

            // At this point all special cases are handled and all inputs validated
            return(ComputeInstanceFieldLayout(type, numInstanceFields));
        }
Exemple #29
0
 private MemberReference HandleFieldReference(Cts.FieldDesc field)
 {
     Debug.Assert(field.GetTypicalFieldDefinition() == field);
     Debug.Assert(!_policy.GeneratesMetadata(field));
     return((MemberReference)_fields.GetOrCreate(field, _initFieldRef ?? (_initFieldRef = InitializeFieldReference)));
 }
        protected internal override bool ComputeHasGCStaticBase(FieldDesc field)
        {
            Debug.Assert(field.IsStatic);

            if (field is NativeLayoutFieldDesc)
            {
                return ((NativeLayoutFieldDesc)field).FieldStorage == Internal.NativeFormat.FieldStorage.GCStatic;
            }

            TypeDesc fieldType = field.FieldType;
            if (fieldType.IsValueType)
            {
                FieldDesc typicalField = field.GetTypicalFieldDefinition();

                if (field != typicalField)
                {
                    if (typicalField.FieldType.IsSignatureVariable)
                        return true;
                }
                if (fieldType.IsEnum || fieldType.IsPrimitive)
                    return false;
                return true;
            }
            else
                return fieldType.IsGCPointer;
        }
Exemple #31
0
        internal RegisteredField GetRegisteredField(FieldDesc field)
        {
            RegisteredField existingRegistration;
            if (_registeredFields.TryGetValue(field, out existingRegistration))
                return existingRegistration;

            RegisteredField registration = new RegisteredField() { Field = field };
            _registeredFields.Add(field, registration);

            GetRegisteredType(field.OwningType);

            return registration;
        }
Exemple #32
0
        public void AddField(FieldDesc field)
        {
            RegisteredField reg = GetRegisteredField(field);
            if (reg.IncludedInCompilation)
                return;
            reg.IncludedInCompilation = true;

            if (_options.IsCppCodeGen)
            {
                // Precreate name to ensure that all types referenced by signatures are present
                GetRegisteredType(field.OwningType);
                GetRegisteredType(field.FieldType);
            }
        }
Exemple #33
0
 public ILToken NewToken(FieldDesc value)
 {
     return NewToken(value, 0x0a000000);
 }
 public bool GeneratesMetadata(FieldDesc fieldDef)
 {
     return GeneratesMetadata((MetadataType)fieldDef.OwningType);
 }
Exemple #35
0
 /// <summary>
 /// Gets an object representing the static data for RVA mapped fields from the PE image.
 /// </summary>
 public ObjectNode GetFieldRvaData(FieldDesc field)
 {
     if (field.GetType() == typeof(Internal.IL.Stubs.PInvokeLazyFixupField))
     {
         var pInvokeFixup = (Internal.IL.Stubs.PInvokeLazyFixupField)field;
         PInvokeMetadata metadata = pInvokeFixup.PInvokeMetadata;
         return _nodeFactory.PInvokeMethodFixup(metadata.Module, metadata.Name);
     }
     else
     {
         return _nodeFactory.ReadOnlyDataBlob(NameMangler.GetMangledFieldName(field),
             ((EcmaField)field).GetFieldRvaData(), _typeSystemContext.Target.PointerSize);
     }
 }
 /// <summary>
 /// Retrieves an existing <see cref="QualifiedField"> or a <see cref="MemberReference"/> record
 /// representing specified field in the metadata writer object model, or creates a new one.
 /// </summary>
 public abstract MetadataRecord HandleQualifiedField(Cts.FieldDesc field);
Exemple #37
0
 public string GetCppStaticFieldName(FieldDesc field)
 {
     TypeDesc type = field.OwningType;
     string typeName = GetCppTypeName(type);
     return typeName.Replace("::", "__") + "__" + _compilation.NameMangler.GetMangledFieldName(field);
 }
Exemple #38
0
 public RvaFieldData(Compilation compilation, FieldDesc field)
 {
     Debug.Assert(field.HasRva);
     Field = field;
     _compilation = compilation;
 }
Exemple #39
0
 private CORINFO_FIELD_STRUCT_* ObjectToHandle(FieldDesc field) { return (CORINFO_FIELD_STRUCT_*)ObjectToHandle((Object)field); }
Exemple #40
0
 public bool GeneratesMetadata(FieldDesc fieldDef)
 {
     if (_fieldGeneratesMetadata != null)
         return _fieldGeneratesMetadata(fieldDef);
     return false;
 }
Exemple #41
0
        private string ComputeMangledFieldName(FieldDesc field)
        {
            string prependTypeName = null;
            if (!_mangleForCplusPlus)
                prependTypeName = GetMangledTypeName(field.OwningType);

            if (field is EcmaField)
            {
                var deduplicator = new HashSet<string>();

                // Add consistent names for all fields of the type, independent on the order in which
                // they are compiled
                lock (this)
                {
                    foreach (var f in field.OwningType.GetFields())
                    {
                        string name = SanitizeName(f.Name);

                        name = DisambiguateName(name, deduplicator);
                        deduplicator.Add(name);

                        if (prependTypeName != null)
                            name = prependTypeName + "__" + name;

                        _mangledFieldNames = _mangledFieldNames.Add(f, name);
                    }
                }

                return _mangledFieldNames[field];
            }


            string mangledName = SanitizeName(field.Name);

            if (prependTypeName != null)
                mangledName = prependTypeName + "__" + mangledName;

            lock (this)
            {
                _mangledFieldNames = _mangledFieldNames.Add(field, mangledName);
            }

            return mangledName;
        }
Exemple #42
0
 public override MetadataRecord HandleQualifiedField(Cts.FieldDesc field)
 {
     return(HandleFieldReference(field));
 }