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

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// ComposedType.cs
//
// Author:
@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ICSharpCode.NRefactory.CSharp
{
@ -48,8 +48,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -48,8 +48,16 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
public IEnumerable<ArraySpecifier> Compositions {
get { return GetChildrenByRole (ArraySpecRole).Cast<ArraySpecifier> () ?? new ArraySpecifier[0]; }
public bool HasNullableSpecifier {
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)
@ -57,6 +65,21 @@ namespace ICSharpCode.NRefactory.CSharp @@ -57,6 +65,21 @@ namespace ICSharpCode.NRefactory.CSharp
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 override NodeType NodeType {
@ -69,11 +92,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -69,11 +92,14 @@ namespace ICSharpCode.NRefactory.CSharp
get { return (CSharpTokenNode)GetChildByRole (Roles.LBracket); }
}
public int Rank {
get { return GetChildrenByRole(Roles.Comma).Count(); }
}
public CSharpTokenNode RBracket {
get { return (CSharpTokenNode)GetChildByRole (Roles.RBracket); }
}
public override S AcceptVisitor<T, S> (DomVisitor<T, S> visitor, T data)
{
return default (S);

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

@ -298,8 +298,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -298,8 +298,9 @@ namespace ICSharpCode.NRefactory.CSharp
public const int TypeParameter = 64;
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 @@ @@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using System.Linq;
using System.Text;
namespace ICSharpCode.NRefactory.CSharp
@ -42,12 +43,32 @@ namespace ICSharpCode.NRefactory.CSharp @@ -42,12 +43,32 @@ namespace ICSharpCode.NRefactory.CSharp
public string Namespace {
get {
StringBuilder b = new StringBuilder();
SimpleType t = this.Import as SimpleType;
if (t == null)
if (AppendName(b, this.Import))
return b.ToString();
else
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);
return b.ToString();
return true;
}
return false;
}
public DomNode Import {

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

@ -52,7 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -52,7 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp
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)
{

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

@ -32,13 +32,28 @@ namespace ICSharpCode.NRefactory.CSharp @@ -32,13 +32,28 @@ namespace ICSharpCode.NRefactory.CSharp
{
public class SimpleType : DomNode
{
public const int AliasRole = 100;
public override NodeType NodeType {
get {
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 {
get {
@ -50,7 +65,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -50,7 +65,11 @@ namespace ICSharpCode.NRefactory.CSharp
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)
{

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

@ -73,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -73,7 +73,7 @@ namespace ICSharpCode.NRefactory.CSharp
if (nspace.Name != null) {
nDecl = new NamespaceDeclaration ();
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);
AddToNamespace (nDecl);
namespaceStack.Push (nDecl);
@ -91,16 +91,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -91,16 +91,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
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 (ConvertMemberName (u.NSpace), UsingAliasDeclaration.Roles.Identifier);
ud.AddChild (new CSharpTokenNode (Convert (u.SemicolonLocation), 1), UsingDeclaration.Roles.Semicolon);
AddToNamespace (ud);
}
QualifiedIdentifier ConvertMemberName (MemberName memberName)
QualifiedIdentifier ConvertNamespaceName (MemberName memberName)
{
QualifiedIdentifier qi = new QualifiedIdentifier();
while (memberName != null) {
@ -110,17 +101,49 @@ namespace ICSharpCode.NRefactory.CSharp @@ -110,17 +101,49 @@ namespace ICSharpCode.NRefactory.CSharp
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)
{
UsingAliasDeclaration ud = new UsingAliasDeclaration ();
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 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);
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)
{
Console.WriteLine ("Unknown member:");
@ -580,7 +603,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -580,7 +603,7 @@ namespace ICSharpCode.NRefactory.CSharp
// if (m.Block is ToplevelBlock) {
// newMethod.AddChild (bodyBlock.FirstChild.NextSibling, MethodDeclaration.Roles.Body);
// } else {
newMethod.AddChild (bodyBlock, MethodDeclaration.Roles.Body);
newMethod.AddChild (bodyBlock, MethodDeclaration.Roles.Body);
// }
}
typeStack.Peek ().AddChild (newMethod, TypeDeclaration.Roles.Member);
@ -1104,7 +1127,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1104,7 +1127,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (Statement stmt in blockStatement.Statements) {
if (stmt == null)
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);
curLocal++;
}*/
@ -1380,10 +1403,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1380,10 +1403,12 @@ namespace ICSharpCode.NRefactory.CSharp
var result = new PrimitiveType ();
if (typeExpression.Type == TypeManager.void_type) {
result.Keyword = "void";
} else if (typeExpression.Type == TypeManager.object_type) {
result.Keyword = "object";
} else if (typeExpression.Type == TypeManager.string_type) {
result.Keyword = "string";
} else if (typeExpression.Type == TypeManager.int32_type) {
result.Keyword = "int";
} else if (typeExpression.Type == TypeManager.object_type) {
result.Keyword = "object";
} else {
throw new NotImplementedException();
}
@ -2178,7 +2203,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2178,7 +2203,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override object Visit (Mono.CSharp.Linq.SelectMany selectMany)
{
var result = new QueryExpressionFromClause ();
// TODO:
// TODO:
// Mono.CSharp.Linq.Cast cast = selectMany.Expr as Mono.CSharp.Linq.Cast;
var location = LocationsBag.GetLocations (selectMany);
if (location != null)

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

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

Loading…
Cancel
Save