2007-09-09 Jb Evain <jbevain@novell.com>
authorJb Evain <jbevain@gmail.com>
Sun, 9 Sep 2007 13:08:38 +0000 (13:08 -0000)
committerJb Evain <jbevain@gmail.com>
Sun, 9 Sep 2007 13:08:38 +0000 (13:08 -0000)
* Mono.Cecil/GenericContext.cs
  Mono.Cecil/ReflectionReader.cs
  Mono.Cecil/DefaultImporter.cs:
Better handling of generic instances of references, where
the number of generic parameters is not known.

svn path=/trunk/mcs/; revision=85531

mcs/class/Mono.Cecil/ChangeLog
mcs/class/Mono.Cecil/Mono.Cecil/DefaultImporter.cs
mcs/class/Mono.Cecil/Mono.Cecil/GenericContext.cs
mcs/class/Mono.Cecil/Mono.Cecil/ReflectionReader.cs

index 9baded38e8d57c148ac5f80a349c918e23617d1e..258882dce18f553282a694cb072dae8a705b867d 100644 (file)
@@ -1,3 +1,11 @@
+2007-09-09  Jb Evain  <jbevain@novell.com>
+
+       * Mono.Cecil/GenericContext.cs
+         Mono.Cecil/ReflectionReader.cs
+         Mono.Cecil/DefaultImporter.cs:
+               Better handling of generic instances of references, where
+               the number of generic parameters is not known.
+
 2007-09-06  Jb Evain  <jbevain@novell.com>
 
        * Mono.Cecil/MethodDefinition: add Is** methods for MethodImplAttributes.
index 5c9df115b905e05ef074d836ca7def74bcf08bca..f8d3011ff661fa6f96db82eb522e1ae64e15e6f9 100644 (file)
@@ -87,6 +87,8 @@ namespace Mono.Cecil {
                                else if (t is GenericInstanceType) {
                                        GenericInstanceType git = t as GenericInstanceType;
                                        GenericInstanceType genElemType = new GenericInstanceType (elementType);
+
+                                       context.GenericContext.CheckProvider (genElemType.GetOriginalType (), git.GenericArguments.Count);
                                        foreach (TypeReference arg in git.GenericArguments)
                                                genElemType.GenericArguments.Add (ImportTypeReference (arg, context));
 
@@ -172,6 +174,7 @@ namespace Mono.Cecil {
                        GenericInstanceMethod ngim = new GenericInstanceMethod (
                                ImportMethodReference (gim.ElementMethod, context));
 
+                       context.GenericContext.CheckProvider (ngim.GetOriginalMethod (), gim.GenericArguments.Count);
                        foreach (TypeReference arg in gim.GenericArguments)
                                ngim.GenericArguments.Add (ImportTypeReference (arg, context));
 
index 5e11557829b36035ec608d3dcbcf1c2ce1c7c8e1..b3c198d5ed31f3c9f9fd59a88b5b81e8ff9f66be 100644 (file)
@@ -32,7 +32,6 @@ namespace Mono.Cecil {
 
                TypeReference m_type;
                MethodReference m_method;
-               bool m_allowCreation;
 
                public TypeReference Type {
                        get { return m_type; }
@@ -45,8 +44,7 @@ namespace Mono.Cecil {
                }
 
                public bool AllowCreation {
-                       get { return m_allowCreation; }
-                       set { m_allowCreation = value; }
+                       get { return m_type != null && m_type.GetType () == typeof (TypeReference); }
                }
 
                public bool Null {
@@ -74,6 +72,15 @@ namespace Mono.Cecil {
                        }
                }
 
+               internal void CheckProvider (IGenericParameterProvider provider, int count)
+               {
+                       if (!AllowCreation)
+                               return;
+
+                       for (int i = provider.GenericParameters.Count; i < count; i++)
+                               provider.GenericParameters.Add (new GenericParameter (i, provider));
+               }
+
                public GenericContext Clone ()
                {
                        GenericContext ctx = new GenericContext ();
index 483ca68db303f8490158bb6a379b967b9e2889b3..045086dab69b3eb89772b925fd6c60e4168d0d20 100644 (file)
@@ -193,7 +193,6 @@ namespace Mono.Cecil {
                                                ct = (ct as GenericInstanceType).ElementType;
 
                                        nc.Type = ct;
-                                       nc.AllowCreation = ct.GetType () == typeof (TypeReference);
                                }
 
                                if (sig is FieldSig) {
@@ -316,6 +315,7 @@ namespace Mono.Cecil {
                                throw new ReflectionException ("Unknown method type for method spec");
 
                        gim = new GenericInstanceMethod (meth);
+                       context.CheckProvider (meth, sig.Signature.Arity);
                        foreach (GenericArg arg in sig.Signature.Types)
                                gim.GenericArguments.Add (GetGenericArg (arg, context));
 
@@ -1045,8 +1045,7 @@ namespace Mono.Cecil {
                                return fnptr;
                        case ElementType.Var:
                                VAR var = t as VAR;
-                               if (context.AllowCreation)
-                                       CheckGenericParameters (context, var);
+                               context.CheckProvider (context.Type, var.Index + 1);
 
                                if (context.Type is GenericInstanceType)
                                        return (context.Type as GenericInstanceType).GenericArguments [var.Index];
@@ -1054,6 +1053,8 @@ namespace Mono.Cecil {
                                        return context.Type.GenericParameters [var.Index];
                        case ElementType.MVar:
                                MVAR mvar = t as MVAR;
+                               context.CheckProvider (context.Method, mvar.Index + 1);
+
                                if (context.Method is GenericInstanceMethod)
                                        return (context.Method as GenericInstanceMethod).GenericArguments [mvar.Index];
                                else
@@ -1062,6 +1063,7 @@ namespace Mono.Cecil {
                                GENERICINST ginst = t as GENERICINST;
                                GenericInstanceType instance = new GenericInstanceType (GetTypeDefOrRef (ginst.Type, context));
                                instance.IsValueType = ginst.ValueType;
+                               context.CheckProvider (instance.GetOriginalType (), ginst.Signature.Arity);
 
                                for (int i = 0; i < ginst.Signature.Arity; i++)
                                        instance.GenericArguments.Add (GetGenericArg (
@@ -1082,12 +1084,6 @@ namespace Mono.Cecil {
                        return type;
                }
 
-               static void CheckGenericParameters (GenericContext context, VAR v)
-               {
-                       for (int i = context.Type.GenericParameters.Count; i <= v.Index; i++)
-                               context.Type.GenericParameters.Add (new GenericParameter (i, context.Type));
-               }
-
                protected object GetConstant (uint pos, ElementType elemType)
                {
                        byte [] constant = m_root.Streams.BlobHeap.Read (pos);