コード例 #1
0
        private AnalysisNet.IInstruction ProcessMethodCallIndirect(Cecil.Cil.Instruction op)
        {
            Cecil.FunctionPointerType             cciFunctionPointer = op.Operand as Cecil.FunctionPointerType;
            AnalysisNet.Types.FunctionPointerType ourFunctionPointer = typeExtractor.ExtractType(cciFunctionPointer) as AnalysisNet.Types.FunctionPointerType;

            AnalysisNetBytecode.IndirectMethodCallInstruction instruction = new AnalysisNetBytecode.IndirectMethodCallInstruction((uint)op.Offset, ourFunctionPointer);
            return(instruction);
        }
コード例 #2
0
        private AnalysisNet.Types.FunctionPointerType ExtractType(Cecil.FunctionPointerType typeref)
        {
            AnalysisNet.Types.IType returnType         = ExtractType(typeref.ElementType);
            AnalysisNet.Types.FunctionPointerType type = new AnalysisNet.Types.FunctionPointerType(returnType);

            //ExtractCustomAttributes(type.Attributes, typeref.Attr);
            ExtractParameters(type.Parameters, typeref.Parameters);

            //type.IsStatic = typeref.IsStatic;
            type.IsStatic = !(typeref.HasThis || typeref.ExplicitThis);
            return(type);
        }
コード例 #3
0
ファイル: ReferenceGenerator.cs プロジェクト: m7nu3l/tac2cil
        public Cecil.TypeReference TypeReference(AnalysisNet.Types.IType type)
        {
            if (type is AnalysisNet.Types.IBasicType basicType)
            {
                return(TypeReference(basicType));
            }

            if (type is AnalysisNet.Types.IGenericParameterReference iGenericParam)
            {
                return(TypeReference(iGenericParam));
            }

            if (type is AnalysisNet.Types.FunctionPointerType functionPointerType)
            {
                var funcPtr = new Cecil.FunctionPointerType()
                {
                    ReturnType = TypeReference(functionPointerType.ReturnType),
                    HasThis    = !functionPointerType.IsStatic,
                };
                funcPtr.Parameters.AddRange(functionPointerType.Parameters.Select(p => new Cecil.ParameterDefinition(TypeReference(p.Type))));
                return(funcPtr);
            }

            if (type is AnalysisNet.Types.PointerType pointerType)
            {
                // Mono.Cecil.PointerType is an unsafe reference
                return(new Cecil.ByReferenceType(TypeReference(pointerType.TargetType)));
            }

            if (type is AnalysisNet.Types.ArrayType arrayType)
            {
                return(Cecil.Rocks.TypeReferenceRocks.MakeArrayType(TypeReference(arrayType.ElementsType), (int)arrayType.Rank));
            }

            if (type is AnalysisNet.Types.UnknownType unknownType)
            {
                throw new NotImplementedException();
            }

            throw new NotImplementedException();
        }
コード例 #4
0
        public void WriteTypeSignature(TypeReference type)
        {
            GenericParameter  parameter;
            TypeSpecification specification;

            if (type == null)
            {
                throw new ArgumentNullException();
            }
            ElementType etype = type.etype;
            ElementType type7 = etype;

            if (type7 > ElementType.GenericInst)
            {
                switch (type7)
                {
                case ElementType.FnPtr:
                {
                    FunctionPointerType method = (FunctionPointerType)type;
                    this.WriteElementType(ElementType.FnPtr);
                    this.WriteMethodSignature(method);
                    return;
                }

                case ElementType.Object:
                case ElementType.SzArray:
                    break;

                case ElementType.MVar:
                    goto TR_0007;

                case ElementType.CModReqD:
                case ElementType.CModOpt:
                    this.WriteModifierSignature(etype, (IModifierType)type);
                    return;

                default:
                    if ((type7 != ElementType.Sentinel) && (type7 != ElementType.Pinned))
                    {
                        break;
                    }
                    goto TR_0004;
                }
                goto TR_0003;
            }
            else
            {
                if (type7 == ElementType.None)
                {
                    this.WriteElementType(type.IsValueType ? ElementType.ValueType : ElementType.Class);
                    base.WriteCompressedUInt32(this.MakeTypeDefOrRefCodedRID(type));
                    return;
                }
                switch (type7)
                {
                case ElementType.Ptr:
                case ElementType.ByRef:
                    goto TR_0004;

                case ElementType.Var:
                    break;

                case ElementType.Array:
                {
                    ArrayType array = (ArrayType)type;
                    if (!array.IsVector)
                    {
                        this.WriteArrayTypeSignature(array);
                        return;
                    }
                    this.WriteElementType(ElementType.SzArray);
                    this.WriteTypeSignature(array.ElementType);
                    return;
                }

                case ElementType.GenericInst:
                {
                    GenericInstanceType instance = (GenericInstanceType)type;
                    this.WriteElementType(ElementType.GenericInst);
                    this.WriteElementType(instance.IsValueType ? ElementType.ValueType : ElementType.Class);
                    base.WriteCompressedUInt32(this.MakeTypeDefOrRefCodedRID(instance.ElementType));
                    this.WriteGenericInstanceSignature(instance);
                    return;
                }

                default:
                    goto TR_0003;
                }
            }
            goto TR_0007;
TR_0003:
            if (!this.TryWriteElementType(type))
            {
                throw new NotSupportedException();
            }
            return;

TR_0004:
            specification = (TypeSpecification)type;
            this.WriteElementType(etype);
            this.WriteTypeSignature(specification.ElementType);
            return;

TR_0007:
            parameter = (GenericParameter)type;
            this.WriteElementType(etype);
            int position = parameter.Position;

            if (position == -1)
            {
                throw new NotSupportedException();
            }
            base.WriteCompressedUInt32((uint)position);
        }
コード例 #5
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(
                                             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);
        }
コード例 #6
0
 static int functionPointerTypeHashCode(FunctionPointerType a)
 {
     if (a == null)
         return 0;
     return methodReferenceHashCode(a.function) + typeSpecificationHashCode(a);
 }
コード例 #7
0
ファイル: ResolverUtils.cs プロジェクト: ldh0227/de4dot
 static bool compareFunctionPointerTypes(Type a, FunctionPointerType b)
 {
     return compareTypes(a, b.ElementType);
 }
コード例 #8
0
        private TypeReference ReadTypeSignature(ElementType etype)
        {
            ElementType type3 = etype;

            switch (type3)
            {
            case ElementType.Void:
                return(this.TypeSystem.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.Array | ElementType.Boolean | ElementType.Void):
            case (ElementType.Boolean | ElementType.ByRef | ElementType.I4):
                break;

            case ElementType.Ptr:
                return(new PointerType(this.ReadTypeSignature()));

            case ElementType.ByRef:
                return(new ByReferenceType(this.ReadTypeSignature()));

            case ElementType.ValueType:
            {
                TypeReference typeDefOrRef = this.GetTypeDefOrRef(this.ReadTypeTokenSignature());
                typeDefOrRef.IsValueType = true;
                return(typeDefOrRef);
            }

            case ElementType.Class:
                return(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()));

            case ElementType.Var:
                return(this.GetGenericParameter(GenericParameterType.Type, base.ReadCompressedUInt32()));

            case ElementType.Array:
                return(this.ReadArrayTypeSignature());

            case ElementType.GenericInst:
            {
                bool                flag         = base.ReadByte() == 0x11;
                TypeReference       typeDefOrRef = this.GetTypeDefOrRef(this.ReadTypeTokenSignature());
                GenericInstanceType instance     = new GenericInstanceType(typeDefOrRef);
                this.ReadGenericInstanceSignature(typeDefOrRef, instance);
                if (flag)
                {
                    instance.IsValueType = true;
                    typeDefOrRef.GetElementType().IsValueType = true;
                }
                return(instance);
            }

            case ElementType.TypedByRef:
                return(this.TypeSystem.TypedReference);

            case ElementType.I:
                return(this.TypeSystem.IntPtr);

            case ElementType.U:
                return(this.TypeSystem.UIntPtr);

            case ElementType.FnPtr:
            {
                FunctionPointerType method = new FunctionPointerType();
                this.ReadMethodSignature(method);
                return(method);
            }

            case ElementType.Object:
                return(this.TypeSystem.Object);

            case ElementType.SzArray:
                return(new ArrayType(this.ReadTypeSignature()));

            case ElementType.MVar:
                return(this.GetGenericParameter(GenericParameterType.Method, base.ReadCompressedUInt32()));

            case ElementType.CModReqD:
                return(new RequiredModifierType(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()), this.ReadTypeSignature()));

            case ElementType.CModOpt:
                return(new OptionalModifierType(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()), this.ReadTypeSignature()));

            default:
                if (type3 == ElementType.Sentinel)
                {
                    return(new SentinelType(this.ReadTypeSignature()));
                }
                if (type3 != ElementType.Pinned)
                {
                    break;
                }
                return(new PinnedType(this.ReadTypeSignature()));
            }
            return(this.GetPrimitiveType(etype));
        }
コード例 #9
0
 static bool compareFunctionPointerTypes(FunctionPointerType a, FunctionPointerType b)
 {
     return compareMethodReference(a.function, b.function) &&
         compareTypeSpecifications(a, b);
 }
コード例 #10
0
ファイル: Import.cs プロジェクト: KonajuGames/cecil
        TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context)
        {
            switch (type.etype) {
            case ElementType.SzArray:
                var vector = (ArrayType) type;
                return new ArrayType (ImportType (vector.ElementType, context));
            case ElementType.Ptr:
                var pointer = (PointerType) type;
                return new PointerType (ImportType (pointer.ElementType, context));
            case ElementType.ByRef:
                var byref = (ByReferenceType) type;
                return new ByReferenceType (ImportType (byref.ElementType, context));
            case ElementType.Pinned:
                var pinned = (PinnedType) type;
                return new PinnedType (ImportType (pinned.ElementType, context));
            case ElementType.Sentinel:
                var sentinel = (SentinelType) type;
                return new SentinelType (ImportType (sentinel.ElementType, context));
            case ElementType.CModOpt:
                var modopt = (OptionalModifierType) type;
                return new OptionalModifierType (
                    ImportType (modopt.ModifierType, context),
                    ImportType (modopt.ElementType, context));
            case ElementType.CModReqD:
                var modreq = (RequiredModifierType) type;
                return new RequiredModifierType (
                    ImportType (modreq.ModifierType, context),
                    ImportType (modreq.ElementType, context));
            case ElementType.Array:
                var array = (ArrayType) type;
                var imported_array = new ArrayType (ImportType (array.ElementType, context));
                if (array.IsVector)
                    return imported_array;

                var dimensions = array.Dimensions;
                var imported_dimensions = imported_array.Dimensions;

                imported_dimensions.Clear ();

                for (int i = 0; i < dimensions.Count; i++) {
                    var dimension = dimensions [i];

                    imported_dimensions.Add (new ArrayDimension (dimension.LowerBound, dimension.UpperBound));
                }

                return imported_array;
            case ElementType.GenericInst:
                var instance = (GenericInstanceType) type;
                var element_type = ImportType (instance.ElementType, context);
                var imported_instance = new GenericInstanceType (element_type);

                var arguments = instance.GenericArguments;
                var imported_arguments = imported_instance.GenericArguments;

                for (int i = 0; i < arguments.Count; i++)
                    imported_arguments.Add (ImportType (arguments [i], context));

                return imported_instance;
            case ElementType.Var:
                var var_parameter = (GenericParameter) type;
                return context.TypeParameter (type.DeclaringType.FullName, var_parameter.Position);
            case ElementType.MVar:
                var mvar_parameter = (GenericParameter) type;
                return context.MethodParameter (mvar_parameter.DeclaringMethod.Name, mvar_parameter.Position);
            case ElementType.FnPtr:
                var funcPtr = (FunctionPointerType)type;
                var imported = new FunctionPointerType() {
                    HasThis = funcPtr.HasThis,
                    ExplicitThis = funcPtr.ExplicitThis,
                    CallingConvention = funcPtr.CallingConvention,
                    ReturnType = ImportType (funcPtr.ReturnType, context)
                };
                var parameters = funcPtr.Parameters;
                for (int i = 0; i < parameters.Count; i++)
                    imported.Parameters.Add(
                        new ParameterDefinition (ImportType (parameters [i].ParameterType, context)));
                return imported;
            }

            throw new NotSupportedException (type.etype.ToString ());
        }
コード例 #11
0
ファイル: MemberRefFinder.cs プロジェクト: ldh0227/de4dot
        void addFunctionPointerType(FunctionPointerType fpt)
        {
            if (fpt == null)
                return;
            addTypeSpecification(fpt);

            // It's an anon MethodReference created by the class. Not useful to us.
            //pushMember(fpt.function);
        }
コード例 #12
0
ファイル: MemberRefFinder.cs プロジェクト: Joelone/de4dot
 void doFunctionPointerType(FunctionPointerType functionPointerType)
 {
     if (functionPointerTypes.ContainsKey(functionPointerType))
         return;
     functionPointerTypes[functionPointerType] = true;
     addFunctionPointerType(functionPointerType);
 }
コード例 #13
0
ファイル: ReflectionReader.cs プロジェクト: contra/DotNOS
        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;
        }
コード例 #14
0
        public void WriteTypeSignature(TypeReference type)
        {
            int position;
            TypeSpecification typeSpecification;
            GenericParameter  genericParameter;

            if (type == null)
            {
                throw new ArgumentNullException();
            }
            ElementType elementType = type.etype;

            if (elementType > ElementType.GenericInst)
            {
                switch (elementType)
                {
                case ElementType.FnPtr:
                {
                    FunctionPointerType functionPointerType = (FunctionPointerType)type;
                    this.WriteElementType(ElementType.FnPtr);
                    this.WriteMethodSignature(functionPointerType);
                    return;
                }

                case ElementType.Object:
                case ElementType.SzArray:
                {
                    break;
                }

                case ElementType.MVar:
                {
                    genericParameter = (GenericParameter)type;
                    this.WriteElementType(elementType);
                    position = genericParameter.Position;
                    if (position == -1)
                    {
                        throw new NotSupportedException();
                    }
                    base.WriteCompressedUInt32((uint)position);
                    return;
                }

                case ElementType.CModReqD:
                case ElementType.CModOpt:
                {
                    this.WriteModifierSignature(elementType, (IModifierType)type);
                    return;
                }

                default:
                {
                    if (elementType == ElementType.Sentinel || elementType == ElementType.Pinned)
                    {
                        typeSpecification = (TypeSpecification)type;
                        this.WriteElementType(elementType);
                        this.WriteTypeSignature(typeSpecification.ElementType);
                        return;
                    }
                    break;
                }
                }
            }
            else
            {
                if (elementType == ElementType.None)
                {
                    this.WriteElementType((type.IsValueType ? ElementType.ValueType : ElementType.Class));
                    base.WriteCompressedUInt32(this.MakeTypeDefOrRefCodedRID(type));
                    return;
                }
                switch (elementType)
                {
                case ElementType.Ptr:
                case ElementType.ByRef:
                {
                    typeSpecification = (TypeSpecification)type;
                    this.WriteElementType(elementType);
                    this.WriteTypeSignature(typeSpecification.ElementType);
                    return;
                }

                case ElementType.Var:
                {
                    genericParameter = (GenericParameter)type;
                    this.WriteElementType(elementType);
                    position = genericParameter.Position;
                    if (position == -1)
                    {
                        throw new NotSupportedException();
                    }
                    base.WriteCompressedUInt32((uint)position);
                    return;
                }

                case ElementType.Array:
                {
                    ArrayType arrayType = (ArrayType)type;
                    if (!arrayType.IsVector)
                    {
                        this.WriteArrayTypeSignature(arrayType);
                        return;
                    }
                    this.WriteElementType(ElementType.SzArray);
                    this.WriteTypeSignature(arrayType.ElementType);
                    return;
                }

                case ElementType.GenericInst:
                {
                    GenericInstanceType genericInstanceType = (GenericInstanceType)type;
                    this.WriteElementType(ElementType.GenericInst);
                    this.WriteElementType((genericInstanceType.IsValueType ? ElementType.ValueType : ElementType.Class));
                    base.WriteCompressedUInt32(this.MakeTypeDefOrRefCodedRID(genericInstanceType.ElementType));
                    this.WriteGenericInstanceSignature(genericInstanceType);
                    return;
                }
                }
            }
            if (!this.TryWriteElementType(type))
            {
                throw new NotSupportedException();
            }
        }
コード例 #15
0
        private TypeReference ReadTypeSignature(ElementType etype)
        {
            switch (etype)
            {
            case ElementType.Void:
            {
                return(this.TypeSystem.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.Void | ElementType.Boolean | ElementType.Char | ElementType.I1 | ElementType.U1 | ElementType.I2 | ElementType.U2 | ElementType.ByRef | ElementType.ValueType | ElementType.Class | ElementType.Var | ElementType.Array | ElementType.GenericInst | ElementType.TypedByRef:
            case ElementType.Boolean | ElementType.I4 | ElementType.I8 | ElementType.ByRef | ElementType.Class | ElementType.I:
            {
                return(this.GetPrimitiveType(etype));
            }

            case ElementType.Ptr:
            {
                return(new PointerType(this.ReadTypeSignature()));
            }

            case ElementType.ByRef:
            {
                return(new ByReferenceType(this.ReadTypeSignature()));
            }

            case ElementType.ValueType:
            {
                TypeReference typeDefOrRef = this.GetTypeDefOrRef(this.ReadTypeTokenSignature());
                typeDefOrRef.IsValueType = true;
                return(typeDefOrRef);
            }

            case ElementType.Class:
            {
                return(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()));
            }

            case ElementType.Var:
            {
                return(this.GetGenericParameter(GenericParameterType.Type, base.ReadCompressedUInt32()));
            }

            case ElementType.Array:
            {
                return(this.ReadArrayTypeSignature());
            }

            case ElementType.GenericInst:
            {
                TypeReference       typeReference       = this.GetTypeDefOrRef(this.ReadTypeTokenSignature());
                GenericInstanceType genericInstanceType = new GenericInstanceType(typeReference);
                this.ReadGenericInstanceSignature(typeReference, genericInstanceType);
                if (base.ReadByte() == 17)
                {
                    genericInstanceType.IsValueType = true;
                    typeReference.GetElementType().IsValueType = true;
                }
                return(genericInstanceType);
            }

            case ElementType.TypedByRef:
            {
                return(this.TypeSystem.TypedReference);
            }

            case ElementType.I:
            {
                return(this.TypeSystem.IntPtr);
            }

            case ElementType.U:
            {
                return(this.TypeSystem.UIntPtr);
            }

            case ElementType.FnPtr:
            {
                FunctionPointerType functionPointerType = new FunctionPointerType();
                this.ReadMethodSignature(functionPointerType);
                return(functionPointerType);
            }

            case ElementType.Object:
            {
                return(this.TypeSystem.Object);
            }

            case ElementType.SzArray:
            {
                return(new ArrayType(this.ReadTypeSignature()));
            }

            case ElementType.MVar:
            {
                return(this.GetGenericParameter(GenericParameterType.Method, base.ReadCompressedUInt32()));
            }

            case ElementType.CModReqD:
            {
                return(new RequiredModifierType(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()), this.ReadTypeSignature()));
            }

            case ElementType.CModOpt:
            {
                return(new OptionalModifierType(this.GetTypeDefOrRef(this.ReadTypeTokenSignature()), this.ReadTypeSignature()));
            }

            default:
            {
                if (etype == ElementType.Sentinel)
                {
                    return(new SentinelType(this.ReadTypeSignature()));
                }
                if (etype == ElementType.Pinned)
                {
                    return(new PinnedType(this.ReadTypeSignature()));
                }
                return(this.GetPrimitiveType(etype));
            }
            }
        }
コード例 #16
0
        private TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context)
        {
            switch (type.etype)
            {
            case ElementType.SzArray:
            {
                ArrayType arrayType = (ArrayType)type;
                return(new ArrayType(ImportType(arrayType.ElementType, context)));
            }

            case ElementType.Ptr:
            {
                PointerType pointerType = (PointerType)type;
                return(new PointerType(ImportType(pointerType.ElementType, context)));
            }

            case ElementType.ByRef:
            {
                ByReferenceType byReferenceType = (ByReferenceType)type;
                return(new ByReferenceType(ImportType(byReferenceType.ElementType, context)));
            }

            case ElementType.Pinned:
            {
                PinnedType pinnedType = (PinnedType)type;
                return(new PinnedType(ImportType(pinnedType.ElementType, context)));
            }

            case ElementType.Sentinel:
            {
                SentinelType sentinelType = (SentinelType)type;
                return(new SentinelType(ImportType(sentinelType.ElementType, context)));
            }

            case ElementType.FnPtr:
            {
                FunctionPointerType functionPointerType  = (FunctionPointerType)type;
                FunctionPointerType functionPointerType2 = new FunctionPointerType
                {
                    HasThis           = functionPointerType.HasThis,
                    ExplicitThis      = functionPointerType.ExplicitThis,
                    CallingConvention = functionPointerType.CallingConvention,
                    ReturnType        = ImportType(functionPointerType.ReturnType, context)
                };
                if (!functionPointerType.HasParameters)
                {
                    return(functionPointerType2);
                }
                for (int j = 0; j < functionPointerType.Parameters.Count; j++)
                {
                    functionPointerType2.Parameters.Add(new ParameterDefinition(ImportType(functionPointerType.Parameters[j].ParameterType, context)));
                }
                return(functionPointerType2);
            }

            case ElementType.CModOpt:
            {
                OptionalModifierType optionalModifierType = (OptionalModifierType)type;
                return(new OptionalModifierType(ImportType(optionalModifierType.ModifierType, context), ImportType(optionalModifierType.ElementType, context)));
            }

            case ElementType.CModReqD:
            {
                RequiredModifierType requiredModifierType = (RequiredModifierType)type;
                return(new RequiredModifierType(ImportType(requiredModifierType.ModifierType, context), ImportType(requiredModifierType.ElementType, context)));
            }

            case ElementType.Array:
            {
                ArrayType arrayType2 = (ArrayType)type;
                ArrayType arrayType3 = new ArrayType(ImportType(arrayType2.ElementType, context));
                if (arrayType2.IsVector)
                {
                    return(arrayType3);
                }
                Collection <ArrayDimension> dimensions  = arrayType2.Dimensions;
                Collection <ArrayDimension> dimensions2 = arrayType3.Dimensions;
                dimensions2.Clear();
                for (int k = 0; k < dimensions.Count; k++)
                {
                    ArrayDimension arrayDimension = dimensions[k];
                    dimensions2.Add(new ArrayDimension(arrayDimension.LowerBound, arrayDimension.UpperBound));
                }
                return(arrayType3);
            }

            case ElementType.GenericInst:
            {
                GenericInstanceType        genericInstanceType  = (GenericInstanceType)type;
                GenericInstanceType        genericInstanceType2 = new GenericInstanceType(ImportType(genericInstanceType.ElementType, context));
                Collection <TypeReference> genericArguments     = genericInstanceType.GenericArguments;
                Collection <TypeReference> genericArguments2    = genericInstanceType2.GenericArguments;
                for (int i = 0; i < genericArguments.Count; i++)
                {
                    genericArguments2.Add(ImportType(genericArguments[i], context));
                }
                return(genericInstanceType2);
            }

            case ElementType.Var:
            {
                GenericParameter genericParameter2 = (GenericParameter)type;
                if (genericParameter2.DeclaringType == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.TypeParameter(genericParameter2.DeclaringType.FullName, genericParameter2.Position));
            }

            case ElementType.MVar:
            {
                GenericParameter genericParameter = (GenericParameter)type;
                if (genericParameter.DeclaringMethod == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.MethodParameter(context.NormalizeMethodName(genericParameter.DeclaringMethod), genericParameter.Position));
            }

            default:
                throw new NotSupportedException(type.etype.ToString());
            }
        }
コード例 #17
0
        private TypeReference ReadTypeSignature(ElementType etype)
        {
            switch (etype)
            {
            case ElementType.ValueType:
            {
                TypeReference typeDefOrRef2 = GetTypeDefOrRef(ReadTypeTokenSignature());
                typeDefOrRef2.KnownValueType();
                return(typeDefOrRef2);
            }

            case ElementType.Class:
                return(GetTypeDefOrRef(ReadTypeTokenSignature()));

            case ElementType.Ptr:
                return(new PointerType(ReadTypeSignature()));

            case ElementType.FnPtr:
            {
                FunctionPointerType functionPointerType = new FunctionPointerType();
                ReadMethodSignature(functionPointerType);
                return(functionPointerType);
            }

            case ElementType.ByRef:
                return(new ByReferenceType(ReadTypeSignature()));

            case ElementType.Pinned:
                return(new PinnedType(ReadTypeSignature()));

            case ElementType.SzArray:
                return(new ArrayType(ReadTypeSignature()));

            case ElementType.Array:
                return(ReadArrayTypeSignature());

            case ElementType.CModOpt:
                return(new OptionalModifierType(GetTypeDefOrRef(ReadTypeTokenSignature()), ReadTypeSignature()));

            case ElementType.CModReqD:
                return(new RequiredModifierType(GetTypeDefOrRef(ReadTypeTokenSignature()), ReadTypeSignature()));

            case ElementType.Sentinel:
                return(new SentinelType(ReadTypeSignature()));

            case ElementType.Var:
                return(GetGenericParameter(GenericParameterType.Type, base.ReadCompressedUInt32()));

            case ElementType.MVar:
                return(GetGenericParameter(GenericParameterType.Method, base.ReadCompressedUInt32()));

            case ElementType.GenericInst:
            {
                bool                num                 = base.ReadByte() == 17;
                TypeReference       typeDefOrRef        = GetTypeDefOrRef(ReadTypeTokenSignature());
                GenericInstanceType genericInstanceType = new GenericInstanceType(typeDefOrRef);
                ReadGenericInstanceSignature(typeDefOrRef, genericInstanceType);
                if (num)
                {
                    genericInstanceType.KnownValueType();
                    typeDefOrRef.GetElementType().KnownValueType();
                }
                return(genericInstanceType);
            }

            case ElementType.Object:
                return(TypeSystem.Object);

            case ElementType.Void:
                return(TypeSystem.Void);

            case ElementType.TypedByRef:
                return(TypeSystem.TypedReference);

            case ElementType.I:
                return(TypeSystem.IntPtr);

            case ElementType.U:
                return(TypeSystem.UIntPtr);

            default:
                return(GetPrimitiveType(etype));
            }
        }
コード例 #18
0
 protected virtual FunctionPointerType updateFunctionPointerType(FunctionPointerType a)
 {
     var rv = new FunctionPointerType();
     rv.function = a.function;
     return rv;
 }
コード例 #19
0
        TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context)
        {
            switch (type.etype)
            {
            case ElementType.SzArray:
                var vector = (ArrayType)type;
                return(new ArrayType(ImportType(vector.ElementType, context)));

            case ElementType.Ptr:
                var pointer = (PointerType)type;
                return(new PointerType(ImportType(pointer.ElementType, context)));

            case ElementType.ByRef:
                var byref = (ByReferenceType)type;
                return(new ByReferenceType(ImportType(byref.ElementType, context)));

            case ElementType.Pinned:
                var pinned = (PinnedType)type;
                return(new PinnedType(ImportType(pinned.ElementType, context)));

            case ElementType.Sentinel:
                var sentinel = (SentinelType)type;
                return(new SentinelType(ImportType(sentinel.ElementType, context)));

            case ElementType.FnPtr:
                var fnptr          = (FunctionPointerType)type;
                var imported_fnptr = new FunctionPointerType()
                {
                    HasThis           = fnptr.HasThis,
                    ExplicitThis      = fnptr.ExplicitThis,
                    CallingConvention = fnptr.CallingConvention,
                    ReturnType        = ImportType(fnptr.ReturnType, context),
                };

                if (!fnptr.HasParameters)
                {
                    return(imported_fnptr);
                }

                for (int i = 0; i < fnptr.Parameters.Count; i++)
                {
                    imported_fnptr.Parameters.Add(new ParameterDefinition(
                                                      ImportType(fnptr.Parameters [i].ParameterType, context)));
                }

                return(imported_fnptr);

            case ElementType.CModOpt:
                var modopt = (OptionalModifierType)type;
                return(new OptionalModifierType(
                           ImportType(modopt.ModifierType, context),
                           ImportType(modopt.ElementType, context)));

            case ElementType.CModReqD:
                var modreq = (RequiredModifierType)type;
                return(new RequiredModifierType(
                           ImportType(modreq.ModifierType, context),
                           ImportType(modreq.ElementType, context)));

            case ElementType.Array:
                var array          = (ArrayType)type;
                var imported_array = new ArrayType(ImportType(array.ElementType, context));
                if (array.IsVector)
                {
                    return(imported_array);
                }

                var dimensions          = array.Dimensions;
                var imported_dimensions = imported_array.Dimensions;

                imported_dimensions.Clear();

                for (int i = 0; i < dimensions.Count; i++)
                {
                    var dimension = dimensions [i];

                    imported_dimensions.Add(new ArrayDimension(dimension.LowerBound, dimension.UpperBound));
                }

                return(imported_array);

            case ElementType.GenericInst:
                var instance          = (GenericInstanceType)type;
                var element_type      = ImportType(instance.ElementType, context);
                var imported_instance = new GenericInstanceType(element_type);

                var arguments          = instance.GenericArguments;
                var imported_arguments = imported_instance.GenericArguments;

                for (int i = 0; i < arguments.Count; i++)
                {
                    imported_arguments.Add(ImportType(arguments [i], context));
                }

                return(imported_instance);

            case ElementType.Var:
                var var_parameter = (GenericParameter)type;
                if (var_parameter.DeclaringType == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.TypeParameter(var_parameter.DeclaringType.FullName, var_parameter.Position));

            case ElementType.MVar:
                var mvar_parameter = (GenericParameter)type;
                if (mvar_parameter.DeclaringMethod == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.MethodParameter(context.NormalizeMethodName(mvar_parameter.DeclaringMethod), mvar_parameter.Position));
            }

            throw new NotSupportedException(type.etype.ToString());
        }
コード例 #20
0
        TypeSpecification GetTypeSpec(TypeSpecification original, ImportContext context)
        {
            TypeSpecification typeSpec;

            TypeReference elementType = ImportTypeReference(original.ElementType, context);

            if (original is PointerType)
            {
                typeSpec = new PointerType(elementType);
            }
            else if (original is ArrayType)                 // deal with complex arrays
            {
                typeSpec = new ArrayType(elementType);
            }
            else if (original is ReferenceType)
            {
                typeSpec = new ReferenceType(elementType);
            }
            else if (original is GenericInstanceType)
            {
                GenericInstanceType git         = original as GenericInstanceType;
                GenericInstanceType genElemType = new GenericInstanceType(elementType);

                context.GenericContext.CheckProvider(genElemType.GetOriginalType(), git.GenericArguments.Count);
                foreach (TypeReference arg in git.GenericArguments)
                {
                    genElemType.GenericArguments.Add(ImportTypeReference(arg, context));
                }

                typeSpec = genElemType;
            }
            else if (original is ModifierOptional)
            {
                TypeReference mt = (original as ModifierOptional).ModifierType;
                typeSpec = new ModifierOptional(elementType, ImportTypeReference(mt, context));
            }
            else if (original is ModifierRequired)
            {
                TypeReference mt = (original as ModifierRequired).ModifierType;
                typeSpec = new ModifierRequired(elementType, ImportTypeReference(mt, context));
            }
            else if (original is SentinelType)
            {
                typeSpec = new SentinelType(elementType);
            }
            else if (original is FunctionPointerType)
            {
                FunctionPointerType ori = original as FunctionPointerType;

                FunctionPointerType fnptr = new FunctionPointerType(
                    ori.HasThis,
                    ori.ExplicitThis,
                    ori.CallingConvention,
                    new MethodReturnType(ImportTypeReference(ori.ReturnType.ReturnType, context)));

                foreach (ParameterDefinition parameter in ori.Parameters)
                {
                    fnptr.Parameters.Add(new ParameterDefinition(ImportTypeReference(parameter.ParameterType, context)));
                }

                typeSpec = fnptr;
            }
            else
            {
                throw new ReflectionException("Unknown element type: {0}", original.GetType().Name);
            }

            return(typeSpec);
        }
コード例 #21
0
ファイル: ReferenceFixator.cs プロジェクト: peters/il-repack
        private TypeSpecification Fix(TypeSpecification type)
        {
            var fet = Fix(type.ElementType);
            if (type is ArrayType)
            {
                var array = (ArrayType)type;
                var imported_array = new ArrayType(fet);
                if (array.IsVector)
                    return imported_array;

                var dimensions = array.Dimensions;
                var imported_dimensions = imported_array.Dimensions;

                imported_dimensions.Clear();

                for (int i = 0; i < dimensions.Count; i++)
                {
                    var dimension = dimensions[i];

                    imported_dimensions.Add(new ArrayDimension(dimension.LowerBound, dimension.UpperBound));
                }

                return imported_array;
            }
            if (type is PointerType)
                return new PointerType(fet);
            if (type is ByReferenceType)
                return new ByReferenceType(fet);
            if (type is PinnedType)
                return new PinnedType(fet);
            if (type is SentinelType)
                return new SentinelType(fet);
            if (type is OptionalModifierType)
            {
                TypeReference fmt = Fix(((OptionalModifierType)type).ModifierType);
                return new OptionalModifierType(fmt, fet);
            }
            if (type is RequiredModifierType)
            {
                TypeReference fmt = Fix(((RequiredModifierType)type).ModifierType);
                return new RequiredModifierType(fmt, fet);
            }
            if (type is GenericInstanceType)
            {
                var instance = (GenericInstanceType)type;
                var imported_instance = new GenericInstanceType(fet);

                var arguments = instance.GenericArguments;
                var imported_arguments = imported_instance.GenericArguments;

                for (int i = 0; i < arguments.Count; i++)
                    imported_arguments.Add(Fix(arguments[i]));

                return imported_instance;
            }
            if (type is FunctionPointerType)
            {
                var funcPtr = (FunctionPointerType)type;
                var imported_instance = new FunctionPointerType()
                {
                    HasThis = funcPtr.HasThis,
                    ExplicitThis = funcPtr.ExplicitThis,
                    CallingConvention = funcPtr.CallingConvention,
                    ReturnType = Fix(funcPtr.ReturnType)
                };
                if (funcPtr.HasParameters)
                {
                    foreach (var pd in funcPtr.Parameters)
                    {
                        imported_instance.Parameters.Add(pd);
                    }
                    FixReferences(imported_instance.Parameters);
                }
                return imported_instance;
            }
            throw new InvalidOperationException();
        }
コード例 #22
0
ファイル: DefaultImporter.cs プロジェクト: sidecut/xaeios
        protected TypeSpecification GetTypeSpec(TypeSpecification original, ImportContext context)
        {
            TypeSpecification typeSpec;

            TypeReference elementType = ImportTypeReference (original.ElementType, context);
            if (original is PointerType) {
                typeSpec = new PointerType (elementType);
            } else if (original is ArrayType) { // deal with complex arrays
                typeSpec = new ArrayType (elementType);
            } else if (original is ReferenceType) {
                typeSpec = new ReferenceType (elementType);
            } else if (original is GenericInstanceType) {
                GenericInstanceType git = original as GenericInstanceType;
                GenericInstanceType genElemType = new GenericInstanceType (elementType);

                context.GenericContext.CheckProvider (genElemType.GetOriginalType (), git.GenericArguments.Count);

                foreach (GenericParameter genericParameter in git.GenericParameters)
                    genElemType.GenericParameters.Add(GetGenericParameter(genericParameter, context));
                foreach (TypeReference arg in git.GenericArguments)
                    genElemType.GenericArguments.Add (ImportTypeReference (arg, context));

                typeSpec = genElemType;
            } else if (original is ModifierOptional) {
                TypeReference mt = (original as ModifierOptional).ModifierType;
                typeSpec = new ModifierOptional (elementType, ImportTypeReference (mt, context));
            } else if (original is ModifierRequired) {
                TypeReference mt = (original as ModifierRequired).ModifierType;
                typeSpec = new ModifierRequired (elementType, ImportTypeReference (mt, context));
            } else if (original is SentinelType) {
                typeSpec = new SentinelType (elementType);
            } else if (original is FunctionPointerType) {
                FunctionPointerType ori = original as FunctionPointerType;

                FunctionPointerType fnptr = new FunctionPointerType (
                    ori.HasThis,
                    ori.ExplicitThis,
                    ori.CallingConvention,
                    new MethodReturnType (ImportTypeReference (ori.ReturnType.ReturnType, context)));

                foreach (ParameterDefinition parameter in ori.Parameters)
                    fnptr.Parameters.Add (new ParameterDefinition (ImportTypeReference (parameter.ParameterType, context)));

                typeSpec = fnptr;
            } else
                throw new ReflectionException ("Unknown element type: {0}", original.GetType ().Name);

            return typeSpec;
        }
コード例 #23
0
ファイル: MemberRefFinder.cs プロジェクト: ldh0227/de4dot
 void doFunctionPointerType(FunctionPointerType functionPointerType)
 {
     bool present;
     if (functionPointerTypes.TryGetValue(functionPointerType, out present))
         return;
     functionPointerTypes[functionPointerType] = true;
     addFunctionPointerType(functionPointerType);
 }
コード例 #24
0
ファイル: SignatureWriter.cs プロジェクト: xyz91/EasyAop
        public void WriteTypeSignature(TypeReference type)
        {
            if (type == null)
            {
                throw new ArgumentNullException();
            }
            ElementType etype = type.etype;

            switch (etype)
            {
            case ElementType.Var:
            case ElementType.MVar:
            {
                GenericParameter obj = (GenericParameter)type;
                WriteElementType(etype);
                int position = obj.Position;
                if (position == -1)
                {
                    throw new NotSupportedException();
                }
                base.WriteCompressedUInt32((uint)position);
                break;
            }

            case ElementType.GenericInst:
            {
                GenericInstanceType genericInstanceType = (GenericInstanceType)type;
                WriteElementType(ElementType.GenericInst);
                WriteElementType(genericInstanceType.IsValueType ? ElementType.ValueType : ElementType.Class);
                base.WriteCompressedUInt32(MakeTypeDefOrRefCodedRID(genericInstanceType.ElementType));
                WriteGenericInstanceSignature(genericInstanceType);
                break;
            }

            case ElementType.Ptr:
            case ElementType.ByRef:
            case ElementType.Sentinel:
            case ElementType.Pinned:
            {
                TypeSpecification typeSpecification = (TypeSpecification)type;
                WriteElementType(etype);
                WriteTypeSignature(typeSpecification.ElementType);
                break;
            }

            case ElementType.FnPtr:
            {
                FunctionPointerType method = (FunctionPointerType)type;
                WriteElementType(ElementType.FnPtr);
                WriteMethodSignature(method);
                break;
            }

            case ElementType.CModReqD:
            case ElementType.CModOpt:
            {
                IModifierType type2 = (IModifierType)type;
                WriteModifierSignature(etype, type2);
                break;
            }

            case ElementType.Array:
            {
                ArrayType arrayType = (ArrayType)type;
                if (!arrayType.IsVector)
                {
                    WriteArrayTypeSignature(arrayType);
                }
                else
                {
                    WriteElementType(ElementType.SzArray);
                    WriteTypeSignature(arrayType.ElementType);
                }
                break;
            }

            case ElementType.None:
                WriteElementType(type.IsValueType ? ElementType.ValueType : ElementType.Class);
                base.WriteCompressedUInt32(MakeTypeDefOrRefCodedRID(type));
                break;

            default:
                if (TryWriteElementType(type))
                {
                    break;
                }
                throw new NotSupportedException();
            }
        }