EntityDeclaration ConvertEvent(IEvent ev) { if (this.UseCustomEvents) { CustomEventDeclaration decl = new CustomEventDeclaration(); decl.Modifiers = GetMemberModifiers(ev); if (ShowAttributes) { decl.Attributes.AddRange (ev.Attributes.Select ((a) => new AttributeSection (ConvertAttribute (a)))); } if (AddResolveResultAnnotations) { decl.AddAnnotation(new MemberResolveResult(null, ev)); } decl.ReturnType = ConvertType(ev.ReturnType); decl.Name = ev.Name; decl.AddAccessor = ConvertAccessor(ev.AddAccessor, ev.Accessibility, true); decl.RemoveAccessor = ConvertAccessor(ev.RemoveAccessor, ev.Accessibility, true); return decl; } else { EventDeclaration decl = new EventDeclaration(); decl.Modifiers = GetMemberModifiers(ev); if (ShowAttributes) { decl.Attributes.AddRange (ev.Attributes.Select ((a) => new AttributeSection (ConvertAttribute (a)))); } if (AddResolveResultAnnotations) { decl.AddAnnotation(new MemberResolveResult(null, ev)); } decl.ReturnType = ConvertType(ev.ReturnType); decl.Variables.Add(new VariableInitializer(ev.Name)); return decl; } }
EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev) { if (!ev.PrivateImplementationType.IsNull) { return(null); } if (!ev.Modifiers.HasFlag(Modifiers.Abstract)) { if (!CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev)) { return(null); } } RemoveCompilerGeneratedAttribute(ev.AddAccessor.Attributes, attributeTypesToRemoveFromAutoEvents); EventDeclaration ed = new EventDeclaration(); ev.Attributes.MoveTo(ed.Attributes); foreach (var attr in ev.AddAccessor.Attributes) { attr.AttributeTarget = "method"; ed.Attributes.Add(attr.Detach()); } ed.ReturnType = ev.ReturnType.Detach(); ed.Modifiers = ev.Modifiers; ed.Variables.Add(new VariableInitializer(ev.Name)); ed.CopyAnnotationsFrom(ev); IEvent eventDef = ev.GetSymbol() as IEvent; if (eventDef != null) { IField field = eventDef.DeclaringType.GetFields(f => f.Name == ev.Name, GetMemberOptions.IgnoreInheritedMembers).SingleOrDefault(); if (field != null) { ed.AddAnnotation(field); var attributes = field.GetAttributes() .Where(a => !attributeTypesToRemoveFromAutoEvents.Contains(a.AttributeType.FullName)) .Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray(); if (attributes.Length > 0) { var section = new AttributeSection { AttributeTarget = "field" }; section.Attributes.AddRange(attributes); ed.Attributes.Add(section); } } } ev.ReplaceWith(ed); return(ed); }
EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev) { Match m1 = automaticEventPatternV4.Match(ev.AddAccessor); if (!CheckAutomaticEventV4Match(m1, ev, true)) { return(null); } Match m2 = automaticEventPatternV4.Match(ev.RemoveAccessor); if (!CheckAutomaticEventV4Match(m2, ev, false)) { return(null); } EventDeclaration ed = new EventDeclaration(); ev.Attributes.MoveTo(ed.Attributes); ed.ReturnType = ev.ReturnType.Detach(); ed.Modifiers = ev.Modifiers; ed.Variables.Add(new VariableInitializer(ev.Name)); ed.CopyAnnotationsFrom(ev); EventDefinition eventDef = ev.Annotation <EventDefinition>(); if (eventDef != null) { FieldDefinition field = eventDef.DeclaringType.Fields.FirstOrDefault(f => f.Name == ev.Name); if (field != null) { ed.AddAnnotation(field); AstBuilder.ConvertAttributes(ed, field, AttributeTarget.Field); } } ev.ReplaceWith(ed); return(ed); }
AttributedNode CreateEvent(EventDefinition eventDef) { if (eventDef.AddMethod != null && eventDef.AddMethod.IsAbstract) { // An abstract event cannot be custom EventDeclaration astEvent = new EventDeclaration(); ConvertCustomAttributes(astEvent, eventDef); astEvent.AddAnnotation(eventDef); astEvent.Variables.Add(new VariableInitializer(CleanName(eventDef.Name))); astEvent.ReturnType = ConvertType(eventDef.EventType, eventDef); if (!eventDef.DeclaringType.IsInterface) astEvent.Modifiers = ConvertModifiers(eventDef.AddMethod); return astEvent; } else { CustomEventDeclaration astEvent = new CustomEventDeclaration(); ConvertCustomAttributes(astEvent, eventDef); astEvent.AddAnnotation(eventDef); astEvent.Name = CleanName(eventDef.Name); astEvent.ReturnType = ConvertType(eventDef.EventType, eventDef); if (eventDef.AddMethod == null || !eventDef.AddMethod.HasOverrides) astEvent.Modifiers = ConvertModifiers(eventDef.AddMethod); else astEvent.PrivateImplementationType = ConvertType(eventDef.AddMethod.Overrides.First().DeclaringType); if (eventDef.AddMethod != null) { // Create mapping - used in debugger MemberMapping methodMapping = eventDef.AddMethod.CreateCodeMapping(this.CodeMappings); astEvent.AddAccessor = new Accessor { Body = CreateMethodBody(eventDef.AddMethod) }.WithAnnotation(eventDef.AddMethod); ConvertAttributes(astEvent.AddAccessor, eventDef.AddMethod); astEvent.AddAccessor.WithAnnotation(methodMapping); } if (eventDef.RemoveMethod != null) { // Create mapping - used in debugger MemberMapping methodMapping = eventDef.RemoveMethod.CreateCodeMapping(this.CodeMappings); astEvent.RemoveAccessor = new Accessor { Body = CreateMethodBody(eventDef.RemoveMethod) }.WithAnnotation(eventDef.RemoveMethod); ConvertAttributes(astEvent.RemoveAccessor, eventDef.RemoveMethod); astEvent.RemoveAccessor.WithAnnotation(methodMapping); } MethodDefinition accessor = eventDef.AddMethod ?? eventDef.RemoveMethod; if (accessor.IsVirtual ^ !accessor.IsNewSlot) { if (TypesHierarchyHelpers.FindBaseMethods(accessor).Any()) astEvent.Modifiers |= Modifiers.New; } return astEvent; } }
AttributedNode CreateEvent(EventDefinition eventDef) { if (eventDef.AddMethod != null && eventDef.AddMethod.IsAbstract) { // An abstract event cannot be custom EventDeclaration astEvent = new EventDeclaration(); ConvertCustomAttributes(astEvent, eventDef); astEvent.AddAnnotation(eventDef); astEvent.Variables.Add(new VariableInitializer(CleanName(eventDef.Name))); astEvent.ReturnType = ConvertType(eventDef.EventType, eventDef); if (!eventDef.DeclaringType.IsInterface) astEvent.Modifiers = ConvertModifiers(eventDef.AddMethod); return astEvent; } else { CustomEventDeclaration astEvent = new CustomEventDeclaration(); ConvertCustomAttributes(astEvent, eventDef); astEvent.AddAnnotation(eventDef); astEvent.Name = CleanName(eventDef.Name); astEvent.ReturnType = ConvertType(eventDef.EventType, eventDef); if (eventDef.AddMethod == null || !eventDef.AddMethod.HasOverrides) astEvent.Modifiers = ConvertModifiers(eventDef.AddMethod); else astEvent.PrivateImplementationType = ConvertType(eventDef.AddMethod.Overrides.First().DeclaringType); if (eventDef.AddMethod != null) { astEvent.AddAccessor = new Accessor { Body = AstMethodBodyBuilder.CreateMethodBody(eventDef.AddMethod, context) }.WithAnnotation(eventDef.AddMethod); ConvertAttributes(astEvent.AddAccessor, eventDef.AddMethod); } if (eventDef.RemoveMethod != null) { astEvent.RemoveAccessor = new Accessor { Body = AstMethodBodyBuilder.CreateMethodBody(eventDef.RemoveMethod, context) }.WithAnnotation(eventDef.RemoveMethod); ConvertAttributes(astEvent.RemoveAccessor, eventDef.RemoveMethod); } return astEvent; } }