Пример #1
0
        void AddCModReqdSig()
        {
            var type = GetTypeSig();

            if (type != null)
            {
                TypeSig = new CModReqdSig(type.ToTypeDefOrRef(), TypeSig);
            }
        }
Пример #2
0
        internal static TypeSig CopyModifiers(TypeSig from, TypeSig to)
        {
            if (from is NonLeafSig current)
            {
                // There are additional signatures. Store all of the in a stack and process them one by one.
                var sigStack = new Stack <NonLeafSig>();
                while (current != null)
                {
                    sigStack.Push(current);
                    current = current.Next as NonLeafSig;
                }

                // Now process the entries on the stack one by one.
                while (sigStack.Any())
                {
                    current = sigStack.Pop();
                    if (current is SZArraySig arraySig)
                    {
                        to = new ArraySig(to, arraySig.Rank, arraySig.GetSizes(), arraySig.GetLowerBounds());
                    }
                    else if (current is ByRefSig)
                    {
                        to = new ByRefSig(to);
                    }
                    else if (current is CModReqdSig cModReqdSig)
                    {
                        to = new CModReqdSig(cModReqdSig.Modifier, to);
                    }
                    else if (current is CModOptSig cModOptSig)
                    {
                        to = new CModOptSig(cModOptSig.Modifier, to);
                    }
                    else if (current is PtrSig)
                    {
                        to = new PtrSig(to);
                    }
                    else if (current is PinnedSig)
                    {
                        to = new PinnedSig(to);
                    }
                    else
                    {
                        Debug.Fail("Unexpected leaf signature: " + current.GetType().FullName);
                    }
                }
            }

            return(to);
        }
        TypeSig ResolveGenericArgs(TypeSig typeSig)
        {
            if (!recursionCounter.Increment())
            {
                return(null);
            }

            if (ReplaceGenericArg(ref typeSig))
            {
                recursionCounter.Decrement();
                return(typeSig);
            }

            TypeSig result;

            switch (typeSig.ElementType)
            {
            case ElementType.Ptr: result = new PtrSig(ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.ByRef: result = new ByRefSig(ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.Var: result = new GenericVar((typeSig as GenericVar).Number); break;

            case ElementType.ValueArray: result = new ValueArraySig(ResolveGenericArgs(typeSig.Next), (typeSig as ValueArraySig).Size); break;

            case ElementType.SZArray: result = new SZArraySig(ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.MVar: result = new GenericMVar((typeSig as GenericMVar).Number); break;

            case ElementType.CModReqd: result = new CModReqdSig((typeSig as ModifierSig).Modifier, ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.CModOpt: result = new CModOptSig((typeSig as ModifierSig).Modifier, ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.Module: result = new ModuleSig((typeSig as ModuleSig).Index, ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.Pinned: result = new PinnedSig(ResolveGenericArgs(typeSig.Next)); break;

            case ElementType.FnPtr: throw new NotSupportedException("FnPtr is not supported.");

            case ElementType.Array:
                ArraySig    arraySig = (ArraySig)typeSig;
                List <uint> sizes    = new List <uint>(arraySig.Sizes);
                List <int>  lbounds  = new List <int>(arraySig.LowerBounds);
                result = new ArraySig(ResolveGenericArgs(typeSig.Next), arraySig.Rank, sizes, lbounds);
                break;

            case ElementType.GenericInst:
                GenericInstSig gis     = (GenericInstSig)typeSig;
                List <TypeSig> genArgs = new List <TypeSig>(gis.GenericArguments.Count);
                foreach (TypeSig ga in gis.GenericArguments)
                {
                    genArgs.Add(ResolveGenericArgs(ga));
                }
                result = new GenericInstSig(ResolveGenericArgs((TypeSig)gis.GenericType) as ClassOrValueTypeSig, genArgs);
                break;

            default:
                result = typeSig;
                break;
            }

            recursionCounter.Decrement();

            return(result);
        }
        TypeSig create2(TypeSig type)
        {
            if (type == null)
            {
                return(type);
            }
            TypeSig result;

            GenericSig varSig;

            switch (type.ElementType)
            {
            case ElementType.Void:
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
            case ElementType.I8:
            case ElementType.U8:
            case ElementType.R4:
            case ElementType.R8:
            case ElementType.String:
            case ElementType.TypedByRef:
            case ElementType.I:
            case ElementType.U:
            case ElementType.Object:
                result = type;
                break;

            case ElementType.Ptr:
                result = new PtrSig(create2(type.Next));
                break;

            case ElementType.ByRef:
                result = new ByRefSig(create2(type.Next));
                break;

            case ElementType.Array:
                var ary = (ArraySig)type;
                result = new ArraySig(ary.Next, ary.Rank, ary.Sizes, ary.LowerBounds);
                break;

            case ElementType.SZArray:
                result = new SZArraySig(create2(type.Next));
                break;

            case ElementType.Pinned:
                result = new PinnedSig(create2(type.Next));
                break;

            case ElementType.ValueType:
            case ElementType.Class:
                result = type;
                break;

            case ElementType.Var:
                varSig = (GenericSig)type;
                if (genericArgs != null && varSig.Number < (uint)genericArgs.Count)
                {
                    result  = genericArgs[(int)varSig.Number];
                    updated = true;
                }
                else
                {
                    result = type;
                }
                break;

            case ElementType.MVar:
                varSig = (GenericSig)type;
                if (genericMethodArgs != null && varSig.Number < (uint)genericMethodArgs.Count)
                {
                    result  = genericMethodArgs[(int)varSig.Number];
                    updated = true;
                }
                else
                {
                    result = type;
                }
                break;

            case ElementType.GenericInst:
                var gis    = (GenericInstSig)type;
                var newGis = new GenericInstSig(create2(gis.GenericType) as ClassOrValueTypeSig, gis.GenericArguments.Count);
                for (int i = 0; i < gis.GenericArguments.Count; i++)
                {
                    newGis.GenericArguments.Add(create2(gis.GenericArguments[i]));
                }
                result = newGis;
                break;

            case ElementType.ValueArray:
                result = new ValueArraySig(type.Next, ((ValueArraySig)type).Size);
                break;

            case ElementType.Module:
                result = new ModuleSig(((ModuleSig)type).Index, type.Next);
                break;

            case ElementType.CModReqd:
                result = new CModReqdSig(((ModifierSig)type).Modifier, type.Next);
                break;

            case ElementType.CModOpt:
                result = new CModOptSig(((ModifierSig)type).Modifier, type.Next);
                break;

            case ElementType.FnPtr:
                result = new FnPtrSig(create(((FnPtrSig)type).MethodSig));
                break;

            case ElementType.End:
            case ElementType.R:
            case ElementType.Sentinel:
            case ElementType.Internal:
            default:
                result = type;
                break;
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Reads the next type
        /// </summary>
        /// <returns>A new <see cref="TypeSig"/> instance or <c>null</c> if invalid element type</returns>
        TypeSig ReadType()
        {
            if (!recursionCounter.Increment())
            {
                return(null);
            }

            uint    num, i;
            TypeSig nextType, result = null;

            switch ((ElementType)reader.ReadByte())
            {
            case ElementType.Void:          result = corLibTypes.Void; break;

            case ElementType.Boolean:       result = corLibTypes.Boolean; break;

            case ElementType.Char:          result = corLibTypes.Char; break;

            case ElementType.I1:            result = corLibTypes.SByte; break;

            case ElementType.U1:            result = corLibTypes.Byte; break;

            case ElementType.I2:            result = corLibTypes.Int16; break;

            case ElementType.U2:            result = corLibTypes.UInt16; break;

            case ElementType.I4:            result = corLibTypes.Int32; break;

            case ElementType.U4:            result = corLibTypes.UInt32; break;

            case ElementType.I8:            result = corLibTypes.Int64; break;

            case ElementType.U8:            result = corLibTypes.UInt64; break;

            case ElementType.R4:            result = corLibTypes.Single; break;

            case ElementType.R8:            result = corLibTypes.Double; break;

            case ElementType.String:        result = corLibTypes.String; break;

            case ElementType.TypedByRef: result = corLibTypes.TypedReference; break;

            case ElementType.I:                     result = corLibTypes.IntPtr; break;

            case ElementType.U:                     result = corLibTypes.UIntPtr; break;

            case ElementType.Object:        result = corLibTypes.Object; break;

            case ElementType.Ptr:           result = new PtrSig(ReadType()); break;

            case ElementType.ByRef:         result = new ByRefSig(ReadType()); break;

            case ElementType.ValueType:     result = new ValueTypeSig(ReadTypeDefOrRef()); break;

            case ElementType.Class:         result = new ClassSig(ReadTypeDefOrRef()); break;

            case ElementType.FnPtr:         result = new FnPtrSig(ReadSig()); break;

            case ElementType.SZArray:       result = new SZArraySig(ReadType()); break;

            case ElementType.CModReqd:      result = new CModReqdSig(ReadTypeDefOrRef(), ReadType()); break;

            case ElementType.CModOpt:       result = new CModOptSig(ReadTypeDefOrRef(), ReadType()); break;

            case ElementType.Sentinel:      result = new SentinelSig(); break;

            case ElementType.Pinned:        result = new PinnedSig(ReadType()); break;

            case ElementType.Var:
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                result = new GenericVar(num, gpContext.Type);
                break;

            case ElementType.MVar:
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                result = new GenericMVar(num, gpContext.Method);
                break;

            case ElementType.ValueArray:
                nextType = ReadType();
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                result = new ValueArraySig(nextType, num);
                break;

            case ElementType.Module:
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                result = new ModuleSig(num, ReadType());
                break;

            case ElementType.GenericInst:
                nextType = ReadType();
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                var genericInstSig = new GenericInstSig(nextType as ClassOrValueTypeSig, num);
                var args           = genericInstSig.GenericArguments;
                for (i = 0; i < num; i++)
                {
                    args.Add(ReadType());
                }
                result = genericInstSig;
                break;

            case ElementType.Array:
                nextType = ReadType();
                uint rank;
                if (!reader.TryReadCompressedUInt32(out rank))
                {
                    break;
                }
                if (rank > MaxArrayRank)
                {
                    break;
                }
                if (rank == 0)
                {
                    result = new ArraySig(nextType, rank);
                    break;
                }
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                if (num > MaxArrayRank)
                {
                    break;
                }
                var sizes = new List <uint>((int)num);
                for (i = 0; i < num; i++)
                {
                    if (!reader.TryReadCompressedUInt32(out uint size))
                    {
                        goto exit;
                    }
                    sizes.Add(size);
                }
                if (!reader.TryReadCompressedUInt32(out num))
                {
                    break;
                }
                if (num > MaxArrayRank)
                {
                    break;
                }
                var lowerBounds = new List <int>((int)num);
                for (i = 0; i < num; i++)
                {
                    if (!reader.TryReadCompressedInt32(out int size))
                    {
                        goto exit;
                    }
                    lowerBounds.Add(size);
                }
                result = new ArraySig(nextType, rank, sizes, lowerBounds);
                break;

            case ElementType.Internal:
                IntPtr address;
                if (IntPtr.Size == 4)
                {
                    address = new IntPtr(reader.ReadInt32());
                }
                else
                {
                    address = new IntPtr(reader.ReadInt64());
                }
                result = helper.ConvertRTInternalAddress(address);
                break;

            case ElementType.End:
            case ElementType.R:
            default:
                result = null;
                break;
            }
exit:
            recursionCounter.Decrement();
            return(result);
        }
Пример #6
0
        private static TypeSig ReplaceGenericSigImpl(TypeSig tySig, IGenericReplacer replacer)
        {
            if (tySig == null)
            {
                return(null);
            }

            switch (tySig.ElementType)
            {
            case ElementType.Class:
            case ElementType.ValueType:
            case ElementType.TypedByRef:
                return(tySig);

            case ElementType.Ptr:
                return(new PtrSig(ReplaceGenericSigImpl(tySig.Next, replacer)));

            case ElementType.ByRef:
                return(new ByRefSig(ReplaceGenericSigImpl(tySig.Next, replacer)));

            case ElementType.Pinned:
                return(new PinnedSig(ReplaceGenericSigImpl(tySig.Next, replacer)));

            case ElementType.SZArray:
                return(new SZArraySig(ReplaceGenericSigImpl(tySig.Next, replacer)));

            case ElementType.Array:
            {
                ArraySig arySig = (ArraySig)tySig;
                return(new ArraySig(ReplaceGenericSigImpl(arySig.Next, replacer),
                                    arySig.Rank,
                                    arySig.Sizes,
                                    arySig.LowerBounds));
            }

            case ElementType.CModReqd:
            {
                CModReqdSig modreqdSig = (CModReqdSig)tySig;
                return(new CModReqdSig(modreqdSig.Modifier, ReplaceGenericSigImpl(modreqdSig.Next, replacer)));
            }

            case ElementType.CModOpt:
            {
                CModOptSig modoptSig = (CModOptSig)tySig;
                return(new CModOptSig(modoptSig.Modifier, ReplaceGenericSigImpl(modoptSig.Next, replacer)));
            }

            case ElementType.GenericInst:
            {
                GenericInstSig genInstSig = (GenericInstSig)tySig;
                return(new GenericInstSig(genInstSig.GenericType, ReplaceGenericSigListImpl(genInstSig.GenericArguments, replacer)));
            }

            case ElementType.Var:
            {
                GenericVar genVarSig = (GenericVar)tySig;
                TypeSig    result    = replacer.Replace(genVarSig);
                if (result != null)
                {
                    return(result);
                }
                return(genVarSig);
            }

            case ElementType.MVar:
            {
                GenericMVar genMVarSig = (GenericMVar)tySig;
                TypeSig     result     = replacer.Replace(genMVarSig);
                if (result != null)
                {
                    return(result);
                }
                return(genMVarSig);
            }

            default:
                if (tySig is CorLibTypeSig)
                {
                    return(tySig);
                }

                throw new NotSupportedException();
            }
        }
Пример #7
0
        public string GetTypeName(TypeSig tySig, bool hasVolatile = true)
        {
            switch (tySig.ElementType)
            {
            case ElementType.Void:
                return("void");

            case ElementType.Boolean:
                return("uint8_t");

            case ElementType.Char:
                return("uint16_t");

            case ElementType.I1:
                return("int8_t");

            case ElementType.I2:
                return("int16_t");

            case ElementType.I4:
                return("int32_t");

            case ElementType.I8:
                return("int64_t");

            case ElementType.U1:
                return("uint8_t");

            case ElementType.U2:
                return("uint16_t");

            case ElementType.U4:
                return("uint32_t");

            case ElementType.U8:
                return("uint64_t");

            case ElementType.R4:
                return("float");

            case ElementType.R8:
                return("double");

            case ElementType.I:
                return("intptr_t");

            case ElementType.U:
                return("uintptr_t");

            case ElementType.Ptr:
            case ElementType.ByRef:
                return(GetTypeName(tySig.Next) + '*');

            case ElementType.Object:
                return("cls_Object*");

            case ElementType.Class:
            case ElementType.ValueType:
            case ElementType.TypedByRef:
            case ElementType.GenericInst:
            case ElementType.SZArray:
            case ElementType.Array:
            case ElementType.String:
            {
                if (Helper.IsEnumType(tySig, out var enumTySig))
                {
                    return(GetTypeName(enumTySig));
                }

                bool  isValueType = tySig.IsValueType;
                TypeX tyX         = GetTypeBySig(tySig);
                if (tyX == null)
                {
                    if (isValueType)
                    {
                        return("il2cppDummy");
                    }
                    else
                    {
                        return("cls_Object*");
                    }
                }

                return("struct " +
                       GetTypeName(tyX) +
                       (isValueType ? null : "*"));
            }

            case ElementType.Pinned:
                return(GetTypeName(tySig.Next));

            case ElementType.CModReqd:
            {
                CModReqdSig modReqdSig = (CModReqdSig)tySig;
                string      strMod     = null;
                if (hasVolatile &&
                    modReqdSig.Modifier.FullName == "System.Runtime.CompilerServices.IsVolatile")
                {
                    strMod = "volatile ";
                }
                return(strMod + GetTypeName(tySig.Next));
            }

            case ElementType.CModOpt:
            {
                CModOptSig modOptSig = (CModOptSig)tySig;
                return(GetTypeName(tySig.Next));
            }
            }

            throw new NotImplementedException(tySig.ElementType.ToString());
        }
        // Token: 0x06000231 RID: 561 RVA: 0x0001F4AC File Offset: 0x0001D6AC
        private TypeSig ResolveGenericArgs(TypeSig typeSig)
        {
            if (!this.recursionCounter.Increment())
            {
                return(null);
            }
            if (this.ReplaceGenericArg(ref typeSig))
            {
                this.recursionCounter.Decrement();
                return(typeSig);
            }
            ElementType elementType = typeSig.ElementType;
            TypeSig     result;

            switch (elementType)
            {
            case ElementType.Ptr:
                result = new PtrSig(this.ResolveGenericArgs(typeSig.Next));
                goto IL_265;

            case ElementType.ByRef:
                result = new ByRefSig(this.ResolveGenericArgs(typeSig.Next));
                goto IL_265;

            case ElementType.ValueType:
            case ElementType.Class:
            case ElementType.TypedByRef:
            case ElementType.I:
            case ElementType.U:
            case ElementType.R:
            case ElementType.Object:
                break;

            case ElementType.Var:
                result = new GenericVar((typeSig as GenericVar).Number);
                goto IL_265;

            case ElementType.Array:
            {
                ArraySig    arraySig = (ArraySig)typeSig;
                List <uint> sizes    = new List <uint>(arraySig.Sizes);
                List <int>  lbounds  = new List <int>(arraySig.LowerBounds);
                result = new ArraySig(this.ResolveGenericArgs(typeSig.Next), arraySig.Rank, sizes, lbounds);
                goto IL_265;
            }

            case ElementType.GenericInst:
            {
                GenericInstSig gis     = (GenericInstSig)typeSig;
                List <TypeSig> genArgs = new List <TypeSig>(gis.GenericArguments.Count);
                foreach (TypeSig ga in gis.GenericArguments)
                {
                    genArgs.Add(this.ResolveGenericArgs(ga));
                }
                result = new GenericInstSig(this.ResolveGenericArgs(gis.GenericType) as ClassOrValueTypeSig, genArgs);
                goto IL_265;
            }

            case ElementType.ValueArray:
                result = new ValueArraySig(this.ResolveGenericArgs(typeSig.Next), (typeSig as ValueArraySig).Size);
                goto IL_265;

            case ElementType.FnPtr:
                throw new NotSupportedException("FnPtr is not supported.");

            case ElementType.SZArray:
                result = new SZArraySig(this.ResolveGenericArgs(typeSig.Next));
                goto IL_265;

            case ElementType.MVar:
                result = new GenericMVar((typeSig as GenericMVar).Number);
                goto IL_265;

            case ElementType.CModReqd:
                result = new CModReqdSig((typeSig as ModifierSig).Modifier, this.ResolveGenericArgs(typeSig.Next));
                goto IL_265;

            case ElementType.CModOpt:
                result = new CModOptSig((typeSig as ModifierSig).Modifier, this.ResolveGenericArgs(typeSig.Next));
                goto IL_265;

            default:
                if (elementType == ElementType.Module)
                {
                    result = new ModuleSig((typeSig as ModuleSig).Index, this.ResolveGenericArgs(typeSig.Next));
                    goto IL_265;
                }
                if (elementType == ElementType.Pinned)
                {
                    result = new PinnedSig(this.ResolveGenericArgs(typeSig.Next));
                    goto IL_265;
                }
                break;
            }
            result = typeSig;
IL_265:
            this.recursionCounter.Decrement();
            return(result);
        }
Пример #9
0
        bool ReadCore(out TypeSig type, out object value)
        {
            if (!recursionCounter.Increment())
            {
                type  = null;
                value = null;
                return(false);
            }

            bool          res;
            ITypeDefOrRef tdr;
            UTF8String    ns, name;
            var           et = (ElementType)reader.ReadByte();

            switch (et)
            {
            case ElementType.Boolean:
                type  = module.CorLibTypes.Boolean;
                value = reader.ReadBoolean();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.Char:
                type  = module.CorLibTypes.Char;
                value = reader.ReadChar();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.I1:
                type  = module.CorLibTypes.SByte;
                value = reader.ReadSByte();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.U1:
                type  = module.CorLibTypes.Byte;
                value = reader.ReadByte();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.I2:
                type  = module.CorLibTypes.Int16;
                value = reader.ReadInt16();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.U2:
                type  = module.CorLibTypes.UInt16;
                value = reader.ReadUInt16();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.I4:
                type  = module.CorLibTypes.Int32;
                value = reader.ReadInt32();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.U4:
                type  = module.CorLibTypes.UInt32;
                value = reader.ReadUInt32();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.I8:
                type  = module.CorLibTypes.Int64;
                value = reader.ReadInt64();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.U8:
                type  = module.CorLibTypes.UInt64;
                value = reader.ReadUInt64();
                if (reader.Position < reader.Length)
                {
                    type = ReadTypeDefOrRefSig();
                }
                res = true;
                break;

            case ElementType.R4:
                type  = module.CorLibTypes.Single;
                value = reader.ReadSingle();
                res   = true;
                break;

            case ElementType.R8:
                type  = module.CorLibTypes.Double;
                value = reader.ReadDouble();
                res   = true;
                break;

            case ElementType.String:
                type  = module.CorLibTypes.String;
                value = ReadString();
                res   = true;
                break;

            case ElementType.Ptr:
                res = ReadCatch(out type, out value);
                if (res)
                {
                    type = new PtrSig(type);
                }
                break;

            case ElementType.ByRef:
                res = ReadCatch(out type, out value);
                if (res)
                {
                    type = new ByRefSig(type);
                }
                break;

            case ElementType.Object:
                type  = module.CorLibTypes.Object;
                value = null;
                res   = true;
                break;

            case ElementType.ValueType:
                tdr   = ReadTypeDefOrRef();
                type  = tdr.ToTypeSig();
                value = null;
                if (GetName(tdr, out ns, out name) && ns == stringSystem && tdr.DefinitionAssembly.IsCorLib())
                {
                    if (name == stringDecimal)
                    {
                        if (reader.Length - reader.Position != 13)
                        {
                            goto default;
                        }
                        try {
                            byte b = reader.ReadByte();
                            value = new Decimal(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), (b & 0x80) != 0, (byte)(b & 0x7F));
                        }
                        catch {
                            goto default;
                        }
                    }
                    else if (name == stringDateTime)
                    {
                        if (reader.Length - reader.Position != 8)
                        {
                            goto default;
                        }
                        try {
                            value = new DateTime(reader.ReadInt64());
                        }
                        catch {
                            goto default;
                        }
                    }
                }
                if (value == null && reader.Position != reader.Length)
                {
                    value = reader.ReadRemainingBytes();
                }
                res = true;
                break;

            case ElementType.Class:
                type  = new ClassSig(ReadTypeDefOrRef());
                value = reader.Position == reader.Length ? null : reader.ReadRemainingBytes();
                res   = true;
                break;

            case ElementType.CModReqd:
                tdr = ReadTypeDefOrRef();
                res = ReadCatch(out type, out value);
                if (res)
                {
                    type = new CModReqdSig(tdr, type);
                }
                break;

            case ElementType.CModOpt:
                tdr = ReadTypeDefOrRef();
                res = ReadCatch(out type, out value);
                if (res)
                {
                    type = new CModOptSig(tdr, type);
                }
                break;

            case ElementType.Var:
            case ElementType.Array:
            case ElementType.GenericInst:
            case ElementType.TypedByRef:
            case ElementType.I:
            case ElementType.U:
            case ElementType.FnPtr:
            case ElementType.SZArray:
            case ElementType.MVar:
            case ElementType.End:
            case ElementType.Void:
            case ElementType.ValueArray:
            case ElementType.R:
            case ElementType.Internal:
            case ElementType.Module:
            case ElementType.Sentinel:
            case ElementType.Pinned:
            default:
                Debug.Fail("Unsupported element type in LocalConstant sig blob: " + et.ToString());
                res   = false;
                type  = null;
                value = null;
                break;
            }

            recursionCounter.Decrement();
            return(res);
        }
Пример #10
0
        public TypeSig Duplicate(TypeSig typeSig)
        {
            if (!IsDuplicateNeeded(typeSig))
            {
                return(typeSig);
            }

            switch (typeSig.ElementType)
            {
            case ElementType.Class:
            case ElementType.ValueType:
                return(typeSig);

            case ElementType.Ptr:
                return(new PtrSig(Duplicate(typeSig.Next)));

            case ElementType.ByRef:
                return(new ByRefSig(Duplicate(typeSig.Next)));

            case ElementType.SZArray:
                return(new SZArraySig(Duplicate(typeSig.Next)));

            case ElementType.Pinned:
                return(new PinnedSig(Duplicate(typeSig.Next)));

            case ElementType.Array:
            {
                ArraySig arySig = (ArraySig)typeSig;
                return(new ArraySig(Duplicate(arySig.Next), arySig.Rank, Duplicate(arySig.Sizes), Duplicate(arySig.LowerBounds)));
            }

            case ElementType.Var:
            {
                GenericVar genVar = (GenericVar)typeSig;
                TypeSig    result = GenReplacer.Replace(genVar);
                if (result != null)
                {
                    return(result);
                }
                return(new GenericVar(genVar.Number, genVar.OwnerType));
            }

            case ElementType.MVar:
            {
                GenericMVar genMVar = (GenericMVar)typeSig;
                TypeSig     result  = GenReplacer.Replace(genMVar);
                if (result != null)
                {
                    return(result);
                }
                return(new GenericMVar(genMVar.Number, genMVar.OwnerMethod));
            }

            case ElementType.GenericInst:
            {
                GenericInstSig genSig = (GenericInstSig)typeSig;
                return(new GenericInstSig(genSig.GenericType, Duplicate(genSig.GenericArguments)));
            }

            case ElementType.CModReqd:
            {
                CModReqdSig modreq = (CModReqdSig)typeSig;
                return(new CModReqdSig(modreq.Modifier, Duplicate(modreq.Next)));
            }

            case ElementType.CModOpt:
            {
                CModOptSig modopt = (CModOptSig)typeSig;
                return(new CModOptSig(modopt.Modifier, Duplicate(modopt.Next)));
            }

            default:
                if (typeSig is CorLibTypeSig)
                {
                    return(typeSig);
                }

                throw new ArgumentOutOfRangeException("Duplicate TypeSig " + typeSig.GetType().Name);
            }
        }
Пример #11
0
        private static void PrettyName(StringBuilder sb, TypeSig typeSig, bool prettyBaseType)
        {
            if (typeSig == null)
            {
                return;
            }

            switch (typeSig.ElementType)
            {
            case ElementType.Class:
            case ElementType.ValueType:
                sb.Append(SigPrettyName(typeSig, prettyBaseType));
                return;

            case ElementType.Ptr:
                PrettyName(sb, typeSig.Next, prettyBaseType);
                sb.Append('*');
                return;

            case ElementType.ByRef:
                PrettyName(sb, typeSig.Next, prettyBaseType);
                sb.Append('&');
                return;

            case ElementType.SZArray:
                PrettyName(sb, typeSig.Next, prettyBaseType);
                sb.Append("[]");
                return;

            case ElementType.Pinned:
                PrettyName(sb, typeSig.Next, prettyBaseType);
                return;

            case ElementType.Array:
            {
                PrettyName(sb, typeSig.Next, prettyBaseType);
                ArraySig arraySig = (ArraySig)typeSig;
                sb.Append('[');
                uint rank = arraySig.Rank;
                if (rank == 0)
                {
                    throw new NotSupportedException();
                }
                else if (rank == 1)
                {
                    sb.Append('*');
                }
                else
                {
                    for (int i = 0; i < (int)rank; i++)
                    {
                        if (i != 0)
                        {
                            sb.Append(',');
                        }

                        const int  NO_LOWER = int.MinValue;
                        const uint NO_SIZE  = uint.MaxValue;
                        int        lower    = arraySig.LowerBounds.Get(i, NO_LOWER);
                        uint       size     = arraySig.Sizes.Get(i, NO_SIZE);
                        if (lower != NO_LOWER)
                        {
                            sb.Append(lower);
                            sb.Append("..");
                            if (size != NO_SIZE)
                            {
                                sb.Append(lower + (int)size - 1);
                            }
                            else
                            {
                                sb.Append('.');
                            }
                        }
                    }
                }
                sb.Append(']');
                return;
            }

            case ElementType.Var:
            case ElementType.MVar:
            {
                var gs = (GenericSig)typeSig;
                sb.Append(gs.IsMethodVar ? "!!" : "!");
                sb.Append(gs.Number);
                return;
            }

            case ElementType.GenericInst:
            {
                GenericInstSig genSig = (GenericInstSig)typeSig;
                sb.Append(SigPrettyName(genSig.GenericType, prettyBaseType));

                sb.Append('<');
                bool last = false;
                foreach (var arg in genSig.GenericArguments)
                {
                    if (last)
                    {
                        sb.Append(',');
                    }
                    last = true;
                    PrettyName(sb, arg, prettyBaseType);
                }
                sb.Append('>');
                return;
            }

            case ElementType.CModReqd:
            {
                PrettyName(sb, typeSig.Next, prettyBaseType);
                CModReqdSig modreq = (CModReqdSig)typeSig;
                sb.AppendFormat(" modreq({0})", modreq.Modifier.FullName);
                return;
            }

            case ElementType.CModOpt:
            {
                PrettyName(sb, typeSig.Next, prettyBaseType);
                CModOptSig modopt = (CModOptSig)typeSig;
                sb.AppendFormat(" modopt({0})", modopt.Modifier.FullName);
                return;
            }

            default:
                if (typeSig is CorLibTypeSig corTypeSig)
                {
                    sb.Append(SigPrettyName(corTypeSig, prettyBaseType));
                    return;
                }

                throw new ArgumentOutOfRangeException("PrettyName " + typeSig.GetType().Name);
            }
        }