예제 #1
0
 public static GenericInstanceMethod InflateMethod(GenericContext context, MethodDefinition methodDefinition)
 {
     TypeReference declaringType = methodDefinition.DeclaringType;
     if (declaringType.Resolve().HasGenericParameters)
     {
         declaringType = InflateType(context, methodDefinition.DeclaringType);
     }
     return ConstructGenericMethod(context, declaringType, methodDefinition, (IEnumerable<TypeReference>) methodDefinition.GenericParameters);
 }
예제 #2
0
 private static GenericInstanceType ConstructGenericType(GenericContext context, TypeDefinition typeDefinition, IEnumerable<TypeReference> genericArguments)
 {
     GenericInstanceType type = new GenericInstanceType(typeDefinition);
     foreach (TypeReference reference in genericArguments)
     {
         type.GenericArguments.Add(InflateType(context, reference));
     }
     return type;
 }
예제 #3
0
 public static TypeReference InflateType(GenericContext context, TypeReference typeReference)
 {
     TypeReference reference = InflateTypeWithoutException(context, typeReference);
     if (reference == null)
     {
         throw new Exception();
     }
     return reference;
 }
 internal static void ProcessGenericType(GenericInstanceType type, InflatedCollectionCollector generics, GenericInstanceMethod contextMethod, CollectionMode mode)
 {
     bool flag = generics.TypeDeclarations.Add(type);
     if (((mode != CollectionMode.Types) || flag) && ((mode != CollectionMode.MethodsAndTypes) || generics.TypeMethodDeclarations.Add(type)))
     {
         GenericContext genericContext = new GenericContext(type, contextMethod);
         Unity.Cecil.Visitor.Extensions.Accept(type.ElementType.Resolve(), new GenericContextAwareDeclarationOnlyVisitor(generics, genericContext, CollectionMode.Types));
         foreach (GenericInstanceType type2 in Enumerable.OfType<GenericInstanceType>(type.GenericArguments))
         {
             ProcessGenericType(Inflater.InflateType(genericContext, type2), generics, null, mode);
         }
     }
 }
        void ReadAllFields()
        {
            TypeDefTable tdefTable = m_tableReader.GetTypeDefTable();

            if (!m_tHeap.HasTable(FieldTable.RId))
            {
                m_fields = new FieldDefinition [0];
                return;
            }

            FieldTable fldTable = m_tableReader.GetFieldTable();

            m_fields = new FieldDefinition [fldTable.Rows.Count];

            for (int i = 0; i < m_typeDefs.Length; i++)
            {
                TypeDefinition dec     = m_typeDefs [i];
                GenericContext context = new GenericContext(dec);

                int index = i, next;

                if (index == tdefTable.Rows.Count - 1)
                {
                    next = fldTable.Rows.Count + 1;
                }
                else
                {
                    next = (int)(tdefTable [index + 1]).FieldList;
                }

                for (int j = (int)tdefTable [index].FieldList; j < next; j++)
                {
                    FieldRow        frow = fldTable [j - 1];
                    FieldSig        fsig = m_sigReader.GetFieldSig(frow.Signature);
                    FieldDefinition fdef = new FieldDefinition(
                        m_root.Streams.StringsHeap [frow.Name],
                        this.GetTypeRefFromSig(fsig.Type, context), frow.Flags);
                    fdef.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Field, j - 1);

                    if (fsig.CustomMods.Length > 0)
                    {
                        fdef.FieldType = GetModifierType(fsig.CustomMods, fdef.FieldType);
                    }

                    dec.Fields.Add(fdef);
                    m_fields [j - 1] = fdef;
                }
            }
        }
        void ReadProperties()
        {
            if (!m_tHeap.HasTable(PropertyTable.RId))
            {
                return;
            }

            PropertyTable    propsTable = m_tableReader.GetPropertyTable();
            PropertyMapTable pmapTable  = m_tableReader.GetPropertyMapTable();

            m_properties = new PropertyDefinition [propsTable.Rows.Count];
            for (int i = 0; i < pmapTable.Rows.Count; i++)
            {
                PropertyMapRow pmapRow = pmapTable [i];
                TypeDefinition owner   = GetTypeDefAt(pmapRow.Parent);
                GenericContext context = new GenericContext(owner);

                int start = (int)pmapRow.PropertyList, end;
                if (i < pmapTable.Rows.Count - 1)
                {
                    end = (int)pmapTable [i + 1].PropertyList;
                }
                else
                {
                    end = propsTable.Rows.Count + 1;
                }

                for (int j = start; j < end; j++)
                {
                    PropertyRow        prow = propsTable [j - 1];
                    PropertySig        psig = m_sigReader.GetPropSig(prow.Type);
                    PropertyDefinition pdef = new PropertyDefinition(
                        m_root.Streams.StringsHeap [prow.Name],
                        GetTypeRefFromSig(psig.Type, context),
                        prow.Flags);
                    pdef.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Property, j - 1);

                    if (psig.CustomMods != null && psig.CustomMods.Length > 0)
                    {
                        pdef.PropertyType = GetModifierType(psig.CustomMods, pdef.PropertyType);
                    }

                    owner.Properties.Add(pdef);
                    m_properties [j - 1] = pdef;
                }
            }
        }
        public TypeReference GetTypeSpecAt(uint rid, GenericContext context)
        {
            int           index = (int)rid - 1;
            TypeReference tspec = m_typeSpecs [index];

            if (tspec != null)
            {
                return(tspec);
            }

            TypeSpecTable tsTable = m_tableReader.GetTypeSpecTable();
            TypeSpecRow   tsRow   = tsTable [index];
            TypeSpec      ts      = m_sigReader.GetTypeSpec(tsRow.Signature);

            tspec = GetTypeRefFromSig(ts.Type, context);
            tspec.MetadataToken = MetadataToken.FromMetadataRow(TokenType.TypeSpec, index);
            m_typeSpecs [index] = tspec;

            return(tspec);
        }
        public GenericInstanceMethod GetMethodSpecAt(uint rid, GenericContext context)
        {
            int index = (int)rid - 1;
            GenericInstanceMethod gim = m_methodSpecs [index];

            if (gim != null)
            {
                return(gim);
            }

            MethodSpecTable msTable = m_tableReader.GetMethodSpecTable();
            MethodSpecRow   msRow   = msTable [index];

            MethodSpec sig = m_sigReader.GetMethodSpec(msRow.Instantiation);

            MethodReference meth;

            if (msRow.Method.TokenType == TokenType.Method)
            {
                meth = GetMethodDefAt(msRow.Method.RID);
            }
            else if (msRow.Method.TokenType == TokenType.MemberRef)
            {
                meth = (MethodReference)GetMemberRefAt(msRow.Method.RID, context);
            }
            else
            {
                throw new ReflectionException("Unknown method type for method spec");
            }

            gim = new GenericInstanceMethod(meth);
            foreach (SigType st in sig.Signature.Types)
            {
                gim.GenericArguments.Add(GetTypeRefFromSig(st, context));
            }

            m_methodSpecs [index] = gim;

            return(gim);
        }
        void ReadEvents()
        {
            if (!m_tHeap.HasTable(EventTable.RId))
            {
                return;
            }

            EventTable    evtTable  = m_tableReader.GetEventTable();
            EventMapTable emapTable = m_tableReader.GetEventMapTable();

            m_events = new EventDefinition [evtTable.Rows.Count];
            for (int i = 0; i < emapTable.Rows.Count; i++)
            {
                EventMapRow    emapRow = emapTable [i];
                TypeDefinition owner   = GetTypeDefAt(emapRow.Parent);
                GenericContext context = new GenericContext(owner);

                int start = (int)emapRow.EventList, end;
                if (i < (emapTable.Rows.Count - 1))
                {
                    end = (int)emapTable [i + 1].EventList;
                }
                else
                {
                    end = evtTable.Rows.Count + 1;
                }

                for (int j = start; j < end; j++)
                {
                    EventRow        erow = evtTable [j - 1];
                    EventDefinition edef = new EventDefinition(
                        m_root.Streams.StringsHeap [erow.Name],
                        GetTypeDefOrRef(erow.EventType, context), erow.EventFlags);
                    edef.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Event, j - 1);

                    owner.Events.Add(edef);
                    m_events [j - 1] = edef;
                }
            }
        }
        public TypeReference GetTypeDefOrRef(MetadataToken token, GenericContext context)
        {
            if (token.RID == 0)
            {
                return(null);
            }

            switch (token.TokenType)
            {
            case TokenType.TypeDef:
                return(GetTypeDefAt(token.RID));

            case TokenType.TypeRef:
                return(GetTypeRefAt(token.RID));

            case TokenType.TypeSpec:
                return(GetTypeSpecAt(token.RID, context));

            default:
                return(null);
            }
        }
예제 #11
0
 public static TypeReference InflateTypeWithoutException(GenericContext context, TypeReference typeReference)
 {
     GenericParameter parameter = typeReference as GenericParameter;
     if (parameter != null)
     {
         return ((parameter.Type != GenericParameterType.Type) ? context.Method.GenericArguments[parameter.Position] : context.Type.GenericArguments[parameter.Position]);
     }
     GenericInstanceType genericInstanceType = typeReference as GenericInstanceType;
     if (genericInstanceType != null)
     {
         return InflateType(context, genericInstanceType);
     }
     ArrayType type2 = typeReference as ArrayType;
     if (type2 != null)
     {
         return new ArrayType(InflateType(context, type2.ElementType), type2.Rank);
     }
     PointerType type3 = typeReference as PointerType;
     if (type3 != null)
     {
         return new PointerType(InflateType(context, type3.ElementType));
     }
     return typeReference.Resolve();
 }
예제 #12
0
 private static GenericInstanceMethod ConstructGenericMethod(GenericContext context, TypeReference declaringType, MethodDefinition method, IEnumerable<TypeReference> genericArguments)
 {
     MethodReference owner = new MethodReference(method.Name, method.ReturnType, declaringType) {
         HasThis = method.HasThis
     };
     foreach (GenericParameter parameter in method.GenericParameters)
     {
         owner.GenericParameters.Add(new GenericParameter(parameter.Name, owner));
     }
     foreach (ParameterDefinition definition in method.Parameters)
     {
         owner.Parameters.Add(new ParameterDefinition(definition.Name, definition.Attributes, definition.ParameterType));
     }
     if (owner.Resolve() == null)
     {
         throw new Exception();
     }
     GenericInstanceMethod method2 = new GenericInstanceMethod(owner);
     foreach (TypeReference reference3 in genericArguments)
     {
         method2.GenericArguments.Add(InflateType(context, reference3));
     }
     return method2;
 }
예제 #13
0
        public TypeReference GetTypeSpecAt(uint rid, GenericContext context)
        {
            int index = (int) rid - 1;
            TypeReference tspec = m_typeSpecs [index];
            if (tspec != null)
                return tspec;

            TypeSpecTable tsTable = m_tableReader.GetTypeSpecTable ();
            TypeSpecRow tsRow = tsTable [index];
            TypeSpec ts = m_sigReader.GetTypeSpec (tsRow.Signature);
            tspec = GetTypeRefFromSig (ts.Type, context);
            tspec = GetModifierType (ts.CustomMods, tspec);
            tspec.MetadataToken = MetadataToken.FromMetadataRow (TokenType.TypeSpec, index);
            m_typeSpecs [index] = tspec;

            return tspec;
        }
예제 #14
0
        public TypeReference GetTypeRefFromSig(SigType t, GenericContext context)
        {
            switch (t.ElementType) {
            case ElementType.Class :
                CLASS c = t as CLASS;
                return GetTypeDefOrRef (c.Type, context);
            case ElementType.ValueType :
                VALUETYPE vt = t as VALUETYPE;
                TypeReference vtr = GetTypeDefOrRef (vt.Type, context);
                vtr.IsValueType = true;
                return vtr;
            case ElementType.String :
                return SearchCoreType (Constants.String);
            case ElementType.Object :
                return SearchCoreType (Constants.Object);
            case ElementType.Void :
                return SearchCoreType (Constants.Void);
            case ElementType.Boolean :
                return SearchCoreType (Constants.Boolean);
            case ElementType.Char :
                return SearchCoreType (Constants.Char);
            case ElementType.I1 :
                return SearchCoreType (Constants.SByte);
            case ElementType.U1 :
                return SearchCoreType (Constants.Byte);
            case ElementType.I2 :
                return SearchCoreType (Constants.Int16);
            case ElementType.U2 :
                return SearchCoreType (Constants.UInt16);
            case ElementType.I4 :
                return SearchCoreType (Constants.Int32);
            case ElementType.U4 :
                return SearchCoreType (Constants.UInt32);
            case ElementType.I8 :
                return SearchCoreType (Constants.Int64);
            case ElementType.U8 :
                return SearchCoreType (Constants.UInt64);
            case ElementType.R4 :
                return SearchCoreType (Constants.Single);
            case ElementType.R8 :
                return SearchCoreType (Constants.Double);
            case ElementType.I :
                return SearchCoreType (Constants.IntPtr);
            case ElementType.U :
                return SearchCoreType (Constants.UIntPtr);
            case ElementType.TypedByRef :
                return SearchCoreType (Constants.TypedReference);
            case ElementType.Array :
                ARRAY ary = t as ARRAY;
                return new ArrayType (GetTypeRefFromSig (ary.Type, context), ary.Shape);
            case ElementType.SzArray :
                SZARRAY szary = t as SZARRAY;
                ArrayType at = new ArrayType (GetTypeRefFromSig (szary.Type, context));
                return at;
            case ElementType.Ptr :
                PTR pointer = t as PTR;
                if (pointer.Void)
                    return new PointerType (SearchCoreType (Constants.Void));
                return new PointerType (GetTypeRefFromSig (pointer.PtrType, context));
            case ElementType.FnPtr :
                FNPTR funcptr = t as FNPTR;
                FunctionPointerType fnptr = new FunctionPointerType (funcptr.Method.HasThis, funcptr.Method.ExplicitThis,
                    funcptr.Method.MethCallConv, GetMethodReturnType (funcptr.Method, context));

                for (int i = 0; i < funcptr.Method.ParamCount; i++) {
                    Param p = funcptr.Method.Parameters [i];
                    fnptr.Parameters.Add (BuildParameterDefinition (i, p, context));
                }

                CreateSentinelIfNeeded (fnptr, funcptr.Method);

                return fnptr;
            case ElementType.Var:
                VAR var = t as VAR;
                context.CheckProvider (context.Type, var.Index + 1);

                if (context.Type is GenericInstanceType)
                    return (context.Type as GenericInstanceType).GenericArguments [var.Index];
                else
                    return context.Type.GenericParameters [var.Index];
            case ElementType.MVar:
                MVAR mvar = t as MVAR;
                context.CheckProvider (context.Method, mvar.Index + 1);

                if (context.Method is GenericInstanceMethod)
                    return (context.Method as GenericInstanceMethod).GenericArguments [mvar.Index];
                else
                    return context.Method.GenericParameters [mvar.Index];
            case ElementType.GenericInst:
                GENERICINST ginst = t as GENERICINST;
                GenericInstanceType instance = new GenericInstanceType (GetTypeDefOrRef (ginst.Type, context));
                instance.IsValueType = ginst.ValueType;
                context.CheckProvider (instance.GetOriginalType (), ginst.Signature.Arity);

                for (int i = 0; i < ginst.Signature.Arity; i++)
                    instance.GenericArguments.Add (GetGenericArg (
                        ginst.Signature.Types [i], context));

                return instance;
            default:
                break;
            }
            return null;
        }
        void CompleteMethods()
        {
            TypeDefTable tdefTable = m_tableReader.GetTypeDefTable();

            if (!m_tHeap.HasTable(MethodTable.RId))
            {
                m_meths = new MethodDefinition [0];
                return;
            }

            MethodTable methTable  = m_tableReader.GetMethodTable();
            ParamTable  paramTable = m_tableReader.GetParamTable();

            if (!m_tHeap.HasTable(ParamTable.RId))
            {
                m_parameters = new ParameterDefinition [0];
            }
            else
            {
                m_parameters = new ParameterDefinition [paramTable.Rows.Count];
            }

            for (int i = 0; i < m_typeDefs.Length; i++)
            {
                TypeDefinition dec = m_typeDefs [i];

                int index = i, next;

                if (index == tdefTable.Rows.Count - 1)
                {
                    next = methTable.Rows.Count + 1;
                }
                else
                {
                    next = (int)(tdefTable [index + 1]).MethodList;
                }

                for (int j = (int)tdefTable [index].MethodList; j < next; j++)
                {
                    MethodRow        methRow = methTable [j - 1];
                    MethodDefinition mdef    = m_meths [j - 1];
                    if (mdef.IsConstructor)
                    {
                        dec.Constructors.Add(mdef);
                    }
                    else
                    {
                        dec.Methods.Add(mdef);
                    }
                    GenericContext context = new GenericContext(mdef);

                    MethodDefSig msig = m_sigReader.GetMethodDefSig(methRow.Signature);
                    mdef.HasThis           = msig.HasThis;
                    mdef.ExplicitThis      = msig.ExplicitThis;
                    mdef.CallingConvention = msig.MethCallConv;

                    int prms;
                    if (j == methTable.Rows.Count)
                    {
                        prms = m_parameters.Length + 1;
                    }
                    else
                    {
                        prms = (int)(methTable [j]).ParamList;
                    }

                    ParameterDefinition retparam = null;

                    //TODO: optimize this
                    ParamRow pRow  = null;
                    int      start = (int)methRow.ParamList - 1;

                    if (paramTable != null && start < prms - 1)
                    {
                        pRow = paramTable [start];
                    }

                    if (pRow != null && pRow.Sequence == 0)                       // ret type
                    {
                        retparam = new ParameterDefinition(
                            m_root.Streams.StringsHeap [pRow.Name],
                            0,
                            pRow.Flags,
                            null);
                        retparam.Method      = mdef;
                        m_parameters [start] = retparam;
                        start++;
                    }

                    for (int k = 0; k < msig.ParamCount; k++)
                    {
                        int pointer = start + k;

                        if (paramTable != null && pointer < prms - 1)
                        {
                            pRow = paramTable [pointer];
                        }

                        Param psig = msig.Parameters [k];

                        ParameterDefinition pdef;
                        if (pRow != null)
                        {
                            pdef = BuildParameterDefinition(
                                m_root.Streams.StringsHeap [pRow.Name],
                                pRow.Sequence, pRow.Flags, psig, context);
                            pdef.MetadataToken     = MetadataToken.FromMetadataRow(TokenType.Param, pointer);
                            m_parameters [pointer] = pdef;
                        }
                        else
                        {
                            pdef = BuildParameterDefinition(
                                string.Concat("A_", mdef.IsStatic ? k : k + 1),
                                k + 1, (ParamAttributes)0, psig, context);
                        }

                        pdef.Method = mdef;
                        mdef.Parameters.Add(pdef);
                    }

                    mdef.ReturnType = GetMethodReturnType(msig, context);
                    MethodReturnType mrt = mdef.ReturnType as MethodReturnType;
                    mrt.Method = mdef;
                    if (retparam != null)
                    {
                        mrt.Parameter = retparam;
                        mrt.Parameter.ParameterType = mrt.ReturnType;
                    }
                }
            }

            uint eprid = CodeReader.GetRid((int)m_reader.Image.CLIHeader.EntryPointToken);

            if (eprid > 0 && eprid <= m_meths.Length)
            {
                m_module.Assembly.EntryPoint = GetMethodDefAt(eprid);
            }
        }
예제 #16
0
        void ReadCilBody(MethodBody body, BinaryReader br, out IDictionary instructions)
        {
            long start = br.BaseStream.Position, offset;
            Instruction last = null;
            InstructionCollection code = body.Instructions;
            instructions = new Hashtable ();
            GenericContext context = new GenericContext (body.Method);

            while (br.BaseStream.Position < start + body.CodeSize) {
                OpCode op;
                offset = br.BaseStream.Position - start;
                int cursor = br.ReadByte ();
                if (cursor == 0xfe)
                    op = OpCodes.Cache.Instance.TwoBytesOpCode [br.ReadByte ()];
                else
                    op = OpCodes.Cache.Instance.OneByteOpCode [cursor];

                Instruction instr = new Instruction ((int) offset, op);
                switch (op.OperandType) {
                case OperandType.InlineNone :
                    break;
                case OperandType.InlineSwitch :
                    uint length = br.ReadUInt32 ();
                    int [] branches = new int [length];
                    int [] buf = new int [length];
                    for (int i = 0; i < length; i++)
                        buf [i] = br.ReadInt32 ();
                    for (int i = 0; i < length; i++)
                        branches [i] = Convert.ToInt32 (br.BaseStream.Position - start + buf [i]);
                    instr.Operand = branches;
                    break;
                case OperandType.ShortInlineBrTarget :
                    sbyte sbrtgt = br.ReadSByte ();
                    instr.Operand = Convert.ToInt32 (br.BaseStream.Position - start + sbrtgt);
                    break;
                case OperandType.InlineBrTarget :
                    int brtgt = br.ReadInt32 ();
                    instr.Operand = Convert.ToInt32 (br.BaseStream.Position - start + brtgt);
                    break;
                case OperandType.ShortInlineI :
                    if (op == OpCodes.Ldc_I4_S)
                        instr.Operand = br.ReadSByte ();
                    else
                        instr.Operand = br.ReadByte ();
                    break;
                case OperandType.ShortInlineVar :
                    instr.Operand = body.Variables [(int) br.ReadByte ()];
                    break;
                case OperandType.ShortInlineParam :
                    instr.Operand = GetParameter (body, (int) br.ReadByte ());
                    break;
                case OperandType.InlineSig :
                    instr.Operand = GetCallSiteAt (br.ReadInt32 (), context);
                    break;
                case OperandType.InlineI :
                    instr.Operand = br.ReadInt32 ();
                    break;
                case OperandType.InlineVar :
                    instr.Operand = body.Variables [(int) br.ReadInt16 ()];
                    break;
                case OperandType.InlineParam :
                    instr.Operand = GetParameter (body, (int) br.ReadInt16 ());
                    break;
                case OperandType.InlineI8 :
                    instr.Operand = br.ReadInt64 ();
                    break;
                case OperandType.ShortInlineR :
                    instr.Operand = br.ReadSingle ();
                    break;
                case OperandType.InlineR :
                    instr.Operand = br.ReadDouble ();
                    break;
                case OperandType.InlineString :
                    instr.Operand = m_root.Streams.UserStringsHeap [GetRid (br.ReadInt32 ())];
                    break;
                case OperandType.InlineField :
                    int field = br.ReadInt32 ();
                    if (IsToken (field, TokenType.Field))
                        instr.Operand = m_reflectReader.GetFieldDefAt (GetRid (field));
                    else if (IsToken (field, TokenType.MemberRef))
                        instr.Operand = m_reflectReader.GetMemberRefAt (GetRid (field), context);
                    else
                        throw new ReflectionException ("Wrong token for InlineField Operand: {0}", field.ToString ("x8"));
                    break;
                case OperandType.InlineMethod :
                    int meth = br.ReadInt32 ();
                    if (IsToken (meth, TokenType.Method))
                        instr.Operand = m_reflectReader.GetMethodDefAt (GetRid (meth));
                    else if (IsToken (meth, TokenType.MemberRef))
                        instr.Operand = m_reflectReader.GetMemberRefAt (GetRid (meth), context);
                    else if (IsToken (meth, TokenType.MethodSpec))
                        instr.Operand = m_reflectReader.GetMethodSpecAt (GetRid (meth), context);
                    else
                        throw new ReflectionException ("Wrong token for InlineMethod Operand: {0}", meth.ToString ("x8"));
                    break;
                case OperandType.InlineType :
                    int type = br.ReadInt32 ();
                    if (IsToken (type, TokenType.TypeDef))
                        instr.Operand = m_reflectReader.GetTypeDefAt (GetRid (type));
                    else if (IsToken (type, TokenType.TypeRef))
                        instr.Operand = m_reflectReader.GetTypeRefAt (GetRid (type));
                    else if (IsToken (type, TokenType.TypeSpec))
                        instr.Operand = m_reflectReader.GetTypeSpecAt (GetRid (type), context);
                    else
                        throw new ReflectionException ("Wrong token for InlineType Operand: {0}", type.ToString ("x8"));
                    break;
                case OperandType.InlineTok :
                    int token = br.ReadInt32 ();
                    if (IsToken (token, TokenType.TypeDef))
                        instr.Operand = m_reflectReader.GetTypeDefAt (GetRid (token));
                    else if (IsToken (token, TokenType.TypeRef))
                        instr.Operand = m_reflectReader.GetTypeRefAt (GetRid (token));
                    else if (IsToken (token, TokenType.TypeSpec))
                        instr.Operand = m_reflectReader.GetTypeSpecAt (GetRid (token), context);
                    else if (IsToken (token, TokenType.Field))
                        instr.Operand = m_reflectReader.GetFieldDefAt (GetRid (token));
                    else if (IsToken (token, TokenType.Method))
                        instr.Operand = m_reflectReader.GetMethodDefAt (GetRid (token));
                    else if (IsToken (token, TokenType.MethodSpec))
                        instr.Operand = m_reflectReader.GetMethodSpecAt (GetRid (token), context);
                    else if (IsToken (token, TokenType.MemberRef))
                        instr.Operand = m_reflectReader.GetMemberRefAt (GetRid (token), context);
                    else
                        throw new ReflectionException ("Wrong token following ldtoken: {0}", token.ToString ("x8"));
                    break;
                }

                instructions.Add (instr.Offset, instr);

                if (last != null) {
                    last.Next = instr;
                    instr.Previous = last;
                }

                last = instr;

                code.Add (instr);
            }

            // resolve branches
            foreach (Instruction i in code) {
                switch (i.OpCode.OperandType) {
                case OperandType.ShortInlineBrTarget:
                case OperandType.InlineBrTarget:
                    i.Operand = instructions [(int) i.Operand];
                    break;
                case OperandType.InlineSwitch:
                    int [] lbls = (int []) i.Operand;
                    Instruction [] instrs = new Instruction [lbls.Length];
                    for (int j = 0; j < lbls.Length; j++)
                        instrs [j] = instructions [lbls [j]] as Instruction;
                    i.Operand = instrs;
                    break;
                }
            }
        }
예제 #17
0
 public static GenericInstanceType InflateType(GenericContext context, TypeDefinition typeDefinition)
 {
     return ConstructGenericType(context, typeDefinition, (IEnumerable<TypeReference>) typeDefinition.GenericParameters);
 }
예제 #18
0
		void ReadProperties ()
		{
			if (!m_tHeap.HasTable (PropertyTable.RId)) {
				m_properties = new PropertyDefinition [0];
				return;
			}

			PropertyTable propsTable = m_tableReader.GetPropertyTable ();
			PropertyMapTable pmapTable = m_tableReader.GetPropertyMapTable ();
			m_properties = new PropertyDefinition [propsTable.Rows.Count];
			for (int i = 0; i < pmapTable.Rows.Count; i++) {
				PropertyMapRow pmapRow = pmapTable [i];
				if (pmapRow.Parent == 0)
					continue;

				TypeDefinition owner = GetTypeDefAt (pmapRow.Parent);

				GenericContext context = new GenericContext (owner);

				int start = (int) pmapRow.PropertyList, last = propsTable.Rows.Count + 1, end;
				if (i < pmapTable.Rows.Count - 1)
					end = (int) pmapTable [i + 1].PropertyList;
				else
					end = last;

				if (end > last)
					end = last;

				for (int j = start; j < end; j++) {
					PropertyRow prow = propsTable [j - 1];
					PropertySig psig = m_sigReader.GetPropSig (prow.Type);
					PropertyDefinition pdef = new PropertyDefinition (
						m_root.Streams.StringsHeap [prow.Name],
						GetTypeRefFromSig (psig.Type, context),
					prow.Flags);
					pdef.MetadataToken = MetadataToken.FromMetadataRow (TokenType.Property, j - 1);

					pdef.PropertyType = GetModifierType (psig.CustomMods, pdef.PropertyType);

					if (!IsDeleted (pdef))
						owner.Properties.Add (pdef);

					m_properties [j - 1] = pdef;
				}
			}
		}
        public TypeReference GetTypeRefFromSig(SigType t, GenericContext context)
        {
            switch (t.ElementType)
            {
            case ElementType.Class:
                CLASS c = t as CLASS;
                return(GetTypeDefOrRef(c.Type, context));

            case ElementType.ValueType:
                VALUETYPE     vt  = t as VALUETYPE;
                TypeReference vtr = GetTypeDefOrRef(vt.Type, context);
                vtr.IsValueType = true;
                return(vtr);

            case ElementType.String:
                return(SearchCoreType(Constants.String));

            case ElementType.Object:
                return(SearchCoreType(Constants.Object));

            case ElementType.Void:
                return(SearchCoreType(Constants.Void));

            case ElementType.Boolean:
                return(SearchCoreType(Constants.Boolean));

            case ElementType.Char:
                return(SearchCoreType(Constants.Char));

            case ElementType.I1:
                return(SearchCoreType(Constants.SByte));

            case ElementType.U1:
                return(SearchCoreType(Constants.Byte));

            case ElementType.I2:
                return(SearchCoreType(Constants.Int16));

            case ElementType.U2:
                return(SearchCoreType(Constants.UInt16));

            case ElementType.I4:
                return(SearchCoreType(Constants.Int32));

            case ElementType.U4:
                return(SearchCoreType(Constants.UInt32));

            case ElementType.I8:
                return(SearchCoreType(Constants.Int64));

            case ElementType.U8:
                return(SearchCoreType(Constants.UInt64));

            case ElementType.R4:
                return(SearchCoreType(Constants.Single));

            case ElementType.R8:
                return(SearchCoreType(Constants.Double));

            case ElementType.I:
                return(SearchCoreType(Constants.IntPtr));

            case ElementType.U:
                return(SearchCoreType(Constants.UIntPtr));

            case ElementType.TypedByRef:
                return(SearchCoreType(Constants.TypedReference));

            case ElementType.Array:
                ARRAY ary = t as ARRAY;
                return(new ArrayType(GetTypeRefFromSig(ary.Type, context), ary.Shape));

            case ElementType.SzArray:
                SZARRAY   szary = t as SZARRAY;
                ArrayType at    = new ArrayType(GetTypeRefFromSig(szary.Type, context));
                return(at);

            case ElementType.Ptr:
                PTR pointer = t as PTR;
                if (pointer.Void)
                {
                    return(new PointerType(SearchCoreType(Constants.Void)));
                }
                return(new PointerType(GetTypeRefFromSig(pointer.PtrType, context)));

            case ElementType.FnPtr:
                FNPTR funcptr             = t as FNPTR;
                FunctionPointerType fnptr = new FunctionPointerType(funcptr.Method.HasThis, funcptr.Method.ExplicitThis,
                                                                    funcptr.Method.MethCallConv, GetMethodReturnType(funcptr.Method, context));

                for (int i = 0; i < funcptr.Method.ParamCount; i++)
                {
                    Param p = funcptr.Method.Parameters [i];
                    fnptr.Parameters.Add(BuildParameterDefinition(
                                             string.Concat("A_", i),
                                             i, (ParamAttributes)0,
                                             p, context));
                }
                return(fnptr);

            case ElementType.Var:
                VAR var = t as VAR;
                if (context.AllowCreation)
                {
                    CheckGenericParameters(context, var);
                }

                if (context.Type is GenericInstanceType)
                {
                    return((context.Type as GenericInstanceType).GenericArguments [var.Index]);
                }
                else
                {
                    return(context.Type.GenericParameters [var.Index]);
                }

            case ElementType.MVar:
                MVAR mvar = t as MVAR;
                if (context.Method is GenericInstanceMethod)
                {
                    return((context.Method as GenericInstanceMethod).GenericArguments [mvar.Index]);
                }
                else
                {
                    return(context.Method.GenericParameters [mvar.Index]);
                }

            case ElementType.GenericInst:
                GENERICINST         ginst    = t as GENERICINST;
                GenericInstanceType instance = new GenericInstanceType(GetTypeDefOrRef(ginst.Type, context));
                instance.IsValueType = ginst.ValueType;

                for (int i = 0; i < ginst.Signature.Arity; i++)
                {
                    instance.GenericArguments.Add(GetTypeRefFromSig(
                                                      ginst.Signature.Types [i], context));
                }

                return(instance);

            default:
                break;
            }
            return(null);
        }
예제 #20
0
 TypeReference GetGenericArg(GenericArg arg, GenericContext context)
 {
     TypeReference type = GetTypeRefFromSig (arg.Type, context);
     type = GetModifierType (arg.CustomMods, type);
     return type;
 }
예제 #21
0
        void ReadAllFields()
        {
            TypeDefTable tdefTable = m_tableReader.GetTypeDefTable ();

            if (!m_tHeap.HasTable(FieldTable.RId)) {
                m_fields = new FieldDefinition [0];
                return;
            }

            FieldTable fldTable = m_tableReader.GetFieldTable ();
            m_fields = new FieldDefinition [fldTable.Rows.Count];

            for (int i = 0; i < m_typeDefs.Length; i++) {
                TypeDefinition dec = m_typeDefs [i];
                GenericContext context = new GenericContext (dec);

                int index = i, next;

                if (index == tdefTable.Rows.Count - 1)
                    next = fldTable.Rows.Count + 1;
                else
                    next = (int) (tdefTable [index + 1]).FieldList;

                for (int j = (int) tdefTable [index].FieldList; j < next; j++) {
                    FieldRow frow = fldTable [j - 1];
                    FieldSig fsig = m_sigReader.GetFieldSig (frow.Signature);

                    FieldDefinition fdef = new FieldDefinition (
                        m_root.Streams.StringsHeap [frow.Name],
                        GetTypeRefFromSig (fsig.Type, context), frow.Flags);
                    fdef.MetadataToken = MetadataToken.FromMetadataRow (TokenType.Field, j - 1);

                    if (fsig.CustomMods.Length > 0)
                        fdef.FieldType = GetModifierType (fsig.CustomMods, fdef.FieldType);

                    if (!IsDeleted (fdef))
                        dec.Fields.Add (fdef);

                    m_fields [j - 1] = fdef;
                }
            }
        }
예제 #22
0
        MethodReference CreateMethodReferenceFromSig(MethodSig ms, string name, TypeReference declaringType, GenericContext context)
        {
            MethodReference methref = new MethodReference (
                name, ms.HasThis, ms.ExplicitThis, ms.MethCallConv);
            methref.DeclaringType = declaringType;

            if (ms is MethodDefSig) {
                int arity = (ms as MethodDefSig).GenericParameterCount;
                for (int i = 0; i < arity; i++)
                    methref.GenericParameters.Add (new GenericParameter (i, methref));
            }

            if (methref.GenericParameters.Count > 0)
                context.Method = methref;

            methref.ReturnType = GetMethodReturnType (ms, context);

            methref.ReturnType.Method = methref;
            for (int j = 0; j < ms.ParamCount; j++) {
                Param p = ms.Parameters [j];
                ParameterDefinition pdef = BuildParameterDefinition (j, p, context);
                pdef.Method = methref;
                methref.Parameters.Add (pdef);
            }

            CreateSentinelIfNeeded (methref, ms);

            return methref;
        }
예제 #23
0
        void CompleteParameter(ParameterDefinition parameter, Param signature, GenericContext context)
        {
            TypeReference paramType;

            if (signature.ByRef)
                paramType = new ReferenceType (GetTypeRefFromSig (signature.Type, context));
            else if (signature.TypedByRef)
                paramType = SearchCoreType (Constants.TypedReference);
            else
                paramType = GetTypeRefFromSig (signature.Type, context);

            paramType = GetModifierType (signature.CustomMods, paramType);

            parameter.ParameterType = paramType;
        }
        public ParameterDefinition BuildParameterDefinition(string name, int sequence,
                                                            ParamAttributes attrs, Param psig, GenericContext context)
        {
            ParameterDefinition ret       = new ParameterDefinition(name, sequence, attrs, null);
            TypeReference       paramType = null;

            if (psig.ByRef)
            {
                paramType = new ReferenceType(GetTypeRefFromSig(psig.Type, context));
            }
            else if (psig.TypedByRef)
            {
                paramType = SearchCoreType(Constants.TypedReference);
            }
            else
            {
                paramType = GetTypeRefFromSig(psig.Type, context);
            }

            if (psig.CustomMods.Length > 0)
            {
                paramType = GetModifierType(psig.CustomMods, paramType);
            }

            ret.ParameterType = paramType;

            return(ret);
        }
예제 #25
0
        void CompleteMethods()
        {
            TypeDefTable tdefTable = m_tableReader.GetTypeDefTable ();

            if (!m_tHeap.HasTable (MethodTable.RId)) {
                m_meths = new MethodDefinition [0];
                return;
            }

            MethodTable methTable = m_tableReader.GetMethodTable ();
            ParamTable paramTable = m_tableReader.GetParamTable ();
            if (!m_tHeap.HasTable (ParamTable.RId))
                m_parameters = new ParameterDefinition [0];
            else
                m_parameters = new ParameterDefinition [paramTable.Rows.Count];

            for (int i = 0; i < m_typeDefs.Length; i++) {
                TypeDefinition dec = m_typeDefs [i];

                int index = i, next;

                if (index == tdefTable.Rows.Count - 1)
                    next = methTable.Rows.Count + 1;
                else
                    next = (int) (tdefTable [index + 1]).MethodList;

                for (int j = (int) tdefTable [index].MethodList; j < next; j++) {
                    MethodRow methRow = methTable [j - 1];
                    MethodDefinition mdef = m_meths [j - 1];

                    if (!IsDeleted (mdef)) {
                        if (mdef.IsConstructor)
                            dec.Constructors.Add (mdef);
                        else
                            dec.Methods.Add (mdef);
                    }

                    GenericContext context = new GenericContext (mdef);

                    MethodDefSig msig = m_sigReader.GetMethodDefSig (methRow.Signature);
                    mdef.HasThis = msig.HasThis;
                    mdef.ExplicitThis = msig.ExplicitThis;
                    mdef.CallingConvention = msig.MethCallConv;

                    int prms;
                    if (j == methTable.Rows.Count)
                        prms = m_parameters.Length + 1;
                    else
                        prms = (int) (methTable [j]).ParamList;

                    ParameterDefinition retparam = null;

                    //TODO: optimize this
                    int start = (int) methRow.ParamList - 1;

                    if (paramTable != null && start < prms - 1) {

                        ParamRow pRetRow = paramTable [start];

                        if (pRetRow != null && pRetRow.Sequence == 0) { // ret type

                            retparam = new ParameterDefinition (
                                m_root.Streams.StringsHeap [pRetRow.Name],
                                0,
                                pRetRow.Flags,
                                null);

                            retparam.Method = mdef;
                            m_parameters [start] = retparam;
                            start++;
                        }
                    }

                    for (int k = 0; k < msig.ParamCount; k++) {

                        int pointer = start + k;

                        ParamRow pRow = null;

                        if (paramTable != null && pointer < prms - 1)
                            pRow = paramTable [pointer];

                        Param psig = msig.Parameters [k];

                        ParameterDefinition pdef;
                        if (pRow != null) {
                            pdef = BuildParameterDefinition (
                                m_root.Streams.StringsHeap [pRow.Name],
                                pRow.Sequence, pRow.Flags, psig, context);
                            pdef.MetadataToken = MetadataToken.FromMetadataRow (TokenType.Param, pointer);
                            m_parameters [pointer] = pdef;
                        } else
                            pdef = BuildParameterDefinition (k + 1, psig, context);

                        pdef.Method = mdef;
                        mdef.Parameters.Add (pdef);
                    }

                    mdef.ReturnType = GetMethodReturnType (msig, context);
                    MethodReturnType mrt = mdef.ReturnType;
                    mrt.Method = mdef;
                    if (retparam != null) {
                        mrt.Parameter = retparam;
                        mrt.Parameter.ParameterType = mrt.ReturnType;
                    }
                }
            }

            uint eprid = CodeReader.GetRid ((int) m_reader.Image.CLIHeader.EntryPointToken);
            if (eprid > 0 && eprid <= m_meths.Length)
                m_module.Assembly.EntryPoint = GetMethodDefAt (eprid);
        }
		void ReadCilBody (MethodBody body, BinaryReader br)
		{
			long start = br.BaseStream.Position;
			Instruction last = null;
			m_instructions.Clear();
			InstructionCollection code = body.Instructions;
			GenericContext context = new GenericContext (body.Method);

			while (br.BaseStream.Position < start + body.CodeSize) {
				OpCode op;
				long offset = br.BaseStream.Position - start;
				int cursor = br.ReadByte ();
				if (cursor == 0xfe)
					op = OpCodes.TwoBytesOpCode [br.ReadByte ()];
				else
					op = OpCodes.OneByteOpCode [cursor];

				Instruction instr = new Instruction ((int) offset, op);
				switch (op.OperandType) {
				case OperandType.InlineNone :
					break;
				case OperandType.InlineSwitch :
					uint length = br.ReadUInt32 ();
					int [] branches = new int [length];
					int [] buf = new int [length];
					for (int i = 0; i < length; i++)
						buf [i] = br.ReadInt32 ();
					for (int i = 0; i < length; i++)
						branches [i] = Convert.ToInt32 (br.BaseStream.Position - start + buf [i]);
					instr.Operand = branches;
					break;
				case OperandType.ShortInlineBrTarget :
					sbyte sbrtgt = br.ReadSByte ();
					instr.Operand = Convert.ToInt32 (br.BaseStream.Position - start + sbrtgt);
					break;
				case OperandType.InlineBrTarget :
					int brtgt = br.ReadInt32 ();
					instr.Operand = Convert.ToInt32 (br.BaseStream.Position - start + brtgt);
					break;
				case OperandType.ShortInlineI :
					if (op == OpCodes.Ldc_I4_S)
						instr.Operand = br.ReadSByte ();
					else
						instr.Operand = br.ReadByte ();
					break;
				case OperandType.ShortInlineVar :
					instr.Operand = GetVariable (body, br.ReadByte ());
					break;
				case OperandType.ShortInlineParam :
					instr.Operand = GetParameter (body, br.ReadByte ());
					break;
				case OperandType.InlineSig :
					instr.Operand = GetCallSiteAt (br.ReadInt32 (), context);
					break;
				case OperandType.InlineI :
					instr.Operand = br.ReadInt32 ();
					break;
				case OperandType.InlineVar :
					instr.Operand = GetVariable (body, br.ReadInt16 ());
					break;
				case OperandType.InlineParam :
					instr.Operand = GetParameter (body, br.ReadInt16 ());
					break;
				case OperandType.InlineI8 :
					instr.Operand = br.ReadInt64 ();
					break;
				case OperandType.ShortInlineR :
					instr.Operand = br.ReadSingle ();
					break;
				case OperandType.InlineR :
					instr.Operand = br.ReadDouble ();
					break;
				case OperandType.InlineString :
					instr.Operand = m_root.Streams.UserStringsHeap [GetRid (br.ReadInt32 ())];
					break;
				case OperandType.InlineField :
				case OperandType.InlineMethod :
				case OperandType.InlineType :
				case OperandType.InlineTok :
					MetadataToken token = new MetadataToken (br.ReadInt32 ());
					switch (token.TokenType) {
					case TokenType.TypeDef:
						instr.Operand = m_reflectReader.GetTypeDefAt (token.RID);
						break;
					case TokenType.TypeRef:
						instr.Operand = m_reflectReader.GetTypeRefAt (token.RID);
						break;
					case TokenType.TypeSpec:
						instr.Operand = m_reflectReader.GetTypeSpecAt (token.RID, context);
						break;
					case TokenType.Field:
						instr.Operand = m_reflectReader.GetFieldDefAt (token.RID);
						break;
					case TokenType.Method:
						instr.Operand = m_reflectReader.GetMethodDefAt (token.RID);
						break;
					case TokenType.MethodSpec:
						instr.Operand = m_reflectReader.GetMethodSpecAt (token.RID, context);
						break;
					case TokenType.MemberRef:
						instr.Operand = m_reflectReader.GetMemberRefAt (token.RID, context);
						break;
					default:
						throw new ReflectionException ("Wrong token: " + token);
					}
					break;
				}

				m_instructions.Add (instr.Offset, instr);

				if (last != null) {
					last.Next = instr;
					instr.Previous = last;
				}

				last = instr;

				code.Add (instr);
			}

			// resolve branches
			foreach (Instruction i in code) {
				switch (i.OpCode.OperandType) {
				case OperandType.ShortInlineBrTarget:
				case OperandType.InlineBrTarget:
					i.Operand = GetInstruction (body, (int) i.Operand);
					break;
				case OperandType.InlineSwitch:
					int [] lbls = (int []) i.Operand;
					Instruction [] instrs = new Instruction [lbls.Length];
					for (int j = 0; j < lbls.Length; j++)
						instrs [j] = GetInstruction (body, lbls [j]);
					i.Operand = instrs;
					break;
				}
			}

			if (m_reflectReader.SymbolReader != null)
				m_reflectReader.SymbolReader.Read (body, m_instructions);
		}
예제 #27
0
 public GenericContext Clone()
 {
     GenericContext ctx = new GenericContext ();
     ctx.Type = m_type;
     ctx.Method = m_method;
     return ctx;
 }
예제 #28
0
		TypeReference CreateTypeSpecFromSig (TypeSpec ts, int index, GenericContext context)
		{
			TypeReference tspec = GetTypeRefFromSig (ts.Type, context);
			tspec = GetModifierType (ts.CustomMods, tspec);
			tspec.MetadataToken = MetadataToken.FromMetadataRow (TokenType.TypeSpec, index);
			return tspec;
		}
예제 #29
0
 public static GenericInstanceMethod InflateMethod(GenericContext context, GenericInstanceMethod genericInstanceMethod)
 {
     GenericInstanceType declaringType = genericInstanceMethod.DeclaringType as GenericInstanceType;
     TypeReference reference = (declaringType == null) ? InflateType(context, genericInstanceMethod.DeclaringType) : InflateType(context, declaringType);
     return ConstructGenericMethod(context, reference, genericInstanceMethod.Resolve(), genericInstanceMethod.GenericArguments);
 }
예제 #30
0
        public MethodReturnType GetMethodReturnType(MethodSig msig, GenericContext context)
        {
            TypeReference retType;
            if (msig.RetType.Void)
                retType = SearchCoreType (Constants.Void);
            else if (msig.RetType.ByRef)
                retType = new ReferenceType (GetTypeRefFromSig (msig.RetType.Type, context));
            else if (msig.RetType.TypedByRef)
                retType = SearchCoreType (Constants.TypedReference);
            else
                retType = GetTypeRefFromSig (msig.RetType.Type, context);

            retType = GetModifierType (msig.RetType.CustomMods, retType);

            return new MethodReturnType (retType);
        }
예제 #31
0
 public static GenericInstanceType InflateType(GenericContext context, GenericInstanceType genericInstanceType)
 {
     return ConstructGenericType(context, genericInstanceType.Resolve(), genericInstanceType.GenericArguments);
 }
예제 #32
0
        public GenericInstanceMethod GetMethodSpecAt(uint rid, GenericContext context)
        {
            int index = (int) rid - 1;
            GenericInstanceMethod gim = m_methodSpecs [index];
            if (gim != null)
                return gim;

            MethodSpecTable msTable = m_tableReader.GetMethodSpecTable ();
            MethodSpecRow msRow = msTable [index];

            MethodSpec sig = m_sigReader.GetMethodSpec (msRow.Instantiation);

            MethodReference meth;
            if (msRow.Method.TokenType == TokenType.Method)
                meth = GetMethodDefAt (msRow.Method.RID);
            else if (msRow.Method.TokenType == TokenType.MemberRef)
                meth = (MethodReference) GetMemberRefAt (msRow.Method.RID, context);
            else
                throw new ReflectionException ("Unknown method type for method spec");

            gim = new GenericInstanceMethod (meth);
            context.CheckProvider (meth, sig.Signature.Arity);
            foreach (GenericArg arg in sig.Signature.Types)
                gim.GenericArguments.Add (GetGenericArg (arg, context));

            m_methodSpecs [index] = gim;

            return gim;
        }
예제 #33
0
        CallSite GetCallSiteAt(int token, GenericContext context)
        {
            StandAloneSigTable sasTable = m_reflectReader.TableReader.GetStandAloneSigTable ();
            MethodSig ms = m_reflectReader.SigReader.GetStandAloneMethodSig (
                sasTable [(int) GetRid (token) - 1].Signature);
            CallSite cs = new CallSite (ms.HasThis, ms.ExplicitThis,
                ms.MethCallConv, m_reflectReader.GetMethodReturnType (ms, context));
            cs.MetadataToken = new MetadataToken (TokenType.Signature, GetRid (token));

            for (int i = 0; i < ms.ParamCount; i++) {
                Param p = ms.Parameters [i];
                cs.Parameters.Add (m_reflectReader.BuildParameterDefinition (
                        string.Concat ("A_", i),
                        i, (ParamAttributes) 0,
                        p, context));
            }

            return cs;
        }
예제 #34
0
        public TypeReference GetTypeDefOrRef(MetadataToken token, GenericContext context)
        {
            if (token.RID == 0)
                return null;

            switch (token.TokenType) {
            case TokenType.TypeDef :
                return GetTypeDefAt (token.RID);
            case TokenType.TypeRef :
                return GetTypeRefAt (token.RID);
            case TokenType.TypeSpec :
                return GetTypeSpecAt (token.RID, context);
            default :
                return null;
            }
        }
예제 #35
0
        public ParameterDefinition BuildParameterDefinition(int sequence, Param psig, GenericContext context)
        {
            ParameterDefinition parameter = new ParameterDefinition (null);
            parameter.Sequence = sequence;

            CompleteParameter (parameter, psig, context);

            return parameter;
        }
        public MemberReference GetMemberRefAt(uint rid, GenericContext context)
        {
            int             index  = (int)rid - 1;
            MemberReference member = m_memberRefs [rid - 1];

            if (member != null)
            {
                return(member);
            }

            MemberRefTable mrTable = m_tableReader.GetMemberRefTable();
            MemberRefRow   mrefRow = mrTable [index];

            Signature sig = m_sigReader.GetMemberRefSig(mrefRow.Class.TokenType, mrefRow.Signature);

            switch (mrefRow.Class.TokenType)
            {
            case TokenType.TypeDef:
            case TokenType.TypeRef:
            case TokenType.TypeSpec:
                TypeReference  declaringType = GetTypeDefOrRef(mrefRow.Class, context);
                GenericContext nc            = context.Clone();

                if (declaringType is GenericInstanceType)
                {
                    TypeReference ct = declaringType;
                    while (ct is GenericInstanceType)
                    {
                        ct = (ct as GenericInstanceType).ElementType;
                    }

                    nc.Type          = ct;
                    nc.AllowCreation = ct.GetType() == typeof(TypeReference);
                }

                if (sig is FieldSig)
                {
                    FieldSig fs = sig as FieldSig;
                    member = new FieldReference(
                        m_root.Streams.StringsHeap [mrefRow.Name],
                        declaringType,
                        GetTypeRefFromSig(fs.Type, nc));
                }
                else
                {
                    string    name = m_root.Streams.StringsHeap [mrefRow.Name];
                    MethodSig ms   = sig as MethodSig;

                    MethodReference methref = new MethodReference(
                        name, ms.HasThis, ms.ExplicitThis, ms.MethCallConv);
                    methref.DeclaringType = declaringType;

                    if (sig is MethodDefSig)
                    {
                        int arity = (sig as MethodDefSig).GenericParameterCount;
                        for (int i = 0; i < arity; i++)
                        {
                            methref.GenericParameters.Add(new GenericParameter(i, methref));
                        }
                    }

                    nc.Method = methref;

                    methref.ReturnType = GetMethodReturnType(ms, nc);

                    methref.ReturnType.Method = methref;
                    for (int j = 0; j < ms.ParamCount; j++)
                    {
                        Param p = ms.Parameters [j];
                        ParameterDefinition pdef = BuildParameterDefinition(
                            string.Concat("A_", j), j, new ParamAttributes(), p, nc);
                        pdef.Method = methref;
                        methref.Parameters.Add(pdef);
                    }
                    member    = methref;
                    nc.Method = methref;
                }
                break;

            case TokenType.Method:
                // really not sure about this
                MethodDefinition methdef = GetMethodDefAt(mrefRow.Class.RID);
                member = new MethodReference(
                    methdef.Name, methdef.HasThis,
                    methdef.ExplicitThis, methdef.CallingConvention);
                member.DeclaringType = methdef.DeclaringType;
                break;

            case TokenType.ModuleRef:
                break;                 // TODO, implement that, or not
            }

            member.MetadataToken = MetadataToken.FromMetadataRow(TokenType.MemberRef, index);
            m_module.MemberReferences.Add(member);
            m_memberRefs [index] = member;

            return(member);
        }
예제 #37
0
		void ReadEvents ()
		{
			if (!m_tHeap.HasTable (EventTable.RId)) {
				m_events = new EventDefinition [0];
				return;
			}

			EventTable evtTable = m_tableReader.GetEventTable ();
			EventMapTable emapTable = m_tableReader.GetEventMapTable ();
			m_events = new EventDefinition [evtTable.Rows.Count];
			for (int i = 0; i < emapTable.Rows.Count; i++) {
				EventMapRow emapRow = emapTable [i];
				if (emapRow.Parent == 0)
					continue;

				TypeDefinition owner = GetTypeDefAt (emapRow.Parent);
				GenericContext context = new GenericContext (owner);

				int start = (int) emapRow.EventList, last = evtTable.Rows.Count + 1, end;
				if (i < (emapTable.Rows.Count - 1))
					end = (int) emapTable [i + 1].EventList;
				else
					end = last;

				if (end > last)
					end = last;

				for (int j = start; j < end; j++) {
					EventRow erow = evtTable [j - 1];
					EventDefinition edef = new EventDefinition (
						m_root.Streams.StringsHeap [erow.Name],
						GetTypeDefOrRef (erow.EventType, context), erow.EventFlags);
					edef.MetadataToken = MetadataToken.FromMetadataRow (TokenType.Event, j - 1);

					if (!IsDeleted (edef))
						owner.Events.Add (edef);

					m_events [j - 1] = edef;
				}
			}
		}
예제 #38
0
        public ParameterDefinition BuildParameterDefinition(string name, int sequence, ParameterAttributes attrs, Param psig, GenericContext context)
        {
            ParameterDefinition parameter = new ParameterDefinition (name, sequence, attrs, null);

            CompleteParameter (parameter, psig, context);

            return parameter;
        }
		CallSite GetCallSiteAt (int token, GenericContext context)
		{
			StandAloneSigTable sasTable = m_reflectReader.TableReader.GetStandAloneSigTable ();
			MethodSig ms = m_reflectReader.SigReader.GetStandAloneMethodSig (
				sasTable [(int) GetRid (token) - 1].Signature);
			CallSite cs = new CallSite (ms.HasThis, ms.ExplicitThis,
				ms.MethCallConv, m_reflectReader.GetMethodReturnType (ms, context));
			cs.MetadataToken = new MetadataToken (token);

			for (int i = 0; i < ms.ParamCount; i++) {
				Param p = ms.Parameters [i];
				cs.Parameters.Add (m_reflectReader.BuildParameterDefinition (i, p, context));
			}

			ReflectionReader.CreateSentinelIfNeeded (cs, ms);

			return cs;
		}
예제 #40
0
        public MemberReference GetMemberRefAt(uint rid, GenericContext context)
        {
            int index = (int) rid - 1;
            MemberReference member = m_memberRefs [rid - 1];
            if (member != null)
                return member;

            MemberRefTable mrTable = m_tableReader.GetMemberRefTable ();
            MemberRefRow mrefRow = mrTable [index];

            Signature sig = m_sigReader.GetMemberRefSig (mrefRow.Class.TokenType, mrefRow.Signature);
            switch (mrefRow.Class.TokenType) {
            case TokenType.TypeDef :
            case TokenType.TypeRef :
            case TokenType.TypeSpec :
                TypeReference declaringType = GetTypeDefOrRef (mrefRow.Class, context);
                GenericContext nc = context.Clone ();

                if (declaringType is GenericInstanceType) {
                    TypeReference ct = declaringType;
                    while (ct is GenericInstanceType)
                        ct = (ct as GenericInstanceType).ElementType;

                    nc.Type = ct;
                }

                if (sig is FieldSig) {
                    FieldSig fs = sig as FieldSig;
                    TypeReference fieldType = GetTypeRefFromSig (fs.Type, nc);
                    fieldType = GetModifierType (fs.CustomMods, fieldType);

                    member = new FieldReference (
                        m_root.Streams.StringsHeap [mrefRow.Name],
                        declaringType,
                        fieldType);
                } else {
                    string name = m_root.Streams.StringsHeap [mrefRow.Name];
                    MethodSig ms = (MethodSig) sig;

                    member = CreateMethodReferenceFromSig (ms, name, declaringType, nc);
                }
                break;
            case TokenType.Method :
                // really not sure about this
                MethodDefinition methdef = GetMethodDefAt (mrefRow.Class.RID);

                member = CreateMethodReferenceFromSig ((MethodSig) sig, methdef.Name, methdef.DeclaringType, new GenericContext ());
                break;
            case TokenType.ModuleRef :
                break; // TODO, implement that, or not
            }

            member.MetadataToken = MetadataToken.FromMetadataRow (TokenType.MemberRef, index);
            m_module.MemberReferences.Add (member);
            m_memberRefs [index] = member;

            return member;
        }