Browse Source

Split EventDeclaration in normal one (can declare multiple events) and CustomEventDeclaration (with add/remove blocks, can only declare a single event).

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
54c454b0f8
  1. 2
      ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/EventDeclarationTests.cs
  2. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  3. 5
      ICSharpCode.NRefactory/CSharp/Ast/AstVisitor.cs
  4. 17
      ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EventDeclaration.cs
  5. 24
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  6. 21
      ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs
  7. 14
      ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

2
ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/EventDeclarationTests.cs

@ -12,7 +12,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
[Test] [Test]
public void SimpleEventDeclarationTest() public void SimpleEventDeclarationTest()
{ {
EventDeclaration ed = ParseUtilCSharp.ParseTypeMember<EventDeclaration>("event System.EventHandler MyEvent;"); CustomEventDeclaration ed = ParseUtilCSharp.ParseTypeMember<CustomEventDeclaration>("event System.EventHandler MyEvent;");
Assert.AreEqual("MyEvent", ed.Name); Assert.AreEqual("MyEvent", ed.Name);
//Assert.AreEqual("System.EventHandler", ed.TypeReference.Type); //Assert.AreEqual("System.EventHandler", ed.TypeReference.Type);
Assert.Ignore(); // check type Assert.Ignore(); // check type

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -58,6 +58,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CSharp\AstStructureTest.cs" />
<Compile Include="CSharp\Parser\GeneralScope\DelegateDeclarationTests.cs" /> <Compile Include="CSharp\Parser\GeneralScope\DelegateDeclarationTests.cs" />
<Compile Include="CSharp\Parser\GeneralScope\NamespaceDeclarationTests.cs" /> <Compile Include="CSharp\Parser\GeneralScope\NamespaceDeclarationTests.cs" />
<Compile Include="CSharp\Parser\GeneralScope\TypeDeclarationTests.cs" /> <Compile Include="CSharp\Parser\GeneralScope\TypeDeclarationTests.cs" />

5
ICSharpCode.NRefactory/CSharp/Ast/AstVisitor.cs

@ -142,6 +142,11 @@ namespace ICSharpCode.NRefactory.CSharp
return VisitChildren (eventDeclaration, data); return VisitChildren (eventDeclaration, data);
} }
public virtual S VisitCustomEventDeclaration (CustomEventDeclaration eventDeclaration, T data)
{
return VisitChildren (eventDeclaration, data);
}
public virtual S VisitFieldDeclaration (FieldDeclaration fieldDeclaration, T data) public virtual S VisitFieldDeclaration (FieldDeclaration fieldDeclaration, T data)
{ {
return VisitChildren (fieldDeclaration, data); return VisitChildren (fieldDeclaration, data);

17
ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/EventDeclaration.cs

@ -24,9 +24,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp namespace ICSharpCode.NRefactory.CSharp
{ {
public class EventDeclaration : MemberDeclaration public class EventDeclaration : MemberDeclaration
{
public IEnumerable<VariableInitializer> Variables {
get { return GetChildrenByRole (Roles.Variable); }
set { SetChildrenByRole (Roles.Variable, value); }
}
public override S AcceptVisitor<T, S> (AstVisitor<T, S> visitor, T data)
{
return visitor.VisitEventDeclaration (this, data);
}
}
public class CustomEventDeclaration : MemberDeclaration
{ {
public static readonly Role<Accessor> AddAccessorRole = new Role<Accessor>("AddAccessor", Accessor.Null); public static readonly Role<Accessor> AddAccessorRole = new Role<Accessor>("AddAccessor", Accessor.Null);
public static readonly Role<Accessor> RemoveAccessorRole = new Role<Accessor>("RemoveAccessor", Accessor.Null); public static readonly Role<Accessor> RemoveAccessorRole = new Role<Accessor>("RemoveAccessor", Accessor.Null);
@ -43,7 +58,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override S AcceptVisitor<T, S> (AstVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (AstVisitor<T, S> visitor, T data)
{ {
return visitor.VisitEventDeclaration (this, data); return visitor.VisitCustomEventDeclaration (this, data);
} }
} }
} }

24
ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -739,40 +739,40 @@ namespace ICSharpCode.NRefactory.CSharp
public override void Visit (EventProperty ep) public override void Visit (EventProperty ep)
{ {
EventDeclaration newEvent = new EventDeclaration (); CustomEventDeclaration newEvent = new CustomEventDeclaration ();
var location = LocationsBag.GetMemberLocation (ep); var location = LocationsBag.GetMemberLocation (ep);
AddModifiers (newEvent, location); AddModifiers (newEvent, location);
if (location != null) if (location != null)
newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), EventDeclaration.Roles.Keyword); newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), CustomEventDeclaration.Roles.Keyword);
newEvent.AddChild ((AstType)ep.TypeName.Accept (this), EventDeclaration.Roles.Type); newEvent.AddChild ((AstType)ep.TypeName.Accept (this), CustomEventDeclaration.Roles.Type);
newEvent.AddChild (new Identifier (ep.MemberName.Name, Convert (ep.MemberName.Location)), EventDeclaration.Roles.Identifier); newEvent.AddChild (new Identifier (ep.MemberName.Name, Convert (ep.MemberName.Location)), CustomEventDeclaration.Roles.Identifier);
if (location != null && location.Count >= 2) if (location != null && location.Count >= 2)
newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), 1), EventDeclaration.Roles.LBrace); newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), 1), CustomEventDeclaration.Roles.LBrace);
if (ep.Add != null) { if (ep.Add != null) {
Accessor addAccessor = new Accessor (); Accessor addAccessor = new Accessor ();
var addLocation = LocationsBag.GetMemberLocation (ep.Add); var addLocation = LocationsBag.GetMemberLocation (ep.Add);
AddModifiers (addAccessor, addLocation); AddModifiers (addAccessor, addLocation);
addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location), "add".Length), EventDeclaration.Roles.Keyword); addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location), "add".Length), CustomEventDeclaration.Roles.Keyword);
if (ep.Add.Block != null) if (ep.Add.Block != null)
addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), EventDeclaration.Roles.Body); addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), CustomEventDeclaration.Roles.Body);
newEvent.AddChild (addAccessor, EventDeclaration.AddAccessorRole); newEvent.AddChild (addAccessor, CustomEventDeclaration.AddAccessorRole);
} }
if (ep.Remove != null) { if (ep.Remove != null) {
Accessor removeAccessor = new Accessor (); Accessor removeAccessor = new Accessor ();
var removeLocation = LocationsBag.GetMemberLocation (ep.Remove); var removeLocation = LocationsBag.GetMemberLocation (ep.Remove);
AddModifiers (removeAccessor, removeLocation); AddModifiers (removeAccessor, removeLocation);
removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location), "remove".Length), EventDeclaration.Roles.Keyword); removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location), "remove".Length), CustomEventDeclaration.Roles.Keyword);
if (ep.Remove.Block != null) if (ep.Remove.Block != null)
removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), EventDeclaration.Roles.Body); removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), CustomEventDeclaration.Roles.Body);
newEvent.AddChild (removeAccessor, EventDeclaration.RemoveAccessorRole); newEvent.AddChild (removeAccessor, CustomEventDeclaration.RemoveAccessorRole);
} }
if (location != null && location.Count >= 3) if (location != null && location.Count >= 3)
newEvent.AddChild (new CSharpTokenNode (Convert (location[2]), 1), EventDeclaration.Roles.RBrace); newEvent.AddChild (new CSharpTokenNode (Convert (location[2]), 1), CustomEventDeclaration.Roles.RBrace);
typeStack.Peek ().AddChild (newEvent, TypeDeclaration.MemberRole); typeStack.Peek ().AddChild (newEvent, TypeDeclaration.MemberRole);
} }

21
ICSharpCode.NRefactory/CSharp/Parser/TypeSystemConvertVisitor.cs

@ -437,6 +437,27 @@ namespace ICSharpCode.NRefactory.CSharp
#region Events #region Events
public override IEntity VisitEventDeclaration(EventDeclaration eventDeclaration, object data) public override IEntity VisitEventDeclaration(EventDeclaration eventDeclaration, object data)
{
bool isSingleEvent = eventDeclaration.Variables.Count() == 1;
Modifiers modifiers = eventDeclaration.Modifiers;
DefaultEvent ev = null;
foreach (VariableInitializer vi in eventDeclaration.Variables) {
ev = new DefaultEvent(currentTypeDefinition, vi.Name);
ev.Region = isSingleEvent ? MakeRegion(eventDeclaration) : MakeRegion(vi);
ev.BodyRegion = MakeRegion(vi);
ConvertAttributes(ev.Attributes, eventDeclaration.Attributes);
ApplyModifiers(ev, modifiers);
ev.ReturnType = ConvertType(eventDeclaration.ReturnType);
currentTypeDefinition.Events.Add(ev);
}
return isSingleEvent ? ev : null;
}
public override IEntity VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration, object data)
{ {
DefaultEvent e = new DefaultEvent(currentTypeDefinition, eventDeclaration.Name); DefaultEvent e = new DefaultEvent(currentTypeDefinition, eventDeclaration.Name);
e.Region = MakeRegion(eventDeclaration); e.Region = MakeRegion(eventDeclaration);

14
ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

@ -216,7 +216,17 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#region Track CurrentMember #region Track CurrentMember
public override ResolveResult VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data) public override ResolveResult VisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data)
{ {
int initializerCount = fieldDeclaration.Variables.Count(); return VisitFieldOrEventDeclaration(fieldDeclaration);
}
public override ResolveResult VisitEventDeclaration(EventDeclaration eventDeclaration, object data)
{
return VisitFieldOrEventDeclaration(eventDeclaration);
}
ResolveResult VisitFieldOrEventDeclaration(MemberDeclaration fieldDeclaration)
{
int initializerCount = fieldDeclaration.GetChildrenByRole(FieldDeclaration.Roles.Variable).Count();
ResolveResult result = null; ResolveResult result = null;
for (AstNode node = fieldDeclaration.FirstChild; node != null; node = node.NextSibling) { for (AstNode node = fieldDeclaration.FirstChild; node != null; node = node.NextSibling) {
if (node.Role == FieldDeclaration.Roles.Variable) { if (node.Role == FieldDeclaration.Roles.Variable) {
@ -335,7 +345,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
return VisitPropertyMember(indexerDeclaration); return VisitPropertyMember(indexerDeclaration);
} }
public override ResolveResult VisitEventDeclaration(EventDeclaration eventDeclaration, object data) public override ResolveResult VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration, object data)
{ {
try { try {
if (resolver.CurrentTypeDefinition != null) { if (resolver.CurrentTypeDefinition != null) {

Loading…
Cancel
Save