Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EventEmitter"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="evt">The evt.</param>
        public EventEmitter(TypeEmitter parent, EventReference evt)
        {
            Parent = parent;
            Target = evt as EventDefinition ?? evt.Resolve();

            if (Target.AddMethod != null)
            {
                add = new MethodEmitter(parent, Target.AddMethod);
            }
            else
            {
                var existing = parent.Target.GetMethod($"add_{Target.Name}", parent.Context.Module.TypeSystem.Void, new[] { evt.EventType }, new GenericParameter[0]);
                if (existing != null)
                {
                    Target.AddMethod = existing.Resolve();
                    add = new MethodEmitter(parent, Target.AddMethod);
                }
            }

            if (Target.RemoveMethod != null)
            {
                remove = new MethodEmitter(parent, Target.RemoveMethod);
            }
            else
            {
                var existing = parent.Target.GetMethod($"remove_{Target.Name}", parent.Context.Module.TypeSystem.Void, new[] { evt.EventType }, new GenericParameter[0]);
                if (existing != null)
                {
                    Target.RemoveMethod = existing.Resolve();
                    remove = new MethodEmitter(parent, Target.RemoveMethod);
                }
            }
        }
Esempio n. 2
0
        public MemberIdentifier(ITypeInfoSource ti, EventReference er)
        {
            Type                 = MemberType.Event;
            Name                 = er.Name;
            ReturnType           = er.EventType;
            GenericArgumentCount = 0;
            ParameterTypes       = null;
            ti.CacheProxyNames(er);

            var ed = er.Resolve();

            if (ed != null)
            {
                if (ed.AddMethod != null)
                {
                    IsStatic = ed.AddMethod.IsStatic;
                }
                else if (ed.RemoveMethod != null)
                {
                    IsStatic = ed.RemoveMethod.IsStatic;
                }
                else
                {
                    // FIXME
                    IsStatic = false;
                }
            }
            else
            {
                // FIXME
                IsStatic = false;
            }

            HashCode = Type.GetHashCode() ^ Name.GetHashCode();
        }
Esempio n. 3
0
        static MethodAttributes GetEvtVisibility(EventReference evt)
        {
            MethodAttributes ret        = MethodAttributes.Public;
            EventDefinition  Definition = evt.Resolve();

            if (Definition != null)
            {
                MethodReference  addMethod    = Definition.AddMethod;
                MethodDefinition addDecl      = (addMethod == null) ? null : addMethod.Resolve();
                MethodReference  removeMethod = Definition.RemoveMethod;
                MethodDefinition removeDecl   = (removeMethod == null) ? null : removeMethod.Resolve();
                MethodReference  invokeMethod = Definition.InvokeMethod;
                MethodDefinition invokeDecl   = (invokeMethod == null) ? null : invokeMethod.Resolve();
                if (((addDecl != null) && (removeDecl != null)) && (invokeDecl != null))
                {
                    if (((addDecl.Attributes & MethodAttributes.MemberAccessMask) == (removeDecl.Attributes & MethodAttributes.MemberAccessMask)) && ((addDecl.Attributes & MethodAttributes.MemberAccessMask) == (invokeDecl.Attributes & MethodAttributes.MemberAccessMask)))
                    {
                        return(addDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                }
                else if ((addDecl != null) && (removeDecl != null))
                {
                    if ((addDecl.Attributes & MethodAttributes.MemberAccessMask) == (removeDecl.Attributes & MethodAttributes.MemberAccessMask))
                    {
                        return(addDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                }
                else if ((addDecl != null) && (invokeDecl != null))
                {
                    if ((addDecl.Attributes & MethodAttributes.MemberAccessMask) == (invokeDecl.Attributes & MethodAttributes.MemberAccessMask))
                    {
                        return(addDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                }
                else if ((removeDecl != null) && (invokeDecl != null))
                {
                    if ((removeDecl.Attributes & MethodAttributes.MemberAccessMask) == (invokeDecl.Attributes & MethodAttributes.MemberAccessMask))
                    {
                        return(removeDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                }
                else
                {
                    if (addDecl != null)
                    {
                        return(addDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                    if (removeDecl != null)
                    {
                        return(removeDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                    if (invokeDecl != null)
                    {
                        return(invokeDecl.Attributes & MethodAttributes.MemberAccessMask);
                    }
                }
            }
            return(ret);
        }
Esempio n. 4
0
        public void Collect(EventReference evet)
        {
            EventDefinition def;

            if ((def = evet as EventDefinition ?? evet.Resolve()) != null)
            {
                Collect(def);
            }
        }
        protected virtual string GetEventName(EventReference @event)
        {
            EventDefinition eventDefinition = @event.Resolve();

            if (eventDefinition != null && eventDefinition.Module.FilePath == this.ModuleContext.Module.FilePath)
            {
                return(this.ModuleContext.RenamedMembersMap[eventDefinition.MetadataToken.ToUInt32()]);
            }

            return(Utilities.EscapeNameIfNeeded(GenericHelper.GetNonGenericName(@event.Name), this.Language));
        }
Esempio n. 6
0
 public static EventDefinition TryResolve(this EventReference r)
 {
     try
     {
         return(r.Resolve());
     }
     catch (Exception)
     {
         return(null);
     }
 }
Esempio n. 7
0
        static bool IsStatic(EventReference prop)
        {
            bool            flag       = false;
            EventDefinition Definition = prop.Resolve();

            if (Definition != null)
            {
                MethodReference  addMethod    = Definition.AddMethod;
                MethodDefinition addDecl      = (addMethod == null) ? null : addMethod.Resolve();
                MethodReference  removeMethod = Definition.RemoveMethod;
                MethodDefinition removeDecl   = (removeMethod == null) ? null : removeMethod.Resolve();
                MethodReference  invokeMethod = Definition.InvokeMethod;
                MethodDefinition invokeDecl   = (invokeMethod == null) ? null : invokeMethod.Resolve();
                flag |= (addDecl != null) && addDecl.IsStatic;
                flag |= (removeDecl != null) && removeDecl.IsStatic;
                flag |= (invokeDecl != null) && invokeDecl.IsStatic;
            }
            return(flag);
        }
Esempio n. 8
0
        /// <summary>
        /// Mark all eachable items in argument as such.
        /// </summary>
        private static void Walk(ReachableContext context, EventReference evt)
        {
            // Type
            evt.EventType.MarkReachable(context);

            var evtDef = evt as EventDefinition;

            if (evtDef != null)
            {
                evtDef.AddMethod.MarkReachable(context);
                evtDef.RemoveMethod.MarkReachable(context);
                evtDef.InvokeMethod.MarkReachable(context);

                // Custom attributes
                Walk(context, (ICustomAttributeProvider)evtDef);
            }
            else
            {
                // Try to resolve
                evt.Resolve(context).MarkReachable(context);
            }
        }
Esempio n. 9
0
    /// <summary>
    /// Weave an implementation of an event against the provided type.
    /// </summary>
    /// <param name="emitter">The emitter.</param>
    /// <param name="field">The attribute field.</param>
    /// <param name="evt">The event.</param>
    /// <param name="interfaceType">The type of the interface.</param>
    public void WeaveImplementedEvent(TypeEmitter emitter, Variable field, EventReference evt, TypeDefinition interfaceType)
    {
        var implemented = field.Type.GetEvent(evt.Name, evt.EventType);

        if (implemented == null)
        {
            implemented = field.Type.GetEvent($"{interfaceType.FullName}.{evt.Name}", evt.EventType);

            if (implemented == null)
            {
                throw new MissingMemberException($"Cannot implement '{field.Type.FullName}' as it does not implement event '{evt.Name}'");
            }
        }

        var emitted    = emitter.EmitEvent(evt.Name, implemented.EventType);
        var definition = evt.Resolve() ?? implemented.Resolve();

        var hasAdd = emitted.HasAdd;
        var add    = emitted.GetAdd();

        if (!hasAdd)
        {
            add.Body.SimplifyMacros();

            var ail          = add.GetIL();
            var interfaceAdd = definition.AddMethod.Import();

            ail.Emit(Codes.Nop);
            ail.Emit(Codes.ThisIf(field));
            ail.Emit(Codes.Load(field));
            ail.Emit(Codes.Arg(add.IsStatic ? 0 : 1));
            ail.Emit(Codes.Invoke(interfaceAdd.GetGeneric()));
            ail.Emit(Codes.Return);

            add.Body.OptimizeMacros();
        }


        add.Body.InitLocals = true;

        var hasRemove = emitted.HasRemove;
        var remove    = emitted.GetRemove();

        if (!emitted.HasRemove)
        {
            remove.Body.SimplifyMacros();

            var ril             = remove.GetIL();
            var interfaceRemove = definition.RemoveMethod.Import();

            ril.Emit(Codes.Nop);
            ril.Emit(Codes.ThisIf(field));
            ril.Emit(Codes.Load(field));
            ril.Emit(Codes.Arg(remove.IsStatic ? 0 : 1));
            ril.Emit(Codes.Invoke(interfaceRemove.GetGeneric()));
            ril.Emit(Codes.Return);

            remove.Body.OptimizeMacros();
        }

        remove.Body.InitLocals = true;
    }
Esempio n. 10
0
 /// <summary>
 /// A cecil event reference will be directed to this method.
 /// </summary>
 /// <param name="item">Cecil reference.</param>
 /// <param name="resolvedItem">Output parameter that will be populated with the resolved cecil definition.</param>
 /// <returns><c>true</c></returns>
 private static bool TryResolve(EventReference item, out object resolvedItem)
 {
     resolvedItem = item.Resolve();
     return(true);
 }