Browse Source

CSharpParser: UsingDeclaration makes use of type reference

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
7656e7d937
  1. 23
      ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IsExpressionTests.cs
  2. 36
      ICSharpCode.NRefactory/CSharp/Dom/ComposedType.cs
  3. 3
      ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs
  4. 27
      ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/UsingDeclaration.cs
  5. 6
      ICSharpCode.NRefactory/CSharp/Dom/MemberType.cs
  6. 23
      ICSharpCode.NRefactory/CSharp/Dom/SimpleType.cs
  7. 59
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  8. 2
      ICSharpCode.NRefactory/CSharp/Parser/mcs/decl.cs

23
ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/IsExpressionTests.cs

@ -20,27 +20,26 @@ namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
Assert.IsTrue(ce.Expression is IdentifierExpression);*/ Assert.IsTrue(ce.Expression is IdentifierExpression);*/
} }
[Test, Ignore] [Test]
public void NullableIsExpression() public void NullableIsExpression()
{ {
/* TODO IsExpression ce = ParseUtilCSharp.ParseExpression<IsExpression>("o is int?");
TypeOfIsExpression ce = ParseUtilCSharp.ParseExpression<TypeOfIsExpression>("o is int?"); ComposedType type = (ComposedType)ce.TypeReference;
Assert.AreEqual("System.Nullable", ce.TypeReference.Type); Assert.IsTrue(type.HasNullableSpecifier);
Assert.AreEqual("System.Int32", ce.TypeReference.GenericTypes[0].Type); Assert.AreEqual("int", ((PrimitiveType)type.BaseType).Keyword);
Assert.IsTrue(ce.Expression is IdentifierExpression);*/ Assert.IsTrue(ce.Expression is IdentifierExpression);
} }
[Test, Ignore] [Test]
public void NullableIsExpressionInBinaryOperatorExpression() public void NullableIsExpressionInBinaryOperatorExpression()
{ {
/* TODO
BinaryOperatorExpression boe; BinaryOperatorExpression boe;
boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("o is int? == true"); boe = ParseUtilCSharp.ParseExpression<BinaryOperatorExpression>("o is int? == true");
TypeOfIsExpression ce = (TypeOfIsExpression)boe.Left; IsExpression ce = (IsExpression)boe.Left;
Assert.AreEqual("System.Nullable", ce.TypeReference.Type); ComposedType type = (ComposedType)ce.TypeReference;
Assert.AreEqual("System.Int32", ce.TypeReference.GenericTypes[0].Type); Assert.IsTrue(type.HasNullableSpecifier);
Assert.AreEqual("int", ((PrimitiveType)type.BaseType).Keyword);
Assert.IsTrue(ce.Expression is IdentifierExpression); Assert.IsTrue(ce.Expression is IdentifierExpression);
*/
} }
} }
} }

36
ICSharpCode.NRefactory/CSharp/Dom/ComposedType.cs

@ -1,4 +1,4 @@
// //
// ComposedType.cs // ComposedType.cs
// //
// Author: // Author:
@ -26,7 +26,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
namespace ICSharpCode.NRefactory.CSharp namespace ICSharpCode.NRefactory.CSharp
{ {
@ -48,8 +48,16 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public IEnumerable<ArraySpecifier> Compositions { public bool HasNullableSpecifier {
get { return GetChildrenByRole (ArraySpecRole).Cast<ArraySpecifier> () ?? new ArraySpecifier[0]; } get { return GetChildByRole(NullableRole) != null; }
}
public int PointerRank {
get { return GetChildrenByRole(PointerRole).Count(); }
}
public IEnumerable<ArraySpecifier> ArraySpecifiers {
get { return GetChildrenByRole (ArraySpecRole).Cast<ArraySpecifier> (); }
} }
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
@ -57,6 +65,21 @@ namespace ICSharpCode.NRefactory.CSharp
return visitor.VisitComposedType (this, data); return visitor.VisitComposedType (this, data);
} }
public override string ToString()
{
StringBuilder b = new StringBuilder();
b.Append(BaseType.ToString());
if (this.HasNullableSpecifier)
b.Append('?');
b.Append('*', this.PointerRank);
foreach (var arraySpecifier in this.ArraySpecifiers) {
b.Append('[');
b.Append(',', arraySpecifier.Rank);
b.Append(']');
}
return b.ToString();
}
public class ArraySpecifier : DomNode public class ArraySpecifier : DomNode
{ {
public override NodeType NodeType { public override NodeType NodeType {
@ -69,11 +92,14 @@ namespace ICSharpCode.NRefactory.CSharp
get { return (CSharpTokenNode)GetChildByRole (Roles.LBracket); } get { return (CSharpTokenNode)GetChildByRole (Roles.LBracket); }
} }
public int Rank {
get { return GetChildrenByRole(Roles.Comma).Count(); }
}
public CSharpTokenNode RBracket { public CSharpTokenNode RBracket {
get { return (CSharpTokenNode)GetChildByRole (Roles.RBracket); } get { return (CSharpTokenNode)GetChildByRole (Roles.RBracket); }
} }
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
{ {
return default (S); return default (S);

3
ICSharpCode.NRefactory/CSharp/Dom/DomNode.cs

@ -298,8 +298,9 @@ namespace ICSharpCode.NRefactory.CSharp
public const int TypeParameter = 64; public const int TypeParameter = 64;
public const int Constraint = 65; public const int Constraint = 65;
public const int TypeArgument = 66;
public const int Comment = 66; public const int Comment = 67;
} }
} }
} }

27
ICSharpCode.NRefactory/CSharp/Dom/GeneralScope/UsingDeclaration.cs

@ -25,6 +25,7 @@
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
using System.Linq;
using System.Text; using System.Text;
namespace ICSharpCode.NRefactory.CSharp namespace ICSharpCode.NRefactory.CSharp
@ -42,12 +43,32 @@ namespace ICSharpCode.NRefactory.CSharp
public string Namespace { public string Namespace {
get { get {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
SimpleType t = this.Import as SimpleType; if (AppendName(b, this.Import))
if (t == null) return b.ToString();
else
return null; return null;
}
}
static bool AppendName(StringBuilder b, DomNode import)
{
MemberType m = import as MemberType;
if (m != null && !m.TypeArguments.Any()) {
AppendName(b, m.Target);
b.Append('.');
b.Append(m.Identifier);
return true;
}
SimpleType t = import as SimpleType;
if (t != null && !t.TypeArguments.Any()) {
if (t.IsQualifiedWithAlias) {
b.Append(t.AliasIdentifier.Name);
b.Append("::");
}
b.Append(t.Identifier); b.Append(t.Identifier);
return b.ToString(); return true;
} }
return false;
} }
public DomNode Import { public DomNode Import {

6
ICSharpCode.NRefactory/CSharp/Dom/MemberType.cs

@ -52,7 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp
get { return IdentifierToken.Name; } get { return IdentifierToken.Name; }
} }
// TODO: add type arguments public IEnumerable<DomNode> TypeArguments {
get {
return GetChildrenByRole (Roles.TypeArgument);
}
}
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
{ {

23
ICSharpCode.NRefactory/CSharp/Dom/SimpleType.cs

@ -32,13 +32,28 @@ namespace ICSharpCode.NRefactory.CSharp
{ {
public class SimpleType : DomNode public class SimpleType : DomNode
{ {
public const int AliasRole = 100;
public override NodeType NodeType { public override NodeType NodeType {
get { get {
return NodeType.Type; return NodeType.Type;
} }
} }
// TODO: add alias /// <summary>
/// Gets whether this simple type is qualified with an alias
/// </summary>
public bool IsQualifiedWithAlias {
get {
return GetChildByRole (AliasRole) != null;
}
}
public Identifier AliasIdentifier {
get {
return (Identifier)GetChildByRole (AliasRole) ?? CSharp.Identifier.Null;
}
}
public Identifier IdentifierToken { public Identifier IdentifierToken {
get { get {
@ -50,7 +65,11 @@ namespace ICSharpCode.NRefactory.CSharp
get { return IdentifierToken.Name; } get { return IdentifierToken.Name; }
} }
// TODO: add type arguments public IEnumerable<DomNode> TypeArguments {
get {
return GetChildrenByRole (Roles.TypeArgument);
}
}
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data) public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
{ {

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

@ -73,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (nspace.Name != null) { if (nspace.Name != null) {
nDecl = new NamespaceDeclaration (); nDecl = new NamespaceDeclaration ();
nDecl.AddChild (new CSharpTokenNode (Convert (nspace.NamespaceLocation), "namespace".Length), NamespaceDeclaration.Roles.Keyword); nDecl.AddChild (new CSharpTokenNode (Convert (nspace.NamespaceLocation), "namespace".Length), NamespaceDeclaration.Roles.Keyword);
nDecl.AddChild (ConvertMemberName (nspace.Name), NamespaceDeclaration.Roles.Identifier); nDecl.AddChild (ConvertNamespaceName (nspace.Name), NamespaceDeclaration.Roles.Identifier);
nDecl.AddChild (new CSharpTokenNode (Convert (nspace.OpenBrace), 1), NamespaceDeclaration.Roles.LBrace); nDecl.AddChild (new CSharpTokenNode (Convert (nspace.OpenBrace), 1), NamespaceDeclaration.Roles.LBrace);
AddToNamespace (nDecl); AddToNamespace (nDecl);
namespaceStack.Push (nDecl); namespaceStack.Push (nDecl);
@ -91,16 +91,7 @@ namespace ICSharpCode.NRefactory.CSharp
} }
} }
public override void Visit (UsingsBag.Using u) QualifiedIdentifier ConvertNamespaceName (MemberName memberName)
{
UsingDeclaration ud = new UsingDeclaration ();
ud.AddChild (new CSharpTokenNode (Convert (u.UsingLocation), "using".Length), UsingDeclaration.Roles.Keyword);
ud.AddChild (ConvertMemberName (u.NSpace), UsingAliasDeclaration.Roles.Identifier);
ud.AddChild (new CSharpTokenNode (Convert (u.SemicolonLocation), 1), UsingDeclaration.Roles.Semicolon);
AddToNamespace (ud);
}
QualifiedIdentifier ConvertMemberName (MemberName memberName)
{ {
QualifiedIdentifier qi = new QualifiedIdentifier(); QualifiedIdentifier qi = new QualifiedIdentifier();
while (memberName != null) { while (memberName != null) {
@ -110,17 +101,49 @@ namespace ICSharpCode.NRefactory.CSharp
return qi; return qi;
} }
public override void Visit (UsingsBag.Using u)
{
UsingDeclaration ud = new UsingDeclaration ();
ud.AddChild (new CSharpTokenNode (Convert (u.UsingLocation), "using".Length), UsingDeclaration.Roles.Keyword);
ud.AddChild (ConvertImport (u.NSpace), UsingDeclaration.ImportRole);
ud.AddChild (new CSharpTokenNode (Convert (u.SemicolonLocation), 1), UsingDeclaration.Roles.Semicolon);
AddToNamespace (ud);
}
public override void Visit (UsingsBag.AliasUsing u) public override void Visit (UsingsBag.AliasUsing u)
{ {
UsingAliasDeclaration ud = new UsingAliasDeclaration (); UsingAliasDeclaration ud = new UsingAliasDeclaration ();
ud.AddChild (new CSharpTokenNode (Convert (u.UsingLocation), "using".Length), UsingAliasDeclaration.Roles.Keyword); ud.AddChild (new CSharpTokenNode (Convert (u.UsingLocation), "using".Length), UsingAliasDeclaration.Roles.Keyword);
ud.AddChild (new Identifier (u.Identifier.Value, Convert (u.Identifier.Location)), UsingAliasDeclaration.AliasRole); ud.AddChild (new Identifier (u.Identifier.Value, Convert (u.Identifier.Location)), UsingAliasDeclaration.AliasRole);
ud.AddChild (new CSharpTokenNode (Convert (u.AssignLocation), 1), UsingAliasDeclaration.Roles.Assign); ud.AddChild (new CSharpTokenNode (Convert (u.AssignLocation), 1), UsingAliasDeclaration.Roles.Assign);
ud.AddChild (new Identifier (u.Nspace.Name, Convert (u.Nspace.Location)), UsingAliasDeclaration.Roles.Identifier); ud.AddChild (ConvertImport (u.Nspace), UsingAliasDeclaration.ImportRole);
ud.AddChild (new CSharpTokenNode (Convert (u.SemicolonLocation), 1), UsingAliasDeclaration.Roles.Semicolon); ud.AddChild (new CSharpTokenNode (Convert (u.SemicolonLocation), 1), UsingAliasDeclaration.Roles.Semicolon);
AddToNamespace (ud); AddToNamespace (ud);
} }
DomNode ConvertImport (MemberName memberName)
{
if (memberName.IsDoubleColon && memberName.Left != null) {
// left::name
SimpleType t = new SimpleType();
t.AddChild (new Identifier (memberName.Left.Name, Convert(memberName.Location)), SimpleType.AliasRole);
t.AddChild (new Identifier (memberName.Name, Convert(memberName.Location)), SimpleType.Roles.Identifier);
// TODO type arguments
return t;
} else if (memberName.Left != null) {
// left.name
MemberType t = new MemberType();
t.AddChild (ConvertImport (memberName.Left), MemberType.Roles.TargetExpression);
t.AddChild (new Identifier (memberName.Name, Convert(memberName.Location)), MemberType.Roles.Identifier);
return t;
} else {
SimpleType t = new SimpleType();
t.AddChild (new Identifier (memberName.Name, Convert(memberName.Location)), SimpleType.Roles.Identifier);
// TODO type arguments
return t;
}
}
public override void Visit (MemberCore member) public override void Visit (MemberCore member)
{ {
Console.WriteLine ("Unknown member:"); Console.WriteLine ("Unknown member:");
@ -580,7 +603,7 @@ namespace ICSharpCode.NRefactory.CSharp
// if (m.Block is ToplevelBlock) { // if (m.Block is ToplevelBlock) {
// newMethod.AddChild (bodyBlock.FirstChild.NextSibling, MethodDeclaration.Roles.Body); // newMethod.AddChild (bodyBlock.FirstChild.NextSibling, MethodDeclaration.Roles.Body);
// } else { // } else {
newMethod.AddChild (bodyBlock, MethodDeclaration.Roles.Body); newMethod.AddChild (bodyBlock, MethodDeclaration.Roles.Body);
// } // }
} }
typeStack.Peek ().AddChild (newMethod, TypeDeclaration.Roles.Member); typeStack.Peek ().AddChild (newMethod, TypeDeclaration.Roles.Member);
@ -1104,7 +1127,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (Statement stmt in blockStatement.Statements) { foreach (Statement stmt in blockStatement.Statements) {
if (stmt == null) if (stmt == null)
continue; continue;
/* if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) { /* if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) {
result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), AstNode.Roles.Statement); result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), AstNode.Roles.Statement);
curLocal++; curLocal++;
}*/ }*/
@ -1380,10 +1403,12 @@ namespace ICSharpCode.NRefactory.CSharp
var result = new PrimitiveType (); var result = new PrimitiveType ();
if (typeExpression.Type == TypeManager.void_type) { if (typeExpression.Type == TypeManager.void_type) {
result.Keyword = "void"; result.Keyword = "void";
} else if (typeExpression.Type == TypeManager.object_type) { } else if (typeExpression.Type == TypeManager.string_type) {
result.Keyword = "object"; result.Keyword = "string";
} else if (typeExpression.Type == TypeManager.int32_type) { } else if (typeExpression.Type == TypeManager.int32_type) {
result.Keyword = "int"; result.Keyword = "int";
} else if (typeExpression.Type == TypeManager.object_type) {
result.Keyword = "object";
} else { } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -2178,7 +2203,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override object Visit (Mono.CSharp.Linq.SelectMany selectMany) public override object Visit (Mono.CSharp.Linq.SelectMany selectMany)
{ {
var result = new QueryExpressionFromClause (); var result = new QueryExpressionFromClause ();
// TODO: // TODO:
// Mono.CSharp.Linq.Cast cast = selectMany.Expr as Mono.CSharp.Linq.Cast; // Mono.CSharp.Linq.Cast cast = selectMany.Expr as Mono.CSharp.Linq.Cast;
var location = LocationsBag.GetLocations (selectMany); var location = LocationsBag.GetLocations (selectMany);
if (location != null) if (location != null)

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

@ -40,6 +40,8 @@ namespace Mono.CSharp {
bool is_double_colon; bool is_double_colon;
public bool IsDoubleColon { get { return is_double_colon; } }
private MemberName (MemberName left, string name, bool is_double_colon, private MemberName (MemberName left, string name, bool is_double_colon,
Location loc) Location loc)
{ {

Loading…
Cancel
Save