Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented type system -> cecil object table.

  • Loading branch information...
commit 93b3ebf41a88ddb3b2779cdfbbe70ed9c9c4122f 1 parent 6a85f0c
@mkrueger mkrueger authored
Showing with 88 additions and 9 deletions.
  1. +88 −9 ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
View
97 ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs
@@ -43,8 +43,28 @@ public class CecilLoader
/// Gets/Sets the interning provider.
/// </summary>
public IInterningProvider InterningProvider { get; set; }
+
+ /// <summary>
+ /// Gets a value indicating whether this instance stores references to the cecil objects.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this instance has references to the cecil objects; otherwise, <c>false</c>.
+ /// </value>
+ public bool HasCecilReferences { get { return typeSystemTranslationTable != null; } }
#endregion
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.TypeSystem.CecilLoader"/> class.
+ /// </summary>
+ /// <param name='createCecilReferences'>
+ /// If true references to the cecil objects are hold. In this case the cecil loader can do a type system -> cecil mapping.
+ /// </param>
+ public CecilLoader (bool createCecilReferences = false)
+ {
+ if (createCecilReferences)
+ typeSystemTranslationTable = new Dictionary<object, object> ();
+ }
+
#region Load From AssemblyDefinition
/// <summary>
/// Loads the assembly definition into a project content.
@@ -90,6 +110,8 @@ public IProjectContent LoadAssembly(AssemblyDefinition assemblyDefinition)
foreach (CecilTypeDefinition c in types) {
c.Init(this);
}
+ if (HasCecilReferences)
+ typeSystemTranslationTable[pc] = assemblyDefinition;
return pc;
} finally {
this.EarlyBindContext = oldEarlyBindContext;
@@ -173,7 +195,10 @@ public IProjectContent LoadAssemblyFile(string fileName)
throw new ArgumentNullException("fileName");
var param = new ReaderParameters { AssemblyResolver = new DummyAssemblyResolver() };
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(fileName, param);
- return LoadAssembly(asm);
+ var result = LoadAssembly(asm);
+ if (HasCecilReferences)
+ typeSystemTranslationTable[result] = asm;
+ return result;
}
// used to prevent Cecil from loading referenced assemblies
@@ -658,7 +683,7 @@ public IConstantValue ReadConstantValue(CustomAttributeArgument arg)
#region Read Type Definition
sealed class CecilTypeDefinition : DefaultTypeDefinition
{
- TypeDefinition typeDefinition;
+ internal TypeDefinition typeDefinition;
public CecilTypeDefinition(IProjectContent pc, TypeDefinition typeDefinition)
: base(pc, typeDefinition.Namespace, ReflectionHelper.SplitTypeParameterCountFromReflectionName(typeDefinition.Name))
@@ -722,8 +747,8 @@ public void Init(CecilLoader loader)
}
InitMembers(loader);
-
- this.typeDefinition = null;
+ if (!loader.HasCecilReferences)
+ this.typeDefinition = null;
Freeze(); // freeze after initialization
ApplyInterningProvider(loader.InterningProvider);
}
@@ -913,7 +938,7 @@ public IMethod ReadMethod(MethodDefinition method, ITypeDefinition parentType, E
m.IsExtensionMethod = true;
}
- FinishReadMember(m);
+ FinishReadMember(m, method);
return m;
}
@@ -1047,7 +1072,7 @@ public IField ReadField(FieldDefinition field, ITypeDefinition parentType)
f.IsVolatile = true;
}
- FinishReadMember(f);
+ FinishReadMember(f, field);
return f;
}
@@ -1116,7 +1141,7 @@ public IProperty ReadProperty(PropertyDefinition property, ITypeDefinition paren
}
AddAttributes(property, p);
- FinishReadMember(p);
+ FinishReadMember(p, property);
return p;
}
@@ -1156,15 +1181,69 @@ public IEvent ReadEvent(EventDefinition ev, ITypeDefinition parentType)
AddAttributes(ev, e);
- FinishReadMember(e);
+ FinishReadMember(e, ev);
+
return e;
}
#endregion
- void FinishReadMember(AbstractMember member)
+ void FinishReadMember(AbstractMember member, object cecilDefinition)
{
member.Freeze();
member.ApplyInterningProvider(this.InterningProvider);
+ if (HasCecilReferences)
+ typeSystemTranslationTable[member] = cecilDefinition;
+ }
+
+ #region Type system translation table
+ Dictionary<object, object> typeSystemTranslationTable;
+
+ T InternalGetCecilObject<T> (object typeSystemObject) where T : class
+ {
+ if (typeSystemObject == null)
+ throw new ArgumentNullException ("typeSystemObject");
+ if (!HasCecilReferences)
+ throw new NotSupportedException ("This instance contains no cecil references.");
+ object result;
+ if (!typeSystemTranslationTable.TryGetValue (typeSystemObject, out result))
+ throw new InvalidOperationException ("No cecil reference stored for " + typeSystemObject);
+ return result as T;
+ }
+
+ public AssemblyDefinition GetCecilObject (IProjectContent content)
+ {
+ return InternalGetCecilObject<AssemblyDefinition> (content);
}
+
+ public TypeDefinition GetCecilObject (ITypeDefinition type)
+ {
+ if (type == null)
+ throw new ArgumentNullException ("type");
+ var cecilType = type as CecilTypeDefinition;
+ if (cecilType == null)
+ throw new ArgumentException ("type is no cecil type definition.");
+ return cecilType.typeDefinition;
+ }
+
+ public MethodDefinition GetCecilObject (IMethod method)
+ {
+ return InternalGetCecilObject<MethodDefinition> (method);
+ }
+
+ public FieldDefinition GetCecilObject (IField field)
+ {
+ return InternalGetCecilObject<FieldDefinition> (field);
+ }
+
+ public EventDefinition GetCecilObject (IEvent evt)
+ {
+ return InternalGetCecilObject<EventDefinition> (evt);
+ }
+
+ public PropertyDefinition GetCecilObject (IProperty property)
+ {
+ return InternalGetCecilObject<PropertyDefinition> (property);
+ }
+ #endregion
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.