private DumpTypeRef(DumpTypeRef other, GenericTypeMap genericTypes)
        {
            Namespace         = other.Namespace;
            Name              = other.Name;
            IsGenericInstance = true;
            IsGenericTemplate = false;
            var newGenerics = new List <TypeRef>(other.Generics.Count);

            foreach (var genericParameter in other.Generics)
            {
                if (genericParameter.IsGeneric)
                {
                    newGenerics.Add(genericParameter.MakeGenericInstance(genericTypes));
                }
                else if (genericTypes.TryGetValue(genericParameter, out var genericArgument))
                {
                    newGenerics.Add(genericArgument);
                }
                else
                {
                    throw new UnresolvedTypeException(genericParameter, other);
                }
            }
            Generics = newGenerics;
            OriginalDeclaringType = other.DeclaringType;
            UnNested    = other.UnNested;
            ElementType = other.ElementType;
        }
Esempio n. 2
0
        private TypeRef(
            Resolver resolver,
            AssemblyName assemblyName,
            StrongAssemblyIdentity?assemblyId,
            int metadataToken,
            string fullName,
            TypeRefFlags typeFlags,
            int genericTypeParameterCount,
            ImmutableArray <TypeRef> genericTypeArguments,
            bool shallow,
            ImmutableArray <TypeRef> baseTypes,
            TypeRef?elementTypeRef)
        {
            Requires.NotNull(resolver, nameof(resolver));
            Requires.NotNull(assemblyName, nameof(assemblyName));
            Requires.Argument(((MetadataTokenType)metadataToken & MetadataTokenType.Mask) == MetadataTokenType.Type, "metadataToken", Strings.NotATypeSpec);
            Requires.Argument(metadataToken != (int)MetadataTokenType.Type, "metadataToken", Strings.UnresolvableMetadataToken);
            Requires.NotNullOrEmpty(fullName, nameof(fullName));

            this.resolver                  = resolver;
            this.AssemblyName              = GetNormalizedAssemblyName(assemblyName);
            this.assemblyId                = assemblyId;
            this.MetadataToken             = metadataToken;
            this.FullName                  = fullName;
            this.TypeFlags                 = typeFlags;
            this.GenericTypeParameterCount = genericTypeParameterCount;
            this.GenericTypeArguments      = genericTypeArguments;
            if (!shallow)
            {
                this.baseTypes = baseTypes;
            }

            this.ElementTypeRef = elementTypeRef ?? this;
        }
Esempio n. 3
0
        private TypeRef?Scramble(TypeRef?typeRef)
        {
            if (typeRef == null)
            {
                return(null);
            }

            if (!this.scrambledTypeRefs.TryGetValue(typeRef, out TypeRef? scrambled))
            {
                scrambled = TypeRef.Get(
                    this.resolver,
                    this.Scramble(typeRef.AssemblyId),
                    typeRef.MetadataToken,
                    typeRef.FullName,
                    typeRef.TypeFlags,
                    typeRef.GenericTypeParameterCount,
                    typeRef.GenericTypeArguments.Select(this.Scramble).ToImmutableArray() !,
                    typeRef.IsShallow,
                    typeRef.IsShallow ? ImmutableArray <TypeRef> .Empty : typeRef.BaseTypes.Select(this.Scramble).ToImmutableArray() !,
                    typeRef.ElementTypeRef);
                this.scrambledTypeRefs.Add(typeRef, scrambled);
            }

            return(scrambled);
        }
Esempio n. 4
0
 public ITypeData?Resolve(TypeRef?typeRef)
 {
     if (typeRef is null)
     {
         throw new ArgumentNullException(nameof(typeRef));
     }
     return(Resolve(typeRef.AsDllTypeRef));
 }
 private DumpTypeRef(DumpTypeRef other, string?nameOverride = null)
 {
     Namespace         = other.Namespace;
     Name              = nameOverride ?? other.Name;
     IsGenericInstance = other.IsGenericInstance;
     IsGenericTemplate = other.IsGenericTemplate;
     Generics          = new List <TypeRef>(other.Generics);
     DeclaringType     = other.DeclaringType;
     ElementType       = other.ElementType;
 }
Esempio n. 6
0
            public RuntimeExport(string contractName, TypeRef declaringTypeRef, MemberRef?memberRef, TypeRef?exportedValueTypeRef, IReadOnlyDictionary <string, object?> metadata)
            {
                Requires.NotNullOrEmpty(contractName, nameof(contractName));
                Requires.NotNull(declaringTypeRef, nameof(declaringTypeRef));
                Requires.NotNull(metadata, nameof(metadata));

                this.ContractName         = contractName;
                this.DeclaringTypeRef     = declaringTypeRef;
                this.MemberRef            = memberRef;
                this.exportedValueTypeRef = exportedValueTypeRef;
                this.Metadata             = metadata;
            }
        internal DumpTypeRef(string @namespace, string typeName)
        {
            Namespace = @namespace;

            var GenericTypes = new List <TypeRef>();

            if (typeName.EndsWith(">") && !typeName.StartsWith("<"))
            {
                var ind   = typeName.IndexOf("<");
                var types = typeName.Substring(ind + 1, typeName.Length - ind - 2);
                var spl   = types.Split(new string[] { ", " }, StringSplitOptions.None);
                for (int i = 0; i < spl.Length;)
                {
                    string s        = spl[i];
                    int    unclosed = s.Count(c => c == '<');
                    unclosed -= s.Count(c => c == '>');
                    i++;
                    while (unclosed > 0)
                    {
                        unclosed += spl[i].Count(c => c == '<');
                        unclosed -= spl[i].Count(c => c == '>');
                        s        += ", " + spl[i];
                        i++;
                    }
                    // TODO: if this DumpTypeRef is the This for a DumpTypeData, mark these IsGenericParameter. "out" is not in dump.cs.
                    GenericTypes.Add(new DumpTypeRef(s));
                }
            }
            Generics = GenericTypes;
            // TODO: check that this gives correct results
            if (string.IsNullOrEmpty(@namespace))
            {
                IsGenericInstance = true;
            }
            else
            {
                IsGenericTemplate = true;
            }

            var declInd = typeName.LastIndexOf('.');

            if (declInd != -1)
            {
                // Create a new TypeRef for the declaring type, it should recursively create more declaring types
                OriginalDeclaringType = new DumpTypeRef(typeName.Substring(0, declInd));
                // TODO: need to resolve DeclaringType before this will make sense?
                Namespace = OriginalDeclaringType.Namespace;
            }
            Name = typeName.Replace('.', '/');
            if (IsArray())
            {
                ElementType = new DumpTypeRef(Name[0..^ 2]);
        /// <summary>
        /// Gets a type color
        /// </summary>
        /// <param name="type">Type</param>
        /// <returns></returns>
        public virtual object GetColor(TypeRef?type)
        {
            if (type is null)
            {
                return(BoxedTextColor.Text);
            }

            var td = type.Resolve();

            if (!(td is null))
            {
                return(GetColor(td));
            }

            return(BoxedTextColor.Type);
        }
Esempio n. 9
0
        private bool Equals(TypeRef?other, bool allowMvidMismatch)
        {
            if (other == null)
            {
                return(false);
            }

            // If we ever stop comparing metadata tokens,
            // we would need to compare the other properties that describe this member.
            bool result = this.MetadataToken == other.MetadataToken &&
                          this.AssemblyId.Equals(other.AssemblyId, allowMvidMismatch) &&
                          this.IsArray == other.IsArray &&
                          this.GenericTypeParameterCount == other.GenericTypeParameterCount &&
                          EqualsByValue(this.GenericTypeArguments, other.GenericTypeArguments, allowMvidMismatch) &&
                          this.IsShallow == other.IsShallow;

            return(result);
Esempio n. 10
0
        // TODO: Ensure this behaves as intended for recursive DeclaringTypes (it probably does not)
        public bool Equals(TypeRef?x, TypeRef?y)
        {
            if (x is null || y is null)
            {
                return(x is null == y is null);
            }

            if (x.Namespace != y.Namespace || x.Name != y.Name)
            {
                return(false);
            }
            if (x.IsGeneric && y.IsGeneric)
            {
                // If both x and y are generic
                if (x.IsGenericInstance == y.IsGenericInstance || x.IsGenericTemplate == y.IsGenericTemplate)
                {
                    // If they are both an instance or both a template, return sequence equal
                    return(x.Generics is null ? y.Generics is null : x.Generics.SequenceEqual(y.Generics, this));
                }
                // Otherwise, if one is a template and the other is an instance, if their counts match, consider it good enough.
                return(x.Generics is null ? y.Generics is null : x.Generics.Count == y.Generics.Count);
            }
            return(true);
        }
Esempio n. 11
0
 protected void WriteObject(object?value)
 {
     if (value == null)
     {
         using (this.Trace("Object"))
         {
             this.Write(ObjectType.Null);
         }
     }
     else
     {
         Type valueType = value.GetType();
         using (this.Trace("Object"))
         {
             if (valueType.IsArray)
             {
                 Array array = (Array)value;
                 this.Write(ObjectType.Array);
                 TypeRef?elementTypeRef = TypeRef.Get(valueType.GetElementType(), this.Resolver);
                 this.Write(elementTypeRef);
                 this.Write(array, this.WriteObject);
             }
             else if (valueType == typeof(bool))
             {
                 this.Write((bool)value ? ObjectType.BoolTrue : ObjectType.BoolFalse);
             }
             else if (valueType == typeof(string))
             {
                 this.Write(ObjectType.String);
                 this.Write((string)value);
             }
             else if (valueType == typeof(long))
             {
                 this.Write(ObjectType.Int64);
                 this.writer.Write((long)value);
             }
             else if (valueType == typeof(ulong))
             {
                 this.Write(ObjectType.UInt64);
                 this.writer.Write((ulong)value);
             }
             else if (valueType == typeof(int))
             {
                 this.Write(ObjectType.Int32);
                 this.writer.Write((int)value);
             }
             else if (valueType == typeof(uint))
             {
                 this.Write(ObjectType.UInt32);
                 this.writer.Write((uint)value);
             }
             else if (valueType == typeof(short))
             {
                 this.Write(ObjectType.Int16);
                 this.writer.Write((short)value);
             }
             else if (valueType == typeof(ushort))
             {
                 this.Write(ObjectType.UInt16);
                 this.writer.Write((ushort)value);
             }
             else if (valueType == typeof(byte))
             {
                 this.Write(ObjectType.Byte);
                 this.writer.Write((byte)value);
             }
             else if (valueType == typeof(sbyte))
             {
                 this.Write(ObjectType.SByte);
                 this.writer.Write((sbyte)value);
             }
             else if (valueType == typeof(float))
             {
                 this.Write(ObjectType.Single);
                 this.writer.Write((float)value);
             }
             else if (valueType == typeof(double))
             {
                 this.Write(ObjectType.Double);
                 this.writer.Write((double)value);
             }
             else if (valueType == typeof(char))
             {
                 this.Write(ObjectType.Char);
                 this.writer.Write((char)value);
             }
             else if (valueType == typeof(Guid))
             {
                 this.Write(ObjectType.Guid);
                 this.Write((Guid)value);
             }
             else if (valueType == typeof(CreationPolicy)) // TODO: how do we handle arbitrary value types?
             {
                 this.Write(ObjectType.CreationPolicy);
                 this.writer.Write((byte)(CreationPolicy)value);
             }
             else if (typeof(Type).GetTypeInfo().IsAssignableFrom(valueType))
             {
                 this.Write(ObjectType.Type);
                 this.Write(TypeRef.Get((Type)value, this.Resolver));
             }
             else if (typeof(TypeRef) == valueType)
             {
                 this.Write(ObjectType.TypeRef);
                 this.Write((TypeRef)value);
             }
             else if (typeof(LazyMetadataWrapper.Enum32Substitution) == valueType)
             {
                 var substValue = (LazyMetadataWrapper.Enum32Substitution)value;
                 this.Write(ObjectType.Enum32Substitution);
                 this.Write(substValue.EnumType);
                 this.writer.Write(substValue.RawValue);
             }
             else if (typeof(LazyMetadataWrapper.TypeSubstitution) == valueType)
             {
                 var substValue = (LazyMetadataWrapper.TypeSubstitution)value;
                 this.Write(ObjectType.TypeSubstitution);
                 this.Write(substValue.TypeRef);
             }
             else if (typeof(LazyMetadataWrapper.TypeArraySubstitution) == valueType)
             {
                 var substValue = (LazyMetadataWrapper.TypeArraySubstitution)value;
                 this.Write(ObjectType.TypeArraySubstitution);
                 this.Write(substValue.TypeRefArray, this.Write);
             }
             else
             {
                 Debug.WriteLine("Falling back to binary formatter for value of type: {0}", valueType);
                 this.Write(ObjectType.BinaryFormattedObject);
                 var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                 this.writer.Flush();
                 formatter.Serialize(this.writer.BaseStream, value);
             }
         }
     }
 }
Esempio n. 12
0
        protected object?ReadObject()
        {
            using (this.Trace(nameof(Object)))
            {
                ObjectType objectType = this.ReadObjectType();
                switch (objectType)
                {
                case ObjectType.Null:
                    return(null);

                case ObjectType.Array:
                    Type?elementType = this.ReadTypeRef().Resolve();
                    return(this.ReadArray(this.reader, this.readObjectDelegate, elementType));

                case ObjectType.BoolTrue:
                    return(true);

                case ObjectType.BoolFalse:
                    return(false);

                case ObjectType.Int64:
                    return(this.reader.ReadInt64());

                case ObjectType.UInt64:
                    return(this.reader.ReadUInt64());

                case ObjectType.Int32:
                    return(this.reader.ReadInt32());

                case ObjectType.UInt32:
                    return(this.reader.ReadUInt32());

                case ObjectType.Int16:
                    return(this.reader.ReadInt16());

                case ObjectType.UInt16:
                    return(this.reader.ReadUInt16());

                case ObjectType.Byte:
                    return(this.reader.ReadByte());

                case ObjectType.SByte:
                    return(this.reader.ReadSByte());

                case ObjectType.Single:
                    return(this.reader.ReadSingle());

                case ObjectType.Double:
                    return(this.reader.ReadDouble());

                case ObjectType.String:
                    return(this.ReadString());

                case ObjectType.Char:
                    return(this.reader.ReadChar());

                case ObjectType.Guid:
                    return(this.ReadGuid());

                case ObjectType.CreationPolicy:
                    return((CreationPolicy)this.reader.ReadByte());

                case ObjectType.Type:
                    return(this.ReadTypeRef().Resolve());

                case ObjectType.TypeRef:
                    return(this.ReadTypeRef());

                case ObjectType.Enum32Substitution:
                    TypeRef?enumType = this.ReadTypeRef();
                    int     rawValue = this.reader.ReadInt32();
                    return(new LazyMetadataWrapper.Enum32Substitution(enumType, rawValue));

                case ObjectType.TypeSubstitution:
                    TypeRef?typeRef = this.ReadTypeRef();
                    return(new LazyMetadataWrapper.TypeSubstitution(typeRef));

                case ObjectType.TypeArraySubstitution:
                    IReadOnlyList <TypeRef?> typeRefArray = this.ReadList(this.reader, this.readTypeRefDelegate);
                    return(new LazyMetadataWrapper.TypeArraySubstitution(typeRefArray !, this.Resolver));

                case ObjectType.BinaryFormattedObject:
                    var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    return(formatter.Deserialize(this.reader.BaseStream));

                default:
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Strings.UnsupportedFormat, objectType));
                }
            }
        }
Esempio n. 13
0
        public void Local()
        {
            TypeRef?local = null;

            DoNothing(ref local);
        }
Esempio n. 14
0
 public static TypeRef Get(Resolver resolver, StrongAssemblyIdentity assemblyId, int metadataToken, string fullName, TypeRefFlags typeFlags, int genericTypeParameterCount, ImmutableArray <TypeRef> genericTypeArguments, bool shallow, ImmutableArray <TypeRef> baseTypes, TypeRef?elementTypeRef)
 {
     return(new TypeRef(resolver, assemblyId.Name, assemblyId, metadataToken, fullName, typeFlags, genericTypeParameterCount, genericTypeArguments, shallow, baseTypes, elementTypeRef));
 }
Esempio n. 15
0
 public static Type?Resolve(this TypeRef?typeRef)
 {
     return(typeRef?.ResolvedType);
 }
Esempio n. 16
0
 public static TypeRef Get(Resolver resolver, AssemblyName assemblyName, int metadataToken, string fullName, TypeRefFlags typeFlags, int genericTypeParameterCount, ImmutableArray <TypeRef> genericTypeArguments, bool shallow, ImmutableArray <TypeRef> baseTypes, TypeRef?elementTypeRef)
 {
     Requires.NotNull(resolver, nameof(resolver));
     return(new TypeRef(resolver, assemblyName, null, metadataToken, fullName, typeFlags, genericTypeParameterCount, genericTypeArguments, shallow, baseTypes, elementTypeRef));
 }
Esempio n. 17
0
 public bool Equals(TypeRef?other) => this.Equals(other, allowMvidMismatch: false);