diff options
author | Michal Strehovský <michals@microsoft.com> | 2015-12-15 04:45:22 +0300 |
---|---|---|
committer | Michal Strehovský <michals@microsoft.com> | 2015-12-15 05:56:59 +0300 |
commit | 40a39c672a29555918d153313ae1332e1333c94e (patch) | |
tree | 064a121c32725e8ad298c90bccaa9fec224f5d13 /src/Common | |
parent | 964952dfbda231ab28e582ce4638488729e94e57 (diff) |
Type system metadata tweaks
- Move name to MetadataType and separate Namespace from Name. Name can
be a MetadataString once we have it.
- Introduce GenerericParameterDesc in the type hierarchy. For now it
doesn't have anything.
- Move ContainingType to MetadataType
- Expose literal fields.
Diffstat (limited to 'src/Common')
14 files changed, 106 insertions, 45 deletions
diff --git a/src/Common/src/TypeSystem/Common/FieldDesc.FieldLayout.cs b/src/Common/src/TypeSystem/Common/FieldDesc.FieldLayout.cs index 823ab7ef9..ce9bf407c 100644 --- a/src/Common/src/TypeSystem/Common/FieldDesc.FieldLayout.cs +++ b/src/Common/src/TypeSystem/Common/FieldDesc.FieldLayout.cs @@ -25,7 +25,7 @@ namespace Internal.TypeSystem if (_offset == FieldAndOffset.InvalidOffset) { - // Must be a field that doesn't participate in layout (literal?) + // Must be a field that doesn't participate in layout (literal or RVA mapped) throw new BadImageFormatException(); } } diff --git a/src/Common/src/TypeSystem/Common/FieldDesc.cs b/src/Common/src/TypeSystem/Common/FieldDesc.cs index 69e7a5c9e..1dda31671 100644 --- a/src/Common/src/TypeSystem/Common/FieldDesc.cs +++ b/src/Common/src/TypeSystem/Common/FieldDesc.cs @@ -66,6 +66,11 @@ namespace Internal.TypeSystem get; } + public abstract bool IsLiteral + { + get; + } + public abstract bool HasCustomAttribute(string attributeNamespace, string attributeName); public virtual FieldDesc GetTypicalFieldDefinition() diff --git a/src/Common/src/TypeSystem/Common/FieldForInstantiatedType.cs b/src/Common/src/TypeSystem/Common/FieldForInstantiatedType.cs index 5e59674f7..87960a01f 100644 --- a/src/Common/src/TypeSystem/Common/FieldForInstantiatedType.cs +++ b/src/Common/src/TypeSystem/Common/FieldForInstantiatedType.cs @@ -80,6 +80,14 @@ namespace Internal.TypeSystem } } + public override bool IsLiteral + { + get + { + return _fieldDef.IsLiteral; + } + } + public override bool HasCustomAttribute(string attributeNamespace, string attributeName) { return _fieldDef.HasCustomAttribute(attributeNamespace, attributeName); diff --git a/src/Common/src/TypeSystem/Common/GenericParameterDesc.cs b/src/Common/src/TypeSystem/Common/GenericParameterDesc.cs new file mode 100644 index 000000000..b52ce0b8b --- /dev/null +++ b/src/Common/src/TypeSystem/Common/GenericParameterDesc.cs @@ -0,0 +1,9 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Internal.TypeSystem +{ + public abstract partial class GenericParameterDesc : TypeDesc + { + } +} diff --git a/src/Common/src/TypeSystem/Common/InstantiatedType.cs b/src/Common/src/TypeSystem/Common/InstantiatedType.cs index 9b7cbb39c..72aca7ee6 100644 --- a/src/Common/src/TypeSystem/Common/InstantiatedType.cs +++ b/src/Common/src/TypeSystem/Common/InstantiatedType.cs @@ -112,6 +112,14 @@ namespace Internal.TypeSystem } } + public override string Namespace + { + get + { + return _typeDef.Namespace; + } + } + public override IEnumerable<MethodDesc> GetMethods() { foreach (var typicalMethodDef in _typeDef.GetMethods()) @@ -282,6 +290,15 @@ namespace Internal.TypeSystem return _typeDef.HasCustomAttribute(attributeNamespace, attributeName); } + public override MetadataType ContainingType + { + get + { + // Return the result from the typical type definition. + return _typeDef.ContainingType; + } + } + public override MetadataType GetNestedType(string name) { // Return the result from the typical type definition. diff --git a/src/Common/src/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs b/src/Common/src/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs index 57fea83e4..919dbb243 100644 --- a/src/Common/src/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs +++ b/src/Common/src/TypeSystem/Common/MetadataFieldLayoutAlgorithm.cs @@ -161,7 +161,7 @@ namespace Internal.TypeSystem foreach (var field in type.GetFields()) { - if (!field.IsStatic || field.HasRva) + if (!field.IsStatic || field.HasRva || field.IsLiteral) continue; numStaticFields++; @@ -186,8 +186,8 @@ namespace Internal.TypeSystem foreach (var field in type.GetFields()) { - // Nonstatic fields and RVA mapped fields don't participate in layout - if (!field.IsStatic || field.HasRva) + // Nonstatic fields, literal fields, and RVA mapped fields don't participate in layout + if (!field.IsStatic || field.HasRva || field.IsLiteral) continue; StaticsBlock* block = diff --git a/src/Common/src/TypeSystem/Common/MetadataType.cs b/src/Common/src/TypeSystem/Common/MetadataType.cs index fab854fad..0acd42d7a 100644 --- a/src/Common/src/TypeSystem/Common/MetadataType.cs +++ b/src/Common/src/TypeSystem/Common/MetadataType.cs @@ -27,6 +27,16 @@ namespace Internal.TypeSystem } /// <summary> + /// Gets the name of the type as represented in the metadata. + /// </summary> + public abstract string Name { get; } + + /// <summary> + /// Gets the namespace of the type. + /// </summary> + public abstract string Namespace { get; } + + /// <summary> /// Gets metadata that controls instance layout of this type. /// </summary> public abstract ClassLayoutMetadata GetClassLayout(); @@ -71,6 +81,11 @@ namespace Internal.TypeSystem public abstract bool HasCustomAttribute(string attributeNamespace, string attributeName); /// <summary> + /// Gets the containing type of this type or null if the type is not nested. + /// </summary> + public abstract MetadataType ContainingType { get; } + + /// <summary> /// Get all of the types nested in this type. /// </summary> public abstract IEnumerable<MetadataType> GetNestedTypes(); diff --git a/src/Common/src/TypeSystem/Common/TypeDesc.cs b/src/Common/src/TypeSystem/Common/TypeDesc.cs index e1954ff29..81204c132 100644 --- a/src/Common/src/TypeSystem/Common/TypeDesc.cs +++ b/src/Common/src/TypeSystem/Common/TypeDesc.cs @@ -359,14 +359,6 @@ namespace Internal.TypeSystem } } - public virtual string Name - { - get - { - return null; - } - } - public virtual bool HasStaticConstructor { get diff --git a/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs b/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs index a270279c3..ccf44431f 100644 --- a/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs +++ b/src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs @@ -214,5 +214,14 @@ namespace Internal.TypeSystem return method; } + + /// <summary> + /// Retrieves the namespace qualified name of a <see cref="MetadataType"/>. + /// </summary> + public static string GetFullName(this MetadataType metadataType) + { + string ns = metadataType.Namespace; + return ns.Length > 0 ? String.Concat(ns, ".", metadataType.Name) : metadataType.Name; + } } } diff --git a/src/Common/src/TypeSystem/Ecma/EcmaField.cs b/src/Common/src/TypeSystem/Ecma/EcmaField.cs index 0e339dc9a..45e6577b6 100644 --- a/src/Common/src/TypeSystem/Ecma/EcmaField.cs +++ b/src/Common/src/TypeSystem/Ecma/EcmaField.cs @@ -213,7 +213,7 @@ namespace Internal.TypeSystem.Ecma } } - public bool IsLiteral + public override bool IsLiteral { get { diff --git a/src/Common/src/TypeSystem/Ecma/EcmaGenericParameter.cs b/src/Common/src/TypeSystem/Ecma/EcmaGenericParameter.cs index 040611a38..389a319f6 100644 --- a/src/Common/src/TypeSystem/Ecma/EcmaGenericParameter.cs +++ b/src/Common/src/TypeSystem/Ecma/EcmaGenericParameter.cs @@ -1,19 +1,14 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System; -using System.Reflection; using System.Reflection.Metadata; -using System.Threading; - -using Internal.TypeSystem; using Internal.NativeFormat; using Debug = System.Diagnostics.Debug; namespace Internal.TypeSystem.Ecma { - public sealed partial class EcmaGenericParameter : TypeDesc + public sealed partial class EcmaGenericParameter : GenericParameterDesc { private EcmaModule _module; private GenericParameterHandle _handle; diff --git a/src/Common/src/TypeSystem/Ecma/EcmaType.cs b/src/Common/src/TypeSystem/Ecma/EcmaType.cs index 4ee82c16d..a577ad42f 100644 --- a/src/Common/src/TypeSystem/Ecma/EcmaType.cs +++ b/src/Common/src/TypeSystem/Ecma/EcmaType.cs @@ -24,7 +24,8 @@ namespace Internal.TypeSystem.Ecma private TypeDefinition _typeDefinition; // Cached values - private string _name; + private string _typeName; + private string _typeNamespace; private TypeDesc[] _genericParameters; private MetadataType _baseType; private int _hashcode; @@ -50,7 +51,7 @@ namespace Internal.TypeSystem.Ecma { return _hashcode; } - int nameHash = TypeHashingAlgorithms.ComputeNameHashCode(Name); + int nameHash = TypeHashingAlgorithms.ComputeNameHashCode(this.GetFullName()); TypeDesc containingType = ContainingType; if (containingType == null) { @@ -221,19 +222,34 @@ namespace Internal.TypeSystem.Ecma private string InitializeName() { var metadataReader = this.MetadataReader; - string typeName = metadataReader.GetString(_typeDefinition.Name); - string typeNamespace = metadataReader.GetString(_typeDefinition.Namespace); - string name = (typeNamespace.Length > 0) ? (typeNamespace + "." + typeName) : typeName; - return (_name = name); + _typeName = metadataReader.GetString(_typeDefinition.Name); + return _typeName; } public override string Name { get { - if (_name == null) + if (_typeName == null) return InitializeName(); - return _name; + return _typeName; + } + } + + private string InitializeNamespace() + { + var metadataReader = this.MetadataReader; + _typeNamespace = metadataReader.GetString(_typeDefinition.Namespace); + return _typeNamespace; + } + + public override string Namespace + { + get + { + if (_typeNamespace == null) + return InitializeNamespace(); + return _typeNamespace; } } @@ -313,10 +329,7 @@ namespace Internal.TypeSystem.Ecma foreach (var handle in _typeDefinition.GetFields()) { var field = (EcmaField)this.Module.GetObject(handle); - - // Literal fields are not interesting for codegen purposes - if (!field.IsLiteral) - yield return field; + yield return field; } } @@ -330,10 +343,7 @@ namespace Internal.TypeSystem.Ecma if (stringComparer.Equals(metadataReader.GetFieldDefinition(handle).Name, name)) { var field = (EcmaField)this.Module.GetObject(handle); - - // Literal fields are not interesting for codegen purposes - if (!field.IsLiteral) - return field; + return field; } } @@ -370,17 +380,15 @@ namespace Internal.TypeSystem.Ecma } } - // - // ContainingType of nested type - // - public TypeDesc ContainingType + public override MetadataType ContainingType { get { - var handle = _typeDefinition.GetDeclaringType(); - if (handle.IsNil) + if (!_typeDefinition.Attributes.IsNested()) return null; - return _module.GetType(handle); + + var handle = _typeDefinition.GetDeclaringType(); + return (MetadataType)_module.GetType(handle); } } @@ -392,7 +400,7 @@ namespace Internal.TypeSystem.Ecma public override string ToString() { - return "[" + Module.GetName().Name + "]" + this.Name; + return "[" + Module.GetName().Name + "]" + this.GetFullName(); } public override ClassLayoutMetadata GetClassLayout() diff --git a/src/Common/src/TypeSystem/Ecma/MetadataExtensions.cs b/src/Common/src/TypeSystem/Ecma/MetadataExtensions.cs index 0b6ced563..0cd43e9d0 100644 --- a/src/Common/src/TypeSystem/Ecma/MetadataExtensions.cs +++ b/src/Common/src/TypeSystem/Ecma/MetadataExtensions.cs @@ -110,7 +110,7 @@ namespace Internal.TypeSystem.Ecma // the other masks in the enum. private const TypeAttributes NestedMask = (TypeAttributes)0x00000006; - private static bool IsNested(TypeAttributes flags) + public static bool IsNested(this TypeAttributes flags) { return (flags & NestedMask) != 0; } diff --git a/src/Common/src/TypeSystem/IL/ILProvider.cs b/src/Common/src/TypeSystem/IL/ILProvider.cs index 880d16ad2..48e0ced4e 100644 --- a/src/Common/src/TypeSystem/IL/ILProvider.cs +++ b/src/Common/src/TypeSystem/IL/ILProvider.cs @@ -29,8 +29,11 @@ namespace Internal.IL // but an intrinsic e.g. recognized by codegen. Debug.Assert(method.IsIntrinsic); + MetadataType owningType = method.OwningType as MetadataType; + if (owningType == null) + return null; - if (method.Name == "UncheckedCast" && method.OwningType.Name == "System.Runtime.CompilerServices.RuntimeHelpers") + if (method.Name == "UncheckedCast" && owningType.Name == "RuntimeHelpers" && owningType.Namespace == "System.Runtime.CompilerServices") { return new ILStubMethodIL(new byte[] { (byte)ILOpcode.ldarg_0, (byte)ILOpcode.ret }, Array.Empty<LocalVariableDefinition>(), null); } |