Browse Source

CecilLoader: fixed NotImplementedExceptions that prevented loading mscorlib

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
f63047ceef
  1. 3
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  2. 68
      ICSharpCode.NRefactory/TypeSystem/ByReferenceType.cs
  3. 91
      ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
  4. 18
      ICSharpCode.NRefactory/TypeSystem/ConstructedType.cs
  5. 23
      ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs
  6. 5
      ICSharpCode.NRefactory/TypeSystem/Implementation/BitVector16.cs
  7. 69
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAttribute.cs
  8. 167
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs
  9. 15
      ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs

3
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -142,6 +142,7 @@ @@ -142,6 +142,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TypeSystem\Accessibility.cs" />
<Compile Include="TypeSystem\ArrayType.cs" />
<Compile Include="TypeSystem\ByReferenceType.cs" />
<Compile Include="TypeSystem\CecilLoader.cs" />
<Compile Include="TypeSystem\ClassType.cs" />
<Compile Include="TypeSystem\ConstructedType.cs" />
@ -162,9 +163,11 @@ @@ -162,9 +163,11 @@
<Compile Include="TypeSystem\Implementation\AbstractType.cs" />
<Compile Include="TypeSystem\Implementation\AbstractTypeReference.cs" />
<Compile Include="TypeSystem\Implementation\BitVector16.cs" />
<Compile Include="TypeSystem\Implementation\DefaultAttribute.cs" />
<Compile Include="TypeSystem\Implementation\DefaultExplicitInterfaceImplementation.cs" />
<Compile Include="TypeSystem\Implementation\DefaultMethod.cs" />
<Compile Include="TypeSystem\Implementation\DefaultParameter.cs" />
<Compile Include="TypeSystem\Implementation\DefaultTypeParameter.cs" />
<Compile Include="TypeSystem\Implementation\GetClassTypeReference.cs" />
<Compile Include="TypeSystem\Implementation\MultiTypeResolveContext.cs" />
<Compile Include="TypeSystem\Implementation\NestedTypeReference.cs" />

68
ICSharpCode.NRefactory/TypeSystem/ByReferenceType.cs

@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@

using System;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace ICSharpCode.NRefactory.TypeSystem
{
public class ByReferenceType : TypeWithElementType
{
public ByReferenceType(IType elementType) : base(elementType)
{
}
public override string NameSuffix {
get {
return "&";
}
}
public override Nullable<bool> IsReferenceType {
get { return null; }
}
public override int GetHashCode()
{
return elementType.GetHashCode() ^ 91725813;
}
public override bool Equals(IType other)
{
ByReferenceType a = other as ByReferenceType;
return a != null && elementType.Equals(a.elementType);
}
}
public class ByReferenceTypeReference : AbstractTypeReference
{
readonly ITypeReference elementType;
public ByReferenceTypeReference(ITypeReference elementType)
{
if (elementType == null)
throw new ArgumentNullException("elementType");
this.elementType = elementType;
}
public ITypeReference ElementType {
get { return elementType; }
}
public override IType Resolve(ITypeResolveContext context)
{
return new ByReferenceType(elementType.Resolve(context));
}
public override string ToString()
{
return elementType.ToString() + "&";
}
public static ITypeReference Create(ITypeReference elementType)
{
if (elementType is IType)
return new ByReferenceType((IType)elementType);
else
return new ByReferenceTypeReference(elementType);
}
}
}

91
ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -164,7 +164,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -164,7 +164,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
ITypeReference CreateType(
TypeReference type,
IEntity entity ,
IEntity entity,
ICustomAttributeProvider typeAttributes, ref int typeIndex)
{
while (type is OptionalModifierType || type is RequiredModifierType) {
@ -174,8 +174,11 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -174,8 +174,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
return SharedTypes.UnknownType;
}
if (type is ByReferenceType) {
throw new NotImplementedException();
if (type is Mono.Cecil.ByReferenceType) {
return ByReferenceTypeReference.Create(
CreateType(
(type as Mono.Cecil.ByReferenceType).ElementType,
entity, typeAttributes, ref typeIndex));
} else if (type is Mono.Cecil.PointerType) {
typeIndex++;
return PointerTypeReference.Create(
@ -197,7 +200,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -197,7 +200,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
typeIndex++;
para[i] = CreateType(gType.GenericArguments[i], entity, typeAttributes, ref typeIndex);
}
return ConstructedType.Create(baseType, para);
return ConstructedTypeReference.Create(baseType, para);
} else if (type is GenericParameter) {
GenericParameter typeGP = type as GenericParameter;
if (typeGP.Owner is MethodDefinition) {
@ -286,7 +289,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -286,7 +289,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
if (attributeProvider == null || !attributeProvider.HasCustomAttributes)
return false;
foreach (CustomAttribute a in attributeProvider.CustomAttributes) {
if (a.Constructor.DeclaringType.FullName == "System.Runtime.CompilerServices.DynamicAttribute") {
if (a.Constructor.DeclaringType.FullName == typeof(DynamicAttribute).FullName) {
if (a.ConstructorArguments.Count == 1) {
CustomAttributeArgument[] values = a.ConstructorArguments[0].Value as CustomAttributeArgument[];
if (values != null && typeIndex < values.Length && values[typeIndex].Value is bool)
@ -406,14 +409,14 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -406,14 +409,14 @@ namespace ICSharpCode.NRefactory.TypeSystem
InitModifiers();
if (typeDefinition.HasGenericParameters) {
throw new NotImplementedException();
/*foreach (GenericParameter g in td.GenericParameters) {
this.TypeParameters.Add(new DefaultTypeParameter(this, g.Name, g.Position));
for (int i = 0; i < typeDefinition.GenericParameters.Count; i++) {
if (typeDefinition.GenericParameters[i].Position != i)
throw new InvalidOperationException("g.Position != i");
this.TypeParameters.Add(new DefaultTypeParameter(this, i, typeDefinition.GenericParameters[i].Name));
}
for (int i = 0; i < typeDefinition.GenericParameters.Count; i++) {
loader.AddConstraints((DefaultTypeParameter)this.TypeParameters[i], typeDefinition.GenericParameters[i]);
}
int i = 0;
foreach (GenericParameter g in td.GenericParameters) {
AddConstraintsFromType(this.TypeParameters[i++], g);
}*/
}
InitNestedTypes(loader); // nested types can be initialized only after generic parameters were created
@ -556,20 +559,20 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -556,20 +559,20 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
#endregion
#region ReadMethod
#region Read Method
IMethod ReadMethod(MethodDefinition method, ITypeDefinition parentType, EntityType methodType)
{
DefaultMethod m = new DefaultMethod(parentType, method.Name);
m.EntityType = methodType;
if (method.HasGenericParameters) {
throw new NotImplementedException();
/*foreach (GenericParameter g in method.GenericParameters) {
m.TypeParameters.Add(new DefaultTypeParameter(this, g.Name, g.Position));
for (int i = 0; i < method.GenericParameters.Count; i++) {
if (method.GenericParameters[i].Position != i)
throw new InvalidOperationException("g.Position != i");
m.TypeParameters.Add(new DefaultTypeParameter(m, i, method.GenericParameters[i].Name));
}
for (int i = 0; i < method.GenericParameters.Count; i++) {
AddConstraints((DefaultTypeParameter)m.TypeParameters[i], method.GenericParameters[i]);
}
int i = 0;
foreach (GenericParameter g in method.GenericParameters) {
AddConstraintsFromType(m.TypeParameters[i++], g);
}*/
}
if (method.IsConstructor)
@ -647,16 +650,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -647,16 +650,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
#endregion
bool IsVisible(FieldAttributes att)
{
att &= FieldAttributes.FieldAccessMask;
return IncludeInternalMembers
|| att == FieldAttributes.Public
|| att == FieldAttributes.Family
|| att == FieldAttributes.FamORAssem;
}
#region ReadParameter
#region Read Parameter
public IParameter ReadParameter(ParameterDefinition parameter, IParameterizedMember parentMember = null)
{
if (parameter == null)
@ -668,7 +662,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -668,7 +662,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
if (parameter.HasCustomAttributes)
AddAttributes(parameter, p.Attributes);
if (parameter.ParameterType is ByReferenceType) {
if (parameter.ParameterType is Mono.Cecil.ByReferenceType) {
if (parameter.IsOut)
p.IsOut = true;
else
@ -692,11 +686,46 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -692,11 +686,46 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
#endregion
#region Read Field
bool IsVisible(FieldAttributes att)
{
att &= FieldAttributes.FieldAccessMask;
return IncludeInternalMembers
|| att == FieldAttributes.Public
|| att == FieldAttributes.Family
|| att == FieldAttributes.FamORAssem;
}
#endregion
void AddExplicitInterfaceImplementations(MethodDefinition method, AbstractMember targetMember)
{
if (method.HasOverrides) {
throw new NotImplementedException();
}
}
#region Type Parameter Constraints
void AddConstraints(DefaultTypeParameter tp, GenericParameter g)
{
switch (g.Attributes & GenericParameterAttributes.VarianceMask) {
case GenericParameterAttributes.Contravariant:
tp.Variance = VarianceModifier.Contravariant;
break;
case GenericParameterAttributes.Covariant:
tp.Variance = VarianceModifier.Covariant;
break;
}
tp.HasDefaultConstructorConstraint = g.HasReferenceTypeConstraint;
tp.HasValueTypeConstraint = g.HasNotNullableValueTypeConstraint;
tp.HasDefaultConstructorConstraint = g.HasDefaultConstructorConstraint;
if (g.HasConstraints) {
foreach (TypeReference constraint in g.Constraints) {
tp.Constraints.Add(ReadTypeReference(constraint, entity: tp.Parent));
}
}
}
#endregion
}
}

18
ICSharpCode.NRefactory/TypeSystem/ConstructedType.cs

@ -176,7 +176,14 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -176,7 +176,14 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
return hashCode;
}
}
/// <summary>
/// ConstructedTypeReference is a reference to generic class that specifies the type parameters.
/// Example: List&lt;string&gt;
/// </summary>
public class ConstructedTypeReference : AbstractTypeReference
{
public static ITypeReference Create(ITypeReference genericType, IEnumerable<ITypeReference> typeArguments)
{
if (genericType == null)
@ -195,14 +202,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -195,14 +202,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
return new ConstructedTypeReference(genericType, typeArgs);
}
}
}
/// <summary>
/// ConstructedTypeReference is a reference to generic class that specifies the type parameters.
/// Example: List&lt;string&gt;
/// </summary>
public class ConstructedTypeReference : AbstractTypeReference
{
readonly ITypeReference genericType;
readonly ITypeReference[] typeArguments;

23
ICSharpCode.NRefactory/TypeSystem/ITypeParameter.cs

@ -24,15 +24,21 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -24,15 +24,21 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary>
IList<IAttribute> Attributes { get; }
/// <summary>
/// Gets the class or method for which this type parameter is defined.
/// This property never returns null.
/// </summary>
IEntity Parent { get; }
/// <summary>
/// The method this type parameter is defined for.
/// This property returns null if the type parameter belong to a class.
/// This property returns null if the type parameter belongs to a class.
/// </summary>
IMethod ParentMethod { get; }
/// <summary>
/// The class this type parameter is defined for.
/// This property returns null if the type parameter belong to a method.
/// This property returns null if the type parameter belongs to a method.
/// </summary>
ITypeDefinition ParentClass { get; }
@ -44,7 +50,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -44,7 +50,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Gets if the type parameter has the 'new()' constraint.
/// </summary>
bool HasConstructableConstraint { get; }
bool HasDefaultConstructorConstraint { get; }
/// <summary>
/// Gets if the type parameter has the 'class' constraint.
@ -77,7 +83,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -77,7 +83,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary>
/// Represents the variance of a type parameter.
/// </summary>
public enum VarianceModifier
public enum VarianceModifier : byte
{
/// <summary>
/// The type parameter is not variant.
@ -110,6 +116,13 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -110,6 +116,13 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
}
IEntity ITypeParameter.Parent {
get {
Contract.Ensures(Contract.Result<IEntity>() != null);
return null;
}
}
IMethod ITypeParameter.ParentMethod {
get {
return null;
@ -129,7 +142,7 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -129,7 +142,7 @@ namespace ICSharpCode.NRefactory.TypeSystem
}
}
bool ITypeParameter.HasConstructableConstraint {
bool ITypeParameter.HasDefaultConstructorConstraint {
get { return false; }
}

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

@ -22,6 +22,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -22,6 +22,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
}
public ushort Data {
get { return data; }
set { data = value; }
}
#region Equals and GetHashCode implementation
public override bool Equals(object obj)
{

69
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultAttribute.cs

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
/// <summary>
/// Default implementation of <see cref="IAttribute"/>.
/// </summary>
public sealed class DefaultAttribute : AbstractFreezable, IAttribute
{
DomRegion region;
ITypeReference attributeType = SharedTypes.UnknownType;
IList<IConstantValue> positionalArguments;
IList<KeyValuePair<string, IConstantValue>> namedArguments;
protected override void FreezeInternal()
{
attributeType.Freeze();
positionalArguments = FreezeList(positionalArguments);
if (namedArguments == null || namedArguments.Count == 0) {
namedArguments = EmptyList<KeyValuePair<string, IConstantValue>>.Instance;
} else {
namedArguments = Array.AsReadOnly(namedArguments.ToArray());
foreach (var pair in namedArguments) {
pair.Value.Freeze();
}
}
base.FreezeInternal();
}
public DomRegion Region {
get { return region; }
set {
CheckBeforeMutation();
region = value;
}
}
public ITypeReference AttributeType {
get { return attributeType; }
set {
CheckBeforeMutation();
attributeType = value;
}
}
public IList<IConstantValue> PositionalArguments {
get {
if (positionalArguments == null)
positionalArguments = new List<IConstantValue>();
return positionalArguments;
}
}
public IList<KeyValuePair<string, IConstantValue>> NamedArguments {
get {
if (namedArguments == null)
namedArguments = new List<KeyValuePair<string, IConstantValue>>();
return namedArguments;
}
}
}
}

167
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultTypeParameter.cs

@ -0,0 +1,167 @@ @@ -0,0 +1,167 @@
// Copyright (c) 2010 AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
/// <summary>
/// Default implementation of <see cref="ITypeParameter"/>.
/// </summary>
public class DefaultTypeParameter : AbstractType, ITypeParameter
{
IEntity parent;
string name;
int index;
IList<ITypeReference> constraints;
IList<IAttribute> attributes;
VarianceModifier variance;
BitVector16 flags;
const ushort FlagReferenceTypeConstraint = 0x0001;
const ushort FlagValueTypeConstraint = 0x0002;
const ushort FlagDefaultConstructorConstraint = 0x0004;
protected override void FreezeInternal()
{
constraints = FreezeList(constraints);
attributes = FreezeList(attributes);
base.FreezeInternal();
}
public DefaultTypeParameter(IMethod parentMethod, int index, string name)
{
if (parentMethod == null)
throw new ArgumentNullException("parentMethod");
if (index < 0)
throw new ArgumentOutOfRangeException("index", index, "Value must not be negative");
if (name == null)
throw new ArgumentNullException("name");
this.parent = parentMethod;
this.index = index;
this.name = name;
}
public DefaultTypeParameter(ITypeDefinition parentClass, int index, string name)
{
if (parentClass == null)
throw new ArgumentNullException("parentClass");
if (index < 0)
throw new ArgumentOutOfRangeException("index", index, "Value must not be negative");
if (name == null)
throw new ArgumentNullException("name");
this.parent = parentClass;
this.index = index;
this.name = name;
}
public override string Name {
get { return name; }
}
public override bool? IsReferenceType {
get {
switch (flags.Data & (FlagReferenceTypeConstraint | FlagValueTypeConstraint)) {
case FlagReferenceTypeConstraint:
return true;
case FlagValueTypeConstraint:
return false;
default:
return null;
}
}
}
public override int GetHashCode()
{
int hashCode = parent.GetHashCode();
unchecked {
hashCode += 1000000033 * index.GetHashCode();
}
return hashCode;
}
public override bool Equals(IType other)
{
DefaultTypeParameter p = other as DefaultTypeParameter;
if (p == null)
return false;
return parent.Equals(p.parent)
&& index == p.index;
}
public int Index {
get { return index; }
}
public IList<IAttribute> Attributes {
get {
if (attributes == null)
attributes = new List<IAttribute>();
return attributes;
}
}
public IEntity Parent {
get { return parent; }
}
public IMethod ParentMethod {
get { return parent as IMethod; }
}
public ITypeDefinition ParentClass {
get { return parent as ITypeDefinition; }
}
public IList<ITypeReference> Constraints {
get {
if (constraints == null)
constraints = new List<ITypeReference>();
return constraints;
}
}
public bool HasDefaultConstructorConstraint {
get { return flags[FlagDefaultConstructorConstraint]; }
set {
CheckBeforeMutation();
flags[FlagDefaultConstructorConstraint] = value;
}
}
public bool HasReferenceTypeConstraint {
get { return flags[FlagReferenceTypeConstraint]; }
set {
CheckBeforeMutation();
flags[FlagReferenceTypeConstraint] = value;
}
}
public bool HasValueTypeConstraint {
get { return flags[FlagValueTypeConstraint]; }
set {
CheckBeforeMutation();
flags[FlagValueTypeConstraint] = value;
}
}
public VarianceModifier Variance {
get { return variance; }
set {
CheckBeforeMutation();
variance = value;
}
}
public virtual IType BoundTo {
get { return null; }
}
public virtual ITypeParameter UnboundTypeParameter {
get { return null; }
}
}
}

15
ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs

@ -54,6 +54,11 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -54,6 +54,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary>
sealed class UnknownTypeImpl : AbstractType
{
public UnknownTypeImpl()
{
Freeze();
}
public override string Name {
get { return "?"; }
}
@ -78,6 +83,11 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -78,6 +83,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary>
sealed class NullType : AbstractType
{
public NullType()
{
Freeze();
}
public override string Name {
get { return "null"; }
}
@ -102,6 +112,11 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -102,6 +112,11 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// </summary>
sealed class DynamicType : AbstractType
{
public DynamicType()
{
Freeze();
}
public override string Name {
get { return "dynamic"; }
}

Loading…
Cancel
Save