Inheritance: MemberReference
Beispiel #1
0
 public FieldInfo resolve(FieldReference fieldReference)
 {
     var resolver = getTypeResolver(fieldReference.DeclaringType);
     if (resolver != null)
         return resolver.resolve(fieldReference);
     return resolveGlobalField(fieldReference);
 }
 public WeakEventWeaver(FieldReference eventDelegate, ModuleImporter moduleimporter)
 {
     _eventDelegate = eventDelegate.Resolve();
     _moduleimporter = moduleimporter;
     _closedGenericEventHandler = moduleimporter.GetClosedEventHandlerT(eventDelegate.FieldType.GetEventArgsType());
     _isGenericHandler = _closedGenericEventHandler.FullName.Equals(eventDelegate.FieldType.FullName);
 }
Beispiel #3
0
 public MField getField(FieldReference fieldReference)
 {
     initFields();
     MField field;
     fieldReferenceToField.TryGetValue(new FieldReferenceKey(fieldReference), out field);
     return field;
 }
 public static FieldDefinition FindMatchingField(this TypeDefinition tdef, FieldReference fref, bool throwOnNotFound = false)
 {
     if (throwOnNotFound)
         return tdef.Fields.First(fdef => fdef.FieldMatches(fref));
     else
         return tdef.Fields.FirstOrDefault(fdef => fdef.FieldMatches(fref));
 }
    MethodDefinition InjectMethod(TypeDefinition targetType, string eventInvokerName, FieldReference propertyChangingField)
    {
        var method = new MethodDefinition(eventInvokerName, GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
		method.Parameters.Add(new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String));

        var handlerVariable = new VariableDefinition(PropChangingHandlerReference);
        method.Body.Variables.Add(handlerVariable);
		var boolVariable = new VariableDefinition(ModuleDefinition.TypeSystem.Boolean);
        method.Body.Variables.Add(boolVariable);

        var instructions = method.Body.Instructions;

        var last = Instruction.Create(OpCodes.Ret);
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Ldfld, propertyChangingField)); 
        instructions.Add(Instruction.Create(OpCodes.Stloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldnull));
        instructions.Add(Instruction.Create(OpCodes.Ceq));
        instructions.Add(Instruction.Create(OpCodes.Stloc_1));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
        instructions.Add(Instruction.Create(OpCodes.Brtrue_S, last));
        instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
        instructions.Add(Instruction.Create(OpCodes.Newobj, ComponentModelPropertyChangingEventConstructorReference));
        instructions.Add(Instruction.Create(OpCodes.Callvirt, ComponentModelPropertyChangingEventHandlerInvokeReference));


        instructions.Add(last);
        method.Body.InitLocals = true;
        targetType.Methods.Add(method);
        return method;
    }
Beispiel #6
0
        private static MethodDefinition CreateEventRemoveMethod(AssemblyDefinition assemblyDefinition,
            FieldReference propertyChangedFieldDefinition)
        {
            var removeMethodDefinition = assemblyDefinition.MainModule.Import(typeof(Delegate).GetMethod("Remove",
                new[] { typeof(Delegate), typeof(Delegate) }));

            var removePropertyChanged = new MethodDefinition("remove_PropertyChanged", MethodAttributes.Public |
                                                                                       MethodAttributes.SpecialName |
                                                                                       MethodAttributes.NewSlot |
                                                                                       MethodAttributes.HideBySig |
                                                                                       MethodAttributes.Virtual |
                                                                                       MethodAttributes.Final
                , assemblyDefinition.MainModule.Import(typeof(void)));

            removePropertyChanged.Overrides.Add(
                assemblyDefinition.MainModule.Import(typeof(INotifyPropertyChanged).GetMethod("remove_PropertyChanged")));
            removePropertyChanged.Parameters.Add(
                new ParameterDefinition(assemblyDefinition.MainModule.Import(typeof(PropertyChangedEventHandler))));
            var il = removePropertyChanged.Body.GetILProcessor();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, propertyChangedFieldDefinition);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Call, removeMethodDefinition);
            il.Emit(OpCodes.Castclass, assemblyDefinition.MainModule.Import(typeof(PropertyChangedEventHandler)));
            il.Emit(OpCodes.Stfld, propertyChangedFieldDefinition);
            il.Emit(OpCodes.Ret);
            return removePropertyChanged;
        }
        public void AddConstructor(TypeDefinition adapterType,
           FieldReference targetField)
        {
            var adapterCtor = adapterType.AddDefaultConstructor();
            var adapterParameter = new ParameterDefinition(_targetDependency);
            adapterCtor.Parameters.Add(adapterParameter);

            // HACK: Remove the ret instruction from the default constructor and replace it with
            // the field setter
            var adapterBody = adapterCtor.Body;
            var adapterInstructions = adapterBody.Instructions.Cast<Instruction>().Where(i => i.OpCode != OpCodes.Ret).ToArray();
            adapterBody.Instructions.Clear();

            // Copy the old instructions
            var IL = adapterBody.CilWorker;
            foreach (var instruction in adapterInstructions)
            {
                IL.Append(instruction);
            }

            IL.Emit(OpCodes.Ldarg_0);
            IL.Emit(OpCodes.Ldarg, adapterParameter);
            IL.Emit(OpCodes.Stfld, targetField);
            IL.Emit(OpCodes.Ret);
        }
        internal FieldReferenceReflectionEmitter(MemberReferenceExpression memberReferenceExpression,
                                                 Type target,
                                                 MemberInfo member,
                                                 ILGenerator ilGenerator,
                                                 IOpCodeIndexer instructionsIndexer,
                                                 IAstVisitor<ILGenerator, AstNode> visitor,
                                                 List<LocalBuilder> locals,
                                                 bool isSetter = false)
            : base(memberReferenceExpression, target, member, ilGenerator, instructionsIndexer, visitor, locals) {

            FieldReference = MemberReference.Annotation<FieldReference>();
            IsByRef = MemberReference.Parent is DirectionExpression;
            Type = FieldReference.FieldType.GetActualType();
            FieldInfo = FieldReference.GetActualField();
            NonPublic = !((FieldInfo.Attributes & (_publicFieldAttributes)) == _publicFieldAttributes);

            if (isSetter) {
                _emitAction = EmitStoreFieldReference;
                _emitPrivateAction = EmitPrivateStoreFieldReference;
            }
            else {
                _emitAction = EmitLoadFieldReference;
                _emitPrivateAction = EmitPrivateLoadFieldReference;
            }
        }
        public FieldReference convert(FieldReference fieldRef)
        {
            if (isInOurModule(fieldRef))
                return tryGetFieldDefinition(fieldRef);

            return new FieldReference(fieldRef.Name, convert(fieldRef.FieldType), convert(fieldRef.DeclaringType));
        }
    public static bool AlreadyHasEquality(PropertyDefinition propertyDefinition, FieldReference backingFieldReference)
    {
        var instructions = propertyDefinition.SetMethod.Body.Instructions;
        var list = instructions.Where(IsNotNop).ToList();
        if (list.Count < 4)
        {
            return false;
        }
        var firstFive = list.Take(5).ToList();
        if (firstFive.All(x => x.OpCode != OpCodes.Ldarg_1))
        {
            return false;
        }
        if (firstFive.All(x => x.OpCode != OpCodes.Ldarg_0))
        {
            return false;
        }
        if (firstFive.All(x => !x.IsEquality()))
        {
            return false;
        }

        if (firstFive.Any(x => x.Operand == backingFieldReference))
        {
            return true;
        }
        if (firstFive.Any(x => x.Operand == propertyDefinition))
        {
            return true;
        }
        return false;
    }
Beispiel #11
0
 private static FieldReference FieldReferenceFor(FieldReference field)
 {
     if (!IsGeneric(field.DeclaringType)) return field;
     var reference = new FieldReference(field.Name, field.FieldType);
     reference.DeclaringType = GenericReferenceFor(field.DeclaringType);
     return reference;
 }
        private void WeaveDependencyProperty(MethodBody staticCtorBody, FieldReference field, PropertyDefinition property)
        {
            var assembly = property.DeclaringType.Module.Assembly;
            var propertyType = assembly.ImportType(Type.GetType(property.PropertyType.FullName));
            var getTypeFromHandle = assembly.ImportMethod(typeof(Type).GetMethod("GetTypeFromHandle"));
            var register = assembly.ImportMethod(typeof(DependencyProperty).GetMethod("Register", new[] { typeof(string), typeof(Type), typeof(Type) }));

            // ignore previously weaved DPs
            if (staticCtorBody.Instructions.Any(i => i.Operand != null && i.Operand.ToString() == field.ToString()))
            {
                return;
            }

            var ret = staticCtorBody.Instructions.Last();
            if (ret.OpCode != OpCodes.Ret)
                throw new InvalidOperationException("The last instruction should be OpCode.Ret");

            HasChanges = true;

            var proc = staticCtorBody.GetILProcessor();
            proc.InsertBefore(ret, proc.Create(OpCodes.Ldstr, property.Name));
            proc.InsertBefore(ret, proc.Create(OpCodes.Ldtoken, propertyType));
            proc.InsertBefore(ret, proc.Create(OpCodes.Call, getTypeFromHandle));
            proc.InsertBefore(ret, proc.Create(OpCodes.Ldtoken, property.DeclaringType));
            proc.InsertBefore(ret, proc.Create(OpCodes.Call, getTypeFromHandle));
            proc.InsertBefore(ret, proc.Create(OpCodes.Call, register));
            proc.InsertBefore(ret, proc.Create(OpCodes.Stsfld, field));
        }
Beispiel #13
0
        private static void EmitArchsInit(MethodBody body, FieldReference archRef, Action<Instruction> emit)
        {
            var module = body.Method.Module;
              GenericInstanceType dictStrStrRef = (GenericInstanceType)archRef.FieldType;
              TypeReference dictOpenRef = dictStrStrRef.ElementType;
              GenericInstanceType iEqCompStrRef = new GenericInstanceType(module.Import(typeof(IEqualityComparer<>)));
              iEqCompStrRef.GenericArguments.Add(dictOpenRef.GenericParameters[0]);
              MethodReference dictStrStrCtor = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, ".ctor", null, iEqCompStrRef);
              MethodReference dictAddRef = CecilUtils.ImportInstanceMethodRef(module, dictStrStrRef, "Add", null, dictOpenRef.GenericParameters[0], dictOpenRef.GenericParameters[1]);

            // Variables
              body.Variables.Add(new VariableDefinition(dictStrStrRef));
              int varIdx = body.Variables.Count - 1;
            Instruction varSt = CecilUtils.ShortestStloc(varIdx);
            Instruction varLd = CecilUtils.ShortestLdloc(varIdx);

            emit(Instruction.Create(OpCodes.Ldnull));
            emit(Instruction.Create(OpCodes.Newobj, dictStrStrCtor));
              emit(varSt.Clone());
              emit(varLd.Clone());
              emit(Instruction.Create(OpCodes.Stsfld, archRef));
              Action<string, string> emitAddPair = (k, v) =>
              {
              emit(varLd.Clone());
                emit(Instruction.Create(OpCodes.Ldstr, k));
                emit(Instruction.Create(OpCodes.Ldstr, v));
            emit(Instruction.Create(OpCodes.Callvirt, dictAddRef));
              };
              emitAddPair("x86", "Win32");
              emitAddPair("AMD64", "x64");
              emitAddPair("IA64", "Itanium");
              emitAddPair("ARM", "WinCE");
        }
Beispiel #14
0
        public override void Visit(Model.Bytecode.LoadFieldInstruction instruction)
        {
            Cecil.FieldReference       fieldReference = referenceGenerator.FieldReference(instruction.Field);
            Mono.Cecil.Cil.Instruction cilIns;
            if (instruction.Operation == Model.Bytecode.LoadFieldOperation.Content)
            {
                if (!instruction.Field.IsStatic)
                {
                    cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Ldfld, fieldReference);
                }
                else
                {
                    cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Ldsfld, fieldReference);
                }
            }
            else
            {
                if (!instruction.Field.IsStatic)
                {
                    cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Ldflda, fieldReference);
                }
                else
                {
                    cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Ldsflda, fieldReference);
                }
            }

            Result = new List <Mono.Cecil.Cil.Instruction>()
            {
                cilIns
            };
        }
Beispiel #15
0
 public FieldNode(string name, SignatureNode type, bool isStatic = false, FieldReference field = null)
 {
     Name = name;
       Field = field;
       Type = type;
       Static = isStatic;
 }
 public static FieldDefinition TryResolve(this IMetadataResolver metadataResolver, FieldReference fieldRef)
 {
     try {
         return metadataResolver.Resolve(fieldRef);
     } catch (AssemblyResolutionException) { }
     return null;
 }
    MethodDefinition InjectValueConstructor(FieldReference valueField)
    {
      const MethodAttributes methodAttributes = MethodAttributes.SpecialName |
        MethodAttributes.RTSpecialName | MethodAttributes.HideBySig;

      // public void .ctor(object value)
      var constructor = new MethodDefinition(".ctor", methodAttributes,
        ModuleDefinition.TypeSystem.Void);
      constructor.Parameters.Add(new ParameterDefinition(
        ModuleDefinition.TypeSystem.Object));

      // base.ctor();
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Call,
        new MethodReference(".ctor", ModuleDefinition.TypeSystem.Void,
          ModuleDefinition.TypeSystem.Object) {HasThis = true}));

      // this._value = value;
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, valueField));

      constructor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));

      return constructor;
    }
 public ExprFieldAddress(Ctx ctx, Expr obj, FieldReference field)
     : base(ctx) {
     this.Obj = obj;
     this.Field = field;
     this.ElementType = field.FieldType.FullResolve(field);
     this.type = this.ElementType.MakePointer();
 }
Beispiel #19
0
		public FieldDefinition Resolve (FieldReference field)
		{
			TypeDefinition type = Resolve (field.DeclaringType);
			if (type == null)
				return null;

			return type.HasFields ? GetField (type.Fields, field) : null;
		}
Beispiel #20
0
        public FieldInfo Resolve(FieldReference fieldReference)
        {
            Type declaringType = _typeResolver.Resolve(fieldReference.DeclaringType);
            var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
            FieldInfo result = declaringType.GetField(fieldReference.Name, flags);

            return result;
        }
Beispiel #21
0
		public Instruction Create (OpCode opcode, FieldReference field)
		{
			if (opcode.OperandType != OperandType.InlineField &&
				opcode.OperandType != OperandType.InlineTok)
				throw new ArgumentException ("opcode");

			return FinalCreate (opcode, field);
		}
Beispiel #22
0
        public static FieldInfo getRtField(FieldReference fieldReference)
        {
            var mfield = getField(fieldReference);
            if (mfield != null)
                return mfield.fieldInfo;

            return Resolver.resolve(fieldReference);
        }
Beispiel #23
0
        private AnalysisNet.IInstruction ProcessStoreField(Cecil.Cil.Instruction op)
        {
            Cecil.FieldReference             cciField = op.Operand as Cecil.FieldReference;//op.Operand as Cci.IFieldReference;
            AnalysisNet.Types.FieldReference ourField = typeExtractor.ExtractField(cciField, op.OpCode.Code == Cecil.Cil.Code.Stsfld);

            AnalysisNetBytecode.StoreFieldInstruction instruction = new AnalysisNetBytecode.StoreFieldInstruction((uint)op.Offset, ourField);
            return(instruction);
        }
 public void AddExternalFieldReference(MethodDefinition method, FieldReference attr)
 {
     // TODO: Why duplication? =\
     if (!_externalFieldReferences.ContainsKey(method))
     {
         _externalFieldReferences.Add(method, attr);
     }
 }
    private static void AddICollectionTProxy(ModuleDefinition moduleDefinition, TypeDefinition type, GenericInstanceType collectionT)
    {
        var itemType = collectionT.GenericArguments[0];
        var itemArray = itemType.MakeArrayType();

        var proxyType = CreateProxy(moduleDefinition, type);
        TypeReference proxyTypeRef = proxyType;
        if (type.HasGenericParameters)
            proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());

        var field = proxyType.Fields[0];
        var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef);

        var countProperty = type.Properties.First(p => p.Name == "Count" || p.Name == "System.Collections.ICollection.Count");
        MethodReference countMethod = countProperty.GetMethod;
        MethodReference copyToMethod = type.Methods.First(p => p.Name == "CopyTo" || p.Name == "System.Collections.ICollection.CopyTo");

        if (type.HasGenericParameters)
        {
            countMethod = countMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
            copyToMethod = copyToMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(gp => new GenericParameter(gp.Position, gp.Type, gp.Module)).ToArray());
        }

        var getMethod = new MethodDefinition("get_Items", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, itemArray);
        var getMethodBody = getMethod.Body;

        var localItems = new VariableDefinition("items", itemArray);
        getMethodBody.Variables.Add(localItems);

        getMethodBody.SimplifyMacros();
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, countMethod));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Newarr, itemType));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Stloc, localItems));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldlen));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Conv_I4));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, copyToMethod));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems));
        getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ret));
        getMethodBody.InitLocals = true;
        getMethodBody.OptimizeMacros();

        proxyType.Methods.Add(getMethod);

        var property = new PropertyDefinition("Items", PropertyAttributes.None, itemArray);
        property.GetMethod = getMethod;
        var debuggerBrowsableAttribute = new CustomAttribute(ReferenceFinder.DebuggerBrowsableAttributeCtor);
        debuggerBrowsableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(ReferenceFinder.DebuggerBrowsableStateType, DebuggerBrowsableState.RootHidden));
        property.CustomAttributes.Add(debuggerBrowsableAttribute);
        proxyType.Properties.Add(property);

        AddDebuggerTypeProxyAttribute(type, proxyType);
    }
        public static FieldReference MakeGeneric(this FieldReference field, TypeReference declaringType)
        {
            var reference = new FieldReference(field.Name, field.FieldType)
            {
                DeclaringType = declaringType.MakeGenericIfRequired(),
            };

            return reference;
        }
Beispiel #27
0
 public static MField getField(FieldReference fieldReference)
 {
     if (fieldReference == null)
         return null;
     var module = getModule(fieldReference.DeclaringType.Scope);
     if (module != null)
         return module.getField(fieldReference);
     return null;
 }
 public FieldReference Resolve(FieldReference field)
 {
     TypeReference declaringType = this.Resolve(field.DeclaringType);
     if (declaringType == field.DeclaringType)
     {
         return field;
     }
     return new FieldReference(field.Name, field.FieldType, declaringType);
 }
Beispiel #29
0
        FieldReference IImportMapper.MapField(FieldReference fref)
        {
            if (fref.DeclaringType.Scope.Name == _targetModule.Name)
                return fref;

            foreach (var cInj in _classInjectees.Where(i => i is TypeAddInjectee).Cast<TypeAddInjectee>())
            {
                var injCecilType = cInj.GetInjecteeCecilType(_injectionModule);
                if (!fref.DeclaringType.TypeMatches(injCecilType))
                    continue;

                var type = _targetModule.FindMatchingType(cInj.Import(_targetModule, fref.DeclaringType.Scope, this));
                if (type == null)
                    throw new InvalidOperationException("Unable to find add class for: " + fref.DeclaringType);

                var field = type.FindMatchingField(fref, true);
                var decl = FixTypeImport(_targetModule, fref.DeclaringType);
                return new FieldReference(field.Name, field.FieldType, decl);
            }

            foreach (var cInj in _classInjectees.Where(i => i is TypeTargetInjecteeBase))
            {
                var injCecilType = cInj.GetInjecteeCecilType(_injectionModule);
                if (!fref.DeclaringType.TypeMatches(injCecilType))
                    continue;

                var classInjectee = cInj as TypeInjectee;
                var classStubInjectee = cInj as TypeStubInjectee;

                if (classInjectee != null)
                {
                    foreach (var inj in classInjectee.Injectees.Where(i => i is FieldInjecteeBase).Cast<FieldInjecteeBase>())
                    {
                        if (!fref.FieldMatches(inj.InjecteeField))
                            continue;

                        if (inj is FieldStubInjectee)
                            return ((FieldStubInjectee)inj).GetTargetCecilDefintion(_targetModule);
                    }
                }
                else if (classStubInjectee != null)
                {
                    var stubTargetClass = classStubInjectee.TargetType;
                    var tdef = _targetModule.FindMatchingType(stubTargetClass);
                    if (tdef == null)
                        throw new InvalidOperationException("Type not found for: " + stubTargetClass.Name);

                    var target = tdef.FindMatchingField(fref);
                    if (target == null)
                        throw new InvalidOperationException("Target field not found for: " + stubTargetClass.Name);

                    return target;
                }
            }

            return null;
        }
 public Coupling(string to, bool isReadOnly, bool ignoreWalk, bool isSelfCall, FieldReference fieldReference, MemberReference actualReference)
 {
     To = to;
     FieldReference = fieldReference;
     IsSelfCall = isSelfCall;
     IsReadOnly = isReadOnly;
     IgnoreWalk = ignoreWalk;
     ActualReference = actualReference;
 }
 public MemberAccess(MemberReference memberReference, bool isReadOnly, bool isSelfCall, FieldReference onField, MethodReference actualMethodDefinition, bool isLocalVariable)
 {
     MemberReference = memberReference;
     IsLocalVariable = isLocalVariable;
     OnField = onField;
     IsSelfCall = isSelfCall;
     IsReadOnly = isReadOnly;
     ActualMethodDefinition = actualMethodDefinition;
 }
Beispiel #32
0
        public static bool compareFields(FieldInfo a, FieldReference b)
        {
            if (a == null && b == null)
                return true;
            if (a == null || b == null)
                return false;

            return a.Name == b.Name &&
                compareTypes(a.FieldType, b.FieldType);
        }
Beispiel #33
0
        private AnalysisNet.IInstruction ProcessLoadField(Cecil.Cil.Instruction op)
        {
            AnalysisNetBytecode.LoadFieldOperation operation = OperationHelper.ToLoadFieldOperation(op.OpCode.Code);
            Cecil.FieldReference cciField = op.Operand as Cecil.FieldReference;
            bool isStatic = op.OpCode.Code == Cecil.Cil.OpCodes.Ldsfld.Code || op.OpCode.Code == Cecil.Cil.OpCodes.Ldsflda.Code;

            AnalysisNet.Types.FieldReference ourField = typeExtractor.ExtractField(cciField, isStatic);

            AnalysisNetBytecode.LoadFieldInstruction instruction = new AnalysisNetBytecode.LoadFieldInstruction((uint)op.Offset, operation, ourField);
            return(instruction);
        }
Beispiel #34
0
 public AnalysisNet.Types.FieldReference ExtractField(Cecil.FieldReference field, bool isStatic)
 {
     (Cecil.FieldReference, bool)key = ValueTuple.Create(field, isStatic);
     return(performanceCache.GetOrCreate(key, (cacheEntry) =>
     {
         AnalysisNet.Types.FieldReference analysisNetField = new AnalysisNet.Types.FieldReference(field.Name, ExtractType(field.FieldType))
         {
             ContainingType = (AnalysisNet.Types.IBasicType)ExtractType(field.DeclaringType),
             IsStatic = isStatic
         };
         return analysisNetField;
     }));
 }
Beispiel #35
0
        /// <summary>
        /// Gets the converted name of the given field.
        /// </summary>
        public static string GetConvertedName(FieldReference field)
        {
            var name         = field.Name;
            var originalName = name;

            name = name.Replace('<', '_');
            name = name.Replace('>', '_');
            if (name != originalName)
            {
                // Add hash to ensure unique
                name = name + GetHashPostfix(originalName);
            }
            return(name);
        }
Beispiel #36
0
        public override void Visit(Model.Bytecode.StoreFieldInstruction instruction)
        {
            Mono.Cecil.Cil.Instruction cilIns;
            Cecil.FieldReference       fieldReference = referenceGenerator.FieldReference(instruction.Field);

            if (!instruction.Field.IsStatic)
            {
                cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Stfld, fieldReference);
            }
            else
            {
                cilIns = processor.Create(Mono.Cecil.Cil.OpCodes.Stsfld, fieldReference);
            }

            Result = new List <Mono.Cecil.Cil.Instruction>()
            {
                cilIns
            };
        }
Beispiel #37
0
        public Cecil.FieldReference FieldReference(AnalysisNet.Types.IFieldReference fieldReference)
        {
            if (fieldsCache.TryGetValue(fieldReference, out Cecil.FieldReference cecilField))
            {
                return(cecilField);
            }

            Cecil.TypeReference cecilType     = TypeReference(fieldReference.Type);
            Cecil.TypeReference declaringType = TypeReference(fieldReference.ContainingType);

            cecilField = new Cecil.FieldReference(
                fieldReference.Name,
                cecilType,
                declaringType
                );

            fieldsCache[fieldReference] = cecilField;

            return(cecilField);
        }
Beispiel #38
0
        public override void Visit(Model.Bytecode.LoadTokenInstruction instruction)
        {
            Cecil.Cil.Instruction cilIns;
            if (instruction.Token is AnalysisNet.Types.IType type)
            {
                Cecil.TypeReference token = referenceGenerator.TypeReference(type);
                cilIns = processor.Create(Cecil.Cil.OpCodes.Ldtoken, token);
            }
            else if (instruction.Token is AnalysisNet.Types.IFieldReference field)
            {
                Cecil.FieldReference token = referenceGenerator.FieldReference(field);
                cilIns = processor.Create(Cecil.Cil.OpCodes.Ldtoken, token);
            }
            else
            {
                throw new NotImplementedException();
            }

            Result = new List <Mono.Cecil.Cil.Instruction>()
            {
                cilIns
            };
        }
Beispiel #39
0
 private AnalysisNet.Types.VisibilityKind ExtractVisibilityKind(Cecil.FieldReference field)
 {
     return(AnalysisNet.Types.VisibilityKind.Public);
 }
Beispiel #40
0
 public static FieldReference ImportEx(this ModuleDefinition module, FieldReference field)
 {
     return(module.ImportReference(field));
 }
Beispiel #41
0
 public FieldReference ImportReference(FieldReference field)
 {
     return(ImportReference(field, null));
 }
Beispiel #42
0
 public static FieldReference ImportEx(this ModuleDefinition module, FieldReference field, IGenericParameterProvider context)
 {
     return(module.ImportReference(field, context));
 }
 internal static TypeReference ResolveFieldTypeIfNeeded(FieldReference fieldReference)
 {
     return(ResolveIfNeeded(null, fieldReference.DeclaringType as GenericInstanceType, fieldReference.FieldType));
 }
Beispiel #44
0
        void EmitInstruction(ILGenerator il, OpCode opcode, Cecil.FieldReference operandField)
        {
            var ourType = FindType(operandField.DeclaringType);

            il.Emit(opcode, ourType.GetField(operandField.Name, bindingAttr: bflags_all));
        }
        public static string Disassemble(DomCecilMethod method, bool markup)
        {
            if (method.MethodDefinition.IsPInvokeImpl)
            {
                return(GettextCatalog.GetString("Method is P/Invoke"));
            }
            if (method.MethodDefinition.Body == null)
            {
                IType type = method.DeclaringType;
                return(type == null ||  type.ClassType == ClassType.Interface ? GettextCatalog.GetString("Interface method") : GettextCatalog.GetString("Abstract method"));
            }

            StringBuilder result = new StringBuilder();

            foreach (Instruction instruction in method.MethodDefinition.Body.Instructions)
            {
                if (markup)
                {
                    result.Append("<b>");
                }
                result.Append(GetInstructionOffset(instruction));
                result.Append(markup ? ":</b> " : ": ");
                result.Append(instruction.OpCode);
                if (markup)
                {
                    result.Append("<i>");
                }
                if (instruction.Operand != null)
                {
                    result.Append(' ');
                    if (instruction.Operand is string)
                    {
                        result.Append('"');
                        result.Append(AssemblyBrowserWidget.FormatText(instruction.Operand.ToString()));
                        result.Append('"');
                    }
                    else if (instruction.Operand is Mono.Cecil.Cil.Instruction)
                    {
                        result.Append(GetInstructionOffset((Mono.Cecil.Cil.Instruction)instruction.Operand));
                    }
                    else if (instruction.Operand is Mono.Cecil.TypeDefinition)
                    {
                        AppendLink(result,
                                   new DomCecilType((Mono.Cecil.TypeDefinition)instruction.Operand).HelpUrl,
                                   instruction.Operand.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.MethodDefinition)
                    {
                        Mono.Cecil.MethodDefinition md = instruction.Operand as Mono.Cecil.MethodDefinition;
                        AppendLink(result,
                                   new DomCecilMethod(md)
                        {
                            DeclaringType = new DomCecilType(md.DeclaringType)
                        }.HelpUrl,
                                   instruction.Operand.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.FieldDefinition)
                    {
                        Mono.Cecil.FieldDefinition fd = instruction.Operand as Mono.Cecil.FieldDefinition;
                        AppendLink(result,
                                   new DomCecilField(fd)
                        {
                            DeclaringType = new DomCecilType(fd.DeclaringType)
                        }.HelpUrl,
                                   instruction.Operand.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.PropertyDefinition)
                    {
                        Mono.Cecil.PropertyDefinition pd = instruction.Operand as Mono.Cecil.PropertyDefinition;
                        AppendLink(result,
                                   new DomCecilProperty(pd)
                        {
                            DeclaringType = new DomCecilType(pd.DeclaringType)
                        }.HelpUrl,
                                   instruction.Operand.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.TypeReference)
                    {
                        AppendLink(result,
                                   "T:" + ((TypeReference)instruction.Operand).FullName,
                                   instruction.Operand.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.MethodReference)
                    {
                        Mono.Cecil.MethodReference mr = instruction.Operand as Mono.Cecil.MethodReference;
                        StringBuilder id            = new StringBuilder(mr.DeclaringType.ToString());
                        bool          isConstructor = mr.Name == ".ctor";
                        if (!isConstructor)
                        {
                            id.Append("." + mr.Name);
                        }
                        id.Append("(");
                        for (int i = 0; i < mr.Parameters.Count; i++)
                        {
                            if (i > 0)
                            {
                                id.Append(',');
                            }
                            id.Append(mr.Parameters[i].ParameterType.FullName);
                        }
                        id.Append(")");
                        AppendLink(result, (isConstructor ? "C:" : "M:") + id, id.ToString());
                    }
                    else if (instruction.Operand is Mono.Cecil.FieldReference)
                    {
                        Mono.Cecil.FieldReference fr = instruction.Operand as Mono.Cecil.FieldReference;
                        string id = fr.DeclaringType + "." + fr.Name;
                        AppendLink(result, "F:" + id, id);
                    }
                    else if (instruction.Operand is Mono.Cecil.PropertyReference)
                    {
                        Mono.Cecil.PropertyReference pr = instruction.Operand as Mono.Cecil.PropertyReference;
                        string id = pr.DeclaringType + "." + pr.Name;
                        AppendLink(result, "P:" + id, id);
                    }
                    else
                    {
                        result.Append(AssemblyBrowserWidget.FormatText(instruction.Operand.ToString()));
                    }
                }
                if (markup)
                {
                    result.Append("</i>");
                }
                result.AppendLine();
            }
            result.AppendLine();
            return(result.ToString());
        }
Beispiel #46
0
 public FieldReference Import(FieldReference field, IGenericParameterProvider context)
 {
     return(ImportReference(field, context));
 }
Beispiel #47
0
 static public int Emit(this MethodBody body, OpCode instruction, FieldReference field)
 {
     return(body.Add(Instruction.Create(instruction, field)));
 }
        static public FieldReference Reference(this GenericInstanceType genericInstanceType, FieldReference field)
        {
            var _fieldReference = new FieldReference(field.Name, field.FieldType, genericInstanceType);

            return(_fieldReference);
        }
Beispiel #49
0
 public static FieldReference MakeGeneric(this FieldReference self, params TypeReference[] arguments)
 {
     return(new FieldReference(self.Name, self.FieldType, self.DeclaringType.MakeGenericType(arguments)));
 }
Beispiel #50
0
 internal FieldDefinition Resolve(FieldReference field)
 {
     return(MetadataResolver.Resolve(field));
 }
Beispiel #51
0
 public FieldReference Import(FieldReference field, MethodReference context)
 {
     return(Import(field, (IGenericParameterProvider)context));
 }
Beispiel #52
0
        static FieldDefinition GetField(IAssemblyResolver resolver, TypeDefinition type, FieldReference reference)
        {
            while (type != null)
            {
                var field = GetField(type.Fields, reference);
                if (field != null)
                {
                    return(field);
                }

                if (type.BaseType == null)
                {
                    return(null);
                }

                type = Resolve(resolver, type.BaseType);
            }

            return(null);
        }
Beispiel #53
0
 public virtual FieldReference ImportReference(FieldReference field, IGenericParameterProvider context)
 {
     Mixin.CheckField(field);
     return(ImportField(field, ImportGenericContext.For(context)));
 }