Ejemplo n.º 1
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);
        }
Ejemplo n.º 3
0
 void AddPinnedSig() => TypeSig = new PinnedSig(TypeSig);
        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);
        }
Ejemplo n.º 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);
        }
        // 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);
        }
Ejemplo n.º 7
0
 void AddPinnedSig()
 {
     TypeSig = new PinnedSig(TypeSig);
 }