Browse Source

Add simple type references.

newNRvisualizers
Daniel Grunwald 16 years ago
parent
commit
4d7b266e87
  1. 2
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  2. 81
      ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs
  3. 5
      ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs
  4. 40
      ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs
  5. 46
      ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs
  6. 3
      README

2
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -149,6 +149,8 @@
<Compile Include="TypeSystem\Implementation\AbstractTypeReference.cs" /> <Compile Include="TypeSystem\Implementation\AbstractTypeReference.cs" />
<Compile Include="TypeSystem\Implementation\DefaultExplicitInterfaceImplementation.cs" /> <Compile Include="TypeSystem\Implementation\DefaultExplicitInterfaceImplementation.cs" />
<Compile Include="TypeSystem\Implementation\DefaultParameter.cs" /> <Compile Include="TypeSystem\Implementation\DefaultParameter.cs" />
<Compile Include="TypeSystem\Implementation\GetClassTypeReference.cs" />
<Compile Include="TypeSystem\Implementation\NestedTypeReference.cs" />
<Compile Include="TypeSystem\Implementation\ProxyTypeResolveContext.cs" /> <Compile Include="TypeSystem\Implementation\ProxyTypeResolveContext.cs" />
<Compile Include="TypeSystem\Implementation\DefaultTypeDefinition.cs" /> <Compile Include="TypeSystem\Implementation\DefaultTypeDefinition.cs" />
<Compile Include="TypeSystem\Implementation\NullType.cs" /> <Compile Include="TypeSystem\Implementation\NullType.cs" />

81
ICSharpCode.NRefactory/TypeSystem/CecilProjectContent.cs

@ -77,10 +77,59 @@ namespace ICSharpCode.NRefactory.TypeSystem
#endregion #endregion
#region Read Type Reference #region Read Type Reference
public static ITypeReference ReadTypeReference(TypeReference attributeType, ITypeResolveContext earlyBindContext) /// <summary>
/// Reads a type reference.
/// </summary>
/// <param name="type">The Cecil type reference that should be converted into
/// a type system type reference.</param>
/// <param name="typeAttributes">Attributes associated with the Cecil type reference.
/// This is used to support the 'dynamic' type.</param>
/// <param name="earlyBindContext">Early binding context - used to pre-resolve
/// type references where possible.</param>
/// <param name="entity">The entity that owns this type reference.
/// Used for generic type references.</param>
public static ITypeReference ReadTypeReference(
TypeReference type,
ICustomAttributeProvider typeAttributes = null,
IEntity entity = null,
ITypeResolveContext earlyBindContext = null)
{ {
int typeIndex = 0;
return ReadTypeReference(type, typeAttributes, entity, earlyBindContext, ref typeIndex);
}
static ITypeReference ReadTypeReference(
TypeReference type,
ICustomAttributeProvider typeAttributes,
IEntity entity ,
ITypeResolveContext earlyBindContext,
ref int typeIndex)
{
while (type is OptionalModifierType || type is RequiredModifierType) {
type = ((TypeSpecification)type).ElementType;
}
if (type == null) {
return SharedTypes.UnknownType;
}
throw new NotImplementedException(); throw new NotImplementedException();
} }
static bool HasDynamicAttribute(ICustomAttributeProvider attributeProvider, int typeIndex)
{
if (attributeProvider == null || !attributeProvider.HasCustomAttributes)
return false;
foreach (CustomAttribute a in attributeProvider.CustomAttributes) {
if (a.Constructor.DeclaringType.FullName == "System.Runtime.CompilerServices.DynamicAttribute") {
if (a.ConstructorArguments.Count == 1) {
CustomAttributeArgument[] values = a.ConstructorArguments[0].Value as CustomAttributeArgument[];
if (values != null && typeIndex < values.Length && values[typeIndex].Value is bool)
return (bool)values[typeIndex].Value;
}
return true;
}
}
return false;
}
#endregion #endregion
#region Read Attributes #region Read Attributes
@ -107,7 +156,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
public CecilAttribute(CustomAttribute ca, ITypeResolveContext earlyBindContext) public CecilAttribute(CustomAttribute ca, ITypeResolveContext earlyBindContext)
{ {
this.attributeType = ReadTypeReference(ca.AttributeType, earlyBindContext); this.attributeType = ReadTypeReference(ca.AttributeType, earlyBindContext: earlyBindContext);
this.ca = ca; this.ca = ca;
this.earlyBindContext = earlyBindContext; this.earlyBindContext = earlyBindContext;
} }
@ -176,7 +225,33 @@ namespace ICSharpCode.NRefactory.TypeSystem
#region Read Constant Value #region Read Constant Value
public static IConstantValue ReadConstantValue(CustomAttributeArgument arg, ITypeResolveContext earlyBindContext) public static IConstantValue ReadConstantValue(CustomAttributeArgument arg, ITypeResolveContext earlyBindContext)
{ {
throw new NotImplementedException(); return new CecilConstantValue(arg.Type, arg.Value, earlyBindContext);
}
sealed class CecilConstantValue : Immutable, IConstantValue
{
ITypeReference type;
object value;
public CecilConstantValue(TypeReference type, object value, ITypeResolveContext earlyBindContext)
{
this.type = ReadTypeReference(type, earlyBindContext: earlyBindContext);
TypeReference valueType = value as TypeReference;
if (valueType != null)
this.value = ReadTypeReference(valueType, earlyBindContext: earlyBindContext);
else
this.value = value;
}
public IType GetValueType(ITypeResolveContext context)
{
return type.Resolve(context);
}
public object GetValue(ITypeResolveContext context)
{
return value;
}
} }
#endregion #endregion
} }

5
ICSharpCode.NRefactory/TypeSystem/Implementation/AbstractType.cs

@ -110,5 +110,10 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public abstract override int GetHashCode(); public abstract override int GetHashCode();
public abstract bool Equals(IType other); public abstract bool Equals(IType other);
public override string ToString()
{
return this.DotNetName;
}
} }
} }

40
ICSharpCode.NRefactory/TypeSystem/Implementation/GetClassTypeReference.cs

@ -0,0 +1,40 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
/// <summary>
/// Type Reference used when the fully qualified type name is known.
/// </summary>
public class GetClassTypeReference : AbstractTypeReference
{
string fullTypeName;
int typeParameterCount;
public GetClassTypeReference(string fullTypeName, int typeParameterCount)
{
if (fullTypeName == null)
throw new ArgumentNullException("fullTypeName");
this.fullTypeName = fullTypeName;
this.typeParameterCount = typeParameterCount;
}
public override IType Resolve(ITypeResolveContext context)
{
return context.GetClass(fullTypeName, typeParameterCount, StringComparer.Ordinal);
}
public override string ToString()
{
if (typeParameterCount == 0)
return fullTypeName;
else
return fullTypeName + "`" + typeParameterCount;
}
}
}

46
ICSharpCode.NRefactory/TypeSystem/Implementation/NestedTypeReference.cs

@ -0,0 +1,46 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
/// <summary>
/// Type reference used to reference nested types.
/// </summary>
public class NestedTypeReference : AbstractTypeReference
{
ITypeReference baseTypeRef; string name; int typeParameterCount;
public NestedTypeReference(ITypeReference baseTypeRef, string name, int typeParameterCount)
{
if (baseTypeRef == null)
throw new ArgumentNullException("baseTypeRef");
if (name == null)
throw new ArgumentNullException("name");
this.baseTypeRef = baseTypeRef;
this.name = name;
this.typeParameterCount = typeParameterCount;
}
public override IType Resolve(ITypeResolveContext context)
{
foreach (IType type in baseTypeRef.GetNestedTypes(context)) {
if (type.Name == name && type.TypeParameterCount == typeParameterCount)
return type;
}
return SharedTypes.UnknownType;
}
public override string ToString()
{
if (typeParameterCount == 0)
return baseTypeRef + "+" + name;
else
return baseTypeRef + "+" + name + "`" + typeParameterCount;
}
}
}

3
README

@ -5,3 +5,6 @@ ICSharpCode.NRefactory.TypeSystem:
ICSharpCode.NRefactory.TypeSystem.Implementation: ICSharpCode.NRefactory.TypeSystem.Implementation:
Contains base classes that help implementing the type system interfaces. Contains base classes that help implementing the type system interfaces.
ICSharpCode.NRefactory.CSharp.Dom:
Abstract Syntax Tree for C#

Loading…
Cancel
Save