Author: jbevain
Date: 2007-06-20 16:11:22 -0400 (Wed, 20 Jun 2007)
New Revision: 80359

Modified:
   trunk/cecil/lib/ChangeLog
   trunk/cecil/lib/Mono.Cecil/DefaultReferenceImporter.cs
   trunk/cecil/lib/Mono.Cecil/ModuleDefinition.cs
   trunk/cecil/lib/Mono.Cecil/ReflectionHelper.cs
Log:
2007-06-20  Jb Evain  <[EMAIL PROTECTED]>

        * Mono.Cecil/ReflectionHelper.cs
          Mono.Cecil/DefaultReferenceImporter.cs
          Mono.Cecil/ModuleDefinition.cs:
                Move Cecil types import logic into
                the DefaultReferenceImporter.

                Needed by the merger and the extractor.


Modified: trunk/cecil/lib/ChangeLog
===================================================================
--- trunk/cecil/lib/ChangeLog   2007-06-20 20:04:00 UTC (rev 80358)
+++ trunk/cecil/lib/ChangeLog   2007-06-20 20:11:22 UTC (rev 80359)
@@ -1,3 +1,13 @@
+2007-06-20  Jb Evain  <[EMAIL PROTECTED]>
+
+       * Mono.Cecil/ReflectionHelper.cs
+         Mono.Cecil/DefaultReferenceImporter.cs
+         Mono.Cecil/ModuleDefinition.cs:
+               Move Cecil types import logic into
+               the DefaultReferenceImporter.
+
+               Needed by the merger and the extractor.
+
 2007-06-19  Jb Evain  <[EMAIL PROTECTED]>
 
        * Mono.Cecil.Cil/MethodBody.cs:

Modified: trunk/cecil/lib/Mono.Cecil/DefaultReferenceImporter.cs
===================================================================
--- trunk/cecil/lib/Mono.Cecil/DefaultReferenceImporter.cs      2007-06-20 
20:04:00 UTC (rev 80358)
+++ trunk/cecil/lib/Mono.Cecil/DefaultReferenceImporter.cs      2007-06-20 
20:11:22 UTC (rev 80359)
@@ -28,33 +28,202 @@
 
 namespace Mono.Cecil {
 
+       using System;
+       using System.Collections;
+
        public class DefaultReferenceImporter : IReferenceImporter {
 
-               ModuleDefinition _module;
+               ModuleDefinition m_module;
 
-               public ModuleDefinition Module
-               {
-                       get { return _module; }
+               public ModuleDefinition Module {
+                       get { return m_module; }
                }
 
                public DefaultReferenceImporter (ModuleDefinition module)
                {
-                       _module = module;
+                       m_module = module;
                }
 
-               public virtual TypeReference ImportTypeReference (TypeReference 
type, ImportContext context)
+               public AssemblyNameReference ImportAssembly 
(AssemblyNameReference asm)
                {
-                       return _module.Controller.Helper.ImportTypeReference 
(type, context);
+                       AssemblyNameReference asmRef = GetAssemblyNameReference 
(asm);
+                       if (asmRef != null)
+                               return asmRef;
+
+                       asmRef = new AssemblyNameReference (
+                               asm.Name, asm.Culture, asm.Version);
+                       asmRef.PublicKeyToken = asm.PublicKeyToken;
+                       asmRef.HashAlgorithm = asm.HashAlgorithm;
+                       m_module.AssemblyReferences.Add (asmRef);
+                       return asmRef;
                }
 
-               public virtual FieldReference ImportFieldReference 
(FieldReference field, ImportContext context)
+               AssemblyNameReference GetAssemblyNameReference 
(AssemblyNameReference asm)
                {
-                       return _module.Controller.Helper.ImportFieldReference 
(field, context);
+                       foreach (AssemblyNameReference reference in 
m_module.AssemblyReferences)
+                               if (reference.FullName == asm.FullName)
+                                       return reference;
+
+                       return null;
                }
 
-               public virtual MethodReference ImportMethodReference 
(MethodReference method, ImportContext context)
+               TypeReference GetTypeSpec (TypeReference t, ImportContext 
context)
                {
-                       return _module.Controller.Helper.ImportMethodReference 
(method, context);
+                       Stack s = new Stack ();
+                       while (t is TypeSpecification) {
+                               s.Push (t);
+                               t = (t as TypeSpecification).ElementType;
+                       }
+
+                       TypeReference elementType = ImportTypeReference (t, 
context);
+                       while (s.Count > 0) {
+                               t = (TypeReference) s.Pop ();
+                               if (t is PointerType)
+                                       elementType = new PointerType 
(elementType);
+                               else if (t is ArrayType) // deal with complex 
arrays
+                                       elementType = new ArrayType 
(elementType);
+                               else if (t is ReferenceType)
+                                       elementType = new ReferenceType 
(elementType);
+                               else if (t is GenericInstanceType) {
+                                       GenericInstanceType git = t as 
GenericInstanceType;
+                                       GenericInstanceType genElemType = new 
GenericInstanceType (elementType);
+                                       foreach (TypeReference arg in 
git.GenericArguments)
+                                               
genElemType.GenericArguments.Add (ImportTypeReference (arg, context));
+
+                                       elementType = genElemType;
+                               } else
+                                       throw new ReflectionException ("Unknown 
element type: {0}", t.GetType ().Name);
+                       }
+
+                       return elementType;
                }
+
+               static GenericParameter GetGenericParameter (GenericParameter 
gp, ImportContext context)
+               {
+                       GenericParameter p;
+                       if (gp.Owner is TypeReference)
+                               p = 
context.GenericContext.Type.GenericParameters [gp.Position];
+                       else if (gp.Owner is MethodReference)
+                               p = 
context.GenericContext.Method.GenericParameters [gp.Position];
+                       else
+                               throw new NotSupportedException ();
+
+                       return p;
+               }
+
+               public virtual TypeReference ImportTypeReference (TypeReference 
t, ImportContext context)
+               {
+                       if (t.Module == m_module)
+                               return t;
+
+                       if (t is TypeSpecification)
+                               return GetTypeSpec (t, context);
+
+                       if (t is GenericParameter)
+                               return GetGenericParameter (t as 
GenericParameter, context);
+
+                       TypeReference type = m_module.TypeReferences 
[t.FullName];
+                       if (type != null)
+                               return type;
+
+                       AssemblyNameReference asm;
+                       if (t.Scope is AssemblyNameReference)
+                               asm = ImportAssembly ((AssemblyNameReference) 
t.Scope);
+                       else if (t.Scope is ModuleDefinition)
+                               asm = ImportAssembly (((ModuleDefinition) 
t.Scope).Assembly.Name);
+                       else
+                               throw new NotImplementedException ();
+
+                       type = new TypeReference (t.Name, t.Namespace, asm, 
t.IsValueType);
+
+                       context.GenericContext.Type = type;
+
+                       foreach (GenericParameter gp in t.GenericParameters)
+                               type.GenericParameters.Add 
(GenericParameter.Clone (gp, context));
+
+                       m_module.TypeReferences.Add (type);
+                       return type;
+               }
+
+               public virtual FieldReference ImportFieldReference 
(FieldReference fr, ImportContext context)
+               {
+                       if (fr.DeclaringType.Module == m_module)
+                               return fr;
+
+                       FieldReference field = (FieldReference) 
GetMemberReference (fr);
+                       if (field != null)
+                               return field;
+
+                       field = new FieldReference (
+                               fr.Name,
+                               ImportTypeReference (fr.DeclaringType, context),
+                               ImportTypeReference (fr.FieldType, context));
+
+                       m_module.MemberReferences.Add (field);
+                       return field;
+               }
+
+               MethodReference GetMethodSpec (MethodReference meth, 
ImportContext context)
+               {
+                       if (!(meth is GenericInstanceMethod))
+                               return null;
+
+                       GenericInstanceMethod gim = meth as 
GenericInstanceMethod;
+                       GenericInstanceMethod ngim = new GenericInstanceMethod (
+                               ImportMethodReference (gim.ElementMethod, 
context));
+
+                       foreach (TypeReference arg in gim.GenericArguments)
+                               ngim.GenericArguments.Add (ImportTypeReference 
(arg, context));
+
+                       return ngim;
+               }
+
+               public virtual MethodReference ImportMethodReference 
(MethodReference mr, ImportContext context)
+               {
+                       if (mr.DeclaringType.Module == m_module)
+                               return mr;
+
+                       if (mr is MethodSpecification)
+                               return GetMethodSpec (mr, context);
+
+                       MethodReference meth = (MethodReference) 
GetMemberReference (mr);
+                       if (meth != null)
+                               return meth;
+
+                       meth = new MethodReference (
+                               mr.Name,
+                               mr.HasThis,
+                               mr.ExplicitThis,
+                               mr.CallingConvention);
+                       meth.DeclaringType = ImportTypeReference 
(mr.DeclaringType, context);
+
+                       TypeReference contextType = meth.DeclaringType;
+                       while (contextType is TypeSpecification)
+                               contextType = (contextType as 
TypeSpecification).ElementType;
+
+                       context.GenericContext.Method = meth;
+                       context.GenericContext.Type = contextType;
+
+                       foreach (GenericParameter gp in mr.GenericParameters)
+                               meth.GenericParameters.Add 
(GenericParameter.Clone (gp, context));
+
+                       meth.ReturnType.ReturnType = ImportTypeReference 
(mr.ReturnType.ReturnType, context);
+
+                       foreach (ParameterDefinition param in mr.Parameters)
+                               meth.Parameters.Add (new ParameterDefinition (
+                                       ImportTypeReference 
(param.ParameterType, context)));
+
+                       m_module.MemberReferences.Add (meth);
+                       return meth;
+               }
+
+               MemberReference GetMemberReference (MemberReference member)
+               {
+                       foreach (MemberReference reference in 
m_module.MemberReferences)
+                               if (reference.ToString () == member.ToString ())
+                                       return reference;
+
+                       return null;
+               }
        }
 }

Modified: trunk/cecil/lib/Mono.Cecil/ModuleDefinition.cs
===================================================================
--- trunk/cecil/lib/Mono.Cecil/ModuleDefinition.cs      2007-06-20 20:04:00 UTC 
(rev 80358)
+++ trunk/cecil/lib/Mono.Cecil/ModuleDefinition.cs      2007-06-20 20:11:22 UTC 
(rev 80359)
@@ -283,7 +283,7 @@
                        if (type == null)
                                throw new ArgumentNullException ("type");
 
-                       return m_controller.Helper.ImportTypeReference (type, 
GetContext ());
+                       return m_controller.Importer.ImportTypeReference (type, 
GetContext ());
                }
 
                public TypeReference Import (TypeReference type, TypeDefinition 
context)
@@ -292,7 +292,7 @@
                                throw new ArgumentNullException ("type");
                        CheckContext (context);
 
-                       return m_controller.Helper.ImportTypeReference (type, 
GetContext (context));
+                       return m_controller.Importer.ImportTypeReference (type, 
GetContext (context));
                }
 
                public MethodReference Import (MethodReference meth)
@@ -300,7 +300,7 @@
                        if (meth == null)
                                throw new ArgumentNullException ("meth");
 
-                       return m_controller.Helper.ImportMethodReference (meth, 
GetContext ());
+                       return m_controller.Importer.ImportMethodReference 
(meth, GetContext ());
                }
 
                public MethodReference Import (MethodReference meth, 
TypeDefinition context)
@@ -309,7 +309,7 @@
                                throw new ArgumentNullException ("meth");
                        CheckContext (context);
 
-                       return m_controller.Helper.ImportMethodReference (meth, 
GetContext (context));
+                       return m_controller.Importer.ImportMethodReference 
(meth, GetContext (context));
                }
 
                public FieldReference Import (FieldReference field)
@@ -317,7 +317,7 @@
                        if (field == null)
                                throw new ArgumentNullException ("field");
 
-                       return m_controller.Helper.ImportFieldReference (field, 
GetContext ());
+                       return m_controller.Importer.ImportFieldReference 
(field, GetContext ());
                }
 
                public FieldReference Import (FieldReference field, 
TypeDefinition context)
@@ -326,7 +326,7 @@
                                throw new ArgumentNullException ("field");
                        CheckContext (context);
 
-                       return m_controller.Helper.ImportFieldReference (field, 
GetContext (context));
+                       return m_controller.Importer.ImportFieldReference 
(field, GetContext (context));
                }
 
                static FieldDefinition ImportFieldDefinition (FieldDefinition 
field, ImportContext context)

Modified: trunk/cecil/lib/Mono.Cecil/ReflectionHelper.cs
===================================================================
--- trunk/cecil/lib/Mono.Cecil/ReflectionHelper.cs      2007-06-20 20:04:00 UTC 
(rev 80358)
+++ trunk/cecil/lib/Mono.Cecil/ReflectionHelper.cs      2007-06-20 20:11:22 UTC 
(rev 80359)
@@ -39,38 +39,14 @@
 
                ModuleDefinition m_module;
 
-               IDictionary m_asmCache;
-               IDictionary m_memberRefCache;
-               bool m_cacheLoaded;
-
                public ReflectionHelper (ModuleDefinition module)
                {
                        m_module = module;
-                       m_asmCache = new Hashtable ();
-                       m_memberRefCache = new Hashtable ();
-                       m_cacheLoaded = false;
                }
 
-               void ImportCache ()
-               {
-                       if (m_cacheLoaded)
-                               return;
-
-                       m_module.FullLoad ();
-
-                       foreach (AssemblyNameReference ar in 
m_module.AssemblyReferences)
-                               m_asmCache [ar.FullName] = ar;
-                       foreach (MemberReference member in 
m_module.MemberReferences)
-                               m_memberRefCache [member.ToString ()] = member;
-
-                       m_cacheLoaded = true;
-               }
-
                public AssemblyNameReference ImportAssembly (SR.Assembly asm)
                {
-                       ImportCache ();
-
-                       AssemblyNameReference asmRef = (AssemblyNameReference) 
m_asmCache [asm.FullName];
+                       AssemblyNameReference asmRef = GetAssemblyNameReference 
(asm.GetName ());
                        if (asmRef != null)
                                return asmRef;
 
@@ -78,13 +54,21 @@
                        asmRef = new AssemblyNameReference (
                                asmName.Name, asmName.CultureInfo.Name, 
asmName.Version);
                        asmRef.PublicKeyToken = asmName.GetPublicKeyToken ();
-                       asmRef.HashAlgorithm = 
(Mono.Cecil.AssemblyHashAlgorithm) asmName.HashAlgorithm;
+                       asmRef.HashAlgorithm = (AssemblyHashAlgorithm) 
asmName.HashAlgorithm;
                        asmRef.Culture = asmName.CultureInfo.ToString ();
                        m_module.AssemblyReferences.Add (asmRef);
-                       m_asmCache [asm.FullName] = asmRef;
                        return asmRef;
                }
 
+               AssemblyNameReference GetAssemblyNameReference (SR.AssemblyName 
name)
+               {
+                       foreach (AssemblyNameReference reference in 
m_module.AssemblyReferences)
+                               if (reference.FullName == name.FullName)
+                                       return reference;
+
+                       return null;
+               }
+
                public static string GetTypeSignature (Type t)
                {
                        if (t.HasElementType) {
@@ -199,7 +183,7 @@
 
                        TypeReference elementType = ImportSystemType (t, 
context);
                        while (s.Count > 0) {
-                               t = s.Pop () as Type;
+                               t = (Type) s.Pop ();
                                if (t.IsPointer)
                                        elementType = new PointerType 
(elementType);
                                else if (t.IsArray) // deal with complex arrays
@@ -223,8 +207,6 @@
                        if (IsGenericParameter (t))
                                return GetGenericParameter (t, context);
 
-                       ImportCache ();
-
                        TypeReference type = m_module.TypeReferences 
[GetTypeSignature (t)];
                        if (type != null) {
                                if (t.IsValueType && !type.IsValueType)
@@ -317,8 +299,6 @@
                        if (IsGenericMethod (mb) && !IsGenericMethodDefinition 
(mb))
                                return ImportGenericInstanceMethod 
((SR.MethodInfo) mb, context);
 
-                       ImportCache ();
-
                        Type originalDecType = mb.DeclaringType;
                        Type declaringTypeDef = originalDecType;
                        while (IsGenericTypeSpec (declaringTypeDef))
@@ -337,7 +317,7 @@
                        }
 
                        string sig = GetMethodBaseSignature (mb, 
originalDecType, retType);
-                       MethodReference meth = m_memberRefCache [sig] as 
MethodReference;
+                       MethodReference meth = (MethodReference) 
GetMemberReference (sig);
                        if (meth != null)
                                return meth;
 
@@ -363,7 +343,6 @@
                                        ImportSystemType (parameters 
[i].ParameterType, context)));
 
                        m_module.MemberReferences.Add (meth);
-                       m_memberRefCache [sig] = meth;
                        return meth;
                }
 
@@ -390,10 +369,8 @@
 
                public FieldReference ImportFieldInfo (SR.FieldInfo fi, 
ImportContext context)
                {
-                       ImportCache ();
-
                        string sig = GetFieldSignature (fi);
-                       FieldReference f = (FieldReference) m_memberRefCache 
[sig];
+                       FieldReference f = (FieldReference) GetMemberReference 
(sig);
                        if (f != null)
                                return f;
 
@@ -403,183 +380,16 @@
                                ImportSystemType (fi.FieldType, context));
 
                        m_module.MemberReferences.Add (f);
-                       m_memberRefCache [sig] = f;
                        return f;
                }
 
-               public AssemblyNameReference ImportAssembly 
(AssemblyNameReference asm)
+               MemberReference GetMemberReference (string signature)
                {
-                       ImportCache ();
+                       foreach (MemberReference reference in 
m_module.MemberReferences)
+                               if (reference.ToString () == signature)
+                                       return reference;
 
-                       AssemblyNameReference asmRef = (AssemblyNameReference) 
m_asmCache [asm.FullName];
-                       if (asmRef != null)
-                               return asmRef;
-
-                       asmRef = new AssemblyNameReference (
-                               asm.Name, asm.Culture, asm.Version);
-                       asmRef.PublicKeyToken = asm.PublicKeyToken;
-                       asmRef.HashAlgorithm = asm.HashAlgorithm;
-                       m_module.AssemblyReferences.Add (asmRef);
-                       m_asmCache [asm.FullName] = asmRef;
-                       return asmRef;
+                       return null;
                }
-
-               TypeReference GetTypeSpec (TypeReference t, ImportContext 
context)
-               {
-                       Stack s = new Stack ();
-                       while (t is TypeSpecification) {
-                               s.Push (t);
-                               t = (t as TypeSpecification).ElementType;
-                       }
-
-                       TypeReference elementType = ImportTypeReference (t, 
context);
-                       while (s.Count > 0) {
-                               t = s.Pop () as TypeReference;
-                               if (t is PointerType)
-                                       elementType = new PointerType 
(elementType);
-                               else if (t is ArrayType) // deal with complex 
arrays
-                                       elementType = new ArrayType 
(elementType);
-                               else if (t is ReferenceType)
-                                       elementType = new ReferenceType 
(elementType);
-                               else if (t is GenericInstanceType) {
-                                       GenericInstanceType git = t as 
GenericInstanceType;
-                                       GenericInstanceType genElemType = new 
GenericInstanceType (elementType);
-                                       foreach (TypeReference arg in 
git.GenericArguments)
-                                               
genElemType.GenericArguments.Add (ImportTypeReference (arg, context));
-
-                                       elementType = genElemType;
-                               } else
-                                       throw new ReflectionException ("Unknown 
element type: {0}", t.GetType ().Name);
-                       }
-
-                       return elementType;
-               }
-
-               static GenericParameter GetGenericParameter (GenericParameter 
gp, ImportContext context)
-               {
-                       GenericParameter p;
-                       if (gp.Owner is TypeReference)
-                               p = 
context.GenericContext.Type.GenericParameters [gp.Position];
-                       else if (gp.Owner is MethodReference)
-                               p = 
context.GenericContext.Method.GenericParameters [gp.Position];
-                       else
-                               throw new NotSupportedException ();
-
-                       return p;
-               }
-
-               public TypeReference ImportTypeReference (TypeReference t, 
ImportContext context)
-               {
-                       if (t.Module == m_module)
-                               return t;
-
-                       ImportCache ();
-
-                       if (t is TypeSpecification)
-                               return GetTypeSpec (t, context);
-
-                       if (t is GenericParameter)
-                               return GetGenericParameter (t as 
GenericParameter, context);
-
-                       TypeReference type = m_module.TypeReferences 
[t.FullName];
-                       if (type != null)
-                               return type;
-
-                       AssemblyNameReference asm;
-                       if (t.Scope is AssemblyNameReference)
-                               asm = ImportAssembly ((AssemblyNameReference) 
t.Scope);
-                       else if (t.Scope is ModuleDefinition)
-                               asm = ImportAssembly (((ModuleDefinition) 
t.Scope).Assembly.Name);
-                       else
-                               throw new NotImplementedException ();
-
-                       type = new TypeReference (t.Name, t.Namespace, asm, 
t.IsValueType);
-
-                       context.GenericContext.Type = type;
-
-                       foreach (GenericParameter gp in t.GenericParameters)
-                               type.GenericParameters.Add 
(GenericParameter.Clone (gp, context));
-
-                       m_module.TypeReferences.Add (type);
-                       return type;
-               }
-
-               MethodReference GetMethodSpec (MethodReference meth, 
ImportContext context)
-               {
-                       if (!(meth is GenericInstanceMethod))
-                               return null;
-
-                       GenericInstanceMethod gim = meth as 
GenericInstanceMethod;
-                       GenericInstanceMethod ngim = new GenericInstanceMethod (
-                               ImportMethodReference (gim.ElementMethod, 
context));
-
-                       foreach (TypeReference arg in gim.GenericArguments)
-                               ngim.GenericArguments.Add (ImportTypeReference 
(arg, context));
-
-                       return ngim;
-               }
-
-               public MethodReference ImportMethodReference (MethodReference 
mr, ImportContext context)
-               {
-                       if (mr.DeclaringType.Module == m_module)
-                               return mr;
-
-                       ImportCache ();
-
-                       if (mr is MethodSpecification)
-                               return GetMethodSpec (mr, context);
-
-                       MethodReference meth = m_memberRefCache [mr.ToString 
()] as MethodReference;
-                       if (meth != null)
-                               return meth;
-
-                       meth = new MethodReference (
-                               mr.Name,
-                               mr.HasThis,
-                               mr.ExplicitThis,
-                               mr.CallingConvention);
-                       meth.DeclaringType = ImportTypeReference 
(mr.DeclaringType, context);
-
-                       TypeReference contextType = meth.DeclaringType;
-                       while (contextType is TypeSpecification)
-                               contextType = (contextType as 
TypeSpecification).ElementType;
-
-                       context.GenericContext.Method = meth;
-                       context.GenericContext.Type = contextType;
-
-                       foreach (GenericParameter gp in mr.GenericParameters)
-                               meth.GenericParameters.Add 
(GenericParameter.Clone (gp, context));
-
-                       meth.ReturnType.ReturnType = ImportTypeReference 
(mr.ReturnType.ReturnType, context);
-
-                       foreach (ParameterDefinition param in mr.Parameters)
-                               meth.Parameters.Add (new ParameterDefinition (
-                                       ImportTypeReference 
(param.ParameterType, context)));
-
-                       m_module.MemberReferences.Add (meth);
-                       m_memberRefCache [mr.ToString ()] = meth;
-                       return meth;
-               }
-
-               public FieldReference ImportFieldReference (FieldReference 
field, ImportContext context)
-               {
-                       if (field.DeclaringType.Module == m_module)
-                               return field;
-
-                       ImportCache ();
-
-                       FieldReference f = (FieldReference) m_memberRefCache 
[field.ToString ()];
-                       if (f != null)
-                               return f;
-
-                       f = new FieldReference (
-                               field.Name,
-                               ImportTypeReference (field.DeclaringType, 
context),
-                               ImportTypeReference (field.FieldType, context));
-
-                       m_module.MemberReferences.Add (f);
-                       m_memberRefCache [field.ToString ()] = f;
-                       return f;
-               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to