Browse Source

Fixed method declaration tests.

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
4e60911bb0
  1. 2
      ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs
  2. 21
      ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs
  3. 22
      ICSharpCode.NRefactory/CSharp/Ast/Expressions/NullReferenceExpression.cs
  4. 84
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  5. 15
      ICSharpCode.NRefactory/CSharp/Parser/mcs/decl.cs

2
ICSharpCode.NRefactory.Tests/CSharp/Parser/ParseUtil.cs

@ -79,8 +79,6 @@ namespace ICSharpCode.NRefactory.CSharp.Parser @@ -79,8 +79,6 @@ namespace ICSharpCode.NRefactory.CSharp.Parser
public static T ParseTypeMember<T>(string expr, bool expectErrors = false) where T : AttributedNode
{
if (expectErrors) Assert.Ignore("errors not yet implemented");
CSharpParser parser = new CSharpParser();
var members = parser.ParseTypeMembers(new StringReader(expr));

21
ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/MethodDeclarationTests.cs

@ -8,7 +8,7 @@ using NUnit.Framework; @@ -8,7 +8,7 @@ using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
{
[TestFixture, Ignore("Generics not yet implemented")]
[TestFixture]
public class MethodDeclarationTests
{
[Test]
@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
public void MethodWithUnnamedParameterDeclarationTest()
{
MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>("void MyMethod(int) {} ", true);
Assert.AreEqual("System.Void", md.ReturnType);
Assert.AreEqual("void", md.ReturnType.ToString ());
Assert.AreEqual(1, md.Parameters.Count());
Assert.AreEqual("int", ((PrimitiveType)md.Parameters.Single().Type).Keyword);
}
@ -202,6 +202,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -202,6 +202,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
",
new TypeDeclaration {
ClassType = ClassType.Interface,
Name = "MyInterface",
BaseTypes = { new SimpleType("IDisposable") },
Members = {
new MethodDeclaration {
@ -214,7 +215,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -214,7 +215,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
[Test]
public void MethodImplementingInterfaceTest()
{
ParseUtilCSharp.AssertGlobal(
ParseUtilCSharp.AssertTypeMember(
"int MyInterface.MyMethod() {} ",
new MethodDeclaration {
ReturnType = new PrimitiveType("int"),
@ -227,7 +228,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -227,7 +228,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
[Test]
public void MethodImplementingGenericInterfaceTest()
{
ParseUtilCSharp.AssertGlobal(
ParseUtilCSharp.AssertTypeMember(
"int MyInterface<string>.MyMethod() {} ",
new MethodDeclaration {
ReturnType = new PrimitiveType("int"),
@ -240,7 +241,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -240,7 +241,7 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
[Test]
public void VoidMethodImplementingInterfaceTest()
{
ParseUtilCSharp.AssertGlobal(
ParseUtilCSharp.AssertTypeMember (
"void MyInterface.MyMethod() {} ",
new MethodDeclaration {
ReturnType = new PrimitiveType("void"),
@ -253,11 +254,11 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -253,11 +254,11 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
[Test]
public void VoidMethodImplementingGenericInterfaceTest()
{
ParseUtilCSharp.AssertGlobal(
ParseUtilCSharp.AssertTypeMember (
"void MyInterface<string>.MyMethod() {} ",
new MethodDeclaration {
ReturnType = new PrimitiveType("void"),
PrivateImplementationType = new SimpleType("MyInterface"),
PrivateImplementationType = new SimpleType("MyInterface") { TypeArguments = { new PrimitiveType("string") } },
Name = "MyMethod",
Body = new BlockStatement()
});
@ -305,15 +306,15 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers @@ -305,15 +306,15 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
public void MethodWithEmptyAssignmentErrorInBody()
{
MethodDeclaration md = ParseUtilCSharp.ParseTypeMember<MethodDeclaration>(
"void A\n" +
"void A ()\n" +
"{\n" +
"int a = 3;\n" +
" = 4;\n" +
"}", true // expect errors
);
Assert.AreEqual("A", md.Name);
Assert.AreEqual(new AstLocation(1, 2), md.Body.StartLocation);
Assert.AreEqual(new AstLocation(2, 5), md.Body.EndLocation);
Assert.AreEqual(new AstLocation(2, 1), md.Body.StartLocation);
Assert.AreEqual(new AstLocation(5, 2), md.Body.EndLocation);
}
[Test]

22
ICSharpCode.NRefactory/CSharp/Ast/Expressions/NullReferenceExpression.cs

@ -31,6 +31,28 @@ namespace ICSharpCode.NRefactory.CSharp @@ -31,6 +31,28 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public class NullReferenceExpression : Expression
{
AstLocation location;
public override AstLocation StartLocation {
get {
return location;
}
}
public override AstLocation EndLocation {
get {
return new AstLocation (location.Line, location.Column + "null".Length);
}
}
public NullReferenceExpression ()
{
}
public NullReferenceExpression (AstLocation location)
{
this.location = location;
}
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitNullReferenceExpression (this, data);

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

@ -76,6 +76,23 @@ namespace ICSharpCode.NRefactory.CSharp @@ -76,6 +76,23 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
AstType ConvertToType (MemberName memberName)
{
AstType result;
if (memberName.Left != null) {
result = new MemberType () { MemberName = memberName.Name };
result.AddChild (ConvertToType (memberName.Left), MemberType.TargetRole);
} else {
result = new SimpleType () { Identifier = memberName.Name };
}
if (memberName.TypeArguments != null && !memberName.TypeArguments.IsEmpty) {
foreach (var arg in memberName.TypeArguments.Args) {
result.AddChild (ConvertToType (arg), AstType.Roles.TypeArgument);
}
}
return result;
}
AstType ConvertToType (Mono.CSharp.Expression typeName)
{
if (typeName is TypeExpression) {
@ -250,6 +267,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -250,6 +267,11 @@ namespace ICSharpCode.NRefactory.CSharp
newType.AddChild (new CSharpTokenNode (Convert (typeArgLocation[1]), 1), TypeDeclaration.Roles.RChevron);
AddConstraints (newType, c);
}
if (c.TypeBaseExpressions != null) {
foreach (var baseTypes in c.TypeBaseExpressions) {
newType.AddChild (ConvertToType (baseTypes), TypeDeclaration.BaseTypeRole);
}
}
if (location != null && location.Count > 1)
newType.AddChild (new CSharpTokenNode (Convert (location[1]), 1), AstNode.Roles.LBrace);
typeStack.Push (newType);
@ -279,6 +301,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -279,6 +301,13 @@ namespace ICSharpCode.NRefactory.CSharp
newType.AddChild (new CSharpTokenNode (Convert (typeArgLocation[1]), 1), TypeDeclaration.Roles.RChevron);
AddConstraints (newType, s);
}
if (s.TypeBaseExpressions != null) {
foreach (var baseTypes in s.TypeBaseExpressions) {
newType.AddChild (ConvertToType (baseTypes), TypeDeclaration.BaseTypeRole);
}
}
if (location != null && location.Count > 1)
newType.AddChild (new CSharpTokenNode (Convert (location[1]), 1), AstNode.Roles.LBrace);
typeStack.Push (newType);
@ -308,6 +337,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -308,6 +337,11 @@ namespace ICSharpCode.NRefactory.CSharp
newType.AddChild (new CSharpTokenNode (Convert (typeArgLocation[1]), 1), MemberReferenceExpression.Roles.RChevron);
AddConstraints (newType, i);
}
if (i.TypeBaseExpressions != null) {
foreach (var baseTypes in i.TypeBaseExpressions) {
newType.AddChild (ConvertToType (baseTypes), TypeDeclaration.BaseTypeRole);
}
}
if (location != null && location.Count > 1)
newType.AddChild (new CSharpTokenNode (Convert (location[1]), 1), AstNode.Roles.LBrace);
typeStack.Push (newType);
@ -376,6 +410,13 @@ namespace ICSharpCode.NRefactory.CSharp @@ -376,6 +410,13 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
newType.AddChild (new CSharpTokenNode (Convert (location[0]), "enum".Length), TypeDeclaration.Roles.Keyword);
newType.AddChild (new Identifier (e.Basename, Convert (e.MemberName.Location)), AstNode.Roles.Identifier);
if (e.TypeBaseExpressions != null) {
foreach (var baseTypes in e.TypeBaseExpressions) {
newType.AddChild (ConvertToType (baseTypes), TypeDeclaration.BaseTypeRole);
}
}
if (location != null && location.Count > 1)
newType.AddChild (new CSharpTokenNode (Convert (location[1]), 1), AstNode.Roles.LBrace);
typeStack.Push (newType);
@ -654,7 +695,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -654,7 +695,10 @@ namespace ICSharpCode.NRefactory.CSharp
AddModifiers (newMethod, location);
newMethod.AddChild (ConvertToType (m.TypeName), AstNode.Roles.Type);
newMethod.AddChild (new Identifier (m.Name, Convert (m.Location)), AstNode.Roles.Identifier);
if (m.MethodName.Left != null)
newMethod.AddChild (ConvertToType (m.MethodName.Left), MethodDeclaration.PrivateImplementationTypeRole);
newMethod.AddChild (new Identifier (m.MethodName.Name, Convert (m.Location)), AstNode.Roles.Identifier);
if (m.MemberName.TypeArguments != null) {
var typeArgLocation = LocationsBag.GetLocations (m.MemberName);
@ -663,7 +707,6 @@ namespace ICSharpCode.NRefactory.CSharp @@ -663,7 +707,6 @@ namespace ICSharpCode.NRefactory.CSharp
AddTypeParameters (newMethod, typeArgLocation, m.MemberName.TypeArguments);
if (typeArgLocation != null)
newMethod.AddChild (new CSharpTokenNode (Convert (typeArgLocation[1]), 1), MemberReferenceExpression.Roles.RChevron);
AddConstraints (newMethod, m.GenericMethod);
}
@ -742,7 +785,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -742,7 +785,11 @@ namespace ICSharpCode.NRefactory.CSharp
var location = LocationsBag.GetMemberLocation (p);
AddModifiers (newProperty, location);
newProperty.AddChild (ConvertToType (p.TypeName), AstNode.Roles.Type);
newProperty.AddChild (new Identifier (p.MemberName.Name, Convert (p.MemberName.Location)), AstNode.Roles.Identifier);
if (p.MemberName.Left != null)
newProperty.AddChild (ConvertToType (p.MemberName.Left), PropertyDeclaration.PrivateImplementationTypeRole);
newProperty.AddChild (new Identifier (p.MemberName.Name, Convert (p.Location)), PropertyDeclaration.Roles.Identifier);
if (location != null)
newProperty.AddChild (new CSharpTokenNode (Convert (location[0]), 1), MethodDeclaration.Roles.LBrace);
@ -830,7 +877,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -830,7 +877,12 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), EventDeclaration.Roles.Keyword);
newEvent.AddChild (ConvertToType (e.TypeName), AstNode.Roles.Type);
newEvent.AddChild (new Identifier (e.MemberName.Name, Convert (e.MemberName.Location)), EventDeclaration.Roles.Identifier);
// if (e.MemberName.Left != null)
// newEvent.AddChild (ConvertToType (e.MemberName.Left), EventDeclaration.PrivateImplementationTypeRole);
newEvent.AddChild (new Identifier (e.MemberName.Name, Convert (e.Location)), EventDeclaration.Roles.Identifier);
if (location != null)
newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), ";".Length), EventDeclaration.Roles.Semicolon);
@ -847,7 +899,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -847,7 +899,11 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
newEvent.AddChild (new CSharpTokenNode (Convert (location[0]), "event".Length), CustomEventDeclaration.Roles.Keyword);
newEvent.AddChild (ConvertToType (ep.TypeName), CustomEventDeclaration.Roles.Type);
newEvent.AddChild (new Identifier (ep.MemberName.Name, Convert (ep.MemberName.Location)), CustomEventDeclaration.Roles.Identifier);
if (ep.MemberName.Left != null)
newEvent.AddChild (ConvertToType (ep.MemberName.Left), CustomEventDeclaration.PrivateImplementationTypeRole);
newEvent.AddChild (new Identifier (ep.MemberName.Name, Convert (ep.Location)), CustomEventDeclaration.Roles.Identifier);
if (location != null && location.Count >= 2)
newEvent.AddChild (new CSharpTokenNode (Convert (location[1]), 1), CustomEventDeclaration.Roles.LBrace);
@ -1567,6 +1623,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1567,6 +1623,8 @@ namespace ICSharpCode.NRefactory.CSharp
public override object Visit (Constant constant)
{
if (constant.GetValue () == null)
return new NullReferenceExpression (Convert (constant.Location));
var result = new PrimitiveExpression (constant.GetValue (), Convert (constant.Location), constant.GetValueAsLiteral ().Length);
return result;
}
@ -1882,7 +1940,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1882,7 +1940,8 @@ namespace ICSharpCode.NRefactory.CSharp
}
if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), ParameterDeclaration.Roles.Type);
parameterDeclarationExpression.AddChild (new Identifier (p.Name, Convert (p.Location)), ParameterDeclaration.Roles.Identifier);
if (p.Name != null)
parameterDeclarationExpression.AddChild (new Identifier (p.Name, Convert (p.Location)), ParameterDeclaration.Roles.Identifier);
if (p.HasDefaultValue) {
if (location != null)
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [1]), 1), ParameterDeclaration.Roles.Assign);
@ -1943,13 +2002,15 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1943,13 +2002,15 @@ namespace ICSharpCode.NRefactory.CSharp
{
if (d == null || d.Constraints == null)
return;
for (int i = 0; i < d.Constraints.Count; i++) {
Constraints c = d.Constraints [i];
for (int i = 0; i < d.PlainConstraints.Count; i++) {
Constraints c = d.PlainConstraints [i];
var location = LocationsBag.GetLocations (c);
var constraint = new Constraint ();
constraint.AddChild (new CSharpTokenNode (Convert (location [0]), "where".Length), InvocationExpression.Roles.Keyword);
if (location != null)
constraint.AddChild (new CSharpTokenNode (Convert (location [0]), "where".Length), InvocationExpression.Roles.Keyword);
constraint.AddChild (new Identifier (c.TypeParameter.Value, Convert (c.TypeParameter.Location)), InvocationExpression.Roles.Identifier);
constraint.AddChild (new CSharpTokenNode (Convert (location [1]), 1), Constraint.ColonRole);
if (location != null && location.Count > 1)
constraint.AddChild (new CSharpTokenNode (Convert (location [1]), 1), Constraint.ColonRole);
foreach (var expr in c.ConstraintExpressions)
constraint.AddChild (ConvertToType (expr), Constraint.BaseTypeRole);
parent.AddChild (constraint, AstNode.Roles.Constraint);
@ -2583,6 +2644,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2583,6 +2644,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override void Print (AbstractMessage msg)
{
Console.WriteLine (msg.MessageType + " (" + msg.Location + ")" + ": "+ msg.Text);
base.Print (msg);
// Error newError = new Error (msg.IsWarning ? ErrorType.Warning : ErrorType.Error, msg.Location.Row, msg.Location.Column, msg.Text);
// Errors.Add (newError);
@ -2638,7 +2700,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2638,7 +2700,7 @@ namespace ICSharpCode.NRefactory.CSharp
public IEnumerable<AttributedNode> ParseTypeMembers(TextReader reader)
{
string code = "unsafe class MyClass { " + reader.ReadToEnd() + "}";
string code = "unsafe partial class MyClass { " + reader.ReadToEnd() + "}";
var cu = Parse(new StringReader(code));
var td = cu.Children.FirstOrDefault() as TypeDeclaration;
if (td != null)

15
ICSharpCode.NRefactory/CSharp/Parser/mcs/decl.cs

@ -27,6 +27,7 @@ using IKVM.Reflection.Emit; @@ -27,6 +27,7 @@ using IKVM.Reflection.Emit;
#else
using System.Reflection;
using System.Reflection.Emit;
using Mono.Collections.Generic;
#endif
namespace Mono.CSharp {
@ -1425,6 +1426,13 @@ namespace Mono.CSharp { @@ -1425,6 +1426,13 @@ namespace Mono.CSharp {
return type_param_list;
}
#if FULL_AST
public List<Constraints> PlainConstraints {
get;
private set;
}
#endif
public List<Constraints> Constraints {
get;
private set;
@ -1432,7 +1440,14 @@ namespace Mono.CSharp { @@ -1432,7 +1440,14 @@ namespace Mono.CSharp {
public virtual void SetParameterInfo (List<Constraints> constraints_list)
{
#if FULL_AST
if (constraints_list != null) {
this.PlainConstraints = constraints_list;
constraints_list = this.Constraints = new List<Constraints> (constraints_list);
}
#else
this.Constraints = constraints_list;
#endif
if (!is_generic) {
if (constraints_list != null) {
Report.Error (

Loading…
Cancel
Save